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 class core_target final : public target_ops
59 { to_stratum = process_stratum; }
61 const char *shortname () override
64 const char *longname () override
65 { return _("Local core dump file"); }
67 const char *doc () override
70 Use a core file as a target. Specify the filename of the core file.");
73 void open (const char *, int) override;
74 void close () override;
75 void detach (inferior *, int) override;
76 void fetch_registers (struct regcache *, int) override;
78 enum target_xfer_status xfer_partial (enum target_object object,
81 const gdb_byte *writebuf,
82 ULONGEST offset, ULONGEST len,
83 ULONGEST *xfered_len) override;
84 void files_info () override;
86 int thread_alive (ptid_t ptid) override;
87 const struct target_desc *read_description () override;
89 const char *pid_to_str (ptid_t) override;
91 const char *thread_name (struct thread_info *) override;
93 int has_memory () override;
94 int has_stack () override;
95 int has_registers () override;
96 bool info_proc (const char *, enum info_proc_what) override;
100 struct target_ops *the_core_target;
102 /* List of all available core_fns. On gdb startup, each core file
103 register reader calls deprecated_add_core_fns() to register
104 information on each core format it is prepared to read. */
106 static struct core_fns *core_file_fns = NULL;
108 /* The core_fns for a core file handler that is prepared to read the
109 core file currently open on core_bfd. */
111 static struct core_fns *core_vec = NULL;
113 /* FIXME: kettenis/20031023: Eventually this variable should
116 static struct gdbarch *core_gdbarch = NULL;
118 /* Per-core data. Currently, only the section table. Note that these
119 target sections are *not* mapped in the current address spaces' set
120 of target sections --- those should come only from pure executable
121 or shared library bfds. The core bfd sections are an
122 implementation detail of the core target, just like ptrace is for
123 unix child targets. */
124 static struct target_section_table *core_data;
126 static struct core_fns *sniff_core_bfd (bfd *);
128 static int gdb_check_format (bfd *);
130 static void core_close_cleanup (void *ignore);
132 static void add_to_thread_list (bfd *, asection *, void *);
134 static core_target core_ops;
136 /* An arbitrary identifier for the core inferior. */
137 #define CORELOW_PID 1
139 /* Link a new core_fns into the global core_file_fns list. Called on
140 gdb startup by the _initialize routine in each core file register
141 reader, to register information about each format the reader is
142 prepared to handle. */
145 deprecated_add_core_fns (struct core_fns *cf)
147 cf->next = core_file_fns;
151 /* The default function that core file handlers can use to examine a
152 core file BFD and decide whether or not to accept the job of
153 reading the core file. */
156 default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
160 result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
164 /* Walk through the list of core functions to find a set that can
165 handle the core file open on ABFD. Returns pointer to set that is
168 static struct core_fns *
169 sniff_core_bfd (bfd *abfd)
172 struct core_fns *yummy = NULL;
175 /* Don't sniff if we have support for register sets in
177 if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
180 for (cf = core_file_fns; cf != NULL; cf = cf->next)
182 if (cf->core_sniffer (cf, abfd))
190 warning (_("\"%s\": ambiguous core format, %d handlers match"),
191 bfd_get_filename (abfd), matches);
193 else if (matches == 0)
194 error (_("\"%s\": no core file handler recognizes format"),
195 bfd_get_filename (abfd));
200 /* The default is to reject every core file format we see. Either
201 BFD has to recognize it, or we have to provide a function in the
202 core file handler that recognizes it. */
205 default_check_format (bfd *abfd)
210 /* Attempt to recognize core file formats that BFD rejects. */
213 gdb_check_format (bfd *abfd)
217 for (cf = core_file_fns; cf != NULL; cf = cf->next)
219 if (cf->check_format (abfd))
227 /* Discard all vestiges of any previous core file and mark data and
228 stack spaces as empty. */
235 int pid = ptid_get_pid (inferior_ptid);
236 inferior_ptid = null_ptid; /* Avoid confusion from thread
239 exit_inferior_silent (pid);
241 /* Clear out solib state while the bfd is still open. See
242 comments in clear_solib in solib.c. */
247 xfree (core_data->sections);
252 gdb_bfd_unref (core_bfd);
260 core_close_cleanup (void *ignore)
266 core_target::close ()
271 /* Look for sections whose names start with `.reg/' so that we can
272 extract the list of threads in a core file. */
275 add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
280 asection *reg_sect = (asection *) reg_sect_arg;
282 struct inferior *inf;
284 if (!startswith (bfd_section_name (abfd, asect), ".reg/"))
287 core_tid = atoi (bfd_section_name (abfd, asect) + 5);
289 pid = bfd_core_file_pid (core_bfd);
298 inf = current_inferior ();
301 inferior_appeared (inf, pid);
302 inf->fake_pid_p = fake_pid_p;
305 ptid = ptid_build (pid, lwpid, 0);
309 /* Warning, Will Robinson, looking at BFD private data! */
312 && asect->filepos == reg_sect->filepos) /* Did we find .reg? */
313 inferior_ptid = ptid; /* Yes, make it current. */
316 /* This routine opens and sets up the core file bfd. */
319 core_target::open (const char *arg, int from_tty)
323 struct cleanup *old_chain;
327 target_preopen (from_tty);
331 error (_("No core file specified. (Use `detach' "
332 "to stop debugging a core file.)"));
334 error (_("No core file specified."));
337 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg));
338 if (!IS_ABSOLUTE_PATH (filename.get ()))
339 filename.reset (concat (current_directory, "/",
340 filename.get (), (char *) NULL));
342 flags = O_BINARY | O_LARGEFILE;
347 scratch_chan = gdb_open_cloexec (filename.get (), flags, 0);
348 if (scratch_chan < 0)
349 perror_with_name (filename.get ());
351 gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget,
352 write_files ? FOPEN_RUB : FOPEN_RB,
354 if (temp_bfd == NULL)
355 perror_with_name (filename.get ());
357 if (!bfd_check_format (temp_bfd.get (), bfd_core)
358 && !gdb_check_format (temp_bfd.get ()))
360 /* Do it after the err msg */
361 /* FIXME: should be checking for errors from bfd_close (for one
362 thing, on error it does not free all the storage associated
364 error (_("\"%s\" is not a core dump: %s"),
365 filename.get (), bfd_errmsg (bfd_get_error ()));
368 /* Looks semi-reasonable. Toss the old core file and work on the
371 unpush_target (&core_ops);
372 core_bfd = temp_bfd.release ();
373 old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
375 core_gdbarch = gdbarch_from_bfd (core_bfd);
377 /* Find a suitable core file handler to munch on core_bfd */
378 core_vec = sniff_core_bfd (core_bfd);
382 core_data = XCNEW (struct target_section_table);
384 /* Find the data section */
385 if (build_section_table (core_bfd,
386 &core_data->sections,
387 &core_data->sections_end))
388 error (_("\"%s\": Can't find sections: %s"),
389 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
391 /* If we have no exec file, try to set the architecture from the
392 core file. We don't do this unconditionally since an exec file
393 typically contains more information that helps us determine the
394 architecture than a core file. */
396 set_gdbarch_from_file (core_bfd);
398 push_target (&core_ops);
399 discard_cleanups (old_chain);
401 /* Do this before acknowledging the inferior, so if
402 post_create_inferior throws (can happen easilly if you're loading
403 a core file with the wrong exec), we aren't left with threads
404 from the previous inferior. */
407 inferior_ptid = null_ptid;
409 /* Need to flush the register cache (and the frame cache) from a
410 previous debug session. If inferior_ptid ends up the same as the
411 last debug session --- e.g., b foo; run; gcore core1; step; gcore
412 core2; core core1; core core2 --- then there's potential for
413 get_current_regcache to return the cached regcache of the
414 previous session, and the frame cache being stale. */
415 registers_changed ();
417 /* Build up thread list from BFD sections, and possibly set the
418 current thread to the .reg/NN section matching the .reg
420 bfd_map_over_sections (core_bfd, add_to_thread_list,
421 bfd_get_section_by_name (core_bfd, ".reg"));
423 if (ptid_equal (inferior_ptid, null_ptid))
425 /* Either we found no .reg/NN section, and hence we have a
426 non-threaded core (single-threaded, from gdb's perspective),
427 or for some reason add_to_thread_list couldn't determine
428 which was the "main" thread. The latter case shouldn't
429 usually happen, but we're dealing with input here, which can
430 always be broken in different ways. */
431 struct thread_info *thread = first_thread_of_process (-1);
435 inferior_appeared (current_inferior (), CORELOW_PID);
436 inferior_ptid = pid_to_ptid (CORELOW_PID);
437 add_thread_silent (inferior_ptid);
440 switch_to_thread (thread->ptid);
443 post_create_inferior (&core_ops, from_tty);
445 /* Now go through the target stack looking for threads since there
446 may be a thread_stratum target loaded on top of target core by
447 now. The layer above should claim threads found in the BFD
451 target_update_thread_list ();
454 CATCH (except, RETURN_MASK_ERROR)
456 exception_print (gdb_stderr, except);
460 p = bfd_core_file_failing_command (core_bfd);
462 printf_filtered (_("Core was generated by `%s'.\n"), p);
464 /* Clearing any previous state of convenience variables. */
465 clear_exit_convenience_vars ();
467 siggy = bfd_core_file_failing_signal (core_bfd);
470 /* If we don't have a CORE_GDBARCH to work with, assume a native
471 core (map gdb_signal from host signals). If we do have
472 CORE_GDBARCH to work with, but no gdb_signal_from_target
473 implementation for that gdbarch, as a fallback measure,
474 assume the host signal mapping. It'll be correct for native
475 cores, but most likely incorrect for cross-cores. */
476 enum gdb_signal sig = (core_gdbarch != NULL
477 && gdbarch_gdb_signal_from_target_p (core_gdbarch)
478 ? gdbarch_gdb_signal_from_target (core_gdbarch,
480 : gdb_signal_from_host (siggy));
482 printf_filtered (_("Program terminated with signal %s, %s.\n"),
483 gdb_signal_to_name (sig), gdb_signal_to_string (sig));
485 /* Set the value of the internal variable $_exitsignal,
486 which holds the signal uncaught by the inferior. */
487 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
491 /* Fetch all registers from core file. */
492 target_fetch_registers (get_current_regcache (), -1);
494 /* Now, set up the frame cache, and print the top of stack. */
495 reinit_frame_cache ();
496 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
498 /* Current thread should be NUM 1 but the user does not know that.
499 If a program is single threaded gdb in general does not mention
500 anything about threads. That is why the test is >= 2. */
501 if (thread_count () >= 2)
505 thread_command (NULL, from_tty);
507 CATCH (except, RETURN_MASK_ERROR)
509 exception_print (gdb_stderr, except);
516 core_target::detach (inferior *inf, int from_tty)
518 unpush_target (this);
519 reinit_frame_cache ();
521 printf_filtered (_("No core file now.\n"));
524 /* Try to retrieve registers from a section in core_bfd, and supply
525 them to core_vec->core_read_registers, as the register set numbered
528 If ptid's lwp member is zero, do the single-threaded
529 thing: look for a section named NAME. If ptid's lwp
530 member is non-zero, do the multi-threaded thing: look for a section
531 named "NAME/LWP", where LWP is the shortest ASCII decimal
532 representation of ptid's lwp member.
534 HUMAN_NAME is a human-readable name for the kind of registers the
535 NAME section contains, for use in error messages.
537 If REQUIRED is non-zero, print an error if the core file doesn't
538 have a section by the appropriate name. Otherwise, just do
542 get_core_register_section (struct regcache *regcache,
543 const struct regset *regset,
547 const char *human_name,
550 struct bfd_section *section;
553 bool variable_size_section = (regset != NULL
554 && regset->flags & REGSET_VARIABLE_SIZE);
556 thread_section_name section_name (name, regcache->ptid ());
558 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
562 warning (_("Couldn't find %s registers in core file."),
567 size = bfd_section_size (core_bfd, section);
570 warning (_("Section `%s' in core file too small."),
571 section_name.c_str ());
574 if (size != min_size && !variable_size_section)
576 warning (_("Unexpected size of section `%s' in core file."),
577 section_name.c_str ());
580 contents = (char *) alloca (size);
581 if (! bfd_get_section_contents (core_bfd, section, contents,
584 warning (_("Couldn't read %s registers from `%s' section in core file."),
585 human_name, section_name.c_str ());
591 regset->supply_regset (regset, regcache, -1, contents, size);
595 gdb_assert (core_vec);
596 core_vec->core_read_registers (regcache, contents, size, which,
598 bfd_section_vma (core_bfd, section)));
601 /* Callback for get_core_registers that handles a single core file
602 register note section. */
605 get_core_registers_cb (const char *sect_name, int size,
606 const struct regset *regset,
607 const char *human_name, void *cb_data)
609 struct regcache *regcache = (struct regcache *) cb_data;
612 if (strcmp (sect_name, ".reg") == 0)
615 if (human_name == NULL)
616 human_name = "general-purpose";
618 else if (strcmp (sect_name, ".reg2") == 0)
620 if (human_name == NULL)
621 human_name = "floating-point";
624 /* The 'which' parameter is only used when no regset is provided.
625 Thus we just set it to -1. */
626 get_core_register_section (regcache, regset, sect_name,
627 size, -1, human_name, required);
630 /* Get the registers out of a core file. This is the machine-
631 independent part. Fetch_core_registers is the machine-dependent
632 part, typically implemented in the xm-file for each
635 /* We just get all the registers, so we don't use regno. */
638 core_target::fetch_registers (struct regcache *regcache, int regno)
641 struct gdbarch *gdbarch;
643 if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
644 && (core_vec == NULL || core_vec->core_read_registers == NULL))
646 fprintf_filtered (gdb_stderr,
647 "Can't fetch registers from this type of core file\n");
651 gdbarch = regcache->arch ();
652 if (gdbarch_iterate_over_regset_sections_p (gdbarch))
653 gdbarch_iterate_over_regset_sections (gdbarch,
654 get_core_registers_cb,
655 (void *) regcache, NULL);
658 get_core_register_section (regcache, NULL,
659 ".reg", 0, 0, "general-purpose", 1);
660 get_core_register_section (regcache, NULL,
661 ".reg2", 0, 2, "floating-point", 0);
664 /* Mark all registers not found in the core as unavailable. */
665 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
666 if (regcache_register_status (regcache, i) == REG_UNKNOWN)
667 regcache_raw_supply (regcache, i, NULL);
671 core_target::files_info ()
673 print_section_info (core_data, core_bfd);
686 add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
688 struct spuid_list *list = (struct spuid_list *) list_p;
689 enum bfd_endian byte_order
690 = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
693 sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
697 if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
699 store_unsigned_integer (list->buf + list->pos - list->offset,
706 enum target_xfer_status
707 core_target::xfer_partial (enum target_object object, const char *annex,
708 gdb_byte *readbuf, const gdb_byte *writebuf,
709 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
713 case TARGET_OBJECT_MEMORY:
714 return section_table_xfer_memory_partial (readbuf, writebuf,
715 offset, len, xfered_len,
717 core_data->sections_end,
720 case TARGET_OBJECT_AUXV:
723 /* When the aux vector is stored in core file, BFD
724 represents this with a fake section called ".auxv". */
726 struct bfd_section *section;
729 section = bfd_get_section_by_name (core_bfd, ".auxv");
731 return TARGET_XFER_E_IO;
733 size = bfd_section_size (core_bfd, section);
735 return TARGET_XFER_EOF;
741 return TARGET_XFER_EOF;
742 if (!bfd_get_section_contents (core_bfd, section, readbuf,
743 (file_ptr) offset, size))
745 warning (_("Couldn't read NT_AUXV note in core file."));
746 return TARGET_XFER_E_IO;
749 *xfered_len = (ULONGEST) size;
750 return TARGET_XFER_OK;
752 return TARGET_XFER_E_IO;
754 case TARGET_OBJECT_WCOOKIE:
757 /* When the StackGhost cookie is stored in core file, BFD
758 represents this with a fake section called
761 struct bfd_section *section;
764 section = bfd_get_section_by_name (core_bfd, ".wcookie");
766 return TARGET_XFER_E_IO;
768 size = bfd_section_size (core_bfd, section);
770 return TARGET_XFER_EOF;
776 return TARGET_XFER_EOF;
777 if (!bfd_get_section_contents (core_bfd, section, readbuf,
778 (file_ptr) offset, size))
780 warning (_("Couldn't read StackGhost cookie in core file."));
781 return TARGET_XFER_E_IO;
784 *xfered_len = (ULONGEST) size;
785 return TARGET_XFER_OK;
788 return TARGET_XFER_E_IO;
790 case TARGET_OBJECT_LIBRARIES:
792 && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
795 return TARGET_XFER_E_IO;
798 *xfered_len = gdbarch_core_xfer_shared_libraries (core_gdbarch,
802 if (*xfered_len == 0)
803 return TARGET_XFER_EOF;
805 return TARGET_XFER_OK;
810 case TARGET_OBJECT_LIBRARIES_AIX:
812 && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch))
815 return TARGET_XFER_E_IO;
819 = gdbarch_core_xfer_shared_libraries_aix (core_gdbarch,
823 if (*xfered_len == 0)
824 return TARGET_XFER_EOF;
826 return TARGET_XFER_OK;
831 case TARGET_OBJECT_SPU:
832 if (readbuf && annex)
834 /* When the SPU contexts are stored in a core file, BFD
835 represents this with a fake section called
838 struct bfd_section *section;
840 char sectionstr[100];
842 xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
844 section = bfd_get_section_by_name (core_bfd, sectionstr);
846 return TARGET_XFER_E_IO;
848 size = bfd_section_size (core_bfd, section);
850 return TARGET_XFER_EOF;
856 return TARGET_XFER_EOF;
857 if (!bfd_get_section_contents (core_bfd, section, readbuf,
858 (file_ptr) offset, size))
860 warning (_("Couldn't read SPU section in core file."));
861 return TARGET_XFER_E_IO;
864 *xfered_len = (ULONGEST) size;
865 return TARGET_XFER_OK;
869 /* NULL annex requests list of all present spuids. */
870 struct spuid_list list;
873 list.offset = offset;
877 bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
879 if (list.written == 0)
880 return TARGET_XFER_EOF;
883 *xfered_len = (ULONGEST) list.written;
884 return TARGET_XFER_OK;
887 return TARGET_XFER_E_IO;
889 case TARGET_OBJECT_SIGNAL_INFO:
893 && gdbarch_core_xfer_siginfo_p (core_gdbarch))
895 LONGEST l = gdbarch_core_xfer_siginfo (core_gdbarch, readbuf,
902 return TARGET_XFER_EOF;
904 return TARGET_XFER_OK;
908 return TARGET_XFER_E_IO;
911 return this->beneath->xfer_partial (object, annex, readbuf,
912 writebuf, offset, len,
919 /* Okay, let's be honest: threads gleaned from a core file aren't
920 exactly lively, are they? On the other hand, if we don't claim
921 that each & every one is alive, then we don't get any of them
922 to appear in an "info thread" command, which is quite a useful
926 core_target::thread_alive (ptid_t ptid)
931 /* Ask the current architecture what it knows about this core file.
932 That will be used, in turn, to pick a better architecture. This
933 wrapper could be avoided if targets got a chance to specialize
936 const struct target_desc *
937 core_target::read_description ()
939 if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
941 const struct target_desc *result;
943 result = gdbarch_core_read_description (core_gdbarch, this, core_bfd);
948 return this->beneath->read_description ();
952 core_target::pid_to_str (ptid_t ptid)
955 struct inferior *inf;
958 /* The preferred way is to have a gdbarch/OS specific
961 && gdbarch_core_pid_to_str_p (core_gdbarch))
962 return gdbarch_core_pid_to_str (core_gdbarch, ptid);
964 /* Otherwise, if we don't have one, we'll just fallback to
965 "process", with normal_pid_to_str. */
967 /* Try the LWPID field first. */
968 pid = ptid_get_lwp (ptid);
970 return normal_pid_to_str (pid_to_ptid (pid));
972 /* Otherwise, this isn't a "threaded" core -- use the PID field, but
973 only if it isn't a fake PID. */
974 inf = find_inferior_ptid (ptid);
975 if (inf != NULL && !inf->fake_pid_p)
976 return normal_pid_to_str (ptid);
978 /* No luck. We simply don't have a valid PID to print. */
979 xsnprintf (buf, sizeof buf, "<main task>");
984 core_target::thread_name (struct thread_info *thr)
987 && gdbarch_core_thread_name_p (core_gdbarch))
988 return gdbarch_core_thread_name (core_gdbarch, thr);
993 core_target::has_memory ()
995 return (core_bfd != NULL);
999 core_target::has_stack ()
1001 return (core_bfd != NULL);
1005 core_target::has_registers ()
1007 return (core_bfd != NULL);
1010 /* Implement the to_info_proc method. */
1013 core_target::info_proc (const char *args, enum info_proc_what request)
1015 struct gdbarch *gdbarch = get_current_arch ();
1017 /* Since this is the core file target, call the 'core_info_proc'
1018 method on gdbarch, not 'info_proc'. */
1019 if (gdbarch_core_info_proc_p (gdbarch))
1020 gdbarch_core_info_proc (gdbarch, args, request);
1026 _initialize_corelow (void)
1028 if (the_core_target != NULL)
1029 internal_error (__FILE__, __LINE__,
1030 _("core target already exists (\"%s\")."),
1031 the_core_target->longname ());
1032 the_core_target = &core_ops;
1034 add_target_with_completer (&core_ops, filename_completer);