Home | History | Annotate | Line # | Download | only in compile
compile.c revision 1.5
      1  1.1  christos /* General Compile and inject code
      2  1.1  christos 
      3  1.5  christos    Copyright (C) 2014-2017 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.1  christos #include "defs.h"
     21  1.4  christos #include "top.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.1  christos #include "completer.h"
     27  1.1  christos #include "gdbcmd.h"
     28  1.1  christos #include "compile.h"
     29  1.1  christos #include "compile-internal.h"
     30  1.1  christos #include "compile-object-load.h"
     31  1.1  christos #include "compile-object-run.h"
     32  1.1  christos #include "language.h"
     33  1.1  christos #include "frame.h"
     34  1.1  christos #include "source.h"
     35  1.1  christos #include "block.h"
     36  1.1  christos #include "arch-utils.h"
     37  1.1  christos #include "filestuff.h"
     38  1.1  christos #include "target.h"
     39  1.1  christos #include "osabi.h"
     40  1.1  christos #include "gdb_wait.h"
     41  1.3  christos #include "valprint.h"
     42  1.1  christos 
     43  1.1  christos 
     44  1.1  christos 
     46  1.1  christos /* Initial filename for temporary files.  */
     47  1.1  christos 
     48  1.1  christos #define TMP_PREFIX "/tmp/gdbobj-"
     49  1.1  christos 
     50  1.1  christos /* Hold "compile" commands.  */
     51  1.1  christos 
     52  1.1  christos static struct cmd_list_element *compile_command_list;
     53  1.1  christos 
     54  1.1  christos /* Debug flag for "compile" commands.  */
     55  1.1  christos 
     56  1.1  christos int compile_debug;
     57  1.1  christos 
     58  1.1  christos /* Implement "show debug compile".  */
     59  1.1  christos 
     60  1.1  christos static void
     61  1.1  christos show_compile_debug (struct ui_file *file, int from_tty,
     62  1.1  christos 		    struct cmd_list_element *c, const char *value)
     63  1.1  christos {
     64  1.1  christos   fprintf_filtered (file, _("Compile debugging is %s.\n"), value);
     65  1.1  christos }
     66  1.1  christos 
     67  1.1  christos 
     68  1.1  christos 
     70  1.1  christos /* Check *ARG for a "-raw" or "-r" argument.  Return 0 if not seen.
     71  1.1  christos    Return 1 if seen and update *ARG.  */
     72  1.1  christos 
     73  1.1  christos static int
     74  1.1  christos check_raw_argument (char **arg)
     75  1.1  christos {
     76  1.1  christos   *arg = skip_spaces (*arg);
     77  1.1  christos 
     78  1.1  christos   if (arg != NULL
     79  1.1  christos       && (check_for_argument (arg, "-raw", sizeof ("-raw") - 1)
     80  1.1  christos 	  || check_for_argument (arg, "-r", sizeof ("-r") - 1)))
     81  1.1  christos       return 1;
     82  1.1  christos   return 0;
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos /* Handle the input from the 'compile file' command.  The "compile
     86  1.1  christos    file" command is used to evaluate an expression contained in a file
     87  1.1  christos    that may contain calls to the GCC compiler.  */
     88  1.1  christos 
     89  1.1  christos static void
     90  1.1  christos compile_file_command (char *arg, int from_tty)
     91  1.1  christos {
     92  1.1  christos   enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
     93  1.1  christos   char *buffer;
     94  1.5  christos   struct cleanup *cleanup;
     95  1.1  christos 
     96  1.1  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
     97  1.1  christos 
     98  1.1  christos   /* Check the user did not just <enter> after command.  */
     99  1.1  christos   if (arg == NULL)
    100  1.1  christos     error (_("You must provide a filename for this command."));
    101  1.1  christos 
    102  1.1  christos   /* Check if a raw (-r|-raw) argument is provided.  */
    103  1.1  christos   if (arg != NULL && check_raw_argument (&arg))
    104  1.1  christos     {
    105  1.1  christos       scope = COMPILE_I_RAW_SCOPE;
    106  1.1  christos       arg = skip_spaces (arg);
    107  1.1  christos     }
    108  1.1  christos 
    109  1.1  christos   /* After processing arguments, check there is a filename at the end
    110  1.1  christos      of the command.  */
    111  1.1  christos   if (arg[0] == '\0')
    112  1.1  christos     error (_("You must provide a filename with the raw option set."));
    113  1.1  christos 
    114  1.1  christos   if (arg[0] == '-')
    115  1.1  christos     error (_("Unknown argument specified."));
    116  1.1  christos 
    117  1.5  christos   arg = skip_spaces (arg);
    118  1.1  christos   arg = gdb_abspath (arg);
    119  1.1  christos   cleanup = make_cleanup (xfree, arg);
    120  1.3  christos   buffer = xstrprintf ("#include \"%s\"\n", arg);
    121  1.1  christos   make_cleanup (xfree, buffer);
    122  1.1  christos   eval_compile_command (NULL, buffer, scope, NULL);
    123  1.1  christos   do_cleanups (cleanup);
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos /* Handle the input from the 'compile code' command.  The
    127  1.1  christos    "compile code" command is used to evaluate an expression that may
    128  1.1  christos    contain calls to the GCC compiler.  The language expected in this
    129  1.1  christos    compile command is the language currently set in GDB.  */
    130  1.1  christos 
    131  1.1  christos static void
    132  1.1  christos compile_code_command (char *arg, int from_tty)
    133  1.1  christos {
    134  1.5  christos   enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
    135  1.1  christos 
    136  1.1  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    137  1.1  christos 
    138  1.1  christos   if (arg != NULL && check_raw_argument (&arg))
    139  1.1  christos     {
    140  1.1  christos       scope = COMPILE_I_RAW_SCOPE;
    141  1.1  christos       arg = skip_spaces (arg);
    142  1.1  christos     }
    143  1.1  christos 
    144  1.1  christos   arg = skip_spaces (arg);
    145  1.1  christos 
    146  1.1  christos   if (arg != NULL && !check_for_argument (&arg, "--", sizeof ("--") - 1))
    147  1.1  christos     {
    148  1.1  christos       if (arg[0] == '-')
    149  1.1  christos 	error (_("Unknown argument specified."));
    150  1.1  christos     }
    151  1.3  christos 
    152  1.1  christos   if (arg && *arg)
    153  1.1  christos     eval_compile_command (NULL, arg, scope, NULL);
    154  1.5  christos   else
    155  1.1  christos     {
    156  1.1  christos       command_line_up l = get_command_line (compile_control, "");
    157  1.5  christos 
    158  1.1  christos       l->control_u.compile.scope = scope;
    159  1.1  christos       execute_control_command_untraced (l.get ());
    160  1.1  christos     }
    161  1.3  christos }
    162  1.3  christos 
    163  1.3  christos /* Callback for compile_print_command.  */
    164  1.3  christos 
    165  1.3  christos void
    166  1.4  christos compile_print_value (struct value *val, void *data_voidp)
    167  1.3  christos {
    168  1.3  christos   const struct format_data *fmtp = (const struct format_data *) data_voidp;
    169  1.3  christos 
    170  1.3  christos   print_value (val, fmtp);
    171  1.3  christos }
    172  1.3  christos 
    173  1.3  christos /* Handle the input from the 'compile print' command.  The "compile
    174  1.3  christos    print" command is used to evaluate and print an expression that may
    175  1.3  christos    contain calls to the GCC compiler.  The language expected in this
    176  1.3  christos    compile command is the language currently set in GDB.  */
    177  1.3  christos 
    178  1.3  christos static void
    179  1.3  christos compile_print_command (char *arg_param, int from_tty)
    180  1.3  christos {
    181  1.3  christos   const char *arg = arg_param;
    182  1.3  christos   enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
    183  1.5  christos   struct format_data fmt;
    184  1.3  christos 
    185  1.3  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    186  1.3  christos 
    187  1.3  christos   /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not
    188  1.3  christos      touch the stale pointer if compile_object_run has already quit.  */
    189  1.3  christos   print_command_parse_format (&arg, "compile print", &fmt);
    190  1.3  christos 
    191  1.3  christos   if (arg && *arg)
    192  1.3  christos     eval_compile_command (NULL, arg, scope, &fmt);
    193  1.5  christos   else
    194  1.3  christos     {
    195  1.3  christos       command_line_up l = get_command_line (compile_control, "");
    196  1.3  christos 
    197  1.5  christos       l->control_u.compile.scope = scope;
    198  1.3  christos       l->control_u.compile.scope_data = &fmt;
    199  1.3  christos       execute_control_command_untraced (l.get ());
    200  1.3  christos     }
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos /* A cleanup function to remove a directory and all its contents.  */
    204  1.1  christos 
    205  1.1  christos static void
    206  1.4  christos do_rmdir (void *arg)
    207  1.1  christos {
    208  1.1  christos   const char *dir = (const char *) arg;
    209  1.1  christos   char *zap;
    210  1.3  christos   int wstat;
    211  1.1  christos 
    212  1.1  christos   gdb_assert (startswith (dir, TMP_PREFIX));
    213  1.1  christos   zap = concat ("rm -rf ", dir, (char *) NULL);
    214  1.1  christos   wstat = system (zap);
    215  1.1  christos   if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
    216  1.1  christos     warning (_("Could not remove temporary directory %s"), dir);
    217  1.1  christos   XDELETEVEC (zap);
    218  1.1  christos }
    219  1.1  christos 
    220  1.1  christos /* Return the name of the temporary directory to use for .o files, and
    221  1.1  christos    arrange for the directory to be removed at shutdown.  */
    222  1.1  christos 
    223  1.1  christos static const char *
    224  1.1  christos get_compile_file_tempdir (void)
    225  1.1  christos {
    226  1.1  christos   static char *tempdir_name;
    227  1.1  christos 
    228  1.1  christos #define TEMPLATE TMP_PREFIX "XXXXXX"
    229  1.1  christos   char tname[sizeof (TEMPLATE)];
    230  1.1  christos 
    231  1.1  christos   if (tempdir_name != NULL)
    232  1.1  christos     return tempdir_name;
    233  1.1  christos 
    234  1.1  christos   strcpy (tname, TEMPLATE);
    235  1.1  christos #undef TEMPLATE
    236  1.1  christos #ifdef HAVE_MKDTEMP
    237  1.1  christos   tempdir_name = mkdtemp (tname);
    238  1.1  christos #else
    239  1.1  christos   error (_("Command not supported on this host."));
    240  1.1  christos #endif
    241  1.1  christos   if (tempdir_name == NULL)
    242  1.1  christos     perror_with_name (_("Could not make temporary directory"));
    243  1.1  christos 
    244  1.1  christos   tempdir_name = xstrdup (tempdir_name);
    245  1.1  christos   make_final_cleanup (do_rmdir, tempdir_name);
    246  1.1  christos   return tempdir_name;
    247  1.5  christos }
    248  1.1  christos 
    249  1.5  christos /* Compute the names of source and object files to use.  */
    250  1.5  christos 
    251  1.1  christos static compile_file_names
    252  1.1  christos get_new_file_names ()
    253  1.1  christos {
    254  1.1  christos   static int seq;
    255  1.1  christos   const char *dir = get_compile_file_tempdir ();
    256  1.5  christos 
    257  1.5  christos   ++seq;
    258  1.5  christos 
    259  1.5  christos   return compile_file_names (string_printf ("%s%sout%d.c",
    260  1.5  christos 					    dir, SLASH_STRING, seq),
    261  1.1  christos 			     string_printf ("%s%sout%d.o",
    262  1.1  christos 					    dir, SLASH_STRING, seq));
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos /* Get the block and PC at which to evaluate an expression.  */
    266  1.1  christos 
    267  1.1  christos static const struct block *
    268  1.1  christos get_expr_block_and_pc (CORE_ADDR *pc)
    269  1.1  christos {
    270  1.1  christos   const struct block *block = get_selected_block (pc);
    271  1.1  christos 
    272  1.1  christos   if (block == NULL)
    273  1.1  christos     {
    274  1.1  christos       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
    275  1.1  christos 
    276  1.1  christos       if (cursal.symtab)
    277  1.1  christos 	block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
    278  1.1  christos 				   STATIC_BLOCK);
    279  1.1  christos       if (block != NULL)
    280  1.1  christos 	*pc = BLOCK_START (block);
    281  1.1  christos     }
    282  1.1  christos   else
    283  1.1  christos     *pc = BLOCK_START (block);
    284  1.1  christos 
    285  1.1  christos   return block;
    286  1.1  christos }
    287  1.1  christos 
    288  1.1  christos /* Call gdb_buildargv, set its result for S into *ARGVP but calculate also the
    289  1.1  christos    number of parsed arguments into *ARGCP.  If gdb_buildargv has returned NULL
    290  1.1  christos    then *ARGCP is set to zero.  */
    291  1.1  christos 
    292  1.1  christos static void
    293  1.1  christos build_argc_argv (const char *s, int *argcp, char ***argvp)
    294  1.1  christos {
    295  1.1  christos   *argvp = gdb_buildargv (s);
    296  1.1  christos   *argcp = countargv (*argvp);
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos /* String for 'set compile-args' and 'show compile-args'.  */
    300  1.1  christos static char *compile_args;
    301  1.1  christos 
    302  1.1  christos /* Parsed form of COMPILE_ARGS.  COMPILE_ARGS_ARGV is NULL terminated.  */
    303  1.1  christos static int compile_args_argc;
    304  1.1  christos static char **compile_args_argv;
    305  1.1  christos 
    306  1.1  christos /* Implement 'set compile-args'.  */
    307  1.1  christos 
    308  1.1  christos static void
    309  1.1  christos set_compile_args (char *args, int from_tty, struct cmd_list_element *c)
    310  1.1  christos {
    311  1.1  christos   freeargv (compile_args_argv);
    312  1.1  christos   build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv);
    313  1.1  christos }
    314  1.1  christos 
    315  1.1  christos /* Implement 'show compile-args'.  */
    316  1.1  christos 
    317  1.1  christos static void
    318  1.1  christos show_compile_args (struct ui_file *file, int from_tty,
    319  1.1  christos 		   struct cmd_list_element *c, const char *value)
    320  1.1  christos {
    321  1.1  christos   fprintf_filtered (file, _("Compile command command-line arguments "
    322  1.1  christos 			    "are \"%s\".\n"),
    323  1.1  christos 		    value);
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos /* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP.
    327  1.1  christos    ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL.  */
    328  1.1  christos 
    329  1.1  christos static void
    330  1.1  christos append_args (int *argcp, char ***argvp, int argc, char **argv)
    331  1.1  christos {
    332  1.4  christos   int argi;
    333  1.1  christos 
    334  1.1  christos   *argvp = XRESIZEVEC (char *, *argvp, (*argcp + argc + 1));
    335  1.1  christos 
    336  1.1  christos   for (argi = 0; argi < argc; argi++)
    337  1.1  christos     (*argvp)[(*argcp)++] = xstrdup (argv[argi]);
    338  1.1  christos   (*argvp)[(*argcp)] = NULL;
    339  1.1  christos }
    340  1.1  christos 
    341  1.1  christos /* Return DW_AT_producer parsed for get_selected_frame () (if any).
    342  1.1  christos    Return NULL otherwise.
    343  1.1  christos 
    344  1.1  christos    GCC already filters its command-line arguments only for the suitable ones to
    345  1.1  christos    put into DW_AT_producer - see GCC function gen_producer_string.  */
    346  1.1  christos 
    347  1.1  christos static const char *
    348  1.1  christos get_selected_pc_producer_options (void)
    349  1.1  christos {
    350  1.1  christos   CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
    351  1.1  christos   struct compunit_symtab *symtab = find_pc_compunit_symtab (pc);
    352  1.1  christos   const char *cs;
    353  1.3  christos 
    354  1.1  christos   if (symtab == NULL || symtab->producer == NULL
    355  1.1  christos       || !startswith (symtab->producer, "GNU "))
    356  1.1  christos     return NULL;
    357  1.1  christos 
    358  1.1  christos   cs = symtab->producer;
    359  1.1  christos   while (*cs != 0 && *cs != '-')
    360  1.1  christos     cs = skip_spaces_const (skip_to_space_const (cs));
    361  1.1  christos   if (*cs != '-')
    362  1.1  christos     return NULL;
    363  1.1  christos   return cs;
    364  1.1  christos }
    365  1.1  christos 
    366  1.1  christos /* Filter out unwanted options from *ARGCP and ARGV.  */
    367  1.1  christos 
    368  1.1  christos static void
    369  1.1  christos filter_args (int *argcp, char **argv)
    370  1.1  christos {
    371  1.1  christos   char **destv;
    372  1.1  christos 
    373  1.1  christos   for (destv = argv; *argv != NULL; argv++)
    374  1.1  christos     {
    375  1.1  christos       /* -fpreprocessed may get in commonly from ccache.  */
    376  1.1  christos       if (strcmp (*argv, "-fpreprocessed") == 0)
    377  1.1  christos 	{
    378  1.1  christos 	  xfree (*argv);
    379  1.1  christos 	  (*argcp)--;
    380  1.1  christos 	  continue;
    381  1.1  christos 	}
    382  1.1  christos       *destv++ = *argv;
    383  1.1  christos     }
    384  1.1  christos   *destv = NULL;
    385  1.1  christos }
    386  1.1  christos 
    387  1.1  christos /* Produce final vector of GCC compilation options.  First element is target
    388  1.1  christos    size ("-m64", "-m32" etc.), optionally followed by DW_AT_producer options
    389  1.1  christos    and then compile-args string GDB variable.  */
    390  1.1  christos 
    391  1.1  christos static void
    392  1.1  christos get_args (const struct compile_instance *compiler, struct gdbarch *gdbarch,
    393  1.1  christos 	  int *argcp, char ***argvp)
    394  1.1  christos {
    395  1.1  christos   const char *cs_producer_options;
    396  1.1  christos   int argc_compiler;
    397  1.1  christos   char **argv_compiler;
    398  1.1  christos 
    399  1.1  christos   build_argc_argv (gdbarch_gcc_target_options (gdbarch),
    400  1.1  christos 		   argcp, argvp);
    401  1.1  christos 
    402  1.1  christos   cs_producer_options = get_selected_pc_producer_options ();
    403  1.1  christos   if (cs_producer_options != NULL)
    404  1.1  christos     {
    405  1.1  christos       int argc_producer;
    406  1.1  christos       char **argv_producer;
    407  1.1  christos 
    408  1.1  christos       build_argc_argv (cs_producer_options, &argc_producer, &argv_producer);
    409  1.1  christos       filter_args (&argc_producer, argv_producer);
    410  1.1  christos       append_args (argcp, argvp, argc_producer, argv_producer);
    411  1.1  christos       freeargv (argv_producer);
    412  1.1  christos     }
    413  1.1  christos 
    414  1.1  christos   build_argc_argv (compiler->gcc_target_options,
    415  1.1  christos 		   &argc_compiler, &argv_compiler);
    416  1.1  christos   append_args (argcp, argvp, argc_compiler, argv_compiler);
    417  1.1  christos   freeargv (argv_compiler);
    418  1.1  christos 
    419  1.1  christos   append_args (argcp, argvp, compile_args_argc, compile_args_argv);
    420  1.1  christos }
    421  1.1  christos 
    422  1.1  christos /* A cleanup function to destroy a gdb_gcc_instance.  */
    423  1.1  christos 
    424  1.1  christos static void
    425  1.4  christos cleanup_compile_instance (void *arg)
    426  1.1  christos {
    427  1.1  christos   struct compile_instance *inst = (struct compile_instance *) arg;
    428  1.1  christos 
    429  1.1  christos   inst->destroy (inst);
    430  1.1  christos }
    431  1.1  christos 
    432  1.1  christos /* A cleanup function to unlink a file.  */
    433  1.1  christos 
    434  1.1  christos static void
    435  1.4  christos cleanup_unlink_file (void *arg)
    436  1.1  christos {
    437  1.1  christos   const char *filename = (const char *) arg;
    438  1.1  christos 
    439  1.1  christos   unlink (filename);
    440  1.1  christos }
    441  1.1  christos 
    442  1.1  christos /* A helper function suitable for use as the "print_callback" in the
    443  1.1  christos    compiler object.  */
    444  1.1  christos 
    445  1.1  christos static void
    446  1.1  christos print_callback (void *ignore, const char *message)
    447  1.1  christos {
    448  1.1  christos   fputs_filtered (message, gdb_stderr);
    449  1.1  christos }
    450  1.5  christos 
    451  1.5  christos /* Process the compilation request.  On success it returns the object
    452  1.1  christos    and source file names.  On an error condition, error () is
    453  1.5  christos    called.  */
    454  1.3  christos 
    455  1.5  christos static compile_file_names
    456  1.1  christos compile_to_object (struct command_line *cmd, const char *cmd_string,
    457  1.1  christos 		   enum compile_i_scope_types scope)
    458  1.1  christos {
    459  1.1  christos   struct compile_instance *compiler;
    460  1.1  christos   struct cleanup *cleanup, *inner_cleanup;
    461  1.1  christos   const struct block *expr_block;
    462  1.1  christos   CORE_ADDR trash_pc, expr_pc;
    463  1.1  christos   int argc;
    464  1.1  christos   char **argv;
    465  1.1  christos   int ok;
    466  1.1  christos   FILE *src;
    467  1.1  christos   struct gdbarch *gdbarch = get_current_arch ();
    468  1.1  christos   const char *os_rx;
    469  1.1  christos   const char *arch_rx;
    470  1.1  christos   char *triplet_rx;
    471  1.1  christos   char *error_message;
    472  1.1  christos 
    473  1.1  christos   if (!target_has_execution)
    474  1.1  christos     error (_("The program must be running for the compile command to "\
    475  1.1  christos 	     "work."));
    476  1.1  christos 
    477  1.1  christos   expr_block = get_expr_block_and_pc (&trash_pc);
    478  1.1  christos   expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
    479  1.1  christos 
    480  1.4  christos   /* Set up instance and context for the compiler.  */
    481  1.4  christos   if (current_language->la_get_compile_instance == NULL)
    482  1.1  christos     error (_("No compiler support for language %s."),
    483  1.1  christos 	   current_language->la_name);
    484  1.1  christos   compiler = current_language->la_get_compile_instance ();
    485  1.1  christos   cleanup = make_cleanup (cleanup_compile_instance, compiler);
    486  1.1  christos 
    487  1.1  christos   compiler->fe->ops->set_print_callback (compiler->fe, print_callback, NULL);
    488  1.1  christos 
    489  1.1  christos   compiler->scope = scope;
    490  1.1  christos   compiler->block = expr_block;
    491  1.1  christos 
    492  1.5  christos   /* From the provided expression, build a scope to pass to the
    493  1.5  christos      compiler.  */
    494  1.5  christos 
    495  1.5  christos   string_file input_buf;
    496  1.1  christos   const char *input;
    497  1.1  christos 
    498  1.1  christos   if (cmd != NULL)
    499  1.1  christos     {
    500  1.1  christos       struct command_line *iter;
    501  1.1  christos 
    502  1.5  christos       for (iter = cmd->body_list[0]; iter; iter = iter->next)
    503  1.5  christos 	{
    504  1.1  christos 	  input_buf.puts (iter->line);
    505  1.1  christos 	  input_buf.puts ("\n");
    506  1.5  christos 	}
    507  1.1  christos 
    508  1.1  christos       input = input_buf.c_str ();
    509  1.3  christos     }
    510  1.1  christos   else if (cmd_string != NULL)
    511  1.1  christos     input = cmd_string;
    512  1.1  christos   else
    513  1.5  christos     error (_("Neither a simple expression, or a multi-line specified."));
    514  1.5  christos 
    515  1.5  christos   std::string code
    516  1.1  christos     = current_language->la_compute_program (compiler, input, gdbarch,
    517  1.5  christos 					    expr_block, expr_pc);
    518  1.1  christos   if (compile_debug)
    519  1.1  christos     fprintf_unfiltered (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
    520  1.1  christos 
    521  1.1  christos   os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
    522  1.1  christos   arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
    523  1.1  christos 
    524  1.1  christos   /* Allow triplets with or without vendor set.  */
    525  1.1  christos   triplet_rx = concat (arch_rx, "(-[^-]*)?-", os_rx, (char *) NULL);
    526  1.1  christos   make_cleanup (xfree, triplet_rx);
    527  1.1  christos 
    528  1.1  christos   /* Set compiler command-line arguments.  */
    529  1.1  christos   get_args (compiler, gdbarch, &argc, &argv);
    530  1.1  christos   make_cleanup_freeargv (argv);
    531  1.1  christos 
    532  1.1  christos   error_message = compiler->fe->ops->set_arguments (compiler->fe, triplet_rx,
    533  1.1  christos 						    argc, argv);
    534  1.1  christos   if (error_message != NULL)
    535  1.1  christos     {
    536  1.1  christos       make_cleanup (xfree, error_message);
    537  1.1  christos       error ("%s", error_message);
    538  1.1  christos     }
    539  1.1  christos 
    540  1.1  christos   if (compile_debug)
    541  1.1  christos     {
    542  1.3  christos       int argi;
    543  1.1  christos 
    544  1.3  christos       fprintf_unfiltered (gdb_stdlog, "Passing %d compiler options:\n", argc);
    545  1.1  christos       for (argi = 0; argi < argc; argi++)
    546  1.1  christos 	fprintf_unfiltered (gdb_stdlog, "Compiler option %d: <%s>\n",
    547  1.1  christos 			    argi, argv[argi]);
    548  1.5  christos     }
    549  1.1  christos 
    550  1.5  christos   compile_file_names fnames = get_new_file_names ();
    551  1.1  christos 
    552  1.1  christos   src = gdb_fopen_cloexec (fnames.source_file (), "w");
    553  1.5  christos   if (src == NULL)
    554  1.5  christos     perror_with_name (_("Could not open source file for writing"));
    555  1.5  christos   inner_cleanup = make_cleanup (cleanup_unlink_file,
    556  1.1  christos 				(void *) fnames.source_file ());
    557  1.1  christos   if (fputs (code.c_str (), src) == EOF)
    558  1.1  christos     perror_with_name (_("Could not write to source file"));
    559  1.1  christos   fclose (src);
    560  1.3  christos 
    561  1.5  christos   if (compile_debug)
    562  1.1  christos     fprintf_unfiltered (gdb_stdlog, "source file produced: %s\n\n",
    563  1.1  christos 			fnames.source_file ());
    564  1.5  christos 
    565  1.1  christos   /* Call the compiler and start the compilation process.  */
    566  1.5  christos   compiler->fe->ops->set_source_file (compiler->fe, fnames.source_file ());
    567  1.1  christos 
    568  1.1  christos   if (!compiler->fe->ops->compile (compiler->fe, fnames.object_file (),
    569  1.1  christos 				   compile_debug))
    570  1.1  christos     error (_("Compilation failed."));
    571  1.3  christos 
    572  1.5  christos   if (compile_debug)
    573  1.1  christos     fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n",
    574  1.1  christos 			fnames.object_file ());
    575  1.1  christos 
    576  1.5  christos   discard_cleanups (inner_cleanup);
    577  1.5  christos   do_cleanups (cleanup);
    578  1.1  christos 
    579  1.1  christos   return fnames;
    580  1.1  christos }
    581  1.1  christos 
    582  1.1  christos /* The "compile" prefix command.  */
    583  1.1  christos 
    584  1.1  christos static void
    585  1.1  christos compile_command (char *args, int from_tty)
    586  1.1  christos {
    587  1.1  christos   /* If a sub-command is not specified to the compile prefix command,
    588  1.1  christos      assume it is a direct code compilation.  */
    589  1.1  christos   compile_code_command (args, from_tty);
    590  1.1  christos }
    591  1.1  christos 
    592  1.1  christos /* See compile.h.  */
    593  1.3  christos 
    594  1.3  christos void
    595  1.1  christos eval_compile_command (struct command_line *cmd, const char *cmd_string,
    596  1.5  christos 		      enum compile_i_scope_types scope, void *scope_data)
    597  1.5  christos {
    598  1.1  christos   struct cleanup *cleanup_unlink;
    599  1.5  christos   struct compile_module *compile_module;
    600  1.1  christos 
    601  1.5  christos   compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
    602  1.5  christos 
    603  1.5  christos   cleanup_unlink = make_cleanup (cleanup_unlink_file,
    604  1.5  christos 				 (void *) fnames.object_file ());
    605  1.5  christos   make_cleanup (cleanup_unlink_file, (void *) fnames.source_file ());
    606  1.5  christos   compile_module = compile_object_load (fnames, scope, scope_data);
    607  1.5  christos   if (compile_module == NULL)
    608  1.5  christos     {
    609  1.5  christos       gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
    610  1.5  christos       eval_compile_command (cmd, cmd_string,
    611  1.1  christos 			    COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
    612  1.5  christos       return;
    613  1.5  christos     }
    614  1.1  christos   discard_cleanups (cleanup_unlink);
    615  1.1  christos   compile_object_run (compile_module);
    616  1.1  christos }
    617  1.1  christos 
    618  1.1  christos /* See compile/compile-internal.h.  */
    619  1.1  christos 
    620  1.1  christos char *
    621  1.1  christos compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
    622  1.1  christos {
    623  1.1  christos   const char *regname = gdbarch_register_name (gdbarch, regnum);
    624  1.1  christos 
    625  1.1  christos   return xstrprintf ("__%s", regname);
    626  1.1  christos }
    627  1.1  christos 
    628  1.1  christos /* See compile/compile-internal.h.  */
    629  1.1  christos 
    630  1.1  christos int
    631  1.1  christos compile_register_name_demangle (struct gdbarch *gdbarch,
    632  1.1  christos 				 const char *regname)
    633  1.1  christos {
    634  1.1  christos   int regnum;
    635  1.1  christos 
    636  1.1  christos   if (regname[0] != '_' || regname[1] != '_')
    637  1.1  christos     error (_("Invalid register name \"%s\"."), regname);
    638  1.1  christos   regname += 2;
    639  1.1  christos 
    640  1.1  christos   for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
    641  1.1  christos     if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
    642  1.1  christos       return regnum;
    643  1.1  christos 
    644  1.1  christos   error (_("Cannot find gdbarch register \"%s\"."), regname);
    645  1.1  christos }
    646  1.1  christos 
    647  1.1  christos extern initialize_file_ftype _initialize_compile;
    648  1.1  christos 
    649  1.1  christos void
    650  1.1  christos _initialize_compile (void)
    651  1.1  christos {
    652  1.1  christos   struct cmd_list_element *c = NULL;
    653  1.1  christos 
    654  1.1  christos   add_prefix_cmd ("compile", class_obscure, compile_command,
    655  1.1  christos 		  _("\
    656  1.1  christos Command to compile source code and inject it into the inferior."),
    657  1.1  christos 		  &compile_command_list, "compile ", 1, &cmdlist);
    658  1.1  christos   add_com_alias ("expression", "compile", class_obscure, 0);
    659  1.1  christos 
    660  1.1  christos   add_cmd ("code", class_obscure, compile_code_command,
    661  1.1  christos 	   _("\
    662  1.1  christos Compile, inject, and execute code.\n\
    663  1.1  christos \n\
    664  1.1  christos Usage: compile code [-r|-raw] [--] [CODE]\n\
    665  1.1  christos -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping.\n\
    666  1.1  christos --: Do not parse any options beyond this delimiter.  All text to the\n\
    667  1.1  christos     right will be treated as source code.\n\
    668  1.1  christos \n\
    669  1.1  christos The source code may be specified as a simple one line expression, e.g.:\n\
    670  1.1  christos \n\
    671  1.3  christos     compile code printf(\"Hello world\\n\");\n\
    672  1.3  christos \n\
    673  1.3  christos Alternatively, you can type a multiline expression by invoking\n\
    674  1.3  christos this command with no argument.  GDB will then prompt for the\n\
    675  1.1  christos expression interactively; type a line containing \"end\" to\n\
    676  1.1  christos indicate the end of the expression."),
    677  1.1  christos 	   &compile_command_list);
    678  1.1  christos 
    679  1.1  christos   c = add_cmd ("file", class_obscure, compile_file_command,
    680  1.1  christos 	       _("\
    681  1.1  christos Evaluate a file containing source code.\n\
    682  1.1  christos \n\
    683  1.1  christos Usage: compile file [-r|-raw] [filename]\n\
    684  1.1  christos -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
    685  1.1  christos 	       &compile_command_list);
    686  1.3  christos   set_cmd_completer (c, filename_completer);
    687  1.3  christos 
    688  1.3  christos   add_cmd ("print", class_obscure, compile_print_command,
    689  1.3  christos 	   _("\
    690  1.3  christos Evaluate EXPR by using the compiler and print result.\n\
    691  1.3  christos \n\
    692  1.3  christos Usage: compile print[/FMT] [EXPR]\n\
    693  1.3  christos \n\
    694  1.3  christos The expression may be specified on the same line as the command, e.g.:\n\
    695  1.3  christos \n\
    696  1.3  christos     compile print i\n\
    697  1.3  christos \n\
    698  1.3  christos Alternatively, you can type a multiline expression by invoking\n\
    699  1.3  christos this command with no argument.  GDB will then prompt for the\n\
    700  1.3  christos expression interactively; type a line containing \"end\" to\n\
    701  1.3  christos indicate the end of the expression.\n\
    702  1.3  christos \n\
    703  1.3  christos EXPR may be preceded with /FMT, where FMT is a format letter\n\
    704  1.3  christos but no count or size letter (see \"x\" command)."),
    705  1.1  christos 	   &compile_command_list);
    706  1.1  christos 
    707  1.1  christos   add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
    708  1.1  christos Set compile command debugging."), _("\
    709  1.1  christos Show compile command debugging."), _("\
    710  1.1  christos When on, compile command debugging is enabled."),
    711  1.1  christos 			   NULL, show_compile_debug,
    712  1.1  christos 			   &setdebuglist, &showdebuglist);
    713  1.1  christos 
    714  1.1  christos   add_setshow_string_cmd ("compile-args", class_support,
    715  1.1  christos 			  &compile_args,
    716  1.1  christos 			  _("Set compile command GCC command-line arguments"),
    717  1.1  christos 			  _("Show compile command GCC command-line arguments"),
    718  1.1  christos 			  _("\
    719  1.1  christos Use options like -I (include file directory) or ABI settings.\n\
    720  1.1  christos String quoting is parsed like in shell, for example:\n\
    721  1.1  christos   -mno-align-double \"-I/dir with a space/include\""),
    722  1.1  christos 			  set_compile_args, show_compile_args, &setlist, &showlist);
    723  1.1  christos 
    724  1.1  christos   /* Override flags possibly coming from DW_AT_producer.  */
    725  1.1  christos   compile_args = xstrdup ("-O0 -gdwarf-4"
    726  1.1  christos   /* We use -fPIE Otherwise GDB would need to reserve space large enough for
    727  1.1  christos      any object file in the inferior in advance to get the final address when
    728  1.1  christos      to link the object file to and additionally the default system linker
    729  1.1  christos      script would need to be modified so that one can specify there the
    730  1.1  christos      absolute target address.
    731  1.3  christos      -fPIC is not used at is would require from GDB to generate .got.  */
    732  1.3  christos 			 " -fPIE"
    733  1.3  christos   /* We want warnings, except for some commonly happening for GDB commands.  */
    734  1.3  christos 			 " -Wall "
    735  1.3  christos 			 " -Wno-implicit-function-declaration"
    736  1.1  christos 			 " -Wno-unused-but-set-variable"
    737  1.1  christos 			 " -Wno-unused-variable"
    738  1.1  christos   /* Override CU's possible -fstack-protector-strong.  */
    739  1.1  christos 			 " -fno-stack-protector"
    740  1.1  christos   );
    741                  set_compile_args (compile_args, 0, NULL);
    742                }
    743