1 //===-- AddressResolverName.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/AddressResolverName.h"
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/AddressRange.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/SymbolContext.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Logging.h"
20 #include "lldb/Utility/Stream.h"
21 #include "lldb/lldb-enumerations.h"
22 #include "lldb/lldb-forward.h"
23 #include "lldb/lldb-types.h"
24 #include "llvm/ADT/StringRef.h"
33 using namespace lldb_private;
35 AddressResolverName::AddressResolverName(const char *func_name,
36 AddressResolver::MatchType type)
37 : AddressResolver(), m_func_name(func_name), m_class_name(nullptr),
38 m_regex(), m_match_type(type) {
39 if (m_match_type == AddressResolver::Regexp) {
40 if (!m_regex.Compile(m_func_name.GetStringRef())) {
41 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
44 log->Warning("function name regexp: \"%s\" did not compile.",
45 m_func_name.AsCString());
50 AddressResolverName::AddressResolverName(RegularExpression &func_regex)
51 : AddressResolver(), m_func_name(nullptr), m_class_name(nullptr),
52 m_regex(func_regex), m_match_type(AddressResolver::Regexp) {}
54 AddressResolverName::AddressResolverName(const char *class_name,
56 AddressResolver::MatchType type)
57 : AddressResolver(), m_func_name(method), m_class_name(class_name),
58 m_regex(), m_match_type(type) {}
60 AddressResolverName::~AddressResolverName() = default;
62 // FIXME: Right now we look at the module level, and call the module's
64 // Greg says he will add function tables, maybe at the CompileUnit level to
65 // accelerate function lookup. At that point, we should switch the depth to
66 // CompileUnit, and look in these tables.
68 Searcher::CallbackReturn
69 AddressResolverName::SearchCallback(SearchFilter &filter,
70 SymbolContext &context, Address *addr,
72 SymbolContextList func_list;
73 SymbolContextList sym_list;
75 bool skip_prologue = true;
80 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
84 log->Warning("Class/method function specification not supported yet.\n");
85 return Searcher::eCallbackReturnStop;
88 const bool include_symbols = false;
89 const bool include_inlines = true;
90 const bool append = false;
91 switch (m_match_type) {
92 case AddressResolver::Exact:
93 if (context.module_sp) {
94 context.module_sp->FindSymbolsWithNameAndType(m_func_name,
95 eSymbolTypeCode, sym_list);
96 context.module_sp->FindFunctions(m_func_name, nullptr,
97 eFunctionNameTypeAuto, include_symbols,
98 include_inlines, append, func_list);
102 case AddressResolver::Regexp:
103 if (context.module_sp) {
104 context.module_sp->FindSymbolsMatchingRegExAndType(
105 m_regex, eSymbolTypeCode, sym_list);
106 context.module_sp->FindFunctions(m_regex, include_symbols,
107 include_inlines, append, func_list);
111 case AddressResolver::Glob:
113 log->Warning("glob is not supported yet.");
117 // Remove any duplicates between the function list and the symbol list
118 if (func_list.GetSize()) {
119 for (i = 0; i < func_list.GetSize(); i++) {
120 if (!func_list.GetContextAtIndex(i, sc))
123 if (sc.function == nullptr)
126 while (j < sym_list.GetSize()) {
127 SymbolContext symbol_sc;
128 if (sym_list.GetContextAtIndex(j, symbol_sc)) {
129 if (symbol_sc.symbol && symbol_sc.symbol->ValueIsAddress()) {
130 if (sc.function->GetAddressRange().GetBaseAddress() ==
131 symbol_sc.symbol->GetAddressRef()) {
132 sym_list.RemoveContextAtIndex(j);
133 continue; // Don't increment j
142 for (i = 0; i < func_list.GetSize(); i++) {
143 if (func_list.GetContextAtIndex(i, sc)) {
145 func_addr = sc.function->GetAddressRange().GetBaseAddress();
146 addr_t byte_size = sc.function->GetAddressRange().GetByteSize();
148 const uint32_t prologue_byte_size =
149 sc.function->GetPrologueByteSize();
150 if (prologue_byte_size) {
151 func_addr.SetOffset(func_addr.GetOffset() + prologue_byte_size);
152 byte_size -= prologue_byte_size;
156 if (filter.AddressPasses(func_addr)) {
157 AddressRange new_range(func_addr, byte_size);
158 m_address_ranges.push_back(new_range);
165 for (i = 0; i < sym_list.GetSize(); i++) {
166 if (sym_list.GetContextAtIndex(i, sc)) {
167 if (sc.symbol && sc.symbol->ValueIsAddress()) {
168 func_addr = sc.symbol->GetAddressRef();
169 addr_t byte_size = sc.symbol->GetByteSize();
172 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
173 if (prologue_byte_size) {
174 func_addr.SetOffset(func_addr.GetOffset() + prologue_byte_size);
175 byte_size -= prologue_byte_size;
179 if (filter.AddressPasses(func_addr)) {
180 AddressRange new_range(func_addr, byte_size);
181 m_address_ranges.push_back(new_range);
186 return Searcher::eCallbackReturnContinue;
189 lldb::SearchDepth AddressResolverName::GetDepth() {
190 return lldb::eSearchDepthModule;
193 void AddressResolverName::GetDescription(Stream *s) {
194 s->PutCString("Address by function name: ");
196 if (m_match_type == AddressResolver::Regexp)
197 s->Printf("'%s' (regular expression)", m_regex.GetText().str().c_str());
199 s->Printf("'%s'", m_func_name.AsCString());