1 /* General Compile and inject code
3 Copyright (C) 2014-2024 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"
44 #include "gdbsupport/gdb_unlinker.h"
45 #include "gdbsupport/pathstuff.h"
46 #include "gdbsupport/scoped_ignore_signal.h"
47 #include "gdbsupport/buildargv.h"
51 /* Initial filename for temporary files. */
53 #define TMP_PREFIX "/tmp/gdbobj-"
55 /* Hold "compile" commands. */
57 static struct cmd_list_element
*compile_command_list
;
59 /* Debug flag for "compile" commands. */
63 /* Object of this type are stored in the compiler's symbol_err_map. */
69 const struct symbol
*sym
;
71 /* The error message to emit. This is malloc'd and owned by the
77 /* An object that maps a gdb type to a gcc type. */
79 struct type_map_instance
85 /* The corresponding gcc type handle. */
87 gcc_type gcc_type_handle
;
90 /* Hash a type_map_instance. */
93 hash_type_map_instance (const void *p
)
95 const struct type_map_instance
*inst
= (const struct type_map_instance
*) p
;
97 return htab_hash_pointer (inst
->type
);
100 /* Check two type_map_instance objects for equality. */
103 eq_type_map_instance (const void *a
, const void *b
)
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
;
108 return insta
->type
== instb
->type
;
111 /* Hash function for struct symbol_error. */
114 hash_symbol_error (const void *a
)
116 const struct symbol_error
*se
= (const struct symbol_error
*) a
;
118 return htab_hash_pointer (se
->sym
);
121 /* Equality function for struct symbol_error. */
124 eq_symbol_error (const void *a
, const void *b
)
126 const struct symbol_error
*sea
= (const struct symbol_error
*) a
;
127 const struct symbol_error
*seb
= (const struct symbol_error
*) b
;
129 return sea
->sym
== seb
->sym
;
132 /* Deletion function for struct symbol_error. */
135 del_symbol_error (void *a
)
137 struct symbol_error
*se
= (struct symbol_error
*) a
;
143 /* Constructor for compile_instance. */
145 compile_instance::compile_instance (struct gcc_base_context
*gcc_fe
,
147 : m_gcc_fe (gcc_fe
), m_gcc_target_options (options
),
148 m_type_map (htab_create_alloc (10, hash_type_map_instance
,
149 eq_type_map_instance
,
150 xfree
, xcalloc
, xfree
)),
151 m_symbol_err_map (htab_create_alloc (10, hash_symbol_error
,
152 eq_symbol_error
, del_symbol_error
,
157 /* See compile-internal.h. */
160 compile_instance::get_cached_type (struct type
*type
, gcc_type
*ret
) const
162 struct type_map_instance inst
, *found
;
165 found
= (struct type_map_instance
*) htab_find (m_type_map
.get (), &inst
);
168 *ret
= found
->gcc_type_handle
;
175 /* See compile-internal.h. */
178 compile_instance::insert_type (struct type
*type
, gcc_type gcc_type
)
180 struct type_map_instance inst
, *add
;
184 inst
.gcc_type_handle
= gcc_type
;
185 slot
= htab_find_slot (m_type_map
.get (), &inst
, INSERT
);
187 add
= (struct type_map_instance
*) *slot
;
188 /* The type might have already been inserted in order to handle
190 if (add
!= NULL
&& add
->gcc_type_handle
!= gcc_type
)
191 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
195 add
= XNEW (struct type_map_instance
);
201 /* See compile-internal.h. */
204 compile_instance::insert_symbol_error (const struct symbol
*sym
,
207 struct symbol_error e
;
211 slot
= htab_find_slot (m_symbol_err_map
.get (), &e
, INSERT
);
214 struct symbol_error
*ep
= XNEW (struct symbol_error
);
217 ep
->message
= xstrdup (text
);
222 /* See compile-internal.h. */
225 compile_instance::error_symbol_once (const struct symbol
*sym
)
227 struct symbol_error search
;
228 struct symbol_error
*err
;
230 if (m_symbol_err_map
== NULL
)
234 err
= (struct symbol_error
*) htab_find (m_symbol_err_map
.get (), &search
);
235 if (err
== NULL
|| err
->message
== NULL
)
238 gdb::unique_xmalloc_ptr
<char> message (err
->message
);
240 error (_("%s"), message
.get ());
243 /* Implement "show debug compile". */
246 show_compile_debug (struct ui_file
*file
, int from_tty
,
247 struct cmd_list_element
*c
, const char *value
)
249 gdb_printf (file
, _("Compile debugging is %s.\n"), value
);
254 /* Options for the compile command. */
256 struct compile_options
262 using compile_flag_option_def
263 = gdb::option::flag_option_def
<compile_options
>;
265 static const gdb::option::option_def compile_command_option_defs
[] = {
267 compile_flag_option_def
{
269 [] (compile_options
*opts
) { return &opts
->raw
; },
270 N_("Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
275 /* Create an option_def_group for the "compile" command's options,
276 with OPTS as context. */
278 static gdb::option::option_def_group
279 make_compile_options_def_group (compile_options
*opts
)
281 return {{compile_command_option_defs
}, opts
};
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. */
289 compile_file_command (const char *args
, int from_tty
)
291 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
293 /* Check if a -raw option is provided. */
295 compile_options options
;
297 const gdb::option::option_def_group group
298 = make_compile_options_def_group (&options
);
299 gdb::option::process_options
300 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
,
303 enum compile_i_scope_types scope
304 = options
.raw
? COMPILE_I_RAW_SCOPE
: COMPILE_I_SIMPLE_SCOPE
;
306 args
= skip_spaces (args
);
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."));
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
);
318 /* Completer for the "compile file" command. */
321 compile_file_command_completer (struct cmd_list_element
*ignore
,
322 completion_tracker
&tracker
,
323 const char *text
, const char *word
)
325 const gdb::option::option_def_group group
326 = make_compile_options_def_group (nullptr);
327 if (gdb::option::complete_options
328 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
331 word
= advance_to_filename_complete_word_point (tracker
, text
);
332 filename_completer (ignore
, tracker
, text
, word
);
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. */
341 compile_code_command (const char *args
, int from_tty
)
343 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
345 compile_options options
;
347 const gdb::option::option_def_group group
348 = make_compile_options_def_group (&options
);
349 gdb::option::process_options
350 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
352 enum compile_i_scope_types scope
353 = options
.raw
? COMPILE_I_RAW_SCOPE
: COMPILE_I_SIMPLE_SCOPE
;
356 eval_compile_command (NULL
, args
, scope
, NULL
);
359 counted_command_line l
= get_command_line (compile_control
, "");
361 l
->control_u
.compile
.scope
= scope
;
362 execute_control_command_untraced (l
.get ());
366 /* Completer for the "compile code" command. */
369 compile_code_command_completer (struct cmd_list_element
*ignore
,
370 completion_tracker
&tracker
,
371 const char *text
, const char *word
)
373 const gdb::option::option_def_group group
374 = make_compile_options_def_group (nullptr);
375 if (gdb::option::complete_options
376 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
379 word
= advance_to_expression_complete_word_point (tracker
, text
);
380 symbol_completer (ignore
, tracker
, text
, word
);
383 /* Callback for compile_print_command. */
386 compile_print_value (struct value
*val
, void *data_voidp
)
388 const value_print_options
*print_opts
= (value_print_options
*) data_voidp
;
390 print_value (val
, *print_opts
);
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. */
399 compile_print_command (const char *arg
, int from_tty
)
401 enum compile_i_scope_types scope
= COMPILE_I_PRINT_ADDRESS_SCOPE
;
402 value_print_options print_opts
;
404 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
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
);
409 gdb::option::process_options
410 (&arg
, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER
, group
);
412 print_command_parse_format (&arg
, "compile print", &print_opts
);
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
419 eval_compile_command (NULL
, arg
, scope
, &print_opts
);
422 counted_command_line l
= get_command_line (compile_control
, "");
424 l
->control_u
.compile
.scope
= scope
;
425 l
->control_u
.compile
.scope_data
= &print_opts
;
426 execute_control_command_untraced (l
.get ());
430 /* A cleanup function to remove a directory and all its contents. */
435 const char *dir
= (const char *) arg
;
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
);
447 /* Return the name of the temporary directory to use for .o files, and
448 arrange for the directory to be removed at shutdown. */
451 get_compile_file_tempdir (void)
453 static char *tempdir_name
;
455 #define TEMPLATE TMP_PREFIX "XXXXXX"
456 char tname
[sizeof (TEMPLATE
)];
458 if (tempdir_name
!= NULL
)
461 strcpy (tname
, TEMPLATE
);
463 tempdir_name
= mkdtemp (tname
);
464 if (tempdir_name
== NULL
)
465 perror_with_name (_("Could not make temporary directory"));
467 tempdir_name
= xstrdup (tempdir_name
);
468 make_final_cleanup (do_rmdir
, tempdir_name
);
472 /* Compute the names of source and object files to use. */
474 static compile_file_names
475 get_new_file_names ()
478 const char *dir
= get_compile_file_tempdir ();
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
));
488 /* Get the block and PC at which to evaluate an expression. */
490 static const struct block
*
491 get_expr_block_and_pc (CORE_ADDR
*pc
)
493 const struct block
*block
= get_selected_block (pc
);
497 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
500 block
= cursal
.symtab
->compunit ()->blockvector ()->static_block ();
503 *pc
= block
->entry_pc ();
506 *pc
= block
->entry_pc ();
511 /* String for 'set compile-args' and 'show compile-args'. */
512 static std::string compile_args
=
513 /* Override flags possibly coming from DW_AT_producer. */
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. */
522 /* We want warnings, except for some commonly happening for GDB commands. */
524 " -Wno-unused-but-set-variable"
525 " -Wno-unused-variable"
526 /* Override CU's possible -fstack-protector-strong. */
527 " -fno-stack-protector";
529 /* Parsed form of COMPILE_ARGS. */
530 static gdb_argv compile_args_argv
;
532 /* Implement 'set compile-args'. */
535 set_compile_args (const char *args
, int from_tty
, struct cmd_list_element
*c
)
537 compile_args_argv
= gdb_argv (compile_args
.c_str ());
540 /* Implement 'show compile-args'. */
543 show_compile_args (struct ui_file
*file
, int from_tty
,
544 struct cmd_list_element
*c
, const char *value
)
546 gdb_printf (file
, _("Compile command command-line arguments "
551 /* String for 'set compile-gcc' and 'show compile-gcc'. */
552 static std::string compile_gcc
;
554 /* Implement 'show compile-gcc'. */
557 show_compile_gcc (struct ui_file
*file
, int from_tty
,
558 struct cmd_list_element
*c
, const char *value
)
560 gdb_printf (file
, _("Compile command GCC driver filename is \"%s\".\n"),
564 /* Return DW_AT_producer parsed for get_selected_frame () (if any).
565 Return NULL otherwise.
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. */
571 get_selected_pc_producer_options (void)
573 CORE_ADDR pc
= get_frame_pc (get_selected_frame (NULL
));
574 struct compunit_symtab
*symtab
= find_pc_compunit_symtab (pc
);
577 if (symtab
== NULL
|| symtab
->producer () == NULL
578 || !startswith (symtab
->producer (), "GNU "))
581 cs
= symtab
->producer ();
582 while (*cs
!= 0 && *cs
!= '-')
583 cs
= skip_spaces (skip_to_space (cs
));
589 /* Filter out unwanted options from ARGV. */
592 filter_args (char **argv
)
596 for (destv
= argv
; *argv
!= NULL
; argv
++)
598 /* -fpreprocessed may get in commonly from ccache. */
599 if (strcmp (*argv
, "-fpreprocessed") == 0)
609 /* Produce final vector of GCC compilation options.
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.
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
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.
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
630 get_args (const compile_instance
*compiler
, struct gdbarch
*gdbarch
)
632 const char *cs_producer_options
;
635 std::string gcc_options
= gdbarch_gcc_target_options (gdbarch
);
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 ());
642 cs_producer_options
= get_selected_pc_producer_options ();
643 if (cs_producer_options
!= NULL
)
645 gdb_argv
argv_producer (cs_producer_options
);
646 filter_args (argv_producer
.get ());
648 result
.append (std::move (argv_producer
));
651 result
.append (gdb_argv (compiler
->gcc_target_options ().c_str ()));
652 result
.append (compile_args_argv
);
657 /* A helper function suitable for use as the "print_callback" in the
661 print_callback (void *ignore
, const char *message
)
663 gdb_puts (message
, gdb_stderr
);
666 /* Process the compilation request. On success it returns the object
667 and source file names. On an error condition, error () is
670 static compile_file_names
671 compile_to_object (struct command_line
*cmd
, const char *cmd_string
,
672 enum compile_i_scope_types scope
)
674 const struct block
*expr_block
;
675 CORE_ADDR trash_pc
, expr_pc
;
677 struct gdbarch
*gdbarch
= get_current_arch ();
678 std::string triplet_rx
;
680 if (!target_has_execution ())
681 error (_("The program must be running for the compile command to "\
684 expr_block
= get_expr_block_and_pc (&trash_pc
);
685 expr_pc
= get_frame_address_in_block (get_selected_frame (NULL
));
687 /* Set up instance and context for the compiler. */
688 std::unique_ptr
<compile_instance
> compiler
689 = current_language
->get_compile_instance ();
690 if (compiler
== nullptr)
691 error (_("No compiler support for language %s."),
692 current_language
->name ());
693 compiler
->set_print_callback (print_callback
, NULL
);
694 compiler
->set_scope (scope
);
695 compiler
->set_block (expr_block
);
697 /* From the provided expression, build a scope to pass to the
700 string_file input_buf
;
705 struct command_line
*iter
;
707 for (iter
= cmd
->body_list_0
.get (); iter
; iter
= iter
->next
)
709 input_buf
.puts (iter
->line
);
710 input_buf
.puts ("\n");
713 input
= input_buf
.c_str ();
715 else if (cmd_string
!= NULL
)
718 error (_("Neither a simple expression, or a multi-line specified."));
721 = current_language
->compute_program (compiler
.get (), input
, gdbarch
,
722 expr_block
, expr_pc
);
724 gdb_printf (gdb_stdlog
, "debug output:\n\n%s", code
.c_str ());
726 compiler
->set_verbose (compile_debug
);
728 if (!compile_gcc
.empty ())
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)"));
734 compiler
->set_driver_filename (compile_gcc
.c_str ());
738 const char *os_rx
= osabi_triplet_regexp (gdbarch_osabi (gdbarch
));
739 const char *arch_rx
= gdbarch_gnu_triplet_regexp (gdbarch
);
741 /* Allow triplets with or without vendor set. */
742 triplet_rx
= std::string (arch_rx
) + "(-[^-]*)?-";
743 if (os_rx
!= nullptr)
745 compiler
->set_triplet_regexp (triplet_rx
.c_str ());
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 ();
753 gdb::unique_xmalloc_ptr
<char> error_message
754 = compiler
->set_arguments (argc
, argv
, triplet_rx
.c_str ());
756 if (error_message
!= NULL
)
757 error ("%s", error_message
.get ());
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",
769 compile_file_names fnames
= get_new_file_names ();
771 std::optional
<gdb::unlinker
> source_remover
;
774 gdb_file_up src
= gdb_fopen_cloexec (fnames
.source_file (), "w");
776 perror_with_name (_("Could not open source file for writing"));
778 source_remover
.emplace (fnames
.source_file ());
780 if (fputs (code
.c_str (), src
.get ()) == EOF
)
781 perror_with_name (_("Could not write to source file"));
785 gdb_printf (gdb_stdlog
, "source file produced: %s\n\n",
786 fnames
.source_file ());
788 /* If we don't do this, then GDB simply exits
789 when the compiler dies. */
790 scoped_ignore_sigpipe ignore_sigpipe
;
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
);
796 error (_("Compilation failed."));
799 gdb_printf (gdb_stdlog
, "object file produced: %s\n\n",
800 fnames
.object_file ());
802 /* Keep the source file. */
803 source_remover
->keep ();
807 /* The "compile" prefix command. */
810 compile_command (const char *args
, int from_tty
)
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
);
820 eval_compile_command (struct command_line
*cmd
, const char *cmd_string
,
821 enum compile_i_scope_types scope
, void *scope_data
)
823 compile_file_names fnames
= compile_to_object (cmd
, cmd_string
, scope
);
825 gdb::unlinker
object_remover (fnames
.object_file ());
826 gdb::unlinker
source_remover (fnames
.source_file ());
828 compile_module_up compile_module
= compile_object_load (fnames
, scope
,
830 if (compile_module
== NULL
)
832 gdb_assert (scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
);
833 eval_compile_command (cmd
, cmd_string
,
834 COMPILE_I_PRINT_VALUE_SCOPE
, scope_data
);
838 /* Keep the files. */
839 source_remover
.keep ();
840 object_remover
.keep ();
842 compile_object_run (std::move (compile_module
));
845 /* See compile/compile-internal.h. */
848 compile_register_name_mangled (struct gdbarch
*gdbarch
, int regnum
)
850 const char *regname
= gdbarch_register_name (gdbarch
, regnum
);
852 return string_printf ("__%s", regname
);
855 /* See compile/compile-internal.h. */
858 compile_register_name_demangle (struct gdbarch
*gdbarch
,
863 if (regname
[0] != '_' || regname
[1] != '_')
864 error (_("Invalid register name \"%s\"."), regname
);
867 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
868 if (strcmp (regname
, gdbarch_register_name (gdbarch
, regnum
)) == 0)
871 error (_("Cannot find gdbarch register \"%s\"."), regname
);
874 /* Forwards to the plug-in. */
876 #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
878 /* See compile-internal.h. */
881 compile_instance::set_print_callback
882 (void (*print_function
) (void *, const char *), void *datum
)
884 FORWARD (set_print_callback
, print_function
, datum
);
887 /* See compile-internal.h. */
890 compile_instance::version () const
892 return m_gcc_fe
->ops
->version
;
895 /* See compile-internal.h. */
898 compile_instance::set_verbose (int level
)
900 if (version () >= GCC_FE_VERSION_1
)
901 FORWARD (set_verbose
, level
);
904 /* See compile-internal.h. */
907 compile_instance::set_driver_filename (const char *filename
)
909 if (version () >= GCC_FE_VERSION_1
)
910 FORWARD (set_driver_filename
, filename
);
913 /* See compile-internal.h. */
916 compile_instance::set_triplet_regexp (const char *regexp
)
918 if (version () >= GCC_FE_VERSION_1
)
919 FORWARD (set_triplet_regexp
, regexp
);
922 /* See compile-internal.h. */
924 gdb::unique_xmalloc_ptr
<char>
925 compile_instance::set_arguments (int argc
, char **argv
, const char *regexp
)
927 if (version () >= GCC_FE_VERSION_1
)
928 return gdb::unique_xmalloc_ptr
<char> (FORWARD (set_arguments
, argc
, argv
));
930 return gdb::unique_xmalloc_ptr
<char> (FORWARD (set_arguments_v0
, regexp
,
934 /* See compile-internal.h. */
937 compile_instance::set_source_file (const char *filename
)
939 FORWARD (set_source_file
, filename
);
942 /* See compile-internal.h. */
945 compile_instance::compile (const char *filename
, int verbose_level
)
947 if (version () >= GCC_FE_VERSION_1
)
948 return FORWARD (compile
, filename
);
950 return FORWARD (compile_v0
, filename
, verbose_level
);
956 cmd_list_element
*compile_cmd_element
= nullptr;
958 void _initialize_compile ();
960 _initialize_compile ()
962 struct cmd_list_element
*c
= NULL
;
964 compile_cmd_element
= add_prefix_cmd ("compile", class_obscure
,
965 compile_command
, _("\
966 Command to compile source code and inject it into the inferior."),
967 &compile_command_list
, 1, &cmdlist
);
968 add_com_alias ("expression", compile_cmd_element
, class_obscure
, 0);
970 const auto compile_opts
= make_compile_options_def_group (nullptr);
972 static const std::string compile_code_help
973 = gdb::option::build_help (_("\
974 Compile, inject, and execute code.\n\
976 Usage: compile code [OPTION]... [CODE]\n\
981 The source code may be specified as a simple one line expression, e.g.:\n\
983 compile code printf(\"Hello world\\n\");\n\
985 Alternatively, you can type a multiline expression by invoking\n\
986 this command with no argument. GDB will then prompt for the\n\
987 expression interactively; type a line containing \"end\" to\n\
988 indicate the end of the expression."),
991 c
= add_cmd ("code", class_obscure
, compile_code_command
,
992 compile_code_help
.c_str (),
993 &compile_command_list
);
994 set_cmd_completer_handle_brkchars (c
, compile_code_command_completer
);
996 static const std::string compile_file_help
997 = gdb::option::build_help (_("\
998 Evaluate a file containing source code.\n\
1000 Usage: compile file [OPTION].. [FILENAME]\n\
1006 c
= add_cmd ("file", class_obscure
, compile_file_command
,
1007 compile_file_help
.c_str (),
1008 &compile_command_list
);
1009 set_cmd_completer_handle_brkchars (c
, compile_file_command_completer
);
1011 const auto compile_print_opts
= make_value_print_options_def_group (nullptr);
1013 static const std::string compile_print_help
1014 = gdb::option::build_help (_("\
1015 Evaluate EXPR by using the compiler and print result.\n\
1017 Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
1022 Note: because this command accepts arbitrary expressions, if you\n\
1023 specify any command option, you must use a double dash (\"--\")\n\
1024 to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
1026 The expression may be specified on the same line as the command, e.g.:\n\
1030 Alternatively, you can type a multiline expression by invoking\n\
1031 this command with no argument. GDB will then prompt for the\n\
1032 expression interactively; type a line containing \"end\" to\n\
1033 indicate the end of the expression.\n\
1035 EXPR may be preceded with /FMT, where FMT is a format letter\n\
1036 but no count or size letter (see \"x\" command)."),
1037 compile_print_opts
);
1039 c
= add_cmd ("print", class_obscure
, compile_print_command
,
1040 compile_print_help
.c_str (),
1041 &compile_command_list
);
1042 set_cmd_completer_handle_brkchars (c
, print_command_completer
);
1044 add_setshow_boolean_cmd ("compile", class_maintenance
, &compile_debug
, _("\
1045 Set compile command debugging."), _("\
1046 Show compile command debugging."), _("\
1047 When on, compile command debugging is enabled."),
1048 NULL
, show_compile_debug
,
1049 &setdebuglist
, &showdebuglist
);
1051 add_setshow_string_cmd ("compile-args", class_support
,
1053 _("Set compile command GCC command-line arguments."),
1054 _("Show compile command GCC command-line arguments."),
1056 Use options like -I (include file directory) or ABI settings.\n\
1057 String quoting is parsed like in shell, for example:\n\
1058 -mno-align-double \"-I/dir with a space/include\""),
1059 set_compile_args
, show_compile_args
, &setlist
, &showlist
);
1062 /* Initialize compile_args_argv. */
1063 set_compile_args (compile_args
.c_str (), 0, NULL
);
1065 add_setshow_optional_filename_cmd ("compile-gcc", class_support
,
1067 _("Set compile command "
1068 "GCC driver filename."),
1069 _("Show compile command "
1070 "GCC driver filename."),
1072 It should be absolute filename of the gcc executable.\n\
1073 If empty the default target triplet will be searched in $PATH."),
1074 NULL
, show_compile_gcc
, &setlist
,