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 llvm::for_each(variant_lookups, [&](auto &variant_lookup) {
234 variant_lookup.SetLookupName(variant.GetName());
235 });
236 llvm::append_range(m_lookups, variant_lookups);
237 }
238 }
240 };
241
243 add_variant_funcs(lang);
244 } else {
245 // Most likely m_language is eLanguageTypeUnknown. We check each language for
246 // possible variants or more qualified names and create lookups for those as
247 // well.
248 Language::ForEach(add_variant_funcs);
249 }
250}
251
252// FIXME: Right now we look at the module level, and call the module's
253// "FindFunctions".
254// Greg says he will add function tables, maybe at the CompileUnit level to
255// accelerate function lookup. At that point, we should switch the depth to
256// CompileUnit, and look in these tables.
257
260 SymbolContext &context, Address *addr) {
262
263 if (m_class_name) {
264 if (log)
265 log->Warning("Class/method function specification not supported yet.\n");
267 }
268
269 SymbolContextList func_list;
270 bool filter_by_cu =
271 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
272 bool filter_by_language = (m_language != eLanguageTypeUnknown);
273
274 ModuleFunctionSearchOptions function_options;
275 function_options.include_symbols = !filter_by_cu;
276 function_options.include_inlines = true;
277
278 switch (m_match_type) {
280 if (context.module_sp) {
281 for (const auto &lookup : m_lookups) {
282 const size_t start_func_idx = func_list.GetSize();
283 context.module_sp->FindFunctions(lookup, CompilerDeclContext(),
284 function_options, func_list);
285
286 const size_t end_func_idx = func_list.GetSize();
287
288 if (start_func_idx < end_func_idx)
289 lookup.Prune(func_list, start_func_idx);
290 }
291 }
292 break;
294 if (context.module_sp) {
295 context.module_sp->FindFunctions(m_regex, function_options, func_list);
296 }
297 break;
298 case Breakpoint::Glob:
299 if (log)
300 log->Warning("glob is not supported yet.");
301 break;
302 }
303
304 // If the filter specifies a Compilation Unit, remove the ones that don't
305 // pass at this point.
306 if (filter_by_cu || filter_by_language) {
307 uint32_t num_functions = func_list.GetSize();
308
309 for (size_t idx = 0; idx < num_functions; idx++) {
310 bool remove_it = false;
311 SymbolContext sc;
312 func_list.GetContextAtIndex(idx, sc);
313 if (filter_by_cu) {
314 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
315 remove_it = true;
316 }
317
318 if (filter_by_language) {
319 LanguageType sym_language = sc.GetLanguage();
320 if ((Language::GetPrimaryLanguage(sym_language) !=
322 (sym_language != eLanguageTypeUnknown)) {
323 remove_it = true;
324 }
325 }
326
327 if (remove_it) {
328 func_list.RemoveContextAtIndex(idx);
329 num_functions--;
330 idx--;
331 }
332 }
333 }
334
335 BreakpointSP breakpoint_sp = GetBreakpoint();
336 Breakpoint &breakpoint = *breakpoint_sp;
337 Address break_addr;
338
339 // Remove any duplicates between the function list and the symbol list
340 for (const SymbolContext &sc : func_list) {
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->GetAddress();
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
397}
398
402
405 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
406 else {
407 // Since there may be many lookups objects for the same name breakpoint (one
408 // per language available), unique them by name, and operate on those unique
409 // names.
410 std::vector<ConstString> unique_lookups;
411 for (auto &lookup : m_lookups) {
412 if (!llvm::is_contained(unique_lookups, lookup.GetName()))
413 unique_lookups.push_back(lookup.GetName());
414 }
415 if (unique_lookups.size() == 1)
416 s->Printf("name = '%s'", unique_lookups[0].GetCString());
417 else {
418 size_t num_names = unique_lookups.size();
419 s->Printf("names = {");
420 for (size_t i = 0; i < num_names; i++) {
421 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
422 unique_lookups[i].GetCString());
423 }
424 s->Printf("}");
425 }
426 }
428 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
429 }
430}
431
433
437 ret_sp->SetBreakpoint(breakpoint);
438 return ret_sp;
439}
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)
Creates a vector of lookup infos for function name resolution.
Definition Module.cpp:700
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