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