GDB (xrefs)
Loading...
Searching...
No Matches
compile.c
Go to the documentation of this file.
1/* General Compile and inject code
2
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "ui.h"
22#include "ui-out.h"
23#include "command.h"
24#include "cli/cli-script.h"
25#include "cli/cli-utils.h"
26#include "cli/cli-option.h"
27#include "completer.h"
28#include "gdbcmd.h"
29#include "compile.h"
30#include "compile-internal.h"
31#include "compile-object-load.h"
32#include "compile-object-run.h"
33#include "language.h"
34#include "frame.h"
35#include "source.h"
36#include "block.h"
37#include "arch-utils.h"
38#include "gdbsupport/filestuff.h"
39#include "target.h"
40#include "osabi.h"
41#include "gdbsupport/gdb_wait.h"
42#include "valprint.h"
43#include "gdbsupport/gdb_optional.h"
44#include "gdbsupport/gdb_unlinker.h"
45#include "gdbsupport/pathstuff.h"
46#include "gdbsupport/scoped_ignore_signal.h"
47#include "gdbsupport/buildargv.h"
48
49
50
51/* Initial filename for temporary files. */
52
53#define TMP_PREFIX "/tmp/gdbobj-"
54
55/* Hold "compile" commands. */
56
58
59/* Debug flag for "compile" commands. */
60
62
63/* Object of this type are stored in the compiler's symbol_err_map. */
64
66{
67 /* The symbol. */
68
69 const struct symbol *sym;
70
71 /* The error message to emit. This is malloc'd and owned by the
72 hash table. */
73
74 char *message;
75};
76
77/* An object that maps a gdb type to a gcc type. */
78
80{
81 /* The gdb type. */
82
83 struct type *type;
84
85 /* The corresponding gcc type handle. */
86
88};
89
90/* Hash a type_map_instance. */
91
92static hashval_t
94{
95 const struct type_map_instance *inst = (const struct type_map_instance *) p;
96
97 return htab_hash_pointer (inst->type);
98}
99
100/* Check two type_map_instance objects for equality. */
101
102static int
103eq_type_map_instance (const void *a, const void *b)
104{
105 const struct type_map_instance *insta = (const struct type_map_instance *) a;
106 const struct type_map_instance *instb = (const struct type_map_instance *) b;
107
108 return insta->type == instb->type;
109}
110
111/* Hash function for struct symbol_error. */
112
113static hashval_t
114hash_symbol_error (const void *a)
115{
116 const struct symbol_error *se = (const struct symbol_error *) a;
117
118 return htab_hash_pointer (se->sym);
119}
120
121/* Equality function for struct symbol_error. */
122
123static int
124eq_symbol_error (const void *a, const void *b)
125{
126 const struct symbol_error *sea = (const struct symbol_error *) a;
127 const struct symbol_error *seb = (const struct symbol_error *) b;
128
129 return sea->sym == seb->sym;
130}
131
132/* Deletion function for struct symbol_error. */
133
134static void
136{
137 struct symbol_error *se = (struct symbol_error *) a;
138
139 xfree (se->message);
140 xfree (se);
141}
142
143/* Constructor for compile_instance. */
144
145compile_instance::compile_instance (struct gcc_base_context *gcc_fe,
146 const char *options)
147 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
148 m_type_map (htab_create_alloc (10, hash_type_map_instance,
150 xfree, xcalloc, xfree)),
151 m_symbol_err_map (htab_create_alloc (10, hash_symbol_error,
153 xcalloc, xfree))
154{
155}
156
157/* See compile-internal.h. */
158
159bool
160compile_instance::get_cached_type (struct type *type, gcc_type *ret) const
161{
162 struct type_map_instance inst, *found;
163
164 inst.type = type;
165 found = (struct type_map_instance *) htab_find (m_type_map.get (), &inst);
166 if (found != NULL)
167 {
168 *ret = found->gcc_type_handle;
169 return true;
170 }
171
172 return false;
173}
174
175/* See compile-internal.h. */
176
177void
178compile_instance::insert_type (struct type *type, gcc_type gcc_type)
179{
180 struct type_map_instance inst, *add;
181 void **slot;
182
183 inst.type = type;
184 inst.gcc_type_handle = gcc_type;
185 slot = htab_find_slot (m_type_map.get (), &inst, INSERT);
186
187 add = (struct type_map_instance *) *slot;
188 /* The type might have already been inserted in order to handle
189 recursive types. */
190 if (add != NULL && add->gcc_type_handle != gcc_type)
191 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
192
193 if (add == NULL)
194 {
195 add = XNEW (struct type_map_instance);
196 *add = inst;
197 *slot = add;
198 }
199}
200
201/* See compile-internal.h. */
202
203void
205 const char *text)
206{
207 struct symbol_error e;
208 void **slot;
209
210 e.sym = sym;
211 slot = htab_find_slot (m_symbol_err_map.get (), &e, INSERT);
212 if (*slot == NULL)
213 {
214 struct symbol_error *ep = XNEW (struct symbol_error);
215
216 ep->sym = sym;
217 ep->message = xstrdup (text);
218 *slot = ep;
219 }
220}
221
222/* See compile-internal.h. */
223
224void
226{
227 struct symbol_error search;
228 struct symbol_error *err;
229
230 if (m_symbol_err_map == NULL)
231 return;
232
233 search.sym = sym;
234 err = (struct symbol_error *) htab_find (m_symbol_err_map.get (), &search);
235 if (err == NULL || err->message == NULL)
236 return;
237
238 gdb::unique_xmalloc_ptr<char> message (err->message);
239 err->message = NULL;
240 error (_("%s"), message.get ());
241}
242
243/* Implement "show debug compile". */
244
245static void
246show_compile_debug (struct ui_file *file, int from_tty,
247 struct cmd_list_element *c, const char *value)
248{
249 gdb_printf (file, _("Compile debugging is %s.\n"), value);
250}
251
252
253
254/* Options for the compile command. */
255
257{
258 /* For -raw. */
259 bool raw = false;
260};
261
266
268 "raw",
269 [] (compile_options *opts) { return &opts->raw; },
270 N_("Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
271 },
272
273};
274
275/* Create an option_def_group for the "compile" command's options,
276 with OPTS as context. */
277
282}
283
284/* Handle the input from the 'compile file' command. The "compile
285 file" command is used to evaluate an expression contained in a file
286 that may contain calls to the GCC compiler. */
287
288static void
289compile_file_command (const char *args, int from_tty)
290{
291 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
292
293 /* Check if a -raw option is provided. */
294
295 compile_options options;
296
301 group);
302
303 enum compile_i_scope_types scope
305
306 args = skip_spaces (args);
307
308 /* After processing options, check whether we have a filename. */
309 if (args == nullptr || args[0] == '\0')
310 error (_("You must provide a filename for this command."));
311
312 args = skip_spaces (args);
313 std::string abspath = gdb_abspath (args);
314 std::string buffer = string_printf ("#include \"%s\"\n", abspath.c_str ());
315 eval_compile_command (NULL, buffer.c_str (), scope, NULL);
316}
317
318/* Completer for the "compile file" command. */
319
320static void
322 completion_tracker &tracker,
323 const char *text, const char *word)
324{
328 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
329 return;
330
331 word = advance_to_filename_complete_word_point (tracker, text);
332 filename_completer (ignore, tracker, text, word);
333}
334
335/* Handle the input from the 'compile code' command. The
336 "compile code" command is used to evaluate an expression that may
337 contain calls to the GCC compiler. The language expected in this
338 compile command is the language currently set in GDB. */
339
340static void
341compile_code_command (const char *args, int from_tty)
342{
343 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
344
345 compile_options options;
346
351
352 enum compile_i_scope_types scope
354
355 if (args && *args)
356 eval_compile_command (NULL, args, scope, NULL);
357 else
358 {
360
361 l->control_u.compile.scope = scope;
363 }
364}
365
366/* Completer for the "compile code" command. */
367
368static void
370 completion_tracker &tracker,
371 const char *text, const char *word)
372{
376 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
377 return;
378
379 word = advance_to_expression_complete_word_point (tracker, text);
380 symbol_completer (ignore, tracker, text, word);
381}
382
383/* Callback for compile_print_command. */
384
385void
386compile_print_value (struct value *val, void *data_voidp)
387{
388 const value_print_options *print_opts = (value_print_options *) data_voidp;
389
390 print_value (val, *print_opts);
391}
392
393/* Handle the input from the 'compile print' command. The "compile
394 print" command is used to evaluate and print an expression that may
395 contain calls to the GCC compiler. The language expected in this
396 compile command is the language currently set in GDB. */
397
398static void
399compile_print_command (const char *arg, int from_tty)
400{
402 value_print_options print_opts;
403
404 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
405
406 get_user_print_options (&print_opts);
407 /* Override global settings with explicit options, if any. */
408 auto group = make_value_print_options_def_group (&print_opts);
411
412 print_command_parse_format (&arg, "compile print", &print_opts);
413
414 /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup
415 will not touch the stale pointer if compile_object_run has
416 already quit. */
417
418 if (arg && *arg)
419 eval_compile_command (NULL, arg, scope, &print_opts);
420 else
421 {
423
424 l->control_u.compile.scope = scope;
425 l->control_u.compile.scope_data = &print_opts;
427 }
428}
429
430/* A cleanup function to remove a directory and all its contents. */
431
432static void
433do_rmdir (void *arg)
434{
435 const char *dir = (const char *) arg;
436 char *zap;
437 int wstat;
438
439 gdb_assert (startswith (dir, TMP_PREFIX));
440 zap = concat ("rm -rf ", dir, (char *) NULL);
441 wstat = system (zap);
442 if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
443 warning (_("Could not remove temporary directory %s"), dir);
444 XDELETEVEC (zap);
445}
446
447/* Return the name of the temporary directory to use for .o files, and
448 arrange for the directory to be removed at shutdown. */
449
450static const char *
452{
453 static char *tempdir_name;
454
455#define TEMPLATE TMP_PREFIX "XXXXXX"
456 char tname[sizeof (TEMPLATE)];
457
458 if (tempdir_name != NULL)
459 return tempdir_name;
460
461 strcpy (tname, TEMPLATE);
462#undef TEMPLATE
463 tempdir_name = mkdtemp (tname);
464 if (tempdir_name == NULL)
465 perror_with_name (_("Could not make temporary directory"));
466
467 tempdir_name = xstrdup (tempdir_name);
468 make_final_cleanup (do_rmdir, tempdir_name);
469 return tempdir_name;
470}
471
472/* Compute the names of source and object files to use. */
473
476{
477 static int seq;
478 const char *dir = get_compile_file_tempdir ();
479
480 ++seq;
481
482 return compile_file_names (string_printf ("%s%sout%d.c",
483 dir, SLASH_STRING, seq),
484 string_printf ("%s%sout%d.o",
485 dir, SLASH_STRING, seq));
486}
487
488/* Get the block and PC at which to evaluate an expression. */
489
490static const struct block *
491get_expr_block_and_pc (CORE_ADDR *pc)
492{
493 const struct block *block = get_selected_block (pc);
494
495 if (block == NULL)
496 {
498
499 if (cursal.symtab)
500 block = cursal.symtab->compunit ()->blockvector ()->static_block ();
501
502 if (block != NULL)
503 *pc = block->entry_pc ();
504 }
505 else
506 *pc = block->entry_pc ();
507
508 return block;
509}
510
511/* String for 'set compile-args' and 'show compile-args'. */
512static std::string compile_args =
513 /* Override flags possibly coming from DW_AT_producer. */
514 "-O0 -gdwarf-4"
515 /* We use -fPIE Otherwise GDB would need to reserve space large enough for
516 any object file in the inferior in advance to get the final address when
517 to link the object file to and additionally the default system linker
518 script would need to be modified so that one can specify there the
519 absolute target address.
520 -fPIC is not used at is would require from GDB to generate .got. */
521 " -fPIE"
522 /* We want warnings, except for some commonly happening for GDB commands. */
523 " -Wall "
524 " -Wno-unused-but-set-variable"
525 " -Wno-unused-variable"
526 /* Override CU's possible -fstack-protector-strong. */
527 " -fno-stack-protector";
528
529/* Parsed form of COMPILE_ARGS. */
530static gdb_argv compile_args_argv;
531
532/* Implement 'set compile-args'. */
533
534static void
535set_compile_args (const char *args, int from_tty, struct cmd_list_element *c)
536{
537 compile_args_argv = gdb_argv (compile_args.c_str ());
538}
539
540/* Implement 'show compile-args'. */
541
542static void
543show_compile_args (struct ui_file *file, int from_tty,
544 struct cmd_list_element *c, const char *value)
545{
546 gdb_printf (file, _("Compile command command-line arguments "
547 "are \"%s\".\n"),
548 value);
549}
550
551/* String for 'set compile-gcc' and 'show compile-gcc'. */
552static std::string compile_gcc;
553
554/* Implement 'show compile-gcc'. */
555
556static void
557show_compile_gcc (struct ui_file *file, int from_tty,
558 struct cmd_list_element *c, const char *value)
559{
560 gdb_printf (file, _("Compile command GCC driver filename is \"%s\".\n"),
561 value);
562}
563
564/* Return DW_AT_producer parsed for get_selected_frame () (if any).
565 Return NULL otherwise.
566
567 GCC already filters its command-line arguments only for the suitable ones to
568 put into DW_AT_producer - see GCC function gen_producer_string. */
569
570static const char *
572{
573 CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
575 const char *cs;
576
577 if (symtab == NULL || symtab->producer () == NULL
578 || !startswith (symtab->producer (), "GNU "))
579 return NULL;
580
581 cs = symtab->producer ();
582 while (*cs != 0 && *cs != '-')
583 cs = skip_spaces (skip_to_space (cs));
584 if (*cs != '-')
585 return NULL;
586 return cs;
587}
588
589/* Filter out unwanted options from ARGV. */
590
591static void
592filter_args (char **argv)
593{
594 char **destv;
595
596 for (destv = argv; *argv != NULL; argv++)
597 {
598 /* -fpreprocessed may get in commonly from ccache. */
599 if (strcmp (*argv, "-fpreprocessed") == 0)
600 {
601 xfree (*argv);
602 continue;
603 }
604 *destv++ = *argv;
605 }
606 *destv = NULL;
607}
608
609/* Produce final vector of GCC compilation options.
610
611 The first element of the combined argument vector are arguments
612 relating to the target size ("-m64", "-m32" etc.). These are
613 sourced from the inferior's architecture.
614
615 The second element of the combined argument vector are arguments
616 stored in the inferior DW_AT_producer section. If these are stored
617 in the inferior (there is no guarantee that they are), they are
618 added to the vector.
619
620 The third element of the combined argument vector are argument
621 supplied by the language implementation provided by
622 compile-{lang}-support. These contain language specific arguments.
623
624 The final element of the combined argument vector are arguments
625 supplied by the "set compile-args" command. These are always
626 appended last so as to override any of the arguments automatically
627 generated above. */
628
629static gdb_argv
630get_args (const compile_instance *compiler, struct gdbarch *gdbarch)
631{
632 const char *cs_producer_options;
633 gdb_argv result;
634
635 std::string gcc_options = gdbarch_gcc_target_options (gdbarch);
636
637 /* Make sure we have a non-empty set of options, otherwise GCC will
638 error out trying to look for a filename that is an empty string. */
639 if (!gcc_options.empty ())
640 result = gdb_argv (gcc_options.c_str ());
641
642 cs_producer_options = get_selected_pc_producer_options ();
643 if (cs_producer_options != NULL)
644 {
645 gdb_argv argv_producer (cs_producer_options);
646 filter_args (argv_producer.get ());
647
648 result.append (std::move (argv_producer));
649 }
650
651 result.append (gdb_argv (compiler->gcc_target_options ().c_str ()));
652 result.append (compile_args_argv);
653
654 return result;
655}
656
657/* A helper function suitable for use as the "print_callback" in the
658 compiler object. */
659
660static void
661print_callback (void *ignore, const char *message)
662{
663 gdb_puts (message, gdb_stderr);
664}
665
666/* Process the compilation request. On success it returns the object
667 and source file names. On an error condition, error () is
668 called. */
669
671compile_to_object (struct command_line *cmd, const char *cmd_string,
672 enum compile_i_scope_types scope)
673{
674 const struct block *expr_block;
675 CORE_ADDR trash_pc, expr_pc;
676 int ok;
677 struct gdbarch *gdbarch = get_current_arch ();
678 std::string triplet_rx;
679
680 if (!target_has_execution ())
681 error (_("The program must be running for the compile command to "\
682 "work."));
683
684 expr_block = get_expr_block_and_pc (&trash_pc);
686
687 /* Set up instance and context for the compiler. */
688 std::unique_ptr<compile_instance> compiler
690 if (compiler == nullptr)
691 error (_("No compiler support for language %s."),
693 compiler->set_print_callback (print_callback, NULL);
694 compiler->set_scope (scope);
695 compiler->set_block (expr_block);
696
697 /* From the provided expression, build a scope to pass to the
698 compiler. */
699
700 string_file input_buf;
701 const char *input;
702
703 if (cmd != NULL)
704 {
705 struct command_line *iter;
706
707 for (iter = cmd->body_list_0.get (); iter; iter = iter->next)
708 {
709 input_buf.puts (iter->line);
710 input_buf.puts ("\n");
711 }
712
713 input = input_buf.c_str ();
714 }
715 else if (cmd_string != NULL)
716 input = cmd_string;
717 else
718 error (_("Neither a simple expression, or a multi-line specified."));
719
720 std::string code
721 = current_language->compute_program (compiler.get (), input, gdbarch,
722 expr_block, expr_pc);
723 if (compile_debug)
724 gdb_printf (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
725
726 compiler->set_verbose (compile_debug);
727
728 if (!compile_gcc.empty ())
729 {
730 if (compiler->version () < GCC_FE_VERSION_1)
731 error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
732 "(libcc1 interface version 1 or higher)"));
733
734 compiler->set_driver_filename (compile_gcc.c_str ());
735 }
736 else
737 {
738 const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
739 const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
740
741 /* Allow triplets with or without vendor set. */
742 triplet_rx = std::string (arch_rx) + "(-[^-]*)?-";
743 if (os_rx != nullptr)
744 triplet_rx += os_rx;
745 compiler->set_triplet_regexp (triplet_rx.c_str ());
746 }
747
748 /* Set compiler command-line arguments. */
749 gdb_argv argv_holder = get_args (compiler.get (), gdbarch);
750 int argc = argv_holder.count ();
751 char **argv = argv_holder.get ();
752
753 gdb::unique_xmalloc_ptr<char> error_message
754 = compiler->set_arguments (argc, argv, triplet_rx.c_str ());
755
756 if (error_message != NULL)
757 error ("%s", error_message.get ());
758
759 if (compile_debug)
760 {
761 int argi;
762
763 gdb_printf (gdb_stdlog, "Passing %d compiler options:\n", argc);
764 for (argi = 0; argi < argc; argi++)
765 gdb_printf (gdb_stdlog, "Compiler option %d: <%s>\n",
766 argi, argv[argi]);
767 }
768
770
771 gdb::optional<gdb::unlinker> source_remover;
772
773 {
774 gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w");
775 if (src == NULL)
776 perror_with_name (_("Could not open source file for writing"));
777
778 source_remover.emplace (fnames.source_file ());
779
780 if (fputs (code.c_str (), src.get ()) == EOF)
781 perror_with_name (_("Could not write to source file"));
782 }
783
784 if (compile_debug)
785 gdb_printf (gdb_stdlog, "source file produced: %s\n\n",
786 fnames.source_file ());
787
788 /* If we don't do this, then GDB simply exits
789 when the compiler dies. */
790 scoped_ignore_sigpipe ignore_sigpipe;
791
792 /* Call the compiler and start the compilation process. */
793 compiler->set_source_file (fnames.source_file ());
794 ok = compiler->compile (fnames.object_file (), compile_debug);
795 if (!ok)
796 error (_("Compilation failed."));
797
798 if (compile_debug)
799 gdb_printf (gdb_stdlog, "object file produced: %s\n\n",
800 fnames.object_file ());
801
802 /* Keep the source file. */
803 source_remover->keep ();
804 return fnames;
805}
806
807/* The "compile" prefix command. */
808
809static void
810compile_command (const char *args, int from_tty)
811{
812 /* If a sub-command is not specified to the compile prefix command,
813 assume it is a direct code compilation. */
814 compile_code_command (args, from_tty);
815}
816
817/* See compile.h. */
818
819void
820eval_compile_command (struct command_line *cmd, const char *cmd_string,
822{
823 compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
824
825 gdb::unlinker object_remover (fnames.object_file ());
826 gdb::unlinker source_remover (fnames.source_file ());
827
829 scope_data);
830 if (compile_module == NULL)
831 {
832 gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
833 eval_compile_command (cmd, cmd_string,
835 return;
836 }
837
838 /* Keep the files. */
839 source_remover.keep ();
840 object_remover.keep ();
841
843}
844
845/* See compile/compile-internal.h. */
846
847std::string
849{
850 const char *regname = gdbarch_register_name (gdbarch, regnum);
851
852 return string_printf ("__%s", regname);
853}
854
855/* See compile/compile-internal.h. */
856
857int
859 const char *regname)
860{
861 int regnum;
862
863 if (regname[0] != '_' || regname[1] != '_')
864 error (_("Invalid register name \"%s\"."), regname);
865 regname += 2;
866
868 if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
869 return regnum;
870
871 error (_("Cannot find gdbarch register \"%s\"."), regname);
872}
873
874/* Forwards to the plug-in. */
876#define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
877
878/* See compile-internal.h. */
879
880void
882 (void (*print_function) (void *, const char *), void *datum)
883{
884 FORWARD (set_print_callback, print_function, datum);
885}
886
887/* See compile-internal.h. */
888
889unsigned int
891{
892 return m_gcc_fe->ops->version;
893}
894
895/* See compile-internal.h. */
896
897void
899{
900 if (version () >= GCC_FE_VERSION_1)
901 FORWARD (set_verbose, level);
902}
903
904/* See compile-internal.h. */
905
906void
907compile_instance::set_driver_filename (const char *filename)
908{
909 if (version () >= GCC_FE_VERSION_1)
910 FORWARD (set_driver_filename, filename);
911}
912
913/* See compile-internal.h. */
914
915void
916compile_instance::set_triplet_regexp (const char *regexp)
917{
918 if (version () >= GCC_FE_VERSION_1)
919 FORWARD (set_triplet_regexp, regexp);
920}
921
922/* See compile-internal.h. */
923
924gdb::unique_xmalloc_ptr<char>
925compile_instance::set_arguments (int argc, char **argv, const char *regexp)
926{
927 if (version () >= GCC_FE_VERSION_1)
928 return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments, argc, argv));
929 else
930 return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments_v0, regexp,
931 argc, argv));
932}
933
934/* See compile-internal.h. */
935
936void
937compile_instance::set_source_file (const char *filename)
938{
939 FORWARD (set_source_file, filename);
940}
941
942/* See compile-internal.h. */
943
944bool
945compile_instance::compile (const char *filename, int verbose_level)
946{
947 if (version () >= GCC_FE_VERSION_1)
948 return FORWARD (compile, filename);
949 else
950 return FORWARD (compile_v0, filename, verbose_level);
951}
952
953#undef FORWARD
954
955/* See compile.h. */
957
958void _initialize_compile ();
959void
961{
962 struct cmd_list_element *c = NULL;
963
965 compile_command, _("\
966Command to compile source code and inject it into the inferior."),
969
970 const auto compile_opts = make_compile_options_def_group (nullptr);
971
972 static const std::string compile_code_help
974Compile, inject, and execute code.\n\
975\n\
976Usage: compile code [OPTION]... [CODE]\n\
977\n\
978Options:\n\
979%OPTIONS%\n\
980\n\
981The source code may be specified as a simple one line expression, e.g.:\n\
982\n\
983 compile code printf(\"Hello world\\n\");\n\
984\n\
985Alternatively, you can type a multiline expression by invoking\n\
986this command with no argument. GDB will then prompt for the\n\
987expression interactively; type a line containing \"end\" to\n\
988indicate the end of the expression."),
989 compile_opts);
990
992 compile_code_help.c_str (),
995
996static const std::string compile_file_help
998Evaluate a file containing source code.\n\
999\n\
1000Usage: compile file [OPTION].. [FILENAME]\n\
1001\n\
1002Options:\n\
1003%OPTIONS%"),
1004 compile_opts);
1005
1007 compile_file_help.c_str (),
1010
1011 const auto compile_print_opts = make_value_print_options_def_group (nullptr);
1012
1013 static const std::string compile_print_help
1015Evaluate EXPR by using the compiler and print result.\n\
1016\n\
1017Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
1018\n\
1019Options:\n\
1020%OPTIONS%\n\
1021\n\
1022Note: because this command accepts arbitrary expressions, if you\n\
1023specify any command option, you must use a double dash (\"--\")\n\
1024to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
1025\n\
1026The expression may be specified on the same line as the command, e.g.:\n\
1027\n\
1028 compile print i\n\
1029\n\
1030Alternatively, you can type a multiline expression by invoking\n\
1031this command with no argument. GDB will then prompt for the\n\
1032expression interactively; type a line containing \"end\" to\n\
1033indicate the end of the expression.\n\
1034\n\
1035EXPR may be preceded with /FMT, where FMT is a format letter\n\
1036but no count or size letter (see \"x\" command)."),
1037 compile_print_opts);
1038
1040 compile_print_help.c_str (),
1043
1045Set compile command debugging."), _("\
1046Show compile command debugging."), _("\
1047When on, compile command debugging is enabled."),
1048 NULL, show_compile_debug,
1050
1051 add_setshow_string_cmd ("compile-args", class_support,
1052 &compile_args,
1053 _("Set compile command GCC command-line arguments."),
1054 _("Show compile command GCC command-line arguments."),
1055 _("\
1056Use options like -I (include file directory) or ABI settings.\n\
1057String quoting is parsed like in shell, for example:\n\
1058 -mno-align-double \"-I/dir with a space/include\""),
1060
1061
1062 /* Initialize compile_args_argv. */
1063 set_compile_args (compile_args.c_str (), 0, NULL);
1064
1066 &compile_gcc,
1067 _("Set compile command "
1068 "GCC driver filename."),
1069 _("Show compile command "
1070 "GCC driver filename."),
1071 _("\
1072It should be absolute filename of the gcc executable.\n\
1073If empty the default target triplet will be searched in $PATH."),
1074 NULL, show_compile_gcc, &setlist,
1075 &showlist);
1076}
int regnum
void xfree(void *)
int code
Definition ada-lex.l:670
void * xcalloc(size_t number, size_t size)
Definition alloc.c:85
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
const char * source_file() const
const char * object_file() const
unsigned int version() const
Definition compile.c:889
void set_driver_filename(const char *filename)
Definition compile.c:906
struct gcc_base_context * m_gcc_fe
Definition compile.h:125
void error_symbol_once(const struct symbol *sym)
Definition compile.c:225
void set_print_callback(void(*print_function)(void *, const char *), void *datum)
Definition compile.c:881
void insert_symbol_error(const struct symbol *sym, const char *text)
Definition compile.c:204
compile_instance(struct gcc_base_context *gcc_fe, const char *options)
Definition compile.c:145
bool get_cached_type(struct type *type, gcc_type *ret) const
Definition compile.c:160
void set_triplet_regexp(const char *regexp)
Definition compile.c:915
gdb::unique_xmalloc_ptr< char > set_arguments(int argc, char **argv, const char *regexp=NULL)
Definition compile.c:924
htab_up m_type_map
Definition compile.h:138
void insert_type(struct type *type, gcc_type gcc_type)
Definition compile.c:178
const std::string & gcc_target_options() const
Definition compile.h:44
void set_verbose(int level)
Definition compile.c:897
htab_up m_symbol_err_map
Definition compile.h:141
void set_source_file(const char *filename)
Definition compile.c:936
bool compile(const char *filename, int verbose_level=-1)
Definition compile.c:944
const char * c_str() const
Definition ui-file.h:222
virtual void puts(const char *str)
Definition ui-file.h:76
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * cmdlist
Definition cli-cmds.c:87
struct cmd_list_element * setlist
Definition cli-cmds.c:119
struct cmd_list_element * showdebuglist
Definition cli-cmds.c:167
struct cmd_list_element * setdebuglist
Definition cli-cmds.c:165
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
void set_cmd_completer_handle_brkchars(struct cmd_list_element *cmd, completer_handle_brkchars_ftype *func)
Definition cli-decode.c:125
cmd_list_element * add_com_alias(const char *name, cmd_list_element *target, command_class theclass, int abbrev_flag)
set_show_commands add_setshow_optional_filename_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
set_show_commands add_setshow_string_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:903
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
Definition cli-decode.c:357
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
counted_command_line get_command_line(enum command_control_type type, const char *arg)
Definition cli-script.c:182
enum command_control_type execute_control_command_untraced(struct command_line *cmd)
Definition cli-script.c:716
@ compile_control
Definition cli-script.h:44
std::shared_ptr< command_line > counted_command_line
Definition cli-script.h:67
@ class_obscure
Definition command.h:64
@ class_maintenance
Definition command.h:65
@ class_support
Definition command.h:58
#define FORWARD(OP,...)
compile_module_up compile_object_load(const compile_file_names &file_names, enum compile_i_scope_types scope, void *scope_data)
std::unique_ptr< compile_module > compile_module_up
void compile_object_run(compile_module_up &&module)
static gdb_argv compile_args_argv
Definition compile.c:529
static void compile_code_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition compile.c:368
static std::string compile_args
Definition compile.c:511
static void compile_command(const char *args, int from_tty)
Definition compile.c:809
static compile_file_names get_new_file_names()
Definition compile.c:474
static gdb_argv get_args(const compile_instance *compiler, struct gdbarch *gdbarch)
Definition compile.c:629
static gdb::option::option_def_group make_compile_options_def_group(compile_options *opts)
Definition compile.c:278
void eval_compile_command(struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope, void *scope_data)
Definition compile.c:819
static void filter_args(char **argv)
Definition compile.c:591
static const char * get_selected_pc_producer_options(void)
Definition compile.c:570
static const struct block * get_expr_block_and_pc(CORE_ADDR *pc)
Definition compile.c:490
bool compile_debug
Definition compile.c:61
#define TEMPLATE
static void do_rmdir(void *arg)
Definition compile.c:432
static hashval_t hash_type_map_instance(const void *p)
Definition compile.c:93
static hashval_t hash_symbol_error(const void *a)
Definition compile.c:114
static void set_compile_args(const char *args, int from_tty, struct cmd_list_element *c)
Definition compile.c:534
static std::string compile_gcc
Definition compile.c:551
static void compile_file_command(const char *args, int from_tty)
Definition compile.c:288
static int eq_type_map_instance(const void *a, const void *b)
Definition compile.c:103
static compile_file_names compile_to_object(struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope)
Definition compile.c:670
static void show_compile_gcc(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition compile.c:556
static void compile_code_command(const char *args, int from_tty)
Definition compile.c:340
static void show_compile_args(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition compile.c:542
static const gdb::option::option_def compile_command_option_defs[]
Definition compile.c:264
static void print_callback(void *ignore, const char *message)
Definition compile.c:660
void compile_print_value(struct value *val, void *data_voidp)
Definition compile.c:385
static void show_compile_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition compile.c:246
static struct cmd_list_element * compile_command_list
Definition compile.c:57
cmd_list_element * compile_cmd_element
Definition compile.c:955
void _initialize_compile()
Definition compile.c:959
int compile_register_name_demangle(struct gdbarch *gdbarch, const char *regname)
Definition compile.c:857
#define TMP_PREFIX
Definition compile.c:53
static void compile_file_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition compile.c:320
static int eq_symbol_error(const void *a, const void *b)
Definition compile.c:124
std::string compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
Definition compile.c:847
static const char * get_compile_file_tempdir(void)
Definition compile.c:450
static void del_symbol_error(void *a)
Definition compile.c:135
static void compile_print_command(const char *arg, int from_tty)
Definition compile.c:398
const char * advance_to_expression_complete_word_point(completion_tracker &tracker, const char *text)
Definition completer.c:422
const char * advance_to_filename_complete_word_point(completion_tracker &tracker, const char *text)
Definition completer.c:432
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition completer.c:204
void symbol_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition completer.c:1110
compile_i_scope_types
Definition defs.h:70
@ COMPILE_I_PRINT_ADDRESS_SCOPE
Definition defs.h:89
@ COMPILE_I_PRINT_VALUE_SCOPE
Definition defs.h:90
@ COMPILE_I_SIMPLE_SCOPE
Definition defs.h:77
@ COMPILE_I_RAW_SCOPE
Definition defs.h:81
CORE_ADDR get_frame_pc(frame_info_ptr frame)
Definition frame.c:2712
frame_info_ptr get_selected_frame(const char *message)
Definition frame.c:1888
CORE_ADDR get_frame_address_in_block(frame_info_ptr this_frame)
Definition frame.c:2742
const struct block * get_selected_block(CORE_ADDR *addr_in_block)
Definition stack.c:2570
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition gdbarch.c:2173
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition gdbarch.c:1930
const char * gdbarch_gnu_triplet_regexp(struct gdbarch *gdbarch)
Definition gdbarch.c:5283
std::string gdbarch_gcc_target_options(struct gdbarch *gdbarch)
Definition gdbarch.c:5266
enum gdb_osabi gdbarch_osabi(struct gdbarch *gdbarch)
Definition gdbarch.c:1414
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
const struct language_defn * current_language
Definition language.c:82
bool process_options(const char **args, process_options_mode mode, gdb::array_view< const option_def_group > options_group)
Definition cli-option.c:627
@ PROCESS_OPTIONS_REQUIRE_DELIMITER
Definition cli-option.h:328
@ PROCESS_OPTIONS_UNKNOWN_IS_ERROR
Definition cli-option.h:333
std::string build_help(const char *help_tmpl, gdb::array_view< const option_def_group > options_group)
Definition cli-option.c:766
bool complete_options(completion_tracker &tracker, const char **args, process_options_mode mode, gdb::array_view< const option_def_group > options_group)
Definition cli-option.c:467
const char * osabi_triplet_regexp(enum gdb_osabi osabi)
Definition osabi.c:101
void print_command_parse_format(const char **expp, const char *cmdname, value_print_options *opts)
Definition printcmd.c:1214
void print_value(value *val, const value_print_options &opts)
Definition printcmd.c:1245
void print_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *)
Definition printcmd.c:1449
enum var_types type
Definition scm-param.c:142
struct symtab_and_line get_current_source_symtab_and_line(void)
Definition source.c:239
Definition block.h:109
CORE_ADDR entry_pc() const
Definition block.h:195
struct block * static_block()
Definition block.h:405
counted_command_line body_list_0
Definition cli-script.h:102
enum compile_i_scope_types scope
Definition cli-script.h:93
struct command_line * next
Definition cli-script.h:86
char * line
Definition cli-script.h:87
void * scope_data
Definition cli-script.h:94
struct blockvector * blockvector()
Definition symtab.h:1847
virtual std::string compute_program(compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc) const
Definition language.h:414
virtual const char * name() const =0
virtual std::unique_ptr< compile_instance > get_compile_instance() const
Definition language.c:631
const struct symbol * sym
Definition compile.c:69
char * message
Definition compile.c:74
struct symtab * symtab
Definition symtab.h:2328
CORE_ADDR pc
Definition symtab.h:2337
struct compunit_symtab * compunit() const
Definition symtab.h:1677
struct type * type
Definition compile.c:83
gcc_type gcc_type_handle
Definition compile.c:87
int async
Definition ui.h:106
Definition value.h:130
struct compunit_symtab * find_pc_compunit_symtab(CORE_ADDR pc)
Definition symtab.c:2946
bool target_has_execution(inferior *inf)
Definition target.c:201
struct ui * current_ui
Definition ui.c:35
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition utils.c:1809
#define gdb_stderr
Definition utils.h:187
#define gdb_stdlog
Definition utils.h:190
gdb::option::option_def_group make_value_print_options_def_group(value_print_options *opts)
Definition valprint.c:3093
void get_user_print_options(struct value_print_options *opts)
Definition valprint.c:135