Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 10 | #ifndef LLDB_EXPRESSION_UTILITYFUNCTION_H |
| 11 | #define LLDB_EXPRESSION_UTILITYFUNCTION_H |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 31 | // LLVM RTTI support |
| 32 | static char ID; |
| 33 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 34 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 35 | bool isA(const void *ClassID) const override { return ClassID == &ID; } |
| 36 | static bool classof(const Expression *obj) { return obj->isA(&ID); } |
| 37 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 45 | UtilityFunction(ExecutionContextScope &exe_scope, std::string text, |
| 46 | std::string name); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 63 | /// Check whether the given address is inside the function |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 64 | /// |
| 65 | /// Especially useful if the function dereferences nullptr to indicate a |
| 66 | /// failed assert. |
| 67 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 68 | /// \param[in] address |
| 69 | /// The address to check. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 70 | /// |
| 71 | /// \return |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 72 | /// True if the address falls within the function's bounds; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 73 | /// false if not (or the function is not JIT compiled) |
| 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. |
| 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. |
| 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. |
| 90 | ExpressionVariableList *LocalVariables() { return nullptr; } |
| 91 | |
| 92 | /// Return true if validation code should be inserted into the expression. |
| 93 | bool NeedsValidation() override { return false; } |
| 94 | |
| 95 | /// Return true if external variables in the expression should be resolved. |
| 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 |
| 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; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 117 | std::unique_ptr<FunctionCaller> m_caller_up; |
| 118 | }; |
| 119 | |
| 120 | } // namespace lldb_private |
| 121 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 122 | #endif // LLDB_EXPRESSION_UTILITYFUNCTION_H |