1 //===-- SearchFilter.h ------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_SearchFilter_h_
10 #define liblldb_SearchFilter_h_
12 #include "lldb/Core/FileSpecList.h"
13 #include "lldb/Utility/StructuredData.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/lldb-forward.h"
20 namespace lldb_private {
33 namespace lldb_private {
35 /// \class Searcher SearchFilter.h "lldb/Core/SearchFilter.h" Class that is
36 /// driven by the SearchFilter to search the SymbolContext space of the target
40 /// Provides the callback and search depth for the SearchFilter search.
45 eCallbackReturnStop = 0, // Stop the iteration
46 eCallbackReturnContinue, // Continue the iteration
47 eCallbackReturnPop // Pop one level up and continue iterating
54 virtual CallbackReturn SearchCallback(SearchFilter &filter,
55 SymbolContext &context, Address *addr,
58 virtual lldb::SearchDepth GetDepth() = 0;
60 /// Prints a canonical description for the searcher to the stream \a s.
63 /// Stream to which the output is copied.
64 virtual void GetDescription(Stream *s);
67 /// \class SearchFilter SearchFilter.h "lldb/Core/SearchFilter.h" Class
68 /// descends through the SymbolContext space of the target, applying a filter
69 /// at each stage till it reaches the depth specified by the GetDepth method
70 /// of the searcher, and calls its callback at that point.
73 /// Provides the callback and search depth for the SearchFilter search.
75 /// The search is done by cooperation between the search filter and the
76 /// searcher. The search filter does the heavy work of recursing through the
77 /// SymbolContext space of the target program's symbol space. The Searcher
78 /// specifies the depth at which it wants its callback to be invoked. Note
79 /// that since the resolution of the Searcher may be greater than that of the
80 /// SearchFilter, before the Searcher qualifies an address it should pass it
81 /// to "AddressPasses." The default implementation is "Everything Passes."
85 /// The basic constructor takes a Target, which gives the space to search.
88 /// The Target that provides the module list to search.
89 SearchFilter(const lldb::TargetSP &target_sp);
91 SearchFilter(const lldb::TargetSP &target_sp, unsigned char filterType);
93 virtual ~SearchFilter();
95 /// Call this method with a file spec to see if that spec passes the filter.
98 /// The file spec to check against the filter.
100 /// \b true if \a spec passes, and \b false otherwise.
101 virtual bool ModulePasses(const FileSpec &spec);
103 /// Call this method with a Module to see if that module passes the filter.
105 /// \param[in] module
106 /// The Module to check against the filter.
109 /// \b true if \a module passes, and \b false otherwise.
110 virtual bool ModulePasses(const lldb::ModuleSP &module_sp);
112 /// Call this method with a Address to see if \a address passes the filter.
115 /// The address to check against the filter.
118 /// \b true if \a address passes, and \b false otherwise.
119 virtual bool AddressPasses(Address &addr);
121 /// Call this method with a FileSpec to see if \a file spec passes the
122 /// filter as the name of a compilation unit.
124 /// \param[in] fileSpec
125 /// The file spec to check against the filter.
128 /// \b true if \a file spec passes, and \b false otherwise.
129 virtual bool CompUnitPasses(FileSpec &fileSpec);
131 /// Call this method with a CompileUnit to see if \a comp unit passes the
134 /// \param[in] compUnit
135 /// The CompileUnit to check against the filter.
138 /// \b true if \a Comp Unit passes, and \b false otherwise.
139 virtual bool CompUnitPasses(CompileUnit &compUnit);
141 /// Call this method with a Function to see if \a function passes the
144 /// \param[in] function
145 /// The Functions to check against the filter.
148 /// \b true if \a function passes, and \b false otherwise.
149 virtual bool FunctionPasses(Function &function);
151 /// Call this method to do the search using the Searcher.
153 /// \param[in] searcher
154 /// The searcher to drive with this search.
156 virtual void Search(Searcher &searcher);
158 /// Call this method to do the search using the Searcher in the module list
161 /// \param[in] searcher
162 /// The searcher to drive with this search.
164 /// \param[in] modules
165 /// The module list within which to restrict the search.
167 virtual void SearchInModuleList(Searcher &searcher, ModuleList &modules);
169 /// This determines which items are REQUIRED for the filter to pass. For
170 /// instance, if you are filtering by Compilation Unit, obviously symbols
171 /// that have no compilation unit can't pass So return eSymbolContextCU and
172 /// search callbacks can then short cut the search to avoid looking at
173 /// things that obviously won't pass.
176 /// The required elements for the search, which is an or'ed together
177 /// set of lldb:SearchContextItem enum's.
179 virtual uint32_t GetFilterRequiredItems();
181 /// Prints a canonical description for the search filter to the stream \a s.
184 /// Stream to which the output is copied.
185 virtual void GetDescription(Stream *s);
187 /// Standard "Dump" method. At present it does nothing.
188 virtual void Dump(Stream *s) const;
190 lldb::SearchFilterSP CopyForBreakpoint(Breakpoint &breakpoint);
192 static lldb::SearchFilterSP
193 CreateFromStructuredData(Target &target,
194 const StructuredData::Dictionary &data_dict,
197 virtual StructuredData::ObjectSP SerializeToStructuredData() {
198 return StructuredData::ObjectSP();
201 static const char *GetSerializationKey() { return "SearchFilter"; }
203 static const char *GetSerializationSubclassKey() { return "Type"; }
205 static const char *GetSerializationSubclassOptionsKey() { return "Options"; }
213 LastKnownFilterType = ByModulesAndCU,
217 static const char *g_ty_to_name[LastKnownFilterType + 2];
219 enum FilterTy GetFilterTy() {
220 if (SubclassID > FilterTy::LastKnownFilterType)
221 return FilterTy::UnknownFilter;
223 return (enum FilterTy)SubclassID;
226 const char *GetFilterName() { return FilterTyToName(GetFilterTy()); }
228 static const char *FilterTyToName(enum FilterTy);
230 static FilterTy NameToFilterTy(llvm::StringRef name);
233 // Serialization of SearchFilter options:
234 enum OptionNames { ModList = 0, CUList, LanguageName, LastOptionName };
235 static const char *g_option_names[LastOptionName];
237 static const char *GetKey(enum OptionNames enum_value) {
238 return g_option_names[enum_value];
241 StructuredData::DictionarySP
242 WrapOptionsDict(StructuredData::DictionarySP options_dict_sp);
244 void SerializeFileSpecList(StructuredData::DictionarySP &options_dict_sp,
245 OptionNames name, FileSpecList &file_list);
247 // These are utility functions to assist with the search iteration. They are
248 // used by the default Search method.
250 Searcher::CallbackReturn DoModuleIteration(const SymbolContext &context,
253 Searcher::CallbackReturn DoModuleIteration(const lldb::ModuleSP &module_sp,
256 Searcher::CallbackReturn DoCUIteration(const lldb::ModuleSP &module_sp,
257 const SymbolContext &context,
260 Searcher::CallbackReturn DoFunctionIteration(Function *function,
261 const SymbolContext &context,
264 virtual lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) = 0;
266 void SetTarget(lldb::TargetSP &target_sp) { m_target_sp = target_sp; }
269 m_target_sp; // Every filter has to be associated with a target for
270 // now since you need a starting place for the search.
272 unsigned char SubclassID;
275 /// \class SearchFilterForUnconstrainedSearches SearchFilter.h
276 /// "lldb/Core/SearchFilter.h" This is a SearchFilter that searches through
277 /// all modules. It also consults the
278 /// Target::ModuleIsExcludedForUnconstrainedSearches.
279 class SearchFilterForUnconstrainedSearches : public SearchFilter {
281 SearchFilterForUnconstrainedSearches(const lldb::TargetSP &target_sp)
282 : SearchFilter(target_sp, FilterTy::Unconstrained) {}
284 ~SearchFilterForUnconstrainedSearches() override = default;
286 bool ModulePasses(const FileSpec &module_spec) override;
288 bool ModulePasses(const lldb::ModuleSP &module_sp) override;
290 static lldb::SearchFilterSP
291 CreateFromStructuredData(Target &target,
292 const StructuredData::Dictionary &data_dict,
295 StructuredData::ObjectSP SerializeToStructuredData() override;
298 lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) override;
301 /// \class SearchFilterByModule SearchFilter.h "lldb/Core/SearchFilter.h" This
302 /// is a SearchFilter that restricts the search to a given module.
304 class SearchFilterByModule : public SearchFilter {
306 /// The basic constructor takes a Target, which gives the space to search,
307 /// and the module to restrict the search to.
309 /// \param[in] target
310 /// The Target that provides the module list to search.
312 /// \param[in] module
313 /// The Module that limits the search.
314 SearchFilterByModule(const lldb::TargetSP &targetSP, const FileSpec &module);
316 ~SearchFilterByModule() override;
318 bool ModulePasses(const lldb::ModuleSP &module_sp) override;
320 bool ModulePasses(const FileSpec &spec) override;
322 bool AddressPasses(Address &address) override;
324 bool CompUnitPasses(FileSpec &fileSpec) override;
326 bool CompUnitPasses(CompileUnit &compUnit) override;
328 void GetDescription(Stream *s) override;
330 uint32_t GetFilterRequiredItems() override;
332 void Dump(Stream *s) const override;
334 void Search(Searcher &searcher) override;
336 static lldb::SearchFilterSP
337 CreateFromStructuredData(Target &target,
338 const StructuredData::Dictionary &data_dict,
341 StructuredData::ObjectSP SerializeToStructuredData() override;
344 lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) override;
347 FileSpec m_module_spec;
350 class SearchFilterByModuleList : public SearchFilter {
352 /// The basic constructor takes a Target, which gives the space to search,
353 /// and the module list to restrict the search to.
355 /// \param[in] target
356 /// The Target that provides the module list to search.
358 /// \param[in] module
359 /// The Module that limits the search.
360 SearchFilterByModuleList(const lldb::TargetSP &targetSP,
361 const FileSpecList &module_list);
363 SearchFilterByModuleList(const lldb::TargetSP &targetSP,
364 const FileSpecList &module_list,
365 enum FilterTy filter_ty);
367 ~SearchFilterByModuleList() override;
369 SearchFilterByModuleList &operator=(const SearchFilterByModuleList &rhs);
371 bool ModulePasses(const lldb::ModuleSP &module_sp) override;
373 bool ModulePasses(const FileSpec &spec) override;
375 bool AddressPasses(Address &address) override;
377 bool CompUnitPasses(FileSpec &fileSpec) override;
379 bool CompUnitPasses(CompileUnit &compUnit) override;
381 void GetDescription(Stream *s) override;
383 uint32_t GetFilterRequiredItems() override;
385 void Dump(Stream *s) const override;
387 void Search(Searcher &searcher) override;
389 static lldb::SearchFilterSP
390 CreateFromStructuredData(Target &target,
391 const StructuredData::Dictionary &data_dict,
394 StructuredData::ObjectSP SerializeToStructuredData() override;
396 void SerializeUnwrapped(StructuredData::DictionarySP &options_dict_sp);
399 lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) override;
402 FileSpecList m_module_spec_list;
405 class SearchFilterByModuleListAndCU : public SearchFilterByModuleList {
407 /// The basic constructor takes a Target, which gives the space to search,
408 /// and the module list to restrict the search to.
410 /// \param[in] target
411 /// The Target that provides the module list to search.
413 /// \param[in] module
414 /// The Module that limits the search.
415 SearchFilterByModuleListAndCU(const lldb::TargetSP &targetSP,
416 const FileSpecList &module_list,
417 const FileSpecList &cu_list);
419 SearchFilterByModuleListAndCU(const SearchFilterByModuleListAndCU &rhs);
421 ~SearchFilterByModuleListAndCU() override;
423 SearchFilterByModuleListAndCU &
424 operator=(const SearchFilterByModuleListAndCU &rhs);
426 bool AddressPasses(Address &address) override;
428 bool CompUnitPasses(FileSpec &fileSpec) override;
430 bool CompUnitPasses(CompileUnit &compUnit) override;
432 void GetDescription(Stream *s) override;
434 uint32_t GetFilterRequiredItems() override;
436 void Dump(Stream *s) const override;
438 void Search(Searcher &searcher) override;
440 static lldb::SearchFilterSP
441 CreateFromStructuredData(Target &target,
442 const StructuredData::Dictionary &data_dict,
445 StructuredData::ObjectSP SerializeToStructuredData() override;
448 lldb::SearchFilterSP DoCopyForBreakpoint(Breakpoint &breakpoint) override;
451 FileSpecList m_cu_spec_list;
454 } // namespace lldb_private
456 #endif // liblldb_SearchFilter_h_