1 //===-- BreakpointResolver.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 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/BreakpointResolver.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Address.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/ModuleList.h"
21 #include "lldb/Core/SearchFilter.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/Function.h"
29 using namespace lldb_private;
32 //----------------------------------------------------------------------
33 // BreakpointResolver:
34 //----------------------------------------------------------------------
35 BreakpointResolver::BreakpointResolver (Breakpoint *bkpt, const unsigned char resolverTy, lldb::addr_t offset) :
38 SubclassID (resolverTy)
42 BreakpointResolver::~BreakpointResolver ()
48 BreakpointResolver::SetBreakpoint (Breakpoint *bkpt)
54 BreakpointResolver::ResolveBreakpointInModules (SearchFilter &filter, ModuleList &modules)
56 filter.SearchInModuleList(*this, modules);
60 BreakpointResolver::ResolveBreakpoint (SearchFilter &filter)
62 filter.Search (*this);
66 BreakpointResolver::SetSCMatchesByLine (SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, const char *log_ident)
68 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
70 while (sc_list.GetSize() > 0)
72 SymbolContextList tmp_sc_list;
73 unsigned current_idx = 0;
75 bool first_entry = true;
77 FileSpec match_file_spec;
78 FileSpec match_original_file_spec;
79 uint32_t closest_line_number = UINT32_MAX;
81 // Pull out the first entry, and all the others that match its file spec, and stuff them in the tmp list.
82 while (current_idx < sc_list.GetSize())
86 sc_list.GetContextAtIndex (current_idx, sc);
89 match_file_spec = sc.line_entry.file;
90 match_original_file_spec = sc.line_entry.original_file;
95 matches = ((sc.line_entry.file == match_file_spec) ||
96 (sc.line_entry.original_file == match_original_file_spec));
100 tmp_sc_list.Append (sc);
101 sc_list.RemoveContextAtIndex(current_idx);
103 // ResolveSymbolContext will always return a number that is >= the line number you pass in.
104 // So the smaller line number is always better.
105 if (sc.line_entry.line < closest_line_number)
106 closest_line_number = sc.line_entry.line;
112 // Okay, we've found the closest line number match, now throw away all the others:
115 while (current_idx < tmp_sc_list.GetSize())
117 if (tmp_sc_list.GetContextAtIndex(current_idx, sc))
119 if (sc.line_entry.line != closest_line_number)
120 tmp_sc_list.RemoveContextAtIndex(current_idx);
126 // Next go through and see if there are line table entries that are contiguous, and if so keep only the
127 // first of the contiguous range:
130 std::map<Block *, lldb::addr_t> blocks_with_breakpoints;
132 while (current_idx < tmp_sc_list.GetSize())
134 if (tmp_sc_list.GetContextAtIndex(current_idx, sc))
136 if (blocks_with_breakpoints.find (sc.block) != blocks_with_breakpoints.end())
137 tmp_sc_list.RemoveContextAtIndex(current_idx);
140 blocks_with_breakpoints.insert (std::pair<Block *, lldb::addr_t>(sc.block, sc.line_entry.range.GetBaseAddress().GetFileAddress()));
146 // and make breakpoints out of the closest line number match.
148 uint32_t tmp_sc_list_size = tmp_sc_list.GetSize();
150 for (uint32_t i = 0; i < tmp_sc_list_size; i++)
152 if (tmp_sc_list.GetContextAtIndex(i, sc))
154 Address line_start = sc.line_entry.range.GetBaseAddress();
155 if (line_start.IsValid())
157 if (filter.AddressPasses(line_start))
159 // If the line number is before the prologue end, move it there...
160 bool skipped_prologue = false;
165 Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
166 if (prologue_addr.IsValid() && (line_start == prologue_addr))
168 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
169 if (prologue_byte_size)
171 prologue_addr.Slide(prologue_byte_size);
173 if (filter.AddressPasses(prologue_addr))
175 skipped_prologue = true;
176 line_start = prologue_addr;
183 BreakpointLocationSP bp_loc_sp (AddLocation(line_start));
184 if (log && bp_loc_sp && !m_breakpoint->IsInternal())
187 bp_loc_sp->GetDescription (&s, lldb::eDescriptionLevelVerbose);
188 log->Printf ("Added location (skipped prologue: %s): %s \n", skipped_prologue ? "yes" : "no", s.GetData());
193 log->Printf ("Breakpoint %s at file address 0x%" PRIx64 " didn't pass the filter.\n",
194 log_ident ? log_ident : "",
195 line_start.GetFileAddress());
201 log->Printf ("error: Unable to set breakpoint %s at file address 0x%" PRIx64 "\n",
202 log_ident ? log_ident : "",
203 line_start.GetFileAddress());
211 BreakpointResolver::AddLocation(Address loc_addr, bool *new_location)
213 loc_addr.Slide(m_offset);
214 return m_breakpoint->AddLocation(loc_addr, new_location);
219 BreakpointResolver::SetOffset (lldb::addr_t offset)
221 // There may already be an offset, so we are actually adjusting location addresses by the difference.
222 // lldb::addr_t slide = offset - m_offset;
223 // FIXME: We should go fix up all the already set locations for the new slide.