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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Breakpoint/BreakpointResolverName.h"
16 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
17 #include "Plugins/Language/ObjC/ObjCLanguage.h"
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Symbol/Block.h"
23 #include "lldb/Symbol/Function.h"
24 #include "lldb/Symbol/Symbol.h"
25 #include "lldb/Symbol/SymbolContext.h"
28 using namespace lldb_private;
30 BreakpointResolverName::BreakpointResolverName(
31 Breakpoint *bkpt, const char *name_cstr, uint32_t name_type_mask,
32 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
34 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
35 m_class_name(), m_regex(), m_match_type(type), m_language(language),
36 m_skip_prologue(skip_prologue) {
37 if (m_match_type == Breakpoint::Regexp) {
38 if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
39 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
42 log->Warning("function name regexp: \"%s\" did not compile.",
46 AddNameLookup(ConstString(name_cstr), name_type_mask);
50 BreakpointResolverName::BreakpointResolverName(
51 Breakpoint *bkpt, const char *names[], size_t num_names,
52 uint32_t name_type_mask, LanguageType language, lldb::addr_t offset,
54 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
55 m_match_type(Breakpoint::Exact), m_language(language),
56 m_skip_prologue(skip_prologue) {
57 for (size_t i = 0; i < num_names; i++) {
58 AddNameLookup(ConstString(names[i]), name_type_mask);
62 BreakpointResolverName::BreakpointResolverName(
63 Breakpoint *bkpt, std::vector<std::string> names, uint32_t name_type_mask,
64 LanguageType language, lldb::addr_t offset, bool skip_prologue)
65 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
66 m_match_type(Breakpoint::Exact), m_language(language),
67 m_skip_prologue(skip_prologue) {
68 for (const std::string &name : names) {
69 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
73 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
74 RegularExpression &func_regex,
75 lldb::LanguageType language,
78 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
79 m_class_name(nullptr), m_regex(func_regex),
80 m_match_type(Breakpoint::Regexp), m_language(language),
81 m_skip_prologue(skip_prologue) {}
83 BreakpointResolverName::~BreakpointResolverName() = default;
85 BreakpointResolverName::BreakpointResolverName(
86 const BreakpointResolverName &rhs)
87 : BreakpointResolver(rhs.m_breakpoint, BreakpointResolver::NameResolver,
89 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
90 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
91 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
93 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData(
94 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
96 LanguageType language = eLanguageTypeUnknown;
97 std::string language_name;
98 bool success = options_dict.GetValueForKeyAsString(
99 GetKey(OptionNames::LanguageName), language_name);
101 language = Language::GetLanguageTypeFromString(language_name);
102 if (language == eLanguageTypeUnknown) {
103 error.SetErrorStringWithFormat("BRN::CFSD: Unknown language: %s.",
104 language_name.c_str());
109 lldb::addr_t offset = 0;
111 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
113 error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
118 success = options_dict.GetValueForKeyAsBoolean(
119 GetKey(OptionNames::SkipPrologue), skip_prologue);
121 error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
125 std::string regex_text;
126 success = options_dict.GetValueForKeyAsString(
127 GetKey(OptionNames::RegexString), regex_text);
129 RegularExpression regex(regex_text);
130 return new BreakpointResolverName(bkpt, regex, language, offset,
133 StructuredData::Array *names_array;
134 success = options_dict.GetValueForKeyAsArray(
135 GetKey(OptionNames::SymbolNameArray), names_array);
137 error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
140 StructuredData::Array *names_mask_array;
141 success = options_dict.GetValueForKeyAsArray(
142 GetKey(OptionNames::NameMaskArray), names_mask_array);
144 error.SetErrorStringWithFormat(
145 "BRN::CFSD: Missing symbol names mask entry.");
149 size_t num_elem = names_array->GetSize();
150 if (num_elem != names_mask_array->GetSize()) {
151 error.SetErrorString(
152 "BRN::CFSD: names and names mask arrays have different sizes.");
157 error.SetErrorString(
158 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
161 std::vector<std::string> names;
162 std::vector<uint32_t> name_masks;
163 for (size_t i = 0; i < num_elem; i++) {
167 success = names_array->GetItemAtIndexAsString(i, name);
169 error.SetErrorString("BRN::CFSD: name entry is not a string.");
172 success = names_mask_array->GetItemAtIndexAsInteger(i, name_mask);
174 error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
177 names.push_back(name);
178 name_masks.push_back(name_mask);
181 BreakpointResolverName *resolver = new BreakpointResolverName(
182 bkpt, names[0].c_str(), name_masks[0], language,
183 Breakpoint::MatchType::Exact, offset, skip_prologue);
184 for (size_t i = 1; i < num_elem; i++) {
185 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
191 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
192 StructuredData::DictionarySP options_dict_sp(
193 new StructuredData::Dictionary());
195 if (m_regex.IsValid()) {
196 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
199 StructuredData::ArraySP names_sp(new StructuredData::Array());
200 StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
201 for (auto lookup : m_lookups) {
202 names_sp->AddItem(StructuredData::StringSP(
203 new StructuredData::String(lookup.GetName().AsCString())));
204 name_masks_sp->AddItem(StructuredData::IntegerSP(
205 new StructuredData::Integer(lookup.GetNameTypeMask())));
207 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
208 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
210 if (m_language != eLanguageTypeUnknown)
211 options_dict_sp->AddStringItem(
212 GetKey(OptionNames::LanguageName),
213 Language::GetNameForLanguageType(m_language));
214 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
217 return WrapOptionsDict(options_dict_sp);
220 void BreakpointResolverName::AddNameLookup(const ConstString &name,
221 uint32_t name_type_mask) {
222 ObjCLanguage::MethodName objc_method(name.GetCString(), false);
223 if (objc_method.IsValid(false)) {
224 std::vector<ConstString> objc_names;
225 objc_method.GetFullNames(objc_names, true);
226 for (ConstString objc_name : objc_names) {
227 Module::LookupInfo lookup;
228 lookup.SetName(name);
229 lookup.SetLookupName(objc_name);
230 lookup.SetNameTypeMask(eFunctionNameTypeFull);
231 m_lookups.push_back(lookup);
234 Module::LookupInfo lookup(name, name_type_mask, m_language);
235 m_lookups.push_back(lookup);
239 // FIXME: Right now we look at the module level, and call the module's
241 // Greg says he will add function tables, maybe at the CompileUnit level to
242 // accelerate function
243 // lookup. At that point, we should switch the depth to CompileUnit, and look
246 Searcher::CallbackReturn
247 BreakpointResolverName::SearchCallback(SearchFilter &filter,
248 SymbolContext &context, Address *addr,
250 SymbolContextList func_list;
251 // SymbolContextList sym_list;
256 assert(m_breakpoint != nullptr);
258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
262 log->Warning("Class/method function specification not supported yet.\n");
263 return Searcher::eCallbackReturnStop;
266 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
267 bool filter_by_language = (m_language != eLanguageTypeUnknown);
268 const bool include_symbols = !filter_by_cu;
269 const bool include_inlines = true;
270 const bool append = true;
272 switch (m_match_type) {
273 case Breakpoint::Exact:
274 if (context.module_sp) {
275 for (const auto &lookup : m_lookups) {
276 const size_t start_func_idx = func_list.GetSize();
277 context.module_sp->FindFunctions(
278 lookup.GetLookupName(), nullptr, lookup.GetNameTypeMask(),
279 include_symbols, include_inlines, append, func_list);
281 const size_t end_func_idx = func_list.GetSize();
283 if (start_func_idx < end_func_idx)
284 lookup.Prune(func_list, start_func_idx);
288 case Breakpoint::Regexp:
289 if (context.module_sp) {
290 context.module_sp->FindFunctions(
292 !filter_by_cu, // include symbols only if we aren't filtering by CU
293 include_inlines, append, func_list);
296 case Breakpoint::Glob:
298 log->Warning("glob is not supported yet.");
302 // If the filter specifies a Compilation Unit, remove the ones that don't pass
304 if (filter_by_cu || filter_by_language) {
305 uint32_t num_functions = func_list.GetSize();
307 for (size_t idx = 0; idx < num_functions; idx++) {
308 bool remove_it = false;
310 func_list.GetContextAtIndex(idx, sc);
312 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
316 if (filter_by_language) {
317 LanguageType sym_language = sc.GetLanguage();
318 if ((Language::GetPrimaryLanguage(sym_language) !=
319 Language::GetPrimaryLanguage(m_language)) &&
320 (sym_language != eLanguageTypeUnknown)) {
326 func_list.RemoveContextAtIndex(idx);
333 // Remove any duplicates between the function list and the symbol list
335 if (func_list.GetSize()) {
336 for (i = 0; i < func_list.GetSize(); i++) {
337 if (func_list.GetContextAtIndex(i, sc)) {
338 bool is_reexported = false;
340 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
341 if (!sc.block->GetStartAddress(break_addr))
343 } else if (sc.function) {
344 break_addr = sc.function->GetAddressRange().GetBaseAddress();
345 if (m_skip_prologue && break_addr.IsValid()) {
346 const uint32_t prologue_byte_size =
347 sc.function->GetPrologueByteSize();
348 if (prologue_byte_size)
349 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
351 } else if (sc.symbol) {
352 if (sc.symbol->GetType() == eSymbolTypeReExported) {
353 const Symbol *actual_symbol =
354 sc.symbol->ResolveReExportedSymbol(m_breakpoint->GetTarget());
356 is_reexported = true;
357 break_addr = actual_symbol->GetAddress();
360 break_addr = sc.symbol->GetAddress();
363 if (m_skip_prologue && break_addr.IsValid()) {
364 const uint32_t prologue_byte_size =
365 sc.symbol->GetPrologueByteSize();
366 if (prologue_byte_size)
367 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
371 if (break_addr.IsValid()) {
372 if (filter.AddressPasses(break_addr)) {
373 BreakpointLocationSP bp_loc_sp(
374 AddLocation(break_addr, &new_location));
375 bp_loc_sp->SetIsReExported(is_reexported);
376 if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) {
379 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
380 log->Printf("Added location: %s\n", s.GetData());
389 return Searcher::eCallbackReturnContinue;
392 Searcher::Depth BreakpointResolverName::GetDepth() {
393 return Searcher::eDepthModule;
396 void BreakpointResolverName::GetDescription(Stream *s) {
397 if (m_match_type == Breakpoint::Regexp)
398 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
400 size_t num_names = m_lookups.size();
402 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
404 s->Printf("names = {");
405 for (size_t i = 0; i < num_names; i++) {
406 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
407 m_lookups[i].GetName().GetCString());
412 if (m_language != eLanguageTypeUnknown) {
413 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
417 void BreakpointResolverName::Dump(Stream *s) const {}
419 lldb::BreakpointResolverSP
420 BreakpointResolverName::CopyForBreakpoint(Breakpoint &breakpoint) {
421 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
422 ret_sp->SetBreakpoint(&breakpoint);