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 "lldb/API/SBFrame.h" 11 12 #include <string> 13 #include <algorithm> 14 15 #include "lldb/lldb-types.h" 16 17 #include "lldb/Core/Address.h" 18 #include "lldb/Core/ConstString.h" 19 #include "lldb/Core/Log.h" 20 #include "lldb/Core/Stream.h" 21 #include "lldb/Core/StreamFile.h" 22 #include "lldb/Core/ValueObjectRegister.h" 23 #include "lldb/Core/ValueObjectVariable.h" 24 #include "lldb/Expression/ClangUserExpression.h" 25 #include "lldb/Host/Host.h" 26 #include "lldb/Symbol/Block.h" 27 #include "lldb/Symbol/SymbolContext.h" 28 #include "lldb/Symbol/VariableList.h" 29 #include "lldb/Symbol/Variable.h" 30 #include "lldb/Target/ExecutionContext.h" 31 #include "lldb/Target/Target.h" 32 #include "lldb/Target/Process.h" 33 #include "lldb/Target/RegisterContext.h" 34 #include "lldb/Target/StackFrame.h" 35 #include "lldb/Target/StackID.h" 36 #include "lldb/Target/Thread.h" 37 38 #include "lldb/API/SBDebugger.h" 39 #include "lldb/API/SBValue.h" 40 #include "lldb/API/SBAddress.h" 41 #include "lldb/API/SBStream.h" 42 #include "lldb/API/SBSymbolContext.h" 43 #include "lldb/API/SBThread.h" 44 45 namespace lldb_private { 46 47 class StackFrameImpl 48 { 49 public: 50 StackFrameImpl (const lldb::StackFrameSP &frame_sp) : 51 m_frame_wp (frame_sp), 52 m_thread_wp (), 53 m_stack_id () 54 { 55 if (frame_sp) 56 { 57 m_thread_wp = frame_sp->GetThread(); 58 m_stack_id = frame_sp->GetStackID(); 59 } 60 } 61 62 ~StackFrameImpl() 63 { 64 } 65 66 lldb::StackFrameSP 67 GetFrameSP () 68 { 69 lldb::StackFrameSP frame_sp; 70 // We have a weak pointer to our thread, which might 71 // be NULL'ed out if the thread went away, so first 72 // make sure our thread is still alive. 73 lldb::ThreadSP thread_sp (m_thread_wp.lock()); 74 if (thread_sp) 75 { 76 // Our thread is still here, check if our frame 77 // is still alive as well. 78 frame_sp = m_frame_wp.lock(); 79 if (frame_sp) 80 { 81 // Our frame is still alive, make sure that our thread 82 // still has this exact frame... 83 lldb::StackFrameSP tmp_frame_sp (thread_sp->GetStackFrameAtIndex (frame_sp->GetFrameIndex())); 84 if (tmp_frame_sp == frame_sp) 85 return frame_sp; 86 } 87 // The original stack frame might have gone away, 88 // we need to check for the frame by stack ID 89 frame_sp = thread_sp->GetFrameWithStackID (m_stack_id); 90 m_frame_wp = frame_sp; 91 } 92 return frame_sp; 93 } 94 95 void 96 SetFrameSP (const lldb::StackFrameSP &frame_sp) 97 { 98 if (frame_sp) 99 { 100 m_frame_wp = frame_sp; 101 m_thread_wp = frame_sp->GetThread(); 102 m_stack_id = frame_sp->GetStackID(); 103 } 104 else 105 { 106 m_frame_wp.reset(); 107 m_thread_wp.reset(); 108 m_stack_id.Clear(); 109 } 110 } 111 112 protected: 113 lldb::StackFrameWP m_frame_wp; 114 lldb::ThreadWP m_thread_wp; 115 StackID m_stack_id; 116 }; 117 } // namespace lldb_private 118 119 using namespace lldb; 120 using namespace lldb_private; 121 122 123 SBFrame::SBFrame () : 124 m_opaque_sp () 125 { 126 } 127 128 SBFrame::SBFrame (const StackFrameSP &lldb_object_sp) : 129 m_opaque_sp (new StackFrameImpl (lldb_object_sp)) 130 { 131 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 132 133 if (log) 134 { 135 SBStream sstr; 136 GetDescription (sstr); 137 log->Printf ("SBFrame::SBFrame (sp=%p) => SBFrame(%p): %s", 138 lldb_object_sp.get(), lldb_object_sp.get(), sstr.GetData()); 139 140 } 141 } 142 143 SBFrame::SBFrame(const SBFrame &rhs) : 144 m_opaque_sp (rhs.m_opaque_sp) 145 { 146 } 147 148 const SBFrame & 149 SBFrame::operator = (const SBFrame &rhs) 150 { 151 if (this != &rhs) 152 m_opaque_sp = rhs.m_opaque_sp; 153 return *this; 154 } 155 156 SBFrame::~SBFrame() 157 { 158 } 159 160 StackFrameSP 161 SBFrame::GetFrameSP() const 162 { 163 StackFrameImplSP impl_sp (m_opaque_sp); 164 StackFrameSP frame_sp; 165 if (impl_sp) 166 frame_sp = impl_sp->GetFrameSP(); 167 return frame_sp; 168 } 169 170 void 171 SBFrame::SetFrameSP (const StackFrameSP &lldb_object_sp) 172 { 173 if (lldb_object_sp) 174 { 175 if (m_opaque_sp) 176 { 177 StackFrameImplSP impl_sp (m_opaque_sp); 178 if (impl_sp) 179 impl_sp->SetFrameSP (lldb_object_sp); 180 } 181 else 182 { 183 m_opaque_sp = StackFrameImplSP (new StackFrameImpl(lldb_object_sp)); 184 } 185 } 186 else 187 { 188 m_opaque_sp.reset(); 189 } 190 } 191 192 bool 193 SBFrame::IsValid() const 194 { 195 StackFrameImplSP impl_sp (m_opaque_sp); 196 if (impl_sp) 197 return (impl_sp->GetFrameSP().get() != NULL); 198 return false; 199 } 200 201 SBSymbolContext 202 SBFrame::GetSymbolContext (uint32_t resolve_scope) const 203 { 204 205 SBSymbolContext sb_sym_ctx; 206 ExecutionContext exe_ctx(GetFrameSP()); 207 StackFrame *frame = exe_ctx.GetFramePtr(); 208 Target *target = exe_ctx.GetTargetPtr(); 209 if (frame && target) 210 { 211 Mutex::Locker api_locker (target->GetAPIMutex()); 212 sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext (resolve_scope)); 213 } 214 215 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 216 if (log) 217 log->Printf ("SBFrame(%p)::GetSymbolContext (resolve_scope=0x%8.8x) => SBSymbolContext(%p)", 218 frame, resolve_scope, sb_sym_ctx.get()); 219 220 return sb_sym_ctx; 221 } 222 223 SBModule 224 SBFrame::GetModule () const 225 { 226 SBModule sb_module; 227 ModuleSP module_sp; 228 ExecutionContext exe_ctx(GetFrameSP()); 229 StackFrame *frame = exe_ctx.GetFramePtr(); 230 Target *target = exe_ctx.GetTargetPtr(); 231 if (frame && target) 232 { 233 Mutex::Locker api_locker (target->GetAPIMutex()); 234 module_sp = frame->GetSymbolContext (eSymbolContextModule).module_sp; 235 sb_module.SetSP (module_sp); 236 } 237 238 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 239 if (log) 240 log->Printf ("SBFrame(%p)::GetModule () => SBModule(%p)", 241 frame, module_sp.get()); 242 243 return sb_module; 244 } 245 246 SBCompileUnit 247 SBFrame::GetCompileUnit () const 248 { 249 SBCompileUnit sb_comp_unit; 250 ExecutionContext exe_ctx(GetFrameSP()); 251 StackFrame *frame = exe_ctx.GetFramePtr(); 252 Target *target = exe_ctx.GetTargetPtr(); 253 if (frame && target) 254 { 255 Mutex::Locker api_locker (target->GetAPIMutex()); 256 sb_comp_unit.reset (frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit); 257 } 258 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 259 if (log) 260 log->Printf ("SBFrame(%p)::GetModule () => SBCompileUnit(%p)", 261 frame, sb_comp_unit.get()); 262 263 return sb_comp_unit; 264 } 265 266 SBFunction 267 SBFrame::GetFunction () const 268 { 269 SBFunction sb_function; 270 ExecutionContext exe_ctx(GetFrameSP()); 271 StackFrame *frame = exe_ctx.GetFramePtr(); 272 Target *target = exe_ctx.GetTargetPtr(); 273 if (frame && target) 274 { 275 Mutex::Locker api_locker (target->GetAPIMutex()); 276 sb_function.reset(frame->GetSymbolContext (eSymbolContextFunction).function); 277 } 278 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 279 if (log) 280 log->Printf ("SBFrame(%p)::GetFunction () => SBFunction(%p)", 281 frame, sb_function.get()); 282 283 return sb_function; 284 } 285 286 SBSymbol 287 SBFrame::GetSymbol () const 288 { 289 SBSymbol sb_symbol; 290 ExecutionContext exe_ctx(GetFrameSP()); 291 StackFrame *frame = exe_ctx.GetFramePtr(); 292 Target *target = exe_ctx.GetTargetPtr(); 293 if (frame && target) 294 { 295 Mutex::Locker api_locker (target->GetAPIMutex()); 296 sb_symbol.reset(frame->GetSymbolContext (eSymbolContextSymbol).symbol); 297 } 298 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 299 if (log) 300 log->Printf ("SBFrame(%p)::GetSymbol () => SBSymbol(%p)", 301 frame, sb_symbol.get()); 302 return sb_symbol; 303 } 304 305 SBBlock 306 SBFrame::GetBlock () const 307 { 308 SBBlock sb_block; 309 ExecutionContext exe_ctx(GetFrameSP()); 310 StackFrame *frame = exe_ctx.GetFramePtr(); 311 Target *target = exe_ctx.GetTargetPtr(); 312 if (frame && target) 313 { 314 Mutex::Locker api_locker (target->GetAPIMutex()); 315 sb_block.SetPtr (frame->GetSymbolContext (eSymbolContextBlock).block); 316 } 317 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 318 if (log) 319 log->Printf ("SBFrame(%p)::GetBlock () => SBBlock(%p)", 320 frame, sb_block.GetPtr()); 321 return sb_block; 322 } 323 324 SBBlock 325 SBFrame::GetFrameBlock () const 326 { 327 SBBlock sb_block; 328 ExecutionContext exe_ctx(GetFrameSP()); 329 StackFrame *frame = exe_ctx.GetFramePtr(); 330 Target *target = exe_ctx.GetTargetPtr(); 331 if (frame && target) 332 { 333 Mutex::Locker api_locker (target->GetAPIMutex()); 334 sb_block.SetPtr(frame->GetFrameBlock ()); 335 } 336 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 337 if (log) 338 log->Printf ("SBFrame(%p)::GetFrameBlock () => SBBlock(%p)", 339 frame, sb_block.GetPtr()); 340 return sb_block; 341 } 342 343 SBLineEntry 344 SBFrame::GetLineEntry () const 345 { 346 SBLineEntry sb_line_entry; 347 ExecutionContext exe_ctx(GetFrameSP()); 348 StackFrame *frame = exe_ctx.GetFramePtr(); 349 Target *target = exe_ctx.GetTargetPtr(); 350 if (frame && target) 351 { 352 Mutex::Locker api_locker (target->GetAPIMutex()); 353 sb_line_entry.SetLineEntry (frame->GetSymbolContext (eSymbolContextLineEntry).line_entry); 354 } 355 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 356 if (log) 357 log->Printf ("SBFrame(%p)::GetLineEntry () => SBLineEntry(%p)", 358 frame, sb_line_entry.get()); 359 return sb_line_entry; 360 } 361 362 uint32_t 363 SBFrame::GetFrameID () const 364 { 365 uint32_t frame_idx = UINT32_MAX; 366 367 368 ExecutionContext exe_ctx(GetFrameSP()); 369 StackFrame *frame = exe_ctx.GetFramePtr(); 370 Target *target = exe_ctx.GetTargetPtr(); 371 if (frame && target) 372 frame_idx = frame->GetFrameIndex (); 373 374 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 375 if (log) 376 log->Printf ("SBFrame(%p)::GetFrameID () => %u", 377 frame, frame_idx); 378 return frame_idx; 379 } 380 381 addr_t 382 SBFrame::GetPC () const 383 { 384 addr_t addr = LLDB_INVALID_ADDRESS; 385 ExecutionContext exe_ctx(GetFrameSP()); 386 StackFrame *frame = exe_ctx.GetFramePtr(); 387 Target *target = exe_ctx.GetTargetPtr(); 388 if (frame && target) 389 { 390 Mutex::Locker api_locker (target->GetAPIMutex()); 391 addr = frame->GetFrameCodeAddress().GetOpcodeLoadAddress (target); 392 } 393 394 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 395 if (log) 396 log->Printf ("SBFrame(%p)::GetPC () => 0x%llx", frame, addr); 397 398 return addr; 399 } 400 401 bool 402 SBFrame::SetPC (addr_t new_pc) 403 { 404 bool ret_val = false; 405 ExecutionContext exe_ctx(GetFrameSP()); 406 StackFrame *frame = exe_ctx.GetFramePtr(); 407 Target *target = exe_ctx.GetTargetPtr(); 408 if (frame && target) 409 { 410 Mutex::Locker api_locker (target->GetAPIMutex()); 411 ret_val = frame->GetRegisterContext()->SetPC (new_pc); 412 } 413 414 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 415 if (log) 416 log->Printf ("SBFrame(%p)::SetPC (new_pc=0x%llx) => %i", 417 frame, new_pc, ret_val); 418 419 return ret_val; 420 } 421 422 addr_t 423 SBFrame::GetSP () const 424 { 425 addr_t addr = LLDB_INVALID_ADDRESS; 426 ExecutionContext exe_ctx(GetFrameSP()); 427 StackFrame *frame = exe_ctx.GetFramePtr(); 428 Target *target = exe_ctx.GetTargetPtr(); 429 if (frame && target) 430 { 431 Mutex::Locker api_locker (target->GetAPIMutex()); 432 addr = frame->GetRegisterContext()->GetSP(); 433 } 434 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 435 if (log) 436 log->Printf ("SBFrame(%p)::GetSP () => 0x%llx", frame, addr); 437 438 return addr; 439 } 440 441 442 addr_t 443 SBFrame::GetFP () const 444 { 445 addr_t addr = LLDB_INVALID_ADDRESS; 446 ExecutionContext exe_ctx(GetFrameSP()); 447 StackFrame *frame = exe_ctx.GetFramePtr(); 448 Target *target = exe_ctx.GetTargetPtr(); 449 if (frame && target) 450 { 451 Mutex::Locker api_locker (target->GetAPIMutex()); 452 addr = frame->GetRegisterContext()->GetFP(); 453 } 454 455 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 456 if (log) 457 log->Printf ("SBFrame(%p)::GetFP () => 0x%llx", frame, addr); 458 return addr; 459 } 460 461 462 SBAddress 463 SBFrame::GetPCAddress () const 464 { 465 SBAddress sb_addr; 466 ExecutionContext exe_ctx(GetFrameSP()); 467 StackFrame *frame = exe_ctx.GetFramePtr(); 468 Target *target = exe_ctx.GetTargetPtr(); 469 if (frame && target) 470 { 471 Mutex::Locker api_locker (target->GetAPIMutex()); 472 sb_addr.SetAddress (&frame->GetFrameCodeAddress()); 473 } 474 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 475 if (log) 476 log->Printf ("SBFrame(%p)::GetPCAddress () => SBAddress(%p)", frame, sb_addr.get()); 477 return sb_addr; 478 } 479 480 void 481 SBFrame::Clear() 482 { 483 m_opaque_sp.reset(); 484 } 485 486 lldb::SBValue 487 SBFrame::GetValueForVariablePath (const char *var_path) 488 { 489 SBValue sb_value; 490 ExecutionContext exe_ctx(GetFrameSP()); 491 StackFrame *frame = exe_ctx.GetFramePtr(); 492 Target *target = exe_ctx.GetTargetPtr(); 493 if (frame && target) 494 { 495 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 496 sb_value = GetValueForVariablePath (var_path, use_dynamic); 497 } 498 return sb_value; 499 } 500 501 lldb::SBValue 502 SBFrame::GetValueForVariablePath (const char *var_path, DynamicValueType use_dynamic) 503 { 504 SBValue sb_value; 505 ExecutionContext exe_ctx(GetFrameSP()); 506 StackFrame *frame = exe_ctx.GetFramePtr(); 507 Target *target = exe_ctx.GetTargetPtr(); 508 if (frame && target && var_path && var_path[0]) 509 { 510 Mutex::Locker api_locker (target->GetAPIMutex()); 511 VariableSP var_sp; 512 Error error; 513 ValueObjectSP value_sp (frame->GetValueForVariableExpressionPath (var_path, 514 use_dynamic, 515 StackFrame::eExpressionPathOptionCheckPtrVsMember, 516 var_sp, 517 error)); 518 sb_value.SetSP(value_sp); 519 } 520 return sb_value; 521 } 522 523 SBValue 524 SBFrame::FindVariable (const char *name) 525 { 526 SBValue value; 527 ExecutionContext exe_ctx(GetFrameSP()); 528 StackFrame *frame = exe_ctx.GetFramePtr(); 529 Target *target = exe_ctx.GetTargetPtr(); 530 if (frame && target) 531 { 532 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 533 value = FindVariable (name, use_dynamic); 534 } 535 return value; 536 } 537 538 539 SBValue 540 SBFrame::FindVariable (const char *name, lldb::DynamicValueType use_dynamic) 541 { 542 VariableSP var_sp; 543 SBValue sb_value; 544 ValueObjectSP value_sp; 545 ExecutionContext exe_ctx(GetFrameSP()); 546 StackFrame *frame = exe_ctx.GetFramePtr(); 547 Target *target = exe_ctx.GetTargetPtr(); 548 if (frame && target && name && name[0]) 549 { 550 VariableList variable_list; 551 Mutex::Locker api_locker (target->GetAPIMutex()); 552 SymbolContext sc (frame->GetSymbolContext (eSymbolContextBlock)); 553 554 if (sc.block) 555 { 556 const bool can_create = true; 557 const bool get_parent_variables = true; 558 const bool stop_if_block_is_inlined_function = true; 559 560 if (sc.block->AppendVariables (can_create, 561 get_parent_variables, 562 stop_if_block_is_inlined_function, 563 &variable_list)) 564 { 565 var_sp = variable_list.FindVariable (ConstString(name)); 566 } 567 } 568 569 if (var_sp) 570 { 571 value_sp = frame->GetValueObjectForFrameVariable(var_sp, use_dynamic); 572 sb_value.SetSP(value_sp); 573 } 574 575 } 576 577 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 578 if (log) 579 log->Printf ("SBFrame(%p)::FindVariable (name=\"%s\") => SBValue(%p)", 580 frame, name, value_sp.get()); 581 582 return sb_value; 583 } 584 585 SBValue 586 SBFrame::FindValue (const char *name, ValueType value_type) 587 { 588 SBValue value; 589 ExecutionContext exe_ctx(GetFrameSP()); 590 StackFrame *frame = exe_ctx.GetFramePtr(); 591 Target *target = exe_ctx.GetTargetPtr(); 592 if (frame && target) 593 { 594 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 595 value = FindValue (name, value_type, use_dynamic); 596 } 597 return value; 598 } 599 600 SBValue 601 SBFrame::FindValue (const char *name, ValueType value_type, lldb::DynamicValueType use_dynamic) 602 { 603 SBValue sb_value; 604 ValueObjectSP value_sp; 605 ExecutionContext exe_ctx(GetFrameSP()); 606 StackFrame *frame = exe_ctx.GetFramePtr(); 607 Target *target = exe_ctx.GetTargetPtr(); 608 if (frame && target && name && name[0]) 609 { 610 Mutex::Locker api_locker (target->GetAPIMutex()); 611 612 switch (value_type) 613 { 614 case eValueTypeVariableGlobal: // global variable 615 case eValueTypeVariableStatic: // static variable 616 case eValueTypeVariableArgument: // function argument variables 617 case eValueTypeVariableLocal: // function local variables 618 { 619 VariableList *variable_list = frame->GetVariableList(true); 620 621 SymbolContext sc (frame->GetSymbolContext (eSymbolContextBlock)); 622 623 const bool can_create = true; 624 const bool get_parent_variables = true; 625 const bool stop_if_block_is_inlined_function = true; 626 627 if (sc.block && sc.block->AppendVariables (can_create, 628 get_parent_variables, 629 stop_if_block_is_inlined_function, 630 variable_list)) 631 { 632 ConstString const_name(name); 633 const uint32_t num_variables = variable_list->GetSize(); 634 for (uint32_t i = 0; i < num_variables; ++i) 635 { 636 VariableSP variable_sp (variable_list->GetVariableAtIndex(i)); 637 if (variable_sp && 638 variable_sp->GetScope() == value_type && 639 variable_sp->GetName() == const_name) 640 { 641 value_sp = frame->GetValueObjectForFrameVariable (variable_sp, use_dynamic); 642 sb_value.SetSP (value_sp); 643 break; 644 } 645 } 646 } 647 } 648 break; 649 650 case eValueTypeRegister: // stack frame register value 651 { 652 RegisterContextSP reg_ctx (frame->GetRegisterContext()); 653 if (reg_ctx) 654 { 655 const uint32_t num_regs = reg_ctx->GetRegisterCount(); 656 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) 657 { 658 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx); 659 if (reg_info && 660 ((reg_info->name && strcasecmp (reg_info->name, name) == 0) || 661 (reg_info->alt_name && strcasecmp (reg_info->alt_name, name) == 0))) 662 { 663 value_sp = ValueObjectRegister::Create (frame, reg_ctx, reg_idx); 664 sb_value.SetSP (value_sp); 665 break; 666 } 667 } 668 } 669 } 670 break; 671 672 case eValueTypeRegisterSet: // A collection of stack frame register values 673 { 674 RegisterContextSP reg_ctx (frame->GetRegisterContext()); 675 if (reg_ctx) 676 { 677 const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); 678 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) 679 { 680 const RegisterSet *reg_set = reg_ctx->GetRegisterSet (set_idx); 681 if (reg_set && 682 ((reg_set->name && strcasecmp (reg_set->name, name) == 0) || 683 (reg_set->short_name && strcasecmp (reg_set->short_name, name) == 0))) 684 { 685 value_sp = ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx); 686 sb_value.SetSP (value_sp); 687 break; 688 } 689 } 690 } 691 } 692 break; 693 694 case eValueTypeConstResult: // constant result variables 695 { 696 ConstString const_name(name); 697 ClangExpressionVariableSP expr_var_sp (target->GetPersistentVariables().GetVariable (const_name)); 698 if (expr_var_sp) 699 { 700 value_sp = expr_var_sp->GetValueObject(); 701 sb_value.SetSP (value_sp); 702 } 703 } 704 break; 705 706 default: 707 break; 708 } 709 } 710 711 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 712 if (log) 713 log->Printf ("SBFrame(%p)::FindVariableInScope (name=\"%s\", value_type=%i) => SBValue(%p)", 714 frame, name, value_type, value_sp.get()); 715 716 717 return sb_value; 718 } 719 720 bool 721 SBFrame::IsEqual (const SBFrame &that) const 722 { 723 lldb::StackFrameSP this_sp = GetFrameSP(); 724 lldb::StackFrameSP that_sp = that.GetFrameSP(); 725 return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID()); 726 } 727 728 bool 729 SBFrame::operator == (const SBFrame &rhs) const 730 { 731 return IsEqual(rhs); 732 } 733 734 bool 735 SBFrame::operator != (const SBFrame &rhs) const 736 { 737 return !IsEqual(rhs); 738 } 739 740 SBThread 741 SBFrame::GetThread () const 742 { 743 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 744 745 ExecutionContext exe_ctx(GetFrameSP()); 746 ThreadSP thread_sp (exe_ctx.GetThreadSP()); 747 SBThread sb_thread (thread_sp); 748 749 if (log) 750 { 751 SBStream sstr; 752 sb_thread.GetDescription (sstr); 753 log->Printf ("SBFrame(%p)::GetThread () => SBThread(%p): %s", 754 exe_ctx.GetFramePtr(), 755 thread_sp.get(), 756 sstr.GetData()); 757 } 758 759 return sb_thread; 760 } 761 762 const char * 763 SBFrame::Disassemble () const 764 { 765 const char *disassembly = NULL; 766 ExecutionContext exe_ctx(GetFrameSP()); 767 StackFrame *frame = exe_ctx.GetFramePtr(); 768 Target *target = exe_ctx.GetTargetPtr(); 769 if (frame && target) 770 { 771 Mutex::Locker api_locker (target->GetAPIMutex()); 772 disassembly = frame->Disassemble(); 773 } 774 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 775 776 if (log) 777 log->Printf ("SBFrame(%p)::Disassemble () => %s", frame, disassembly); 778 779 return disassembly; 780 } 781 782 783 SBValueList 784 SBFrame::GetVariables (bool arguments, 785 bool locals, 786 bool statics, 787 bool in_scope_only) 788 { 789 SBValueList value_list; 790 ExecutionContext exe_ctx(GetFrameSP()); 791 StackFrame *frame = exe_ctx.GetFramePtr(); 792 Target *target = exe_ctx.GetTargetPtr(); 793 if (frame && target) 794 { 795 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 796 value_list = GetVariables (arguments, locals, statics, in_scope_only, use_dynamic); 797 } 798 return value_list; 799 } 800 801 SBValueList 802 SBFrame::GetVariables (bool arguments, 803 bool locals, 804 bool statics, 805 bool in_scope_only, 806 lldb::DynamicValueType use_dynamic) 807 { 808 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 809 810 SBValueList value_list; 811 ExecutionContext exe_ctx(GetFrameSP()); 812 StackFrame *frame = exe_ctx.GetFramePtr(); 813 Target *target = exe_ctx.GetTargetPtr(); 814 815 if (log) 816 log->Printf ("SBFrame(%p)::GetVariables (arguments=%i, locals=%i, statics=%i, in_scope_only=%i)", 817 frame, 818 arguments, 819 locals, 820 statics, 821 in_scope_only); 822 823 if (frame && target) 824 { 825 826 size_t i; 827 VariableList *variable_list = NULL; 828 // Scope for locker 829 { 830 Mutex::Locker api_locker (target->GetAPIMutex()); 831 variable_list = frame->GetVariableList(true); 832 } 833 if (variable_list) 834 { 835 const size_t num_variables = variable_list->GetSize(); 836 if (num_variables) 837 { 838 for (i = 0; i < num_variables; ++i) 839 { 840 VariableSP variable_sp (variable_list->GetVariableAtIndex(i)); 841 if (variable_sp) 842 { 843 bool add_variable = false; 844 switch (variable_sp->GetScope()) 845 { 846 case eValueTypeVariableGlobal: 847 case eValueTypeVariableStatic: 848 add_variable = statics; 849 break; 850 851 case eValueTypeVariableArgument: 852 add_variable = arguments; 853 break; 854 855 case eValueTypeVariableLocal: 856 add_variable = locals; 857 break; 858 859 default: 860 break; 861 } 862 if (add_variable) 863 { 864 if (in_scope_only && !variable_sp->IsInScope(frame)) 865 continue; 866 867 value_list.Append(frame->GetValueObjectForFrameVariable (variable_sp, use_dynamic)); 868 } 869 } 870 } 871 } 872 } 873 } 874 875 if (log) 876 { 877 log->Printf ("SBFrame(%p)::GetVariables (...) => SBValueList(%p)", frame, 878 value_list.get()); 879 } 880 881 return value_list; 882 } 883 884 SBValueList 885 SBFrame::GetRegisters () 886 { 887 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 888 889 SBValueList value_list; 890 ExecutionContext exe_ctx(GetFrameSP()); 891 StackFrame *frame = exe_ctx.GetFramePtr(); 892 Target *target = exe_ctx.GetTargetPtr(); 893 if (frame && target) 894 { 895 Mutex::Locker api_locker (target->GetAPIMutex()); 896 RegisterContextSP reg_ctx (frame->GetRegisterContext()); 897 if (reg_ctx) 898 { 899 const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); 900 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) 901 { 902 value_list.Append(ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx)); 903 } 904 } 905 } 906 907 if (log) 908 log->Printf ("SBFrame(%p)::Registers () => SBValueList(%p)", frame, value_list.get()); 909 910 return value_list; 911 } 912 913 bool 914 SBFrame::GetDescription (SBStream &description) 915 { 916 Stream &strm = description.ref(); 917 918 ExecutionContext exe_ctx(GetFrameSP()); 919 StackFrame *frame = exe_ctx.GetFramePtr(); 920 Target *target = exe_ctx.GetTargetPtr(); 921 if (frame && target) 922 { 923 Mutex::Locker api_locker (target->GetAPIMutex()); 924 frame->DumpUsingSettingsFormat (&strm); 925 } 926 else 927 strm.PutCString ("No value"); 928 929 return true; 930 } 931 932 SBValue 933 SBFrame::EvaluateExpression (const char *expr) 934 { 935 SBValue result; 936 ExecutionContext exe_ctx(GetFrameSP()); 937 StackFrame *frame = exe_ctx.GetFramePtr(); 938 Target *target = exe_ctx.GetTargetPtr(); 939 if (frame && target) 940 { 941 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 942 result = EvaluateExpression (expr, use_dynamic); 943 } 944 return result; 945 } 946 947 SBValue 948 SBFrame::EvaluateExpression (const char *expr, lldb::DynamicValueType fetch_dynamic_value) 949 { 950 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 951 952 LogSP expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 953 954 ExecutionResults exe_results; 955 SBValue expr_result; 956 ValueObjectSP expr_value_sp; 957 958 ExecutionContext exe_ctx(GetFrameSP()); 959 StackFrame *frame = exe_ctx.GetFramePtr(); 960 Target *target = exe_ctx.GetTargetPtr(); 961 if (log) 962 log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\")...", frame, expr); 963 964 if (frame && target) 965 { 966 Mutex::Locker api_locker (target->GetAPIMutex()); 967 968 969 StreamString frame_description; 970 frame->DumpUsingSettingsFormat (&frame_description); 971 972 Host::SetCrashDescriptionWithFormat ("SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s", 973 expr, fetch_dynamic_value, frame_description.GetString().c_str()); 974 975 const bool coerce_to_id = false; 976 const bool unwind_on_error = true; 977 const bool keep_in_memory = false; 978 979 exe_results = target->EvaluateExpression (expr, 980 frame, 981 eExecutionPolicyOnlyWhenNeeded, 982 coerce_to_id, 983 unwind_on_error, 984 keep_in_memory, 985 fetch_dynamic_value, 986 expr_value_sp); 987 expr_result.SetSP(expr_value_sp); 988 Host::SetCrashDescription (NULL); 989 } 990 991 #ifndef LLDB_DISABLE_PYTHON 992 if (expr_log) 993 expr_log->Printf("** [SBFrame::EvaluateExpression] Expression result is %s, summary %s **", 994 expr_result.GetValue(), 995 expr_result.GetSummary()); 996 997 if (log) 998 log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)", 999 frame, 1000 expr, 1001 expr_value_sp.get(), 1002 exe_results); 1003 #endif 1004 1005 return expr_result; 1006 } 1007 1008 bool 1009 SBFrame::IsInlined() 1010 { 1011 ExecutionContext exe_ctx(GetFrameSP()); 1012 StackFrame *frame = exe_ctx.GetFramePtr(); 1013 Target *target = exe_ctx.GetTargetPtr(); 1014 if (frame && target) 1015 { 1016 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 1017 if (block) 1018 return block->GetContainingInlinedBlock () != NULL; 1019 } 1020 return false; 1021 } 1022 1023 const char * 1024 SBFrame::GetFunctionName() 1025 { 1026 const char *name = NULL; 1027 ExecutionContext exe_ctx(GetFrameSP()); 1028 StackFrame *frame = exe_ctx.GetFramePtr(); 1029 Target *target = exe_ctx.GetTargetPtr(); 1030 if (frame && target) 1031 { 1032 SymbolContext sc (frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol)); 1033 if (sc.block) 1034 { 1035 Block *inlined_block = sc.block->GetContainingInlinedBlock (); 1036 if (inlined_block) 1037 { 1038 const InlineFunctionInfo* inlined_info = inlined_block->GetInlinedFunctionInfo(); 1039 name = inlined_info->GetName().AsCString(); 1040 } 1041 } 1042 1043 if (name == NULL) 1044 { 1045 if (sc.function) 1046 name = sc.function->GetName().GetCString(); 1047 } 1048 1049 if (name == NULL) 1050 { 1051 if (sc.symbol) 1052 name = sc.symbol->GetName().GetCString(); 1053 } 1054 } 1055 return name; 1056 } 1057 1058