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