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