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 char *name_cstr, FunctionNameType name_type_mask,
29 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
30 bool skip_prologue)
31 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
32 m_match_type(type), m_language(language), m_skip_prologue(skip_prologue) {
33 if (m_match_type == Breakpoint::Regexp) {
34 m_regex = RegularExpression(name_cstr);
35 if (!m_regex.IsValid()) {
36 Log *log = GetLog(LLDBLog::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
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
60 const BreakpointSP &bkpt, const std::vector<std::string> &names,
61 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
62 bool skip_prologue)
63 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
64 m_match_type(Breakpoint::Exact), m_language(language),
65 m_skip_prologue(skip_prologue) {
66 for (const std::string &name : names) {
67 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
68 }
69}
70
72 RegularExpression func_regex,
73 lldb::LanguageType language,
74 lldb::addr_t offset,
75 bool skip_prologue)
76 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
77 m_class_name(nullptr), m_regex(std::move(func_regex)),
78 m_match_type(Breakpoint::Regexp), m_language(language),
79 m_skip_prologue(skip_prologue) {}
80
82 const BreakpointResolverName &rhs)
83 : BreakpointResolver(rhs.GetBreakpoint(), BreakpointResolver::NameResolver,
84 rhs.GetOffset()),
85 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
86 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
87 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
88
90 const StructuredData::Dictionary &options_dict, Status &error) {
92 llvm::StringRef language_name;
93 bool success = options_dict.GetValueForKeyAsString(
94 GetKey(OptionNames::LanguageName), language_name);
95 if (success) {
96 language = Language::GetLanguageTypeFromString(language_name);
97 if (language == eLanguageTypeUnknown) {
99 "BRN::CFSD: Unknown language: {0}.", language_name);
100 return nullptr;
101 }
102 }
103
104 lldb::offset_t offset = 0;
105 success =
107 if (!success) {
108 error = Status::FromErrorString("BRN::CFSD: Missing offset entry.");
109 return nullptr;
110 }
111
112 bool skip_prologue;
113 success = options_dict.GetValueForKeyAsBoolean(
114 GetKey(OptionNames::SkipPrologue), skip_prologue);
115 if (!success) {
116 error = Status::FromErrorString("BRN::CFSD: Missing Skip prologue entry.");
117 return nullptr;
118 }
119
120 llvm::StringRef regex_text;
121 success = options_dict.GetValueForKeyAsString(
122 GetKey(OptionNames::RegexString), regex_text);
123 if (success) {
124 return std::make_shared<BreakpointResolverName>(
125 nullptr, RegularExpression(regex_text), language, offset,
126 skip_prologue);
127 } else {
128 StructuredData::Array *names_array;
129 success = options_dict.GetValueForKeyAsArray(
131 if (!success) {
132 error = Status::FromErrorString("BRN::CFSD: Missing symbol names entry.");
133 return nullptr;
134 }
135 StructuredData::Array *names_mask_array;
136 success = options_dict.GetValueForKeyAsArray(
137 GetKey(OptionNames::NameMaskArray), names_mask_array);
138 if (!success) {
140 "BRN::CFSD: Missing symbol names mask entry.");
141 return nullptr;
142 }
143
144 size_t num_elem = names_array->GetSize();
145 if (num_elem != names_mask_array->GetSize()) {
147 "BRN::CFSD: names and names mask arrays have different sizes.");
148 return nullptr;
149 }
150
151 if (num_elem == 0) {
153 "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
154 return nullptr;
155 }
156 std::vector<std::string> names;
157 std::vector<FunctionNameType> name_masks;
158 for (size_t i = 0; i < num_elem; i++) {
159 std::optional<llvm::StringRef> maybe_name =
160 names_array->GetItemAtIndexAsString(i);
161 if (!maybe_name) {
162 error =
163 Status::FromErrorString("BRN::CFSD: name entry is not a string.");
164 return nullptr;
165 }
166 auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
167 std::underlying_type<FunctionNameType>::type>(i);
168 if (!maybe_fnt) {
170 "BRN::CFSD: name mask entry is not an integer.");
171 return nullptr;
172 }
173 names.push_back(std::string(*maybe_name));
174 name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
175 }
176
177 std::shared_ptr<BreakpointResolverName> resolver_sp =
178 std::make_shared<BreakpointResolverName>(
179 nullptr, names[0].c_str(), name_masks[0], language,
180 Breakpoint::MatchType::Exact, offset, skip_prologue);
181 for (size_t i = 1; i < num_elem; i++) {
182 resolver_sp->AddNameLookup(ConstString(names[i]), name_masks[i]);
183 }
184 return resolver_sp;
185 }
186}
187
189 StructuredData::DictionarySP options_dict_sp(
191
192 if (m_regex.IsValid()) {
193 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
194 m_regex.GetText());
195 } else {
198 for (auto lookup : m_lookups) {
199 names_sp->AddItem(StructuredData::StringSP(
200 new StructuredData::String(lookup.GetName().GetStringRef())));
201 name_masks_sp->AddItem(StructuredData::UnsignedIntegerSP(
202 new StructuredData::UnsignedInteger(lookup.GetNameTypeMask())));
203 }
204 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
205 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
206 }
208 options_dict_sp->AddStringItem(
211 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
213
214 return WrapOptionsDict(options_dict_sp);
215}
216
218 FunctionNameType name_type_mask) {
219
220 Module::LookupInfo lookup(name, name_type_mask, m_language);
221 m_lookups.emplace_back(lookup);
222
223 auto add_variant_funcs = [&](Language *lang) {
224 for (Language::MethodNameVariant variant :
225 lang->GetMethodNameVariants(name)) {
226 // FIXME: Should we be adding variants that aren't of type Full?
227 if (variant.GetType() & lldb::eFunctionNameTypeFull) {
228 Module::LookupInfo variant_lookup(name, variant.GetType(),
229 lang->GetLanguageType());
230 variant_lookup.SetLookupName(variant.GetName());
231 m_lookups.emplace_back(variant_lookup);
232 }
233 }
234 return true;
235 };
236
238 add_variant_funcs(lang);
239 } else {
240 // Most likely m_language is eLanguageTypeUnknown. We check each language for
241 // possible variants or more qualified names and create lookups for those as
242 // well.
243 Language::ForEach(add_variant_funcs);
244 }
245}
246
247// FIXME: Right now we look at the module level, and call the module's
248// "FindFunctions".
249// Greg says he will add function tables, maybe at the CompileUnit level to
250// accelerate function lookup. At that point, we should switch the depth to
251// CompileUnit, and look in these tables.
252
255 SymbolContext &context, Address *addr) {
257
258 if (m_class_name) {
259 if (log)
260 log->Warning("Class/method function specification not supported yet.\n");
262 }
263
264 SymbolContextList func_list;
265 bool filter_by_cu =
266 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
267 bool filter_by_language = (m_language != eLanguageTypeUnknown);
268
269 ModuleFunctionSearchOptions function_options;
270 function_options.include_symbols = !filter_by_cu;
271 function_options.include_inlines = true;
272
273 switch (m_match_type) {
275 if (context.module_sp) {
276 for (const auto &lookup : m_lookups) {
277 const size_t start_func_idx = func_list.GetSize();
278 context.module_sp->FindFunctions(lookup, CompilerDeclContext(),
279 function_options, func_list);
280
281 const size_t end_func_idx = func_list.GetSize();
282
283 if (start_func_idx < end_func_idx)
284 lookup.Prune(func_list, start_func_idx);
285 }
286 }
287 break;
289 if (context.module_sp) {
290 context.module_sp->FindFunctions(m_regex, function_options, func_list);
291 }
292 break;
293 case Breakpoint::Glob:
294 if (log)
295 log->Warning("glob is not supported yet.");
296 break;
297 }
298
299 // If the filter specifies a Compilation Unit, remove the ones that don't
300 // pass at this point.
301 if (filter_by_cu || filter_by_language) {
302 uint32_t num_functions = func_list.GetSize();
303
304 for (size_t idx = 0; idx < num_functions; idx++) {
305 bool remove_it = false;
306 SymbolContext sc;
307 func_list.GetContextAtIndex(idx, sc);
308 if (filter_by_cu) {
309 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
310 remove_it = true;
311 }
312
313 if (filter_by_language) {
314 LanguageType sym_language = sc.GetLanguage();
315 if ((Language::GetPrimaryLanguage(sym_language) !=
317 (sym_language != eLanguageTypeUnknown)) {
318 remove_it = true;
319 }
320 }
321
322 if (remove_it) {
323 func_list.RemoveContextAtIndex(idx);
324 num_functions--;
325 idx--;
326 }
327 }
328 }
329
330 BreakpointSP breakpoint_sp = GetBreakpoint();
331 Breakpoint &breakpoint = *breakpoint_sp;
332 Address break_addr;
333
334 // Remove any duplicates between the function list and the symbol list
335 for (const SymbolContext &sc : func_list) {
336 bool is_reexported = false;
337
338 if (sc.block && sc.block->GetInlinedFunctionInfo()) {
339 if (!sc.block->GetStartAddress(break_addr))
340 break_addr.Clear();
341 } else if (sc.function) {
342 break_addr = sc.function->GetAddressRange().GetBaseAddress();
343 if (m_skip_prologue && break_addr.IsValid()) {
344 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
345 if (prologue_byte_size)
346 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
347 }
348 } else if (sc.symbol) {
349 if (sc.symbol->GetType() == eSymbolTypeReExported) {
350 const Symbol *actual_symbol =
351 sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget());
352 if (actual_symbol) {
353 is_reexported = true;
354 break_addr = actual_symbol->GetAddress();
355 }
356 } else {
357 break_addr = sc.symbol->GetAddress();
358 }
359
360 if (m_skip_prologue && break_addr.IsValid()) {
361 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
362 if (prologue_byte_size)
363 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
364 else {
365 const Architecture *arch =
366 breakpoint.GetTarget().GetArchitecturePlugin();
367 if (arch)
368 arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
369 }
370 }
371 }
372
373 if (!break_addr.IsValid())
374 continue;
375
376 if (!filter.AddressPasses(break_addr))
377 continue;
378
379 bool new_location;
380 BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location));
381 bp_loc_sp->SetIsReExported(is_reexported);
382 if (bp_loc_sp && new_location && !breakpoint.IsInternal()) {
383 if (log) {
384 StreamString s;
385 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
386 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
387 }
388 }
389 }
390
392}
393
396}
397
400 s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
401 else {
402 size_t num_names = m_lookups.size();
403 if (num_names == 1)
404 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
405 else {
406 s->Printf("names = {");
407 for (size_t i = 0; i < num_names; i++) {
408 s->Printf("%s'%s'", (i == 0 ? "" : ", "),
409 m_lookups[i].GetName().GetCString());
410 }
411 s->Printf("}");
412 }
413 }
415 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
416 }
417}
418
420
424 ret_sp->SetBreakpoint(breakpoint);
425 return ret_sp;
426}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:376
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition: XcodeSDK.cpp:21
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:448
virtual void AdjustBreakpointAddress(const Symbol &func, Address &addr) const
Adjust function breakpoint address, if needed.
Definition: Architecture.h:59
"lldb/Breakpoint/BreakpointResolverName.h" This class sets breakpoints on a given function name,...
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
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
StructuredData::ObjectSP SerializeToStructuredData() 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 skip_prologue)
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::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
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:463
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:250
Represents a generic declaration context in a program.
A uniqued constant string class.
Definition: ConstString.h:40
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:84
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:266
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:365
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
static void ForEach(std::function< bool(Language *)> callback)
Definition: Language.cpp:131
void void void void void Warning(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:211
A class that encapsulates name lookup information.
Definition: Module.h:907
void SetLookupName(ConstString name)
Definition: Module.h:920
bool IsValid() const
Test if this object contains a valid regular expression.
llvm::StringRef GetText() const
Access the regular expression text.
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
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:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
const char * GetData() const
Definition: StreamString.h:45
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< UnsignedInteger > UnsignedIntegerSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< String > StringSP
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.
Definition: SymbolContext.h:34
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:88
Symbol * ResolveReExportedSymbol(Target &target) const
Definition: Symbol.cpp:520
Architecture * GetArchitecturePlugin() const
Definition: Target.h:1078
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
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:324
@ 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
Definition: lldb-forward.h:321
@ eSymbolTypeReExported
@ eSearchDepthModule
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