]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
template tweaks for known-dwarf.h lookup, new dwarf-print test program
authorRoland McGrath <roland@redhat.com>
Wed, 25 Mar 2009 03:02:32 +0000 (20:02 -0700)
committerRoland McGrath <roland@redhat.com>
Wed, 25 Mar 2009 03:02:32 +0000 (20:02 -0700)
libdw/c++/dwarf
libdw/c++/known.cc
libdw/c++/subr.hh
tests/ChangeLog
tests/Makefile.am
tests/dwarf-print.cc [new file with mode: 0644]

index 8d0737292b6594673172983af44fa0420f14448a..aba8b0a05b8469bcbcb0bc214e1a8d3dde942670 100644 (file)
@@ -228,25 +228,19 @@ namespace elfutils
   // One DWARF object file.
   class dwarf
   {
-  public:
-    static const char *known_attribute (int);
+  private:
     static const char *known_tag (int);
+    static const char *known_attribute (int);
 
-    static inline std::string tag_name (int code)
-    {
-      return subr::known_name<known_tag> (code);
-    }
+  public:
+    typedef subr::known<__typeof ("DW_TAG_"), known_tag> tags;
+    typedef subr::known<__typeof ("DW_AT_"), known_attribute> attributes;
 
     template<typename attribute>
     static inline std::string attribute_name (const attribute &attr)
     {
       int code = attr.first;
-      return attribute_name (code);
-    }
-
-    static inline std::string attribute_name (const unsigned int code)
-    {
-      return subr::known_name<known_attribute> (code);
+      return attributes::name (code);
     }
 
   private:
@@ -1765,7 +1759,7 @@ namespace elfutils
 
       inline std::string to_string () const
       {
-       std::string result = attribute_name (::dwarf_whatattr (thisattr ()));
+       std::string result = attributes::name (::dwarf_whatattr (thisattr ()));
        result += "=";
        result += second.to_string ();
        return result;
index 18ca6297c78c8b223977366d08bd39a43ded0c0b..04473ca5c729dce774b10e7bc862a8a088008b48 100644 (file)
@@ -11,7 +11,7 @@ dwarf::known_tag (int tag)
 {
   switch (tag)
     {
-#define ONE_KNOWN_DW_TAG(name, id)             case id: return #name;
+#define ONE_KNOWN_DW_TAG(name, id)             case id: return #id;
 #define ONE_KNOWN_DW_TAG_DESC(name, id, desc)  ONE_KNOWN_DW_TAG (name, id)
       ALL_KNOWN_DW_TAG
     }
@@ -23,7 +23,7 @@ dwarf::known_attribute (int name)
 {
   switch (name)
     {
-#define ONE_KNOWN_DW_AT(name, id)              case id: return #name;
+#define ONE_KNOWN_DW_AT(name, id)              case id: return #id;
 #define ONE_KNOWN_DW_AT_DESC(name, id, desc)   ONE_KNOWN_DW_AT (name, id)
       ALL_KNOWN_DW_AT
     }
index ef632e57a433307cf35a8fb67869a419b0bc5d5b..d8862e530b4b0ae116a354e965a647617c2dfc8b 100644 (file)
@@ -35,18 +35,33 @@ namespace elfutils
       }
     };
 
-    template<const char *lookup_known (int)>
-    static inline std::string known_name (int code)
+    static inline std::string hex_string (int code)
     {
-      const char *known = lookup_known (code);
-      if (known != NULL)
-       return std::string (known);
       std::ostringstream os;
       os.setf(std::ios::hex, std::ios::basefield);
       os << code;
       return os.str ();
     }
 
+    template<typename prefix_type, const char *lookup_known (int)>
+    struct known
+    {
+      // The names in the table are the identifiers, with prefix.
+      static inline std::string identifier (int code)
+      {
+       const char *known = lookup_known (code);
+       return known == NULL ? hex_string (code) : std::string (known);
+      }
+
+      // For the pretty name, skip over the prefix.
+      static inline std::string name (int code)
+      {
+       const char *known = lookup_known (code);
+       return (known == NULL ? hex_string (code)
+               : std::string (&known[sizeof (prefix_type) - 1]));
+      }
+    };
+
     template<typename t1, typename t2>
     struct equal_to : public std::binary_function<t1, t2, bool>
     {
index b23cf8c7bc5b4010e8e7f573050dc73db11d0847..5affd0c477dacec8d146af48f70dc00d3da32650 100644 (file)
@@ -1,3 +1,9 @@
+2009-03-24  Roland McGrath  <roland@redhat.com>
+
+       * dwarf-print.cc: New file.
+       * Makefile.am (noinst_PROGRAMS): Add it.
+       (dwarf_print_SOURCES, dwarf_print_LDADD): New variables.
+
 2009-01-10  Roland McGrath  <roland@redhat.com>
 
        * run-dwarfcmp-self.sh: Also run with -T.
index 577f4c18b0a40df9bd786b19e5e8a5e980e4cc22..e075fd037dfc0e36d339fda718b9b9970666319d 100644 (file)
@@ -60,7 +60,8 @@ noinst_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
                  find-prologues funcretval allregs rdwrmmap \
                  dwfl-bug-addr-overflow arls dwfl-bug-fd-leak \
                  dwfl-addr-sect dwfl-bug-report early-offscn \
-                 dwfl-bug-getmodules
+                 dwfl-bug-getmodules \
+                 dwarf-print
 # get-ciefde
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
            asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -185,6 +186,12 @@ libebl = ../libebl/libebl.a
 libeu = ../lib/libeu.a
 endif !STANDALONE
 
+# XXX later the C++ stuff will be in libdw.so directly
+libdwpp = ../libdw/libdwpp.a $(libdw)
+
+dwarf_print_SOURCES = dwarf-print.cc
+dwarf_print_LDADD = $(libdwpp) $(libmudflap) -ldl
+
 arextract_LDADD = $(libelf) $(libmudflap)
 arsymtest_LDADD = $(libelf) $(libmudflap)
 newfile_LDADD = $(libelf) $(libmudflap)
diff --git a/tests/dwarf-print.cc b/tests/dwarf-print.cc
new file mode 100644 (file)
index 0000000..85dc3a7
--- /dev/null
@@ -0,0 +1,116 @@
+/* Test program for elfutils::dwarf basics.
+   Copyright (C) 2009 Red Hat, Inc.
+   This file is part of Red Hat elfutils.
+
+   Red Hat elfutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by the
+   Free Software Foundation; version 2 of the License.
+
+   Red Hat elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with Red Hat elfutils; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+   Red Hat elfutils is an included package of the Open Invention Network.
+   An included package of the Open Invention Network is a package for which
+   Open Invention Network licensees cross-license their patents.  No patent
+   license is granted, either expressly or impliedly, by designation as an
+   included package.  Should you wish to participate in the Open Invention
+   Network licensing program, please visit www.openinventionnetwork.com
+   <http://www.openinventionnetwork.com>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <clocale>
+#include <libintl.h>
+#include <ostream>
+#include <iomanip>
+
+#include "c++/dwarf"
+
+using namespace elfutils;
+using namespace std;
+
+static Dwarf *
+open_file (const char *fname)
+{
+  int fd = open (fname, O_RDONLY);
+  if (unlikely (fd == -1))
+    error (2, errno, gettext ("cannot open '%s'"), fname);
+  Dwarf *dw = dwarf_begin (fd, DWARF_C_READ);
+  if (dw == NULL)
+    {
+      error (2, 0,
+            gettext ("cannot create DWARF descriptor for '%s': %s"),
+            fname, dwarf_errmsg (-1));
+    }
+  return dw;
+}
+
+static void
+print_die (const dwarf::debug_info_entry &die, unsigned int indent)
+{
+  string prefix (indent, ' ');
+  const string tag = dwarf::tags::name (die.tag ());
+
+  cout << prefix << "<" << tag << " offset=[" << die.offset () << "]";
+
+  for (dwarf::debug_info_entry::attributes::const_iterator i
+        = die.attributes ().begin (); i != die.attributes ().end (); ++i)
+    cout << " " << (*i).to_string ();
+
+  if (die.has_children ())
+    {
+      cout << ">\n";
+
+      for (dwarf::debug_info_entry::children::const_iterator i
+            = die.children ().begin (); i != die.children ().end (); ++i)
+       print_die (*i, indent + 1);
+
+      cout << prefix << "</" << tag << ">\n";
+    }
+  else
+    cout << "/>\n";
+}
+
+static void
+process_file (const char *file)
+{
+  dwarf dw (open_file (file));
+
+  cout << file << ":\n";
+
+  for (dwarf::compile_units::const_iterator i = dw.compile_units ().begin ();
+       i != dw.compile_units ().end ();
+       ++i)
+    print_die (*i, 1);
+}
+
+int
+main (int argc, char *argv[])
+{
+  /* Set locale.  */
+  (void) setlocale (LC_ALL, "");
+
+  /* Make sure the message catalog can be found.  */
+  (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
+
+  /* Initialize the message catalog.  */
+  (void) textdomain (PACKAGE_TARNAME);
+
+  cout << hex << setiosflags (ios::showbase);
+
+  for (int i = 1; i < argc; ++i)
+    process_file (argv[i]);
+
+  return 0;
+}