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