LLDB mainline
BreakpointResolverName.cpp
Go to the documentation of this file.
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
10
13#include "lldb/Core/Module.h"
14#include "lldb/Symbol/Block.h"
16#include "lldb/Symbol/Symbol.h"
19#include "lldb/Target/Target.h"
21#include "lldb/Utility/Log.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
28 const BreakpointSP &bkpt, const char *name_cstr,
29 FunctionNameType name_type_mask, LanguageType language,
30 Breakpoint::MatchType type, lldb::addr_t offset, bool offset_is_insn_count,
31 bool skip_prologue)
32 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset,
33 offset_is_insn_count),
34 m_match_type(type), m_language(language), m_skip_prologue(skip_prologue) {
35 if (m_match_type == Breakpoint::Regexp) {
36 m_regex = RegularExpression(name_cstr);
37 if (!m_regex.IsValid()) {
39
40 if (log)
41 log->Warning("function name regexp: \"%s\" did not compile.",
42 name_cstr);
43 }
44 } else {
45 AddNameLookup(ConstString(name_cstr), name_type_mask);
46 }
47}
48
50 const BreakpointSP &bkpt, const char *names[], size_t num_names,
51 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
52 bool skip_prologue)
53 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
54 m_match_type(Breakpoint::Exact), m_language(language),
55 m_skip_prologue(skip_prologue) {
56 for (size_t i = 0; i < num_names; i++) {
57 AddNameLookup(ConstString(names[i]), name_type_mask);
58 }
59}
60
62 const BreakpointSP &bkpt, const std::vector<std::string> &names,
63 FunctionNameType name_type_mask, LanguageType language, 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
74 RegularExpression func_regex,
75 lldb::LanguageType language,
76 lldb::addr_t offset,
77 bool skip_prologue)
79 m_regex(std::move(func_regex)), m_match_type(Breakpoint::Regexp),
80 m_language(language), m_skip_prologue(skip_prologue) {}
81
89
91 const StructuredData::Dictionary &options_dict, Status &error) {
93 llvm::StringRef language_name;
94 bool success = options_dict.GetValueForKeyAsString(
95 GetKey(OptionNames::LanguageName), language_name);
96 if (success) {
97 language = Language::GetLanguageTypeFromString(language_name);
98 if (language == eLanguageTypeUnknown) {
100 "BRN::CFSD: Unknown language: {0}.", language_name);
101 return nullptr;
102 }
103 }
104
105 lldb::offset_t offset = 0;
106 success =
108 if (!success) {
109 error = Status::FromErrorString("BRN::CFSD: Missing offset entry.");
110 return nullptr;
111 }
112
113 bool skip_prologue;
114 success = options_dict.GetValueForKeyAsBoolean(
115 GetKey(OptionNames::SkipPrologue), skip_prologue);
116 if (!success) {
117 error = Status::FromErrorString("BRN::CFSD: Missing Skip prologue entry.");
118 return nullptr;
119 }
120
121 llvm::StringRef regex_text;
122 success = options_dict.GetValueForKeyAsString(
123 GetKey(OptionNames::RegexString), regex_text);
124 if (success) {
125 return std::make_shared<BreakpointResolverName>(
126 nullptr, RegularExpression(regex_text), language, offset,
127 skip_prologue);
128 }
129 StructuredData::Array *names_array;
130 success = options_dict.GetValueForKeyAsArray(
132 if (!success) {
133 error = Status::FromErrorString("BRN::CFSD: Missing symbol names entry.");
134 return nullptr;
135 }
136 StructuredData::Array *names_mask_array;
137 success = options_dict.GetValueForKeyAsArray(
138 GetKey(OptionNames::NameMaskArray), names_mask_array);
139 if (!success) {
141 "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()) {
148 "BRN::CFSD: names and names mask arrays have different sizes.");
149 return nullptr;
150 }
151
152 if (num_elem == 0) {
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 std::optional<llvm::StringRef> maybe_name =
161 names_array->GetItemAtIndexAsString(i);
162 if (!maybe_name) {
163 error =
164 Status::FromErrorString("BRN::CFSD: name entry is not a string.");
165 return nullptr;
166 }
167 auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
168 std::underlying_type<FunctionNameType>::type>(i);
169 if (!maybe_fnt) {
171 "BRN::CFSD: name mask entry is not an integer.");
172 return nullptr;
173 }
174 names.push_back(std::string(*maybe_name));
175 name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
176 }
177
178 std::shared_ptr<BreakpointResolverName> resolver_sp =
179 std::make_shared<BreakpointResolverName>(
180 nullptr, names[0].c_str(), name_masks[0], language,
182 /*offset_is_insn_count = */ false, skip_prologue);
183 for (size_t i = 1; i < num_elem; i++) {
184 resolver_sp->AddNameLookup(ConstString(names[i]), name_masks[i]);
185 }
186 return resolver_sp;
187}
188
190 StructuredData::DictionarySP options_dict_sp(
192
193 if (m_regex.IsValid()) {
194 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
195 m_regex.GetText());
196 } else {
199 for (auto lookup : m_lookups) {
200 names_sp->AddItem(std::make_shared<StructuredData::String>(
201 lookup.GetName().GetStringRef()));
202 name_masks_sp->AddItem(std::make_shared<StructuredData::UnsignedInteger>(
203 lookup.GetNameTypeMask()));
204 }
205 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
206 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
207 }
209 options_dict_sp->AddStringItem(
212 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
214
215 return WrapOptionsDict(options_dict_sp);
216}
217
219 FunctionNameType name_type_mask) {
220 std::vector<Module::LookupInfo> infos =
221 Module::LookupInfo::MakeLookupInfos(name, name_type_mask, m_language);
222 llvm::append_range(m_lookups, infos);
223
224 auto add_variant_funcs = [&](Language *lang) {
225 for (Language::MethodNameVariant variant :
226 lang->GetMethodNameVariants(name)) {
227 // FIXME: Should we be adding variants that aren't of type Full?
228 if (variant.GetType() & lldb::eFunctionNameTypeFull) {
229 std::vector<Module::LookupInfo> variant_lookups =
230 Module::LookupInfo::MakeLookupInfos(name, variant.GetType(),
231 lang->GetLanguageType(),
232 variant.GetName());
233 llvm::append_range(m_lookups, variant_lookups);
234 }
235 }
237 };
238
240 add_variant_funcs(lang);
241 } else {
242 // Most likely m_language is eLanguageTypeUnknown. We check each language for
243 // possible variants or more qualified names and create lookups for those as
244 // well.
245 Language::ForEach(add_variant_funcs);
246 }
247}
248
249// FIXME: Right now we look at the module level, and call the module's
250// "FindFunctions".
251// Greg says he will add function tables, maybe at the CompileUnit level to
252// accelerate function lookup. At that point, we should switch the depth to
253// CompileUnit, and look in these tables.
254
257 SymbolContext &context, Address *addr) {
259
260 SymbolContextList func_list;
261 bool filter_by_cu =
262 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
263 bool filter_by_language = (m_language != eLanguageTypeUnknown);
264
265 ModuleFunctionSearchOptions function_options;
266 function_options.include_symbols = !filter_by_cu;
267 function_options.include_inlines = true;
268
269 switch (m_match_type) {
271 if (context.module_sp) {
272 for (const auto &lookup : m_lookups) {
273 const size_t start_func_idx = func_list.GetSize();
274 context.module_sp->FindFunctions(lookup, CompilerDeclContext(),
275 function_options, func_list);
276
277 const size_t end_func_idx = func_list.GetSize();
278
279 if (start_func_idx < end_func_idx)
280 lookup.Prune(func_list, start_func_idx);
281 }
282 }
283 break;
285 if (context.module_sp) {
286 context.module_sp->FindFunctions(m_regex, function_options, func_list);
287 }
288 break;
289 case Breakpoint::Glob:
290 if (log)
291 log->Warning("glob is not supported yet.");
292 break;
293 }
294
295 // If the filter specifies a Compilation Unit, remove the ones that don't
296 // pass at this point.
297 if (filter_by_cu || filter_by_language) {
298 uint32_t num_functions = func_list.GetSize();
299
300 for (size_t idx = 0; idx < num_functions; idx++) {
301 bool remove_it = false;
302 SymbolContext sc;
303 func_list.GetContextAtIndex(idx, sc);
304 if (filter_by_cu) {
305 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
306 remove_it = true;
307 }
308
309 if (filter_by_language) {
310 LanguageType sym_language = sc.GetLanguage();
311 if ((Language::GetPrimaryLanguage(sym_language) !=
313 (sym_language != eLanguageTypeUnknown)) {
314 remove_it = true;
315 }
316 }
317
318 if (remove_it) {
319 func_list.RemoveContextAtIndex(idx);
320 num_functions--;
321 idx--;
322 }
323 }
324 }
325
326 BreakpointSP breakpoint_sp = GetBreakpoint();
327 Breakpoint &breakpoint = *breakpoint_sp;
328 Address break_addr;
329
330 // Remove any duplicates between the function list and the symbol list
331 for (const SymbolContext &sc : func_list) {
332 bool is_reexported = false;
333
334 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
335 if (!sc.block->GetStartAddress(break_addr))
336 break_addr.Clear();
337 } else if (sc.function) {
338 break_addr = sc.function->GetAddress();
339 if (m_skip_prologue && break_addr.IsValid()) {
340 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
341 if (prologue_byte_size)
342 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
343 }
344 } else if (sc.symbol) {
345 if (sc.symbol->GetType() == eSymbolTypeReExported) {
346 const Symbol *actual_symbol =
347 sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget());
348 if (actual_symbol) {
349 is_reexported = true;
350 break_addr = actual_symbol->GetAddress();
351 }
352 } else {
353 break_addr = sc.symbol->GetAddress();
354 }
355
356 if (m_skip_prologue && break_addr.IsValid()) {
357 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
358 if (prologue_byte_size)
359 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
360 else {
361 const Architecture *arch =
362 breakpoint.GetTarget().GetArchitecturePlugin();
363 if (arch)
364 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
365 }
366 }
367 }
368
369 if (!break_addr.IsValid())
370 continue;
371
372 if (!filter.AddressPasses(break_addr))
373 continue;
374
375 bool new_location;
376 BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location));
377 bp_loc_sp->SetIsReExported(is_reexported);
378 if (bp_loc_sp && new_location && !breakpoint.IsInternal()) {
379 if (log) {
380 StreamString s;
381 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
382 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
383 }
384 }
385 }
386
388}
389
393
396 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
397 else {
398 // Since there may be many lookups objects for the same name breakpoint (one
399 // per language available), unique them by name, and operate on those unique
400 // names.
401 std::vector<ConstString> unique_lookups;
402 for (auto &lookup : m_lookups) {
403 if (!llvm::is_contained(unique_lookups, lookup.GetName()))
404 unique_lookups.push_back(lookup.GetName());
405 }
406 if (unique_lookups.size() == 1)
407 s->Printf("name = '%s'", unique_lookups[0].GetCString());
408 else {
409 size_t num_names = unique_lookups.size();
410 s->Printf("names = {");
411 for (size_t i = 0; i < num_names; i++) {
412 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
413 unique_lookups[i].GetCString());
414 }
415 s->Printf("}");
416 }
417 }
419 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
420 }
421}
422
424
428 ret_sp->SetBreakpoint(breakpoint);
429 return ret_sp;
430}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:376
A section + offset based address class.
Definition Address.h:62
void Clear()
Clear the object's state.
Definition Address.h:181
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition Address.h:329
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition Address.h:441
virtual void AdjustBreakpointAddress(const Symbol &func, Address &addr) const
Adjust function breakpoint address, if needed.
void AddNameLookup(ConstString name, lldb::FunctionNameType name_type_mask)
std::vector< Module::LookupInfo > m_lookups
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
BreakpointResolverName(const lldb::BreakpointSP &bkpt, const char *name, lldb::FunctionNameType name_type_mask, lldb::LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset, bool offset_is_insn_count, bool skip_prologue)
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
StructuredData::ObjectSP SerializeToStructuredData() override
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &data_dict, Status &error)
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
General Outline: The BreakpointResolver is a Searcher.
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
static const char * GetKey(OptionNames enum_value)
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
lldb::addr_t GetOffsetIsInsnCount() const
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
BreakpointResolver(const lldb::BreakpointSP &bkpt, unsigned char resolverType, lldb::addr_t offset=0, bool offset_is_insn_count=false)
The breakpoint resolver need to have a breakpoint for "ResolveBreakpoint to make sense.
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition Breakpoint.h:81
MatchType
An enum specifying the match style for breakpoint settings.
Definition Breakpoint.h:88
Target & GetTarget()
Accessor for the breakpoint Target.
Definition Breakpoint.h:493
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Represents a generic declaration context in a program.
A uniqued constant string class.
Definition ConstString.h:40
static void ForEach(llvm::function_ref< IterationAction(Language *)> callback)
Definition Language.cpp:131
static Language * FindPlugin(lldb::LanguageType language)
Definition Language.cpp:84
static const char * GetNameForLanguageType(lldb::LanguageType language)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:309
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition Language.cpp:412
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
void void void void void Warning(const char *fmt,...) __attribute__((format(printf
Definition Log.cpp:211
static std::vector< LookupInfo > MakeLookupInfos(ConstString name, lldb::FunctionNameType name_type_mask, lldb::LanguageType lang_type, ConstString lookup_name_override={})
Creates a vector of lookup infos for function name resolution.
Definition Module.cpp:713
General Outline: Provides the callback and search depth for the SearchFilter search.
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
virtual bool CompUnitPasses(FileSpec &fileSpec)
Call this method with a FileSpec to see if file spec passes the filter as the name of a compilation u...
virtual uint32_t GetFilterRequiredItems()
This determines which items are REQUIRED for the filter to pass.
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
std::optional< IntType > GetItemAtIndexAsInteger(size_t idx) const
std::optional< llvm::StringRef > GetItemAtIndexAsString(size_t idx) const
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
lldb::LanguageType GetLanguage() const
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
Address GetAddress() const
Definition Symbol.h:89
Symbol * ResolveReExportedSymbol(Target &target) const
Definition Symbol.cpp:483
Architecture * GetArchitecturePlugin() const
Definition Target.h:1192
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
@ eDescriptionLevelVerbose
uint64_t offset_t
Definition lldb-types.h:85
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
@ eSymbolTypeReExported
uint64_t addr_t
Definition lldb-types.h:80
Options used by Module::FindFunctions.
Definition Module.h:66
bool include_inlines
Include inlined functions.
Definition Module.h:70
bool include_symbols
Include the symbol table.
Definition Module.h:68