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_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
90
92 const StructuredData::Dictionary &options_dict, Status &error) {
94 llvm::StringRef language_name;
95 bool success = options_dict.GetValueForKeyAsString(
96 GetKey(OptionNames::LanguageName), language_name);
97 if (success) {
98 language = Language::GetLanguageTypeFromString(language_name);
99 if (language == eLanguageTypeUnknown) {
101 "BRN::CFSD: Unknown language: {0}.", language_name);
102 return nullptr;
103 }
104 }
105
106 lldb::offset_t offset = 0;
107 success =
109 if (!success) {
110 error = Status::FromErrorString("BRN::CFSD: Missing offset entry.");
111 return nullptr;
112 }
113
114 bool skip_prologue;
115 success = options_dict.GetValueForKeyAsBoolean(
116 GetKey(OptionNames::SkipPrologue), skip_prologue);
117 if (!success) {
118 error = Status::FromErrorString("BRN::CFSD: Missing Skip prologue entry.");
119 return nullptr;
120 }
121
122 llvm::StringRef regex_text;
123 success = options_dict.GetValueForKeyAsString(
124 GetKey(OptionNames::RegexString), regex_text);
125 if (success) {
126 return std::make_shared<BreakpointResolverName>(
127 nullptr, RegularExpression(regex_text), language, offset,
128 skip_prologue);
129 }
130 StructuredData::Array *names_array;
131 success = options_dict.GetValueForKeyAsArray(
133 if (!success) {
134 error = Status::FromErrorString("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) {
142 "BRN::CFSD: Missing symbol names mask entry.");
143 return nullptr;
144 }
145
146 size_t num_elem = names_array->GetSize();
147 if (num_elem != names_mask_array->GetSize()) {
149 "BRN::CFSD: names and names mask arrays have different sizes.");
150 return nullptr;
151 }
152
153 if (num_elem == 0) {
155 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
156 return nullptr;
157 }
158 std::vector<std::string> names;
159 std::vector<FunctionNameType> name_masks;
160 for (size_t i = 0; i < num_elem; i++) {
161 std::optional<llvm::StringRef> maybe_name =
162 names_array->GetItemAtIndexAsString(i);
163 if (!maybe_name) {
164 error =
165 Status::FromErrorString("BRN::CFSD: name entry is not a string.");
166 return nullptr;
167 }
168 auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
169 std::underlying_type<FunctionNameType>::type>(i);
170 if (!maybe_fnt) {
172 "BRN::CFSD: name mask entry is not an integer.");
173 return nullptr;
174 }
175 names.push_back(std::string(*maybe_name));
176 name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
177 }
178
179 std::shared_ptr<BreakpointResolverName> resolver_sp =
180 std::make_shared<BreakpointResolverName>(
181 nullptr, names[0].c_str(), name_masks[0], language,
183 /*offset_is_insn_count = */ false, skip_prologue);
184 for (size_t i = 1; i < num_elem; i++) {
185 resolver_sp->AddNameLookup(ConstString(names[i]), name_masks[i]);
186 }
187 return resolver_sp;
188}
189
191 StructuredData::DictionarySP options_dict_sp(
193
194 if (m_regex.IsValid()) {
195 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
196 m_regex.GetText());
197 } else {
200 for (auto lookup : m_lookups) {
201 names_sp->AddItem(std::make_shared<StructuredData::String>(
202 lookup.GetName().GetStringRef()));
203 name_masks_sp->AddItem(std::make_shared<StructuredData::UnsignedInteger>(
204 lookup.GetNameTypeMask()));
205 }
206 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
207 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
208 }
210 options_dict_sp->AddStringItem(
213 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
215
216 return WrapOptionsDict(options_dict_sp);
217}
218
220 FunctionNameType name_type_mask) {
221 std::vector<Module::LookupInfo> infos =
222 Module::LookupInfo::MakeLookupInfos(name, name_type_mask, m_language);
223 llvm::append_range(m_lookups, infos);
224
225 auto add_variant_funcs = [&](Language *lang) {
226 for (Language::MethodNameVariant variant :
227 lang->GetMethodNameVariants(name)) {
228 // FIXME: Should we be adding variants that aren't of type Full?
229 if (variant.GetType() & lldb::eFunctionNameTypeFull) {
230 std::vector<Module::LookupInfo> variant_lookups =
231 Module::LookupInfo::MakeLookupInfos(name, variant.GetType(),
232 lang->GetLanguageType(),
233 variant.GetName());
234 llvm::append_range(m_lookups, variant_lookups);
235 }
236 }
238 };
239
241 add_variant_funcs(lang);
242 } else {
243 // Most likely m_language is eLanguageTypeUnknown. We check each language for
244 // possible variants or more qualified names and create lookups for those as
245 // well.
246 Language::ForEach(add_variant_funcs);
247 }
248}
249
250// FIXME: Right now we look at the module level, and call the module's
251// "FindFunctions".
252// Greg says he will add function tables, maybe at the CompileUnit level to
253// accelerate function lookup. At that point, we should switch the depth to
254// CompileUnit, and look in these tables.
255
258 SymbolContext &context, Address *addr) {
260
261 if (m_class_name) {
262 if (log)
263 log->Warning("Class/method function specification not supported yet.\n");
265 }
266
267 SymbolContextList func_list;
268 bool filter_by_cu =
269 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
270 bool filter_by_language = (m_language != eLanguageTypeUnknown);
271
272 ModuleFunctionSearchOptions function_options;
273 function_options.include_symbols = !filter_by_cu;
274 function_options.include_inlines = true;
275
276 switch (m_match_type) {
278 if (context.module_sp) {
279 for (const auto &lookup : m_lookups) {
280 const size_t start_func_idx = func_list.GetSize();
281 context.module_sp->FindFunctions(lookup, CompilerDeclContext(),
282 function_options, func_list);
283
284 const size_t end_func_idx = func_list.GetSize();
285
286 if (start_func_idx < end_func_idx)
287 lookup.Prune(func_list, start_func_idx);
288 }
289 }
290 break;
292 if (context.module_sp) {
293 context.module_sp->FindFunctions(m_regex, function_options, func_list);
294 }
295 break;
296 case Breakpoint::Glob:
297 if (log)
298 log->Warning("glob is not supported yet.");
299 break;
300 }
301
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();
306
307 for (size_t idx = 0; idx < num_functions; idx++) {
308 bool remove_it = false;
309 SymbolContext sc;
310 func_list.GetContextAtIndex(idx, sc);
311 if (filter_by_cu) {
312 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
313 remove_it = true;
314 }
315
316 if (filter_by_language) {
317 LanguageType sym_language = sc.GetLanguage();
318 if ((Language::GetPrimaryLanguage(sym_language) !=
320 (sym_language != eLanguageTypeUnknown)) {
321 remove_it = true;
322 }
323 }
324
325 if (remove_it) {
326 func_list.RemoveContextAtIndex(idx);
327 num_functions--;
328 idx--;
329 }
330 }
331 }
332
333 BreakpointSP breakpoint_sp = GetBreakpoint();
334 Breakpoint &breakpoint = *breakpoint_sp;
335 Address break_addr;
336
337 // Remove any duplicates between the function list and the symbol list
338 for (const SymbolContext &sc : func_list) {
339 bool is_reexported = false;
340
341 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
342 if (!sc.block->GetStartAddress(break_addr))
343 break_addr.Clear();
344 } else if (sc.function) {
345 break_addr = sc.function->GetAddress();
346 if (m_skip_prologue && break_addr.IsValid()) {
347 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
348 if (prologue_byte_size)
349 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
350 }
351 } else if (sc.symbol) {
352 if (sc.symbol->GetType() == eSymbolTypeReExported) {
353 const Symbol *actual_symbol =
354 sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget());
355 if (actual_symbol) {
356 is_reexported = true;
357 break_addr = actual_symbol->GetAddress();
358 }
359 } else {
360 break_addr = sc.symbol->GetAddress();
361 }
362
363 if (m_skip_prologue && break_addr.IsValid()) {
364 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
365 if (prologue_byte_size)
366 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
367 else {
368 const Architecture *arch =
369 breakpoint.GetTarget().GetArchitecturePlugin();
370 if (arch)
371 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
372 }
373 }
374 }
375
376 if (!break_addr.IsValid())
377 continue;
378
379 if (!filter.AddressPasses(break_addr))
380 continue;
381
382 bool new_location;
383 BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location));
384 bp_loc_sp->SetIsReExported(is_reexported);
385 if (bp_loc_sp && new_location && !breakpoint.IsInternal()) {
386 if (log) {
387 StreamString s;
388 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
389 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
390 }
391 }
392 }
393
395}
396
400
403 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
404 else {
405 // Since there may be many lookups objects for the same name breakpoint (one
406 // per language available), unique them by name, and operate on those unique
407 // names.
408 std::vector<ConstString> unique_lookups;
409 for (auto &lookup : m_lookups) {
410 if (!llvm::is_contained(unique_lookups, lookup.GetName()))
411 unique_lookups.push_back(lookup.GetName());
412 }
413 if (unique_lookups.size() == 1)
414 s->Printf("name = '%s'", unique_lookups[0].GetCString());
415 else {
416 size_t num_names = unique_lookups.size();
417 s->Printf("names = {");
418 for (size_t i = 0; i < num_names; i++) {
419 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
420 unique_lookups[i].GetCString());
421 }
422 s->Printf("}");
423 }
424 }
426 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
427 }
428}
429
431
435 ret_sp->SetBreakpoint(breakpoint);
436 return ret_sp;
437}
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:705
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