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