Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepThrough.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/ThreadPlanStepThrough.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/lldb-private-log.h" |
| 17 | #include "lldb/Core/Log.h" |
| 18 | #include "lldb/Core/Stream.h" |
| 19 | #include "lldb/Target/DynamicLoader.h" |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Process.h" |
| 22 | #include "lldb/Target/RegisterContext.h" |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Target.h" |
| 24 | #include "lldb/Breakpoint/Breakpoint.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | //---------------------------------------------------------------------- |
| 30 | // ThreadPlanStepThrough: If the current instruction is a trampoline, step through it |
| 31 | // If it is the beginning of the prologue of a function, step through that as well. |
| 32 | // FIXME: At present only handles DYLD trampolines. |
| 33 | //---------------------------------------------------------------------- |
| 34 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 35 | ThreadPlanStepThrough::ThreadPlanStepThrough (Thread &thread, StackID &m_stack_id, bool stop_others) : |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 36 | ThreadPlan (ThreadPlan::eKindStepThrough, "Step through trampolines and prologues", thread, eVoteNoOpinion, eVoteNoOpinion), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | m_start_address (0), |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 38 | m_backstop_bkpt_id (LLDB_INVALID_BREAK_ID), |
| 39 | m_backstop_addr(LLDB_INVALID_ADDRESS), |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 40 | m_return_stack_id (m_stack_id), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | m_stop_others (stop_others) |
| 42 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 43 | |
| 44 | LookForPlanToStepThroughFromCurrentPC(); |
| 45 | |
| 46 | // If we don't get a valid step through plan, don't bother to set up a backstop. |
| 47 | if (m_sub_plan_sp) |
| 48 | { |
| 49 | m_start_address = GetThread().GetRegisterContext()->GetPC(0); |
Jim Ingham | 41ce4f1 | 2012-03-01 20:01:22 +0000 | [diff] [blame] | 50 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 51 | // We are going to return back to the concrete frame 1, we might pass by some inlined code that we're in |
| 52 | // the middle of by doing this, but it's easier than trying to figure out where the inlined code might return to. |
| 53 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 54 | StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID (m_stack_id); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 55 | |
| 56 | if (return_frame_sp) |
| 57 | { |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 58 | m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(m_thread.CalculateTarget().get()); |
| 59 | Breakpoint *return_bp = m_thread.GetProcess()->GetTarget().CreateBreakpoint (m_backstop_addr, true).get(); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 60 | if (return_bp != NULL) |
| 61 | { |
| 62 | return_bp->SetThreadID(m_thread.GetID()); |
| 63 | m_backstop_bkpt_id = return_bp->GetID(); |
Jim Ingham | 090f831 | 2013-01-26 02:19:28 +0000 | [diff] [blame] | 64 | return_bp->SetBreakpointKind("step-through-backstop"); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 65 | } |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 66 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 67 | if (log) |
| 68 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 69 | log->Printf ("Setting backstop breakpoint %d at address: 0x%" PRIx64, m_backstop_bkpt_id, m_backstop_addr); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | ThreadPlanStepThrough::~ThreadPlanStepThrough () |
| 76 | { |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 77 | ClearBackstopBreakpoint (); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | ThreadPlanStepThrough::DidPush () |
| 82 | { |
| 83 | if (m_sub_plan_sp) |
| 84 | PushPlan(m_sub_plan_sp); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() |
| 89 | { |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 90 | m_sub_plan_sp = m_thread.GetProcess()->GetDynamicLoader()->GetStepThroughTrampolinePlan (m_thread, m_stop_others); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 91 | // If that didn't come up with anything, try the ObjC runtime plugin: |
| 92 | if (!m_sub_plan_sp.get()) |
| 93 | { |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 94 | ObjCLanguageRuntime *objc_runtime = m_thread.GetProcess()->GetObjCLanguageRuntime(); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 95 | if (objc_runtime) |
| 96 | m_sub_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (m_thread, m_stop_others); |
| 97 | } |
| 98 | |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 99 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 100 | if (log) |
| 101 | { |
| 102 | lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0); |
| 103 | if (m_sub_plan_sp) |
| 104 | { |
| 105 | StreamString s; |
| 106 | m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 107 | log->Printf ("Found step through plan from 0x%" PRIx64 ": %s", current_address, s.GetData()); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 111 | log->Printf ("Couldn't find step through plan from address 0x%" PRIx64 ".", current_address); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | ThreadPlanStepThrough::GetDescription (Stream *s, lldb::DescriptionLevel level) |
| 118 | { |
| 119 | if (level == lldb::eDescriptionLevelBrief) |
| 120 | s->Printf ("Step through"); |
| 121 | else |
| 122 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 123 | s->PutCString ("Stepping through trampoline code from: "); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | s->Address(m_start_address, sizeof (addr_t)); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 125 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) |
| 126 | { |
| 127 | s->Printf (" with backstop breakpoint id: %d at address: ", m_backstop_bkpt_id); |
| 128 | s->Address (m_backstop_addr, sizeof (addr_t)); |
| 129 | } |
| 130 | else |
| 131 | s->PutCString (" unable to set a backstop breakpoint."); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | bool |
| 136 | ThreadPlanStepThrough::ValidatePlan (Stream *error) |
| 137 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 138 | return m_sub_plan_sp.get() != NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool |
Jim Ingham | 7c79a27 | 2013-05-08 00:35:16 +0000 | [diff] [blame^] | 142 | ThreadPlanStepThrough::DoPlanExplainsStop (Event *event_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 144 | // If we have a sub-plan, it will have been asked first if we explain the stop, and |
| 145 | // we won't get asked. The only time we would be the one directly asked this question |
| 146 | // is if we hit our backstop breakpoint. |
| 147 | |
| 148 | if (HitOurBackstopBreakpoint()) |
| 149 | return true; |
| 150 | else |
| 151 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool |
| 155 | ThreadPlanStepThrough::ShouldStop (Event *event_ptr) |
| 156 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 157 | // If we've already marked ourselves done, then we're done... |
| 158 | if (IsPlanComplete()) |
| 159 | return true; |
| 160 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 161 | // First, did we hit the backstop breakpoint? |
| 162 | if (HitOurBackstopBreakpoint()) |
| 163 | { |
| 164 | SetPlanComplete(false); |
| 165 | return true; |
| 166 | } |
| 167 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 168 | // If we don't have a sub-plan, then we're also done (can't see how we would ever get here |
| 169 | // without a plan, but just in case. |
| 170 | |
| 171 | if (!m_sub_plan_sp) |
| 172 | { |
| 173 | SetPlanComplete(); |
| 174 | return true; |
| 175 | } |
| 176 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 177 | // If the current sub plan is not done, we don't want to stop. Actually, we probably won't |
| 178 | // ever get here in this state, since we generally won't get asked any questions if out |
| 179 | // current sub-plan is not done... |
| 180 | if (!m_sub_plan_sp->IsPlanComplete()) |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 181 | return false; |
| 182 | |
| 183 | // If our current sub plan failed, then let's just run to our backstop. If we can't do that then just stop. |
| 184 | if (!m_sub_plan_sp->PlanSucceeded()) |
| 185 | { |
| 186 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) |
| 187 | { |
| 188 | m_sub_plan_sp.reset(); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 189 | return false; |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
| 193 | SetPlanComplete(false); |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 198 | // Next see if there is a specific step through plan at our current pc (these might |
| 199 | // chain, for instance stepping through a dylib trampoline to the objc dispatch function...) |
| 200 | LookForPlanToStepThroughFromCurrentPC(); |
| 201 | if (m_sub_plan_sp) |
| 202 | { |
| 203 | PushPlan (m_sub_plan_sp); |
| 204 | return false; |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | SetPlanComplete(); |
| 209 | return true; |
| 210 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool |
| 214 | ThreadPlanStepThrough::StopOthers () |
| 215 | { |
| 216 | return m_stop_others; |
| 217 | } |
| 218 | |
| 219 | StateType |
Jim Ingham | 745ac7a | 2010-11-11 19:26:09 +0000 | [diff] [blame] | 220 | ThreadPlanStepThrough::GetPlanRunState () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | { |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 222 | return eStateRunning; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool |
Jim Ingham | 7c79a27 | 2013-05-08 00:35:16 +0000 | [diff] [blame^] | 226 | ThreadPlanStepThrough::DoWillResume (StateType resume_state, bool current_plan) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool |
| 232 | ThreadPlanStepThrough::WillStop () |
| 233 | { |
| 234 | return true; |
| 235 | } |
| 236 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 237 | void |
| 238 | ThreadPlanStepThrough::ClearBackstopBreakpoint () |
| 239 | { |
| 240 | if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) |
| 241 | { |
| 242 | m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id); |
| 243 | m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID; |
| 244 | } |
| 245 | } |
| 246 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | bool |
| 248 | ThreadPlanStepThrough::MischiefManaged () |
| 249 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 250 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 252 | if (!IsPlanComplete()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | if (log) |
| 259 | log->Printf("Completed step through step plan."); |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 260 | |
| 261 | ClearBackstopBreakpoint (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | ThreadPlan::MischiefManaged (); |
| 263 | return true; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bool |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 268 | ThreadPlanStepThrough::HitOurBackstopBreakpoint() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | { |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 270 | StopInfoSP stop_info_sp(m_thread.GetStopInfo()); |
| 271 | if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) |
| 272 | { |
| 273 | break_id_t stop_value = (break_id_t) stop_info_sp->GetValue(); |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 274 | BreakpointSiteSP cur_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 275 | if (cur_site_sp && cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) |
| 276 | { |
Jim Ingham | 41ce4f1 | 2012-03-01 20:01:22 +0000 | [diff] [blame] | 277 | StackID cur_frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); |
| 278 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 279 | if (cur_frame_zero_id == m_return_stack_id) |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 280 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 281 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 282 | if (log) |
| 283 | log->PutCString ("ThreadPlanStepThrough hit backstop breakpoint."); |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |