-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
BListValueNode.cpp
367 lines (287 loc) · 8.41 KB
/
BListValueNode.cpp
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
367
/*
* Copyright 2012, Rene Gollent, rene@gollent.com
* Distributed under the terms of the MIT License.
*/
#include "BListValueNode.h"
#include <new>
#include <AutoDeleter.h>
#include "AddressValueNode.h"
#include "Architecture.h"
#include "StringValue.h"
#include "Tracing.h"
#include "Type.h"
#include "TypeLookupConstraints.h"
#include "ValueLoader.h"
#include "ValueLocation.h"
#include "ValueNodeContainer.h"
// maximum number of array elements to show by default
static const int64 kMaxArrayElementCount = 20;
//#pragma mark - BListValueNode::BListElementNodeChild
class BListValueNode::BListElementNodeChild : public ValueNodeChild {
public:
BListElementNodeChild(BListValueNode* parent,
int64 elementIndex, Type* type);
virtual ~BListElementNodeChild();
virtual const BString& Name() const { return fName; };
virtual Type* GetType() const { return fType; };
virtual ValueNode* Parent() const { return fParent; };
virtual status_t ResolveLocation(ValueLoader* valueLoader,
ValueLocation*& _location);
private:
Type* fType;
BListValueNode* fParent;
int64 fElementIndex;
BString fName;
};
BListValueNode::BListElementNodeChild::BListElementNodeChild(
BListValueNode* parent, int64 elementIndex, Type* type)
:
ValueNodeChild(),
fType(type),
fParent(parent),
fElementIndex(elementIndex),
fName()
{
fType->AcquireReference();
fParent->AcquireReference();
fName.SetToFormat("[%" B_PRId64 "]", fElementIndex);
}
BListValueNode::BListElementNodeChild::~BListElementNodeChild()
{
fType->ReleaseReference();
fParent->ReleaseReference();
}
status_t
BListValueNode::BListElementNodeChild::ResolveLocation(
ValueLoader* valueLoader, ValueLocation*& _location)
{
uint8 addressSize = valueLoader->GetArchitecture()->AddressSize();
ValueLocation* location = new(std::nothrow) ValueLocation();
if (location == NULL)
return B_NO_MEMORY;
uint64 listAddress = fParent->fDataLocation.ToUInt64();
listAddress += fElementIndex * addressSize;
ValuePieceLocation piece;
piece.SetToMemory(listAddress);
piece.SetSize(addressSize);
location->AddPiece(piece);
_location = location;
return B_OK;
}
//#pragma mark - BListItemCountNodeChild
class BListValueNode::BListItemCountNodeChild : public ValueNodeChild {
public:
BListItemCountNodeChild(BVariant location,
BListValueNode* parent, Type* type);
virtual ~BListItemCountNodeChild();
virtual const BString& Name() const { return fName; };
virtual Type* GetType() const { return fType; };
virtual ValueNode* Parent() const { return fParent; };
virtual status_t ResolveLocation(ValueLoader* valueLoader,
ValueLocation*& _location);
private:
Type* fType;
BListValueNode* fParent;
BVariant fLocation;
BString fName;
};
BListValueNode::BListItemCountNodeChild::BListItemCountNodeChild(
BVariant location, BListValueNode* parent, Type* type)
:
ValueNodeChild(),
fType(type),
fParent(parent),
fLocation(location),
fName("Capacity")
{
fType->AcquireReference();
fParent->AcquireReference();
}
BListValueNode::BListItemCountNodeChild::~BListItemCountNodeChild()
{
fType->ReleaseReference();
fParent->ReleaseReference();
}
status_t
BListValueNode::BListItemCountNodeChild::ResolveLocation(
ValueLoader* valueLoader, ValueLocation*& _location)
{
ValueLocation* location = new(std::nothrow) ValueLocation();
if (location == NULL)
return B_NO_MEMORY;
ValuePieceLocation piece;
piece.SetToMemory(fLocation.ToUInt64());
piece.SetSize(sizeof(int32));
location->AddPiece(piece);
_location = location;
return B_OK;
}
//#pragma mark - BListValueNode
BListValueNode::BListValueNode(ValueNodeChild* nodeChild,
Type* type)
:
ValueNode(nodeChild),
fType(type),
fLoader(NULL),
fItemCountType(NULL),
fItemCount(0)
{
fType->AcquireReference();
}
BListValueNode::~BListValueNode()
{
fType->ReleaseReference();
for (int32 i = 0; i < fChildren.CountItems(); i++)
fChildren.ItemAt(i)->ReleaseReference();
if (fItemCountType != NULL)
fItemCountType->ReleaseReference();
delete fLoader;
}
Type*
BListValueNode::GetType() const
{
return fType;
}
status_t
BListValueNode::ResolvedLocationAndValue(ValueLoader* valueLoader,
ValueLocation*& _location, Value*& _value)
{
// get the location
ValueLocation* location = NodeChild()->Location();
if (location == NULL)
return B_BAD_VALUE;
// get the value type
type_code valueType;
if (valueLoader->GetArchitecture()->AddressSize() == 4) {
valueType = B_UINT32_TYPE;
TRACE_LOCALS(" -> 32 bit\n");
} else {
valueType = B_UINT64_TYPE;
TRACE_LOCALS(" -> 64 bit\n");
}
// load the value data
status_t error = B_OK;
_location = location;
_value = NULL;
ValueLocation* memberLocation = NULL;
CompoundType* baseType = dynamic_cast<CompoundType*>(fType);
if (baseType->CountTemplateParameters() != 0) {
// for BObjectList we need to walk up
// the hierarchy: BObjectList -> _PointerList_ -> BList
baseType = dynamic_cast<CompoundType*>(baseType->BaseTypeAt(0)
->GetType());
if (baseType == NULL || baseType->Name() != "_PointerList_")
return B_BAD_DATA;
baseType = dynamic_cast<CompoundType*>(baseType->BaseTypeAt(0)
->GetType());
if (baseType == NULL || baseType->Name() != "BList")
return B_BAD_DATA;
}
for (int32 i = 0; i < baseType->CountDataMembers(); i++) {
DataMember* member = baseType->DataMemberAt(i);
if (strcmp(member->Name(), "fObjectList") == 0) {
error = baseType->ResolveDataMemberLocation(member,
*location, memberLocation);
if (error != B_OK) {
TRACE_LOCALS(
"BListValueNode::ResolvedLocationAndValue(): "
"failed to resolve location of header member: %s\n",
strerror(error));
delete memberLocation;
return error;
}
error = valueLoader->LoadValue(memberLocation, valueType,
false, fDataLocation);
delete memberLocation;
if (error != B_OK)
return error;
} else if (strcmp(member->Name(), "fItemCount") == 0) {
error = baseType->ResolveDataMemberLocation(member,
*location, memberLocation);
if (error != B_OK) {
TRACE_LOCALS(
"BListValueNode::ResolvedLocationAndValue(): "
"failed to resolve location of header member: %s\n",
strerror(error));
delete memberLocation;
return error;
}
fItemCountType = member->GetType();
fItemCountType->AcquireReference();
fItemCountLocation = memberLocation->PieceAt(0).address;
BVariant listSize;
error = valueLoader->LoadValue(memberLocation, valueType,
false, listSize);
delete memberLocation;
if (error != B_OK)
return error;
fItemCount = listSize.ToInt32();
TRACE_LOCALS(
"BListValueNode::ResolvedLocationAndValue(): "
"detected list size %" B_PRId32 "\n",
fItemCount);
}
memberLocation = NULL;
}
if (fLoader == NULL) {
fLoader = new(std::nothrow)ValueLoader(*valueLoader);
if (fLoader == NULL)
return B_NO_MEMORY;
}
return B_OK;
}
status_t
BListValueNode::CreateChildren()
{
if (fChildrenCreated)
return B_OK;
BListItemCountNodeChild* countChild = new(std::nothrow)
BListItemCountNodeChild(fItemCountLocation, this, fItemCountType);
if (countChild == NULL)
return B_NO_MEMORY;
countChild->SetContainer(fContainer);
fChildren.AddItem(countChild);
BReference<Type> addressTypeRef;
Type* type = NULL;
CompoundType* objectType = dynamic_cast<CompoundType*>(fType);
if (objectType->CountTemplateParameters() != 0) {
AddressType* addressType = NULL;
status_t result = objectType->TemplateParameterAt(0)->GetType()
->CreateDerivedAddressType(DERIVED_TYPE_POINTER, addressType);
if (result != B_OK)
return result;
type = addressType;
addressTypeRef.SetTo(type, true);
} else {
BString typeName;
TypeLookupConstraints constraints;
constraints.SetTypeKind(TYPE_ADDRESS);
constraints.SetBaseTypeName("void");
status_t result = fLoader->LookupTypeByName(typeName, constraints, type);
if (result != B_OK)
return result;
}
for (int32 i = 0; i < fItemCount && i < kMaxArrayElementCount; i++)
{
BListElementNodeChild* child =
new(std::nothrow) BListElementNodeChild(this, i, type);
if (child == NULL)
return B_NO_MEMORY;
child->SetContainer(fContainer);
fChildren.AddItem(child);
}
fChildrenCreated = true;
if (fContainer != NULL)
fContainer->NotifyValueNodeChildrenCreated(this);
return B_OK;
}
int32
BListValueNode::CountChildren() const
{
return fChildren.CountItems();
}
ValueNodeChild*
BListValueNode::ChildAt(int32 index) const
{
return fChildren.ItemAt(index);
}