Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepOverRange.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/ThreadPlanStepOverRange.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | |
| 17 | #include "lldb/lldb-private-log.h" |
| 18 | #include "lldb/Core/Log.h" |
| 19 | #include "lldb/Core/Stream.h" |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 20 | #include "lldb/Symbol/Block.h" |
| 21 | #include "lldb/Symbol/Function.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Process.h" |
| 23 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Thread.h" |
| 26 | #include "lldb/Target/ThreadPlanStepOut.h" |
| 27 | #include "lldb/Target/ThreadPlanStepThrough.h" |
| 28 | |
| 29 | using namespace lldb_private; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 30 | using namespace lldb; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | //---------------------------------------------------------------------- |
| 34 | // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into |
| 35 | // based on the value of \a type. |
| 36 | //---------------------------------------------------------------------- |
| 37 | |
| 38 | ThreadPlanStepOverRange::ThreadPlanStepOverRange |
| 39 | ( |
| 40 | Thread &thread, |
| 41 | const AddressRange &range, |
| 42 | const SymbolContext &addr_context, |
Jim Ingham | 88e3de2 | 2012-05-03 21:19:36 +0000 | [diff] [blame] | 43 | lldb::RunMode stop_others |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | ) : |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 45 | ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others), |
| 46 | m_first_resume(true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ThreadPlanStepOverRange::~ThreadPlanStepOverRange () |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level) |
| 56 | { |
| 57 | if (level == lldb::eDescriptionLevelBrief) |
| 58 | s->Printf("step over"); |
| 59 | else |
| 60 | { |
| 61 | s->Printf ("stepping through range (stepping over functions): "); |
Jim Ingham | 76e55f7 | 2011-10-15 00:24:48 +0000 | [diff] [blame] | 62 | DumpRanges(s); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | bool |
| 67 | ThreadPlanStepOverRange::ShouldStop (Event *event_ptr) |
| 68 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 69 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
| 71 | if (log) |
| 72 | { |
| 73 | StreamString s; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 74 | s.Address (m_thread.GetRegisterContext()->GetPC(), |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 75 | m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData()); |
| 77 | } |
| 78 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | // If we're out of the range but in the same frame or in our caller's frame |
| 80 | // then we should stop. |
| 81 | // When stepping out we only step if we are forcing running one thread. |
| 82 | bool stop_others; |
| 83 | if (m_stop_others == lldb::eOnlyThisThread) |
| 84 | stop_others = true; |
| 85 | else |
| 86 | stop_others = false; |
| 87 | |
| 88 | ThreadPlan* new_plan = NULL; |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 89 | |
| 90 | FrameComparison frame_order = CompareCurrentFrameToStartFrame(); |
| 91 | |
| 92 | if (frame_order == eFrameCompareOlder) |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 93 | { |
| 94 | // If we're in an older frame then we should stop. |
| 95 | // |
| 96 | // A caveat to this is if we think the frame is older but we're actually in a trampoline. |
| 97 | // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are |
| 98 | // in a trampoline we think the frame is older because the trampoline confused the backtracer. |
| 99 | // As below, we step through first, and then try to figure out how to get back out again. |
| 100 | |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 101 | new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 102 | |
| 103 | if (new_plan != NULL && log) |
| 104 | log->Printf("Thought I stepped out, but in fact arrived at a trampoline."); |
| 105 | } |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 106 | else if (frame_order == eFrameCompareYounger) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | { |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 108 | // Make sure we really are in a new frame. Do that by unwinding and seeing if the |
| 109 | // start function really is our start function... |
| 110 | StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1); |
| 111 | |
| 112 | // But if we can't even unwind one frame we should just get out of here & stop... |
| 113 | if (older_frame_sp) |
| 114 | { |
| 115 | const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything); |
Jim Ingham | c3ba2af | 2012-07-26 18:23:21 +0000 | [diff] [blame] | 116 | |
| 117 | // Match as much as is specified in the m_addr_context: |
| 118 | // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled |
| 119 | // in so I left out the target check. And sometimes the module comes in as the .o file from the |
| 120 | // inlined range, so I left that out too... |
| 121 | |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 122 | bool older_ctx_is_equivalent = true; |
Jim Ingham | c3ba2af | 2012-07-26 18:23:21 +0000 | [diff] [blame] | 123 | if (m_addr_context.comp_unit) |
| 124 | { |
| 125 | if (m_addr_context.comp_unit == older_context.comp_unit) |
| 126 | { |
| 127 | if (m_addr_context.function && m_addr_context.function == older_context.function) |
| 128 | { |
| 129 | if (m_addr_context.block && m_addr_context.block == older_context.block) |
| 130 | { |
| 131 | older_ctx_is_equivalent = true; |
Jim Ingham | c3ba2af | 2012-07-26 18:23:21 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | else if (m_addr_context.symbol && m_addr_context.symbol == older_context.symbol) |
| 137 | { |
| 138 | older_ctx_is_equivalent = true; |
| 139 | } |
| 140 | |
| 141 | if (older_ctx_is_equivalent) |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 142 | { |
| 143 | new_plan = m_thread.QueueThreadPlanForStepOut (false, |
| 144 | NULL, |
| 145 | true, |
| 146 | stop_others, |
| 147 | eVoteNo, |
| 148 | eVoteNoOpinion, |
| 149 | 0); |
| 150 | } |
| 151 | else |
| 152 | { |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 153 | new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 154 | |
| 155 | } |
| 156 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | } |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 158 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | { |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 160 | // If we're still in the range, keep going. |
| 161 | if (InRange()) |
| 162 | { |
| 163 | SetNextBranchBreakpoint(); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | if (!InSymbol()) |
| 169 | { |
| 170 | // This one is a little tricky. Sometimes we may be in a stub or something similar, |
| 171 | // in which case we need to get out of there. But if we are in a stub then it's |
| 172 | // likely going to be hard to get out from here. It is probably easiest to step into the |
| 173 | // stub, and then it will be straight-forward to step out. |
Jim Ingham | 038fa8e | 2012-05-10 01:35:39 +0000 | [diff] [blame] | 174 | new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 178 | // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it: |
| 179 | ClearNextBranchBreakpoint(); |
| 180 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | if (new_plan == NULL) |
| 182 | m_no_more_plans = true; |
| 183 | else |
| 184 | m_no_more_plans = false; |
| 185 | |
| 186 | if (new_plan == NULL) |
| 187 | { |
| 188 | // For efficiencies sake, we know we're done here so we don't have to do this |
| 189 | // calculation again in MischiefManaged. |
| 190 | SetPlanComplete(); |
| 191 | return true; |
| 192 | } |
| 193 | else |
| 194 | return false; |
| 195 | } |
Jim Ingham | 707b7a8 | 2012-05-01 18:38:37 +0000 | [diff] [blame] | 196 | |
| 197 | bool |
| 198 | ThreadPlanStepOverRange::PlanExplainsStop () |
| 199 | { |
| 200 | // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us) |
| 201 | // handle the stop. That way the user can see the stop, step around, and then when they |
| 202 | // are done, continue and have their step complete. The exception is if we've hit our |
| 203 | // "run to next branch" breakpoint. |
| 204 | // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an |
| 205 | // unexplained breakpoint/crash. |
| 206 | |
| 207 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 208 | StopInfoSP stop_info_sp = GetPrivateStopReason(); |
| 209 | if (stop_info_sp) |
| 210 | { |
| 211 | StopReason reason = stop_info_sp->GetStopReason(); |
| 212 | |
| 213 | switch (reason) |
| 214 | { |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 215 | case eStopReasonTrace: |
| 216 | return true; |
| 217 | break; |
Jim Ingham | 707b7a8 | 2012-05-01 18:38:37 +0000 | [diff] [blame] | 218 | case eStopReasonBreakpoint: |
| 219 | if (NextRangeBreakpointExplainsStop(stop_info_sp)) |
| 220 | return true; |
| 221 | else |
| 222 | return false; |
| 223 | break; |
| 224 | case eStopReasonWatchpoint: |
| 225 | case eStopReasonSignal: |
| 226 | case eStopReasonException: |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 227 | default: |
Jim Ingham | 707b7a8 | 2012-05-01 18:38:37 +0000 | [diff] [blame] | 228 | if (log) |
| 229 | log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step."); |
| 230 | return false; |
| 231 | break; |
Jim Ingham | 707b7a8 | 2012-05-01 18:38:37 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | return true; |
| 235 | } |
Jim Ingham | 0c8fa2d | 2012-09-01 01:02:41 +0000 | [diff] [blame^] | 236 | |
| 237 | bool |
| 238 | ThreadPlanStepOverRange::WillResume (lldb::StateType resume_state, bool current_plan) |
| 239 | { |
| 240 | if (resume_state != eStateSuspended && m_first_resume) |
| 241 | { |
| 242 | m_first_resume = false; |
| 243 | if (resume_state == eStateStepping && current_plan) |
| 244 | { |
| 245 | // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure |
| 246 | // out its extents and reset our range to step over that. |
| 247 | bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth(); |
| 248 | if (in_inlined_stack) |
| 249 | { |
| 250 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 251 | if (log) |
| 252 | log->Printf ("ThreadPlanStepInRange::WillResume: adjusting range to the frame at inlined depth %d.", |
| 253 | m_thread.GetCurrentInlinedDepth()); |
| 254 | StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0); |
| 255 | if (stack_sp) |
| 256 | { |
| 257 | Block *frame_block = stack_sp->GetFrameBlock(); |
| 258 | lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); |
| 259 | AddressRange my_range; |
| 260 | if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range)) |
| 261 | { |
| 262 | m_address_ranges.clear(); |
| 263 | m_address_ranges.push_back(my_range); |
| 264 | if (log) |
| 265 | { |
| 266 | StreamString s; |
| 267 | const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo(); |
| 268 | const char *name; |
| 269 | if (inline_info) |
| 270 | name = inline_info->GetName().AsCString(); |
| 271 | else |
| 272 | name = "<unknown-notinlined>"; |
| 273 | |
| 274 | s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name); |
| 275 | DumpRanges(&s); |
| 276 | log->PutCString(s.GetData()); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return ThreadPlan::WillResume(resume_state, current_plan); |
| 286 | } |