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