15e098073Schristos /* General Compile and inject code
25e098073Schristos
3*1424dfb3Schristos Copyright (C) 2014-2020 Free Software Foundation, Inc.
45e098073Schristos
55e098073Schristos This file is part of GDB.
65e098073Schristos
75e098073Schristos This program is free software; you can redistribute it and/or modify
85e098073Schristos it under the terms of the GNU General Public License as published by
95e098073Schristos the Free Software Foundation; either version 3 of the License, or
105e098073Schristos (at your option) any later version.
115e098073Schristos
125e098073Schristos This program is distributed in the hope that it will be useful,
135e098073Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
145e098073Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155e098073Schristos GNU General Public License for more details.
165e098073Schristos
175e098073Schristos You should have received a copy of the GNU General Public License
185e098073Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */
195e098073Schristos
205e098073Schristos #include "defs.h"
21c03b94e9Schristos #include "top.h"
225e098073Schristos #include "ui-out.h"
235e098073Schristos #include "command.h"
245e098073Schristos #include "cli/cli-script.h"
255e098073Schristos #include "cli/cli-utils.h"
26*1424dfb3Schristos #include "cli/cli-option.h"
275e098073Schristos #include "completer.h"
285e098073Schristos #include "gdbcmd.h"
295e098073Schristos #include "compile.h"
305e098073Schristos #include "compile-internal.h"
315e098073Schristos #include "compile-object-load.h"
325e098073Schristos #include "compile-object-run.h"
335e098073Schristos #include "language.h"
345e098073Schristos #include "frame.h"
355e098073Schristos #include "source.h"
365e098073Schristos #include "block.h"
375e098073Schristos #include "arch-utils.h"
38*1424dfb3Schristos #include "gdbsupport/filestuff.h"
395e098073Schristos #include "target.h"
405e098073Schristos #include "osabi.h"
41*1424dfb3Schristos #include "gdbsupport/gdb_wait.h"
42ed6a76a9Schristos #include "valprint.h"
43*1424dfb3Schristos #include "gdbsupport/gdb_optional.h"
44*1424dfb3Schristos #include "gdbsupport/gdb_unlinker.h"
45*1424dfb3Schristos #include "gdbsupport/pathstuff.h"
465e098073Schristos
475e098073Schristos
485e098073Schristos
495e098073Schristos /* Initial filename for temporary files. */
505e098073Schristos
515e098073Schristos #define TMP_PREFIX "/tmp/gdbobj-"
525e098073Schristos
535e098073Schristos /* Hold "compile" commands. */
545e098073Schristos
555e098073Schristos static struct cmd_list_element *compile_command_list;
565e098073Schristos
575e098073Schristos /* Debug flag for "compile" commands. */
585e098073Schristos
59*1424dfb3Schristos bool compile_debug;
605e098073Schristos
6107163879Schristos /* Object of this type are stored in the compiler's symbol_err_map. */
6207163879Schristos
6307163879Schristos struct symbol_error
6407163879Schristos {
6507163879Schristos /* The symbol. */
6607163879Schristos
6707163879Schristos const struct symbol *sym;
6807163879Schristos
6907163879Schristos /* The error message to emit. This is malloc'd and owned by the
7007163879Schristos hash table. */
7107163879Schristos
7207163879Schristos char *message;
7307163879Schristos };
7407163879Schristos
7507163879Schristos /* Hash a type_map_instance. */
7607163879Schristos
7707163879Schristos static hashval_t
hash_type_map_instance(const void * p)7807163879Schristos hash_type_map_instance (const void *p)
7907163879Schristos {
8007163879Schristos const struct type_map_instance *inst = (const struct type_map_instance *) p;
8107163879Schristos
8207163879Schristos return htab_hash_pointer (inst->type);
8307163879Schristos }
8407163879Schristos
8507163879Schristos /* Check two type_map_instance objects for equality. */
8607163879Schristos
8707163879Schristos static int
eq_type_map_instance(const void * a,const void * b)8807163879Schristos eq_type_map_instance (const void *a, const void *b)
8907163879Schristos {
9007163879Schristos const struct type_map_instance *insta = (const struct type_map_instance *) a;
9107163879Schristos const struct type_map_instance *instb = (const struct type_map_instance *) b;
9207163879Schristos
9307163879Schristos return insta->type == instb->type;
9407163879Schristos }
9507163879Schristos
9607163879Schristos /* Hash function for struct symbol_error. */
9707163879Schristos
9807163879Schristos static hashval_t
hash_symbol_error(const void * a)9907163879Schristos hash_symbol_error (const void *a)
10007163879Schristos {
10107163879Schristos const struct symbol_error *se = (const struct symbol_error *) a;
10207163879Schristos
10307163879Schristos return htab_hash_pointer (se->sym);
10407163879Schristos }
10507163879Schristos
10607163879Schristos /* Equality function for struct symbol_error. */
10707163879Schristos
10807163879Schristos static int
eq_symbol_error(const void * a,const void * b)10907163879Schristos eq_symbol_error (const void *a, const void *b)
11007163879Schristos {
11107163879Schristos const struct symbol_error *sea = (const struct symbol_error *) a;
11207163879Schristos const struct symbol_error *seb = (const struct symbol_error *) b;
11307163879Schristos
11407163879Schristos return sea->sym == seb->sym;
11507163879Schristos }
11607163879Schristos
11707163879Schristos /* Deletion function for struct symbol_error. */
11807163879Schristos
11907163879Schristos static void
del_symbol_error(void * a)12007163879Schristos del_symbol_error (void *a)
12107163879Schristos {
12207163879Schristos struct symbol_error *se = (struct symbol_error *) a;
12307163879Schristos
12407163879Schristos xfree (se->message);
12507163879Schristos xfree (se);
12607163879Schristos }
12707163879Schristos
12807163879Schristos /* Constructor for compile_instance. */
12907163879Schristos
compile_instance(struct gcc_base_context * gcc_fe,const char * options)13007163879Schristos compile_instance::compile_instance (struct gcc_base_context *gcc_fe,
13107163879Schristos const char *options)
13207163879Schristos : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
13307163879Schristos m_type_map (htab_create_alloc (10, hash_type_map_instance,
13407163879Schristos eq_type_map_instance,
13507163879Schristos xfree, xcalloc, xfree)),
13607163879Schristos m_symbol_err_map (htab_create_alloc (10, hash_symbol_error,
13707163879Schristos eq_symbol_error, del_symbol_error,
13807163879Schristos xcalloc, xfree))
13907163879Schristos {
14007163879Schristos }
14107163879Schristos
14207163879Schristos /* See compile-internal.h. */
14307163879Schristos
14407163879Schristos bool
get_cached_type(struct type * type,gcc_type * ret)14507163879Schristos compile_instance::get_cached_type (struct type *type, gcc_type *ret) const
14607163879Schristos {
14707163879Schristos struct type_map_instance inst, *found;
14807163879Schristos
14907163879Schristos inst.type = type;
15007163879Schristos found = (struct type_map_instance *) htab_find (m_type_map.get (), &inst);
15107163879Schristos if (found != NULL)
15207163879Schristos {
15307163879Schristos *ret = found->gcc_type_handle;
15407163879Schristos return true;
15507163879Schristos }
15607163879Schristos
15707163879Schristos return false;
15807163879Schristos }
15907163879Schristos
16007163879Schristos /* See compile-internal.h. */
16107163879Schristos
16207163879Schristos void
insert_type(struct type * type,gcc_type gcc_type)16307163879Schristos compile_instance::insert_type (struct type *type, gcc_type gcc_type)
16407163879Schristos {
16507163879Schristos struct type_map_instance inst, *add;
16607163879Schristos void **slot;
16707163879Schristos
16807163879Schristos inst.type = type;
16907163879Schristos inst.gcc_type_handle = gcc_type;
17007163879Schristos slot = htab_find_slot (m_type_map.get (), &inst, INSERT);
17107163879Schristos
17207163879Schristos add = (struct type_map_instance *) *slot;
17307163879Schristos /* The type might have already been inserted in order to handle
17407163879Schristos recursive types. */
17507163879Schristos if (add != NULL && add->gcc_type_handle != gcc_type)
17607163879Schristos error (_("Unexpected type id from GCC, check you use recent enough GCC."));
17707163879Schristos
17807163879Schristos if (add == NULL)
17907163879Schristos {
18007163879Schristos add = XNEW (struct type_map_instance);
18107163879Schristos *add = inst;
18207163879Schristos *slot = add;
18307163879Schristos }
18407163879Schristos }
18507163879Schristos
18607163879Schristos /* See compile-internal.h. */
18707163879Schristos
18807163879Schristos void
insert_symbol_error(const struct symbol * sym,const char * text)18907163879Schristos compile_instance::insert_symbol_error (const struct symbol *sym,
19007163879Schristos const char *text)
19107163879Schristos {
19207163879Schristos struct symbol_error e;
19307163879Schristos void **slot;
19407163879Schristos
19507163879Schristos e.sym = sym;
19607163879Schristos slot = htab_find_slot (m_symbol_err_map.get (), &e, INSERT);
19707163879Schristos if (*slot == NULL)
19807163879Schristos {
19907163879Schristos struct symbol_error *ep = XNEW (struct symbol_error);
20007163879Schristos
20107163879Schristos ep->sym = sym;
20207163879Schristos ep->message = xstrdup (text);
20307163879Schristos *slot = ep;
20407163879Schristos }
20507163879Schristos }
20607163879Schristos
20707163879Schristos /* See compile-internal.h. */
20807163879Schristos
20907163879Schristos void
error_symbol_once(const struct symbol * sym)21007163879Schristos compile_instance::error_symbol_once (const struct symbol *sym)
21107163879Schristos {
21207163879Schristos struct symbol_error search;
21307163879Schristos struct symbol_error *err;
21407163879Schristos
21507163879Schristos if (m_symbol_err_map == NULL)
21607163879Schristos return;
21707163879Schristos
21807163879Schristos search.sym = sym;
21907163879Schristos err = (struct symbol_error *) htab_find (m_symbol_err_map.get (), &search);
22007163879Schristos if (err == NULL || err->message == NULL)
22107163879Schristos return;
22207163879Schristos
22307163879Schristos gdb::unique_xmalloc_ptr<char> message (err->message);
22407163879Schristos err->message = NULL;
22507163879Schristos error (_("%s"), message.get ());
22607163879Schristos }
22707163879Schristos
2285e098073Schristos /* Implement "show debug compile". */
2295e098073Schristos
2305e098073Schristos static void
show_compile_debug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2315e098073Schristos show_compile_debug (struct ui_file *file, int from_tty,
2325e098073Schristos struct cmd_list_element *c, const char *value)
2335e098073Schristos {
2345e098073Schristos fprintf_filtered (file, _("Compile debugging is %s.\n"), value);
2355e098073Schristos }
2365e098073Schristos
2375e098073Schristos
2385e098073Schristos
239*1424dfb3Schristos /* Options for the compile command. */
2405e098073Schristos
241*1424dfb3Schristos struct compile_options
2425e098073Schristos {
243*1424dfb3Schristos /* For -raw. */
244*1424dfb3Schristos bool raw = false;
245*1424dfb3Schristos };
2465e098073Schristos
247*1424dfb3Schristos using compile_flag_option_def
248*1424dfb3Schristos = gdb::option::flag_option_def<compile_options>;
249*1424dfb3Schristos
250*1424dfb3Schristos static const gdb::option::option_def compile_command_option_defs[] = {
251*1424dfb3Schristos
252*1424dfb3Schristos compile_flag_option_def {
253*1424dfb3Schristos "raw",
254*1424dfb3Schristos [] (compile_options *opts) { return &opts->raw; },
255*1424dfb3Schristos N_("Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
256*1424dfb3Schristos },
257*1424dfb3Schristos
258*1424dfb3Schristos };
259*1424dfb3Schristos
260*1424dfb3Schristos /* Create an option_def_group for the "compile" command's options,
261*1424dfb3Schristos with OPTS as context. */
262*1424dfb3Schristos
263*1424dfb3Schristos static gdb::option::option_def_group
make_compile_options_def_group(compile_options * opts)264*1424dfb3Schristos make_compile_options_def_group (compile_options *opts)
265*1424dfb3Schristos {
266*1424dfb3Schristos return {{compile_command_option_defs}, opts};
2675e098073Schristos }
2685e098073Schristos
2695e098073Schristos /* Handle the input from the 'compile file' command. The "compile
2705e098073Schristos file" command is used to evaluate an expression contained in a file
2715e098073Schristos that may contain calls to the GCC compiler. */
2725e098073Schristos
2735e098073Schristos static void
compile_file_command(const char * args,int from_tty)274*1424dfb3Schristos compile_file_command (const char *args, int from_tty)
2755e098073Schristos {
2761c468f90Schristos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
2775e098073Schristos
278*1424dfb3Schristos /* Check if a -raw option is provided. */
279*1424dfb3Schristos
280*1424dfb3Schristos compile_options options;
281*1424dfb3Schristos
282*1424dfb3Schristos const gdb::option::option_def_group group
283*1424dfb3Schristos = make_compile_options_def_group (&options);
284*1424dfb3Schristos gdb::option::process_options
285*1424dfb3Schristos (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR,
286*1424dfb3Schristos group);
287*1424dfb3Schristos
288*1424dfb3Schristos enum compile_i_scope_types scope
289*1424dfb3Schristos = options.raw ? COMPILE_I_RAW_SCOPE : COMPILE_I_SIMPLE_SCOPE;
290*1424dfb3Schristos
291*1424dfb3Schristos args = skip_spaces (args);
292*1424dfb3Schristos
293*1424dfb3Schristos /* After processing options, check whether we have a filename. */
294*1424dfb3Schristos if (args == nullptr || args[0] == '\0')
2955e098073Schristos error (_("You must provide a filename for this command."));
2965e098073Schristos
297*1424dfb3Schristos args = skip_spaces (args);
298*1424dfb3Schristos gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (args);
29907163879Schristos std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ());
30007163879Schristos eval_compile_command (NULL, buffer.c_str (), scope, NULL);
3015e098073Schristos }
3025e098073Schristos
303*1424dfb3Schristos /* Completer for the "compile file" command. */
304*1424dfb3Schristos
305*1424dfb3Schristos static void
compile_file_command_completer(struct cmd_list_element * ignore,completion_tracker & tracker,const char * text,const char * word)306*1424dfb3Schristos compile_file_command_completer (struct cmd_list_element *ignore,
307*1424dfb3Schristos completion_tracker &tracker,
308*1424dfb3Schristos const char *text, const char *word)
309*1424dfb3Schristos {
310*1424dfb3Schristos const gdb::option::option_def_group group
311*1424dfb3Schristos = make_compile_options_def_group (nullptr);
312*1424dfb3Schristos if (gdb::option::complete_options
313*1424dfb3Schristos (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
314*1424dfb3Schristos return;
315*1424dfb3Schristos
316*1424dfb3Schristos word = advance_to_filename_complete_word_point (tracker, text);
317*1424dfb3Schristos filename_completer (ignore, tracker, text, word);
318*1424dfb3Schristos }
319*1424dfb3Schristos
3205e098073Schristos /* Handle the input from the 'compile code' command. The
3215e098073Schristos "compile code" command is used to evaluate an expression that may
3225e098073Schristos contain calls to the GCC compiler. The language expected in this
3235e098073Schristos compile command is the language currently set in GDB. */
3245e098073Schristos
3255e098073Schristos static void
compile_code_command(const char * args,int from_tty)326*1424dfb3Schristos compile_code_command (const char *args, int from_tty)
3275e098073Schristos {
3281c468f90Schristos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
3295e098073Schristos
330*1424dfb3Schristos compile_options options;
3315e098073Schristos
332*1424dfb3Schristos const gdb::option::option_def_group group
333*1424dfb3Schristos = make_compile_options_def_group (&options);
334*1424dfb3Schristos gdb::option::process_options
335*1424dfb3Schristos (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
3365e098073Schristos
337*1424dfb3Schristos enum compile_i_scope_types scope
338*1424dfb3Schristos = options.raw ? COMPILE_I_RAW_SCOPE : COMPILE_I_SIMPLE_SCOPE;
3395e098073Schristos
340*1424dfb3Schristos if (args && *args)
341*1424dfb3Schristos eval_compile_command (NULL, args, scope, NULL);
3425e098073Schristos else
3435e098073Schristos {
34407163879Schristos counted_command_line l = get_command_line (compile_control, "");
3455e098073Schristos
3465e098073Schristos l->control_u.compile.scope = scope;
3471c468f90Schristos execute_control_command_untraced (l.get ());
3485e098073Schristos }
3495e098073Schristos }
3505e098073Schristos
351*1424dfb3Schristos /* Completer for the "compile code" command. */
352*1424dfb3Schristos
353*1424dfb3Schristos static void
compile_code_command_completer(struct cmd_list_element * ignore,completion_tracker & tracker,const char * text,const char * word)354*1424dfb3Schristos compile_code_command_completer (struct cmd_list_element *ignore,
355*1424dfb3Schristos completion_tracker &tracker,
356*1424dfb3Schristos const char *text, const char *word)
357*1424dfb3Schristos {
358*1424dfb3Schristos const gdb::option::option_def_group group
359*1424dfb3Schristos = make_compile_options_def_group (nullptr);
360*1424dfb3Schristos if (gdb::option::complete_options
361*1424dfb3Schristos (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
362*1424dfb3Schristos return;
363*1424dfb3Schristos
364*1424dfb3Schristos word = advance_to_expression_complete_word_point (tracker, text);
365*1424dfb3Schristos symbol_completer (ignore, tracker, text, word);
366*1424dfb3Schristos }
367*1424dfb3Schristos
368ed6a76a9Schristos /* Callback for compile_print_command. */
369ed6a76a9Schristos
370ed6a76a9Schristos void
compile_print_value(struct value * val,void * data_voidp)371ed6a76a9Schristos compile_print_value (struct value *val, void *data_voidp)
372ed6a76a9Schristos {
373*1424dfb3Schristos const value_print_options *print_opts = (value_print_options *) data_voidp;
374ed6a76a9Schristos
375*1424dfb3Schristos print_value (val, *print_opts);
376ed6a76a9Schristos }
377ed6a76a9Schristos
378ed6a76a9Schristos /* Handle the input from the 'compile print' command. The "compile
379ed6a76a9Schristos print" command is used to evaluate and print an expression that may
380ed6a76a9Schristos contain calls to the GCC compiler. The language expected in this
381ed6a76a9Schristos compile command is the language currently set in GDB. */
382ed6a76a9Schristos
383ed6a76a9Schristos static void
compile_print_command(const char * arg,int from_tty)38407163879Schristos compile_print_command (const char *arg, int from_tty)
385ed6a76a9Schristos {
386ed6a76a9Schristos enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
387*1424dfb3Schristos value_print_options print_opts;
388ed6a76a9Schristos
3891c468f90Schristos scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
390ed6a76a9Schristos
391*1424dfb3Schristos get_user_print_options (&print_opts);
392*1424dfb3Schristos /* Override global settings with explicit options, if any. */
393*1424dfb3Schristos auto group = make_value_print_options_def_group (&print_opts);
394*1424dfb3Schristos gdb::option::process_options
395*1424dfb3Schristos (&arg, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
396*1424dfb3Schristos
397*1424dfb3Schristos print_command_parse_format (&arg, "compile print", &print_opts);
398*1424dfb3Schristos
399*1424dfb3Schristos /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup
400*1424dfb3Schristos will not touch the stale pointer if compile_object_run has
401*1424dfb3Schristos already quit. */
402ed6a76a9Schristos
403ed6a76a9Schristos if (arg && *arg)
404*1424dfb3Schristos eval_compile_command (NULL, arg, scope, &print_opts);
405ed6a76a9Schristos else
406ed6a76a9Schristos {
40707163879Schristos counted_command_line l = get_command_line (compile_control, "");
408ed6a76a9Schristos
409ed6a76a9Schristos l->control_u.compile.scope = scope;
410*1424dfb3Schristos l->control_u.compile.scope_data = &print_opts;
4111c468f90Schristos execute_control_command_untraced (l.get ());
412ed6a76a9Schristos }
413ed6a76a9Schristos }
414ed6a76a9Schristos
4155e098073Schristos /* A cleanup function to remove a directory and all its contents. */
4165e098073Schristos
4175e098073Schristos static void
do_rmdir(void * arg)4185e098073Schristos do_rmdir (void *arg)
4195e098073Schristos {
420c03b94e9Schristos const char *dir = (const char *) arg;
4215e098073Schristos char *zap;
4225e098073Schristos int wstat;
4235e098073Schristos
424ed6a76a9Schristos gdb_assert (startswith (dir, TMP_PREFIX));
4255e098073Schristos zap = concat ("rm -rf ", dir, (char *) NULL);
4265e098073Schristos wstat = system (zap);
4275e098073Schristos if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
4285e098073Schristos warning (_("Could not remove temporary directory %s"), dir);
4295e098073Schristos XDELETEVEC (zap);
4305e098073Schristos }
4315e098073Schristos
4325e098073Schristos /* Return the name of the temporary directory to use for .o files, and
4335e098073Schristos arrange for the directory to be removed at shutdown. */
4345e098073Schristos
4355e098073Schristos static const char *
get_compile_file_tempdir(void)4365e098073Schristos get_compile_file_tempdir (void)
4375e098073Schristos {
4385e098073Schristos static char *tempdir_name;
4395e098073Schristos
4405e098073Schristos #define TEMPLATE TMP_PREFIX "XXXXXX"
4415e098073Schristos char tname[sizeof (TEMPLATE)];
4425e098073Schristos
4435e098073Schristos if (tempdir_name != NULL)
4445e098073Schristos return tempdir_name;
4455e098073Schristos
4465e098073Schristos strcpy (tname, TEMPLATE);
4475e098073Schristos #undef TEMPLATE
4485e098073Schristos tempdir_name = mkdtemp (tname);
4495e098073Schristos if (tempdir_name == NULL)
4505e098073Schristos perror_with_name (_("Could not make temporary directory"));
4515e098073Schristos
4525e098073Schristos tempdir_name = xstrdup (tempdir_name);
4535e098073Schristos make_final_cleanup (do_rmdir, tempdir_name);
4545e098073Schristos return tempdir_name;
4555e098073Schristos }
4565e098073Schristos
4571c468f90Schristos /* Compute the names of source and object files to use. */
4585e098073Schristos
4591c468f90Schristos static compile_file_names
get_new_file_names()4601c468f90Schristos get_new_file_names ()
4615e098073Schristos {
4625e098073Schristos static int seq;
4635e098073Schristos const char *dir = get_compile_file_tempdir ();
4645e098073Schristos
4655e098073Schristos ++seq;
4661c468f90Schristos
4671c468f90Schristos return compile_file_names (string_printf ("%s%sout%d.c",
4681c468f90Schristos dir, SLASH_STRING, seq),
4691c468f90Schristos string_printf ("%s%sout%d.o",
4701c468f90Schristos dir, SLASH_STRING, seq));
4715e098073Schristos }
4725e098073Schristos
4735e098073Schristos /* Get the block and PC at which to evaluate an expression. */
4745e098073Schristos
4755e098073Schristos static const struct block *
get_expr_block_and_pc(CORE_ADDR * pc)4765e098073Schristos get_expr_block_and_pc (CORE_ADDR *pc)
4775e098073Schristos {
4785e098073Schristos const struct block *block = get_selected_block (pc);
4795e098073Schristos
4805e098073Schristos if (block == NULL)
4815e098073Schristos {
4825e098073Schristos struct symtab_and_line cursal = get_current_source_symtab_and_line ();
4835e098073Schristos
4845e098073Schristos if (cursal.symtab)
4855e098073Schristos block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
4865e098073Schristos STATIC_BLOCK);
4875e098073Schristos if (block != NULL)
48807163879Schristos *pc = BLOCK_ENTRY_PC (block);
4895e098073Schristos }
4905e098073Schristos else
49107163879Schristos *pc = BLOCK_ENTRY_PC (block);
4925e098073Schristos
4935e098073Schristos return block;
4945e098073Schristos }
4955e098073Schristos
49607163879Schristos /* Call buildargv (via gdb_argv), set its result for S into *ARGVP but
49707163879Schristos calculate also the number of parsed arguments into *ARGCP. If
49807163879Schristos buildargv has returned NULL then *ARGCP is set to zero. */
4995e098073Schristos
5005e098073Schristos static void
build_argc_argv(const char * s,int * argcp,char *** argvp)5015e098073Schristos build_argc_argv (const char *s, int *argcp, char ***argvp)
5025e098073Schristos {
50307163879Schristos gdb_argv args (s);
50407163879Schristos
50507163879Schristos *argcp = args.count ();
50607163879Schristos *argvp = args.release ();
5075e098073Schristos }
5085e098073Schristos
5095e098073Schristos /* String for 'set compile-args' and 'show compile-args'. */
5105e098073Schristos static char *compile_args;
5115e098073Schristos
5125e098073Schristos /* Parsed form of COMPILE_ARGS. COMPILE_ARGS_ARGV is NULL terminated. */
5135e098073Schristos static int compile_args_argc;
5145e098073Schristos static char **compile_args_argv;
5155e098073Schristos
5165e098073Schristos /* Implement 'set compile-args'. */
5175e098073Schristos
5185e098073Schristos static void
set_compile_args(const char * args,int from_tty,struct cmd_list_element * c)51907163879Schristos set_compile_args (const char *args, int from_tty, struct cmd_list_element *c)
5205e098073Schristos {
5215e098073Schristos freeargv (compile_args_argv);
5225e098073Schristos build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv);
5235e098073Schristos }
5245e098073Schristos
5255e098073Schristos /* Implement 'show compile-args'. */
5265e098073Schristos
5275e098073Schristos static void
show_compile_args(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)5285e098073Schristos show_compile_args (struct ui_file *file, int from_tty,
5295e098073Schristos struct cmd_list_element *c, const char *value)
5305e098073Schristos {
5315e098073Schristos fprintf_filtered (file, _("Compile command command-line arguments "
5325e098073Schristos "are \"%s\".\n"),
5335e098073Schristos value);
5345e098073Schristos }
5355e098073Schristos
5365e098073Schristos /* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP.
5375e098073Schristos ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL. */
5385e098073Schristos
5395e098073Schristos static void
append_args(int * argcp,char *** argvp,int argc,char ** argv)5405e098073Schristos append_args (int *argcp, char ***argvp, int argc, char **argv)
5415e098073Schristos {
5425e098073Schristos int argi;
5435e098073Schristos
544c03b94e9Schristos *argvp = XRESIZEVEC (char *, *argvp, (*argcp + argc + 1));
5455e098073Schristos
5465e098073Schristos for (argi = 0; argi < argc; argi++)
5475e098073Schristos (*argvp)[(*argcp)++] = xstrdup (argv[argi]);
5485e098073Schristos (*argvp)[(*argcp)] = NULL;
5495e098073Schristos }
5505e098073Schristos
55107163879Schristos /* String for 'set compile-gcc' and 'show compile-gcc'. */
55207163879Schristos static char *compile_gcc;
55307163879Schristos
55407163879Schristos /* Implement 'show compile-gcc'. */
55507163879Schristos
55607163879Schristos static void
show_compile_gcc(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)55707163879Schristos show_compile_gcc (struct ui_file *file, int from_tty,
55807163879Schristos struct cmd_list_element *c, const char *value)
55907163879Schristos {
56007163879Schristos fprintf_filtered (file, _("Compile command GCC driver filename is \"%s\".\n"),
56107163879Schristos value);
56207163879Schristos }
56307163879Schristos
5645e098073Schristos /* Return DW_AT_producer parsed for get_selected_frame () (if any).
5655e098073Schristos Return NULL otherwise.
5665e098073Schristos
5675e098073Schristos GCC already filters its command-line arguments only for the suitable ones to
5685e098073Schristos put into DW_AT_producer - see GCC function gen_producer_string. */
5695e098073Schristos
5705e098073Schristos static const char *
get_selected_pc_producer_options(void)5715e098073Schristos get_selected_pc_producer_options (void)
5725e098073Schristos {
5735e098073Schristos CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
5745e098073Schristos struct compunit_symtab *symtab = find_pc_compunit_symtab (pc);
5755e098073Schristos const char *cs;
5765e098073Schristos
5775e098073Schristos if (symtab == NULL || symtab->producer == NULL
578ed6a76a9Schristos || !startswith (symtab->producer, "GNU "))
5795e098073Schristos return NULL;
5805e098073Schristos
5815e098073Schristos cs = symtab->producer;
5825e098073Schristos while (*cs != 0 && *cs != '-')
58307163879Schristos cs = skip_spaces (skip_to_space (cs));
5845e098073Schristos if (*cs != '-')
5855e098073Schristos return NULL;
5865e098073Schristos return cs;
5875e098073Schristos }
5885e098073Schristos
5895e098073Schristos /* Filter out unwanted options from *ARGCP and ARGV. */
5905e098073Schristos
5915e098073Schristos static void
filter_args(int * argcp,char ** argv)5925e098073Schristos filter_args (int *argcp, char **argv)
5935e098073Schristos {
5945e098073Schristos char **destv;
5955e098073Schristos
5965e098073Schristos for (destv = argv; *argv != NULL; argv++)
5975e098073Schristos {
5985e098073Schristos /* -fpreprocessed may get in commonly from ccache. */
5995e098073Schristos if (strcmp (*argv, "-fpreprocessed") == 0)
6005e098073Schristos {
6015e098073Schristos xfree (*argv);
6025e098073Schristos (*argcp)--;
6035e098073Schristos continue;
6045e098073Schristos }
6055e098073Schristos *destv++ = *argv;
6065e098073Schristos }
6075e098073Schristos *destv = NULL;
6085e098073Schristos }
6095e098073Schristos
61007163879Schristos /* Produce final vector of GCC compilation options.
61107163879Schristos
61207163879Schristos The first element of the combined argument vector are arguments
61307163879Schristos relating to the target size ("-m64", "-m32" etc.). These are
61407163879Schristos sourced from the inferior's architecture.
61507163879Schristos
61607163879Schristos The second element of the combined argument vector are arguments
61707163879Schristos stored in the inferior DW_AT_producer section. If these are stored
61807163879Schristos in the inferior (there is no guarantee that they are), they are
61907163879Schristos added to the vector.
62007163879Schristos
62107163879Schristos The third element of the combined argument vector are argument
62207163879Schristos supplied by the language implementation provided by
62307163879Schristos compile-{lang}-support. These contain language specific arguments.
62407163879Schristos
62507163879Schristos The final element of the combined argument vector are arguments
62607163879Schristos supplied by the "set compile-args" command. These are always
62707163879Schristos appended last so as to override any of the arguments automatically
62807163879Schristos generated above. */
6295e098073Schristos
6305e098073Schristos static void
get_args(const compile_instance * compiler,struct gdbarch * gdbarch,int * argcp,char *** argvp)63107163879Schristos get_args (const compile_instance *compiler, struct gdbarch *gdbarch,
6325e098073Schristos int *argcp, char ***argvp)
6335e098073Schristos {
6345e098073Schristos const char *cs_producer_options;
6355e098073Schristos int argc_compiler;
6365e098073Schristos char **argv_compiler;
6375e098073Schristos
638*1424dfb3Schristos build_argc_argv (gdbarch_gcc_target_options (gdbarch).c_str (),
6395e098073Schristos argcp, argvp);
6405e098073Schristos
6415e098073Schristos cs_producer_options = get_selected_pc_producer_options ();
6425e098073Schristos if (cs_producer_options != NULL)
6435e098073Schristos {
6445e098073Schristos int argc_producer;
6455e098073Schristos char **argv_producer;
6465e098073Schristos
6475e098073Schristos build_argc_argv (cs_producer_options, &argc_producer, &argv_producer);
6485e098073Schristos filter_args (&argc_producer, argv_producer);
6495e098073Schristos append_args (argcp, argvp, argc_producer, argv_producer);
6505e098073Schristos freeargv (argv_producer);
6515e098073Schristos }
6525e098073Schristos
65307163879Schristos build_argc_argv (compiler->gcc_target_options ().c_str (),
6545e098073Schristos &argc_compiler, &argv_compiler);
6555e098073Schristos append_args (argcp, argvp, argc_compiler, argv_compiler);
6565e098073Schristos freeargv (argv_compiler);
6575e098073Schristos
6585e098073Schristos append_args (argcp, argvp, compile_args_argc, compile_args_argv);
6595e098073Schristos }
6605e098073Schristos
6615e098073Schristos /* A helper function suitable for use as the "print_callback" in the
6625e098073Schristos compiler object. */
6635e098073Schristos
6645e098073Schristos static void
print_callback(void * ignore,const char * message)6655e098073Schristos print_callback (void *ignore, const char *message)
6665e098073Schristos {
6675e098073Schristos fputs_filtered (message, gdb_stderr);
6685e098073Schristos }
6695e098073Schristos
6705e098073Schristos /* Process the compilation request. On success it returns the object
6711c468f90Schristos and source file names. On an error condition, error () is
6721c468f90Schristos called. */
6735e098073Schristos
6741c468f90Schristos static compile_file_names
compile_to_object(struct command_line * cmd,const char * cmd_string,enum compile_i_scope_types scope)675ed6a76a9Schristos compile_to_object (struct command_line *cmd, const char *cmd_string,
6761c468f90Schristos enum compile_i_scope_types scope)
6775e098073Schristos {
6785e098073Schristos const struct block *expr_block;
6795e098073Schristos CORE_ADDR trash_pc, expr_pc;
6805e098073Schristos int argc;
6815e098073Schristos char **argv;
6825e098073Schristos int ok;
6835e098073Schristos struct gdbarch *gdbarch = get_current_arch ();
68407163879Schristos std::string triplet_rx;
6855e098073Schristos
6865e098073Schristos if (!target_has_execution)
6875e098073Schristos error (_("The program must be running for the compile command to "\
6885e098073Schristos "work."));
6895e098073Schristos
6905e098073Schristos expr_block = get_expr_block_and_pc (&trash_pc);
6915e098073Schristos expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
6925e098073Schristos
6935e098073Schristos /* Set up instance and context for the compiler. */
694*1424dfb3Schristos std::unique_ptr <compile_instance> compiler
695*1424dfb3Schristos (current_language->get_compile_instance ());
696*1424dfb3Schristos if (compiler == nullptr)
697c03b94e9Schristos error (_("No compiler support for language %s."),
698c03b94e9Schristos current_language->la_name);
69907163879Schristos compiler->set_print_callback (print_callback, NULL);
70007163879Schristos compiler->set_scope (scope);
70107163879Schristos compiler->set_block (expr_block);
7025e098073Schristos
7035e098073Schristos /* From the provided expression, build a scope to pass to the
7045e098073Schristos compiler. */
7051c468f90Schristos
7061c468f90Schristos string_file input_buf;
7071c468f90Schristos const char *input;
7081c468f90Schristos
7095e098073Schristos if (cmd != NULL)
7105e098073Schristos {
7115e098073Schristos struct command_line *iter;
7125e098073Schristos
71307163879Schristos for (iter = cmd->body_list_0.get (); iter; iter = iter->next)
7145e098073Schristos {
7151c468f90Schristos input_buf.puts (iter->line);
7161c468f90Schristos input_buf.puts ("\n");
7175e098073Schristos }
7185e098073Schristos
7191c468f90Schristos input = input_buf.c_str ();
7205e098073Schristos }
7215e098073Schristos else if (cmd_string != NULL)
722ed6a76a9Schristos input = cmd_string;
7235e098073Schristos else
7245e098073Schristos error (_("Neither a simple expression, or a multi-line specified."));
7255e098073Schristos
7261c468f90Schristos std::string code
727*1424dfb3Schristos = current_language->compute_program (compiler.get (), input, gdbarch,
7285e098073Schristos expr_block, expr_pc);
7295e098073Schristos if (compile_debug)
7301c468f90Schristos fprintf_unfiltered (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
7315e098073Schristos
73207163879Schristos compiler->set_verbose (compile_debug);
73307163879Schristos
73407163879Schristos if (compile_gcc[0] != 0)
73507163879Schristos {
73607163879Schristos if (compiler->version () < GCC_FE_VERSION_1)
73707163879Schristos error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
73807163879Schristos "(libcc1 interface version 1 or higher)"));
73907163879Schristos
74007163879Schristos compiler->set_driver_filename (compile_gcc);
74107163879Schristos }
74207163879Schristos else
74307163879Schristos {
74407163879Schristos const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
74507163879Schristos const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
7465e098073Schristos
7475e098073Schristos /* Allow triplets with or without vendor set. */
74807163879Schristos triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
74907163879Schristos compiler->set_triplet_regexp (triplet_rx.c_str ());
75007163879Schristos }
7515e098073Schristos
7525e098073Schristos /* Set compiler command-line arguments. */
75307163879Schristos get_args (compiler.get (), gdbarch, &argc, &argv);
75407163879Schristos gdb_argv argv_holder (argv);
7555e098073Schristos
75607163879Schristos gdb::unique_xmalloc_ptr<char> error_message;
75707163879Schristos error_message.reset (compiler->set_arguments (argc, argv,
75807163879Schristos triplet_rx.c_str ()));
75907163879Schristos
7605e098073Schristos if (error_message != NULL)
76107163879Schristos error ("%s", error_message.get ());
7625e098073Schristos
7635e098073Schristos if (compile_debug)
7645e098073Schristos {
7655e098073Schristos int argi;
7665e098073Schristos
767ed6a76a9Schristos fprintf_unfiltered (gdb_stdlog, "Passing %d compiler options:\n", argc);
7685e098073Schristos for (argi = 0; argi < argc; argi++)
769ed6a76a9Schristos fprintf_unfiltered (gdb_stdlog, "Compiler option %d: <%s>\n",
7705e098073Schristos argi, argv[argi]);
7715e098073Schristos }
7725e098073Schristos
7731c468f90Schristos compile_file_names fnames = get_new_file_names ();
7745e098073Schristos
77507163879Schristos gdb::optional<gdb::unlinker> source_remover;
77607163879Schristos
77707163879Schristos {
77807163879Schristos gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w");
7795e098073Schristos if (src == NULL)
7805e098073Schristos perror_with_name (_("Could not open source file for writing"));
78107163879Schristos
78207163879Schristos source_remover.emplace (fnames.source_file ());
78307163879Schristos
78407163879Schristos if (fputs (code.c_str (), src.get ()) == EOF)
7855e098073Schristos perror_with_name (_("Could not write to source file"));
78607163879Schristos }
7875e098073Schristos
7885e098073Schristos if (compile_debug)
789ed6a76a9Schristos fprintf_unfiltered (gdb_stdlog, "source file produced: %s\n\n",
7901c468f90Schristos fnames.source_file ());
7915e098073Schristos
7925e098073Schristos /* Call the compiler and start the compilation process. */
79307163879Schristos compiler->set_source_file (fnames.source_file ());
79407163879Schristos ok = compiler->compile (fnames.object_file (), compile_debug);
79507163879Schristos if (!ok)
7965e098073Schristos error (_("Compilation failed."));
7975e098073Schristos
7985e098073Schristos if (compile_debug)
799ed6a76a9Schristos fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n",
8001c468f90Schristos fnames.object_file ());
8015e098073Schristos
80207163879Schristos /* Keep the source file. */
80307163879Schristos source_remover->keep ();
8041c468f90Schristos return fnames;
8055e098073Schristos }
8065e098073Schristos
8075e098073Schristos /* The "compile" prefix command. */
8085e098073Schristos
8095e098073Schristos static void
compile_command(const char * args,int from_tty)81007163879Schristos compile_command (const char *args, int from_tty)
8115e098073Schristos {
8125e098073Schristos /* If a sub-command is not specified to the compile prefix command,
8135e098073Schristos assume it is a direct code compilation. */
8145e098073Schristos compile_code_command (args, from_tty);
8155e098073Schristos }
8165e098073Schristos
8175e098073Schristos /* See compile.h. */
8185e098073Schristos
8195e098073Schristos void
eval_compile_command(struct command_line * cmd,const char * cmd_string,enum compile_i_scope_types scope,void * scope_data)820ed6a76a9Schristos eval_compile_command (struct command_line *cmd, const char *cmd_string,
821ed6a76a9Schristos enum compile_i_scope_types scope, void *scope_data)
8225e098073Schristos {
8235e098073Schristos struct compile_module *compile_module;
8245e098073Schristos
8251c468f90Schristos compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
8261c468f90Schristos
82707163879Schristos gdb::unlinker object_remover (fnames.object_file ());
82807163879Schristos gdb::unlinker source_remover (fnames.source_file ());
82907163879Schristos
8301c468f90Schristos compile_module = compile_object_load (fnames, scope, scope_data);
831ed6a76a9Schristos if (compile_module == NULL)
832ed6a76a9Schristos {
833ed6a76a9Schristos gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
834ed6a76a9Schristos eval_compile_command (cmd, cmd_string,
835ed6a76a9Schristos COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
836ed6a76a9Schristos return;
837ed6a76a9Schristos }
83807163879Schristos
83907163879Schristos /* Keep the files. */
84007163879Schristos source_remover.keep ();
84107163879Schristos object_remover.keep ();
84207163879Schristos
8435e098073Schristos compile_object_run (compile_module);
8445e098073Schristos }
8455e098073Schristos
8465e098073Schristos /* See compile/compile-internal.h. */
8475e098073Schristos
84807163879Schristos std::string
compile_register_name_mangled(struct gdbarch * gdbarch,int regnum)8495e098073Schristos compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
8505e098073Schristos {
8515e098073Schristos const char *regname = gdbarch_register_name (gdbarch, regnum);
8525e098073Schristos
85307163879Schristos return string_printf ("__%s", regname);
8545e098073Schristos }
8555e098073Schristos
8565e098073Schristos /* See compile/compile-internal.h. */
8575e098073Schristos
8585e098073Schristos int
compile_register_name_demangle(struct gdbarch * gdbarch,const char * regname)8595e098073Schristos compile_register_name_demangle (struct gdbarch *gdbarch,
8605e098073Schristos const char *regname)
8615e098073Schristos {
8625e098073Schristos int regnum;
8635e098073Schristos
8645e098073Schristos if (regname[0] != '_' || regname[1] != '_')
8655e098073Schristos error (_("Invalid register name \"%s\"."), regname);
8665e098073Schristos regname += 2;
8675e098073Schristos
8685e098073Schristos for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
8695e098073Schristos if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
8705e098073Schristos return regnum;
8715e098073Schristos
8725e098073Schristos error (_("Cannot find gdbarch register \"%s\"."), regname);
8735e098073Schristos }
8745e098073Schristos
87507163879Schristos /* Forwards to the plug-in. */
87607163879Schristos
87707163879Schristos #define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
87807163879Schristos
87907163879Schristos /* See compile-internal.h. */
88007163879Schristos
88107163879Schristos void
set_print_callback(void (* print_function)(void *,const char *),void * datum)88207163879Schristos compile_instance::set_print_callback
88307163879Schristos (void (*print_function) (void *, const char *), void *datum)
88407163879Schristos {
88507163879Schristos FORWARD (set_print_callback, print_function, datum);
88607163879Schristos }
88707163879Schristos
88807163879Schristos /* See compile-internal.h. */
88907163879Schristos
89007163879Schristos unsigned int
version()89107163879Schristos compile_instance::version () const
89207163879Schristos {
89307163879Schristos return m_gcc_fe->ops->version;
89407163879Schristos }
89507163879Schristos
89607163879Schristos /* See compile-internal.h. */
89707163879Schristos
89807163879Schristos void
set_verbose(int level)89907163879Schristos compile_instance::set_verbose (int level)
90007163879Schristos {
90107163879Schristos if (version () >= GCC_FE_VERSION_1)
90207163879Schristos FORWARD (set_verbose, level);
90307163879Schristos }
90407163879Schristos
90507163879Schristos /* See compile-internal.h. */
90607163879Schristos
90707163879Schristos void
set_driver_filename(const char * filename)90807163879Schristos compile_instance::set_driver_filename (const char *filename)
90907163879Schristos {
91007163879Schristos if (version () >= GCC_FE_VERSION_1)
91107163879Schristos FORWARD (set_driver_filename, filename);
91207163879Schristos }
91307163879Schristos
91407163879Schristos /* See compile-internal.h. */
91507163879Schristos
91607163879Schristos void
set_triplet_regexp(const char * regexp)91707163879Schristos compile_instance::set_triplet_regexp (const char *regexp)
91807163879Schristos {
91907163879Schristos if (version () >= GCC_FE_VERSION_1)
92007163879Schristos FORWARD (set_triplet_regexp, regexp);
92107163879Schristos }
92207163879Schristos
92307163879Schristos /* See compile-internal.h. */
92407163879Schristos
92507163879Schristos char *
set_arguments(int argc,char ** argv,const char * regexp)92607163879Schristos compile_instance::set_arguments (int argc, char **argv, const char *regexp)
92707163879Schristos {
92807163879Schristos if (version () >= GCC_FE_VERSION_1)
92907163879Schristos return FORWARD (set_arguments, argc, argv);
93007163879Schristos else
93107163879Schristos return FORWARD (set_arguments_v0, regexp, argc, argv);
93207163879Schristos }
93307163879Schristos
93407163879Schristos /* See compile-internal.h. */
93507163879Schristos
93607163879Schristos void
set_source_file(const char * filename)93707163879Schristos compile_instance::set_source_file (const char *filename)
93807163879Schristos {
93907163879Schristos FORWARD (set_source_file, filename);
94007163879Schristos }
94107163879Schristos
94207163879Schristos /* See compile-internal.h. */
94307163879Schristos
94407163879Schristos bool
compile(const char * filename,int verbose_level)94507163879Schristos compile_instance::compile (const char *filename, int verbose_level)
94607163879Schristos {
94707163879Schristos if (version () >= GCC_FE_VERSION_1)
94807163879Schristos return FORWARD (compile, filename);
94907163879Schristos else
95007163879Schristos return FORWARD (compile_v0, filename, verbose_level);
95107163879Schristos }
95207163879Schristos
95307163879Schristos #undef FORWARD
95407163879Schristos
95507163879Schristos /* See compile.h. */
95607163879Schristos cmd_list_element *compile_cmd_element = nullptr;
9575e098073Schristos
958*1424dfb3Schristos void _initialize_compile ();
9595e098073Schristos void
_initialize_compile()960*1424dfb3Schristos _initialize_compile ()
9615e098073Schristos {
9625e098073Schristos struct cmd_list_element *c = NULL;
9635e098073Schristos
96407163879Schristos compile_cmd_element = add_prefix_cmd ("compile", class_obscure,
96507163879Schristos compile_command, _("\
9665e098073Schristos Command to compile source code and inject it into the inferior."),
9675e098073Schristos &compile_command_list, "compile ", 1, &cmdlist);
9685e098073Schristos add_com_alias ("expression", "compile", class_obscure, 0);
9695e098073Schristos
970*1424dfb3Schristos const auto compile_opts = make_compile_options_def_group (nullptr);
971*1424dfb3Schristos
972*1424dfb3Schristos static const std::string compile_code_help
973*1424dfb3Schristos = gdb::option::build_help (_("\
9745e098073Schristos Compile, inject, and execute code.\n\
9755e098073Schristos \n\
976*1424dfb3Schristos Usage: compile code [OPTION]... [CODE]\n\
977*1424dfb3Schristos \n\
978*1424dfb3Schristos Options:\n\
979*1424dfb3Schristos %OPTIONS%\n\
9805e098073Schristos \n\
9815e098073Schristos The source code may be specified as a simple one line expression, e.g.:\n\
9825e098073Schristos \n\
9835e098073Schristos compile code printf(\"Hello world\\n\");\n\
9845e098073Schristos \n\
985ed6a76a9Schristos Alternatively, you can type a multiline expression by invoking\n\
986ed6a76a9Schristos this command with no argument. GDB will then prompt for the\n\
987ed6a76a9Schristos expression interactively; type a line containing \"end\" to\n\
988ed6a76a9Schristos indicate the end of the expression."),
989*1424dfb3Schristos compile_opts);
9905e098073Schristos
991*1424dfb3Schristos c = add_cmd ("code", class_obscure, compile_code_command,
992*1424dfb3Schristos compile_code_help.c_str (),
993*1424dfb3Schristos &compile_command_list);
994*1424dfb3Schristos set_cmd_completer_handle_brkchars (c, compile_code_command_completer);
995*1424dfb3Schristos
996*1424dfb3Schristos static const std::string compile_file_help
997*1424dfb3Schristos = gdb::option::build_help (_("\
9985e098073Schristos Evaluate a file containing source code.\n\
9995e098073Schristos \n\
1000*1424dfb3Schristos Usage: compile file [OPTION].. [FILENAME]\n\
1001*1424dfb3Schristos \n\
1002*1424dfb3Schristos Options:\n\
1003*1424dfb3Schristos %OPTIONS%"),
1004*1424dfb3Schristos compile_opts);
10055e098073Schristos
1006*1424dfb3Schristos c = add_cmd ("file", class_obscure, compile_file_command,
1007*1424dfb3Schristos compile_file_help.c_str (),
1008*1424dfb3Schristos &compile_command_list);
1009*1424dfb3Schristos set_cmd_completer_handle_brkchars (c, compile_file_command_completer);
1010*1424dfb3Schristos
1011*1424dfb3Schristos const auto compile_print_opts = make_value_print_options_def_group (nullptr);
1012*1424dfb3Schristos
1013*1424dfb3Schristos static const std::string compile_print_help
1014*1424dfb3Schristos = gdb::option::build_help (_("\
1015ed6a76a9Schristos Evaluate EXPR by using the compiler and print result.\n\
1016ed6a76a9Schristos \n\
1017*1424dfb3Schristos Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
1018*1424dfb3Schristos \n\
1019*1424dfb3Schristos Options:\n\
1020*1424dfb3Schristos %OPTIONS%\n\
1021*1424dfb3Schristos \n\
1022*1424dfb3Schristos Note: because this command accepts arbitrary expressions, if you\n\
1023*1424dfb3Schristos specify any command option, you must use a double dash (\"--\")\n\
1024*1424dfb3Schristos to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
1025ed6a76a9Schristos \n\
1026ed6a76a9Schristos The expression may be specified on the same line as the command, e.g.:\n\
1027ed6a76a9Schristos \n\
1028ed6a76a9Schristos compile print i\n\
1029ed6a76a9Schristos \n\
1030ed6a76a9Schristos Alternatively, you can type a multiline expression by invoking\n\
1031ed6a76a9Schristos this command with no argument. GDB will then prompt for the\n\
1032ed6a76a9Schristos expression interactively; type a line containing \"end\" to\n\
1033ed6a76a9Schristos indicate the end of the expression.\n\
1034ed6a76a9Schristos \n\
1035ed6a76a9Schristos EXPR may be preceded with /FMT, where FMT is a format letter\n\
1036ed6a76a9Schristos but no count or size letter (see \"x\" command)."),
1037*1424dfb3Schristos compile_print_opts);
1038*1424dfb3Schristos
1039*1424dfb3Schristos c = add_cmd ("print", class_obscure, compile_print_command,
1040*1424dfb3Schristos compile_print_help.c_str (),
1041ed6a76a9Schristos &compile_command_list);
1042*1424dfb3Schristos set_cmd_completer_handle_brkchars (c, print_command_completer);
1043ed6a76a9Schristos
10445e098073Schristos add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
10455e098073Schristos Set compile command debugging."), _("\
10465e098073Schristos Show compile command debugging."), _("\
10475e098073Schristos When on, compile command debugging is enabled."),
10485e098073Schristos NULL, show_compile_debug,
10495e098073Schristos &setdebuglist, &showdebuglist);
10505e098073Schristos
10515e098073Schristos add_setshow_string_cmd ("compile-args", class_support,
10525e098073Schristos &compile_args,
1053*1424dfb3Schristos _("Set compile command GCC command-line arguments."),
1054*1424dfb3Schristos _("Show compile command GCC command-line arguments."),
10555e098073Schristos _("\
10565e098073Schristos Use options like -I (include file directory) or ABI settings.\n\
10575e098073Schristos String quoting is parsed like in shell, for example:\n\
10585e098073Schristos -mno-align-double \"-I/dir with a space/include\""),
10595e098073Schristos set_compile_args, show_compile_args, &setlist, &showlist);
10605e098073Schristos
10615e098073Schristos /* Override flags possibly coming from DW_AT_producer. */
10625e098073Schristos compile_args = xstrdup ("-O0 -gdwarf-4"
10635e098073Schristos /* We use -fPIE Otherwise GDB would need to reserve space large enough for
10645e098073Schristos any object file in the inferior in advance to get the final address when
10655e098073Schristos to link the object file to and additionally the default system linker
10665e098073Schristos script would need to be modified so that one can specify there the
10675e098073Schristos absolute target address.
10685e098073Schristos -fPIC is not used at is would require from GDB to generate .got. */
10695e098073Schristos " -fPIE"
1070ed6a76a9Schristos /* We want warnings, except for some commonly happening for GDB commands. */
1071ed6a76a9Schristos " -Wall "
1072ed6a76a9Schristos " -Wno-unused-but-set-variable"
1073ed6a76a9Schristos " -Wno-unused-variable"
10745e098073Schristos /* Override CU's possible -fstack-protector-strong. */
10755e098073Schristos " -fno-stack-protector"
10765e098073Schristos );
10775e098073Schristos set_compile_args (compile_args, 0, NULL);
107807163879Schristos
107907163879Schristos add_setshow_optional_filename_cmd ("compile-gcc", class_support,
108007163879Schristos &compile_gcc,
108107163879Schristos _("Set compile command "
1082*1424dfb3Schristos "GCC driver filename."),
108307163879Schristos _("Show compile command "
1084*1424dfb3Schristos "GCC driver filename."),
108507163879Schristos _("\
108607163879Schristos It should be absolute filename of the gcc executable.\n\
108707163879Schristos If empty the default target triplet will be searched in $PATH."),
108807163879Schristos NULL, show_compile_gcc, &setlist,
108907163879Schristos &showlist);
109007163879Schristos compile_gcc = xstrdup ("");
10915e098073Schristos }
1092