vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / StackFrame.cpp
blob6cb760c7d2154be802314c90857e9ba9e56c0920
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "StackFrame.h"
8 #include <new>
10 #include "CpuState.h"
11 #include "FunctionInstance.h"
12 #include "Image.h"
13 #include "StackFrameDebugInfo.h"
14 #include "StackFrameValueInfos.h"
15 #include "StackFrameValues.h"
16 #include "Variable.h"
19 // #pragma mark - StackFrame
22 StackFrame::StackFrame(stack_frame_type type, CpuState* cpuState,
23 target_addr_t frameAddress, target_addr_t instructionPointer,
24 StackFrameDebugInfo* debugInfo)
26 fType(type),
27 fCpuState(cpuState),
28 fPreviousCpuState(NULL),
29 fFrameAddress(frameAddress),
30 fInstructionPointer(instructionPointer),
31 fReturnAddress(0),
32 fDebugInfo(debugInfo),
33 fImage(NULL),
34 fFunction(NULL),
35 fValues(NULL),
36 fValueInfos(NULL)
38 fCpuState->AcquireReference();
39 fDebugInfo->AcquireReference();
43 StackFrame::~StackFrame()
45 for (int32 i = 0; Variable* variable = fParameters.ItemAt(i); i++)
46 variable->ReleaseReference();
48 for (int32 i = 0; Variable* variable = fLocalVariables.ItemAt(i); i++)
49 variable->ReleaseReference();
51 SetImage(NULL);
52 SetFunction(NULL);
53 SetPreviousCpuState(NULL);
55 fDebugInfo->ReleaseReference();
56 fCpuState->ReleaseReference();
60 status_t
61 StackFrame::Init()
63 // create values map
64 fValues = new(std::nothrow) StackFrameValues;
65 if (fValues == NULL)
66 return B_NO_MEMORY;
68 status_t error = fValues->Init();
69 if (error != B_OK)
70 return error;
72 // create value infos map
73 fValueInfos = new(std::nothrow) StackFrameValueInfos;
74 if (fValueInfos == NULL)
75 return B_NO_MEMORY;
77 error = fValueInfos->Init();
78 if (error != B_OK)
79 return error;
81 return B_OK;
85 void
86 StackFrame::SetPreviousCpuState(CpuState* state)
88 if (fPreviousCpuState != NULL)
89 fPreviousCpuState->ReleaseReference();
91 fPreviousCpuState = state;
93 if (fPreviousCpuState != NULL)
94 fPreviousCpuState->AcquireReference();
97 void
98 StackFrame::SetReturnAddress(target_addr_t address)
100 fReturnAddress = address;
104 void
105 StackFrame::SetImage(Image* image)
107 if (fImage != NULL)
108 fImage->ReleaseReference();
110 fImage = image;
112 if (fImage != NULL)
113 fImage->AcquireReference();
117 void
118 StackFrame::SetFunction(FunctionInstance* function)
120 if (fFunction != NULL)
121 fFunction->ReleaseReference();
123 fFunction = function;
125 if (fFunction != NULL)
126 fFunction->AcquireReference();
130 int32
131 StackFrame::CountParameters() const
133 return fParameters.CountItems();
137 Variable*
138 StackFrame::ParameterAt(int32 index) const
140 return fParameters.ItemAt(index);
144 bool
145 StackFrame::AddParameter(Variable* parameter)
147 if (!fParameters.AddItem(parameter))
148 return false;
150 parameter->AcquireReference();
151 return true;
155 int32
156 StackFrame::CountLocalVariables() const
158 return fLocalVariables.CountItems();
162 Variable*
163 StackFrame::LocalVariableAt(int32 index) const
165 return fLocalVariables.ItemAt(index);
169 bool
170 StackFrame::AddLocalVariable(Variable* variable)
172 if (!fLocalVariables.AddItem(variable))
173 return false;
175 variable->AcquireReference();
176 return true;
180 void
181 StackFrame::AddListener(Listener* listener)
183 fListeners.Add(listener);
187 void
188 StackFrame::RemoveListener(Listener* listener)
190 fListeners.Remove(listener);
194 void
195 StackFrame::NotifyValueRetrieved(Variable* variable, TypeComponentPath* path)
197 for (ListenerList::Iterator it = fListeners.GetIterator();
198 Listener* listener = it.Next();) {
199 listener->StackFrameValueRetrieved(this, variable, path);
204 // #pragma mark - StackFrame
207 StackFrame::Listener::~Listener()
212 void
213 StackFrame::Listener::StackFrameValueRetrieved(StackFrame* stackFrame,
214 Variable* variable, TypeComponentPath* path)