ThreadPlan.h revision b2cf58a817f6c0d15b2077316810c17d9c8ac173
1//===-- ThreadPlan.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_ThreadPlan_h_ 11#define liblldb_ThreadPlan_h_ 12 13// C Includes 14// C++ Includes 15#include <string> 16// Other libraries and framework includes 17// Project includes 18#include "lldb/lldb-private.h" 19#include "lldb/Core/UserID.h" 20#include "lldb/Host/Mutex.h" 21#include "lldb/Target/Process.h" 22#include "lldb/Target/Target.h" 23#include "lldb/Target/Thread.h" 24#include "lldb/Target/ThreadPlanTracer.h" 25#include "lldb/Target/StopInfo.h" 26 27namespace lldb_private { 28 29//------------------------------------------------------------------ 30// ThreadPlan: 31// This is the pure virtual base class for thread plans. 32// 33// The thread plans provide the "atoms" of behavior that 34// all the logical process control, either directly from commands or through 35// more complex composite plans will rely on. 36// 37// Plan Stack: 38// 39// The thread maintaining a thread plan stack, and you program the actions of a particular thread 40// by pushing plans onto the plan stack. 41// There is always a "Current" plan, which is the head of the plan stack, though in some cases 42// a plan may defer to plans higher in the stack for some piece of information. 43// 44// The plan stack is never empty, there is always a Base Plan which persists through the life 45// of the running process. 46// 47// 48// Creating Plans: 49// 50// The thread plan is generally created and added to the plan stack through the QueueThreadPlanFor... API 51// in lldb::Thread. Those API's will return the plan that performs the named operation in a manner 52// appropriate for the current process. The plans in lldb/source/Target are generic 53// implementations, but a Process plugin can override them. 54// 55// ValidatePlan is then called. If it returns false, the plan is unshipped. This is a little 56// convenience which keeps us from having to error out of the constructor. 57// 58// Then the plan is added to the plan stack. When the plan is added to the plan stack its DidPush 59// will get called. This is useful if a plan wants to push any additional plans as it is constructed, 60// since you need to make sure you're already on the stack before you push additional plans. 61// 62// Completed Plans: 63// 64// When the target process stops the plans are queried, among other things, for whether their job is done. 65// If it is they are moved from the plan stack to the Completed Plan stack in reverse order from their position 66// on the plan stack (since multiple plans may be done at a given stop.) This is used primarily so that 67// the lldb::Thread::StopInfo for the thread can be set properly. If one plan pushes another to achieve part of 68// its job, but it doesn't want that sub-plan to be the one that sets the StopInfo, then call SetPrivate on the 69// sub-plan when you create it, and the Thread will pass over that plan in reporting the reason for the stop. 70// 71// Discarded plans: 72// 73// Your plan may also get discarded, i.e. moved from the plan stack to the "discarded plan stack". This can 74// happen, for instance, if the plan is calling a function and the function call crashes and you want 75// to unwind the attempt to call. So don't assume that your plan will always successfully stop. Which leads to: 76// 77// Cleaning up after your plans: 78// 79// When the plan is moved from the plan stack its WillPop method is always called, no matter why. Once it is 80// moved off the plan stack it is done, and won't get a chance to run again. So you should 81// undo anything that affects target state in this method. But be sure to leave the plan able to correctly 82// fill the StopInfo, however. 83// N.B. Don't wait to do clean up target state till the destructor, since that will usually get called when 84// the target resumes, and you want to leave the target state correct for new plans in the time between when 85// your plan gets unshipped and the next resume. 86// 87// Over the lifetime of the plan, various methods of the ThreadPlan are then called in response to changes of state in 88// the process we are debugging as follows: 89// 90// Resuming: 91// 92// When the target process is about to be restarted, the plan's WillResume method is called, 93// giving the plan a chance to prepare for the run. If WillResume returns false, then the 94// process is not restarted. Be sure to set an appropriate error value in the Process if 95// you have to do this. 96// Next the "StopOthers" method of all the threads are polled, and if one thread's Current plan 97// returns "true" then only that thread gets to run. If more than one returns "true" the threads that want to run solo 98// get run one by one round robin fashion. Otherwise all are let to run. 99// 100// Note, the way StopOthers is implemented, the base class implementation just asks the previous plan. So if your plan 101// has no opinion about whether it should run stopping others or not, just don't implement StopOthers, and the parent 102// will be asked. 103// 104// Finally, for each thread that is running, it run state is set to the return of RunState from the 105// thread's Current plan. 106// 107// Responding to a stop: 108// 109// When the target process stops, the plan is called in the following stages: 110// 111// First the thread asks the Current Plan if it can handle this stop by calling PlanExplainsStop. 112// If the Current plan answers "true" then it is asked if the stop should percolate all the way to the 113// user by calling the ShouldStop method. If the current plan doesn't explain the stop, then we query down 114// the plan stack for a plan that does explain the stop. The plan that does explain the stop then needs to 115// figure out what to do about the plans below it in the stack. If the stop is recoverable, then the plan that 116// understands it can just do what it needs to set up to restart, and then continue. 117// Otherwise, the plan that understood the stop should call DiscardPlanStack to clean up the stack below it. 118// In the normal case, this will just collapse the plan stack up to the point of the plan that understood 119// the stop reason. However, if a plan wishes to stay on the stack after an event it didn't directly handle 120// it can designate itself a "Master" plan by responding true to IsMasterPlan, and then if it wants not to be 121// discarded, it can return true to OkayToDiscard, and it and all its dependent plans will be preserved when 122// we resume execution. 123// 124// Actually Stopping: 125// 126// If a plan says responds "true" to ShouldStop, then it is asked if it's job is complete by calling 127// MischiefManaged. If that returns true, the thread is popped from the plan stack and added to the 128// Completed Plan Stack. Then the next plan in the stack is asked if it ShouldStop, and it returns "true", 129// it is asked if it is done, and if yes popped, and so on till we reach a plan that is not done. 130// 131// Since you often know in the ShouldStop method whether your plan is complete, as a convenience you can call 132// SetPlanComplete and the ThreadPlan implementation of MischiefManaged will return "true", without your having 133// to redo the calculation when your sub-classes MischiefManaged is called. If you call SetPlanComplete, you can 134// later use IsPlanComplete to determine whether the plan is complete. This is only a convenience for sub-classes, 135// the logic in lldb::Thread will only call MischiefManaged. 136// 137// One slightly tricky point is you have to be careful using SetPlanComplete in PlanExplainsStop because you 138// are not guaranteed that PlanExplainsStop for a plan will get called before ShouldStop gets called. If your sub-plan 139// explained the stop and then popped itself, only your ShouldStop will get called. 140// 141// If ShouldStop for any thread returns "true", then the WillStop method of the Current plan of 142// all threads will be called, the stop event is placed on the Process's public broadcaster, and 143// control returns to the upper layers of the debugger. 144// 145// Automatically Resuming: 146// 147// If ShouldStop for all threads returns "false", then the target process will resume. This then cycles back to 148// Resuming above. 149// 150// Reporting eStateStopped events when the target is restarted: 151// 152// If a plan decides to auto-continue the target by returning "false" from ShouldStop, then it will be asked 153// whether the Stopped event should still be reported. For instance, if you hit a breakpoint that is a User set 154// breakpoint, but the breakpoint callback said to continue the target process, you might still want to inform 155// the upper layers of lldb that the stop had happened. 156// The way this works is every thread gets to vote on whether to report the stop. If all votes are eVoteNoOpinion, 157// then the thread list will decide what to do (at present it will pretty much always suppress these stopped events.) 158// If there is an eVoteYes, then the event will be reported regardless of the other votes. If there is an eVoteNo 159// and no eVoteYes's, then the event won't be reported. 160// 161// One other little detail here, sometimes a plan will push another plan onto the plan stack to do some part of 162// the first plan's job, and it would be convenient to tell that plan how it should respond to ShouldReportStop. 163// You can do that by setting the stop_vote in the child plan when you create it. 164// 165// Suppressing the initial eStateRunning event: 166// 167// The private process running thread will take care of ensuring that only one "eStateRunning" event will be 168// delivered to the public Process broadcaster per public eStateStopped event. However there are some cases 169// where the public state of this process is eStateStopped, but a thread plan needs to restart the target, but 170// doesn't want the running event to be publically broadcast. The obvious example of this is running functions 171// by hand as part of expression evaluation. To suppress the running event return eVoteNo from ShouldReportStop, 172// to force a running event to be reported return eVoteYes, in general though you should return eVoteNoOpinion 173// which will allow the ThreadList to figure out the right thing to do. 174// The run_vote argument to the constructor works like stop_vote, and is a way for a plan to instruct a sub-plan 175// on how to respond to ShouldReportStop. 176// 177//------------------------------------------------------------------ 178 179class ThreadPlan : 180 public UserID 181{ 182public: 183 typedef enum 184 { 185 eAllThreads, 186 eSomeThreads, 187 eThisThread 188 } ThreadScope; 189 190 // We use these enums so that we can cast a base thread plan to it's real type without having to resort 191 // to dynamic casting. 192 typedef enum 193 { 194 eKindGeneric, 195 eKindBase, 196 eKindCallFunction, 197 eKindStepInstruction, 198 eKindStepOut, 199 eKindStepOverBreakpoint, 200 eKindStepOverRange, 201 eKindStepInRange, 202 eKindRunToAddress, 203 eKindStepThrough, 204 eKindStepUntil, 205 eKindTestCondition 206 207 } ThreadPlanKind; 208 209 //------------------------------------------------------------------ 210 // Constructors and Destructors 211 //------------------------------------------------------------------ 212 ThreadPlan (ThreadPlanKind kind, 213 const char *name, 214 Thread &thread, 215 Vote stop_vote, 216 Vote run_vote); 217 218 virtual 219 ~ThreadPlan(); 220 221 //------------------------------------------------------------------ 222 /// Returns the name of this thread plan. 223 /// 224 /// @return 225 /// A const char * pointer to the thread plan's name. 226 //------------------------------------------------------------------ 227 const char * 228 GetName () const 229 { 230 return m_name.c_str(); 231 } 232 233 //------------------------------------------------------------------ 234 /// Returns the Thread that is using this thread plan. 235 /// 236 /// @return 237 /// A pointer to the thread plan's owning thread. 238 //------------------------------------------------------------------ 239 Thread & 240 GetThread() 241 { 242 return m_thread; 243 } 244 245 const Thread & 246 GetThread() const 247 { 248 return m_thread; 249 } 250 251 Target & 252 GetTarget() 253 { 254 return m_thread.GetProcess()->GetTarget(); 255 } 256 257 const Target & 258 GetTarget() const 259 { 260 return m_thread.GetProcess()->GetTarget(); 261 } 262 263 //------------------------------------------------------------------ 264 /// Print a description of this thread to the stream \a s. 265 /// \a thread. 266 /// 267 /// @param[in] s 268 /// The stream to which to print the description. 269 /// 270 /// @param[in] level 271 /// The level of description desired. Note that eDescriptionLevelBrief 272 /// will be used in the stop message printed when the plan is complete. 273 //------------------------------------------------------------------ 274 virtual void 275 GetDescription (Stream *s, 276 lldb::DescriptionLevel level) = 0; 277 278 //------------------------------------------------------------------ 279 /// Returns whether this plan could be successfully created. 280 /// 281 /// @param[in] error 282 /// A stream to which to print some reason why the plan could not be created. 283 /// Can be NULL. 284 /// 285 /// @return 286 /// \b true if the plan should be queued, \b false otherwise. 287 //------------------------------------------------------------------ 288 virtual bool 289 ValidatePlan (Stream *error) = 0; 290 291 virtual bool 292 PlanExplainsStop () = 0; 293 294 bool 295 TracerExplainsStop () 296 { 297 if (!m_tracer_sp) 298 return false; 299 else 300 return m_tracer_sp->TracerExplainsStop(); 301 } 302 303 304 lldb::StateType 305 RunState (); 306 307 virtual bool 308 ShouldStop (Event *event_ptr) = 0; 309 310 virtual bool 311 ShouldAutoContinue (Event *event_ptr) 312 { 313 return false; 314 } 315 316 // Whether a "stop class" event should be reported to the "outside world". In general 317 // if a thread plan is active, events should not be reported. 318 319 virtual Vote 320 ShouldReportStop (Event *event_ptr); 321 322 virtual Vote 323 ShouldReportRun (Event *event_ptr); 324 325 virtual void 326 SetStopOthers (bool new_value); 327 328 virtual bool 329 StopOthers (); 330 331 virtual bool 332 WillResume (lldb::StateType resume_state, bool current_plan); 333 334 virtual bool 335 WillStop () = 0; 336 337 virtual bool 338 IsMasterPlan() 339 { 340 return false; 341 } 342 343 virtual bool 344 OkayToDiscard(); 345 346 void 347 SetOkayToDiscard (bool value) 348 { 349 m_okay_to_discard = value; 350 } 351 352 // The base class MischiefManaged does some cleanup - so you have to call it 353 // in your MischiefManaged derived class. 354 virtual bool 355 MischiefManaged (); 356 357 bool 358 GetPrivate () 359 { 360 return m_plan_private; 361 } 362 363 void 364 SetPrivate (bool input) 365 { 366 m_plan_private = input; 367 } 368 369 virtual void 370 DidPush(); 371 372 virtual void 373 WillPop(); 374 375 // This pushes \a plan onto the plan stack of the current plan's thread. 376 void 377 PushPlan (lldb::ThreadPlanSP &thread_plan_sp) 378 { 379 m_thread.PushPlan (thread_plan_sp); 380 } 381 382 ThreadPlanKind GetKind() const 383 { 384 return m_kind; 385 } 386 387 bool 388 IsPlanComplete(); 389 390 void 391 SetPlanComplete (); 392 393 lldb::ThreadPlanTracerSP & 394 GetThreadPlanTracer() 395 { 396 return m_tracer_sp; 397 } 398 399 void 400 SetThreadPlanTracer (lldb::ThreadPlanTracerSP new_tracer_sp) 401 { 402 m_tracer_sp = new_tracer_sp; 403 } 404 405 void 406 DoTraceLog () 407 { 408 if (m_tracer_sp && m_tracer_sp->TracingEnabled()) 409 m_tracer_sp->Log(); 410 } 411 412 // Some thread plans hide away the actual stop info which caused any particular stop. For 413 // instance the ThreadPlanCallFunction restores the original stop reason so that stopping and 414 // calling a few functions won't lose the history of the run. 415 // This call can be implemented to get you back to the real stop info. 416 virtual lldb::StopInfoSP 417 GetRealStopInfo () 418 { 419 return m_thread.GetStopInfo (); 420 } 421 422 virtual lldb::ValueObjectSP 423 GetReturnValueObject () 424 { 425 return lldb::ValueObjectSP(); 426 } 427 428protected: 429 //------------------------------------------------------------------ 430 // Classes that inherit from ThreadPlan can see and modify these 431 //------------------------------------------------------------------ 432 433 // This gets the previous plan to the current plan (for forwarding requests). 434 // This is mostly a formal requirement, it allows us to make the Thread's 435 // GetPreviousPlan protected, but only friend ThreadPlan to thread. 436 437 ThreadPlan * 438 GetPreviousPlan () 439 { 440 return m_thread.GetPreviousPlan (this); 441 } 442 443 // This forwards the private Thread::GetPrivateStopReason which is generally what 444 // ThreadPlan's need to know. 445 446 lldb::StopInfoSP 447 GetPrivateStopReason() 448 { 449 return m_thread.GetPrivateStopReason (); 450 } 451 452 void 453 SetStopInfo (lldb::StopInfoSP stop_reason_sp) 454 { 455 m_thread.SetStopInfo (stop_reason_sp); 456 } 457 458 virtual lldb::StateType 459 GetPlanRunState () = 0; 460 461 Thread &m_thread; 462 Vote m_stop_vote; 463 Vote m_run_vote; 464 465private: 466 //------------------------------------------------------------------ 467 // For ThreadPlan only 468 //------------------------------------------------------------------ 469 static lldb::user_id_t GetNextID (); 470 471 ThreadPlanKind m_kind; 472 std::string m_name; 473 Mutex m_plan_complete_mutex; 474 bool m_plan_complete; 475 bool m_plan_private; 476 bool m_okay_to_discard; 477 478 lldb::ThreadPlanTracerSP m_tracer_sp; 479 480private: 481 DISALLOW_COPY_AND_ASSIGN(ThreadPlan); 482}; 483 484 485} // namespace lldb_private 486 487#endif // liblldb_ThreadPlan_h_ 488