Thread.h revision 3c7b5b9f83cae58ca366db2bba37dc09485f7dcc
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 virtual bool 267 MatchesSpec (const ThreadSpec *spec); 268 269 bool 270 GetStopInfo (StopInfo *stop_info); 271 272 bool 273 ThreadStoppedForAReason (); 274 275 virtual const char * 276 GetInfo () = 0; 277 278 virtual const char * 279 GetName () 280 { 281 return NULL; 282 } 283 284 virtual const char * 285 GetQueueName () 286 { 287 return NULL; 288 } 289 290 virtual uint32_t 291 GetStackFrameCount() = 0; 292 293 virtual lldb::StackFrameSP 294 GetStackFrameAtIndex (uint32_t idx) = 0; 295 296 lldb::StackFrameSP 297 GetCurrentFrame (); 298 299 uint32_t 300 SetCurrentFrame (lldb_private::StackFrame *frame); 301 302 void 303 SetCurrentFrameByIndex (uint32_t frame_idx); 304 305 virtual RegisterContext * 306 GetRegisterContext () = 0; 307 308 virtual bool 309 SaveFrameZeroState (RegisterCheckpoint &checkpoint) = 0; 310 311 virtual bool 312 RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) = 0; 313 314 virtual RegisterContext * 315 CreateRegisterContextForFrame (StackFrame *frame) = 0; 316 317 virtual void 318 ClearStackFrames () 319 { 320 m_frames.Clear(); 321 } 322 323 void 324 DumpInfo (Stream &strm, 325 bool show_stop_reason, 326 bool show_name, 327 bool show_queue, 328 uint32_t frame_idx);// = UINT32_MAX); 329 330 //------------------------------------------------------------------ 331 // Thread Plan Providers: 332 // This section provides the basic thread plans that the Process control 333 // machinery uses to run the target. ThreadPlan.h provides more details on 334 // how this mechanism works. 335 // The thread provides accessors to a set of plans that perform basic operations. 336 // The idea is that particular Platform plugins can override these methods to 337 // provide the implementation of these basic operations appropriate to their 338 // environment. 339 //------------------------------------------------------------------ 340 341 //------------------------------------------------------------------ 342 /// Queues the base plan for a thread. 343 /// The version returned by Process does some things that are useful, 344 /// like handle breakpoints and signals, so if you return a plugin specific 345 /// one you probably want to call through to the Process one for anything 346 /// your plugin doesn't explicitly handle. 347 /// 348 /// @param[in] abort_other_plans 349 /// \b true if we discard the currently queued plans and replace them with this one. 350 /// Otherwise this plan will go on the end of the plan stack. 351 /// 352 /// @return 353 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 354 //------------------------------------------------------------------ 355 virtual ThreadPlan * 356 QueueFundamentalPlan (bool abort_other_plans); 357 358 //------------------------------------------------------------------ 359 /// Queues the plan used to step over a breakpoint at the current PC of \a thread. 360 /// The default version returned by Process handles trap based breakpoints, and 361 /// will disable the breakpoint, single step over it, then re-enable it. 362 /// 363 /// @param[in] abort_other_plans 364 /// \b true if we discard the currently queued plans and replace them with this one. 365 /// Otherwise this plan will go on the end of the plan stack. 366 /// 367 /// @return 368 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 369 //------------------------------------------------------------------ 370 virtual ThreadPlan * 371 QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans); 372 373 //------------------------------------------------------------------ 374 /// Queues the plan used to step one instruction from the current PC of \a thread. 375 /// 376 /// @param[in] step_over 377 /// \b true if we step over calls to functions, false if we step in. 378 /// 379 /// @param[in] abort_other_plans 380 /// \b true if we discard the currently queued plans and replace them with this one. 381 /// Otherwise this plan will go on the end of the plan stack. 382 /// 383 /// @param[in] stop_other_threads 384 /// \b true if we will stop other threads while we single step this one. 385 /// 386 /// @return 387 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 388 //------------------------------------------------------------------ 389 virtual ThreadPlan * 390 QueueThreadPlanForStepSingleInstruction (bool step_over, 391 bool abort_other_plans, 392 bool stop_other_threads); 393 394 //------------------------------------------------------------------ 395 /// Queues the plan used to step through an address range, stepping into or over 396 /// function calls depending on the value of StepType. 397 /// 398 /// @param[in] abort_other_plans 399 /// \b true if we discard the currently queued plans and replace them with this one. 400 /// Otherwise this plan will go on the end of the plan stack. 401 /// 402 /// @param[in] type 403 /// Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan. 404 /// 405 /// @param[in] range 406 /// The address range to step through. 407 /// 408 /// @param[in] addr_context 409 /// When dealing with stepping through inlined functions the current PC is not enough information to know 410 /// what "step" means. For instance a series of nested inline functions might start at the same address. 411 // The \a addr_context provides the current symbol context the step 412 /// is supposed to be out of. 413 // FIXME: Currently unused. 414 /// 415 /// @param[in] stop_other_threads 416 /// \b true if we will stop other threads while we single step this one. 417 /// 418 /// @return 419 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 420 //------------------------------------------------------------------ 421 virtual ThreadPlan * 422 QueueThreadPlanForStepRange (bool abort_other_plans, 423 lldb::StepType type, 424 const AddressRange &range, 425 const SymbolContext &addr_context, 426 lldb::RunMode stop_other_threads, 427 bool avoid_code_without_debug_info); 428 429 //------------------------------------------------------------------ 430 /// Queue the plan used to step out of the function at the current PC of 431 /// \a thread. 432 /// 433 /// @param[in] abort_other_plans 434 /// \b true if we discard the currently queued plans and replace them with this one. 435 /// Otherwise this plan will go on the end of the plan stack. 436 /// 437 /// @param[in] addr_context 438 /// When dealing with stepping through inlined functions the current PC is not enough information to know 439 /// what "step" means. For instance a series of nested inline functions might start at the same address. 440 // The \a addr_context provides the current symbol context the step 441 /// is supposed to be out of. 442 // FIXME: Currently unused. 443 /// 444 /// @param[in] first_insn 445 /// \b true if this is the first instruction of a function. 446 /// 447 /// @param[in] stop_other_threads 448 /// \b true if we will stop other threads while we single step this one. 449 /// 450 /// @param[in] stop_vote 451 /// @param[in] run_vote 452 /// See standard meanings for the stop & run votes in ThreadPlan.h. 453 /// 454 /// @return 455 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 456 //------------------------------------------------------------------ 457 virtual ThreadPlan * 458 QueueThreadPlanForStepOut (bool abort_other_plans, 459 SymbolContext *addr_context, 460 bool first_insn, 461 bool stop_other_threads, 462 lldb::Vote stop_vote = lldb::eVoteYes, 463 lldb::Vote run_vote = lldb::eVoteNoOpinion); 464 465 //------------------------------------------------------------------ 466 /// Gets the plan used to step through the code that steps from a function 467 /// call site at the current PC into the actual function call. 468 /// 469 /// @param[in] abort_other_plans 470 /// \b true if we discard the currently queued plans and replace them with this one. 471 /// Otherwise this plan will go on the end of the plan stack. 472 /// 473 /// @param[in] stop_other_threads 474 /// \b true if we will stop other threads while we single step this one. 475 /// 476 /// @return 477 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 478 //------------------------------------------------------------------ 479 virtual ThreadPlan * 480 QueueThreadPlanForStepThrough (bool abort_other_plans, 481 bool stop_other_threads); 482 483 //------------------------------------------------------------------ 484 /// Gets the plan used to continue from the current PC. 485 /// This is a simple plan, mostly useful as a backstop when you are continuing 486 /// for some particular purpose. 487 /// 488 /// @param[in] abort_other_plans 489 /// \b true if we discard the currently queued plans and replace them with this one. 490 /// Otherwise this plan will go on the end of the plan stack. 491 /// 492 /// @param[in] stop_other_threads 493 /// \b true if we will stop other threads while we single step this one. 494 /// 495 /// @param[in] stop_vote 496 /// @param[in] run_vote 497 /// See standard meanings for the stop & run votes in ThreadPlan.h. 498 /// 499 /// @return 500 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 501 //------------------------------------------------------------------ 502 virtual ThreadPlan * 503 QueueThreadPlanForContinue (bool abort_other_plans, 504 bool stop_other_threads, 505 lldb::Vote stop_vote, 506 lldb::Vote run_vote = lldb::eVoteNoOpinion, 507 bool immediate = false); 508 //------------------------------------------------------------------ 509 /// Gets the plan used to continue from the current PC. 510 /// This is a simple plan, mostly useful as a backstop when you are continuing 511 /// for some particular purpose. 512 /// 513 /// @param[in] abort_other_plans 514 /// \b true if we discard the currently queued plans and replace them with this one. 515 /// Otherwise this plan will go on the end of the plan stack. 516 /// 517 /// @param[in] target_addr 518 /// The address to which we're running. 519 /// 520 /// @param[in] stop_other_threads 521 /// \b true if we will stop other threads while we single step this one. 522 /// 523 /// @return 524 /// A pointer to the newly queued thread plan, or NULL if the plan could not be queued. 525 //------------------------------------------------------------------ 526 virtual ThreadPlan * 527 QueueThreadPlanForRunToAddress (bool abort_other_plans, 528 Address &target_addr, 529 bool stop_other_threads); 530 531 virtual ThreadPlan * 532 QueueThreadPlanForStepUntil (bool abort_other_plans, 533 lldb::addr_t *address_list, 534 size_t num_addresses, 535 bool stop_others); 536 537 virtual ThreadPlan * 538 QueueThreadPlanForCallFunction (bool abort_other_plans, 539 Address& function, 540 lldb::addr_t arg, 541 bool stop_other_threads, 542 bool discard_on_error = false); 543 544 virtual ThreadPlan * 545 QueueThreadPlanForCallFunction (bool abort_other_plans, 546 Address& function, 547 ValueList &args, 548 bool stop_other_threads, 549 bool discard_on_error = false); 550 551 //------------------------------------------------------------------ 552 // Thread Plan accessors: 553 //------------------------------------------------------------------ 554 555 //------------------------------------------------------------------ 556 /// Gets the plan which will execute next on the plan stack. 557 /// 558 /// @return 559 /// A pointer to the next executed plan. 560 //------------------------------------------------------------------ 561 ThreadPlan * 562 GetCurrentPlan (); 563 564 //------------------------------------------------------------------ 565 /// Gets the inner-most plan that was popped off the plan stack in the 566 /// most recent stop. Useful for printing the stop reason accurately. 567 /// 568 /// @return 569 /// A pointer to the last completed plan. 570 //------------------------------------------------------------------ 571 lldb::ThreadPlanSP 572 GetCompletedPlan (); 573 574 //------------------------------------------------------------------ 575 /// Checks whether the given plan is in the completed plans for this 576 /// stop. 577 /// 578 /// @param[in] plan 579 /// Pointer to the plan you're checking. 580 /// 581 /// @return 582 /// Returns true if the input plan is in the completed plan stack, 583 /// false otherwise. 584 //------------------------------------------------------------------ 585 bool 586 IsThreadPlanDone (ThreadPlan *plan); 587 588 //------------------------------------------------------------------ 589 /// Checks whether the given plan is in the discarded plans for this 590 /// stop. 591 /// 592 /// @param[in] plan 593 /// Pointer to the plan you're checking. 594 /// 595 /// @return 596 /// Returns true if the input plan is in the discarded plan stack, 597 /// false otherwise. 598 //------------------------------------------------------------------ 599 bool 600 WasThreadPlanDiscarded (ThreadPlan *plan); 601 602 //------------------------------------------------------------------ 603 /// Queues a generic thread plan. 604 /// 605 /// @param[in] plan_sp 606 /// The plan to queue. 607 /// 608 /// @param[in] abort_other_plans 609 /// \b true if we discard the currently queued plans and replace them with this one. 610 /// Otherwise this plan will go on the end of the plan stack. 611 /// 612 /// @return 613 /// A pointer to the last completed plan. 614 //------------------------------------------------------------------ 615 void 616 QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans); 617 618 619 //------------------------------------------------------------------ 620 /// Discards the plans queued on the plan stack of the current thread. This is 621 /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call. 622 // But if \a force is true, all thread plans are discarded. 623 //------------------------------------------------------------------ 624 void 625 DiscardThreadPlans (bool force); 626 627 //------------------------------------------------------------------ 628 /// Prints the current plan stack. 629 /// 630 /// @param[in] s 631 /// The stream to which to dump the plan stack info. 632 /// 633 //------------------------------------------------------------------ 634 void 635 DumpThreadPlans (Stream *s) const; 636 637 // Get the thread index ID. The index ID that is guaranteed to not be 638 // re-used by a process. They start at 1 and increase with each new thread. 639 // This allows easy command line access by a unique ID that is easier to 640 // type than the actual system thread ID. 641 uint32_t 642 GetIndexID () const; 643 644 //------------------------------------------------------------------ 645 // lldb::ExecutionContextScope pure virtual functions 646 //------------------------------------------------------------------ 647 virtual Target * 648 CalculateTarget (); 649 650 virtual Process * 651 CalculateProcess (); 652 653 virtual Thread * 654 CalculateThread (); 655 656 virtual StackFrame * 657 CalculateStackFrame (); 658 659 virtual void 660 Calculate (ExecutionContext &exe_ctx); 661 662protected: 663 void 664 PushPlan (lldb::ThreadPlanSP &plan_sp); 665 666 void 667 PopPlan (); 668 669 void 670 DiscardPlan (); 671 672 ThreadPlan *GetPreviousPlan (ThreadPlan *plan); 673 674 virtual bool 675 GetRawStopReason (StopInfo *stop_info) = 0; 676 677 typedef std::vector<lldb::ThreadPlanSP> plan_stack; 678 679 //------------------------------------------------------------------ 680 // Classes that inherit from Process can see and modify these 681 //------------------------------------------------------------------ 682 Process & m_process; ///< The process that owns this thread. 683 const uint32_t m_index_id; ///< A unique 1 based index assigned to each thread for easy UI/command line access. 684 lldb::RegisterContextSP m_reg_context_sp; ///< The register context for this thread's current register state. 685 lldb::StateType m_state; ///< The state of our process. 686 plan_stack m_plan_stack; ///< The stack of plans this thread is executing. 687 plan_stack m_immediate_plan_stack; ///< The plans that need to get executed before any other work gets done. 688 plan_stack m_completed_plan_stack; ///< Plans that have been completed by this stop. They get deleted when the thread resumes. 689 plan_stack m_discarded_plan_stack; ///< Plans that have been discarded by this stop. They get deleted when the thread resumes. 690 mutable Mutex m_state_mutex; ///< Multithreaded protection for m_state. 691 StackFrameList m_frames; ///< The stack frames that get lazily populated after a thread stops. 692 uint32_t m_current_frame_idx;///< The current frame for this thread 693 int m_resume_signal; ///< The signal that should be used when continuing this thread. 694 lldb::StateType m_resume_state; ///< The state that indicates what this thread should do when the process is resumed. 695private: 696 //------------------------------------------------------------------ 697 // For Thread only 698 //------------------------------------------------------------------ 699 DISALLOW_COPY_AND_ASSIGN (Thread); 700 701}; 702 703} // namespace lldb_private 704 705#endif // liblldb_Thread_h_ 706