Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepInRange.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/ThreadPlanStepInRange.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" |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/Symbol.h" |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/Function.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Process.h" |
| 23 | #include "lldb/Target/RegisterContext.h" |
| 24 | #include "lldb/Target/Thread.h" |
| 25 | #include "lldb/Target/ThreadPlanStepOut.h" |
| 26 | #include "lldb/Target/ThreadPlanStepThrough.h" |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 27 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug; |
| 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | // ThreadPlanStepInRange: Step through a stack range, either stepping over or into |
| 36 | // based on the value of \a type. |
| 37 | //---------------------------------------------------------------------- |
| 38 | |
| 39 | ThreadPlanStepInRange::ThreadPlanStepInRange |
| 40 | ( |
| 41 | Thread &thread, |
| 42 | const AddressRange &range, |
| 43 | const SymbolContext &addr_context, |
| 44 | lldb::RunMode stop_others |
| 45 | ) : |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 46 | ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others), |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 47 | ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL), |
| 48 | m_step_past_prologue (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | { |
| 50 | SetFlagsToDefault (); |
| 51 | } |
| 52 | |
| 53 | ThreadPlanStepInRange::~ThreadPlanStepInRange () |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level) |
| 59 | { |
| 60 | if (level == lldb::eDescriptionLevelBrief) |
| 61 | s->Printf("step in"); |
| 62 | else |
| 63 | { |
| 64 | s->Printf ("Stepping through range (stepping into functions): "); |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 65 | m_address_range.Dump (s, &m_thread.GetProcess().GetTarget(), Address::DumpStyleLoadAddress); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | bool |
| 70 | ThreadPlanStepInRange::ShouldStop (Event *event_ptr) |
| 71 | { |
| 72 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 73 | m_no_more_plans = false; |
| 74 | |
| 75 | if (log) |
| 76 | { |
| 77 | StreamString s; |
| 78 | s.Address (m_thread.GetRegisterContext()->GetPC(), m_thread.GetProcess().GetAddressByteSize()); |
| 79 | log->Printf("ThreadPlanStepInRange reached %s.", s.GetData()); |
| 80 | } |
| 81 | |
| 82 | // If we're still in the range, keep going. |
| 83 | if (InRange()) |
| 84 | return false; |
| 85 | |
| 86 | // If we're in an older frame then we should stop. |
| 87 | if (FrameIsOlder()) |
| 88 | return true; |
| 89 | |
| 90 | // See if we are in a place we should step through (i.e. a trampoline of some sort): |
| 91 | // One tricky bit here is that some stubs don't push a frame, so we have to check |
| 92 | // both the case of a frame that is younger, or the same as this frame. |
| 93 | // However, if the frame is the same, and we are still in the symbol we started |
| 94 | // in, the we don't need to do this. This first check isn't strictly necessary, |
| 95 | // but it is more efficient. |
| 96 | |
| 97 | if (!FrameIsYounger() && InSymbol()) |
| 98 | { |
| 99 | SetPlanComplete(); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | ThreadPlan* new_plan = NULL; |
| 104 | |
Jim Ingham | 81cfc99 | 2010-07-14 02:25:06 +0000 | [diff] [blame] | 105 | // Stepping through should be done stopping other threads in general, since we're setting a breakpoint and |
| 106 | // continuing... |
| 107 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | bool stop_others; |
Jim Ingham | 81cfc99 | 2010-07-14 02:25:06 +0000 | [diff] [blame] | 109 | if (m_stop_others != lldb::eAllThreads) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | stop_others = true; |
| 111 | else |
| 112 | stop_others = false; |
| 113 | |
| 114 | new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others); |
Jim Ingham | 17454cf | 2010-09-14 22:03:00 +0000 | [diff] [blame] | 115 | |
| 116 | if (log) |
| 117 | { |
| 118 | if (new_plan != NULL) |
| 119 | log->Printf ("Found a step through plan: %s", new_plan->GetName()); |
| 120 | else |
| 121 | log->Printf ("No step through plan found."); |
| 122 | } |
| 123 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | // If not, give the "should_stop" callback a chance to push a plan to get us out of here. |
| 125 | // But only do that if we actually have stepped in. |
| 126 | if (!new_plan && FrameIsYounger()) |
| 127 | new_plan = InvokeShouldStopHereCallback(); |
| 128 | |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 129 | // If we've stepped in and we are going to stop here, check to see if we were asked to |
| 130 | // run past the prologue, and if so do that. |
| 131 | |
| 132 | if (new_plan == NULL && FrameIsYounger() && m_step_past_prologue) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | { |
Jim Ingham | 0e81b64 | 2010-09-16 00:58:09 +0000 | [diff] [blame] | 134 | lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0); |
| 135 | if (curr_frame) |
| 136 | { |
| 137 | size_t bytes_to_skip = 0; |
| 138 | lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC(); |
| 139 | Address func_start_address; |
| 140 | |
| 141 | SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol); |
| 142 | |
| 143 | if (sc.function) |
| 144 | { |
| 145 | func_start_address = sc.function->GetAddressRange().GetBaseAddress(); |
| 146 | if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget())) |
| 147 | bytes_to_skip = sc.function->GetPrologueByteSize(); |
| 148 | } |
| 149 | else if (sc.symbol) |
| 150 | { |
| 151 | func_start_address = sc.symbol->GetValue(); |
| 152 | if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget())) |
| 153 | bytes_to_skip = sc.symbol->GetPrologueByteSize(); |
| 154 | } |
| 155 | |
| 156 | if (bytes_to_skip != 0) |
| 157 | { |
| 158 | func_start_address.Slide (bytes_to_skip); |
| 159 | if (log) |
| 160 | log->Printf ("Pushing past prologue "); |
| 161 | |
| 162 | new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (new_plan == NULL) |
| 168 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | m_no_more_plans = true; |
| 170 | SetPlanComplete(); |
| 171 | return true; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | m_no_more_plans = false; |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | ThreadPlanStepInRange::SetFlagsToDefault () |
| 182 | { |
| 183 | GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values); |
| 184 | } |
| 185 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 186 | void |
| 187 | ThreadPlanStepInRange::SetAvoidRegexp(const char *name) |
| 188 | { |
| 189 | if (m_avoid_regexp_ap.get() == NULL) |
| 190 | m_avoid_regexp_ap.reset (new RegularExpression(name)); |
| 191 | |
| 192 | m_avoid_regexp_ap->Compile (name); |
| 193 | } |
| 194 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | void |
| 196 | ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value) |
| 197 | { |
| 198 | // TODO: Should we test this for sanity? |
| 199 | ThreadPlanStepInRange::s_default_flag_values = new_value; |
| 200 | } |
| 201 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 202 | bool |
| 203 | ThreadPlanStepInRange::FrameMatchesAvoidRegexp () |
| 204 | { |
| 205 | StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get(); |
| 206 | |
Jim Ingham | 20594b1 | 2010-09-08 03:14:33 +0000 | [diff] [blame] | 207 | RegularExpression *avoid_regexp_to_use; |
| 208 | |
| 209 | avoid_regexp_to_use = m_avoid_regexp_ap.get(); |
| 210 | if (avoid_regexp_to_use == NULL) |
| 211 | avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp(); |
| 212 | |
| 213 | if (avoid_regexp_to_use != NULL) |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 214 | { |
| 215 | SymbolContext sc = frame->GetSymbolContext(eSymbolContextSymbol); |
| 216 | if (sc.symbol != NULL) |
| 217 | { |
| 218 | const char *unnamed_symbol = "<UNKNOWN>"; |
| 219 | const char *sym_name = sc.symbol->GetMangled().GetName().AsCString(unnamed_symbol); |
| 220 | if (strcmp (sym_name, unnamed_symbol) != 0) |
Jim Ingham | 20594b1 | 2010-09-08 03:14:33 +0000 | [diff] [blame] | 221 | return avoid_regexp_to_use->Execute(sym_name); |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | ThreadPlan * |
| 228 | ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton) |
| 229 | { |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 230 | bool should_step_out = false; |
| 231 | StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get(); |
Jim Ingham | e4b8aeb | 2010-09-15 00:06:51 +0000 | [diff] [blame] | 232 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | if (flags.IsSet(eAvoidNoDebug)) |
| 235 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | if (!frame->HasDebugInformation()) |
Jim Ingham | e4b8aeb | 2010-09-15 00:06:51 +0000 | [diff] [blame] | 237 | { |
| 238 | if (log) |
| 239 | log->Printf ("Stepping out of frame with no debug info"); |
| 240 | |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 241 | should_step_out = true; |
Jim Ingham | e4b8aeb | 2010-09-15 00:06:51 +0000 | [diff] [blame] | 242 | } |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (!should_step_out) |
| 246 | { |
| 247 | if (current_plan->GetKind() == eKindStepInRange) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | { |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 249 | ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan); |
| 250 | should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
Jim Ingham | 809ab9b | 2010-07-10 02:27:39 +0000 | [diff] [blame] | 253 | |
| 254 | if (should_step_out) |
| 255 | { |
| 256 | // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions. |
| 257 | return current_plan->GetThread().QueueThreadPlanForStepOut (false, NULL, true, |
| 258 | current_plan->StopOthers(), |
| 259 | eVoteNo, eVoteNoOpinion); |
| 260 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | |
| 262 | return NULL; |
| 263 | } |