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