1 //===-- Thread.h ------------------------------------------------*- 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 #ifndef LLDB_TARGET_THREAD_H 10 #define LLDB_TARGET_THREAD_H 11 12 #include <memory> 13 #include <mutex> 14 #include <optional> 15 #include <string> 16 #include <vector> 17 18 #include "lldb/Core/UserSettingsController.h" 19 #include "lldb/Target/ExecutionContextScope.h" 20 #include "lldb/Target/RegisterCheckpoint.h" 21 #include "lldb/Target/StackFrameList.h" 22 #include "lldb/Utility/Broadcaster.h" 23 #include "lldb/Utility/CompletionRequest.h" 24 #include "lldb/Utility/Event.h" 25 #include "lldb/Utility/StructuredData.h" 26 #include "lldb/Utility/UnimplementedError.h" 27 #include "lldb/Utility/UserID.h" 28 #include "lldb/lldb-private.h" 29 #include "llvm/Support/MemoryBuffer.h" 30 31 #define LLDB_THREAD_MAX_STOP_EXC_DATA 8 32 33 namespace lldb_private { 34 35 class ThreadPlanStack; 36 37 class ThreadProperties : public Properties { 38 public: 39 ThreadProperties(bool is_global); 40 41 ~ThreadProperties() override; 42 43 /// The regular expression returned determines symbols that this 44 /// thread won't stop in during "step-in" operations. 45 /// 46 /// \return 47 /// A pointer to a regular expression to compare against symbols, 48 /// or nullptr if all symbols are allowed. 49 /// 50 const RegularExpression *GetSymbolsToAvoidRegexp(); 51 52 FileSpecList GetLibrariesToAvoid() const; 53 54 bool GetTraceEnabledState() const; 55 56 bool GetStepInAvoidsNoDebug() const; 57 58 bool GetStepOutAvoidsNoDebug() const; 59 60 uint64_t GetMaxBacktraceDepth() const; 61 }; 62 63 class Thread : public std::enable_shared_from_this<Thread>, 64 public ThreadProperties, 65 public UserID, 66 public ExecutionContextScope, 67 public Broadcaster { 68 public: 69 /// Broadcaster event bits definitions. 70 enum { 71 eBroadcastBitStackChanged = (1 << 0), 72 eBroadcastBitThreadSuspended = (1 << 1), 73 eBroadcastBitThreadResumed = (1 << 2), 74 eBroadcastBitSelectedFrameChanged = (1 << 3), 75 eBroadcastBitThreadSelected = (1 << 4) 76 }; 77 78 static llvm::StringRef GetStaticBroadcasterClass(); 79 GetBroadcasterClass()80 llvm::StringRef GetBroadcasterClass() const override { 81 return GetStaticBroadcasterClass(); 82 } 83 84 class ThreadEventData : public EventData { 85 public: 86 ThreadEventData(const lldb::ThreadSP thread_sp); 87 88 ThreadEventData(const lldb::ThreadSP thread_sp, const StackID &stack_id); 89 90 ThreadEventData(); 91 92 ~ThreadEventData() override; 93 94 static llvm::StringRef GetFlavorString(); 95 GetFlavor()96 llvm::StringRef GetFlavor() const override { 97 return ThreadEventData::GetFlavorString(); 98 } 99 100 void Dump(Stream *s) const override; 101 102 static const ThreadEventData *GetEventDataFromEvent(const Event *event_ptr); 103 104 static lldb::ThreadSP GetThreadFromEvent(const Event *event_ptr); 105 106 static StackID GetStackIDFromEvent(const Event *event_ptr); 107 108 static lldb::StackFrameSP GetStackFrameFromEvent(const Event *event_ptr); 109 GetThread()110 lldb::ThreadSP GetThread() const { return m_thread_sp; } 111 GetStackID()112 StackID GetStackID() const { return m_stack_id; } 113 114 private: 115 lldb::ThreadSP m_thread_sp; 116 StackID m_stack_id; 117 118 ThreadEventData(const ThreadEventData &) = delete; 119 const ThreadEventData &operator=(const ThreadEventData &) = delete; 120 }; 121 122 struct ThreadStateCheckpoint { 123 uint32_t orig_stop_id; // Dunno if I need this yet but it is an interesting 124 // bit of data. 125 lldb::StopInfoSP stop_info_sp; // You have to restore the stop info or you 126 // might continue with the wrong signals. 127 size_t m_completed_plan_checkpoint; 128 lldb::RegisterCheckpointSP 129 register_backup_sp; // You need to restore the registers, of course... 130 uint32_t current_inlined_depth; 131 lldb::addr_t current_inlined_pc; 132 }; 133 134 /// Constructor 135 /// 136 /// \param [in] use_invalid_index_id 137 /// Optional parameter, defaults to false. The only subclass that 138 /// is likely to set use_invalid_index_id == true is the HistoryThread 139 /// class. In that case, the Thread we are constructing represents 140 /// a thread from earlier in the program execution. We may have the 141 /// tid of the original thread that they represent but we don't want 142 /// to reuse the IndexID of that thread, or create a new one. If a 143 /// client wants to know the original thread's IndexID, they should use 144 /// Thread::GetExtendedBacktraceOriginatingIndexID(). 145 Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id = false); 146 147 ~Thread() override; 148 149 static void SettingsInitialize(); 150 151 static void SettingsTerminate(); 152 153 static ThreadProperties &GetGlobalProperties(); 154 GetProcess()155 lldb::ProcessSP GetProcess() const { return m_process_wp.lock(); } 156 GetResumeSignal()157 int GetResumeSignal() const { return m_resume_signal; } 158 SetResumeSignal(int signal)159 void SetResumeSignal(int signal) { m_resume_signal = signal; } 160 161 lldb::StateType GetState() const; 162 163 void SetState(lldb::StateType state); 164 165 /// Sets the USER resume state for this thread. If you set a thread to 166 /// suspended with 167 /// this API, it won't take part in any of the arbitration for ShouldResume, 168 /// and will stay 169 /// suspended even when other threads do get to run. 170 /// 171 /// N.B. This is not the state that is used internally by thread plans to 172 /// implement 173 /// staying on one thread while stepping over a breakpoint, etc. The is the 174 /// TemporaryResume state, and if you are implementing some bit of strategy in 175 /// the stepping 176 /// machinery you should be using that state and not the user resume state. 177 /// 178 /// If you are just preparing all threads to run, you should not override the 179 /// threads that are 180 /// marked as suspended by the debugger. In that case, pass override_suspend 181 /// = false. If you want 182 /// to force the thread to run (e.g. the "thread continue" command, or are 183 /// resetting the state 184 /// (e.g. in SBThread::Resume()), then pass true to override_suspend. 185 void SetResumeState(lldb::StateType state, bool override_suspend = false) { 186 if (m_resume_state == lldb::eStateSuspended && !override_suspend) 187 return; 188 m_resume_state = state; 189 } 190 191 /// Gets the USER resume state for this thread. This is not the same as what 192 /// this thread is going to do for any particular step, however if this thread 193 /// returns eStateSuspended, then the process control logic will never allow 194 /// this 195 /// thread to run. 196 /// 197 /// \return 198 /// The User resume state for this thread. GetResumeState()199 lldb::StateType GetResumeState() const { return m_resume_state; } 200 201 // This function is called on all the threads before "ShouldResume" and 202 // "WillResume" in case a thread needs to change its state before the 203 // ThreadList polls all the threads to figure out which ones actually will 204 // get to run and how. 205 void SetupForResume(); 206 207 // Do not override this function, it is for thread plan logic only 208 bool ShouldResume(lldb::StateType resume_state); 209 210 // Override this to do platform specific tasks before resume. WillResume(lldb::StateType resume_state)211 virtual void WillResume(lldb::StateType resume_state) {} 212 213 // This clears generic thread state after a resume. If you subclass this, be 214 // sure to call it. 215 virtual void DidResume(); 216 217 // This notifies the thread when a private stop occurs. 218 virtual void DidStop(); 219 220 virtual void RefreshStateAfterStop() = 0; 221 222 std::string GetStopDescription(); 223 224 std::string GetStopDescriptionRaw(); 225 226 void WillStop(); 227 228 bool ShouldStop(Event *event_ptr); 229 230 Vote ShouldReportStop(Event *event_ptr); 231 232 Vote ShouldReportRun(Event *event_ptr); 233 234 void Flush(); 235 236 // Return whether this thread matches the specification in ThreadSpec. This 237 // is a virtual method because at some point we may extend the thread spec 238 // with a platform specific dictionary of attributes, which then only the 239 // platform specific Thread implementation would know how to match. For now, 240 // this just calls through to the ThreadSpec's ThreadPassesBasicTests method. 241 virtual bool MatchesSpec(const ThreadSpec *spec); 242 243 // Get the current public stop info, calculating it if necessary. 244 lldb::StopInfoSP GetStopInfo(); 245 246 lldb::StopReason GetStopReason(); 247 248 bool StopInfoIsUpToDate() const; 249 250 // This sets the stop reason to a "blank" stop reason, so you can call 251 // functions on the thread without having the called function run with 252 // whatever stop reason you stopped with. 253 void SetStopInfoToNothing(); 254 255 bool ThreadStoppedForAReason(); 256 257 static std::string RunModeAsString(lldb::RunMode mode); 258 259 static std::string StopReasonAsString(lldb::StopReason reason); 260 GetInfo()261 virtual const char *GetInfo() { return nullptr; } 262 263 /// Retrieve a dictionary of information about this thread 264 /// 265 /// On Mac OS X systems there may be voucher information. 266 /// The top level dictionary returned will have an "activity" key and the 267 /// value of the activity is a dictionary. Keys in that dictionary will 268 /// be "name" and "id", among others. 269 /// There may also be "trace_messages" (an array) with each entry in that 270 /// array 271 /// being a dictionary (keys include "message" with the text of the trace 272 /// message). GetExtendedInfo()273 StructuredData::ObjectSP GetExtendedInfo() { 274 if (!m_extended_info_fetched) { 275 m_extended_info = FetchThreadExtendedInfo(); 276 m_extended_info_fetched = true; 277 } 278 return m_extended_info; 279 } 280 GetName()281 virtual const char *GetName() { return nullptr; } 282 SetName(const char * name)283 virtual void SetName(const char *name) {} 284 285 /// Whether this thread can be associated with a libdispatch queue 286 /// 287 /// The Thread may know if it is associated with a libdispatch queue, 288 /// it may know definitively that it is NOT associated with a libdispatch 289 /// queue, or it may be unknown whether it is associated with a libdispatch 290 /// queue. 291 /// 292 /// \return 293 /// eLazyBoolNo if this thread is definitely not associated with a 294 /// libdispatch queue (e.g. on a non-Darwin system where GCD aka 295 /// libdispatch is not available). 296 /// 297 /// eLazyBoolYes this thread is associated with a libdispatch queue. 298 /// 299 /// eLazyBoolCalculate this thread may be associated with a libdispatch 300 /// queue but the thread doesn't know one way or the other. GetAssociatedWithLibdispatchQueue()301 virtual lldb_private::LazyBool GetAssociatedWithLibdispatchQueue() { 302 return eLazyBoolNo; 303 } 304 SetAssociatedWithLibdispatchQueue(lldb_private::LazyBool associated_with_libdispatch_queue)305 virtual void SetAssociatedWithLibdispatchQueue( 306 lldb_private::LazyBool associated_with_libdispatch_queue) {} 307 308 /// Retrieve the Queue ID for the queue currently using this Thread 309 /// 310 /// If this Thread is doing work on behalf of a libdispatch/GCD queue, 311 /// retrieve the QueueID. 312 /// 313 /// This is a unique identifier for the libdispatch/GCD queue in a 314 /// process. Often starting at 1 for the initial system-created 315 /// queues and incrementing, a QueueID will not be reused for a 316 /// different queue during the lifetime of a process. 317 /// 318 /// \return 319 /// A QueueID if the Thread subclass implements this, else 320 /// LLDB_INVALID_QUEUE_ID. GetQueueID()321 virtual lldb::queue_id_t GetQueueID() { return LLDB_INVALID_QUEUE_ID; } 322 SetQueueID(lldb::queue_id_t new_val)323 virtual void SetQueueID(lldb::queue_id_t new_val) {} 324 325 /// Retrieve the Queue name for the queue currently using this Thread 326 /// 327 /// If this Thread is doing work on behalf of a libdispatch/GCD queue, 328 /// retrieve the Queue name. 329 /// 330 /// \return 331 /// The Queue name, if the Thread subclass implements this, else 332 /// nullptr. GetQueueName()333 virtual const char *GetQueueName() { return nullptr; } 334 SetQueueName(const char * name)335 virtual void SetQueueName(const char *name) {} 336 337 /// Retrieve the Queue kind for the queue currently using this Thread 338 /// 339 /// If this Thread is doing work on behalf of a libdispatch/GCD queue, 340 /// retrieve the Queue kind - either eQueueKindSerial or 341 /// eQueueKindConcurrent, indicating that this queue processes work 342 /// items serially or concurrently. 343 /// 344 /// \return 345 /// The Queue kind, if the Thread subclass implements this, else 346 /// eQueueKindUnknown. GetQueueKind()347 virtual lldb::QueueKind GetQueueKind() { return lldb::eQueueKindUnknown; } 348 SetQueueKind(lldb::QueueKind kind)349 virtual void SetQueueKind(lldb::QueueKind kind) {} 350 351 /// Retrieve the Queue for this thread, if any. 352 /// 353 /// \return 354 /// A QueueSP for the queue that is currently associated with this 355 /// thread. 356 /// An empty shared pointer indicates that this thread is not 357 /// associated with a queue, or libdispatch queues are not 358 /// supported on this target. GetQueue()359 virtual lldb::QueueSP GetQueue() { return lldb::QueueSP(); } 360 361 /// Retrieve the address of the libdispatch_queue_t struct for queue 362 /// currently using this Thread 363 /// 364 /// If this Thread is doing work on behalf of a libdispatch/GCD queue, 365 /// retrieve the address of the libdispatch_queue_t structure describing 366 /// the queue. 367 /// 368 /// This address may be reused for different queues later in the Process 369 /// lifetime and should not be used to identify a queue uniquely. Use 370 /// the GetQueueID() call for that. 371 /// 372 /// \return 373 /// The Queue's libdispatch_queue_t address if the Thread subclass 374 /// implements this, else LLDB_INVALID_ADDRESS. GetQueueLibdispatchQueueAddress()375 virtual lldb::addr_t GetQueueLibdispatchQueueAddress() { 376 return LLDB_INVALID_ADDRESS; 377 } 378 SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t)379 virtual void SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t) {} 380 381 /// Whether this Thread already has all the Queue information cached or not 382 /// 383 /// A Thread may be associated with a libdispatch work Queue at a given 384 /// public stop event. If so, the thread can satisify requests like 385 /// GetQueueLibdispatchQueueAddress, GetQueueKind, GetQueueName, and 386 /// GetQueueID 387 /// either from information from the remote debug stub when it is initially 388 /// created, or it can query the SystemRuntime for that information. 389 /// 390 /// This method allows the SystemRuntime to discover if a thread has this 391 /// information already, instead of calling the thread to get the information 392 /// and having the thread call the SystemRuntime again. ThreadHasQueueInformation()393 virtual bool ThreadHasQueueInformation() const { return false; } 394 395 /// GetStackFrameCount can be expensive. Stacks can get very deep, and they 396 /// require memory reads for each frame. So only use GetStackFrameCount when 397 /// you need to know the depth of the stack. When iterating over frames, its 398 /// better to generate the frames one by one with GetFrameAtIndex, and when 399 /// that returns NULL, you are at the end of the stack. That way your loop 400 /// will only do the work it needs to, without forcing lldb to realize 401 /// StackFrames you weren't going to look at. GetStackFrameCount()402 virtual uint32_t GetStackFrameCount() { 403 return GetStackFrameList()->GetNumFrames(); 404 } 405 GetStackFrameAtIndex(uint32_t idx)406 virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx) { 407 return GetStackFrameList()->GetFrameAtIndex(idx); 408 } 409 410 virtual lldb::StackFrameSP 411 GetFrameWithConcreteFrameIndex(uint32_t unwind_idx); 412 DecrementCurrentInlinedDepth()413 bool DecrementCurrentInlinedDepth() { 414 return GetStackFrameList()->DecrementCurrentInlinedDepth(); 415 } 416 GetCurrentInlinedDepth()417 uint32_t GetCurrentInlinedDepth() { 418 return GetStackFrameList()->GetCurrentInlinedDepth(); 419 } 420 421 Status ReturnFromFrameWithIndex(uint32_t frame_idx, 422 lldb::ValueObjectSP return_value_sp, 423 bool broadcast = false); 424 425 Status ReturnFromFrame(lldb::StackFrameSP frame_sp, 426 lldb::ValueObjectSP return_value_sp, 427 bool broadcast = false); 428 429 Status JumpToLine(const FileSpec &file, uint32_t line, 430 bool can_leave_function, std::string *warnings = nullptr); 431 GetFrameWithStackID(const StackID & stack_id)432 virtual lldb::StackFrameSP GetFrameWithStackID(const StackID &stack_id) { 433 if (stack_id.IsValid()) 434 return GetStackFrameList()->GetFrameWithStackID(stack_id); 435 return lldb::StackFrameSP(); 436 } 437 438 // Only pass true to select_most_relevant if you are fulfilling an explicit 439 // user request for GetSelectedFrameIndex. The most relevant frame is only 440 // for showing to the user, and can do arbitrary work, so we don't want to 441 // call it internally. GetSelectedFrameIndex(SelectMostRelevant select_most_relevant)442 uint32_t GetSelectedFrameIndex(SelectMostRelevant select_most_relevant) { 443 return GetStackFrameList()->GetSelectedFrameIndex(select_most_relevant); 444 } 445 446 lldb::StackFrameSP 447 GetSelectedFrame(SelectMostRelevant select_most_relevant); 448 449 uint32_t SetSelectedFrame(lldb_private::StackFrame *frame, 450 bool broadcast = false); 451 452 bool SetSelectedFrameByIndex(uint32_t frame_idx, bool broadcast = false); 453 454 bool SetSelectedFrameByIndexNoisily(uint32_t frame_idx, 455 Stream &output_stream); 456 SetDefaultFileAndLineToSelectedFrame()457 void SetDefaultFileAndLineToSelectedFrame() { 458 GetStackFrameList()->SetDefaultFileAndLineToSelectedFrame(); 459 } 460 461 virtual lldb::RegisterContextSP GetRegisterContext() = 0; 462 463 virtual lldb::RegisterContextSP 464 CreateRegisterContextForFrame(StackFrame *frame) = 0; 465 466 virtual void ClearStackFrames(); 467 SetBackingThread(const lldb::ThreadSP & thread_sp)468 virtual bool SetBackingThread(const lldb::ThreadSP &thread_sp) { 469 return false; 470 } 471 GetBackingThread()472 virtual lldb::ThreadSP GetBackingThread() const { return lldb::ThreadSP(); } 473 ClearBackingThread()474 virtual void ClearBackingThread() { 475 // Subclasses can use this function if a thread is actually backed by 476 // another thread. This is currently used for the OperatingSystem plug-ins 477 // where they might have a thread that is in memory, yet its registers are 478 // available through the lldb_private::Thread subclass for the current 479 // lldb_private::Process class. Since each time the process stops the 480 // backing threads for memory threads can change, we need a way to clear 481 // the backing thread for all memory threads each time we stop. 482 } 483 484 /// Dump \a count instructions of the thread's \a Trace starting at the \a 485 /// start_position position in reverse order. 486 /// 487 /// The instructions are indexed in reverse order, which means that the \a 488 /// start_position 0 represents the last instruction of the trace 489 /// chronologically. 490 /// 491 /// \param[in] s 492 /// The stream object where the instructions are printed. 493 /// 494 /// \param[in] count 495 /// The number of instructions to print. 496 /// 497 /// \param[in] start_position 498 /// The position of the first instruction to print. 499 void DumpTraceInstructions(Stream &s, size_t count, 500 size_t start_position = 0) const; 501 502 /// Print a description of this thread using the provided thread format. 503 /// 504 /// \param[out] strm 505 /// The Stream to print the description to. 506 /// 507 /// \param[in] frame_idx 508 /// If not \b LLDB_INVALID_FRAME_ID, then use this frame index as context to 509 /// generate the description. 510 /// 511 /// \param[in] format 512 /// The input format. 513 /// 514 /// \return 515 /// \b true if and only if dumping with the given \p format worked. 516 bool DumpUsingFormat(Stream &strm, uint32_t frame_idx, 517 const FormatEntity::Entry *format); 518 519 // If stop_format is true, this will be the form used when we print stop 520 // info. If false, it will be the form we use for thread list and co. 521 void DumpUsingSettingsFormat(Stream &strm, uint32_t frame_idx, 522 bool stop_format); 523 524 bool GetDescription(Stream &s, lldb::DescriptionLevel level, 525 bool print_json_thread, bool print_json_stopinfo); 526 527 /// Default implementation for stepping into. 528 /// 529 /// This function is designed to be used by commands where the 530 /// process is publicly stopped. 531 /// 532 /// \param[in] source_step 533 /// If true and the frame has debug info, then do a source level 534 /// step in, else do a single instruction step in. 535 /// 536 /// \param[in] step_in_avoids_code_without_debug_info 537 /// If \a true, then avoid stepping into code that doesn't have 538 /// debug info, else step into any code regardless of whether it 539 /// has debug info. 540 /// 541 /// \param[in] step_out_avoids_code_without_debug_info 542 /// If \a true, then if you step out to code with no debug info, keep 543 /// stepping out till you get to code with debug info. 544 /// 545 /// \return 546 /// An error that describes anything that went wrong 547 virtual Status 548 StepIn(bool source_step, 549 LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate, 550 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 551 552 /// Default implementation for stepping over. 553 /// 554 /// This function is designed to be used by commands where the 555 /// process is publicly stopped. 556 /// 557 /// \param[in] source_step 558 /// If true and the frame has debug info, then do a source level 559 /// step over, else do a single instruction step over. 560 /// 561 /// \return 562 /// An error that describes anything that went wrong 563 virtual Status StepOver( 564 bool source_step, 565 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 566 567 /// Default implementation for stepping out. 568 /// 569 /// This function is designed to be used by commands where the 570 /// process is publicly stopped. 571 /// 572 /// \param[in] frame_idx 573 /// The frame index to step out of. 574 /// 575 /// \return 576 /// An error that describes anything that went wrong 577 virtual Status StepOut(uint32_t frame_idx = 0); 578 579 /// Retrieves the per-thread data area. 580 /// Most OSs maintain a per-thread pointer (e.g. the FS register on 581 /// x64), which we return the value of here. 582 /// 583 /// \return 584 /// LLDB_INVALID_ADDRESS if not supported, otherwise the thread 585 /// pointer value. 586 virtual lldb::addr_t GetThreadPointer(); 587 588 /// Retrieves the per-module TLS block for a thread. 589 /// 590 /// \param[in] module 591 /// The module to query TLS data for. 592 /// 593 /// \param[in] tls_file_addr 594 /// The thread local address in module 595 /// \return 596 /// If the thread has TLS data allocated for the 597 /// module, the address of the TLS block. Otherwise 598 /// LLDB_INVALID_ADDRESS is returned. 599 virtual lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module, 600 lldb::addr_t tls_file_addr); 601 602 /// Check whether this thread is safe to run functions 603 /// 604 /// The SystemRuntime may know of certain thread states (functions in 605 /// process of execution, for instance) which can make it unsafe for 606 /// functions to be called. 607 /// 608 /// \return 609 /// True if it is safe to call functions on this thread. 610 /// False if function calls should be avoided on this thread. 611 virtual bool SafeToCallFunctions(); 612 613 // Thread Plan Providers: 614 // This section provides the basic thread plans that the Process control 615 // machinery uses to run the target. ThreadPlan.h provides more details on 616 // how this mechanism works. The thread provides accessors to a set of plans 617 // that perform basic operations. The idea is that particular Platform 618 // plugins can override these methods to provide the implementation of these 619 // basic operations appropriate to their environment. 620 // 621 // NB: All the QueueThreadPlanXXX providers return Shared Pointers to 622 // Thread plans. This is useful so that you can modify the plans after 623 // creation in ways specific to that plan type. Also, it is often necessary 624 // for ThreadPlans that utilize other ThreadPlans to implement their task to 625 // keep a shared pointer to the sub-plan. But besides that, the shared 626 // pointers should only be held onto by entities who live no longer than the 627 // thread containing the ThreadPlan. 628 // FIXME: If this becomes a problem, we can make a version that just returns a 629 // pointer, 630 // which it is clearly unsafe to hold onto, and a shared pointer version, and 631 // only allow ThreadPlan and Co. to use the latter. That is made more 632 // annoying to do because there's no elegant way to friend a method to all 633 // sub-classes of a given class. 634 // 635 636 /// Queues the base plan for a thread. 637 /// The version returned by Process does some things that are useful, 638 /// like handle breakpoints and signals, so if you return a plugin specific 639 /// one you probably want to call through to the Process one for anything 640 /// your plugin doesn't explicitly handle. 641 /// 642 /// \param[in] abort_other_plans 643 /// \b true if we discard the currently queued plans and replace them with 644 /// this one. 645 /// Otherwise this plan will go on the end of the plan stack. 646 /// 647 /// \return 648 /// A shared pointer to the newly queued thread plan, or nullptr if the 649 /// plan could not be queued. 650 lldb::ThreadPlanSP QueueBasePlan(bool abort_other_plans); 651 652 /// Queues the plan used to step one instruction from the current PC of \a 653 /// thread. 654 /// 655 /// \param[in] step_over 656 /// \b true if we step over calls to functions, false if we step in. 657 /// 658 /// \param[in] abort_other_plans 659 /// \b true if we discard the currently queued plans and replace them with 660 /// this one. 661 /// Otherwise this plan will go on the end of the plan stack. 662 /// 663 /// \param[in] stop_other_threads 664 /// \b true if we will stop other threads while we single step this one. 665 /// 666 /// \param[out] status 667 /// A status with an error if queuing failed. 668 /// 669 /// \return 670 /// A shared pointer to the newly queued thread plan, or nullptr if the 671 /// plan could not be queued. 672 virtual lldb::ThreadPlanSP QueueThreadPlanForStepSingleInstruction( 673 bool step_over, bool abort_other_plans, bool stop_other_threads, 674 Status &status); 675 676 /// Queues the plan used to step through an address range, stepping over 677 /// function calls. 678 /// 679 /// \param[in] abort_other_plans 680 /// \b true if we discard the currently queued plans and replace them with 681 /// this one. 682 /// Otherwise this plan will go on the end of the plan stack. 683 /// 684 /// \param[in] type 685 /// Type of step to do, only eStepTypeInto and eStepTypeOver are supported 686 /// by this plan. 687 /// 688 /// \param[in] range 689 /// The address range to step through. 690 /// 691 /// \param[in] addr_context 692 /// When dealing with stepping through inlined functions the current PC is 693 /// not enough information to know 694 /// what "step" means. For instance a series of nested inline functions 695 /// might start at the same address. 696 // The \a addr_context provides the current symbol context the step 697 /// is supposed to be out of. 698 // FIXME: Currently unused. 699 /// 700 /// \param[in] stop_other_threads 701 /// \b true if we will stop other threads while we single step this one. 702 /// 703 /// \param[out] status 704 /// A status with an error if queuing failed. 705 /// 706 /// \param[in] step_out_avoids_code_without_debug_info 707 /// If eLazyBoolYes, if the step over steps out it will continue to step 708 /// out till it comes to a frame with debug info. 709 /// If eLazyBoolCalculate, we will consult the default set in the thread. 710 /// 711 /// \return 712 /// A shared pointer to the newly queued thread plan, or nullptr if the 713 /// plan could not be queued. 714 virtual lldb::ThreadPlanSP QueueThreadPlanForStepOverRange( 715 bool abort_other_plans, const AddressRange &range, 716 const SymbolContext &addr_context, lldb::RunMode stop_other_threads, 717 Status &status, 718 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 719 720 // Helper function that takes a LineEntry to step, insted of an AddressRange. 721 // This may combine multiple LineEntries of the same source line number to 722 // step over a longer address range in a single operation. 723 virtual lldb::ThreadPlanSP QueueThreadPlanForStepOverRange( 724 bool abort_other_plans, const LineEntry &line_entry, 725 const SymbolContext &addr_context, lldb::RunMode stop_other_threads, 726 Status &status, 727 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 728 729 /// Queues the plan used to step through an address range, stepping into 730 /// functions. 731 /// 732 /// \param[in] abort_other_plans 733 /// \b true if we discard the currently queued plans and replace them with 734 /// this one. 735 /// Otherwise this plan will go on the end of the plan stack. 736 /// 737 /// \param[in] type 738 /// Type of step to do, only eStepTypeInto and eStepTypeOver are supported 739 /// by this plan. 740 /// 741 /// \param[in] range 742 /// The address range to step through. 743 /// 744 /// \param[in] addr_context 745 /// When dealing with stepping through inlined functions the current PC is 746 /// not enough information to know 747 /// what "step" means. For instance a series of nested inline functions 748 /// might start at the same address. 749 // The \a addr_context provides the current symbol context the step 750 /// is supposed to be out of. 751 // FIXME: Currently unused. 752 /// 753 /// \param[in] step_in_target 754 /// Name if function we are trying to step into. We will step out if we 755 /// don't land in that function. 756 /// 757 /// \param[in] stop_other_threads 758 /// \b true if we will stop other threads while we single step this one. 759 /// 760 /// \param[out] status 761 /// A status with an error if queuing failed. 762 /// 763 /// \param[in] step_in_avoids_code_without_debug_info 764 /// If eLazyBoolYes we will step out if we step into code with no debug 765 /// info. 766 /// If eLazyBoolCalculate we will consult the default set in the thread. 767 /// 768 /// \param[in] step_out_avoids_code_without_debug_info 769 /// If eLazyBoolYes, if the step over steps out it will continue to step 770 /// out till it comes to a frame with debug info. 771 /// If eLazyBoolCalculate, it will consult the default set in the thread. 772 /// 773 /// \return 774 /// A shared pointer to the newly queued thread plan, or nullptr if the 775 /// plan could not be queued. 776 virtual lldb::ThreadPlanSP QueueThreadPlanForStepInRange( 777 bool abort_other_plans, const AddressRange &range, 778 const SymbolContext &addr_context, const char *step_in_target, 779 lldb::RunMode stop_other_threads, Status &status, 780 LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate, 781 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 782 783 // Helper function that takes a LineEntry to step, insted of an AddressRange. 784 // This may combine multiple LineEntries of the same source line number to 785 // step over a longer address range in a single operation. 786 virtual lldb::ThreadPlanSP QueueThreadPlanForStepInRange( 787 bool abort_other_plans, const LineEntry &line_entry, 788 const SymbolContext &addr_context, const char *step_in_target, 789 lldb::RunMode stop_other_threads, Status &status, 790 LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate, 791 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 792 793 /// Queue the plan used to step out of the function at the current PC of 794 /// \a thread. 795 /// 796 /// \param[in] abort_other_plans 797 /// \b true if we discard the currently queued plans and replace them with 798 /// this one. 799 /// Otherwise this plan will go on the end of the plan stack. 800 /// 801 /// \param[in] addr_context 802 /// When dealing with stepping through inlined functions the current PC is 803 /// not enough information to know 804 /// what "step" means. For instance a series of nested inline functions 805 /// might start at the same address. 806 // The \a addr_context provides the current symbol context the step 807 /// is supposed to be out of. 808 // FIXME: Currently unused. 809 /// 810 /// \param[in] first_insn 811 /// \b true if this is the first instruction of a function. 812 /// 813 /// \param[in] stop_other_threads 814 /// \b true if we will stop other threads while we single step this one. 815 /// 816 /// \param[in] report_stop_vote 817 /// See standard meanings for the stop & run votes in ThreadPlan.h. 818 /// 819 /// \param[in] report_run_vote 820 /// See standard meanings for the stop & run votes in ThreadPlan.h. 821 /// 822 /// \param[out] status 823 /// A status with an error if queuing failed. 824 /// 825 /// \param[in] step_out_avoids_code_without_debug_info 826 /// If eLazyBoolYes, if the step over steps out it will continue to step 827 /// out till it comes to a frame with debug info. 828 /// If eLazyBoolCalculate, it will consult the default set in the thread. 829 /// 830 /// \return 831 /// A shared pointer to the newly queued thread plan, or nullptr if the 832 /// plan could not be queued. 833 virtual lldb::ThreadPlanSP QueueThreadPlanForStepOut( 834 bool abort_other_plans, SymbolContext *addr_context, bool first_insn, 835 bool stop_other_threads, Vote report_stop_vote, Vote report_run_vote, 836 uint32_t frame_idx, Status &status, 837 LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate); 838 839 /// Queue the plan used to step out of the function at the current PC of 840 /// a thread. This version does not consult the should stop here callback, 841 /// and should only 842 /// be used by other thread plans when they need to retain control of the step 843 /// out. 844 /// 845 /// \param[in] abort_other_plans 846 /// \b true if we discard the currently queued plans and replace them with 847 /// this one. 848 /// Otherwise this plan will go on the end of the plan stack. 849 /// 850 /// \param[in] addr_context 851 /// When dealing with stepping through inlined functions the current PC is 852 /// not enough information to know 853 /// what "step" means. For instance a series of nested inline functions 854 /// might start at the same address. 855 // The \a addr_context provides the current symbol context the step 856 /// is supposed to be out of. 857 // FIXME: Currently unused. 858 /// 859 /// \param[in] first_insn 860 /// \b true if this is the first instruction of a function. 861 /// 862 /// \param[in] stop_other_threads 863 /// \b true if we will stop other threads while we single step this one. 864 /// 865 /// \param[in] report_stop_vote 866 /// See standard meanings for the stop & run votes in ThreadPlan.h. 867 /// 868 /// \param[in] report_run_vote 869 /// See standard meanings for the stop & run votes in ThreadPlan.h. 870 /// 871 /// \param[in] frame_idx 872 /// The frame index. 873 /// 874 /// \param[out] status 875 /// A status with an error if queuing failed. 876 /// 877 /// \param[in] continue_to_next_branch 878 /// Normally this will enqueue a plan that will put a breakpoint on the 879 /// return address and continue 880 /// to there. If continue_to_next_branch is true, this is an operation not 881 /// involving the user -- 882 /// e.g. stepping "next" in a source line and we instruction stepped into 883 /// another function -- 884 /// so instead of putting a breakpoint on the return address, advance the 885 /// breakpoint to the 886 /// end of the source line that is doing the call, or until the next flow 887 /// control instruction. 888 /// If the return value from the function call is to be retrieved / 889 /// displayed to the user, you must stop 890 /// on the return address. The return value may be stored in volatile 891 /// registers which are overwritten 892 /// before the next branch instruction. 893 /// 894 /// \return 895 /// A shared pointer to the newly queued thread plan, or nullptr if the 896 /// plan could not be queued. 897 virtual lldb::ThreadPlanSP QueueThreadPlanForStepOutNoShouldStop( 898 bool abort_other_plans, SymbolContext *addr_context, bool first_insn, 899 bool stop_other_threads, Vote report_stop_vote, Vote report_run_vote, 900 uint32_t frame_idx, Status &status, bool continue_to_next_branch = false); 901 902 /// Gets the plan used to step through the code that steps from a function 903 /// call site at the current PC into the actual function call. 904 /// 905 /// \param[in] return_stack_id 906 /// The stack id that we will return to (by setting backstop breakpoints on 907 /// the return 908 /// address to that frame) if we fail to step through. 909 /// 910 /// \param[in] abort_other_plans 911 /// \b true if we discard the currently queued plans and replace them with 912 /// this one. 913 /// Otherwise this plan will go on the end of the plan stack. 914 /// 915 /// \param[in] stop_other_threads 916 /// \b true if we will stop other threads while we single step this one. 917 /// 918 /// \param[out] status 919 /// A status with an error if queuing failed. 920 /// 921 /// \return 922 /// A shared pointer to the newly queued thread plan, or nullptr if the 923 /// plan could not be queued. 924 virtual lldb::ThreadPlanSP 925 QueueThreadPlanForStepThrough(StackID &return_stack_id, 926 bool abort_other_plans, bool stop_other_threads, 927 Status &status); 928 929 /// Gets the plan used to continue from the current PC. 930 /// This is a simple plan, mostly useful as a backstop when you are continuing 931 /// for some particular purpose. 932 /// 933 /// \param[in] abort_other_plans 934 /// \b true if we discard the currently queued plans and replace them with 935 /// this one. 936 /// Otherwise this plan will go on the end of the plan stack. 937 /// 938 /// \param[in] target_addr 939 /// The address to which we're running. 940 /// 941 /// \param[in] stop_other_threads 942 /// \b true if we will stop other threads while we single step this one. 943 /// 944 /// \param[out] status 945 /// A status with an error if queuing failed. 946 /// 947 /// \return 948 /// A shared pointer to the newly queued thread plan, or nullptr if the 949 /// plan could not be queued. 950 virtual lldb::ThreadPlanSP 951 QueueThreadPlanForRunToAddress(bool abort_other_plans, Address &target_addr, 952 bool stop_other_threads, Status &status); 953 954 virtual lldb::ThreadPlanSP QueueThreadPlanForStepUntil( 955 bool abort_other_plans, lldb::addr_t *address_list, size_t num_addresses, 956 bool stop_others, uint32_t frame_idx, Status &status); 957 958 virtual lldb::ThreadPlanSP 959 QueueThreadPlanForStepScripted(bool abort_other_plans, const char *class_name, 960 StructuredData::ObjectSP extra_args_sp, 961 bool stop_other_threads, Status &status); 962 963 // Thread Plan accessors: 964 965 /// Format the thread plan information for auto completion. 966 /// 967 /// \param[in] request 968 /// The reference to the completion handler. 969 void AutoCompleteThreadPlans(CompletionRequest &request) const; 970 971 /// Gets the plan which will execute next on the plan stack. 972 /// 973 /// \return 974 /// A pointer to the next executed plan. 975 ThreadPlan *GetCurrentPlan() const; 976 977 /// Unwinds the thread stack for the innermost expression plan currently 978 /// on the thread plan stack. 979 /// 980 /// \return 981 /// An error if the thread plan could not be unwound. 982 983 Status UnwindInnermostExpression(); 984 985 /// Gets the outer-most plan that was popped off the plan stack in the 986 /// most recent stop. Useful for printing the stop reason accurately. 987 /// 988 /// \return 989 /// A pointer to the last completed plan. 990 lldb::ThreadPlanSP GetCompletedPlan() const; 991 992 /// Gets the outer-most return value from the completed plans 993 /// 994 /// \return 995 /// A ValueObjectSP, either empty if there is no return value, 996 /// or containing the return value. 997 lldb::ValueObjectSP GetReturnValueObject() const; 998 999 /// Gets the outer-most expression variable from the completed plans 1000 /// 1001 /// \return 1002 /// A ExpressionVariableSP, either empty if there is no 1003 /// plan completed an expression during the current stop 1004 /// or the expression variable that was made for the completed expression. 1005 lldb::ExpressionVariableSP GetExpressionVariable() const; 1006 1007 /// Checks whether the given plan is in the completed plans for this 1008 /// stop. 1009 /// 1010 /// \param[in] plan 1011 /// Pointer to the plan you're checking. 1012 /// 1013 /// \return 1014 /// Returns true if the input plan is in the completed plan stack, 1015 /// false otherwise. 1016 bool IsThreadPlanDone(ThreadPlan *plan) const; 1017 1018 /// Checks whether the given plan is in the discarded plans for this 1019 /// stop. 1020 /// 1021 /// \param[in] plan 1022 /// Pointer to the plan you're checking. 1023 /// 1024 /// \return 1025 /// Returns true if the input plan is in the discarded plan stack, 1026 /// false otherwise. 1027 bool WasThreadPlanDiscarded(ThreadPlan *plan) const; 1028 1029 /// Check if we have completed plan to override breakpoint stop reason 1030 /// 1031 /// \return 1032 /// Returns true if completed plan stack is not empty 1033 /// false otherwise. 1034 bool CompletedPlanOverridesBreakpoint() const; 1035 1036 /// Queues a generic thread plan. 1037 /// 1038 /// \param[in] plan_sp 1039 /// The plan to queue. 1040 /// 1041 /// \param[in] abort_other_plans 1042 /// \b true if we discard the currently queued plans and replace them with 1043 /// this one. 1044 /// Otherwise this plan will go on the end of the plan stack. 1045 /// 1046 /// \return 1047 /// A pointer to the last completed plan. 1048 Status QueueThreadPlan(lldb::ThreadPlanSP &plan_sp, bool abort_other_plans); 1049 1050 /// Discards the plans queued on the plan stack of the current thread. This 1051 /// is 1052 /// arbitrated by the "Controlling" ThreadPlans, using the "OkayToDiscard" 1053 /// call. 1054 // But if \a force is true, all thread plans are discarded. 1055 void DiscardThreadPlans(bool force); 1056 1057 /// Discards the plans queued on the plan stack of the current thread up to 1058 /// and 1059 /// including up_to_plan_sp. 1060 // 1061 // \param[in] up_to_plan_sp 1062 // Discard all plans up to and including this one. 1063 void DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP &up_to_plan_sp); 1064 1065 void DiscardThreadPlansUpToPlan(ThreadPlan *up_to_plan_ptr); 1066 1067 /// Discards the plans queued on the plan stack of the current thread up to 1068 /// and 1069 /// including the plan in that matches \a thread_index counting only 1070 /// the non-Private plans. 1071 /// 1072 /// \param[in] thread_index 1073 /// Discard all plans up to and including this user plan given by this 1074 /// index. 1075 /// 1076 /// \return 1077 /// \b true if there was a thread plan with that user index, \b false 1078 /// otherwise. 1079 bool DiscardUserThreadPlansUpToIndex(uint32_t thread_index); 1080 1081 virtual bool CheckpointThreadState(ThreadStateCheckpoint &saved_state); 1082 1083 virtual bool 1084 RestoreRegisterStateFromCheckpoint(ThreadStateCheckpoint &saved_state); 1085 1086 void RestoreThreadStateFromCheckpoint(ThreadStateCheckpoint &saved_state); 1087 1088 // Get the thread index ID. The index ID that is guaranteed to not be re-used 1089 // by a process. They start at 1 and increase with each new thread. This 1090 // allows easy command line access by a unique ID that is easier to type than 1091 // the actual system thread ID. 1092 uint32_t GetIndexID() const; 1093 1094 // Get the originating thread's index ID. 1095 // In the case of an "extended" thread -- a thread which represents the stack 1096 // that enqueued/spawned work that is currently executing -- we need to 1097 // provide the IndexID of the thread that actually did this work. We don't 1098 // want to just masquerade as that thread's IndexID by using it in our own 1099 // IndexID because that way leads to madness - but the driver program which 1100 // is iterating over extended threads may ask for the OriginatingThreadID to 1101 // display that information to the user. 1102 // Normal threads will return the same thing as GetIndexID(); GetExtendedBacktraceOriginatingIndexID()1103 virtual uint32_t GetExtendedBacktraceOriginatingIndexID() { 1104 return GetIndexID(); 1105 } 1106 1107 // The API ID is often the same as the Thread::GetID(), but not in all cases. 1108 // Thread::GetID() is the user visible thread ID that clients would want to 1109 // see. The API thread ID is the thread ID that is used when sending data 1110 // to/from the debugging protocol. GetProtocolID()1111 virtual lldb::user_id_t GetProtocolID() const { return GetID(); } 1112 1113 // lldb::ExecutionContextScope pure virtual functions 1114 lldb::TargetSP CalculateTarget() override; 1115 1116 lldb::ProcessSP CalculateProcess() override; 1117 1118 lldb::ThreadSP CalculateThread() override; 1119 1120 lldb::StackFrameSP CalculateStackFrame() override; 1121 1122 void CalculateExecutionContext(ExecutionContext &exe_ctx) override; 1123 1124 lldb::StackFrameSP 1125 GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr); 1126 1127 size_t GetStatus(Stream &strm, uint32_t start_frame, uint32_t num_frames, 1128 uint32_t num_frames_with_source, bool stop_format, 1129 bool only_stacks = false); 1130 1131 size_t GetStackFrameStatus(Stream &strm, uint32_t first_frame, 1132 uint32_t num_frames, bool show_frame_info, 1133 uint32_t num_frames_with_source); 1134 1135 // We need a way to verify that even though we have a thread in a shared 1136 // pointer that the object itself is still valid. Currently this won't be the 1137 // case if DestroyThread() was called. DestroyThread is called when a thread 1138 // has been removed from the Process' thread list. IsValid()1139 bool IsValid() const { return !m_destroy_called; } 1140 1141 // Sets and returns a valid stop info based on the process stop ID and the 1142 // current thread plan. If the thread stop ID does not match the process' 1143 // stop ID, the private stop reason is not set and an invalid StopInfoSP may 1144 // be returned. 1145 // 1146 // NOTE: This function must be called before the current thread plan is 1147 // moved to the completed plan stack (in Thread::ShouldStop()). 1148 // 1149 // NOTE: If subclasses override this function, ensure they do not overwrite 1150 // the m_actual_stop_info if it is valid. The stop info may be a 1151 // "checkpointed and restored" stop info, so if it is still around it is 1152 // right even if you have not calculated this yourself, or if it disagrees 1153 // with what you might have calculated. 1154 virtual lldb::StopInfoSP GetPrivateStopInfo(bool calculate = true); 1155 1156 // Calculate the stop info that will be shown to lldb clients. For instance, 1157 // a "step out" is implemented by running to a breakpoint on the function 1158 // return PC, so the process plugin initially sets the stop info to a 1159 // StopInfoBreakpoint. But once we've run the ShouldStop machinery, we 1160 // discover that there's a completed ThreadPlanStepOut, and that's really 1161 // the StopInfo we want to show. That will happen naturally the next 1162 // time GetStopInfo is called, but if you want to force the replacement, 1163 // you can call this. 1164 1165 void CalculatePublicStopInfo(); 1166 1167 /// Ask the thread subclass to set its stop info. 1168 /// 1169 /// Thread subclasses should call Thread::SetStopInfo(...) with the reason the 1170 /// thread stopped. 1171 /// 1172 /// A thread that is sitting at a breakpoint site, but has not yet executed 1173 /// the breakpoint instruction, should have a breakpoint-hit StopInfo set. 1174 /// When execution is resumed, any thread sitting at a breakpoint site will 1175 /// instruction-step over the breakpoint instruction silently, and we will 1176 /// never record this breakpoint as being hit, updating the hit count, 1177 /// possibly executing breakpoint commands or conditions. 1178 /// 1179 /// \return 1180 /// True if Thread::SetStopInfo(...) was called, false otherwise. 1181 virtual bool CalculateStopInfo() = 0; 1182 1183 // Gets the temporary resume state for a thread. 1184 // 1185 // This value gets set in each thread by complex debugger logic in 1186 // Thread::ShouldResume() and an appropriate thread resume state will get set 1187 // in each thread every time the process is resumed prior to calling 1188 // Process::DoResume(). The lldb_private::Process subclass should adhere to 1189 // the thread resume state request which will be one of: 1190 // 1191 // eStateRunning - thread will resume when process is resumed 1192 // eStateStepping - thread should step 1 instruction and stop when process 1193 // is resumed 1194 // eStateSuspended - thread should not execute any instructions when 1195 // process is resumed GetTemporaryResumeState()1196 lldb::StateType GetTemporaryResumeState() const { 1197 return m_temporary_resume_state; 1198 } 1199 1200 void SetStopInfo(const lldb::StopInfoSP &stop_info_sp); 1201 1202 void ResetStopInfo(); 1203 1204 void SetShouldReportStop(Vote vote); 1205 SetShouldRunBeforePublicStop(bool newval)1206 void SetShouldRunBeforePublicStop(bool newval) { 1207 m_should_run_before_public_stop = newval; 1208 } 1209 ShouldRunBeforePublicStop()1210 bool ShouldRunBeforePublicStop() { 1211 return m_should_run_before_public_stop; 1212 } 1213 1214 /// Sets the extended backtrace token for this thread 1215 /// 1216 /// Some Thread subclasses may maintain a token to help with providing 1217 /// an extended backtrace. The SystemRuntime plugin will set/request this. 1218 /// 1219 /// \param [in] token The extended backtrace token. SetExtendedBacktraceToken(uint64_t token)1220 virtual void SetExtendedBacktraceToken(uint64_t token) {} 1221 1222 /// Gets the extended backtrace token for this thread 1223 /// 1224 /// Some Thread subclasses may maintain a token to help with providing 1225 /// an extended backtrace. The SystemRuntime plugin will set/request this. 1226 /// 1227 /// \return 1228 /// The token needed by the SystemRuntime to create an extended backtrace. 1229 /// LLDB_INVALID_ADDRESS is returned if no token is available. GetExtendedBacktraceToken()1230 virtual uint64_t GetExtendedBacktraceToken() { return LLDB_INVALID_ADDRESS; } 1231 1232 lldb::ValueObjectSP GetCurrentException(); 1233 1234 lldb::ThreadSP GetCurrentExceptionBacktrace(); 1235 1236 lldb::ValueObjectSP GetSiginfoValue(); 1237 1238 /// Request the pc value the thread had when previously stopped. 1239 /// 1240 /// When the thread performs execution, it copies the current RegisterContext 1241 /// GetPC() value. This method returns that value, if it is available. 1242 /// 1243 /// \return 1244 /// The PC value before execution was resumed. May not be available; 1245 /// an empty std::optional is returned in that case. 1246 std::optional<lldb::addr_t> GetPreviousFrameZeroPC(); 1247 1248 protected: 1249 friend class ThreadPlan; 1250 friend class ThreadList; 1251 friend class ThreadEventData; 1252 friend class StackFrameList; 1253 friend class StackFrame; 1254 friend class OperatingSystem; 1255 1256 // This is necessary to make sure thread assets get destroyed while the 1257 // thread is still in good shape to call virtual thread methods. This must 1258 // be called by classes that derive from Thread in their destructor. 1259 virtual void DestroyThread(); 1260 1261 ThreadPlanStack &GetPlans() const; 1262 1263 void PushPlan(lldb::ThreadPlanSP plan_sp); 1264 1265 void PopPlan(); 1266 1267 void DiscardPlan(); 1268 1269 ThreadPlan *GetPreviousPlan(ThreadPlan *plan) const; 1270 1271 virtual Unwind &GetUnwinder(); 1272 1273 // Check to see whether the thread is still at the last breakpoint hit that 1274 // stopped it. 1275 virtual bool IsStillAtLastBreakpointHit(); 1276 1277 // Some threads are threads that are made up by OperatingSystem plugins that 1278 // are threads that exist and are context switched out into memory. The 1279 // OperatingSystem plug-in need a ways to know if a thread is "real" or made 1280 // up. IsOperatingSystemPluginThread()1281 virtual bool IsOperatingSystemPluginThread() const { return false; } 1282 1283 // Subclasses that have a way to get an extended info dictionary for this 1284 // thread should fill FetchThreadExtendedInfo()1285 virtual lldb_private::StructuredData::ObjectSP FetchThreadExtendedInfo() { 1286 return StructuredData::ObjectSP(); 1287 } 1288 1289 lldb::StackFrameListSP GetStackFrameList(); 1290 SetTemporaryResumeState(lldb::StateType new_state)1291 void SetTemporaryResumeState(lldb::StateType new_state) { 1292 m_temporary_resume_state = new_state; 1293 } 1294 1295 void FrameSelectedCallback(lldb_private::StackFrame *frame); 1296 1297 virtual llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> GetSiginfo(size_t max_size)1298 GetSiginfo(size_t max_size) const { 1299 return llvm::make_error<UnimplementedError>(); 1300 } 1301 1302 // Classes that inherit from Process can see and modify these 1303 lldb::ProcessWP m_process_wp; ///< The process that owns this thread. 1304 lldb::StopInfoSP m_stop_info_sp; ///< The private stop reason for this thread 1305 uint32_t m_stop_info_stop_id; // This is the stop id for which the StopInfo is 1306 // valid. Can use this so you know that 1307 // the thread's m_stop_info_sp is current and you don't have to fetch it 1308 // again 1309 uint32_t m_stop_info_override_stop_id; // The stop ID containing the last time 1310 // the stop info was checked against 1311 // the stop info override 1312 bool m_should_run_before_public_stop; // If this thread has "stop others" 1313 // private work to do, then it will 1314 // set this. 1315 const uint32_t m_index_id; ///< A unique 1 based index assigned to each thread 1316 /// for easy UI/command line access. 1317 lldb::RegisterContextSP m_reg_context_sp; ///< The register context for this 1318 ///thread's current register state. 1319 lldb::StateType m_state; ///< The state of our process. 1320 mutable std::recursive_mutex 1321 m_state_mutex; ///< Multithreaded protection for m_state. 1322 mutable std::recursive_mutex 1323 m_frame_mutex; ///< Multithreaded protection for m_state. 1324 lldb::StackFrameListSP m_curr_frames_sp; ///< The stack frames that get lazily 1325 ///populated after a thread stops. 1326 lldb::StackFrameListSP m_prev_frames_sp; ///< The previous stack frames from 1327 ///the last time this thread stopped. 1328 std::optional<lldb::addr_t> 1329 m_prev_framezero_pc; ///< Frame 0's PC the last 1330 /// time this thread was stopped. 1331 int m_resume_signal; ///< The signal that should be used when continuing this 1332 ///thread. 1333 lldb::StateType m_resume_state; ///< This state is used to force a thread to 1334 ///be suspended from outside the ThreadPlan 1335 ///logic. 1336 lldb::StateType m_temporary_resume_state; ///< This state records what the 1337 ///thread was told to do by the 1338 ///thread plan logic for the current 1339 ///resume. 1340 /// It gets set in Thread::ShouldResume. 1341 std::unique_ptr<lldb_private::Unwind> m_unwinder_up; 1342 bool m_destroy_called; // This is used internally to make sure derived Thread 1343 // classes call DestroyThread. 1344 LazyBool m_override_should_notify; 1345 mutable std::unique_ptr<ThreadPlanStack> m_null_plan_stack_up; 1346 1347 private: 1348 bool m_extended_info_fetched; // Have we tried to retrieve the m_extended_info 1349 // for this thread? 1350 StructuredData::ObjectSP m_extended_info; // The extended info for this thread 1351 1352 void BroadcastSelectedFrameChange(StackID &new_frame_id); 1353 1354 Thread(const Thread &) = delete; 1355 const Thread &operator=(const Thread &) = delete; 1356 }; 1357 1358 } // namespace lldb_private 1359 1360 #endif // LLDB_TARGET_THREAD_H 1361