38#include "gdbsupport/filestuff.h"
41#include "gdbsupport/gdb_wait.h"
43#include "gdbsupport/gdb_optional.h"
44#include "gdbsupport/gdb_unlinker.h"
45#include "gdbsupport/pathstuff.h"
46#include "gdbsupport/scoped_ignore_signal.h"
47#include "gdbsupport/buildargv.h"
53#define TMP_PREFIX "/tmp/gdbobj-"
97 return htab_hash_pointer (inst->
type);
118 return htab_hash_pointer (se->
sym);
129 return sea->
sym == seb->
sym;
147 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
185 slot = htab_find_slot (
m_type_map.get (), &inst, INSERT);
191 error (_(
"Unexpected type id from GCC, check you use recent enough GCC."));
235 if (
err == NULL ||
err->message == NULL)
238 gdb::unique_xmalloc_ptr<char> message (
err->message);
240 error (_(
"%s"), message.get ());
270 N_(
"Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
291 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
306 args = skip_spaces (args);
309 if (args ==
nullptr || args[0] ==
'\0')
310 error (_(
"You must provide a filename for this command."));
312 args = skip_spaces (args);
313 std::string abspath = gdb_abspath (args);
314 std::string buffer = string_printf (
"#include \"%s\"\n", abspath.c_str ());
323 const char *text,
const char *word)
343 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
361 l->control_u.compile.scope = scope;
371 const char *text,
const char *word)
404 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
424 l->control_u.compile.scope = scope;
425 l->control_u.compile.scope_data = &print_opts;
435 const char *dir = (
const char *) arg;
440 zap = concat (
"rm -rf ", dir, (
char *) NULL);
441 wstat = system (zap);
442 if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
443 warning (_(
"Could not remove temporary directory %s"), dir);
453 static char *tempdir_name;
455#define TEMPLATE TMP_PREFIX "XXXXXX"
458 if (tempdir_name != NULL)
463 tempdir_name = mkdtemp (tname);
464 if (tempdir_name == NULL)
465 perror_with_name (_(
"Could not make temporary directory"));
467 tempdir_name = xstrdup (tempdir_name);
468 make_final_cleanup (
do_rmdir, tempdir_name);
483 dir, SLASH_STRING, seq),
484 string_printf (
"%s%sout%d.o",
485 dir, SLASH_STRING, seq));
524 " -Wno-unused-but-set-variable"
525 " -Wno-unused-variable"
527 " -fno-stack-protector";
546 gdb_printf (file, _(
"Compile command command-line arguments "
560 gdb_printf (file, _(
"Compile command GCC driver filename is \"%s\".\n"),
578 || !startswith (
symtab->producer (),
"GNU "))
582 while (*cs != 0 && *cs !=
'-')
583 cs = skip_spaces (skip_to_space (cs));
596 for (destv = argv; *argv != NULL; argv++)
599 if (strcmp (*argv,
"-fpreprocessed") == 0)
632 const char *cs_producer_options;
639 if (!gcc_options.empty ())
640 result = gdb_argv (gcc_options.c_str ());
643 if (cs_producer_options != NULL)
645 gdb_argv argv_producer (cs_producer_options);
648 result.append (std::move (argv_producer));
674 const struct block *expr_block;
675 CORE_ADDR trash_pc, expr_pc;
678 std::string triplet_rx;
681 error (_(
"The program must be running for the compile command to "\
688 std::unique_ptr<compile_instance> compiler
690 if (compiler ==
nullptr)
691 error (_(
"No compiler support for language %s."),
694 compiler->set_scope (scope);
695 compiler->set_block (expr_block);
710 input_buf.
puts (
"\n");
713 input = input_buf.
c_str ();
715 else if (cmd_string != NULL)
718 error (_(
"Neither a simple expression, or a multi-line specified."));
722 expr_block, expr_pc);
730 if (compiler->version () < GCC_FE_VERSION_1)
731 error (_(
"Command 'set compile-gcc' requires GCC version 6 or higher "
732 "(libcc1 interface version 1 or higher)"));
734 compiler->set_driver_filename (
compile_gcc.c_str ());
742 triplet_rx = std::string (arch_rx) +
"(-[^-]*)?-";
743 if (os_rx !=
nullptr)
745 compiler->set_triplet_regexp (triplet_rx.c_str ());
750 int argc = argv_holder.count ();
751 char **argv = argv_holder.get ();
753 gdb::unique_xmalloc_ptr<char> error_message
754 = compiler->set_arguments (argc, argv, triplet_rx.c_str ());
756 if (error_message != NULL)
757 error (
"%s", error_message.get ());
764 for (argi = 0; argi < argc; argi++)
771 gdb::optional<gdb::unlinker> source_remover;
774 gdb_file_up src = gdb_fopen_cloexec (fnames.
source_file (),
"w");
776 perror_with_name (_(
"Could not open source file for writing"));
780 if (fputs (
code.c_str (), src.get ()) == EOF)
781 perror_with_name (_(
"Could not write to source file"));
790 scoped_ignore_sigpipe ignore_sigpipe;
796 error (_(
"Compilation failed."));
803 source_remover->keep ();
825 gdb::unlinker object_remover (fnames.
object_file ());
826 gdb::unlinker source_remover (fnames.
source_file ());
839 source_remover.keep ();
840 object_remover.keep ();
852 return string_printf (
"__%s", regname);
863 if (regname[0] !=
'_' || regname[1] !=
'_')
864 error (_(
"Invalid register name \"%s\"."), regname);
871 error (_(
"Cannot find gdbarch register \"%s\"."), regname);
876#define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
882 (
void (*print_function) (
void *,
const char *),
void *datum)
900 if (
version () >= GCC_FE_VERSION_1)
909 if (
version () >= GCC_FE_VERSION_1)
918 if (
version () >= GCC_FE_VERSION_1)
924gdb::unique_xmalloc_ptr<char>
927 if (
version () >= GCC_FE_VERSION_1)
930 return gdb::unique_xmalloc_ptr<char> (
FORWARD (set_arguments_v0, regexp,
947 if (
version () >= GCC_FE_VERSION_1)
950 return FORWARD (compile_v0, filename, verbose_level);
966Command to compile source code and inject it into the inferior."),
972 static const std::string compile_code_help
974Compile, inject, and execute code.\n\
976Usage: compile code [OPTION]... [CODE]\n\
981The source code may be specified as a simple one line expression, e.g.:\n\
983 compile code printf(\"Hello world\\n\");\n\
985Alternatively, you can type a multiline expression by invoking\n\
986this command with no argument. GDB will then prompt for the\n\
987expression interactively; type a line containing \"end\" to\n\
988indicate the end of the expression."),
992 compile_code_help.c_str (),
996static const std::string compile_file_help
998Evaluate a file containing source code.\n\
1000Usage: compile file [OPTION].. [FILENAME]\n\
1007 compile_file_help.c_str (),
1013 static const std::string compile_print_help
1015Evaluate EXPR by using the compiler and print result.\n\
1017Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
1022Note: because this command accepts arbitrary expressions, if you\n\
1023specify any command option, you must use a double dash (\"--\")\n\
1024to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
1026The expression may be specified on the same line as the command, e.g.:\n\
1030Alternatively, you can type a multiline expression by invoking\n\
1031this command with no argument. GDB will then prompt for the\n\
1032expression interactively; type a line containing \"end\" to\n\
1033indicate the end of the expression.\n\
1035EXPR may be preceded with /FMT, where FMT is a format letter\n\
1036but no count or size letter (see \"x\" command)."),
1037 compile_print_opts);
1040 compile_print_help.c_str (),
1045Set compile command debugging."), _(
"\
1046Show compile command debugging."), _(
"\
1047When on, compile command debugging is enabled."),
1053 _(
"Set compile command GCC command-line arguments."),
1054 _(
"Show compile command GCC command-line arguments."),
1056Use options like -I (include file directory) or ABI settings.\n\
1057String quoting is parsed like in shell, for example:\n\
1058 -mno-align-double \"-I/dir with a space/include\""),
1067 _(
"Set compile command "
1068 "GCC driver filename."),
1069 _(
"Show compile command "
1070 "GCC driver filename."),
1072It should be absolute filename of the gcc executable.\n\
1073If empty the default target triplet will be searched in $PATH."),
void * xcalloc(size_t number, size_t size)
struct gdbarch * get_current_arch(void)
const char * source_file() const
const char * object_file() const
unsigned int version() const
void set_driver_filename(const char *filename)
struct gcc_base_context * m_gcc_fe
void error_symbol_once(const struct symbol *sym)
void set_print_callback(void(*print_function)(void *, const char *), void *datum)
void insert_symbol_error(const struct symbol *sym, const char *text)
compile_instance(struct gcc_base_context *gcc_fe, const char *options)
bool get_cached_type(struct type *type, gcc_type *ret) const
void set_triplet_regexp(const char *regexp)
gdb::unique_xmalloc_ptr< char > set_arguments(int argc, char **argv, const char *regexp=NULL)
void insert_type(struct type *type, gcc_type gcc_type)
const std::string & gcc_target_options() const
void set_verbose(int level)
void set_source_file(const char *filename)
bool compile(const char *filename, int verbose_level=-1)
const char * c_str() const
virtual void puts(const char *str)
struct cmd_list_element * showlist
struct cmd_list_element * cmdlist
struct cmd_list_element * setlist
struct cmd_list_element * showdebuglist
struct cmd_list_element * setdebuglist
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
void set_cmd_completer_handle_brkchars(struct cmd_list_element *cmd, completer_handle_brkchars_ftype *func)
cmd_list_element * add_com_alias(const char *name, cmd_list_element *target, command_class theclass, int abbrev_flag)
set_show_commands add_setshow_optional_filename_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
set_show_commands add_setshow_string_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
counted_command_line get_command_line(enum command_control_type type, const char *arg)
enum command_control_type execute_control_command_untraced(struct command_line *cmd)
std::shared_ptr< command_line > counted_command_line
compile_module_up compile_object_load(const compile_file_names &file_names, enum compile_i_scope_types scope, void *scope_data)
std::unique_ptr< compile_module > compile_module_up
void compile_object_run(compile_module_up &&module)
static gdb_argv compile_args_argv
static void compile_code_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
static std::string compile_args
static void compile_command(const char *args, int from_tty)
static compile_file_names get_new_file_names()
static gdb_argv get_args(const compile_instance *compiler, struct gdbarch *gdbarch)
static gdb::option::option_def_group make_compile_options_def_group(compile_options *opts)
void eval_compile_command(struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope, void *scope_data)
static void filter_args(char **argv)
static const char * get_selected_pc_producer_options(void)
static const struct block * get_expr_block_and_pc(CORE_ADDR *pc)
static void do_rmdir(void *arg)
static hashval_t hash_type_map_instance(const void *p)
static hashval_t hash_symbol_error(const void *a)
static void set_compile_args(const char *args, int from_tty, struct cmd_list_element *c)
static std::string compile_gcc
static void compile_file_command(const char *args, int from_tty)
static int eq_type_map_instance(const void *a, const void *b)
static compile_file_names compile_to_object(struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope)
static void show_compile_gcc(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static void compile_code_command(const char *args, int from_tty)
static void show_compile_args(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static const gdb::option::option_def compile_command_option_defs[]
static void print_callback(void *ignore, const char *message)
void compile_print_value(struct value *val, void *data_voidp)
static void show_compile_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static struct cmd_list_element * compile_command_list
cmd_list_element * compile_cmd_element
void _initialize_compile()
int compile_register_name_demangle(struct gdbarch *gdbarch, const char *regname)
static void compile_file_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
static int eq_symbol_error(const void *a, const void *b)
std::string compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
static const char * get_compile_file_tempdir(void)
static void del_symbol_error(void *a)
static void compile_print_command(const char *arg, int from_tty)
const char * advance_to_expression_complete_word_point(completion_tracker &tracker, const char *text)
const char * advance_to_filename_complete_word_point(completion_tracker &tracker, const char *text)
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
void symbol_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
@ COMPILE_I_PRINT_ADDRESS_SCOPE
@ COMPILE_I_PRINT_VALUE_SCOPE
CORE_ADDR get_frame_pc(frame_info_ptr frame)
frame_info_ptr get_selected_frame(const char *message)
CORE_ADDR get_frame_address_in_block(frame_info_ptr this_frame)
const struct block * get_selected_block(CORE_ADDR *addr_in_block)
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
int gdbarch_num_regs(struct gdbarch *gdbarch)
const char * gdbarch_gnu_triplet_regexp(struct gdbarch *gdbarch)
std::string gdbarch_gcc_target_options(struct gdbarch *gdbarch)
enum gdb_osabi gdbarch_osabi(struct gdbarch *gdbarch)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
const struct language_defn * current_language
bool process_options(const char **args, process_options_mode mode, gdb::array_view< const option_def_group > options_group)
@ PROCESS_OPTIONS_REQUIRE_DELIMITER
@ PROCESS_OPTIONS_UNKNOWN_IS_ERROR
std::string build_help(const char *help_tmpl, gdb::array_view< const option_def_group > options_group)
bool complete_options(completion_tracker &tracker, const char **args, process_options_mode mode, gdb::array_view< const option_def_group > options_group)
const char * osabi_triplet_regexp(enum gdb_osabi osabi)
void print_command_parse_format(const char **expp, const char *cmdname, value_print_options *opts)
void print_value(value *val, const value_print_options &opts)
void print_command_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *)
struct symtab_and_line get_current_source_symtab_and_line(void)
CORE_ADDR entry_pc() const
struct block * static_block()
counted_command_line body_list_0
enum compile_i_scope_types scope
struct command_line * next
struct blockvector * blockvector()
virtual std::string compute_program(compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc) const
virtual const char * name() const =0
virtual std::unique_ptr< compile_instance > get_compile_instance() const
const struct symbol * sym
struct compunit_symtab * compunit() const
struct compunit_symtab * find_pc_compunit_symtab(CORE_ADDR pc)
bool target_has_execution(inferior *inf)
void gdb_printf(struct ui_file *stream, const char *format,...)
void gdb_puts(const char *linebuffer, struct ui_file *stream)
gdb::option::option_def_group make_value_print_options_def_group(value_print_options *opts)
void get_user_print_options(struct value_print_options *opts)