Thread.h revision 5a47e8bcc7277dc3683f2af2aeb9717184e8360c
1//===-- Thread.h ------------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_Thread_h_ 11#define liblldb_Thread_h_ 12 13#include "lldb/lldb-private.h" 14#include "lldb/Host/Mutex.h" 15#include "lldb/Core/UserID.h" 16#include "lldb/Target/ExecutionContextScope.h" 17#include "lldb/Target/StackFrameList.h" 18 19#define LLDB_THREAD_MAX_STOP_EXC_DATA 8 20 21// I forward declare these here so I don't have to #include ThreadPlan, so in turn I 22// can use Thread.h in ThreadPlan.h. 23 24namespace lldb_private { 25 26class Thread : 27 public UserID, 28 public ExecutionContextScope 29{ 30friend class ThreadPlan; 31public: 32 //---------------------------------------------------------------------- 33 // StopInfo 34 // 35 // Describes the reason the thread it was created with stopped. 36 //---------------------------------------------------------------------- 37 class StopInfo 38 { 39 public: 40 StopInfo(Thread *thread = NULL); 41 42 ~StopInfo(); 43 44 // Clear clears the stop reason, but it does not clear the thread this 45 // StopInfo is tied to. 46 void 47 Clear(); 48 49 lldb::StopReason 50 GetStopReason() const; 51 52 void 53 SetThread (Thread *thread); 54 55 Thread * 56 GetThread (); 57 58 void 59 SetStopReasonWithBreakpointSiteID (lldb::user_id_t break_id); 60 61 void 62 SetStopReasonWithWatchpointID (lldb::user_id_t watch_id); 63 64 void 65 SetStopReasonWithSignal (int signo); 66 67 void 68 SetStopReasonToTrace (); 69 70 void 71 SetStopReasonWithException (uint32_t exc_type, size_t exc_data_count); 72 73 void 74 SetStopReasonWithPlan (lldb::ThreadPlanSP &plan); 75 76 void 77 SetStopReasonToNone (); 78 79 const char * 80 GetStopDescription() const; 81 82 void 83 SetStopDescription(const char *desc); 84 85 lldb::user_id_t 86 GetBreakpointSiteID() const; 87 88 lldb::user_id_t 89 GetWatchpointID() const; 90 91 int 92 GetSignal() const; 93 94 lldb::user_id_t 95 GetPlanID () const; 96 97 uint32_t 98 GetExceptionType() const; 99 100 size_t 101 GetExceptionDataCount() const; 102 103 lldb::addr_t 104 GetExceptionDataAtIndex (uint32_t idx) const; 105 106 bool 107 SetExceptionDataAtIndex (uint32_t idx, lldb::addr_t data); 108 109 void 110 Dump (Stream *s) const; 111 112 protected: 113 lldb::StopReason m_reason; 114 //-------------------------------------------------------------- 115 // For eStopReasonPlan the completed plan is stored in this shared pointer. 116 //-------------------------------------------------------------- 117 lldb::ThreadPlanSP m_completed_plan_sp; 118 Thread *m_thread; 119 char m_description[256]; 120 union 121 { 122 //-------------------------------------------------------------- 123 // eStopReasonBreakpoint 124 //-------------------------------------------------------------- 125 struct 126 { 127 lldb::user_id_t bp_site_id; 128 } breakpoint; 129 //-------------------------------------------------------------- 130 // eStopReasonWatchpoint 131 //-------------------------------------------------------------- 132 struct 133 { 134 lldb::user_id_t watch_id; 135 } watchpoint; 136 //-------------------------------------------------------------- 137 // eStopReasonSignal 138 //-------------------------------------------------------------- 139 struct 140 { 141 int signo; 142 } signal; 143 //-------------------------------------------------------------- 144 // eStopReasonException 145 //-------------------------------------------------------------- 146 struct 147 { 148 uint32_t type; 149 size_t data_count; 150 lldb::addr_t data[LLDB_THREAD_MAX_STOP_EXC_DATA]; 151 } exception; 152 } m_details; 153 }; 154 155 class RegisterCheckpoint 156 { 157 public: 158 159 RegisterCheckpoint() : 160 m_stack_id (), 161 m_data_sp () 162 { 163 } 164 165 RegisterCheckpoint (const StackID &stack_id) : 166 m_stack_id (stack_id), 167 m_data_sp () 168 { 169 } 170 171 ~RegisterCheckpoint() 172 { 173 } 174 175 const StackID & 176 GetStackID() 177 { 178 return m_stack_id; 179 } 180 181 void 182 SetStackID (const StackID &stack_id) 183 { 184 m_stack_id = stack_id; 185 } 186 187 lldb::DataBufferSP & 188 GetData() 189 { 190 return m_data_sp; 191 } 192 193 const lldb::DataBufferSP & 194 GetData() const 195 { 196 return m_data_sp; 197 } 198 199 protected: 200 StackID m_stack_id; 201 lldb::DataBufferSP m_data_sp; 202 }; 203 204 Thread (Process &process, lldb::tid_t tid); 205 virtual ~Thread(); 206 207 Process & 208 GetProcess() { return m_process; } 209 210 const Process & 211 GetProcess() const { return m_process; } 212 213 int 214 GetResumeSignal () const; 215 216 void 217 SetResumeSignal (int signal); 218 219 lldb::StateType 220 GetState() const; 221 222 lldb::ThreadSP 223 GetSP (); 224 225 void 226 SetState (lldb::StateType state); 227 228 lldb::StateType 229 GetResumeState () const; 230 231 void 232 SetResumeState (lldb::StateType state); 233 234 // This function is called on all the threads before "WillResume" in case 235 // a thread needs to change its state before the ThreadList polls all the 236 // threads to figure out which ones actually will get to run and how. 237 void 238 SetupForResume (); 239 240 // Override this to do platform specific tasks before resume, but always 241 // call the Thread::WillResume at the end of your work. 242 243 virtual bool 244 WillResume (lldb::StateType resume_state); 245 246 // This clears generic thread state after a resume. If you subclass this, 247 // be sure to call it. 248 virtual void 249 DidResume (); 250 251 virtual void 252 RefreshStateAfterStop() = 0; 253 254 void 255 WillStop (); 256 257 bool 258 ShouldStop (Event *event_ptr); 259 260 lldb::Vote 261 ShouldReportStop (Event *event_ptr); 262 263 lldb::Vote 264 ShouldReportRun (Event *event_ptr); 265 266 // Return whether this thread matches the specification in ThreadSpec. This is a virtual 267 // method because at some point we may extend the thread spec with a platform specific 268 // dictionary of attributes, which then only the platform specific Thread implementation 269 // would know how to match. For now, this just calls through to the ThreadSpec's 270 // ThreadPassesBasicTests method. 271 virtual bool 272 MatchesSpec (const ThreadSpec *spec); 273 274 bool 275 GetStopInfo (StopInfo *stop_info); 276 277 bool 278 ThreadStoppedForAReason (); 279 280 virtual const char * 281 GetInfo () = 0; 282 283 virtual const char * 284 GetName () 285 { 286 return NULL; 287 } 288 289 virtual const char * 290 GetQueueName () 291 { 292 return NULL; 293 } 294 295 virtual uint32_t 296 GetStackFrameCount() = 0; 297 298 virtual lldb::StackFrameSP 299 GetStackFrameAtIndex (uint32_t idx) = 0; 300 301 lldb::StackFrameSP 302 GetCurrentFrame (); 303 304 uint32_t 305 SetCurrentFrame (lldb_private::StackFrame *frame); 306 307 void 308 SetCurrentFrameByIndex (uint32_t frame_idx); 309 310 virtual RegisterContext * 311 GetRegisterContext () = 0; 312 313 virtual bool 314 SaveFrameZeroState (RegisterCheckpoint &checkpoint) = 0; 315 316 virtual bool 317 RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) = 0; 318 319 virtual RegisterContext * 320 CreateRegisterContextForFrame (StackFrame *frame) = 0; 321 322 virtual void 323 ClearStackFrames () 324 { 325 m_frames.Clear(); 326 } 327 328 void 329 DumpInfo (Stream &strm, 330 bool show_stop_reason, 331 bool show_name, 332 bool show_queue, 333 uint32_t frame_idx);// = UINT32_MAX); 334 335 //------------------------------------------------------------------ 336 // Thread Plan Providers: 337 // This section provides the basic thread plans that the Process control 338 // machinery uses to run the target. ThreadPlan.h provides more details on 339 // how this mechanism works. 340 // The thread provides accessors to a set of plans that perform basic operations. 341 // The idea is that particular Platform plugins can override these methods to 342 // provide the implementation of these basic operations appropriate to their 343 // environment. 344 //------------------------------------------------------------------ 345 346 //------------------------------------------------------------------ 347 /// Queues the base plan for a thread. 348 /// The version returned by Process does some things that are useful, 349 /// like handle breakpoints and signals, so if you return a plugin specific 350 /// one you probably want to call through to the Process one for anything 351 /// your plugin doesn't explicitly handle. 352 /// 353 /// @param[in] abort_other_plans 354 /// \b true if we discard the currently queued plans and replace them with this one. 355 /// Otherwise this plan will go on the end of the plan stack. 356 /// 357 /// @return 358 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 359 //------------------------------------------------------------------ 360 virtual ThreadPlan * 361 QueueFundamentalPlan (bool abort_other_plans); 362 363 //------------------------------------------------------------------ 364 /// Queues the plan used to step over a breakpoint at the current PC of \a thread. 365 /// The default version returned by Process handles trap based breakpoints, and 366 /// will disable the breakpoint, single step over it, then re-enable it. 367 /// 368 /// @param[in] abort_other_plans 369 /// \b true if we discard the currently queued plans and replace them with this one. 370 /// Otherwise this plan will go on the end of the plan stack. 371 /// 372 /// @return 373 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 374 //------------------------------------------------------------------ 375 virtual ThreadPlan * 376 QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans); 377 378 //------------------------------------------------------------------ 379 /// Queues the plan used to step one instruction from the current PC of \a thread. 380 /// 381 /// @param[in] step_over 382 /// \b true if we step over calls to functions, false if we step in. 383 /// 384 /// @param[in] abort_other_plans 385 /// \b true if we discard the currently queued plans and replace them with this one. 386 /// Otherwise this plan will go on the end of the plan stack. 387 /// 388 /// @param[in] stop_other_threads 389 /// \b true if we will stop other threads while we single step this one. 390 /// 391 /// @return 392 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 393 //------------------------------------------------------------------ 394 virtual ThreadPlan * 395 QueueThreadPlanForStepSingleInstruction (bool step_over, 396 bool abort_other_plans, 397 bool stop_other_threads); 398 399 //------------------------------------------------------------------ 400 /// Queues the plan used to step through an address range, stepping into or over 401 /// function calls depending on the value of StepType. 402 /// 403 /// @param[in] abort_other_plans 404 /// \b true if we discard the currently queued plans and replace them with this one. 405 /// Otherwise this plan will go on the end of the plan stack. 406 /// 407 /// @param[in] type 408 /// Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan. 409 /// 410 /// @param[in] range 411 /// The address range to step through. 412 /// 413 /// @param[in] addr_context 414 /// When dealing with stepping through inlined functions the current PC is not enough information to know 415 /// what "step" means. For instance a series of nested inline functions might start at the same address. 416 // The \a addr_context provides the current symbol context the step 417 /// is supposed to be out of. 418 // FIXME: Currently unused. 419 /// 420 /// @param[in] stop_other_threads 421 /// \b true if we will stop other threads while we single step this one. 422 /// 423 /// @return 424 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 425 //------------------------------------------------------------------ 426 virtual ThreadPlan * 427 QueueThreadPlanForStepRange (bool abort_other_plans, 428 lldb::StepType type, 429 const AddressRange &range, 430 const SymbolContext &addr_context, 431 lldb::RunMode stop_other_threads, 432 bool avoid_code_without_debug_info); 433 434 //------------------------------------------------------------------ 435 /// Queue the plan used to step out of the function at the current PC of 436 /// \a thread. 437 /// 438 /// @param[in] abort_other_plans 439 /// \b true if we discard the currently queued plans and replace them with this one. 440 /// Otherwise this plan will go on the end of the plan stack. 441 /// 442 /// @param[in] addr_context 443 /// When dealing with stepping through inlined functions the current PC is not enough information to know 444 /// what "step" means. For instance a series of nested inline functions might start at the same address. 445 // The \a addr_context provides the current symbol context the step 446 /// is supposed to be out of. 447 // FIXME: Currently unused. 448 /// 449 /// @param[in] first_insn 450 /// \b true if this is the first instruction of a function. 451 /// 452 /// @param[in] stop_other_threads 453 /// \b true if we will stop other threads while we single step this one. 454 /// 455 /// @param[in] stop_vote 456 /// @param[in] run_vote 457 /// See standard meanings for the stop & run votes in ThreadPlan.h. 458 /// 459 /// @return 460 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 461 //------------------------------------------------------------------ 462 virtual ThreadPlan * 463 QueueThreadPlanForStepOut (bool abort_other_plans, 464 SymbolContext *addr_context, 465 bool first_insn, 466 bool stop_other_threads, 467 lldb::Vote stop_vote = lldb::eVoteYes, 468 lldb::Vote run_vote = lldb::eVoteNoOpinion); 469 470 //------------------------------------------------------------------ 471 /// Gets the plan used to step through the code that steps from a function 472 /// call site at the current PC into the actual function call. 473 /// 474 /// @param[in] abort_other_plans 475 /// \b true if we discard the currently queued plans and replace them with this one. 476 /// Otherwise this plan will go on the end of the plan stack. 477 /// 478 /// @param[in] stop_other_threads 479 /// \b true if we will stop other threads while we single step this one. 480 /// 481 /// @return 482 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 483 //------------------------------------------------------------------ 484 virtual ThreadPlan * 485 QueueThreadPlanForStepThrough (bool abort_other_plans, 486 bool stop_other_threads); 487 488 //------------------------------------------------------------------ 489 /// Gets the plan used to continue from the current PC. 490 /// This is a simple plan, mostly useful as a backstop when you are continuing 491 /// for some particular purpose. 492 /// 493 /// @param[in] abort_other_plans 494 /// \b true if we discard the currently queued plans and replace them with this one. 495 /// Otherwise this plan will go on the end of the plan stack. 496 /// 497 /// @param[in] target_addr 498 /// The address to which we're running. 499 /// 500 /// @param[in] stop_other_threads 501 /// \b true if we will stop other threads while we single step this one. 502 /// 503 /// @return 504 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 505 //------------------------------------------------------------------ 506 virtual ThreadPlan * 507 QueueThreadPlanForRunToAddress (bool abort_other_plans, 508 Address &target_addr, 509 bool stop_other_threads); 510 511 virtual ThreadPlan * 512 QueueThreadPlanForStepUntil (bool abort_other_plans, 513 lldb::addr_t *address_list, 514 size_t num_addresses, 515 bool stop_others); 516 517 virtual ThreadPlan * 518 QueueThreadPlanForCallFunction (bool abort_other_plans, 519 Address& function, 520 lldb::addr_t arg, 521 bool stop_other_threads, 522 bool discard_on_error = false); 523 524 virtual ThreadPlan * 525 QueueThreadPlanForCallFunction (bool abort_other_plans, 526 Address& function, 527 ValueList &args, 528 bool stop_other_threads, 529 bool discard_on_error = false); 530 531 //------------------------------------------------------------------ 532 // Thread Plan accessors: 533 //------------------------------------------------------------------ 534 535 //------------------------------------------------------------------ 536 /// Gets the plan which will execute next on the plan stack. 537 /// 538 /// @return 539 /// A pointer to the next executed plan. 540 //------------------------------------------------------------------ 541 ThreadPlan * 542 GetCurrentPlan (); 543 544 //------------------------------------------------------------------ 545 /// Gets the inner-most plan that was popped off the plan stack in the 546 /// most recent stop. Useful for printing the stop reason accurately. 547 /// 548 /// @return 549 /// A pointer to the last completed plan. 550 //------------------------------------------------------------------ 551 lldb::ThreadPlanSP 552 GetCompletedPlan (); 553 554 //------------------------------------------------------------------ 555 /// Checks whether the given plan is in the completed plans for this 556 /// stop. 557 /// 558 /// @param[in] plan 559 /// Pointer to the plan you're checking. 560 /// 561 /// @return 562 /// Returns true if the input plan is in the completed plan stack, 563 /// false otherwise. 564 //------------------------------------------------------------------ 565 bool 566 IsThreadPlanDone (ThreadPlan *plan); 567 568 //------------------------------------------------------------------ 569 /// Checks whether the given plan is in the discarded plans for this 570 /// stop. 571 /// 572 /// @param[in] plan 573 /// Pointer to the plan you're checking. 574 /// 575 /// @return 576 /// Returns true if the input plan is in the discarded plan stack, 577 /// false otherwise. 578 //------------------------------------------------------------------ 579 bool 580 WasThreadPlanDiscarded (ThreadPlan *plan); 581 582 //------------------------------------------------------------------ 583 /// Queues a generic thread plan. 584 /// 585 /// @param[in] plan_sp 586 /// The plan to queue. 587 /// 588 /// @param[in] abort_other_plans 589 /// \b true if we discard the currently queued plans and replace them with this one. 590 /// Otherwise this plan will go on the end of the plan stack. 591 /// 592 /// @return 593 /// A pointer to the last completed plan. 594 //------------------------------------------------------------------ 595 void 596 QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans); 597 598 599 //------------------------------------------------------------------ 600 /// Discards the plans queued on the plan stack of the current thread. This is 601 /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call. 602 // But if \a force is true, all thread plans are discarded. 603 //------------------------------------------------------------------ 604 void 605 DiscardThreadPlans (bool force); 606 607 //------------------------------------------------------------------ 608 /// Prints the current plan stack. 609 /// 610 /// @param[in] s 611 /// The stream to which to dump the plan stack info. 612 /// 613 //------------------------------------------------------------------ 614 void 615 DumpThreadPlans (Stream *s) const; 616 617 // Get the thread index ID. The index ID that is guaranteed to not be 618 // re-used by a process. They start at 1 and increase with each new thread. 619 // This allows easy command line access by a unique ID that is easier to 620 // type than the actual system thread ID. 621 uint32_t 622 GetIndexID () const; 623 624 //------------------------------------------------------------------ 625 // lldb::ExecutionContextScope pure virtual functions 626 //------------------------------------------------------------------ 627 virtual Target * 628 CalculateTarget (); 629 630 virtual Process * 631 CalculateProcess (); 632 633 virtual Thread * 634 CalculateThread (); 635 636 virtual StackFrame * 637 CalculateStackFrame (); 638 639 virtual void 640 Calculate (ExecutionContext &exe_ctx); 641 642protected: 643 void 644 PushPlan (lldb::ThreadPlanSP &plan_sp); 645 646 void 647 PopPlan (); 648 649 void 650 DiscardPlan (); 651 652 ThreadPlan *GetPreviousPlan (ThreadPlan *plan); 653 654 virtual bool 655 GetRawStopReason (StopInfo *stop_info) = 0; 656 657 typedef std::vector<lldb::ThreadPlanSP> plan_stack; 658 659 //------------------------------------------------------------------ 660 // Classes that inherit from Process can see and modify these 661 //------------------------------------------------------------------ 662 Process & m_process; ///< The process that owns this thread. 663 const uint32_t m_index_id; ///< A unique 1 based index assigned to each thread for easy UI/command line access. 664 lldb::RegisterContextSP m_reg_context_sp; ///< The register context for this thread's current register state. 665 lldb::StateType m_state; ///< The state of our process. 666 plan_stack m_plan_stack; ///< The stack of plans this thread is executing. 667 plan_stack m_immediate_plan_stack; ///< The plans that need to get executed before any other work gets done. 668 plan_stack m_completed_plan_stack; ///< Plans that have been completed by this stop. They get deleted when the thread resumes. 669 plan_stack m_discarded_plan_stack; ///< Plans that have been discarded by this stop. They get deleted when the thread resumes. 670 mutable Mutex m_state_mutex; ///< Multithreaded protection for m_state. 671 StackFrameList m_frames; ///< The stack frames that get lazily populated after a thread stops. 672 uint32_t m_current_frame_idx;///< The current frame for this thread 673 int m_resume_signal; ///< The signal that should be used when continuing this thread. 674 lldb::StateType m_resume_state; ///< The state that indicates what this thread should do when the process is resumed. 675private: 676 //------------------------------------------------------------------ 677 // For Thread only 678 //------------------------------------------------------------------ 679 DISALLOW_COPY_AND_ASSIGN (Thread); 680 681}; 682 683} // namespace lldb_private 684 685#endif // liblldb_Thread_h_ 686