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