Home | History | Annotate | Line # | Download | only in compile
compile.c revision 1.10
      1 /* General Compile and inject code
      2 
      3    Copyright (C) 2014-2024 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 "progspace.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 "cli/cli-cmds.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 <optional>
     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 
     52 /* Initial filename for temporary files.  */
     53 
     54 #define TMP_PREFIX "/tmp/gdbobj-"
     55 
     56 /* Hold "compile" commands.  */
     57 
     58 static struct cmd_list_element *compile_command_list;
     59 
     60 /* Debug flag for "compile" commands.  */
     61 
     62 bool compile_debug;
     63 
     64 /* See compile-internal.h.  */
     65 
     66 bool
     67 compile_instance::get_cached_type (struct type *type, gcc_type *ret) const
     68 {
     69   if (auto iter = m_type_map.find (type);
     70       iter != m_type_map.end ())
     71     {
     72       *ret = iter->second;
     73       return true;
     74     }
     75 
     76   return false;
     77 }
     78 
     79 /* See compile-internal.h.  */
     80 
     81 void
     82 compile_instance::insert_type (struct type *type, gcc_type gcc_type)
     83 {
     84   auto [it, inserted] = m_type_map.emplace (type, gcc_type);
     85 
     86   /* The type might have already been inserted in order to handle
     87      recursive types.  */
     88   if (!inserted && it->second != gcc_type)
     89     error (_("Unexpected type id from GCC, check you use recent enough GCC."));
     90 }
     91 
     92 /* See compile-internal.h.  */
     93 
     94 void
     95 compile_instance::insert_symbol_error (const struct symbol *sym,
     96 				       const char *text)
     97 {
     98   m_symbol_err_map.emplace (sym, text);
     99 }
    100 
    101 /* See compile-internal.h.  */
    102 
    103 void
    104 compile_instance::error_symbol_once (const struct symbol *sym)
    105 {
    106   if (auto iter = m_symbol_err_map.find (sym);
    107       iter != m_symbol_err_map.end () && !iter->second.empty ())
    108     {
    109       std::string message = std::move (iter->second);
    110       error (_("%s"), message.c_str ());
    111     }
    112 }
    113 
    114 /* Implement "show debug compile".  */
    115 
    116 static void
    117 show_compile_debug (struct ui_file *file, int from_tty,
    118 		    struct cmd_list_element *c, const char *value)
    119 {
    120   gdb_printf (file, _("Compile debugging is %s.\n"), value);
    121 }
    122 
    123 
    124 
    126 /* Options for the compile command.  */
    127 
    128 struct compile_options
    129 {
    130   /* For -raw.  */
    131   bool raw = false;
    132 };
    133 
    134 using compile_flag_option_def
    135   = gdb::option::flag_option_def<compile_options>;
    136 
    137 static const gdb::option::option_def compile_command_option_defs[] = {
    138 
    139   compile_flag_option_def {
    140     "raw",
    141     [] (compile_options *opts) { return &opts->raw; },
    142     N_("Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
    143   },
    144 
    145 };
    146 
    147 /* Create an option_def_group for the "compile" command's options,
    148    with OPTS as context.  */
    149 
    150 static gdb::option::option_def_group
    151 make_compile_options_def_group (compile_options *opts)
    152 {
    153   return {{compile_command_option_defs}, opts};
    154 }
    155 
    156 /* Handle the input from the 'compile file' command.  The "compile
    157    file" command is used to evaluate an expression contained in a file
    158    that may contain calls to the GCC compiler.  */
    159 
    160 static void
    161 compile_file_command (const char *args, int from_tty)
    162 {
    163   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    164 
    165   /* Check if a -raw option is provided.  */
    166 
    167   compile_options options;
    168 
    169   const gdb::option::option_def_group group
    170     = make_compile_options_def_group (&options);
    171   gdb::option::process_options
    172     (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR,
    173      group);
    174 
    175   enum compile_i_scope_types scope
    176     = options.raw ? COMPILE_I_RAW_SCOPE : COMPILE_I_SIMPLE_SCOPE;
    177 
    178   std::string filename = extract_single_filename_arg (args);
    179 
    180   /* After processing options, check whether we have a filename.  */
    181   if (filename.empty ())
    182     error (_("You must provide a filename for this command."));
    183 
    184   std::string abspath = gdb_abspath (filename.c_str ());
    185   std::string buffer = string_printf ("#include \"%s\"\n", abspath.c_str ());
    186   eval_compile_command (NULL, buffer.c_str (), scope, NULL);
    187 }
    188 
    189 /* Completer for the "compile file" command.  */
    190 
    191 static void
    192 compile_file_command_completer (struct cmd_list_element *ignore,
    193 				completion_tracker &tracker,
    194 				const char *text, const char *word)
    195 {
    196   const gdb::option::option_def_group group
    197     = make_compile_options_def_group (nullptr);
    198   if (gdb::option::complete_options
    199       (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
    200     return;
    201 
    202   word = advance_to_filename_maybe_quoted_complete_word_point (tracker, text);
    203   filename_maybe_quoted_completer (ignore, tracker, text, word);
    204 }
    205 
    206 /* Handle the input from the 'compile code' command.  The
    207    "compile code" command is used to evaluate an expression that may
    208    contain calls to the GCC compiler.  The language expected in this
    209    compile command is the language currently set in GDB.  */
    210 
    211 static void
    212 compile_code_command (const char *args, int from_tty)
    213 {
    214   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    215 
    216   compile_options options;
    217 
    218   const gdb::option::option_def_group group
    219     = make_compile_options_def_group (&options);
    220   gdb::option::process_options
    221     (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
    222 
    223   enum compile_i_scope_types scope
    224     = options.raw ? COMPILE_I_RAW_SCOPE : COMPILE_I_SIMPLE_SCOPE;
    225 
    226   if (args && *args)
    227     eval_compile_command (NULL, args, scope, NULL);
    228   else
    229     {
    230       counted_command_line l = get_command_line (compile_control, "");
    231 
    232       l->control_u.compile.scope = scope;
    233       execute_control_command_untraced (l.get ());
    234     }
    235 }
    236 
    237 /* Completer for the "compile code" command.  */
    238 
    239 static void
    240 compile_code_command_completer (struct cmd_list_element *ignore,
    241 				completion_tracker &tracker,
    242 				const char *text, const char *word)
    243 {
    244   const gdb::option::option_def_group group
    245     = make_compile_options_def_group (nullptr);
    246   if (gdb::option::complete_options
    247       (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
    248     return;
    249 
    250   word = advance_to_expression_complete_word_point (tracker, text);
    251   symbol_completer (ignore, tracker, text, word);
    252 }
    253 
    254 /* Callback for compile_print_command.  */
    255 
    256 void
    257 compile_print_value (struct value *val, void *data_voidp)
    258 {
    259   const value_print_options *print_opts = (value_print_options *) data_voidp;
    260 
    261   print_value (val, *print_opts);
    262 }
    263 
    264 /* Handle the input from the 'compile print' command.  The "compile
    265    print" command is used to evaluate and print an expression that may
    266    contain calls to the GCC compiler.  The language expected in this
    267    compile command is the language currently set in GDB.  */
    268 
    269 static void
    270 compile_print_command (const char *arg, int from_tty)
    271 {
    272   enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
    273   value_print_options print_opts;
    274 
    275   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    276 
    277   get_user_print_options (&print_opts);
    278   /* Override global settings with explicit options, if any.  */
    279   auto group = make_value_print_options_def_group (&print_opts);
    280   gdb::option::process_options
    281     (&arg, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
    282 
    283   print_command_parse_format (&arg, "compile print", &print_opts);
    284 
    285   /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup
    286      will not touch the stale pointer if compile_object_run has
    287      already quit.  */
    288 
    289   if (arg && *arg)
    290     eval_compile_command (NULL, arg, scope, &print_opts);
    291   else
    292     {
    293       counted_command_line l = get_command_line (compile_control, "");
    294 
    295       l->control_u.compile.scope = scope;
    296       l->control_u.compile.scope_data = &print_opts;
    297       execute_control_command_untraced (l.get ());
    298     }
    299 }
    300 
    301 /* Return the name of the temporary directory to use for .o files, and
    302    arrange for the directory to be removed at shutdown.  */
    303 
    304 static const char *
    305 get_compile_file_tempdir (void)
    306 {
    307   static char *tempdir_name;
    308 
    309 #define TEMPLATE TMP_PREFIX "XXXXXX"
    310   char tname[sizeof (TEMPLATE)];
    311 
    312   if (tempdir_name != NULL)
    313     return tempdir_name;
    314 
    315   strcpy (tname, TEMPLATE);
    316 #undef TEMPLATE
    317   tempdir_name = mkdtemp (tname);
    318   if (tempdir_name == NULL)
    319     perror_with_name (_("Could not make temporary directory"));
    320 
    321   tempdir_name = xstrdup (tempdir_name);
    322   add_final_cleanup ([] ()
    323     {
    324       char *zap;
    325       int wstat;
    326 
    327       gdb_assert (startswith (tempdir_name, TMP_PREFIX));
    328       zap = concat ("rm -rf ", tempdir_name, (char *) NULL);
    329       wstat = system (zap);
    330       if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
    331 	warning (_("Could not remove temporary directory %s"), tempdir_name);
    332       XDELETEVEC (zap);
    333     });
    334   return tempdir_name;
    335 }
    336 
    337 /* Compute the names of source and object files to use.  */
    338 
    339 static compile_file_names
    340 get_new_file_names ()
    341 {
    342   static int seq;
    343   const char *dir = get_compile_file_tempdir ();
    344 
    345   ++seq;
    346 
    347   return compile_file_names (string_printf ("%s%sout%d.c",
    348 					    dir, SLASH_STRING, seq),
    349 			     string_printf ("%s%sout%d.o",
    350 					    dir, SLASH_STRING, seq));
    351 }
    352 
    353 /* Get the block and PC at which to evaluate an expression.  */
    354 
    355 static const struct block *
    356 get_expr_block_and_pc (CORE_ADDR *pc)
    357 {
    358   const struct block *block = get_selected_block (pc);
    359 
    360   if (block == NULL)
    361     {
    362       symtab_and_line cursal
    363 	= get_current_source_symtab_and_line (current_program_space);
    364 
    365       if (cursal.symtab)
    366 	block = cursal.symtab->compunit ()->blockvector ()->static_block ();
    367 
    368       if (block != NULL)
    369 	*pc = block->entry_pc ();
    370     }
    371   else
    372     *pc = block->entry_pc ();
    373 
    374   return block;
    375 }
    376 
    377 /* String for 'set compile-args' and 'show compile-args'.  */
    378 static std::string compile_args =
    379   /* Override flags possibly coming from DW_AT_producer.  */
    380   "-O0 -gdwarf-4"
    381   /* We use -fPIE Otherwise GDB would need to reserve space large enough for
    382      any object file in the inferior in advance to get the final address when
    383      to link the object file to and additionally the default system linker
    384      script would need to be modified so that one can specify there the
    385      absolute target address.
    386      -fPIC is not used at is would require from GDB to generate .got.  */
    387   " -fPIE"
    388   /* We want warnings, except for some commonly happening for GDB commands.  */
    389   " -Wall "
    390   " -Wno-unused-but-set-variable"
    391   " -Wno-unused-variable"
    392   /* Override CU's possible -fstack-protector-strong.  */
    393   " -fno-stack-protector";
    394 
    395 /* Parsed form of COMPILE_ARGS.  */
    396 static gdb_argv compile_args_argv;
    397 
    398 /* Implement 'set compile-args'.  */
    399 
    400 static void
    401 set_compile_args (const char *args, int from_tty, struct cmd_list_element *c)
    402 {
    403   compile_args_argv = gdb_argv (compile_args.c_str ());
    404 }
    405 
    406 /* Implement 'show compile-args'.  */
    407 
    408 static void
    409 show_compile_args (struct ui_file *file, int from_tty,
    410 		   struct cmd_list_element *c, const char *value)
    411 {
    412   gdb_printf (file, _("Compile command command-line arguments "
    413 		      "are \"%s\".\n"),
    414 	      value);
    415 }
    416 
    417 /* String for 'set compile-gcc' and 'show compile-gcc'.  */
    418 static std::string compile_gcc;
    419 
    420 /* Implement 'show compile-gcc'.  */
    421 
    422 static void
    423 show_compile_gcc (struct ui_file *file, int from_tty,
    424 		  struct cmd_list_element *c, const char *value)
    425 {
    426   gdb_printf (file, _("Compile command GCC driver filename is \"%s\".\n"),
    427 	      value);
    428 }
    429 
    430 /* Return DW_AT_producer parsed for get_selected_frame () (if any).
    431    Return NULL otherwise.
    432 
    433    GCC already filters its command-line arguments only for the suitable ones to
    434    put into DW_AT_producer - see GCC function gen_producer_string.  */
    435 
    436 static const char *
    437 get_selected_pc_producer_options (void)
    438 {
    439   CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
    440   struct compunit_symtab *symtab = find_pc_compunit_symtab (pc);
    441   const char *cs;
    442 
    443   if (symtab == NULL || symtab->producer () == NULL
    444       || !startswith (symtab->producer (), "GNU "))
    445     return NULL;
    446 
    447   cs = symtab->producer ();
    448   while (*cs != 0 && *cs != '-')
    449     cs = skip_spaces (skip_to_space (cs));
    450   if (*cs != '-')
    451     return NULL;
    452   return cs;
    453 }
    454 
    455 /* Filter out unwanted options from ARGV.  */
    456 
    457 static void
    458 filter_args (char **argv)
    459 {
    460   char **destv;
    461 
    462   for (destv = argv; *argv != NULL; argv++)
    463     {
    464       /* -fpreprocessed may get in commonly from ccache.  */
    465       if (strcmp (*argv, "-fpreprocessed") == 0)
    466 	{
    467 	  xfree (*argv);
    468 	  continue;
    469 	}
    470       *destv++ = *argv;
    471     }
    472   *destv = NULL;
    473 }
    474 
    475 /* Produce final vector of GCC compilation options.
    476 
    477    The first element of the combined argument vector are arguments
    478    relating to the target size ("-m64", "-m32" etc.).  These are
    479    sourced from the inferior's architecture.
    480 
    481    The second element of the combined argument vector are arguments
    482    stored in the inferior DW_AT_producer section.  If these are stored
    483    in the inferior (there is no guarantee that they are), they are
    484    added to the vector.
    485 
    486    The third element of the combined argument vector are argument
    487    supplied by the language implementation provided by
    488    compile-{lang}-support.  These contain language specific arguments.
    489 
    490    The final element of the combined argument vector are arguments
    491    supplied by the "set compile-args" command.  These are always
    492    appended last so as to override any of the arguments automatically
    493    generated above.  */
    494 
    495 static gdb_argv
    496 get_args (const compile_instance *compiler, struct gdbarch *gdbarch)
    497 {
    498   const char *cs_producer_options;
    499   gdb_argv result;
    500 
    501   std::string gcc_options = gdbarch_gcc_target_options (gdbarch);
    502 
    503   /* Make sure we have a non-empty set of options, otherwise GCC will
    504      error out trying to look for a filename that is an empty string.  */
    505   if (!gcc_options.empty ())
    506     result = gdb_argv (gcc_options.c_str ());
    507 
    508   cs_producer_options = get_selected_pc_producer_options ();
    509   if (cs_producer_options != NULL)
    510     {
    511       gdb_argv argv_producer (cs_producer_options);
    512       filter_args (argv_producer.get ());
    513 
    514       result.append (std::move (argv_producer));
    515     }
    516 
    517   result.append (gdb_argv (compiler->gcc_target_options ().c_str ()));
    518   result.append (compile_args_argv);
    519 
    520   return result;
    521 }
    522 
    523 /* A helper function suitable for use as the "print_callback" in the
    524    compiler object.  */
    525 
    526 static void
    527 print_callback (void *ignore, const char *message)
    528 {
    529   gdb_puts (message, gdb_stderr);
    530 }
    531 
    532 /* Process the compilation request.  On success it returns the object
    533    and source file names.  On an error condition, error () is
    534    called.  */
    535 
    536 static compile_file_names
    537 compile_to_object (struct command_line *cmd, const char *cmd_string,
    538 		   enum compile_i_scope_types scope)
    539 {
    540   const struct block *expr_block;
    541   CORE_ADDR trash_pc, expr_pc;
    542   int ok;
    543   struct gdbarch *gdbarch = get_current_arch ();
    544   std::string triplet_rx;
    545 
    546   if (!target_has_execution ())
    547     error (_("The program must be running for the compile command to "\
    548 	     "work."));
    549 
    550   expr_block = get_expr_block_and_pc (&trash_pc);
    551   expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
    552 
    553   /* Set up instance and context for the compiler.  */
    554   std::unique_ptr<compile_instance> compiler
    555     = current_language->get_compile_instance ();
    556   if (compiler == nullptr)
    557     error (_("No compiler support for language %s."),
    558 	   current_language->name ());
    559   compiler->set_print_callback (print_callback, NULL);
    560   compiler->set_scope (scope);
    561   compiler->set_block (expr_block);
    562 
    563   /* From the provided expression, build a scope to pass to the
    564      compiler.  */
    565 
    566   string_file input_buf;
    567   const char *input;
    568 
    569   if (cmd != NULL)
    570     {
    571       struct command_line *iter;
    572 
    573       for (iter = cmd->body_list_0.get (); iter; iter = iter->next)
    574 	{
    575 	  input_buf.puts (iter->line);
    576 	  input_buf.puts ("\n");
    577 	}
    578 
    579       input = input_buf.c_str ();
    580     }
    581   else if (cmd_string != NULL)
    582     input = cmd_string;
    583   else
    584     error (_("Neither a simple expression, or a multi-line specified."));
    585 
    586   std::string code
    587     = current_language->compute_program (compiler.get (), input, gdbarch,
    588 					 expr_block, expr_pc);
    589   if (compile_debug)
    590     gdb_printf (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
    591 
    592   compiler->set_verbose (compile_debug);
    593 
    594   if (!compile_gcc.empty ())
    595     {
    596       if (compiler->version () < GCC_FE_VERSION_1)
    597 	error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
    598 		 "(libcc1 interface version 1 or higher)"));
    599 
    600       compiler->set_driver_filename (compile_gcc.c_str ());
    601     }
    602   else
    603     {
    604       const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
    605       const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
    606 
    607       /* Allow triplets with or without vendor set.  */
    608       triplet_rx = std::string (arch_rx) + "(-[^-]*)?-";
    609       if (os_rx != nullptr)
    610 	triplet_rx += os_rx;
    611       compiler->set_triplet_regexp (triplet_rx.c_str ());
    612     }
    613 
    614   /* Set compiler command-line arguments.  */
    615   gdb_argv argv_holder = get_args (compiler.get (), gdbarch);
    616   int argc = argv_holder.count ();
    617   char **argv = argv_holder.get ();
    618 
    619   gdb::unique_xmalloc_ptr<char> error_message
    620     = compiler->set_arguments (argc, argv, triplet_rx.c_str ());
    621 
    622   if (error_message != NULL)
    623     error ("%s", error_message.get ());
    624 
    625   if (compile_debug)
    626     {
    627       int argi;
    628 
    629       gdb_printf (gdb_stdlog, "Passing %d compiler options:\n", argc);
    630       for (argi = 0; argi < argc; argi++)
    631 	gdb_printf (gdb_stdlog, "Compiler option %d: <%s>\n",
    632 		    argi, argv[argi]);
    633     }
    634 
    635   compile_file_names fnames = get_new_file_names ();
    636 
    637   std::optional<gdb::unlinker> source_remover;
    638 
    639   {
    640     gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w");
    641     if (src == NULL)
    642       perror_with_name (_("Could not open source file for writing"));
    643 
    644     source_remover.emplace (fnames.source_file ());
    645 
    646     if (fputs (code.c_str (), src.get ()) == EOF)
    647       perror_with_name (_("Could not write to source file"));
    648   }
    649 
    650   if (compile_debug)
    651     gdb_printf (gdb_stdlog, "source file produced: %s\n\n",
    652 		fnames.source_file ());
    653 
    654   /* If we don't do this, then GDB simply exits
    655      when the compiler dies.  */
    656   scoped_ignore_sigpipe ignore_sigpipe;
    657 
    658   /* Call the compiler and start the compilation process.  */
    659   compiler->set_source_file (fnames.source_file ());
    660   ok = compiler->compile (fnames.object_file (), compile_debug);
    661   if (!ok)
    662     error (_("Compilation failed."));
    663 
    664   if (compile_debug)
    665     gdb_printf (gdb_stdlog, "object file produced: %s\n\n",
    666 		fnames.object_file ());
    667 
    668   /* Keep the source file.  */
    669   source_remover->keep ();
    670   return fnames;
    671 }
    672 
    673 /* The "compile" prefix command.  */
    674 
    675 static void
    676 compile_command (const char *args, int from_tty)
    677 {
    678   /* If a sub-command is not specified to the compile prefix command,
    679      assume it is a direct code compilation.  */
    680   compile_code_command (args, from_tty);
    681 }
    682 
    683 /* See compile.h.  */
    684 
    685 void
    686 eval_compile_command (struct command_line *cmd, const char *cmd_string,
    687 		      enum compile_i_scope_types scope, void *scope_data)
    688 {
    689   compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
    690 
    691   gdb::unlinker object_remover (fnames.object_file ());
    692   gdb::unlinker source_remover (fnames.source_file ());
    693 
    694   compile_module_up compile_module = compile_object_load (fnames, scope,
    695 							  scope_data);
    696   if (compile_module == NULL)
    697     {
    698       gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
    699       eval_compile_command (cmd, cmd_string,
    700 			    COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
    701       return;
    702     }
    703 
    704   /* Keep the files.  */
    705   source_remover.keep ();
    706   object_remover.keep ();
    707 
    708   compile_object_run (std::move (compile_module));
    709 }
    710 
    711 /* See compile/compile-internal.h.  */
    712 
    713 std::string
    714 compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
    715 {
    716   const char *regname = gdbarch_register_name (gdbarch, regnum);
    717 
    718   return string_printf ("__%s", regname);
    719 }
    720 
    721 /* See compile/compile-internal.h.  */
    722 
    723 int
    724 compile_register_name_demangle (struct gdbarch *gdbarch,
    725 				 const char *regname)
    726 {
    727   int regnum;
    728 
    729   if (regname[0] != '_' || regname[1] != '_')
    730     error (_("Invalid register name \"%s\"."), regname);
    731   regname += 2;
    732 
    733   for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
    734     if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
    735       return regnum;
    736 
    737   error (_("Cannot find gdbarch register \"%s\"."), regname);
    738 }
    739 
    740 /* Forwards to the plug-in.  */
    741 
    742 #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
    743 
    744 /* See compile-internal.h.  */
    745 
    746 void
    747 compile_instance::set_print_callback
    748   (void (*print_function) (void *, const char *), void *datum)
    749 {
    750   FORWARD (set_print_callback, print_function, datum);
    751 }
    752 
    753 /* See compile-internal.h.  */
    754 
    755 unsigned int
    756 compile_instance::version () const
    757 {
    758   return m_gcc_fe->ops->version;
    759 }
    760 
    761 /* See compile-internal.h.  */
    762 
    763 void
    764 compile_instance::set_verbose (int level)
    765 {
    766   if (version () >= GCC_FE_VERSION_1)
    767     FORWARD (set_verbose, level);
    768 }
    769 
    770 /* See compile-internal.h.  */
    771 
    772 void
    773 compile_instance::set_driver_filename (const char *filename)
    774 {
    775   if (version () >= GCC_FE_VERSION_1)
    776     FORWARD (set_driver_filename, filename);
    777 }
    778 
    779 /* See compile-internal.h.  */
    780 
    781 void
    782 compile_instance::set_triplet_regexp (const char *regexp)
    783 {
    784   if (version () >= GCC_FE_VERSION_1)
    785     FORWARD (set_triplet_regexp, regexp);
    786 }
    787 
    788 /* See compile-internal.h.  */
    789 
    790 gdb::unique_xmalloc_ptr<char>
    791 compile_instance::set_arguments (int argc, char **argv, const char *regexp)
    792 {
    793   if (version () >= GCC_FE_VERSION_1)
    794     return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments, argc, argv));
    795   else
    796     return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments_v0, regexp,
    797 						   argc, argv));
    798 }
    799 
    800 /* See compile-internal.h.  */
    801 
    802 void
    803 compile_instance::set_source_file (const char *filename)
    804 {
    805   FORWARD (set_source_file, filename);
    806 }
    807 
    808 /* See compile-internal.h.  */
    809 
    810 bool
    811 compile_instance::compile (const char *filename, int verbose_level)
    812 {
    813   if (version () >= GCC_FE_VERSION_1)
    814     return FORWARD (compile, filename);
    815   else
    816     return FORWARD (compile_v0, filename, verbose_level);
    817 }
    818 
    819 #undef FORWARD
    820 
    821 /* See compile.h.  */
    822 cmd_list_element *compile_cmd_element = nullptr;
    823 
    824 void _initialize_compile ();
    825 void
    826 _initialize_compile ()
    827 {
    828   struct cmd_list_element *c = NULL;
    829 
    830   compile_cmd_element = add_prefix_cmd ("compile", class_obscure,
    831 					compile_command, _("\
    832 Command to compile source code and inject it into the inferior."),
    833 		  &compile_command_list, 1, &cmdlist);
    834   add_com_alias ("expression", compile_cmd_element, class_obscure, 0);
    835 
    836   const auto compile_opts = make_compile_options_def_group (nullptr);
    837 
    838   static const std::string compile_code_help
    839     = gdb::option::build_help (_("\
    840 Compile, inject, and execute code.\n\
    841 \n\
    842 Usage: compile code [OPTION]... [CODE]\n\
    843 \n\
    844 Options:\n\
    845 %OPTIONS%\n\
    846 \n\
    847 The source code may be specified as a simple one line expression, e.g.:\n\
    848 \n\
    849     compile code printf(\"Hello world\\n\");\n\
    850 \n\
    851 Alternatively, you can type a multiline expression by invoking\n\
    852 this command with no argument.  GDB will then prompt for the\n\
    853 expression interactively; type a line containing \"end\" to\n\
    854 indicate the end of the expression."),
    855 			       compile_opts);
    856 
    857   c = add_cmd ("code", class_obscure, compile_code_command,
    858 	       compile_code_help.c_str (),
    859 	       &compile_command_list);
    860   set_cmd_completer_handle_brkchars (c, compile_code_command_completer);
    861 
    862 static const std::string compile_file_help
    863     = gdb::option::build_help (_("\
    864 Evaluate a file containing source code.\n\
    865 \n\
    866 Usage: compile file [OPTION].. [FILENAME]\n\
    867 \n\
    868 Options:\n\
    869 %OPTIONS%"),
    870 			       compile_opts);
    871 
    872   c = add_cmd ("file", class_obscure, compile_file_command,
    873 	       compile_file_help.c_str (),
    874 	       &compile_command_list);
    875   set_cmd_completer_handle_brkchars (c, compile_file_command_completer);
    876 
    877   const auto compile_print_opts = make_value_print_options_def_group (nullptr);
    878 
    879   static const std::string compile_print_help
    880     = gdb::option::build_help (_("\
    881 Evaluate EXPR by using the compiler and print result.\n\
    882 \n\
    883 Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
    884 \n\
    885 Options:\n\
    886 %OPTIONS%\n\
    887 \n\
    888 Note: because this command accepts arbitrary expressions, if you\n\
    889 specify any command option, you must use a double dash (\"--\")\n\
    890 to mark the end of option processing.  E.g.: \"compile print -o -- myobj\".\n\
    891 \n\
    892 The expression may be specified on the same line as the command, e.g.:\n\
    893 \n\
    894     compile print i\n\
    895 \n\
    896 Alternatively, you can type a multiline expression by invoking\n\
    897 this command with no argument.  GDB will then prompt for the\n\
    898 expression interactively; type a line containing \"end\" to\n\
    899 indicate the end of the expression.\n\
    900 \n\
    901 EXPR may be preceded with /FMT, where FMT is a format letter\n\
    902 but no count or size letter (see \"x\" command)."),
    903 			       compile_print_opts);
    904 
    905   c = add_cmd ("print", class_obscure, compile_print_command,
    906 	       compile_print_help.c_str (),
    907 	       &compile_command_list);
    908   set_cmd_completer_handle_brkchars (c, print_command_completer);
    909 
    910   add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
    911 Set compile command debugging."), _("\
    912 Show compile command debugging."), _("\
    913 When on, compile command debugging is enabled."),
    914 			   NULL, show_compile_debug,
    915 			   &setdebuglist, &showdebuglist);
    916 
    917   add_setshow_string_cmd ("compile-args", class_support,
    918 			  &compile_args,
    919 			  _("Set compile command GCC command-line arguments."),
    920 			  _("Show compile command GCC command-line arguments."),
    921 			  _("\
    922 Use options like -I (include file directory) or ABI settings.\n\
    923 String quoting is parsed like in shell, for example:\n\
    924   -mno-align-double \"-I/dir with a space/include\""),
    925 			  set_compile_args, show_compile_args, &setlist, &showlist);
    926 
    927 
    928   /* Initialize compile_args_argv.  */
    929   set_compile_args (compile_args.c_str (), 0, NULL);
    930 
    931   add_setshow_optional_filename_cmd ("compile-gcc", class_support,
    932 				     &compile_gcc,
    933 				     _("Set compile command "
    934 				       "GCC driver filename."),
    935 				     _("Show compile command "
    936 				       "GCC driver filename."),
    937 				     _("\
    938 It should be absolute filename of the gcc executable.\n\
    939 If empty the default target triplet will be searched in $PATH."),
    940 				     NULL, show_compile_gcc, &setlist,
    941 				     &showlist);
    942 }
    943