summaryrefslogtreecommitdiff
path: root/tests/print-die.cc
blob: 8a7f770ce99120d161ed7ff53dd2aa0a8942b145 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* 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>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#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"

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 bool output_stats;
static bool refs_shared_cu;
static bool refs_shared_file;

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], "--refs-shared-cu"))
    {
      refs_shared_cu = true;
      --argc;
      ++argv;
    }

  if (argc > 1 && !strcmp (argv[1], "--refs-shared-file"))
    {
      refs_shared_file = refs_shared_cu = 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], "--stats"))
    {
      output_stats = true;
      --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<dwarf::debug_info_entry::identity_type,
			   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)
    {
      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<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);
}

static int nth;
static std::map<int, int> nth_ref;

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 ());

  ++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<file> (*i, indent + 1, limit, refs);

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

static inline void
dump_nth (pair<int, int> p)
{
  cout << dec << p.first << ": ref" << p.second << "\n";
}

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

  if (!print_offset && !elide_refs)
    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)
{
  if (no_print)
    return;

  static refs_map common_refs;
  refs_map file_refs;

  for (typename file::compile_units_type::const_iterator i
	 = dw.compile_units ().begin (); i != dw.compile_units ().end (); ++i)
    if (refs_shared_cu)
      print_cu<file> (*i, limit, refs_shared_file ? common_refs : file_refs);
    else
      {
	refs_map refs;
	print_cu<file> (*i, limit, refs);
      }

  if (dump_refs)
    for_each (nth_ref.begin (), nth_ref.end (), dump_nth);
}

template<typename file>
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.
	dwarf_output out (dw, c);
	if (output_stats)
	  c.stats ();
	print_file (out, 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);
template void print_file (const char *, const dwarf_output &,
			  const unsigned int);