1 //===-- SBFrame.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <algorithm>
10 #include <set>
11 #include <string>
12 
13 #include "lldb/API/SBFrame.h"
14 
15 #include "lldb/lldb-types.h"
16 
17 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
18 #include "Utils.h"
19 #include "lldb/Core/Address.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/ValueObjectRegister.h"
22 #include "lldb/Core/ValueObjectVariable.h"
23 #include "lldb/Expression/UserExpression.h"
24 #include "lldb/Host/Host.h"
25 #include "lldb/Symbol/Block.h"
26 #include "lldb/Symbol/Function.h"
27 #include "lldb/Symbol/Symbol.h"
28 #include "lldb/Symbol/SymbolContext.h"
29 #include "lldb/Symbol/Variable.h"
30 #include "lldb/Symbol/VariableList.h"
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/RegisterContext.h"
34 #include "lldb/Target/StackFrame.h"
35 #include "lldb/Target/StackFrameRecognizer.h"
36 #include "lldb/Target/StackID.h"
37 #include "lldb/Target/Target.h"
38 #include "lldb/Target/Thread.h"
39 #include "lldb/Utility/ConstString.h"
40 #include "lldb/Utility/Log.h"
41 #include "lldb/Utility/Stream.h"
42 
43 #include "lldb/API/SBAddress.h"
44 #include "lldb/API/SBDebugger.h"
45 #include "lldb/API/SBExpressionOptions.h"
46 #include "lldb/API/SBStream.h"
47 #include "lldb/API/SBSymbolContext.h"
48 #include "lldb/API/SBThread.h"
49 #include "lldb/API/SBValue.h"
50 #include "lldb/API/SBVariablesOptions.h"
51 
52 #include "llvm/Support/PrettyStackTrace.h"
53 
54 using namespace lldb;
55 using namespace lldb_private;
56 
57 SBFrame::SBFrame() : m_opaque_sp(new ExecutionContextRef()) {}
58 
59 SBFrame::SBFrame(const StackFrameSP &lldb_object_sp)
60     : m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {
61   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
62 
63   if (log) {
64     SBStream sstr;
65     GetDescription(sstr);
66     log->Printf("SBFrame::SBFrame (sp=%p) => SBFrame(%p): %s",
67                 static_cast<void *>(lldb_object_sp.get()),
68                 static_cast<void *>(lldb_object_sp.get()), sstr.GetData());
69   }
70 }
71 
72 SBFrame::SBFrame(const SBFrame &rhs) : m_opaque_sp() {
73   m_opaque_sp = clone(rhs.m_opaque_sp);
74 }
75 
76 SBFrame::~SBFrame() = default;
77 
78 const SBFrame &SBFrame::operator=(const SBFrame &rhs) {
79   if (this != &rhs)
80     m_opaque_sp = clone(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   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
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(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, AddressClass::eCode);
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         Status 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         value_sp = frame->FindVariable(ConstString(name));
668 
669         if (value_sp)
670           sb_value.SetSP(value_sp, use_dynamic);
671       } else {
672         if (log)
673           log->Printf("SBFrame::FindVariable () => error: could not "
674                       "reconstruct frame object for this SBFrame.");
675       }
676     } else {
677       if (log)
678         log->Printf("SBFrame::FindVariable () => error: process is running");
679     }
680   }
681 
682   if (log)
683     log->Printf("SBFrame(%p)::FindVariable (name=\"%s\") => SBValue(%p)",
684                 static_cast<void *>(frame), name,
685                 static_cast<void *>(value_sp.get()));
686 
687   return sb_value;
688 }
689 
690 SBValue SBFrame::FindValue(const char *name, ValueType value_type) {
691   SBValue value;
692   std::unique_lock<std::recursive_mutex> lock;
693   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
694 
695   StackFrame *frame = exe_ctx.GetFramePtr();
696   Target *target = exe_ctx.GetTargetPtr();
697   if (frame && target) {
698     lldb::DynamicValueType use_dynamic =
699         frame->CalculateTarget()->GetPreferDynamicValue();
700     value = FindValue(name, value_type, use_dynamic);
701   }
702   return value;
703 }
704 
705 SBValue SBFrame::FindValue(const char *name, ValueType value_type,
706                            lldb::DynamicValueType use_dynamic) {
707   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
708   SBValue sb_value;
709 
710   if (name == nullptr || name[0] == '\0') {
711     if (log)
712       log->Printf("SBFrame::FindValue called with empty name.");
713     return sb_value;
714   }
715 
716   ValueObjectSP value_sp;
717   std::unique_lock<std::recursive_mutex> lock;
718   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
719 
720   StackFrame *frame = nullptr;
721   Target *target = exe_ctx.GetTargetPtr();
722   Process *process = exe_ctx.GetProcessPtr();
723   if (target && process) {
724     Process::StopLocker stop_locker;
725     if (stop_locker.TryLock(&process->GetRunLock())) {
726       frame = exe_ctx.GetFramePtr();
727       if (frame) {
728         VariableList variable_list;
729 
730         switch (value_type) {
731         case eValueTypeVariableGlobal:      // global variable
732         case eValueTypeVariableStatic:      // static variable
733         case eValueTypeVariableArgument:    // function argument variables
734         case eValueTypeVariableLocal:       // function local variables
735         case eValueTypeVariableThreadLocal: // thread local variables
736         {
737           SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
738 
739           const bool can_create = true;
740           const bool get_parent_variables = true;
741           const bool stop_if_block_is_inlined_function = true;
742 
743           if (sc.block)
744             sc.block->AppendVariables(
745                 can_create, get_parent_variables,
746                 stop_if_block_is_inlined_function,
747                 [frame](Variable *v) { return v->IsInScope(frame); },
748                 &variable_list);
749           if (value_type == eValueTypeVariableGlobal) {
750             const bool get_file_globals = true;
751             VariableList *frame_vars = frame->GetVariableList(get_file_globals);
752             if (frame_vars)
753               frame_vars->AppendVariablesIfUnique(variable_list);
754           }
755           ConstString const_name(name);
756           VariableSP variable_sp(
757               variable_list.FindVariable(const_name, value_type));
758           if (variable_sp) {
759             value_sp = frame->GetValueObjectForFrameVariable(variable_sp,
760                                                              eNoDynamicValues);
761             sb_value.SetSP(value_sp, use_dynamic);
762           }
763         } break;
764 
765         case eValueTypeRegister: // stack frame register value
766         {
767           RegisterContextSP reg_ctx(frame->GetRegisterContext());
768           if (reg_ctx) {
769             const uint32_t num_regs = reg_ctx->GetRegisterCount();
770             for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
771               const RegisterInfo *reg_info =
772                   reg_ctx->GetRegisterInfoAtIndex(reg_idx);
773               if (reg_info &&
774                   ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
775                    (reg_info->alt_name &&
776                     strcasecmp(reg_info->alt_name, name) == 0))) {
777                 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
778                 sb_value.SetSP(value_sp);
779                 break;
780               }
781             }
782           }
783         } break;
784 
785         case eValueTypeRegisterSet: // A collection of stack frame register
786                                     // values
787         {
788           RegisterContextSP reg_ctx(frame->GetRegisterContext());
789           if (reg_ctx) {
790             const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
791             for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
792               const RegisterSet *reg_set = reg_ctx->GetRegisterSet(set_idx);
793               if (reg_set &&
794                   ((reg_set->name && strcasecmp(reg_set->name, name) == 0) ||
795                    (reg_set->short_name &&
796                     strcasecmp(reg_set->short_name, name) == 0))) {
797                 value_sp =
798                     ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx);
799                 sb_value.SetSP(value_sp);
800                 break;
801               }
802             }
803           }
804         } break;
805 
806         case eValueTypeConstResult: // constant result variables
807         {
808           ConstString const_name(name);
809           ExpressionVariableSP expr_var_sp(
810               target->GetPersistentVariable(const_name));
811           if (expr_var_sp) {
812             value_sp = expr_var_sp->GetValueObject();
813             sb_value.SetSP(value_sp, use_dynamic);
814           }
815         } break;
816 
817         default:
818           break;
819         }
820       } else {
821         if (log)
822           log->Printf("SBFrame::FindValue () => error: could not reconstruct "
823                       "frame object for this SBFrame.");
824       }
825     } else {
826       if (log)
827         log->Printf("SBFrame::FindValue () => error: process is running");
828     }
829   }
830 
831   if (log)
832     log->Printf("SBFrame(%p)::FindVariableInScope (name=\"%s\", value_type=%i) "
833                 "=> SBValue(%p)",
834                 static_cast<void *>(frame), name, value_type,
835                 static_cast<void *>(value_sp.get()));
836 
837   return sb_value;
838 }
839 
840 bool SBFrame::IsEqual(const SBFrame &that) const {
841   lldb::StackFrameSP this_sp = GetFrameSP();
842   lldb::StackFrameSP that_sp = that.GetFrameSP();
843   return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID());
844 }
845 
846 bool SBFrame::operator==(const SBFrame &rhs) const { return IsEqual(rhs); }
847 
848 bool SBFrame::operator!=(const SBFrame &rhs) const { return !IsEqual(rhs); }
849 
850 SBThread SBFrame::GetThread() const {
851   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
852 
853   std::unique_lock<std::recursive_mutex> lock;
854   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
855 
856   ThreadSP thread_sp(exe_ctx.GetThreadSP());
857   SBThread sb_thread(thread_sp);
858 
859   if (log) {
860     SBStream sstr;
861     sb_thread.GetDescription(sstr);
862     log->Printf("SBFrame(%p)::GetThread () => SBThread(%p): %s",
863                 static_cast<void *>(exe_ctx.GetFramePtr()),
864                 static_cast<void *>(thread_sp.get()), sstr.GetData());
865   }
866 
867   return sb_thread;
868 }
869 
870 const char *SBFrame::Disassemble() const {
871   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
872   const char *disassembly = nullptr;
873   std::unique_lock<std::recursive_mutex> lock;
874   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
875 
876   StackFrame *frame = nullptr;
877   Target *target = exe_ctx.GetTargetPtr();
878   Process *process = exe_ctx.GetProcessPtr();
879   if (target && process) {
880     Process::StopLocker stop_locker;
881     if (stop_locker.TryLock(&process->GetRunLock())) {
882       frame = exe_ctx.GetFramePtr();
883       if (frame) {
884         disassembly = frame->Disassemble();
885       } else {
886         if (log)
887           log->Printf("SBFrame::Disassemble () => error: could not reconstruct "
888                       "frame object for this SBFrame.");
889       }
890     } else {
891       if (log)
892         log->Printf("SBFrame::Disassemble () => error: process is running");
893     }
894   }
895 
896   if (log)
897     log->Printf("SBFrame(%p)::Disassemble () => %s", static_cast<void *>(frame),
898                 disassembly);
899 
900   return disassembly;
901 }
902 
903 SBValueList SBFrame::GetVariables(bool arguments, bool locals, bool statics,
904                                   bool in_scope_only) {
905   SBValueList value_list;
906   std::unique_lock<std::recursive_mutex> lock;
907   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
908 
909   StackFrame *frame = exe_ctx.GetFramePtr();
910   Target *target = exe_ctx.GetTargetPtr();
911   if (frame && target) {
912     lldb::DynamicValueType use_dynamic =
913         frame->CalculateTarget()->GetPreferDynamicValue();
914     const bool include_runtime_support_values =
915         target ? target->GetDisplayRuntimeSupportValues() : false;
916 
917     SBVariablesOptions options;
918     options.SetIncludeArguments(arguments);
919     options.SetIncludeLocals(locals);
920     options.SetIncludeStatics(statics);
921     options.SetInScopeOnly(in_scope_only);
922     options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
923     options.SetUseDynamic(use_dynamic);
924 
925     value_list = GetVariables(options);
926   }
927   return value_list;
928 }
929 
930 lldb::SBValueList SBFrame::GetVariables(bool arguments, bool locals,
931                                         bool statics, bool in_scope_only,
932                                         lldb::DynamicValueType use_dynamic) {
933   std::unique_lock<std::recursive_mutex> lock;
934   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
935 
936   Target *target = exe_ctx.GetTargetPtr();
937   const bool include_runtime_support_values =
938       target ? target->GetDisplayRuntimeSupportValues() : false;
939   SBVariablesOptions options;
940   options.SetIncludeArguments(arguments);
941   options.SetIncludeLocals(locals);
942   options.SetIncludeStatics(statics);
943   options.SetInScopeOnly(in_scope_only);
944   options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
945   options.SetUseDynamic(use_dynamic);
946   return GetVariables(options);
947 }
948 
949 SBValueList SBFrame::GetVariables(const lldb::SBVariablesOptions &options) {
950   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
951 
952   SBValueList value_list;
953   std::unique_lock<std::recursive_mutex> lock;
954   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
955 
956   StackFrame *frame = nullptr;
957   Target *target = exe_ctx.GetTargetPtr();
958 
959   const bool statics = options.GetIncludeStatics();
960   const bool arguments = options.GetIncludeArguments();
961   const bool recognized_arguments =
962         options.GetIncludeRecognizedArguments(SBTarget(exe_ctx.GetTargetSP()));
963   const bool locals = options.GetIncludeLocals();
964   const bool in_scope_only = options.GetInScopeOnly();
965   const bool include_runtime_support_values =
966       options.GetIncludeRuntimeSupportValues();
967   const lldb::DynamicValueType use_dynamic = options.GetUseDynamic();
968 
969   if (log)
970     log->Printf(
971         "SBFrame::GetVariables (arguments=%i, recognized_arguments=%i, "
972         "locals=%i, statics=%i, in_scope_only=%i runtime=%i dynamic=%i)",
973         arguments, recognized_arguments, locals, statics, in_scope_only,
974         include_runtime_support_values, use_dynamic);
975 
976   std::set<VariableSP> variable_set;
977   Process *process = exe_ctx.GetProcessPtr();
978   if (target && process) {
979     Process::StopLocker stop_locker;
980     if (stop_locker.TryLock(&process->GetRunLock())) {
981       frame = exe_ctx.GetFramePtr();
982       if (frame) {
983         size_t i;
984         VariableList *variable_list = nullptr;
985         variable_list = frame->GetVariableList(true);
986         if (variable_list) {
987           const size_t num_variables = variable_list->GetSize();
988           if (num_variables) {
989             for (i = 0; i < num_variables; ++i) {
990               VariableSP variable_sp(variable_list->GetVariableAtIndex(i));
991               if (variable_sp) {
992                 bool add_variable = false;
993                 switch (variable_sp->GetScope()) {
994                 case eValueTypeVariableGlobal:
995                 case eValueTypeVariableStatic:
996                 case eValueTypeVariableThreadLocal:
997                   add_variable = statics;
998                   break;
999 
1000                 case eValueTypeVariableArgument:
1001                   add_variable = arguments;
1002                   break;
1003 
1004                 case eValueTypeVariableLocal:
1005                   add_variable = locals;
1006                   break;
1007 
1008                 default:
1009                   break;
1010                 }
1011                 if (add_variable) {
1012                   // Only add variables once so we don't end up with duplicates
1013                   if (variable_set.find(variable_sp) == variable_set.end())
1014                     variable_set.insert(variable_sp);
1015                   else
1016                     continue;
1017 
1018                   if (in_scope_only && !variable_sp->IsInScope(frame))
1019                     continue;
1020 
1021                   ValueObjectSP valobj_sp(frame->GetValueObjectForFrameVariable(
1022                       variable_sp, eNoDynamicValues));
1023 
1024                   if (!include_runtime_support_values && valobj_sp != nullptr &&
1025                       valobj_sp->IsRuntimeSupportValue())
1026                     continue;
1027 
1028                   SBValue value_sb;
1029                   value_sb.SetSP(valobj_sp, use_dynamic);
1030                   value_list.Append(value_sb);
1031                 }
1032               }
1033             }
1034           }
1035         }
1036         if (recognized_arguments) {
1037           auto recognized_frame = frame->GetRecognizedFrame();
1038           if (recognized_frame) {
1039             ValueObjectListSP recognized_arg_list =
1040                 recognized_frame->GetRecognizedArguments();
1041             if (recognized_arg_list) {
1042               for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
1043                 SBValue value_sb;
1044                 value_sb.SetSP(rec_value_sp, use_dynamic);
1045                 value_list.Append(value_sb);
1046               }
1047             }
1048           }
1049         }
1050       } else {
1051         if (log)
1052           log->Printf("SBFrame::GetVariables () => error: could not "
1053                       "reconstruct frame object for this SBFrame.");
1054       }
1055     } else {
1056       if (log)
1057         log->Printf("SBFrame::GetVariables () => error: process is running");
1058     }
1059   }
1060 
1061   if (log)
1062     log->Printf("SBFrame(%p)::GetVariables (...) => SBValueList(%p)",
1063                 static_cast<void *>(frame),
1064                 static_cast<void *>(value_list.opaque_ptr()));
1065 
1066   return value_list;
1067 }
1068 
1069 SBValueList SBFrame::GetRegisters() {
1070   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1071 
1072   SBValueList value_list;
1073   std::unique_lock<std::recursive_mutex> lock;
1074   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1075 
1076   StackFrame *frame = nullptr;
1077   Target *target = exe_ctx.GetTargetPtr();
1078   Process *process = exe_ctx.GetProcessPtr();
1079   if (target && process) {
1080     Process::StopLocker stop_locker;
1081     if (stop_locker.TryLock(&process->GetRunLock())) {
1082       frame = exe_ctx.GetFramePtr();
1083       if (frame) {
1084         RegisterContextSP reg_ctx(frame->GetRegisterContext());
1085         if (reg_ctx) {
1086           const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
1087           for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
1088             value_list.Append(
1089                 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx));
1090           }
1091         }
1092       } else {
1093         if (log)
1094           log->Printf("SBFrame::GetRegisters () => error: could not "
1095                       "reconstruct frame object for this SBFrame.");
1096       }
1097     } else {
1098       if (log)
1099         log->Printf("SBFrame::GetRegisters () => error: process is running");
1100     }
1101   }
1102 
1103   if (log)
1104     log->Printf("SBFrame(%p)::GetRegisters () => SBValueList(%p)",
1105                 static_cast<void *>(frame),
1106                 static_cast<void *>(value_list.opaque_ptr()));
1107 
1108   return value_list;
1109 }
1110 
1111 SBValue SBFrame::FindRegister(const char *name) {
1112   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1113 
1114   SBValue result;
1115   ValueObjectSP value_sp;
1116   std::unique_lock<std::recursive_mutex> lock;
1117   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1118 
1119   StackFrame *frame = nullptr;
1120   Target *target = exe_ctx.GetTargetPtr();
1121   Process *process = exe_ctx.GetProcessPtr();
1122   if (target && process) {
1123     Process::StopLocker stop_locker;
1124     if (stop_locker.TryLock(&process->GetRunLock())) {
1125       frame = exe_ctx.GetFramePtr();
1126       if (frame) {
1127         RegisterContextSP reg_ctx(frame->GetRegisterContext());
1128         if (reg_ctx) {
1129           const uint32_t num_regs = reg_ctx->GetRegisterCount();
1130           for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
1131             const RegisterInfo *reg_info =
1132                 reg_ctx->GetRegisterInfoAtIndex(reg_idx);
1133             if (reg_info &&
1134                 ((reg_info->name && strcasecmp(reg_info->name, name) == 0) ||
1135                  (reg_info->alt_name &&
1136                   strcasecmp(reg_info->alt_name, name) == 0))) {
1137               value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_idx);
1138               result.SetSP(value_sp);
1139               break;
1140             }
1141           }
1142         }
1143       } else {
1144         if (log)
1145           log->Printf("SBFrame::FindRegister () => error: could not "
1146                       "reconstruct frame object for this SBFrame.");
1147       }
1148     } else {
1149       if (log)
1150         log->Printf("SBFrame::FindRegister () => error: process is running");
1151     }
1152   }
1153 
1154   if (log)
1155     log->Printf("SBFrame(%p)::FindRegister () => SBValue(%p)",
1156                 static_cast<void *>(frame),
1157                 static_cast<void *>(value_sp.get()));
1158 
1159   return result;
1160 }
1161 
1162 bool SBFrame::GetDescription(SBStream &description) {
1163   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1164   Stream &strm = description.ref();
1165 
1166   std::unique_lock<std::recursive_mutex> lock;
1167   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1168 
1169   StackFrame *frame;
1170   Target *target = exe_ctx.GetTargetPtr();
1171   Process *process = exe_ctx.GetProcessPtr();
1172   if (target && process) {
1173     Process::StopLocker stop_locker;
1174     if (stop_locker.TryLock(&process->GetRunLock())) {
1175       frame = exe_ctx.GetFramePtr();
1176       if (frame) {
1177         frame->DumpUsingSettingsFormat(&strm);
1178       } else {
1179         if (log)
1180           log->Printf("SBFrame::GetDescription () => error: could not "
1181                       "reconstruct frame object for this SBFrame.");
1182       }
1183     } else {
1184       if (log)
1185         log->Printf("SBFrame::GetDescription () => error: process is running");
1186     }
1187 
1188   } else
1189     strm.PutCString("No value");
1190 
1191   return true;
1192 }
1193 
1194 SBValue SBFrame::EvaluateExpression(const char *expr) {
1195   SBValue result;
1196   std::unique_lock<std::recursive_mutex> lock;
1197   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1198 
1199   StackFrame *frame = exe_ctx.GetFramePtr();
1200   Target *target = exe_ctx.GetTargetPtr();
1201   if (frame && target) {
1202     SBExpressionOptions options;
1203     lldb::DynamicValueType fetch_dynamic_value =
1204         frame->CalculateTarget()->GetPreferDynamicValue();
1205     options.SetFetchDynamicValue(fetch_dynamic_value);
1206     options.SetUnwindOnError(true);
1207     options.SetIgnoreBreakpoints(true);
1208     if (target->GetLanguage() != eLanguageTypeUnknown)
1209       options.SetLanguage(target->GetLanguage());
1210     else
1211       options.SetLanguage(frame->GetLanguage());
1212     return EvaluateExpression(expr, options);
1213   }
1214   return result;
1215 }
1216 
1217 SBValue
1218 SBFrame::EvaluateExpression(const char *expr,
1219                             lldb::DynamicValueType fetch_dynamic_value) {
1220   SBExpressionOptions options;
1221   options.SetFetchDynamicValue(fetch_dynamic_value);
1222   options.SetUnwindOnError(true);
1223   options.SetIgnoreBreakpoints(true);
1224   std::unique_lock<std::recursive_mutex> lock;
1225   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1226 
1227   StackFrame *frame = exe_ctx.GetFramePtr();
1228   Target *target = exe_ctx.GetTargetPtr();
1229   if (target && target->GetLanguage() != eLanguageTypeUnknown)
1230     options.SetLanguage(target->GetLanguage());
1231   else if (frame)
1232     options.SetLanguage(frame->GetLanguage());
1233   return EvaluateExpression(expr, options);
1234 }
1235 
1236 SBValue SBFrame::EvaluateExpression(const char *expr,
1237                                     lldb::DynamicValueType fetch_dynamic_value,
1238                                     bool unwind_on_error) {
1239   SBExpressionOptions options;
1240   std::unique_lock<std::recursive_mutex> lock;
1241   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1242 
1243   options.SetFetchDynamicValue(fetch_dynamic_value);
1244   options.SetUnwindOnError(unwind_on_error);
1245   options.SetIgnoreBreakpoints(true);
1246   StackFrame *frame = exe_ctx.GetFramePtr();
1247   Target *target = exe_ctx.GetTargetPtr();
1248   if (target && target->GetLanguage() != eLanguageTypeUnknown)
1249     options.SetLanguage(target->GetLanguage());
1250   else if (frame)
1251     options.SetLanguage(frame->GetLanguage());
1252   return EvaluateExpression(expr, options);
1253 }
1254 
1255 lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
1256                                           const SBExpressionOptions &options) {
1257   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1258 
1259 #ifndef LLDB_DISABLE_PYTHON
1260   Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1261 #endif
1262 
1263   ExpressionResults exe_results = eExpressionSetupError;
1264   SBValue expr_result;
1265 
1266   if (expr == nullptr || expr[0] == '\0') {
1267     if (log)
1268       log->Printf(
1269           "SBFrame::EvaluateExpression called with an empty expression");
1270     return expr_result;
1271   }
1272 
1273   ValueObjectSP expr_value_sp;
1274 
1275   std::unique_lock<std::recursive_mutex> lock;
1276   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1277 
1278   if (log)
1279     log->Printf("SBFrame()::EvaluateExpression (expr=\"%s\")...", expr);
1280 
1281   StackFrame *frame = nullptr;
1282   Target *target = exe_ctx.GetTargetPtr();
1283   Process *process = exe_ctx.GetProcessPtr();
1284 
1285   if (target && process) {
1286     Process::StopLocker stop_locker;
1287     if (stop_locker.TryLock(&process->GetRunLock())) {
1288       frame = exe_ctx.GetFramePtr();
1289       if (frame) {
1290         std::unique_ptr<llvm::PrettyStackTraceFormat> stack_trace;
1291         if (target->GetDisplayExpressionsInCrashlogs()) {
1292           StreamString frame_description;
1293           frame->DumpUsingSettingsFormat(&frame_description);
1294           stack_trace = llvm::make_unique<llvm::PrettyStackTraceFormat>(
1295               "SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value "
1296               "= %u) %s",
1297               expr, options.GetFetchDynamicValue(),
1298               frame_description.GetData());
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       } else {
1305         if (log)
1306           log->Printf("SBFrame::EvaluateExpression () => error: could not "
1307                       "reconstruct frame object for this SBFrame.");
1308       }
1309     } else {
1310       if (log)
1311         log->Printf(
1312             "SBFrame::EvaluateExpression () => error: process is running");
1313     }
1314   }
1315 
1316 #ifndef LLDB_DISABLE_PYTHON
1317   if (expr_log)
1318     expr_log->Printf("** [SBFrame::EvaluateExpression] Expression result is "
1319                      "%s, summary %s **",
1320                      expr_result.GetValue(), expr_result.GetSummary());
1321 
1322   if (log)
1323     log->Printf("SBFrame(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
1324                 "(execution result=%d)",
1325                 static_cast<void *>(frame), expr,
1326                 static_cast<void *>(expr_value_sp.get()), exe_results);
1327 #endif
1328 
1329   return expr_result;
1330 }
1331 
1332 bool SBFrame::IsInlined() {
1333   return static_cast<const SBFrame *>(this)->IsInlined();
1334 }
1335 
1336 bool SBFrame::IsInlined() const {
1337   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1338   std::unique_lock<std::recursive_mutex> lock;
1339   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1340 
1341   StackFrame *frame = nullptr;
1342   Target *target = exe_ctx.GetTargetPtr();
1343   Process *process = exe_ctx.GetProcessPtr();
1344   if (target && process) {
1345     Process::StopLocker stop_locker;
1346     if (stop_locker.TryLock(&process->GetRunLock())) {
1347       frame = exe_ctx.GetFramePtr();
1348       if (frame) {
1349 
1350         Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
1351         if (block)
1352           return block->GetContainingInlinedBlock() != nullptr;
1353       } else {
1354         if (log)
1355           log->Printf("SBFrame::IsInlined () => error: could not reconstruct "
1356                       "frame object for this SBFrame.");
1357       }
1358     } else {
1359       if (log)
1360         log->Printf("SBFrame::IsInlined () => error: process is running");
1361     }
1362   }
1363   return false;
1364 }
1365 
1366 bool SBFrame::IsArtificial() {
1367   return static_cast<const SBFrame *>(this)->IsArtificial();
1368 }
1369 
1370 bool SBFrame::IsArtificial() const {
1371   std::unique_lock<std::recursive_mutex> lock;
1372   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1373 
1374   StackFrame *frame = exe_ctx.GetFramePtr();
1375   if (frame)
1376     return frame->IsArtificial();
1377 
1378   return false;
1379 }
1380 
1381 const char *SBFrame::GetFunctionName() {
1382   return static_cast<const SBFrame *>(this)->GetFunctionName();
1383 }
1384 
1385 lldb::LanguageType SBFrame::GuessLanguage() const {
1386   std::unique_lock<std::recursive_mutex> lock;
1387   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1388 
1389   StackFrame *frame = nullptr;
1390   Target *target = exe_ctx.GetTargetPtr();
1391   Process *process = exe_ctx.GetProcessPtr();
1392   if (target && process) {
1393     Process::StopLocker stop_locker;
1394     if (stop_locker.TryLock(&process->GetRunLock())) {
1395       frame = exe_ctx.GetFramePtr();
1396       if (frame) {
1397         return frame->GuessLanguage();
1398       }
1399     }
1400   }
1401   return eLanguageTypeUnknown;
1402 }
1403 
1404 const char *SBFrame::GetFunctionName() const {
1405   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1406   const char *name = nullptr;
1407   std::unique_lock<std::recursive_mutex> lock;
1408   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1409 
1410   StackFrame *frame = nullptr;
1411   Target *target = exe_ctx.GetTargetPtr();
1412   Process *process = exe_ctx.GetProcessPtr();
1413   if (target && process) {
1414     Process::StopLocker stop_locker;
1415     if (stop_locker.TryLock(&process->GetRunLock())) {
1416       frame = exe_ctx.GetFramePtr();
1417       if (frame) {
1418         SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
1419                                                  eSymbolContextBlock |
1420                                                  eSymbolContextSymbol));
1421         if (sc.block) {
1422           Block *inlined_block = sc.block->GetContainingInlinedBlock();
1423           if (inlined_block) {
1424             const InlineFunctionInfo *inlined_info =
1425                 inlined_block->GetInlinedFunctionInfo();
1426             name =
1427                 inlined_info->GetName(sc.function->GetLanguage()).AsCString();
1428           }
1429         }
1430 
1431         if (name == nullptr) {
1432           if (sc.function)
1433             name = sc.function->GetName().GetCString();
1434         }
1435 
1436         if (name == nullptr) {
1437           if (sc.symbol)
1438             name = sc.symbol->GetName().GetCString();
1439         }
1440       } else {
1441         if (log)
1442           log->Printf("SBFrame::GetFunctionName () => error: could not "
1443                       "reconstruct frame object for this SBFrame.");
1444       }
1445     } else {
1446       if (log)
1447         log->Printf("SBFrame::GetFunctionName() => error: process is running");
1448     }
1449   }
1450   return name;
1451 }
1452 
1453 const char *SBFrame::GetDisplayFunctionName() {
1454   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1455   const char *name = nullptr;
1456 
1457   std::unique_lock<std::recursive_mutex> lock;
1458   ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1459 
1460   StackFrame *frame = nullptr;
1461   Target *target = exe_ctx.GetTargetPtr();
1462   Process *process = exe_ctx.GetProcessPtr();
1463   if (target && process) {
1464     Process::StopLocker stop_locker;
1465     if (stop_locker.TryLock(&process->GetRunLock())) {
1466       frame = exe_ctx.GetFramePtr();
1467       if (frame) {
1468         SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
1469                                                  eSymbolContextBlock |
1470                                                  eSymbolContextSymbol));
1471         if (sc.block) {
1472           Block *inlined_block = sc.block->GetContainingInlinedBlock();
1473           if (inlined_block) {
1474             const InlineFunctionInfo *inlined_info =
1475                 inlined_block->GetInlinedFunctionInfo();
1476             name = inlined_info->GetDisplayName(sc.function->GetLanguage())
1477                        .AsCString();
1478           }
1479         }
1480 
1481         if (name == nullptr) {
1482           if (sc.function)
1483             name = sc.function->GetDisplayName().GetCString();
1484         }
1485 
1486         if (name == nullptr) {
1487           if (sc.symbol)
1488             name = sc.symbol->GetDisplayName().GetCString();
1489         }
1490       } else {
1491         if (log)
1492           log->Printf("SBFrame::GetDisplayFunctionName () => error: could not "
1493                       "reconstruct frame object for this SBFrame.");
1494       }
1495     } else {
1496       if (log)
1497         log->Printf(
1498             "SBFrame::GetDisplayFunctionName() => error: process is running");
1499     }
1500   }
1501   return name;
1502 }
1503