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::operator == (const SBFrame &rhs) const 722 { 723 return GetFrameSP().get() == rhs.GetFrameSP().get(); 724 } 725 726 bool 727 SBFrame::operator != (const SBFrame &rhs) const 728 { 729 return GetFrameSP().get() != rhs.GetFrameSP().get(); 730 } 731 732 SBThread 733 SBFrame::GetThread () const 734 { 735 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 736 737 ExecutionContext exe_ctx(GetFrameSP()); 738 ThreadSP thread_sp (exe_ctx.GetThreadSP()); 739 SBThread sb_thread (thread_sp); 740 741 if (log) 742 { 743 SBStream sstr; 744 sb_thread.GetDescription (sstr); 745 log->Printf ("SBFrame(%p)::GetThread () => SBThread(%p): %s", 746 exe_ctx.GetFramePtr(), 747 thread_sp.get(), 748 sstr.GetData()); 749 } 750 751 return sb_thread; 752 } 753 754 const char * 755 SBFrame::Disassemble () const 756 { 757 const char *disassembly = NULL; 758 ExecutionContext exe_ctx(GetFrameSP()); 759 StackFrame *frame = exe_ctx.GetFramePtr(); 760 Target *target = exe_ctx.GetTargetPtr(); 761 if (frame && target) 762 { 763 Mutex::Locker api_locker (target->GetAPIMutex()); 764 disassembly = frame->Disassemble(); 765 } 766 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 767 768 if (log) 769 log->Printf ("SBFrame(%p)::Disassemble () => %s", frame, disassembly); 770 771 return disassembly; 772 } 773 774 775 SBValueList 776 SBFrame::GetVariables (bool arguments, 777 bool locals, 778 bool statics, 779 bool in_scope_only) 780 { 781 SBValueList value_list; 782 ExecutionContext exe_ctx(GetFrameSP()); 783 StackFrame *frame = exe_ctx.GetFramePtr(); 784 Target *target = exe_ctx.GetTargetPtr(); 785 if (frame && target) 786 { 787 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 788 value_list = GetVariables (arguments, locals, statics, in_scope_only, use_dynamic); 789 } 790 return value_list; 791 } 792 793 SBValueList 794 SBFrame::GetVariables (bool arguments, 795 bool locals, 796 bool statics, 797 bool in_scope_only, 798 lldb::DynamicValueType use_dynamic) 799 { 800 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 801 802 SBValueList value_list; 803 ExecutionContext exe_ctx(GetFrameSP()); 804 StackFrame *frame = exe_ctx.GetFramePtr(); 805 Target *target = exe_ctx.GetTargetPtr(); 806 807 if (log) 808 log->Printf ("SBFrame(%p)::GetVariables (arguments=%i, locals=%i, statics=%i, in_scope_only=%i)", 809 frame, 810 arguments, 811 locals, 812 statics, 813 in_scope_only); 814 815 if (frame && target) 816 { 817 818 size_t i; 819 VariableList *variable_list = NULL; 820 // Scope for locker 821 { 822 Mutex::Locker api_locker (target->GetAPIMutex()); 823 variable_list = frame->GetVariableList(true); 824 } 825 if (variable_list) 826 { 827 const size_t num_variables = variable_list->GetSize(); 828 if (num_variables) 829 { 830 for (i = 0; i < num_variables; ++i) 831 { 832 VariableSP variable_sp (variable_list->GetVariableAtIndex(i)); 833 if (variable_sp) 834 { 835 bool add_variable = false; 836 switch (variable_sp->GetScope()) 837 { 838 case eValueTypeVariableGlobal: 839 case eValueTypeVariableStatic: 840 add_variable = statics; 841 break; 842 843 case eValueTypeVariableArgument: 844 add_variable = arguments; 845 break; 846 847 case eValueTypeVariableLocal: 848 add_variable = locals; 849 break; 850 851 default: 852 break; 853 } 854 if (add_variable) 855 { 856 if (in_scope_only && !variable_sp->IsInScope(frame)) 857 continue; 858 859 value_list.Append(frame->GetValueObjectForFrameVariable (variable_sp, use_dynamic)); 860 } 861 } 862 } 863 } 864 } 865 } 866 867 if (log) 868 { 869 log->Printf ("SBFrame(%p)::GetVariables (...) => SBValueList(%p)", frame, 870 value_list.get()); 871 } 872 873 return value_list; 874 } 875 876 SBValueList 877 SBFrame::GetRegisters () 878 { 879 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 880 881 SBValueList value_list; 882 ExecutionContext exe_ctx(GetFrameSP()); 883 StackFrame *frame = exe_ctx.GetFramePtr(); 884 Target *target = exe_ctx.GetTargetPtr(); 885 if (frame && target) 886 { 887 Mutex::Locker api_locker (target->GetAPIMutex()); 888 RegisterContextSP reg_ctx (frame->GetRegisterContext()); 889 if (reg_ctx) 890 { 891 const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); 892 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) 893 { 894 value_list.Append(ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx)); 895 } 896 } 897 } 898 899 if (log) 900 log->Printf ("SBFrame(%p)::Registers () => SBValueList(%p)", frame, value_list.get()); 901 902 return value_list; 903 } 904 905 bool 906 SBFrame::GetDescription (SBStream &description) 907 { 908 Stream &strm = description.ref(); 909 910 ExecutionContext exe_ctx(GetFrameSP()); 911 StackFrame *frame = exe_ctx.GetFramePtr(); 912 Target *target = exe_ctx.GetTargetPtr(); 913 if (frame && target) 914 { 915 Mutex::Locker api_locker (target->GetAPIMutex()); 916 frame->DumpUsingSettingsFormat (&strm); 917 } 918 else 919 strm.PutCString ("No value"); 920 921 return true; 922 } 923 924 SBValue 925 SBFrame::EvaluateExpression (const char *expr) 926 { 927 SBValue result; 928 ExecutionContext exe_ctx(GetFrameSP()); 929 StackFrame *frame = exe_ctx.GetFramePtr(); 930 Target *target = exe_ctx.GetTargetPtr(); 931 if (frame && target) 932 { 933 lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue(); 934 result = EvaluateExpression (expr, use_dynamic); 935 } 936 return result; 937 } 938 939 SBValue 940 SBFrame::EvaluateExpression (const char *expr, lldb::DynamicValueType fetch_dynamic_value) 941 { 942 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 943 944 LogSP expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 945 946 ExecutionResults exe_results; 947 SBValue expr_result; 948 ValueObjectSP expr_value_sp; 949 950 ExecutionContext exe_ctx(GetFrameSP()); 951 StackFrame *frame = exe_ctx.GetFramePtr(); 952 Target *target = exe_ctx.GetTargetPtr(); 953 if (log) 954 log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\")...", frame, expr); 955 956 if (frame && target) 957 { 958 Mutex::Locker api_locker (target->GetAPIMutex()); 959 960 961 StreamString frame_description; 962 frame->DumpUsingSettingsFormat (&frame_description); 963 964 Host::SetCrashDescriptionWithFormat ("SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s", 965 expr, fetch_dynamic_value, frame_description.GetString().c_str()); 966 967 const bool coerce_to_id = false; 968 const bool unwind_on_error = true; 969 const bool keep_in_memory = false; 970 971 exe_results = target->EvaluateExpression (expr, 972 frame, 973 eExecutionPolicyOnlyWhenNeeded, 974 coerce_to_id, 975 unwind_on_error, 976 keep_in_memory, 977 fetch_dynamic_value, 978 expr_value_sp); 979 expr_result.SetSP(expr_value_sp); 980 Host::SetCrashDescription (NULL); 981 } 982 983 if (expr_log) 984 expr_log->Printf("** [SBFrame::EvaluateExpression] Expression result is %s, summary %s **", 985 expr_result.GetValue(), 986 expr_result.GetSummary()); 987 988 if (log) 989 log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)", 990 frame, 991 expr, 992 expr_value_sp.get(), 993 exe_results); 994 995 return expr_result; 996 } 997 998 bool 999 SBFrame::IsInlined() 1000 { 1001 ExecutionContext exe_ctx(GetFrameSP()); 1002 StackFrame *frame = exe_ctx.GetFramePtr(); 1003 Target *target = exe_ctx.GetTargetPtr(); 1004 if (frame && target) 1005 { 1006 Block *block = frame->GetSymbolContext(eSymbolContextBlock).block; 1007 if (block) 1008 return block->GetContainingInlinedBlock () != NULL; 1009 } 1010 return false; 1011 } 1012 1013 const char * 1014 SBFrame::GetFunctionName() 1015 { 1016 const char *name = NULL; 1017 ExecutionContext exe_ctx(GetFrameSP()); 1018 StackFrame *frame = exe_ctx.GetFramePtr(); 1019 Target *target = exe_ctx.GetTargetPtr(); 1020 if (frame && target) 1021 { 1022 SymbolContext sc (frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol)); 1023 if (sc.block) 1024 { 1025 Block *inlined_block = sc.block->GetContainingInlinedBlock (); 1026 if (inlined_block) 1027 { 1028 const InlineFunctionInfo* inlined_info = inlined_block->GetInlinedFunctionInfo(); 1029 name = inlined_info->GetName().AsCString(); 1030 } 1031 } 1032 1033 if (name == NULL) 1034 { 1035 if (sc.function) 1036 name = sc.function->GetName().GetCString(); 1037 } 1038 1039 if (name == NULL) 1040 { 1041 if (sc.symbol) 1042 name = sc.symbol->GetName().GetCString(); 1043 } 1044 } 1045 return name; 1046 } 1047 1048