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