1 //===-- LLVMUserExpression.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Expression/LLVMUserExpression.h"
15 #include "lldb/Core/ConstString.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Core/StreamString.h"
20 #include "lldb/Core/ValueObjectConstResult.h"
21 #include "lldb/Expression/DiagnosticManager.h"
22 #include "lldb/Expression/ExpressionSourceCode.h"
23 #include "lldb/Expression/IRExecutionUnit.h"
24 #include "lldb/Expression/IRInterpreter.h"
25 #include "lldb/Expression/Materializer.h"
26 #include "lldb/Host/HostInfo.h"
27 #include "lldb/Symbol/Block.h"
28 #include "lldb/Symbol/ClangASTContext.h"
29 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
30 #include "lldb/Symbol/Function.h"
31 #include "lldb/Symbol/ObjectFile.h"
32 #include "lldb/Symbol/SymbolVendor.h"
33 #include "lldb/Symbol/Type.h"
34 #include "lldb/Symbol/VariableList.h"
35 #include "lldb/Target/ExecutionContext.h"
36 #include "lldb/Target/Process.h"
37 #include "lldb/Target/StackFrame.h"
38 #include "lldb/Target/Target.h"
39 #include "lldb/Target/ThreadPlan.h"
40 #include "lldb/Target/ThreadPlanCallUserExpression.h"
42 using namespace lldb_private;
44 LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
46 llvm::StringRef prefix,
47 lldb::LanguageType language,
48 ResultType desired_type,
49 const EvaluateExpressionOptions &options)
50 : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
51 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
52 m_stack_frame_top(LLDB_INVALID_ADDRESS), m_transformed_text(),
53 m_execution_unit_sp(), m_materializer_ap(), m_jit_module_wp(),
54 m_enforce_valid_object(true), m_in_cplusplus_method(false),
55 m_in_objectivec_method(false), m_in_static_method(false),
56 m_needs_object_ptr(false), m_target(NULL), m_can_interpret(false),
57 m_materialized_address(LLDB_INVALID_ADDRESS) {}
59 LLVMUserExpression::~LLVMUserExpression() {
61 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
63 m_target->GetImages().Remove(jit_module_sp);
67 lldb::ExpressionResults
68 LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
69 ExecutionContext &exe_ctx,
70 const EvaluateExpressionOptions &options,
71 lldb::UserExpressionSP &shared_ptr_to_me,
72 lldb::ExpressionVariableSP &result) {
73 // The expression log is quite verbose, and if you're just tracking the
75 // expression, it's quite convenient to have these logs come out with the STEP
77 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
80 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
81 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
83 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
85 diagnostic_manager.Printf(
86 eDiagnosticSeverityError,
87 "errored out in %s, couldn't PrepareToExecuteJITExpression",
89 return lldb::eExpressionSetupError;
92 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
93 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
95 if (m_can_interpret) {
96 llvm::Module *module = m_execution_unit_sp->GetModule();
97 llvm::Function *function = m_execution_unit_sp->GetFunction();
99 if (!module || !function) {
100 diagnostic_manager.PutString(
101 eDiagnosticSeverityError,
102 "supposed to interpret, but nothing is there");
103 return lldb::eExpressionSetupError;
106 Error interpreter_error;
108 std::vector<lldb::addr_t> args;
110 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
111 diagnostic_manager.Printf(eDiagnosticSeverityError,
112 "errored out in %s, couldn't AddArguments",
114 return lldb::eExpressionSetupError;
117 function_stack_bottom = m_stack_frame_bottom;
118 function_stack_top = m_stack_frame_top;
120 IRInterpreter::Interpret(*module, *function, args,
121 *m_execution_unit_sp.get(), interpreter_error,
122 function_stack_bottom, function_stack_top,
125 if (!interpreter_error.Success()) {
126 diagnostic_manager.Printf(eDiagnosticSeverityError,
127 "supposed to interpret, but failed: %s",
128 interpreter_error.AsCString());
129 return lldb::eExpressionDiscarded;
132 if (!exe_ctx.HasThreadScope()) {
133 diagnostic_manager.Printf(eDiagnosticSeverityError,
134 "%s called with no thread selected",
136 return lldb::eExpressionSetupError;
139 Address wrapper_address(m_jit_start_addr);
141 std::vector<lldb::addr_t> args;
143 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
144 diagnostic_manager.Printf(eDiagnosticSeverityError,
145 "errored out in %s, couldn't AddArguments",
147 return lldb::eExpressionSetupError;
150 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
151 exe_ctx.GetThreadRef(), wrapper_address, args, options,
155 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
156 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
157 return lldb::eExpressionSetupError;
160 ThreadPlanCallUserExpression *user_expression_plan =
161 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
163 lldb::addr_t function_stack_pointer =
164 user_expression_plan->GetFunctionStackPointer();
166 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
167 function_stack_top = function_stack_pointer;
171 "-- [UserExpression::Execute] Execution of expression begins --");
173 if (exe_ctx.GetProcessPtr())
174 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
176 lldb::ExpressionResults execution_result =
177 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
180 if (exe_ctx.GetProcessPtr())
181 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
184 log->Printf("-- [UserExpression::Execute] Execution of expression "
187 if (execution_result == lldb::eExpressionInterrupted ||
188 execution_result == lldb::eExpressionHitBreakpoint) {
189 const char *error_desc = NULL;
192 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
193 if (real_stop_info_sp)
194 error_desc = real_stop_info_sp->GetDescription();
197 diagnostic_manager.Printf(eDiagnosticSeverityError,
198 "Execution was interrupted, reason: %s.",
201 diagnostic_manager.PutString(eDiagnosticSeverityError,
202 "Execution was interrupted.");
204 if ((execution_result == lldb::eExpressionInterrupted &&
205 options.DoesUnwindOnError()) ||
206 (execution_result == lldb::eExpressionHitBreakpoint &&
207 options.DoesIgnoreBreakpoints()))
208 diagnostic_manager.AppendMessageToDiagnostic(
209 "The process has been returned to the state before expression "
212 if (execution_result == lldb::eExpressionHitBreakpoint)
213 user_expression_plan->TransferExpressionOwnership();
214 diagnostic_manager.AppendMessageToDiagnostic(
215 "The process has been left at the point where it was "
217 "use \"thread return -x\" to return to the state before "
218 "expression evaluation.");
221 return execution_result;
222 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
223 diagnostic_manager.PutString(
224 eDiagnosticSeverityRemark,
225 "Execution was halted at the first instruction of the expression "
226 "function because \"debug\" was requested.\n"
227 "Use \"thread return -x\" to return to the state before expression "
229 return execution_result;
230 } else if (execution_result != lldb::eExpressionCompleted) {
231 diagnostic_manager.Printf(
232 eDiagnosticSeverityError,
233 "Couldn't execute function; result was %s",
234 Process::ExecutionResultAsCString(execution_result));
235 return execution_result;
239 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
240 function_stack_bottom, function_stack_top)) {
241 return lldb::eExpressionCompleted;
243 return lldb::eExpressionResultUnavailable;
246 diagnostic_manager.PutString(
247 eDiagnosticSeverityError,
248 "Expression can't be run, because there is no JIT compiled function");
249 return lldb::eExpressionSetupError;
253 bool LLVMUserExpression::FinalizeJITExecution(
254 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
255 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
256 lldb::addr_t function_stack_top) {
257 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
260 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
261 "after execution --");
263 if (!m_dematerializer_sp) {
264 diagnostic_manager.Printf(eDiagnosticSeverityError,
265 "Couldn't apply expression side effects : no "
266 "dematerializer is present");
270 Error dematerialize_error;
272 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
275 if (!dematerialize_error.Success()) {
276 diagnostic_manager.Printf(eDiagnosticSeverityError,
277 "Couldn't apply expression side effects : %s",
278 dematerialize_error.AsCString("unknown error"));
283 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
286 result->TransferAddress();
288 m_dematerializer_sp.reset();
293 bool LLVMUserExpression::PrepareToExecuteJITExpression(
294 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
295 lldb::addr_t &struct_address) {
296 lldb::TargetSP target;
297 lldb::ProcessSP process;
298 lldb::StackFrameSP frame;
300 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
301 diagnostic_manager.PutString(
302 eDiagnosticSeverityError,
303 "The context has changed before we could JIT the expression!");
307 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
308 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
311 IRMemoryMap::AllocationPolicy policy =
312 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
313 : IRMemoryMap::eAllocationPolicyMirror;
315 const bool zero_memory = false;
317 m_materialized_address = m_execution_unit_sp->Malloc(
318 m_materializer_ap->GetStructByteSize(),
319 m_materializer_ap->GetStructAlignment(),
320 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
321 zero_memory, alloc_error);
323 if (!alloc_error.Success()) {
324 diagnostic_manager.Printf(
325 eDiagnosticSeverityError,
326 "Couldn't allocate space for materialized struct: %s",
327 alloc_error.AsCString());
332 struct_address = m_materialized_address;
334 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
337 const size_t stack_frame_size = 512 * 1024;
339 const bool zero_memory = false;
341 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
343 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
344 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
346 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
348 if (!alloc_error.Success()) {
349 diagnostic_manager.Printf(
350 eDiagnosticSeverityError,
351 "Couldn't allocate space for the stack frame: %s",
352 alloc_error.AsCString());
357 Error materialize_error;
359 m_dematerializer_sp = m_materializer_ap->Materialize(
360 frame, *m_execution_unit_sp, struct_address, materialize_error);
362 if (!materialize_error.Success()) {
363 diagnostic_manager.Printf(eDiagnosticSeverityError,
364 "Couldn't materialize: %s",
365 materialize_error.AsCString());
372 lldb::ModuleSP LLVMUserExpression::GetJITModule() {
373 if (m_execution_unit_sp)
374 return m_execution_unit_sp->GetJITModule();
375 return lldb::ModuleSP();