Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepInRange.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/ThreadPlanStepInRange.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 | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/Symbol.h" |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 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" |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 28 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug; |
| 34 | |
| 35 | //---------------------------------------------------------------------- |
| 36 | // ThreadPlanStepInRange: Step through a stack range, either stepping over or into |
| 37 | // based on the value of \a type. |
| 38 | //---------------------------------------------------------------------- |
| 39 | |
| 40 | ThreadPlanStepInRange::ThreadPlanStepInRange |
| 41 | ( |
| 42 | Thread &thread, |
| 43 | const AddressRange &range, |
| 44 | const SymbolContext &addr_context, |
| 45 | lldb::RunMode stop_others |
| 46 | ) : |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 47 | ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others), |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 48 | ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL), |
| 49 | m_step_past_prologue (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | { |
| 51 | SetFlagsToDefault (); |
| 52 | } |
| 53 | |
| 54 | ThreadPlanStepInRange::~ThreadPlanStepInRange () |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level) |
| 60 | { |
| 61 | if (level == lldb::eDescriptionLevelBrief) |
| 62 | s->Printf("step in"); |
| 63 | else |
| 64 | { |
| 65 | s->Printf ("Stepping through range (stepping into functions): "); |
Jim Ingham | 76e55f7 | 2011-10-15 00:24:48 +0000 | [diff] [blame] | 66 | DumpRanges(s); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | bool |
| 71 | ThreadPlanStepInRange::ShouldStop (Event *event_ptr) |
| 72 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 73 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | m_no_more_plans = false; |
| 75 | |
| 76 | if (log) |
| 77 | { |
| 78 | StreamString s; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 79 | s.Address (m_thread.GetRegisterContext()->GetPC(), |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 80 | m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | log->Printf("ThreadPlanStepInRange reached %s.", s.GetData()); |
| 82 | } |
| 83 | |
Jim Ingham | ad382c5 | 2011-12-03 01:52:59 +0000 | [diff] [blame] | 84 | if (IsPlanComplete()) |
| 85 | return true; |
| 86 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | ThreadPlan* new_plan = NULL; |
| 88 | |
Jim Ingham | 81cfc99 | 2010-07-14 02:25:06 +0000 | [diff] [blame] | 89 | // Stepping through should be done stopping other threads in general, since we're setting a breakpoint and |
| 90 | // continuing... |
| 91 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | bool stop_others; |
Jim Ingham | 81cfc99 | 2010-07-14 02:25:06 +0000 | [diff] [blame] | 93 | if (m_stop_others != lldb::eAllThreads) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | stop_others = true; |
| 95 | else |
| 96 | stop_others = false; |
| 97 | |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 98 | FrameComparison frame_order = CompareCurrentFrameToStartFrame(); |
| 99 | |
| 100 | if (frame_order == eFrameCompareOlder) |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 101 | { |
| 102 | // If we're in an older frame then we should stop. |
| 103 | // |
| 104 | // A caveat to this is if we think the frame is older but we're actually in a trampoline. |
| 105 | // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are |
| 106 | // in a trampoline we think the frame is older because the trampoline confused the backtracer. |
| 107 | new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others); |
| 108 | if (new_plan == NULL) |
| 109 | return true; |
| 110 | else if (log) |
| 111 | { |
| 112 | log->Printf("Thought I stepped out, but in fact arrived at a trampoline."); |
| 113 | } |
| 114 | |
| 115 | } |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 116 | else if (frame_order == eFrameCompareEqual && InSymbol()) |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 117 | { |
| 118 | // If we are not in a place we should step through, we're done. |
| 119 | // One tricky bit here is that some stubs don't push a frame, so we have to check |
| 120 | // both the case of a frame that is younger, or the same as this frame. |
| 121 | // However, if the frame is the same, and we are still in the symbol we started |
| 122 | // in, the we don't need to do this. This first check isn't strictly necessary, |
| 123 | // but it is more efficient. |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 124 | |
| 125 | // If we're still in the range, keep going, either by running to the next branch breakpoint, or by |
| 126 | // stepping. |
| 127 | if (InRange()) |
| 128 | { |
| 129 | SetNextBranchBreakpoint(); |
| 130 | return false; |
| 131 | } |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 132 | |
| 133 | SetPlanComplete(); |
| 134 | return true; |
| 135 | } |
| 136 | |
Jim Ingham | b2cf58a | 2012-03-09 04:10:47 +0000 | [diff] [blame] | 137 | // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it: |
| 138 | ClearNextBranchBreakpoint(); |
| 139 | |
Jim Ingham | 4f385f1 | 2010-11-05 00:18:21 +0000 | [diff] [blame] | 140 | // We may have set the plan up above in the FrameIsOlder section: |
| 141 | |
| 142 | if (new_plan == NULL) |
| 143 | new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others); |
Jim Ingham | 17454cf | 2010-09-14 22:03:00 +0000 | [diff] [blame] | 144 | |
| 145 | if (log) |
| 146 | { |
| 147 | if (new_plan != NULL) |
| 148 | log->Printf ("Found a step through plan: %s", new_plan->GetName()); |
| 149 | else |
| 150 | log->Printf ("No step through plan found."); |
| 151 | } |
| 152 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | // If not, give the "should_stop" callback a chance to push a plan to get us out of here. |
| 154 | // But only do that if we actually have stepped in. |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 155 | if (!new_plan && frame_order == eFrameCompareYounger) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | new_plan = InvokeShouldStopHereCallback(); |
| 157 | |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 158 | // If we've stepped in and we are going to stop here, check to see if we were asked to |
| 159 | // run past the prologue, and if so do that. |
| 160 | |
Jim Ingham | 441e3b9 | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 161 | if (new_plan == NULL && frame_order == eFrameCompareYounger && m_step_past_prologue) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | { |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 163 | lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0); |
| 164 | if (curr_frame) |
| 165 | { |
| 166 | size_t bytes_to_skip = 0; |
| 167 | lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC(); |
| 168 | Address func_start_address; |
| 169 | |
| 170 | SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol); |
| 171 | |
| 172 | if (sc.function) |
| 173 | { |
| 174 | func_start_address = sc.function->GetAddressRange().GetBaseAddress(); |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 175 | if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get())) |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 176 | bytes_to_skip = sc.function->GetPrologueByteSize(); |
| 177 | } |
| 178 | else if (sc.symbol) |
| 179 | { |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 180 | func_start_address = sc.symbol->GetAddress(); |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 181 | if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get())) |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 182 | bytes_to_skip = sc.symbol->GetPrologueByteSize(); |
| 183 | } |
| 184 | |
| 185 | if (bytes_to_skip != 0) |
| 186 | { |
| 187 | func_start_address.Slide (bytes_to_skip); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 188 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 189 | if (log) |
| 190 | log->Printf ("Pushing past prologue "); |
| 191 | |
| 192 | new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (new_plan == NULL) |
| 198 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | m_no_more_plans = true; |
| 200 | SetPlanComplete(); |
| 201 | return true; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | m_no_more_plans = false; |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | ThreadPlanStepInRange::SetFlagsToDefault () |
| 212 | { |
| 213 | GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values); |
| 214 | } |
| 215 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 216 | void |
| 217 | ThreadPlanStepInRange::SetAvoidRegexp(const char *name) |
| 218 | { |
| 219 | if (m_avoid_regexp_ap.get() == NULL) |
| 220 | m_avoid_regexp_ap.reset (new RegularExpression(name)); |
| 221 | |
| 222 | m_avoid_regexp_ap->Compile (name); |
| 223 | } |
| 224 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | void |
| 226 | ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value) |
| 227 | { |
| 228 | // TODO: Should we test this for sanity? |
| 229 | ThreadPlanStepInRange::s_default_flag_values = new_value; |
| 230 | } |
| 231 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 232 | bool |
| 233 | ThreadPlanStepInRange::FrameMatchesAvoidRegexp () |
| 234 | { |
| 235 | StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get(); |
| 236 | |
Jim Ingham | 20594b1 | 2010-09-08 03:14:33 +0000 | [diff] [blame] | 237 | RegularExpression *avoid_regexp_to_use; |
| 238 | |
| 239 | avoid_regexp_to_use = m_avoid_regexp_ap.get(); |
| 240 | if (avoid_regexp_to_use == NULL) |
| 241 | avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp(); |
| 242 | |
| 243 | if (avoid_regexp_to_use != NULL) |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 244 | { |
| 245 | SymbolContext sc = frame->GetSymbolContext(eSymbolContextSymbol); |
| 246 | if (sc.symbol != NULL) |
| 247 | { |
| 248 | const char *unnamed_symbol = "<UNKNOWN>"; |
| 249 | const char *sym_name = sc.symbol->GetMangled().GetName().AsCString(unnamed_symbol); |
| 250 | if (strcmp (sym_name, unnamed_symbol) != 0) |
Jim Ingham | 20594b1 | 2010-09-08 03:14:33 +0000 | [diff] [blame] | 251 | return avoid_regexp_to_use->Execute(sym_name); |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | ThreadPlan * |
| 258 | ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton) |
| 259 | { |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 260 | bool should_step_out = false; |
| 261 | StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get(); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 262 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 263 | |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 264 | if (flags.Test(eAvoidNoDebug)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | if (!frame->HasDebugInformation()) |
Jim Ingham | e4b8aeb | 2010-09-15 00:06:51 +0000 | [diff] [blame] | 267 | { |
| 268 | if (log) |
| 269 | log->Printf ("Stepping out of frame with no debug info"); |
| 270 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 271 | should_step_out = true; |
Jim Ingham | e4b8aeb | 2010-09-15 00:06:51 +0000 | [diff] [blame] | 272 | } |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if (!should_step_out) |
| 276 | { |
| 277 | if (current_plan->GetKind() == eKindStepInRange) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | { |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 279 | ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan); |
| 280 | should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 283 | |
| 284 | if (should_step_out) |
| 285 | { |
| 286 | // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions. |
Greg Clayton | 1ebdcc7 | 2011-01-21 06:11:58 +0000 | [diff] [blame] | 287 | return current_plan->GetThread().QueueThreadPlanForStepOut (false, |
| 288 | NULL, |
| 289 | true, |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 290 | current_plan->StopOthers(), |
Greg Clayton | 1ebdcc7 | 2011-01-21 06:11:58 +0000 | [diff] [blame] | 291 | eVoteNo, |
| 292 | eVoteNoOpinion, |
| 293 | 0); // Frame index |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 294 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | |
| 296 | return NULL; |
| 297 | } |
Jim Ingham | 707b7a8 | 2012-05-01 18:38:37 +0000 | [diff] [blame^] | 298 | |
| 299 | bool |
| 300 | ThreadPlanStepInRange::PlanExplainsStop () |
| 301 | { |
| 302 | // We always explain a stop. Either we've just done a single step, in which |
| 303 | // case we'll do our ordinary processing, or we stopped for some |
| 304 | // reason that isn't handled by our sub-plans, in which case we want to just stop right |
| 305 | // away. |
| 306 | // We also set ourselves complete when we stop for this sort of unintended reason, but mark |
| 307 | // success as false so we don't end up being the reason for the stop. |
| 308 | // |
| 309 | // The only variation is that if we are doing "step by running to next branch" in which case |
| 310 | // if we hit our branch breakpoint we don't set the plan to complete. |
| 311 | |
| 312 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 313 | StopInfoSP stop_info_sp = GetPrivateStopReason(); |
| 314 | if (stop_info_sp) |
| 315 | { |
| 316 | StopReason reason = stop_info_sp->GetStopReason(); |
| 317 | |
| 318 | switch (reason) |
| 319 | { |
| 320 | case eStopReasonBreakpoint: |
| 321 | if (NextRangeBreakpointExplainsStop(stop_info_sp)) |
| 322 | return true; |
| 323 | case eStopReasonWatchpoint: |
| 324 | case eStopReasonSignal: |
| 325 | case eStopReasonException: |
| 326 | if (log) |
| 327 | log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step."); |
| 328 | SetPlanComplete(false); |
| 329 | break; |
| 330 | default: |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | return true; |
| 335 | } |