LLDB  mainline
LLVMUserExpression.cpp
Go to the documentation of this file.
1 //===-- LLVMUserExpression.cpp --------------------------------------------===//
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 
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Host/HostInfo.h"
20 #include "lldb/Symbol/Block.h"
21 #include "lldb/Symbol/Function.h"
22 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/Type.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/ThreadPlan.h"
33 #include "lldb/Utility/Log.h"
35 
36 using namespace lldb_private;
37 
39 
41  llvm::StringRef expr,
42  llvm::StringRef prefix,
43  lldb::LanguageType language,
44  ResultType desired_type,
45  const EvaluateExpressionOptions &options)
46  : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
47  m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
48  m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false),
49  m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(),
50  m_materializer_up(), m_jit_module_wp(), m_can_interpret(false),
51  m_materialized_address(LLDB_INVALID_ADDRESS) {}
52 
54  if (m_target) {
55  lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
56  if (jit_module_sp)
57  m_target->GetImages().Remove(jit_module_sp);
58  }
59 }
60 
63  ExecutionContext &exe_ctx,
64  const EvaluateExpressionOptions &options,
65  lldb::UserExpressionSP &shared_ptr_to_me,
66  lldb::ExpressionVariableSP &result) {
67  // The expression log is quite verbose, and if you're just tracking the
68  // execution of the expression, it's quite convenient to have these logs come
69  // out with the STEP log as well.
72 
74  diagnostic_manager.PutString(
76  "Expression can't be run, because there is no JIT compiled function");
78  }
79 
80  lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
81 
82  if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
83  struct_address)) {
84  diagnostic_manager.Printf(
86  "errored out in %s, couldn't PrepareToExecuteJITExpression",
87  __FUNCTION__);
89  }
90 
91  lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
92  lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
93 
94  if (m_can_interpret) {
95  llvm::Module *module = m_execution_unit_sp->GetModule();
96  llvm::Function *function = m_execution_unit_sp->GetFunction();
97 
98  if (!module || !function) {
99  diagnostic_manager.PutString(
101  "supposed to interpret, but nothing is there");
103  }
104 
105  Status interpreter_error;
106 
107  std::vector<lldb::addr_t> args;
108 
109  if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
110  diagnostic_manager.Printf(eDiagnosticSeverityError,
111  "errored out in %s, couldn't AddArguments",
112  __FUNCTION__);
114  }
115 
116  function_stack_bottom = m_stack_frame_bottom;
117  function_stack_top = m_stack_frame_top;
118 
119  IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
120  interpreter_error, function_stack_bottom,
121  function_stack_top, exe_ctx);
122 
123  if (!interpreter_error.Success()) {
124  diagnostic_manager.Printf(eDiagnosticSeverityError,
125  "supposed to interpret, but failed: %s",
126  interpreter_error.AsCString());
128  }
129  } else {
130  if (!exe_ctx.HasThreadScope()) {
131  diagnostic_manager.Printf(eDiagnosticSeverityError,
132  "%s called with no thread selected",
133  __FUNCTION__);
135  }
136 
137  // Store away the thread ID for error reporting, in case it exits
138  // during execution:
139  lldb::tid_t expr_thread_id = exe_ctx.GetThreadRef().GetID();
140 
141  Address wrapper_address(m_jit_start_addr);
142 
143  std::vector<lldb::addr_t> args;
144 
145  if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
146  diagnostic_manager.Printf(eDiagnosticSeverityError,
147  "errored out in %s, couldn't AddArguments",
148  __FUNCTION__);
150  }
151 
152  lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
153  exe_ctx.GetThreadRef(), wrapper_address, args, options,
154  shared_ptr_to_me));
155 
156  StreamString ss;
157  if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
158  diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
160  }
161 
162  ThreadPlanCallUserExpression *user_expression_plan =
163  static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
164 
165  lldb::addr_t function_stack_pointer =
166  user_expression_plan->GetFunctionStackPointer();
167 
168  function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
169  function_stack_top = function_stack_pointer;
170 
171  LLDB_LOGF(log,
172  "-- [UserExpression::Execute] Execution of expression begins --");
173 
174  if (exe_ctx.GetProcessPtr())
175  exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
176 
177  lldb::ExpressionResults execution_result =
178  exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
179  diagnostic_manager);
180 
181  if (exe_ctx.GetProcessPtr())
182  exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
183 
184  LLDB_LOGF(log, "-- [UserExpression::Execute] Execution of expression "
185  "completed --");
186 
187  if (execution_result == lldb::eExpressionInterrupted ||
188  execution_result == lldb::eExpressionHitBreakpoint) {
189  const char *error_desc = nullptr;
190 
191  if (user_expression_plan) {
192  if (auto real_stop_info_sp = user_expression_plan->GetRealStopInfo())
193  error_desc = real_stop_info_sp->GetDescription();
194  }
195  if (error_desc)
196  diagnostic_manager.Printf(eDiagnosticSeverityError,
197  "Execution was interrupted, reason: %s.",
198  error_desc);
199  else
200  diagnostic_manager.PutString(eDiagnosticSeverityError,
201  "Execution was interrupted.");
202 
203  if ((execution_result == lldb::eExpressionInterrupted &&
204  options.DoesUnwindOnError()) ||
205  (execution_result == lldb::eExpressionHitBreakpoint &&
206  options.DoesIgnoreBreakpoints()))
207  diagnostic_manager.AppendMessageToDiagnostic(
208  "The process has been returned to the state before expression "
209  "evaluation.");
210  else {
211  if (execution_result == lldb::eExpressionHitBreakpoint)
212  user_expression_plan->TransferExpressionOwnership();
213  diagnostic_manager.AppendMessageToDiagnostic(
214  "The process has been left at the point where it was "
215  "interrupted, "
216  "use \"thread return -x\" to return to the state before "
217  "expression evaluation.");
218  }
219 
220  return execution_result;
221  } else if (execution_result == lldb::eExpressionStoppedForDebug) {
222  diagnostic_manager.PutString(
224  "Execution was halted at the first instruction of the expression "
225  "function because \"debug\" was requested.\n"
226  "Use \"thread return -x\" to return to the state before expression "
227  "evaluation.");
228  return execution_result;
229  } else if (execution_result == lldb::eExpressionThreadVanished) {
230  diagnostic_manager.Printf(
232  "Couldn't complete execution; the thread "
233  "on which the expression was being run: 0x%" PRIx64
234  " exited during its execution.",
235  expr_thread_id);
236  return execution_result;
237  } else if (execution_result != lldb::eExpressionCompleted) {
238  diagnostic_manager.Printf(
239  eDiagnosticSeverityError, "Couldn't execute function; result was %s",
240  Process::ExecutionResultAsCString(execution_result));
241  return execution_result;
242  }
243  }
244 
245  if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
246  function_stack_bottom, function_stack_top)) {
248  } else {
250  }
251 }
252 
254  DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
255  lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
256  lldb::addr_t function_stack_top) {
258 
259  LLDB_LOGF(log, "-- [UserExpression::FinalizeJITExecution] Dematerializing "
260  "after execution --");
261 
262  if (!m_dematerializer_sp) {
263  diagnostic_manager.Printf(eDiagnosticSeverityError,
264  "Couldn't apply expression side effects : no "
265  "dematerializer is present");
266  return false;
267  }
268 
269  Status dematerialize_error;
270 
271  m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
272  function_stack_top);
273 
274  if (!dematerialize_error.Success()) {
275  diagnostic_manager.Printf(eDiagnosticSeverityError,
276  "Couldn't apply expression side effects : %s",
277  dematerialize_error.AsCString("unknown error"));
278  return false;
279  }
280 
281  result =
283 
284  if (result)
285  result->TransferAddress();
286 
287  m_dematerializer_sp.reset();
288 
289  return true;
290 }
291 
293  DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
294  lldb::addr_t &struct_address) {
295  lldb::TargetSP target;
296  lldb::ProcessSP process;
297  lldb::StackFrameSP frame;
298 
299  if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
300  diagnostic_manager.PutString(
302  "The context has changed before we could JIT the expression!");
303  return false;
304  }
305 
308  Status alloc_error;
309 
313 
314  const bool zero_memory = false;
315 
317  m_materializer_up->GetStructByteSize(),
318  m_materializer_up->GetStructAlignment(),
319  lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
320  zero_memory, alloc_error);
321 
322  if (!alloc_error.Success()) {
323  diagnostic_manager.Printf(
325  "Couldn't allocate space for materialized struct: %s",
326  alloc_error.AsCString());
327  return false;
328  }
329  }
330 
331  struct_address = m_materialized_address;
332 
334  Status alloc_error;
335 
336  const size_t stack_frame_size = 512 * 1024;
337 
338  const bool zero_memory = false;
339 
341  stack_frame_size, 8,
342  lldb::ePermissionsReadable | lldb::ePermissionsWritable,
343  IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
344 
345  m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
346 
347  if (!alloc_error.Success()) {
348  diagnostic_manager.Printf(
350  "Couldn't allocate space for the stack frame: %s",
351  alloc_error.AsCString());
352  return false;
353  }
354  }
355 
356  Status materialize_error;
357 
358  m_dematerializer_sp = m_materializer_up->Materialize(
359  frame, *m_execution_unit_sp, struct_address, materialize_error);
360 
361  if (!materialize_error.Success()) {
362  diagnostic_manager.Printf(eDiagnosticSeverityError,
363  "Couldn't materialize: %s",
364  materialize_error.AsCString());
365  return false;
366  }
367  }
368  return true;
369 }
370 
Block.h
lldb::eExpressionThreadVanished
@ eExpressionThreadVanished
Definition: lldb-enumerations.h:280
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
ExpressionVariable.h
lldb_private::LLVMUserExpression::m_jit_module_wp
lldb::ModuleWP m_jit_module_wp
Definition: LLVMUserExpression.h:107
lldb::eExpressionResultUnavailable
@ eExpressionResultUnavailable
Definition: lldb-enumerations.h:278
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::Process::SetRunningUserExpression
void SetRunningUserExpression(bool on)
Definition: Process.cpp:1465
lldb_private::ExecutionContext::GetProcessPtr
Process * GetProcessPtr() const
Returns a pointer to the process object.
Definition: ExecutionContext.cpp:208
lldb::eExpressionStoppedForDebug
@ eExpressionStoppedForDebug
Definition: lldb-enumerations.h:279
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::IRMemoryMap::AllocationPolicy
AllocationPolicy
Definition: IRMemoryMap.h:39
lldb_private::ThreadPlanCallUserExpression::TransferExpressionOwnership
void TransferExpressionOwnership()
Definition: ThreadPlanCallUserExpression.h:41
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
Module.h
lldb_private::EvaluateExpressionOptions::DoesIgnoreBreakpoints
bool DoesIgnoreBreakpoints() const
Definition: Target.h:311
lldb_private::EvaluateExpressionOptions
Definition: Target.h:262
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb::eExpressionSetupError
@ eExpressionSetupError
Definition: lldb-enumerations.h:272
lldb_private::Expression::m_jit_start_addr
lldb::addr_t m_jit_start_addr
An expression might have a process, but it doesn't need to (e.g.
Definition: Expression.h:96
StackFrame.h
lldb_private::UserExpression
Definition: UserExpression.h:35
lldb_private::IRMemoryMap::eAllocationPolicyHostOnly
@ eAllocationPolicyHostOnly
This allocation was created in the host and will never make it into the process.
Definition: IRMemoryMap.h:42
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
IRInterpreter.h
lldb_private::UserExpression::GetResultAfterDematerialization
virtual lldb::ExpressionVariableSP GetResultAfterDematerialization(ExecutionContextScope *exe_scope)
Definition: UserExpression.h:212
lldb_private::ExecutionContext::GetProcessRef
Process & GetProcessRef() const
Returns a reference to the process object.
Definition: ExecutionContext.cpp:231
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:928
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
Process.h
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
Target.h
lldb_private::ThreadPlanCallUserExpression::GetRealStopInfo
lldb::StopInfoSP GetRealStopInfo() override
Definition: ThreadPlanCallUserExpression.cpp:99
LIBLLDB_LOG_EXPRESSIONS
#define LIBLLDB_LOG_EXPRESSIONS
Definition: Logging.h:22
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:288
lldb_private::eDiagnosticSeverityError
@ eDiagnosticSeverityError
Definition: DiagnosticManager.h:32
lldb_private::ModuleList::Remove
bool Remove(const lldb::ModuleSP &module_sp, bool notify=true)
Remove a module from the module list.
lldb_private::ThreadPlanCallUserExpression
Definition: ThreadPlanCallUserExpression.h:22
Log.h
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb::eExpressionDiscarded
@ eExpressionDiscarded
Definition: lldb-enumerations.h:274
lldb::eExpressionCompleted
@ eExpressionCompleted
Definition: lldb-enumerations.h:271
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::Process::ExecutionResultAsCString
static const char * ExecutionResultAsCString(lldb::ExpressionResults result)
Definition: Process.cpp:5440
StreamString.h
Type.h
lldb_private::IRMemoryMap::eAllocationPolicyMirror
@ eAllocationPolicyMirror
The intent is that this allocation exist both in the host and the process and have the same content i...
Definition: IRMemoryMap.h:46
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::eDiagnosticSeverityRemark
@ eDiagnosticSeverityRemark
Definition: DiagnosticManager.h:34
lldb_private::StreamString
Definition: StreamString.h:23
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::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
IRExecutionUnit.h
HostInfo.h
lldb::eExpressionInterrupted
@ eExpressionInterrupted
Definition: lldb-enumerations.h:275
IRInterpreter::Interpret
static bool Interpret(llvm::Module &module, llvm::Function &function, llvm::ArrayRef< lldb::addr_t > args, lldb_private::IRExecutionUnit &execution_unit, lldb_private::Status &error, lldb::addr_t stack_frame_bottom, lldb::addr_t stack_frame_top, lldb_private::ExecutionContext &exe_ctx)
Definition: IRInterpreter.cpp:633
VariableList.h
lldb_private::ExecutionContext::HasThreadScope
bool HasThreadScope() const
Returns true the ExecutionContext object contains a valid target, process, and thread.
Definition: ExecutionContext.cpp:389
ObjectFile.h
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
ValueObjectConstResult.h
lldb::eExpressionHitBreakpoint
@ eExpressionHitBreakpoint
Definition: lldb-enumerations.h:276
ThreadPlan.h
StreamFile.h
lldb_private::ExecutionContext::GetThreadRef
Thread & GetThreadRef() const
Returns a reference to the thread object.
Definition: ExecutionContext.cpp:236
Materializer.h
lldb_private::Status
Definition: Status.h:44
lldb_private::DiagnosticManager::AppendMessageToDiagnostic
void AppendMessageToDiagnostic(llvm::StringRef str)
Definition: DiagnosticManager.h:124
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:216
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::Address
Definition: Address.h:59
lldb_private::DiagnosticManager::PutString
size_t void PutString(DiagnosticSeverity severity, llvm::StringRef str)
Definition: DiagnosticManager.cpp:73
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
ThreadPlanCallUserExpression.h
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
lldb_private::EvaluateExpressionOptions::DoesUnwindOnError
bool DoesUnwindOnError() const
Definition: Target.h:307
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
Function.h
DiagnosticManager.h
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
LIBLLDB_LOG_STEP
#define LIBLLDB_LOG_STEP
Definition: Logging.h:21
ConstString.h
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::UserExpression::LockAndCheckContext
bool LockAndCheckContext(ExecutionContext &exe_ctx, lldb::TargetSP &target_sp, lldb::ProcessSP &process_sp, lldb::StackFrameSP &frame_sp)
Definition: UserExpression.cpp:67
lldb_private::Log
Definition: Log.h:49
lldb_private::GetLogIfAnyCategoriesSet
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:62
lldb_private::Process::RunThreadPlan
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition: Process.cpp:4651
SymbolVendor.h
lldb_private::ThreadPlanCallFunction::GetFunctionStackPointer
lldb::addr_t GetFunctionStackPointer()
Definition: ThreadPlanCallFunction.h:69
LLVMUserExpression.h
ExecutionContext.h
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
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86
lldb_private::DiagnosticManager::Printf
size_t Printf(DiagnosticSeverity severity, const char *format,...) __attribute__((format(printf
Definition: DiagnosticManager.cpp:59
lldb_private::Status::AsCString
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:131