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