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