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