blob: 8dcbe85c3a2ae71e69dc0992e0e7d009520c34b3 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ThreadPlan.cpp ------------------------------------------*- 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#include "lldb/Target/ThreadPlan.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham745ac7a2010-11-11 19:26:09 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/State.h"
Greg Clayton5205f0b2010-09-03 17:10:42 +000019#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/Thread.h"
Jim Ingham745ac7a2010-11-11 19:26:09 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27//----------------------------------------------------------------------
28// ThreadPlan constructor
29//----------------------------------------------------------------------
Jim Ingham5a47e8b2010-06-19 04:45:32 +000030ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote) :
Chris Lattner24943d22010-06-08 16:52:24 +000031 m_thread (thread),
Chris Lattner24943d22010-06-08 16:52:24 +000032 m_stop_vote (stop_vote),
33 m_run_vote (run_vote),
Benjamin Kramer36a08102010-07-16 12:32:33 +000034 m_kind (kind),
35 m_name (name),
36 m_plan_complete_mutex (Mutex::eMutexTypeRecursive),
37 m_plan_complete (false),
38 m_plan_private (false),
Jim Ingham88e3de22012-05-03 21:19:36 +000039 m_okay_to_discard (true),
Jim Ingham707b7a82012-05-01 18:38:37 +000040 m_is_master_plan (false),
41 m_plan_succeeded(true)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43 SetID (GetNextID());
44}
45
46//----------------------------------------------------------------------
47// Destructor
48//----------------------------------------------------------------------
49ThreadPlan::~ThreadPlan()
50{
51}
52
Chris Lattner24943d22010-06-08 16:52:24 +000053bool
54ThreadPlan::IsPlanComplete ()
55{
Greg Claytonbef15832010-07-14 00:18:15 +000056 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000057 return m_plan_complete;
58}
59
60void
Jim Ingham707b7a82012-05-01 18:38:37 +000061ThreadPlan::SetPlanComplete (bool success)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
Greg Claytonbef15832010-07-14 00:18:15 +000063 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000064 m_plan_complete = true;
Jim Ingham707b7a82012-05-01 18:38:37 +000065 m_plan_succeeded = success;
Chris Lattner24943d22010-06-08 16:52:24 +000066}
67
68bool
69ThreadPlan::MischiefManaged ()
70{
Greg Claytonbef15832010-07-14 00:18:15 +000071 Mutex::Locker locker(m_plan_complete_mutex);
Jim Ingham038fa8e2012-05-10 01:35:39 +000072 // Mark the plan is complete, but don't override the success flag.
Chris Lattner24943d22010-06-08 16:52:24 +000073 m_plan_complete = true;
74 return true;
75}
76
77Vote
78ThreadPlan::ShouldReportStop (Event *event_ptr)
79{
Greg Claytone005f2c2010-11-06 01:53:30 +000080 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +000081
Chris Lattner24943d22010-06-08 16:52:24 +000082 if (m_stop_vote == eVoteNoOpinion)
83 {
84 ThreadPlan *prev_plan = GetPreviousPlan ();
85 if (prev_plan)
Greg Clayton5205f0b2010-09-03 17:10:42 +000086 {
87 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
88 if (log)
Jim Ingham441e3b92012-03-01 00:50:50 +000089 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s",
90 GetVoteAsCString (prev_vote));
Greg Clayton5205f0b2010-09-03 17:10:42 +000091 return prev_vote;
92 }
Chris Lattner24943d22010-06-08 16:52:24 +000093 }
Greg Clayton5205f0b2010-09-03 17:10:42 +000094 if (log)
Greg Claytonf04d6612010-09-03 22:45:01 +000095 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
Chris Lattner24943d22010-06-08 16:52:24 +000096 return m_stop_vote;
97}
98
99Vote
100ThreadPlan::ShouldReportRun (Event *event_ptr)
101{
102 if (m_run_vote == eVoteNoOpinion)
103 {
104 ThreadPlan *prev_plan = GetPreviousPlan ();
105 if (prev_plan)
106 return prev_plan->ShouldReportRun (event_ptr);
107 }
108 return m_run_vote;
109}
110
111bool
112ThreadPlan::StopOthers ()
113{
114 ThreadPlan *prev_plan;
115 prev_plan = GetPreviousPlan ();
116 if (prev_plan == NULL)
117 return false;
118 else
119 return prev_plan->StopOthers();
120}
121
Jim Ingham360f53f2010-11-30 02:22:11 +0000122void
123ThreadPlan::SetStopOthers (bool new_value)
124{
125 // SetStopOthers doesn't work up the hierarchy. You have to set the
126 // explicit ThreadPlan you want to affect.
127}
128
Chris Lattner24943d22010-06-08 16:52:24 +0000129bool
130ThreadPlan::WillResume (StateType resume_state, bool current_plan)
131{
132 if (current_plan)
133 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000134 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000135
136 if (log)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000137 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000138 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000139 addr_t pc = reg_ctx->GetPC();
140 addr_t sp = reg_ctx->GetSP();
141 addr_t fp = reg_ctx->GetFP();
Jim Ingham441e3b92012-03-01 00:50:50 +0000142 log->Printf("%s Thread #%u: tid = 0x%4.4llx, pc = 0x%8.8llx, sp = 0x%8.8llx, fp = 0x%8.8llx, "
143 "plan = '%s', state = %s, stop others = %d",
Greg Claytonf04d6612010-09-03 22:45:01 +0000144 __FUNCTION__,
Greg Clayton5205f0b2010-09-03 17:10:42 +0000145 m_thread.GetIndexID(),
146 m_thread.GetID(),
147 (uint64_t)pc,
148 (uint64_t)sp,
149 (uint64_t)fp,
150 m_name.c_str(),
151 StateAsCString(resume_state),
152 StopOthers());
153 }
Chris Lattner24943d22010-06-08 16:52:24 +0000154 }
155 return true;
156}
157
158lldb::user_id_t
159ThreadPlan::GetNextID()
160{
161 static uint32_t g_nextPlanID = 0;
162 return ++g_nextPlanID;
163}
164
165void
166ThreadPlan::DidPush()
167{
168}
169
170void
171ThreadPlan::WillPop()
172{
173}
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175bool
176ThreadPlan::OkayToDiscard()
177{
178 if (!IsMasterPlan())
179 return true;
180 else
181 return m_okay_to_discard;
182}
183
Jim Ingham745ac7a2010-11-11 19:26:09 +0000184lldb::StateType
185ThreadPlan::RunState ()
186{
187 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
188 return eStateStepping;
189 else
190 return GetPlanRunState();
191}