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"
38 #include "arch-utils.h"
41 reopen_exec_file (void)
43 bfd
*exec_bfd
= current_program_space
->exec_bfd ();
45 /* Don't do anything if there isn't an exec file. */
46 if (exec_bfd
== nullptr)
49 /* The main executable can't be an in-memory BFD object. If it was then
50 the use of bfd_stat below would not work as expected. */
51 gdb_assert ((exec_bfd
->flags
& BFD_IN_MEMORY
) == 0);
53 /* If the timestamp of the exec file has changed, reopen it. */
55 int res
= gdb_bfd_stat (exec_bfd
, &st
);
58 && current_program_space
->ebfd_mtime
!= 0
59 && current_program_space
->ebfd_mtime
!= st
.st_mtime
)
60 exec_file_attach (bfd_get_filename (exec_bfd
), 0);
63 /* If we have both a core file and an exec file,
64 print a warning if they don't go together. */
69 if (current_program_space
->exec_bfd () && current_program_space
->core_bfd ())
71 if (!core_file_matches_executable_p (current_program_space
->core_bfd (),
72 current_program_space
->exec_bfd ()))
73 warning (_("core file may not match specified executable file."));
74 else if (gdb_bfd_get_mtime (current_program_space
->exec_bfd ())
75 > gdb_bfd_get_mtime (current_program_space
->core_bfd ()))
76 warning (_("exec file is newer than core file."));
80 /* See arch-utils.h. */
82 core_file_exec_context
83 default_core_parse_exec_context (struct gdbarch
*gdbarch
, bfd
*cbfd
)
90 memory_error_message (enum target_xfer_status err
,
91 struct gdbarch
*gdbarch
, CORE_ADDR memaddr
)
95 case TARGET_XFER_E_IO
:
96 /* Actually, address between memaddr and memaddr + len was out of
98 return string_printf (_("Cannot access memory at address %s"),
99 paddress (gdbarch
, memaddr
));
100 case TARGET_XFER_UNAVAILABLE
:
101 return string_printf (_("Memory at address %s unavailable."),
102 paddress (gdbarch
, memaddr
));
104 internal_error ("unhandled target_xfer_status: %s (%s)",
105 target_xfer_status_to_string (err
),
110 /* Report a memory error by throwing a suitable exception. */
113 memory_error (enum target_xfer_status err
, CORE_ADDR memaddr
)
115 enum errors exception
= GDB_NO_ERROR
;
117 /* Build error string. */
119 = memory_error_message (err
, current_inferior ()->arch (), memaddr
);
121 /* Choose the right error to throw. */
124 case TARGET_XFER_E_IO
:
125 exception
= MEMORY_ERROR
;
127 case TARGET_XFER_UNAVAILABLE
:
128 exception
= NOT_AVAILABLE_ERROR
;
133 throw_error (exception
, ("%s"), str
.c_str ());
136 /* Helper function. */
139 read_memory_object (enum target_object object
, CORE_ADDR memaddr
,
140 gdb_byte
*myaddr
, ssize_t len
)
146 enum target_xfer_status status
;
149 status
= target_xfer_partial (current_inferior ()->top_target (), object
,
150 NULL
, myaddr
+ xfered
, NULL
,
151 memaddr
+ xfered
, len
- xfered
,
154 if (status
!= TARGET_XFER_OK
)
155 memory_error (status
== TARGET_XFER_EOF
? TARGET_XFER_E_IO
: status
,
158 xfered
+= xfered_len
;
163 /* Same as target_read_memory, but report an error if can't read. */
166 read_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
168 read_memory_object (TARGET_OBJECT_MEMORY
, memaddr
, myaddr
, len
);
171 /* Same as target_read_stack, but report an error if can't read. */
174 read_stack (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
176 read_memory_object (TARGET_OBJECT_STACK_MEMORY
, memaddr
, myaddr
, len
);
179 /* Same as target_read_code, but report an error if can't read. */
182 read_code (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ssize_t len
)
184 read_memory_object (TARGET_OBJECT_CODE_MEMORY
, memaddr
, myaddr
, len
);
187 /* Read memory at MEMADDR of length LEN and put the contents in
188 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
192 safe_read_memory_integer (CORE_ADDR memaddr
, int len
,
193 enum bfd_endian byte_order
,
194 LONGEST
*return_value
)
196 gdb_byte buf
[sizeof (LONGEST
)];
198 if (target_read_memory (memaddr
, buf
, len
))
201 *return_value
= extract_signed_integer (buf
, len
, byte_order
);
205 /* Read memory at MEMADDR of length LEN and put the contents in
206 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
210 safe_read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
211 enum bfd_endian byte_order
,
212 ULONGEST
*return_value
)
214 gdb_byte buf
[sizeof (ULONGEST
)];
216 if (target_read_memory (memaddr
, buf
, len
))
219 *return_value
= extract_unsigned_integer (buf
, len
, byte_order
);
224 read_memory_integer (CORE_ADDR memaddr
, int len
,
225 enum bfd_endian byte_order
)
227 gdb_byte buf
[sizeof (LONGEST
)];
229 read_memory (memaddr
, buf
, len
);
230 return extract_signed_integer (buf
, len
, byte_order
);
234 read_memory_unsigned_integer (CORE_ADDR memaddr
, int len
,
235 enum bfd_endian byte_order
)
237 gdb_byte buf
[sizeof (ULONGEST
)];
239 read_memory (memaddr
, buf
, len
);
240 return extract_unsigned_integer (buf
, len
, byte_order
);
244 read_code_integer (CORE_ADDR memaddr
, int len
,
245 enum bfd_endian byte_order
)
247 gdb_byte buf
[sizeof (LONGEST
)];
249 read_code (memaddr
, buf
, len
);
250 return extract_signed_integer (buf
, len
, byte_order
);
254 read_code_unsigned_integer (CORE_ADDR memaddr
, int len
,
255 enum bfd_endian byte_order
)
257 gdb_byte buf
[sizeof (ULONGEST
)];
259 read_code (memaddr
, buf
, len
);
260 return extract_unsigned_integer (buf
, len
, byte_order
);
264 read_memory_typed_address (CORE_ADDR addr
, struct type
*type
)
266 gdb_byte
*buf
= (gdb_byte
*) alloca (type
->length ());
268 read_memory (addr
, buf
, type
->length ());
269 return extract_typed_address (buf
, type
);
275 write_memory (CORE_ADDR memaddr
,
276 const bfd_byte
*myaddr
, ssize_t len
)
280 status
= target_write_memory (memaddr
, myaddr
, len
);
282 memory_error (TARGET_XFER_E_IO
, memaddr
);
285 /* Notify interpreters and observers that INF's memory was changed. */
288 notify_memory_changed (inferior
*inf
, CORE_ADDR addr
, ssize_t len
,
289 const bfd_byte
*data
)
291 interps_notify_memory_changed (inf
, addr
, len
, data
);
292 gdb::observers::memory_changed
.notify (inf
, addr
, len
, data
);
295 /* Same as write_memory, but notify 'memory_changed' observers. */
298 write_memory_with_notification (CORE_ADDR memaddr
, const bfd_byte
*myaddr
,
301 write_memory (memaddr
, myaddr
, len
);
302 notify_memory_changed (current_inferior (), memaddr
, len
, myaddr
);
305 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
308 write_memory_unsigned_integer (CORE_ADDR addr
, int len
,
309 enum bfd_endian byte_order
,
312 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
314 store_unsigned_integer (buf
, len
, byte_order
, value
);
315 write_memory (addr
, buf
, len
);
318 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
321 write_memory_signed_integer (CORE_ADDR addr
, int len
,
322 enum bfd_endian byte_order
,
325 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
327 store_signed_integer (buf
, len
, byte_order
, value
);
328 write_memory (addr
, buf
, len
);
331 /* The current default bfd target. Points to storage allocated for
333 const char *gnutarget
;
335 /* Same thing, except it is "auto" not NULL for the default case. */
336 static std::string gnutarget_string
;
338 show_gnutarget_string (struct ui_file
*file
, int from_tty
,
339 struct cmd_list_element
*c
,
343 _("The current BFD target is \"%s\".\n"), value
);
347 set_gnutarget_command (const char *ignore
, int from_tty
,
348 struct cmd_list_element
*c
)
350 const char *gend
= gnutarget_string
.c_str () + gnutarget_string
.size ();
351 gend
= remove_trailing_whitespace (gnutarget_string
.c_str (), gend
);
353 = gnutarget_string
.substr (0, gend
- gnutarget_string
.data ());
355 if (gnutarget_string
== "auto")
358 gnutarget
= gnutarget_string
.c_str ();
361 /* A completion function for "set gnutarget". */
364 complete_set_gnutarget (struct cmd_list_element
*cmd
,
365 completion_tracker
&tracker
,
366 const char *text
, const char *word
)
368 static const char **bfd_targets
;
370 if (bfd_targets
== NULL
)
374 bfd_targets
= bfd_target_list ();
375 for (last
= 0; bfd_targets
[last
] != NULL
; ++last
)
378 bfd_targets
= XRESIZEVEC (const char *, bfd_targets
, last
+ 2);
379 bfd_targets
[last
] = "auto";
380 bfd_targets
[last
+ 1] = NULL
;
383 complete_on_enum (tracker
, bfd_targets
, text
, word
);
386 /* Set the gnutarget. */
388 set_gnutarget (const char *newtarget
)
390 gnutarget_string
= newtarget
;
391 set_gnutarget_command (NULL
, 0, NULL
);
394 void _initialize_core ();
398 cmd_list_element
*core_file_cmd
399 = add_cmd ("core-file", class_files
, core_file_command
, _("\
400 Use FILE as core dump for examining memory and registers.\n\
401 Usage: core-file FILE\n\
402 No arg means have no core file. This command has been superseded by the\n\
403 `target core' and `detach' commands."), &cmdlist
);
404 set_cmd_completer (core_file_cmd
, deprecated_filename_completer
);
406 set_show_commands set_show_gnutarget
407 = add_setshow_string_noescape_cmd ("gnutarget", class_files
,
408 &gnutarget_string
, _("\
409 Set the current BFD target."), _("\
410 Show the current BFD target."), _("\
411 Use `set gnutarget auto' to specify automatic detection."),
412 set_gnutarget_command
,
413 show_gnutarget_string
,
414 &setlist
, &showlist
);
415 set_cmd_completer (set_show_gnutarget
.set
, complete_set_gnutarget
);
417 add_alias_cmd ("g", set_show_gnutarget
.set
, class_files
, 1, &setlist
);
419 if (getenv ("GNUTARGET"))
420 set_gnutarget (getenv ("GNUTARGET"));
422 set_gnutarget ("auto");