1 //===-- ThreadPlanShouldStopHere.cpp ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Core/Log.h"
15 #include "lldb/Symbol/Symbol.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Target/ThreadPlanShouldStopHere.h"
21 using namespace lldb_private;
23 //----------------------------------------------------------------------
24 // ThreadPlanShouldStopHere constructor
25 //----------------------------------------------------------------------
26 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(ThreadPlan *owner) :
30 m_flags(ThreadPlanShouldStopHere::eNone)
32 m_callbacks.should_stop_here_callback = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback;
33 m_callbacks.step_from_here_callback = ThreadPlanShouldStopHere::DefaultStepFromHereCallback;
36 ThreadPlanShouldStopHere::ThreadPlanShouldStopHere(ThreadPlan *owner, const ThreadPlanShouldStopHereCallbacks *callbacks, void *baton) :
40 m_flags (ThreadPlanShouldStopHere::eNone)
42 SetShouldStopHereCallbacks(callbacks, baton);
45 ThreadPlanShouldStopHere::~ThreadPlanShouldStopHere() = default;
48 ThreadPlanShouldStopHere::InvokeShouldStopHereCallback (FrameComparison operation)
50 bool should_stop_here = true;
51 if (m_callbacks.should_stop_here_callback)
53 should_stop_here = m_callbacks.should_stop_here_callback (m_owner, m_flags, operation, m_baton);
54 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
57 lldb::addr_t current_addr = m_owner->GetThread().GetRegisterContext()->GetPC(0);
59 log->Printf ("ShouldStopHere callback returned %u from 0x%" PRIx64 ".", should_stop_here, current_addr);
63 return should_stop_here;
67 ThreadPlanShouldStopHere::DefaultShouldStopHereCallback (ThreadPlan *current_plan,
69 FrameComparison operation,
72 bool should_stop_here = true;
73 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
77 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
79 if ((operation == eFrameCompareOlder && flags.Test(eStepOutAvoidNoDebug))
80 || (operation == eFrameCompareYounger && flags.Test(eStepInAvoidNoDebug))
81 || (operation == eFrameCompareSameParent && flags.Test(eStepInAvoidNoDebug)))
83 if (!frame->HasDebugInformation())
86 log->Printf ("Stepping out of frame with no debug info");
88 should_stop_here = false;
92 // Always avoid code with line number 0.
93 // FIXME: At present the ShouldStop and the StepFromHere calculate this independently. If this ever
94 // becomes expensive (this one isn't) we can try to have this set a state that the StepFromHere can use.
98 sc = frame->GetSymbolContext (eSymbolContextLineEntry);
99 if (sc.line_entry.line == 0)
100 should_stop_here = false;
103 return should_stop_here;
107 ThreadPlanShouldStopHere::DefaultStepFromHereCallback (ThreadPlan *current_plan,
109 FrameComparison operation,
112 const bool stop_others = false;
113 const size_t frame_index = 0;
114 ThreadPlanSP return_plan_sp;
115 // If we are stepping through code at line number 0, then we need to step over this range. Otherwise
117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
119 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
121 return return_plan_sp;
123 sc = frame->GetSymbolContext (eSymbolContextLineEntry|eSymbolContextSymbol);
125 if (sc.line_entry.line == 0)
127 AddressRange range = sc.line_entry.range;
129 // If the whole function is marked line 0 just step out, that's easier & faster than continuing
130 // to step through it.
131 bool just_step_out = false;
132 if (sc.symbol && sc.symbol->ValueIsAddress())
134 Address symbol_end = sc.symbol->GetAddress();
135 symbol_end.Slide(sc.symbol->GetByteSize() - 1);
136 if (range.ContainsFileAddress(sc.symbol->GetAddress()) && range.ContainsFileAddress(symbol_end))
139 log->Printf("Stopped in a function with only line 0 lines, just stepping out.");
140 just_step_out = true;
146 log->Printf ("ThreadPlanShouldStopHere::DefaultStepFromHereCallback Queueing StepInRange plan to step through line 0 code.");
148 return_plan_sp = current_plan->GetThread().QueueThreadPlanForStepInRange(false,
159 return_plan_sp = current_plan->GetThread().QueueThreadPlanForStepOutNoShouldStop(false,
167 return return_plan_sp;
171 ThreadPlanShouldStopHere::QueueStepOutFromHerePlan(lldb_private::Flags &flags, lldb::FrameComparison operation)
173 ThreadPlanSP return_plan_sp;
174 if (m_callbacks.step_from_here_callback)
176 return_plan_sp = m_callbacks.step_from_here_callback (m_owner, flags, operation, m_baton);
178 return return_plan_sp;
182 ThreadPlanShouldStopHere::CheckShouldStopHereAndQueueStepOut (lldb::FrameComparison operation)
184 if (!InvokeShouldStopHereCallback(operation))
185 return QueueStepOutFromHerePlan(m_flags, operation);
187 return ThreadPlanSP();