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"
12 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
13 #include "Plugins/Language/ObjC/ObjCLanguage.h"
14 #include "lldb/Breakpoint/BreakpointLocation.h"
15 #include "lldb/Core/Architecture.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/StreamString.h"
26 using namespace lldb_private;
28 BreakpointResolverName::BreakpointResolverName(
29 Breakpoint *bkpt, const char *name_cstr, FunctionNameType name_type_mask,
30 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
32 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
33 m_class_name(), m_regex(), m_match_type(type), m_language(language),
34 m_skip_prologue(skip_prologue) {
35 if (m_match_type == Breakpoint::Regexp) {
36 if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
37 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
40 log->Warning("function name regexp: \"%s\" did not compile.",
44 AddNameLookup(ConstString(name_cstr), name_type_mask);
48 BreakpointResolverName::BreakpointResolverName(
49 Breakpoint *bkpt, const char *names[], size_t num_names,
50 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
52 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
53 m_match_type(Breakpoint::Exact), m_language(language),
54 m_skip_prologue(skip_prologue) {
55 for (size_t i = 0; i < num_names; i++) {
56 AddNameLookup(ConstString(names[i]), name_type_mask);
60 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
61 std::vector<std::string> names,
62 FunctionNameType name_type_mask,
63 LanguageType language,
66 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
67 m_match_type(Breakpoint::Exact), m_language(language),
68 m_skip_prologue(skip_prologue) {
69 for (const std::string &name : names) {
70 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
74 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
75 RegularExpression &func_regex,
76 lldb::LanguageType language,
79 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
80 m_class_name(nullptr), m_regex(func_regex),
81 m_match_type(Breakpoint::Regexp), m_language(language),
82 m_skip_prologue(skip_prologue) {}
84 BreakpointResolverName::~BreakpointResolverName() = default;
86 BreakpointResolverName::BreakpointResolverName(
87 const BreakpointResolverName &rhs)
88 : BreakpointResolver(rhs.m_breakpoint, BreakpointResolver::NameResolver,
90 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
91 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
92 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
94 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData(
95 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
97 LanguageType language = eLanguageTypeUnknown;
98 llvm::StringRef language_name;
99 bool success = options_dict.GetValueForKeyAsString(
100 GetKey(OptionNames::LanguageName), language_name);
102 language = Language::GetLanguageTypeFromString(language_name);
103 if (language == eLanguageTypeUnknown) {
104 error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
110 lldb::addr_t offset = 0;
112 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
114 error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
119 success = options_dict.GetValueForKeyAsBoolean(
120 GetKey(OptionNames::SkipPrologue), skip_prologue);
122 error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
126 llvm::StringRef regex_text;
127 success = options_dict.GetValueForKeyAsString(
128 GetKey(OptionNames::RegexString), regex_text);
130 RegularExpression regex(regex_text);
131 return new BreakpointResolverName(bkpt, regex, language, offset,
134 StructuredData::Array *names_array;
135 success = options_dict.GetValueForKeyAsArray(
136 GetKey(OptionNames::SymbolNameArray), names_array);
138 error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
141 StructuredData::Array *names_mask_array;
142 success = options_dict.GetValueForKeyAsArray(
143 GetKey(OptionNames::NameMaskArray), names_mask_array);
145 error.SetErrorStringWithFormat(
146 "BRN::CFSD: Missing symbol names mask entry.");
150 size_t num_elem = names_array->GetSize();
151 if (num_elem != names_mask_array->GetSize()) {
152 error.SetErrorString(
153 "BRN::CFSD: names and names mask arrays have different sizes.");
158 error.SetErrorString(
159 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
162 std::vector<std::string> names;
163 std::vector<FunctionNameType> name_masks;
164 for (size_t i = 0; i < num_elem; i++) {
165 llvm::StringRef name;
167 success = names_array->GetItemAtIndexAsString(i, name);
169 error.SetErrorString("BRN::CFSD: name entry is not a string.");
172 std::underlying_type<FunctionNameType>::type fnt;
173 success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
175 error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
178 names.push_back(name);
179 name_masks.push_back(static_cast<FunctionNameType>(fnt));
182 BreakpointResolverName *resolver = new BreakpointResolverName(
183 bkpt, names[0].c_str(), name_masks[0], language,
184 Breakpoint::MatchType::Exact, offset, skip_prologue);
185 for (size_t i = 1; i < num_elem; i++) {
186 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
192 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
193 StructuredData::DictionarySP options_dict_sp(
194 new StructuredData::Dictionary());
196 if (m_regex.IsValid()) {
197 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
200 StructuredData::ArraySP names_sp(new StructuredData::Array());
201 StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
202 for (auto lookup : m_lookups) {
203 names_sp->AddItem(StructuredData::StringSP(
204 new StructuredData::String(lookup.GetName().AsCString())));
205 name_masks_sp->AddItem(StructuredData::IntegerSP(
206 new StructuredData::Integer(lookup.GetNameTypeMask())));
208 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
209 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
211 if (m_language != eLanguageTypeUnknown)
212 options_dict_sp->AddStringItem(
213 GetKey(OptionNames::LanguageName),
214 Language::GetNameForLanguageType(m_language));
215 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
218 return WrapOptionsDict(options_dict_sp);
221 void BreakpointResolverName::AddNameLookup(const ConstString &name,
222 FunctionNameType name_type_mask) {
223 ObjCLanguage::MethodName objc_method(name.GetCString(), false);
224 if (objc_method.IsValid(false)) {
225 std::vector<ConstString> objc_names;
226 objc_method.GetFullNames(objc_names, true);
227 for (ConstString objc_name : objc_names) {
228 Module::LookupInfo lookup;
229 lookup.SetName(name);
230 lookup.SetLookupName(objc_name);
231 lookup.SetNameTypeMask(eFunctionNameTypeFull);
232 m_lookups.push_back(lookup);
235 Module::LookupInfo lookup(name, name_type_mask, m_language);
236 m_lookups.push_back(lookup);
240 // FIXME: Right now we look at the module level, and call the module's
242 // Greg says he will add function tables, maybe at the CompileUnit level to
243 // accelerate function lookup. At that point, we should switch the depth to
244 // CompileUnit, and look in these tables.
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
303 // pass at this point.
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);
369 const Architecture *arch =
370 m_breakpoint->GetTarget().GetArchitecturePlugin();
372 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
377 if (break_addr.IsValid()) {
378 if (filter.AddressPasses(break_addr)) {
379 BreakpointLocationSP bp_loc_sp(
380 AddLocation(break_addr, &new_location));
381 bp_loc_sp->SetIsReExported(is_reexported);
382 if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) {
385 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
386 log->Printf("Added location: %s\n", s.GetData());
395 return Searcher::eCallbackReturnContinue;
398 lldb::SearchDepth BreakpointResolverName::GetDepth() {
399 return lldb::eSearchDepthModule;
402 void BreakpointResolverName::GetDescription(Stream *s) {
403 if (m_match_type == Breakpoint::Regexp)
404 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
406 size_t num_names = m_lookups.size();
408 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
410 s->Printf("names = {");
411 for (size_t i = 0; i < num_names; i++) {
412 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
413 m_lookups[i].GetName().GetCString());
418 if (m_language != eLanguageTypeUnknown) {
419 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
423 void BreakpointResolverName::Dump(Stream *s) const {}
425 lldb::BreakpointResolverSP
426 BreakpointResolverName::CopyForBreakpoint(Breakpoint &breakpoint) {
427 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
428 ret_sp->SetBreakpoint(&breakpoint);