summaryrefslogtreecommitdiff
path: root/tests/print-die.hh
blob: 7b4fa3f54274e3c57688b28b52ce930ff44baf98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* 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
   <http://www.openinventionnetwork.com>.  */

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <clocale>
#include <libintl.h>
#include <ostream>
#include <iomanip>
#include <tr1/unordered_map>
#include <functional>
#include <algorithm>

#include "c++/dwarf_edit"
#include "c++/dwarf_output"

static bool print_offset;
static bool sort_attrs;

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], "--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;
    }

  depth = 0;
  if (argc > 1 && sscanf (argv[1], "--depth=%u", &depth) == 1)
    {
      --argc;
      ++argv;
    }
}

static int next_ref = 1;
typedef tr1::unordered_map< ::Dwarf_Off, int> refs_map;

template<typename attrs_type,
	 void (*act) (const typename attrs_type::value_type &, refs_map &)
	 >
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<int, iterator> &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<int, iterator> sorted;
	for (iterator i = attrs.begin (); i != attrs.end (); ++i)
	  sorted[(*i).first] = i;
	for_each (sorted.begin (), sorted.end (),
		  attr_walker<attrs_type, act> (r));
      }
  }
};

template<typename attrs_type>
void
print_attr (const typename attrs_type::value_type &attr, refs_map &refs)
{
  if (!print_offset && attr.second.what_space () == dwarf::VS_reference)
    cout << " " << dwarf::attributes::name (attr.first) << "=\"#ref"
	 << dec << refs[attr.second.reference ()->identity ()] << "\"";
  else
    cout << " " << to_string (attr);
}

template<typename attrs_type>
static void
print_attrs (const attrs_type &attrs, refs_map &refs)
{
  attr_walker<attrs_type, print_attr<attrs_type> >::walk (attrs, refs);
}

template<typename attrs_type>
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<typename attrs_type>
static void
prewalk_attrs (const attrs_type &attrs, refs_map &refs)
{
  attr_walker<attrs_type, prewalk_attr<attrs_type> >::walk (attrs, refs);
}

template<typename file>
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<file> (*i, refs);

  prewalk_attrs (die.attributes (), refs);
}

template<typename file>
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 ());

  cout << prefix << "<" << tag;
  if (print_offset)
    cout << " offset=[" << die.offset () << "]";
  else
    {
      refs_map::const_iterator it = refs.find (die.identity ());
      if (it != refs.end ())
	cout << " ref=\"ref" << dec << 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<file> (*i, indent + 1, limit, refs);

      cout << prefix << "</" << tag << ">\n";
    }
  else
    cout << "/>\n";
}

template<typename file>
static void
print_cu (const typename file::compile_unit &cu, const unsigned int limit)
{
  const typename file::debug_info_entry &die = cu;
  // static_cast<const typename file::debug_info_entry &> (cu),

  refs_map refs;

  if (!print_offset)
    prewalk_die<file> (die, refs);

  print_die<file> (die, 1, limit, refs);
}

template<typename file>
static void
print_file (const file &dw, const unsigned int limit)
{
  for (typename file::compile_units::const_iterator i
	 = dw.compile_units ().begin (); i != dw.compile_units ().end (); ++i)
    print_cu<file> (*i, limit);
}

template<typename file>
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 ();
    }
}