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-"
84 return htab_hash_pointer (inst->
type);
105 return htab_hash_pointer (se->
sym);
116 return sea->
sym == seb->
sym;
134 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
172 slot = htab_find_slot (
m_type_map.get (), &inst, INSERT);
178 error (_(
"Unexpected type id from GCC, check you use recent enough GCC."));
222 if (
err == NULL ||
err->message == NULL)
225 gdb::unique_xmalloc_ptr<char> message (
err->message);
227 error (_(
"%s"), message.get ());
257 N_(
"Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
278 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
293 args = skip_spaces (args);
296 if (args ==
nullptr || args[0] ==
'\0')
297 error (_(
"You must provide a filename for this command."));
299 args = skip_spaces (args);
300 std::string abspath = gdb_abspath (args);
301 std::string buffer = string_printf (
"#include \"%s\"\n", abspath.c_str ());
310 const char *text,
const char *word)
330 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
348 l->control_u.compile.scope = scope;
358 const char *text,
const char *word)
391 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
411 l->control_u.compile.scope = scope;
412 l->control_u.compile.scope_data = &print_opts;
422 const char *dir = (
const char *) arg;
427 zap = concat (
"rm -rf ", dir, (
char *) NULL);
428 wstat = system (zap);
429 if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
430 warning (_(
"Could not remove temporary directory %s"), dir);
440 static char *tempdir_name;
442#define TEMPLATE TMP_PREFIX "XXXXXX"
445 if (tempdir_name != NULL)
450 tempdir_name = mkdtemp (tname);
451 if (tempdir_name == NULL)
454 tempdir_name = xstrdup (tempdir_name);
455 make_final_cleanup (
do_rmdir, tempdir_name);
470 dir, SLASH_STRING, seq),
471 string_printf (
"%s%sout%d.o",
472 dir, SLASH_STRING, seq));
477static const struct block *
511 " -Wno-unused-but-set-variable"
512 " -Wno-unused-variable"
514 " -fno-stack-protector";
533 gdb_printf (file, _(
"Compile command command-line arguments "
547 gdb_printf (file, _(
"Compile command GCC driver filename is \"%s\".\n"),
565 || !startswith (
symtab->producer (),
"GNU "))
569 while (*cs != 0 && *cs !=
'-')
570 cs = skip_spaces (skip_to_space (cs));
583 for (destv = argv; *argv != NULL; argv++)
586 if (strcmp (*argv,
"-fpreprocessed") == 0)
619 const char *cs_producer_options;
626 if (!gcc_options.empty ())
627 result = gdb_argv (gcc_options.c_str ());
630 if (cs_producer_options != NULL)
632 gdb_argv argv_producer (cs_producer_options);
635 result.append (std::move (argv_producer));
661 const struct block *expr_block;
662 CORE_ADDR trash_pc, expr_pc;
665 std::string triplet_rx;
668 error (_(
"The program must be running for the compile command to "\
675 std::unique_ptr<compile_instance> compiler
677 if (compiler ==
nullptr)
678 error (_(
"No compiler support for language %s."),
681 compiler->set_scope (scope);
682 compiler->set_block (expr_block);
697 input_buf.
puts (
"\n");
700 input = input_buf.
c_str ();
702 else if (cmd_string != NULL)
705 error (_(
"Neither a simple expression, or a multi-line specified."));
709 expr_block, expr_pc);
717 if (compiler->version () < GCC_FE_VERSION_1)
718 error (_(
"Command 'set compile-gcc' requires GCC version 6 or higher "
719 "(libcc1 interface version 1 or higher)"));
721 compiler->set_driver_filename (
compile_gcc.c_str ());
729 triplet_rx = std::string (arch_rx) +
"(-[^-]*)?-";
730 if (os_rx !=
nullptr)
732 compiler->set_triplet_regexp (triplet_rx.c_str ());
737 int argc = argv_holder.count ();
738 char **argv = argv_holder.get ();
740 gdb::unique_xmalloc_ptr<char> error_message
741 = compiler->set_arguments (argc, argv, triplet_rx.c_str ());
743 if (error_message != NULL)
744 error (
"%s", error_message.get ());
751 for (argi = 0; argi < argc; argi++)
758 gdb::optional<gdb::unlinker> source_remover;
761 gdb_file_up src = gdb_fopen_cloexec (fnames.
source_file (),
"w");
767 if (fputs (
code.c_str (), src.get ()) == EOF)
777 scoped_ignore_sigpipe ignore_sigpipe;
783 error (_(
"Compilation failed."));
790 source_remover->keep ();
812 gdb::unlinker object_remover (fnames.
object_file ());
813 gdb::unlinker source_remover (fnames.
source_file ());
826 source_remover.keep ();
827 object_remover.keep ();
839 return string_printf (
"__%s", regname);
850 if (regname[0] !=
'_' || regname[1] !=
'_')
851 error (_(
"Invalid register name \"%s\"."), regname);
858 error (_(
"Cannot find gdbarch register \"%s\"."), regname);
863#define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
869 (
void (*print_function) (
void *,
const char *),
void *datum)
887 if (
version () >= GCC_FE_VERSION_1)
896 if (
version () >= GCC_FE_VERSION_1)
905 if (
version () >= GCC_FE_VERSION_1)
911gdb::unique_xmalloc_ptr<char>
914 if (
version () >= GCC_FE_VERSION_1)
917 return gdb::unique_xmalloc_ptr<char> (
FORWARD (set_arguments_v0, regexp,
934 if (
version () >= GCC_FE_VERSION_1)
937 return FORWARD (compile_v0, filename, verbose_level);
953Command to compile source code and inject it into the inferior."),
959 static const std::string compile_code_help
961Compile, inject, and execute code.\n\
963Usage: compile code [OPTION]... [CODE]\n\
968The source code may be specified as a simple one line expression, e.g.:\n\
970 compile code printf(\"Hello world\\n\");\n\
972Alternatively, you can type a multiline expression by invoking\n\
973this command with no argument. GDB will then prompt for the\n\
974expression interactively; type a line containing \"end\" to\n\
975indicate the end of the expression."),
979 compile_code_help.c_str (),
983static const std::string compile_file_help
985Evaluate a file containing source code.\n\
987Usage: compile file [OPTION].. [FILENAME]\n\
994 compile_file_help.c_str (),
1000 static const std::string compile_print_help
1002Evaluate EXPR by using the compiler and print result.\n\
1004Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
1009Note: because this command accepts arbitrary expressions, if you\n\
1010specify any command option, you must use a double dash (\"--\")\n\
1011to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
1013The expression may be specified on the same line as the command, e.g.:\n\
1017Alternatively, you can type a multiline expression by invoking\n\
1018this command with no argument. GDB will then prompt for the\n\
1019expression interactively; type a line containing \"end\" to\n\
1020indicate the end of the expression.\n\
1022EXPR may be preceded with /FMT, where FMT is a format letter\n\
1023but no count or size letter (see \"x\" command)."),
1024 compile_print_opts);
1027 compile_print_help.c_str (),
1032Set compile command debugging."), _(
"\
1033Show compile command debugging."), _(
"\
1034When on, compile command debugging is enabled."),
1040 _(
"Set compile command GCC command-line arguments."),
1041 _(
"Show compile command GCC command-line arguments."),
1043Use options like -I (include file directory) or ABI settings.\n\
1044String quoting is parsed like in shell, for example:\n\
1045 -mno-align-double \"-I/dir with a space/include\""),
1054 _(
"Set compile command "
1055 "GCC driver filename."),
1056 _(
"Show compile command "
1057 "GCC driver filename."),
1059It should be absolute filename of the gcc executable.\n\
1060If 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 perror_with_name(const char *string)
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)