1 //===-- BreakpointResolverName.cpp ----------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Breakpoint/BreakpointResolverName.h"
10
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"
22
23 using namespace lldb;
24 using namespace lldb_private;
25
BreakpointResolverName(const BreakpointSP & bkpt,const char * name_cstr,FunctionNameType name_type_mask,LanguageType language,Breakpoint::MatchType type,lldb::addr_t offset,bool skip_prologue)26 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
27 const char *name_cstr, FunctionNameType name_type_mask,
28 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
29 bool skip_prologue)
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 m_regex = RegularExpression(llvm::StringRef::withNullAsEmpty(name_cstr));
35 if (!m_regex.IsValid()) {
36 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
37
38 if (log)
39 log->Warning("function name regexp: \"%s\" did not compile.",
40 name_cstr);
41 }
42 } else {
43 AddNameLookup(ConstString(name_cstr), name_type_mask);
44 }
45 }
46
BreakpointResolverName(const BreakpointSP & bkpt,const char * names[],size_t num_names,FunctionNameType name_type_mask,LanguageType language,lldb::addr_t offset,bool skip_prologue)47 BreakpointResolverName::BreakpointResolverName(
48 const BreakpointSP &bkpt, const char *names[], size_t num_names,
49 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
50 bool skip_prologue)
51 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
52 m_match_type(Breakpoint::Exact), m_language(language),
53 m_skip_prologue(skip_prologue) {
54 for (size_t i = 0; i < num_names; i++) {
55 AddNameLookup(ConstString(names[i]), name_type_mask);
56 }
57 }
58
BreakpointResolverName(const BreakpointSP & bkpt,std::vector<std::string> names,FunctionNameType name_type_mask,LanguageType language,lldb::addr_t offset,bool skip_prologue)59 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
60 std::vector<std::string> names,
61 FunctionNameType name_type_mask,
62 LanguageType language,
63 lldb::addr_t offset,
64 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);
70 }
71 }
72
BreakpointResolverName(const BreakpointSP & bkpt,RegularExpression func_regex,lldb::LanguageType language,lldb::addr_t offset,bool skip_prologue)73 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
74 RegularExpression func_regex,
75 lldb::LanguageType language,
76 lldb::addr_t offset,
77 bool skip_prologue)
78 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
79 m_class_name(nullptr), m_regex(std::move(func_regex)),
80 m_match_type(Breakpoint::Regexp), m_language(language),
81 m_skip_prologue(skip_prologue) {}
82
BreakpointResolverName(const BreakpointResolverName & rhs)83 BreakpointResolverName::BreakpointResolverName(
84 const BreakpointResolverName &rhs)
85 : BreakpointResolver(rhs.GetBreakpoint(), BreakpointResolver::NameResolver,
86 rhs.GetOffset()),
87 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
88 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
89 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
90
CreateFromStructuredData(const BreakpointSP & bkpt,const StructuredData::Dictionary & options_dict,Status & error)91 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData(
92 const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
93 Status &error) {
94 LanguageType language = eLanguageTypeUnknown;
95 llvm::StringRef language_name;
96 bool success = options_dict.GetValueForKeyAsString(
97 GetKey(OptionNames::LanguageName), language_name);
98 if (success) {
99 language = Language::GetLanguageTypeFromString(language_name);
100 if (language == eLanguageTypeUnknown) {
101 error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
102 language_name);
103 return nullptr;
104 }
105 }
106
107 lldb::addr_t offset = 0;
108 success =
109 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
110 if (!success) {
111 error.SetErrorString("BRN::CFSD: Missing offset entry.");
112 return nullptr;
113 }
114
115 bool skip_prologue;
116 success = options_dict.GetValueForKeyAsBoolean(
117 GetKey(OptionNames::SkipPrologue), skip_prologue);
118 if (!success) {
119 error.SetErrorString("BRN::CFSD: Missing Skip prologue entry.");
120 return nullptr;
121 }
122
123 llvm::StringRef regex_text;
124 success = options_dict.GetValueForKeyAsString(
125 GetKey(OptionNames::RegexString), regex_text);
126 if (success) {
127 return new BreakpointResolverName(bkpt, RegularExpression(regex_text),
128 language, offset, skip_prologue);
129 } else {
130 StructuredData::Array *names_array;
131 success = options_dict.GetValueForKeyAsArray(
132 GetKey(OptionNames::SymbolNameArray), names_array);
133 if (!success) {
134 error.SetErrorString("BRN::CFSD: Missing symbol names entry.");
135 return nullptr;
136 }
137 StructuredData::Array *names_mask_array;
138 success = options_dict.GetValueForKeyAsArray(
139 GetKey(OptionNames::NameMaskArray), names_mask_array);
140 if (!success) {
141 error.SetErrorString("BRN::CFSD: Missing symbol names mask entry.");
142 return nullptr;
143 }
144
145 size_t num_elem = names_array->GetSize();
146 if (num_elem != names_mask_array->GetSize()) {
147 error.SetErrorString(
148 "BRN::CFSD: names and names mask arrays have different sizes.");
149 return nullptr;
150 }
151
152 if (num_elem == 0) {
153 error.SetErrorString(
154 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
155 return nullptr;
156 }
157 std::vector<std::string> names;
158 std::vector<FunctionNameType> name_masks;
159 for (size_t i = 0; i < num_elem; i++) {
160 llvm::StringRef name;
161
162 success = names_array->GetItemAtIndexAsString(i, name);
163 if (!success) {
164 error.SetErrorString("BRN::CFSD: name entry is not a string.");
165 return nullptr;
166 }
167 std::underlying_type<FunctionNameType>::type fnt;
168 success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
169 if (!success) {
170 error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
171 return nullptr;
172 }
173 names.push_back(std::string(name));
174 name_masks.push_back(static_cast<FunctionNameType>(fnt));
175 }
176
177 BreakpointResolverName *resolver = new BreakpointResolverName(
178 bkpt, names[0].c_str(), name_masks[0], language,
179 Breakpoint::MatchType::Exact, offset, skip_prologue);
180 for (size_t i = 1; i < num_elem; i++) {
181 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
182 }
183 return resolver;
184 }
185 }
186
SerializeToStructuredData()187 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() {
188 StructuredData::DictionarySP options_dict_sp(
189 new StructuredData::Dictionary());
190
191 if (m_regex.IsValid()) {
192 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
193 m_regex.GetText());
194 } else {
195 StructuredData::ArraySP names_sp(new StructuredData::Array());
196 StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
197 for (auto lookup : m_lookups) {
198 names_sp->AddItem(StructuredData::StringSP(
199 new StructuredData::String(lookup.GetName().GetStringRef())));
200 name_masks_sp->AddItem(StructuredData::IntegerSP(
201 new StructuredData::Integer(lookup.GetNameTypeMask())));
202 }
203 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
204 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
205 }
206 if (m_language != eLanguageTypeUnknown)
207 options_dict_sp->AddStringItem(
208 GetKey(OptionNames::LanguageName),
209 Language::GetNameForLanguageType(m_language));
210 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
211 m_skip_prologue);
212
213 return WrapOptionsDict(options_dict_sp);
214 }
215
AddNameLookup(ConstString name,FunctionNameType name_type_mask)216 void BreakpointResolverName::AddNameLookup(ConstString name,
217 FunctionNameType name_type_mask) {
218
219 Module::LookupInfo lookup(name, name_type_mask, m_language);
220 m_lookups.emplace_back(lookup);
221
222 auto add_variant_funcs = [&](Language *lang) {
223 for (ConstString variant_name : lang->GetMethodNameVariants(name)) {
224 Module::LookupInfo variant_lookup(name, name_type_mask,
225 lang->GetLanguageType());
226 variant_lookup.SetLookupName(variant_name);
227 m_lookups.emplace_back(variant_lookup);
228 }
229 return true;
230 };
231
232 if (Language *lang = Language::FindPlugin(m_language)) {
233 add_variant_funcs(lang);
234 } else {
235 // Most likely m_language is eLanguageTypeUnknown. We check each language for
236 // possible variants or more qualified names and create lookups for those as
237 // well.
238 Language::ForEach(add_variant_funcs);
239 }
240 }
241
242 // FIXME: Right now we look at the module level, and call the module's
243 // "FindFunctions".
244 // Greg says he will add function tables, maybe at the CompileUnit level to
245 // accelerate function lookup. At that point, we should switch the depth to
246 // CompileUnit, and look in these tables.
247
248 Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)249 BreakpointResolverName::SearchCallback(SearchFilter &filter,
250 SymbolContext &context, Address *addr) {
251 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
252
253 if (m_class_name) {
254 if (log)
255 log->Warning("Class/method function specification not supported yet.\n");
256 return Searcher::eCallbackReturnStop;
257 }
258
259 SymbolContextList func_list;
260 bool filter_by_cu =
261 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
262 bool filter_by_language = (m_language != eLanguageTypeUnknown);
263 const bool include_symbols = !filter_by_cu;
264 const bool include_inlines = true;
265
266 switch (m_match_type) {
267 case Breakpoint::Exact:
268 if (context.module_sp) {
269 for (const auto &lookup : m_lookups) {
270 const size_t start_func_idx = func_list.GetSize();
271 context.module_sp->FindFunctions(
272 lookup.GetLookupName(), CompilerDeclContext(),
273 lookup.GetNameTypeMask(), include_symbols, include_inlines,
274 func_list);
275
276 const size_t end_func_idx = func_list.GetSize();
277
278 if (start_func_idx < end_func_idx)
279 lookup.Prune(func_list, start_func_idx);
280 }
281 }
282 break;
283 case Breakpoint::Regexp:
284 if (context.module_sp) {
285 context.module_sp->FindFunctions(
286 m_regex,
287 !filter_by_cu, // include symbols only if we aren't filtering by CU
288 include_inlines, func_list);
289 }
290 break;
291 case Breakpoint::Glob:
292 if (log)
293 log->Warning("glob is not supported yet.");
294 break;
295 }
296
297 // If the filter specifies a Compilation Unit, remove the ones that don't
298 // pass at this point.
299 if (filter_by_cu || filter_by_language) {
300 uint32_t num_functions = func_list.GetSize();
301
302 for (size_t idx = 0; idx < num_functions; idx++) {
303 bool remove_it = false;
304 SymbolContext sc;
305 func_list.GetContextAtIndex(idx, sc);
306 if (filter_by_cu) {
307 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
308 remove_it = true;
309 }
310
311 if (filter_by_language) {
312 LanguageType sym_language = sc.GetLanguage();
313 if ((Language::GetPrimaryLanguage(sym_language) !=
314 Language::GetPrimaryLanguage(m_language)) &&
315 (sym_language != eLanguageTypeUnknown)) {
316 remove_it = true;
317 }
318 }
319
320 if (remove_it) {
321 func_list.RemoveContextAtIndex(idx);
322 num_functions--;
323 idx--;
324 }
325 }
326 }
327
328 BreakpointSP breakpoint_sp = GetBreakpoint();
329 Breakpoint &breakpoint = *breakpoint_sp;
330 Address break_addr;
331
332 // Remove any duplicates between the function list and the symbol list
333 SymbolContext sc;
334 if (!func_list.GetSize())
335 return Searcher::eCallbackReturnContinue;
336
337 for (uint32_t i = 0; i < func_list.GetSize(); i++) {
338 if (!func_list.GetContextAtIndex(i, sc))
339 continue;
340
341 bool is_reexported = false;
342
343 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
344 if (!sc.block->GetStartAddress(break_addr))
345 break_addr.Clear();
346 } else if (sc.function) {
347 break_addr = sc.function->GetAddressRange().GetBaseAddress();
348 if (m_skip_prologue && break_addr.IsValid()) {
349 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
350 if (prologue_byte_size)
351 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
352 }
353 } else if (sc.symbol) {
354 if (sc.symbol->GetType() == eSymbolTypeReExported) {
355 const Symbol *actual_symbol =
356 sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget());
357 if (actual_symbol) {
358 is_reexported = true;
359 break_addr = actual_symbol->GetAddress();
360 }
361 } else {
362 break_addr = sc.symbol->GetAddress();
363 }
364
365 if (m_skip_prologue && break_addr.IsValid()) {
366 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
367 if (prologue_byte_size)
368 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
369 else {
370 const Architecture *arch =
371 breakpoint.GetTarget().GetArchitecturePlugin();
372 if (arch)
373 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
374 }
375 }
376 }
377
378 if (!break_addr.IsValid())
379 continue;
380
381 if (!filter.AddressPasses(break_addr))
382 continue;
383
384 bool new_location;
385 BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location));
386 bp_loc_sp->SetIsReExported(is_reexported);
387 if (bp_loc_sp && new_location && !breakpoint.IsInternal()) {
388 if (log) {
389 StreamString s;
390 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
391 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
392 }
393 }
394 }
395
396 return Searcher::eCallbackReturnContinue;
397 }
398
GetDepth()399 lldb::SearchDepth BreakpointResolverName::GetDepth() {
400 return lldb::eSearchDepthModule;
401 }
402
GetDescription(Stream * s)403 void BreakpointResolverName::GetDescription(Stream *s) {
404 if (m_match_type == Breakpoint::Regexp)
405 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
406 else {
407 size_t num_names = m_lookups.size();
408 if (num_names == 1)
409 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
410 else {
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());
415 }
416 s->Printf("}");
417 }
418 }
419 if (m_language != eLanguageTypeUnknown) {
420 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
421 }
422 }
423
Dump(Stream * s) const424 void BreakpointResolverName::Dump(Stream *s) const {}
425
426 lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)427 BreakpointResolverName::CopyForBreakpoint(BreakpointSP &breakpoint) {
428 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
429 ret_sp->SetBreakpoint(breakpoint);
430 return ret_sp;
431 }
432