[12/14] tests: Optionally dump all units in dwarf-getmacros

Message ID 4453f4ff42a3234e60438733bcf1147451b7c2a7.1695837512.git.osandov@fb.com
State Committed
Delegated to: Mark Wielaard
Headers
Series elfutils: DWARF package (.dwp) file support |

Commit Message

Omar Sandoval Sept. 27, 2023, 6:21 p.m. UTC
  From: Omar Sandoval <osandov@fb.com>

If instead of a CU offset an empty string is given as the second
argument, dump all units.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 tests/ChangeLog         |  3 +++
 tests/dwarf-getmacros.c | 51 +++++++++++++++++++++++++++++++++--------
 2 files changed, 44 insertions(+), 10 deletions(-)
  

Comments

Mark Wielaard Nov. 2, 2023, 8:39 p.m. UTC | #1
Hi Omar,

On Wed, Sep 27, 2023 at 11:21:01AM -0700, Omar Sandoval wrote:
> If instead of a CU offset an empty string is given as the second
> argument, dump all units.

Pushed,

Mark
  

Patch

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 4380c57f..97261444 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -17,6 +17,9 @@ 
 	* dwarf-getmacros.c (mac): Add DW_MACRO_define_sup,
 	DW_MACRO_define_strx, DW_MACRO_undef_sup, and DW_MACRO_undef_strx
 	cases to opcode switch statement.
+	(getmacros): New function.
+	(main): If second argument is empty, call getmacros on every unit.
+	Otherwise, call getmacros on given unit.
 
 2023-04-21  Frank Ch. Eigler <fche@redhat.com>
 
diff --git a/tests/dwarf-getmacros.c b/tests/dwarf-getmacros.c
index e291bfd2..8381d42c 100644
--- a/tests/dwarf-getmacros.c
+++ b/tests/dwarf-getmacros.c
@@ -121,26 +121,57 @@  include (Dwarf *dbg, Dwarf_Off macoff, ptrdiff_t token)
       }
 }
 
+static void
+getmacros (Dwarf *dbg, Dwarf_Die *die, bool new_style)
+{
+  for (ptrdiff_t off = new_style ? DWARF_GETMACROS_START : 0;
+       (off = dwarf_getmacros (die, mac, dbg, off)); )
+    if (off == -1)
+      {
+	puts (dwarf_errmsg (-1));
+	break;
+      }
+}
+
 int
 main (int argc, char *argv[])
 {
   assert (argc >= 3);
   const char *name = argv[1];
-  ptrdiff_t cuoff = strtol (argv[2], NULL, 0);
   bool new_style = argc > 3;
 
   int fd = open (name, O_RDONLY);
   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
 
-  Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
-
-  for (ptrdiff_t off = new_style ? DWARF_GETMACROS_START : 0;
-       (off = dwarf_getmacros (cudie, mac, dbg, off)); )
-    if (off == -1)
-      {
-	puts (dwarf_errmsg (dwarf_errno ()));
-	break;
-      }
+  if (argv[2][0] == '\0')
+    {
+      Dwarf_CU *cu = NULL;
+      Dwarf_Die cudie, subdie;
+      uint8_t unit_type;
+      while (dwarf_get_units (dbg, cu, &cu, NULL,
+			      &unit_type, &cudie, &subdie) == 0)
+	{
+	  Dwarf_Die *die = (unit_type == DW_UT_skeleton
+			    ? &subdie : &cudie);
+	  if (! dwarf_hasattr (die, DW_AT_macro_info)
+	      && ! dwarf_hasattr (die, DW_AT_GNU_macros)
+	      && ! dwarf_hasattr (die, DW_AT_macros))
+	    continue;
+	  printf ("CU %s\n", dwarf_diename (die));
+	  getmacros (dbg, die, new_style);
+	}
+    }
+  else
+    {
+      ptrdiff_t cuoff = strtol (argv[2], NULL, 0);
+      Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
+      if (cudie == NULL)
+	{
+	  puts (dwarf_errmsg (-1));
+	  return 1;
+	}
+      getmacros (dbg, cudie, new_style);
+    }
 
   dwarf_end (dbg);