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