1 /* General Compile and inject code
3 Copyright (C) 2014-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "cli/cli-script.h"
25 #include "cli/cli-utils.h"
26 #include "cli/cli-option.h"
27 #include "completer.h"
30 #include "compile-internal.h"
31 #include "compile-object-load.h"
32 #include "compile-object-run.h"
37 #include "arch-utils.h"
38 #include "gdbsupport/filestuff.h"
41 #include "gdbsupport/gdb_wait.h"
43 #include "gdbsupport/gdb_optional.h"
44 #include "gdbsupport/gdb_unlinker.h"
45 #include "gdbsupport/pathstuff.h"
49 /* Initial filename for temporary files. */
51 #define TMP_PREFIX "/tmp/gdbobj-"
53 /* Hold "compile" commands. */
55 static struct cmd_list_element
*compile_command_list
;
57 /* Debug flag for "compile" commands. */
61 /* Object of this type are stored in the compiler's symbol_err_map. */
67 const struct symbol
*sym
;
69 /* The error message to emit. This is malloc'd and owned by the
75 /* Hash a type_map_instance. */
78 hash_type_map_instance (const void *p
)
80 const struct type_map_instance
*inst
= (const struct type_map_instance
*) p
;
82 return htab_hash_pointer (inst
->type
);
85 /* Check two type_map_instance objects for equality. */
88 eq_type_map_instance (const void *a
, const void *b
)
90 const struct type_map_instance
*insta
= (const struct type_map_instance
*) a
;
91 const struct type_map_instance
*instb
= (const struct type_map_instance
*) b
;
93 return insta
->type
== instb
->type
;
96 /* Hash function for struct symbol_error. */
99 hash_symbol_error (const void *a
)
101 const struct symbol_error
*se
= (const struct symbol_error
*) a
;
103 return htab_hash_pointer (se
->sym
);
106 /* Equality function for struct symbol_error. */
109 eq_symbol_error (const void *a
, const void *b
)
111 const struct symbol_error
*sea
= (const struct symbol_error
*) a
;
112 const struct symbol_error
*seb
= (const struct symbol_error
*) b
;
114 return sea
->sym
== seb
->sym
;
117 /* Deletion function for struct symbol_error. */
120 del_symbol_error (void *a
)
122 struct symbol_error
*se
= (struct symbol_error
*) a
;
128 /* Constructor for compile_instance. */
130 compile_instance::compile_instance (struct gcc_base_context
*gcc_fe
,
132 : m_gcc_fe (gcc_fe
), m_gcc_target_options (options
),
133 m_type_map (htab_create_alloc (10, hash_type_map_instance
,
134 eq_type_map_instance
,
135 xfree
, xcalloc
, xfree
)),
136 m_symbol_err_map (htab_create_alloc (10, hash_symbol_error
,
137 eq_symbol_error
, del_symbol_error
,
142 /* See compile-internal.h. */
145 compile_instance::get_cached_type (struct type
*type
, gcc_type
*ret
) const
147 struct type_map_instance inst
, *found
;
150 found
= (struct type_map_instance
*) htab_find (m_type_map
.get (), &inst
);
153 *ret
= found
->gcc_type_handle
;
160 /* See compile-internal.h. */
163 compile_instance::insert_type (struct type
*type
, gcc_type gcc_type
)
165 struct type_map_instance inst
, *add
;
169 inst
.gcc_type_handle
= gcc_type
;
170 slot
= htab_find_slot (m_type_map
.get (), &inst
, INSERT
);
172 add
= (struct type_map_instance
*) *slot
;
173 /* The type might have already been inserted in order to handle
175 if (add
!= NULL
&& add
->gcc_type_handle
!= gcc_type
)
176 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
180 add
= XNEW (struct type_map_instance
);
186 /* See compile-internal.h. */
189 compile_instance::insert_symbol_error (const struct symbol
*sym
,
192 struct symbol_error e
;
196 slot
= htab_find_slot (m_symbol_err_map
.get (), &e
, INSERT
);
199 struct symbol_error
*ep
= XNEW (struct symbol_error
);
202 ep
->message
= xstrdup (text
);
207 /* See compile-internal.h. */
210 compile_instance::error_symbol_once (const struct symbol
*sym
)
212 struct symbol_error search
;
213 struct symbol_error
*err
;
215 if (m_symbol_err_map
== NULL
)
219 err
= (struct symbol_error
*) htab_find (m_symbol_err_map
.get (), &search
);
220 if (err
== NULL
|| err
->message
== NULL
)
223 gdb::unique_xmalloc_ptr
<char> message (err
->message
);
225 error (_("%s"), message
.get ());
228 /* Implement "show debug compile". */
231 show_compile_debug (struct ui_file
*file
, int from_tty
,
232 struct cmd_list_element
*c
, const char *value
)
234 fprintf_filtered (file
, _("Compile debugging is %s.\n"), value
);
239 /* Options for the compile command. */
241 struct compile_options
247 using compile_flag_option_def
248 = gdb::option::flag_option_def
<compile_options
>;
250 static const gdb::option::option_def compile_command_option_defs
[] = {
252 compile_flag_option_def
{
254 [] (compile_options
*opts
) { return &opts
->raw
; },
255 N_("Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
260 /* Create an option_def_group for the "compile" command's options,
261 with OPTS as context. */
263 static gdb::option::option_def_group
264 make_compile_options_def_group (compile_options
*opts
)
266 return {{compile_command_option_defs
}, opts
};
269 /* Handle the input from the 'compile file' command. The "compile
270 file" command is used to evaluate an expression contained in a file
271 that may contain calls to the GCC compiler. */
274 compile_file_command (const char *args
, int from_tty
)
276 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
278 /* Check if a -raw option is provided. */
280 compile_options options
;
282 const gdb::option::option_def_group group
283 = make_compile_options_def_group (&options
);
284 gdb::option::process_options
285 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
,
288 enum compile_i_scope_types scope
289 = options
.raw
? COMPILE_I_RAW_SCOPE
: COMPILE_I_SIMPLE_SCOPE
;
291 args
= skip_spaces (args
);
293 /* After processing options, check whether we have a filename. */
294 if (args
== nullptr || args
[0] == '\0')
295 error (_("You must provide a filename for this command."));
297 args
= skip_spaces (args
);
298 gdb::unique_xmalloc_ptr
<char> abspath
= gdb_abspath (args
);
299 std::string buffer
= string_printf ("#include \"%s\"\n", abspath
.get ());
300 eval_compile_command (NULL
, buffer
.c_str (), scope
, NULL
);
303 /* Completer for the "compile file" command. */
306 compile_file_command_completer (struct cmd_list_element
*ignore
,
307 completion_tracker
&tracker
,
308 const char *text
, const char *word
)
310 const gdb::option::option_def_group group
311 = make_compile_options_def_group (nullptr);
312 if (gdb::option::complete_options
313 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
316 word
= advance_to_filename_complete_word_point (tracker
, text
);
317 filename_completer (ignore
, tracker
, text
, word
);
320 /* Handle the input from the 'compile code' command. The
321 "compile code" command is used to evaluate an expression that may
322 contain calls to the GCC compiler. The language expected in this
323 compile command is the language currently set in GDB. */
326 compile_code_command (const char *args
, int from_tty
)
328 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
330 compile_options options
;
332 const gdb::option::option_def_group group
333 = make_compile_options_def_group (&options
);
334 gdb::option::process_options
335 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
337 enum compile_i_scope_types scope
338 = options
.raw
? COMPILE_I_RAW_SCOPE
: COMPILE_I_SIMPLE_SCOPE
;
341 eval_compile_command (NULL
, args
, scope
, NULL
);
344 counted_command_line l
= get_command_line (compile_control
, "");
346 l
->control_u
.compile
.scope
= scope
;
347 execute_control_command_untraced (l
.get ());
351 /* Completer for the "compile code" command. */
354 compile_code_command_completer (struct cmd_list_element
*ignore
,
355 completion_tracker
&tracker
,
356 const char *text
, const char *word
)
358 const gdb::option::option_def_group group
359 = make_compile_options_def_group (nullptr);
360 if (gdb::option::complete_options
361 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
364 word
= advance_to_expression_complete_word_point (tracker
, text
);
365 symbol_completer (ignore
, tracker
, text
, word
);
368 /* Callback for compile_print_command. */
371 compile_print_value (struct value
*val
, void *data_voidp
)
373 const value_print_options
*print_opts
= (value_print_options
*) data_voidp
;
375 print_value (val
, *print_opts
);
378 /* Handle the input from the 'compile print' command. The "compile
379 print" command is used to evaluate and print an expression that may
380 contain calls to the GCC compiler. The language expected in this
381 compile command is the language currently set in GDB. */
384 compile_print_command (const char *arg
, int from_tty
)
386 enum compile_i_scope_types scope
= COMPILE_I_PRINT_ADDRESS_SCOPE
;
387 value_print_options print_opts
;
389 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
391 get_user_print_options (&print_opts
);
392 /* Override global settings with explicit options, if any. */
393 auto group
= make_value_print_options_def_group (&print_opts
);
394 gdb::option::process_options
395 (&arg
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
397 print_command_parse_format (&arg
, "compile print", &print_opts
);
399 /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup
400 will not touch the stale pointer if compile_object_run has
404 eval_compile_command (NULL
, arg
, scope
, &print_opts
);
407 counted_command_line l
= get_command_line (compile_control
, "");
409 l
->control_u
.compile
.scope
= scope
;
410 l
->control_u
.compile
.scope_data
= &print_opts
;
411 execute_control_command_untraced (l
.get ());
415 /* A cleanup function to remove a directory and all its contents. */
420 const char *dir
= (const char *) arg
;
424 gdb_assert (startswith (dir
, TMP_PREFIX
));
425 zap
= concat ("rm -rf ", dir
, (char *) NULL
);
426 wstat
= system (zap
);
427 if (wstat
== -1 || !WIFEXITED (wstat
) || WEXITSTATUS (wstat
) != 0)
428 warning (_("Could not remove temporary directory %s"), dir
);
432 /* Return the name of the temporary directory to use for .o files, and
433 arrange for the directory to be removed at shutdown. */
436 get_compile_file_tempdir (void)
438 static char *tempdir_name
;
440 #define TEMPLATE TMP_PREFIX "XXXXXX"
441 char tname
[sizeof (TEMPLATE
)];
443 if (tempdir_name
!= NULL
)
446 strcpy (tname
, TEMPLATE
);
448 tempdir_name
= mkdtemp (tname
);
449 if (tempdir_name
== NULL
)
450 perror_with_name (_("Could not make temporary directory"));
452 tempdir_name
= xstrdup (tempdir_name
);
453 make_final_cleanup (do_rmdir
, tempdir_name
);
457 /* Compute the names of source and object files to use. */
459 static compile_file_names
460 get_new_file_names ()
463 const char *dir
= get_compile_file_tempdir ();
467 return compile_file_names (string_printf ("%s%sout%d.c",
468 dir
, SLASH_STRING
, seq
),
469 string_printf ("%s%sout%d.o",
470 dir
, SLASH_STRING
, seq
));
473 /* Get the block and PC at which to evaluate an expression. */
475 static const struct block
*
476 get_expr_block_and_pc (CORE_ADDR
*pc
)
478 const struct block
*block
= get_selected_block (pc
);
482 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
485 block
= BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal
.symtab
),
488 *pc
= BLOCK_ENTRY_PC (block
);
491 *pc
= BLOCK_ENTRY_PC (block
);
496 /* String for 'set compile-args' and 'show compile-args'. */
497 static char *compile_args
;
499 /* Parsed form of COMPILE_ARGS. */
500 static gdb_argv compile_args_argv
;
502 /* Implement 'set compile-args'. */
505 set_compile_args (const char *args
, int from_tty
, struct cmd_list_element
*c
)
507 compile_args_argv
= gdb_argv (compile_args
);
510 /* Implement 'show compile-args'. */
513 show_compile_args (struct ui_file
*file
, int from_tty
,
514 struct cmd_list_element
*c
, const char *value
)
516 fprintf_filtered (file
, _("Compile command command-line arguments "
521 /* String for 'set compile-gcc' and 'show compile-gcc'. */
522 static char *compile_gcc
;
524 /* Implement 'show compile-gcc'. */
527 show_compile_gcc (struct ui_file
*file
, int from_tty
,
528 struct cmd_list_element
*c
, const char *value
)
530 fprintf_filtered (file
, _("Compile command GCC driver filename is \"%s\".\n"),
534 /* Return DW_AT_producer parsed for get_selected_frame () (if any).
535 Return NULL otherwise.
537 GCC already filters its command-line arguments only for the suitable ones to
538 put into DW_AT_producer - see GCC function gen_producer_string. */
541 get_selected_pc_producer_options (void)
543 CORE_ADDR pc
= get_frame_pc (get_selected_frame (NULL
));
544 struct compunit_symtab
*symtab
= find_pc_compunit_symtab (pc
);
547 if (symtab
== NULL
|| symtab
->producer
== NULL
548 || !startswith (symtab
->producer
, "GNU "))
551 cs
= symtab
->producer
;
552 while (*cs
!= 0 && *cs
!= '-')
553 cs
= skip_spaces (skip_to_space (cs
));
559 /* Filter out unwanted options from ARGV. */
562 filter_args (char **argv
)
566 for (destv
= argv
; *argv
!= NULL
; argv
++)
568 /* -fpreprocessed may get in commonly from ccache. */
569 if (strcmp (*argv
, "-fpreprocessed") == 0)
579 /* Produce final vector of GCC compilation options.
581 The first element of the combined argument vector are arguments
582 relating to the target size ("-m64", "-m32" etc.). These are
583 sourced from the inferior's architecture.
585 The second element of the combined argument vector are arguments
586 stored in the inferior DW_AT_producer section. If these are stored
587 in the inferior (there is no guarantee that they are), they are
590 The third element of the combined argument vector are argument
591 supplied by the language implementation provided by
592 compile-{lang}-support. These contain language specific arguments.
594 The final element of the combined argument vector are arguments
595 supplied by the "set compile-args" command. These are always
596 appended last so as to override any of the arguments automatically
600 get_args (const compile_instance
*compiler
, struct gdbarch
*gdbarch
)
602 const char *cs_producer_options
;
605 std::string gcc_options
= gdbarch_gcc_target_options (gdbarch
);
607 /* Make sure we have a non-empty set of options, otherwise GCC will
608 error out trying to look for a filename that is an empty string. */
609 if (!gcc_options
.empty ())
610 result
= gdb_argv (gcc_options
.c_str ());
612 cs_producer_options
= get_selected_pc_producer_options ();
613 if (cs_producer_options
!= NULL
)
615 gdb_argv
argv_producer (cs_producer_options
);
616 filter_args (argv_producer
.get ());
618 result
.append (std::move (argv_producer
));
621 result
.append (gdb_argv (compiler
->gcc_target_options ().c_str ()));
622 result
.append (compile_args_argv
);
627 /* A helper function suitable for use as the "print_callback" in the
631 print_callback (void *ignore
, const char *message
)
633 fputs_filtered (message
, gdb_stderr
);
636 /* Process the compilation request. On success it returns the object
637 and source file names. On an error condition, error () is
640 static compile_file_names
641 compile_to_object (struct command_line
*cmd
, const char *cmd_string
,
642 enum compile_i_scope_types scope
)
644 const struct block
*expr_block
;
645 CORE_ADDR trash_pc
, expr_pc
;
647 struct gdbarch
*gdbarch
= get_current_arch ();
648 std::string triplet_rx
;
650 if (!target_has_execution ())
651 error (_("The program must be running for the compile command to "\
654 expr_block
= get_expr_block_and_pc (&trash_pc
);
655 expr_pc
= get_frame_address_in_block (get_selected_frame (NULL
));
657 /* Set up instance and context for the compiler. */
658 std::unique_ptr
<compile_instance
> compiler
659 = current_language
->get_compile_instance ();
660 if (compiler
== nullptr)
661 error (_("No compiler support for language %s."),
662 current_language
->name ());
663 compiler
->set_print_callback (print_callback
, NULL
);
664 compiler
->set_scope (scope
);
665 compiler
->set_block (expr_block
);
667 /* From the provided expression, build a scope to pass to the
670 string_file input_buf
;
675 struct command_line
*iter
;
677 for (iter
= cmd
->body_list_0
.get (); iter
; iter
= iter
->next
)
679 input_buf
.puts (iter
->line
);
680 input_buf
.puts ("\n");
683 input
= input_buf
.c_str ();
685 else if (cmd_string
!= NULL
)
688 error (_("Neither a simple expression, or a multi-line specified."));
691 = current_language
->compute_program (compiler
.get (), input
, gdbarch
,
692 expr_block
, expr_pc
);
694 fprintf_unfiltered (gdb_stdlog
, "debug output:\n\n%s", code
.c_str ());
696 compiler
->set_verbose (compile_debug
);
698 if (compile_gcc
[0] != 0)
700 if (compiler
->version () < GCC_FE_VERSION_1
)
701 error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
702 "(libcc1 interface version 1 or higher)"));
704 compiler
->set_driver_filename (compile_gcc
);
708 const char *os_rx
= osabi_triplet_regexp (gdbarch_osabi (gdbarch
));
709 const char *arch_rx
= gdbarch_gnu_triplet_regexp (gdbarch
);
711 /* Allow triplets with or without vendor set. */
712 triplet_rx
= std::string (arch_rx
) + "(-[^-]*)?-";
713 if (os_rx
!= nullptr)
715 compiler
->set_triplet_regexp (triplet_rx
.c_str ());
718 /* Set compiler command-line arguments. */
719 gdb_argv argv_holder
= get_args (compiler
.get (), gdbarch
);
720 int argc
= argv_holder
.count ();
721 char **argv
= argv_holder
.get ();
723 gdb::unique_xmalloc_ptr
<char> error_message
724 = compiler
->set_arguments (argc
, argv
, triplet_rx
.c_str ());
726 if (error_message
!= NULL
)
727 error ("%s", error_message
.get ());
733 fprintf_unfiltered (gdb_stdlog
, "Passing %d compiler options:\n", argc
);
734 for (argi
= 0; argi
< argc
; argi
++)
735 fprintf_unfiltered (gdb_stdlog
, "Compiler option %d: <%s>\n",
739 compile_file_names fnames
= get_new_file_names ();
741 gdb::optional
<gdb::unlinker
> source_remover
;
744 gdb_file_up src
= gdb_fopen_cloexec (fnames
.source_file (), "w");
746 perror_with_name (_("Could not open source file for writing"));
748 source_remover
.emplace (fnames
.source_file ());
750 if (fputs (code
.c_str (), src
.get ()) == EOF
)
751 perror_with_name (_("Could not write to source file"));
755 fprintf_unfiltered (gdb_stdlog
, "source file produced: %s\n\n",
756 fnames
.source_file ());
758 /* Call the compiler and start the compilation process. */
759 compiler
->set_source_file (fnames
.source_file ());
760 ok
= compiler
->compile (fnames
.object_file (), compile_debug
);
762 error (_("Compilation failed."));
765 fprintf_unfiltered (gdb_stdlog
, "object file produced: %s\n\n",
766 fnames
.object_file ());
768 /* Keep the source file. */
769 source_remover
->keep ();
773 /* The "compile" prefix command. */
776 compile_command (const char *args
, int from_tty
)
778 /* If a sub-command is not specified to the compile prefix command,
779 assume it is a direct code compilation. */
780 compile_code_command (args
, from_tty
);
786 eval_compile_command (struct command_line
*cmd
, const char *cmd_string
,
787 enum compile_i_scope_types scope
, void *scope_data
)
789 compile_file_names fnames
= compile_to_object (cmd
, cmd_string
, scope
);
791 gdb::unlinker
object_remover (fnames
.object_file ());
792 gdb::unlinker
source_remover (fnames
.source_file ());
794 compile_module_up compile_module
= compile_object_load (fnames
, scope
,
796 if (compile_module
== NULL
)
798 gdb_assert (scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
);
799 eval_compile_command (cmd
, cmd_string
,
800 COMPILE_I_PRINT_VALUE_SCOPE
, scope_data
);
804 /* Keep the files. */
805 source_remover
.keep ();
806 object_remover
.keep ();
808 compile_object_run (std::move (compile_module
));
811 /* See compile/compile-internal.h. */
814 compile_register_name_mangled (struct gdbarch
*gdbarch
, int regnum
)
816 const char *regname
= gdbarch_register_name (gdbarch
, regnum
);
818 return string_printf ("__%s", regname
);
821 /* See compile/compile-internal.h. */
824 compile_register_name_demangle (struct gdbarch
*gdbarch
,
829 if (regname
[0] != '_' || regname
[1] != '_')
830 error (_("Invalid register name \"%s\"."), regname
);
833 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
834 if (strcmp (regname
, gdbarch_register_name (gdbarch
, regnum
)) == 0)
837 error (_("Cannot find gdbarch register \"%s\"."), regname
);
840 /* Forwards to the plug-in. */
842 #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
844 /* See compile-internal.h. */
847 compile_instance::set_print_callback
848 (void (*print_function
) (void *, const char *), void *datum
)
850 FORWARD (set_print_callback
, print_function
, datum
);
853 /* See compile-internal.h. */
856 compile_instance::version () const
858 return m_gcc_fe
->ops
->version
;
861 /* See compile-internal.h. */
864 compile_instance::set_verbose (int level
)
866 if (version () >= GCC_FE_VERSION_1
)
867 FORWARD (set_verbose
, level
);
870 /* See compile-internal.h. */
873 compile_instance::set_driver_filename (const char *filename
)
875 if (version () >= GCC_FE_VERSION_1
)
876 FORWARD (set_driver_filename
, filename
);
879 /* See compile-internal.h. */
882 compile_instance::set_triplet_regexp (const char *regexp
)
884 if (version () >= GCC_FE_VERSION_1
)
885 FORWARD (set_triplet_regexp
, regexp
);
888 /* See compile-internal.h. */
890 gdb::unique_xmalloc_ptr
<char>
891 compile_instance::set_arguments (int argc
, char **argv
, const char *regexp
)
893 if (version () >= GCC_FE_VERSION_1
)
894 return gdb::unique_xmalloc_ptr
<char> (FORWARD (set_arguments
, argc
, argv
));
896 return gdb::unique_xmalloc_ptr
<char> (FORWARD (set_arguments_v0
, regexp
,
900 /* See compile-internal.h. */
903 compile_instance::set_source_file (const char *filename
)
905 FORWARD (set_source_file
, filename
);
908 /* See compile-internal.h. */
911 compile_instance::compile (const char *filename
, int verbose_level
)
913 if (version () >= GCC_FE_VERSION_1
)
914 return FORWARD (compile
, filename
);
916 return FORWARD (compile_v0
, filename
, verbose_level
);
922 cmd_list_element
*compile_cmd_element
= nullptr;
924 void _initialize_compile ();
926 _initialize_compile ()
928 struct cmd_list_element
*c
= NULL
;
930 compile_cmd_element
= add_prefix_cmd ("compile", class_obscure
,
931 compile_command
, _("\
932 Command to compile source code and inject it into the inferior."),
933 &compile_command_list
, "compile ", 1, &cmdlist
);
934 add_com_alias ("expression", "compile", class_obscure
, 0);
936 const auto compile_opts
= make_compile_options_def_group (nullptr);
938 static const std::string compile_code_help
939 = gdb::option::build_help (_("\
940 Compile, inject, and execute code.\n\
942 Usage: compile code [OPTION]... [CODE]\n\
947 The source code may be specified as a simple one line expression, e.g.:\n\
949 compile code printf(\"Hello world\\n\");\n\
951 Alternatively, you can type a multiline expression by invoking\n\
952 this command with no argument. GDB will then prompt for the\n\
953 expression interactively; type a line containing \"end\" to\n\
954 indicate the end of the expression."),
957 c
= add_cmd ("code", class_obscure
, compile_code_command
,
958 compile_code_help
.c_str (),
959 &compile_command_list
);
960 set_cmd_completer_handle_brkchars (c
, compile_code_command_completer
);
962 static const std::string compile_file_help
963 = gdb::option::build_help (_("\
964 Evaluate a file containing source code.\n\
966 Usage: compile file [OPTION].. [FILENAME]\n\
972 c
= add_cmd ("file", class_obscure
, compile_file_command
,
973 compile_file_help
.c_str (),
974 &compile_command_list
);
975 set_cmd_completer_handle_brkchars (c
, compile_file_command_completer
);
977 const auto compile_print_opts
= make_value_print_options_def_group (nullptr);
979 static const std::string compile_print_help
980 = gdb::option::build_help (_("\
981 Evaluate EXPR by using the compiler and print result.\n\
983 Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
988 Note: because this command accepts arbitrary expressions, if you\n\
989 specify any command option, you must use a double dash (\"--\")\n\
990 to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
992 The expression may be specified on the same line as the command, e.g.:\n\
996 Alternatively, you can type a multiline expression by invoking\n\
997 this command with no argument. GDB will then prompt for the\n\
998 expression interactively; type a line containing \"end\" to\n\
999 indicate the end of the expression.\n\
1001 EXPR may be preceded with /FMT, where FMT is a format letter\n\
1002 but no count or size letter (see \"x\" command)."),
1003 compile_print_opts
);
1005 c
= add_cmd ("print", class_obscure
, compile_print_command
,
1006 compile_print_help
.c_str (),
1007 &compile_command_list
);
1008 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
1010 add_setshow_boolean_cmd ("compile", class_maintenance
, &compile_debug
, _("\
1011 Set compile command debugging."), _("\
1012 Show compile command debugging."), _("\
1013 When on, compile command debugging is enabled."),
1014 NULL
, show_compile_debug
,
1015 &setdebuglist
, &showdebuglist
);
1017 add_setshow_string_cmd ("compile-args", class_support
,
1019 _("Set compile command GCC command-line arguments."),
1020 _("Show compile command GCC command-line arguments."),
1022 Use options like -I (include file directory) or ABI settings.\n\
1023 String quoting is parsed like in shell, for example:\n\
1024 -mno-align-double \"-I/dir with a space/include\""),
1025 set_compile_args
, show_compile_args
, &setlist
, &showlist
);
1027 /* Override flags possibly coming from DW_AT_producer. */
1028 compile_args
= xstrdup ("-O0 -gdwarf-4"
1029 /* We use -fPIE Otherwise GDB would need to reserve space large enough for
1030 any object file in the inferior in advance to get the final address when
1031 to link the object file to and additionally the default system linker
1032 script would need to be modified so that one can specify there the
1033 absolute target address.
1034 -fPIC is not used at is would require from GDB to generate .got. */
1036 /* We want warnings, except for some commonly happening for GDB commands. */
1038 " -Wno-unused-but-set-variable"
1039 " -Wno-unused-variable"
1040 /* Override CU's possible -fstack-protector-strong. */
1041 " -fno-stack-protector"
1043 set_compile_args (compile_args
, 0, NULL
);
1045 add_setshow_optional_filename_cmd ("compile-gcc", class_support
,
1047 _("Set compile command "
1048 "GCC driver filename."),
1049 _("Show compile command "
1050 "GCC driver filename."),
1052 It should be absolute filename of the gcc executable.\n\
1053 If empty the default target triplet will be searched in $PATH."),
1054 NULL
, show_compile_gcc
, &setlist
,
1056 compile_gcc
= xstrdup ("");