]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/corelow.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / corelow.c
CommitLineData
c906108c 1/* Core dump and executable file functions below target vector, for GDB.
4646aa9d 2
3666a048 3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
d55e5aa6 21#include "arch-utils.h"
4de283e4
TT
22#include <signal.h>
23#include <fcntl.h>
24#include "frame.h" /* required by inferior.h */
d55e5aa6
TT
25#include "inferior.h"
26#include "infrun.h"
4de283e4
TT
27#include "symtab.h"
28#include "command.h"
29#include "bfd.h"
30#include "target.h"
d55e5aa6 31#include "process-stratum-target.h"
4de283e4
TT
32#include "gdbcore.h"
33#include "gdbthread.h"
4e052eda 34#include "regcache.h"
0e24ac5d 35#include "regset.h"
d55e5aa6 36#include "symfile.h"
4de283e4 37#include "exec.h"
e0eac551 38#include "readline/tilde.h"
4de283e4 39#include "solib.h"
db082f59 40#include "solist.h"
4de283e4
TT
41#include "filenames.h"
42#include "progspace.h"
43#include "objfiles.h"
44#include "gdb_bfd.h"
45#include "completer.h"
268a13a5 46#include "gdbsupport/filestuff.h"
aa2d5a42 47#include "build-id.h"
ff8577f6 48#include "gdbsupport/pathstuff.h"
db082f59 49#include <unordered_map>
973695d6 50#include <unordered_set>
09c2f5d4 51#include "gdbcmd.h"
8e860359 52
ee28ca0f
AC
53#ifndef O_LARGEFILE
54#define O_LARGEFILE 0
55#endif
56
f6ac5f3d
PA
57/* The core file target. */
58
d9f719f1
PA
59static const target_info core_target_info = {
60 "core",
61 N_("Local core dump file"),
590042fc
PW
62 N_("Use a core file as a target.\n\
63Specify the filename of the core file.")
d9f719f1
PA
64};
65
3b3dac9b 66class core_target final : public process_stratum_target
f6ac5f3d
PA
67{
68public:
15244507 69 core_target ();
f6ac5f3d 70
d9f719f1
PA
71 const target_info &info () const override
72 { return core_target_info; }
f6ac5f3d 73
f6ac5f3d
PA
74 void close () override;
75 void detach (inferior *, int) override;
76 void fetch_registers (struct regcache *, int) override;
77
78 enum target_xfer_status xfer_partial (enum target_object object,
79 const char *annex,
80 gdb_byte *readbuf,
81 const gdb_byte *writebuf,
82 ULONGEST offset, ULONGEST len,
83 ULONGEST *xfered_len) override;
84 void files_info () override;
85
57810aa7 86 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
87 const struct target_desc *read_description () override;
88
a068643d 89 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
90
91 const char *thread_name (struct thread_info *) override;
92
2735d421 93 bool has_all_memory () override { return true; }
57810aa7
PA
94 bool has_memory () override;
95 bool has_stack () override;
96 bool has_registers () override;
5018ce90 97 bool has_execution (inferior *inf) override { return false; }
f3d11a9a 98
f6ac5f3d 99 bool info_proc (const char *, enum info_proc_what) override;
f6ac5f3d 100
15244507
PA
101 /* A few helpers. */
102
103 /* Getter, see variable definition. */
104 struct gdbarch *core_gdbarch ()
105 {
106 return m_core_gdbarch;
107 }
108
109 /* See definition. */
110 void get_core_register_section (struct regcache *regcache,
111 const struct regset *regset,
112 const char *name,
dbd534fe 113 int section_min_size,
15244507
PA
114 const char *human_name,
115 bool required);
116
09c2f5d4
KB
117 /* See definition. */
118 void info_proc_mappings (struct gdbarch *gdbarch);
119
15244507
PA
120private: /* per-core data */
121
122 /* The core's section table. Note that these target sections are
123 *not* mapped in the current address spaces' set of target
124 sections --- those should come only from pure executable or
125 shared library bfds. The core bfd sections are an implementation
126 detail of the core target, just like ptrace is for unix child
127 targets. */
bb2a6777 128 target_section_table m_core_section_table;
15244507 129
db082f59
KB
130 /* File-backed address space mappings: some core files include
131 information about memory mapped files. */
bb2a6777 132 target_section_table m_core_file_mappings;
db082f59 133
973695d6
KB
134 /* Unavailable mappings. These correspond to pathnames which either
135 weren't found or could not be opened. Knowing these addresses can
136 still be useful. */
137 std::vector<mem_range> m_core_unavailable_mappings;
138
db082f59
KB
139 /* Build m_core_file_mappings. Called from the constructor. */
140 void build_file_mappings ();
141
973695d6
KB
142 /* Helper method for xfer_partial. */
143 enum target_xfer_status xfer_memory_via_mappings (gdb_byte *readbuf,
144 const gdb_byte *writebuf,
145 ULONGEST offset,
146 ULONGEST len,
147 ULONGEST *xfered_len);
148
15244507
PA
149 /* FIXME: kettenis/20031023: Eventually this field should
150 disappear. */
151 struct gdbarch *m_core_gdbarch = NULL;
152};
c906108c 153
15244507
PA
154core_target::core_target ()
155{
15244507 156 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
2acceee2 157
6ba0a321
CB
158 if (!m_core_gdbarch
159 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
160 error (_("\"%s\": Core file format not supported"),
161 bfd_get_filename (core_bfd));
2acceee2 162
15244507 163 /* Find the data section */
2d128614 164 m_core_section_table = build_section_table (core_bfd);
db082f59
KB
165
166 build_file_mappings ();
15244507 167}
0e24ac5d 168
db082f59
KB
169/* Construct the target_section_table for file-backed mappings if
170 they exist.
171
172 For each unique path in the note, we'll open a BFD with a bfd
173 target of "binary". This is an unstructured bfd target upon which
174 we'll impose a structure from the mappings in the architecture-specific
175 mappings note. A BFD section is allocated and initialized for each
176 file-backed mapping.
177
178 We take care to not share already open bfds with other parts of
179 GDB; in particular, we don't want to add new sections to existing
180 BFDs. We do, however, ensure that the BFDs that we allocate here
181 will go away (be deallocated) when the core target is detached. */
182
183void
184core_target::build_file_mappings ()
185{
186 std::unordered_map<std::string, struct bfd *> bfd_map;
973695d6 187 std::unordered_set<std::string> unavailable_paths;
db082f59
KB
188
189 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
190 read_core_file_mappings method. */
191 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
192
193 /* After determining the number of mappings, read_core_file_mappings
bb2a6777
TT
194 will invoke this lambda. */
195 [&] (ULONGEST)
db082f59 196 {
db082f59
KB
197 },
198
199 /* read_core_file_mappings will invoke this lambda for each mapping
200 that it finds. */
201 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
70125a45 202 const char *filename)
db082f59
KB
203 {
204 /* Architecture-specific read_core_mapping methods are expected to
205 weed out non-file-backed mappings. */
206 gdb_assert (filename != nullptr);
207
208 struct bfd *bfd = bfd_map[filename];
209 if (bfd == nullptr)
210 {
211 /* Use exec_file_find() to do sysroot expansion. It'll
212 also strip the potential sysroot "target:" prefix. If
213 there is no sysroot, an equivalent (possibly more
214 canonical) pathname will be provided. */
215 gdb::unique_xmalloc_ptr<char> expanded_fname
216 = exec_file_find (filename, NULL);
217 if (expanded_fname == nullptr)
218 {
973695d6
KB
219 m_core_unavailable_mappings.emplace_back (start, end - start);
220 /* Print just one warning per path. */
221 if (unavailable_paths.insert (filename).second)
222 warning (_("Can't open file %s during file-backed mapping "
223 "note processing"),
224 filename);
db082f59
KB
225 return;
226 }
227
228 bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
dda83cd7 229 "binary");
db082f59
KB
230
231 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
232 {
973695d6 233 m_core_unavailable_mappings.emplace_back (start, end - start);
db082f59
KB
234 /* If we get here, there's a good chance that it's due to
235 an internal error. We issue a warning instead of an
236 internal error because of the possibility that the
237 file was removed in between checking for its
238 existence during the expansion in exec_file_find()
239 and the calls to bfd_openr() / bfd_check_format().
240 Output both the path from the core file note along
241 with its expansion to make debugging this problem
242 easier. */
243 warning (_("Can't open file %s which was expanded to %s "
244 "during file-backed mapping note processing"),
245 filename, expanded_fname.get ());
246 if (bfd != nullptr)
247 bfd_close (bfd);
248 return;
249 }
250 /* Ensure that the bfd will be closed when core_bfd is closed.
251 This can be checked before/after a core file detach via
252 "maint info bfds". */
253 gdb_bfd_record_inclusion (core_bfd, bfd);
254 }
255
256 /* Make new BFD section. All sections have the same name,
257 which is permitted by bfd_make_section_anyway(). */
258 asection *sec = bfd_make_section_anyway (bfd, "load");
259 if (sec == nullptr)
260 error (_("Can't make section"));
261 sec->filepos = file_ofs;
262 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
263 bfd_set_section_size (sec, end - start);
264 bfd_set_section_vma (sec, start);
265 bfd_set_section_lma (sec, start);
266 bfd_set_section_alignment (sec, 2);
267
268 /* Set target_section fields. */
6be2a9ab 269 m_core_file_mappings.emplace_back (start, end, sec);
db082f59 270 });
973695d6
KB
271
272 normalize_mem_ranges (&m_core_unavailable_mappings);
15244507 273}
0e24ac5d 274
7f9f62ba
PA
275/* An arbitrary identifier for the core inferior. */
276#define CORELOW_PID 1
277
15244507 278/* Close the core target. */
c906108c 279
15244507
PA
280void
281core_target::close ()
c906108c 282{
c906108c
SS
283 if (core_bfd)
284 {
60db1b85
PA
285 switch_to_no_thread (); /* Avoid confusion from thread
286 stuff. */
00431a78 287 exit_inferior_silent (current_inferior ());
c906108c 288
aff410f1 289 /* Clear out solib state while the bfd is still open. See
dda83cd7 290 comments in clear_solib in solib.c. */
a77053c2 291 clear_solib ();
7a292a7a 292
06333fea 293 current_program_space->cbfd.reset (nullptr);
c906108c 294 }
c906108c 295
15244507
PA
296 /* Core targets are heap-allocated (see core_target_open), so here
297 we delete ourselves. */
298 delete this;
74b7792f
AC
299}
300
aff410f1
MS
301/* Look for sections whose names start with `.reg/' so that we can
302 extract the list of threads in a core file. */
c906108c
SS
303
304static void
a190fabb 305add_to_thread_list (asection *asect, asection *reg_sect)
c906108c 306{
3cdd9356
PA
307 int core_tid;
308 int pid, lwpid;
9ab8741a 309 bool fake_pid_p = false;
88f38a04 310 struct inferior *inf;
c906108c 311
fd361982 312 if (!startswith (bfd_section_name (asect), ".reg/"))
c906108c
SS
313 return;
314
fd361982 315 core_tid = atoi (bfd_section_name (asect) + 5);
c906108c 316
261b8d08
PA
317 pid = bfd_core_file_pid (core_bfd);
318 if (pid == 0)
3cdd9356 319 {
9ab8741a 320 fake_pid_p = true;
3cdd9356 321 pid = CORELOW_PID;
3cdd9356 322 }
0de3b513 323
261b8d08
PA
324 lwpid = core_tid;
325
88f38a04
PA
326 inf = current_inferior ();
327 if (inf->pid == 0)
328 {
329 inferior_appeared (inf, pid);
330 inf->fake_pid_p = fake_pid_p;
331 }
3cdd9356 332
60db1b85 333 ptid_t ptid (pid, lwpid);
3cdd9356 334
60db1b85 335 thread_info *thr = add_thread (inf->process_target (), ptid);
c906108c
SS
336
337/* Warning, Will Robinson, looking at BFD private data! */
338
339 if (reg_sect != NULL
aff410f1 340 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
60db1b85 341 switch_to_thread (thr); /* Yes, make it current. */
c906108c
SS
342}
343
451953fa
PA
344/* Issue a message saying we have no core to debug, if FROM_TTY. */
345
346static void
347maybe_say_no_core_file_now (int from_tty)
348{
349 if (from_tty)
350 printf_filtered (_("No core file now.\n"));
351}
352
30baf67b 353/* Backward compatibility with old way of specifying core files. */
451953fa
PA
354
355void
356core_file_command (const char *filename, int from_tty)
357{
358 dont_repeat (); /* Either way, seems bogus. */
359
360 if (filename == NULL)
361 {
362 if (core_bfd != NULL)
363 {
364 target_detach (current_inferior (), from_tty);
365 gdb_assert (core_bfd == NULL);
366 }
367 else
368 maybe_say_no_core_file_now (from_tty);
369 }
370 else
371 core_target_open (filename, from_tty);
372}
373
aa2d5a42
KS
374/* Locate (and load) an executable file (and symbols) given the core file
375 BFD ABFD. */
376
377static void
378locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
379{
380 const bfd_build_id *build_id = build_id_bfd_get (abfd);
381 if (build_id == nullptr)
382 return;
383
384 gdb_bfd_ref_ptr execbfd
385 = build_id_to_exec_bfd (build_id->size, build_id->data);
386
387 if (execbfd != nullptr)
388 {
389 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
390 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
391 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
392 }
393}
394
d9f719f1 395/* See gdbcore.h. */
c906108c 396
f6ac5f3d 397void
d9f719f1 398core_target_open (const char *arg, int from_tty)
c906108c
SS
399{
400 const char *p;
401 int siggy;
c906108c 402 int scratch_chan;
ee28ca0f 403 int flags;
c906108c
SS
404
405 target_preopen (from_tty);
014f9477 406 if (!arg)
c906108c 407 {
8a3fe4f8 408 if (core_bfd)
3e43a32a
MS
409 error (_("No core file specified. (Use `detach' "
410 "to stop debugging a core file.)"));
8a3fe4f8
AC
411 else
412 error (_("No core file specified."));
c906108c
SS
413 }
414
ee0c3293
TT
415 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
416 if (!IS_ABSOLUTE_PATH (filename.get ()))
ff8577f6 417 filename = gdb_abspath (filename.get ());
c906108c 418
ee28ca0f
AC
419 flags = O_BINARY | O_LARGEFILE;
420 if (write_files)
421 flags |= O_RDWR;
422 else
423 flags |= O_RDONLY;
ee0c3293 424 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
c906108c 425 if (scratch_chan < 0)
ee0c3293 426 perror_with_name (filename.get ());
c906108c 427
ee0c3293 428 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
192b62ce
TT
429 write_files ? FOPEN_RUB : FOPEN_RB,
430 scratch_chan));
c906108c 431 if (temp_bfd == NULL)
ee0c3293 432 perror_with_name (filename.get ());
c906108c 433
6ba0a321 434 if (!bfd_check_format (temp_bfd.get (), bfd_core))
c906108c
SS
435 {
436 /* Do it after the err msg */
aff410f1 437 /* FIXME: should be checking for errors from bfd_close (for one
dda83cd7
SM
438 thing, on error it does not free all the storage associated
439 with the bfd). */
8a3fe4f8 440 error (_("\"%s\" is not a core dump: %s"),
ee0c3293 441 filename.get (), bfd_errmsg (bfd_get_error ()));
c906108c
SS
442 }
443
06333fea 444 current_program_space->cbfd = std::move (temp_bfd);
c906108c 445
15244507 446 core_target *target = new core_target ();
0e24ac5d 447
15244507
PA
448 /* Own the target until it is successfully pushed. */
449 target_ops_up target_holder (target);
2acceee2 450
c906108c
SS
451 validate_files ();
452
2f1b5984
MK
453 /* If we have no exec file, try to set the architecture from the
454 core file. We don't do this unconditionally since an exec file
455 typically contains more information that helps us determine the
456 architecture than a core file. */
7e10abd1 457 if (!current_program_space->exec_bfd ())
2f1b5984 458 set_gdbarch_from_file (core_bfd);
cbda0a99 459
dea57a62 460 push_target (std::move (target_holder));
c906108c 461
60db1b85 462 switch_to_no_thread ();
0de3b513 463
739fc47a
PA
464 /* Need to flush the register cache (and the frame cache) from a
465 previous debug session. If inferior_ptid ends up the same as the
466 last debug session --- e.g., b foo; run; gcore core1; step; gcore
467 core2; core core1; core core2 --- then there's potential for
468 get_current_regcache to return the cached regcache of the
469 previous session, and the frame cache being stale. */
470 registers_changed ();
471
0de3b513
PA
472 /* Build up thread list from BFD sections, and possibly set the
473 current thread to the .reg/NN section matching the .reg
aff410f1 474 section. */
a190fabb
TT
475 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
476 for (asection *sect : gdb_bfd_sections (core_bfd))
477 add_to_thread_list (sect, reg_sect);
0de3b513 478
d7e15655 479 if (inferior_ptid == null_ptid)
3cdd9356
PA
480 {
481 /* Either we found no .reg/NN section, and hence we have a
482 non-threaded core (single-threaded, from gdb's perspective),
483 or for some reason add_to_thread_list couldn't determine
484 which was the "main" thread. The latter case shouldn't
485 usually happen, but we're dealing with input here, which can
486 always be broken in different ways. */
00431a78 487 thread_info *thread = first_thread_of_inferior (current_inferior ());
c5504eaf 488
3cdd9356
PA
489 if (thread == NULL)
490 {
c45ceae0 491 inferior_appeared (current_inferior (), CORELOW_PID);
60db1b85 492 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
3cdd9356 493 }
60db1b85
PA
494
495 switch_to_thread (thread);
3cdd9356
PA
496 }
497
7e10abd1 498 if (current_program_space->exec_bfd () == nullptr)
aa2d5a42
KS
499 locate_exec_from_corefile_build_id (core_bfd, from_tty);
500
a7aba266 501 post_create_inferior (from_tty);
959b8724 502
0de3b513
PA
503 /* Now go through the target stack looking for threads since there
504 may be a thread_stratum target loaded on top of target core by
505 now. The layer above should claim threads found in the BFD
506 sections. */
a70b8144 507 try
8e7b59a5 508 {
e8032dde 509 target_update_thread_list ();
8e7b59a5
KS
510 }
511
230d2906 512 catch (const gdb_exception_error &except)
492d29ea
PA
513 {
514 exception_print (gdb_stderr, except);
515 }
0de3b513 516
c906108c
SS
517 p = bfd_core_file_failing_command (core_bfd);
518 if (p)
a3f17187 519 printf_filtered (_("Core was generated by `%s'.\n"), p);
c906108c 520
0c557179
SDJ
521 /* Clearing any previous state of convenience variables. */
522 clear_exit_convenience_vars ();
523
c906108c
SS
524 siggy = bfd_core_file_failing_signal (core_bfd);
525 if (siggy > 0)
423ec54c 526 {
15244507
PA
527 gdbarch *core_gdbarch = target->core_gdbarch ();
528
22203bbf 529 /* If we don't have a CORE_GDBARCH to work with, assume a native
1f8cf220
PA
530 core (map gdb_signal from host signals). If we do have
531 CORE_GDBARCH to work with, but no gdb_signal_from_target
532 implementation for that gdbarch, as a fallback measure,
533 assume the host signal mapping. It'll be correct for native
534 cores, but most likely incorrect for cross-cores. */
2ea28649 535 enum gdb_signal sig = (core_gdbarch != NULL
1f8cf220
PA
536 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
537 ? gdbarch_gdb_signal_from_target (core_gdbarch,
538 siggy)
539 : gdb_signal_from_host (siggy));
423ec54c 540
ad97bfc5 541 printf_filtered (_("Program terminated with signal %s, %s"),
2d503272 542 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
ad97bfc5
JB
543 if (gdbarch_report_signal_info_p (core_gdbarch))
544 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
545 printf_filtered (_(".\n"));
0c557179
SDJ
546
547 /* Set the value of the internal variable $_exitsignal,
548 which holds the signal uncaught by the inferior. */
549 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
550 siggy);
423ec54c 551 }
c906108c 552
87ab71f0
PA
553 /* Fetch all registers from core file. */
554 target_fetch_registers (get_current_regcache (), -1);
c906108c 555
87ab71f0
PA
556 /* Now, set up the frame cache, and print the top of stack. */
557 reinit_frame_cache ();
08d72866 558 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
f0e8c4c5
JK
559
560 /* Current thread should be NUM 1 but the user does not know that.
561 If a program is single threaded gdb in general does not mention
562 anything about threads. That is why the test is >= 2. */
5b6d1e4f 563 if (thread_count (target) >= 2)
f0e8c4c5 564 {
a70b8144 565 try
f0e8c4c5
JK
566 {
567 thread_command (NULL, from_tty);
568 }
230d2906 569 catch (const gdb_exception_error &except)
492d29ea
PA
570 {
571 exception_print (gdb_stderr, except);
572 }
f0e8c4c5 573 }
c906108c
SS
574}
575
f6ac5f3d
PA
576void
577core_target::detach (inferior *inf, int from_tty)
c906108c 578{
15244507
PA
579 /* Note that 'this' is dangling after this call. unpush_target
580 closes the target, and our close implementation deletes
581 'this'. */
f6ac5f3d 582 unpush_target (this);
15244507 583
66452beb
PW
584 /* Clear the register cache and the frame cache. */
585 registers_changed ();
c906108c 586 reinit_frame_cache ();
451953fa 587 maybe_say_no_core_file_now (from_tty);
c906108c
SS
588}
589
de57eccd 590/* Try to retrieve registers from a section in core_bfd, and supply
6ba0a321 591 them to REGSET.
de57eccd 592
11a33714
SM
593 If ptid's lwp member is zero, do the single-threaded
594 thing: look for a section named NAME. If ptid's lwp
0de3b513
PA
595 member is non-zero, do the multi-threaded thing: look for a section
596 named "NAME/LWP", where LWP is the shortest ASCII decimal
11a33714 597 representation of ptid's lwp member.
de57eccd
JM
598
599 HUMAN_NAME is a human-readable name for the kind of registers the
600 NAME section contains, for use in error messages.
601
15244507
PA
602 If REQUIRED is true, print an error if the core file doesn't have a
603 section by the appropriate name. Otherwise, just do nothing. */
de57eccd 604
15244507
PA
605void
606core_target::get_core_register_section (struct regcache *regcache,
607 const struct regset *regset,
608 const char *name,
dbd534fe 609 int section_min_size,
15244507
PA
610 const char *human_name,
611 bool required)
de57eccd 612{
6ba0a321
CB
613 gdb_assert (regset != nullptr);
614
7be0c536 615 struct bfd_section *section;
de57eccd 616 bfd_size_type size;
6ba0a321 617 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
de57eccd 618
3c3ae77e 619 thread_section_name section_name (name, regcache->ptid ());
de57eccd 620
3c3ae77e 621 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
de57eccd
JM
622 if (! section)
623 {
624 if (required)
aff410f1
MS
625 warning (_("Couldn't find %s registers in core file."),
626 human_name);
de57eccd
JM
627 return;
628 }
629
fd361982 630 size = bfd_section_size (section);
dbd534fe 631 if (size < section_min_size)
8f0435f7 632 {
3c3ae77e
PA
633 warning (_("Section `%s' in core file too small."),
634 section_name.c_str ());
8f0435f7
AA
635 return;
636 }
dbd534fe 637 if (size != section_min_size && !variable_size_section)
f962539a
AA
638 {
639 warning (_("Unexpected size of section `%s' in core file."),
3c3ae77e 640 section_name.c_str ());
f962539a 641 }
8f0435f7 642
0cac9354 643 gdb::byte_vector contents (size);
d8b2f9e3
SM
644 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
645 (file_ptr) 0, size))
de57eccd 646 {
8a3fe4f8 647 warning (_("Couldn't read %s registers from `%s' section in core file."),
3c3ae77e 648 human_name, section_name.c_str ());
de57eccd
JM
649 return;
650 }
651
6ba0a321 652 regset->supply_regset (regset, regcache, -1, contents.data (), size);
de57eccd
JM
653}
654
15244507
PA
655/* Data passed to gdbarch_iterate_over_regset_sections's callback. */
656struct get_core_registers_cb_data
657{
658 core_target *target;
659 struct regcache *regcache;
660};
661
5aa82d05
AA
662/* Callback for get_core_registers that handles a single core file
663 register note section. */
664
665static void
a616bb94 666get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
8f0435f7 667 const struct regset *regset,
5aa82d05
AA
668 const char *human_name, void *cb_data)
669{
6ba0a321
CB
670 gdb_assert (regset != nullptr);
671
15244507
PA
672 auto *data = (get_core_registers_cb_data *) cb_data;
673 bool required = false;
6ba0a321 674 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
a616bb94
AH
675
676 if (!variable_size_section)
677 gdb_assert (supply_size == collect_size);
5aa82d05
AA
678
679 if (strcmp (sect_name, ".reg") == 0)
8f0435f7 680 {
15244507 681 required = true;
8f0435f7
AA
682 if (human_name == NULL)
683 human_name = "general-purpose";
684 }
5aa82d05 685 else if (strcmp (sect_name, ".reg2") == 0)
8f0435f7
AA
686 {
687 if (human_name == NULL)
688 human_name = "floating-point";
689 }
690
15244507 691 data->target->get_core_register_section (data->regcache, regset, sect_name,
6ba0a321 692 supply_size, human_name, required);
5aa82d05 693}
de57eccd 694
c906108c
SS
695/* Get the registers out of a core file. This is the machine-
696 independent part. Fetch_core_registers is the machine-dependent
aff410f1
MS
697 part, typically implemented in the xm-file for each
698 architecture. */
c906108c
SS
699
700/* We just get all the registers, so we don't use regno. */
701
f6ac5f3d
PA
702void
703core_target::fetch_registers (struct regcache *regcache, int regno)
c906108c 704{
15244507 705 if (!(m_core_gdbarch != nullptr
6ba0a321 706 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
c906108c
SS
707 {
708 fprintf_filtered (gdb_stderr,
c5aa993b 709 "Can't fetch registers from this type of core file\n");
c906108c
SS
710 return;
711 }
712
6ba0a321
CB
713 struct gdbarch *gdbarch = regcache->arch ();
714 get_core_registers_cb_data data = { this, regcache };
715 gdbarch_iterate_over_regset_sections (gdbarch,
716 get_core_registers_cb,
717 (void *) &data, NULL);
c906108c 718
ee99023e 719 /* Mark all registers not found in the core as unavailable. */
6ba0a321 720 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
0ec9f114 721 if (regcache->get_register_status (i) == REG_UNKNOWN)
73e1c03f 722 regcache->raw_supply (i, NULL);
c906108c
SS
723}
724
f6ac5f3d
PA
725void
726core_target::files_info ()
c906108c 727{
15244507 728 print_section_info (&m_core_section_table, core_bfd);
c906108c 729}
e2544d02 730\f
973695d6
KB
731/* Helper method for core_target::xfer_partial. */
732
733enum target_xfer_status
734core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
735 const gdb_byte *writebuf,
736 ULONGEST offset, ULONGEST len,
737 ULONGEST *xfered_len)
738{
739 enum target_xfer_status xfer_status;
740
741 xfer_status = (section_table_xfer_memory_partial
742 (readbuf, writebuf,
743 offset, len, xfered_len,
bb2a6777 744 m_core_file_mappings));
973695d6
KB
745
746 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
747 return xfer_status;
748
749 /* There are instances - e.g. when debugging within a docker
750 container using the AUFS storage driver - where the pathnames
751 obtained from the note section are incorrect. Despite the path
752 being wrong, just knowing the start and end addresses of the
753 mappings is still useful; we can attempt an access of the file
754 stratum constrained to the address ranges corresponding to the
755 unavailable mappings. */
756
757 ULONGEST memaddr = offset;
758 ULONGEST memend = offset + len;
759
760 for (const auto &mr : m_core_unavailable_mappings)
761 {
762 if (address_in_mem_range (memaddr, &mr))
dda83cd7 763 {
973695d6
KB
764 if (!address_in_mem_range (memend, &mr))
765 len = mr.start + mr.length - memaddr;
766
767 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
768 NULL,
769 readbuf,
770 writebuf,
771 offset,
772 len,
773 xfered_len);
774 break;
775 }
776 }
777
778 return xfer_status;
779}
780
f6ac5f3d
PA
781enum target_xfer_status
782core_target::xfer_partial (enum target_object object, const char *annex,
783 gdb_byte *readbuf, const gdb_byte *writebuf,
784 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
e2544d02
RM
785{
786 switch (object)
787 {
788 case TARGET_OBJECT_MEMORY:
2735d421
KB
789 {
790 enum target_xfer_status xfer_status;
791
792 /* Try accessing memory contents from core file data,
793 restricting consideration to those sections for which
794 the BFD section flag SEC_HAS_CONTENTS is set. */
795 auto has_contents_cb = [] (const struct target_section *s)
796 {
797 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
798 };
799 xfer_status = section_table_xfer_memory_partial
800 (readbuf, writebuf,
801 offset, len, xfered_len,
bb2a6777 802 m_core_section_table,
2735d421
KB
803 has_contents_cb);
804 if (xfer_status == TARGET_XFER_OK)
805 return TARGET_XFER_OK;
806
db082f59
KB
807 /* Check file backed mappings. If they're available, use
808 core file provided mappings (e.g. from .note.linuxcore.file
809 or the like) as this should provide a more accurate
810 result. If not, check the stratum beneath us, which should
1bd57575
LM
811 be the file stratum.
812
813 We also check unavailable mappings due to Docker/AUFS driver
814 issues. */
815 if (!m_core_file_mappings.empty ()
816 || !m_core_unavailable_mappings.empty ())
817 {
818 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
819 len, xfered_len);
820 }
db082f59
KB
821 else
822 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
823 writebuf, offset, len,
824 xfered_len);
2735d421
KB
825 if (xfer_status == TARGET_XFER_OK)
826 return TARGET_XFER_OK;
e2544d02 827
2735d421
KB
828 /* Finally, attempt to access data in core file sections with
829 no contents. These will typically read as all zero. */
830 auto no_contents_cb = [&] (const struct target_section *s)
831 {
832 return !has_contents_cb (s);
833 };
834 xfer_status = section_table_xfer_memory_partial
835 (readbuf, writebuf,
836 offset, len, xfered_len,
bb2a6777 837 m_core_section_table,
2735d421
KB
838 no_contents_cb);
839
840 return xfer_status;
841 }
e2544d02
RM
842 case TARGET_OBJECT_AUXV:
843 if (readbuf)
844 {
845 /* When the aux vector is stored in core file, BFD
846 represents this with a fake section called ".auxv". */
847
c4c5b7ba 848 struct bfd_section *section;
e2544d02 849 bfd_size_type size;
e2544d02
RM
850
851 section = bfd_get_section_by_name (core_bfd, ".auxv");
852 if (section == NULL)
2ed4b548 853 return TARGET_XFER_E_IO;
e2544d02 854
fd361982 855 size = bfd_section_size (section);
e2544d02 856 if (offset >= size)
9b409511 857 return TARGET_XFER_EOF;
e2544d02
RM
858 size -= offset;
859 if (size > len)
860 size = len;
9b409511
YQ
861
862 if (size == 0)
863 return TARGET_XFER_EOF;
864 if (!bfd_get_section_contents (core_bfd, section, readbuf,
865 (file_ptr) offset, size))
e2544d02 866 {
8a3fe4f8 867 warning (_("Couldn't read NT_AUXV note in core file."));
2ed4b548 868 return TARGET_XFER_E_IO;
e2544d02
RM
869 }
870
9b409511
YQ
871 *xfered_len = (ULONGEST) size;
872 return TARGET_XFER_OK;
e2544d02 873 }
2ed4b548 874 return TARGET_XFER_E_IO;
e2544d02 875
403e1656
MK
876 case TARGET_OBJECT_WCOOKIE:
877 if (readbuf)
878 {
879 /* When the StackGhost cookie is stored in core file, BFD
aff410f1
MS
880 represents this with a fake section called
881 ".wcookie". */
403e1656
MK
882
883 struct bfd_section *section;
884 bfd_size_type size;
403e1656
MK
885
886 section = bfd_get_section_by_name (core_bfd, ".wcookie");
887 if (section == NULL)
2ed4b548 888 return TARGET_XFER_E_IO;
403e1656 889
fd361982 890 size = bfd_section_size (section);
403e1656 891 if (offset >= size)
96c4f946 892 return TARGET_XFER_EOF;
403e1656
MK
893 size -= offset;
894 if (size > len)
895 size = len;
9b409511
YQ
896
897 if (size == 0)
898 return TARGET_XFER_EOF;
899 if (!bfd_get_section_contents (core_bfd, section, readbuf,
900 (file_ptr) offset, size))
403e1656 901 {
8a3fe4f8 902 warning (_("Couldn't read StackGhost cookie in core file."));
2ed4b548 903 return TARGET_XFER_E_IO;
403e1656
MK
904 }
905
9b409511
YQ
906 *xfered_len = (ULONGEST) size;
907 return TARGET_XFER_OK;
908
403e1656 909 }
2ed4b548 910 return TARGET_XFER_E_IO;
403e1656 911
de584861 912 case TARGET_OBJECT_LIBRARIES:
15244507
PA
913 if (m_core_gdbarch != nullptr
914 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
de584861
PA
915 {
916 if (writebuf)
2ed4b548 917 return TARGET_XFER_E_IO;
9b409511
YQ
918 else
919 {
15244507 920 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
9b409511
YQ
921 readbuf,
922 offset, len);
923
924 if (*xfered_len == 0)
925 return TARGET_XFER_EOF;
926 else
927 return TARGET_XFER_OK;
928 }
de584861
PA
929 }
930 /* FALL THROUGH */
931
356a5233 932 case TARGET_OBJECT_LIBRARIES_AIX:
15244507
PA
933 if (m_core_gdbarch != nullptr
934 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
356a5233
JB
935 {
936 if (writebuf)
2ed4b548 937 return TARGET_XFER_E_IO;
9b409511
YQ
938 else
939 {
940 *xfered_len
15244507 941 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
9b409511
YQ
942 readbuf, offset,
943 len);
944
945 if (*xfered_len == 0)
946 return TARGET_XFER_EOF;
947 else
948 return TARGET_XFER_OK;
949 }
356a5233
JB
950 }
951 /* FALL THROUGH */
952
9015683b
TT
953 case TARGET_OBJECT_SIGNAL_INFO:
954 if (readbuf)
9b409511 955 {
15244507
PA
956 if (m_core_gdbarch != nullptr
957 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
9b409511 958 {
15244507 959 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
382b69bb
JB
960 offset, len);
961
962 if (l >= 0)
963 {
964 *xfered_len = l;
965 if (l == 0)
966 return TARGET_XFER_EOF;
967 else
968 return TARGET_XFER_OK;
969 }
9b409511
YQ
970 }
971 }
2ed4b548 972 return TARGET_XFER_E_IO;
9015683b 973
e2544d02 974 default:
b6a8c27b
PA
975 return this->beneath ()->xfer_partial (object, annex, readbuf,
976 writebuf, offset, len,
977 xfered_len);
e2544d02
RM
978 }
979}
980
c906108c 981\f
c906108c
SS
982
983/* Okay, let's be honest: threads gleaned from a core file aren't
984 exactly lively, are they? On the other hand, if we don't claim
985 that each & every one is alive, then we don't get any of them
986 to appear in an "info thread" command, which is quite a useful
987 behaviour.
c5aa993b 988 */
57810aa7 989bool
f6ac5f3d 990core_target::thread_alive (ptid_t ptid)
c906108c 991{
57810aa7 992 return true;
c906108c
SS
993}
994
4eb0ad19
DJ
995/* Ask the current architecture what it knows about this core file.
996 That will be used, in turn, to pick a better architecture. This
997 wrapper could be avoided if targets got a chance to specialize
15244507 998 core_target. */
4eb0ad19 999
f6ac5f3d
PA
1000const struct target_desc *
1001core_target::read_description ()
4eb0ad19 1002{
15244507 1003 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
2117c711
TT
1004 {
1005 const struct target_desc *result;
1006
15244507 1007 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
2117c711
TT
1008 if (result != NULL)
1009 return result;
1010 }
4eb0ad19 1011
b6a8c27b 1012 return this->beneath ()->read_description ();
4eb0ad19
DJ
1013}
1014
a068643d 1015std::string
f6ac5f3d 1016core_target::pid_to_str (ptid_t ptid)
0de3b513 1017{
88f38a04 1018 struct inferior *inf;
a5ee0f0c 1019 int pid;
0de3b513 1020
a5ee0f0c
PA
1021 /* The preferred way is to have a gdbarch/OS specific
1022 implementation. */
15244507
PA
1023 if (m_core_gdbarch != nullptr
1024 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1025 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
c5504eaf 1026
a5ee0f0c
PA
1027 /* Otherwise, if we don't have one, we'll just fallback to
1028 "process", with normal_pid_to_str. */
28439f5e 1029
a5ee0f0c 1030 /* Try the LWPID field first. */
e38504b3 1031 pid = ptid.lwp ();
a5ee0f0c 1032 if (pid != 0)
f2907e49 1033 return normal_pid_to_str (ptid_t (pid));
a5ee0f0c
PA
1034
1035 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1036 only if it isn't a fake PID. */
5b6d1e4f 1037 inf = find_inferior_ptid (this, ptid);
88f38a04 1038 if (inf != NULL && !inf->fake_pid_p)
a5ee0f0c 1039 return normal_pid_to_str (ptid);
0de3b513 1040
a5ee0f0c 1041 /* No luck. We simply don't have a valid PID to print. */
a068643d 1042 return "<main task>";
0de3b513
PA
1043}
1044
f6ac5f3d
PA
1045const char *
1046core_target::thread_name (struct thread_info *thr)
4dfc5dbc 1047{
15244507
PA
1048 if (m_core_gdbarch != nullptr
1049 && gdbarch_core_thread_name_p (m_core_gdbarch))
1050 return gdbarch_core_thread_name (m_core_gdbarch, thr);
4dfc5dbc
JB
1051 return NULL;
1052}
1053
57810aa7 1054bool
f6ac5f3d 1055core_target::has_memory ()
c35b1492
PA
1056{
1057 return (core_bfd != NULL);
1058}
1059
57810aa7 1060bool
f6ac5f3d 1061core_target::has_stack ()
c35b1492
PA
1062{
1063 return (core_bfd != NULL);
1064}
1065
57810aa7 1066bool
f6ac5f3d 1067core_target::has_registers ()
c35b1492
PA
1068{
1069 return (core_bfd != NULL);
1070}
1071
451b7c33
TT
1072/* Implement the to_info_proc method. */
1073
f6ac5f3d
PA
1074bool
1075core_target::info_proc (const char *args, enum info_proc_what request)
451b7c33
TT
1076{
1077 struct gdbarch *gdbarch = get_current_arch ();
1078
1079 /* Since this is the core file target, call the 'core_info_proc'
1080 method on gdbarch, not 'info_proc'. */
1081 if (gdbarch_core_info_proc_p (gdbarch))
1082 gdbarch_core_info_proc (gdbarch, args, request);
c906108c 1083
f6ac5f3d 1084 return true;
c906108c
SS
1085}
1086
09c2f5d4
KB
1087/* Get a pointer to the current core target. If not connected to a
1088 core target, return NULL. */
1089
1090static core_target *
1091get_current_core_target ()
1092{
1093 target_ops *proc_target = current_inferior ()->process_target ();
1094 return dynamic_cast<core_target *> (proc_target);
1095}
1096
1097/* Display file backed mappings from core file. */
1098
1099void
1100core_target::info_proc_mappings (struct gdbarch *gdbarch)
1101{
d7a78e5c 1102 if (!m_core_file_mappings.empty ())
09c2f5d4
KB
1103 {
1104 printf_filtered (_("Mapped address spaces:\n\n"));
1105 if (gdbarch_addr_bit (gdbarch) == 32)
1106 {
1107 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1108 "Start Addr",
1109 " End Addr",
1110 " Size", " Offset", "objfile");
1111 }
1112 else
1113 {
1114 printf_filtered (" %18s %18s %10s %10s %s\n",
1115 "Start Addr",
1116 " End Addr",
1117 " Size", " Offset", "objfile");
1118 }
1119 }
1120
d7a78e5c 1121 for (const target_section &tsp : m_core_file_mappings)
09c2f5d4 1122 {
bb2a6777
TT
1123 ULONGEST start = tsp.addr;
1124 ULONGEST end = tsp.endaddr;
1125 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1126 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
09c2f5d4
KB
1127
1128 if (gdbarch_addr_bit (gdbarch) == 32)
1129 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1130 paddress (gdbarch, start),
1131 paddress (gdbarch, end),
1132 hex_string (end - start),
1133 hex_string (file_ofs),
1134 filename);
1135 else
1136 printf_filtered (" %18s %18s %10s %10s %s\n",
1137 paddress (gdbarch, start),
1138 paddress (gdbarch, end),
1139 hex_string (end - start),
1140 hex_string (file_ofs),
1141 filename);
1142 }
1143}
1144
1145/* Implement "maintenance print core-file-backed-mappings" command.
1146
1147 If mappings are loaded, the results should be similar to the
1148 mappings shown by "info proc mappings". This command is mainly a
1149 debugging tool for GDB developers to make sure that the expected
1150 mappings are present after loading a core file. For Linux, the
1151 output provided by this command will be very similar (if not
1152 identical) to that provided by "info proc mappings". This is not
1153 necessarily the case for other OSes which might provide
1154 more/different information in the "info proc mappings" output. */
1155
1156static void
1157maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1158{
1159 core_target *targ = get_current_core_target ();
1160 if (targ != nullptr)
1161 targ->info_proc_mappings (targ->core_gdbarch ());
1162}
1163
6c265988 1164void _initialize_corelow ();
c906108c 1165void
6c265988 1166_initialize_corelow ()
c906108c 1167{
d9f719f1 1168 add_target (core_target_info, core_target_open, filename_completer);
09c2f5d4 1169 add_cmd ("core-file-backed-mappings", class_maintenance,
dda83cd7 1170 maintenance_print_core_file_backed_mappings,
513487e1 1171 _("Print core file's file-backed mappings."),
09c2f5d4 1172 &maintenanceprintlist);
c906108c 1173}