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