Automatic date update in version.in
[binutils-gdb.git] / gdb / corefile.c
blob16cd60f7106dd27ae0148ef32ca24a52853179bf
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/>. */
20 #include <signal.h>
21 #include <fcntl.h>
22 #include "inferior.h"
23 #include "symtab.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "bfd.h"
27 #include "target.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"
30 #include <sys/stat.h>
31 #include "completer.h"
32 #include "observable.h"
33 #include "cli/cli-utils.h"
34 #include "gdbarch.h"
35 #include "interps.h"
37 void
38 reopen_exec_file (void)
40 bfd *exec_bfd = current_program_space->exec_bfd ();
42 /* Don't do anything if there isn't an exec file. */
43 if (exec_bfd == nullptr)
44 return;
46 /* The main executable can't be an in-memory BFD object. If it was then
47 the use of bfd_stat below would not work as expected. */
48 gdb_assert ((exec_bfd->flags & BFD_IN_MEMORY) == 0);
50 /* If the timestamp of the exec file has changed, reopen it. */
51 struct stat st;
52 int res = bfd_stat (exec_bfd, &st);
54 if (res == 0
55 && current_program_space->ebfd_mtime != 0
56 && current_program_space->ebfd_mtime != st.st_mtime)
57 exec_file_attach (bfd_get_filename (exec_bfd), 0);
60 /* If we have both a core file and an exec file,
61 print a warning if they don't go together. */
63 void
64 validate_files (void)
66 if (current_program_space->exec_bfd () && current_program_space->core_bfd ())
68 if (!core_file_matches_executable_p (current_program_space->core_bfd (),
69 current_program_space->exec_bfd ()))
70 warning (_("core file may not match specified executable file."));
71 else if (bfd_get_mtime (current_program_space->exec_bfd ())
72 > bfd_get_mtime (current_program_space->core_bfd ()))
73 warning (_("exec file is newer than core file."));
77 /* See gdbsupport/common-inferior.h. */
79 const char *
80 get_exec_file (int err)
82 if (current_program_space->exec_filename != nullptr)
83 return current_program_space->exec_filename.get ();
84 if (!err)
85 return NULL;
87 error (_("No executable file specified.\n\
88 Use the \"file\" or \"exec-file\" command."));
92 std::string
93 memory_error_message (enum target_xfer_status err,
94 struct gdbarch *gdbarch, CORE_ADDR memaddr)
96 switch (err)
98 case TARGET_XFER_E_IO:
99 /* Actually, address between memaddr and memaddr + len was out of
100 bounds. */
101 return string_printf (_("Cannot access memory at address %s"),
102 paddress (gdbarch, memaddr));
103 case TARGET_XFER_UNAVAILABLE:
104 return string_printf (_("Memory at address %s unavailable."),
105 paddress (gdbarch, memaddr));
106 default:
107 internal_error ("unhandled target_xfer_status: %s (%s)",
108 target_xfer_status_to_string (err),
109 plongest (err));
113 /* Report a memory error by throwing a suitable exception. */
115 void
116 memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
118 enum errors exception = GDB_NO_ERROR;
120 /* Build error string. */
121 std::string str
122 = memory_error_message (err, current_inferior ()->arch (), memaddr);
124 /* Choose the right error to throw. */
125 switch (err)
127 case TARGET_XFER_E_IO:
128 exception = MEMORY_ERROR;
129 break;
130 case TARGET_XFER_UNAVAILABLE:
131 exception = NOT_AVAILABLE_ERROR;
132 break;
135 /* Throw it. */
136 throw_error (exception, ("%s"), str.c_str ());
139 /* Helper function. */
141 static void
142 read_memory_object (enum target_object object, CORE_ADDR memaddr,
143 gdb_byte *myaddr, ssize_t len)
145 ULONGEST xfered = 0;
147 while (xfered < len)
149 enum target_xfer_status status;
150 ULONGEST xfered_len;
152 status = target_xfer_partial (current_inferior ()->top_target (), object,
153 NULL, myaddr + xfered, NULL,
154 memaddr + xfered, len - xfered,
155 &xfered_len);
157 if (status != TARGET_XFER_OK)
158 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
159 memaddr + xfered);
161 xfered += xfered_len;
162 QUIT;
166 /* Same as target_read_memory, but report an error if can't read. */
168 void
169 read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
171 read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
174 /* Same as target_read_stack, but report an error if can't read. */
176 void
177 read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
179 read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
182 /* Same as target_read_code, but report an error if can't read. */
184 void
185 read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
187 read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
190 /* Read memory at MEMADDR of length LEN and put the contents in
191 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
192 if successful. */
195 safe_read_memory_integer (CORE_ADDR memaddr, int len,
196 enum bfd_endian byte_order,
197 LONGEST *return_value)
199 gdb_byte buf[sizeof (LONGEST)];
201 if (target_read_memory (memaddr, buf, len))
202 return 0;
204 *return_value = extract_signed_integer (buf, len, byte_order);
205 return 1;
208 /* Read memory at MEMADDR of length LEN and put the contents in
209 RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero
210 if successful. */
213 safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
214 enum bfd_endian byte_order,
215 ULONGEST *return_value)
217 gdb_byte buf[sizeof (ULONGEST)];
219 if (target_read_memory (memaddr, buf, len))
220 return 0;
222 *return_value = extract_unsigned_integer (buf, len, byte_order);
223 return 1;
226 LONGEST
227 read_memory_integer (CORE_ADDR memaddr, int len,
228 enum bfd_endian byte_order)
230 gdb_byte buf[sizeof (LONGEST)];
232 read_memory (memaddr, buf, len);
233 return extract_signed_integer (buf, len, byte_order);
236 ULONGEST
237 read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
238 enum bfd_endian byte_order)
240 gdb_byte buf[sizeof (ULONGEST)];
242 read_memory (memaddr, buf, len);
243 return extract_unsigned_integer (buf, len, byte_order);
246 LONGEST
247 read_code_integer (CORE_ADDR memaddr, int len,
248 enum bfd_endian byte_order)
250 gdb_byte buf[sizeof (LONGEST)];
252 read_code (memaddr, buf, len);
253 return extract_signed_integer (buf, len, byte_order);
256 ULONGEST
257 read_code_unsigned_integer (CORE_ADDR memaddr, int len,
258 enum bfd_endian byte_order)
260 gdb_byte buf[sizeof (ULONGEST)];
262 read_code (memaddr, buf, len);
263 return extract_unsigned_integer (buf, len, byte_order);
266 CORE_ADDR
267 read_memory_typed_address (CORE_ADDR addr, struct type *type)
269 gdb_byte *buf = (gdb_byte *) alloca (type->length ());
271 read_memory (addr, buf, type->length ());
272 return extract_typed_address (buf, type);
275 /* See gdbcore.h. */
277 void
278 write_memory (CORE_ADDR memaddr,
279 const bfd_byte *myaddr, ssize_t len)
281 int status;
283 status = target_write_memory (memaddr, myaddr, len);
284 if (status != 0)
285 memory_error (TARGET_XFER_E_IO, memaddr);
288 /* Notify interpreters and observers that INF's memory was changed. */
290 static void
291 notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
292 const bfd_byte *data)
294 interps_notify_memory_changed (inf, addr, len, data);
295 gdb::observers::memory_changed.notify (inf, addr, len, data);
298 /* Same as write_memory, but notify 'memory_changed' observers. */
300 void
301 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
302 ssize_t len)
304 write_memory (memaddr, myaddr, len);
305 notify_memory_changed (current_inferior (), memaddr, len, myaddr);
308 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
309 integer. */
310 void
311 write_memory_unsigned_integer (CORE_ADDR addr, int len,
312 enum bfd_endian byte_order,
313 ULONGEST value)
315 gdb_byte *buf = (gdb_byte *) alloca (len);
317 store_unsigned_integer (buf, len, byte_order, value);
318 write_memory (addr, buf, len);
321 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
322 integer. */
323 void
324 write_memory_signed_integer (CORE_ADDR addr, int len,
325 enum bfd_endian byte_order,
326 LONGEST value)
328 gdb_byte *buf = (gdb_byte *) alloca (len);
330 store_signed_integer (buf, len, byte_order, value);
331 write_memory (addr, buf, len);
334 /* The current default bfd target. Points to storage allocated for
335 gnutarget_string. */
336 const char *gnutarget;
338 /* Same thing, except it is "auto" not NULL for the default case. */
339 static std::string gnutarget_string;
340 static void
341 show_gnutarget_string (struct ui_file *file, int from_tty,
342 struct cmd_list_element *c,
343 const char *value)
345 gdb_printf (file,
346 _("The current BFD target is \"%s\".\n"), value);
349 static void
350 set_gnutarget_command (const char *ignore, int from_tty,
351 struct cmd_list_element *c)
353 const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
354 gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
355 gnutarget_string
356 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
358 if (gnutarget_string == "auto")
359 gnutarget = NULL;
360 else
361 gnutarget = gnutarget_string.c_str ();
364 /* A completion function for "set gnutarget". */
366 static void
367 complete_set_gnutarget (struct cmd_list_element *cmd,
368 completion_tracker &tracker,
369 const char *text, const char *word)
371 static const char **bfd_targets;
373 if (bfd_targets == NULL)
375 int last;
377 bfd_targets = bfd_target_list ();
378 for (last = 0; bfd_targets[last] != NULL; ++last)
381 bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
382 bfd_targets[last] = "auto";
383 bfd_targets[last + 1] = NULL;
386 complete_on_enum (tracker, bfd_targets, text, word);
389 /* Set the gnutarget. */
390 void
391 set_gnutarget (const char *newtarget)
393 gnutarget_string = newtarget;
394 set_gnutarget_command (NULL, 0, NULL);
397 void _initialize_core ();
398 void
399 _initialize_core ()
401 cmd_list_element *core_file_cmd
402 = add_cmd ("core-file", class_files, core_file_command, _("\
403 Use FILE as core dump for examining memory and registers.\n\
404 Usage: core-file FILE\n\
405 No arg means have no core file. This command has been superseded by the\n\
406 `target core' and `detach' commands."), &cmdlist);
407 set_cmd_completer (core_file_cmd, filename_completer);
410 set_show_commands set_show_gnutarget
411 = add_setshow_string_noescape_cmd ("gnutarget", class_files,
412 &gnutarget_string, _("\
413 Set the current BFD target."), _("\
414 Show the current BFD target."), _("\
415 Use `set gnutarget auto' to specify automatic detection."),
416 set_gnutarget_command,
417 show_gnutarget_string,
418 &setlist, &showlist);
419 set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
421 add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
423 if (getenv ("GNUTARGET"))
424 set_gnutarget (getenv ("GNUTARGET"));
425 else
426 set_gnutarget ("auto");