-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterators.hh
357 lines (302 loc) · 6.59 KB
/
iterators.hh
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
/*
Copyright (C) 2009, 2010, 2011, 2012 Red Hat, Inc.
This file is part of dwlocstat.
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
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 copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>. */
#ifndef ALL_DIES_IT_H
#define ALL_DIES_IT_H
#include <vector>
#include <stdexcept>
#include <cassert>
#include <algorithm>
#include <elfutils/libdw.h>
class cu_iterator
: public std::iterator<std::input_iterator_tag, Dwarf_Die *>
{
friend class all_dies_iterator;
Dwarf *m_dw;
Dwarf_Off m_offset;
Dwarf_Die m_cudie;
cu_iterator (Dwarf_Off off)
: m_dw (nullptr)
, m_offset (off)
, m_cudie ({})
{}
void
move ()
{
assert (*this != end ());
do
{
Dwarf_Off old_offset = m_offset;
size_t hsize;
if (dwarf_nextcu (m_dw, m_offset, &m_offset, &hsize,
nullptr, nullptr, nullptr) != 0)
done ();
else if (dwarf_offdie (m_dw, old_offset + hsize, &m_cudie) == nullptr)
continue;
else
// XXX partial unit, type unit, what else?
assert (dwarf_tag (&m_cudie) == DW_TAG_compile_unit);
}
while (false);
}
void
done ()
{
*this = end ();
}
public:
cu_iterator (cu_iterator const &other) = default;
explicit cu_iterator (Dwarf *dw)
: m_dw (dw)
, m_offset (0)
, m_cudie ({})
{
move ();
}
static cu_iterator
end ()
{
return cu_iterator ((Dwarf_Off)-1);
}
bool
operator== (cu_iterator const &other) const
{
return m_offset == other.m_offset;
}
bool
operator!= (cu_iterator const &other) const
{
return !(*this == other);
}
cu_iterator
operator++ ()
{
move ();
return *this;
}
cu_iterator
operator++ (int)
{
cu_iterator tmp = *this;
++*this;
return tmp;
}
Dwarf_Off
offset () const
{
return m_offset;
}
Dwarf_Die *
operator* ()
{
return &m_cudie;
}
};
// Tree flattening iterator. It pre-order iterates all CUs in given
// dwarf file.
class all_dies_iterator
: public std::iterator<std::input_iterator_tag, Dwarf_Die *>
{
cu_iterator m_cuit;
std::vector<Dwarf_Off> m_stack;
Dwarf_Die m_die;
all_dies_iterator (Dwarf_Off offset)
: m_cuit (cu_iterator::end ())
{
}
public:
all_dies_iterator (all_dies_iterator const &other) = default;
all_dies_iterator (Dwarf *dw)
: m_cuit (dw)
, m_stack ()
, m_die (**m_cuit)
{
}
static all_dies_iterator
end ()
{
return all_dies_iterator ((Dwarf_Off)-1);
}
bool
operator== (all_dies_iterator const &other) const
{
return m_cuit == other.m_cuit
&& m_stack == other.m_stack
&& (m_cuit == cu_iterator::end ()
|| m_die.addr == other.m_die.addr);
}
bool
operator!= (all_dies_iterator const &other) const
{
return !(*this == other);
}
all_dies_iterator
operator++ ()
{
if (dwarf_haschildren (&m_die))
{
m_stack.push_back (dwarf_dieoffset (&m_die));
if (dwarf_child (&m_die, &m_die))
throw std::runtime_error ("dwarf_child");
return *this;
}
do
switch (dwarf_siblingof (&m_die, &m_die))
{
case -1:
throw std::runtime_error ("dwarf_siblingof");
case 0:
return *this;
case 1:
assert (!m_stack.empty ());
// No sibling found, go a level up and retry.
if (dwarf_offdie (m_cuit.m_dw, m_stack.back (), &m_die) == nullptr)
throw std::runtime_error ("dwarf_offdie");
m_stack.pop_back ();
}
while (!m_stack.empty ());
m_die = **++m_cuit;
return *this;
}
all_dies_iterator
operator++ (int)
{
all_dies_iterator prev = *this;
++*this;
return prev;
}
Dwarf_Die *
operator* ()
{
return &m_die;
}
std::vector<Dwarf_Die>
stack () const
{
std::vector<Dwarf_Die> ret;
for (all_dies_iterator it = *this; it != end (); it = it.parent ())
ret.push_back (**it);
std::reverse (ret.begin (), ret.end ());
return ret;
}
all_dies_iterator
parent () const
{
assert (*this != end ());
if (m_stack.empty ())
return end ();
all_dies_iterator ret = *this;
if (dwarf_offdie (m_cuit.m_dw, m_stack.back (), &ret.m_die) == nullptr)
throw std::runtime_error ("parent:dwarf_offdie");
ret.m_stack.pop_back ();
return ret;
}
cu_iterator
cu () const
{
return m_cuit;
}
};
class attr_iterator
: public std::iterator<std::input_iterator_tag, Dwarf_Attribute *>
{
Dwarf_Die *m_die;
Dwarf_Attribute m_at;
ptrdiff_t m_offset;
struct cb_data
{
Dwarf_Attribute *at;
bool been;
};
static int
callback (Dwarf_Attribute *at, void *data)
{
cb_data *d = static_cast<cb_data *> (data);
if (d->been)
return DWARF_CB_ABORT;
*d->at = *at;
d->been = true;
// Do a second iteration to find the next offset.
return DWARF_CB_OK;
}
void
move ()
{
// If m_offset is already 1, we are done iterating.
if (m_offset == 1)
{
*this = end ();
return;
}
cb_data data = {&m_at, false};
m_offset = dwarf_getattrs (m_die, &callback, &data, m_offset);
if (m_offset == -1)
throw std::runtime_error ("dwarf_getattrs");
}
attr_iterator (ptrdiff_t offset)
: m_die (nullptr)
, m_at ({0})
, m_offset (offset)
{}
public:
attr_iterator (Dwarf_Die *die)
: m_die (die)
, m_at ({0})
, m_offset (0)
{
move ();
}
bool
operator== (attr_iterator const &other) const
{
return m_offset == other.m_offset
&& m_at.code == other.m_at.code;
}
bool
operator!= (attr_iterator const &other) const
{
return !(*this == other);
}
attr_iterator &
operator++ ()
{
assert (*this != end ());
move ();
return *this;
}
attr_iterator
operator++ (int)
{
attr_iterator tmp = *this;
++*this;
return tmp;
}
Dwarf_Attribute *
operator* ()
{
return &m_at;
}
static attr_iterator
end ()
{
return attr_iterator ((ptrdiff_t)1);
}
};
#endif /* ALL_DIES_IT_H */