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