]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/corelow.c
91b442b4426faf18a11d57463460f2efee407949
[thirdparty/binutils-gdb.git] / gdb / corelow.c
1 /* Core dump and executable file functions below target vector, for GDB.
2
3 Copyright (C) 1986-2023 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 "defs.h"
21 #include "arch-utils.h"
22 #include <signal.h>
23 #include <fcntl.h>
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "symtab.h"
28 #include "command.h"
29 #include "bfd.h"
30 #include "target.h"
31 #include "process-stratum-target.h"
32 #include "gdbcore.h"
33 #include "gdbthread.h"
34 #include "regcache.h"
35 #include "regset.h"
36 #include "symfile.h"
37 #include "exec.h"
38 #include "readline/tilde.h"
39 #include "solib.h"
40 #include "solist.h"
41 #include "filenames.h"
42 #include "progspace.h"
43 #include "objfiles.h"
44 #include "gdb_bfd.h"
45 #include "completer.h"
46 #include "gdbsupport/filestuff.h"
47 #include "build-id.h"
48 #include "gdbsupport/pathstuff.h"
49 #include "gdbsupport/scoped_fd.h"
50 #include "gdbsupport/x86-xstate.h"
51 #include "debuginfod-support.h"
52 #include <unordered_map>
53 #include <unordered_set>
54 #include "gdbcmd.h"
55 #include "xml-tdesc.h"
56 #include "memtag.h"
57
58 #ifndef O_LARGEFILE
59 #define O_LARGEFILE 0
60 #endif
61
62 /* The core file target. */
63
64 static const target_info core_target_info = {
65 "core",
66 N_("Local core dump file"),
67 N_("Use a core file as a target.\n\
68 Specify the filename of the core file.")
69 };
70
71 class core_target final : public process_stratum_target
72 {
73 public:
74 core_target ();
75
76 const target_info &info () const override
77 { return core_target_info; }
78
79 void close () override;
80 void detach (inferior *, int) override;
81 void fetch_registers (struct regcache *, int) override;
82
83 enum target_xfer_status xfer_partial (enum target_object object,
84 const char *annex,
85 gdb_byte *readbuf,
86 const gdb_byte *writebuf,
87 ULONGEST offset, ULONGEST len,
88 ULONGEST *xfered_len) override;
89 void files_info () override;
90
91 bool thread_alive (ptid_t ptid) override;
92 const struct target_desc *read_description () override;
93
94 std::string pid_to_str (ptid_t) override;
95
96 const char *thread_name (struct thread_info *) override;
97
98 bool has_all_memory () override { return true; }
99 bool has_memory () override;
100 bool has_stack () override;
101 bool has_registers () override;
102 bool has_execution (inferior *inf) override { return false; }
103
104 bool info_proc (const char *, enum info_proc_what) override;
105
106 bool supports_memory_tagging () override;
107
108 /* Core file implementation of fetch_memtags. Fetch the memory tags from
109 core file notes. */
110 bool fetch_memtags (CORE_ADDR address, size_t len,
111 gdb::byte_vector &tags, int type) override;
112
113 x86_xsave_layout fetch_x86_xsave_layout () override;
114
115 /* A few helpers. */
116
117 /* Getter, see variable definition. */
118 struct gdbarch *core_gdbarch ()
119 {
120 return m_core_gdbarch;
121 }
122
123 /* See definition. */
124 void get_core_register_section (struct regcache *regcache,
125 const struct regset *regset,
126 const char *name,
127 int section_min_size,
128 const char *human_name,
129 bool required);
130
131 /* See definition. */
132 void info_proc_mappings (struct gdbarch *gdbarch);
133
134 private: /* per-core data */
135
136 /* Get rid of the core inferior. */
137 void clear_core ();
138
139 /* The core's section table. Note that these target sections are
140 *not* mapped in the current address spaces' set of target
141 sections --- those should come only from pure executable or
142 shared library bfds. The core bfd sections are an implementation
143 detail of the core target, just like ptrace is for unix child
144 targets. */
145 std::vector<target_section> m_core_section_table;
146
147 /* File-backed address space mappings: some core files include
148 information about memory mapped files. */
149 std::vector<target_section> m_core_file_mappings;
150
151 /* Unavailable mappings. These correspond to pathnames which either
152 weren't found or could not be opened. Knowing these addresses can
153 still be useful. */
154 std::vector<mem_range> m_core_unavailable_mappings;
155
156 /* Build m_core_file_mappings. Called from the constructor. */
157 void build_file_mappings ();
158
159 /* Helper method for xfer_partial. */
160 enum target_xfer_status xfer_memory_via_mappings (gdb_byte *readbuf,
161 const gdb_byte *writebuf,
162 ULONGEST offset,
163 ULONGEST len,
164 ULONGEST *xfered_len);
165
166 /* FIXME: kettenis/20031023: Eventually this field should
167 disappear. */
168 struct gdbarch *m_core_gdbarch = NULL;
169 };
170
171 core_target::core_target ()
172 {
173 /* Find a first arch based on the BFD. We need the initial gdbarch so
174 we can setup the hooks to find a target description. */
175 m_core_gdbarch = gdbarch_from_bfd (core_bfd);
176
177 /* If the arch is able to read a target description from the core, it
178 could yield a more specific gdbarch. */
179 const struct target_desc *tdesc = read_description ();
180
181 if (tdesc != nullptr)
182 {
183 struct gdbarch_info info;
184 info.abfd = core_bfd;
185 info.target_desc = tdesc;
186 m_core_gdbarch = gdbarch_find_by_info (info);
187 }
188
189 if (!m_core_gdbarch
190 || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
191 error (_("\"%s\": Core file format not supported"),
192 bfd_get_filename (core_bfd));
193
194 /* Find the data section */
195 m_core_section_table = build_section_table (core_bfd);
196
197 build_file_mappings ();
198 }
199
200 /* Construct the table for file-backed mappings if they exist.
201
202 For each unique path in the note, we'll open a BFD with a bfd
203 target of "binary". This is an unstructured bfd target upon which
204 we'll impose a structure from the mappings in the architecture-specific
205 mappings note. A BFD section is allocated and initialized for each
206 file-backed mapping.
207
208 We take care to not share already open bfds with other parts of
209 GDB; in particular, we don't want to add new sections to existing
210 BFDs. We do, however, ensure that the BFDs that we allocate here
211 will go away (be deallocated) when the core target is detached. */
212
213 void
214 core_target::build_file_mappings ()
215 {
216 std::unordered_map<std::string, struct bfd *> bfd_map;
217 std::unordered_set<std::string> unavailable_paths;
218
219 /* See linux_read_core_file_mappings() in linux-tdep.c for an example
220 read_core_file_mappings method. */
221 gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
222
223 /* After determining the number of mappings, read_core_file_mappings
224 will invoke this lambda. */
225 [&] (ULONGEST)
226 {
227 },
228
229 /* read_core_file_mappings will invoke this lambda for each mapping
230 that it finds. */
231 [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
232 const char *filename, const bfd_build_id *build_id)
233 {
234 /* Architecture-specific read_core_mapping methods are expected to
235 weed out non-file-backed mappings. */
236 gdb_assert (filename != nullptr);
237
238 if (unavailable_paths.find (filename) != unavailable_paths.end ())
239 {
240 /* We have already seen some mapping for FILENAME but failed to
241 find/open the file. There is no point in trying the same
242 thing again so just record that the range [start, end) is
243 unavailable. */
244 m_core_unavailable_mappings.emplace_back (start, end - start);
245 return;
246 }
247
248 struct bfd *bfd = bfd_map[filename];
249 if (bfd == nullptr)
250 {
251 /* Use exec_file_find() to do sysroot expansion. It'll
252 also strip the potential sysroot "target:" prefix. If
253 there is no sysroot, an equivalent (possibly more
254 canonical) pathname will be provided. */
255 gdb::unique_xmalloc_ptr<char> expanded_fname
256 = exec_file_find (filename, NULL);
257
258 if (expanded_fname == nullptr && build_id != nullptr)
259 debuginfod_exec_query (build_id->data, build_id->size,
260 filename, &expanded_fname);
261
262 if (expanded_fname == nullptr)
263 {
264 m_core_unavailable_mappings.emplace_back (start, end - start);
265 unavailable_paths.insert (filename);
266 warning (_("Can't open file %s during file-backed mapping "
267 "note processing"),
268 filename);
269 return;
270 }
271
272 bfd = bfd_openr (expanded_fname.get (), "binary");
273
274 if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
275 {
276 m_core_unavailable_mappings.emplace_back (start, end - start);
277 unavailable_paths.insert (filename);
278 warning (_("Can't open file %s which was expanded to %s "
279 "during file-backed mapping note processing"),
280 filename, expanded_fname.get ());
281
282 if (bfd != nullptr)
283 bfd_close (bfd);
284 return;
285 }
286 /* Ensure that the bfd will be closed when core_bfd is closed.
287 This can be checked before/after a core file detach via
288 "maint info bfds". */
289 gdb_bfd_record_inclusion (core_bfd, bfd);
290 bfd_map[filename] = bfd;
291 }
292
293 /* Make new BFD section. All sections have the same name,
294 which is permitted by bfd_make_section_anyway(). */
295 asection *sec = bfd_make_section_anyway (bfd, "load");
296 if (sec == nullptr)
297 error (_("Can't make section"));
298 sec->filepos = file_ofs;
299 bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
300 bfd_set_section_size (sec, end - start);
301 bfd_set_section_vma (sec, start);
302 bfd_set_section_lma (sec, start);
303 bfd_set_section_alignment (sec, 2);
304
305 /* Set target_section fields. */
306 m_core_file_mappings.emplace_back (start, end, sec);
307
308 /* If this is a bfd of a shared library, record its soname
309 and build id. */
310 if (build_id != nullptr)
311 {
312 gdb::unique_xmalloc_ptr<char> soname
313 = gdb_bfd_read_elf_soname (bfd->filename);
314 if (soname != nullptr)
315 set_cbfd_soname_build_id (current_program_space->cbfd,
316 soname.get (), build_id);
317 }
318 });
319
320 normalize_mem_ranges (&m_core_unavailable_mappings);
321 }
322
323 /* An arbitrary identifier for the core inferior. */
324 #define CORELOW_PID 1
325
326 void
327 core_target::clear_core ()
328 {
329 if (core_bfd)
330 {
331 switch_to_no_thread (); /* Avoid confusion from thread
332 stuff. */
333 exit_inferior (current_inferior ());
334
335 /* Clear out solib state while the bfd is still open. See
336 comments in clear_solib in solib.c. */
337 clear_solib ();
338
339 current_program_space->cbfd.reset (nullptr);
340 }
341 }
342
343 /* Close the core target. */
344
345 void
346 core_target::close ()
347 {
348 clear_core ();
349
350 /* Core targets are heap-allocated (see core_target_open), so here
351 we delete ourselves. */
352 delete this;
353 }
354
355 /* Look for sections whose names start with `.reg/' so that we can
356 extract the list of threads in a core file. */
357
358 /* If ASECT is a section whose name begins with '.reg/' then extract the
359 lwpid after the '/' and create a new thread in INF.
360
361 If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the
362 same position in the parent bfd object then switch to the newly created
363 thread, otherwise, the selected thread is left unchanged. */
364
365 static void
366 add_to_thread_list (asection *asect, asection *reg_sect, inferior *inf)
367 {
368 if (!startswith (bfd_section_name (asect), ".reg/"))
369 return;
370
371 int lwpid = atoi (bfd_section_name (asect) + 5);
372 ptid_t ptid (inf->pid, lwpid);
373 thread_info *thr = add_thread (inf->process_target (), ptid);
374
375 /* Warning, Will Robinson, looking at BFD private data! */
376
377 if (reg_sect != NULL
378 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
379 switch_to_thread (thr); /* Yes, make it current. */
380 }
381
382 /* Issue a message saying we have no core to debug, if FROM_TTY. */
383
384 static void
385 maybe_say_no_core_file_now (int from_tty)
386 {
387 if (from_tty)
388 gdb_printf (_("No core file now.\n"));
389 }
390
391 /* Backward compatibility with old way of specifying core files. */
392
393 void
394 core_file_command (const char *filename, int from_tty)
395 {
396 dont_repeat (); /* Either way, seems bogus. */
397
398 if (filename == NULL)
399 {
400 if (core_bfd != NULL)
401 {
402 target_detach (current_inferior (), from_tty);
403 gdb_assert (core_bfd == NULL);
404 }
405 else
406 maybe_say_no_core_file_now (from_tty);
407 }
408 else
409 core_target_open (filename, from_tty);
410 }
411
412 /* A vmcore file is a core file created by the Linux kernel at the point of
413 a crash. Each thread in the core file represents a real CPU core, and
414 the lwpid for each thread is the pid of the process that was running on
415 that core at the moment of the crash.
416
417 However, not every CPU core will have been running a process, some cores
418 will be idle. For these idle cores the CPU writes an lwpid of 0. And
419 of course, multiple cores might be idle, so there could be multiple
420 threads with an lwpid of 0.
421
422 The problem is GDB doesn't really like threads with an lwpid of 0; GDB
423 presents such a thread as a process rather than a thread. And GDB
424 certainly doesn't like multiple threads having the same lwpid, each time
425 a new thread is seen with the same lwpid the earlier thread (with the
426 same lwpid) will be deleted.
427
428 This function addresses both of these problems by assigning a fake lwpid
429 to any thread with an lwpid of 0.
430
431 GDB finds the lwpid information by looking at the bfd section names
432 which include the lwpid, e.g. .reg/NN where NN is the lwpid. This
433 function looks though all the section names looking for sections named
434 .reg/NN. If any sections are found where NN == 0, then we assign a new
435 unique value of NN. Then, in a second pass, any sections ending /0 are
436 assigned their new number.
437
438 Remember, a core file may contain multiple register sections for
439 different register sets, but the sets are always grouped by thread, so
440 we can figure out which registers should be assigned the same new
441 lwpid. For example, consider a core file containing:
442
443 .reg/0, .reg2/0, .reg/0, .reg2/0
444
445 This represents two threads, each thread contains a .reg and .reg2
446 register set. The .reg represents the start of each thread. After
447 renaming the sections will now look like this:
448
449 .reg/1, .reg2/1, .reg/2, .reg2/2
450
451 After calling this function the rest of the core file handling code can
452 treat this core file just like any other core file. */
453
454 static void
455 rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
456 {
457 /* Map from the bfd section to its lwpid (the /NN number). */
458 std::vector<std::pair<asection *, int>> sections_and_lwpids;
459
460 /* The set of all /NN numbers found. Needed so we can easily find unused
461 numbers in the case that we need to rename some sections. */
462 std::unordered_set<int> all_lwpids;
463
464 /* A count of how many sections called .reg/0 we have found. */
465 unsigned zero_lwpid_count = 0;
466
467 /* Look for all the .reg sections. Record the section object and the
468 lwpid which is extracted from the section name. Spot if any have an
469 lwpid of zero. */
470 for (asection *sect : gdb_bfd_sections (core_bfd))
471 {
472 if (startswith (bfd_section_name (sect), ".reg/"))
473 {
474 int lwpid = atoi (bfd_section_name (sect) + 5);
475 sections_and_lwpids.emplace_back (sect, lwpid);
476 all_lwpids.insert (lwpid);
477 if (lwpid == 0)
478 zero_lwpid_count++;
479 }
480 }
481
482 /* If every ".reg/NN" section has a non-zero lwpid then we don't need to
483 do any renaming. */
484 if (zero_lwpid_count == 0)
485 return;
486
487 /* Assign a new number to any .reg sections with an lwpid of 0. */
488 int new_lwpid = 1;
489 for (auto &sect_and_lwpid : sections_and_lwpids)
490 if (sect_and_lwpid.second == 0)
491 {
492 while (all_lwpids.find (new_lwpid) != all_lwpids.end ())
493 new_lwpid++;
494 sect_and_lwpid.second = new_lwpid;
495 new_lwpid++;
496 }
497
498 /* Now update the names of any sections with an lwpid of 0. This is
499 more than just the .reg sections we originally found. */
500 std::string replacement_lwpid_str;
501 auto iter = sections_and_lwpids.begin ();
502 int replacement_lwpid = 0;
503 for (asection *sect : gdb_bfd_sections (core_bfd))
504 {
505 if (iter != sections_and_lwpids.end () && sect == iter->first)
506 {
507 gdb_assert (startswith (bfd_section_name (sect), ".reg/"));
508
509 int lwpid = atoi (bfd_section_name (sect) + 5);
510 if (lwpid == iter->second)
511 {
512 /* This section was not given a new number. */
513 gdb_assert (lwpid != 0);
514 replacement_lwpid = 0;
515 }
516 else
517 {
518 replacement_lwpid = iter->second;
519 ptid_t ptid (inf->pid, replacement_lwpid);
520 if (!replacement_lwpid_str.empty ())
521 replacement_lwpid_str += ", ";
522 replacement_lwpid_str += target_pid_to_str (ptid);
523 }
524
525 iter++;
526 }
527
528 if (replacement_lwpid != 0)
529 {
530 const char *name = bfd_section_name (sect);
531 size_t len = strlen (name);
532
533 if (strncmp (name + len - 2, "/0", 2) == 0)
534 {
535 /* This section needs a new name. */
536 std::string name_str
537 = string_printf ("%.*s/%d",
538 static_cast<int> (len - 2),
539 name, replacement_lwpid);
540 char *name_buf
541 = static_cast<char *> (bfd_alloc (abfd, name_str.size () + 1));
542 if (name_buf == nullptr)
543 error (_("failed to allocate space for section name '%s'"),
544 name_str.c_str ());
545 memcpy (name_buf, name_str.c_str(), name_str.size () + 1);
546 bfd_rename_section (sect, name_buf);
547 }
548 }
549 }
550
551 if (zero_lwpid_count == 1)
552 warning (_("found thread with pid 0, assigned replacement Target Id: %s"),
553 replacement_lwpid_str.c_str ());
554 else
555 warning (_("found threads with pid 0, assigned replacement Target Ids: %s"),
556 replacement_lwpid_str.c_str ());
557 }
558
559 /* Locate (and load) an executable file (and symbols) given the core file
560 BFD ABFD. */
561
562 static void
563 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
564 {
565 const bfd_build_id *build_id = build_id_bfd_get (abfd);
566 if (build_id == nullptr)
567 return;
568
569 gdb_bfd_ref_ptr execbfd
570 = build_id_to_exec_bfd (build_id->size, build_id->data);
571
572 if (execbfd == nullptr)
573 {
574 /* Attempt to query debuginfod for the executable. */
575 gdb::unique_xmalloc_ptr<char> execpath;
576 scoped_fd fd = debuginfod_exec_query (build_id->data, build_id->size,
577 abfd->filename, &execpath);
578
579 if (fd.get () >= 0)
580 {
581 execbfd = gdb_bfd_open (execpath.get (), gnutarget);
582
583 if (execbfd == nullptr)
584 warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"),
585 execpath.get (),
586 gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
587 else if (!build_id_verify (execbfd.get (), build_id->size,
588 build_id->data))
589 execbfd.reset (nullptr);
590 }
591 }
592
593 if (execbfd != nullptr)
594 {
595 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
596 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
597 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
598 }
599 }
600
601 /* See gdbcore.h. */
602
603 void
604 core_target_open (const char *arg, int from_tty)
605 {
606 const char *p;
607 int siggy;
608 int scratch_chan;
609 int flags;
610
611 target_preopen (from_tty);
612 if (!arg)
613 {
614 if (core_bfd)
615 error (_("No core file specified. (Use `detach' "
616 "to stop debugging a core file.)"));
617 else
618 error (_("No core file specified."));
619 }
620
621 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
622 if (strlen (filename.get ()) != 0
623 && !IS_ABSOLUTE_PATH (filename.get ()))
624 filename = make_unique_xstrdup (gdb_abspath (filename.get ()).c_str ());
625
626 flags = O_BINARY | O_LARGEFILE;
627 if (write_files)
628 flags |= O_RDWR;
629 else
630 flags |= O_RDONLY;
631 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0).release ();
632 if (scratch_chan < 0)
633 perror_with_name (filename.get ());
634
635 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
636 write_files ? FOPEN_RUB : FOPEN_RB,
637 scratch_chan));
638 if (temp_bfd == NULL)
639 perror_with_name (filename.get ());
640
641 if (!bfd_check_format (temp_bfd.get (), bfd_core))
642 {
643 /* Do it after the err msg */
644 /* FIXME: should be checking for errors from bfd_close (for one
645 thing, on error it does not free all the storage associated
646 with the bfd). */
647 error (_("\"%s\" is not a core dump: %s"),
648 filename.get (), bfd_errmsg (bfd_get_error ()));
649 }
650
651 current_program_space->cbfd = std::move (temp_bfd);
652
653 core_target *target = new core_target ();
654
655 /* Own the target until it is successfully pushed. */
656 target_ops_up target_holder (target);
657
658 validate_files ();
659
660 /* If we have no exec file, try to set the architecture from the
661 core file. We don't do this unconditionally since an exec file
662 typically contains more information that helps us determine the
663 architecture than a core file. */
664 if (!current_program_space->exec_bfd ())
665 set_gdbarch_from_file (core_bfd);
666
667 current_inferior ()->push_target (std::move (target_holder));
668
669 switch_to_no_thread ();
670
671 /* Need to flush the register cache (and the frame cache) from a
672 previous debug session. If inferior_ptid ends up the same as the
673 last debug session --- e.g., b foo; run; gcore core1; step; gcore
674 core2; core core1; core core2 --- then there's potential for
675 get_current_regcache to return the cached regcache of the
676 previous session, and the frame cache being stale. */
677 registers_changed ();
678
679 /* Find (or fake) the pid for the process in this core file, and
680 initialise the current inferior with that pid. */
681 bool fake_pid_p = false;
682 int pid = bfd_core_file_pid (core_bfd);
683 if (pid == 0)
684 {
685 fake_pid_p = true;
686 pid = CORELOW_PID;
687 }
688
689 inferior *inf = current_inferior ();
690 gdb_assert (inf->pid == 0);
691 inferior_appeared (inf, pid);
692 inf->fake_pid_p = fake_pid_p;
693
694 /* Rename any .reg/0 sections, giving them each a fake lwpid. */
695 rename_vmcore_idle_reg_sections (core_bfd, inf);
696
697 /* Build up thread list from BFD sections, and possibly set the
698 current thread to the .reg/NN section matching the .reg
699 section. */
700 asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
701 for (asection *sect : gdb_bfd_sections (core_bfd))
702 add_to_thread_list (sect, reg_sect, inf);
703
704 if (inferior_ptid == null_ptid)
705 {
706 /* Either we found no .reg/NN section, and hence we have a
707 non-threaded core (single-threaded, from gdb's perspective),
708 or for some reason add_to_thread_list couldn't determine
709 which was the "main" thread. The latter case shouldn't
710 usually happen, but we're dealing with input here, which can
711 always be broken in different ways. */
712 thread_info *thread = first_thread_of_inferior (inf);
713
714 if (thread == NULL)
715 thread = add_thread_silent (target, ptid_t (CORELOW_PID));
716
717 switch_to_thread (thread);
718 }
719
720 if (current_program_space->exec_bfd () == nullptr)
721 locate_exec_from_corefile_build_id (core_bfd, from_tty);
722
723 post_create_inferior (from_tty);
724
725 /* Now go through the target stack looking for threads since there
726 may be a thread_stratum target loaded on top of target core by
727 now. The layer above should claim threads found in the BFD
728 sections. */
729 try
730 {
731 target_update_thread_list ();
732 }
733
734 catch (const gdb_exception_error &except)
735 {
736 exception_print (gdb_stderr, except);
737 }
738
739 p = bfd_core_file_failing_command (core_bfd);
740 if (p)
741 gdb_printf (_("Core was generated by `%s'.\n"), p);
742
743 /* Clearing any previous state of convenience variables. */
744 clear_exit_convenience_vars ();
745
746 siggy = bfd_core_file_failing_signal (core_bfd);
747 if (siggy > 0)
748 {
749 gdbarch *core_gdbarch = target->core_gdbarch ();
750
751 /* If we don't have a CORE_GDBARCH to work with, assume a native
752 core (map gdb_signal from host signals). If we do have
753 CORE_GDBARCH to work with, but no gdb_signal_from_target
754 implementation for that gdbarch, as a fallback measure,
755 assume the host signal mapping. It'll be correct for native
756 cores, but most likely incorrect for cross-cores. */
757 enum gdb_signal sig = (core_gdbarch != NULL
758 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
759 ? gdbarch_gdb_signal_from_target (core_gdbarch,
760 siggy)
761 : gdb_signal_from_host (siggy));
762
763 gdb_printf (_("Program terminated with signal %s, %s"),
764 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
765 if (gdbarch_report_signal_info_p (core_gdbarch))
766 gdbarch_report_signal_info (core_gdbarch, current_uiout, sig);
767 gdb_printf (_(".\n"));
768
769 /* Set the value of the internal variable $_exitsignal,
770 which holds the signal uncaught by the inferior. */
771 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
772 siggy);
773 }
774
775 /* Fetch all registers from core file. */
776 target_fetch_registers (get_current_regcache (), -1);
777
778 /* Now, set up the frame cache, and print the top of stack. */
779 reinit_frame_cache ();
780 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
781
782 /* Current thread should be NUM 1 but the user does not know that.
783 If a program is single threaded gdb in general does not mention
784 anything about threads. That is why the test is >= 2. */
785 if (thread_count (target) >= 2)
786 {
787 try
788 {
789 thread_command (NULL, from_tty);
790 }
791 catch (const gdb_exception_error &except)
792 {
793 exception_print (gdb_stderr, except);
794 }
795 }
796 }
797
798 void
799 core_target::detach (inferior *inf, int from_tty)
800 {
801 /* Get rid of the core. Don't rely on core_target::close doing it,
802 because target_detach may be called with core_target's refcount > 1,
803 meaning core_target::close may not be called yet by the
804 unpush_target call below. */
805 clear_core ();
806
807 /* Note that 'this' may be dangling after this call. unpush_target
808 closes the target if the refcount reaches 0, and our close
809 implementation deletes 'this'. */
810 inf->unpush_target (this);
811
812 /* Clear the register cache and the frame cache. */
813 registers_changed ();
814 reinit_frame_cache ();
815 maybe_say_no_core_file_now (from_tty);
816 }
817
818 /* Try to retrieve registers from a section in core_bfd, and supply
819 them to REGSET.
820
821 If ptid's lwp member is zero, do the single-threaded
822 thing: look for a section named NAME. If ptid's lwp
823 member is non-zero, do the multi-threaded thing: look for a section
824 named "NAME/LWP", where LWP is the shortest ASCII decimal
825 representation of ptid's lwp member.
826
827 HUMAN_NAME is a human-readable name for the kind of registers the
828 NAME section contains, for use in error messages.
829
830 If REQUIRED is true, print an error if the core file doesn't have a
831 section by the appropriate name. Otherwise, just do nothing. */
832
833 void
834 core_target::get_core_register_section (struct regcache *regcache,
835 const struct regset *regset,
836 const char *name,
837 int section_min_size,
838 const char *human_name,
839 bool required)
840 {
841 gdb_assert (regset != nullptr);
842
843 struct bfd_section *section;
844 bfd_size_type size;
845 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
846
847 thread_section_name section_name (name, regcache->ptid ());
848
849 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
850 if (! section)
851 {
852 if (required)
853 warning (_("Couldn't find %s registers in core file."),
854 human_name);
855 return;
856 }
857
858 size = bfd_section_size (section);
859 if (size < section_min_size)
860 {
861 warning (_("Section `%s' in core file too small."),
862 section_name.c_str ());
863 return;
864 }
865 if (size != section_min_size && !variable_size_section)
866 {
867 warning (_("Unexpected size of section `%s' in core file."),
868 section_name.c_str ());
869 }
870
871 gdb::byte_vector contents (size);
872 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
873 (file_ptr) 0, size))
874 {
875 warning (_("Couldn't read %s registers from `%s' section in core file."),
876 human_name, section_name.c_str ());
877 return;
878 }
879
880 regset->supply_regset (regset, regcache, -1, contents.data (), size);
881 }
882
883 /* Data passed to gdbarch_iterate_over_regset_sections's callback. */
884 struct get_core_registers_cb_data
885 {
886 core_target *target;
887 struct regcache *regcache;
888 };
889
890 /* Callback for get_core_registers that handles a single core file
891 register note section. */
892
893 static void
894 get_core_registers_cb (const char *sect_name, int supply_size, int collect_size,
895 const struct regset *regset,
896 const char *human_name, void *cb_data)
897 {
898 gdb_assert (regset != nullptr);
899
900 auto *data = (get_core_registers_cb_data *) cb_data;
901 bool required = false;
902 bool variable_size_section = (regset->flags & REGSET_VARIABLE_SIZE);
903
904 if (!variable_size_section)
905 gdb_assert (supply_size == collect_size);
906
907 if (strcmp (sect_name, ".reg") == 0)
908 {
909 required = true;
910 if (human_name == NULL)
911 human_name = "general-purpose";
912 }
913 else if (strcmp (sect_name, ".reg2") == 0)
914 {
915 if (human_name == NULL)
916 human_name = "floating-point";
917 }
918
919 data->target->get_core_register_section (data->regcache, regset, sect_name,
920 supply_size, human_name, required);
921 }
922
923 /* Get the registers out of a core file. This is the machine-
924 independent part. Fetch_core_registers is the machine-dependent
925 part, typically implemented in the xm-file for each
926 architecture. */
927
928 /* We just get all the registers, so we don't use regno. */
929
930 void
931 core_target::fetch_registers (struct regcache *regcache, int regno)
932 {
933 if (!(m_core_gdbarch != nullptr
934 && gdbarch_iterate_over_regset_sections_p (m_core_gdbarch)))
935 {
936 gdb_printf (gdb_stderr,
937 "Can't fetch registers from this type of core file\n");
938 return;
939 }
940
941 struct gdbarch *gdbarch = regcache->arch ();
942 get_core_registers_cb_data data = { this, regcache };
943 gdbarch_iterate_over_regset_sections (gdbarch,
944 get_core_registers_cb,
945 (void *) &data, NULL);
946
947 /* Mark all registers not found in the core as unavailable. */
948 for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
949 if (regcache->get_register_status (i) == REG_UNKNOWN)
950 regcache->raw_supply (i, NULL);
951 }
952
953 void
954 core_target::files_info ()
955 {
956 print_section_info (&m_core_section_table, core_bfd);
957 }
958 \f
959 /* Helper method for core_target::xfer_partial. */
960
961 enum target_xfer_status
962 core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
963 const gdb_byte *writebuf,
964 ULONGEST offset, ULONGEST len,
965 ULONGEST *xfered_len)
966 {
967 enum target_xfer_status xfer_status;
968
969 xfer_status = (section_table_xfer_memory_partial
970 (readbuf, writebuf,
971 offset, len, xfered_len,
972 m_core_file_mappings));
973
974 if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
975 return xfer_status;
976
977 /* There are instances - e.g. when debugging within a docker
978 container using the AUFS storage driver - where the pathnames
979 obtained from the note section are incorrect. Despite the path
980 being wrong, just knowing the start and end addresses of the
981 mappings is still useful; we can attempt an access of the file
982 stratum constrained to the address ranges corresponding to the
983 unavailable mappings. */
984
985 ULONGEST memaddr = offset;
986 ULONGEST memend = offset + len;
987
988 for (const auto &mr : m_core_unavailable_mappings)
989 {
990 if (address_in_mem_range (memaddr, &mr))
991 {
992 if (!address_in_mem_range (memend, &mr))
993 len = mr.start + mr.length - memaddr;
994
995 xfer_status = this->beneath ()->xfer_partial (TARGET_OBJECT_MEMORY,
996 NULL,
997 readbuf,
998 writebuf,
999 offset,
1000 len,
1001 xfered_len);
1002 break;
1003 }
1004 }
1005
1006 return xfer_status;
1007 }
1008
1009 enum target_xfer_status
1010 core_target::xfer_partial (enum target_object object, const char *annex,
1011 gdb_byte *readbuf, const gdb_byte *writebuf,
1012 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1013 {
1014 switch (object)
1015 {
1016 case TARGET_OBJECT_MEMORY:
1017 {
1018 enum target_xfer_status xfer_status;
1019
1020 /* Try accessing memory contents from core file data,
1021 restricting consideration to those sections for which
1022 the BFD section flag SEC_HAS_CONTENTS is set. */
1023 auto has_contents_cb = [] (const struct target_section *s)
1024 {
1025 return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
1026 };
1027 xfer_status = section_table_xfer_memory_partial
1028 (readbuf, writebuf,
1029 offset, len, xfered_len,
1030 m_core_section_table,
1031 has_contents_cb);
1032 if (xfer_status == TARGET_XFER_OK)
1033 return TARGET_XFER_OK;
1034
1035 /* Check file backed mappings. If they're available, use
1036 core file provided mappings (e.g. from .note.linuxcore.file
1037 or the like) as this should provide a more accurate
1038 result. If not, check the stratum beneath us, which should
1039 be the file stratum.
1040
1041 We also check unavailable mappings due to Docker/AUFS driver
1042 issues. */
1043 if (!m_core_file_mappings.empty ()
1044 || !m_core_unavailable_mappings.empty ())
1045 {
1046 xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
1047 len, xfered_len);
1048 }
1049 else
1050 xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
1051 writebuf, offset, len,
1052 xfered_len);
1053 if (xfer_status == TARGET_XFER_OK)
1054 return TARGET_XFER_OK;
1055
1056 /* Finally, attempt to access data in core file sections with
1057 no contents. These will typically read as all zero. */
1058 auto no_contents_cb = [&] (const struct target_section *s)
1059 {
1060 return !has_contents_cb (s);
1061 };
1062 xfer_status = section_table_xfer_memory_partial
1063 (readbuf, writebuf,
1064 offset, len, xfered_len,
1065 m_core_section_table,
1066 no_contents_cb);
1067
1068 return xfer_status;
1069 }
1070 case TARGET_OBJECT_AUXV:
1071 if (readbuf)
1072 {
1073 /* When the aux vector is stored in core file, BFD
1074 represents this with a fake section called ".auxv". */
1075
1076 struct bfd_section *section;
1077 bfd_size_type size;
1078
1079 section = bfd_get_section_by_name (core_bfd, ".auxv");
1080 if (section == NULL)
1081 return TARGET_XFER_E_IO;
1082
1083 size = bfd_section_size (section);
1084 if (offset >= size)
1085 return TARGET_XFER_EOF;
1086 size -= offset;
1087 if (size > len)
1088 size = len;
1089
1090 if (size == 0)
1091 return TARGET_XFER_EOF;
1092 if (!bfd_get_section_contents (core_bfd, section, readbuf,
1093 (file_ptr) offset, size))
1094 {
1095 warning (_("Couldn't read NT_AUXV note in core file."));
1096 return TARGET_XFER_E_IO;
1097 }
1098
1099 *xfered_len = (ULONGEST) size;
1100 return TARGET_XFER_OK;
1101 }
1102 return TARGET_XFER_E_IO;
1103
1104 case TARGET_OBJECT_WCOOKIE:
1105 if (readbuf)
1106 {
1107 /* When the StackGhost cookie is stored in core file, BFD
1108 represents this with a fake section called
1109 ".wcookie". */
1110
1111 struct bfd_section *section;
1112 bfd_size_type size;
1113
1114 section = bfd_get_section_by_name (core_bfd, ".wcookie");
1115 if (section == NULL)
1116 return TARGET_XFER_E_IO;
1117
1118 size = bfd_section_size (section);
1119 if (offset >= size)
1120 return TARGET_XFER_EOF;
1121 size -= offset;
1122 if (size > len)
1123 size = len;
1124
1125 if (size == 0)
1126 return TARGET_XFER_EOF;
1127 if (!bfd_get_section_contents (core_bfd, section, readbuf,
1128 (file_ptr) offset, size))
1129 {
1130 warning (_("Couldn't read StackGhost cookie in core file."));
1131 return TARGET_XFER_E_IO;
1132 }
1133
1134 *xfered_len = (ULONGEST) size;
1135 return TARGET_XFER_OK;
1136
1137 }
1138 return TARGET_XFER_E_IO;
1139
1140 case TARGET_OBJECT_LIBRARIES:
1141 if (m_core_gdbarch != nullptr
1142 && gdbarch_core_xfer_shared_libraries_p (m_core_gdbarch))
1143 {
1144 if (writebuf)
1145 return TARGET_XFER_E_IO;
1146 else
1147 {
1148 *xfered_len = gdbarch_core_xfer_shared_libraries (m_core_gdbarch,
1149 readbuf,
1150 offset, len);
1151
1152 if (*xfered_len == 0)
1153 return TARGET_XFER_EOF;
1154 else
1155 return TARGET_XFER_OK;
1156 }
1157 }
1158 return TARGET_XFER_E_IO;
1159
1160 case TARGET_OBJECT_LIBRARIES_AIX:
1161 if (m_core_gdbarch != nullptr
1162 && gdbarch_core_xfer_shared_libraries_aix_p (m_core_gdbarch))
1163 {
1164 if (writebuf)
1165 return TARGET_XFER_E_IO;
1166 else
1167 {
1168 *xfered_len
1169 = gdbarch_core_xfer_shared_libraries_aix (m_core_gdbarch,
1170 readbuf, offset,
1171 len);
1172
1173 if (*xfered_len == 0)
1174 return TARGET_XFER_EOF;
1175 else
1176 return TARGET_XFER_OK;
1177 }
1178 }
1179 return TARGET_XFER_E_IO;
1180
1181 case TARGET_OBJECT_SIGNAL_INFO:
1182 if (readbuf)
1183 {
1184 if (m_core_gdbarch != nullptr
1185 && gdbarch_core_xfer_siginfo_p (m_core_gdbarch))
1186 {
1187 LONGEST l = gdbarch_core_xfer_siginfo (m_core_gdbarch, readbuf,
1188 offset, len);
1189
1190 if (l >= 0)
1191 {
1192 *xfered_len = l;
1193 if (l == 0)
1194 return TARGET_XFER_EOF;
1195 else
1196 return TARGET_XFER_OK;
1197 }
1198 }
1199 }
1200 return TARGET_XFER_E_IO;
1201
1202 default:
1203 return this->beneath ()->xfer_partial (object, annex, readbuf,
1204 writebuf, offset, len,
1205 xfered_len);
1206 }
1207 }
1208
1209 \f
1210
1211 /* Okay, let's be honest: threads gleaned from a core file aren't
1212 exactly lively, are they? On the other hand, if we don't claim
1213 that each & every one is alive, then we don't get any of them
1214 to appear in an "info thread" command, which is quite a useful
1215 behaviour.
1216 */
1217 bool
1218 core_target::thread_alive (ptid_t ptid)
1219 {
1220 return true;
1221 }
1222
1223 /* Ask the current architecture what it knows about this core file.
1224 That will be used, in turn, to pick a better architecture. This
1225 wrapper could be avoided if targets got a chance to specialize
1226 core_target. */
1227
1228 const struct target_desc *
1229 core_target::read_description ()
1230 {
1231 /* First check whether the target wants us to use the corefile target
1232 description notes. */
1233 if (gdbarch_use_target_description_from_corefile_notes (m_core_gdbarch,
1234 core_bfd))
1235 {
1236 /* If the core file contains a target description note then go ahead and
1237 use that. */
1238 bfd_size_type tdesc_note_size = 0;
1239 struct bfd_section *tdesc_note_section
1240 = bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
1241 if (tdesc_note_section != nullptr)
1242 tdesc_note_size = bfd_section_size (tdesc_note_section);
1243 if (tdesc_note_size > 0)
1244 {
1245 gdb::char_vector contents (tdesc_note_size + 1);
1246 if (bfd_get_section_contents (core_bfd, tdesc_note_section,
1247 contents.data (), (file_ptr) 0,
1248 tdesc_note_size))
1249 {
1250 /* Ensure we have a null terminator. */
1251 contents[tdesc_note_size] = '\0';
1252 const struct target_desc *result
1253 = string_read_description_xml (contents.data ());
1254 if (result != nullptr)
1255 return result;
1256 }
1257 }
1258 }
1259
1260 /* If the architecture provides a corefile target description hook, use
1261 it now. Even if the core file contains a target description in a note
1262 section, it is not useful for targets that can potentially have distinct
1263 descriptions for each thread. One example is AArch64's SVE/SME
1264 extensions that allow per-thread vector length changes, resulting in
1265 registers with different sizes. */
1266 if (m_core_gdbarch && gdbarch_core_read_description_p (m_core_gdbarch))
1267 {
1268 const struct target_desc *result;
1269
1270 result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
1271 if (result != nullptr)
1272 return result;
1273 }
1274
1275 return this->beneath ()->read_description ();
1276 }
1277
1278 std::string
1279 core_target::pid_to_str (ptid_t ptid)
1280 {
1281 struct inferior *inf;
1282 int pid;
1283
1284 /* The preferred way is to have a gdbarch/OS specific
1285 implementation. */
1286 if (m_core_gdbarch != nullptr
1287 && gdbarch_core_pid_to_str_p (m_core_gdbarch))
1288 return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);
1289
1290 /* Otherwise, if we don't have one, we'll just fallback to
1291 "process", with normal_pid_to_str. */
1292
1293 /* Try the LWPID field first. */
1294 pid = ptid.lwp ();
1295 if (pid != 0)
1296 return normal_pid_to_str (ptid_t (pid));
1297
1298 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
1299 only if it isn't a fake PID. */
1300 inf = find_inferior_ptid (this, ptid);
1301 if (inf != NULL && !inf->fake_pid_p)
1302 return normal_pid_to_str (ptid);
1303
1304 /* No luck. We simply don't have a valid PID to print. */
1305 return "<main task>";
1306 }
1307
1308 const char *
1309 core_target::thread_name (struct thread_info *thr)
1310 {
1311 if (m_core_gdbarch != nullptr
1312 && gdbarch_core_thread_name_p (m_core_gdbarch))
1313 return gdbarch_core_thread_name (m_core_gdbarch, thr);
1314 return NULL;
1315 }
1316
1317 bool
1318 core_target::has_memory ()
1319 {
1320 return (core_bfd != NULL);
1321 }
1322
1323 bool
1324 core_target::has_stack ()
1325 {
1326 return (core_bfd != NULL);
1327 }
1328
1329 bool
1330 core_target::has_registers ()
1331 {
1332 return (core_bfd != NULL);
1333 }
1334
1335 /* Implement the to_info_proc method. */
1336
1337 bool
1338 core_target::info_proc (const char *args, enum info_proc_what request)
1339 {
1340 struct gdbarch *gdbarch = get_current_arch ();
1341
1342 /* Since this is the core file target, call the 'core_info_proc'
1343 method on gdbarch, not 'info_proc'. */
1344 if (gdbarch_core_info_proc_p (gdbarch))
1345 gdbarch_core_info_proc (gdbarch, args, request);
1346
1347 return true;
1348 }
1349
1350 /* Implementation of the "supports_memory_tagging" target_ops method. */
1351
1352 bool
1353 core_target::supports_memory_tagging ()
1354 {
1355 /* Look for memory tag sections. If they exist, that means this core file
1356 supports memory tagging. */
1357
1358 return (bfd_get_section_by_name (core_bfd, "memtag") != nullptr);
1359 }
1360
1361 /* Implementation of the "fetch_memtags" target_ops method. */
1362
1363 bool
1364 core_target::fetch_memtags (CORE_ADDR address, size_t len,
1365 gdb::byte_vector &tags, int type)
1366 {
1367 gdbarch *gdbarch = current_inferior ()->arch ();
1368
1369 /* Make sure we have a way to decode the memory tag notes. */
1370 if (!gdbarch_decode_memtag_section_p (gdbarch))
1371 error (_("gdbarch_decode_memtag_section not implemented for this "
1372 "architecture."));
1373
1374 memtag_section_info info;
1375 info.memtag_section = nullptr;
1376
1377 while (get_next_core_memtag_section (core_bfd, info.memtag_section,
1378 address, info))
1379 {
1380 size_t adjusted_length
1381 = (address + len < info.end_address) ? len : (info.end_address - address);
1382
1383 /* Decode the memory tag note and return the tags. */
1384 gdb::byte_vector tags_read
1385 = gdbarch_decode_memtag_section (gdbarch, info.memtag_section, type,
1386 address, adjusted_length);
1387
1388 /* Transfer over the tags that have been read. */
1389 tags.insert (tags.end (), tags_read.begin (), tags_read.end ());
1390
1391 /* ADDRESS + LEN may cross the boundaries of a particular memory tag
1392 segment. Check if we need to fetch tags from a different section. */
1393 if (!tags_read.empty () && (address + len) < info.end_address)
1394 return true;
1395
1396 /* There are more tags to fetch. Update ADDRESS and LEN. */
1397 len -= (info.end_address - address);
1398 address = info.end_address;
1399 }
1400
1401 return false;
1402 }
1403
1404 /* Implementation of the "fetch_x86_xsave_layout" target_ops method. */
1405
1406 x86_xsave_layout
1407 core_target::fetch_x86_xsave_layout ()
1408 {
1409 if (m_core_gdbarch != nullptr &&
1410 gdbarch_core_read_x86_xsave_layout_p (m_core_gdbarch))
1411 {
1412 x86_xsave_layout layout;
1413 if (!gdbarch_core_read_x86_xsave_layout (m_core_gdbarch, layout))
1414 return {};
1415
1416 return layout;
1417 }
1418
1419 return {};
1420 }
1421
1422 /* Get a pointer to the current core target. If not connected to a
1423 core target, return NULL. */
1424
1425 static core_target *
1426 get_current_core_target ()
1427 {
1428 target_ops *proc_target = current_inferior ()->process_target ();
1429 return dynamic_cast<core_target *> (proc_target);
1430 }
1431
1432 /* Display file backed mappings from core file. */
1433
1434 void
1435 core_target::info_proc_mappings (struct gdbarch *gdbarch)
1436 {
1437 if (!m_core_file_mappings.empty ())
1438 {
1439 gdb_printf (_("Mapped address spaces:\n\n"));
1440 if (gdbarch_addr_bit (gdbarch) == 32)
1441 {
1442 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1443 "Start Addr",
1444 " End Addr",
1445 " Size", " Offset", "objfile");
1446 }
1447 else
1448 {
1449 gdb_printf (" %18s %18s %10s %10s %s\n",
1450 "Start Addr",
1451 " End Addr",
1452 " Size", " Offset", "objfile");
1453 }
1454 }
1455
1456 for (const target_section &tsp : m_core_file_mappings)
1457 {
1458 ULONGEST start = tsp.addr;
1459 ULONGEST end = tsp.endaddr;
1460 ULONGEST file_ofs = tsp.the_bfd_section->filepos;
1461 const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
1462
1463 if (gdbarch_addr_bit (gdbarch) == 32)
1464 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1465 paddress (gdbarch, start),
1466 paddress (gdbarch, end),
1467 hex_string (end - start),
1468 hex_string (file_ofs),
1469 filename);
1470 else
1471 gdb_printf (" %18s %18s %10s %10s %s\n",
1472 paddress (gdbarch, start),
1473 paddress (gdbarch, end),
1474 hex_string (end - start),
1475 hex_string (file_ofs),
1476 filename);
1477 }
1478 }
1479
1480 /* Implement "maintenance print core-file-backed-mappings" command.
1481
1482 If mappings are loaded, the results should be similar to the
1483 mappings shown by "info proc mappings". This command is mainly a
1484 debugging tool for GDB developers to make sure that the expected
1485 mappings are present after loading a core file. For Linux, the
1486 output provided by this command will be very similar (if not
1487 identical) to that provided by "info proc mappings". This is not
1488 necessarily the case for other OSes which might provide
1489 more/different information in the "info proc mappings" output. */
1490
1491 static void
1492 maintenance_print_core_file_backed_mappings (const char *args, int from_tty)
1493 {
1494 core_target *targ = get_current_core_target ();
1495 if (targ != nullptr)
1496 targ->info_proc_mappings (targ->core_gdbarch ());
1497 }
1498
1499 void _initialize_corelow ();
1500 void
1501 _initialize_corelow ()
1502 {
1503 add_target (core_target_info, core_target_open, filename_completer);
1504 add_cmd ("core-file-backed-mappings", class_maintenance,
1505 maintenance_print_core_file_backed_mappings,
1506 _("Print core file's file-backed mappings."),
1507 &maintenanceprintlist);
1508 }