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