From 0fd6fd4fd45be23c885f6fa0654c780773ce9517 Mon Sep 17 00:00:00 2001 From: Carlo Kok Date: Fri, 19 Sep 2014 19:38:19 +0000 Subject: [PATCH] Adds two new functions to SBTarget FindGlobalVariables and FindGlobalFunctions that lets you search by name, by regular expression and by starts with. llvm-svn: 218140 --- lldb/include/lldb/API/SBTarget.h | 40 +++++++++++ lldb/include/lldb/Core/ModuleList.h | 10 +++ lldb/include/lldb/lldb-enumerations.h | 10 +++ lldb/scripts/Python/interface/SBTarget.i | 9 +++ lldb/source/API/SBTarget.cpp | 84 ++++++++++++++++++++++++ lldb/source/Core/ModuleList.cpp | 20 ++++++ 6 files changed, 173 insertions(+) diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h index 370d40d0454a..6b9a3c584f05 100644 --- a/lldb/include/lldb/API/SBTarget.h +++ b/lldb/include/lldb/API/SBTarget.h @@ -683,7 +683,47 @@ public: //------------------------------------------------------------------ lldb::SBValue FindFirstGlobalVariable (const char* name); + + //------------------------------------------------------------------ + /// Find global and static variables by pattern. + /// + /// @param[in] name + /// The pattern to search for global or static variables + /// + /// @param[in] max_matches + /// Allow the number of matches to be limited to \a max_matches. + /// + /// @param[in] matchtype + /// The match type to use. + /// + /// @return + /// A list of matched variables in an SBValueList. + //------------------------------------------------------------------ + lldb::SBValueList + FindGlobalVariables(const char *name, + uint32_t max_matches, + MatchType matchtype); + //------------------------------------------------------------------ + /// Find global functions by their name with pattern matching. + /// + /// @param[in] name + /// The pattern to search for global or static variables + /// + /// @param[in] max_matches + /// Allow the number of matches to be limited to \a max_matches. + /// + /// @param[in] matchtype + /// The match type to use. + /// + /// @return + /// A list of matched variables in an SBValueList. + //------------------------------------------------------------------ + lldb::SBSymbolContextList + FindGlobalFunctions(const char *name, + uint32_t max_matches, + MatchType matchtype); + void Clear (); diff --git a/lldb/include/lldb/Core/ModuleList.h b/lldb/include/lldb/Core/ModuleList.h index fcc1b99c31c1..a46e212da9bb 100644 --- a/lldb/include/lldb/Core/ModuleList.h +++ b/lldb/include/lldb/Core/ModuleList.h @@ -278,6 +278,16 @@ public: uint32_t name_type_mask, SymbolContextList& sc_list); + //------------------------------------------------------------------ + /// @see Module::FindFunctions () + //------------------------------------------------------------------ + size_t + FindFunctions(const RegularExpression &name, + bool include_symbols, + bool include_inlines, + bool append, + SymbolContextList& sc_list); + //------------------------------------------------------------------ /// Find global and static variables by name. /// diff --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h index 45e81b741cbb..5164326c8957 100644 --- a/lldb/include/lldb/lldb-enumerations.h +++ b/lldb/include/lldb/lldb-enumerations.h @@ -864,6 +864,16 @@ namespace lldb { eMemberFunctionKindStaticMethod // A function that applies to a type rather than any instance } MemberFunctionKind; + + //---------------------------------------------------------------------- + // String matching algorithm used by SBTarget + //---------------------------------------------------------------------- + typedef enum MatchType { + eMatchTypeNormal, + eMatchTypeRegex, + eMatchTypeStartsWith + } MatchType; + } // namespace lldb diff --git a/lldb/scripts/Python/interface/SBTarget.i b/lldb/scripts/Python/interface/SBTarget.i index bbf1f74faa5d..4de442e0b8be 100644 --- a/lldb/scripts/Python/interface/SBTarget.i +++ b/lldb/scripts/Python/interface/SBTarget.i @@ -113,6 +113,15 @@ public: void SetDetachOnError(bool enable); + lldb::SBValueList + FindGlobalVariables(const char *name, + uint32_t max_matches, + MatchType matchtype); + + lldb::SBSymbolContextList + FindGlobalFunctions(const char *name, + uint32_t max_matches, + MatchType matchtype); }; class SBAttachInfo diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp index 3d5828c5fe00..e26692d9d8c1 100644 --- a/lldb/source/API/SBTarget.cpp +++ b/lldb/source/API/SBTarget.cpp @@ -58,6 +58,7 @@ #include "lldb/Interpreter/CommandReturnObject.h" #include "../source/Commands/CommandObjectBreakpoint.h" +#include "llvm/Support/Regex.h" using namespace lldb; @@ -2154,6 +2155,34 @@ SBTarget::FindFunctions (const char *name, uint32_t name_type_mask) return sb_sc_list; } +lldb::SBSymbolContextList +SBTarget::FindGlobalFunctions(const char *name, uint32_t max_matches, MatchType matchtype) +{ + lldb::SBSymbolContextList sb_sc_list; + if (name && name[0]) + { + TargetSP target_sp(GetSP()); + if (target_sp) + { + std::string regexstr; + switch (matchtype) + { + case eMatchTypeRegex: + target_sp->GetImages().FindFunctions(RegularExpression(name), true, true, true, *sb_sc_list); + break; + case eMatchTypeStartsWith: + regexstr = llvm::Regex::escape(name) + ".*"; + target_sp->GetImages().FindFunctions(RegularExpression(regexstr.c_str()), true, true, true, *sb_sc_list); + break; + default: + target_sp->GetImages().FindFunctions(ConstString(name), eFunctionNameTypeAny, true, true, true, *sb_sc_list); + break; + } + } + } + return sb_sc_list; +} + lldb::SBType SBTarget::FindFirstType (const char* typename_cstr) { @@ -2321,6 +2350,61 @@ SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches) return sb_value_list; } +SBValueList +SBTarget::FindGlobalVariables(const char *name, uint32_t max_matches, MatchType matchtype) +{ + SBValueList sb_value_list; + + TargetSP target_sp(GetSP()); + if (name && target_sp) + { + VariableList variable_list; + const bool append = true; + + std::string regexstr; + uint32_t match_count; + switch (matchtype) + { + case eMatchTypeNormal: + match_count = target_sp->GetImages().FindGlobalVariables(ConstString(name), + append, + max_matches, + variable_list); + break; + case eMatchTypeRegex: + match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(name), + append, + max_matches, + variable_list); + break; + case eMatchTypeStartsWith: + regexstr = llvm::Regex::escape(name) + ".*"; + match_count = target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr.c_str()), + append, + max_matches, + variable_list); + break; + } + + + if (match_count > 0) + { + ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get(); + if (exe_scope == NULL) + exe_scope = target_sp.get(); + for (uint32_t i = 0; iFindFunctions (name, include_symbols, include_inlines, append, sc_list); + } + + return sc_list.GetSize() - old_size; +} + size_t ModuleList::FindCompileUnits (const FileSpec &path, bool append,