-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure_type.cc
More file actions
214 lines (175 loc) · 4.72 KB
/
structure_type.cc
File metadata and controls
214 lines (175 loc) · 4.72 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
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
#include <climits>
#include <cassert>
#include <dwarf.h>
#include <libdwarf.h>
#include "common.h"
#include "structure_type.h"
using std::map;
using std::vector;
using std::ostream;
using std::endl;
namespace {
int getMemberOffset(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Unsigned* offset);
void printBitField(void* data, Dwarf_Unsigned byteSize,
Dwarf_Unsigned bitOffset, Dwarf_Unsigned bitSize, ostream& out);
template<typename T>
T getBitFieldValue(T value, Dwarf_Unsigned bitOffset, Dwarf_Unsigned bitSize);
}
StructureType::StructureType(Dwarf_Off id)
: Type(id)
{
}
int
StructureType::init(Dwarf_Debug dbg, map<Dwarf_Off, Type*>& types)
{
Dwarf_Error err;
Dwarf_Die die;
Dwarf_Die child;
int rc;
rc = dwarf_offdie(dbg, this->id(), &die, &err);
assert(rc == DW_DLV_OK);
rc = get_attr_udata(die, DW_AT_byte_size, &size_);
assert(rc == 0);
rc = dwarf_child(die, &child, &err);
assert(rc == DW_DLV_OK);
for (;;)
{
rc = this->addMember(dbg, child, types);
assert(rc == 0);
rc = dwarf_siblingof(dbg, child, &child, &err);
assert(rc == DW_DLV_OK || rc == DW_DLV_NO_ENTRY);
if (rc == DW_DLV_NO_ENTRY)
{
break;
}
}
return 0;
}
int
StructureType::dumpData(void* data, int indent, ostream& out)
{
out << '{' << endl;
for (vector<Member>::iterator it = members_.begin();
it != members_.end();
++it)
{
print_indent(indent + 1, out);
out << "." << it->name << " = ";
if (it->bitSize > 0)
{
printBitField(static_cast<char *>(data) + it->offset, it->byteSize, it->bitOffset, it->bitSize, out);
}
else
{
it->type->dumpData(static_cast<char *>(data) + it->offset, indent + 1, out);
}
out << "," << endl;
}
print_indent(indent, out);
out << '}';
return 0;
}
int
StructureType::addMember(Dwarf_Debug dbg, Dwarf_Die die,
map<Dwarf_Off, Type*>& types)
{
Dwarf_Error err;
Dwarf_Attribute attr;
Dwarf_Off typeId;
Dwarf_Bool hasBitSize;
char* str;
Member member;
int rc;
rc = dwarf_diename(die, &str, &err);
assert(rc == DW_DLV_OK);
member.name = str;
rc = getMemberOffset(dbg, die, &member.offset);
assert(rc == 0);
rc = dwarf_attr(die, DW_AT_type, &attr, &err);
assert(rc == DW_DLV_OK);
rc = dwarf_global_formref(attr, &typeId, &err);
assert(rc == DW_DLV_OK);
map<Dwarf_Off, Type*>::iterator it
= types.find(typeId);
assert(it != types.end());
member.type = it->second;
rc = dwarf_hasattr(die, DW_AT_bit_size, &hasBitSize, &err);
assert(rc == DW_DLV_OK);
member.byteSize = 0;
member.bitSize = 0;
member.bitOffset = 0;
if (hasBitSize)
{
rc = get_attr_udata(die, DW_AT_byte_size, &member.byteSize);
assert(rc == DW_DLV_OK);
rc = get_attr_udata(die, DW_AT_bit_size, &member.bitSize);
assert(rc == DW_DLV_OK);
rc = get_attr_udata(die, DW_AT_bit_offset, &member.bitOffset);
assert(rc == DW_DLV_OK);
}
members_.push_back(member);
return 0;
}
Dwarf_Unsigned
StructureType::size() const
{
return size_;
}
bool
StructureType::isCharacter() const
{
return false;
}
namespace {
int
getMemberOffset(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Unsigned* offset)
{
Dwarf_Error err;
Dwarf_Attribute attr;
Dwarf_Locdesc** locList;
Dwarf_Signed numberOfLocList;
int rc;
rc = dwarf_attr(die, DW_AT_data_member_location, &attr, &err);
assert(rc == DW_DLV_OK);
rc = dwarf_loclist_n(attr, &locList, &numberOfLocList, &err);
assert(rc == DW_DLV_OK);
*offset = locList[0]->ld_s->lr_number;
for (Dwarf_Signed i = 0; i < numberOfLocList; i++)
{
dwarf_dealloc(dbg, locList[i]->ld_s, DW_DLA_LOC_BLOCK);
dwarf_dealloc(dbg, locList[i], DW_DLA_LOCDESC);
}
dwarf_dealloc(dbg, locList, DW_DLA_LIST);
return 0;
}
void
printBitField(void* data, Dwarf_Unsigned byteSize,
Dwarf_Unsigned bitOffset, Dwarf_Unsigned bitSize, ostream& out)
{
if (byteSize == 1)
{
out << static_cast<int>(getBitFieldValue(*reinterpret_cast<unsigned char *>(data), bitOffset, bitSize));
}
else if (byteSize == 2)
{
out << getBitFieldValue(*reinterpret_cast<unsigned short *>(data), bitOffset, bitSize);
}
else if (byteSize == 4)
{
out << getBitFieldValue(*reinterpret_cast<unsigned int *>(data), bitOffset, bitSize);
}
else if (byteSize == 8)
{
out << getBitFieldValue(*reinterpret_cast<unsigned long long *>(data), bitOffset, bitSize);
}
}
template<typename T>
T
getBitFieldValue(T val, Dwarf_Unsigned bitOffset, Dwarf_Unsigned bitSize)
{
Dwarf_Unsigned wholeSize = CHAR_BIT * sizeof(T);
Dwarf_Unsigned shift = wholeSize - (bitOffset + bitSize);
T mask = (static_cast<T>(1) << bitSize) - 1;
return (val >> shift) & mask;
}
}