]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/corefile.c
a2c75c02c2f0440d937a77a09a1a1079f40567f9
[thirdparty/binutils-gdb.git] / gdb / corefile.c
1 /* Core dump and executable file functions above target vector, for GDB.
2
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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"
39
40 void
41 reopen_exec_file (void)
42 {
43 bfd *exec_bfd = current_program_space->exec_bfd ();
44
45 /* Don't do anything if there isn't an exec file. */
46 if (exec_bfd == nullptr)
47 return;
48
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);
52
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);
56
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);
61 }
62 \f
63 /* If we have both a core file and an exec file,
64 print a warning if they don't go together. */
65
66 void
67 validate_files (void)
68 {
69 if (current_program_space->exec_bfd () && current_program_space->core_bfd ())
70 {
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."));
77 }
78 }
79
80 /* See arch-utils.h. */
81
82 core_file_exec_context
83 default_core_parse_exec_context (struct gdbarch *gdbarch, bfd *cbfd)
84 {
85 return {};
86 }
87 \f
88
89 std::string
90 memory_error_message (enum target_xfer_status err,
91 struct gdbarch *gdbarch, CORE_ADDR memaddr)
92 {
93 switch (err)
94 {
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));
107 }
108 }
109
110 /* Report a memory error by throwing a suitable exception. */
111
112 void
113 memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
114 {
115 enum errors exception = GDB_NO_ERROR;
116
117 /* Build error string. */
118 std::string str
119 = memory_error_message (err, current_inferior ()->arch (), memaddr);
120
121 /* Choose the right error to throw. */
122 switch (err)
123 {
124 case TARGET_XFER_E_IO:
125 exception = MEMORY_ERROR;
126 break;
127 case TARGET_XFER_UNAVAILABLE:
128 exception = NOT_AVAILABLE_ERROR;
129 break;
130 }
131
132 /* Throw it. */
133 throw_error (exception, ("%s"), str.c_str ());
134 }
135
136 /* Helper function. */
137
138 static void
139 read_memory_object (enum target_object object, CORE_ADDR memaddr,
140 gdb_byte *myaddr, ssize_t len)
141 {
142 ULONGEST xfered = 0;
143
144 while (xfered < len)
145 {
146 enum target_xfer_status status;
147 ULONGEST xfered_len;
148
149 status = target_xfer_partial (current_inferior ()->top_target (), object,
150 NULL, myaddr + xfered, NULL,
151 memaddr + xfered, len - xfered,
152 &xfered_len);
153
154 if (status != TARGET_XFER_OK)
155 memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
156 memaddr + xfered);
157
158 xfered += xfered_len;
159 QUIT;
160 }
161 }
162
163 /* Same as target_read_memory, but report an error if can't read. */
164
165 void
166 read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
167 {
168 read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
169 }
170
171 /* Same as target_read_stack, but report an error if can't read. */
172
173 void
174 read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
175 {
176 read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
177 }
178
179 /* Same as target_read_code, but report an error if can't read. */
180
181 void
182 read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
183 {
184 read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
185 }
186
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. */
190
191 int
192 safe_read_memory_integer (CORE_ADDR memaddr, int len,
193 enum bfd_endian byte_order,
194 LONGEST *return_value)
195 {
196 gdb_byte buf[sizeof (LONGEST)];
197
198 if (target_read_memory (memaddr, buf, len))
199 return 0;
200
201 *return_value = extract_signed_integer (buf, len, byte_order);
202 return 1;
203 }
204
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. */
208
209 int
210 safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
211 enum bfd_endian byte_order,
212 ULONGEST *return_value)
213 {
214 gdb_byte buf[sizeof (ULONGEST)];
215
216 if (target_read_memory (memaddr, buf, len))
217 return 0;
218
219 *return_value = extract_unsigned_integer (buf, len, byte_order);
220 return 1;
221 }
222
223 LONGEST
224 read_memory_integer (CORE_ADDR memaddr, int len,
225 enum bfd_endian byte_order)
226 {
227 gdb_byte buf[sizeof (LONGEST)];
228
229 read_memory (memaddr, buf, len);
230 return extract_signed_integer (buf, len, byte_order);
231 }
232
233 ULONGEST
234 read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
235 enum bfd_endian byte_order)
236 {
237 gdb_byte buf[sizeof (ULONGEST)];
238
239 read_memory (memaddr, buf, len);
240 return extract_unsigned_integer (buf, len, byte_order);
241 }
242
243 LONGEST
244 read_code_integer (CORE_ADDR memaddr, int len,
245 enum bfd_endian byte_order)
246 {
247 gdb_byte buf[sizeof (LONGEST)];
248
249 read_code (memaddr, buf, len);
250 return extract_signed_integer (buf, len, byte_order);
251 }
252
253 ULONGEST
254 read_code_unsigned_integer (CORE_ADDR memaddr, int len,
255 enum bfd_endian byte_order)
256 {
257 gdb_byte buf[sizeof (ULONGEST)];
258
259 read_code (memaddr, buf, len);
260 return extract_unsigned_integer (buf, len, byte_order);
261 }
262
263 CORE_ADDR
264 read_memory_typed_address (CORE_ADDR addr, struct type *type)
265 {
266 gdb_byte *buf = (gdb_byte *) alloca (type->length ());
267
268 read_memory (addr, buf, type->length ());
269 return extract_typed_address (buf, type);
270 }
271
272 /* See gdbcore.h. */
273
274 void
275 write_memory (CORE_ADDR memaddr,
276 const bfd_byte *myaddr, ssize_t len)
277 {
278 int status;
279
280 status = target_write_memory (memaddr, myaddr, len);
281 if (status != 0)
282 memory_error (TARGET_XFER_E_IO, memaddr);
283 }
284
285 /* Notify interpreters and observers that INF's memory was changed. */
286
287 static void
288 notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
289 const bfd_byte *data)
290 {
291 interps_notify_memory_changed (inf, addr, len, data);
292 gdb::observers::memory_changed.notify (inf, addr, len, data);
293 }
294
295 /* Same as write_memory, but notify 'memory_changed' observers. */
296
297 void
298 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
299 ssize_t len)
300 {
301 write_memory (memaddr, myaddr, len);
302 notify_memory_changed (current_inferior (), memaddr, len, myaddr);
303 }
304
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)
311 {
312 gdb_byte *buf = (gdb_byte *) alloca (len);
313
314 store_unsigned_integer (buf, len, byte_order, value);
315 write_memory (addr, buf, len);
316 }
317
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)
324 {
325 gdb_byte *buf = (gdb_byte *) alloca (len);
326
327 store_signed_integer (buf, len, byte_order, value);
328 write_memory (addr, buf, len);
329 }
330 \f
331 /* The current default bfd target. Points to storage allocated for
332 gnutarget_string. */
333 const char *gnutarget;
334
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)
341 {
342 gdb_printf (file,
343 _("The current BFD target is \"%s\".\n"), value);
344 }
345
346 static void
347 set_gnutarget_command (const char *ignore, int from_tty,
348 struct cmd_list_element *c)
349 {
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 ());
354
355 if (gnutarget_string == "auto")
356 gnutarget = NULL;
357 else
358 gnutarget = gnutarget_string.c_str ();
359 }
360
361 /* A completion function for "set gnutarget". */
362
363 static void
364 complete_set_gnutarget (struct cmd_list_element *cmd,
365 completion_tracker &tracker,
366 const char *text, const char *word)
367 {
368 static const char **bfd_targets;
369
370 if (bfd_targets == NULL)
371 {
372 int last;
373
374 bfd_targets = bfd_target_list ();
375 for (last = 0; bfd_targets[last] != NULL; ++last)
376 ;
377
378 bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
379 bfd_targets[last] = "auto";
380 bfd_targets[last + 1] = NULL;
381 }
382
383 complete_on_enum (tracker, bfd_targets, text, word);
384 }
385
386 /* Set the gnutarget. */
387 void
388 set_gnutarget (const char *newtarget)
389 {
390 gnutarget_string = newtarget;
391 set_gnutarget_command (NULL, 0, NULL);
392 }
393
394 INIT_GDB_FILE (core)
395 {
396 cmd_list_element *core_file_cmd
397 = add_cmd ("core-file", class_files, core_file_command, _("\
398 Use FILE as core dump for examining memory and registers.\n\
399 Usage: core-file FILE\n\
400 No arg means have no core file. This command has been superseded by the\n\
401 `target core' and `detach' commands."), &cmdlist);
402 set_cmd_completer (core_file_cmd, deprecated_filename_completer);
403
404 set_show_commands set_show_gnutarget
405 = add_setshow_string_noescape_cmd ("gnutarget", class_files,
406 &gnutarget_string, _("\
407 Set the current BFD target."), _("\
408 Show the current BFD target."), _("\
409 Use `set gnutarget auto' to specify automatic detection."),
410 set_gnutarget_command,
411 show_gnutarget_string,
412 &setlist, &showlist);
413 set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
414
415 add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
416
417 if (getenv ("GNUTARGET"))
418 set_gnutarget (getenv ("GNUTARGET"));
419 else
420 set_gnutarget ("auto");
421 }