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 return StepUsingScriptedThreadPlan(script_class_name, true); 1165 } 1166 1167 SBError 1168 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name, bool resume_immediately) 1169 { 1170 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1171 SBError sb_error; 1172 1173 std::unique_lock<std::recursive_mutex> lock; 1174 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1175 1176 if (log) 1177 { 1178 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s", 1179 static_cast<void*>(exe_ctx.GetThreadPtr()), 1180 script_class_name); 1181 } 1182 1183 1184 if (!exe_ctx.HasThreadScope()) 1185 { 1186 sb_error.SetErrorString("this SBThread object is invalid"); 1187 return sb_error; 1188 } 1189 1190 Thread *thread = exe_ctx.GetThreadPtr(); 1191 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false); 1192 1193 if (!thread_plan_sp) 1194 { 1195 sb_error.SetErrorStringWithFormat("Error queueing thread plan for class: %s", script_class_name); 1196 return sb_error; 1197 } 1198 1199 if (!resume_immediately) 1200 { 1201 return sb_error; 1202 } 1203 1204 if (thread_plan_sp) 1205 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get()); 1206 else 1207 { 1208 sb_error.SetErrorStringWithFormat("Error resuming thread plan for class: %s.", script_class_name); 1209 if (log) 1210 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s", 1211 static_cast<void*>(exe_ctx.GetThreadPtr()), 1212 script_class_name); 1213 } 1214 1215 return sb_error; 1216 } 1217 1218 SBError 1219 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line) 1220 { 1221 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1222 SBError sb_error; 1223 1224 std::unique_lock<std::recursive_mutex> lock; 1225 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1226 1227 if (log) 1228 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)", 1229 static_cast<void*>(exe_ctx.GetThreadPtr()), 1230 file_spec->GetPath().c_str(), line); 1231 1232 if (!exe_ctx.HasThreadScope()) 1233 { 1234 sb_error.SetErrorString("this SBThread object is invalid"); 1235 return sb_error; 1236 } 1237 1238 Thread *thread = exe_ctx.GetThreadPtr(); 1239 1240 Error err = thread->JumpToLine (file_spec.get(), line, true); 1241 sb_error.SetError (err); 1242 return sb_error; 1243 } 1244 1245 SBError 1246 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value) 1247 { 1248 SBError sb_error; 1249 1250 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1251 1252 std::unique_lock<std::recursive_mutex> lock; 1253 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1254 1255 if (log) 1256 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", 1257 static_cast<void*>(exe_ctx.GetThreadPtr()), 1258 frame.GetFrameID()); 1259 1260 if (exe_ctx.HasThreadScope()) 1261 { 1262 Thread *thread = exe_ctx.GetThreadPtr(); 1263 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP())); 1264 } 1265 1266 return sb_error; 1267 } 1268 1269 SBError 1270 SBThread::UnwindInnermostExpression() 1271 { 1272 SBError sb_error; 1273 1274 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1275 1276 std::unique_lock<std::recursive_mutex> lock; 1277 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1278 1279 if (log) 1280 log->Printf ("SBThread(%p)::UnwindExpressionEvaluation", 1281 static_cast<void*>(exe_ctx.GetThreadPtr())); 1282 1283 if (exe_ctx.HasThreadScope()) 1284 { 1285 Thread *thread = exe_ctx.GetThreadPtr(); 1286 sb_error.SetError (thread->UnwindInnermostExpression()); 1287 if (sb_error.Success()) 1288 thread->SetSelectedFrameByIndex(0, false); 1289 } 1290 1291 return sb_error; 1292 1293 } 1294 1295 bool 1296 SBThread::Suspend() 1297 { 1298 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1299 std::unique_lock<std::recursive_mutex> lock; 1300 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1301 1302 bool result = false; 1303 if (exe_ctx.HasThreadScope()) 1304 { 1305 Process::StopLocker stop_locker; 1306 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1307 { 1308 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended); 1309 result = true; 1310 } 1311 else 1312 { 1313 if (log) 1314 log->Printf ("SBThread(%p)::Suspend() => error: process is running", 1315 static_cast<void*>(exe_ctx.GetThreadPtr())); 1316 } 1317 } 1318 if (log) 1319 log->Printf ("SBThread(%p)::Suspend() => %i", 1320 static_cast<void*>(exe_ctx.GetThreadPtr()), result); 1321 return result; 1322 } 1323 1324 bool 1325 SBThread::Resume () 1326 { 1327 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1328 std::unique_lock<std::recursive_mutex> lock; 1329 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1330 1331 bool result = false; 1332 if (exe_ctx.HasThreadScope()) 1333 { 1334 Process::StopLocker stop_locker; 1335 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1336 { 1337 const bool override_suspend = true; 1338 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend); 1339 result = true; 1340 } 1341 else 1342 { 1343 if (log) 1344 log->Printf ("SBThread(%p)::Resume() => error: process is running", 1345 static_cast<void*>(exe_ctx.GetThreadPtr())); 1346 } 1347 } 1348 if (log) 1349 log->Printf ("SBThread(%p)::Resume() => %i", 1350 static_cast<void*>(exe_ctx.GetThreadPtr()), result); 1351 return result; 1352 } 1353 1354 bool 1355 SBThread::IsSuspended() 1356 { 1357 std::unique_lock<std::recursive_mutex> lock; 1358 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1359 1360 if (exe_ctx.HasThreadScope()) 1361 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended; 1362 return false; 1363 } 1364 1365 bool 1366 SBThread::IsStopped() 1367 { 1368 std::unique_lock<std::recursive_mutex> lock; 1369 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1370 1371 if (exe_ctx.HasThreadScope()) 1372 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true); 1373 return false; 1374 } 1375 1376 SBProcess 1377 SBThread::GetProcess () 1378 { 1379 SBProcess sb_process; 1380 std::unique_lock<std::recursive_mutex> lock; 1381 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1382 1383 if (exe_ctx.HasThreadScope()) 1384 { 1385 // Have to go up to the target so we can get a shared pointer to our process... 1386 sb_process.SetSP (exe_ctx.GetProcessSP()); 1387 } 1388 1389 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1390 if (log) 1391 { 1392 SBStream frame_desc_strm; 1393 sb_process.GetDescription (frame_desc_strm); 1394 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", 1395 static_cast<void*>(exe_ctx.GetThreadPtr()), 1396 static_cast<void*>(sb_process.GetSP().get()), 1397 frame_desc_strm.GetData()); 1398 } 1399 1400 return sb_process; 1401 } 1402 1403 uint32_t 1404 SBThread::GetNumFrames () 1405 { 1406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1407 1408 uint32_t num_frames = 0; 1409 std::unique_lock<std::recursive_mutex> lock; 1410 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1411 1412 if (exe_ctx.HasThreadScope()) 1413 { 1414 Process::StopLocker stop_locker; 1415 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1416 { 1417 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount(); 1418 } 1419 else 1420 { 1421 if (log) 1422 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", 1423 static_cast<void*>(exe_ctx.GetThreadPtr())); 1424 } 1425 } 1426 1427 if (log) 1428 log->Printf ("SBThread(%p)::GetNumFrames () => %u", 1429 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames); 1430 1431 return num_frames; 1432 } 1433 1434 SBFrame 1435 SBThread::GetFrameAtIndex (uint32_t idx) 1436 { 1437 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1438 1439 SBFrame sb_frame; 1440 StackFrameSP frame_sp; 1441 std::unique_lock<std::recursive_mutex> lock; 1442 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1443 1444 if (exe_ctx.HasThreadScope()) 1445 { 1446 Process::StopLocker stop_locker; 1447 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1448 { 1449 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx); 1450 sb_frame.SetFrameSP (frame_sp); 1451 } 1452 else 1453 { 1454 if (log) 1455 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", 1456 static_cast<void*>(exe_ctx.GetThreadPtr())); 1457 } 1458 } 1459 1460 if (log) 1461 { 1462 SBStream frame_desc_strm; 1463 sb_frame.GetDescription (frame_desc_strm); 1464 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s", 1465 static_cast<void*>(exe_ctx.GetThreadPtr()), idx, 1466 static_cast<void*>(frame_sp.get()), 1467 frame_desc_strm.GetData()); 1468 } 1469 1470 return sb_frame; 1471 } 1472 1473 lldb::SBFrame 1474 SBThread::GetSelectedFrame () 1475 { 1476 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1477 1478 SBFrame sb_frame; 1479 StackFrameSP frame_sp; 1480 std::unique_lock<std::recursive_mutex> lock; 1481 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1482 1483 if (exe_ctx.HasThreadScope()) 1484 { 1485 Process::StopLocker stop_locker; 1486 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1487 { 1488 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame (); 1489 sb_frame.SetFrameSP (frame_sp); 1490 } 1491 else 1492 { 1493 if (log) 1494 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", 1495 static_cast<void*>(exe_ctx.GetThreadPtr())); 1496 } 1497 } 1498 1499 if (log) 1500 { 1501 SBStream frame_desc_strm; 1502 sb_frame.GetDescription (frame_desc_strm); 1503 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s", 1504 static_cast<void*>(exe_ctx.GetThreadPtr()), 1505 static_cast<void*>(frame_sp.get()), 1506 frame_desc_strm.GetData()); 1507 } 1508 1509 return sb_frame; 1510 } 1511 1512 lldb::SBFrame 1513 SBThread::SetSelectedFrame (uint32_t idx) 1514 { 1515 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1516 1517 SBFrame sb_frame; 1518 StackFrameSP frame_sp; 1519 std::unique_lock<std::recursive_mutex> lock; 1520 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1521 1522 if (exe_ctx.HasThreadScope()) 1523 { 1524 Process::StopLocker stop_locker; 1525 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1526 { 1527 Thread *thread = exe_ctx.GetThreadPtr(); 1528 frame_sp = thread->GetStackFrameAtIndex (idx); 1529 if (frame_sp) 1530 { 1531 thread->SetSelectedFrame (frame_sp.get()); 1532 sb_frame.SetFrameSP (frame_sp); 1533 } 1534 } 1535 else 1536 { 1537 if (log) 1538 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", 1539 static_cast<void*>(exe_ctx.GetThreadPtr())); 1540 } 1541 } 1542 1543 if (log) 1544 { 1545 SBStream frame_desc_strm; 1546 sb_frame.GetDescription (frame_desc_strm); 1547 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s", 1548 static_cast<void*>(exe_ctx.GetThreadPtr()), idx, 1549 static_cast<void*>(frame_sp.get()), 1550 frame_desc_strm.GetData()); 1551 } 1552 return sb_frame; 1553 } 1554 1555 bool 1556 SBThread::EventIsThreadEvent (const SBEvent &event) 1557 { 1558 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL; 1559 } 1560 1561 SBFrame 1562 SBThread::GetStackFrameFromEvent (const SBEvent &event) 1563 { 1564 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get()); 1565 1566 } 1567 1568 SBThread 1569 SBThread::GetThreadFromEvent (const SBEvent &event) 1570 { 1571 return Thread::ThreadEventData::GetThreadFromEvent (event.get()); 1572 } 1573 1574 bool 1575 SBThread::operator == (const SBThread &rhs) const 1576 { 1577 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get(); 1578 } 1579 1580 bool 1581 SBThread::operator != (const SBThread &rhs) const 1582 { 1583 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get(); 1584 } 1585 1586 bool 1587 SBThread::GetStatus (SBStream &status) const 1588 { 1589 Stream &strm = status.ref(); 1590 1591 std::unique_lock<std::recursive_mutex> lock; 1592 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1593 1594 if (exe_ctx.HasThreadScope()) 1595 { 1596 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1); 1597 } 1598 else 1599 strm.PutCString ("No status"); 1600 1601 return true; 1602 } 1603 1604 bool 1605 SBThread::GetDescription (SBStream &description) const 1606 { 1607 Stream &strm = description.ref(); 1608 1609 std::unique_lock<std::recursive_mutex> lock; 1610 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1611 1612 if (exe_ctx.HasThreadScope()) 1613 { 1614 exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm, LLDB_INVALID_THREAD_ID); 1615 //strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID()); 1616 } 1617 else 1618 strm.PutCString ("No value"); 1619 1620 return true; 1621 } 1622 1623 SBThread 1624 SBThread::GetExtendedBacktraceThread (const char *type) 1625 { 1626 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 1627 std::unique_lock<std::recursive_mutex> lock; 1628 ExecutionContext exe_ctx(m_opaque_sp.get(), lock); 1629 SBThread sb_origin_thread; 1630 1631 if (exe_ctx.HasThreadScope()) 1632 { 1633 Process::StopLocker stop_locker; 1634 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock())) 1635 { 1636 ThreadSP real_thread(exe_ctx.GetThreadSP()); 1637 if (real_thread) 1638 { 1639 ConstString type_const (type); 1640 Process *process = exe_ctx.GetProcessPtr(); 1641 if (process) 1642 { 1643 SystemRuntime *runtime = process->GetSystemRuntime(); 1644 if (runtime) 1645 { 1646 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const)); 1647 if (new_thread_sp) 1648 { 1649 // Save this in the Process' ExtendedThreadList so a strong pointer retains the 1650 // object. 1651 process->GetExtendedThreadList().AddThread (new_thread_sp); 1652 sb_origin_thread.SetThread (new_thread_sp); 1653 if (log) 1654 { 1655 const char *queue_name = new_thread_sp->GetQueueName(); 1656 if (queue_name == NULL) 1657 queue_name = ""; 1658 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread " 1659 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'", 1660 static_cast<void*>(exe_ctx.GetThreadPtr()), 1661 static_cast<void*>(new_thread_sp.get()), 1662 new_thread_sp->GetQueueID(), 1663 queue_name); 1664 } 1665 } 1666 } 1667 } 1668 } 1669 } 1670 else 1671 { 1672 if (log) 1673 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running", 1674 static_cast<void*>(exe_ctx.GetThreadPtr())); 1675 } 1676 } 1677 1678 if (log && sb_origin_thread.IsValid() == false) 1679 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread", 1680 static_cast<void*>(exe_ctx.GetThreadPtr())); 1681 return sb_origin_thread; 1682 } 1683 1684 uint32_t 1685 SBThread::GetExtendedBacktraceOriginatingIndexID () 1686 { 1687 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1688 if (thread_sp) 1689 return thread_sp->GetExtendedBacktraceOriginatingIndexID(); 1690 return LLDB_INVALID_INDEX32; 1691 } 1692 1693 bool 1694 SBThread::SafeToCallFunctions () 1695 { 1696 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1697 if (thread_sp) 1698 return thread_sp->SafeToCallFunctions(); 1699 return true; 1700 } 1701 1702 lldb_private::Thread * 1703 SBThread::operator->() 1704 { 1705 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1706 if (thread_sp) 1707 return thread_sp.get(); 1708 else 1709 return NULL; 1710 } 1711 1712 lldb_private::Thread * 1713 SBThread::get() 1714 { 1715 ThreadSP thread_sp(m_opaque_sp->GetThreadSP()); 1716 if (thread_sp) 1717 return thread_sp.get(); 1718 else 1719 return NULL; 1720 } 1721 1722