43#include "readline/tilde.h"
50#include "gdbsupport/pathstuff.h"
52#include "gdbsupport/buildargv.h"
58 N_(
"Local exec file"),
59 N_(
"Use an executable file as a target.\n\
60Specify the filename of the executable file.")
72 void close ()
override;
76 const gdb_byte *writebuf,
77 ULONGEST offset, ULONGEST len,
78 ULONGEST *xfered_len)
override;
92 = {
"ask",
"warn",
"off", NULL };
107 _(
"exec-file-mismatch handling is currently \"%s\".\n"),
126 internal_error (_(
"Unrecognized exec-file-mismatch setting: \"%s\""),
138 gdb_printf (file, _(
"Writing into executable and core files is %s.\n"),
158 ss->clear_target_sections ();
167 symfile_add_flags add_flags)
169 struct gdb_exception prev_err;
188 catch (gdb_exception_error &
err)
190 if (
err.message != NULL)
191 warning (
"%s",
err.what ());
193 prev_err = std::move (
err);
196 if (exec_file_host != NULL)
202 catch (
const gdb_exception_error &
err)
205 warning (
"%s",
err.what ());
223 bool build_id_mismatch =
false;
226 if (current_exec_file == NULL || pid_exec_file == NULL)
239 const bfd_build_id *exec_file_build_id
241 if (exec_file_build_id !=
nullptr)
245 std::string target_pid_exec_file
252 const bfd_build_id *target_exec_file_build_id
255 if (target_exec_file_build_id !=
nullptr)
257 if (exec_file_build_id->size == target_exec_file_build_id->size
258 && memcmp (exec_file_build_id->data,
259 target_exec_file_build_id->data,
260 exec_file_build_id->size) == 0)
266 build_id_mismatch =
true;
271 if (build_id_mismatch)
273 std::string exec_file_target (pid_exec_file);
281 (_(
"Build ID mismatch between current exec-file %ps\n"
282 "and automatically determined exec-file %ps\n"
283 "exec-file-mismatch handling is currently \"%s\""),
300 catch (gdb_exception_error &
err)
302 warning (_(
"loading %ps %s"),
304 exec_file_target.c_str ()),
305 err.message != NULL ?
err.what () :
"error");
316 const char *exec_file_target;
317 symfile_add_flags add_flags = 0;
325 if (exec_file_target == NULL)
327 warning (_(
"No executable has been specified and target does not "
329 "determining executable automatically. "
330 "Try using the \"file\" command."));
334 gdb::unique_xmalloc_ptr<char> exec_file_host
387 int load_via_target = 0;
388 const char *scratch_pathname, *canonical_pathname;
400 gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
405 warning (_(
"writing into executable files is "
406 "not supported for %s sysroots"),
409 scratch_pathname = filename;
411 canonical_pathname = scratch_pathname;
419#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
420 if (scratch_chan < 0)
422 int first_errno = errno;
423 char *exename = (
char *) alloca (strlen (filename) + 5);
425 strcat (strcpy (exename, filename),
".exe");
431 if (scratch_chan < 0)
435 if (scratch_chan < 0)
438 scratch_pathname = scratch_storage.get ();
442 canonical_storage = gdb_realpath (scratch_pathname);
443 canonical_pathname = canonical_storage.get ();
449 FOPEN_RUB, scratch_chan);
456 error (_(
"\"%s\": could not open as an executable file: %s."),
457 scratch_pathname, bfd_errmsg (bfd_get_error ()));
465 = (make_unique_xstrdup
469 = make_unique_xstrdup (gdb_realpath_keepfile
470 (scratch_pathname).c_str ());
473 bfd_object, &matching))
478 error (_(
"\"%s\": not in executable format: %s"), scratch_pathname,
500 (*deprecated_exec_file_display_hook) (filename);
503 bfd_cache_close_all ();
519 && !
query (_(
"A program is being debugged already.\n"
520 "Are you sure you want to change the file? ")))
521 error (_(
"File not changed."));
528 gdb_argv built_argv (
args);
529 char **argv = built_argv.get ();
531 for (; (*argv != NULL) && (**argv ==
'-'); argv++)
535 error (_(
"No executable file name was specified"));
537 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
577 aflag = bfd_section_flags (asect);
578 if (!(aflag & SEC_ALLOC))
581 table.emplace_back (bfd_section_vma (asect),
582 bfd_section_vma (asect) + bfd_section_size (asect),
596 if (!sections.empty ())
611 if (
inf->pspace !=
this)
630 gdb_assert (
objfile !=
nullptr);
649 gdb_assert (owner != NULL);
655 return sect.owner == owner;
668 if (
inf->pspace !=
this)
690 ULONGEST len, ULONGEST *xfered_len)
703 if ((s->flags & SEC_LOAD) == 0
704 || (s->flags & SEC_READONLY) == 0)
708 size = bfd_section_size (s);
718 readbuf,
offset - vma, amt);
739static std::vector<mem_range>
743 std::vector<mem_range> memory;
747 if ((bfd_section_flags (p.the_bfd_section) & SEC_READONLY) == 0)
753 ULONGEST lo1, hi1, lo2, hi2;
761 CORE_ADDR start = std::max (lo1, lo2);
762 int length = std::min (hi1, hi2) - start;
764 memory.emplace_back (start, length);
773 ULONGEST len, ULONGEST *xfered_len)
777 std::vector<mem_range> available_memory
782 for (
const mem_range &r : available_memory)
790 end = std::min<CORE_ADDR> (
offset + len, r.start + r.length);
792 gdb_assert (end -
offset <= len);
800 *xfered_len = r.start -
offset;
813 ULONGEST
offset, ULONGEST len,
814 ULONGEST *xfered_len,
816 gdb::function_view<
bool
820 ULONGEST memaddr =
offset;
821 ULONGEST memend = memaddr + len;
823 gdb_assert (len != 0);
827 struct bfd_section *asect = p.the_bfd_section;
828 bfd *abfd = asect->owner;
830 if (match_cb !=
nullptr && !match_cb (&p))
832 if (memaddr >= p.addr)
834 if (memend <= p.endaddr)
838 res = bfd_set_section_contents (abfd, asect,
839 writebuf, memaddr - p.addr,
842 res = bfd_get_section_contents (abfd, asect,
843 readbuf, memaddr - p.addr,
854 else if (memaddr >= p.endaddr)
862 len = p.endaddr - memaddr;
864 res = bfd_set_section_contents (abfd, asect,
865 writebuf, memaddr - p.addr,
868 res = bfd_get_section_contents (abfd, asect,
869 readbuf, memaddr - p.addr,
887 const char *annex, gdb_byte *readbuf,
888 const gdb_byte *writebuf,
889 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
895 offset, len, xfered_len,
911 bfd_get_filename (abfd)));
913 gdb_printf (_(
"file type %s.\n"), bfd_get_target (abfd));
918 bfd_vma displacement = 0;
924 struct bfd_section *psect = p.the_bfd_section;
926 if ((bfd_section_flags (psect) & (SEC_ALLOC | SEC_LOAD))
927 != (SEC_ALLOC | SEC_LOAD))
930 if (bfd_section_vma (psect) <= abfd->start_address
931 && abfd->start_address < (bfd_section_vma (psect)
932 + bfd_section_size (psect)))
934 displacement = p.addr - bfd_section_vma (psect);
940 warning (_(
"Cannot find section for the entry point of %ps."),
942 bfd_get_filename (abfd)));
945 bfd_get_start_address (abfd)
952 struct bfd_section *psect = p.the_bfd_section;
953 bfd *pbfd = psect->owner;
955 gdb_printf (
"\t%s", hex_string_custom (p.addr, wid));
956 gdb_printf (
" - %s", hex_string_custom (p.endaddr, wid));
966 hex_string_custom (psect->filepos, 8));
967 gdb_printf (
" is %s", bfd_section_name (psect));
971 bfd_get_filename (pbfd)));
983 gdb_puts (_(
"\t<no file loaded>\n"));
992 error (_(
"Must specify section name and its virtual address"));
995 for (secname = args; !isspace (*args); args++);
996 unsigned seclen = args - secname;
1003 if (!strncmp (secname, bfd_section_name (p.the_bfd_section), seclen)
1004 && bfd_section_name (p.the_bfd_section)[seclen] ==
'\0')
1006 long offset = secaddr - p.addr;
1008 p.endaddr += offset;
1015 std::string secprint (secname, seclen);
1016 error (_(
"Section %s not found"), secprint.c_str ());
1027 if (filename_cmp (filename,
1028 bfd_get_filename (p.the_bfd_section->owner)) == 0
1029 && index == p.the_bfd_section->index)
1031 p.endaddr += address - p.addr;
1045gdb::unique_xmalloc_ptr<char>
1048 error (_(
"Can't create a corefile"));
1064Use FILE as program to be debugged.\n\
1065It is read for its symbols, for getting the contents of pure memory,\n\
1066and it is the program executed when you use the `run' command.\n\
1067If FILE cannot be found as specified, your execution directory path\n\
1068($PATH) is searched for a command of that name.\n\
1069No arg means to have no executable file and no symbols."), &
cmdlist);
1073Use FILE as program for getting contents of pure memory.\n\
1074If FILE cannot be found as specified, your execution directory path\n\
1075is searched for a command of that name.\n\
1076No arg means have no executable file."), &
cmdlist);
1080Change the base address of section SECTION of the exec file to ADDR.\n\
1081This can be used if the exec file does not contain section addresses,\n\
1082(such as in the a.out format), or when the addresses specified in the\n\
1083file itself are wrong. Each section must be changed separately. The\n\
1084``info files'' command lists all the sections and their addresses."));
1087Set writing into executable and core files."), _(
"\
1088Show writing into executable and core files."), NULL,
1097Set exec-file-mismatch handling (ask|warn|off)."),
1099Show exec-file-mismatch handling (ask|warn|off)."),
1101Specifies how to handle a mismatch between the current exec-file\n\
1102loaded by GDB and the exec-file automatically determined when attaching\n\
1104 ask - warn the user and ask whether to load the determined exec-file.\n\
1105 warn - warn the user, but do not change the exec-file.\n\
1106 off - do not check for mismatch.\n\
1108GDB detects a mismatch by comparing the build IDs of the files.\n\
1109If the user confirms loading the determined exec-file, then its symbols\n\
1110will be loaded as well."),
struct gdbarch * gdbarch_from_bfd(bfd *abfd)
void set_gdbarch_from_file(bfd *abfd)
const struct bfd_build_id * build_id_bfd_get(bfd *abfd)
ui_file_style style() const
void push_target(struct target_ops *t)
const std::string & args() const
struct program_space * pspace
struct cmd_list_element * showlist
struct cmd_list_element * cmdlist
struct cmd_list_element * setlist
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(struct cmd_list_element *cmd, completer_ftype *completer)
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc)
set_show_commands add_setshow_enum_cmd(const char *name, enum command_class theclass, const char *const *enumlist, const char **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_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)
cli_style_option file_name_style
void filename_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
void validate_files(void)
void reopen_exec_file(void)
const char * get_exec_file(int err)
hook_type deprecated_exec_file_display_hook
int(* find_memory_region_ftype)(CORE_ADDR addr, unsigned long size, int read, int write, int exec, int modified, bool memory_tagged, void *data)
void symbol_file_command(const char *, int)
CORE_ADDR parse_and_eval_address(const char *exp)
enum target_xfer_status section_table_xfer_memory_partial(gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len, const target_section_table §ions, gdb::function_view< bool(const struct target_section *)> match_cb)
static void show_exec_file_mismatch_command(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static void exec_file_command(const char *args, int from_tty)
static void exec_target_open(const char *args, int from_tty)
void exec_set_section_address(const char *filename, int index, CORE_ADDR address)
enum target_xfer_status section_table_read_available_memory(gdb_byte *readbuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
target_section_table build_section_table(struct bfd *some_bfd)
void exec_file_attach(const char *filename, int from_tty)
static const char * exec_file_mismatch
void exec_file_locate_attach(int pid, int defer_bp_reset, int from_tty)
void try_open_exec_file(const char *exec_file_host, struct inferior *inf, symfile_add_flags add_flags)
static void file_command(const char *arg, int from_tty)
void print_section_info(const target_section_table *t, bfd *abfd)
static const char *const exec_file_mismatch_names[]
static const target_info exec_target_info
void validate_exec_file(int from_tty)
static void set_exec_file_mismatch_command(const char *ignore, int from_tty, struct cmd_list_element *c)
void(* deprecated_file_changed_hook)(const char *)
enum target_xfer_status exec_read_partial_read_only(gdb_byte *readbuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
@ exec_file_mismatch_warn
void exec_on_vfork(inferior *vfork_child)
static void show_write_files(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static exec_target exec_ops
static std::vector< mem_range > section_table_available_memory(CORE_ADDR memaddr, ULONGEST len, const target_section_table §ions)
static void set_section_command(const char *args, int from_tty)
int objfile_find_memory_regions(struct target_ops *self, find_memory_region_ftype func, void *obfd)
gdb_bfd_ref_ptr gdb_bfd_fopen(const char *filename, const char *target, const char *mode, int fd)
std::string gdb_bfd_errmsg(bfd_error_type error_tag, char **matching)
int is_target_filename(const char *name)
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd, bool warn_if_slow)
#define TARGET_SYSROOT_PREFIX
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
int gdbarch_addr_bit(struct gdbarch *gdbarch)
CORE_ADDR gdbarch_addr_bits_remove(struct gdbarch *gdbarch, CORE_ADDR addr)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
struct inferior * current_inferior(void)
void switch_to_inferior_no_thread(inferior *inf)
all_inferiors_range all_inferiors(process_stratum_target *proc_target=nullptr)
int mem_ranges_overlap(CORE_ADDR start1, int len1, CORE_ADDR start2, int len2)
void normalize_mem_ranges(std::vector< mem_range > *memory)
observable executable_changed
#define ALL_OBJFILE_OSECTIONS(objfile, osect)
struct program_space * current_program_space
std::vector< struct program_space * > program_spaces
void(* func)(remote_target *remote, char *)
gdb::unique_xmalloc_ptr< char > exec_file_find(const char *in_pathname, int *fd)
int openp(const char *path, openp_flags opts, const char *string, int mode, gdb::unique_xmalloc_ptr< char > *filename_opened)
enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) override
gdb::unique_xmalloc_ptr< char > make_corefile_notes(bfd *, int *) override
bool has_memory() override
void files_info() override
int find_memory_regions(find_memory_region_ftype func, void *data) override
strata stratum() const override
const target_info & info() const override
CORE_ADDR endaddr() const
struct bfd_section * the_bfd_section
void remove_target_sections(void *owner)
void add_target_sections(void *owner, const target_section_table §ions)
target_section_table m_target_sections
target_section_table & target_sections()
void set_exec_bfd(gdb_bfd_ref_ptr &&abfd)
gdb::unique_xmalloc_ptr< char > exec_filename
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
std::vector< target_section > target_section_table
bool target_filesystem_is_local()
const char * target_pid_to_exec_file(int pid)
bool target_has_execution(inferior *inf)
const target_section_table * target_get_section_table(struct target_ops *target)
void add_target(const target_info &t, target_open_ftype *func, completer_ftype *completer)
void target_preopen(int from_tty)
@ TARGET_XFER_UNAVAILABLE
static styled_string_s * styled_string(const ui_file_style &style, const char *str, styled_string_s &&tmp={})
void perror_with_name(const char *string)
int query(const char *ctlstr,...)
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
void gdb_printf(struct ui_file *stream, const char *format,...)
void gdb_puts(const char *linebuffer, struct ui_file *stream)