Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / corefile.c
blob169d4229e9c13dc40122da56115d837601adefb1
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 "defs.h"
21 #include <signal.h>
22 #include <fcntl.h>
23 #include "inferior.h"
24 #include "symtab.h"
25 #include "command.h"
26 #include "gdbcmd.h"
27 #include "bfd.h"
28 #include "target.h"
29 #include "gdbcore.h"
30 #include "dis-asm.h"
31 #include <sys/stat.h>
32 #include "completer.h"
33 #include "observable.h"
34 #include "cli/cli-utils.h"
35 #include "gdbarch.h"
36 #include "interps.h"
38 void
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)
45 return;
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. */
52 struct stat st;
53 int res = bfd_stat (exec_bfd, &st);
55 if (res == 0
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. */
64 void
65 validate_files (void)
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. */
80 const char *
81 get_exec_file (int err)
83 if (current_program_space->exec_filename != nullptr)
84 return current_program_space->exec_filename.get ();
85 if (!err)
86 return NULL;
88 error (_("No executable file specified.\n\
89 Use the \"file\" or \"exec-file\" command."));
93 std::string
94 memory_error_message (enum target_xfer_status err,
95 struct gdbarch *gdbarch, CORE_ADDR memaddr)
97 switch (err)
99 case TARGET_XFER_E_IO:
100 /* Actually, address between memaddr and memaddr + len was out of
101 bounds. */
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));
107 default:
108 internal_error ("unhandled target_xfer_status: %s (%s)",
109 target_xfer_status_to_string (err),
110 plongest (err));
114 /* Report a memory error by throwing a suitable exception. */
116 void
117 memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
119 enum errors exception = GDB_NO_ERROR;
121 /* Build error string. */
122 std::string str
123 = memory_error_message (err, current_inferior ()->arch (), memaddr);
125 /* Choose the right error to throw. */
126 switch (err)
128 case TARGET_XFER_E_IO:
129 exception = MEMORY_ERROR;
130 break;
131 case TARGET_XFER_UNAVAILABLE:
132 exception = NOT_AVAILABLE_ERROR;
133 break;
136 /* Throw it. */
137 throw_error (exception, ("%s"), str.c_str ());
140 /* Helper function. */
142 static void
143 read_memory_object (enum target_object object, CORE_ADDR memaddr,
144 gdb_byte *myaddr, ssize_t len)
146 ULONGEST xfered = 0;
148 while (xfered < len)
150 enum target_xfer_status status;
151 ULONGEST xfered_len;
153 status = target_xfer_partial (current_inferior ()->top_target (), object,
154 NULL, myaddr + xfered, NULL,
155 memaddr + xfered, len - xfered,
156 &xfered_len);
158 if (status != TARGET_XFER_OK)
159 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
160 memaddr + xfered);
162 xfered += xfered_len;
163 QUIT;
167 /* Same as target_read_memory, but report an error if can't read. */
169 void
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. */
177 void
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. */
185 void
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
193 if successful. */
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))
203 return 0;
205 *return_value = extract_signed_integer (buf, len, byte_order);
206 return 1;
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
211 if successful. */
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))
221 return 0;
223 *return_value = extract_unsigned_integer (buf, len, byte_order);
224 return 1;
227 LONGEST
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);
237 ULONGEST
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);
247 LONGEST
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);
257 ULONGEST
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);
267 CORE_ADDR
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);
276 /* See gdbcore.h. */
278 void
279 write_memory (CORE_ADDR memaddr,
280 const bfd_byte *myaddr, ssize_t len)
282 int status;
284 status = target_write_memory (memaddr, myaddr, len);
285 if (status != 0)
286 memory_error (TARGET_XFER_E_IO, memaddr);
289 /* Notify interpreters and observers that INF's memory was changed. */
291 static void
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. */
301 void
302 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
303 ssize_t len)
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
310 integer. */
311 void
312 write_memory_unsigned_integer (CORE_ADDR addr, int len,
313 enum bfd_endian byte_order,
314 ULONGEST value)
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
323 integer. */
324 void
325 write_memory_signed_integer (CORE_ADDR addr, int len,
326 enum bfd_endian byte_order,
327 LONGEST value)
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
336 gnutarget_string. */
337 const char *gnutarget;
339 /* Same thing, except it is "auto" not NULL for the default case. */
340 static std::string gnutarget_string;
341 static void
342 show_gnutarget_string (struct ui_file *file, int from_tty,
343 struct cmd_list_element *c,
344 const char *value)
346 gdb_printf (file,
347 _("The current BFD target is \"%s\".\n"), value);
350 static void
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);
356 gnutarget_string
357 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
359 if (gnutarget_string == "auto")
360 gnutarget = NULL;
361 else
362 gnutarget = gnutarget_string.c_str ();
365 /* A completion function for "set gnutarget". */
367 static void
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)
376 int last;
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. */
391 void
392 set_gnutarget (const char *newtarget)
394 gnutarget_string = newtarget;
395 set_gnutarget_command (NULL, 0, NULL);
398 void _initialize_core ();
399 void
400 _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"));
426 else
427 set_gnutarget ("auto");