headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / value / value_nodes / AddressValueNode.cpp
blob3131d217b5cf64ffae40ee4e89a1212e400f714c
1 /*
2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
5 */
8 #include "AddressValueNode.h"
10 #include <new>
12 #include "AddressValue.h"
13 #include "Architecture.h"
14 #include "Tracing.h"
15 #include "Type.h"
16 #include "ValueLoader.h"
17 #include "ValueLocation.h"
18 #include "ValueNodeContainer.h"
21 // #pragma mark - AddressValueNode
24 AddressValueNode::AddressValueNode(ValueNodeChild* nodeChild,
25 AddressType* type)
27 ValueNode(nodeChild),
28 fType(type),
29 fChild(NULL)
31 fType->AcquireReference();
35 AddressValueNode::~AddressValueNode()
37 if (fChild != NULL)
38 fChild->ReleaseReference();
39 fType->ReleaseReference();
43 Type*
44 AddressValueNode::GetType() const
46 return fType;
50 status_t
51 AddressValueNode::ResolvedLocationAndValue(ValueLoader* valueLoader,
52 ValueLocation*& _location, Value*& _value)
54 // get the location
55 ValueLocation* location = NodeChild()->Location();
56 if (location == NULL)
57 return B_BAD_VALUE;
59 TRACE_LOCALS(" TYPE_ADDRESS\n");
61 // get the value type
62 type_code valueType;
63 if (valueLoader->GetArchitecture()->AddressSize() == 4) {
64 valueType = B_UINT32_TYPE;
65 TRACE_LOCALS(" -> 32 bit\n");
66 } else {
67 valueType = B_UINT64_TYPE;
68 TRACE_LOCALS(" -> 64 bit\n");
71 // load the value data
72 BVariant valueData;
73 status_t error = valueLoader->LoadValue(location, valueType, false,
74 valueData);
75 if (error != B_OK)
76 return error;
78 // create the type object
79 Value* value = new(std::nothrow) AddressValue(valueData);
80 if (value == NULL)
81 return B_NO_MEMORY;
83 location->AcquireReference();
84 _location = location;
85 _value = value;
86 return B_OK;
90 status_t
91 AddressValueNode::CreateChildren(TeamTypeInformation* info)
93 if (fChild != NULL)
94 return B_OK;
96 // For function pointers, don't bother creating a child, as there
97 // currently isn't any useful information that can be presented there,
98 // and the address node's value already indicates the instruction pointer
99 // of the target function.
100 // TODO: an eventual future possibility might be for a child node to
101 // indicate the name of the function being pointed to, if target address
102 // is valid.
103 Type* baseType = fType->BaseType();
104 if (baseType != NULL && baseType->Kind() == TYPE_FUNCTION)
105 return B_OK;
107 // construct name
108 BString name = "*";
109 name << Name();
111 // create the child
112 fChild = new(std::nothrow) AddressValueNodeChild(this, name,
113 baseType);
114 if (fChild == NULL)
115 return B_NO_MEMORY;
117 fChild->SetContainer(fContainer);
119 if (fContainer != NULL)
120 fContainer->NotifyValueNodeChildrenCreated(this);
122 return B_OK;
126 int32
127 AddressValueNode::CountChildren() const
129 return fChild != NULL ? 1 : 0;
133 ValueNodeChild*
134 AddressValueNode::ChildAt(int32 index) const
136 return index == 0 ? fChild : NULL;
140 // #pragma mark - AddressValueNodeChild
143 AddressValueNodeChild::AddressValueNodeChild(AddressValueNode* parent,
144 const BString& name, Type* type)
146 fParent(parent),
147 fName(name),
148 fType(type)
150 fType->AcquireReference();
154 AddressValueNodeChild::~AddressValueNodeChild()
156 fType->ReleaseReference();
160 const BString&
161 AddressValueNodeChild::Name() const
163 return fName;
167 Type*
168 AddressValueNodeChild::GetType() const
170 return fType;
174 ValueNode*
175 AddressValueNodeChild::Parent() const
177 return fParent;
181 status_t
182 AddressValueNodeChild::ResolveLocation(ValueLoader* valueLoader,
183 ValueLocation*& _location)
185 // The parent's value is an address pointing to this component.
186 AddressValue* parentValue = dynamic_cast<AddressValue*>(
187 fParent->GetValue());
188 if (parentValue == NULL)
189 return B_BAD_VALUE;
191 // resolve the location
192 ValueLocation* location;
193 status_t error = fType->ResolveObjectDataLocation(parentValue->ToUInt64(),
194 location);
195 if (error != B_OK) {
196 TRACE_LOCALS("AddressValueNodeChild::ResolveLocation(): "
197 "ResolveObjectDataLocation() failed: %s\n", strerror(error));
198 return error;
201 _location = location;
202 return B_OK;