blob: 3e7c373998b411c144fdcd00b3a425d2ae08d7d6 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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// Project includes
13#include "lldb/Core/Log.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000014#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/StreamString.h"
Sean Callanan3e80cd92011-10-12 02:08:07 +000016#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000017#include "lldb/Symbol/Function.h"
18#include "lldb/Symbol/SymbolContext.h"
19#include "lldb/Symbol/Symbol.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/lldb-private-log.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25AddressResolverName::AddressResolverName
26(
27 const char *func_name,
28 AddressResolver::MatchType type
29) :
30 AddressResolver (),
31 m_func_name (func_name),
32 m_class_name (NULL),
33 m_regex (),
34 m_match_type (type)
35{
36 if (m_match_type == AddressResolver::Regexp)
37 {
38 if (!m_regex.Compile (m_func_name.AsCString()))
39 {
Greg Claytone005f2c2010-11-06 01:53:30 +000040 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +000041
42 if (log)
43 log->Warning ("function name regexp: \"%s\" did not compile.", m_func_name.AsCString());
44 }
45 }
46}
47
48AddressResolverName::AddressResolverName
49(
50 RegularExpression &func_regex
51) :
52 AddressResolver (),
53 m_func_name (NULL),
54 m_class_name (NULL),
55 m_regex (func_regex),
56 m_match_type (AddressResolver::Regexp)
57{
58
59}
60
61AddressResolverName::AddressResolverName
62(
63 const char *class_name,
64 const char *method,
65 AddressResolver::MatchType type
66) :
67 AddressResolver (),
68 m_func_name (method),
69 m_class_name (class_name),
70 m_regex (),
71 m_match_type (type)
72{
73
74}
75
76AddressResolverName::~AddressResolverName ()
77{
78}
79
80// FIXME: Right now we look at the module level, and call the module's "FindFunctions".
81// Greg says he will add function tables, maybe at the CompileUnit level to accelerate function
82// lookup. At that point, we should switch the depth to CompileUnit, and look in these tables.
83
84Searcher::CallbackReturn
85AddressResolverName::SearchCallback
86(
87 SearchFilter &filter,
88 SymbolContext &context,
89 Address *addr,
90 bool containing
91)
92{
93 SymbolContextList func_list;
94 SymbolContextList sym_list;
95
96 bool skip_prologue = true;
97 uint32_t i;
98 SymbolContext sc;
99 Address func_addr;
100
Greg Claytone005f2c2010-11-06 01:53:30 +0000101 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000102
103 if (m_class_name)
104 {
105 if (log)
106 log->Warning ("Class/method function specification not supported yet.\n");
107 return Searcher::eCallbackReturnStop;
108 }
109
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000110 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000111 const bool include_inlines = true;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000112 const bool append = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000113 switch (m_match_type)
114 {
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000115 case AddressResolver::Exact:
Chris Lattner24943d22010-06-08 16:52:24 +0000116 if (context.module_sp)
117 {
Sean Callanan3e80cd92011-10-12 02:08:07 +0000118 context.module_sp->FindSymbolsWithNameAndType (m_func_name,
Greg Clayton12bec712010-06-28 21:30:43 +0000119 eSymbolTypeCode,
120 sym_list);
Sean Callanan3e80cd92011-10-12 02:08:07 +0000121 context.module_sp->FindFunctions (m_func_name,
122 NULL,
Greg Clayton12bec712010-06-28 21:30:43 +0000123 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000124 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +0000125 include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000126 append,
Greg Clayton12bec712010-06-28 21:30:43 +0000127 func_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000128 }
129 break;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000130
131 case AddressResolver::Regexp:
Chris Lattner24943d22010-06-08 16:52:24 +0000132 if (context.module_sp)
133 {
Greg Clayton12bec712010-06-28 21:30:43 +0000134 context.module_sp->FindSymbolsMatchingRegExAndType (m_regex,
135 eSymbolTypeCode,
136 sym_list);
137 context.module_sp->FindFunctions (m_regex,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000138 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +0000139 include_inlines,
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000140 append,
Greg Clayton12bec712010-06-28 21:30:43 +0000141 func_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000142 }
143 break;
Greg Clayton28d5fcc2011-01-27 06:44:37 +0000144
145 case AddressResolver::Glob:
Chris Lattner24943d22010-06-08 16:52:24 +0000146 if (log)
147 log->Warning ("glob is not supported yet.");
148 break;
149 }
150
151 // Remove any duplicates between the funcion list and the symbol list
152 if (func_list.GetSize())
153 {
154 for (i = 0; i < func_list.GetSize(); i++)
155 {
156 if (func_list.GetContextAtIndex(i, sc) == false)
157 continue;
158
159 if (sc.function == NULL)
160 continue;
161 uint32_t j = 0;
162 while (j < sym_list.GetSize())
163 {
164 SymbolContext symbol_sc;
165 if (sym_list.GetContextAtIndex(j, symbol_sc))
166 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000167 if (symbol_sc.symbol && symbol_sc.symbol->ValueIsAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000168 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000169 if (sc.function->GetAddressRange().GetBaseAddress() == symbol_sc.symbol->GetAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000170 {
171 sym_list.RemoveContextAtIndex(j);
172 continue; // Don't increment j
173 }
174 }
175 }
176
177 j++;
178 }
179 }
180
181 for (i = 0; i < func_list.GetSize(); i++)
182 {
183 if (func_list.GetContextAtIndex(i, sc))
184 {
185 if (sc.function)
186 {
187 func_addr = sc.function->GetAddressRange().GetBaseAddress();
188 addr_t byte_size = sc.function->GetAddressRange().GetByteSize();
189 if (skip_prologue)
190 {
191 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
192 if (prologue_byte_size)
193 {
194 func_addr.SetOffset (func_addr.GetOffset() + prologue_byte_size);
195 byte_size -= prologue_byte_size;
196 }
197 }
198
199 if (filter.AddressPasses (func_addr))
200 {
201 AddressRange new_range (func_addr, byte_size);
202 m_address_ranges.push_back (new_range);
203 }
204 }
205 }
206 }
207 }
208
209 for (i = 0; i < sym_list.GetSize(); i++)
210 {
211 if (sym_list.GetContextAtIndex(i, sc))
212 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000213 if (sc.symbol && sc.symbol->ValueIsAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000214 {
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000215 func_addr = sc.symbol->GetAddress();
216 addr_t byte_size = sc.symbol->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000217
218 if (skip_prologue)
219 {
220 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
221 if (prologue_byte_size)
222 {
223 func_addr.SetOffset (func_addr.GetOffset() + prologue_byte_size);
224 byte_size -= prologue_byte_size;
225 }
226 }
227
228 if (filter.AddressPasses (func_addr))
229 {
230 AddressRange new_range (func_addr, byte_size);
231 m_address_ranges.push_back (new_range);
232 }
233 }
234 }
235 }
236 return Searcher::eCallbackReturnContinue;
237}
238
239Searcher::Depth
240AddressResolverName::GetDepth()
241{
242 return Searcher::eDepthModule;
243}
244
245void
246AddressResolverName::GetDescription (Stream *s)
247{
248 s->PutCString("Address by function name: ");
249
250 if (m_match_type == AddressResolver::Regexp)
251 s->Printf("'%s' (regular expression)", m_regex.GetText());
252 else
253 s->Printf("'%s'", m_func_name.AsCString());
254}
255