Repository navigation
Expand file tree
/
Copy patharray_type.cc
More file actions
162 lines (137 loc) · 3.16 KB
/
Copy patharray_type.cc
File metadata and controls
162 lines (137 loc) · 3.16 KB
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
#include <cassert>
#include <numeric>
#include <algorithm>
#include <functional>
#include <limits>
#include <dwarf.h>
#include <libdwarf.h>
#include "common.h"
#include "array_type.h"
using std::vector;
using std::map;
using std::ostream;
using std::endl;
ArrayType::ArrayType(Dwarf_Off id)
: Type(id)
{
}
int
ArrayType::init(Dwarf_Debug dbg, map<Dwarf_Off, Type*>& types)
{
(void) dbg;
(void) types;
Dwarf_Error err;
Dwarf_Die die;
Dwarf_Attribute typeAttr;
Dwarf_Off typeId;
int rc;
rc = dwarf_offdie(dbg, this->id(), &die, &err);
assert(rc == DW_DLV_OK);
rc = dwarf_attr(die, DW_AT_type, &typeAttr, &err);
assert(rc == DW_DLV_OK);
rc = dwarf_global_formref(typeAttr, &typeId, &err);
assert(rc == DW_DLV_OK);
map<Dwarf_Off, Type*>::iterator it
= types.find(typeId);
assert(it != types.end());
baseType_ = it->second;
Dwarf_Die subrange;
Dwarf_Attribute uboundAttr;
Dwarf_Unsigned ubound;
rc = dwarf_child(die, &subrange, &err);
assert(rc == DW_DLV_OK);
length_.clear();
for (;;)
{
rc = dwarf_attr(subrange, DW_AT_upper_bound, &uboundAttr, &err);
assert(rc == DW_DLV_OK || rc == DW_DLV_NO_ENTRY);
if (rc == DW_DLV_NO_ENTRY)
{
length_.push_back(0);
}
else
{
rc = dwarf_formudata(uboundAttr, &ubound, &err);
assert(rc == DW_DLV_OK);
if (ubound == std::numeric_limits<size_t>::max())
{
length_.push_back(0);
}
else
{
length_.push_back(ubound + 1);
}
rc = dwarf_siblingof(dbg, subrange, &subrange, &err);
assert(rc == DW_DLV_OK || rc == DW_DLV_NO_ENTRY);
if (rc == DW_DLV_NO_ENTRY)
{
break;
}
}
}
std::reverse(length_.begin(), length_.end());
return 0;
}
int
ArrayType::dumpData(void* data, int indent, ostream& out)
{
Dwarf_Unsigned size = this->size();
Dwarf_Unsigned unitSize;
if (length_.back() != 0)
{
unitSize = size / length_.back();
}
else
{
unitSize = 0;
}
return dumpDataSub(data, unitSize, length_.size() - 1, indent, out);
}
int
ArrayType::dumpDataSub(void* data, Dwarf_Unsigned size, int level,
int indent, ostream& out)
{
if (level == 0 && baseType_->isCharacter())
{
out << '"';
for (Dwarf_Unsigned i = 0; i < length_[0]; i++)
{
print_char_repr(static_cast<char *>(data)[i], out);
}
out << '"';
}
else
{
out << '{' << endl;
for (Dwarf_Unsigned i = 0; i < length_[level]; i++)
{
print_indent(indent + 1, out);
out << '[' << i << "] = ";
void* p
= static_cast<char *>(data) + size * i;
if (level == 0)
{
baseType_->dumpData(p, indent + 1, out);
}
else
{
dumpDataSub(p, size / length_[level - 1], level - 1, indent, out);
}
out << "," << endl;
}
print_indent(indent, out);
out << "}";
}
return 0;
}
Dwarf_Unsigned
ArrayType::size() const
{
return std::accumulate(length_.begin(), length_.end(), baseType_->size(),
std::multiplies<Dwarf_Unsigned>());
}
bool
ArrayType::isCharacter() const
{
return false;
}