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