More updated translations
[binutils-gdb.git] / gdb / corefile.c
blob7fe5e83ebf8e0dd13463a986d06518b33eaba607
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 "event-top.h"
23 #include "extract-store-integer.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "command.h"
27 #include "cli/cli-cmds.h"
28 #include "bfd.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "dis-asm.h"
32 #include <sys/stat.h>
33 #include "completer.h"
34 #include "observable.h"
35 #include "cli/cli-utils.h"
36 #include "gdbarch.h"
37 #include "interps.h"
38 #include "arch-utils.h"
40 void
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)
47 return;
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. */
54 struct stat st;
55 int res = gdb_bfd_stat (exec_bfd, &st);
57 if (res == 0
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. */
66 void
67 validate_files (void)
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)
85 return {};
89 std::string
90 memory_error_message (enum target_xfer_status err,
91 struct gdbarch *gdbarch, CORE_ADDR memaddr)
93 switch (err)
95 case TARGET_XFER_E_IO:
96 /* Actually, address between memaddr and memaddr + len was out of
97 bounds. */
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));
103 default:
104 internal_error ("unhandled target_xfer_status: %s (%s)",
105 target_xfer_status_to_string (err),
106 plongest (err));
110 /* Report a memory error by throwing a suitable exception. */
112 void
113 memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
115 enum errors exception = GDB_NO_ERROR;
117 /* Build error string. */
118 std::string str
119 = memory_error_message (err, current_inferior ()->arch (), memaddr);
121 /* Choose the right error to throw. */
122 switch (err)
124 case TARGET_XFER_E_IO:
125 exception = MEMORY_ERROR;
126 break;
127 case TARGET_XFER_UNAVAILABLE:
128 exception = NOT_AVAILABLE_ERROR;
129 break;
132 /* Throw it. */
133 throw_error (exception, ("%s"), str.c_str ());
136 /* Helper function. */
138 static void
139 read_memory_object (enum target_object object, CORE_ADDR memaddr,
140 gdb_byte *myaddr, ssize_t len)
142 ULONGEST xfered = 0;
144 while (xfered < len)
146 enum target_xfer_status status;
147 ULONGEST xfered_len;
149 status = target_xfer_partial (current_inferior ()->top_target (), object,
150 NULL, myaddr + xfered, NULL,
151 memaddr + xfered, len - xfered,
152 &xfered_len);
154 if (status != TARGET_XFER_OK)
155 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
156 memaddr + xfered);
158 xfered += xfered_len;
159 QUIT;
163 /* Same as target_read_memory, but report an error if can't read. */
165 void
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. */
173 void
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. */
181 void
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
189 if successful. */
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))
199 return 0;
201 *return_value = extract_signed_integer (buf, len, byte_order);
202 return 1;
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
207 if successful. */
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))
217 return 0;
219 *return_value = extract_unsigned_integer (buf, len, byte_order);
220 return 1;
223 LONGEST
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);
233 ULONGEST
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);
243 LONGEST
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);
253 ULONGEST
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);
263 CORE_ADDR
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);
272 /* See gdbcore.h. */
274 void
275 write_memory (CORE_ADDR memaddr,
276 const bfd_byte *myaddr, ssize_t len)
278 int status;
280 status = target_write_memory (memaddr, myaddr, len);
281 if (status != 0)
282 memory_error (TARGET_XFER_E_IO, memaddr);
285 /* Notify interpreters and observers that INF's memory was changed. */
287 static void
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. */
297 void
298 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
299 ssize_t len)
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
306 integer. */
307 void
308 write_memory_unsigned_integer (CORE_ADDR addr, int len,
309 enum bfd_endian byte_order,
310 ULONGEST value)
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
319 integer. */
320 void
321 write_memory_signed_integer (CORE_ADDR addr, int len,
322 enum bfd_endian byte_order,
323 LONGEST value)
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
332 gnutarget_string. */
333 const char *gnutarget;
335 /* Same thing, except it is "auto" not NULL for the default case. */
336 static std::string gnutarget_string;
337 static void
338 show_gnutarget_string (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
342 gdb_printf (file,
343 _("The current BFD target is \"%s\".\n"), value);
346 static void
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);
352 gnutarget_string
353 = gnutarget_string.substr (0, gend - gnutarget_string.data ());
355 if (gnutarget_string == "auto")
356 gnutarget = NULL;
357 else
358 gnutarget = gnutarget_string.c_str ();
361 /* A completion function for "set gnutarget". */
363 static void
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)
372 int last;
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. */
387 void
388 set_gnutarget (const char *newtarget)
390 gnutarget_string = newtarget;
391 set_gnutarget_command (NULL, 0, NULL);
394 void _initialize_core ();
395 void
396 _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"));
421 else
422 set_gnutarget ("auto");