Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ThreadPlanStepThrough.cpp -----------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 9 | #include "lldb/Target/ThreadPlanStepThrough.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "lldb/Breakpoint/Breakpoint.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "lldb/Target/DynamicLoader.h" |
Alex Langford | e5a7a85 | 2019-05-30 22:00:18 +0000 | [diff] [blame] | 12 | #include "lldb/Target/LanguageRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Target/Process.h" |
| 14 | #include "lldb/Target/RegisterContext.h" |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | // ThreadPlanStepThrough: If the current instruction is a trampoline, step |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 23 | // through it If it is the beginning of the prologue of a function, step |
| 24 | // through that as well. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | // FIXME: At present only handles DYLD trampolines. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread, |
| 28 | StackID &m_stack_id, |
| 29 | bool stop_others) |
| 30 | : ThreadPlan(ThreadPlan::eKindStepThrough, |
| 31 | "Step through trampolines and prologues", thread, |
| 32 | eVoteNoOpinion, eVoteNoOpinion), |
| 33 | m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID), |
| 34 | m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id), |
| 35 | m_stop_others(stop_others) { |
| 36 | LookForPlanToStepThroughFromCurrentPC(); |
| 37 | |
| 38 | // If we don't get a valid step through plan, don't bother to set up a |
| 39 | // backstop. |
| 40 | if (m_sub_plan_sp) { |
| 41 | m_start_address = GetThread().GetRegisterContext()->GetPC(0); |
| 42 | |
| 43 | // We are going to return back to the concrete frame 1, we might pass by |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 44 | // some inlined code that we're in the middle of by doing this, but it's |
| 45 | // easier than trying to figure out where the inlined code might return to. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 47 | StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | |
| 49 | if (return_frame_sp) { |
| 50 | m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress( |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 51 | thread.CalculateTarget().get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | Breakpoint *return_bp = |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 53 | m_process.GetTarget() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | .CreateBreakpoint(m_backstop_addr, true, false) |
| 55 | .get(); |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | if (return_bp != nullptr) { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 58 | if (return_bp->IsHardware() && !return_bp->HasResolvedLocations()) |
| 59 | m_could_not_resolve_hw_bp = true; |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 60 | return_bp->SetThreadID(m_tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | m_backstop_bkpt_id = return_bp->GetID(); |
| 62 | return_bp->SetBreakpointKind("step-through-backstop"); |
| 63 | } |
| 64 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 65 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 66 | LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64, |
| 67 | m_backstop_bkpt_id, m_backstop_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | } |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 69 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); } |
| 74 | |
| 75 | void ThreadPlanStepThrough::DidPush() { |
| 76 | if (m_sub_plan_sp) |
| 77 | PushPlan(m_sub_plan_sp); |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() { |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 81 | Thread &thread = GetThread(); |
| 82 | DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | if (loader) |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 84 | m_sub_plan_sp = loader->GetStepThroughTrampolinePlan(thread, m_stop_others); |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 85 | |
Alex Langford | e5a7a85 | 2019-05-30 22:00:18 +0000 | [diff] [blame] | 86 | // If the DynamicLoader was unable to provide us with a ThreadPlan, then we |
| 87 | // try the LanguageRuntimes. |
| 88 | if (!m_sub_plan_sp) { |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 89 | for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | m_sub_plan_sp = |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 91 | runtime->GetStepThroughTrampolinePlan(thread, m_stop_others); |
Shafik Yaghmour | aa30268 | 2018-10-12 17:20:39 +0000 | [diff] [blame] | 92 | |
Alex Langford | e5a7a85 | 2019-05-30 22:00:18 +0000 | [diff] [blame] | 93 | if (m_sub_plan_sp) |
| 94 | break; |
| 95 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 99 | if (log) { |
| 100 | lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0); |
| 101 | if (m_sub_plan_sp) { |
| 102 | StreamString s; |
| 103 | m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 104 | LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s", |
| 105 | current_address, s.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 107 | LLDB_LOGF(log, |
| 108 | "Couldn't find step through plan from address 0x%" PRIx64 ".", |
| 109 | current_address); |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 110 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | void ThreadPlanStepThrough::GetDescription(Stream *s, |
| 115 | lldb::DescriptionLevel level) { |
| 116 | if (level == lldb::eDescriptionLevelBrief) |
| 117 | s->Printf("Step through"); |
| 118 | else { |
| 119 | s->PutCString("Stepping through trampoline code from: "); |
Raphael Isemann | 1462f5a | 2019-12-05 14:41:09 +0100 | [diff] [blame] | 120 | DumpAddress(s->AsRawOstream(), m_start_address, sizeof(addr_t)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { |
| 122 | s->Printf(" with backstop breakpoint ID: %d at address: ", |
| 123 | m_backstop_bkpt_id); |
Raphael Isemann | 1462f5a | 2019-12-05 14:41:09 +0100 | [diff] [blame] | 124 | DumpAddress(s->AsRawOstream(), m_backstop_addr, sizeof(addr_t)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | } else |
| 126 | s->PutCString(" unable to set a backstop breakpoint."); |
| 127 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | bool ThreadPlanStepThrough::ValidatePlan(Stream *error) { |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 131 | if (m_could_not_resolve_hw_bp) { |
| 132 | if (error) |
| 133 | error->PutCString( |
| 134 | "Could not create hardware breakpoint for thread plan."); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) { |
| 139 | if (error) |
| 140 | error->PutCString("Could not create backstop breakpoint."); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | if (!m_sub_plan_sp.get()) { |
| 145 | if (error) |
| 146 | error->PutCString("Does not have a subplan."); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) { |
| 154 | // If we have a sub-plan, it will have been asked first if we explain the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 155 | // stop, and we won't get asked. The only time we would be the one directly |
| 156 | // asked this question is if we hit our backstop breakpoint. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | |
| 158 | return HitOurBackstopBreakpoint(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) { |
| 162 | // If we've already marked ourselves done, then we're done... |
| 163 | if (IsPlanComplete()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | // First, did we hit the backstop breakpoint? |
| 167 | if (HitOurBackstopBreakpoint()) { |
| 168 | SetPlanComplete(true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | // If we don't have a sub-plan, then we're also done (can't see how we would |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 173 | // ever get here without a plan, but just in case. |
Jim Ingham | 18de2fd | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | if (!m_sub_plan_sp) { |
| 176 | SetPlanComplete(); |
| 177 | return true; |
| 178 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | // If the current sub plan is not done, we don't want to stop. Actually, we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 181 | // probably won't ever get here in this state, since we generally won't get |
| 182 | // asked any questions if out current sub-plan is not done... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | if (!m_sub_plan_sp->IsPlanComplete()) |
Jim Ingham | 25f6670 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 184 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 186 | // If our current sub plan failed, then let's just run to our backstop. If |
| 187 | // we can't do that then just stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | if (!m_sub_plan_sp->PlanSucceeded()) { |
| 189 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { |
| 190 | m_sub_plan_sp.reset(); |
| 191 | return false; |
| 192 | } else { |
| 193 | SetPlanComplete(false); |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Next see if there is a specific step through plan at our current pc (these |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 199 | // might chain, for instance stepping through a dylib trampoline to the objc |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | // dispatch function...) |
| 201 | LookForPlanToStepThroughFromCurrentPC(); |
| 202 | if (m_sub_plan_sp) { |
| 203 | PushPlan(m_sub_plan_sp); |
| 204 | return false; |
| 205 | } else { |
| 206 | SetPlanComplete(); |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; } |
| 212 | |
| 213 | StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; } |
| 214 | |
| 215 | bool ThreadPlanStepThrough::DoWillResume(StateType resume_state, |
| 216 | bool current_plan) { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool ThreadPlanStepThrough::WillStop() { return true; } |
| 221 | |
| 222 | void ThreadPlanStepThrough::ClearBackstopBreakpoint() { |
| 223 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 224 | m_process.GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID; |
Jonas Devlieghere | e103ae9 | 2018-11-15 01:18:15 +0000 | [diff] [blame] | 226 | m_could_not_resolve_hw_bp = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | bool ThreadPlanStepThrough::MischiefManaged() { |
| 231 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 232 | |
| 233 | if (!IsPlanComplete()) { |
| 234 | return false; |
| 235 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 236 | LLDB_LOGF(log, "Completed step through step plan."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | |
| 238 | ClearBackstopBreakpoint(); |
| 239 | ThreadPlan::MischiefManaged(); |
| 240 | return true; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() { |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 245 | Thread &thread = GetThread(); |
| 246 | StopInfoSP stop_info_sp(thread.GetStopInfo()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) { |
| 248 | break_id_t stop_value = (break_id_t)stop_info_sp->GetValue(); |
| 249 | BreakpointSiteSP cur_site_sp = |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 250 | m_process.GetBreakpointSiteList().FindByID(stop_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | if (cur_site_sp && |
| 252 | cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) { |
Jim Ingham | e4598dc | 2020-03-10 14:03:53 -0700 | [diff] [blame] | 253 | StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | |
| 255 | if (cur_frame_zero_id == m_return_stack_id) { |
| 256 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 257 | if (log) |
| 258 | log->PutCString("ThreadPlanStepThrough hit backstop breakpoint."); |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | } |