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