blob: 47bacb1ef2150e13bcd3020e743078ee2c00db82 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBFrame.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000010// C Includes
11// C++ Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include <algorithm>
Greg Clayton349213f2016-04-29 21:00:38 +000013#include <set>
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000014#include <string>
15
16// Other libraries and framework includes
17// Project includes
18#include "lldb/API/SBFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20#include "lldb/lldb-types.h"
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Address.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000024#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/StreamFile.h"
26#include "lldb/Core/ValueObjectRegister.h"
27#include "lldb/Core/ValueObjectVariable.h"
Jim Ingham151c0322015-09-15 21:13:50 +000028#include "lldb/Expression/UserExpression.h"
Greg Clayton1ba7c4d2011-06-25 04:35:01 +000029#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/Block.h"
Greg Clayton1f746072012-08-29 21:13:06 +000031#include "lldb/Symbol/Function.h"
32#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Symbol/SymbolContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/Variable.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000035#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/Process.h"
38#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000039#include "lldb/Target/StackFrame.h"
Greg Claytonb9556ac2012-01-30 07:41:31 +000040#include "lldb/Target/StackID.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000041#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/Thread.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000043#include "lldb/Utility/ConstString.h"
44#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
Eli Friedman4c5de692010-06-09 07:44:37 +000046#include "lldb/API/SBAddress.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000047#include "lldb/API/SBDebugger.h"
Jim Ingham35e1bda2012-10-16 21:41:58 +000048#include "lldb/API/SBExpressionOptions.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000049#include "lldb/API/SBStream.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000050#include "lldb/API/SBSymbolContext.h"
51#include "lldb/API/SBThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000052#include "lldb/API/SBValue.h"
Zachary Turner51f96ee2015-02-17 17:55:50 +000053#include "lldb/API/SBVariablesOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Sean Callanan237c3ed2016-12-14 21:31:31 +000055#include "llvm/Support/PrettyStackTrace.h"
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb;
58using namespace lldb_private;
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060SBFrame::SBFrame() : m_opaque_sp(new ExecutionContextRef()) {}
61
62SBFrame::SBFrame(const StackFrameSP &lldb_object_sp)
63 : m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {
64 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
65
66 if (log) {
67 SBStream sstr;
68 GetDescription(sstr);
69 log->Printf("SBFrame::SBFrame (sp=%p) => SBFrame(%p): %s",
70 static_cast<void *>(lldb_object_sp.get()),
71 static_cast<void *>(lldb_object_sp.get()), sstr.GetData());
72 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075SBFrame::SBFrame(const SBFrame &rhs)
76 : m_opaque_sp(new ExecutionContextRef(*rhs.m_opaque_sp)) {}
Greg Claytonefabb122010-11-05 23:17:00 +000077
Eugene Zelenkodbb0abb2015-10-31 01:22:59 +000078SBFrame::~SBFrame() = default;
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080const SBFrame &SBFrame::operator=(const SBFrame &rhs) {
81 if (this != &rhs)
82 *m_opaque_sp = *rhs.m_opaque_sp;
83 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086StackFrameSP SBFrame::GetFrameSP() const {
87 return (m_opaque_sp ? m_opaque_sp->GetFrameSP() : StackFrameSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void SBFrame::SetFrameSP(const StackFrameSP &lldb_object_sp) {
91 return m_opaque_sp->SetFrameSP(lldb_object_sp);
Greg Claytonb9556ac2012-01-30 07:41:31 +000092}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094bool SBFrame::IsValid() const {
95 std::unique_lock<std::recursive_mutex> lock;
96 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham7fa7dc32016-05-07 00:54:56 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 Target *target = exe_ctx.GetTargetPtr();
99 Process *process = exe_ctx.GetProcessPtr();
100 if (target && process) {
101 Process::StopLocker stop_locker;
102 if (stop_locker.TryLock(&process->GetRunLock()))
103 return GetFrameSP().get() != nullptr;
104 }
105
106 // Without a target & process we can't have a valid stack frame.
107 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110SBSymbolContext SBFrame::GetSymbolContext(uint32_t resolve_scope) const {
111 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
112 SBSymbolContext sb_sym_ctx;
113 std::unique_lock<std::recursive_mutex> lock;
114 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 StackFrame *frame = nullptr;
117 Target *target = exe_ctx.GetTargetPtr();
118 Process *process = exe_ctx.GetProcessPtr();
119 if (target && process) {
120 Process::StopLocker stop_locker;
121 if (stop_locker.TryLock(&process->GetRunLock())) {
122 frame = exe_ctx.GetFramePtr();
123 if (frame) {
124 sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext(resolve_scope));
125 } else {
Jim Ingham7730b9a2012-11-29 00:26:19 +0000126 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 log->Printf("SBFrame::GetVariables () => error: could not "
128 "reconstruct frame object for this SBFrame.");
129 }
130 } else {
131 if (log)
132 log->Printf(
133 "SBFrame::GetSymbolContext () => error: process is running");
Jim Ingham7730b9a2012-11-29 00:26:19 +0000134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 if (log)
138 log->Printf("SBFrame(%p)::GetSymbolContext (resolve_scope=0x%8.8x) => "
139 "SBSymbolContext(%p)",
140 static_cast<void *>(frame), resolve_scope,
141 static_cast<void *>(sb_sym_ctx.get()));
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 return sb_sym_ctx;
Greg Clayton7edbdfc2012-02-03 07:02:37 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146SBModule SBFrame::GetModule() const {
147 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
148 SBModule sb_module;
149 ModuleSP module_sp;
150 std::unique_lock<std::recursive_mutex> lock;
151 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamc481c7e2016-06-10 00:37:44 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 StackFrame *frame = nullptr;
154 Target *target = exe_ctx.GetTargetPtr();
155 Process *process = exe_ctx.GetProcessPtr();
156 if (target && process) {
157 Process::StopLocker stop_locker;
158 if (stop_locker.TryLock(&process->GetRunLock())) {
159 frame = exe_ctx.GetFramePtr();
160 if (frame) {
161 module_sp = frame->GetSymbolContext(eSymbolContextModule).module_sp;
162 sb_module.SetSP(module_sp);
163 } else {
Jim Ingham7730b9a2012-11-29 00:26:19 +0000164 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 log->Printf("SBFrame::GetModule () => error: could not reconstruct "
166 "frame object for this SBFrame.");
167 }
168 } else {
169 if (log)
170 log->Printf("SBFrame::GetModule () => error: process is running");
Jim Ingham7730b9a2012-11-29 00:26:19 +0000171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 if (log)
175 log->Printf("SBFrame(%p)::GetModule () => SBModule(%p)",
176 static_cast<void *>(frame),
177 static_cast<void *>(module_sp.get()));
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 return sb_module;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182SBCompileUnit SBFrame::GetCompileUnit() const {
183 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
184 SBCompileUnit sb_comp_unit;
185 std::unique_lock<std::recursive_mutex> lock;
186 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamc481c7e2016-06-10 00:37:44 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 StackFrame *frame = nullptr;
189 Target *target = exe_ctx.GetTargetPtr();
190 Process *process = exe_ctx.GetProcessPtr();
191 if (target && process) {
192 Process::StopLocker stop_locker;
193 if (stop_locker.TryLock(&process->GetRunLock())) {
194 frame = exe_ctx.GetFramePtr();
195 if (frame) {
196 sb_comp_unit.reset(
197 frame->GetSymbolContext(eSymbolContextCompUnit).comp_unit);
198 } else {
Jim Ingham7730b9a2012-11-29 00:26:19 +0000199 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 log->Printf("SBFrame::GetCompileUnit () => error: could not "
201 "reconstruct frame object for this SBFrame.");
202 }
203 } else {
204 if (log)
205 log->Printf("SBFrame::GetCompileUnit () => error: process is running");
Jim Ingham7730b9a2012-11-29 00:26:19 +0000206 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 }
208 if (log)
209 log->Printf("SBFrame(%p)::GetCompileUnit () => SBCompileUnit(%p)",
210 static_cast<void *>(frame),
211 static_cast<void *>(sb_comp_unit.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return sb_comp_unit;
214}
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216SBFunction SBFrame::GetFunction() const {
217 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
218 SBFunction sb_function;
219 std::unique_lock<std::recursive_mutex> lock;
220 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 StackFrame *frame = nullptr;
223 Target *target = exe_ctx.GetTargetPtr();
224 Process *process = exe_ctx.GetProcessPtr();
225 if (target && process) {
226 Process::StopLocker stop_locker;
227 if (stop_locker.TryLock(&process->GetRunLock())) {
228 frame = exe_ctx.GetFramePtr();
229 if (frame) {
230 sb_function.reset(
231 frame->GetSymbolContext(eSymbolContextFunction).function);
232 } else {
233 if (log)
234 log->Printf("SBFrame::GetFunction () => error: could not reconstruct "
235 "frame object for this SBFrame.");
236 }
237 } else {
238 if (log)
239 log->Printf("SBFrame::GetFunction () => error: process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
242 if (log)
243 log->Printf("SBFrame(%p)::GetFunction () => SBFunction(%p)",
244 static_cast<void *>(frame),
245 static_cast<void *>(sb_function.get()));
Greg Clayton48381312010-10-30 04:51:46 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 return sb_function;
248}
249
250SBSymbol SBFrame::GetSymbol() const {
251 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
252 SBSymbol sb_symbol;
253 std::unique_lock<std::recursive_mutex> lock;
254 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
255
256 StackFrame *frame = nullptr;
257 Target *target = exe_ctx.GetTargetPtr();
258 Process *process = exe_ctx.GetProcessPtr();
259 if (target && process) {
260 Process::StopLocker stop_locker;
261 if (stop_locker.TryLock(&process->GetRunLock())) {
262 frame = exe_ctx.GetFramePtr();
263 if (frame) {
264 sb_symbol.reset(frame->GetSymbolContext(eSymbolContextSymbol).symbol);
265 } else {
266 if (log)
267 log->Printf("SBFrame::GetSymbol () => error: could not reconstruct "
268 "frame object for this SBFrame.");
269 }
270 } else {
271 if (log)
272 log->Printf("SBFrame::GetSymbol () => error: process is running");
273 }
274 }
275 if (log)
276 log->Printf("SBFrame(%p)::GetSymbol () => SBSymbol(%p)",
277 static_cast<void *>(frame),
278 static_cast<void *>(sb_symbol.get()));
279 return sb_symbol;
280}
281
282SBBlock SBFrame::GetBlock() const {
283 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
284 SBBlock sb_block;
285 std::unique_lock<std::recursive_mutex> lock;
286 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
287
288 StackFrame *frame = nullptr;
289 Target *target = exe_ctx.GetTargetPtr();
290 Process *process = exe_ctx.GetProcessPtr();
291 if (target && process) {
292 Process::StopLocker stop_locker;
293 if (stop_locker.TryLock(&process->GetRunLock())) {
294 frame = exe_ctx.GetFramePtr();
295 if (frame) {
296 sb_block.SetPtr(frame->GetSymbolContext(eSymbolContextBlock).block);
297 } else {
298 if (log)
299 log->Printf("SBFrame::GetBlock () => error: could not reconstruct "
300 "frame object for this SBFrame.");
301 }
302 } else {
303 if (log)
304 log->Printf("SBFrame(%p)::GetBlock () => error: process is running",
305 static_cast<void *>(frame));
306 }
307 }
308 if (log)
309 log->Printf("SBFrame(%p)::GetBlock () => SBBlock(%p)",
310 static_cast<void *>(frame),
311 static_cast<void *>(sb_block.GetPtr()));
312 return sb_block;
313}
314
315SBBlock SBFrame::GetFrameBlock() const {
316 SBBlock sb_block;
317 std::unique_lock<std::recursive_mutex> lock;
318 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
319
320 StackFrame *frame = nullptr;
321 Target *target = exe_ctx.GetTargetPtr();
322 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
323 Process *process = exe_ctx.GetProcessPtr();
324 if (target && process) {
325 Process::StopLocker stop_locker;
326 if (stop_locker.TryLock(&process->GetRunLock())) {
327 frame = exe_ctx.GetFramePtr();
328 if (frame) {
329 sb_block.SetPtr(frame->GetFrameBlock());
330 } else {
331 if (log)
332 log->Printf("SBFrame::GetFrameBlock () => error: could not "
333 "reconstruct frame object for this SBFrame.");
334 }
335 } else {
336 if (log)
337 log->Printf("SBFrame::GetFrameBlock () => error: process is running");
338 }
339 }
340 if (log)
341 log->Printf("SBFrame(%p)::GetFrameBlock () => SBBlock(%p)",
342 static_cast<void *>(frame),
343 static_cast<void *>(sb_block.GetPtr()));
344 return sb_block;
345}
346
347SBLineEntry SBFrame::GetLineEntry() const {
348 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
349 SBLineEntry sb_line_entry;
350 std::unique_lock<std::recursive_mutex> lock;
351 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
352
353 StackFrame *frame = nullptr;
354 Target *target = exe_ctx.GetTargetPtr();
355 Process *process = exe_ctx.GetProcessPtr();
356 if (target && process) {
357 Process::StopLocker stop_locker;
358 if (stop_locker.TryLock(&process->GetRunLock())) {
359 frame = exe_ctx.GetFramePtr();
360 if (frame) {
361 sb_line_entry.SetLineEntry(
362 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
363 } else {
364 if (log)
365 log->Printf("SBFrame::GetLineEntry () => error: could not "
366 "reconstruct frame object for this SBFrame.");
367 }
368 } else {
369 if (log)
370 log->Printf("SBFrame::GetLineEntry () => error: process is running");
371 }
372 }
373 if (log)
374 log->Printf("SBFrame(%p)::GetLineEntry () => SBLineEntry(%p)",
375 static_cast<void *>(frame),
376 static_cast<void *>(sb_line_entry.get()));
377 return sb_line_entry;
378}
379
380uint32_t SBFrame::GetFrameID() const {
381 uint32_t frame_idx = UINT32_MAX;
382
383 std::unique_lock<std::recursive_mutex> lock;
384 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
385
386 StackFrame *frame = exe_ctx.GetFramePtr();
387 if (frame)
388 frame_idx = frame->GetFrameIndex();
389
390 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
391 if (log)
392 log->Printf("SBFrame(%p)::GetFrameID () => %u", static_cast<void *>(frame),
393 frame_idx);
394 return frame_idx;
395}
396
397lldb::addr_t SBFrame::GetCFA() const {
398 std::unique_lock<std::recursive_mutex> lock;
399 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
400
401 StackFrame *frame = exe_ctx.GetFramePtr();
402 if (frame)
403 return frame->GetStackID().GetCallFrameAddress();
404 return LLDB_INVALID_ADDRESS;
405}
406
407addr_t SBFrame::GetPC() const {
408 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
409 addr_t addr = LLDB_INVALID_ADDRESS;
410 std::unique_lock<std::recursive_mutex> lock;
411 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
412
413 StackFrame *frame = nullptr;
414 Target *target = exe_ctx.GetTargetPtr();
415 Process *process = exe_ctx.GetProcessPtr();
416 if (target && process) {
417 Process::StopLocker stop_locker;
418 if (stop_locker.TryLock(&process->GetRunLock())) {
419 frame = exe_ctx.GetFramePtr();
420 if (frame) {
421 addr = frame->GetFrameCodeAddress().GetOpcodeLoadAddress(
422 target, eAddressClassCode);
423 } else {
424 if (log)
425 log->Printf("SBFrame::GetPC () => error: could not reconstruct frame "
426 "object for this SBFrame.");
427 }
428 } else {
429 if (log)
430 log->Printf("SBFrame::GetPC () => error: process is running");
431 }
432 }
433
434 if (log)
435 log->Printf("SBFrame(%p)::GetPC () => 0x%" PRIx64,
436 static_cast<void *>(frame), addr);
437
438 return addr;
439}
440
441bool SBFrame::SetPC(addr_t new_pc) {
442 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
443 bool ret_val = false;
444 std::unique_lock<std::recursive_mutex> lock;
445 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
446
447 StackFrame *frame = nullptr;
448 Target *target = exe_ctx.GetTargetPtr();
449 Process *process = exe_ctx.GetProcessPtr();
450 if (target && process) {
451 Process::StopLocker stop_locker;
452 if (stop_locker.TryLock(&process->GetRunLock())) {
453 frame = exe_ctx.GetFramePtr();
454 if (frame) {
455 ret_val = frame->GetRegisterContext()->SetPC(new_pc);
456 } else {
457 if (log)
458 log->Printf("SBFrame::SetPC () => error: could not reconstruct frame "
459 "object for this SBFrame.");
460 }
461 } else {
462 if (log)
463 log->Printf("SBFrame::SetPC () => error: process is running");
464 }
465 }
466
467 if (log)
468 log->Printf("SBFrame(%p)::SetPC (new_pc=0x%" PRIx64 ") => %i",
469 static_cast<void *>(frame), new_pc, ret_val);
470
471 return ret_val;
472}
473
474addr_t SBFrame::GetSP() const {
475 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
476 addr_t addr = LLDB_INVALID_ADDRESS;
477 std::unique_lock<std::recursive_mutex> lock;
478 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
479
480 StackFrame *frame = nullptr;
481 Target *target = exe_ctx.GetTargetPtr();
482 Process *process = exe_ctx.GetProcessPtr();
483 if (target && process) {
484 Process::StopLocker stop_locker;
485 if (stop_locker.TryLock(&process->GetRunLock())) {
486 frame = exe_ctx.GetFramePtr();
487 if (frame) {
488 addr = frame->GetRegisterContext()->GetSP();
489 } else {
490 if (log)
491 log->Printf("SBFrame::GetSP () => error: could not reconstruct frame "
492 "object for this SBFrame.");
493 }
494 } else {
495 if (log)
496 log->Printf("SBFrame::GetSP () => error: process is running");
497 }
498 }
499 if (log)
500 log->Printf("SBFrame(%p)::GetSP () => 0x%" PRIx64,
501 static_cast<void *>(frame), addr);
502
503 return addr;
504}
505
506addr_t SBFrame::GetFP() const {
507 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
508 addr_t addr = LLDB_INVALID_ADDRESS;
509 std::unique_lock<std::recursive_mutex> lock;
510 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
511
512 StackFrame *frame = nullptr;
513 Target *target = exe_ctx.GetTargetPtr();
514 Process *process = exe_ctx.GetProcessPtr();
515 if (target && process) {
516 Process::StopLocker stop_locker;
517 if (stop_locker.TryLock(&process->GetRunLock())) {
518 frame = exe_ctx.GetFramePtr();
519 if (frame) {
520 addr = frame->GetRegisterContext()->GetFP();
521 } else {
522 if (log)
523 log->Printf("SBFrame::GetFP () => error: could not reconstruct frame "
524 "object for this SBFrame.");
525 }
526 } else {
527 if (log)
528 log->Printf("SBFrame::GetFP () => error: process is running");
529 }
530 }
531
532 if (log)
533 log->Printf("SBFrame(%p)::GetFP () => 0x%" PRIx64,
534 static_cast<void *>(frame), addr);
535 return addr;
536}
537
538SBAddress SBFrame::GetPCAddress() const {
539 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
540 SBAddress sb_addr;
541 std::unique_lock<std::recursive_mutex> lock;
542 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
543
544 StackFrame *frame = exe_ctx.GetFramePtr();
545 Target *target = exe_ctx.GetTargetPtr();
546 Process *process = exe_ctx.GetProcessPtr();
547 if (target && process) {
548 Process::StopLocker stop_locker;
549 if (stop_locker.TryLock(&process->GetRunLock())) {
550 frame = exe_ctx.GetFramePtr();
551 if (frame) {
552 sb_addr.SetAddress(&frame->GetFrameCodeAddress());
553 } else {
554 if (log)
555 log->Printf("SBFrame::GetPCAddress () => error: could not "
556 "reconstruct frame object for this SBFrame.");
557 }
558 } else {
559 if (log)
560 log->Printf("SBFrame::GetPCAddress () => error: process is running");
561 }
562 }
563 if (log)
564 log->Printf("SBFrame(%p)::GetPCAddress () => SBAddress(%p)",
565 static_cast<void *>(frame), static_cast<void *>(sb_addr.get()));
566 return sb_addr;
567}
568
569void SBFrame::Clear() { m_opaque_sp->Clear(); }
570
571lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path) {
572 SBValue sb_value;
573 std::unique_lock<std::recursive_mutex> lock;
574 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
575
576 StackFrame *frame = exe_ctx.GetFramePtr();
577 Target *target = exe_ctx.GetTargetPtr();
578 if (frame && target) {
579 lldb::DynamicValueType use_dynamic =
580 frame->CalculateTarget()->GetPreferDynamicValue();
581 sb_value = GetValueForVariablePath(var_path, use_dynamic);
582 }
583 return sb_value;
584}
585
586lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path,
587 DynamicValueType use_dynamic) {
588 SBValue sb_value;
589 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
590 if (var_path == nullptr || var_path[0] == '\0') {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000591 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 log->Printf(
593 "SBFrame::GetValueForVariablePath called with empty variable path.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594 return sb_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 }
596
597 std::unique_lock<std::recursive_mutex> lock;
598 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
599
600 StackFrame *frame = nullptr;
601 Target *target = exe_ctx.GetTargetPtr();
602 Process *process = exe_ctx.GetProcessPtr();
603 if (target && process) {
604 Process::StopLocker stop_locker;
605 if (stop_locker.TryLock(&process->GetRunLock())) {
606 frame = exe_ctx.GetFramePtr();
607 if (frame) {
608 VariableSP var_sp;
609 Error error;
610 ValueObjectSP value_sp(frame->GetValueForVariableExpressionPath(
611 var_path, eNoDynamicValues,
612 StackFrame::eExpressionPathOptionCheckPtrVsMember |
613 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess,
614 var_sp, error));
615 sb_value.SetSP(value_sp, use_dynamic);
616 } else {
617 if (log)
618 log->Printf("SBFrame::GetValueForVariablePath () => error: could not "
619 "reconstruct frame object for this SBFrame.");
620 }
621 } else {
622 if (log)
623 log->Printf(
624 "SBFrame::GetValueForVariablePath () => error: process is running");
625 }
626 }
627 return sb_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628}
629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630SBValue SBFrame::FindVariable(const char *name) {
631 SBValue value;
632 std::unique_lock<std::recursive_mutex> lock;
633 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
634
635 StackFrame *frame = exe_ctx.GetFramePtr();
636 Target *target = exe_ctx.GetTargetPtr();
637 if (frame && target) {
638 lldb::DynamicValueType use_dynamic =
639 frame->CalculateTarget()->GetPreferDynamicValue();
640 value = FindVariable(name, use_dynamic);
641 }
642 return value;
Johnny Chen35e2ab62012-03-05 19:53:24 +0000643}
644
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645SBValue SBFrame::FindVariable(const char *name,
646 lldb::DynamicValueType use_dynamic) {
647 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
648 VariableSP var_sp;
649 SBValue sb_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (name == nullptr || name[0] == '\0') {
Caroline Ticeceb6b132010-10-26 03:11:13 +0000652 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 log->Printf("SBFrame::FindVariable called with empty name");
654 return sb_value;
655 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 ValueObjectSP value_sp;
658 std::unique_lock<std::recursive_mutex> lock;
659 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
660
661 StackFrame *frame = nullptr;
662 Target *target = exe_ctx.GetTargetPtr();
663 Process *process = exe_ctx.GetProcessPtr();
664 if (target && process) {
665 Process::StopLocker stop_locker;
666 if (stop_locker.TryLock(&process->GetRunLock())) {
667 frame = exe_ctx.GetFramePtr();
668 if (frame) {
669 VariableList variable_list;
670 SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
671
672 if (sc.block) {
673 const bool can_create = true;
674 const bool get_parent_variables = true;
675 const bool stop_if_block_is_inlined_function = true;
676
677 if (sc.block->AppendVariables(
678 can_create, get_parent_variables,
679 stop_if_block_is_inlined_function,
680 [frame](Variable *v) { return v->IsInScope(frame); },
681 &variable_list)) {
682 var_sp = variable_list.FindVariable(ConstString(name));
683 }
684 }
685
686 if (var_sp) {
687 value_sp =
688 frame->GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
689 sb_value.SetSP(value_sp, use_dynamic);
690 }
691 } else {
692 if (log)
693 log->Printf("SBFrame::FindVariable () => error: could not "
694 "reconstruct frame object for this SBFrame.");
695 }
696 } else {
697 if (log)
698 log->Printf("SBFrame::FindVariable () => error: process is running");
699 }
700 }
701
702 if (log)
703 log->Printf("SBFrame(%p)::FindVariable (name=\"%s\") => SBValue(%p)",
704 static_cast<void *>(frame), name,
705 static_cast<void *>(value_sp.get()));
706
707 return sb_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708}
709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710SBValue SBFrame::FindValue(const char *name, ValueType value_type) {
711 SBValue value;
712 std::unique_lock<std::recursive_mutex> lock;
713 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 StackFrame *frame = exe_ctx.GetFramePtr();
716 Target *target = exe_ctx.GetTargetPtr();
717 if (frame && target) {
718 lldb::DynamicValueType use_dynamic =
719 frame->CalculateTarget()->GetPreferDynamicValue();
720 value = FindValue(name, value_type, use_dynamic);
721 }
722 return value;
723}
Greg Clayton48381312010-10-30 04:51:46 +0000724
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725SBValue SBFrame::FindValue(const char *name, ValueType value_type,
726 lldb::DynamicValueType use_dynamic) {
727 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
728 SBValue sb_value;
729
730 if (name == nullptr || name[0] == '\0') {
Greg Clayton48381312010-10-30 04:51:46 +0000731 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 log->Printf("SBFrame::FindValue called with empty name.");
733 return sb_value;
734 }
Greg Clayton48381312010-10-30 04:51:46 +0000735
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 ValueObjectSP value_sp;
737 std::unique_lock<std::recursive_mutex> lock;
738 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 StackFrame *frame = nullptr;
741 Target *target = exe_ctx.GetTargetPtr();
742 Process *process = exe_ctx.GetProcessPtr();
743 if (target && process) {
744 Process::StopLocker stop_locker;
745 if (stop_locker.TryLock(&process->GetRunLock())) {
746 frame = exe_ctx.GetFramePtr();
747 if (frame) {
748 VariableList variable_list;
Jim Inghamc481c7e2016-06-10 00:37:44 +0000749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 switch (value_type) {
751 case eValueTypeVariableGlobal: // global variable
752 case eValueTypeVariableStatic: // static variable
753 case eValueTypeVariableArgument: // function argument variables
754 case eValueTypeVariableLocal: // function local variables
755 case eValueTypeVariableThreadLocal: // thread local variables
756 {
757 SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
758
759 const bool can_create = true;
760 const bool get_parent_variables = true;
761 const bool stop_if_block_is_inlined_function = true;
762
763 if (sc.block)
764 sc.block->AppendVariables(
765 can_create, get_parent_variables,
766 stop_if_block_is_inlined_function,
767 [frame](Variable *v) { return v->IsInScope(frame); },
768 &variable_list);
769 if (value_type == eValueTypeVariableGlobal) {
770 const bool get_file_globals = true;
771 VariableList *frame_vars = frame->GetVariableList(get_file_globals);
772 if (frame_vars)
773 frame_vars->AppendVariablesIfUnique(variable_list);
774 }
775 ConstString const_name(name);
776 VariableSP variable_sp(
777 variable_list.FindVariable(const_name, value_type));
778 if (variable_sp) {
779 value_sp = frame->GetValueObjectForFrameVariable(variable_sp,
780 eNoDynamicValues);
781 sb_value.SetSP(value_sp, use_dynamic);
782 }
783 } break;
784
785 case eValueTypeRegister: // stack frame register value
786 {
787 RegisterContextSP reg_ctx(frame->GetRegisterContext());
788 if (reg_ctx) {
789 const uint32_t num_regs = reg_ctx->GetRegisterCount();
790 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
791 const RegisterInfo *reg_info =
792 reg_ctx->GetRegisterInfoAtIndex(reg_idx);
793 if (reg_info &&
794 ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
795 (reg_info->alt_name &&
796 strcasecmp(reg_info->alt_name, name) == 0))) {
797 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
798 sb_value.SetSP(value_sp);
799 break;
800 }
801 }
802 }
803 } break;
804
805 case eValueTypeRegisterSet: // A collection of stack frame register
806 // values
807 {
808 RegisterContextSP reg_ctx(frame->GetRegisterContext());
809 if (reg_ctx) {
810 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
811 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
812 const RegisterSet *reg_set = reg_ctx->GetRegisterSet(set_idx);
813 if (reg_set &&
814 ((reg_set->name && strcasecmp(reg_set->name, name) == 0) ||
815 (reg_set->short_name &&
816 strcasecmp(reg_set->short_name, name) == 0))) {
817 value_sp =
818 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx);
819 sb_value.SetSP(value_sp);
820 break;
821 }
822 }
823 }
824 } break;
825
826 case eValueTypeConstResult: // constant result variables
827 {
828 ConstString const_name(name);
829 ExpressionVariableSP expr_var_sp(
830 target->GetPersistentVariable(const_name));
831 if (expr_var_sp) {
832 value_sp = expr_var_sp->GetValueObject();
833 sb_value.SetSP(value_sp, use_dynamic);
834 }
835 } break;
836
837 default:
838 break;
839 }
840 } else {
841 if (log)
842 log->Printf("SBFrame::FindValue () => error: could not reconstruct "
843 "frame object for this SBFrame.");
844 }
845 } else {
846 if (log)
847 log->Printf("SBFrame::FindValue () => error: process is running");
Greg Clayton316d4982011-06-18 20:06:08 +0000848 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 }
850
851 if (log)
852 log->Printf("SBFrame(%p)::FindVariableInScope (name=\"%s\", value_type=%i) "
853 "=> SBValue(%p)",
854 static_cast<void *>(frame), name, value_type,
855 static_cast<void *>(value_sp.get()));
856
857 return sb_value;
Jim Ingham78a685a2011-04-16 00:01:13 +0000858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860bool SBFrame::IsEqual(const SBFrame &that) const {
861 lldb::StackFrameSP this_sp = GetFrameSP();
862 lldb::StackFrameSP that_sp = that.GetFrameSP();
863 return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID());
864}
Jim Inghamc481c7e2016-06-10 00:37:44 +0000865
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866bool SBFrame::operator==(const SBFrame &rhs) const { return IsEqual(rhs); }
867
868bool SBFrame::operator!=(const SBFrame &rhs) const { return !IsEqual(rhs); }
869
870SBThread SBFrame::GetThread() const {
871 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
872
873 std::unique_lock<std::recursive_mutex> lock;
874 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
875
876 ThreadSP thread_sp(exe_ctx.GetThreadSP());
877 SBThread sb_thread(thread_sp);
878
879 if (log) {
880 SBStream sstr;
881 sb_thread.GetDescription(sstr);
882 log->Printf("SBFrame(%p)::GetThread () => SBThread(%p): %s",
883 static_cast<void *>(exe_ctx.GetFramePtr()),
884 static_cast<void *>(thread_sp.get()), sstr.GetData());
885 }
886
887 return sb_thread;
888}
889
890const char *SBFrame::Disassemble() const {
891 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
892 const char *disassembly = nullptr;
893 std::unique_lock<std::recursive_mutex> lock;
894 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
895
896 StackFrame *frame = nullptr;
897 Target *target = exe_ctx.GetTargetPtr();
898 Process *process = exe_ctx.GetProcessPtr();
899 if (target && process) {
900 Process::StopLocker stop_locker;
901 if (stop_locker.TryLock(&process->GetRunLock())) {
902 frame = exe_ctx.GetFramePtr();
903 if (frame) {
904 disassembly = frame->Disassemble();
905 } else {
906 if (log)
907 log->Printf("SBFrame::Disassemble () => error: could not reconstruct "
908 "frame object for this SBFrame.");
909 }
910 } else {
911 if (log)
912 log->Printf("SBFrame::Disassemble () => error: process is running");
913 }
914 }
915
916 if (log)
917 log->Printf("SBFrame(%p)::Disassemble () => %s", static_cast<void *>(frame),
918 disassembly);
919
920 return disassembly;
921}
922
923SBValueList SBFrame::GetVariables(bool arguments, bool locals, bool statics,
924 bool in_scope_only) {
925 SBValueList value_list;
926 std::unique_lock<std::recursive_mutex> lock;
927 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
928
929 StackFrame *frame = exe_ctx.GetFramePtr();
930 Target *target = exe_ctx.GetTargetPtr();
931 if (frame && target) {
932 lldb::DynamicValueType use_dynamic =
933 frame->CalculateTarget()->GetPreferDynamicValue();
934 const bool include_runtime_support_values =
935 target ? target->GetDisplayRuntimeSupportValues() : false;
936
Zachary Turner51f96ee2015-02-17 17:55:50 +0000937 SBVariablesOptions options;
938 options.SetIncludeArguments(arguments);
939 options.SetIncludeLocals(locals);
940 options.SetIncludeStatics(statics);
941 options.SetInScopeOnly(in_scope_only);
942 options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
943 options.SetUseDynamic(use_dynamic);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944
945 value_list = GetVariables(options);
946 }
947 return value_list;
Enrico Granata560558e2015-02-11 02:35:39 +0000948}
949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950lldb::SBValueList SBFrame::GetVariables(bool arguments, bool locals,
951 bool statics, bool in_scope_only,
952 lldb::DynamicValueType use_dynamic) {
953 std::unique_lock<std::recursive_mutex> lock;
954 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000955
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 Target *target = exe_ctx.GetTargetPtr();
957 const bool include_runtime_support_values =
958 target ? target->GetDisplayRuntimeSupportValues() : false;
959 SBVariablesOptions options;
960 options.SetIncludeArguments(arguments);
961 options.SetIncludeLocals(locals);
962 options.SetIncludeStatics(statics);
963 options.SetInScopeOnly(in_scope_only);
964 options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
965 options.SetUseDynamic(use_dynamic);
966 return GetVariables(options);
967}
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000968
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969SBValueList SBFrame::GetVariables(const lldb::SBVariablesOptions &options) {
970 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonb9556ac2012-01-30 07:41:31 +0000971
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 SBValueList value_list;
973 std::unique_lock<std::recursive_mutex> lock;
974 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Greg Clayton349213f2016-04-29 21:00:38 +0000975
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 StackFrame *frame = nullptr;
977 Target *target = exe_ctx.GetTargetPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 const bool statics = options.GetIncludeStatics();
980 const bool arguments = options.GetIncludeArguments();
981 const bool locals = options.GetIncludeLocals();
982 const bool in_scope_only = options.GetInScopeOnly();
983 const bool include_runtime_support_values =
984 options.GetIncludeRuntimeSupportValues();
985 const lldb::DynamicValueType use_dynamic = options.GetUseDynamic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 if (log)
988 log->Printf("SBFrame::GetVariables (arguments=%i, locals=%i, statics=%i, "
989 "in_scope_only=%i runtime=%i dynamic=%i)",
990 arguments, locals, statics, in_scope_only,
991 include_runtime_support_values, use_dynamic);
Greg Claytonc982c762010-07-09 20:39:50 +0000992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 std::set<VariableSP> variable_set;
994 Process *process = exe_ctx.GetProcessPtr();
995 if (target && process) {
996 Process::StopLocker stop_locker;
997 if (stop_locker.TryLock(&process->GetRunLock())) {
998 frame = exe_ctx.GetFramePtr();
999 if (frame) {
1000 size_t i;
1001 VariableList *variable_list = nullptr;
1002 variable_list = frame->GetVariableList(true);
1003 if (variable_list) {
1004 const size_t num_variables = variable_list->GetSize();
1005 if (num_variables) {
1006 for (i = 0; i < num_variables; ++i) {
1007 VariableSP variable_sp(variable_list->GetVariableAtIndex(i));
1008 if (variable_sp) {
1009 bool add_variable = false;
1010 switch (variable_sp->GetScope()) {
1011 case eValueTypeVariableGlobal:
1012 case eValueTypeVariableStatic:
1013 case eValueTypeVariableThreadLocal:
1014 add_variable = statics;
1015 break;
Greg Clayton349213f2016-04-29 21:00:38 +00001016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 case eValueTypeVariableArgument:
1018 add_variable = arguments;
1019 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 case eValueTypeVariableLocal:
1022 add_variable = locals;
1023 break;
1024
1025 default:
1026 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 if (add_variable) {
1029 // Only add variables once so we don't end up with duplicates
1030 if (variable_set.find(variable_sp) == variable_set.end())
1031 variable_set.insert(variable_sp);
1032 else
1033 continue;
Caroline Ticeceb6b132010-10-26 03:11:13 +00001034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 if (in_scope_only && !variable_sp->IsInScope(frame))
1036 continue;
Caroline Ticeceb6b132010-10-26 03:11:13 +00001037
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 ValueObjectSP valobj_sp(frame->GetValueObjectForFrameVariable(
1039 variable_sp, eNoDynamicValues));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 if (!include_runtime_support_values && valobj_sp != nullptr &&
1042 valobj_sp->IsRuntimeSupportValue())
1043 continue;
Caroline Ticeceb6b132010-10-26 03:11:13 +00001044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 SBValue value_sb;
1046 value_sb.SetSP(valobj_sp, use_dynamic);
1047 value_list.Append(value_sb);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001048 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 } else {
Jim Ingham7730b9a2012-11-29 00:26:19 +00001054 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 log->Printf("SBFrame::GetVariables () => error: could not "
1056 "reconstruct frame object for this SBFrame.");
1057 }
1058 } else {
1059 if (log)
1060 log->Printf("SBFrame::GetVariables () => error: process is running");
1061 }
1062 }
1063
1064 if (log)
1065 log->Printf("SBFrame(%p)::GetVariables (...) => SBValueList(%p)",
1066 static_cast<void *>(frame),
1067 static_cast<void *>(value_list.opaque_ptr()));
1068
1069 return value_list;
1070}
1071
1072SBValueList SBFrame::GetRegisters() {
1073 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1074
1075 SBValueList value_list;
1076 std::unique_lock<std::recursive_mutex> lock;
1077 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1078
1079 StackFrame *frame = nullptr;
1080 Target *target = exe_ctx.GetTargetPtr();
1081 Process *process = exe_ctx.GetProcessPtr();
1082 if (target && process) {
1083 Process::StopLocker stop_locker;
1084 if (stop_locker.TryLock(&process->GetRunLock())) {
1085 frame = exe_ctx.GetFramePtr();
1086 if (frame) {
1087 RegisterContextSP reg_ctx(frame->GetRegisterContext());
1088 if (reg_ctx) {
1089 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
1090 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
1091 value_list.Append(
1092 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx));
1093 }
1094 }
1095 } else {
1096 if (log)
1097 log->Printf("SBFrame::GetRegisters () => error: could not "
1098 "reconstruct frame object for this SBFrame.");
1099 }
1100 } else {
1101 if (log)
1102 log->Printf("SBFrame::GetRegisters () => error: process is running");
1103 }
1104 }
1105
1106 if (log)
1107 log->Printf("SBFrame(%p)::GetRegisters () => SBValueList(%p)",
1108 static_cast<void *>(frame),
1109 static_cast<void *>(value_list.opaque_ptr()));
1110
1111 return value_list;
1112}
1113
1114SBValue SBFrame::FindRegister(const char *name) {
1115 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1116
1117 SBValue result;
1118 ValueObjectSP value_sp;
1119 std::unique_lock<std::recursive_mutex> lock;
1120 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1121
1122 StackFrame *frame = nullptr;
1123 Target *target = exe_ctx.GetTargetPtr();
1124 Process *process = exe_ctx.GetProcessPtr();
1125 if (target && process) {
1126 Process::StopLocker stop_locker;
1127 if (stop_locker.TryLock(&process->GetRunLock())) {
1128 frame = exe_ctx.GetFramePtr();
1129 if (frame) {
1130 RegisterContextSP reg_ctx(frame->GetRegisterContext());
1131 if (reg_ctx) {
1132 const uint32_t num_regs = reg_ctx->GetRegisterCount();
1133 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
1134 const RegisterInfo *reg_info =
1135 reg_ctx->GetRegisterInfoAtIndex(reg_idx);
1136 if (reg_info &&
1137 ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
1138 (reg_info->alt_name &&
1139 strcasecmp(reg_info->alt_name, name) == 0))) {
1140 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
1141 result.SetSP(value_sp);
1142 break;
1143 }
1144 }
1145 }
1146 } else {
1147 if (log)
1148 log->Printf("SBFrame::FindRegister () => error: could not "
1149 "reconstruct frame object for this SBFrame.");
1150 }
1151 } else {
1152 if (log)
1153 log->Printf("SBFrame::FindRegister () => error: process is running");
1154 }
1155 }
1156
1157 if (log)
1158 log->Printf("SBFrame(%p)::FindRegister () => SBValue(%p)",
1159 static_cast<void *>(frame),
1160 static_cast<void *>(value_sp.get()));
1161
1162 return result;
1163}
1164
1165bool SBFrame::GetDescription(SBStream &description) {
1166 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1167 Stream &strm = description.ref();
1168
1169 std::unique_lock<std::recursive_mutex> lock;
1170 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1171
1172 StackFrame *frame;
1173 Target *target = exe_ctx.GetTargetPtr();
1174 Process *process = exe_ctx.GetProcessPtr();
1175 if (target && process) {
1176 Process::StopLocker stop_locker;
1177 if (stop_locker.TryLock(&process->GetRunLock())) {
1178 frame = exe_ctx.GetFramePtr();
1179 if (frame) {
1180 frame->DumpUsingSettingsFormat(&strm);
1181 } else {
1182 if (log)
1183 log->Printf("SBFrame::GetDescription () => error: could not "
1184 "reconstruct frame object for this SBFrame.");
1185 }
1186 } else {
1187 if (log)
1188 log->Printf("SBFrame::GetDescription () => error: process is running");
Jim Ingham7730b9a2012-11-29 00:26:19 +00001189 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001190
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191 } else
1192 strm.PutCString("No value");
Greg Clayton48381312010-10-30 04:51:46 +00001193
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194 return true;
1195}
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001196
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197SBValue SBFrame::EvaluateExpression(const char *expr) {
1198 SBValue result;
1199 std::unique_lock<std::recursive_mutex> lock;
1200 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Greg Claytonb9556ac2012-01-30 07:41:31 +00001201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 StackFrame *frame = exe_ctx.GetFramePtr();
1203 Target *target = exe_ctx.GetTargetPtr();
1204 if (frame && target) {
1205 SBExpressionOptions options;
1206 lldb::DynamicValueType fetch_dynamic_value =
1207 frame->CalculateTarget()->GetPreferDynamicValue();
1208 options.SetFetchDynamicValue(fetch_dynamic_value);
1209 options.SetUnwindOnError(true);
1210 options.SetIgnoreBreakpoints(true);
1211 if (target->GetLanguage() != eLanguageTypeUnknown)
1212 options.SetLanguage(target->GetLanguage());
1213 else
1214 options.SetLanguage(frame->GetLanguage());
1215 return EvaluateExpression(expr, options);
1216 }
1217 return result;
1218}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220SBValue
1221SBFrame::EvaluateExpression(const char *expr,
1222 lldb::DynamicValueType fetch_dynamic_value) {
1223 SBExpressionOptions options;
1224 options.SetFetchDynamicValue(fetch_dynamic_value);
1225 options.SetUnwindOnError(true);
1226 options.SetIgnoreBreakpoints(true);
1227 std::unique_lock<std::recursive_mutex> lock;
1228 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 StackFrame *frame = exe_ctx.GetFramePtr();
1231 Target *target = exe_ctx.GetTargetPtr();
1232 if (target && target->GetLanguage() != eLanguageTypeUnknown)
1233 options.SetLanguage(target->GetLanguage());
1234 else if (frame)
1235 options.SetLanguage(frame->GetLanguage());
1236 return EvaluateExpression(expr, options);
1237}
Greg Claytonfb6621e2013-12-06 21:59:52 +00001238
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239SBValue SBFrame::EvaluateExpression(const char *expr,
1240 lldb::DynamicValueType fetch_dynamic_value,
1241 bool unwind_on_error) {
1242 SBExpressionOptions options;
1243 std::unique_lock<std::recursive_mutex> lock;
1244 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1245
1246 options.SetFetchDynamicValue(fetch_dynamic_value);
1247 options.SetUnwindOnError(unwind_on_error);
1248 options.SetIgnoreBreakpoints(true);
1249 StackFrame *frame = exe_ctx.GetFramePtr();
1250 Target *target = exe_ctx.GetTargetPtr();
1251 if (target && target->GetLanguage() != eLanguageTypeUnknown)
1252 options.SetLanguage(target->GetLanguage());
1253 else if (frame)
1254 options.SetLanguage(frame->GetLanguage());
1255 return EvaluateExpression(expr, options);
1256}
1257
1258lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
1259 const SBExpressionOptions &options) {
1260 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Jason Molendacf7e2dc2012-02-21 05:33:55 +00001261
1262#ifndef LLDB_DISABLE_PYTHON
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Jason Molendacf7e2dc2012-02-21 05:33:55 +00001264#endif
Greg Clayton48381312010-10-30 04:51:46 +00001265
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266 ExpressionResults exe_results = eExpressionSetupError;
1267 SBValue expr_result;
1268
1269 if (expr == nullptr || expr[0] == '\0') {
1270 if (log)
1271 log->Printf(
1272 "SBFrame::EvaluateExpression called with an empty expression");
Greg Claytoncfd1ace2010-10-31 03:01:06 +00001273 return expr_result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 }
Greg Clayton316d4982011-06-18 20:06:08 +00001275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 ValueObjectSP expr_value_sp;
Oleksiy Vyalov05f75e92015-06-25 17:41:41 +00001277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 std::unique_lock<std::recursive_mutex> lock;
1279 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamc481c7e2016-06-10 00:37:44 +00001280
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 if (log)
1282 log->Printf("SBFrame()::EvaluateExpression (expr=\"%s\")...", expr);
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001283
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 StackFrame *frame = nullptr;
1285 Target *target = exe_ctx.GetTargetPtr();
1286 Process *process = exe_ctx.GetProcessPtr();
1287
1288 if (target && process) {
1289 Process::StopLocker stop_locker;
1290 if (stop_locker.TryLock(&process->GetRunLock())) {
1291 frame = exe_ctx.GetFramePtr();
1292 if (frame) {
Jim Ingham8f7db522016-12-15 00:30:30 +00001293 std::unique_ptr<llvm::PrettyStackTraceFormat> stack_trace;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 if (target->GetDisplayExpressionsInCrashlogs()) {
1295 StreamString frame_description;
1296 frame->DumpUsingSettingsFormat(&frame_description);
Jim Ingham8f7db522016-12-15 00:30:30 +00001297 stack_trace = llvm::make_unique<llvm::PrettyStackTraceFormat>(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 "SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value "
1299 "= %u) %s",
1300 expr, options.GetFetchDynamicValue(),
Zachary Turnerc1564272016-11-16 21:15:24 +00001301 frame_description.GetData());
Greg Clayton7fdf9ef2012-04-05 16:12:35 +00001302 }
Greg Claytonc9858e42012-04-06 02:17:47 +00001303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 exe_results = target->EvaluateExpression(expr, frame, expr_value_sp,
1305 options.ref());
1306 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 } else {
1308 if (log)
1309 log->Printf("SBFrame::EvaluateExpression () => error: could not "
1310 "reconstruct frame object for this SBFrame.");
1311 }
1312 } else {
1313 if (log)
1314 log->Printf(
1315 "SBFrame::EvaluateExpression () => error: process is running");
Greg Clayton316d4982011-06-18 20:06:08 +00001316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 }
1318
1319#ifndef LLDB_DISABLE_PYTHON
1320 if (expr_log)
1321 expr_log->Printf("** [SBFrame::EvaluateExpression] Expression result is "
1322 "%s, summary %s **",
1323 expr_result.GetValue(), expr_result.GetSummary());
1324
1325 if (log)
1326 log->Printf("SBFrame(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
1327 "(execution result=%d)",
1328 static_cast<void *>(frame), expr,
1329 static_cast<void *>(expr_value_sp.get()), exe_results);
1330#endif
1331
1332 return expr_result;
Greg Clayton316d4982011-06-18 20:06:08 +00001333}
1334
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335bool SBFrame::IsInlined() {
1336 return static_cast<const SBFrame *>(this)->IsInlined();
Oleksiy Vyalov05f75e92015-06-25 17:41:41 +00001337}
1338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339bool SBFrame::IsInlined() const {
1340 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1341 std::unique_lock<std::recursive_mutex> lock;
1342 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamc481c7e2016-06-10 00:37:44 +00001343
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 StackFrame *frame = nullptr;
1345 Target *target = exe_ctx.GetTargetPtr();
1346 Process *process = exe_ctx.GetProcessPtr();
1347 if (target && process) {
1348 Process::StopLocker stop_locker;
1349 if (stop_locker.TryLock(&process->GetRunLock())) {
1350 frame = exe_ctx.GetFramePtr();
1351 if (frame) {
Jim Ingham7730b9a2012-11-29 00:26:19 +00001352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
1354 if (block)
1355 return block->GetContainingInlinedBlock() != nullptr;
1356 } else {
1357 if (log)
1358 log->Printf("SBFrame::IsInlined () => error: could not reconstruct "
1359 "frame object for this SBFrame.");
1360 }
1361 } else {
1362 if (log)
1363 log->Printf("SBFrame::IsInlined () => error: process is running");
Greg Clayton316d4982011-06-18 20:06:08 +00001364 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 }
1366 return false;
Greg Clayton316d4982011-06-18 20:06:08 +00001367}
Enrico Granatac1f705c2015-07-06 18:28:46 +00001368
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369const char *SBFrame::GetFunctionName() {
1370 return static_cast<const SBFrame *>(this)->GetFunctionName();
1371}
Jim Inghamc481c7e2016-06-10 00:37:44 +00001372
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373const char *SBFrame::GetFunctionName() const {
1374 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1375 const char *name = nullptr;
1376 std::unique_lock<std::recursive_mutex> lock;
1377 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
Jim Inghamc481c7e2016-06-10 00:37:44 +00001378
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 StackFrame *frame = nullptr;
1380 Target *target = exe_ctx.GetTargetPtr();
1381 Process *process = exe_ctx.GetProcessPtr();
1382 if (target && process) {
1383 Process::StopLocker stop_locker;
1384 if (stop_locker.TryLock(&process->GetRunLock())) {
1385 frame = exe_ctx.GetFramePtr();
1386 if (frame) {
1387 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
1388 eSymbolContextBlock |
1389 eSymbolContextSymbol));
1390 if (sc.block) {
1391 Block *inlined_block = sc.block->GetContainingInlinedBlock();
1392 if (inlined_block) {
1393 const InlineFunctionInfo *inlined_info =
1394 inlined_block->GetInlinedFunctionInfo();
1395 name =
1396 inlined_info->GetName(sc.function->GetLanguage()).AsCString();
1397 }
Enrico Granatac1f705c2015-07-06 18:28:46 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399
1400 if (name == nullptr) {
1401 if (sc.function)
1402 name = sc.function->GetName().GetCString();
Enrico Granatac1f705c2015-07-06 18:28:46 +00001403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404
1405 if (name == nullptr) {
1406 if (sc.symbol)
1407 name = sc.symbol->GetName().GetCString();
1408 }
1409 } else {
1410 if (log)
1411 log->Printf("SBFrame::GetFunctionName () => error: could not "
1412 "reconstruct frame object for this SBFrame.");
1413 }
1414 } else {
1415 if (log)
1416 log->Printf("SBFrame::GetFunctionName() => error: process is running");
Enrico Granatac1f705c2015-07-06 18:28:46 +00001417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 }
1419 return name;
1420}
1421
1422const char *SBFrame::GetDisplayFunctionName() {
1423 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1424 const char *name = nullptr;
1425
1426 std::unique_lock<std::recursive_mutex> lock;
1427 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1428
1429 StackFrame *frame = nullptr;
1430 Target *target = exe_ctx.GetTargetPtr();
1431 Process *process = exe_ctx.GetProcessPtr();
1432 if (target && process) {
1433 Process::StopLocker stop_locker;
1434 if (stop_locker.TryLock(&process->GetRunLock())) {
1435 frame = exe_ctx.GetFramePtr();
1436 if (frame) {
1437 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
1438 eSymbolContextBlock |
1439 eSymbolContextSymbol));
1440 if (sc.block) {
1441 Block *inlined_block = sc.block->GetContainingInlinedBlock();
1442 if (inlined_block) {
1443 const InlineFunctionInfo *inlined_info =
1444 inlined_block->GetInlinedFunctionInfo();
1445 name = inlined_info->GetDisplayName(sc.function->GetLanguage())
1446 .AsCString();
1447 }
1448 }
1449
1450 if (name == nullptr) {
1451 if (sc.function)
1452 name = sc.function->GetDisplayName().GetCString();
1453 }
1454
1455 if (name == nullptr) {
1456 if (sc.symbol)
1457 name = sc.symbol->GetDisplayName().GetCString();
1458 }
1459 } else {
1460 if (log)
1461 log->Printf("SBFrame::GetDisplayFunctionName () => error: could not "
1462 "reconstruct frame object for this SBFrame.");
1463 }
1464 } else {
1465 if (log)
1466 log->Printf(
1467 "SBFrame::GetDisplayFunctionName() => error: process is running");
1468 }
1469 }
1470 return name;
Enrico Granatac1f705c2015-07-06 18:28:46 +00001471}