1 //===-- 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 
10 // C Includes
11 // C++ Includes
12 #include <algorithm>
13 #include <set>
14 #include <string>
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/API/SBFrame.h"
19 
20 #include "lldb/lldb-types.h"
21 
22 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
23 #include "lldb/Core/Address.h"
24 #include "lldb/Core/ConstString.h"
25 #include "lldb/Core/Log.h"
26 #include "lldb/Core/Stream.h"
27 #include "lldb/Core/StreamFile.h"
28 #include "lldb/Core/ValueObjectRegister.h"
29 #include "lldb/Core/ValueObjectVariable.h"
30 #include "lldb/Expression/UserExpression.h"
31 #include "lldb/Host/Host.h"
32 #include "lldb/Symbol/Block.h"
33 #include "lldb/Symbol/Function.h"
34 #include "lldb/Symbol/Symbol.h"
35 #include "lldb/Symbol/SymbolContext.h"
36 #include "lldb/Symbol/Variable.h"
37 #include "lldb/Symbol/VariableList.h"
38 #include "lldb/Target/ExecutionContext.h"
39 #include "lldb/Target/Process.h"
40 #include "lldb/Target/RegisterContext.h"
41 #include "lldb/Target/StackFrame.h"
42 #include "lldb/Target/StackID.h"
43 #include "lldb/Target/Target.h"
44 #include "lldb/Target/Thread.h"
45 
46 #include "lldb/API/SBAddress.h"
47 #include "lldb/API/SBDebugger.h"
48 #include "lldb/API/SBExpressionOptions.h"
49 #include "lldb/API/SBStream.h"
50 #include "lldb/API/SBSymbolContext.h"
51 #include "lldb/API/SBThread.h"
52 #include "lldb/API/SBValue.h"
53 #include "lldb/API/SBVariablesOptions.h"
54 
55 using namespace lldb;
56 using namespace lldb_private;
57 
58 SBFrame::SBFrame() : m_opaque_sp(new ExecutionContextRef()) {}
59 
60 SBFrame::SBFrame(const StackFrameSP &lldb_object_sp)
61     : m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {
62   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
63 
64   if (log) {
65     SBStream sstr;
66     GetDescription(sstr);
67     log->Printf("SBFrame::SBFrame (sp=%p) => SBFrame(%p): %s",
68                 static_cast<void *>(lldb_object_sp.get()),
69                 static_cast<void *>(lldb_object_sp.get()), sstr.GetData());
70   }
71 }
72 
73 SBFrame::SBFrame(const SBFrame &rhs)
74     : m_opaque_sp(new ExecutionContextRef(*rhs.m_opaque_sp)) {}
75 
76 SBFrame::~SBFrame() = default;
77 
78 const SBFrame &SBFrame::operator=(const SBFrame &rhs) {
79   if (this != &rhs)
80     *m_opaque_sp = *rhs.m_opaque_sp;
81   return *this;
82 }
83 
84 StackFrameSP SBFrame::GetFrameSP() const {
85   return (m_opaque_sp ? m_opaque_sp->GetFrameSP() : StackFrameSP());
86 }
87 
88 void SBFrame::SetFrameSP(const StackFrameSP &lldb_object_sp) {
89   return m_opaque_sp->SetFrameSP(lldb_object_sp);
90 }
91 
92 bool SBFrame::IsValid() const {
93   std::unique_lock<std::recursive_mutex> lock;
94   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
95 
96   Target *target = exe_ctx.GetTargetPtr();
97   Process *process = exe_ctx.GetProcessPtr();
98   if (target && process) {
99     Process::StopLocker stop_locker;
100     if (stop_locker.TryLock(&process->GetRunLock()))
101       return GetFrameSP().get() != nullptr;
102   }
103 
104   // Without a target & process we can't have a valid stack frame.
105   return false;
106 }
107 
108 SBSymbolContext SBFrame::GetSymbolContext(uint32_t resolve_scope) const {
109   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
110   SBSymbolContext sb_sym_ctx;
111   std::unique_lock<std::recursive_mutex> lock;
112   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
113 
114   StackFrame *frame = nullptr;
115   Target *target = exe_ctx.GetTargetPtr();
116   Process *process = exe_ctx.GetProcessPtr();
117   if (target && process) {
118     Process::StopLocker stop_locker;
119     if (stop_locker.TryLock(&process->GetRunLock())) {
120       frame = exe_ctx.GetFramePtr();
121       if (frame) {
122         sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext(resolve_scope));
123       } else {
124         if (log)
125           log->Printf("SBFrame::GetVariables () => error: could not "
126                       "reconstruct frame object for this SBFrame.");
127       }
128     } else {
129       if (log)
130         log->Printf(
131             "SBFrame::GetSymbolContext () => error: process is running");
132     }
133   }
134 
135   if (log)
136     log->Printf("SBFrame(%p)::GetSymbolContext (resolve_scope=0x%8.8x) => "
137                 "SBSymbolContext(%p)",
138                 static_cast<void *>(frame), resolve_scope,
139                 static_cast<void *>(sb_sym_ctx.get()));
140 
141   return sb_sym_ctx;
142 }
143 
144 SBModule SBFrame::GetModule() const {
145   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
146   SBModule sb_module;
147   ModuleSP module_sp;
148   std::unique_lock<std::recursive_mutex> lock;
149   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
150 
151   StackFrame *frame = nullptr;
152   Target *target = exe_ctx.GetTargetPtr();
153   Process *process = exe_ctx.GetProcessPtr();
154   if (target && process) {
155     Process::StopLocker stop_locker;
156     if (stop_locker.TryLock(&process->GetRunLock())) {
157       frame = exe_ctx.GetFramePtr();
158       if (frame) {
159         module_sp = frame->GetSymbolContext(eSymbolContextModule).module_sp;
160         sb_module.SetSP(module_sp);
161       } else {
162         if (log)
163           log->Printf("SBFrame::GetModule () => error: could not reconstruct "
164                       "frame object for this SBFrame.");
165       }
166     } else {
167       if (log)
168         log->Printf("SBFrame::GetModule () => error: process is running");
169     }
170   }
171 
172   if (log)
173     log->Printf("SBFrame(%p)::GetModule () => SBModule(%p)",
174                 static_cast<void *>(frame),
175                 static_cast<void *>(module_sp.get()));
176 
177   return sb_module;
178 }
179 
180 SBCompileUnit SBFrame::GetCompileUnit() const {
181   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
182   SBCompileUnit sb_comp_unit;
183   std::unique_lock<std::recursive_mutex> lock;
184   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
185 
186   StackFrame *frame = nullptr;
187   Target *target = exe_ctx.GetTargetPtr();
188   Process *process = exe_ctx.GetProcessPtr();
189   if (target && process) {
190     Process::StopLocker stop_locker;
191     if (stop_locker.TryLock(&process->GetRunLock())) {
192       frame = exe_ctx.GetFramePtr();
193       if (frame) {
194         sb_comp_unit.reset(
195             frame->GetSymbolContext(eSymbolContextCompUnit).comp_unit);
196       } else {
197         if (log)
198           log->Printf("SBFrame::GetCompileUnit () => error: could not "
199                       "reconstruct frame object for this SBFrame.");
200       }
201     } else {
202       if (log)
203         log->Printf("SBFrame::GetCompileUnit () => error: process is running");
204     }
205   }
206   if (log)
207     log->Printf("SBFrame(%p)::GetCompileUnit () => SBCompileUnit(%p)",
208                 static_cast<void *>(frame),
209                 static_cast<void *>(sb_comp_unit.get()));
210 
211   return sb_comp_unit;
212 }
213 
214 SBFunction SBFrame::GetFunction() const {
215   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
216   SBFunction sb_function;
217   std::unique_lock<std::recursive_mutex> lock;
218   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
219 
220   StackFrame *frame = nullptr;
221   Target *target = exe_ctx.GetTargetPtr();
222   Process *process = exe_ctx.GetProcessPtr();
223   if (target && process) {
224     Process::StopLocker stop_locker;
225     if (stop_locker.TryLock(&process->GetRunLock())) {
226       frame = exe_ctx.GetFramePtr();
227       if (frame) {
228         sb_function.reset(
229             frame->GetSymbolContext(eSymbolContextFunction).function);
230       } else {
231         if (log)
232           log->Printf("SBFrame::GetFunction () => error: could not reconstruct "
233                       "frame object for this SBFrame.");
234       }
235     } else {
236       if (log)
237         log->Printf("SBFrame::GetFunction () => error: process is running");
238     }
239   }
240   if (log)
241     log->Printf("SBFrame(%p)::GetFunction () => SBFunction(%p)",
242                 static_cast<void *>(frame),
243                 static_cast<void *>(sb_function.get()));
244 
245   return sb_function;
246 }
247 
248 SBSymbol SBFrame::GetSymbol() const {
249   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
250   SBSymbol sb_symbol;
251   std::unique_lock<std::recursive_mutex> lock;
252   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
253 
254   StackFrame *frame = nullptr;
255   Target *target = exe_ctx.GetTargetPtr();
256   Process *process = exe_ctx.GetProcessPtr();
257   if (target && process) {
258     Process::StopLocker stop_locker;
259     if (stop_locker.TryLock(&process->GetRunLock())) {
260       frame = exe_ctx.GetFramePtr();
261       if (frame) {
262         sb_symbol.reset(frame->GetSymbolContext(eSymbolContextSymbol).symbol);
263       } else {
264         if (log)
265           log->Printf("SBFrame::GetSymbol () => error: could not reconstruct "
266                       "frame object for this SBFrame.");
267       }
268     } else {
269       if (log)
270         log->Printf("SBFrame::GetSymbol () => error: process is running");
271     }
272   }
273   if (log)
274     log->Printf("SBFrame(%p)::GetSymbol () => SBSymbol(%p)",
275                 static_cast<void *>(frame),
276                 static_cast<void *>(sb_symbol.get()));
277   return sb_symbol;
278 }
279 
280 SBBlock SBFrame::GetBlock() const {
281   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
282   SBBlock sb_block;
283   std::unique_lock<std::recursive_mutex> lock;
284   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
285 
286   StackFrame *frame = nullptr;
287   Target *target = exe_ctx.GetTargetPtr();
288   Process *process = exe_ctx.GetProcessPtr();
289   if (target && process) {
290     Process::StopLocker stop_locker;
291     if (stop_locker.TryLock(&process->GetRunLock())) {
292       frame = exe_ctx.GetFramePtr();
293       if (frame) {
294         sb_block.SetPtr(frame->GetSymbolContext(eSymbolContextBlock).block);
295       } else {
296         if (log)
297           log->Printf("SBFrame::GetBlock () => error: could not reconstruct "
298                       "frame object for this SBFrame.");
299       }
300     } else {
301       if (log)
302         log->Printf("SBFrame(%p)::GetBlock () => error: process is running",
303                     static_cast<void *>(frame));
304     }
305   }
306   if (log)
307     log->Printf("SBFrame(%p)::GetBlock () => SBBlock(%p)",
308                 static_cast<void *>(frame),
309                 static_cast<void *>(sb_block.GetPtr()));
310   return sb_block;
311 }
312 
313 SBBlock SBFrame::GetFrameBlock() const {
314   SBBlock sb_block;
315   std::unique_lock<std::recursive_mutex> lock;
316   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
317 
318   StackFrame *frame = nullptr;
319   Target *target = exe_ctx.GetTargetPtr();
320   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
321   Process *process = exe_ctx.GetProcessPtr();
322   if (target && process) {
323     Process::StopLocker stop_locker;
324     if (stop_locker.TryLock(&process->GetRunLock())) {
325       frame = exe_ctx.GetFramePtr();
326       if (frame) {
327         sb_block.SetPtr(frame->GetFrameBlock());
328       } else {
329         if (log)
330           log->Printf("SBFrame::GetFrameBlock () => error: could not "
331                       "reconstruct frame object for this SBFrame.");
332       }
333     } else {
334       if (log)
335         log->Printf("SBFrame::GetFrameBlock () => error: process is running");
336     }
337   }
338   if (log)
339     log->Printf("SBFrame(%p)::GetFrameBlock () => SBBlock(%p)",
340                 static_cast<void *>(frame),
341                 static_cast<void *>(sb_block.GetPtr()));
342   return sb_block;
343 }
344 
345 SBLineEntry SBFrame::GetLineEntry() const {
346   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
347   SBLineEntry sb_line_entry;
348   std::unique_lock<std::recursive_mutex> lock;
349   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
350 
351   StackFrame *frame = nullptr;
352   Target *target = exe_ctx.GetTargetPtr();
353   Process *process = exe_ctx.GetProcessPtr();
354   if (target && process) {
355     Process::StopLocker stop_locker;
356     if (stop_locker.TryLock(&process->GetRunLock())) {
357       frame = exe_ctx.GetFramePtr();
358       if (frame) {
359         sb_line_entry.SetLineEntry(
360             frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
361       } else {
362         if (log)
363           log->Printf("SBFrame::GetLineEntry () => error: could not "
364                       "reconstruct frame object for this SBFrame.");
365       }
366     } else {
367       if (log)
368         log->Printf("SBFrame::GetLineEntry () => error: process is running");
369     }
370   }
371   if (log)
372     log->Printf("SBFrame(%p)::GetLineEntry () => SBLineEntry(%p)",
373                 static_cast<void *>(frame),
374                 static_cast<void *>(sb_line_entry.get()));
375   return sb_line_entry;
376 }
377 
378 uint32_t SBFrame::GetFrameID() const {
379   uint32_t frame_idx = UINT32_MAX;
380 
381   std::unique_lock<std::recursive_mutex> lock;
382   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
383 
384   StackFrame *frame = exe_ctx.GetFramePtr();
385   if (frame)
386     frame_idx = frame->GetFrameIndex();
387 
388   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
389   if (log)
390     log->Printf("SBFrame(%p)::GetFrameID () => %u", static_cast<void *>(frame),
391                 frame_idx);
392   return frame_idx;
393 }
394 
395 lldb::addr_t SBFrame::GetCFA() const {
396   std::unique_lock<std::recursive_mutex> lock;
397   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
398 
399   StackFrame *frame = exe_ctx.GetFramePtr();
400   if (frame)
401     return frame->GetStackID().GetCallFrameAddress();
402   return LLDB_INVALID_ADDRESS;
403 }
404 
405 addr_t SBFrame::GetPC() const {
406   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
407   addr_t addr = LLDB_INVALID_ADDRESS;
408   std::unique_lock<std::recursive_mutex> lock;
409   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
410 
411   StackFrame *frame = nullptr;
412   Target *target = exe_ctx.GetTargetPtr();
413   Process *process = exe_ctx.GetProcessPtr();
414   if (target && process) {
415     Process::StopLocker stop_locker;
416     if (stop_locker.TryLock(&process->GetRunLock())) {
417       frame = exe_ctx.GetFramePtr();
418       if (frame) {
419         addr = frame->GetFrameCodeAddress().GetOpcodeLoadAddress(
420             target, eAddressClassCode);
421       } else {
422         if (log)
423           log->Printf("SBFrame::GetPC () => error: could not reconstruct frame "
424                       "object for this SBFrame.");
425       }
426     } else {
427       if (log)
428         log->Printf("SBFrame::GetPC () => error: process is running");
429     }
430   }
431 
432   if (log)
433     log->Printf("SBFrame(%p)::GetPC () => 0x%" PRIx64,
434                 static_cast<void *>(frame), addr);
435 
436   return addr;
437 }
438 
439 bool SBFrame::SetPC(addr_t new_pc) {
440   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
441   bool ret_val = false;
442   std::unique_lock<std::recursive_mutex> lock;
443   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
444 
445   StackFrame *frame = nullptr;
446   Target *target = exe_ctx.GetTargetPtr();
447   Process *process = exe_ctx.GetProcessPtr();
448   if (target && process) {
449     Process::StopLocker stop_locker;
450     if (stop_locker.TryLock(&process->GetRunLock())) {
451       frame = exe_ctx.GetFramePtr();
452       if (frame) {
453         ret_val = frame->GetRegisterContext()->SetPC(new_pc);
454       } else {
455         if (log)
456           log->Printf("SBFrame::SetPC () => error: could not reconstruct frame "
457                       "object for this SBFrame.");
458       }
459     } else {
460       if (log)
461         log->Printf("SBFrame::SetPC () => error: process is running");
462     }
463   }
464 
465   if (log)
466     log->Printf("SBFrame(%p)::SetPC (new_pc=0x%" PRIx64 ") => %i",
467                 static_cast<void *>(frame), new_pc, ret_val);
468 
469   return ret_val;
470 }
471 
472 addr_t SBFrame::GetSP() const {
473   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
474   addr_t addr = LLDB_INVALID_ADDRESS;
475   std::unique_lock<std::recursive_mutex> lock;
476   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
477 
478   StackFrame *frame = nullptr;
479   Target *target = exe_ctx.GetTargetPtr();
480   Process *process = exe_ctx.GetProcessPtr();
481   if (target && process) {
482     Process::StopLocker stop_locker;
483     if (stop_locker.TryLock(&process->GetRunLock())) {
484       frame = exe_ctx.GetFramePtr();
485       if (frame) {
486         addr = frame->GetRegisterContext()->GetSP();
487       } else {
488         if (log)
489           log->Printf("SBFrame::GetSP () => error: could not reconstruct frame "
490                       "object for this SBFrame.");
491       }
492     } else {
493       if (log)
494         log->Printf("SBFrame::GetSP () => error: process is running");
495     }
496   }
497   if (log)
498     log->Printf("SBFrame(%p)::GetSP () => 0x%" PRIx64,
499                 static_cast<void *>(frame), addr);
500 
501   return addr;
502 }
503 
504 addr_t SBFrame::GetFP() const {
505   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
506   addr_t addr = LLDB_INVALID_ADDRESS;
507   std::unique_lock<std::recursive_mutex> lock;
508   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
509 
510   StackFrame *frame = nullptr;
511   Target *target = exe_ctx.GetTargetPtr();
512   Process *process = exe_ctx.GetProcessPtr();
513   if (target && process) {
514     Process::StopLocker stop_locker;
515     if (stop_locker.TryLock(&process->GetRunLock())) {
516       frame = exe_ctx.GetFramePtr();
517       if (frame) {
518         addr = frame->GetRegisterContext()->GetFP();
519       } else {
520         if (log)
521           log->Printf("SBFrame::GetFP () => error: could not reconstruct frame "
522                       "object for this SBFrame.");
523       }
524     } else {
525       if (log)
526         log->Printf("SBFrame::GetFP () => error: process is running");
527     }
528   }
529 
530   if (log)
531     log->Printf("SBFrame(%p)::GetFP () => 0x%" PRIx64,
532                 static_cast<void *>(frame), addr);
533   return addr;
534 }
535 
536 SBAddress SBFrame::GetPCAddress() const {
537   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
538   SBAddress sb_addr;
539   std::unique_lock<std::recursive_mutex> lock;
540   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
541 
542   StackFrame *frame = exe_ctx.GetFramePtr();
543   Target *target = exe_ctx.GetTargetPtr();
544   Process *process = exe_ctx.GetProcessPtr();
545   if (target && process) {
546     Process::StopLocker stop_locker;
547     if (stop_locker.TryLock(&process->GetRunLock())) {
548       frame = exe_ctx.GetFramePtr();
549       if (frame) {
550         sb_addr.SetAddress(&frame->GetFrameCodeAddress());
551       } else {
552         if (log)
553           log->Printf("SBFrame::GetPCAddress () => error: could not "
554                       "reconstruct frame object for this SBFrame.");
555       }
556     } else {
557       if (log)
558         log->Printf("SBFrame::GetPCAddress () => error: process is running");
559     }
560   }
561   if (log)
562     log->Printf("SBFrame(%p)::GetPCAddress () => SBAddress(%p)",
563                 static_cast<void *>(frame), static_cast<void *>(sb_addr.get()));
564   return sb_addr;
565 }
566 
567 void SBFrame::Clear() { m_opaque_sp->Clear(); }
568 
569 lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path) {
570   SBValue sb_value;
571   std::unique_lock<std::recursive_mutex> lock;
572   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
573 
574   StackFrame *frame = exe_ctx.GetFramePtr();
575   Target *target = exe_ctx.GetTargetPtr();
576   if (frame && target) {
577     lldb::DynamicValueType use_dynamic =
578         frame->CalculateTarget()->GetPreferDynamicValue();
579     sb_value = GetValueForVariablePath(var_path, use_dynamic);
580   }
581   return sb_value;
582 }
583 
584 lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path,
585                                                DynamicValueType use_dynamic) {
586   SBValue sb_value;
587   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
588   if (var_path == nullptr || var_path[0] == '\0') {
589     if (log)
590       log->Printf(
591           "SBFrame::GetValueForVariablePath called with empty variable path.");
592     return sb_value;
593   }
594 
595   std::unique_lock<std::recursive_mutex> lock;
596   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
597 
598   StackFrame *frame = nullptr;
599   Target *target = exe_ctx.GetTargetPtr();
600   Process *process = exe_ctx.GetProcessPtr();
601   if (target && process) {
602     Process::StopLocker stop_locker;
603     if (stop_locker.TryLock(&process->GetRunLock())) {
604       frame = exe_ctx.GetFramePtr();
605       if (frame) {
606         VariableSP var_sp;
607         Error error;
608         ValueObjectSP value_sp(frame->GetValueForVariableExpressionPath(
609             var_path, eNoDynamicValues,
610             StackFrame::eExpressionPathOptionCheckPtrVsMember |
611                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess,
612             var_sp, error));
613         sb_value.SetSP(value_sp, use_dynamic);
614       } else {
615         if (log)
616           log->Printf("SBFrame::GetValueForVariablePath () => error: could not "
617                       "reconstruct frame object for this SBFrame.");
618       }
619     } else {
620       if (log)
621         log->Printf(
622             "SBFrame::GetValueForVariablePath () => error: process is running");
623     }
624   }
625   return sb_value;
626 }
627 
628 SBValue SBFrame::FindVariable(const char *name) {
629   SBValue value;
630   std::unique_lock<std::recursive_mutex> lock;
631   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
632 
633   StackFrame *frame = exe_ctx.GetFramePtr();
634   Target *target = exe_ctx.GetTargetPtr();
635   if (frame && target) {
636     lldb::DynamicValueType use_dynamic =
637         frame->CalculateTarget()->GetPreferDynamicValue();
638     value = FindVariable(name, use_dynamic);
639   }
640   return value;
641 }
642 
643 SBValue SBFrame::FindVariable(const char *name,
644                               lldb::DynamicValueType use_dynamic) {
645   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
646   VariableSP var_sp;
647   SBValue sb_value;
648 
649   if (name == nullptr || name[0] == '\0') {
650     if (log)
651       log->Printf("SBFrame::FindVariable called with empty name");
652     return sb_value;
653   }
654 
655   ValueObjectSP value_sp;
656   std::unique_lock<std::recursive_mutex> lock;
657   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
658 
659   StackFrame *frame = nullptr;
660   Target *target = exe_ctx.GetTargetPtr();
661   Process *process = exe_ctx.GetProcessPtr();
662   if (target && process) {
663     Process::StopLocker stop_locker;
664     if (stop_locker.TryLock(&process->GetRunLock())) {
665       frame = exe_ctx.GetFramePtr();
666       if (frame) {
667         VariableList variable_list;
668         SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
669 
670         if (sc.block) {
671           const bool can_create = true;
672           const bool get_parent_variables = true;
673           const bool stop_if_block_is_inlined_function = true;
674 
675           if (sc.block->AppendVariables(
676                   can_create, get_parent_variables,
677                   stop_if_block_is_inlined_function,
678                   [frame](Variable *v) { return v->IsInScope(frame); },
679                   &variable_list)) {
680             var_sp = variable_list.FindVariable(ConstString(name));
681           }
682         }
683 
684         if (var_sp) {
685           value_sp =
686               frame->GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
687           sb_value.SetSP(value_sp, use_dynamic);
688         }
689       } else {
690         if (log)
691           log->Printf("SBFrame::FindVariable () => error: could not "
692                       "reconstruct frame object for this SBFrame.");
693       }
694     } else {
695       if (log)
696         log->Printf("SBFrame::FindVariable () => error: process is running");
697     }
698   }
699 
700   if (log)
701     log->Printf("SBFrame(%p)::FindVariable (name=\"%s\") => SBValue(%p)",
702                 static_cast<void *>(frame), name,
703                 static_cast<void *>(value_sp.get()));
704 
705   return sb_value;
706 }
707 
708 SBValue SBFrame::FindValue(const char *name, ValueType value_type) {
709   SBValue value;
710   std::unique_lock<std::recursive_mutex> lock;
711   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
712 
713   StackFrame *frame = exe_ctx.GetFramePtr();
714   Target *target = exe_ctx.GetTargetPtr();
715   if (frame && target) {
716     lldb::DynamicValueType use_dynamic =
717         frame->CalculateTarget()->GetPreferDynamicValue();
718     value = FindValue(name, value_type, use_dynamic);
719   }
720   return value;
721 }
722 
723 SBValue SBFrame::FindValue(const char *name, ValueType value_type,
724                            lldb::DynamicValueType use_dynamic) {
725   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
726   SBValue sb_value;
727 
728   if (name == nullptr || name[0] == '\0') {
729     if (log)
730       log->Printf("SBFrame::FindValue called with empty name.");
731     return sb_value;
732   }
733 
734   ValueObjectSP value_sp;
735   std::unique_lock<std::recursive_mutex> lock;
736   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
737 
738   StackFrame *frame = nullptr;
739   Target *target = exe_ctx.GetTargetPtr();
740   Process *process = exe_ctx.GetProcessPtr();
741   if (target && process) {
742     Process::StopLocker stop_locker;
743     if (stop_locker.TryLock(&process->GetRunLock())) {
744       frame = exe_ctx.GetFramePtr();
745       if (frame) {
746         VariableList variable_list;
747 
748         switch (value_type) {
749         case eValueTypeVariableGlobal:      // global variable
750         case eValueTypeVariableStatic:      // static variable
751         case eValueTypeVariableArgument:    // function argument variables
752         case eValueTypeVariableLocal:       // function local variables
753         case eValueTypeVariableThreadLocal: // thread local variables
754         {
755           SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
756 
757           const bool can_create = true;
758           const bool get_parent_variables = true;
759           const bool stop_if_block_is_inlined_function = true;
760 
761           if (sc.block)
762             sc.block->AppendVariables(
763                 can_create, get_parent_variables,
764                 stop_if_block_is_inlined_function,
765                 [frame](Variable *v) { return v->IsInScope(frame); },
766                 &variable_list);
767           if (value_type == eValueTypeVariableGlobal) {
768             const bool get_file_globals = true;
769             VariableList *frame_vars = frame->GetVariableList(get_file_globals);
770             if (frame_vars)
771               frame_vars->AppendVariablesIfUnique(variable_list);
772           }
773           ConstString const_name(name);
774           VariableSP variable_sp(
775               variable_list.FindVariable(const_name, value_type));
776           if (variable_sp) {
777             value_sp = frame->GetValueObjectForFrameVariable(variable_sp,
778                                                              eNoDynamicValues);
779             sb_value.SetSP(value_sp, use_dynamic);
780           }
781         } break;
782 
783         case eValueTypeRegister: // stack frame register value
784         {
785           RegisterContextSP reg_ctx(frame->GetRegisterContext());
786           if (reg_ctx) {
787             const uint32_t num_regs = reg_ctx->GetRegisterCount();
788             for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
789               const RegisterInfo *reg_info =
790                   reg_ctx->GetRegisterInfoAtIndex(reg_idx);
791               if (reg_info &&
792                   ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
793                    (reg_info->alt_name &&
794                     strcasecmp(reg_info->alt_name, name) == 0))) {
795                 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
796                 sb_value.SetSP(value_sp);
797                 break;
798               }
799             }
800           }
801         } break;
802 
803         case eValueTypeRegisterSet: // A collection of stack frame register
804                                     // values
805         {
806           RegisterContextSP reg_ctx(frame->GetRegisterContext());
807           if (reg_ctx) {
808             const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
809             for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
810               const RegisterSet *reg_set = reg_ctx->GetRegisterSet(set_idx);
811               if (reg_set &&
812                   ((reg_set->name && strcasecmp(reg_set->name, name) == 0) ||
813                    (reg_set->short_name &&
814                     strcasecmp(reg_set->short_name, name) == 0))) {
815                 value_sp =
816                     ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx);
817                 sb_value.SetSP(value_sp);
818                 break;
819               }
820             }
821           }
822         } break;
823 
824         case eValueTypeConstResult: // constant result variables
825         {
826           ConstString const_name(name);
827           ExpressionVariableSP expr_var_sp(
828               target->GetPersistentVariable(const_name));
829           if (expr_var_sp) {
830             value_sp = expr_var_sp->GetValueObject();
831             sb_value.SetSP(value_sp, use_dynamic);
832           }
833         } break;
834 
835         default:
836           break;
837         }
838       } else {
839         if (log)
840           log->Printf("SBFrame::FindValue () => error: could not reconstruct "
841                       "frame object for this SBFrame.");
842       }
843     } else {
844       if (log)
845         log->Printf("SBFrame::FindValue () => error: process is running");
846     }
847   }
848 
849   if (log)
850     log->Printf("SBFrame(%p)::FindVariableInScope (name=\"%s\", value_type=%i) "
851                 "=> SBValue(%p)",
852                 static_cast<void *>(frame), name, value_type,
853                 static_cast<void *>(value_sp.get()));
854 
855   return sb_value;
856 }
857 
858 bool SBFrame::IsEqual(const SBFrame &that) const {
859   lldb::StackFrameSP this_sp = GetFrameSP();
860   lldb::StackFrameSP that_sp = that.GetFrameSP();
861   return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID());
862 }
863 
864 bool SBFrame::operator==(const SBFrame &rhs) const { return IsEqual(rhs); }
865 
866 bool SBFrame::operator!=(const SBFrame &rhs) const { return !IsEqual(rhs); }
867 
868 SBThread SBFrame::GetThread() const {
869   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
870 
871   std::unique_lock<std::recursive_mutex> lock;
872   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
873 
874   ThreadSP thread_sp(exe_ctx.GetThreadSP());
875   SBThread sb_thread(thread_sp);
876 
877   if (log) {
878     SBStream sstr;
879     sb_thread.GetDescription(sstr);
880     log->Printf("SBFrame(%p)::GetThread () => SBThread(%p): %s",
881                 static_cast<void *>(exe_ctx.GetFramePtr()),
882                 static_cast<void *>(thread_sp.get()), sstr.GetData());
883   }
884 
885   return sb_thread;
886 }
887 
888 const char *SBFrame::Disassemble() const {
889   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
890   const char *disassembly = nullptr;
891   std::unique_lock<std::recursive_mutex> lock;
892   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
893 
894   StackFrame *frame = nullptr;
895   Target *target = exe_ctx.GetTargetPtr();
896   Process *process = exe_ctx.GetProcessPtr();
897   if (target && process) {
898     Process::StopLocker stop_locker;
899     if (stop_locker.TryLock(&process->GetRunLock())) {
900       frame = exe_ctx.GetFramePtr();
901       if (frame) {
902         disassembly = frame->Disassemble();
903       } else {
904         if (log)
905           log->Printf("SBFrame::Disassemble () => error: could not reconstruct "
906                       "frame object for this SBFrame.");
907       }
908     } else {
909       if (log)
910         log->Printf("SBFrame::Disassemble () => error: process is running");
911     }
912   }
913 
914   if (log)
915     log->Printf("SBFrame(%p)::Disassemble () => %s", static_cast<void *>(frame),
916                 disassembly);
917 
918   return disassembly;
919 }
920 
921 SBValueList SBFrame::GetVariables(bool arguments, bool locals, bool statics,
922                                   bool in_scope_only) {
923   SBValueList value_list;
924   std::unique_lock<std::recursive_mutex> lock;
925   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
926 
927   StackFrame *frame = exe_ctx.GetFramePtr();
928   Target *target = exe_ctx.GetTargetPtr();
929   if (frame && target) {
930     lldb::DynamicValueType use_dynamic =
931         frame->CalculateTarget()->GetPreferDynamicValue();
932     const bool include_runtime_support_values =
933         target ? target->GetDisplayRuntimeSupportValues() : false;
934 
935     SBVariablesOptions options;
936     options.SetIncludeArguments(arguments);
937     options.SetIncludeLocals(locals);
938     options.SetIncludeStatics(statics);
939     options.SetInScopeOnly(in_scope_only);
940     options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
941     options.SetUseDynamic(use_dynamic);
942 
943     value_list = GetVariables(options);
944   }
945   return value_list;
946 }
947 
948 lldb::SBValueList SBFrame::GetVariables(bool arguments, bool locals,
949                                         bool statics, bool in_scope_only,
950                                         lldb::DynamicValueType use_dynamic) {
951   std::unique_lock<std::recursive_mutex> lock;
952   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
953 
954   Target *target = exe_ctx.GetTargetPtr();
955   const bool include_runtime_support_values =
956       target ? target->GetDisplayRuntimeSupportValues() : false;
957   SBVariablesOptions options;
958   options.SetIncludeArguments(arguments);
959   options.SetIncludeLocals(locals);
960   options.SetIncludeStatics(statics);
961   options.SetInScopeOnly(in_scope_only);
962   options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
963   options.SetUseDynamic(use_dynamic);
964   return GetVariables(options);
965 }
966 
967 SBValueList SBFrame::GetVariables(const lldb::SBVariablesOptions &options) {
968   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
969 
970   SBValueList value_list;
971   std::unique_lock<std::recursive_mutex> lock;
972   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
973 
974   StackFrame *frame = nullptr;
975   Target *target = exe_ctx.GetTargetPtr();
976 
977   const bool statics = options.GetIncludeStatics();
978   const bool arguments = options.GetIncludeArguments();
979   const bool locals = options.GetIncludeLocals();
980   const bool in_scope_only = options.GetInScopeOnly();
981   const bool include_runtime_support_values =
982       options.GetIncludeRuntimeSupportValues();
983   const lldb::DynamicValueType use_dynamic = options.GetUseDynamic();
984 
985   if (log)
986     log->Printf("SBFrame::GetVariables (arguments=%i, locals=%i, statics=%i, "
987                 "in_scope_only=%i runtime=%i dynamic=%i)",
988                 arguments, locals, statics, in_scope_only,
989                 include_runtime_support_values, use_dynamic);
990 
991   std::set<VariableSP> variable_set;
992   Process *process = exe_ctx.GetProcessPtr();
993   if (target && process) {
994     Process::StopLocker stop_locker;
995     if (stop_locker.TryLock(&process->GetRunLock())) {
996       frame = exe_ctx.GetFramePtr();
997       if (frame) {
998         size_t i;
999         VariableList *variable_list = nullptr;
1000         variable_list = frame->GetVariableList(true);
1001         if (variable_list) {
1002           const size_t num_variables = variable_list->GetSize();
1003           if (num_variables) {
1004             for (i = 0; i < num_variables; ++i) {
1005               VariableSP variable_sp(variable_list->GetVariableAtIndex(i));
1006               if (variable_sp) {
1007                 bool add_variable = false;
1008                 switch (variable_sp->GetScope()) {
1009                 case eValueTypeVariableGlobal:
1010                 case eValueTypeVariableStatic:
1011                 case eValueTypeVariableThreadLocal:
1012                   add_variable = statics;
1013                   break;
1014 
1015                 case eValueTypeVariableArgument:
1016                   add_variable = arguments;
1017                   break;
1018 
1019                 case eValueTypeVariableLocal:
1020                   add_variable = locals;
1021                   break;
1022 
1023                 default:
1024                   break;
1025                 }
1026                 if (add_variable) {
1027                   // Only add variables once so we don't end up with duplicates
1028                   if (variable_set.find(variable_sp) == variable_set.end())
1029                     variable_set.insert(variable_sp);
1030                   else
1031                     continue;
1032 
1033                   if (in_scope_only && !variable_sp->IsInScope(frame))
1034                     continue;
1035 
1036                   ValueObjectSP valobj_sp(frame->GetValueObjectForFrameVariable(
1037                       variable_sp, eNoDynamicValues));
1038 
1039                   if (!include_runtime_support_values && valobj_sp != nullptr &&
1040                       valobj_sp->IsRuntimeSupportValue())
1041                     continue;
1042 
1043                   SBValue value_sb;
1044                   value_sb.SetSP(valobj_sp, use_dynamic);
1045                   value_list.Append(value_sb);
1046                 }
1047               }
1048             }
1049           }
1050         }
1051       } else {
1052         if (log)
1053           log->Printf("SBFrame::GetVariables () => error: could not "
1054                       "reconstruct frame object for this SBFrame.");
1055       }
1056     } else {
1057       if (log)
1058         log->Printf("SBFrame::GetVariables () => error: process is running");
1059     }
1060   }
1061 
1062   if (log)
1063     log->Printf("SBFrame(%p)::GetVariables (...) => SBValueList(%p)",
1064                 static_cast<void *>(frame),
1065                 static_cast<void *>(value_list.opaque_ptr()));
1066 
1067   return value_list;
1068 }
1069 
1070 SBValueList SBFrame::GetRegisters() {
1071   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1072 
1073   SBValueList value_list;
1074   std::unique_lock<std::recursive_mutex> lock;
1075   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1076 
1077   StackFrame *frame = nullptr;
1078   Target *target = exe_ctx.GetTargetPtr();
1079   Process *process = exe_ctx.GetProcessPtr();
1080   if (target && process) {
1081     Process::StopLocker stop_locker;
1082     if (stop_locker.TryLock(&process->GetRunLock())) {
1083       frame = exe_ctx.GetFramePtr();
1084       if (frame) {
1085         RegisterContextSP reg_ctx(frame->GetRegisterContext());
1086         if (reg_ctx) {
1087           const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
1088           for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
1089             value_list.Append(
1090                 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx));
1091           }
1092         }
1093       } else {
1094         if (log)
1095           log->Printf("SBFrame::GetRegisters () => error: could not "
1096                       "reconstruct frame object for this SBFrame.");
1097       }
1098     } else {
1099       if (log)
1100         log->Printf("SBFrame::GetRegisters () => error: process is running");
1101     }
1102   }
1103 
1104   if (log)
1105     log->Printf("SBFrame(%p)::GetRegisters () => SBValueList(%p)",
1106                 static_cast<void *>(frame),
1107                 static_cast<void *>(value_list.opaque_ptr()));
1108 
1109   return value_list;
1110 }
1111 
1112 SBValue SBFrame::FindRegister(const char *name) {
1113   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1114 
1115   SBValue result;
1116   ValueObjectSP value_sp;
1117   std::unique_lock<std::recursive_mutex> lock;
1118   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1119 
1120   StackFrame *frame = nullptr;
1121   Target *target = exe_ctx.GetTargetPtr();
1122   Process *process = exe_ctx.GetProcessPtr();
1123   if (target && process) {
1124     Process::StopLocker stop_locker;
1125     if (stop_locker.TryLock(&process->GetRunLock())) {
1126       frame = exe_ctx.GetFramePtr();
1127       if (frame) {
1128         RegisterContextSP reg_ctx(frame->GetRegisterContext());
1129         if (reg_ctx) {
1130           const uint32_t num_regs = reg_ctx->GetRegisterCount();
1131           for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
1132             const RegisterInfo *reg_info =
1133                 reg_ctx->GetRegisterInfoAtIndex(reg_idx);
1134             if (reg_info &&
1135                 ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
1136                  (reg_info->alt_name &&
1137                   strcasecmp(reg_info->alt_name, name) == 0))) {
1138               value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
1139               result.SetSP(value_sp);
1140               break;
1141             }
1142           }
1143         }
1144       } else {
1145         if (log)
1146           log->Printf("SBFrame::FindRegister () => error: could not "
1147                       "reconstruct frame object for this SBFrame.");
1148       }
1149     } else {
1150       if (log)
1151         log->Printf("SBFrame::FindRegister () => error: process is running");
1152     }
1153   }
1154 
1155   if (log)
1156     log->Printf("SBFrame(%p)::FindRegister () => SBValue(%p)",
1157                 static_cast<void *>(frame),
1158                 static_cast<void *>(value_sp.get()));
1159 
1160   return result;
1161 }
1162 
1163 bool SBFrame::GetDescription(SBStream &description) {
1164   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1165   Stream &strm = description.ref();
1166 
1167   std::unique_lock<std::recursive_mutex> lock;
1168   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1169 
1170   StackFrame *frame;
1171   Target *target = exe_ctx.GetTargetPtr();
1172   Process *process = exe_ctx.GetProcessPtr();
1173   if (target && process) {
1174     Process::StopLocker stop_locker;
1175     if (stop_locker.TryLock(&process->GetRunLock())) {
1176       frame = exe_ctx.GetFramePtr();
1177       if (frame) {
1178         frame->DumpUsingSettingsFormat(&strm);
1179       } else {
1180         if (log)
1181           log->Printf("SBFrame::GetDescription () => error: could not "
1182                       "reconstruct frame object for this SBFrame.");
1183       }
1184     } else {
1185       if (log)
1186         log->Printf("SBFrame::GetDescription () => error: process is running");
1187     }
1188 
1189   } else
1190     strm.PutCString("No value");
1191 
1192   return true;
1193 }
1194 
1195 SBValue SBFrame::EvaluateExpression(const char *expr) {
1196   SBValue result;
1197   std::unique_lock<std::recursive_mutex> lock;
1198   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1199 
1200   StackFrame *frame = exe_ctx.GetFramePtr();
1201   Target *target = exe_ctx.GetTargetPtr();
1202   if (frame && target) {
1203     SBExpressionOptions options;
1204     lldb::DynamicValueType fetch_dynamic_value =
1205         frame->CalculateTarget()->GetPreferDynamicValue();
1206     options.SetFetchDynamicValue(fetch_dynamic_value);
1207     options.SetUnwindOnError(true);
1208     options.SetIgnoreBreakpoints(true);
1209     if (target->GetLanguage() != eLanguageTypeUnknown)
1210       options.SetLanguage(target->GetLanguage());
1211     else
1212       options.SetLanguage(frame->GetLanguage());
1213     return EvaluateExpression(expr, options);
1214   }
1215   return result;
1216 }
1217 
1218 SBValue
1219 SBFrame::EvaluateExpression(const char *expr,
1220                             lldb::DynamicValueType fetch_dynamic_value) {
1221   SBExpressionOptions options;
1222   options.SetFetchDynamicValue(fetch_dynamic_value);
1223   options.SetUnwindOnError(true);
1224   options.SetIgnoreBreakpoints(true);
1225   std::unique_lock<std::recursive_mutex> lock;
1226   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1227 
1228   StackFrame *frame = exe_ctx.GetFramePtr();
1229   Target *target = exe_ctx.GetTargetPtr();
1230   if (target && target->GetLanguage() != eLanguageTypeUnknown)
1231     options.SetLanguage(target->GetLanguage());
1232   else if (frame)
1233     options.SetLanguage(frame->GetLanguage());
1234   return EvaluateExpression(expr, options);
1235 }
1236 
1237 SBValue SBFrame::EvaluateExpression(const char *expr,
1238                                     lldb::DynamicValueType fetch_dynamic_value,
1239                                     bool unwind_on_error) {
1240   SBExpressionOptions options;
1241   std::unique_lock<std::recursive_mutex> lock;
1242   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1243 
1244   options.SetFetchDynamicValue(fetch_dynamic_value);
1245   options.SetUnwindOnError(unwind_on_error);
1246   options.SetIgnoreBreakpoints(true);
1247   StackFrame *frame = exe_ctx.GetFramePtr();
1248   Target *target = exe_ctx.GetTargetPtr();
1249   if (target && target->GetLanguage() != eLanguageTypeUnknown)
1250     options.SetLanguage(target->GetLanguage());
1251   else if (frame)
1252     options.SetLanguage(frame->GetLanguage());
1253   return EvaluateExpression(expr, options);
1254 }
1255 
1256 lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
1257                                           const SBExpressionOptions &options) {
1258   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1259 
1260 #ifndef LLDB_DISABLE_PYTHON
1261   Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1262 #endif
1263 
1264   ExpressionResults exe_results = eExpressionSetupError;
1265   SBValue expr_result;
1266 
1267   if (expr == nullptr || expr[0] == '\0') {
1268     if (log)
1269       log->Printf(
1270           "SBFrame::EvaluateExpression called with an empty expression");
1271     return expr_result;
1272   }
1273 
1274   ValueObjectSP expr_value_sp;
1275 
1276   std::unique_lock<std::recursive_mutex> lock;
1277   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1278 
1279   if (log)
1280     log->Printf("SBFrame()::EvaluateExpression (expr=\"%s\")...", expr);
1281 
1282   StackFrame *frame = nullptr;
1283   Target *target = exe_ctx.GetTargetPtr();
1284   Process *process = exe_ctx.GetProcessPtr();
1285 
1286   if (target && process) {
1287     Process::StopLocker stop_locker;
1288     if (stop_locker.TryLock(&process->GetRunLock())) {
1289       frame = exe_ctx.GetFramePtr();
1290       if (frame) {
1291         if (target->GetDisplayExpressionsInCrashlogs()) {
1292           StreamString frame_description;
1293           frame->DumpUsingSettingsFormat(&frame_description);
1294           Host::SetCrashDescriptionWithFormat(
1295               "SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value "
1296               "= %u) %s",
1297               expr, options.GetFetchDynamicValue(),
1298               frame_description.GetString().c_str());
1299         }
1300 
1301         exe_results = target->EvaluateExpression(expr, frame, expr_value_sp,
1302                                                  options.ref());
1303         expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
1304 
1305         if (target->GetDisplayExpressionsInCrashlogs())
1306           Host::SetCrashDescription(nullptr);
1307       } 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");
1316     }
1317   }
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;
1333 }
1334 
1335 bool SBFrame::IsInlined() {
1336   return static_cast<const SBFrame *>(this)->IsInlined();
1337 }
1338 
1339 bool 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);
1343 
1344   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) {
1352 
1353         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");
1364     }
1365   }
1366   return false;
1367 }
1368 
1369 const char *SBFrame::GetFunctionName() {
1370   return static_cast<const SBFrame *>(this)->GetFunctionName();
1371 }
1372 
1373 const 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);
1378 
1379   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           }
1398         }
1399 
1400         if (name == nullptr) {
1401           if (sc.function)
1402             name = sc.function->GetName().GetCString();
1403         }
1404 
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");
1417     }
1418   }
1419   return name;
1420 }
1421 
1422 const 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;
1471 }
1472