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/Architecture.h"
20 #include "lldb/Core/Module.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/Target.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/StreamString.h"
30 using namespace lldb_private;
32 BreakpointResolverName::BreakpointResolverName(
33 Breakpoint *bkpt, const char *name_cstr, uint32_t name_type_mask,
34 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
36 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
37 m_class_name(), m_regex(), m_match_type(type), m_language(language),
38 m_skip_prologue(skip_prologue) {
39 if (m_match_type == Breakpoint::Regexp) {
40 if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
41 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
44 log->Warning("function name regexp: \"%s\" did not compile.",
48 AddNameLookup(ConstString(name_cstr), name_type_mask);
52 BreakpointResolverName::BreakpointResolverName(
53 Breakpoint *bkpt, const char *names[], size_t num_names,
54 uint32_t name_type_mask, LanguageType language, lldb::addr_t offset,
56 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
57 m_match_type(Breakpoint::Exact), m_language(language),
58 m_skip_prologue(skip_prologue) {
59 for (size_t i = 0; i < num_names; i++) {
60 AddNameLookup(ConstString(names[i]), name_type_mask);
64 BreakpointResolverName::BreakpointResolverName(
65 Breakpoint *bkpt, std::vector<std::string> names, uint32_t name_type_mask,
66 LanguageType language, lldb::addr_t offset, bool skip_prologue)
67 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
68 m_match_type(Breakpoint::Exact), m_language(language),
69 m_skip_prologue(skip_prologue) {
70 for (const std::string &name : names) {
71 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
75 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
76 RegularExpression &func_regex,
77 lldb::LanguageType language,
80 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
81 m_class_name(nullptr), m_regex(func_regex),
82 m_match_type(Breakpoint::Regexp), m_language(language),
83 m_skip_prologue(skip_prologue) {}
85 BreakpointResolverName::~BreakpointResolverName() = default;
87 BreakpointResolverName::BreakpointResolverName(
88 const BreakpointResolverName &rhs)
89 : BreakpointResolver(rhs.m_breakpoint, BreakpointResolver::NameResolver,
91 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
92 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
93 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
95 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData(
96 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
98 LanguageType language = eLanguageTypeUnknown;
99 llvm::StringRef language_name;
100 bool success = options_dict.GetValueForKeyAsString(
101 GetKey(OptionNames::LanguageName), language_name);
103 language = Language::GetLanguageTypeFromString(language_name);
104 if (language == eLanguageTypeUnknown) {
105 error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
111 lldb::addr_t offset = 0;
113 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
115 error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
120 success = options_dict.GetValueForKeyAsBoolean(
121 GetKey(OptionNames::SkipPrologue), skip_prologue);
123 error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
127 llvm::StringRef regex_text;
128 success = options_dict.GetValueForKeyAsString(
129 GetKey(OptionNames::RegexString), regex_text);
131 RegularExpression regex(regex_text);
132 return new BreakpointResolverName(bkpt, regex, language, offset,
135 StructuredData::Array *names_array;
136 success = options_dict.GetValueForKeyAsArray(
137 GetKey(OptionNames::SymbolNameArray), names_array);
139 error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
142 StructuredData::Array *names_mask_array;
143 success = options_dict.GetValueForKeyAsArray(
144 GetKey(OptionNames::NameMaskArray), names_mask_array);
146 error.SetErrorStringWithFormat(
147 "BRN::CFSD: Missing symbol names mask entry.");
151 size_t num_elem = names_array->GetSize();
152 if (num_elem != names_mask_array->GetSize()) {
153 error.SetErrorString(
154 "BRN::CFSD: names and names mask arrays have different sizes.");
159 error.SetErrorString(
160 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
163 std::vector<std::string> names;
164 std::vector<uint32_t> name_masks;
165 for (size_t i = 0; i < num_elem; i++) {
167 llvm::StringRef name;
169 success = names_array->GetItemAtIndexAsString(i, name);
171 error.SetErrorString("BRN::CFSD: name entry is not a string.");
174 success = names_mask_array->GetItemAtIndexAsInteger(i, name_mask);
176 error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
179 names.push_back(name);
180 name_masks.push_back(name_mask);
183 BreakpointResolverName *resolver = new BreakpointResolverName(
184 bkpt, names[0].c_str(), name_masks[0], language,
185 Breakpoint::MatchType::Exact, offset, skip_prologue);
186 for (size_t i = 1; i < num_elem; i++) {
187 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
193 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
194 StructuredData::DictionarySP options_dict_sp(
195 new StructuredData::Dictionary());
197 if (m_regex.IsValid()) {
198 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
201 StructuredData::ArraySP names_sp(new StructuredData::Array());
202 StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
203 for (auto lookup : m_lookups) {
204 names_sp->AddItem(StructuredData::StringSP(
205 new StructuredData::String(lookup.GetName().AsCString())));
206 name_masks_sp->AddItem(StructuredData::IntegerSP(
207 new StructuredData::Integer(lookup.GetNameTypeMask())));
209 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
210 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
212 if (m_language != eLanguageTypeUnknown)
213 options_dict_sp->AddStringItem(
214 GetKey(OptionNames::LanguageName),
215 Language::GetNameForLanguageType(m_language));
216 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
219 return WrapOptionsDict(options_dict_sp);
222 void BreakpointResolverName::AddNameLookup(const ConstString &name,
223 uint32_t name_type_mask) {
224 ObjCLanguage::MethodName objc_method(name.GetCString(), false);
225 if (objc_method.IsValid(false)) {
226 std::vector<ConstString> objc_names;
227 objc_method.GetFullNames(objc_names, true);
228 for (ConstString objc_name : objc_names) {
229 Module::LookupInfo lookup;
230 lookup.SetName(name);
231 lookup.SetLookupName(objc_name);
232 lookup.SetNameTypeMask(eFunctionNameTypeFull);
233 m_lookups.push_back(lookup);
236 Module::LookupInfo lookup(name, name_type_mask, m_language);
237 m_lookups.push_back(lookup);
241 // FIXME: Right now we look at the module level, and call the module's
243 // Greg says he will add function tables, maybe at the CompileUnit level to
244 // accelerate function lookup. At that point, we should switch the depth to
245 // CompileUnit, and look in these tables.
247 Searcher::CallbackReturn
248 BreakpointResolverName::SearchCallback(SearchFilter &filter,
249 SymbolContext &context, Address *addr,
251 SymbolContextList func_list;
252 // SymbolContextList sym_list;
257 assert(m_breakpoint != nullptr);
259 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
263 log->Warning("Class/method function specification not supported yet.\n");
264 return Searcher::eCallbackReturnStop;
267 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
268 bool filter_by_language = (m_language != eLanguageTypeUnknown);
269 const bool include_symbols = !filter_by_cu;
270 const bool include_inlines = true;
271 const bool append = true;
273 switch (m_match_type) {
274 case Breakpoint::Exact:
275 if (context.module_sp) {
276 for (const auto &lookup : m_lookups) {
277 const size_t start_func_idx = func_list.GetSize();
278 context.module_sp->FindFunctions(
279 lookup.GetLookupName(), nullptr, lookup.GetNameTypeMask(),
280 include_symbols, include_inlines, append, func_list);
282 const size_t end_func_idx = func_list.GetSize();
284 if (start_func_idx < end_func_idx)
285 lookup.Prune(func_list, start_func_idx);
289 case Breakpoint::Regexp:
290 if (context.module_sp) {
291 context.module_sp->FindFunctions(
293 !filter_by_cu, // include symbols only if we aren't filtering by CU
294 include_inlines, append, func_list);
297 case Breakpoint::Glob:
299 log->Warning("glob is not supported yet.");
303 // If the filter specifies a Compilation Unit, remove the ones that don't
304 // pass at this point.
305 if (filter_by_cu || filter_by_language) {
306 uint32_t num_functions = func_list.GetSize();
308 for (size_t idx = 0; idx < num_functions; idx++) {
309 bool remove_it = false;
311 func_list.GetContextAtIndex(idx, sc);
313 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
317 if (filter_by_language) {
318 LanguageType sym_language = sc.GetLanguage();
319 if ((Language::GetPrimaryLanguage(sym_language) !=
320 Language::GetPrimaryLanguage(m_language)) &&
321 (sym_language != eLanguageTypeUnknown)) {
327 func_list.RemoveContextAtIndex(idx);
334 // Remove any duplicates between the function list and the symbol list
336 if (func_list.GetSize()) {
337 for (i = 0; i < func_list.GetSize(); i++) {
338 if (func_list.GetContextAtIndex(i, sc)) {
339 bool is_reexported = false;
341 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
342 if (!sc.block->GetStartAddress(break_addr))
344 } else if (sc.function) {
345 break_addr = sc.function->GetAddressRange().GetBaseAddress();
346 if (m_skip_prologue && break_addr.IsValid()) {
347 const uint32_t prologue_byte_size =
348 sc.function->GetPrologueByteSize();
349 if (prologue_byte_size)
350 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
352 } else if (sc.symbol) {
353 if (sc.symbol->GetType() == eSymbolTypeReExported) {
354 const Symbol *actual_symbol =
355 sc.symbol->ResolveReExportedSymbol(m_breakpoint->GetTarget());
357 is_reexported = true;
358 break_addr = actual_symbol->GetAddress();
361 break_addr = sc.symbol->GetAddress();
364 if (m_skip_prologue && break_addr.IsValid()) {
365 const uint32_t prologue_byte_size =
366 sc.symbol->GetPrologueByteSize();
367 if (prologue_byte_size)
368 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
370 const Architecture *arch =
371 m_breakpoint->GetTarget().GetArchitecturePlugin();
373 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
378 if (break_addr.IsValid()) {
379 if (filter.AddressPasses(break_addr)) {
380 BreakpointLocationSP bp_loc_sp(
381 AddLocation(break_addr, &new_location));
382 bp_loc_sp->SetIsReExported(is_reexported);
383 if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) {
386 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
387 log->Printf("Added location: %s\n", s.GetData());
396 return Searcher::eCallbackReturnContinue;
399 Searcher::Depth BreakpointResolverName::GetDepth() {
400 return Searcher::eDepthModule;
403 void BreakpointResolverName::GetDescription(Stream *s) {
404 if (m_match_type == Breakpoint::Regexp)
405 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
407 size_t num_names = m_lookups.size();
409 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
411 s->Printf("names = {");
412 for (size_t i = 0; i < num_names; i++) {
413 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
414 m_lookups[i].GetName().GetCString());
419 if (m_language != eLanguageTypeUnknown) {
420 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
424 void BreakpointResolverName::Dump(Stream *s) const {}
426 lldb::BreakpointResolverSP
427 BreakpointResolverName::CopyForBreakpoint(Breakpoint &breakpoint) {
428 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
429 ret_sp->SetBreakpoint(&breakpoint);