gdb/corelow.c - gdb
Global variables defined
Data types defined
Functions defined
Macros defined
Source code
- #include "defs.h"
- #include "arch-utils.h"
- #include <signal.h>
- #include <fcntl.h>
- #ifdef HAVE_SYS_FILE_H
- #include <sys/file.h>
- #endif
- #include "frame.h"
- #include "inferior.h"
- #include "infrun.h"
- #include "symtab.h"
- #include "command.h"
- #include "bfd.h"
- #include "target.h"
- #include "gdbcore.h"
- #include "gdbthread.h"
- #include "regcache.h"
- #include "regset.h"
- #include "symfile.h"
- #include "exec.h"
- #include "readline/readline.h"
- #include "solib.h"
- #include "filenames.h"
- #include "progspace.h"
- #include "objfiles.h"
- #include "gdb_bfd.h"
- #include "completer.h"
- #include "filestuff.h"
- #ifndef O_LARGEFILE
- #define O_LARGEFILE 0
- #endif
- static struct core_fns *core_file_fns = NULL;
- static struct core_fns *core_vec = NULL;
- FIXME
- static struct gdbarch *core_gdbarch = NULL;
- static struct target_section_table *core_data;
- static void core_files_info (struct target_ops *);
- static struct core_fns *sniff_core_bfd (bfd *);
- static int gdb_check_format (bfd *);
- static void core_close (struct target_ops *self);
- static void core_close_cleanup (void *ignore);
- static void add_to_thread_list (bfd *, asection *, void *);
- static void init_core_ops (void);
- void _initialize_corelow (void);
- static struct target_ops core_ops;
- #define CORELOW_PID 1
- void
- deprecated_add_core_fns (struct core_fns *cf)
- {
- cf->next = core_file_fns;
- core_file_fns = cf;
- }
- int
- default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
- {
- int result;
- result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
- return (result);
- }
- static struct core_fns *
- sniff_core_bfd (bfd *abfd)
- {
- struct core_fns *cf;
- struct core_fns *yummy = NULL;
- int matches = 0;;
-
- if (core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
- return NULL;
- for (cf = core_file_fns; cf != NULL; cf = cf->next)
- {
- if (cf->core_sniffer (cf, abfd))
- {
- yummy = cf;
- matches++;
- }
- }
- if (matches > 1)
- {
- warning (_("\"%s\": ambiguous core format, %d handlers match"),
- bfd_get_filename (abfd), matches);
- }
- else if (matches == 0)
- error (_("\"%s\": no core file handler recognizes format"),
- bfd_get_filename (abfd));
- return (yummy);
- }
- int
- default_check_format (bfd *abfd)
- {
- return (0);
- }
- static int
- gdb_check_format (bfd *abfd)
- {
- struct core_fns *cf;
- for (cf = core_file_fns; cf != NULL; cf = cf->next)
- {
- if (cf->check_format (abfd))
- {
- return (1);
- }
- }
- return (0);
- }
- static void
- core_close (struct target_ops *self)
- {
- if (core_bfd)
- {
- int pid = ptid_get_pid (inferior_ptid);
- inferior_ptid = null_ptid;
- if (pid != 0)
- exit_inferior_silent (pid);
-
- clear_solib ();
- if (core_data)
- {
- xfree (core_data->sections);
- xfree (core_data);
- core_data = NULL;
- }
- gdb_bfd_unref (core_bfd);
- core_bfd = NULL;
- }
- core_vec = NULL;
- core_gdbarch = NULL;
- }
- static void
- core_close_cleanup (void *ignore)
- {
- core_close (NULL);
- }
- static void
- add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
- {
- ptid_t ptid;
- int core_tid;
- int pid, lwpid;
- asection *reg_sect = (asection *) reg_sect_arg;
- int fake_pid_p = 0;
- struct inferior *inf;
- if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
- return;
- core_tid = atoi (bfd_section_name (abfd, asect) + 5);
- pid = bfd_core_file_pid (core_bfd);
- if (pid == 0)
- {
- fake_pid_p = 1;
- pid = CORELOW_PID;
- }
- lwpid = core_tid;
- inf = current_inferior ();
- if (inf->pid == 0)
- {
- inferior_appeared (inf, pid);
- inf->fake_pid_p = fake_pid_p;
- }
- ptid = ptid_build (pid, lwpid, 0);
- add_thread (ptid);
- if (reg_sect != NULL
- && asect->filepos == reg_sect->filepos)
- inferior_ptid = ptid;
- }
- static void
- core_open (const char *arg, int from_tty)
- {
- const char *p;
- int siggy;
- struct cleanup *old_chain;
- char *temp;
- bfd *temp_bfd;
- int scratch_chan;
- int flags;
- volatile struct gdb_exception except;
- char *filename;
- target_preopen (from_tty);
- if (!arg)
- {
- if (core_bfd)
- error (_("No core file specified. (Use `detach' "
- "to stop debugging a core file.)"));
- else
- error (_("No core file specified."));
- }
- filename = tilde_expand (arg);
- if (!IS_ABSOLUTE_PATH (filename))
- {
- temp = concat (current_directory, "/",
- filename, (char *) NULL);
- xfree (filename);
- filename = temp;
- }
- old_chain = make_cleanup (xfree, filename);
- flags = O_BINARY | O_LARGEFILE;
- if (write_files)
- flags |= O_RDWR;
- else
- flags |= O_RDONLY;
- scratch_chan = gdb_open_cloexec (filename, flags, 0);
- if (scratch_chan < 0)
- perror_with_name (filename);
- temp_bfd = gdb_bfd_fopen (filename, gnutarget,
- write_files ? FOPEN_RUB : FOPEN_RB,
- scratch_chan);
- if (temp_bfd == NULL)
- perror_with_name (filename);
- if (!bfd_check_format (temp_bfd, bfd_core)
- && !gdb_check_format (temp_bfd))
- {
-
- FIXME
- make_cleanup_bfd_unref (temp_bfd);
- error (_("\"%s\" is not a core dump: %s"),
- filename, bfd_errmsg (bfd_get_error ()));
- }
-
- do_cleanups (old_chain);
- unpush_target (&core_ops);
- core_bfd = temp_bfd;
- old_chain = make_cleanup (core_close_cleanup, 0 );
- core_gdbarch = gdbarch_from_bfd (core_bfd);
-
- core_vec = sniff_core_bfd (core_bfd);
- validate_files ();
- core_data = XCNEW (struct target_section_table);
-
- if (build_section_table (core_bfd,
- &core_data->sections,
- &core_data->sections_end))
- error (_("\"%s\": Can't find sections: %s"),
- bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
-
- if (!exec_bfd)
- set_gdbarch_from_file (core_bfd);
- push_target (&core_ops);
- discard_cleanups (old_chain);
-
- init_thread_list ();
- inferior_ptid = null_ptid;
-
- registers_changed ();
-
- bfd_map_over_sections (core_bfd, add_to_thread_list,
- bfd_get_section_by_name (core_bfd, ".reg"));
- if (ptid_equal (inferior_ptid, null_ptid))
- {
-
- struct thread_info *thread = first_thread_of_process (-1);
- if (thread == NULL)
- {
- inferior_appeared (current_inferior (), CORELOW_PID);
- inferior_ptid = pid_to_ptid (CORELOW_PID);
- add_thread_silent (inferior_ptid);
- }
- else
- switch_to_thread (thread->ptid);
- }
- post_create_inferior (&core_ops, from_tty);
-
- TRY_CATCH (except, RETURN_MASK_ERROR)
- {
- target_update_thread_list ();
- }
- if (except.reason < 0)
- exception_print (gdb_stderr, except);
- p = bfd_core_file_failing_command (core_bfd);
- if (p)
- printf_filtered (_("Core was generated by `%s'.\n"), p);
-
- clear_exit_convenience_vars ();
- siggy = bfd_core_file_failing_signal (core_bfd);
- if (siggy > 0)
- {
-
- enum gdb_signal sig = (core_gdbarch != NULL
- && gdbarch_gdb_signal_from_target_p (core_gdbarch)
- ? gdbarch_gdb_signal_from_target (core_gdbarch,
- siggy)
- : gdb_signal_from_host (siggy));
- printf_filtered (_("Program terminated with signal %s, %s.\n"),
- gdb_signal_to_name (sig), gdb_signal_to_string (sig));
-
- set_internalvar_integer (lookup_internalvar ("_exitsignal"),
- siggy);
- }
-
- target_fetch_registers (get_current_regcache (), -1);
-
- reinit_frame_cache ();
- print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
- }
- static void
- core_detach (struct target_ops *ops, const char *args, int from_tty)
- {
- if (args)
- error (_("Too many arguments"));
- unpush_target (ops);
- reinit_frame_cache ();
- if (from_tty)
- printf_filtered (_("No core file now.\n"));
- }
- static void
- get_core_register_section (struct regcache *regcache,
- const struct regset *regset,
- const char *name,
- int min_size,
- int which,
- const char *human_name,
- int required)
- {
- static char *section_name = NULL;
- struct bfd_section *section;
- bfd_size_type size;
- char *contents;
- xfree (section_name);
- if (ptid_get_lwp (inferior_ptid))
- section_name = xstrprintf ("%s/%ld", name,
- ptid_get_lwp (inferior_ptid));
- else
- section_name = xstrdup (name);
- section = bfd_get_section_by_name (core_bfd, section_name);
- if (! section)
- {
- if (required)
- warning (_("Couldn't find %s registers in core file."),
- human_name);
- return;
- }
- size = bfd_section_size (core_bfd, section);
- if (size < min_size)
- {
- warning (_("Section `%s' in core file too small."), section_name);
- return;
- }
- contents = alloca (size);
- if (! bfd_get_section_contents (core_bfd, section, contents,
- (file_ptr) 0, size))
- {
- warning (_("Couldn't read %s registers from `%s' section in core file."),
- human_name, name);
- return;
- }
- if (regset != NULL)
- {
- regset->supply_regset (regset, regcache, -1, contents, size);
- return;
- }
- gdb_assert (core_vec);
- core_vec->core_read_registers (regcache, contents, size, which,
- ((CORE_ADDR)
- bfd_section_vma (core_bfd, section)));
- }
- static void
- get_core_registers_cb (const char *sect_name, int size,
- const struct regset *regset,
- const char *human_name, void *cb_data)
- {
- struct regcache *regcache = (struct regcache *) cb_data;
- int required = 0;
- if (strcmp (sect_name, ".reg") == 0)
- {
- required = 1;
- if (human_name == NULL)
- human_name = "general-purpose";
- }
- else if (strcmp (sect_name, ".reg2") == 0)
- {
- if (human_name == NULL)
- human_name = "floating-point";
- }
-
- get_core_register_section (regcache, regset, sect_name,
- size, -1, human_name, required);
- }
- static void
- get_core_registers (struct target_ops *ops,
- struct regcache *regcache, int regno)
- {
- int i;
- struct gdbarch *gdbarch;
- if (!(core_gdbarch && gdbarch_iterate_over_regset_sections_p (core_gdbarch))
- && (core_vec == NULL || core_vec->core_read_registers == NULL))
- {
- fprintf_filtered (gdb_stderr,
- "Can't fetch registers from this type of core file\n");
- return;
- }
- gdbarch = get_regcache_arch (regcache);
- if (gdbarch_iterate_over_regset_sections_p (gdbarch))
- gdbarch_iterate_over_regset_sections (gdbarch,
- get_core_registers_cb,
- (void *) regcache, NULL);
- else
- {
- get_core_register_section (regcache, NULL,
- ".reg", 0, 0, "general-purpose", 1);
- get_core_register_section (regcache, NULL,
- ".reg2", 0, 2, "floating-point", 0);
- }
-
- for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
- if (regcache_register_status (regcache, i) == REG_UNKNOWN)
- regcache_raw_supply (regcache, i, NULL);
- }
- static void
- core_files_info (struct target_ops *t)
- {
- print_section_info (core_data, core_bfd);
- }
- struct spuid_list
- {
- gdb_byte *buf;
- ULONGEST offset;
- LONGEST len;
- ULONGEST pos;
- ULONGEST written;
- };
- static void
- add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
- {
- struct spuid_list *list = list_p;
- enum bfd_endian byte_order
- = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
- int fd, pos = 0;
- sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
- if (pos == 0)
- return;
- if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
- {
- store_unsigned_integer (list->buf + list->pos - list->offset,
- 4, byte_order, fd);
- list->written += 4;
- }
- list->pos += 4;
- }
- static LONGEST
- get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
- {
- asection *section;
- char *section_name;
- const char *name = ".note.linuxcore.siginfo";
- if (ptid_get_lwp (inferior_ptid))
- section_name = xstrprintf ("%s/%ld", name,
- ptid_get_lwp (inferior_ptid));
- else
- section_name = xstrdup (name);
- section = bfd_get_section_by_name (abfd, section_name);
- xfree (section_name);
- if (section == NULL)
- return -1;
- if (!bfd_get_section_contents (abfd, section, readbuf, offset, len))
- return -1;
- return len;
- }
- static enum target_xfer_status
- core_xfer_partial (struct target_ops *ops, enum target_object object,
- const char *annex, gdb_byte *readbuf,
- const gdb_byte *writebuf, ULONGEST offset,
- ULONGEST len, ULONGEST *xfered_len)
- {
- switch (object)
- {
- case TARGET_OBJECT_MEMORY:
- return section_table_xfer_memory_partial (readbuf, writebuf,
- offset, len, xfered_len,
- core_data->sections,
- core_data->sections_end,
- NULL);
- case TARGET_OBJECT_AUXV:
- if (readbuf)
- {
-
- struct bfd_section *section;
- bfd_size_type size;
- section = bfd_get_section_by_name (core_bfd, ".auxv");
- if (section == NULL)
- return TARGET_XFER_E_IO;
- size = bfd_section_size (core_bfd, section);
- if (offset >= size)
- return TARGET_XFER_EOF;
- size -= offset;
- if (size > len)
- size = len;
- if (size == 0)
- return TARGET_XFER_EOF;
- if (!bfd_get_section_contents (core_bfd, section, readbuf,
- (file_ptr) offset, size))
- {
- warning (_("Couldn't read NT_AUXV note in core file."));
- return TARGET_XFER_E_IO;
- }
- *xfered_len = (ULONGEST) size;
- return TARGET_XFER_OK;
- }
- return TARGET_XFER_E_IO;
- case TARGET_OBJECT_WCOOKIE:
- if (readbuf)
- {
-
- struct bfd_section *section;
- bfd_size_type size;
- section = bfd_get_section_by_name (core_bfd, ".wcookie");
- if (section == NULL)
- return TARGET_XFER_E_IO;
- size = bfd_section_size (core_bfd, section);
- if (offset >= size)
- return TARGET_XFER_EOF;
- size -= offset;
- if (size > len)
- size = len;
- if (size == 0)
- return TARGET_XFER_EOF;
- if (!bfd_get_section_contents (core_bfd, section, readbuf,
- (file_ptr) offset, size))
- {
- warning (_("Couldn't read StackGhost cookie in core file."));
- return TARGET_XFER_E_IO;
- }
- *xfered_len = (ULONGEST) size;
- return TARGET_XFER_OK;
- }
- return TARGET_XFER_E_IO;
- case TARGET_OBJECT_LIBRARIES:
- if (core_gdbarch
- && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
- {
- if (writebuf)
- return TARGET_XFER_E_IO;
- else
- {
- *xfered_len = gdbarch_core_xfer_shared_libraries (core_gdbarch,
- readbuf,
- offset, len);
- if (*xfered_len == 0)
- return TARGET_XFER_EOF;
- else
- return TARGET_XFER_OK;
- }
- }
-
- case TARGET_OBJECT_LIBRARIES_AIX:
- if (core_gdbarch
- && gdbarch_core_xfer_shared_libraries_aix_p (core_gdbarch))
- {
- if (writebuf)
- return TARGET_XFER_E_IO;
- else
- {
- *xfered_len
- = gdbarch_core_xfer_shared_libraries_aix (core_gdbarch,
- readbuf, offset,
- len);
- if (*xfered_len == 0)
- return TARGET_XFER_EOF;
- else
- return TARGET_XFER_OK;
- }
- }
-
- case TARGET_OBJECT_SPU:
- if (readbuf && annex)
- {
-
- struct bfd_section *section;
- bfd_size_type size;
- char sectionstr[100];
- xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
- section = bfd_get_section_by_name (core_bfd, sectionstr);
- if (section == NULL)
- return TARGET_XFER_E_IO;
- size = bfd_section_size (core_bfd, section);
- if (offset >= size)
- return TARGET_XFER_EOF;
- size -= offset;
- if (size > len)
- size = len;
- if (size == 0)
- return TARGET_XFER_EOF;
- if (!bfd_get_section_contents (core_bfd, section, readbuf,
- (file_ptr) offset, size))
- {
- warning (_("Couldn't read SPU section in core file."));
- return TARGET_XFER_E_IO;
- }
- *xfered_len = (ULONGEST) size;
- return TARGET_XFER_OK;
- }
- else if (readbuf)
- {
-
- struct spuid_list list;
- list.buf = readbuf;
- list.offset = offset;
- list.len = len;
- list.pos = 0;
- list.written = 0;
- bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
- if (list.written == 0)
- return TARGET_XFER_EOF;
- else
- {
- *xfered_len = (ULONGEST) list.written;
- return TARGET_XFER_OK;
- }
- }
- return TARGET_XFER_E_IO;
- case TARGET_OBJECT_SIGNAL_INFO:
- if (readbuf)
- {
- LONGEST l = get_core_siginfo (core_bfd, readbuf, offset, len);
- if (l > 0)
- {
- *xfered_len = len;
- return TARGET_XFER_OK;
- }
- }
- return TARGET_XFER_E_IO;
- default:
- return ops->beneath->to_xfer_partial (ops->beneath, object,
- annex, readbuf,
- writebuf, offset, len,
- xfered_len);
- }
- }
- static int
- ignore (struct target_ops *ops, struct gdbarch *gdbarch,
- struct bp_target_info *bp_tgt)
- {
- return 0;
- }
- static int
- core_thread_alive (struct target_ops *ops, ptid_t ptid)
- {
- return 1;
- }
- static const struct target_desc *
- core_read_description (struct target_ops *target)
- {
- if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
- {
- const struct target_desc *result;
- result = gdbarch_core_read_description (core_gdbarch,
- target, core_bfd);
- if (result != NULL)
- return result;
- }
- return target->beneath->to_read_description (target->beneath);
- }
- static char *
- core_pid_to_str (struct target_ops *ops, ptid_t ptid)
- {
- static char buf[64];
- struct inferior *inf;
- int pid;
-
- if (core_gdbarch
- && gdbarch_core_pid_to_str_p (core_gdbarch))
- return gdbarch_core_pid_to_str (core_gdbarch, ptid);
-
-
- pid = ptid_get_lwp (ptid);
- if (pid != 0)
- return normal_pid_to_str (pid_to_ptid (pid));
-
- inf = find_inferior_ptid (ptid);
- if (inf != NULL && !inf->fake_pid_p)
- return normal_pid_to_str (ptid);
-
- xsnprintf (buf, sizeof buf, "<main task>");
- return buf;
- }
- static int
- core_has_memory (struct target_ops *ops)
- {
- return (core_bfd != NULL);
- }
- static int
- core_has_stack (struct target_ops *ops)
- {
- return (core_bfd != NULL);
- }
- static int
- core_has_registers (struct target_ops *ops)
- {
- return (core_bfd != NULL);
- }
- static void
- core_info_proc (struct target_ops *ops, const char *args,
- enum info_proc_what request)
- {
- struct gdbarch *gdbarch = get_current_arch ();
-
- if (gdbarch_core_info_proc_p (gdbarch))
- gdbarch_core_info_proc (gdbarch, args, request);
- }
- static void
- init_core_ops (void)
- {
- core_ops.to_shortname = "core";
- core_ops.to_longname = "Local core dump file";
- core_ops.to_doc =
- "Use a core file as a target. Specify the filename of the core file.";
- core_ops.to_open = core_open;
- core_ops.to_close = core_close;
- core_ops.to_detach = core_detach;
- core_ops.to_fetch_registers = get_core_registers;
- core_ops.to_xfer_partial = core_xfer_partial;
- core_ops.to_files_info = core_files_info;
- core_ops.to_insert_breakpoint = ignore;
- core_ops.to_remove_breakpoint = ignore;
- core_ops.to_thread_alive = core_thread_alive;
- core_ops.to_read_description = core_read_description;
- core_ops.to_pid_to_str = core_pid_to_str;
- core_ops.to_stratum = process_stratum;
- core_ops.to_has_memory = core_has_memory;
- core_ops.to_has_stack = core_has_stack;
- core_ops.to_has_registers = core_has_registers;
- core_ops.to_info_proc = core_info_proc;
- core_ops.to_magic = OPS_MAGIC;
- if (core_target)
- internal_error (__FILE__, __LINE__,
- _("init_core_ops: core target already exists (\"%s\")."),
- core_target->to_longname);
- core_target = &core_ops;
- }
- void
- _initialize_corelow (void)
- {
- init_core_ops ();
- add_target_with_completer (&core_ops, filename_completer);
- }