1 //===-- SBThread.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/SBThread.h" 11 12 #include "lldb/API/SBSymbolContext.h" 13 #include "lldb/API/SBFileSpec.h" 14 #include "lldb/API/SBStream.h" 15 #include "lldb/Breakpoint/BreakpointLocation.h" 16 #include "lldb/Core/Debugger.h" 17 #include "lldb/Core/State.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Core/StreamFile.h" 20 #include "lldb/Core/StructuredData.h" 21 #include "lldb/Core/ValueObject.h" 22 #include "lldb/Interpreter/CommandInterpreter.h" 23 #include "lldb/Symbol/SymbolContext.h" 24 #include "lldb/Symbol/CompileUnit.h" 25 #include "lldb/Target/SystemRuntime.h" 26 #include "lldb/Target/Thread.h" 27 #include "lldb/Target/Process.h" 28 #include "lldb/Target/Queue.h" 29 #include "lldb/Target/UnixSignals.h" 30 #include "lldb/Target/StopInfo.h" 31 #include "lldb/Target/Target.h" 32 #include "lldb/Target/ThreadPlan.h" 33 #include "lldb/Target/ThreadPlanStepInstruction.h" 34 #include "lldb/Target/ThreadPlanStepOut.h" 35 #include "lldb/Target/ThreadPlanStepRange.h" 36 #include "lldb/Target/ThreadPlanStepInRange.h" 37 38 #include "lldb/API/SBAddress.h" 39 #include "lldb/API/SBDebugger.h" 40 #include "lldb/API/SBEvent.h" 41 #include "lldb/API/SBFrame.h" 42 #include "lldb/API/SBProcess.h" 43 #include "lldb/API/SBThreadCollection.h" 44 #include "lldb/API/SBThreadPlan.h" 45 #include "lldb/API/SBValue.h" 46 47 using namespace lldb; 48 using namespace lldb_private; 49 50 const char * 51 SBThread::GetBroadcasterClassName () 52 { 53 return Thread::GetStaticBroadcasterClass().AsCString(); 54 } 55 56 //---------------------------------------------------------------------- 57 // Constructors 58 //---------------------------------------------------------------------- 59 SBThread::SBThread () : 60 m_opaque_sp (new ExecutionContextRef()) 61 { 62 } 63 64 SBThread::SBThread (const ThreadSP& lldb_object_sp) : 65 m_opaque_sp (new ExecutionContextRef(lldb_object_sp)) 66 { 67 } 68 69 SBThread::SBThread (const SBThread &rhs) : 70 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp)) 71 { 72 73 } 74 75 //---------------------------------------------------------------------- 76 // Assignment operator 77 //---------------------------------------------------------------------- 78 79 const lldb::SBThread & 80 SBThread::operator = (const SBThread &rhs) 81 { 82 if (this != &rhs) 83 *m_opaque_sp = *rhs.m_opaque_sp; 84 return *this; 85 } 86 87 //---------------------------------------------------------------------- 88 // Destructor 89 //---------------------------------------------------------------------- 90 SBThread::~SBThread() 91 { 92 } 93 94 lldb::SBQueue 95 SBThread::GetQueue () const 96 { 97 SBQueue sb_queue; 98 QueueSP queue_sp; 99 std::unique_lock<std::recursive_mutex> lock; 100 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 101 102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 103 if (exe_ctx.HasThreadScope()) 104 { 105 Process::StopLocker stop_locker; 106 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 107 { 108 queue_sp = exe_ctx.GetThreadPtr()->GetQueue(); 109 if (queue_sp) 110 { 111 sb_queue.SetQueue (queue_sp); 112 } 113 } 114 else 115 { 116 if (log) 117 log->Printf ("SBThread(%p)::GetQueue() => error: process is running", 118 static_cast<void*>(exe_ctx.GetThreadPtr())); 119 } 120 } 121 122 if (log) 123 log->Printf ("SBThread(%p)::GetQueue () => SBQueue(%p)", 124 static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get())); 125 126 return sb_queue; 127 } 128 129 130 bool 131 SBThread::IsValid() const 132 { 133 std::unique_lock<std::recursive_mutex> lock; 134 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 135 136 Target *target = exe_ctx.GetTargetPtr(); 137 Process *process = exe_ctx.GetProcessPtr(); 138 if (target && process) 139 { 140 Process::StopLocker stop_locker; 141 if (stop_locker.TryLock(&process->GetRunLock())) 142 return m_opaque_sp->GetThreadSP().get() != NULL; 143 } 144 // Without a valid target & process, this thread can't be valid. 145 return false; 146 } 147 148 void 149 SBThread::Clear () 150 { 151 m_opaque_sp->Clear(); 152 } 153 154 155 StopReason 156 SBThread::GetStopReason() 157 { 158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 159 160 StopReason reason = eStopReasonInvalid; 161 std::unique_lock<std::recursive_mutex> lock; 162 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 163 164 if (exe_ctx.HasThreadScope()) 165 { 166 Process::StopLocker stop_locker; 167 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 168 { 169 return exe_ctx.GetThreadPtr()->GetStopReason(); 170 } 171 else 172 { 173 if (log) 174 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", 175 static_cast<void*>(exe_ctx.GetThreadPtr())); 176 } 177 } 178 179 if (log) 180 log->Printf ("SBThread(%p)::GetStopReason () => %s", 181 static_cast<void*>(exe_ctx.GetThreadPtr()), 182 Thread::StopReasonAsCString (reason)); 183 184 return reason; 185 } 186 187 size_t 188 SBThread::GetStopReasonDataCount () 189 { 190 std::unique_lock<std::recursive_mutex> lock; 191 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 192 193 if (exe_ctx.HasThreadScope()) 194 { 195 Process::StopLocker stop_locker; 196 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 197 { 198 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo (); 199 if (stop_info_sp) 200 { 201 StopReason reason = stop_info_sp->GetStopReason(); 202 switch (reason) 203 { 204 case eStopReasonInvalid: 205 case eStopReasonNone: 206 case eStopReasonTrace: 207 case eStopReasonExec: 208 case eStopReasonPlanComplete: 209 case eStopReasonThreadExiting: 210 case eStopReasonInstrumentation: 211 // There is no data for these stop reasons. 212 return 0; 213 214 case eStopReasonBreakpoint: 215 { 216 break_id_t site_id = stop_info_sp->GetValue(); 217 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id)); 218 if (bp_site_sp) 219 return bp_site_sp->GetNumberOfOwners () * 2; 220 else 221 return 0; // Breakpoint must have cleared itself... 222 } 223 break; 224 225 case eStopReasonWatchpoint: 226 return 1; 227 228 case eStopReasonSignal: 229 return 1; 230 231 case eStopReasonException: 232 return 1; 233 } 234 } 235 } 236 else 237 { 238 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 239 if (log) 240 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", 241 static_cast<void*>(exe_ctx.GetThreadPtr())); 242 } 243 } 244 return 0; 245 } 246 247 uint64_t 248 SBThread::GetStopReasonDataAtIndex (uint32_t idx) 249 { 250 std::unique_lock<std::recursive_mutex> lock; 251 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 252 253 if (exe_ctx.HasThreadScope()) 254 { 255 Process::StopLocker stop_locker; 256 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 257 { 258 Thread *thread = exe_ctx.GetThreadPtr(); 259 StopInfoSP stop_info_sp = thread->GetStopInfo (); 260 if (stop_info_sp) 261 { 262 StopReason reason = stop_info_sp->GetStopReason(); 263 switch (reason) 264 { 265 case eStopReasonInvalid: 266 case eStopReasonNone: 267 case eStopReasonTrace: 268 case eStopReasonExec: 269 case eStopReasonPlanComplete: 270 case eStopReasonThreadExiting: 271 case eStopReasonInstrumentation: 272 // There is no data for these stop reasons. 273 return 0; 274 275 case eStopReasonBreakpoint: 276 { 277 break_id_t site_id = stop_info_sp->GetValue(); 278 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id)); 279 if (bp_site_sp) 280 { 281 uint32_t bp_index = idx / 2; 282 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index)); 283 if (bp_loc_sp) 284 { 285 if (idx & 1) 286 { 287 // Odd idx, return the breakpoint location ID 288 return bp_loc_sp->GetID(); 289 } 290 else 291 { 292 // Even idx, return the breakpoint ID 293 return bp_loc_sp->GetBreakpoint().GetID(); 294 } 295 } 296 } 297 return LLDB_INVALID_BREAK_ID; 298 } 299 break; 300 301 case eStopReasonWatchpoint: 302 return stop_info_sp->GetValue(); 303 304 case eStopReasonSignal: 305 return stop_info_sp->GetValue(); 306 307 case eStopReasonException: 308 return stop_info_sp->GetValue(); 309 } 310 } 311 } 312 else 313 { 314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 315 if (log) 316 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", 317 static_cast<void*>(exe_ctx.GetThreadPtr())); 318 } 319 } 320 return 0; 321 } 322 323 bool 324 SBThread::GetStopReasonExtendedInfoAsJSON (lldb::SBStream &stream) 325 { 326 Stream &strm = stream.ref(); 327 328 std::unique_lock<std::recursive_mutex> lock; 329 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 330 331 if (! exe_ctx.HasThreadScope()) 332 return false; 333 334 335 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo(); 336 StructuredData::ObjectSP info = stop_info->GetExtendedInfo(); 337 if (! info) 338 return false; 339 340 info->Dump(strm); 341 342 return true; 343 } 344 345 SBThreadCollection 346 SBThread::GetStopReasonExtendedBacktraces (InstrumentationRuntimeType type) 347 { 348 ThreadCollectionSP threads; 349 threads.reset(new ThreadCollection()); 350 351 // We currently only support ThreadSanitizer. 352 if (type != eInstrumentationRuntimeTypeThreadSanitizer) 353 return threads; 354 355 std::unique_lock<std::recursive_mutex> lock; 356 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 357 358 if (! exe_ctx.HasThreadScope()) 359 return threads; 360 361 ProcessSP process_sp = exe_ctx.GetProcessSP(); 362 363 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo(); 364 StructuredData::ObjectSP info = stop_info->GetExtendedInfo(); 365 if (! info) 366 return threads; 367 368 return process_sp->GetInstrumentationRuntime(type)->GetBacktracesFromExtendedStopInfo(info); 369 } 370 371 size_t 372 SBThread::GetStopDescription (char *dst, size_t dst_len) 373 { 374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 375 376 std::unique_lock<std::recursive_mutex> lock; 377 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 378 379 if (exe_ctx.HasThreadScope()) 380 { 381 Process::StopLocker stop_locker; 382 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 383 { 384 385 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo (); 386 if (stop_info_sp) 387 { 388 const char *stop_desc = stop_info_sp->GetDescription(); 389 if (stop_desc) 390 { 391 if (log) 392 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"", 393 static_cast<void*>(exe_ctx.GetThreadPtr()), 394 stop_desc); 395 if (dst) 396 return ::snprintf (dst, dst_len, "%s", stop_desc); 397 else 398 { 399 // NULL dst passed in, return the length needed to contain the description 400 return ::strlen (stop_desc) + 1; // Include the NULL byte for size 401 } 402 } 403 else 404 { 405 size_t stop_desc_len = 0; 406 switch (stop_info_sp->GetStopReason()) 407 { 408 case eStopReasonTrace: 409 case eStopReasonPlanComplete: 410 { 411 static char trace_desc[] = "step"; 412 stop_desc = trace_desc; 413 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size 414 } 415 break; 416 417 case eStopReasonBreakpoint: 418 { 419 static char bp_desc[] = "breakpoint hit"; 420 stop_desc = bp_desc; 421 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size 422 } 423 break; 424 425 case eStopReasonWatchpoint: 426 { 427 static char wp_desc[] = "watchpoint hit"; 428 stop_desc = wp_desc; 429 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size 430 } 431 break; 432 433 case eStopReasonSignal: 434 { 435 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString(stop_info_sp->GetValue()); 436 if (stop_desc == NULL || stop_desc[0] == '\0') 437 { 438 static char signal_desc[] = "signal"; 439 stop_desc = signal_desc; 440 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size 441 } 442 } 443 break; 444 445 case eStopReasonException: 446 { 447 char exc_desc[] = "exception"; 448 stop_desc = exc_desc; 449 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size 450 } 451 break; 452 453 case eStopReasonExec: 454 { 455 char exc_desc[] = "exec"; 456 stop_desc = exc_desc; 457 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size 458 } 459 break; 460 461 case eStopReasonThreadExiting: 462 { 463 char limbo_desc[] = "thread exiting"; 464 stop_desc = limbo_desc; 465 stop_desc_len = sizeof(limbo_desc); 466 } 467 break; 468 default: 469 break; 470 } 471 472 if (stop_desc && stop_desc[0]) 473 { 474 if (log) 475 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'", 476 static_cast<void*>(exe_ctx.GetThreadPtr()), 477 stop_desc); 478 479 if (dst) 480 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte 481 482 if (stop_desc_len == 0) 483 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte 484 485 return stop_desc_len; 486 } 487 } 488 } 489 } 490 else 491 { 492 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 493 if (log) 494 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", 495 static_cast<void*>(exe_ctx.GetThreadPtr())); 496 } 497 } 498 if (dst) 499 *dst = 0; 500 return 0; 501 } 502 503 SBValue 504 SBThread::GetStopReturnValue () 505 { 506 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 507 ValueObjectSP return_valobj_sp; 508 std::unique_lock<std::recursive_mutex> lock; 509 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 510 511 if (exe_ctx.HasThreadScope()) 512 { 513 Process::StopLocker stop_locker; 514 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 515 { 516 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo (); 517 if (stop_info_sp) 518 { 519 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp); 520 } 521 } 522 else 523 { 524 if (log) 525 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", 526 static_cast<void*>(exe_ctx.GetThreadPtr())); 527 } 528 } 529 530 if (log) 531 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", 532 static_cast<void*>(exe_ctx.GetThreadPtr()), 533 return_valobj_sp.get() 534 ? return_valobj_sp->GetValueAsCString() 535 : "<no return value>"); 536 537 return SBValue (return_valobj_sp); 538 } 539 540 void 541 SBThread::SetThread (const ThreadSP& lldb_object_sp) 542 { 543 m_opaque_sp->SetThreadSP (lldb_object_sp); 544 } 545 546 lldb::tid_t 547 SBThread::GetThreadID () const 548 { 549 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 550 if (thread_sp) 551 return thread_sp->GetID(); 552 return LLDB_INVALID_THREAD_ID; 553 } 554 555 uint32_t 556 SBThread::GetIndexID () const 557 { 558 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 559 if (thread_sp) 560 return thread_sp->GetIndexID(); 561 return LLDB_INVALID_INDEX32; 562 } 563 564 const char * 565 SBThread::GetName () const 566 { 567 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 568 const char *name = NULL; 569 std::unique_lock<std::recursive_mutex> lock; 570 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 571 572 if (exe_ctx.HasThreadScope()) 573 { 574 Process::StopLocker stop_locker; 575 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 576 { 577 name = exe_ctx.GetThreadPtr()->GetName(); 578 } 579 else 580 { 581 if (log) 582 log->Printf ("SBThread(%p)::GetName() => error: process is running", 583 static_cast<void*>(exe_ctx.GetThreadPtr())); 584 } 585 } 586 587 if (log) 588 log->Printf ("SBThread(%p)::GetName () => %s", 589 static_cast<void*>(exe_ctx.GetThreadPtr()), 590 name ? name : "NULL"); 591 592 return name; 593 } 594 595 const char * 596 SBThread::GetQueueName () const 597 { 598 const char *name = NULL; 599 std::unique_lock<std::recursive_mutex> lock; 600 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 601 602 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 603 if (exe_ctx.HasThreadScope()) 604 { 605 Process::StopLocker stop_locker; 606 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 607 { 608 name = exe_ctx.GetThreadPtr()->GetQueueName(); 609 } 610 else 611 { 612 if (log) 613 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", 614 static_cast<void*>(exe_ctx.GetThreadPtr())); 615 } 616 } 617 618 if (log) 619 log->Printf ("SBThread(%p)::GetQueueName () => %s", 620 static_cast<void*>(exe_ctx.GetThreadPtr()), 621 name ? name : "NULL"); 622 623 return name; 624 } 625 626 lldb::queue_id_t 627 SBThread::GetQueueID () const 628 { 629 queue_id_t id = LLDB_INVALID_QUEUE_ID; 630 std::unique_lock<std::recursive_mutex> lock; 631 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 632 633 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 634 if (exe_ctx.HasThreadScope()) 635 { 636 Process::StopLocker stop_locker; 637 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 638 { 639 id = exe_ctx.GetThreadPtr()->GetQueueID(); 640 } 641 else 642 { 643 if (log) 644 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running", 645 static_cast<void*>(exe_ctx.GetThreadPtr())); 646 } 647 } 648 649 if (log) 650 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64, 651 static_cast<void*>(exe_ctx.GetThreadPtr()), id); 652 653 return id; 654 } 655 656 bool 657 SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm) 658 { 659 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 660 bool success = false; 661 std::unique_lock<std::recursive_mutex> lock; 662 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 663 664 if (exe_ctx.HasThreadScope()) 665 { 666 Process::StopLocker stop_locker; 667 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 668 { 669 Thread *thread = exe_ctx.GetThreadPtr(); 670 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo(); 671 if (info_root_sp) 672 { 673 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path); 674 if (node) 675 { 676 if (node->GetType() == StructuredData::Type::eTypeString) 677 { 678 strm.Printf ("%s", node->GetAsString()->GetValue().c_str()); 679 success = true; 680 } 681 if (node->GetType() == StructuredData::Type::eTypeInteger) 682 { 683 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue()); 684 success = true; 685 } 686 if (node->GetType() == StructuredData::Type::eTypeFloat) 687 { 688 strm.Printf ("0x%f", node->GetAsFloat()->GetValue()); 689 success = true; 690 } 691 if (node->GetType() == StructuredData::Type::eTypeBoolean) 692 { 693 if (node->GetAsBoolean()->GetValue() == true) 694 strm.Printf ("true"); 695 else 696 strm.Printf ("false"); 697 success = true; 698 } 699 if (node->GetType() == StructuredData::Type::eTypeNull) 700 { 701 strm.Printf ("null"); 702 success = true; 703 } 704 } 705 } 706 } 707 else 708 { 709 if (log) 710 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running", 711 static_cast<void*>(exe_ctx.GetThreadPtr())); 712 } 713 } 714 715 if (log) 716 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s", 717 static_cast<void*>(exe_ctx.GetThreadPtr()), 718 strm.GetData()); 719 720 return success; 721 } 722 723 724 SBError 725 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan) 726 { 727 SBError sb_error; 728 729 Process *process = exe_ctx.GetProcessPtr(); 730 if (!process) 731 { 732 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan"); 733 return sb_error; 734 } 735 736 Thread *thread = exe_ctx.GetThreadPtr(); 737 if (!thread) 738 { 739 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan"); 740 return sb_error; 741 } 742 743 // User level plans should be Master Plans so they can be interrupted, other plans executed, and 744 // then a "continue" will resume the plan. 745 if (new_plan != NULL) 746 { 747 new_plan->SetIsMasterPlan(true); 748 new_plan->SetOkayToDiscard(false); 749 } 750 751 // Why do we need to set the current thread by ID here??? 752 process->GetThreadList().SetSelectedThreadByID (thread->GetID()); 753 754 if (process->GetTarget().GetDebugger().GetAsyncExecution ()) 755 sb_error.ref() = process->Resume (); 756 else 757 sb_error.ref() = process->ResumeSynchronous (NULL); 758 759 return sb_error; 760 } 761 762 void 763 SBThread::StepOver (lldb::RunMode stop_other_threads) 764 { 765 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 766 767 std::unique_lock<std::recursive_mutex> lock; 768 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 769 770 if (log) 771 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", 772 static_cast<void*>(exe_ctx.GetThreadPtr()), 773 Thread::RunModeAsCString (stop_other_threads)); 774 775 if (exe_ctx.HasThreadScope()) 776 { 777 Thread *thread = exe_ctx.GetThreadPtr(); 778 bool abort_other_plans = false; 779 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0)); 780 781 ThreadPlanSP new_plan_sp; 782 if (frame_sp) 783 { 784 if (frame_sp->HasDebugInformation ()) 785 { 786 const LazyBool avoid_no_debug = eLazyBoolCalculate; 787 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); 788 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans, 789 sc.line_entry, 790 sc, 791 stop_other_threads, 792 avoid_no_debug); 793 } 794 else 795 { 796 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true, 797 abort_other_plans, 798 stop_other_threads); 799 } 800 } 801 802 // This returns an error, we should use it! 803 ResumeNewPlan (exe_ctx, new_plan_sp.get()); 804 } 805 } 806 807 void 808 SBThread::StepInto (lldb::RunMode stop_other_threads) 809 { 810 StepInto (NULL, stop_other_threads); 811 } 812 813 void 814 SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads) 815 { 816 SBError error; 817 StepInto(target_name, LLDB_INVALID_LINE_NUMBER, error, stop_other_threads); 818 } 819 820 void 821 SBThread::StepInto (const char *target_name, uint32_t end_line, SBError &error, lldb::RunMode stop_other_threads) 822 { 823 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 824 825 std::unique_lock<std::recursive_mutex> lock; 826 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 827 828 if (log) 829 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')", 830 static_cast<void*>(exe_ctx.GetThreadPtr()), 831 target_name? target_name: "<NULL>", 832 Thread::RunModeAsCString (stop_other_threads)); 833 834 if (exe_ctx.HasThreadScope()) 835 { 836 bool abort_other_plans = false; 837 838 Thread *thread = exe_ctx.GetThreadPtr(); 839 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0)); 840 ThreadPlanSP new_plan_sp; 841 842 if (frame_sp && frame_sp->HasDebugInformation ()) 843 { 844 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); 845 AddressRange range; 846 if (end_line == LLDB_INVALID_LINE_NUMBER) 847 range = sc.line_entry.range; 848 else 849 { 850 if (!sc.GetAddressRangeFromHereToEndLine(end_line, range, error.ref())) 851 return; 852 } 853 854 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate; 855 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate; 856 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans, 857 range, 858 sc, 859 target_name, 860 stop_other_threads, 861 step_in_avoids_code_without_debug_info, 862 step_out_avoids_code_without_debug_info); 863 } 864 else 865 { 866 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false, 867 abort_other_plans, 868 stop_other_threads); 869 } 870 871 error = ResumeNewPlan (exe_ctx, new_plan_sp.get()); 872 } 873 } 874 875 void 876 SBThread::StepOut () 877 { 878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 879 880 std::unique_lock<std::recursive_mutex> lock; 881 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 882 883 if (log) 884 log->Printf ("SBThread(%p)::StepOut ()", 885 static_cast<void*>(exe_ctx.GetThreadPtr())); 886 887 if (exe_ctx.HasThreadScope()) 888 { 889 bool abort_other_plans = false; 890 bool stop_other_threads = false; 891 892 Thread *thread = exe_ctx.GetThreadPtr(); 893 894 const LazyBool avoid_no_debug = eLazyBoolCalculate; 895 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans, 896 NULL, 897 false, 898 stop_other_threads, 899 eVoteYes, 900 eVoteNoOpinion, 901 0, 902 avoid_no_debug)); 903 904 // This returns an error, we should use it! 905 ResumeNewPlan (exe_ctx, new_plan_sp.get()); 906 } 907 } 908 909 void 910 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame) 911 { 912 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 913 914 std::unique_lock<std::recursive_mutex> lock; 915 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 916 917 if (!sb_frame.IsValid()) 918 { 919 if (log) 920 log->Printf("SBThread(%p)::StepOutOfFrame passed an invalid frame, returning.", 921 static_cast<void*>(exe_ctx.GetThreadPtr())); 922 return; 923 } 924 925 StackFrameSP frame_sp (sb_frame.GetFrameSP()); 926 if (log) 927 { 928 SBStream frame_desc_strm; 929 sb_frame.GetDescription (frame_desc_strm); 930 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)", 931 static_cast<void*>(exe_ctx.GetThreadPtr()), 932 static_cast<void*>(frame_sp.get()), 933 frame_desc_strm.GetData()); 934 } 935 936 if (exe_ctx.HasThreadScope()) 937 { 938 bool abort_other_plans = false; 939 bool stop_other_threads = false; 940 Thread *thread = exe_ctx.GetThreadPtr(); 941 if (sb_frame.GetThread().GetThreadID() != thread->GetID()) 942 { 943 log->Printf("SBThread(%p)::StepOutOfFrame passed a frame from another thread (0x%" PRIx64 " vrs. 0x%" PRIx64 ", returning.", 944 static_cast<void*>(exe_ctx.GetThreadPtr()), 945 sb_frame.GetThread().GetThreadID(), 946 thread->GetID()); 947 } 948 949 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans, 950 NULL, 951 false, 952 stop_other_threads, 953 eVoteYes, 954 eVoteNoOpinion, 955 frame_sp->GetFrameIndex())); 956 957 // This returns an error, we should use it! 958 ResumeNewPlan (exe_ctx, new_plan_sp.get()); 959 } 960 } 961 962 void 963 SBThread::StepInstruction (bool step_over) 964 { 965 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 966 967 std::unique_lock<std::recursive_mutex> lock; 968 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 969 970 if (log) 971 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", 972 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over); 973 974 if (exe_ctx.HasThreadScope()) 975 { 976 Thread *thread = exe_ctx.GetThreadPtr(); 977 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true)); 978 979 // This returns an error, we should use it! 980 ResumeNewPlan (exe_ctx, new_plan_sp.get()); 981 } 982 } 983 984 void 985 SBThread::RunToAddress (lldb::addr_t addr) 986 { 987 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 988 989 std::unique_lock<std::recursive_mutex> lock; 990 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 991 992 if (log) 993 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")", 994 static_cast<void*>(exe_ctx.GetThreadPtr()), addr); 995 996 if (exe_ctx.HasThreadScope()) 997 { 998 bool abort_other_plans = false; 999 bool stop_other_threads = true; 1000 1001 Address target_addr (addr); 1002 1003 Thread *thread = exe_ctx.GetThreadPtr(); 1004 1005 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans, 1006 target_addr, 1007 stop_other_threads)); 1008 1009 // This returns an error, we should use it! 1010 ResumeNewPlan (exe_ctx, new_plan_sp.get()); 1011 } 1012 } 1013 1014 SBError 1015 SBThread::StepOverUntil (lldb::SBFrame &sb_frame, 1016 lldb::SBFileSpec &sb_file_spec, 1017 uint32_t line) 1018 { 1019 SBError sb_error; 1020 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1021 char path[PATH_MAX]; 1022 1023 std::unique_lock<std::recursive_mutex> lock; 1024 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1025 1026 StackFrameSP frame_sp (sb_frame.GetFrameSP()); 1027 1028 if (log) 1029 { 1030 SBStream frame_desc_strm; 1031 sb_frame.GetDescription (frame_desc_strm); 1032 sb_file_spec->GetPath (path, sizeof(path)); 1033 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)", 1034 static_cast<void*>(exe_ctx.GetThreadPtr()), 1035 static_cast<void*>(frame_sp.get()), 1036 frame_desc_strm.GetData(), path, line); 1037 } 1038 1039 if (exe_ctx.HasThreadScope()) 1040 { 1041 Target *target = exe_ctx.GetTargetPtr(); 1042 Thread *thread = exe_ctx.GetThreadPtr(); 1043 1044 if (line == 0) 1045 { 1046 sb_error.SetErrorString("invalid line argument"); 1047 return sb_error; 1048 } 1049 1050 if (!frame_sp) 1051 { 1052 frame_sp = thread->GetSelectedFrame (); 1053 if (!frame_sp) 1054 frame_sp = thread->GetStackFrameAtIndex (0); 1055 } 1056 1057 SymbolContext frame_sc; 1058 if (!frame_sp) 1059 { 1060 sb_error.SetErrorString("no valid frames in thread to step"); 1061 return sb_error; 1062 } 1063 1064 // If we have a frame, get its line 1065 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit | 1066 eSymbolContextFunction | 1067 eSymbolContextLineEntry | 1068 eSymbolContextSymbol ); 1069 1070 if (frame_sc.comp_unit == NULL) 1071 { 1072 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex()); 1073 return sb_error; 1074 } 1075 1076 FileSpec step_file_spec; 1077 if (sb_file_spec.IsValid()) 1078 { 1079 // The file spec passed in was valid, so use it 1080 step_file_spec = sb_file_spec.ref(); 1081 } 1082 else 1083 { 1084 if (frame_sc.line_entry.IsValid()) 1085 step_file_spec = frame_sc.line_entry.file; 1086 else 1087 { 1088 sb_error.SetErrorString("invalid file argument or no file for frame"); 1089 return sb_error; 1090 } 1091 } 1092 1093 // Grab the current function, then we will make sure the "until" address is 1094 // within the function. We discard addresses that are out of the current 1095 // function, and then if there are no addresses remaining, give an appropriate 1096 // error message. 1097 1098 bool all_in_function = true; 1099 AddressRange fun_range = frame_sc.function->GetAddressRange(); 1100 1101 std::vector<addr_t> step_over_until_addrs; 1102 const bool abort_other_plans = false; 1103 const bool stop_other_threads = false; 1104 const bool check_inlines = true; 1105 const bool exact = false; 1106 1107 SymbolContextList sc_list; 1108 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec, 1109 line, 1110 check_inlines, 1111 exact, 1112 eSymbolContextLineEntry, 1113 sc_list); 1114 if (num_matches > 0) 1115 { 1116 SymbolContext sc; 1117 for (uint32_t i=0; i<num_matches; ++i) 1118 { 1119 if (sc_list.GetContextAtIndex(i, sc)) 1120 { 1121 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target); 1122 if (step_addr != LLDB_INVALID_ADDRESS) 1123 { 1124 if (fun_range.ContainsLoadAddress(step_addr, target)) 1125 step_over_until_addrs.push_back(step_addr); 1126 else 1127 all_in_function = false; 1128 } 1129 } 1130 } 1131 } 1132 1133 if (step_over_until_addrs.empty()) 1134 { 1135 if (all_in_function) 1136 { 1137 step_file_spec.GetPath (path, sizeof(path)); 1138 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line); 1139 } 1140 else 1141 sb_error.SetErrorString ("step until target not in current function"); 1142 } 1143 else 1144 { 1145 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans, 1146 &step_over_until_addrs[0], 1147 step_over_until_addrs.size(), 1148 stop_other_threads, 1149 frame_sp->GetFrameIndex())); 1150 1151 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get()); 1152 } 1153 } 1154 else 1155 { 1156 sb_error.SetErrorString("this SBThread object is invalid"); 1157 } 1158 return sb_error; 1159 } 1160 1161 SBError 1162 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name) 1163 { 1164 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1165 SBError sb_error; 1166 1167 std::unique_lock<std::recursive_mutex> lock; 1168 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1169 1170 if (log) 1171 { 1172 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s", 1173 static_cast<void*>(exe_ctx.GetThreadPtr()), 1174 script_class_name); 1175 } 1176 1177 1178 if (!exe_ctx.HasThreadScope()) 1179 { 1180 sb_error.SetErrorString("this SBThread object is invalid"); 1181 return sb_error; 1182 } 1183 1184 Thread *thread = exe_ctx.GetThreadPtr(); 1185 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false); 1186 1187 if (thread_plan_sp) 1188 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get()); 1189 else 1190 { 1191 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name); 1192 if (log) 1193 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s", 1194 static_cast<void*>(exe_ctx.GetThreadPtr()), 1195 script_class_name); 1196 } 1197 1198 return sb_error; 1199 } 1200 1201 SBError 1202 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line) 1203 { 1204 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1205 SBError sb_error; 1206 1207 std::unique_lock<std::recursive_mutex> lock; 1208 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1209 1210 if (log) 1211 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)", 1212 static_cast<void*>(exe_ctx.GetThreadPtr()), 1213 file_spec->GetPath().c_str(), line); 1214 1215 if (!exe_ctx.HasThreadScope()) 1216 { 1217 sb_error.SetErrorString("this SBThread object is invalid"); 1218 return sb_error; 1219 } 1220 1221 Thread *thread = exe_ctx.GetThreadPtr(); 1222 1223 Error err = thread->JumpToLine (file_spec.get(), line, true); 1224 sb_error.SetError (err); 1225 return sb_error; 1226 } 1227 1228 SBError 1229 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value) 1230 { 1231 SBError sb_error; 1232 1233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1234 1235 std::unique_lock<std::recursive_mutex> lock; 1236 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1237 1238 if (log) 1239 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", 1240 static_cast<void*>(exe_ctx.GetThreadPtr()), 1241 frame.GetFrameID()); 1242 1243 if (exe_ctx.HasThreadScope()) 1244 { 1245 Thread *thread = exe_ctx.GetThreadPtr(); 1246 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP())); 1247 } 1248 1249 return sb_error; 1250 } 1251 1252 1253 bool 1254 SBThread::Suspend() 1255 { 1256 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1257 std::unique_lock<std::recursive_mutex> lock; 1258 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1259 1260 bool result = false; 1261 if (exe_ctx.HasThreadScope()) 1262 { 1263 Process::StopLocker stop_locker; 1264 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1265 { 1266 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended); 1267 result = true; 1268 } 1269 else 1270 { 1271 if (log) 1272 log->Printf ("SBThread(%p)::Suspend() => error: process is running", 1273 static_cast<void*>(exe_ctx.GetThreadPtr())); 1274 } 1275 } 1276 if (log) 1277 log->Printf ("SBThread(%p)::Suspend() => %i", 1278 static_cast<void*>(exe_ctx.GetThreadPtr()), result); 1279 return result; 1280 } 1281 1282 bool 1283 SBThread::Resume () 1284 { 1285 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1286 std::unique_lock<std::recursive_mutex> lock; 1287 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1288 1289 bool result = false; 1290 if (exe_ctx.HasThreadScope()) 1291 { 1292 Process::StopLocker stop_locker; 1293 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1294 { 1295 const bool override_suspend = true; 1296 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend); 1297 result = true; 1298 } 1299 else 1300 { 1301 if (log) 1302 log->Printf ("SBThread(%p)::Resume() => error: process is running", 1303 static_cast<void*>(exe_ctx.GetThreadPtr())); 1304 } 1305 } 1306 if (log) 1307 log->Printf ("SBThread(%p)::Resume() => %i", 1308 static_cast<void*>(exe_ctx.GetThreadPtr()), result); 1309 return result; 1310 } 1311 1312 bool 1313 SBThread::IsSuspended() 1314 { 1315 std::unique_lock<std::recursive_mutex> lock; 1316 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1317 1318 if (exe_ctx.HasThreadScope()) 1319 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended; 1320 return false; 1321 } 1322 1323 bool 1324 SBThread::IsStopped() 1325 { 1326 std::unique_lock<std::recursive_mutex> lock; 1327 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1328 1329 if (exe_ctx.HasThreadScope()) 1330 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true); 1331 return false; 1332 } 1333 1334 SBProcess 1335 SBThread::GetProcess () 1336 { 1337 SBProcess sb_process; 1338 std::unique_lock<std::recursive_mutex> lock; 1339 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1340 1341 if (exe_ctx.HasThreadScope()) 1342 { 1343 // Have to go up to the target so we can get a shared pointer to our process... 1344 sb_process.SetSP (exe_ctx.GetProcessSP()); 1345 } 1346 1347 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1348 if (log) 1349 { 1350 SBStream frame_desc_strm; 1351 sb_process.GetDescription (frame_desc_strm); 1352 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", 1353 static_cast<void*>(exe_ctx.GetThreadPtr()), 1354 static_cast<void*>(sb_process.GetSP().get()), 1355 frame_desc_strm.GetData()); 1356 } 1357 1358 return sb_process; 1359 } 1360 1361 uint32_t 1362 SBThread::GetNumFrames () 1363 { 1364 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1365 1366 uint32_t num_frames = 0; 1367 std::unique_lock<std::recursive_mutex> lock; 1368 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1369 1370 if (exe_ctx.HasThreadScope()) 1371 { 1372 Process::StopLocker stop_locker; 1373 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1374 { 1375 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount(); 1376 } 1377 else 1378 { 1379 if (log) 1380 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", 1381 static_cast<void*>(exe_ctx.GetThreadPtr())); 1382 } 1383 } 1384 1385 if (log) 1386 log->Printf ("SBThread(%p)::GetNumFrames () => %u", 1387 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames); 1388 1389 return num_frames; 1390 } 1391 1392 SBFrame 1393 SBThread::GetFrameAtIndex (uint32_t idx) 1394 { 1395 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1396 1397 SBFrame sb_frame; 1398 StackFrameSP frame_sp; 1399 std::unique_lock<std::recursive_mutex> lock; 1400 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1401 1402 if (exe_ctx.HasThreadScope()) 1403 { 1404 Process::StopLocker stop_locker; 1405 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1406 { 1407 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx); 1408 sb_frame.SetFrameSP (frame_sp); 1409 } 1410 else 1411 { 1412 if (log) 1413 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", 1414 static_cast<void*>(exe_ctx.GetThreadPtr())); 1415 } 1416 } 1417 1418 if (log) 1419 { 1420 SBStream frame_desc_strm; 1421 sb_frame.GetDescription (frame_desc_strm); 1422 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s", 1423 static_cast<void*>(exe_ctx.GetThreadPtr()), idx, 1424 static_cast<void*>(frame_sp.get()), 1425 frame_desc_strm.GetData()); 1426 } 1427 1428 return sb_frame; 1429 } 1430 1431 lldb::SBFrame 1432 SBThread::GetSelectedFrame () 1433 { 1434 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1435 1436 SBFrame sb_frame; 1437 StackFrameSP frame_sp; 1438 std::unique_lock<std::recursive_mutex> lock; 1439 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1440 1441 if (exe_ctx.HasThreadScope()) 1442 { 1443 Process::StopLocker stop_locker; 1444 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1445 { 1446 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame (); 1447 sb_frame.SetFrameSP (frame_sp); 1448 } 1449 else 1450 { 1451 if (log) 1452 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", 1453 static_cast<void*>(exe_ctx.GetThreadPtr())); 1454 } 1455 } 1456 1457 if (log) 1458 { 1459 SBStream frame_desc_strm; 1460 sb_frame.GetDescription (frame_desc_strm); 1461 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s", 1462 static_cast<void*>(exe_ctx.GetThreadPtr()), 1463 static_cast<void*>(frame_sp.get()), 1464 frame_desc_strm.GetData()); 1465 } 1466 1467 return sb_frame; 1468 } 1469 1470 lldb::SBFrame 1471 SBThread::SetSelectedFrame (uint32_t idx) 1472 { 1473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1474 1475 SBFrame sb_frame; 1476 StackFrameSP frame_sp; 1477 std::unique_lock<std::recursive_mutex> lock; 1478 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1479 1480 if (exe_ctx.HasThreadScope()) 1481 { 1482 Process::StopLocker stop_locker; 1483 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1484 { 1485 Thread *thread = exe_ctx.GetThreadPtr(); 1486 frame_sp = thread->GetStackFrameAtIndex (idx); 1487 if (frame_sp) 1488 { 1489 thread->SetSelectedFrame (frame_sp.get()); 1490 sb_frame.SetFrameSP (frame_sp); 1491 } 1492 } 1493 else 1494 { 1495 if (log) 1496 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", 1497 static_cast<void*>(exe_ctx.GetThreadPtr())); 1498 } 1499 } 1500 1501 if (log) 1502 { 1503 SBStream frame_desc_strm; 1504 sb_frame.GetDescription (frame_desc_strm); 1505 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s", 1506 static_cast<void*>(exe_ctx.GetThreadPtr()), idx, 1507 static_cast<void*>(frame_sp.get()), 1508 frame_desc_strm.GetData()); 1509 } 1510 return sb_frame; 1511 } 1512 1513 bool 1514 SBThread::EventIsThreadEvent (const SBEvent &event) 1515 { 1516 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL; 1517 } 1518 1519 SBFrame 1520 SBThread::GetStackFrameFromEvent (const SBEvent &event) 1521 { 1522 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get()); 1523 1524 } 1525 1526 SBThread 1527 SBThread::GetThreadFromEvent (const SBEvent &event) 1528 { 1529 return Thread::ThreadEventData::GetThreadFromEvent (event.get()); 1530 } 1531 1532 bool 1533 SBThread::operator == (const SBThread &rhs) const 1534 { 1535 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get(); 1536 } 1537 1538 bool 1539 SBThread::operator != (const SBThread &rhs) const 1540 { 1541 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get(); 1542 } 1543 1544 bool 1545 SBThread::GetStatus (SBStream &status) const 1546 { 1547 Stream &strm = status.ref(); 1548 1549 std::unique_lock<std::recursive_mutex> lock; 1550 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1551 1552 if (exe_ctx.HasThreadScope()) 1553 { 1554 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1); 1555 } 1556 else 1557 strm.PutCString ("No status"); 1558 1559 return true; 1560 } 1561 1562 bool 1563 SBThread::GetDescription (SBStream &description) const 1564 { 1565 Stream &strm = description.ref(); 1566 1567 std::unique_lock<std::recursive_mutex> lock; 1568 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1569 1570 if (exe_ctx.HasThreadScope()) 1571 { 1572 exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm, LLDB_INVALID_THREAD_ID); 1573 //strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID()); 1574 } 1575 else 1576 strm.PutCString ("No value"); 1577 1578 return true; 1579 } 1580 1581 SBThread 1582 SBThread::GetExtendedBacktraceThread (const char *type) 1583 { 1584 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1585 std::unique_lock<std::recursive_mutex> lock; 1586 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1587 SBThread sb_origin_thread; 1588 1589 if (exe_ctx.HasThreadScope()) 1590 { 1591 Process::StopLocker stop_locker; 1592 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1593 { 1594 ThreadSP real_thread(exe_ctx.GetThreadSP()); 1595 if (real_thread) 1596 { 1597 ConstString type_const (type); 1598 Process *process = exe_ctx.GetProcessPtr(); 1599 if (process) 1600 { 1601 SystemRuntime *runtime = process->GetSystemRuntime(); 1602 if (runtime) 1603 { 1604 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const)); 1605 if (new_thread_sp) 1606 { 1607 // Save this in the Process' ExtendedThreadList so a strong pointer retains the 1608 // object. 1609 process->GetExtendedThreadList().AddThread (new_thread_sp); 1610 sb_origin_thread.SetThread (new_thread_sp); 1611 if (log) 1612 { 1613 const char *queue_name = new_thread_sp->GetQueueName(); 1614 if (queue_name == NULL) 1615 queue_name = ""; 1616 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread " 1617 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'", 1618 static_cast<void*>(exe_ctx.GetThreadPtr()), 1619 static_cast<void*>(new_thread_sp.get()), 1620 new_thread_sp->GetQueueID(), 1621 queue_name); 1622 } 1623 } 1624 } 1625 } 1626 } 1627 } 1628 else 1629 { 1630 if (log) 1631 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running", 1632 static_cast<void*>(exe_ctx.GetThreadPtr())); 1633 } 1634 } 1635 1636 if (log && sb_origin_thread.IsValid() == false) 1637 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread", 1638 static_cast<void*>(exe_ctx.GetThreadPtr())); 1639 return sb_origin_thread; 1640 } 1641 1642 uint32_t 1643 SBThread::GetExtendedBacktraceOriginatingIndexID () 1644 { 1645 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1646 if (thread_sp) 1647 return thread_sp->GetExtendedBacktraceOriginatingIndexID(); 1648 return LLDB_INVALID_INDEX32; 1649 } 1650 1651 bool 1652 SBThread::SafeToCallFunctions () 1653 { 1654 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1655 if (thread_sp) 1656 return thread_sp->SafeToCallFunctions(); 1657 return true; 1658 } 1659 1660 lldb_private::Thread * 1661 SBThread::operator->() 1662 { 1663 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1664 if (thread_sp) 1665 return thread_sp.get(); 1666 else 1667 return NULL; 1668 } 1669 1670 lldb_private::Thread * 1671 SBThread::get() 1672 { 1673 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1674 if (thread_sp) 1675 return thread_sp.get(); 1676 else 1677 return NULL; 1678 } 1679 1680