Commit | Line | Data |
---|---|---|
bb2ec1b3 TT |
1 | /* General Compile and inject code |
2 | ||
e2882c85 | 3 | Copyright (C) 2014-2018 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" | |
b4987c95 | 44 | #include "common/pathstuff.h" |
bb2ec1b3 TT |
45 | |
46 | \f | |
47 | ||
48 | /* Initial filename for temporary files. */ | |
49 | ||
50 | #define TMP_PREFIX "/tmp/gdbobj-" | |
51 | ||
52 | /* Hold "compile" commands. */ | |
53 | ||
54 | static struct cmd_list_element *compile_command_list; | |
55 | ||
56 | /* Debug flag for "compile" commands. */ | |
57 | ||
58 | int compile_debug; | |
59 | ||
946d3d10 KS |
60 | /* Object of this type are stored in the compiler's symbol_err_map. */ |
61 | ||
62 | struct symbol_error | |
63 | { | |
64 | /* The symbol. */ | |
65 | ||
66 | const struct symbol *sym; | |
67 | ||
68 | /* The error message to emit. This is malloc'd and owned by the | |
69 | hash table. */ | |
70 | ||
71 | char *message; | |
72 | }; | |
73 | ||
74 | /* Hash a type_map_instance. */ | |
75 | ||
76 | static hashval_t | |
77 | hash_type_map_instance (const void *p) | |
78 | { | |
79 | const struct type_map_instance *inst = (const struct type_map_instance *) p; | |
80 | ||
81 | return htab_hash_pointer (inst->type); | |
82 | } | |
83 | ||
84 | /* Check two type_map_instance objects for equality. */ | |
85 | ||
86 | static int | |
87 | eq_type_map_instance (const void *a, const void *b) | |
88 | { | |
89 | const struct type_map_instance *insta = (const struct type_map_instance *) a; | |
90 | const struct type_map_instance *instb = (const struct type_map_instance *) b; | |
91 | ||
92 | return insta->type == instb->type; | |
93 | } | |
94 | ||
95 | /* Hash function for struct symbol_error. */ | |
96 | ||
97 | static hashval_t | |
98 | hash_symbol_error (const void *a) | |
99 | { | |
100 | const struct symbol_error *se = (const struct symbol_error *) a; | |
101 | ||
102 | return htab_hash_pointer (se->sym); | |
103 | } | |
104 | ||
105 | /* Equality function for struct symbol_error. */ | |
106 | ||
107 | static int | |
108 | eq_symbol_error (const void *a, const void *b) | |
109 | { | |
110 | const struct symbol_error *sea = (const struct symbol_error *) a; | |
111 | const struct symbol_error *seb = (const struct symbol_error *) b; | |
112 | ||
113 | return sea->sym == seb->sym; | |
114 | } | |
115 | ||
116 | /* Deletion function for struct symbol_error. */ | |
117 | ||
118 | static void | |
119 | del_symbol_error (void *a) | |
120 | { | |
121 | struct symbol_error *se = (struct symbol_error *) a; | |
122 | ||
123 | xfree (se->message); | |
124 | xfree (se); | |
125 | } | |
126 | ||
127 | /* Constructor for compile_instance. */ | |
128 | ||
129 | compile_instance::compile_instance (struct gcc_base_context *gcc_fe, | |
130 | const char *options) | |
131 | : m_gcc_fe (gcc_fe), m_gcc_target_options (options), | |
0cfbf430 KS |
132 | m_type_map (htab_create_alloc (10, hash_type_map_instance, |
133 | eq_type_map_instance, | |
134 | xfree, xcalloc, xfree)), | |
135 | m_symbol_err_map (htab_create_alloc (10, hash_symbol_error, | |
136 | eq_symbol_error, del_symbol_error, | |
137 | xcalloc, xfree)) | |
946d3d10 | 138 | { |
946d3d10 KS |
139 | } |
140 | ||
141 | /* See compile-internal.h. */ | |
142 | ||
143 | bool | |
d82b3862 | 144 | compile_instance::get_cached_type (struct type *type, gcc_type *ret) const |
946d3d10 KS |
145 | { |
146 | struct type_map_instance inst, *found; | |
147 | ||
148 | inst.type = type; | |
0cfbf430 | 149 | found = (struct type_map_instance *) htab_find (m_type_map.get (), &inst); |
946d3d10 KS |
150 | if (found != NULL) |
151 | { | |
d82b3862 | 152 | *ret = found->gcc_type_handle; |
946d3d10 KS |
153 | return true; |
154 | } | |
155 | ||
156 | return false; | |
157 | } | |
158 | ||
159 | /* See compile-internal.h. */ | |
160 | ||
161 | void | |
162 | compile_instance::insert_type (struct type *type, gcc_type gcc_type) | |
163 | { | |
164 | struct type_map_instance inst, *add; | |
165 | void **slot; | |
166 | ||
167 | inst.type = type; | |
168 | inst.gcc_type_handle = gcc_type; | |
0cfbf430 | 169 | slot = htab_find_slot (m_type_map.get (), &inst, INSERT); |
946d3d10 KS |
170 | |
171 | add = (struct type_map_instance *) *slot; | |
172 | /* The type might have already been inserted in order to handle | |
173 | recursive types. */ | |
174 | if (add != NULL && add->gcc_type_handle != gcc_type) | |
175 | error (_("Unexpected type id from GCC, check you use recent enough GCC.")); | |
176 | ||
177 | if (add == NULL) | |
178 | { | |
179 | add = XNEW (struct type_map_instance); | |
180 | *add = inst; | |
181 | *slot = add; | |
182 | } | |
183 | } | |
184 | ||
185 | /* See compile-internal.h. */ | |
186 | ||
187 | void | |
188 | compile_instance::insert_symbol_error (const struct symbol *sym, | |
189 | const char *text) | |
190 | { | |
191 | struct symbol_error e; | |
192 | void **slot; | |
193 | ||
946d3d10 | 194 | e.sym = sym; |
0cfbf430 | 195 | slot = htab_find_slot (m_symbol_err_map.get (), &e, INSERT); |
946d3d10 KS |
196 | if (*slot == NULL) |
197 | { | |
b926417a | 198 | struct symbol_error *ep = XNEW (struct symbol_error); |
946d3d10 | 199 | |
b926417a TT |
200 | ep->sym = sym; |
201 | ep->message = xstrdup (text); | |
202 | *slot = ep; | |
946d3d10 KS |
203 | } |
204 | } | |
205 | ||
206 | /* See compile-internal.h. */ | |
207 | ||
208 | void | |
209 | compile_instance::error_symbol_once (const struct symbol *sym) | |
210 | { | |
211 | struct symbol_error search; | |
212 | struct symbol_error *err; | |
213 | ||
214 | if (m_symbol_err_map == NULL) | |
215 | return; | |
216 | ||
217 | search.sym = sym; | |
0cfbf430 | 218 | err = (struct symbol_error *) htab_find (m_symbol_err_map.get (), &search); |
946d3d10 KS |
219 | if (err == NULL || err->message == NULL) |
220 | return; | |
221 | ||
222 | gdb::unique_xmalloc_ptr<char> message (err->message); | |
223 | err->message = NULL; | |
224 | error (_("%s"), message.get ()); | |
225 | } | |
226 | ||
bb2ec1b3 TT |
227 | /* Implement "show debug compile". */ |
228 | ||
229 | static void | |
230 | show_compile_debug (struct ui_file *file, int from_tty, | |
231 | struct cmd_list_element *c, const char *value) | |
232 | { | |
233 | fprintf_filtered (file, _("Compile debugging is %s.\n"), value); | |
234 | } | |
235 | ||
236 | \f | |
237 | ||
238 | /* Check *ARG for a "-raw" or "-r" argument. Return 0 if not seen. | |
239 | Return 1 if seen and update *ARG. */ | |
240 | ||
241 | static int | |
6663cf91 | 242 | check_raw_argument (const char **arg) |
bb2ec1b3 TT |
243 | { |
244 | *arg = skip_spaces (*arg); | |
245 | ||
246 | if (arg != NULL | |
247 | && (check_for_argument (arg, "-raw", sizeof ("-raw") - 1) | |
248 | || check_for_argument (arg, "-r", sizeof ("-r") - 1))) | |
249 | return 1; | |
250 | return 0; | |
251 | } | |
252 | ||
253 | /* Handle the input from the 'compile file' command. The "compile | |
254 | file" command is used to evaluate an expression contained in a file | |
255 | that may contain calls to the GCC compiler. */ | |
256 | ||
257 | static void | |
6663cf91 | 258 | compile_file_command (const char *arg, int from_tty) |
bb2ec1b3 TT |
259 | { |
260 | enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE; | |
bb2ec1b3 | 261 | |
b7b633e9 | 262 | scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); |
bb2ec1b3 TT |
263 | |
264 | /* Check the user did not just <enter> after command. */ | |
265 | if (arg == NULL) | |
266 | error (_("You must provide a filename for this command.")); | |
267 | ||
268 | /* Check if a raw (-r|-raw) argument is provided. */ | |
269 | if (arg != NULL && check_raw_argument (&arg)) | |
270 | { | |
271 | scope = COMPILE_I_RAW_SCOPE; | |
272 | arg = skip_spaces (arg); | |
273 | } | |
274 | ||
275 | /* After processing arguments, check there is a filename at the end | |
276 | of the command. */ | |
277 | if (arg[0] == '\0') | |
278 | error (_("You must provide a filename with the raw option set.")); | |
279 | ||
280 | if (arg[0] == '-') | |
281 | error (_("Unknown argument specified.")); | |
282 | ||
283 | arg = skip_spaces (arg); | |
e3e41d58 TT |
284 | gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (arg); |
285 | std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ()); | |
286 | eval_compile_command (NULL, buffer.c_str (), scope, NULL); | |
bb2ec1b3 TT |
287 | } |
288 | ||
289 | /* Handle the input from the 'compile code' command. The | |
290 | "compile code" command is used to evaluate an expression that may | |
291 | contain calls to the GCC compiler. The language expected in this | |
292 | compile command is the language currently set in GDB. */ | |
293 | ||
294 | static void | |
6663cf91 | 295 | compile_code_command (const char *arg, int from_tty) |
bb2ec1b3 | 296 | { |
bb2ec1b3 TT |
297 | enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE; |
298 | ||
b7b633e9 | 299 | scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); |
bb2ec1b3 TT |
300 | |
301 | if (arg != NULL && check_raw_argument (&arg)) | |
302 | { | |
303 | scope = COMPILE_I_RAW_SCOPE; | |
304 | arg = skip_spaces (arg); | |
305 | } | |
306 | ||
307 | arg = skip_spaces (arg); | |
308 | ||
309 | if (arg != NULL && !check_for_argument (&arg, "--", sizeof ("--") - 1)) | |
310 | { | |
311 | if (arg[0] == '-') | |
312 | error (_("Unknown argument specified.")); | |
313 | } | |
314 | ||
315 | if (arg && *arg) | |
5c65b58a | 316 | eval_compile_command (NULL, arg, scope, NULL); |
bb2ec1b3 TT |
317 | else |
318 | { | |
12973681 | 319 | counted_command_line l = get_command_line (compile_control, ""); |
bb2ec1b3 | 320 | |
bb2ec1b3 | 321 | l->control_u.compile.scope = scope; |
93921405 | 322 | execute_control_command_untraced (l.get ()); |
bb2ec1b3 | 323 | } |
bb2ec1b3 TT |
324 | } |
325 | ||
36de76f9 JK |
326 | /* Callback for compile_print_command. */ |
327 | ||
328 | void | |
329 | compile_print_value (struct value *val, void *data_voidp) | |
330 | { | |
9a3c8263 | 331 | const struct format_data *fmtp = (const struct format_data *) data_voidp; |
36de76f9 JK |
332 | |
333 | print_value (val, fmtp); | |
334 | } | |
335 | ||
336 | /* Handle the input from the 'compile print' command. The "compile | |
337 | print" command is used to evaluate and print an expression that may | |
338 | contain calls to the GCC compiler. The language expected in this | |
339 | compile command is the language currently set in GDB. */ | |
340 | ||
341 | static void | |
6663cf91 | 342 | compile_print_command (const char *arg, int from_tty) |
36de76f9 | 343 | { |
36de76f9 JK |
344 | enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE; |
345 | struct format_data fmt; | |
346 | ||
b7b633e9 | 347 | scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); |
36de76f9 JK |
348 | |
349 | /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not | |
350 | touch the stale pointer if compile_object_run has already quit. */ | |
351 | print_command_parse_format (&arg, "compile print", &fmt); | |
352 | ||
353 | if (arg && *arg) | |
354 | eval_compile_command (NULL, arg, scope, &fmt); | |
355 | else | |
356 | { | |
12973681 | 357 | counted_command_line l = get_command_line (compile_control, ""); |
36de76f9 | 358 | |
36de76f9 JK |
359 | l->control_u.compile.scope = scope; |
360 | l->control_u.compile.scope_data = &fmt; | |
93921405 | 361 | execute_control_command_untraced (l.get ()); |
36de76f9 | 362 | } |
36de76f9 JK |
363 | } |
364 | ||
bb2ec1b3 TT |
365 | /* A cleanup function to remove a directory and all its contents. */ |
366 | ||
367 | static void | |
368 | do_rmdir (void *arg) | |
369 | { | |
9a3c8263 | 370 | const char *dir = (const char *) arg; |
bb2ec1b3 | 371 | char *zap; |
3ce348af CG |
372 | int wstat; |
373 | ||
61012eef | 374 | gdb_assert (startswith (dir, TMP_PREFIX)); |
bb2ec1b3 | 375 | zap = concat ("rm -rf ", dir, (char *) NULL); |
3ce348af CG |
376 | wstat = system (zap); |
377 | if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0) | |
378 | warning (_("Could not remove temporary directory %s"), dir); | |
379 | XDELETEVEC (zap); | |
bb2ec1b3 TT |
380 | } |
381 | ||
382 | /* Return the name of the temporary directory to use for .o files, and | |
383 | arrange for the directory to be removed at shutdown. */ | |
384 | ||
385 | static const char * | |
386 | get_compile_file_tempdir (void) | |
387 | { | |
388 | static char *tempdir_name; | |
389 | ||
390 | #define TEMPLATE TMP_PREFIX "XXXXXX" | |
391 | char tname[sizeof (TEMPLATE)]; | |
392 | ||
393 | if (tempdir_name != NULL) | |
394 | return tempdir_name; | |
395 | ||
396 | strcpy (tname, TEMPLATE); | |
397 | #undef TEMPLATE | |
1bc1068a | 398 | #ifdef HAVE_MKDTEMP |
bb2ec1b3 | 399 | tempdir_name = mkdtemp (tname); |
1bc1068a JK |
400 | #else |
401 | error (_("Command not supported on this host.")); | |
402 | #endif | |
bb2ec1b3 TT |
403 | if (tempdir_name == NULL) |
404 | perror_with_name (_("Could not make temporary directory")); | |
405 | ||
406 | tempdir_name = xstrdup (tempdir_name); | |
407 | make_final_cleanup (do_rmdir, tempdir_name); | |
408 | return tempdir_name; | |
409 | } | |
410 | ||
aaee65ae | 411 | /* Compute the names of source and object files to use. */ |
bb2ec1b3 | 412 | |
aaee65ae PA |
413 | static compile_file_names |
414 | get_new_file_names () | |
bb2ec1b3 TT |
415 | { |
416 | static int seq; | |
417 | const char *dir = get_compile_file_tempdir (); | |
418 | ||
419 | ++seq; | |
aaee65ae PA |
420 | |
421 | return compile_file_names (string_printf ("%s%sout%d.c", | |
422 | dir, SLASH_STRING, seq), | |
423 | string_printf ("%s%sout%d.o", | |
424 | dir, SLASH_STRING, seq)); | |
bb2ec1b3 TT |
425 | } |
426 | ||
427 | /* Get the block and PC at which to evaluate an expression. */ | |
428 | ||
429 | static const struct block * | |
430 | get_expr_block_and_pc (CORE_ADDR *pc) | |
431 | { | |
432 | const struct block *block = get_selected_block (pc); | |
433 | ||
434 | if (block == NULL) | |
435 | { | |
436 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); | |
437 | ||
438 | if (cursal.symtab) | |
439 | block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab), | |
440 | STATIC_BLOCK); | |
441 | if (block != NULL) | |
2b1ffcfd | 442 | *pc = BLOCK_ENTRY_PC (block); |
bb2ec1b3 TT |
443 | } |
444 | else | |
2b1ffcfd | 445 | *pc = BLOCK_ENTRY_PC (block); |
bb2ec1b3 TT |
446 | |
447 | return block; | |
448 | } | |
449 | ||
773a1edc TT |
450 | /* Call buildargv (via gdb_argv), set its result for S into *ARGVP but |
451 | calculate also the number of parsed arguments into *ARGCP. If | |
452 | buildargv has returned NULL then *ARGCP is set to zero. */ | |
bb2ec1b3 TT |
453 | |
454 | static void | |
455 | build_argc_argv (const char *s, int *argcp, char ***argvp) | |
456 | { | |
773a1edc TT |
457 | gdb_argv args (s); |
458 | ||
459 | *argcp = args.count (); | |
460 | *argvp = args.release (); | |
bb2ec1b3 TT |
461 | } |
462 | ||
463 | /* String for 'set compile-args' and 'show compile-args'. */ | |
464 | static char *compile_args; | |
465 | ||
466 | /* Parsed form of COMPILE_ARGS. COMPILE_ARGS_ARGV is NULL terminated. */ | |
467 | static int compile_args_argc; | |
468 | static char **compile_args_argv; | |
469 | ||
470 | /* Implement 'set compile-args'. */ | |
471 | ||
472 | static void | |
eb4c3f4a | 473 | set_compile_args (const char *args, int from_tty, struct cmd_list_element *c) |
bb2ec1b3 TT |
474 | { |
475 | freeargv (compile_args_argv); | |
476 | build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv); | |
477 | } | |
478 | ||
479 | /* Implement 'show compile-args'. */ | |
480 | ||
481 | static void | |
482 | show_compile_args (struct ui_file *file, int from_tty, | |
483 | struct cmd_list_element *c, const char *value) | |
484 | { | |
485 | fprintf_filtered (file, _("Compile command command-line arguments " | |
486 | "are \"%s\".\n"), | |
487 | value); | |
488 | } | |
489 | ||
490 | /* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP. | |
491 | ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL. */ | |
492 | ||
493 | static void | |
494 | append_args (int *argcp, char ***argvp, int argc, char **argv) | |
495 | { | |
496 | int argi; | |
497 | ||
8d749320 | 498 | *argvp = XRESIZEVEC (char *, *argvp, (*argcp + argc + 1)); |
bb2ec1b3 TT |
499 | |
500 | for (argi = 0; argi < argc; argi++) | |
501 | (*argvp)[(*argcp)++] = xstrdup (argv[argi]); | |
502 | (*argvp)[(*argcp)] = NULL; | |
503 | } | |
504 | ||
6e41ddec JK |
505 | /* String for 'set compile-gcc' and 'show compile-gcc'. */ |
506 | static char *compile_gcc; | |
507 | ||
508 | /* Implement 'show compile-gcc'. */ | |
509 | ||
510 | static void | |
511 | show_compile_gcc (struct ui_file *file, int from_tty, | |
512 | struct cmd_list_element *c, const char *value) | |
513 | { | |
514 | fprintf_filtered (file, _("Compile command GCC driver filename is \"%s\".\n"), | |
515 | value); | |
516 | } | |
517 | ||
bb2ec1b3 TT |
518 | /* Return DW_AT_producer parsed for get_selected_frame () (if any). |
519 | Return NULL otherwise. | |
520 | ||
521 | GCC already filters its command-line arguments only for the suitable ones to | |
522 | put into DW_AT_producer - see GCC function gen_producer_string. */ | |
523 | ||
524 | static const char * | |
525 | get_selected_pc_producer_options (void) | |
526 | { | |
527 | CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL)); | |
528 | struct compunit_symtab *symtab = find_pc_compunit_symtab (pc); | |
529 | const char *cs; | |
530 | ||
531 | if (symtab == NULL || symtab->producer == NULL | |
61012eef | 532 | || !startswith (symtab->producer, "GNU ")) |
bb2ec1b3 TT |
533 | return NULL; |
534 | ||
535 | cs = symtab->producer; | |
536 | while (*cs != 0 && *cs != '-') | |
f1735a53 | 537 | cs = skip_spaces (skip_to_space (cs)); |
bb2ec1b3 TT |
538 | if (*cs != '-') |
539 | return NULL; | |
540 | return cs; | |
541 | } | |
542 | ||
a7606d80 JK |
543 | /* Filter out unwanted options from *ARGCP and ARGV. */ |
544 | ||
545 | static void | |
546 | filter_args (int *argcp, char **argv) | |
547 | { | |
548 | char **destv; | |
549 | ||
550 | for (destv = argv; *argv != NULL; argv++) | |
551 | { | |
552 | /* -fpreprocessed may get in commonly from ccache. */ | |
553 | if (strcmp (*argv, "-fpreprocessed") == 0) | |
554 | { | |
555 | xfree (*argv); | |
556 | (*argcp)--; | |
557 | continue; | |
558 | } | |
559 | *destv++ = *argv; | |
560 | } | |
561 | *destv = NULL; | |
562 | } | |
563 | ||
e05cac70 PM |
564 | /* Produce final vector of GCC compilation options. |
565 | ||
566 | The first element of the combined argument vector are arguments | |
567 | relating to the target size ("-m64", "-m32" etc.). These are | |
568 | sourced from the inferior's architecture. | |
569 | ||
570 | The second element of the combined argument vector are arguments | |
571 | stored in the inferior DW_AT_producer section. If these are stored | |
572 | in the inferior (there is no guarantee that they are), they are | |
573 | added to the vector. | |
574 | ||
575 | The third element of the combined argument vector are argument | |
576 | supplied by the language implementation provided by | |
577 | compile-{lang}-support. These contain language specific arguments. | |
578 | ||
579 | The final element of the combined argument vector are arguments | |
580 | supplied by the "set compile-args" command. These are always | |
581 | appended last so as to override any of the arguments automatically | |
582 | generated above. */ | |
bb2ec1b3 TT |
583 | |
584 | static void | |
9cdfd9a2 | 585 | get_args (const compile_instance *compiler, struct gdbarch *gdbarch, |
bb2ec1b3 TT |
586 | int *argcp, char ***argvp) |
587 | { | |
588 | const char *cs_producer_options; | |
589 | int argc_compiler; | |
590 | char **argv_compiler; | |
591 | ||
592 | build_argc_argv (gdbarch_gcc_target_options (gdbarch), | |
593 | argcp, argvp); | |
594 | ||
595 | cs_producer_options = get_selected_pc_producer_options (); | |
596 | if (cs_producer_options != NULL) | |
597 | { | |
598 | int argc_producer; | |
599 | char **argv_producer; | |
600 | ||
601 | build_argc_argv (cs_producer_options, &argc_producer, &argv_producer); | |
a7606d80 | 602 | filter_args (&argc_producer, argv_producer); |
bb2ec1b3 TT |
603 | append_args (argcp, argvp, argc_producer, argv_producer); |
604 | freeargv (argv_producer); | |
605 | } | |
606 | ||
9cdfd9a2 | 607 | build_argc_argv (compiler->gcc_target_options ().c_str (), |
bb2ec1b3 TT |
608 | &argc_compiler, &argv_compiler); |
609 | append_args (argcp, argvp, argc_compiler, argv_compiler); | |
610 | freeargv (argv_compiler); | |
611 | ||
612 | append_args (argcp, argvp, compile_args_argc, compile_args_argv); | |
613 | } | |
614 | ||
bb2ec1b3 TT |
615 | /* A helper function suitable for use as the "print_callback" in the |
616 | compiler object. */ | |
617 | ||
618 | static void | |
619 | print_callback (void *ignore, const char *message) | |
620 | { | |
621 | fputs_filtered (message, gdb_stderr); | |
622 | } | |
623 | ||
624 | /* Process the compilation request. On success it returns the object | |
aaee65ae PA |
625 | and source file names. On an error condition, error () is |
626 | called. */ | |
bb2ec1b3 | 627 | |
aaee65ae | 628 | static compile_file_names |
851c9091 | 629 | compile_to_object (struct command_line *cmd, const char *cmd_string, |
aaee65ae | 630 | enum compile_i_scope_types scope) |
bb2ec1b3 | 631 | { |
bb2ec1b3 TT |
632 | const struct block *expr_block; |
633 | CORE_ADDR trash_pc, expr_pc; | |
634 | int argc; | |
635 | char **argv; | |
636 | int ok; | |
bb2ec1b3 | 637 | struct gdbarch *gdbarch = get_current_arch (); |
7d937cad | 638 | std::string triplet_rx; |
bb2ec1b3 TT |
639 | |
640 | if (!target_has_execution) | |
641 | error (_("The program must be running for the compile command to "\ | |
642 | "work.")); | |
643 | ||
644 | expr_block = get_expr_block_and_pc (&trash_pc); | |
645 | expr_pc = get_frame_address_in_block (get_selected_frame (NULL)); | |
646 | ||
647 | /* Set up instance and context for the compiler. */ | |
648 | if (current_language->la_get_compile_instance == NULL) | |
cdaec3f3 LM |
649 | error (_("No compiler support for language %s."), |
650 | current_language->la_name); | |
bb2ec1b3 | 651 | |
9cdfd9a2 KS |
652 | compile_instance *compiler_instance |
653 | = current_language->la_get_compile_instance (); | |
654 | std::unique_ptr<compile_instance> compiler (compiler_instance); | |
655 | compiler->set_print_callback (print_callback, NULL); | |
656 | compiler->set_scope (scope); | |
657 | compiler->set_block (expr_block); | |
bb2ec1b3 TT |
658 | |
659 | /* From the provided expression, build a scope to pass to the | |
660 | compiler. */ | |
aaee65ae | 661 | |
d7e74731 | 662 | string_file input_buf; |
aaee65ae PA |
663 | const char *input; |
664 | ||
bb2ec1b3 TT |
665 | if (cmd != NULL) |
666 | { | |
bb2ec1b3 TT |
667 | struct command_line *iter; |
668 | ||
12973681 | 669 | for (iter = cmd->body_list_0.get (); iter; iter = iter->next) |
bb2ec1b3 | 670 | { |
d7e74731 PA |
671 | input_buf.puts (iter->line); |
672 | input_buf.puts ("\n"); | |
bb2ec1b3 TT |
673 | } |
674 | ||
aaee65ae | 675 | input = input_buf.c_str (); |
bb2ec1b3 TT |
676 | } |
677 | else if (cmd_string != NULL) | |
851c9091 | 678 | input = cmd_string; |
bb2ec1b3 TT |
679 | else |
680 | error (_("Neither a simple expression, or a multi-line specified.")); | |
681 | ||
aaee65ae | 682 | std::string code |
9cdfd9a2 | 683 | = current_language->la_compute_program (compiler.get (), input, gdbarch, |
aaee65ae | 684 | expr_block, expr_pc); |
bb2ec1b3 | 685 | if (compile_debug) |
aaee65ae | 686 | fprintf_unfiltered (gdb_stdlog, "debug output:\n\n%s", code.c_str ()); |
bb2ec1b3 | 687 | |
9cdfd9a2 | 688 | compiler->set_verbose (compile_debug); |
71b30f27 | 689 | |
6e41ddec JK |
690 | if (compile_gcc[0] != 0) |
691 | { | |
9cdfd9a2 | 692 | if (compiler->version () < GCC_FE_VERSION_1) |
6e41ddec JK |
693 | error (_("Command 'set compile-gcc' requires GCC version 6 or higher " |
694 | "(libcc1 interface version 1 or higher)")); | |
695 | ||
9cdfd9a2 | 696 | compiler->set_driver_filename (compile_gcc); |
6e41ddec JK |
697 | } |
698 | else | |
699 | { | |
700 | const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch)); | |
701 | const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch); | |
702 | ||
703 | /* Allow triplets with or without vendor set. */ | |
7d937cad | 704 | triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx; |
9cdfd9a2 | 705 | compiler->set_triplet_regexp (triplet_rx.c_str ()); |
6e41ddec | 706 | } |
bb2ec1b3 TT |
707 | |
708 | /* Set compiler command-line arguments. */ | |
9cdfd9a2 | 709 | get_args (compiler.get (), gdbarch, &argc, &argv); |
773a1edc | 710 | gdb_argv argv_holder (argv); |
bb2ec1b3 | 711 | |
9cdfd9a2 KS |
712 | gdb::unique_xmalloc_ptr<char> error_message; |
713 | error_message.reset (compiler->set_arguments (argc, argv, | |
714 | triplet_rx.c_str ())); | |
715 | ||
bb2ec1b3 | 716 | if (error_message != NULL) |
9cdfd9a2 | 717 | error ("%s", error_message.get ()); |
bb2ec1b3 TT |
718 | |
719 | if (compile_debug) | |
720 | { | |
721 | int argi; | |
722 | ||
a4063588 | 723 | fprintf_unfiltered (gdb_stdlog, "Passing %d compiler options:\n", argc); |
bb2ec1b3 | 724 | for (argi = 0; argi < argc; argi++) |
a4063588 | 725 | fprintf_unfiltered (gdb_stdlog, "Compiler option %d: <%s>\n", |
bb2ec1b3 TT |
726 | argi, argv[argi]); |
727 | } | |
728 | ||
aaee65ae | 729 | compile_file_names fnames = get_new_file_names (); |
bb2ec1b3 | 730 | |
b80cf838 TT |
731 | gdb::optional<gdb::unlinker> source_remover; |
732 | ||
d419f42d TT |
733 | { |
734 | gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w"); | |
735 | if (src == NULL) | |
736 | perror_with_name (_("Could not open source file for writing")); | |
b80cf838 TT |
737 | |
738 | source_remover.emplace (fnames.source_file ()); | |
739 | ||
d419f42d TT |
740 | if (fputs (code.c_str (), src.get ()) == EOF) |
741 | perror_with_name (_("Could not write to source file")); | |
742 | } | |
bb2ec1b3 TT |
743 | |
744 | if (compile_debug) | |
a4063588 | 745 | fprintf_unfiltered (gdb_stdlog, "source file produced: %s\n\n", |
aaee65ae | 746 | fnames.source_file ()); |
bb2ec1b3 TT |
747 | |
748 | /* Call the compiler and start the compilation process. */ | |
9cdfd9a2 KS |
749 | compiler->set_source_file (fnames.source_file ()); |
750 | ok = compiler->compile (fnames.object_file (), compile_debug); | |
e68c32d5 | 751 | if (!ok) |
bb2ec1b3 TT |
752 | error (_("Compilation failed.")); |
753 | ||
754 | if (compile_debug) | |
a4063588 | 755 | fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n", |
aaee65ae | 756 | fnames.object_file ()); |
bb2ec1b3 | 757 | |
b80cf838 TT |
758 | /* Keep the source file. */ |
759 | source_remover->keep (); | |
aaee65ae | 760 | return fnames; |
bb2ec1b3 TT |
761 | } |
762 | ||
763 | /* The "compile" prefix command. */ | |
764 | ||
765 | static void | |
981a3fb3 | 766 | compile_command (const char *args, int from_tty) |
bb2ec1b3 TT |
767 | { |
768 | /* If a sub-command is not specified to the compile prefix command, | |
769 | assume it is a direct code compilation. */ | |
770 | compile_code_command (args, from_tty); | |
771 | } | |
772 | ||
773 | /* See compile.h. */ | |
774 | ||
775 | void | |
851c9091 | 776 | eval_compile_command (struct command_line *cmd, const char *cmd_string, |
5c65b58a | 777 | enum compile_i_scope_types scope, void *scope_data) |
bb2ec1b3 | 778 | { |
aaee65ae PA |
779 | struct compile_module *compile_module; |
780 | ||
781 | compile_file_names fnames = compile_to_object (cmd, cmd_string, scope); | |
bb2ec1b3 | 782 | |
b80cf838 TT |
783 | gdb::unlinker object_remover (fnames.object_file ()); |
784 | gdb::unlinker source_remover (fnames.source_file ()); | |
785 | ||
aaee65ae PA |
786 | compile_module = compile_object_load (fnames, scope, scope_data); |
787 | if (compile_module == NULL) | |
bb2ec1b3 | 788 | { |
aaee65ae PA |
789 | gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE); |
790 | eval_compile_command (cmd, cmd_string, | |
791 | COMPILE_I_PRINT_VALUE_SCOPE, scope_data); | |
792 | return; | |
bb2ec1b3 | 793 | } |
b80cf838 TT |
794 | |
795 | /* Keep the files. */ | |
796 | source_remover.keep (); | |
797 | object_remover.keep (); | |
798 | ||
aaee65ae | 799 | compile_object_run (compile_module); |
bb2ec1b3 TT |
800 | } |
801 | ||
802 | /* See compile/compile-internal.h. */ | |
803 | ||
8f84fb0e | 804 | std::string |
bb2ec1b3 TT |
805 | compile_register_name_mangled (struct gdbarch *gdbarch, int regnum) |
806 | { | |
807 | const char *regname = gdbarch_register_name (gdbarch, regnum); | |
808 | ||
8f84fb0e | 809 | return string_printf ("__%s", regname); |
bb2ec1b3 TT |
810 | } |
811 | ||
812 | /* See compile/compile-internal.h. */ | |
813 | ||
814 | int | |
815 | compile_register_name_demangle (struct gdbarch *gdbarch, | |
816 | const char *regname) | |
817 | { | |
818 | int regnum; | |
819 | ||
820 | if (regname[0] != '_' || regname[1] != '_') | |
821 | error (_("Invalid register name \"%s\"."), regname); | |
822 | regname += 2; | |
823 | ||
824 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) | |
825 | if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0) | |
826 | return regnum; | |
827 | ||
828 | error (_("Cannot find gdbarch register \"%s\"."), regname); | |
829 | } | |
830 | ||
9cdfd9a2 KS |
831 | /* Forwards to the plug-in. */ |
832 | ||
833 | #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__)) | |
834 | ||
835 | /* See compile-internal.h. */ | |
836 | ||
837 | void | |
838 | compile_instance::set_print_callback | |
839 | (void (*print_function) (void *, const char *), void *datum) | |
840 | { | |
841 | FORWARD (set_print_callback, print_function, datum); | |
842 | } | |
843 | ||
844 | /* See compile-internal.h. */ | |
845 | ||
846 | unsigned int | |
847 | compile_instance::version () const | |
848 | { | |
849 | return m_gcc_fe->ops->version; | |
850 | } | |
851 | ||
852 | /* See compile-internal.h. */ | |
853 | ||
854 | void | |
855 | compile_instance::set_verbose (int level) | |
856 | { | |
857 | if (version () >= GCC_FE_VERSION_1) | |
858 | FORWARD (set_verbose, level); | |
859 | } | |
860 | ||
861 | /* See compile-internal.h. */ | |
862 | ||
863 | void | |
864 | compile_instance::set_driver_filename (const char *filename) | |
865 | { | |
866 | if (version () >= GCC_FE_VERSION_1) | |
867 | FORWARD (set_driver_filename, filename); | |
868 | } | |
869 | ||
870 | /* See compile-internal.h. */ | |
871 | ||
872 | void | |
873 | compile_instance::set_triplet_regexp (const char *regexp) | |
874 | { | |
875 | if (version () >= GCC_FE_VERSION_1) | |
876 | FORWARD (set_triplet_regexp, regexp); | |
877 | } | |
878 | ||
879 | /* See compile-internal.h. */ | |
880 | ||
881 | char * | |
882 | compile_instance::set_arguments (int argc, char **argv, const char *regexp) | |
883 | { | |
884 | if (version () >= GCC_FE_VERSION_1) | |
885 | return FORWARD (set_arguments, argc, argv); | |
886 | else | |
887 | return FORWARD (set_arguments_v0, regexp, argc, argv); | |
888 | } | |
889 | ||
890 | /* See compile-internal.h. */ | |
891 | ||
892 | void | |
893 | compile_instance::set_source_file (const char *filename) | |
894 | { | |
895 | FORWARD (set_source_file, filename); | |
896 | } | |
897 | ||
898 | /* See compile-internal.h. */ | |
899 | ||
900 | bool | |
901 | compile_instance::compile (const char *filename, int verbose_level) | |
902 | { | |
903 | if (version () >= GCC_FE_VERSION_1) | |
904 | return FORWARD (compile, filename); | |
905 | else | |
906 | return FORWARD (compile_v0, filename, verbose_level); | |
907 | } | |
908 | ||
909 | #undef FORWARD | |
910 | ||
8588b356 SM |
911 | /* See compile.h. */ |
912 | cmd_list_element *compile_cmd_element = nullptr; | |
9cdfd9a2 | 913 | |
bb2ec1b3 TT |
914 | void |
915 | _initialize_compile (void) | |
916 | { | |
917 | struct cmd_list_element *c = NULL; | |
918 | ||
8588b356 SM |
919 | compile_cmd_element = add_prefix_cmd ("compile", class_obscure, |
920 | compile_command, _("\ | |
bb2ec1b3 TT |
921 | Command to compile source code and inject it into the inferior."), |
922 | &compile_command_list, "compile ", 1, &cmdlist); | |
923 | add_com_alias ("expression", "compile", class_obscure, 0); | |
924 | ||
925 | add_cmd ("code", class_obscure, compile_code_command, | |
926 | _("\ | |
927 | Compile, inject, and execute code.\n\ | |
928 | \n\ | |
929 | Usage: compile code [-r|-raw] [--] [CODE]\n\ | |
930 | -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping.\n\ | |
931 | --: Do not parse any options beyond this delimiter. All text to the\n\ | |
932 | right will be treated as source code.\n\ | |
933 | \n\ | |
934 | The source code may be specified as a simple one line expression, e.g.:\n\ | |
935 | \n\ | |
936 | compile code printf(\"Hello world\\n\");\n\ | |
937 | \n\ | |
36de76f9 JK |
938 | Alternatively, you can type a multiline expression by invoking\n\ |
939 | this command with no argument. GDB will then prompt for the\n\ | |
940 | expression interactively; type a line containing \"end\" to\n\ | |
941 | indicate the end of the expression."), | |
bb2ec1b3 TT |
942 | &compile_command_list); |
943 | ||
944 | c = add_cmd ("file", class_obscure, compile_file_command, | |
945 | _("\ | |
946 | Evaluate a file containing source code.\n\ | |
947 | \n\ | |
948 | Usage: compile file [-r|-raw] [filename]\n\ | |
949 | -r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping."), | |
950 | &compile_command_list); | |
951 | set_cmd_completer (c, filename_completer); | |
952 | ||
36de76f9 JK |
953 | add_cmd ("print", class_obscure, compile_print_command, |
954 | _("\ | |
955 | Evaluate EXPR by using the compiler and print result.\n\ | |
956 | \n\ | |
957 | Usage: compile print[/FMT] [EXPR]\n\ | |
958 | \n\ | |
959 | The expression may be specified on the same line as the command, e.g.:\n\ | |
960 | \n\ | |
961 | compile print i\n\ | |
962 | \n\ | |
963 | Alternatively, you can type a multiline expression by invoking\n\ | |
964 | this command with no argument. GDB will then prompt for the\n\ | |
965 | expression interactively; type a line containing \"end\" to\n\ | |
966 | indicate the end of the expression.\n\ | |
967 | \n\ | |
968 | EXPR may be preceded with /FMT, where FMT is a format letter\n\ | |
969 | but no count or size letter (see \"x\" command)."), | |
970 | &compile_command_list); | |
971 | ||
bb2ec1b3 TT |
972 | add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\ |
973 | Set compile command debugging."), _("\ | |
974 | Show compile command debugging."), _("\ | |
975 | When on, compile command debugging is enabled."), | |
976 | NULL, show_compile_debug, | |
977 | &setdebuglist, &showdebuglist); | |
978 | ||
979 | add_setshow_string_cmd ("compile-args", class_support, | |
980 | &compile_args, | |
981 | _("Set compile command GCC command-line arguments"), | |
982 | _("Show compile command GCC command-line arguments"), | |
983 | _("\ | |
984 | Use options like -I (include file directory) or ABI settings.\n\ | |
985 | String quoting is parsed like in shell, for example:\n\ | |
986 | -mno-align-double \"-I/dir with a space/include\""), | |
987 | set_compile_args, show_compile_args, &setlist, &showlist); | |
988 | ||
989 | /* Override flags possibly coming from DW_AT_producer. */ | |
990 | compile_args = xstrdup ("-O0 -gdwarf-4" | |
4b62a76e | 991 | /* We use -fPIE Otherwise GDB would need to reserve space large enough for |
bb2ec1b3 TT |
992 | any object file in the inferior in advance to get the final address when |
993 | to link the object file to and additionally the default system linker | |
994 | script would need to be modified so that one can specify there the | |
4b62a76e JK |
995 | absolute target address. |
996 | -fPIC is not used at is would require from GDB to generate .got. */ | |
997 | " -fPIE" | |
3a9558c4 JK |
998 | /* We want warnings, except for some commonly happening for GDB commands. */ |
999 | " -Wall " | |
3a9558c4 JK |
1000 | " -Wno-unused-but-set-variable" |
1001 | " -Wno-unused-variable" | |
bb2ec1b3 TT |
1002 | /* Override CU's possible -fstack-protector-strong. */ |
1003 | " -fno-stack-protector" | |
1004 | ); | |
1005 | set_compile_args (compile_args, 0, NULL); | |
6e41ddec JK |
1006 | |
1007 | add_setshow_optional_filename_cmd ("compile-gcc", class_support, | |
1008 | &compile_gcc, | |
1009 | _("Set compile command " | |
1010 | "GCC driver filename"), | |
1011 | _("Show compile command " | |
1012 | "GCC driver filename"), | |
1013 | _("\ | |
1014 | It should be absolute filename of the gcc executable.\n\ | |
1015 | If empty the default target triplet will be searched in $PATH."), | |
1016 | NULL, show_compile_gcc, &setlist, | |
1017 | &showlist); | |
1018 | compile_gcc = xstrdup (""); | |
bb2ec1b3 | 1019 | } |