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