UtilityFunction.h 4.7 KB

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