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/>. */
32 #include "completer.h"
33 #include "observable.h"
34 #include "cli/cli-utils.h"
39 reopen_exec_file (void)
41 bfd
*exec_bfd
= current_program_space
->exec_bfd ();
43 /* Don't do anything if there isn't an exec file. */
44 if (exec_bfd
== nullptr)
47 /* The main executable can't be an in-memory BFD object. If it was then
48 the use of bfd_stat below would not work as expected. */
49 gdb_assert ((exec_bfd
->flags
& BFD_IN_MEMORY
) == 0);
51 /* If the timestamp of the exec file has changed, reopen it. */
53 int res
= bfd_stat (exec_bfd
, &st
);
56 && current_program_space
->ebfd_mtime
!= 0
57 && current_program_space
->ebfd_mtime
!= st
.st_mtime
)
58 exec_file_attach (bfd_get_filename (exec_bfd
), 0);
61 /* If we have both a core file and an exec file,
62 print a warning if they don't go together. */
67 if (current_program_space
->exec_bfd () && current_program_space
->core_bfd ())
69 if (!core_file_matches_executable_p (current_program_space
->core_bfd (),
70 current_program_space
->exec_bfd ()))
71 warning (_("core file may not match specified executable file."));
72 else if (bfd_get_mtime (current_program_space
->exec_bfd ())
73 > bfd_get_mtime (current_program_space
->core_bfd ()))
74 warning (_("exec file is newer than core file."));
78 /* See gdbsupport/common-inferior.h. */
81 get_exec_file (int err
)
83 if (current_program_space
->exec_filename
!= nullptr)
84 return current_program_space
->exec_filename
.get ();
88 error (_("No executable file specified.\n\
89 Use the \"file\" or \"exec-file\" command."));
94 memory_error_message (enum target_xfer_status err
,
95 struct gdbarch
*gdbarch
, CORE_ADDR memaddr
)
99 case TARGET_XFER_E_IO
:
100 /* Actually, address between memaddr and memaddr + len was out of
102 return string_printf (_("Cannot access memory at address %s"),
103 paddress (gdbarch
, memaddr
));
104 case TARGET_XFER_UNAVAILABLE
:
105 return string_printf (_("Memory at address %s unavailable."),
106 paddress (gdbarch
, memaddr
));
108 internal_error ("unhandled target_xfer_status: %s (%s)",
109 target_xfer_status_to_string (err
),
114 /* Report a memory error by throwing a suitable exception. */
117 memory_error (enum target_xfer_status err
, CORE_ADDR memaddr
)
119 enum errors exception
= GDB_NO_ERROR
;
121 /* Build error string. */
123 = memory_error_message (err
, current_inferior ()->arch (), memaddr
);
125 /* Choose the right error to throw. */
128 case TARGET_XFER_E_IO
:
129 exception
= MEMORY_ERROR
;
131 case TARGET_XFER_UNAVAILABLE
:
132 exception
= NOT_AVAILABLE_ERROR
;
137 throw_error (exception
, ("%s"), str
.c_str ());
140 /* Helper function. */
143 read_memory_object (enum target_object object
, CORE_ADDR memaddr
,
144 gdb_byte
*myaddr
, ssize_t len
)
150 enum target_xfer_status status
;
153 status
= target_xfer_partial (current_inferior ()->top_target (), object
,
154 NULL
, myaddr
+ xfered
, NULL
,
155 memaddr
+ xfered
, len
- xfered
,
158 if (status
!= TARGET_XFER_OK
)
159 memory_error (status
== TARGET_XFER_EOF
? TARGET_XFER_E_IO
: status
,
162 xfered
+= xfered_len
;
167 /* Same as target_read_memory, but report an error if can't read. */
170 read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
172 read_memory_object (TARGET_OBJECT_MEMORY
, memaddr
, myaddr
, len
);
175 /* Same as target_read_stack, but report an error if can't read. */
178 read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
180 read_memory_object (TARGET_OBJECT_STACK_MEMORY
, memaddr
, myaddr
, len
);
183 /* Same as target_read_code, but report an error if can't read. */
186 read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
188 read_memory_object (TARGET_OBJECT_CODE_MEMORY
, memaddr
, myaddr
, len
);
191 /* Read memory at MEMADDR of length LEN and put the contents in
192 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
196 safe_read_memory_integer (CORE_ADDR memaddr
, int len
,
197 enum bfd_endian byte_order
,
198 LONGEST
*return_value
)
200 gdb_byte buf
[sizeof (LONGEST
)];
202 if (target_read_memory (memaddr
, buf
, len
))
205 *return_value
= extract_signed_integer (buf
, len
, byte_order
);
209 /* Read memory at MEMADDR of length LEN and put the contents in
210 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
214 safe_read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
215 enum bfd_endian byte_order
,
216 ULONGEST
*return_value
)
218 gdb_byte buf
[sizeof (ULONGEST
)];
220 if (target_read_memory (memaddr
, buf
, len
))
223 *return_value
= extract_unsigned_integer (buf
, len
, byte_order
);
228 read_memory_integer (CORE_ADDR memaddr
, int len
,
229 enum bfd_endian byte_order
)
231 gdb_byte buf
[sizeof (LONGEST
)];
233 read_memory (memaddr
, buf
, len
);
234 return extract_signed_integer (buf
, len
, byte_order
);
238 read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
239 enum bfd_endian byte_order
)
241 gdb_byte buf
[sizeof (ULONGEST
)];
243 read_memory (memaddr
, buf
, len
);
244 return extract_unsigned_integer (buf
, len
, byte_order
);
248 read_code_integer (CORE_ADDR memaddr
, int len
,
249 enum bfd_endian byte_order
)
251 gdb_byte buf
[sizeof (LONGEST
)];
253 read_code (memaddr
, buf
, len
);
254 return extract_signed_integer (buf
, len
, byte_order
);
258 read_code_unsigned_integer (CORE_ADDR memaddr
, int len
,
259 enum bfd_endian byte_order
)
261 gdb_byte buf
[sizeof (ULONGEST
)];
263 read_code (memaddr
, buf
, len
);
264 return extract_unsigned_integer (buf
, len
, byte_order
);
268 read_memory_typed_address (CORE_ADDR addr
, struct type
*type
)
270 gdb_byte
*buf
= (gdb_byte
*) alloca (type
->length ());
272 read_memory (addr
, buf
, type
->length ());
273 return extract_typed_address (buf
, type
);
279 write_memory (CORE_ADDR memaddr
,
280 const bfd_byte
*myaddr
, ssize_t len
)
284 status
= target_write_memory (memaddr
, myaddr
, len
);
286 memory_error (TARGET_XFER_E_IO
, memaddr
);
289 /* Notify interpreters and observers that INF's memory was changed. */
292 notify_memory_changed (inferior
*inf
, CORE_ADDR addr
, ssize_t len
,
293 const bfd_byte
*data
)
295 interps_notify_memory_changed (inf
, addr
, len
, data
);
296 gdb::observers::memory_changed
.notify (inf
, addr
, len
, data
);
299 /* Same as write_memory, but notify 'memory_changed' observers. */
302 write_memory_with_notification (CORE_ADDR memaddr
, const bfd_byte
*myaddr
,
305 write_memory (memaddr
, myaddr
, len
);
306 notify_memory_changed (current_inferior (), memaddr
, len
, myaddr
);
309 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
312 write_memory_unsigned_integer (CORE_ADDR addr
, int len
,
313 enum bfd_endian byte_order
,
316 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
318 store_unsigned_integer (buf
, len
, byte_order
, value
);
319 write_memory (addr
, buf
, len
);
322 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
325 write_memory_signed_integer (CORE_ADDR addr
, int len
,
326 enum bfd_endian byte_order
,
329 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
331 store_signed_integer (buf
, len
, byte_order
, value
);
332 write_memory (addr
, buf
, len
);
335 /* The current default bfd target. Points to storage allocated for
337 const char *gnutarget
;
339 /* Same thing, except it is "auto" not NULL for the default case. */
340 static std::string gnutarget_string
;
342 show_gnutarget_string (struct ui_file
*file
, int from_tty
,
343 struct cmd_list_element
*c
,
347 _("The current BFD target is \"%s\".\n"), value
);
351 set_gnutarget_command (const char *ignore
, int from_tty
,
352 struct cmd_list_element
*c
)
354 const char *gend
= gnutarget_string
.c_str () + gnutarget_string
.size ();
355 gend
= remove_trailing_whitespace (gnutarget_string
.c_str (), gend
);
357 = gnutarget_string
.substr (0, gend
- gnutarget_string
.data ());
359 if (gnutarget_string
== "auto")
362 gnutarget
= gnutarget_string
.c_str ();
365 /* A completion function for "set gnutarget". */
368 complete_set_gnutarget (struct cmd_list_element
*cmd
,
369 completion_tracker
&tracker
,
370 const char *text
, const char *word
)
372 static const char **bfd_targets
;
374 if (bfd_targets
== NULL
)
378 bfd_targets
= bfd_target_list ();
379 for (last
= 0; bfd_targets
[last
] != NULL
; ++last
)
382 bfd_targets
= XRESIZEVEC (const char *, bfd_targets
, last
+ 2);
383 bfd_targets
[last
] = "auto";
384 bfd_targets
[last
+ 1] = NULL
;
387 complete_on_enum (tracker
, bfd_targets
, text
, word
);
390 /* Set the gnutarget. */
392 set_gnutarget (const char *newtarget
)
394 gnutarget_string
= newtarget
;
395 set_gnutarget_command (NULL
, 0, NULL
);
398 void _initialize_core ();
402 cmd_list_element
*core_file_cmd
403 = add_cmd ("core-file", class_files
, core_file_command
, _("\
404 Use FILE as core dump for examining memory and registers.\n\
405 Usage: core-file FILE\n\
406 No arg means have no core file. This command has been superseded by the\n\
407 `target core' and `detach' commands."), &cmdlist
);
408 set_cmd_completer (core_file_cmd
, filename_completer
);
411 set_show_commands set_show_gnutarget
412 = add_setshow_string_noescape_cmd ("gnutarget", class_files
,
413 &gnutarget_string
, _("\
414 Set the current BFD target."), _("\
415 Show the current BFD target."), _("\
416 Use `set gnutarget auto' to specify automatic detection."),
417 set_gnutarget_command
,
418 show_gnutarget_string
,
419 &setlist
, &showlist
);
420 set_cmd_completer (set_show_gnutarget
.set
, complete_set_gnutarget
);
422 add_alias_cmd ("g", set_show_gnutarget
.set
, class_files
, 1, &setlist
);
424 if (getenv ("GNUTARGET"))
425 set_gnutarget (getenv ("GNUTARGET"));
427 set_gnutarget ("auto");