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