Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Roland McGrath <roland@redhat.com>
To: gdb-patches@sources.redhat.com
Subject: [PATCH] add-symbol-file-from-memory command
Date: Fri, 03 Oct 2003 04:27:00 -0000	[thread overview]
Message-ID: <200310030427.h934REdK018487@magilla.sf.frob.com> (raw)


I posted this originally back in May, and got no response whatsoever.  I've
updated the patch to work with current mainline gdb and tested it again.  I
hope there will be some response this time.  This new user command is not
important, but this code needs review as it will form part of the support
for backtraces from system calls to work on Linux 2.6 kernels.

This adds a user command add-symbol-file-from-memory, which is like
add-symbol-file but takes just the address of an ELF header in inferior
memory (and no other args) instead of a file name.

This command may not really be worth having, but it serves to exercise the
underlying function symbol_file_add_from_memory.  That function does the
work of reading symbols and unwind info from the Linux vsyscall DSO.
So please examine that new code.

This makes symfile.c call bfd_elf_bfd_from_remote_memory, which is
implemented in bfd/elf.c and so won't exist if there is no ELF target
backend configured into libbfd.  I couldn't see any obvious place in gdb
that is conditionalized at compile-time on ELF; unless I'm missing
something elfread.c is always built in regardless of the presence of ELF
targets.  Should I not be using this function in this file?


2003-10-02  Roland McGrath  <roland@redhat.com>

	* symfile.c (symbol_file_add_with_addrs_or_offsets): Take ABFD as
	a new argument.  Call symfile_bfd_open only if passed a null pointer.
	(symbol_file_add, reread_separate_symbols): Update callers.
	(build_addr_info): New function, helper for ...
	(symbol_file_add_from_memory): New function.
	(add_symbol_file_from_memory_command): New function using that.
	(_initialize_symfile): Register it for add-symbol-file-from-memory.
	* symfile.h (symbol_file_add_from_memory): Declare it.


Thanks,
Roland


Index: symfile.c
===================================================================
RCS file: /cvs/src/src/gdb/symfile.c,v
retrieving revision 1.109
diff -p -u -r1.109 symfile.c
--- symfile.c	21 Sep 2003 01:26:45 -0000	1.109
+++ symfile.c	3 Oct 2003 03:21:22 -0000
@@ -813,6 +813,10 @@ new_symfile_objfile (struct objfile *obj
    NAME is the file name (which will be tilde-expanded and made
    absolute herein) (but we don't free or modify NAME itself).
 
+   If ABFD is not null, it's already open and should be used instead of
+   opening the file by name.  This BFD will be closed on error, and
+   is always consumed by this function.
+
    FROM_TTY says how verbose to be.
 
    MAINLINE specifies whether this is the main symbol file, or whether
@@ -825,7 +829,7 @@ new_symfile_objfile (struct objfile *obj
    Upon success, returns a pointer to the objfile that was added.
    Upon failure, jumps back to command level (never returns). */
 static struct objfile *
-symbol_file_add_with_addrs_or_offsets (char *name, int from_tty,
+symbol_file_add_with_addrs_or_offsets (char *name, bfd *abfd, int from_tty,
                                        struct section_addr_info *addrs,
                                        struct section_offsets *offsets,
                                        int num_offsets,
@@ -834,14 +838,16 @@ symbol_file_add_with_addrs_or_offsets (c
   struct objfile *objfile;
   struct partial_symtab *psymtab;
   char *debugfile;
-  bfd *abfd;
   struct section_addr_info *orig_addrs;
   struct cleanup *my_cleanups;
 
   /* Open a bfd for the file, and give user a chance to burp if we'd be
      interactively wiping out any existing symbols.  */
 
-  abfd = symfile_bfd_open (name);
+  if (abfd == NULL)
+    abfd = symfile_bfd_open (name);
+
+  my_cleanups = make_cleanup_bfd_close (abfd);
 
   if ((have_full_symbols () || have_partial_symbols ())
       && mainline
@@ -850,6 +856,7 @@ symbol_file_add_with_addrs_or_offsets (c
     error ("Not confirmed.");
 
   objfile = allocate_objfile (abfd, flags);
+  discard_cleanups (my_cleanups);
 
   orig_addrs = alloc_section_addr_info (bfd_count_sections (abfd));
   my_cleanups = make_cleanup (xfree, orig_addrs);
@@ -987,7 +994,8 @@ struct objfile *
 symbol_file_add (char *name, int from_tty, struct section_addr_info *addrs,
 		 int mainline, int flags)
 {
-  return symbol_file_add_with_addrs_or_offsets (name, from_tty, addrs, 0, 0, 
+  return symbol_file_add_with_addrs_or_offsets (name, NULL, from_tty,
+						addrs, 0, 0,
                                                 mainline, flags);
 }
 
@@ -1845,6 +1853,103 @@ add_shared_symbol_files_command (char *a
 #endif
 }
 \f
+/* Helper function passed to bfd_map_over_sections.  */
+static void
+build_addr_info (bfd *abfd, asection *sectp, void *info)
+{
+  struct section_addr_info *const sai = info;
+  unsigned int i = 0;
+
+  if ((bfd_get_section_flags (abfd, sectp) & (SEC_ALLOC|SEC_LOAD)) == 0)
+    return;
+
+  while (sai->other[i++].name != NULL)
+    if (i == sai->num_sections)
+      return;
+
+  sai->other[i].addr = bfd_get_section_vma (abfd, sectp);
+  sai->other[i].name = (char *) bfd_get_section_name (abfd, sectp);
+  sai->other[i].sectindex = sectp->index;
+}
+
+/* Read inferior memory at ADDR to find the header of a loaded object file
+   and read its in-core symbols out of inferior memory.  TEMPL is a bfd
+   representing the target's format.  */
+struct objfile *
+symbol_file_add_from_memory (bfd *templ, CORE_ADDR addr, int from_tty)
+{
+  struct objfile *objf;
+  bfd *nbfd;
+  bfd_vma loadbase;
+  struct section_addr_info *sai;
+  unsigned int i;
+
+  if (from_tty)
+    {
+      if (bfd_get_flavour (templ) != bfd_target_elf_flavour)
+	error ("add-symbol-file-from-memory not supported for this target");
+    }
+  else
+    gdb_assert (bfd_get_flavour (templ) == bfd_target_elf_flavour);
+
+  nbfd = bfd_elf_bfd_from_remote_memory (templ, addr, &loadbase,
+					 target_read_memory);
+  if (nbfd == NULL)
+    {
+      if (from_tty)
+	error ("Failed to read a valid object file image from memory.");
+      return NULL;
+    }
+
+  if (!bfd_check_format (nbfd, bfd_object))
+    {
+      /* FIXME: should be checking for errors from bfd_close (for one thing,
+         on error it does not free all the storage associated with the
+         bfd).  */
+      bfd_close (nbfd);
+      if (from_tty)
+	error ("Got object file from memory but can't read symbols: %s.",
+	       bfd_errmsg (bfd_get_error ()));
+      return NULL;
+    }
+
+  sai = alloc_section_addr_info (bfd_count_sections (nbfd));
+  make_cleanup (xfree, sai);
+  bfd_map_over_sections (nbfd, build_addr_info, sai);
+  for (i = 0; i < sai->num_sections && sai->other[i].name != NULL; ++i)
+    sai->other[i].addr += loadbase;
+
+  objf = symbol_file_add_with_addrs_or_offsets ("<in-memory>", nbfd, from_tty,
+						sai, NULL, 0, 0, 0);
+
+  reinit_frame_cache ();	/* ??? */
+
+  return objf;
+}
+
+static void
+add_symbol_file_from_memory_command (char *args, int from_tty)
+{
+  CORE_ADDR addr;
+  bfd *templ;
+
+  if (args == NULL)
+    error ("add-symbol-file-from-memory requires an expression argument");
+
+  addr = parse_and_eval_address (args);
+
+  /* We need some representative bfd to know the target we are looking at.  */
+  if (symfile_objfile != NULL)
+    templ = symfile_objfile->obfd;
+  else
+    templ = exec_bfd;
+  if (templ == NULL)
+    error ("\
+Must use symbol-file or exec-file before add-symbol-file-from-memory.");
+
+  (void) symbol_file_add_from_memory (templ, addr, from_tty);
+}
+\f
 /* Re-read symbols if a symbol-file has changed.  */
 void
 reread_symbols (void)
@@ -2104,7 +2209,7 @@ reread_separate_symbols (struct objfile 
          Preserve the flags from objfile that make sense.  */
       objfile->separate_debug_objfile
         = (symbol_file_add_with_addrs_or_offsets
-           (debug_file,
+           (debug_file, NULL,
             info_verbose, /* from_tty: Don't override the default. */
             0, /* No addr table.  */
             objfile->section_offsets, objfile->num_sections,
@@ -2137,7 +2242,7 @@ add_filename_language (char *ext, enum l
@@ -3605,6 +3710,13 @@ with the text.  SECT is a section name t
 	       &cmdlist);
   set_cmd_completer (c, filename_completer);
 
+  c = add_cmd ("add-symbol-file-from-memory", class_files,
+	       add_symbol_file_from_memory_command,
+	       "Usage: add-symbol-file-from-memory ADDR\n\
+Load the symbols out of memory from a dynamically loaded object file.\n\
+ADDR is the starting address of the file's shared object file header.",
+	       &cmdlist);
+
   c = add_cmd ("add-shared-symbol-files", class_files,
 	       add_shared_symbol_files_command,
    "Load the symbols from shared objects in the dynamic linker's link map.",
Index: symfile.h
===================================================================
RCS file: /cvs/src/src/gdb/symfile.h,v
retrieving revision 1.25
diff -p -u -r1.25 symfile.h
--- symfile.h	11 Sep 2003 19:49:19 -0000	1.25
+++ symfile.h	3 Oct 2003 03:21:22 -0000
@@ -306,6 +306,12 @@ extern CORE_ADDR symbol_overlayed_addres
 /* Load symbols from a file.  */
 extern void symbol_file_add_main (char *args, int from_tty);
 
+/* Read inferior memory at ADDR to find the header of a loaded object file
+   and read its in-core symbols out of inferior memory.  TEMPL is a bfd
+   representing the target's format.  */
+extern struct objfile *symbol_file_add_from_memory (bfd *templ, CORE_ADDR addr,
+						    int from_tty);
+
 /* Clear GDB symbol tables.  */
 extern void symbol_file_clear (int from_tty);
 


             reply	other threads:[~2003-10-03  4:27 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-03  4:27 Roland McGrath [this message]
2003-10-03 21:41 ` Jim Blandy
2003-10-03 21:45   ` Daniel Jacobowitz
2003-10-03 22:08   ` Roland McGrath
2003-10-03 22:19   ` Elena Zannoni
2003-10-03 23:00     ` Roland McGrath
2003-10-04 12:57       ` Elena Zannoni
2003-10-04 20:14         ` Roland McGrath
2003-10-07  1:42           ` Andrew Cagney
2003-10-03 22:34   ` Andrew Cagney
2003-10-03 23:02     ` Roland McGrath
2003-10-04 14:39       ` Andrew Cagney
2003-10-06 15:30   ` Jim Blandy
2003-10-06 15:43     ` Daniel Jacobowitz
2003-10-06 19:57       ` Roland McGrath
2003-10-07 23:32       ` Michael Snyder
  -- strict thread matches above, loose matches on Subject: below --
2004-04-08 22:38 Roland McGrath
2004-04-15 18:11 ` Jim Blandy
2004-04-15 18:49   ` Andrew Cagney
2004-04-15 21:42   ` Roland McGrath
2004-04-08 20:47 Roland McGrath
2004-04-08 22:13 ` Jim Blandy
2004-04-08 22:28   ` Roland McGrath
2004-04-08 22:32     ` Daniel Jacobowitz
2004-04-08 22:35   ` Andrew Cagney
2004-04-08 22:42     ` Roland McGrath
2004-04-15 18:52       ` Andrew Cagney
2004-02-12  0:34 Roland McGrath
2004-02-13 15:54 ` Andrew Cagney
2004-02-13 19:13   ` Roland McGrath
2004-02-02  3:38 Roland McGrath
2004-02-02  4:06 ` Daniel Jacobowitz
2004-02-02  4:47   ` Roland McGrath
2004-02-02 15:02     ` Daniel Jacobowitz
2004-02-02 23:31       ` Roland McGrath
2004-02-02 23:34         ` Daniel Jacobowitz
2004-02-02  6:19 ` Eli Zaretskii
2004-02-02  7:35   ` Roland McGrath
2004-02-02 17:29     ` Eli Zaretskii
2003-05-20  7:01 Roland McGrath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200310030427.h934REdK018487@magilla.sf.frob.com \
    --to=roland@redhat.com \
    --cc=gdb-patches@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox