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" 20 #include "lldb/Symbol/Block.h" 21 #include "lldb/Symbol/CompileUnit.h" 22 #include "lldb/Symbol/Function.h" 23 #include "lldb/Symbol/LineTable.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/RegisterContext.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Target/Thread.h" 28 #include "lldb/Target/ThreadPlanStepOut.h" 29 #include "lldb/Target/ThreadPlanStepThrough.h" 30 31 using namespace lldb_private; 32 using namespace lldb; 33 34 uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0; 35 36 //---------------------------------------------------------------------- 37 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into 38 // based on the value of \a type. 39 //---------------------------------------------------------------------- 40 41 ThreadPlanStepOverRange::ThreadPlanStepOverRange 42 ( 43 Thread &thread, 44 const AddressRange &range, 45 const SymbolContext &addr_context, 46 lldb::RunMode stop_others, 47 LazyBool step_out_avoids_code_without_debug_info 48 ) : 49 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others), 50 ThreadPlanShouldStopHere (this), 51 m_first_resume(true) 52 { 53 SetFlagsToDefault(); 54 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info); 55 } 56 57 ThreadPlanStepOverRange::~ThreadPlanStepOverRange () 58 { 59 } 60 61 void 62 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level) 63 { 64 if (level == lldb::eDescriptionLevelBrief) 65 { 66 s->Printf("step over"); 67 return; 68 } 69 s->Printf ("Stepping over"); 70 bool printed_line_info = false; 71 if (m_addr_context.line_entry.IsValid()) 72 { 73 s->Printf (" line "); 74 m_addr_context.line_entry.DumpStopContext (s, false); 75 printed_line_info = true; 76 } 77 78 if (!printed_line_info || level == eDescriptionLevelVerbose) 79 { 80 s->Printf (" using ranges: "); 81 DumpRanges(s); 82 } 83 84 s->PutChar('.'); 85 } 86 87 void 88 ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info) 89 { 90 bool avoid_nodebug = true; 91 switch (step_out_avoids_code_without_debug_info) 92 { 93 case eLazyBoolYes: 94 avoid_nodebug = true; 95 break; 96 case eLazyBoolNo: 97 avoid_nodebug = false; 98 break; 99 case eLazyBoolCalculate: 100 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug(); 101 break; 102 } 103 if (avoid_nodebug) 104 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 105 else 106 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug); 107 // Step Over plans should always avoid no-debug on step in. Seems like you shouldn't 108 // have to say this, but a tail call looks more like a step in that a step out, so 109 // we want to catch this case. 110 GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug); 111 } 112 113 bool 114 ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context) 115 { 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 if (m_addr_context.comp_unit) 122 { 123 if (m_addr_context.comp_unit == context.comp_unit) 124 { 125 if (m_addr_context.function && m_addr_context.function == context.function) 126 { 127 if (m_addr_context.block && m_addr_context.block == context.block) 128 return true; 129 } 130 } 131 } 132 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol) 133 { 134 return true; 135 } 136 return false; 137 } 138 139 bool 140 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr) 141 { 142 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 143 144 if (log) 145 { 146 StreamString s; 147 s.Address (m_thread.GetRegisterContext()->GetPC(), 148 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize()); 149 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData()); 150 } 151 152 // If we're out of the range but in the same frame or in our caller's frame 153 // then we should stop. 154 // When stepping out we only stop others if we are forcing running one thread. 155 bool stop_others; 156 if (m_stop_others == lldb::eOnlyThisThread) 157 stop_others = true; 158 else 159 stop_others = false; 160 161 ThreadPlanSP new_plan_sp; 162 163 FrameComparison frame_order = CompareCurrentFrameToStartFrame(); 164 165 if (frame_order == eFrameCompareOlder) 166 { 167 // If we're in an older frame then we should stop. 168 // 169 // A caveat to this is if we think the frame is older but we're actually in a trampoline. 170 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are 171 // in a trampoline we think the frame is older because the trampoline confused the backtracer. 172 // As below, we step through first, and then try to figure out how to get back out again. 173 174 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); 175 176 if (new_plan_sp && log) 177 log->Printf("Thought I stepped out, but in fact arrived at a trampoline."); 178 } 179 else if (frame_order == eFrameCompareYounger) 180 { 181 // Make sure we really are in a new frame. Do that by unwinding and seeing if the 182 // start function really is our start function... 183 for(uint32_t i = 1;; ++i) 184 { 185 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i); 186 if (!older_frame_sp) { 187 // We can't unwind the next frame we should just get out of here & stop... 188 break; 189 } 190 191 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything); 192 if (IsEquivalentContext(older_context)) 193 { 194 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (false, 195 NULL, 196 true, 197 stop_others, 198 eVoteNo, 199 eVoteNoOpinion, 200 0); 201 break; 202 } 203 else 204 { 205 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); 206 // If we found a way through, then we should stop recursing. 207 if (new_plan_sp) 208 break; 209 } 210 } 211 } 212 else 213 { 214 // If we're still in the range, keep going. 215 if (InRange()) 216 { 217 SetNextBranchBreakpoint(); 218 return false; 219 } 220 221 222 if (!InSymbol()) 223 { 224 // This one is a little tricky. Sometimes we may be in a stub or something similar, 225 // in which case we need to get out of there. But if we are in a stub then it's 226 // likely going to be hard to get out from here. It is probably easiest to step into the 227 // stub, and then it will be straight-forward to step out. 228 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); 229 } 230 else 231 { 232 // The current clang (at least through 424) doesn't always get the address range for the 233 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says 234 // you are still in the source file of the inlining function. This is bad, because now you are missing 235 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get 236 // out of this function you will instead exit the containing function. 237 // To work around this, we check whether we are still in the source file we started in, and if not assume 238 // it is an error, and push a plan to get us out of this line and back to the containing file. 239 240 if (m_addr_context.line_entry.IsValid()) 241 { 242 SymbolContext sc; 243 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0); 244 sc = frame_sp->GetSymbolContext (eSymbolContextEverything); 245 if (sc.line_entry.IsValid()) 246 { 247 if (sc.line_entry.file != m_addr_context.line_entry.file 248 && sc.comp_unit == m_addr_context.comp_unit 249 && sc.function == m_addr_context.function) 250 { 251 // Okay, find the next occurance of this file in the line table: 252 LineTable *line_table = m_addr_context.comp_unit->GetLineTable(); 253 if (line_table) 254 { 255 Address cur_address = frame_sp->GetFrameCodeAddress(); 256 uint32_t entry_idx; 257 LineEntry line_entry; 258 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx)) 259 { 260 LineEntry next_line_entry; 261 bool step_past_remaining_inline = false; 262 if (entry_idx > 0) 263 { 264 // We require the the previous line entry and the current line entry come 265 // from the same file. 266 // The other requirement is that the previous line table entry be part of an 267 // inlined block, we don't want to step past cases where people have inlined 268 // some code fragment by using #include <source-fragment.c> directly. 269 LineEntry prev_line_entry; 270 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry) 271 && prev_line_entry.file == line_entry.file) 272 { 273 SymbolContext prev_sc; 274 Address prev_address = prev_line_entry.range.GetBaseAddress(); 275 prev_address.CalculateSymbolContext(&prev_sc); 276 if (prev_sc.block) 277 { 278 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock(); 279 if (inlined_block) 280 { 281 AddressRange inline_range; 282 inlined_block->GetRangeContainingAddress(prev_address, inline_range); 283 if (!inline_range.ContainsFileAddress(cur_address)) 284 { 285 286 step_past_remaining_inline = true; 287 } 288 289 } 290 } 291 } 292 } 293 294 if (step_past_remaining_inline) 295 { 296 uint32_t look_ahead_step = 1; 297 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry)) 298 { 299 // Make sure we haven't wandered out of the function we started from... 300 Address next_line_address = next_line_entry.range.GetBaseAddress(); 301 Function *next_line_function = next_line_address.CalculateSymbolContextFunction(); 302 if (next_line_function != m_addr_context.function) 303 break; 304 305 if (next_line_entry.file == m_addr_context.line_entry.file) 306 { 307 const bool abort_other_plans = false; 308 const bool stop_other_threads = false; 309 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans, 310 next_line_address, 311 stop_other_threads); 312 break; 313 } 314 look_ahead_step++; 315 } 316 } 317 } 318 } 319 } 320 } 321 } 322 } 323 } 324 325 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it: 326 ClearNextBranchBreakpoint(); 327 328 329 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback: 330 if (!new_plan_sp) 331 { 332 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order); 333 } 334 335 if (!new_plan_sp) 336 m_no_more_plans = true; 337 else 338 { 339 // Any new plan will be an implementation plan, so mark it private: 340 new_plan_sp->SetPrivate(true); 341 m_no_more_plans = false; 342 } 343 344 if (!new_plan_sp) 345 { 346 // For efficiencies sake, we know we're done here so we don't have to do this 347 // calculation again in MischiefManaged. 348 SetPlanComplete(); 349 return true; 350 } 351 else 352 return false; 353 } 354 355 bool 356 ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr) 357 { 358 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us) 359 // handle the stop. That way the user can see the stop, step around, and then when they 360 // are done, continue and have their step complete. The exception is if we've hit our 361 // "run to next branch" breakpoint. 362 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an 363 // unexplained breakpoint/crash. 364 365 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 366 StopInfoSP stop_info_sp = GetPrivateStopInfo (); 367 bool return_value; 368 369 if (stop_info_sp) 370 { 371 StopReason reason = stop_info_sp->GetStopReason(); 372 373 switch (reason) 374 { 375 case eStopReasonTrace: 376 return_value = true; 377 break; 378 case eStopReasonBreakpoint: 379 if (NextRangeBreakpointExplainsStop(stop_info_sp)) 380 return_value = true; 381 else 382 return_value = false; 383 break; 384 case eStopReasonWatchpoint: 385 case eStopReasonSignal: 386 case eStopReasonException: 387 case eStopReasonExec: 388 case eStopReasonThreadExiting: 389 default: 390 if (log) 391 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step."); 392 return_value = false; 393 break; 394 } 395 } 396 else 397 return_value = true; 398 399 return return_value; 400 } 401 402 bool 403 ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan) 404 { 405 if (resume_state != eStateSuspended && m_first_resume) 406 { 407 m_first_resume = false; 408 if (resume_state == eStateStepping && current_plan) 409 { 410 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure 411 // out its extents and reset our range to step over that. 412 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth(); 413 if (in_inlined_stack) 414 { 415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 416 if (log) 417 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.", 418 m_thread.GetCurrentInlinedDepth()); 419 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0); 420 if (stack_sp) 421 { 422 Block *frame_block = stack_sp->GetFrameBlock(); 423 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 424 AddressRange my_range; 425 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range)) 426 { 427 m_address_ranges.clear(); 428 m_address_ranges.push_back(my_range); 429 if (log) 430 { 431 StreamString s; 432 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo(); 433 const char *name; 434 if (inline_info) 435 name = inline_info->GetName().AsCString(); 436 else 437 name = "<unknown-notinlined>"; 438 439 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name); 440 DumpRanges(&s); 441 log->PutCString(s.GetData()); 442 } 443 } 444 445 } 446 } 447 } 448 } 449 450 return true; 451 } 452 453