LLDB  mainline
LLVMUserExpression.h
Go to the documentation of this file.
1 //===-- LLVMUserExpression.h ------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_EXPRESSION_LLVMUSEREXPRESSION_H
10 #define LLDB_EXPRESSION_LLVMUSEREXPRESSION_H
11 
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 #include "llvm/IR/LegacyPassManager.h"
17 
19 
20 namespace lldb_private {
21 
22 /// \class LLVMUserExpression LLVMUserExpression.h
23 /// "lldb/Expression/LLVMUserExpression.h" Encapsulates a one-time expression
24 /// for use in lldb.
25 ///
26 /// LLDB uses expressions for various purposes, notably to call functions
27 /// and as a backend for the expr command. LLVMUserExpression is a virtual
28 /// base class that encapsulates the objects needed to parse and JIT an
29 /// expression. The actual parsing part will be provided by the specific
30 /// implementations of LLVMUserExpression - which will be vended through the
31 /// appropriate TypeSystem.
33  // LLVM RTTI support
34  static char ID;
35 
36 public:
37  bool isA(const void *ClassID) const override {
38  return ClassID == &ID || UserExpression::isA(ClassID);
39  }
40  static bool classof(const Expression *obj) { return obj->isA(&ID); }
41 
42  // The IRPasses struct is filled in by a runtime after an expression is
43  // compiled and can be used to to run fixups/analysis passes as required.
44  // EarlyPasses are run on the generated module before lldb runs its own IR
45  // fixups and inserts instrumentation code/pointer checks. LatePasses are run
46  // after the module has been processed by llvm, before the module is
47  // assembled and run in the ThreadPlan.
48  struct IRPasses {
49  IRPasses() : EarlyPasses(nullptr), LatePasses(nullptr){};
50  std::shared_ptr<llvm::legacy::PassManager> EarlyPasses;
51  std::shared_ptr<llvm::legacy::PassManager> LatePasses;
52  };
53 
54  LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
55  llvm::StringRef prefix, lldb::LanguageType language,
56  ResultType desired_type,
57  const EvaluateExpressionOptions &options);
58  ~LLVMUserExpression() override;
59 
61  DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
62  lldb::ExpressionVariableSP &result,
63  lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS,
64  lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS) override;
65 
66  bool CanInterpret() override { return m_can_interpret; }
67 
68  Materializer *GetMaterializer() override { return m_materializer_up.get(); }
69 
70  /// Return the string that the parser should parse. Must be a full
71  /// translation unit.
72  const char *Text() override { return m_transformed_text.c_str(); }
73 
74 protected:
76  DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
77  const EvaluateExpressionOptions &options,
78  lldb::UserExpressionSP &shared_ptr_to_me,
79  lldb::ExpressionVariableSP &result) override;
80 
81  virtual void ScanContext(ExecutionContext &exe_ctx,
82  lldb_private::Status &err) = 0;
83 
84  bool PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager,
85  ExecutionContext &exe_ctx,
86  lldb::addr_t &struct_address);
87 
88  virtual bool AddArguments(ExecutionContext &exe_ctx,
89  std::vector<lldb::addr_t> &args,
90  lldb::addr_t struct_address,
91  DiagnosticManager &diagnostic_manager) = 0;
92 
94  m_stack_frame_bottom; ///< The bottom of the allocated stack frame.
95  lldb::addr_t m_stack_frame_top; ///< The top of the allocated stack frame.
96 
97  bool m_allow_cxx; ///< True if the language allows C++.
98  bool m_allow_objc; ///< True if the language allows Objective-C.
100  m_transformed_text; ///< The text of the expression, as send to the parser
101 
102  std::shared_ptr<IRExecutionUnit>
103  m_execution_unit_sp; ///< The execution unit the expression is stored in.
104  std::unique_ptr<Materializer> m_materializer_up; ///< The materializer to use
105  /// when running the
106  /// expression.
107  lldb::ModuleWP m_jit_module_wp;
108  Target *m_target; ///< The target for storing persistent data like types and
109  ///variables.
110 
111  bool m_can_interpret; ///< True if the expression could be evaluated
112  ///statically; false otherwise.
113  lldb::addr_t m_materialized_address; ///< The address at which the arguments
114  ///to the expression have been
115  ///materialized.
117 };
118 
119 } // namespace lldb_private
120 #endif
lldb_private::LLVMUserExpression::LLVMUserExpression
LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, ResultType desired_type, const EvaluateExpressionOptions &options)
Definition: LLVMUserExpression.cpp:40
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::LLVMUserExpression::IRPasses::IRPasses
IRPasses()
Definition: LLVMUserExpression.h:49
lldb_private::LLVMUserExpression::m_jit_module_wp
lldb::ModuleWP m_jit_module_wp
Definition: LLVMUserExpression.h:107
lldb_private::LLVMUserExpression::m_can_interpret
bool m_can_interpret
True if the expression could be evaluated statically; false otherwise.
Definition: LLVMUserExpression.h:111
lldb_private::LLVMUserExpression::classof
static bool classof(const Expression *obj)
Definition: LLVMUserExpression.h:40
lldb_private::LLVMUserExpression::DoExecute
lldb::ExpressionResults DoExecute(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, lldb::UserExpressionSP &shared_ptr_to_me, lldb::ExpressionVariableSP &result) override
Definition: LLVMUserExpression.cpp:62
lldb::ExpressionResults
ExpressionResults
The results of expression evaluation.
Definition: lldb-enumerations.h:270
lldb_private::LLVMUserExpression::m_allow_cxx
bool m_allow_cxx
True if the language allows C++.
Definition: LLVMUserExpression.h:97
lldb_private::UserExpression::isA
bool isA(const void *ClassID) const override
Definition: UserExpression.h:40
lldb_private::EvaluateExpressionOptions
Definition: Target.h:259
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
UserExpression.h
lldb_private::UserExpression
Definition: UserExpression.h:35
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::LLVMUserExpression::CanInterpret
bool CanInterpret() override
Definition: LLVMUserExpression.h:66
lldb_private::Target
Definition: Target.h:451
lldb_private::Expression::isA
virtual bool isA(const void *ClassID) const =0
lldb_private::Expression::ResultType
ResultType
Definition: Expression.h:35
lldb_private::LLVMUserExpression::m_dematerializer_sp
Materializer::DematerializerSP m_dematerializer_sp
The dematerializer.
Definition: LLVMUserExpression.h:116
lldb_private::LLVMUserExpression::IRPasses
Definition: LLVMUserExpression.h:48
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::LLVMUserExpression::isA
bool isA(const void *ClassID) const override
Definition: LLVMUserExpression.h:37
lldb_private::Materializer::DematerializerSP
std::shared_ptr< Dematerializer > DematerializerSP
Definition: Materializer.h:64
lldb_private::LLVMUserExpression::m_execution_unit_sp
std::shared_ptr< IRExecutionUnit > m_execution_unit_sp
The execution unit the expression is stored in.
Definition: LLVMUserExpression.h:103
lldb_private::LLVMUserExpression
Definition: LLVMUserExpression.h:32
lldb_private::LLVMUserExpression::m_stack_frame_bottom
lldb::addr_t m_stack_frame_bottom
The bottom of the allocated stack frame.
Definition: LLVMUserExpression.h:94
lldb_private::LLVMUserExpression::FinalizeJITExecution
bool FinalizeJITExecution(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom=LLDB_INVALID_ADDRESS, lldb::addr_t function_stack_top=LLDB_INVALID_ADDRESS) override
Apply the side effects of the function to program state.
Definition: LLVMUserExpression.cpp:253
lldb_private::LLVMUserExpression::m_transformed_text
std::string m_transformed_text
The text of the expression, as send to the parser.
Definition: LLVMUserExpression.h:100
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Materializer
Definition: Materializer.h:23
lldb_private::LLVMUserExpression::GetMaterializer
Materializer * GetMaterializer() override
Return the Materializer that the parser should use when registering external values.
Definition: LLVMUserExpression.h:68
lldb_private::Status
Definition: Status.h:44
lldb_private::LLVMUserExpression::m_materializer_up
std::unique_ptr< Materializer > m_materializer_up
The materializer to use when running the expression.
Definition: LLVMUserExpression.h:104
lldb_private::LLVMUserExpression::Text
const char * Text() override
Return the string that the parser should parse.
Definition: LLVMUserExpression.h:72
lldb_private::LLVMUserExpression::m_materialized_address
lldb::addr_t m_materialized_address
The address at which the arguments to the expression have been materialized.
Definition: LLVMUserExpression.h:113
lldb_private::LLVMUserExpression::m_allow_objc
bool m_allow_objc
True if the language allows Objective-C.
Definition: LLVMUserExpression.h:98
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::LLVMUserExpression::m_stack_frame_top
lldb::addr_t m_stack_frame_top
The top of the allocated stack frame.
Definition: LLVMUserExpression.h:95
lldb_private::LLVMUserExpression::m_target
Target * m_target
The target for storing persistent data like types and variables.
Definition: LLVMUserExpression.h:108
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::LLVMUserExpression::ScanContext
virtual void ScanContext(ExecutionContext &exe_ctx, lldb_private::Status &err)=0
lldb_private::LLVMUserExpression::AddArguments
virtual bool AddArguments(ExecutionContext &exe_ctx, std::vector< lldb::addr_t > &args, lldb::addr_t struct_address, DiagnosticManager &diagnostic_manager)=0
lldb_private::LLVMUserExpression::IRPasses::EarlyPasses
std::shared_ptr< llvm::legacy::PassManager > EarlyPasses
Definition: LLVMUserExpression.h:49
lldb_private::Expression
Definition: Expression.h:33
lldb_private::LLVMUserExpression::IRPasses::LatePasses
std::shared_ptr< llvm::legacy::PassManager > LatePasses
Definition: LLVMUserExpression.h:51
lldb_private::LLVMUserExpression::ID
static char ID
Definition: LLVMUserExpression.h:34
lldb_private::LLVMUserExpression::PrepareToExecuteJITExpression
bool PrepareToExecuteJITExpression(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, lldb::addr_t &struct_address)
Definition: LLVMUserExpression.cpp:292
lldb_private::LLVMUserExpression::~LLVMUserExpression
~LLVMUserExpression() override
Definition: LLVMUserExpression.cpp:53