]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
tests: Optionally dump all units in dwarf-getmacros
authorOmar Sandoval <osandov@fb.com>
Wed, 27 Sep 2023 18:21:01 +0000 (11:21 -0700)
committerMark Wielaard <mark@klomp.org>
Thu, 2 Nov 2023 20:39:00 +0000 (21:39 +0100)
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
tests/dwarf-getmacros.c

index 71fbb5599ef893ab663324e731fdac5e5c0226ad..d97749f5a5df922ef0c1a625fb0a81f170f2b4b3 100644 (file)
@@ -3,6 +3,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-09-27  Omar Sandoval  <osandov@fb.com>
 
index e291bfd23a42c1047babf47081b8e9ef3b955d4b..8381d42c05e863341ef812fc580ed371082f7548 100644 (file)
@@ -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);