1 /* Core dump and executable file functions below target vector, for GDB.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
24 #ifdef HAVE_SYS_FILE_H
25 #include <sys/file.h> /* needed for F_OK and friends */
27 #include "frame.h" /* required by inferior.h */
35 #include "gdbthread.h"
40 #include "readline/readline.h"
42 #include "filenames.h"
43 #include "progspace.h"
46 #include "completer.h"
47 #include "filestuff.h"
53 /* The core file target. */
55 static const target_info core_target_info = {
57 N_("Local core dump file"),
58 N_("Use a core file as a target. Specify the filename of the core file.")
61 class core_target final : public target_ops
65 { to_stratum = process_stratum; }
67 const target_info &info () const override
68 { return core_target_info; }
70 void close () override;
71 void detach (inferior *, int) override;
72 void fetch_registers (struct regcache *, int) override;
74 enum target_xfer_status xfer_partial (enum target_object object,
77 const gdb_byte *writebuf,
78 ULONGEST offset, ULONGEST len,
79 ULONGEST *xfered_len) override;
80 void files_info () override;
82 bool thread_alive (ptid_t ptid) override;
83 const struct target_desc *read_description () override;
85 const char *pid_to_str (ptid_t) override;
87 const char *thread_name (struct thread_info *) override;
89 bool has_memory () override;
90 bool has_stack () override;
91 bool has_registers () override;
92 bool info_proc (const char *, enum info_proc_what) override;
95 /* List of all available core_fns. On gdb startup, each core file
96 register reader calls deprecated_add_core_fns() to register
97 information on each core format it is prepared to read. */
99 static struct core_fns *core_file_fns = NULL;
101 /* The core_fns for a core file handler that is prepared to read the
102 core file currently open on core_bfd. */
104 static struct core_fns *core_vec = NULL;
106 /* FIXME: kettenis/20031023: Eventually this variable should
109 static struct gdbarch *core_gdbarch = NULL;
111 /* Per-core data. Currently, only the section table. Note that these
112 target sections are *not* mapped in the current address spaces' set
113 of target sections --- those should come only from pure executable
114 or shared library bfds. The core bfd sections are an
115 implementation detail of the core target, just like ptrace is for
116 unix child targets. */
117 static struct target_section_table *core_data;
119 static struct core_fns *sniff_core_bfd (bfd *);
121 static int gdb_check_format (bfd *);
123 static void core_close_cleanup (void *ignore);
125 static void add_to_thread_list (bfd *, asection *, void *);
127 static core_target core_ops;
129 /* An arbitrary identifier for the core inferior. */
130 #define CORELOW_PID 1
132 /* Link a new core_fns into the global core_file_fns list. Called on
133 gdb startup by the _initialize routine in each core file register
134 reader, to register information about each format the reader is
135 prepared to handle. */
138 deprecated_add_core_fns (struct core_fns *cf)
140 cf->next = core_file_fns;
144 /* The default function that core file handlers can use to examine a
145 core file BFD and decide whether or not to accept the job of
146 reading the core file. */
149 default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
153 result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
157 /* Walk through the list of core functions to find a set that can
158 handle the core file open on ABFD. Returns pointer to set that is
161 static struct core_fns *
162 sniff_core_bfd (bfd *abfd)
165 struct core_fns *yummy = NULL;
168 /* Don't sniff if we have support for register sets in
170 if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
173 for (cf = core_file_fns; cf != NULL; cf = cf->next)
175 if (cf->core_sniffer (cf, abfd))
183 warning (_("\"%s\": ambiguous core format, %d handlers match"),
184 bfd_get_filename (abfd), matches);
186 else if (matches == 0)
187 error (_("\"%s\": no core file handler recognizes format"),
188 bfd_get_filename (abfd));
193 /* The default is to reject every core file format we see. Either
194 BFD has to recognize it, or we have to provide a function in the
195 core file handler that recognizes it. */
198 default_check_format (bfd *abfd)
203 /* Attempt to recognize core file formats that BFD rejects. */
206 gdb_check_format (bfd *abfd)
210 for (cf = core_file_fns; cf != NULL; cf = cf->next)
212 if (cf->check_format (abfd))
220 /* Discard all vestiges of any previous core file and mark data and
221 stack spaces as empty. */
228 int pid = ptid_get_pid (inferior_ptid);
229 inferior_ptid = null_ptid; /* Avoid confusion from thread
232 exit_inferior_silent (pid);
234 /* Clear out solib state while the bfd is still open. See
235 comments in clear_solib in solib.c. */
240 xfree (core_data->sections);
245 gdb_bfd_unref (core_bfd);
253 core_close_cleanup (void *ignore)
259 core_target::close ()
264 /* Look for sections whose names start with `.reg/' so that we can
265 extract the list of threads in a core file. */
268 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
273 asection *reg_sect = (asection *) reg_sect_arg;
275 struct inferior *inf;
277 if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
280 core_tid = atoi (bfd_section_name (abfd, asect) + 5);
282 pid = bfd_core_file_pid (core_bfd);
291 inf = current_inferior ();
294 inferior_appeared (inf, pid);
295 inf->fake_pid_p = fake_pid_p;
298 ptid = ptid_build (pid, lwpid, 0);
302 /* Warning, Will Robinson, looking at BFD private data! */
305 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
306 inferior_ptid = ptid; /* Yes, make it current. */
309 /* Issue a message saying we have no core to debug, if FROM_TTY. */
312 maybe_say_no_core_file_now (int from_tty)
315 printf_filtered (_("No core file now.\n"));
318 /* Backward compatability with old way of specifying core files. */
321 core_file_command (const char *filename, int from_tty)
323 dont_repeat (); /* Either way, seems bogus. */
325 if (filename == NULL)
327 if (core_bfd != NULL)
329 target_detach (current_inferior (), from_tty);
330 gdb_assert (core_bfd == NULL);
333 maybe_say_no_core_file_now (from_tty);
336 core_target_open (filename, from_tty);
342 core_target_open (const char *arg, int from_tty)
346 struct cleanup *old_chain;
350 target_preopen (from_tty);
354 error (_("No core file specified. (Use `detach' "
355 "to stop debugging a core file.)"));
357 error (_("No core file specified."));
360 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
361 if (!IS_ABSOLUTE_PATH (filename.get ()))
362 filename.reset (concat (current_directory, "/",
363 filename.get (), (char *) NULL));
365 flags = O_BINARY | O_LARGEFILE;
370 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
371 if (scratch_chan < 0)
372 perror_with_name (filename.get ());
374 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
375 write_files ? FOPEN_RUB : FOPEN_RB,
377 if (temp_bfd == NULL)
378 perror_with_name (filename.get ());
380 if (!bfd_check_format (temp_bfd.get (), bfd_core)
381 && !gdb_check_format (temp_bfd.get ()))
383 /* Do it after the err msg */
384 /* FIXME: should be checking for errors from bfd_close (for one
385 thing, on error it does not free all the storage associated
387 error (_("\"%s\" is not a core dump: %s"),
388 filename.get (), bfd_errmsg (bfd_get_error ()));
391 /* Looks semi-reasonable. Toss the old core file and work on the
394 unpush_target (&core_ops);
395 core_bfd = temp_bfd.release ();
396 old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
398 core_gdbarch = gdbarch_from_bfd (core_bfd);
400 /* Find a suitable core file handler to munch on core_bfd */
401 core_vec = sniff_core_bfd (core_bfd);
405 core_data = XCNEW (struct target_section_table);
407 /* Find the data section */
408 if (build_section_table (core_bfd,
409 &core_data->sections,
410 &core_data->sections_end))
411 error (_("\"%s\": Can't find sections: %s"),
412 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
414 /* If we have no exec file, try to set the architecture from the
415 core file. We don't do this unconditionally since an exec file
416 typically contains more information that helps us determine the
417 architecture than a core file. */
419 set_gdbarch_from_file (core_bfd);
421 push_target (&core_ops);
422 discard_cleanups (old_chain);
424 /* Do this before acknowledging the inferior, so if
425 post_create_inferior throws (can happen easilly if you're loading
426 a core file with the wrong exec), we aren't left with threads
427 from the previous inferior. */
430 inferior_ptid = null_ptid;
432 /* Need to flush the register cache (and the frame cache) from a
433 previous debug session. If inferior_ptid ends up the same as the
434 last debug session --- e.g., b foo; run; gcore core1; step; gcore
435 core2; core core1; core core2 --- then there's potential for
436 get_current_regcache to return the cached regcache of the
437 previous session, and the frame cache being stale. */
438 registers_changed ();
440 /* Build up thread list from BFD sections, and possibly set the
441 current thread to the .reg/NN section matching the .reg
443 bfd_map_over_sections (core_bfd, add_to_thread_list,
444 bfd_get_section_by_name (core_bfd, ".reg"));
446 if (ptid_equal (inferior_ptid, null_ptid))
448 /* Either we found no .reg/NN section, and hence we have a
449 non-threaded core (single-threaded, from gdb's perspective),
450 or for some reason add_to_thread_list couldn't determine
451 which was the "main" thread. The latter case shouldn't
452 usually happen, but we're dealing with input here, which can
453 always be broken in different ways. */
454 struct thread_info *thread = first_thread_of_process (-1);
458 inferior_appeared (current_inferior (), CORELOW_PID);
459 inferior_ptid = pid_to_ptid (CORELOW_PID);
460 add_thread_silent (inferior_ptid);
463 switch_to_thread (thread->ptid);
466 post_create_inferior (&core_ops, from_tty);
468 /* Now go through the target stack looking for threads since there
469 may be a thread_stratum target loaded on top of target core by
470 now. The layer above should claim threads found in the BFD
474 target_update_thread_list ();
477 CATCH (except, RETURN_MASK_ERROR)
479 exception_print (gdb_stderr, except);
483 p = bfd_core_file_failing_command (core_bfd);
485 printf_filtered (_("Core was generated by `%s'.\n"), p);
487 /* Clearing any previous state of convenience variables. */
488 clear_exit_convenience_vars ();
490 siggy = bfd_core_file_failing_signal (core_bfd);
493 /* If we don't have a CORE_GDBARCH to work with, assume a native
494 core (map gdb_signal from host signals). If we do have
495 CORE_GDBARCH to work with, but no gdb_signal_from_target
496 implementation for that gdbarch, as a fallback measure,
497 assume the host signal mapping. It'll be correct for native
498 cores, but most likely incorrect for cross-cores. */
499 enum gdb_signal sig = (core_gdbarch != NULL
500 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
501 ? gdbarch_gdb_signal_from_target (core_gdbarch,
503 : gdb_signal_from_host (siggy));
505 printf_filtered (_("Program terminated with signal %s, %s.\n"),
506 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
508 /* Set the value of the internal variable $_exitsignal,
509 which holds the signal uncaught by the inferior. */
510 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
514 /* Fetch all registers from core file. */
515 target_fetch_registers (get_current_regcache (), -1);
517 /* Now, set up the frame cache, and print the top of stack. */
518 reinit_frame_cache ();
519 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
521 /* Current thread should be NUM 1 but the user does not know that.
522 If a program is single threaded gdb in general does not mention
523 anything about threads. That is why the test is >= 2. */
524 if (thread_count () >= 2)
528 thread_command (NULL, from_tty);
530 CATCH (except, RETURN_MASK_ERROR)
532 exception_print (gdb_stderr, except);
539 core_target::detach (inferior *inf, int from_tty)
541 unpush_target (this);
542 reinit_frame_cache ();
543 maybe_say_no_core_file_now (from_tty);
546 /* Try to retrieve registers from a section in core_bfd, and supply
547 them to core_vec->core_read_registers, as the register set numbered
550 If ptid's lwp member is zero, do the single-threaded
551 thing: look for a section named NAME. If ptid's lwp
552 member is non-zero, do the multi-threaded thing: look for a section
553 named "NAME/LWP", where LWP is the shortest ASCII decimal
554 representation of ptid's lwp member.
556 HUMAN_NAME is a human-readable name for the kind of registers the
557 NAME section contains, for use in error messages.
559 If REQUIRED is non-zero, print an error if the core file doesn't
560 have a section by the appropriate name. Otherwise, just do
564 get_core_register_section (struct regcache *regcache,
565 const struct regset *regset,
569 const char *human_name,
572 struct bfd_section *section;
575 bool variable_size_section = (regset != NULL
576 && regset->flags & REGSET_VARIABLE_SIZE);
578 thread_section_name section_name (name, regcache->ptid ());
580 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
584 warning (_("Couldn't find %s registers in core file."),
589 size = bfd_section_size (core_bfd, section);
592 warning (_("Section `%s' in core file too small."),
593 section_name.c_str ());
596 if (size != min_size && !variable_size_section)
598 warning (_("Unexpected size of section `%s' in core file."),
599 section_name.c_str ());
602 contents = (char *) alloca (size);
603 if (! bfd_get_section_contents (core_bfd, section, contents,
606 warning (_("Couldn't read %s registers from `%s' section in core file."),
607 human_name, section_name.c_str ());
613 regset->supply_regset (regset, regcache, -1, contents, size);
617 gdb_assert (core_vec);
618 core_vec->core_read_registers (regcache, contents, size, which,
620 bfd_section_vma (core_bfd, section)));
623 /* Callback for get_core_registers that handles a single core file
624 register note section. */
627 get_core_registers_cb (const char *sect_name, int size,
628 const struct regset *regset,
629 const char *human_name, void *cb_data)
631 struct regcache *regcache = (struct regcache *) cb_data;
634 if (strcmp (sect_name, ".reg") == 0)
637 if (human_name == NULL)
638 human_name = "general-purpose";
640 else if (strcmp (sect_name, ".reg2") == 0)
642 if (human_name == NULL)
643 human_name = "floating-point";
646 /* The 'which' parameter is only used when no regset is provided.
647 Thus we just set it to -1. */
648 get_core_register_section (regcache, regset, sect_name,
649 size, -1, human_name, required);
652 /* Get the registers out of a core file. This is the machine-
653 independent part. Fetch_core_registers is the machine-dependent
654 part, typically implemented in the xm-file for each
657 /* We just get all the registers, so we don't use regno. */
660 core_target::fetch_registers (struct regcache *regcache, int regno)
663 struct gdbarch *gdbarch;
665 if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
666 && (core_vec == NULL || core_vec->core_read_registers == NULL))
668 fprintf_filtered (gdb_stderr,
669 "Can't fetch registers from this type of core file\n");
673 gdbarch = regcache->arch ();
674 if (gdbarch_iterate_over_regset_sections_p (gdbarch))
675 gdbarch_iterate_over_regset_sections (gdbarch,
676 get_core_registers_cb,
677 (void *) regcache, NULL);
680 get_core_register_section (regcache, NULL,
681 ".reg", 0, 0, "general-purpose", 1);
682 get_core_register_section (regcache, NULL,
683 ".reg2", 0, 2, "floating-point", 0);
686 /* Mark all registers not found in the core as unavailable. */
687 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
688 if (regcache_register_status (regcache, i) == REG_UNKNOWN)
689 regcache_raw_supply (regcache, i, NULL);
693 core_target::files_info ()
695 print_section_info (core_data, core_bfd);
708 add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
710 struct spuid_list *list = (struct spuid_list *) list_p;
711 enum bfd_endian byte_order
712 = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
715 sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
719 if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
721 store_unsigned_integer (list->buf + list->pos - list->offset,
728 enum target_xfer_status
729 core_target::xfer_partial (enum target_object object, const char *annex,
730 gdb_byte *readbuf, const gdb_byte *writebuf,
731 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
735 case TARGET_OBJECT_MEMORY:
736 return section_table_xfer_memory_partial (readbuf, writebuf,
737 offset, len, xfered_len,
739 core_data->sections_end,
742 case TARGET_OBJECT_AUXV:
745 /* When the aux vector is stored in core file, BFD
746 represents this with a fake section called ".auxv". */
748 struct bfd_section *section;
751 section = bfd_get_section_by_name (core_bfd, ".auxv");
753 return TARGET_XFER_E_IO;
755 size = bfd_section_size (core_bfd, section);
757 return TARGET_XFER_EOF;
763 return TARGET_XFER_EOF;
764 if (!bfd_get_section_contents (core_bfd, section, readbuf,
765 (file_ptr) offset, size))
767 warning (_("Couldn't read NT_AUXV note in core file."));
768 return TARGET_XFER_E_IO;
771 *xfered_len = (ULONGEST) size;
772 return TARGET_XFER_OK;
774 return TARGET_XFER_E_IO;
776 case TARGET_OBJECT_WCOOKIE:
779 /* When the StackGhost cookie is stored in core file, BFD
780 represents this with a fake section called
783 struct bfd_section *section;
786 section = bfd_get_section_by_name (core_bfd, ".wcookie");
788 return TARGET_XFER_E_IO;
790 size = bfd_section_size (core_bfd, section);
792 return TARGET_XFER_EOF;
798 return TARGET_XFER_EOF;
799 if (!bfd_get_section_contents (core_bfd, section, readbuf,
800 (file_ptr) offset, size))
802 warning (_("Couldn't read StackGhost cookie in core file."));
803 return TARGET_XFER_E_IO;
806 *xfered_len = (ULONGEST) size;
807 return TARGET_XFER_OK;
810 return TARGET_XFER_E_IO;
812 case TARGET_OBJECT_LIBRARIES:
814 && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
817 return TARGET_XFER_E_IO;
820 *xfered_len = gdbarch_core_xfer_shared_libraries (core_gdbarch,
824 if (*xfered_len == 0)
825 return TARGET_XFER_EOF;
827 return TARGET_XFER_OK;
832 case TARGET_OBJECT_LIBRARIES_AIX:
834 && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch))
837 return TARGET_XFER_E_IO;
841 = gdbarch_core_xfer_shared_libraries_aix (core_gdbarch,
845 if (*xfered_len == 0)
846 return TARGET_XFER_EOF;
848 return TARGET_XFER_OK;
853 case TARGET_OBJECT_SPU:
854 if (readbuf && annex)
856 /* When the SPU contexts are stored in a core file, BFD
857 represents this with a fake section called
860 struct bfd_section *section;
862 char sectionstr[100];
864 xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
866 section = bfd_get_section_by_name (core_bfd, sectionstr);
868 return TARGET_XFER_E_IO;
870 size = bfd_section_size (core_bfd, section);
872 return TARGET_XFER_EOF;
878 return TARGET_XFER_EOF;
879 if (!bfd_get_section_contents (core_bfd, section, readbuf,
880 (file_ptr) offset, size))
882 warning (_("Couldn't read SPU section in core file."));
883 return TARGET_XFER_E_IO;
886 *xfered_len = (ULONGEST) size;
887 return TARGET_XFER_OK;
891 /* NULL annex requests list of all present spuids. */
892 struct spuid_list list;
895 list.offset = offset;
899 bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
901 if (list.written == 0)
902 return TARGET_XFER_EOF;
905 *xfered_len = (ULONGEST) list.written;
906 return TARGET_XFER_OK;
909 return TARGET_XFER_E_IO;
911 case TARGET_OBJECT_SIGNAL_INFO:
915 && gdbarch_core_xfer_siginfo_p (core_gdbarch))
917 LONGEST l = gdbarch_core_xfer_siginfo (core_gdbarch, readbuf,
924 return TARGET_XFER_EOF;
926 return TARGET_XFER_OK;
930 return TARGET_XFER_E_IO;
933 return this->beneath->xfer_partial (object, annex, readbuf,
934 writebuf, offset, len,
941 /* Okay, let's be honest: threads gleaned from a core file aren't
942 exactly lively, are they? On the other hand, if we don't claim
943 that each & every one is alive, then we don't get any of them
944 to appear in an "info thread" command, which is quite a useful
948 core_target::thread_alive (ptid_t ptid)
953 /* Ask the current architecture what it knows about this core file.
954 That will be used, in turn, to pick a better architecture. This
955 wrapper could be avoided if targets got a chance to specialize
958 const struct target_desc *
959 core_target::read_description ()
961 if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
963 const struct target_desc *result;
965 result = gdbarch_core_read_description (core_gdbarch, this, core_bfd);
970 return this->beneath->read_description ();
974 core_target::pid_to_str (ptid_t ptid)
977 struct inferior *inf;
980 /* The preferred way is to have a gdbarch/OS specific
983 && gdbarch_core_pid_to_str_p (core_gdbarch))
984 return gdbarch_core_pid_to_str (core_gdbarch, ptid);
986 /* Otherwise, if we don't have one, we'll just fallback to
987 "process", with normal_pid_to_str. */
989 /* Try the LWPID field first. */
990 pid = ptid_get_lwp (ptid);
992 return normal_pid_to_str (pid_to_ptid (pid));
994 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
995 only if it isn't a fake PID. */
996 inf = find_inferior_ptid (ptid);
997 if (inf != NULL && !inf->fake_pid_p)
998 return normal_pid_to_str (ptid);
1000 /* No luck. We simply don't have a valid PID to print. */
1001 xsnprintf (buf, sizeof buf, "<main task>");
1006 core_target::thread_name (struct thread_info *thr)
1009 && gdbarch_core_thread_name_p (core_gdbarch))
1010 return gdbarch_core_thread_name (core_gdbarch, thr);
1015 core_target::has_memory ()
1017 return (core_bfd != NULL);
1021 core_target::has_stack ()
1023 return (core_bfd != NULL);
1027 core_target::has_registers ()
1029 return (core_bfd != NULL);
1032 /* Implement the to_info_proc method. */
1035 core_target::info_proc (const char *args, enum info_proc_what request)
1037 struct gdbarch *gdbarch = get_current_arch ();
1039 /* Since this is the core file target, call the 'core_info_proc'
1040 method on gdbarch, not 'info_proc'. */
1041 if (gdbarch_core_info_proc_p (gdbarch))
1042 gdbarch_core_info_proc (gdbarch, args, request);
1048 _initialize_corelow (void)
1050 add_target (core_target_info, core_target_open, filename_completer);