1cfca06d7SDimitry Andric //===-- ThreadPlanStepThrough.cpp -----------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlanStepThrough.h"
1014f1b3e8SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
11f034231aSEd Maste #include "lldb/Target/DynamicLoader.h"
125f29bb8aSDimitry Andric #include "lldb/Target/LanguageRuntime.h"
13f034231aSEd Maste #include "lldb/Target/Process.h"
14f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
15f034231aSEd Maste #include "lldb/Target/Target.h"
16145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1774a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1874a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
19f034231aSEd Maste
20f034231aSEd Maste using namespace lldb;
21f034231aSEd Maste using namespace lldb_private;
22f034231aSEd Maste
2314f1b3e8SDimitry Andric // ThreadPlanStepThrough: If the current instruction is a trampoline, step
24f73363f1SDimitry Andric // through it If it is the beginning of the prologue of a function, step
25f73363f1SDimitry Andric // through that as well.
26f034231aSEd Maste
ThreadPlanStepThrough(Thread & thread,StackID & m_stack_id,bool stop_others)2714f1b3e8SDimitry Andric ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
2814f1b3e8SDimitry Andric StackID &m_stack_id,
2914f1b3e8SDimitry Andric bool stop_others)
3014f1b3e8SDimitry Andric : ThreadPlan(ThreadPlan::eKindStepThrough,
3114f1b3e8SDimitry Andric "Step through trampolines and prologues", thread,
3214f1b3e8SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
3314f1b3e8SDimitry Andric m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
3414f1b3e8SDimitry Andric m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
3514f1b3e8SDimitry Andric m_stop_others(stop_others) {
36f034231aSEd Maste LookForPlanToStepThroughFromCurrentPC();
37f034231aSEd Maste
3814f1b3e8SDimitry Andric // If we don't get a valid step through plan, don't bother to set up a
3914f1b3e8SDimitry Andric // backstop.
4014f1b3e8SDimitry Andric if (m_sub_plan_sp) {
41f034231aSEd Maste m_start_address = GetThread().GetRegisterContext()->GetPC(0);
42f034231aSEd Maste
4314f1b3e8SDimitry Andric // We are going to return back to the concrete frame 1, we might pass by
44f73363f1SDimitry Andric // some inlined code that we're in the middle of by doing this, but it's
45f73363f1SDimitry Andric // easier than trying to figure out where the inlined code might return to.
46f034231aSEd Maste
47cfca06d7SDimitry Andric StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id);
48f034231aSEd Maste
4914f1b3e8SDimitry Andric if (return_frame_sp) {
5014f1b3e8SDimitry Andric m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
51cfca06d7SDimitry Andric thread.CalculateTarget().get());
5214f1b3e8SDimitry Andric Breakpoint *return_bp =
53cfca06d7SDimitry Andric m_process.GetTarget()
5414f1b3e8SDimitry Andric .CreateBreakpoint(m_backstop_addr, true, false)
5514f1b3e8SDimitry Andric .get();
5694994d37SDimitry Andric
5714f1b3e8SDimitry Andric if (return_bp != nullptr) {
5894994d37SDimitry Andric if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
5994994d37SDimitry Andric m_could_not_resolve_hw_bp = true;
60cfca06d7SDimitry Andric return_bp->SetThreadID(m_tid);
61f034231aSEd Maste m_backstop_bkpt_id = return_bp->GetID();
62f034231aSEd Maste return_bp->SetBreakpointKind("step-through-backstop");
63f034231aSEd Maste }
64145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
6514f1b3e8SDimitry Andric if (log) {
66ead24645SDimitry Andric LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64,
6714f1b3e8SDimitry Andric m_backstop_bkpt_id, m_backstop_addr);
68f034231aSEd Maste }
69f034231aSEd Maste }
70f034231aSEd Maste }
71f034231aSEd Maste }
72f034231aSEd Maste
~ThreadPlanStepThrough()7314f1b3e8SDimitry Andric ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
74f034231aSEd Maste
DidPush()7514f1b3e8SDimitry Andric void ThreadPlanStepThrough::DidPush() {
76f034231aSEd Maste if (m_sub_plan_sp)
77f034231aSEd Maste PushPlan(m_sub_plan_sp);
78f034231aSEd Maste }
79f034231aSEd Maste
LookForPlanToStepThroughFromCurrentPC()8014f1b3e8SDimitry Andric void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
81cfca06d7SDimitry Andric Thread &thread = GetThread();
82cfca06d7SDimitry Andric DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader();
83205afe67SEd Maste if (loader)
84cfca06d7SDimitry Andric m_sub_plan_sp = loader->GetStepThroughTrampolinePlan(thread, m_stop_others);
85205afe67SEd Maste
865f29bb8aSDimitry Andric // If the DynamicLoader was unable to provide us with a ThreadPlan, then we
875f29bb8aSDimitry Andric // try the LanguageRuntimes.
885f29bb8aSDimitry Andric if (!m_sub_plan_sp) {
89cfca06d7SDimitry Andric for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) {
9014f1b3e8SDimitry Andric m_sub_plan_sp =
91cfca06d7SDimitry Andric runtime->GetStepThroughTrampolinePlan(thread, m_stop_others);
9294994d37SDimitry Andric
935f29bb8aSDimitry Andric if (m_sub_plan_sp)
945f29bb8aSDimitry Andric break;
955f29bb8aSDimitry Andric }
96f034231aSEd Maste }
97f034231aSEd Maste
98145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
9914f1b3e8SDimitry Andric if (log) {
100f034231aSEd Maste lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
10114f1b3e8SDimitry Andric if (m_sub_plan_sp) {
102f034231aSEd Maste StreamString s;
103f034231aSEd Maste m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
104ead24645SDimitry Andric LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s",
10514f1b3e8SDimitry Andric current_address, s.GetData());
10614f1b3e8SDimitry Andric } else {
107ead24645SDimitry Andric LLDB_LOGF(log,
108ead24645SDimitry Andric "Couldn't find step through plan from address 0x%" PRIx64 ".",
10914f1b3e8SDimitry Andric current_address);
110f034231aSEd Maste }
111f034231aSEd Maste }
112f034231aSEd Maste }
113f034231aSEd Maste
GetDescription(Stream * s,lldb::DescriptionLevel level)11414f1b3e8SDimitry Andric void ThreadPlanStepThrough::GetDescription(Stream *s,
11514f1b3e8SDimitry Andric lldb::DescriptionLevel level) {
116f034231aSEd Maste if (level == lldb::eDescriptionLevelBrief)
117f034231aSEd Maste s->Printf("Step through");
11814f1b3e8SDimitry Andric else {
119f034231aSEd Maste s->PutCString("Stepping through trampoline code from: ");
120706b4fc4SDimitry Andric DumpAddress(s->AsRawOstream(), m_start_address, sizeof(addr_t));
12114f1b3e8SDimitry Andric if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
12214f1b3e8SDimitry Andric s->Printf(" with backstop breakpoint ID: %d at address: ",
12314f1b3e8SDimitry Andric m_backstop_bkpt_id);
124706b4fc4SDimitry Andric DumpAddress(s->AsRawOstream(), m_backstop_addr, sizeof(addr_t));
12514f1b3e8SDimitry Andric } else
126f034231aSEd Maste s->PutCString(" unable to set a backstop breakpoint.");
127f034231aSEd Maste }
128f034231aSEd Maste }
129f034231aSEd Maste
ValidatePlan(Stream * error)13014f1b3e8SDimitry Andric bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
13194994d37SDimitry Andric if (m_could_not_resolve_hw_bp) {
13294994d37SDimitry Andric if (error)
13394994d37SDimitry Andric error->PutCString(
13494994d37SDimitry Andric "Could not create hardware breakpoint for thread plan.");
13594994d37SDimitry Andric return false;
13694994d37SDimitry Andric }
13794994d37SDimitry Andric
13894994d37SDimitry Andric if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
13994994d37SDimitry Andric if (error)
14094994d37SDimitry Andric error->PutCString("Could not create backstop breakpoint.");
14194994d37SDimitry Andric return false;
14294994d37SDimitry Andric }
14394994d37SDimitry Andric
14494994d37SDimitry Andric if (!m_sub_plan_sp.get()) {
14594994d37SDimitry Andric if (error)
14694994d37SDimitry Andric error->PutCString("Does not have a subplan.");
14794994d37SDimitry Andric return false;
14894994d37SDimitry Andric }
14994994d37SDimitry Andric
15094994d37SDimitry Andric return true;
151f034231aSEd Maste }
152f034231aSEd Maste
DoPlanExplainsStop(Event * event_ptr)15314f1b3e8SDimitry Andric bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
15414f1b3e8SDimitry Andric // If we have a sub-plan, it will have been asked first if we explain the
155f73363f1SDimitry Andric // stop, and we won't get asked. The only time we would be the one directly
156f73363f1SDimitry Andric // asked this question is if we hit our backstop breakpoint.
157f034231aSEd Maste
158e81d9d49SDimitry Andric return HitOurBackstopBreakpoint();
159f034231aSEd Maste }
160f034231aSEd Maste
ShouldStop(Event * event_ptr)16114f1b3e8SDimitry Andric bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
162f034231aSEd Maste // If we've already marked ourselves done, then we're done...
163f034231aSEd Maste if (IsPlanComplete())
164f034231aSEd Maste return true;
165f034231aSEd Maste
166f034231aSEd Maste // First, did we hit the backstop breakpoint?
16714f1b3e8SDimitry Andric if (HitOurBackstopBreakpoint()) {
168866dcdacSEd Maste SetPlanComplete(true);
169f034231aSEd Maste return true;
170f034231aSEd Maste }
171f034231aSEd Maste
17214f1b3e8SDimitry Andric // If we don't have a sub-plan, then we're also done (can't see how we would
173f73363f1SDimitry Andric // ever get here without a plan, but just in case.
174f034231aSEd Maste
17514f1b3e8SDimitry Andric if (!m_sub_plan_sp) {
176f034231aSEd Maste SetPlanComplete();
177f034231aSEd Maste return true;
178f034231aSEd Maste }
179f034231aSEd Maste
18014f1b3e8SDimitry Andric // If the current sub plan is not done, we don't want to stop. Actually, we
181f73363f1SDimitry Andric // probably won't ever get here in this state, since we generally won't get
182f73363f1SDimitry Andric // asked any questions if out current sub-plan is not done...
183f034231aSEd Maste if (!m_sub_plan_sp->IsPlanComplete())
184f034231aSEd Maste return false;
185f034231aSEd Maste
186f73363f1SDimitry Andric // If our current sub plan failed, then let's just run to our backstop. If
187f73363f1SDimitry Andric // we can't do that then just stop.
18814f1b3e8SDimitry Andric if (!m_sub_plan_sp->PlanSucceeded()) {
18914f1b3e8SDimitry Andric if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
190f034231aSEd Maste m_sub_plan_sp.reset();
191f034231aSEd Maste return false;
19214f1b3e8SDimitry Andric } else {
193f034231aSEd Maste SetPlanComplete(false);
194f034231aSEd Maste return true;
195f034231aSEd Maste }
196f034231aSEd Maste }
197f034231aSEd Maste
19814f1b3e8SDimitry Andric // Next see if there is a specific step through plan at our current pc (these
199f73363f1SDimitry Andric // might chain, for instance stepping through a dylib trampoline to the objc
20014f1b3e8SDimitry Andric // dispatch function...)
201f034231aSEd Maste LookForPlanToStepThroughFromCurrentPC();
20214f1b3e8SDimitry Andric if (m_sub_plan_sp) {
203f034231aSEd Maste PushPlan(m_sub_plan_sp);
204f034231aSEd Maste return false;
20514f1b3e8SDimitry Andric } else {
206f034231aSEd Maste SetPlanComplete();
207f034231aSEd Maste return true;
208f034231aSEd Maste }
209f034231aSEd Maste }
210f034231aSEd Maste
StopOthers()21114f1b3e8SDimitry Andric bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
212f034231aSEd Maste
GetPlanRunState()21314f1b3e8SDimitry Andric StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
214f034231aSEd Maste
DoWillResume(StateType resume_state,bool current_plan)21514f1b3e8SDimitry Andric bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
21614f1b3e8SDimitry Andric bool current_plan) {
217f034231aSEd Maste return true;
218f034231aSEd Maste }
219f034231aSEd Maste
WillStop()22014f1b3e8SDimitry Andric bool ThreadPlanStepThrough::WillStop() { return true; }
221f034231aSEd Maste
ClearBackstopBreakpoint()22214f1b3e8SDimitry Andric void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
22314f1b3e8SDimitry Andric if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
224cfca06d7SDimitry Andric m_process.GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
225f034231aSEd Maste m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
22694994d37SDimitry Andric m_could_not_resolve_hw_bp = false;
227f034231aSEd Maste }
228f034231aSEd Maste }
229f034231aSEd Maste
MischiefManaged()23014f1b3e8SDimitry Andric bool ThreadPlanStepThrough::MischiefManaged() {
231145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
232f034231aSEd Maste
23314f1b3e8SDimitry Andric if (!IsPlanComplete()) {
234f034231aSEd Maste return false;
23514f1b3e8SDimitry Andric } else {
236ead24645SDimitry Andric LLDB_LOGF(log, "Completed step through step plan.");
237f034231aSEd Maste
238f034231aSEd Maste ClearBackstopBreakpoint();
239f034231aSEd Maste ThreadPlan::MischiefManaged();
240f034231aSEd Maste return true;
241f034231aSEd Maste }
242f034231aSEd Maste }
243f034231aSEd Maste
HitOurBackstopBreakpoint()24414f1b3e8SDimitry Andric bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
245cfca06d7SDimitry Andric Thread &thread = GetThread();
246cfca06d7SDimitry Andric StopInfoSP stop_info_sp(thread.GetStopInfo());
24714f1b3e8SDimitry Andric if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
248f034231aSEd Maste break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
24914f1b3e8SDimitry Andric BreakpointSiteSP cur_site_sp =
250cfca06d7SDimitry Andric m_process.GetBreakpointSiteList().FindByID(stop_value);
25114f1b3e8SDimitry Andric if (cur_site_sp &&
25214f1b3e8SDimitry Andric cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
253cfca06d7SDimitry Andric StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID();
254f034231aSEd Maste
25514f1b3e8SDimitry Andric if (cur_frame_zero_id == m_return_stack_id) {
256145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
257f034231aSEd Maste if (log)
258f034231aSEd Maste log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
259f034231aSEd Maste return true;
260f034231aSEd Maste }
261f034231aSEd Maste }
262f034231aSEd Maste }
263f034231aSEd Maste return false;
264f034231aSEd Maste }
265