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