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