blob: 4c54a9128014d142774422a3c436f06b758b4afc [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SearchFilter.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// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
14
15#include "lldb/lldb-private.h"
16#include "lldb/Core/SearchFilter.h"
17#include "lldb/Target/Target.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22//----------------------------------------------------------------------
23// SearchFilter constructor
24//----------------------------------------------------------------------
25Searcher::Searcher ()
26{
27
28}
29
30Searcher::~Searcher ()
31{
32
33}
34
35void
36Searcher::GetDescription (Stream *s)
37{
38}
39
40//----------------------------------------------------------------------
41// SearchFilter constructor
42//----------------------------------------------------------------------
43SearchFilter::SearchFilter(lldb::TargetSP &target_sp) :
44 m_target_sp (target_sp)
45{
46}
47
48//----------------------------------------------------------------------
49// SearchFilter copy constructor
50//----------------------------------------------------------------------
51SearchFilter::SearchFilter(const SearchFilter& rhs) :
52 m_target_sp (rhs.m_target_sp)
53{
54}
55
56//----------------------------------------------------------------------
57// SearchFilter assignment operator
58//----------------------------------------------------------------------
59const SearchFilter&
60SearchFilter::operator=(const SearchFilter& rhs)
61{
62 m_target_sp = rhs.m_target_sp;
63 return *this;
64}
65
66//----------------------------------------------------------------------
67// Destructor
68//----------------------------------------------------------------------
69SearchFilter::~SearchFilter()
70{
71}
72
73bool
74SearchFilter::ModulePasses (const FileSpec &spec)
75{
76 return true;
77}
78
79bool
80SearchFilter::ModulePasses (const ModuleSP &module_sp)
81{
82 return true;
83}
84
85bool
86SearchFilter::SymbolContextPasses
87(
88 const SymbolContext &context,
89 lldb::SymbolContextItem scope
90)
91{
92 return true;
93}
94
95bool
96SearchFilter::AddressPasses (Address &address)
97{
98 return true;
99}
100
101bool
102SearchFilter::CompUnitPasses (FileSpec &fileSpec)
103{
104 return true;
105}
106
107bool
108SearchFilter::CompUnitPasses (CompileUnit &compUnit)
109{
110 return true;
111}
112
113void
114SearchFilter::GetDescription (Stream *s)
115{
116 s->PutCString("No Filter");
117}
118
119void
120SearchFilter::Dump (Stream *s) const
121{
122
123}
124
125//----------------------------------------------------------------------
126// UTILITY Functions to help iterate down through the elements of the
127// SymbolContext.
128//----------------------------------------------------------------------
129
130void
131SearchFilter::Search (Searcher &searcher)
132{
133 SymbolContext empty_sc;
134
135 if (m_target_sp == NULL)
136 return;
137 empty_sc.target_sp = m_target_sp;
138
139 if (searcher.GetDepth() == Searcher::eDepthTarget)
140 searcher.SearchCallback (*this, empty_sc, NULL, false);
141 else
142 DoModuleIteration(empty_sc, searcher);
143}
144
145void
146SearchFilter::SearchInModuleList (Searcher &searcher, ModuleList &modules)
147{
148 SymbolContext empty_sc;
149
150 if (m_target_sp == NULL)
151 return;
152 empty_sc.target_sp = m_target_sp;
153
154 if (searcher.GetDepth() == Searcher::eDepthTarget)
155 searcher.SearchCallback (*this, empty_sc, NULL, false);
156 else
157 {
158 size_t numModules = modules.GetSize();
159
160 for (int i = 0; i < numModules; i++)
161 {
162 ModuleSP module_sp(modules.GetModuleAtIndex(i));
163 if (ModulePasses(module_sp))
164 {
165 if (DoModuleIteration(module_sp, searcher) == Searcher::eCallbackReturnStop)
166 return;
167 }
168 }
169 }
170}
171
172
173Searcher::CallbackReturn
174SearchFilter::DoModuleIteration (const lldb::ModuleSP& module_sp, Searcher &searcher)
175{
176 SymbolContext matchingContext (m_target_sp, module_sp);
177 return DoModuleIteration(matchingContext, searcher);
178}
179
180Searcher::CallbackReturn
181SearchFilter::DoModuleIteration (const SymbolContext &context, Searcher &searcher)
182{
183 Searcher::CallbackReturn shouldContinue;
184
185 if (searcher.GetDepth () >= Searcher::eDepthModule)
186 {
187 if (!context.module_sp)
188 {
189 size_t n_modules = m_target_sp->GetImages().GetSize();
190 for (int i = 0; i < n_modules; i++)
191 {
192 // If this is the last level supplied, then call the callback directly,
193 // otherwise descend.
194 ModuleSP module_sp(m_target_sp->GetImages().GetModuleAtIndex(i));
195 if (!ModulePasses (module_sp))
196 continue;
197
198 if (searcher.GetDepth () == Searcher::eDepthModule)
199 {
200 SymbolContext matchingContext(m_target_sp, module_sp);
201
202 shouldContinue = searcher.SearchCallback (*this, matchingContext, NULL, false);
203 if (shouldContinue == Searcher::eCallbackReturnStop
204 || shouldContinue == Searcher::eCallbackReturnPop)
205 return shouldContinue;
206 }
207 else
208 {
209 shouldContinue = DoCUIteration(module_sp, context, searcher);
210 if (shouldContinue == Searcher::eCallbackReturnStop)
211 return shouldContinue;
212 else if (shouldContinue == Searcher::eCallbackReturnPop)
213 continue;
214 }
215 }
216 }
217 else
218 {
219 if (searcher.GetDepth () == Searcher::eDepthModule)
220 {
221 SymbolContext matchingContext(context.module_sp.get());
222
223 shouldContinue = searcher.SearchCallback (*this, matchingContext, NULL, false);
224 }
225 else
226 {
227 return DoCUIteration(context.module_sp, context, searcher);
228 }
229 }
230
231 }
232 return Searcher::eCallbackReturnContinue;
233}
234
235Searcher::CallbackReturn
236SearchFilter::DoCUIteration (const ModuleSP &module_sp, const SymbolContext &context, Searcher &searcher)
237{
238 Searcher::CallbackReturn shouldContinue;
239 if (context.comp_unit == NULL)
240 {
241 uint32_t num_comp_units = module_sp->GetNumCompileUnits();
242 for (uint32_t i = 0; i < num_comp_units; i++)
243 {
244 CompUnitSP cu_sp (module_sp->GetCompileUnitAtIndex (i));
245 if (!CompUnitPasses (*(cu_sp.get())))
246 continue;
247
248 if (searcher.GetDepth () == Searcher::eDepthCompUnit)
249 {
250 SymbolContext matchingContext(m_target_sp, module_sp, cu_sp.get());
251
252 shouldContinue = searcher.SearchCallback (*this, matchingContext, NULL, false);
253
254 if (shouldContinue == Searcher::eCallbackReturnPop)
255 return Searcher::eCallbackReturnContinue;
256 else if (shouldContinue == Searcher::eCallbackReturnStop)
257 return shouldContinue;
258 }
259 else
260 {
261 // FIXME Descend to block.
262 }
263
264 }
265 }
266 else
267 {
268 if (CompUnitPasses(*context.comp_unit))
269 {
270 SymbolContext matchingContext (m_target_sp, module_sp, context.comp_unit);
271 return searcher.SearchCallback (*this, matchingContext, NULL, false);
272 }
273 }
274 return Searcher::eCallbackReturnContinue;
275}
276
277Searcher::CallbackReturn
278SearchFilter::DoFunctionIteration (Function *function, const SymbolContext &context, Searcher &searcher)
279{
280 // FIXME: Implement...
281 return Searcher::eCallbackReturnContinue;
282}
283
284//----------------------------------------------------------------------
285// SearchFilterByModule:
286// Selects a shared library matching a given file spec
287//----------------------------------------------------------------------
288
289//----------------------------------------------------------------------
290// SearchFilterByModule constructors
291//----------------------------------------------------------------------
292
293SearchFilterByModule::SearchFilterByModule (lldb::TargetSP &target_sp, const FileSpec &module) :
294 SearchFilter (target_sp),
295 m_module_spec (module)
296{
297}
298
299
300//----------------------------------------------------------------------
301// SearchFilterByModule copy constructor
302//----------------------------------------------------------------------
303SearchFilterByModule::SearchFilterByModule(const SearchFilterByModule& rhs) :
304 SearchFilter (rhs),
305 m_module_spec (rhs.m_module_spec)
306{
307}
308
309//----------------------------------------------------------------------
310// SearchFilterByModule assignment operator
311//----------------------------------------------------------------------
312const SearchFilterByModule&
313SearchFilterByModule::operator=(const SearchFilterByModule& rhs)
314{
315 m_target_sp = rhs.m_target_sp;
316 m_module_spec = rhs.m_module_spec;
317 return *this;
318}
319
320//----------------------------------------------------------------------
321// Destructor
322//----------------------------------------------------------------------
323SearchFilterByModule::~SearchFilterByModule()
324{
325}
326
327bool
328SearchFilterByModule::ModulePasses (const ModuleSP &module_sp)
329{
330 if (module_sp && FileSpec::Compare (module_sp->GetFileSpec(), m_module_spec, false) == 0)
331 return true;
332 else
333 return false;
334}
335
336bool
337SearchFilterByModule::ModulePasses (const FileSpec &spec)
338{
339 if (FileSpec::Compare(spec, m_module_spec, false) == 0)
340 return true;
341 else
342 return false;
343}
344
345bool
346SearchFilterByModule::SymbolContextPasses
347(
348 const SymbolContext &context,
349 lldb::SymbolContextItem scope
350 )
351{
352 if (!(scope & eSymbolContextModule))
353 return false;
354
355 if (context.module_sp && FileSpec::Compare (context.module_sp->GetFileSpec(), m_module_spec, false) == 0)
356 return true;
357 else
358 return false;
359}
360
361bool
362SearchFilterByModule::AddressPasses (Address &address)
363{
364 // FIXME: Not yet implemented
365 return true;
366}
367
368
369bool
370SearchFilterByModule::CompUnitPasses (FileSpec &fileSpec)
371{
372 return true;
373}
374
375bool
376SearchFilterByModule::CompUnitPasses (CompileUnit &compUnit)
377{
378 return true;
379}
380
381void
382SearchFilterByModule::Search (Searcher &searcher)
383{
384 if (!m_target_sp)
385 return;
386
387 if (searcher.GetDepth() == Searcher::eDepthTarget)
388 {
389 SymbolContext empty_sc;
390 empty_sc.target_sp = m_target_sp;
391 searcher.SearchCallback (*this, empty_sc, NULL, false);
392 }
393
394 // If the module file spec is a full path, then we can just find the one
395 // filespec that passes. Otherwise, we need to go through all modules and
396 // find the ones that match the file name.
397
398 ModuleList matching_modules;
399 // const size_t num_matching_modules = m_target_sp->GetImages().FindModules(&m_module_spec, NULL, NULL, NULL, matching_modules);
400 for (int i = 0; i < m_target_sp->GetImages().GetSize (); i++)
401 {
402 Module* module = m_target_sp->GetImages().GetModulePointerAtIndex(i);
403 if (FileSpec::Compare (m_module_spec, module->GetFileSpec(), false) == 0)
404 {
405 SymbolContext matchingContext(m_target_sp, module->GetSP());
406 Searcher::CallbackReturn shouldContinue;
407
408 shouldContinue = DoModuleIteration(matchingContext, searcher);
409 if (shouldContinue == Searcher::eCallbackReturnStop)
410 return;
411 }
412 }
413}
414
415void
416SearchFilterByModule::GetDescription (Stream *s)
417{
418 s->PutCString("In module ");
419 if (s->GetVerbose())
420 {
421 char buffer[2048];
422 m_module_spec.GetPath(buffer, 2047);
423 s->PutCString(buffer);
424 }
425 else
426 {
427 s->PutCString(m_module_spec.GetFilename().AsCString("<unknown>"));
428 }
429}
430
431void
432SearchFilterByModule::Dump (Stream *s) const
433{
434
435}