1ac7ddfbfSEd Maste //===-- AddressResolverName.cpp ---------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/Core/AddressResolverName.h"
11ac7ddfbfSEd Maste
12*b5893f02SDimitry Andric #include "lldb/Core/Address.h"
13*b5893f02SDimitry Andric #include "lldb/Core/AddressRange.h"
14ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
15ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
16ac7ddfbfSEd Maste #include "lldb/Symbol/Symbol.h"
17435933ddSDimitry Andric #include "lldb/Symbol/SymbolContext.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
19*b5893f02SDimitry Andric #include "lldb/Utility/Logging.h"
20*b5893f02SDimitry Andric #include "lldb/Utility/Stream.h"
21*b5893f02SDimitry Andric #include "lldb/lldb-enumerations.h"
22*b5893f02SDimitry Andric #include "lldb/lldb-forward.h"
23*b5893f02SDimitry Andric #include "lldb/lldb-types.h"
24*b5893f02SDimitry Andric #include "llvm/ADT/StringRef.h"
25f678e45dSDimitry Andric
26*b5893f02SDimitry Andric #include <memory>
27*b5893f02SDimitry Andric #include <string>
28*b5893f02SDimitry Andric #include <vector>
29f678e45dSDimitry Andric
30*b5893f02SDimitry Andric #include <stdint.h>
31ac7ddfbfSEd Maste
32ac7ddfbfSEd Maste using namespace lldb;
33ac7ddfbfSEd Maste using namespace lldb_private;
34ac7ddfbfSEd Maste
AddressResolverName(const char * func_name,AddressResolver::MatchType type)354bb0738eSEd Maste AddressResolverName::AddressResolverName(const char *func_name,
36435933ddSDimitry Andric AddressResolver::MatchType type)
37435933ddSDimitry Andric : AddressResolver(), m_func_name(func_name), m_class_name(nullptr),
38435933ddSDimitry Andric m_regex(), m_match_type(type) {
39435933ddSDimitry Andric if (m_match_type == AddressResolver::Regexp) {
40435933ddSDimitry Andric if (!m_regex.Compile(m_func_name.GetStringRef())) {
41ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
42ac7ddfbfSEd Maste
43ac7ddfbfSEd Maste if (log)
44435933ddSDimitry Andric log->Warning("function name regexp: \"%s\" did not compile.",
45435933ddSDimitry Andric m_func_name.AsCString());
46ac7ddfbfSEd Maste }
47ac7ddfbfSEd Maste }
48ac7ddfbfSEd Maste }
49ac7ddfbfSEd Maste
AddressResolverName(RegularExpression & func_regex)50435933ddSDimitry Andric AddressResolverName::AddressResolverName(RegularExpression &func_regex)
51435933ddSDimitry Andric : AddressResolver(), m_func_name(nullptr), m_class_name(nullptr),
52435933ddSDimitry Andric m_regex(func_regex), m_match_type(AddressResolver::Regexp) {}
53ac7ddfbfSEd Maste
AddressResolverName(const char * class_name,const char * method,AddressResolver::MatchType type)544bb0738eSEd Maste AddressResolverName::AddressResolverName(const char *class_name,
55ac7ddfbfSEd Maste const char *method,
56435933ddSDimitry Andric AddressResolver::MatchType type)
57435933ddSDimitry Andric : AddressResolver(), m_func_name(method), m_class_name(class_name),
58435933ddSDimitry Andric m_regex(), m_match_type(type) {}
59ac7ddfbfSEd Maste
604bb0738eSEd Maste AddressResolverName::~AddressResolverName() = default;
61ac7ddfbfSEd Maste
62435933ddSDimitry Andric // FIXME: Right now we look at the module level, and call the module's
63435933ddSDimitry Andric // "FindFunctions".
64435933ddSDimitry Andric // Greg says he will add function tables, maybe at the CompileUnit level to
654ba319b5SDimitry Andric // accelerate function lookup. At that point, we should switch the depth to
664ba319b5SDimitry Andric // CompileUnit, and look in these tables.
67ac7ddfbfSEd Maste
68ac7ddfbfSEd Maste Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr,bool containing)694bb0738eSEd Maste AddressResolverName::SearchCallback(SearchFilter &filter,
70435933ddSDimitry Andric SymbolContext &context, Address *addr,
71435933ddSDimitry Andric bool containing) {
72ac7ddfbfSEd Maste SymbolContextList func_list;
73ac7ddfbfSEd Maste SymbolContextList sym_list;
74ac7ddfbfSEd Maste
75ac7ddfbfSEd Maste bool skip_prologue = true;
76ac7ddfbfSEd Maste uint32_t i;
77ac7ddfbfSEd Maste SymbolContext sc;
78ac7ddfbfSEd Maste Address func_addr;
79ac7ddfbfSEd Maste
80ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
81ac7ddfbfSEd Maste
82435933ddSDimitry Andric if (m_class_name) {
83ac7ddfbfSEd Maste if (log)
84ac7ddfbfSEd Maste log->Warning("Class/method function specification not supported yet.\n");
85ac7ddfbfSEd Maste return Searcher::eCallbackReturnStop;
86ac7ddfbfSEd Maste }
87ac7ddfbfSEd Maste
88ac7ddfbfSEd Maste const bool include_symbols = false;
89ac7ddfbfSEd Maste const bool include_inlines = true;
90ac7ddfbfSEd Maste const bool append = false;
91435933ddSDimitry Andric switch (m_match_type) {
92ac7ddfbfSEd Maste case AddressResolver::Exact:
93435933ddSDimitry Andric if (context.module_sp) {
94ac7ddfbfSEd Maste context.module_sp->FindSymbolsWithNameAndType(m_func_name,
95435933ddSDimitry Andric eSymbolTypeCode, sym_list);
96435933ddSDimitry Andric context.module_sp->FindFunctions(m_func_name, nullptr,
97435933ddSDimitry Andric eFunctionNameTypeAuto, include_symbols,
98435933ddSDimitry Andric include_inlines, append, func_list);
99ac7ddfbfSEd Maste }
100ac7ddfbfSEd Maste break;
101ac7ddfbfSEd Maste
102ac7ddfbfSEd Maste case AddressResolver::Regexp:
103435933ddSDimitry Andric if (context.module_sp) {
104435933ddSDimitry Andric context.module_sp->FindSymbolsMatchingRegExAndType(
105435933ddSDimitry Andric m_regex, eSymbolTypeCode, sym_list);
106435933ddSDimitry Andric context.module_sp->FindFunctions(m_regex, include_symbols,
107435933ddSDimitry Andric include_inlines, append, func_list);
108ac7ddfbfSEd Maste }
109ac7ddfbfSEd Maste break;
110ac7ddfbfSEd Maste
111ac7ddfbfSEd Maste case AddressResolver::Glob:
112ac7ddfbfSEd Maste if (log)
113ac7ddfbfSEd Maste log->Warning("glob is not supported yet.");
114ac7ddfbfSEd Maste break;
115ac7ddfbfSEd Maste }
116ac7ddfbfSEd Maste
1170127ef0fSEd Maste // Remove any duplicates between the function list and the symbol list
118435933ddSDimitry Andric if (func_list.GetSize()) {
119435933ddSDimitry Andric for (i = 0; i < func_list.GetSize(); i++) {
1204bb0738eSEd Maste if (!func_list.GetContextAtIndex(i, sc))
121ac7ddfbfSEd Maste continue;
122ac7ddfbfSEd Maste
1234bb0738eSEd Maste if (sc.function == nullptr)
124ac7ddfbfSEd Maste continue;
125ac7ddfbfSEd Maste uint32_t j = 0;
126435933ddSDimitry Andric while (j < sym_list.GetSize()) {
127ac7ddfbfSEd Maste SymbolContext symbol_sc;
128435933ddSDimitry Andric if (sym_list.GetContextAtIndex(j, symbol_sc)) {
129435933ddSDimitry Andric if (symbol_sc.symbol && symbol_sc.symbol->ValueIsAddress()) {
130435933ddSDimitry Andric if (sc.function->GetAddressRange().GetBaseAddress() ==
131435933ddSDimitry Andric symbol_sc.symbol->GetAddressRef()) {
132ac7ddfbfSEd Maste sym_list.RemoveContextAtIndex(j);
133ac7ddfbfSEd Maste continue; // Don't increment j
134ac7ddfbfSEd Maste }
135ac7ddfbfSEd Maste }
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste
138ac7ddfbfSEd Maste j++;
139ac7ddfbfSEd Maste }
140ac7ddfbfSEd Maste }
141ac7ddfbfSEd Maste
142435933ddSDimitry Andric for (i = 0; i < func_list.GetSize(); i++) {
143435933ddSDimitry Andric if (func_list.GetContextAtIndex(i, sc)) {
144435933ddSDimitry Andric if (sc.function) {
145ac7ddfbfSEd Maste func_addr = sc.function->GetAddressRange().GetBaseAddress();
146ac7ddfbfSEd Maste addr_t byte_size = sc.function->GetAddressRange().GetByteSize();
147435933ddSDimitry Andric if (skip_prologue) {
148435933ddSDimitry Andric const uint32_t prologue_byte_size =
149435933ddSDimitry Andric sc.function->GetPrologueByteSize();
150435933ddSDimitry Andric if (prologue_byte_size) {
151ac7ddfbfSEd Maste func_addr.SetOffset(func_addr.GetOffset() + prologue_byte_size);
152ac7ddfbfSEd Maste byte_size -= prologue_byte_size;
153ac7ddfbfSEd Maste }
154ac7ddfbfSEd Maste }
155ac7ddfbfSEd Maste
156435933ddSDimitry Andric if (filter.AddressPasses(func_addr)) {
157ac7ddfbfSEd Maste AddressRange new_range(func_addr, byte_size);
158ac7ddfbfSEd Maste m_address_ranges.push_back(new_range);
159ac7ddfbfSEd Maste }
160ac7ddfbfSEd Maste }
161ac7ddfbfSEd Maste }
162ac7ddfbfSEd Maste }
163ac7ddfbfSEd Maste }
164ac7ddfbfSEd Maste
165435933ddSDimitry Andric for (i = 0; i < sym_list.GetSize(); i++) {
166435933ddSDimitry Andric if (sym_list.GetContextAtIndex(i, sc)) {
167435933ddSDimitry Andric if (sc.symbol && sc.symbol->ValueIsAddress()) {
1681c3bbb01SEd Maste func_addr = sc.symbol->GetAddressRef();
169ac7ddfbfSEd Maste addr_t byte_size = sc.symbol->GetByteSize();
170ac7ddfbfSEd Maste
171435933ddSDimitry Andric if (skip_prologue) {
172ac7ddfbfSEd Maste const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
173435933ddSDimitry Andric if (prologue_byte_size) {
174ac7ddfbfSEd Maste func_addr.SetOffset(func_addr.GetOffset() + prologue_byte_size);
175ac7ddfbfSEd Maste byte_size -= prologue_byte_size;
176ac7ddfbfSEd Maste }
177ac7ddfbfSEd Maste }
178ac7ddfbfSEd Maste
179435933ddSDimitry Andric if (filter.AddressPasses(func_addr)) {
180ac7ddfbfSEd Maste AddressRange new_range(func_addr, byte_size);
181ac7ddfbfSEd Maste m_address_ranges.push_back(new_range);
182ac7ddfbfSEd Maste }
183ac7ddfbfSEd Maste }
184ac7ddfbfSEd Maste }
185ac7ddfbfSEd Maste }
186ac7ddfbfSEd Maste return Searcher::eCallbackReturnContinue;
187ac7ddfbfSEd Maste }
188ac7ddfbfSEd Maste
GetDepth()189*b5893f02SDimitry Andric lldb::SearchDepth AddressResolverName::GetDepth() {
190*b5893f02SDimitry Andric return lldb::eSearchDepthModule;
191ac7ddfbfSEd Maste }
192ac7ddfbfSEd Maste
GetDescription(Stream * s)193435933ddSDimitry Andric void AddressResolverName::GetDescription(Stream *s) {
194ac7ddfbfSEd Maste s->PutCString("Address by function name: ");
195ac7ddfbfSEd Maste
196ac7ddfbfSEd Maste if (m_match_type == AddressResolver::Regexp)
197435933ddSDimitry Andric s->Printf("'%s' (regular expression)", m_regex.GetText().str().c_str());
198ac7ddfbfSEd Maste else
199ac7ddfbfSEd Maste s->Printf("'%s'", m_func_name.AsCString());
200ac7ddfbfSEd Maste }
201