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/Module.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Expression/DiagnosticManager.h"
19 #include "lldb/Expression/ExpressionSourceCode.h"
20 #include "lldb/Expression/IRExecutionUnit.h"
21 #include "lldb/Expression/IRInterpreter.h"
22 #include "lldb/Expression/Materializer.h"
23 #include "lldb/Host/HostInfo.h"
24 #include "lldb/Symbol/Block.h"
25 #include "lldb/Symbol/ClangASTContext.h"
26 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
27 #include "lldb/Symbol/Function.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Symbol/SymbolVendor.h"
30 #include "lldb/Symbol/Type.h"
31 #include "lldb/Symbol/VariableList.h"
32 #include "lldb/Target/ExecutionContext.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/StackFrame.h"
35 #include "lldb/Target/Target.h"
36 #include "lldb/Target/ThreadPlan.h"
37 #include "lldb/Target/ThreadPlanCallUserExpression.h"
38 #include "lldb/Utility/ConstString.h"
39 #include "lldb/Utility/Log.h"
40 #include "lldb/Utility/StreamString.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),
56 m_execution_unit_sp(), m_materializer_ap(), m_jit_module_wp(),
57 m_enforce_valid_object(true), m_in_cplusplus_method(false),
58 m_in_objectivec_method(false), m_in_static_method(false),
59 m_needs_object_ptr(false), m_target(NULL), m_can_interpret(false),
60 m_materialized_address(LLDB_INVALID_ADDRESS) {}
62 LLVMUserExpression::~LLVMUserExpression() {
64 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
66 m_target->GetImages().Remove(jit_module_sp);
70 lldb::ExpressionResults
71 LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
72 ExecutionContext &exe_ctx,
73 const EvaluateExpressionOptions &options,
74 lldb::UserExpressionSP &shared_ptr_to_me,
75 lldb::ExpressionVariableSP &result) {
76 // The expression log is quite verbose, and if you're just tracking the
78 // expression, it's quite convenient to have these logs come out with the STEP
80 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
83 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
84 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
86 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
88 diagnostic_manager.Printf(
89 eDiagnosticSeverityError,
90 "errored out in %s, couldn't PrepareToExecuteJITExpression",
92 return lldb::eExpressionSetupError;
95 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
96 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
98 if (m_can_interpret) {
99 llvm::Module *module = m_execution_unit_sp->GetModule();
100 llvm::Function *function = m_execution_unit_sp->GetFunction();
102 if (!module || !function) {
103 diagnostic_manager.PutString(
104 eDiagnosticSeverityError,
105 "supposed to interpret, but nothing is there");
106 return lldb::eExpressionSetupError;
109 Status interpreter_error;
111 std::vector<lldb::addr_t> args;
113 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
114 diagnostic_manager.Printf(eDiagnosticSeverityError,
115 "errored out in %s, couldn't AddArguments",
117 return lldb::eExpressionSetupError;
120 function_stack_bottom = m_stack_frame_bottom;
121 function_stack_top = m_stack_frame_top;
123 IRInterpreter::Interpret(*module, *function, args,
124 *m_execution_unit_sp.get(), interpreter_error,
125 function_stack_bottom, function_stack_top,
128 if (!interpreter_error.Success()) {
129 diagnostic_manager.Printf(eDiagnosticSeverityError,
130 "supposed to interpret, but failed: %s",
131 interpreter_error.AsCString());
132 return lldb::eExpressionDiscarded;
135 if (!exe_ctx.HasThreadScope()) {
136 diagnostic_manager.Printf(eDiagnosticSeverityError,
137 "%s called with no thread selected",
139 return lldb::eExpressionSetupError;
142 Address wrapper_address(m_jit_start_addr);
144 std::vector<lldb::addr_t> args;
146 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
147 diagnostic_manager.Printf(eDiagnosticSeverityError,
148 "errored out in %s, couldn't AddArguments",
150 return lldb::eExpressionSetupError;
153 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
154 exe_ctx.GetThreadRef(), wrapper_address, args, options,
158 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
159 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
160 return lldb::eExpressionSetupError;
163 ThreadPlanCallUserExpression *user_expression_plan =
164 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
166 lldb::addr_t function_stack_pointer =
167 user_expression_plan->GetFunctionStackPointer();
169 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
170 function_stack_top = function_stack_pointer;
174 "-- [UserExpression::Execute] Execution of expression begins --");
176 if (exe_ctx.GetProcessPtr())
177 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
179 lldb::ExpressionResults execution_result =
180 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
183 if (exe_ctx.GetProcessPtr())
184 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
187 log->Printf("-- [UserExpression::Execute] Execution of expression "
190 if (execution_result == lldb::eExpressionInterrupted ||
191 execution_result == lldb::eExpressionHitBreakpoint) {
192 const char *error_desc = NULL;
195 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
196 if (real_stop_info_sp)
197 error_desc = real_stop_info_sp->GetDescription();
200 diagnostic_manager.Printf(eDiagnosticSeverityError,
201 "Execution was interrupted, reason: %s.",
204 diagnostic_manager.PutString(eDiagnosticSeverityError,
205 "Execution was interrupted.");
207 if ((execution_result == lldb::eExpressionInterrupted &&
208 options.DoesUnwindOnError()) ||
209 (execution_result == lldb::eExpressionHitBreakpoint &&
210 options.DoesIgnoreBreakpoints()))
211 diagnostic_manager.AppendMessageToDiagnostic(
212 "The process has been returned to the state before expression "
215 if (execution_result == lldb::eExpressionHitBreakpoint)
216 user_expression_plan->TransferExpressionOwnership();
217 diagnostic_manager.AppendMessageToDiagnostic(
218 "The process has been left at the point where it was "
220 "use \"thread return -x\" to return to the state before "
221 "expression evaluation.");
224 return execution_result;
225 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
226 diagnostic_manager.PutString(
227 eDiagnosticSeverityRemark,
228 "Execution was halted at the first instruction of the expression "
229 "function because \"debug\" was requested.\n"
230 "Use \"thread return -x\" to return to the state before expression "
232 return execution_result;
233 } else if (execution_result != lldb::eExpressionCompleted) {
234 diagnostic_manager.Printf(
235 eDiagnosticSeverityError,
236 "Couldn't execute function; result was %s",
237 Process::ExecutionResultAsCString(execution_result));
238 return execution_result;
242 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
243 function_stack_bottom, function_stack_top)) {
244 return lldb::eExpressionCompleted;
246 return lldb::eExpressionResultUnavailable;
249 diagnostic_manager.PutString(
250 eDiagnosticSeverityError,
251 "Expression can't be run, because there is no JIT compiled function");
252 return lldb::eExpressionSetupError;
256 bool LLVMUserExpression::FinalizeJITExecution(
257 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
258 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
259 lldb::addr_t function_stack_top) {
260 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
263 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
264 "after execution --");
266 if (!m_dematerializer_sp) {
267 diagnostic_manager.Printf(eDiagnosticSeverityError,
268 "Couldn't apply expression side effects : no "
269 "dematerializer is present");
273 Status dematerialize_error;
275 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
278 if (!dematerialize_error.Success()) {
279 diagnostic_manager.Printf(eDiagnosticSeverityError,
280 "Couldn't apply expression side effects : %s",
281 dematerialize_error.AsCString("unknown error"));
286 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
289 result->TransferAddress();
291 m_dematerializer_sp.reset();
296 bool LLVMUserExpression::PrepareToExecuteJITExpression(
297 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
298 lldb::addr_t &struct_address) {
299 lldb::TargetSP target;
300 lldb::ProcessSP process;
301 lldb::StackFrameSP frame;
303 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
304 diagnostic_manager.PutString(
305 eDiagnosticSeverityError,
306 "The context has changed before we could JIT the expression!");
310 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
311 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
314 IRMemoryMap::AllocationPolicy policy =
315 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
316 : IRMemoryMap::eAllocationPolicyMirror;
318 const bool zero_memory = false;
320 m_materialized_address = m_execution_unit_sp->Malloc(
321 m_materializer_ap->GetStructByteSize(),
322 m_materializer_ap->GetStructAlignment(),
323 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
324 zero_memory, alloc_error);
326 if (!alloc_error.Success()) {
327 diagnostic_manager.Printf(
328 eDiagnosticSeverityError,
329 "Couldn't allocate space for materialized struct: %s",
330 alloc_error.AsCString());
335 struct_address = m_materialized_address;
337 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
340 const size_t stack_frame_size = 512 * 1024;
342 const bool zero_memory = false;
344 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
346 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
347 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
349 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
351 if (!alloc_error.Success()) {
352 diagnostic_manager.Printf(
353 eDiagnosticSeverityError,
354 "Couldn't allocate space for the stack frame: %s",
355 alloc_error.AsCString());
360 Status materialize_error;
362 m_dematerializer_sp = m_materializer_ap->Materialize(
363 frame, *m_execution_unit_sp, struct_address, materialize_error);
365 if (!materialize_error.Success()) {
366 diagnostic_manager.Printf(eDiagnosticSeverityError,
367 "Couldn't materialize: %s",
368 materialize_error.AsCString());
375 lldb::ModuleSP LLVMUserExpression::GetJITModule() {
376 if (m_execution_unit_sp)
377 return m_execution_unit_sp->GetJITModule();
378 return lldb::ModuleSP();