]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/ThreadPlanStepRange.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / ThreadPlanStepRange.cpp
1 //===-- ThreadPlanStepRange.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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/ThreadPlanStepRange.h"
15 #include "lldb/Breakpoint/BreakpointLocation.h"
16 #include "lldb/Breakpoint/BreakpointSite.h"
17 #include "lldb/Core/Disassembler.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/StopInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlanRunToAddress.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Stream.h"
29
30 using namespace lldb;
31 using namespace lldb_private;
32
33 //----------------------------------------------------------------------
34 // ThreadPlanStepRange: Step through a stack range, either stepping over or
35 // into based on the value of \a type.
36 //----------------------------------------------------------------------
37
38 ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name,
39                                          Thread &thread,
40                                          const AddressRange &range,
41                                          const SymbolContext &addr_context,
42                                          lldb::RunMode stop_others,
43                                          bool given_ranges_only)
44     : ThreadPlan(kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
45       m_addr_context(addr_context), m_address_ranges(),
46       m_stop_others(stop_others), m_stack_id(), m_parent_stack_id(),
47       m_no_more_plans(false), m_first_run_event(true), m_use_fast_step(false),
48       m_given_ranges_only(given_ranges_only) {
49   m_use_fast_step = GetTarget().GetUseFastStepping();
50   AddRange(range);
51   m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
52   StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
53   if (parent_stack)
54     m_parent_stack_id = parent_stack->GetStackID();
55 }
56
57 ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
58
59 void ThreadPlanStepRange::DidPush() {
60   // See if we can find a "next range" breakpoint:
61   SetNextBranchBreakpoint();
62 }
63
64 bool ThreadPlanStepRange::ValidatePlan(Stream *error) { return true; }
65
66 Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) {
67   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
68
69   const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
70   if (log)
71     log->Printf("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
72                 vote);
73   return vote;
74 }
75
76 void ThreadPlanStepRange::AddRange(const AddressRange &new_range) {
77   // For now I'm just adding the ranges.  At some point we may want to condense
78   // the ranges if they overlap, though I don't think it is likely to be very
79   // important.
80   m_address_ranges.push_back(new_range);
81
82   // Fill the slot for this address range with an empty DisassemblerSP in the
83   // instruction ranges. I want the indices to match, but I don't want to do
84   // the work to disassemble this range if I don't step into it.
85   m_instruction_ranges.push_back(DisassemblerSP());
86 }
87
88 void ThreadPlanStepRange::DumpRanges(Stream *s) {
89   size_t num_ranges = m_address_ranges.size();
90   if (num_ranges == 1) {
91     m_address_ranges[0].Dump(s, m_thread.CalculateTarget().get(),
92                              Address::DumpStyleLoadAddress);
93   } else {
94     for (size_t i = 0; i < num_ranges; i++) {
95       s->Printf(" %" PRIu64 ": ", uint64_t(i));
96       m_address_ranges[i].Dump(s, m_thread.CalculateTarget().get(),
97                                Address::DumpStyleLoadAddress);
98     }
99   }
100 }
101
102 bool ThreadPlanStepRange::InRange() {
103   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
104   bool ret_value = false;
105
106   lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
107
108   size_t num_ranges = m_address_ranges.size();
109   for (size_t i = 0; i < num_ranges; i++) {
110     ret_value = m_address_ranges[i].ContainsLoadAddress(
111         pc_load_addr, m_thread.CalculateTarget().get());
112     if (ret_value)
113       break;
114   }
115
116   if (!ret_value && !m_given_ranges_only) {
117     // See if we've just stepped to another part of the same line number...
118     StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
119
120     SymbolContext new_context(
121         frame->GetSymbolContext(eSymbolContextEverything));
122     if (m_addr_context.line_entry.IsValid() &&
123         new_context.line_entry.IsValid()) {
124       if (m_addr_context.line_entry.original_file ==
125           new_context.line_entry.original_file) {
126         if (m_addr_context.line_entry.line == new_context.line_entry.line) {
127           m_addr_context = new_context;
128           AddRange(
129               m_addr_context.line_entry.GetSameLineContiguousAddressRange());
130           ret_value = true;
131           if (log) {
132             StreamString s;
133             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
134                                            true, Address::DumpStyleLoadAddress,
135                                            Address::DumpStyleLoadAddress, true);
136
137             log->Printf(
138                 "Step range plan stepped to another range of same line: %s",
139                 s.GetData());
140           }
141         } else if (new_context.line_entry.line == 0) {
142           new_context.line_entry.line = m_addr_context.line_entry.line;
143           m_addr_context = new_context;
144           AddRange(
145               m_addr_context.line_entry.GetSameLineContiguousAddressRange());
146           ret_value = true;
147           if (log) {
148             StreamString s;
149             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
150                                            true, Address::DumpStyleLoadAddress,
151                                            Address::DumpStyleLoadAddress, true);
152
153             log->Printf("Step range plan stepped to a range at linenumber 0 "
154                         "stepping through that range: %s",
155                         s.GetData());
156           }
157         } else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(
158                        m_thread.CalculateTarget().get()) != pc_load_addr) {
159           // Another thing that sometimes happens here is that we step out of
160           // one line into the MIDDLE of another line.  So far I mostly see
161           // this due to bugs in the debug information. But we probably don't
162           // want to be in the middle of a line range, so in that case reset
163           // the stepping range to the line we've stepped into the middle of
164           // and continue.
165           m_addr_context = new_context;
166           m_address_ranges.clear();
167           AddRange(m_addr_context.line_entry.range);
168           ret_value = true;
169           if (log) {
170             StreamString s;
171             m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
172                                            true, Address::DumpStyleLoadAddress,
173                                            Address::DumpStyleLoadAddress, true);
174
175             log->Printf("Step range plan stepped to the middle of new "
176                         "line(%d): %s, continuing to clear this line.",
177                         new_context.line_entry.line, s.GetData());
178           }
179         }
180       }
181     }
182   }
183
184   if (!ret_value && log)
185     log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
186
187   return ret_value;
188 }
189
190 bool ThreadPlanStepRange::InSymbol() {
191   lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
192   if (m_addr_context.function != nullptr) {
193     return m_addr_context.function->GetAddressRange().ContainsLoadAddress(
194         cur_pc, m_thread.CalculateTarget().get());
195   } else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress()) {
196     AddressRange range(m_addr_context.symbol->GetAddressRef(),
197                        m_addr_context.symbol->GetByteSize());
198     return range.ContainsLoadAddress(cur_pc, m_thread.CalculateTarget().get());
199   }
200   return false;
201 }
202
203 // FIXME: This should also handle inlining if we aren't going to do inlining in
204 // the
205 // main stack.
206 //
207 // Ideally we should remember the whole stack frame list, and then compare that
208 // to the current list.
209
210 lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
211   FrameComparison frame_order;
212
213   StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
214
215   if (cur_frame_id == m_stack_id) {
216     frame_order = eFrameCompareEqual;
217   } else if (cur_frame_id < m_stack_id) {
218     frame_order = eFrameCompareYounger;
219   } else {
220     StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
221     StackID cur_parent_id;
222     if (cur_parent_frame)
223       cur_parent_id = cur_parent_frame->GetStackID();
224     if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() &&
225         m_parent_stack_id == cur_parent_id)
226       frame_order = eFrameCompareSameParent;
227     else
228       frame_order = eFrameCompareOlder;
229   }
230   return frame_order;
231 }
232
233 bool ThreadPlanStepRange::StopOthers() {
234   return (m_stop_others == lldb::eOnlyThisThread ||
235           m_stop_others == lldb::eOnlyDuringStepping);
236 }
237
238 InstructionList *ThreadPlanStepRange::GetInstructionsForAddress(
239     lldb::addr_t addr, size_t &range_index, size_t &insn_offset) {
240   size_t num_ranges = m_address_ranges.size();
241   for (size_t i = 0; i < num_ranges; i++) {
242     if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) {
243       // Some joker added a zero size range to the stepping range...
244       if (m_address_ranges[i].GetByteSize() == 0)
245         return nullptr;
246
247       if (!m_instruction_ranges[i]) {
248         // Disassemble the address range given:
249         ExecutionContext exe_ctx(m_thread.GetProcess());
250         const char *plugin_name = nullptr;
251         const char *flavor = nullptr;
252         const bool prefer_file_cache = true;
253         m_instruction_ranges[i] = Disassembler::DisassembleRange(
254             GetTarget().GetArchitecture(), plugin_name, flavor, exe_ctx,
255             m_address_ranges[i], prefer_file_cache);
256       }
257       if (!m_instruction_ranges[i])
258         return nullptr;
259       else {
260         // Find where we are in the instruction list as well.  If we aren't at
261         // an instruction, return nullptr. In this case, we're probably lost,
262         // and shouldn't try to do anything fancy.
263
264         insn_offset =
265             m_instruction_ranges[i]
266                 ->GetInstructionList()
267                 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
268         if (insn_offset == UINT32_MAX)
269           return nullptr;
270         else {
271           range_index = i;
272           return &m_instruction_ranges[i]->GetInstructionList();
273         }
274       }
275     }
276   }
277   return nullptr;
278 }
279
280 void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
281   if (m_next_branch_bp_sp) {
282     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
283     if (log)
284       log->Printf("Removing next branch breakpoint: %d.",
285                   m_next_branch_bp_sp->GetID());
286     GetTarget().RemoveBreakpointByID(m_next_branch_bp_sp->GetID());
287     m_next_branch_bp_sp.reset();
288   }
289 }
290
291 bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
292   if (m_next_branch_bp_sp)
293     return true;
294
295   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
296   // Stepping through ranges using breakpoints doesn't work yet, but with this
297   // off we fall back to instruction single stepping.
298   if (!m_use_fast_step)
299     return false;
300
301   lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
302   // Find the current address in our address ranges, and fetch the disassembly
303   // if we haven't already:
304   size_t pc_index;
305   size_t range_index;
306   InstructionList *instructions =
307       GetInstructionsForAddress(cur_addr, range_index, pc_index);
308   if (instructions == nullptr)
309     return false;
310   else {
311     Target &target = GetThread().GetProcess()->GetTarget();
312     uint32_t branch_index;
313     branch_index =
314         instructions->GetIndexOfNextBranchInstruction(pc_index, target);
315
316     Address run_to_address;
317
318     // If we didn't find a branch, run to the end of the range.
319     if (branch_index == UINT32_MAX) {
320       uint32_t last_index = instructions->GetSize() - 1;
321       if (last_index - pc_index > 1) {
322         InstructionSP last_inst =
323             instructions->GetInstructionAtIndex(last_index);
324         size_t last_inst_size = last_inst->GetOpcode().GetByteSize();
325         run_to_address = last_inst->GetAddress();
326         run_to_address.Slide(last_inst_size);
327       }
328     } else if (branch_index - pc_index > 1) {
329       run_to_address =
330           instructions->GetInstructionAtIndex(branch_index)->GetAddress();
331     }
332
333     if (run_to_address.IsValid()) {
334       const bool is_internal = true;
335       m_next_branch_bp_sp =
336           GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
337       if (m_next_branch_bp_sp) {
338         if (log) {
339           lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
340           BreakpointLocationSP bp_loc =
341               m_next_branch_bp_sp->GetLocationAtIndex(0);
342           if (bp_loc) {
343             BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
344             if (bp_site) {
345               bp_site_id = bp_site->GetID();
346             }
347           }
348           log->Printf("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting "
349                       "breakpoint %d (site %d) to run to address 0x%" PRIx64,
350                       m_next_branch_bp_sp->GetID(), bp_site_id,
351                       run_to_address.GetLoadAddress(
352                           &m_thread.GetProcess()->GetTarget()));
353         }
354         m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
355         m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
356         return true;
357       } else
358         return false;
359     }
360   }
361   return false;
362 }
363
364 bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
365     lldb::StopInfoSP stop_info_sp) {
366   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
367   if (!m_next_branch_bp_sp)
368     return false;
369
370   break_id_t bp_site_id = stop_info_sp->GetValue();
371   BreakpointSiteSP bp_site_sp =
372       m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
373   if (!bp_site_sp)
374     return false;
375   else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
376     return false;
377   else {
378     // If we've hit the next branch breakpoint, then clear it.
379     size_t num_owners = bp_site_sp->GetNumberOfOwners();
380     bool explains_stop = true;
381     // If all the owners are internal, then we are probably just stepping over
382     // this range from multiple threads, or multiple frames, so we want to
383     // continue.  If one is not internal, then we should not explain the stop,
384     // and let the user breakpoint handle the stop.
385     for (size_t i = 0; i < num_owners; i++) {
386       if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
387         explains_stop = false;
388         break;
389       }
390     }
391     if (log)
392       log->Printf("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit "
393                   "next range breakpoint which has %" PRIu64
394                   " owners - explains stop: %u.",
395                   (uint64_t)num_owners, explains_stop);
396     ClearNextBranchBreakpoint();
397     return explains_stop;
398   }
399 }
400
401 bool ThreadPlanStepRange::WillStop() { return true; }
402
403 StateType ThreadPlanStepRange::GetPlanRunState() {
404   if (m_next_branch_bp_sp)
405     return eStateRunning;
406   else
407     return eStateStepping;
408 }
409
410 bool ThreadPlanStepRange::MischiefManaged() {
411   // If we have pushed some plans between ShouldStop & MischiefManaged, then
412   // we're not done...
413   // I do this check first because we might have stepped somewhere that will
414   // fool InRange into
415   // thinking it needs to step past the end of that line.  This happens, for
416   // instance, when stepping over inlined code that is in the middle of the
417   // current line.
418
419   if (!m_no_more_plans)
420     return false;
421
422   bool done = true;
423   if (!IsPlanComplete()) {
424     if (InRange()) {
425       done = false;
426     } else {
427       FrameComparison frame_order = CompareCurrentFrameToStartFrame();
428       done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
429     }
430   }
431
432   if (done) {
433     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
434     if (log)
435       log->Printf("Completed step through range plan.");
436     ClearNextBranchBreakpoint();
437     ThreadPlan::MischiefManaged();
438     return true;
439   } else {
440     return false;
441   }
442 }
443
444 bool ThreadPlanStepRange::IsPlanStale() {
445   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
446   FrameComparison frame_order = CompareCurrentFrameToStartFrame();
447
448   if (frame_order == eFrameCompareOlder) {
449     if (log) {
450       log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
451                   "stepped out.");
452     }
453     return true;
454   } else if (frame_order == eFrameCompareEqual && InSymbol()) {
455     // If we are not in a place we should step through, we've gotten stale. One
456     // tricky bit here is that some stubs don't push a frame, so we should.
457     // check that we are in the same symbol.
458     if (!InRange()) {
459       // Set plan Complete when we reach next instruction just after the range
460       lldb::addr_t addr = m_thread.GetRegisterContext()->GetPC() - 1;
461       size_t num_ranges = m_address_ranges.size();
462       for (size_t i = 0; i < num_ranges; i++) {
463         bool in_range = m_address_ranges[i].ContainsLoadAddress(
464             addr, m_thread.CalculateTarget().get());
465         if (in_range) {
466           SetPlanComplete();
467         }
468       }
469       return true;
470     }
471   }
472   return false;
473 }