blob: 396a7e295033f9fe9592b8e85d65f9e7d1979f37 [file] [log] [blame]
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001//===-- LLVMUserExpression.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11// C++ Includes
12
13// Project includes
14#include "lldb/Expression/LLVMUserExpression.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/StreamFile.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000017#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000018#include "lldb/Expression/DiagnosticManager.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000019#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"
Sean Callanan579e70c2016-03-19 00:03:59 +000026#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000027#include "lldb/Symbol/Function.h"
28#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Symbol/SymbolVendor.h"
30#include "lldb/Symbol/Type.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000031#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"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000038#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000039#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000040#include "lldb/Utility/StreamString.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000041
42using namespace lldb_private;
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
Zachary Turnerc5d7df92016-11-08 04:52:16 +000045 llvm::StringRef expr,
46 llvm::StringRef prefix,
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 lldb::LanguageType language,
48 ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +000049 const EvaluateExpressionOptions &options)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000050 : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000051 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
Bruce Mitcheneref4536c2017-03-23 09:52:26 +000052 m_stack_frame_top(LLDB_INVALID_ADDRESS),
Jason Molendaab2dae02017-03-21 02:59:15 +000053 m_allow_cxx(false),
54 m_allow_objc(false),
Bruce Mitcheneref4536c2017-03-23 09:52:26 +000055 m_transformed_text(),
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 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) {}
Ryan Brown998c8a1c12015-11-02 19:30:40 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062LLVMUserExpression::~LLVMUserExpression() {
63 if (m_target) {
64 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
65 if (jit_module_sp)
66 m_target->GetImages().Remove(jit_module_sp);
67 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000068}
69
70lldb::ExpressionResults
Kate Stoneb9c1b512016-09-06 20:57:50 +000071LLVMUserExpression::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
77 // execution of the
78 // expression, it's quite convenient to have these logs come out with the STEP
79 // log as well.
80 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
81 LIBLLDB_LOG_STEP));
Ryan Brown998c8a1c12015-11-02 19:30:40 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
84 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
87 struct_address)) {
88 diagnostic_manager.Printf(
89 eDiagnosticSeverityError,
90 "errored out in %s, couldn't PrepareToExecuteJITExpression",
91 __FUNCTION__);
92 return lldb::eExpressionSetupError;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094
95 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
96 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
97
98 if (m_can_interpret) {
99 llvm::Module *module = m_execution_unit_sp->GetModule();
100 llvm::Function *function = m_execution_unit_sp->GetFunction();
101
102 if (!module || !function) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000103 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 eDiagnosticSeverityError,
105 "supposed to interpret, but nothing is there");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000106 return lldb::eExpressionSetupError;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 Error interpreter_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 std::vector<lldb::addr_t> args;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
Sean Callanan579e70c2016-03-19 00:03:59 +0000114 diagnostic_manager.Printf(eDiagnosticSeverityError,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 "errored out in %s, couldn't AddArguments",
116 __FUNCTION__);
117 return lldb::eExpressionSetupError;
118 }
119
120 function_stack_bottom = m_stack_frame_bottom;
121 function_stack_top = m_stack_frame_top;
122
123 IRInterpreter::Interpret(*module, *function, args,
124 *m_execution_unit_sp.get(), interpreter_error,
125 function_stack_bottom, function_stack_top,
126 exe_ctx);
127
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;
133 }
134 } else {
135 if (!exe_ctx.HasThreadScope()) {
136 diagnostic_manager.Printf(eDiagnosticSeverityError,
137 "%s called with no thread selected",
138 __FUNCTION__);
139 return lldb::eExpressionSetupError;
140 }
141
142 Address wrapper_address(m_jit_start_addr);
143
144 std::vector<lldb::addr_t> args;
145
146 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
147 diagnostic_manager.Printf(eDiagnosticSeverityError,
148 "errored out in %s, couldn't AddArguments",
149 __FUNCTION__);
150 return lldb::eExpressionSetupError;
151 }
152
153 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
154 exe_ctx.GetThreadRef(), wrapper_address, args, options,
155 shared_ptr_to_me));
156
157 StreamString ss;
158 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000159 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 return lldb::eExpressionSetupError;
161 }
162
163 ThreadPlanCallUserExpression *user_expression_plan =
164 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
165
166 lldb::addr_t function_stack_pointer =
167 user_expression_plan->GetFunctionStackPointer();
168
169 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
170 function_stack_top = function_stack_pointer;
171
172 if (log)
173 log->Printf(
174 "-- [UserExpression::Execute] Execution of expression begins --");
175
176 if (exe_ctx.GetProcessPtr())
177 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
178
179 lldb::ExpressionResults execution_result =
180 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
181 diagnostic_manager);
182
183 if (exe_ctx.GetProcessPtr())
184 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
185
186 if (log)
187 log->Printf("-- [UserExpression::Execute] Execution of expression "
188 "completed --");
189
190 if (execution_result == lldb::eExpressionInterrupted ||
191 execution_result == lldb::eExpressionHitBreakpoint) {
192 const char *error_desc = NULL;
193
194 if (call_plan_sp) {
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();
198 }
199 if (error_desc)
200 diagnostic_manager.Printf(eDiagnosticSeverityError,
201 "Execution was interrupted, reason: %s.",
202 error_desc);
203 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000204 diagnostic_manager.PutString(eDiagnosticSeverityError,
205 "Execution was interrupted.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206
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 "
213 "evaluation.");
214 else {
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 "
219 "interrupted, "
220 "use \"thread return -x\" to return to the state before "
221 "expression evaluation.");
222 }
223
224 return execution_result;
225 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000226 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 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 "
231 "evaluation.");
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;
239 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000240 }
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
243 function_stack_bottom, function_stack_top)) {
244 return lldb::eExpressionCompleted;
245 } else {
246 return lldb::eExpressionResultUnavailable;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000247 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000249 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 eDiagnosticSeverityError,
251 "Expression can't be run, because there is no JIT compiled function");
252 return lldb::eExpressionSetupError;
253 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256bool 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));
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 if (log)
263 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
264 "after execution --");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 if (!m_dematerializer_sp) {
267 diagnostic_manager.Printf(eDiagnosticSeverityError,
268 "Couldn't apply expression side effects : no "
269 "dematerializer is present");
270 return false;
271 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 Error dematerialize_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
276 function_stack_top);
Jim Ingham2c381412015-11-04 20:32:27 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (!dematerialize_error.Success()) {
279 diagnostic_manager.Printf(eDiagnosticSeverityError,
280 "Couldn't apply expression side effects : %s",
281 dematerialize_error.AsCString("unknown error"));
282 return false;
283 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 result =
286 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 if (result)
289 result->TransferAddress();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 m_dematerializer_sp.reset();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 return true;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000294}
295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296bool 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;
302
303 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000304 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 eDiagnosticSeverityError,
306 "The context has changed before we could JIT the expression!");
307 return false;
308 }
309
310 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
311 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
312 Error alloc_error;
313
314 IRMemoryMap::AllocationPolicy policy =
315 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
316 : IRMemoryMap::eAllocationPolicyMirror;
317
318 const bool zero_memory = false;
319
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);
325
326 if (!alloc_error.Success()) {
327 diagnostic_manager.Printf(
328 eDiagnosticSeverityError,
329 "Couldn't allocate space for materialized struct: %s",
330 alloc_error.AsCString());
331 return false;
332 }
333 }
334
335 struct_address = m_materialized_address;
336
337 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
338 Error alloc_error;
339
340 const size_t stack_frame_size = 512 * 1024;
341
342 const bool zero_memory = false;
343
344 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
345 stack_frame_size, 8,
346 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
347 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
348
349 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
350
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());
356 return false;
357 }
358 }
359
360 Error materialize_error;
361
362 m_dematerializer_sp = m_materializer_ap->Materialize(
363 frame, *m_execution_unit_sp, struct_address, materialize_error);
364
365 if (!materialize_error.Success()) {
366 diagnostic_manager.Printf(eDiagnosticSeverityError,
367 "Couldn't materialize: %s",
368 materialize_error.AsCString());
369 return false;
370 }
371 }
372 return true;
373}
374
375lldb::ModuleSP LLVMUserExpression::GetJITModule() {
376 if (m_execution_unit_sp)
377 return m_execution_unit_sp->GetJITModule();
378 return lldb::ModuleSP();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000379}