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 //===----------------------------------------------------------------------===//
11 #include "lldb/Expression/LLVMUserExpression.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Core/ValueObjectConstResult.h"
15 #include "lldb/Expression/DiagnosticManager.h"
16 #include "lldb/Expression/ExpressionSourceCode.h"
17 #include "lldb/Expression/IRExecutionUnit.h"
18 #include "lldb/Expression/IRInterpreter.h"
19 #include "lldb/Expression/Materializer.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Symbol/Block.h"
22 #include "lldb/Symbol/ClangASTContext.h"
23 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/ObjectFile.h"
26 #include "lldb/Symbol/SymbolVendor.h"
27 #include "lldb/Symbol/Type.h"
28 #include "lldb/Symbol/VariableList.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/StackFrame.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/ThreadPlan.h"
34 #include "lldb/Target/ThreadPlanCallUserExpression.h"
35 #include "lldb/Utility/ConstString.h"
36 #include "lldb/Utility/Log.h"
37 #include "lldb/Utility/StreamString.h"
39 using namespace lldb_private;
41 LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
43 llvm::StringRef prefix,
44 lldb::LanguageType language,
45 ResultType desired_type,
46 const EvaluateExpressionOptions &options)
47 : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
48 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
49 m_stack_frame_top(LLDB_INVALID_ADDRESS),
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
74 // execution of the expression, it's quite convenient to have these logs come
75 // out with the STEP log as well.
76 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
79 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
80 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
82 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
84 diagnostic_manager.Printf(
85 eDiagnosticSeverityError,
86 "errored out in %s, couldn't PrepareToExecuteJITExpression",
88 return lldb::eExpressionSetupError;
91 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
92 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
94 if (m_can_interpret) {
95 llvm::Module *module = m_execution_unit_sp->GetModule();
96 llvm::Function *function = m_execution_unit_sp->GetFunction();
98 if (!module || !function) {
99 diagnostic_manager.PutString(
100 eDiagnosticSeverityError,
101 "supposed to interpret, but nothing is there");
102 return lldb::eExpressionSetupError;
105 Status interpreter_error;
107 std::vector<lldb::addr_t> args;
109 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
110 diagnostic_manager.Printf(eDiagnosticSeverityError,
111 "errored out in %s, couldn't AddArguments",
113 return lldb::eExpressionSetupError;
116 function_stack_bottom = m_stack_frame_bottom;
117 function_stack_top = m_stack_frame_top;
119 IRInterpreter::Interpret(*module, *function, args,
120 *m_execution_unit_sp.get(), interpreter_error,
121 function_stack_bottom, function_stack_top,
124 if (!interpreter_error.Success()) {
125 diagnostic_manager.Printf(eDiagnosticSeverityError,
126 "supposed to interpret, but failed: %s",
127 interpreter_error.AsCString());
128 return lldb::eExpressionDiscarded;
131 if (!exe_ctx.HasThreadScope()) {
132 diagnostic_manager.Printf(eDiagnosticSeverityError,
133 "%s called with no thread selected",
135 return lldb::eExpressionSetupError;
138 Address wrapper_address(m_jit_start_addr);
140 std::vector<lldb::addr_t> args;
142 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
143 diagnostic_manager.Printf(eDiagnosticSeverityError,
144 "errored out in %s, couldn't AddArguments",
146 return lldb::eExpressionSetupError;
149 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
150 exe_ctx.GetThreadRef(), wrapper_address, args, options,
154 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
155 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
156 return lldb::eExpressionSetupError;
159 ThreadPlanCallUserExpression *user_expression_plan =
160 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
162 lldb::addr_t function_stack_pointer =
163 user_expression_plan->GetFunctionStackPointer();
165 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
166 function_stack_top = function_stack_pointer;
170 "-- [UserExpression::Execute] Execution of expression begins --");
172 if (exe_ctx.GetProcessPtr())
173 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
175 lldb::ExpressionResults execution_result =
176 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
179 if (exe_ctx.GetProcessPtr())
180 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
183 log->Printf("-- [UserExpression::Execute] Execution of expression "
186 if (execution_result == lldb::eExpressionInterrupted ||
187 execution_result == lldb::eExpressionHitBreakpoint) {
188 const char *error_desc = NULL;
191 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
192 if (real_stop_info_sp)
193 error_desc = real_stop_info_sp->GetDescription();
196 diagnostic_manager.Printf(eDiagnosticSeverityError,
197 "Execution was interrupted, reason: %s.",
200 diagnostic_manager.PutString(eDiagnosticSeverityError,
201 "Execution was interrupted.");
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 "
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 "
216 "use \"thread return -x\" to return to the state before "
217 "expression evaluation.");
220 return execution_result;
221 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
222 diagnostic_manager.PutString(
223 eDiagnosticSeverityRemark,
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 "
228 return execution_result;
229 } else if (execution_result != lldb::eExpressionCompleted) {
230 diagnostic_manager.Printf(
231 eDiagnosticSeverityError,
232 "Couldn't execute function; result was %s",
233 Process::ExecutionResultAsCString(execution_result));
234 return execution_result;
238 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
239 function_stack_bottom, function_stack_top)) {
240 return lldb::eExpressionCompleted;
242 return lldb::eExpressionResultUnavailable;
245 diagnostic_manager.PutString(
246 eDiagnosticSeverityError,
247 "Expression can't be run, because there is no JIT compiled function");
248 return lldb::eExpressionSetupError;
252 bool LLVMUserExpression::FinalizeJITExecution(
253 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
254 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
255 lldb::addr_t function_stack_top) {
256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
259 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
260 "after execution --");
262 if (!m_dematerializer_sp) {
263 diagnostic_manager.Printf(eDiagnosticSeverityError,
264 "Couldn't apply expression side effects : no "
265 "dematerializer is present");
269 Status dematerialize_error;
271 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
274 if (!dematerialize_error.Success()) {
275 diagnostic_manager.Printf(eDiagnosticSeverityError,
276 "Couldn't apply expression side effects : %s",
277 dematerialize_error.AsCString("unknown error"));
282 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
285 result->TransferAddress();
287 m_dematerializer_sp.reset();
292 bool LLVMUserExpression::PrepareToExecuteJITExpression(
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;
299 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
300 diagnostic_manager.PutString(
301 eDiagnosticSeverityError,
302 "The context has changed before we could JIT the expression!");
306 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
307 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
310 IRMemoryMap::AllocationPolicy policy =
311 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
312 : IRMemoryMap::eAllocationPolicyMirror;
314 const bool zero_memory = false;
316 m_materialized_address = m_execution_unit_sp->Malloc(
317 m_materializer_ap->GetStructByteSize(),
318 m_materializer_ap->GetStructAlignment(),
319 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
320 zero_memory, alloc_error);
322 if (!alloc_error.Success()) {
323 diagnostic_manager.Printf(
324 eDiagnosticSeverityError,
325 "Couldn't allocate space for materialized struct: %s",
326 alloc_error.AsCString());
331 struct_address = m_materialized_address;
333 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
336 const size_t stack_frame_size = 512 * 1024;
338 const bool zero_memory = false;
340 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
342 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
343 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
345 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
347 if (!alloc_error.Success()) {
348 diagnostic_manager.Printf(
349 eDiagnosticSeverityError,
350 "Couldn't allocate space for the stack frame: %s",
351 alloc_error.AsCString());
356 Status materialize_error;
358 m_dematerializer_sp = m_materializer_ap->Materialize(
359 frame, *m_execution_unit_sp, struct_address, materialize_error);
361 if (!materialize_error.Success()) {
362 diagnostic_manager.Printf(eDiagnosticSeverityError,
363 "Couldn't materialize: %s",
364 materialize_error.AsCString());
371 lldb::ModuleSP LLVMUserExpression::GetJITModule() {
372 if (m_execution_unit_sp)
373 return m_execution_unit_sp->GetJITModule();
374 return lldb::ModuleSP();