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