1 //===-- BreakpointResolverName.cpp ------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointResolverName.h"
11 #include "lldb/Breakpoint/BreakpointLocation.h"
12 #include "lldb/Core/Architecture.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/SymbolContext.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/Language.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/StreamString.h"
24 using namespace lldb_private;
26 BreakpointResolverName::BreakpointResolverName(
27 Breakpoint *bkpt, const char *name_cstr, FunctionNameType name_type_mask,
28 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
30 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
31 m_class_name(), m_regex(), m_match_type(type), m_language(language),
32 m_skip_prologue(skip_prologue) {
33 if (m_match_type == Breakpoint::Regexp) {
34 if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
35 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
38 log->Warning("function name regexp: \"%s\" did not compile.",
42 AddNameLookup(ConstString(name_cstr), name_type_mask);
46 BreakpointResolverName::BreakpointResolverName(
47 Breakpoint *bkpt, const char *names[], size_t num_names,
48 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
50 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
51 m_match_type(Breakpoint::Exact), m_language(language),
52 m_skip_prologue(skip_prologue) {
53 for (size_t i = 0; i < num_names; i++) {
54 AddNameLookup(ConstString(names[i]), name_type_mask);
58 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
59 std::vector<std::string> names,
60 FunctionNameType name_type_mask,
61 LanguageType language,
64 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
65 m_match_type(Breakpoint::Exact), m_language(language),
66 m_skip_prologue(skip_prologue) {
67 for (const std::string &name : names) {
68 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
72 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
73 RegularExpression &func_regex,
74 lldb::LanguageType language,
77 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
78 m_class_name(nullptr), m_regex(func_regex),
79 m_match_type(Breakpoint::Regexp), m_language(language),
80 m_skip_prologue(skip_prologue) {}
82 BreakpointResolverName::~BreakpointResolverName() = default;
84 BreakpointResolverName::BreakpointResolverName(
85 const BreakpointResolverName &rhs)
86 : BreakpointResolver(rhs.m_breakpoint, BreakpointResolver::NameResolver,
88 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
89 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
90 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
92 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData(
93 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
95 LanguageType language = eLanguageTypeUnknown;
96 llvm::StringRef language_name;
97 bool success = options_dict.GetValueForKeyAsString(
98 GetKey(OptionNames::LanguageName), language_name);
100 language = Language::GetLanguageTypeFromString(language_name);
101 if (language == eLanguageTypeUnknown) {
102 error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
108 lldb::addr_t offset = 0;
110 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
112 error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
117 success = options_dict.GetValueForKeyAsBoolean(
118 GetKey(OptionNames::SkipPrologue), skip_prologue);
120 error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
124 llvm::StringRef regex_text;
125 success = options_dict.GetValueForKeyAsString(
126 GetKey(OptionNames::RegexString), regex_text);
128 RegularExpression regex(regex_text);
129 return new BreakpointResolverName(bkpt, regex, language, offset,
132 StructuredData::Array *names_array;
133 success = options_dict.GetValueForKeyAsArray(
134 GetKey(OptionNames::SymbolNameArray), names_array);
136 error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
139 StructuredData::Array *names_mask_array;
140 success = options_dict.GetValueForKeyAsArray(
141 GetKey(OptionNames::NameMaskArray), names_mask_array);
143 error.SetErrorStringWithFormat(
144 "BRN::CFSD: Missing symbol names mask entry.");
148 size_t num_elem = names_array->GetSize();
149 if (num_elem != names_mask_array->GetSize()) {
150 error.SetErrorString(
151 "BRN::CFSD: names and names mask arrays have different sizes.");
156 error.SetErrorString(
157 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
160 std::vector<std::string> names;
161 std::vector<FunctionNameType> name_masks;
162 for (size_t i = 0; i < num_elem; i++) {
163 llvm::StringRef name;
165 success = names_array->GetItemAtIndexAsString(i, name);
167 error.SetErrorString("BRN::CFSD: name entry is not a string.");
170 std::underlying_type<FunctionNameType>::type fnt;
171 success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
173 error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
176 names.push_back(name);
177 name_masks.push_back(static_cast<FunctionNameType>(fnt));
180 BreakpointResolverName *resolver = new BreakpointResolverName(
181 bkpt, names[0].c_str(), name_masks[0], language,
182 Breakpoint::MatchType::Exact, offset, skip_prologue);
183 for (size_t i = 1; i < num_elem; i++) {
184 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
190 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
191 StructuredData::DictionarySP options_dict_sp(
192 new StructuredData::Dictionary());
194 if (m_regex.IsValid()) {
195 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
198 StructuredData::ArraySP names_sp(new StructuredData::Array());
199 StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
200 for (auto lookup : m_lookups) {
201 names_sp->AddItem(StructuredData::StringSP(
202 new StructuredData::String(lookup.GetName().AsCString())));
203 name_masks_sp->AddItem(StructuredData::IntegerSP(
204 new StructuredData::Integer(lookup.GetNameTypeMask())));
206 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
207 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
209 if (m_language != eLanguageTypeUnknown)
210 options_dict_sp->AddStringItem(
211 GetKey(OptionNames::LanguageName),
212 Language::GetNameForLanguageType(m_language));
213 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
216 return WrapOptionsDict(options_dict_sp);
219 void BreakpointResolverName::AddNameLookup(ConstString name,
220 FunctionNameType name_type_mask) {
222 Module::LookupInfo lookup(name, name_type_mask, m_language);
223 m_lookups.emplace_back(lookup);
225 auto add_variant_funcs = [&](Language *lang) {
226 for (ConstString variant_name : lang->GetMethodNameVariants(name)) {
227 Module::LookupInfo variant_lookup(name, name_type_mask,
228 lang->GetLanguageType());
229 variant_lookup.SetLookupName(variant_name);
230 m_lookups.emplace_back(variant_lookup);
235 if (Language *lang = Language::FindPlugin(m_language)) {
236 add_variant_funcs(lang);
238 // Most likely m_language is eLanguageTypeUnknown. We check each language for
239 // possible variants or more qualified names and create lookups for those as
241 Language::ForEach(add_variant_funcs);
245 // FIXME: Right now we look at the module level, and call the module's
247 // Greg says he will add function tables, maybe at the CompileUnit level to
248 // accelerate function lookup. At that point, we should switch the depth to
249 // CompileUnit, and look in these tables.
251 Searcher::CallbackReturn
252 BreakpointResolverName::SearchCallback(SearchFilter &filter,
253 SymbolContext &context, Address *addr,
255 SymbolContextList func_list;
256 // SymbolContextList sym_list;
261 assert(m_breakpoint != nullptr);
263 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
267 log->Warning("Class/method function specification not supported yet.\n");
268 return Searcher::eCallbackReturnStop;
271 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
272 bool filter_by_language = (m_language != eLanguageTypeUnknown);
273 const bool include_symbols = !filter_by_cu;
274 const bool include_inlines = true;
275 const bool append = true;
277 switch (m_match_type) {
278 case Breakpoint::Exact:
279 if (context.module_sp) {
280 for (const auto &lookup : m_lookups) {
281 const size_t start_func_idx = func_list.GetSize();
282 context.module_sp->FindFunctions(
283 lookup.GetLookupName(), nullptr, lookup.GetNameTypeMask(),
284 include_symbols, include_inlines, append, func_list);
286 const size_t end_func_idx = func_list.GetSize();
288 if (start_func_idx < end_func_idx)
289 lookup.Prune(func_list, start_func_idx);
293 case Breakpoint::Regexp:
294 if (context.module_sp) {
295 context.module_sp->FindFunctions(
297 !filter_by_cu, // include symbols only if we aren't filtering by CU
298 include_inlines, append, func_list);
301 case Breakpoint::Glob:
303 log->Warning("glob is not supported yet.");
307 // If the filter specifies a Compilation Unit, remove the ones that don't
308 // pass at this point.
309 if (filter_by_cu || filter_by_language) {
310 uint32_t num_functions = func_list.GetSize();
312 for (size_t idx = 0; idx < num_functions; idx++) {
313 bool remove_it = false;
315 func_list.GetContextAtIndex(idx, sc);
317 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
321 if (filter_by_language) {
322 LanguageType sym_language = sc.GetLanguage();
323 if ((Language::GetPrimaryLanguage(sym_language) !=
324 Language::GetPrimaryLanguage(m_language)) &&
325 (sym_language != eLanguageTypeUnknown)) {
331 func_list.RemoveContextAtIndex(idx);
338 // Remove any duplicates between the function list and the symbol list
340 if (func_list.GetSize()) {
341 for (i = 0; i < func_list.GetSize(); i++) {
342 if (func_list.GetContextAtIndex(i, sc)) {
343 bool is_reexported = false;
345 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
346 if (!sc.block->GetStartAddress(break_addr))
348 } else if (sc.function) {
349 break_addr = sc.function->GetAddressRange().GetBaseAddress();
350 if (m_skip_prologue && break_addr.IsValid()) {
351 const uint32_t prologue_byte_size =
352 sc.function->GetPrologueByteSize();
353 if (prologue_byte_size)
354 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
356 } else if (sc.symbol) {
357 if (sc.symbol->GetType() == eSymbolTypeReExported) {
358 const Symbol *actual_symbol =
359 sc.symbol->ResolveReExportedSymbol(m_breakpoint->GetTarget());
361 is_reexported = true;
362 break_addr = actual_symbol->GetAddress();
365 break_addr = sc.symbol->GetAddress();
368 if (m_skip_prologue && break_addr.IsValid()) {
369 const uint32_t prologue_byte_size =
370 sc.symbol->GetPrologueByteSize();
371 if (prologue_byte_size)
372 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
374 const Architecture *arch =
375 m_breakpoint->GetTarget().GetArchitecturePlugin();
377 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
382 if (break_addr.IsValid()) {
383 if (filter.AddressPasses(break_addr)) {
384 BreakpointLocationSP bp_loc_sp(
385 AddLocation(break_addr, &new_location));
386 bp_loc_sp->SetIsReExported(is_reexported);
387 if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) {
390 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
391 log->Printf("Added location: %s\n", s.GetData());
400 return Searcher::eCallbackReturnContinue;
403 lldb::SearchDepth BreakpointResolverName::GetDepth() {
404 return lldb::eSearchDepthModule;
407 void BreakpointResolverName::GetDescription(Stream *s) {
408 if (m_match_type == Breakpoint::Regexp)
409 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
411 size_t num_names = m_lookups.size();
413 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
415 s->Printf("names = {");
416 for (size_t i = 0; i < num_names; i++) {
417 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
418 m_lookups[i].GetName().GetCString());
423 if (m_language != eLanguageTypeUnknown) {
424 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
428 void BreakpointResolverName::Dump(Stream *s) const {}
430 lldb::BreakpointResolverSP
431 BreakpointResolverName::CopyForBreakpoint(Breakpoint &breakpoint) {
432 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
433 ret_sp->SetBreakpoint(&breakpoint);