]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/AddressResolverName.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / AddressResolverName.cpp
1 //===-- AddressResolverName.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/AddressResolverName.h"
11
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"
25
26 #include <memory>
27 #include <string>
28 #include <vector>
29
30 #include <stdint.h>
31
32 using namespace lldb;
33 using namespace lldb_private;
34
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));
42
43       if (log)
44         log->Warning("function name regexp: \"%s\" did not compile.",
45                      m_func_name.AsCString());
46     }
47   }
48 }
49
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) {}
53
54 AddressResolverName::AddressResolverName(const char *class_name,
55                                          const char *method,
56                                          AddressResolver::MatchType type)
57     : AddressResolver(), m_func_name(method), m_class_name(class_name),
58       m_regex(), m_match_type(type) {}
59
60 AddressResolverName::~AddressResolverName() = default;
61
62 // FIXME: Right now we look at the module level, and call the module's
63 // "FindFunctions".
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.
67
68 Searcher::CallbackReturn
69 AddressResolverName::SearchCallback(SearchFilter &filter,
70                                     SymbolContext &context, Address *addr,
71                                     bool containing) {
72   SymbolContextList func_list;
73   SymbolContextList sym_list;
74
75   bool skip_prologue = true;
76   uint32_t i;
77   SymbolContext sc;
78   Address func_addr;
79
80   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
81
82   if (m_class_name) {
83     if (log)
84       log->Warning("Class/method function specification not supported yet.\n");
85     return Searcher::eCallbackReturnStop;
86   }
87
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);
99     }
100     break;
101
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);
108     }
109     break;
110
111   case AddressResolver::Glob:
112     if (log)
113       log->Warning("glob is not supported yet.");
114     break;
115   }
116
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))
121         continue;
122
123       if (sc.function == nullptr)
124         continue;
125       uint32_t j = 0;
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
134             }
135           }
136         }
137
138         j++;
139       }
140     }
141
142     for (i = 0; i < func_list.GetSize(); i++) {
143       if (func_list.GetContextAtIndex(i, sc)) {
144         if (sc.function) {
145           func_addr = sc.function->GetAddressRange().GetBaseAddress();
146           addr_t byte_size = sc.function->GetAddressRange().GetByteSize();
147           if (skip_prologue) {
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;
153             }
154           }
155
156           if (filter.AddressPasses(func_addr)) {
157             AddressRange new_range(func_addr, byte_size);
158             m_address_ranges.push_back(new_range);
159           }
160         }
161       }
162     }
163   }
164
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();
170
171         if (skip_prologue) {
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;
176           }
177         }
178
179         if (filter.AddressPasses(func_addr)) {
180           AddressRange new_range(func_addr, byte_size);
181           m_address_ranges.push_back(new_range);
182         }
183       }
184     }
185   }
186   return Searcher::eCallbackReturnContinue;
187 }
188
189 lldb::SearchDepth AddressResolverName::GetDepth() {
190   return lldb::eSearchDepthModule;
191 }
192
193 void AddressResolverName::GetDescription(Stream *s) {
194   s->PutCString("Address by function name: ");
195
196   if (m_match_type == AddressResolver::Regexp)
197     s->Printf("'%s' (regular expression)", m_regex.GetText().str().c_str());
198   else
199     s->Printf("'%s'", m_func_name.AsCString());
200 }