1 /* Core dump and executable file functions above target vector, for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "event-top.h"
23 #include "extract-store-integer.h"
27 #include "cli/cli-cmds.h"
33 #include "completer.h"
34 #include "observable.h"
35 #include "cli/cli-utils.h"
40 reopen_exec_file (void)
42 bfd
*exec_bfd
= current_program_space
->exec_bfd ();
44 /* Don't do anything if there isn't an exec file. */
45 if (exec_bfd
== nullptr)
48 /* The main executable can't be an in-memory BFD object. If it was then
49 the use of bfd_stat below would not work as expected. */
50 gdb_assert ((exec_bfd
->flags
& BFD_IN_MEMORY
) == 0);
52 /* If the timestamp of the exec file has changed, reopen it. */
54 int res
= bfd_stat (exec_bfd
, &st
);
57 && current_program_space
->ebfd_mtime
!= 0
58 && current_program_space
->ebfd_mtime
!= st
.st_mtime
)
59 exec_file_attach (bfd_get_filename (exec_bfd
), 0);
62 /* If we have both a core file and an exec file,
63 print a warning if they don't go together. */
68 if (current_program_space
->exec_bfd () && current_program_space
->core_bfd ())
70 if (!core_file_matches_executable_p (current_program_space
->core_bfd (),
71 current_program_space
->exec_bfd ()))
72 warning (_("core file may not match specified executable file."));
73 else if (bfd_get_mtime (current_program_space
->exec_bfd ())
74 > bfd_get_mtime (current_program_space
->core_bfd ()))
75 warning (_("exec file is newer than core file."));
79 /* See gdbsupport/common-inferior.h. */
82 get_exec_file (int err
)
84 if (current_program_space
->exec_filename
!= nullptr)
85 return current_program_space
->exec_filename
.get ();
89 error (_("No executable file specified.\n\
90 Use the \"file\" or \"exec-file\" command."));
95 memory_error_message (enum target_xfer_status err
,
96 struct gdbarch
*gdbarch
, CORE_ADDR memaddr
)
100 case TARGET_XFER_E_IO
:
101 /* Actually, address between memaddr and memaddr + len was out of
103 return string_printf (_("Cannot access memory at address %s"),
104 paddress (gdbarch
, memaddr
));
105 case TARGET_XFER_UNAVAILABLE
:
106 return string_printf (_("Memory at address %s unavailable."),
107 paddress (gdbarch
, memaddr
));
109 internal_error ("unhandled target_xfer_status: %s (%s)",
110 target_xfer_status_to_string (err
),
115 /* Report a memory error by throwing a suitable exception. */
118 memory_error (enum target_xfer_status err
, CORE_ADDR memaddr
)
120 enum errors exception
= GDB_NO_ERROR
;
122 /* Build error string. */
124 = memory_error_message (err
, current_inferior ()->arch (), memaddr
);
126 /* Choose the right error to throw. */
129 case TARGET_XFER_E_IO
:
130 exception
= MEMORY_ERROR
;
132 case TARGET_XFER_UNAVAILABLE
:
133 exception
= NOT_AVAILABLE_ERROR
;
138 throw_error (exception
, ("%s"), str
.c_str ());
141 /* Helper function. */
144 read_memory_object (enum target_object object
, CORE_ADDR memaddr
,
145 gdb_byte
*myaddr
, ssize_t len
)
151 enum target_xfer_status status
;
154 status
= target_xfer_partial (current_inferior ()->top_target (), object
,
155 NULL
, myaddr
+ xfered
, NULL
,
156 memaddr
+ xfered
, len
- xfered
,
159 if (status
!= TARGET_XFER_OK
)
160 memory_error (status
== TARGET_XFER_EOF
? TARGET_XFER_E_IO
: status
,
163 xfered
+= xfered_len
;
168 /* Same as target_read_memory, but report an error if can't read. */
171 read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
173 read_memory_object (TARGET_OBJECT_MEMORY
, memaddr
, myaddr
, len
);
176 /* Same as target_read_stack, but report an error if can't read. */
179 read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
181 read_memory_object (TARGET_OBJECT_STACK_MEMORY
, memaddr
, myaddr
, len
);
184 /* Same as target_read_code, but report an error if can't read. */
187 read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
189 read_memory_object (TARGET_OBJECT_CODE_MEMORY
, memaddr
, myaddr
, len
);
192 /* Read memory at MEMADDR of length LEN and put the contents in
193 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
197 safe_read_memory_integer (CORE_ADDR memaddr
, int len
,
198 enum bfd_endian byte_order
,
199 LONGEST
*return_value
)
201 gdb_byte buf
[sizeof (LONGEST
)];
203 if (target_read_memory (memaddr
, buf
, len
))
206 *return_value
= extract_signed_integer (buf
, len
, byte_order
);
210 /* Read memory at MEMADDR of length LEN and put the contents in
211 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
215 safe_read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
216 enum bfd_endian byte_order
,
217 ULONGEST
*return_value
)
219 gdb_byte buf
[sizeof (ULONGEST
)];
221 if (target_read_memory (memaddr
, buf
, len
))
224 *return_value
= extract_unsigned_integer (buf
, len
, byte_order
);
229 read_memory_integer (CORE_ADDR memaddr
, int len
,
230 enum bfd_endian byte_order
)
232 gdb_byte buf
[sizeof (LONGEST
)];
234 read_memory (memaddr
, buf
, len
);
235 return extract_signed_integer (buf
, len
, byte_order
);
239 read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
240 enum bfd_endian byte_order
)
242 gdb_byte buf
[sizeof (ULONGEST
)];
244 read_memory (memaddr
, buf
, len
);
245 return extract_unsigned_integer (buf
, len
, byte_order
);
249 read_code_integer (CORE_ADDR memaddr
, int len
,
250 enum bfd_endian byte_order
)
252 gdb_byte buf
[sizeof (LONGEST
)];
254 read_code (memaddr
, buf
, len
);
255 return extract_signed_integer (buf
, len
, byte_order
);
259 read_code_unsigned_integer (CORE_ADDR memaddr
, int len
,
260 enum bfd_endian byte_order
)
262 gdb_byte buf
[sizeof (ULONGEST
)];
264 read_code (memaddr
, buf
, len
);
265 return extract_unsigned_integer (buf
, len
, byte_order
);
269 read_memory_typed_address (CORE_ADDR addr
, struct type
*type
)
271 gdb_byte
*buf
= (gdb_byte
*) alloca (type
->length ());
273 read_memory (addr
, buf
, type
->length ());
274 return extract_typed_address (buf
, type
);
280 write_memory (CORE_ADDR memaddr
,
281 const bfd_byte
*myaddr
, ssize_t len
)
285 status
= target_write_memory (memaddr
, myaddr
, len
);
287 memory_error (TARGET_XFER_E_IO
, memaddr
);
290 /* Notify interpreters and observers that INF's memory was changed. */
293 notify_memory_changed (inferior
*inf
, CORE_ADDR addr
, ssize_t len
,
294 const bfd_byte
*data
)
296 interps_notify_memory_changed (inf
, addr
, len
, data
);
297 gdb::observers::memory_changed
.notify (inf
, addr
, len
, data
);
300 /* Same as write_memory, but notify 'memory_changed' observers. */
303 write_memory_with_notification (CORE_ADDR memaddr
, const bfd_byte
*myaddr
,
306 write_memory (memaddr
, myaddr
, len
);
307 notify_memory_changed (current_inferior (), memaddr
, len
, myaddr
);
310 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
313 write_memory_unsigned_integer (CORE_ADDR addr
, int len
,
314 enum bfd_endian byte_order
,
317 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
319 store_unsigned_integer (buf
, len
, byte_order
, value
);
320 write_memory (addr
, buf
, len
);
323 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
326 write_memory_signed_integer (CORE_ADDR addr
, int len
,
327 enum bfd_endian byte_order
,
330 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
332 store_signed_integer (buf
, len
, byte_order
, value
);
333 write_memory (addr
, buf
, len
);
336 /* The current default bfd target. Points to storage allocated for
338 const char *gnutarget
;
340 /* Same thing, except it is "auto" not NULL for the default case. */
341 static std::string gnutarget_string
;
343 show_gnutarget_string (struct ui_file
*file
, int from_tty
,
344 struct cmd_list_element
*c
,
348 _("The current BFD target is \"%s\".\n"), value
);
352 set_gnutarget_command (const char *ignore
, int from_tty
,
353 struct cmd_list_element
*c
)
355 const char *gend
= gnutarget_string
.c_str () + gnutarget_string
.size ();
356 gend
= remove_trailing_whitespace (gnutarget_string
.c_str (), gend
);
358 = gnutarget_string
.substr (0, gend
- gnutarget_string
.data ());
360 if (gnutarget_string
== "auto")
363 gnutarget
= gnutarget_string
.c_str ();
366 /* A completion function for "set gnutarget". */
369 complete_set_gnutarget (struct cmd_list_element
*cmd
,
370 completion_tracker
&tracker
,
371 const char *text
, const char *word
)
373 static const char **bfd_targets
;
375 if (bfd_targets
== NULL
)
379 bfd_targets
= bfd_target_list ();
380 for (last
= 0; bfd_targets
[last
] != NULL
; ++last
)
383 bfd_targets
= XRESIZEVEC (const char *, bfd_targets
, last
+ 2);
384 bfd_targets
[last
] = "auto";
385 bfd_targets
[last
+ 1] = NULL
;
388 complete_on_enum (tracker
, bfd_targets
, text
, word
);
391 /* Set the gnutarget. */
393 set_gnutarget (const char *newtarget
)
395 gnutarget_string
= newtarget
;
396 set_gnutarget_command (NULL
, 0, NULL
);
399 void _initialize_core ();
403 cmd_list_element
*core_file_cmd
404 = add_cmd ("core-file", class_files
, core_file_command
, _("\
405 Use FILE as core dump for examining memory and registers.\n\
406 Usage: core-file FILE\n\
407 No arg means have no core file. This command has been superseded by the\n\
408 `target core' and `detach' commands."), &cmdlist
);
409 set_cmd_completer (core_file_cmd
, filename_completer
);
412 set_show_commands set_show_gnutarget
413 = add_setshow_string_noescape_cmd ("gnutarget", class_files
,
414 &gnutarget_string
, _("\
415 Set the current BFD target."), _("\
416 Show the current BFD target."), _("\
417 Use `set gnutarget auto' to specify automatic detection."),
418 set_gnutarget_command
,
419 show_gnutarget_string
,
420 &setlist
, &showlist
);
421 set_cmd_completer (set_show_gnutarget
.set
, complete_set_gnutarget
);
423 add_alias_cmd ("g", set_show_gnutarget
.set
, class_files
, 1, &setlist
);
425 if (getenv ("GNUTARGET"))
426 set_gnutarget (getenv ("GNUTARGET"));
428 set_gnutarget ("auto");