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