This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Makes objdump --dwarf as flexible as readelf --debug-dump


Hi,

currently objdump --dwarf dumps all dwarf sections while readelf --dwarf
can dump only selected one.  I think it would be useful to have a feature match
(eg to make easier dwarf dumping on Mach-O files).

This patch implements this feature mostly by moving code from readelf.c to
dwarf.c

Unfortunately 'objdump -W' corresponds to 'readelf -w' but it is too late to
change.

No regression on i386/GNU linux.

Tristan.

binutils/
2009-02-03  Tristan Gingold  <gingold@adacore.com>

	* NEWS: Mention feature match between objdump and readelf for dumping
	dwarf info.

	* doc/binutils.texi (objdump): Document -W/--dwarf improvments to
	objdump.

	* objdump.c (usage): Update documentation for -W/--dwarf.
	(enum option_values): Add OPTION_DWARF.
	(long_options): --dwarf can accept arguments.
	(dump_dwarf_section): Also check enabled field.
	(main): Option -W can accept arguments, code moved to
	dwarf.c and call dwarf_select_sections_all instead.
	* readelf.c (process_section_headers): Remove do_debug_lines_decoded.
	(parse_args): Move code to...
	* dwarf.c (dwarf_select_sections_by_letters,
	dwarf_select_sections_by_names): : ...here (new functions).
	(do_debug_lines_decoded): Remove and replaced by ...
	(FLAG_DEBUG_LINES_RAW, FLAG_DEBUG_LINES_DECODED): ... new macros.
	(display_debug_lines): Adjust for previous change.
	(dwarf_select_sections_all): New function.
	(debug_displays): Add initializer for enabled field.
	* dwarf.h (do_debug_lines_decoded): Remove.
	Add prototypes for the new functions.
	(struct dwarf_section_display): Add enabled field.

--- binutils/NEWS	3 Feb 2009 15:48:49 -0000	1.76
+++ binutils/NEWS	3 Feb 2009 16:53:20 -0000
@@ -1,5 +1,7 @@
 -*- text -*-
 
+* Option --dwarf/-W of objdump is now as flexible as readelf --debug-dump/-w.
+
 * --as-needed now links in a dynamic library if it satisfies undefined
   symbols in regular objects, or in other dynamic libraries.  In the
   latter case the library is not linked if it is found in a DT_NEEDED
Index: binutils/dwarf.c
===================================================================
RCS file: /cvs/src/src/binutils/dwarf.c,v
retrieving revision 1.41
diff -u -r1.41 dwarf.c
--- binutils/dwarf.c	6 Oct 2008 16:27:33 -0000	1.41
+++ binutils/dwarf.c	3 Feb 2009 16:53:21 -0000
@@ -44,7 +44,6 @@
 int do_debug_info;
 int do_debug_abbrevs;
 int do_debug_lines;
-int do_debug_lines_decoded;
 int do_debug_pubnames;
 int do_debug_aranges;
 int do_debug_ranges;
@@ -55,6 +54,10 @@
 int do_debug_loc;
 int do_wide;
 
+/* Values for do_debug_lines.  */
+#define FLAG_DEBUG_LINES_RAW	 1
+#define FLAG_DEBUG_LINES_DECODED 2
+
 dwarf_vma (*byte_get) (unsigned char *, int);
 
 dwarf_vma
@@ -2781,8 +2784,8 @@
 {
   unsigned char *data = section->start;
   unsigned char *end = data + section->size;
-  int retValRaw = 0;
-  int retValDecoded = 0;
+  int retValRaw = 1;
+  int retValDecoded = 1;
 
   if (load_debug_info (file) == 0)
     {
@@ -2791,14 +2794,13 @@
       return 0;
     }
 
-  if (do_debug_lines)
+  if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
     retValRaw = display_debug_lines_raw (section, data, end);
 
-  if (do_debug_lines_decoded)
+  if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
     retValDecoded = display_debug_lines_decoded (section, data, end);
 
-  if ((do_debug_lines && !retValRaw)
-      || (do_debug_lines_decoded && !retValDecoded))
+  if (!retValRaw || !retValDecoded)
     return 0;
 
   return 1;
@@ -4624,38 +4626,185 @@
     }
 }
 
+void
+dwarf_select_sections_by_names (const char *names)
+{
+  typedef struct
+  {
+    const char * option;
+    int *        variable;
+    int val;
+  }
+  debug_dump_long_opts;
+
+  static const debug_dump_long_opts opts_table [] =
+    {
+      /* Please keep this table alpha- sorted.  */
+      { "Ranges", & do_debug_ranges, 1 },
+      { "abbrev", & do_debug_abbrevs, 1 },
+      { "aranges", & do_debug_aranges, 1 },
+      { "frames", & do_debug_frames, 1 },
+      { "frames-interp", & do_debug_frames_interp, 1 },
+      { "info", & do_debug_info, 1 },
+      { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility.  */
+      { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
+      { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
+      { "loc",  & do_debug_loc, 1 },
+      { "macro", & do_debug_macinfo, 1 },
+      { "pubnames", & do_debug_pubnames, 1 },
+      /* This entry is for compatability
+	 with earlier versions of readelf.  */
+      { "ranges", & do_debug_aranges, 1 },
+      { "str", & do_debug_str, 1 },
+      { NULL, NULL, 0 }
+    };
+
+  const char *p;
+  
+  p = names;
+  while (*p)
+    {
+      const debug_dump_long_opts * entry;
+      
+      for (entry = opts_table; entry->option; entry++)
+	{
+	  size_t len = strlen (entry->option);
+	  
+	  if (strncmp (p, entry->option, len) == 0
+	      && (p[len] == ',' || p[len] == '\0'))
+	    {
+	      * entry->variable |= entry->val;
+	      
+	      /* The --debug-dump=frames-interp option also
+		 enables the --debug-dump=frames option.  */
+	      if (do_debug_frames_interp)
+		do_debug_frames = 1;
+
+	      p += len;
+	      break;
+	    }
+	}
+      
+      if (entry->option == NULL)
+	{
+	  warn (_("Unrecognized debug option '%s'\n"), p);
+	  p = strchr (p, ',');
+	  if (p == NULL)
+	    break;
+	}
+      
+      if (*p == ',')
+	p++;
+    }
+}
+
+void
+dwarf_select_sections_by_letters (const char *letters)
+{
+  unsigned int index = 0;
+
+  while (letters[index])
+    switch (letters[index++])
+      {
+      case 'i':
+	do_debug_info = 1;
+	break;
+	
+      case 'a':
+	do_debug_abbrevs = 1;
+	break;
+	
+      case 'l':
+	do_debug_lines |= FLAG_DEBUG_LINES_RAW;
+	break;
+	
+      case 'L':
+	do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
+	break;
+	
+      case 'p':
+	do_debug_pubnames = 1;
+	break;
+	
+      case 'r':
+	do_debug_aranges = 1;
+	break;
+	
+      case 'R':
+	do_debug_ranges = 1;
+	break;
+	
+      case 'F':
+	do_debug_frames_interp = 1;
+      case 'f':
+	do_debug_frames = 1;
+	break;
+	
+      case 'm':
+	do_debug_macinfo = 1;
+	break;
+	
+      case 's':
+	do_debug_str = 1;
+	break;
+	
+      case 'o':
+	do_debug_loc = 1;
+	break;
+	
+      default:
+	warn (_("Unrecognized debug option '%s'\n"), optarg);
+	break;
+      }
+}
+
+void
+dwarf_select_sections_all (void)
+{
+  do_debug_info = 1;
+  do_debug_abbrevs = 1;
+  do_debug_lines = FLAG_DEBUG_LINES_RAW;
+  do_debug_pubnames = 1;
+  do_debug_aranges = 1;
+  do_debug_ranges = 1;
+  do_debug_frames = 1;
+  do_debug_macinfo = 1;
+  do_debug_str = 1;
+  do_debug_loc = 1;
+}
+
 struct dwarf_section_display debug_displays[] =
 {
   { { ".debug_abbrev",		".zdebug_abbrev",	NULL,	NULL,	0,	0 },
-    display_debug_abbrev,		0,	0 },
+    display_debug_abbrev,		&do_debug_abbrevs,	0,	0 },
   { { ".debug_aranges",		".zdebug_aranges",	NULL,	NULL,	0,	0 },
-    display_debug_aranges,		0,	0 },
+    display_debug_aranges,		&do_debug_aranges,	0,	0 },
   { { ".debug_frame",		".zdebug_frame",	NULL,	NULL,	0,	0 },
-    display_debug_frames,		1,	0 },
+    display_debug_frames,		&do_debug_frames,	1,	0 },
   { { ".debug_info",		".zdebug_info",		NULL,	NULL,	0,	0 },
-    display_debug_info,			1,	0 },
+    display_debug_info,			&do_debug_info,		1,	0 },
   { { ".debug_line",		".zdebug_line",		NULL,	NULL,	0,	0 },
-    display_debug_lines,		0,	0 },
+    display_debug_lines,		&do_debug_lines,	0,	0 },
   { { ".debug_pubnames",	".zdebug_pubnames",	NULL,	NULL,	0,	0 },
-    display_debug_pubnames,		0,	0 },
+    display_debug_pubnames,		&do_debug_pubnames,	0,	0 },
   { { ".eh_frame",		"",			NULL,	NULL,	0,	0 },
-    display_debug_frames,		1,	1 },
+    display_debug_frames,		&do_debug_frames,	1,	1 },
   { { ".debug_macinfo",		".zdebug_macinfo",	NULL,	NULL,	0,	0 },
-    display_debug_macinfo,		0,	0 },
+    display_debug_macinfo,		&do_debug_macinfo,	0,	0 },
   { { ".debug_str",		".zdebug_str",		NULL,	NULL,	0,	0 },
-    display_debug_str,			0,	0 },
+    display_debug_str,			&do_debug_str,		0,	0 },
   { { ".debug_loc",		".zdebug_loc",		NULL,	NULL,	0,	0 },
-    display_debug_loc,			0,	0 },
+    display_debug_loc,			&do_debug_loc,		0,	0 },
   { { ".debug_pubtypes",	".zdebug_pubtypes",	NULL,	NULL,	0,	0 },
-    display_debug_pubnames,		0,	0 },
+    display_debug_pubnames,		&do_debug_pubnames,	0,	0 },
   { { ".debug_ranges",		".zdebug_ranges",	NULL,	NULL,	0,	0 },
-    display_debug_ranges,		0,	0 },
+    display_debug_ranges,		&do_debug_ranges,	0,	0 },
   { { ".debug_static_func",	".zdebug_static_func",	NULL,	NULL,	0,	0 },
-    display_debug_not_supported,	0,	0 },
+    display_debug_not_supported,	NULL,			0,	0 },
   { { ".debug_static_vars",	".zdebug_static_vars",	NULL,	NULL,	0,	0 },
-    display_debug_not_supported,	0,	0 },
-  { { ".debug_types",	".zdebug_types",		NULL,	NULL,	0,	0 },
-    display_debug_not_supported,	0,	0 },
+    display_debug_not_supported,	NULL,			0,	0 },
+  { { ".debug_types",		".zdebug_types",	NULL,	NULL,	0,	0 },
+    display_debug_not_supported,	NULL,			0,	0 },
   { { ".debug_weaknames",	".zdebug_weaknames",	NULL,	NULL,	0,	0 },
-    display_debug_not_supported,	0,	0 }
+    display_debug_not_supported,	NULL,			0,	0 }
 };
Index: binutils/dwarf.h
===================================================================
RCS file: /cvs/src/src/binutils/dwarf.h,v
retrieving revision 1.7
diff -u -r1.7 dwarf.h
--- binutils/dwarf.h	10 Jul 2008 01:32:23 -0000	1.7
+++ binutils/dwarf.h	3 Feb 2009 16:53:21 -0000
@@ -37,7 +37,7 @@
    * input file, as determined by load_debug_section().  */
   const char *uncompressed_name;
   const char *compressed_name;
-  const char* name;
+  const char *name;
   unsigned char *start;
   dwarf_vma address;
   dwarf_size_type size;
@@ -49,6 +49,7 @@
 {
   struct dwarf_section section;
   int (*display) (struct dwarf_section *, void *);
+  int *enabled;
   unsigned int relocate : 1;
   unsigned int eh_frame : 1;
 };
@@ -102,7 +103,6 @@
 extern int do_debug_info;
 extern int do_debug_abbrevs;
 extern int do_debug_lines;
-extern int do_debug_lines_decoded;
 extern int do_debug_pubnames;
 extern int do_debug_aranges;
 extern int do_debug_ranges;
@@ -120,6 +120,10 @@
 
 extern void free_debug_memory (void);
 
+extern void dwarf_select_sections_by_names (const char *names);
+extern void dwarf_select_sections_by_letters (const char *letters);
+extern void dwarf_select_sections_all (void);
+
 void *cmalloc (size_t, size_t);
 void *xcmalloc (size_t, size_t);
 void *xcrealloc (void *, size_t, size_t);
Index: binutils/objdump.c
===================================================================
RCS file: /cvs/src/src/binutils/objdump.c,v
retrieving revision 1.153
diff -u -r1.153 objdump.c
--- binutils/objdump.c	3 Feb 2009 15:48:49 -0000	1.153
+++ binutils/objdump.c	3 Feb 2009 16:53:21 -0000
@@ -199,7 +199,9 @@
   -g, --debugging          Display debug information in object file\n\
   -e, --debugging-tags     Display debug information using ctags style\n\
   -G, --stabs              Display (in raw form) any STABS info in the file\n\
-  -W, --dwarf              Display DWARF info in the file\n\
+  -W[lLiaprmfFsoR] or\n\
+  --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]\n\
+                           Display DWARF info in the file\n\
   -t, --syms               Display the contents of the symbol table(s)\n\
   -T, --dynamic-syms       Display the contents of the dynamic symbol table\n\
   -r, --reloc              Display the relocation entries in the file\n\
@@ -254,6 +256,7 @@
     OPTION_ENDIAN=150,
     OPTION_START_ADDRESS,
     OPTION_STOP_ADDRESS,
+    OPTION_DWARF,
     OPTION_PREFIX,
     OPTION_PREFIX_STRIP,
     OPTION_ADJUST_VMA
@@ -293,7 +296,7 @@
   {"source", no_argument, NULL, 'S'},
   {"special-syms", no_argument, &dump_special_syms, 1},
   {"include", required_argument, NULL, 'I'},
-  {"dwarf", no_argument, NULL, 'W'},
+  {"dwarf", optional_argument, NULL, OPTION_DWARF},
   {"stabs", no_argument, NULL, 'G'},
   {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
@@ -2210,8 +2213,10 @@
     match = name;
 
   for (i = 0; i < max; i++)
-    if (strcmp (debug_displays [i].section.uncompressed_name, match) == 0
-        || strcmp (debug_displays [i].section.compressed_name, match) == 0)
+    if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
+	 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
+	&& debug_displays [i].enabled != NULL
+	&& *debug_displays [i].enabled)
       {
 	if (!debug_displays [i].eh_frame)
 	  {
@@ -3148,7 +3153,8 @@
   bfd_init ();
   set_default_bfd_target ();
 
-  while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW",
+  while ((c = getopt_long (argc, argv,
+			   "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
 			   long_options, (int *) 0))
 	 != EOF)
     {
@@ -3311,16 +3317,18 @@
 	case 'W':
 	  dump_dwarf_section_info = TRUE;
 	  seenflag = TRUE;
-	  do_debug_info = 1;
-	  do_debug_abbrevs = 1;
-	  do_debug_lines = 1;
-	  do_debug_pubnames = 1;
-	  do_debug_aranges = 1;
-	  do_debug_ranges = 1;
-	  do_debug_frames = 1;
-	  do_debug_macinfo = 1;
-	  do_debug_str = 1;
-	  do_debug_loc = 1;
+	  if (optarg)
+	    dwarf_select_sections_by_letters (optarg);
+	  else
+	    dwarf_select_sections_all ();
+	  break;
+	case OPTION_DWARF:
+	  dump_dwarf_section_info = TRUE;
+	  seenflag = TRUE;
+	  if (optarg)
+	    dwarf_select_sections_by_names (optarg);
+	  else
+	    dwarf_select_sections_all ();
 	  break;
 	case 'G':
 	  dump_stab_section_info = TRUE;
Index: binutils/readelf.c
===================================================================
RCS file: /cvs/src/src/binutils/readelf.c,v
retrieving revision 1.435
diff -u -r1.435 readelf.c
--- binutils/readelf.c	3 Feb 2009 15:48:49 -0000	1.435
+++ binutils/readelf.c	3 Feb 2009 16:53:22 -0000
@@ -3039,63 +3039,8 @@
 	    do_debugging = 1;
 	  else
 	    {
-	      unsigned int index = 0;
-
 	      do_debugging = 0;
-
-	      while (optarg[index])
-		switch (optarg[index++])
-		  {
-		  case 'i':
-		    do_debug_info = 1;
-		    break;
-
-		  case 'a':
-		    do_debug_abbrevs = 1;
-		    break;
-
-		  case 'l':
-		    do_debug_lines = 1;
-		    break;
-
-		  case 'L':
-		    do_debug_lines_decoded = 1;
-		    break;
-
-		  case 'p':
-		    do_debug_pubnames = 1;
-		    break;
-
-		  case 'r':
-		    do_debug_aranges = 1;
-		    break;
-
-		  case 'R':
-		    do_debug_ranges = 1;
-		    break;
-
-		  case 'F':
-		    do_debug_frames_interp = 1;
-		  case 'f':
-		    do_debug_frames = 1;
-		    break;
-
-		  case 'm':
-		    do_debug_macinfo = 1;
-		    break;
-
-		  case 's':
-		    do_debug_str = 1;
-		    break;
-
-		  case 'o':
-		    do_debug_loc = 1;
-		    break;
-
-		  default:
-		    warn (_("Unrecognized debug option '%s'\n"), optarg);
-		    break;
-		  }
+	      dwarf_select_sections_by_letters (optarg);
 	    }
 	  break;
 	case OPTION_DEBUG_DUMP:
@@ -3104,74 +3049,8 @@
 	    do_debugging = 1;
 	  else
 	    {
-	      typedef struct
-	      {
-		const char * option;
-		int *        variable;
-	      }
-	      debug_dump_long_opts;
-
-	      debug_dump_long_opts opts_table [] =
-		{
-		  /* Please keep this table alpha- sorted.  */
-		  { "Ranges", & do_debug_ranges },
-		  { "abbrev", & do_debug_abbrevs },
-		  { "aranges", & do_debug_aranges },
-		  { "frames", & do_debug_frames },
-		  { "frames-interp", & do_debug_frames_interp },
-		  { "info", & do_debug_info },
-		  { "line", & do_debug_lines }, /* For backwards compatibility.  */
-		  { "rawline", & do_debug_lines },
-		  { "decodedline", & do_debug_lines_decoded },
-		  { "loc",  & do_debug_loc },
-		  { "macro", & do_debug_macinfo },
-		  { "pubnames", & do_debug_pubnames },
-		  /* This entry is for compatability
-		     with earlier versions of readelf.  */
-		  { "ranges", & do_debug_aranges },
-		  { "str", & do_debug_str },
-		  { NULL, NULL }
-		};
-
-	      const char *p;
-
 	      do_debugging = 0;
-
-	      p = optarg;
-	      while (*p)
-		{
-		  debug_dump_long_opts * entry;
-
-		  for (entry = opts_table; entry->option; entry++)
-		    {
-		      size_t len = strlen (entry->option);
-
-		      if (strneq (p, entry->option, len)
-			  && (p[len] == ',' || p[len] == '\0'))
-			{
-			  * entry->variable = 1;
-
-			  /* The --debug-dump=frames-interp option also
-			     enables the --debug-dump=frames option.  */
-			  if (do_debug_frames_interp)
-			    do_debug_frames = 1;
-
-			  p += len;
-			  break;
-			}
-		    }
-
-		  if (entry->option == NULL)
-		    {
-		      warn (_("Unrecognized debug option '%s'\n"), p);
-		      p = strchr (p, ',');
-		      if (p == NULL)
-			break;
-		    }
-
-		  if (*p == ',')
-		    p++;
-		}
+	      dwarf_select_sections_by_names (optarg);
 	    }
 	  break;
 #ifdef SUPPORT_DISASSEMBLY
@@ -4285,7 +4164,7 @@
       else if (section->sh_type == SHT_RELA)
 	CHECK_ENTSIZE (section, i, Rela);
       else if ((do_debugging || do_debug_info || do_debug_abbrevs
-		|| do_debug_lines || do_debug_lines_decoded || do_debug_pubnames
+		|| do_debug_lines || do_debug_pubnames
 		|| do_debug_aranges || do_debug_frames || do_debug_macinfo
 		|| do_debug_str || do_debug_loc || do_debug_ranges)
 	       && (const_strneq (name, ".debug_")
@@ -4299,8 +4178,7 @@
 	  if (do_debugging
 	      || (do_debug_info     && streq (name, "info"))
 	      || (do_debug_abbrevs  && streq (name, "abbrev"))
-	      || ((do_debug_lines || do_debug_lines_decoded)
-		  && streq (name, "line"))
+	      || (do_debug_lines    && streq (name, "line"))
 	      || (do_debug_pubnames && streq (name, "pubnames"))
 	      || (do_debug_aranges  && streq (name, "aranges"))
 	      || (do_debug_ranges   && streq (name, "ranges"))
Index: binutils/doc/binutils.texi
===================================================================
RCS file: /cvs/src/src/binutils/doc/binutils.texi,v
retrieving revision 1.139
diff -u -r1.139 binutils.texi
--- binutils/doc/binutils.texi	3 Feb 2009 15:48:50 -0000	1.139
+++ binutils/doc/binutils.texi	3 Feb 2009 16:53:23 -0000
@@ -1602,7 +1602,8 @@
         [@option{-r}|@option{--reloc}]
         [@option{-R}|@option{--dynamic-reloc}]
         [@option{-s}|@option{--full-contents}]
-        [@option{-W}|@option{--dwarf}]
+        [@option{-W[lLiaprmfFsoR]}|
+         @option{--dwarf}[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges]]
         [@option{-G}|@option{--stabs}]
         [@option{-t}|@option{--syms}]
         [@option{-T}|@option{--dynamic-syms}]
@@ -1957,12 +1958,13 @@
 When disassembling instructions, do not print the instruction bytes.
 This is the default when @option{--prefix-addresses} is used.
 
-@item -W
-@itemx --dwarf
+@item -W[lLiaprmfFsoR]
+@itemx --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges]
 @cindex DWARF
 @cindex debug symbols
-Displays the contents of the DWARF debug sections in the file, if any
-are present.
+Displays the contents of the debug sections in the file, if any are
+present.  If one of the optional letters or words follows the switch
+then only data found in those specific sections will be dumped.
 
 @item -G
 @itemx --stabs


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]