From a24b20783cd22a690c9f05a19a334f5baae7b19c Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Tue, 25 Aug 2009 23:48:41 -0700 Subject: Share print-die compilation among tests. --- tests/ChangeLog | 6 + tests/Makefile.am | 4 +- tests/dwarf-print.cc | 1 + tests/print-die.cc | 329 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/print-die.hh | 316 ------------------------------------------------- 5 files changed, 338 insertions(+), 318 deletions(-) create mode 100644 tests/print-die.cc delete mode 100644 tests/print-die.hh (limited to 'tests') diff --git a/tests/ChangeLog b/tests/ChangeLog index 3a81522a..8f7e9dc2 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,9 @@ +2009-08-25 Roland McGrath + + * print-die.hh: New file with extern decls. Contents moved to ... + * print-die.cc: ... here, new file. + * Makefile.am (dwarf_print_SOURCES, dwarf_edit_SOURCES): Build it. + 2009-08-21 Roland McGrath * print-die.hh (print_die_main, print_file): Add --silent option. diff --git a/tests/Makefile.am b/tests/Makefile.am index b0da862c..ad5a466d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -195,10 +195,10 @@ 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_SOURCES = dwarf-print.cc print-die.cc dwarf_print_LDADD = $(libdwpp) $(libmudflap) -ldl -dwarf_edit_SOURCES = dwarf_edit.cc +dwarf_edit_SOURCES = dwarf_edit.cc print-die.cc dwarf_edit_LDADD = $(libdwpp) $(libmudflap) -ldl arextract_LDADD = $(libelf) $(libmudflap) diff --git a/tests/dwarf-print.cc b/tests/dwarf-print.cc index 36b9415b..698dd0da 100644 --- a/tests/dwarf-print.cc +++ b/tests/dwarf-print.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include "c++/dwarf" diff --git a/tests/print-die.cc b/tests/print-die.cc new file mode 100644 index 00000000..728eb74a --- /dev/null +++ b/tests/print-die.cc @@ -0,0 +1,329 @@ +/* Pseudo-XMLish printing for elfutils::dwarf* tests. + 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 + . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "c++/dwarf_edit" +#include "c++/dwarf_output" + +using namespace elfutils; +using namespace std; + +static bool print_offset; +static bool sort_attrs; +static bool elide_refs; +static bool dump_refs; +static bool no_print; + +static enum { copy_none, copy_edit, copy_output } make_copy; + +void +print_die_main (int &argc, char **&argv, unsigned int &depth) +{ + /* 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); + + if (argc > 1 && !strcmp (argv[1], "--offsets")) + { + print_offset = true; + --argc; + ++argv; + } + + if (argc > 1 && !strcmp (argv[1], "--norefs")) + { + elide_refs = true; + --argc; + ++argv; + } + + if (argc > 1 && !strcmp (argv[1], "--dump-refs")) + { + dump_refs = true; + --argc; + ++argv; + } + + if (argc > 1 && !strcmp (argv[1], "--sort-attrs")) + { + sort_attrs = true; + --argc; + ++argv; + } + + if (argc > 1 && !strcmp (argv[1], "--edit")) + { + make_copy = copy_edit; + --argc; + ++argv; + } + else if (argc > 1 && !strcmp (argv[1], "--output")) + { + make_copy = copy_output; + --argc; + ++argv; + } + + if (argc > 1 && !strcmp (argv[1], "--silent")) + { + no_print = true; + --argc; + ++argv; + } + + depth = 0; + if (argc > 1 && sscanf (argv[1], "--depth=%u", &depth) == 1) + { + --argc; + ++argv; + } +} + +static int next_ref = 1; +typedef tr1::unordered_map refs_map; + +template +class attr_walker +{ +private: + refs_map &refs; + inline attr_walker (refs_map &r) : refs (r) {} + + typedef typename attrs_type::const_iterator iterator; + typedef typename iterator::value_type attr_type; + +public: + inline void operator () (const pair &p) const + { + (*act) (*p.second, refs); + } + + static inline void walk (const attrs_type &attrs, refs_map &r) + { + if (attrs_type::ordered () || !sort_attrs) + for (iterator i = attrs.begin (); i != attrs.end (); ++i) + (*act) (*i, r); + else + { + map sorted; + for (iterator i = attrs.begin (); i != attrs.end (); ++i) + sorted[(*i).first] = i; + for_each (sorted.begin (), sorted.end (), + attr_walker (r)); + } + } +}; + +template +void +print_attr (const typename attrs_type::value_type &attr, refs_map &refs) +{ + if (!print_offset && attr.second.what_space () == dwarf::VS_reference) + { + if (elide_refs) + cout << " " << dwarf::attributes::name (attr.first) << "=\"ref\""; + else + cout << " " << dwarf::attributes::name (attr.first) << "=\"#ref" + << dec << refs[attr.second.reference ()->identity ()] << "\""; + } + else + cout << " " << to_string (attr); +} + +template +static void +print_attrs (const attrs_type &attrs, refs_map &refs) +{ + attr_walker >::walk (attrs, refs); +} + +template +void +prewalk_attr (const typename attrs_type::value_type &attr, refs_map &refs) +{ + if (attr.second.what_space () == dwarf::VS_reference + && refs.insert (make_pair (attr.second.reference ()->identity (), + next_ref)).second) + ++next_ref; +} + +template +static void +prewalk_attrs (const attrs_type &attrs, refs_map &refs) +{ + attr_walker >::walk (attrs, refs); +} + +template +static void +prewalk_die (const typename file::debug_info_entry &die, refs_map &refs) +{ + for (typename file::debug_info_entry::children_type::const_iterator i + = die.children ().begin (); i != die.children ().end (); ++i) + prewalk_die (*i, refs); + + prewalk_attrs (die.attributes (), refs); +} + +static int nth; +static std::map nth_ref; + +template +static void +print_die (const typename file::debug_info_entry &die, + unsigned int indent, unsigned int limit, refs_map &refs) +{ + string prefix (indent, ' '); + const string tag = dwarf::tags::name (die.tag ()); + + ++nth; + if (dump_refs) + cout << dec << nth << ": "; + + cout << prefix << "<" << tag; + if (print_offset) + cout << " offset=[" << hex << die.offset () << "]"; + else if (!elide_refs) + { + refs_map::const_iterator it = refs.find (die.identity ()); + if (it != refs.end ()) + { + cout << " ref=\"ref" << dec << it->second << "\""; + nth_ref[nth] = it->second; + } + } + + print_attrs (die.attributes (), refs); + + if (die.has_children ()) + { + if (limit != 0 && indent >= limit) + { + cout << ">...\n"; + return; + } + + cout << ">\n"; + + for (typename file::debug_info_entry::children_type::const_iterator i + = die.children ().begin (); i != die.children ().end (); ++i) + print_die (*i, indent + 1, limit, refs); + + cout << prefix << "\n"; + } + else + cout << "/>\n"; +} + +static inline void +dump_nth (pair p) +{ + cout << dec << p.first << ": ref" << p.second << "\n"; +} + +template +static void +print_cu (const typename file::compile_unit &cu, const unsigned int limit) +{ + const typename file::debug_info_entry &die = cu; + // static_cast (cu), + + refs_map refs; + + if (!print_offset && !elide_refs) + prewalk_die (die, refs); + + print_die (die, 1, limit, refs); + + if (dump_refs) + for_each (nth_ref.begin (), nth_ref.end (), dump_nth); +} + +template +static void +print_file (const file &dw, const unsigned int limit) +{ + if (no_print) + return; + + for (typename file::compile_units::const_iterator i + = dw.compile_units ().begin (); i != dw.compile_units ().end (); ++i) + print_cu (*i, limit); +} + +template +void +print_file (const char *name, const file &dw, const unsigned int limit) +{ + cout << name << ":\n"; + + switch (make_copy) + { + case copy_none: + print_file (dw, limit); + break; + case copy_edit: + print_file (dwarf_edit (dw), limit); + break; + case copy_output: + { + dwarf_output_collector c; // We'll just throw it away. + print_file (dwarf_output (dw, c), limit); + } + break; + default: + abort (); + } +} + +// Explicit instantiations. +template void print_file (const char *, const dwarf &, + const unsigned int); +template void print_file (const char *, const dwarf_edit &, + const unsigned int); diff --git a/tests/print-die.hh b/tests/print-die.hh deleted file mode 100644 index 76f160b4..00000000 --- a/tests/print-die.hh +++ /dev/null @@ -1,316 +0,0 @@ -/* Pseudo-XMLish printing for elfutils::dwarf* tests. - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "c++/dwarf_edit" -#include "c++/dwarf_output" - -static bool print_offset; -static bool sort_attrs; -static bool elide_refs; -static bool dump_refs; -static bool no_print; - -static enum { copy_none, copy_edit, copy_output } make_copy; - -static void -print_die_main (int &argc, char **&argv, unsigned int &depth) -{ - /* 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); - - if (argc > 1 && !strcmp (argv[1], "--offsets")) - { - print_offset = true; - --argc; - ++argv; - } - - if (argc > 1 && !strcmp (argv[1], "--norefs")) - { - elide_refs = true; - --argc; - ++argv; - } - - if (argc > 1 && !strcmp (argv[1], "--dump-refs")) - { - dump_refs = true; - --argc; - ++argv; - } - - if (argc > 1 && !strcmp (argv[1], "--sort-attrs")) - { - sort_attrs = true; - --argc; - ++argv; - } - - if (argc > 1 && !strcmp (argv[1], "--edit")) - { - make_copy = copy_edit; - --argc; - ++argv; - } - else if (argc > 1 && !strcmp (argv[1], "--output")) - { - make_copy = copy_output; - --argc; - ++argv; - } - - if (argc > 1 && !strcmp (argv[1], "--silent")) - { - no_print = true; - --argc; - ++argv; - } - - depth = 0; - if (argc > 1 && sscanf (argv[1], "--depth=%u", &depth) == 1) - { - --argc; - ++argv; - } -} - -static int next_ref = 1; -typedef tr1::unordered_map refs_map; - -template -class attr_walker -{ -private: - refs_map &refs; - inline attr_walker (refs_map &r) : refs (r) {} - - typedef typename attrs_type::const_iterator iterator; - typedef typename iterator::value_type attr_type; - -public: - inline void operator () (const pair &p) const - { - (*act) (*p.second, refs); - } - - static inline void walk (const attrs_type &attrs, refs_map &r) - { - if (attrs_type::ordered () || !sort_attrs) - for (iterator i = attrs.begin (); i != attrs.end (); ++i) - (*act) (*i, r); - else - { - map sorted; - for (iterator i = attrs.begin (); i != attrs.end (); ++i) - sorted[(*i).first] = i; - for_each (sorted.begin (), sorted.end (), - attr_walker (r)); - } - } -}; - -template -void -print_attr (const typename attrs_type::value_type &attr, refs_map &refs) -{ - if (!print_offset && attr.second.what_space () == dwarf::VS_reference) - { - if (elide_refs) - cout << " " << dwarf::attributes::name (attr.first) << "=\"ref\""; - else - cout << " " << dwarf::attributes::name (attr.first) << "=\"#ref" - << dec << refs[attr.second.reference ()->identity ()] << "\""; - } - else - cout << " " << to_string (attr); -} - -template -static void -print_attrs (const attrs_type &attrs, refs_map &refs) -{ - attr_walker >::walk (attrs, refs); -} - -template -void -prewalk_attr (const typename attrs_type::value_type &attr, refs_map &refs) -{ - if (attr.second.what_space () == dwarf::VS_reference - && refs.insert (make_pair (attr.second.reference ()->identity (), - next_ref)).second) - ++next_ref; -} - -template -static void -prewalk_attrs (const attrs_type &attrs, refs_map &refs) -{ - attr_walker >::walk (attrs, refs); -} - -template -static void -prewalk_die (const typename file::debug_info_entry &die, refs_map &refs) -{ - for (typename file::debug_info_entry::children_type::const_iterator i - = die.children ().begin (); i != die.children ().end (); ++i) - prewalk_die (*i, refs); - - prewalk_attrs (die.attributes (), refs); -} - -static int nth; -static std::map nth_ref; - -template -static void -print_die (const typename file::debug_info_entry &die, - unsigned int indent, unsigned int limit, refs_map &refs) -{ - string prefix (indent, ' '); - const string tag = dwarf::tags::name (die.tag ()); - - ++nth; - if (dump_refs) - cout << dec << nth << ": "; - - cout << prefix << "<" << tag; - if (print_offset) - cout << " offset=[" << hex << die.offset () << "]"; - else if (!elide_refs) - { - refs_map::const_iterator it = refs.find (die.identity ()); - if (it != refs.end ()) - { - cout << " ref=\"ref" << dec << it->second << "\""; - nth_ref[nth] = it->second; - } - } - - print_attrs (die.attributes (), refs); - - if (die.has_children ()) - { - if (limit != 0 && indent >= limit) - { - cout << ">...\n"; - return; - } - - cout << ">\n"; - - for (typename file::debug_info_entry::children_type::const_iterator i - = die.children ().begin (); i != die.children ().end (); ++i) - print_die (*i, indent + 1, limit, refs); - - cout << prefix << "\n"; - } - else - cout << "/>\n"; -} - -static inline void -dump_nth (pair p) -{ - cout << dec << p.first << ": ref" << p.second << "\n"; -} - -template -static void -print_cu (const typename file::compile_unit &cu, const unsigned int limit) -{ - const typename file::debug_info_entry &die = cu; - // static_cast (cu), - - refs_map refs; - - if (!print_offset && !elide_refs) - prewalk_die (die, refs); - - print_die (die, 1, limit, refs); - - if (dump_refs) - for_each (nth_ref.begin (), nth_ref.end (), dump_nth); -} - -template -static void -print_file (const file &dw, const unsigned int limit) -{ - if (no_print) - return; - - for (typename file::compile_units::const_iterator i - = dw.compile_units ().begin (); i != dw.compile_units ().end (); ++i) - print_cu (*i, limit); -} - -template -static void -print_file (const char *name, const file &dw, const unsigned int limit) -{ - cout << name << ":\n"; - - switch (make_copy) - { - case copy_none: - print_file (dw, limit); - break; - case copy_edit: - print_file (dwarf_edit (dw), limit); - break; - case copy_output: - { - dwarf_output_collector c; // We'll just throw it away. - print_file (dwarf_output (dw, c), limit); - } - break; - default: - abort (); - } -} -- cgit v1.2.3