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)
436 perror_with_name (filename);
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 ();
506 bfd *prev_bfd = exec_bfd_holder.get ();
508 bool reload_p = (((prev_bfd !=
nullptr) == (curr_bfd !=
nullptr))
509 && (prev_bfd ==
nullptr
510 || (strcmp (bfd_get_filename (prev_bfd),
511 bfd_get_filename (curr_bfd)) == 0)));
528 && !
query (_(
"A program is being debugged already.\n"
529 "Are you sure you want to change the file? ")))
530 error (_(
"File not changed."));
537 gdb_argv built_argv (
args);
538 char **argv = built_argv.get ();
540 for (; (*argv != NULL) && (**argv ==
'-'); argv++)
544 error (_(
"No executable file name was specified"));
546 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
586 aflag = bfd_section_flags (asect);
587 if (!(aflag & SEC_ALLOC))
590 table.emplace_back (bfd_section_vma (asect),
591 bfd_section_vma (asect) + bfd_section_size (asect),
605 if (!sections.empty ())
620 if (
inf->pspace !=
this)
637 gdb_assert (
objfile !=
nullptr);
642 if (bfd_section_size (osect->the_bfd_section) == 0)
646 osect->the_bfd_section, (
void *)
objfile);
656 gdb_assert (owner != NULL);
662 return sect.owner == owner;
675 if (
inf->pspace !=
this)
697 ULONGEST len, ULONGEST *xfered_len)
710 if ((s->flags & SEC_LOAD) == 0
711 || (s->flags & SEC_READONLY) == 0)
715 size = bfd_section_size (s);
716 if (vma <= offset && offset < (vma +
size))
720 amt = (vma +
size) - offset;
725 readbuf, offset - vma, amt);
746static std::vector<mem_range>
750 std::vector<mem_range> memory;
754 if ((bfd_section_flags (p.the_bfd_section) & SEC_READONLY) == 0)
760 ULONGEST lo1, hi1, lo2, hi2;
768 CORE_ADDR start = std::max (lo1, lo2);
769 int length = std::min (hi1, hi2) - start;
771 memory.emplace_back (start, length);
780 ULONGEST len, ULONGEST *xfered_len)
784 std::vector<mem_range> available_memory
789 for (
const mem_range &r : available_memory)
797 end = std::min<CORE_ADDR> (offset + len, r.start + r.length);
799 gdb_assert (end - offset <= len);
801 if (offset >= r.start)
807 *xfered_len = r.start - offset;
820 ULONGEST offset, ULONGEST len,
821 ULONGEST *xfered_len,
823 gdb::function_view<
bool
827 ULONGEST memaddr = offset;
828 ULONGEST memend = memaddr + len;
830 gdb_assert (len != 0);
834 struct bfd_section *asect = p.the_bfd_section;
835 bfd *abfd = asect->owner;
837 if (match_cb !=
nullptr && !match_cb (&p))
839 if (memaddr >= p.addr)
841 if (memend <= p.endaddr)
845 res = bfd_set_section_contents (abfd, asect,
846 writebuf, memaddr - p.addr,
849 res = bfd_get_section_contents (abfd, asect,
850 readbuf, memaddr - p.addr,
861 else if (memaddr >= p.endaddr)
869 len = p.endaddr - memaddr;
871 res = bfd_set_section_contents (abfd, asect,
872 writebuf, memaddr - p.addr,
875 res = bfd_get_section_contents (abfd, asect,
876 readbuf, memaddr - p.addr,
894 const char *annex, gdb_byte *readbuf,
895 const gdb_byte *writebuf,
896 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
902 offset, len, xfered_len,
918 bfd_get_filename (abfd)));
920 gdb_printf (_(
"file type %s.\n"), bfd_get_target (abfd));
925 bfd_vma displacement = 0;
931 struct bfd_section *psect = p.the_bfd_section;
933 if ((bfd_section_flags (psect) & (SEC_ALLOC | SEC_LOAD))
934 != (SEC_ALLOC | SEC_LOAD))
937 if (bfd_section_vma (psect) <= abfd->start_address
938 && abfd->start_address < (bfd_section_vma (psect)
939 + bfd_section_size (psect)))
941 displacement = p.addr - bfd_section_vma (psect);
947 warning (_(
"Cannot find section for the entry point of %ps."),
949 bfd_get_filename (abfd)));
952 bfd_get_start_address (abfd)
959 struct bfd_section *psect = p.the_bfd_section;
960 bfd *pbfd = psect->owner;
962 gdb_printf (
"\t%s", hex_string_custom (p.addr, wid));
963 gdb_printf (
" - %s", hex_string_custom (p.endaddr, wid));
973 hex_string_custom (psect->filepos, 8));
974 gdb_printf (
" is %s", bfd_section_name (psect));
978 bfd_get_filename (pbfd)));
990 gdb_puts (_(
"\t<no file loaded>\n"));
999 error (_(
"Must specify section name and its virtual address"));
1002 for (secname = args; !isspace (*args); args++);
1003 unsigned seclen = args - secname;
1010 if (!strncmp (secname, bfd_section_name (p.the_bfd_section), seclen)
1011 && bfd_section_name (p.the_bfd_section)[seclen] ==
'\0')
1013 long offset = secaddr - p.addr;
1015 p.endaddr += offset;
1022 std::string secprint (secname, seclen);
1023 error (_(
"Section %s not found"), secprint.c_str ());
1034 if (filename_cmp (filename,
1035 bfd_get_filename (p.the_bfd_section->owner)) == 0
1036 && index == p.the_bfd_section->index)
1038 p.endaddr += address - p.addr;
1052gdb::unique_xmalloc_ptr<char>
1055 error (_(
"Can't create a corefile"));
1071Use FILE as program to be debugged.\n\
1072It is read for its symbols, for getting the contents of pure memory,\n\
1073and it is the program executed when you use the `run' command.\n\
1074If FILE cannot be found as specified, your execution directory path\n\
1075($PATH) is searched for a command of that name.\n\
1076No arg means to have no executable file and no symbols."), &
cmdlist);
1080Use FILE as program for getting contents of pure memory.\n\
1081If FILE cannot be found as specified, your execution directory path\n\
1082is searched for a command of that name.\n\
1083No arg means have no executable file."), &
cmdlist);
1087Change the base address of section SECTION of the exec file to ADDR.\n\
1088This can be used if the exec file does not contain section addresses,\n\
1089(such as in the a.out format), or when the addresses specified in the\n\
1090file itself are wrong. Each section must be changed separately. The\n\
1091``info files'' command lists all the sections and their addresses."));
1094Set writing into executable and core files."), _(
"\
1095Show writing into executable and core files."), NULL,
1104Set exec-file-mismatch handling (ask|warn|off)."),
1106Show exec-file-mismatch handling (ask|warn|off)."),
1108Specifies how to handle a mismatch between the current exec-file\n\
1109loaded by GDB and the exec-file automatically determined when attaching\n\
1111 ask - warn the user and ask whether to load the determined exec-file.\n\
1112 warn - warn the user, but do not change the exec-file.\n\
1113 off - do not check for mismatch.\n\
1115GDB detects a mismatch by comparing the build IDs of the files.\n\
1116If the user confirms loading the determined exec-file, then its symbols\n\
1117will 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
target_ops * top_target()
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< struct program_space *, bool > executable_changed
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
iterator_range< section_iterator > sections()
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={})
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)