1 //===-- UtilityFunction.h ----------------------------------------*- C++ 2 //-*-===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLDB_EXPRESSION_UTILITYFUNCTION_H 11 #define LLDB_EXPRESSION_UTILITYFUNCTION_H 12 13 #include <memory> 14 #include <string> 15 16 #include "lldb/Expression/Expression.h" 17 #include "lldb/lldb-forward.h" 18 #include "lldb/lldb-private.h" 19 20 namespace lldb_private { 21 22 /// \class UtilityFunction UtilityFunction.h 23 /// "lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that 24 /// provides a function that is callable 25 /// 26 /// LLDB uses expressions for various purposes, notably to call functions 27 /// and as a backend for the expr command. UtilityFunction encapsulates a 28 /// self-contained function meant to be used from other code. Utility 29 /// functions can perform error-checking for ClangUserExpressions, 30 class UtilityFunction : public Expression { 31 // LLVM RTTI support 32 static char ID; 33 34 public: isA(const void * ClassID)35 bool isA(const void *ClassID) const override { return ClassID == &ID; } classof(const Expression * obj)36 static bool classof(const Expression *obj) { return obj->isA(&ID); } 37 38 /// Constructor 39 /// 40 /// \param[in] text 41 /// The text of the function. Must be a full translation unit. 42 /// 43 /// \param[in] name 44 /// The name of the function, as used in the text. 45 UtilityFunction(ExecutionContextScope &exe_scope, std::string text, 46 std::string name); 47 48 ~UtilityFunction() override; 49 50 /// Install the utility function into a process 51 /// 52 /// \param[in] diagnostic_manager 53 /// A diagnostic manager to print parse errors and warnings to. 54 /// 55 /// \param[in] exe_ctx 56 /// The execution context to install the utility function to. 57 /// 58 /// \return 59 /// True on success (no errors); false otherwise. 60 virtual bool Install(DiagnosticManager &diagnostic_manager, 61 ExecutionContext &exe_ctx) = 0; 62 63 /// Check whether the given address is inside the function 64 /// 65 /// Especially useful if the function dereferences nullptr to indicate a 66 /// failed assert. 67 /// 68 /// \param[in] address 69 /// The address to check. 70 /// 71 /// \return 72 /// True if the address falls within the function's bounds; 73 /// false if not (or the function is not JIT compiled) ContainsAddress(lldb::addr_t address)74 bool ContainsAddress(lldb::addr_t address) { 75 // nothing is both >= LLDB_INVALID_ADDRESS and < LLDB_INVALID_ADDRESS, so 76 // this always returns false if the function is not JIT compiled yet 77 return (address >= m_jit_start_addr && address < m_jit_end_addr); 78 } 79 80 /// Return the string that the parser should parse. Must be a full 81 /// translation unit. Text()82 const char *Text() override { return m_function_text.c_str(); } 83 84 /// Return the function name that should be used for executing the 85 /// expression. Text() should contain the definition of this function. FunctionName()86 const char *FunctionName() override { return m_function_name.c_str(); } 87 88 /// Return the object that the parser should use when registering local 89 /// variables. May be nullptr if the Expression doesn't care. LocalVariables()90 ExpressionVariableList *LocalVariables() { return nullptr; } 91 92 /// Return true if validation code should be inserted into the expression. NeedsValidation()93 bool NeedsValidation() override { return false; } 94 95 /// Return true if external variables in the expression should be resolved. NeedsVariableResolution()96 bool NeedsVariableResolution() override { return false; } 97 98 // This makes the function caller function. Pass in the ThreadSP if you have 99 // one available, compilation can end up calling code (e.g. to look up 100 // indirect functions) and we don't want this to wander onto another thread. 101 FunctionCaller *MakeFunctionCaller(const CompilerType &return_type, 102 const ValueList &arg_value_list, 103 lldb::ThreadSP compilation_thread, 104 Status &error); 105 106 // This one retrieves the function caller that is already made. If you 107 // haven't made it yet, this returns nullptr GetFunctionCaller()108 FunctionCaller *GetFunctionCaller() { return m_caller_up.get(); } 109 110 protected: 111 std::shared_ptr<IRExecutionUnit> m_execution_unit_sp; 112 lldb::ModuleWP m_jit_module_wp; 113 /// The text of the function. Must be a well-formed translation unit. 114 std::string m_function_text; 115 /// The name of the function. 116 std::string m_function_name; 117 std::unique_ptr<FunctionCaller> m_caller_up; 118 }; 119 120 } // namespace lldb_private 121 122 #endif // LLDB_EXPRESSION_UTILITYFUNCTION_H 123