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