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