1 //===-- BreakpointResolverName.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/BreakpointResolverName.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/StreamString.h"
20 #include "lldb/Symbol/ClangNamespaceDecl.h"
21 #include "lldb/Symbol/Block.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/Symbol.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Target/ObjCLanguageRuntime.h"
28 using namespace lldb_private;
30 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
31 const char *name_cstr,
32 uint32_t name_type_mask,
33 Breakpoint::MatchType type,
35 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
39 m_skip_prologue (skip_prologue)
42 if (m_match_type == Breakpoint::Regexp)
44 if (!m_regex.Compile (name_cstr))
46 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
49 log->Warning ("function name regexp: \"%s\" did not compile.", name_cstr);
54 AddNameLookup (ConstString(name_cstr), name_type_mask);
58 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
61 uint32_t name_type_mask,
63 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
64 m_match_type (Breakpoint::Exact),
65 m_skip_prologue (skip_prologue)
67 for (size_t i = 0; i < num_names; i++)
69 AddNameLookup (ConstString (names[i]), name_type_mask);
73 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
74 std::vector<std::string> names,
75 uint32_t name_type_mask,
77 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
78 m_match_type (Breakpoint::Exact),
79 m_skip_prologue (skip_prologue)
81 for (const std::string& name : names)
83 AddNameLookup (ConstString (name.c_str(), name.size()), name_type_mask);
87 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
88 RegularExpression &func_regex,
90 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
93 m_match_type (Breakpoint::Regexp),
94 m_skip_prologue (skip_prologue)
98 BreakpointResolverName::BreakpointResolverName
101 const char *class_name,
103 Breakpoint::MatchType type,
106 BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
107 m_class_name (class_name),
110 m_skip_prologue (skip_prologue)
113 lookup.name.SetCString(method);
114 lookup.lookup_name = lookup.name;
115 lookup.name_type_mask = eFunctionNameTypeMethod;
116 lookup.match_name_after_lookup = false;
117 m_lookups.push_back (lookup);
120 BreakpointResolverName::~BreakpointResolverName ()
125 BreakpointResolverName::AddNameLookup (const ConstString &name, uint32_t name_type_mask)
127 ObjCLanguageRuntime::MethodName objc_method(name.GetCString(), false);
128 if (objc_method.IsValid(false))
130 std::vector<ConstString> objc_names;
131 objc_method.GetFullNames(objc_names, true);
132 for (ConstString objc_name : objc_names)
136 lookup.lookup_name = objc_name;
137 lookup.name_type_mask = eFunctionNameTypeFull;
138 lookup.match_name_after_lookup = false;
139 m_lookups.push_back (lookup);
146 Module::PrepareForFunctionNameLookup(lookup.name, name_type_mask, lookup.lookup_name, lookup.name_type_mask, lookup.match_name_after_lookup);
147 m_lookups.push_back (lookup);
153 BreakpointResolverName::LookupInfo::Prune (SymbolContextList &sc_list, size_t start_idx) const
155 if (match_name_after_lookup && name)
158 size_t i = start_idx;
159 while (i < sc_list.GetSize())
161 if (!sc_list.GetContextAtIndex(i, sc))
163 ConstString full_name (sc.GetFunctionName());
164 if (full_name && ::strstr(full_name.GetCString(), name.GetCString()) == NULL)
166 sc_list.RemoveContextAtIndex(i);
177 // FIXME: Right now we look at the module level, and call the module's "FindFunctions".
178 // Greg says he will add function tables, maybe at the CompileUnit level to accelerate function
179 // lookup. At that point, we should switch the depth to CompileUnit, and look in these tables.
181 Searcher::CallbackReturn
182 BreakpointResolverName::SearchCallback
184 SearchFilter &filter,
185 SymbolContext &context,
190 SymbolContextList func_list;
191 //SymbolContextList sym_list;
196 assert (m_breakpoint != NULL);
198 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
203 log->Warning ("Class/method function specification not supported yet.\n");
204 return Searcher::eCallbackReturnStop;
206 bool filter_by_cu = (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
207 const bool include_symbols = filter_by_cu == false;
208 const bool include_inlines = true;
209 const bool append = true;
211 switch (m_match_type)
213 case Breakpoint::Exact:
214 if (context.module_sp)
216 for (const LookupInfo &lookup : m_lookups)
218 const size_t start_func_idx = func_list.GetSize();
219 context.module_sp->FindFunctions (lookup.lookup_name,
221 lookup.name_type_mask,
226 const size_t end_func_idx = func_list.GetSize();
228 if (start_func_idx < end_func_idx)
229 lookup.Prune (func_list, start_func_idx);
233 case Breakpoint::Regexp:
234 if (context.module_sp)
236 context.module_sp->FindFunctions (m_regex,
237 !filter_by_cu, // include symbols only if we aren't filterning by CU
243 case Breakpoint::Glob:
245 log->Warning ("glob is not supported yet.");
249 // If the filter specifies a Compilation Unit, remove the ones that don't pass at this point.
252 uint32_t num_functions = func_list.GetSize();
254 for (size_t idx = 0; idx < num_functions; idx++)
257 func_list.GetContextAtIndex(idx, sc);
258 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
260 func_list.RemoveContextAtIndex(idx);
267 // Remove any duplicates between the funcion list and the symbol list
269 if (func_list.GetSize())
271 for (i = 0; i < func_list.GetSize(); i++)
273 if (func_list.GetContextAtIndex(i, sc))
275 if (sc.block && sc.block->GetInlinedFunctionInfo())
277 if (!sc.block->GetStartAddress(break_addr))
280 else if (sc.function)
282 break_addr = sc.function->GetAddressRange().GetBaseAddress();
283 if (m_skip_prologue && break_addr.IsValid())
285 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
286 if (prologue_byte_size)
287 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
292 break_addr = sc.symbol->GetAddress();
293 if (m_skip_prologue && break_addr.IsValid())
295 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
296 if (prologue_byte_size)
297 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
301 if (break_addr.IsValid())
303 if (filter.AddressPasses(break_addr))
305 BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location));
306 if (bp_loc_sp && new_location && !m_breakpoint->IsInternal())
311 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
312 log->Printf ("Added location: %s\n", s.GetData());
321 return Searcher::eCallbackReturnContinue;
325 BreakpointResolverName::GetDepth()
327 return Searcher::eDepthModule;
331 BreakpointResolverName::GetDescription (Stream *s)
333 if (m_match_type == Breakpoint::Regexp)
334 s->Printf("regex = '%s'", m_regex.GetText());
337 size_t num_names = m_lookups.size();
339 s->Printf("name = '%s'", m_lookups[0].name.GetCString());
342 s->Printf("names = {");
343 for (size_t i = 0; i < num_names - 1; i++)
345 s->Printf ("'%s', ", m_lookups[i].name.GetCString());
347 s->Printf ("'%s'}", m_lookups[num_names - 1].name.GetCString());
353 BreakpointResolverName::Dump (Stream *s) const