blob: 6a9fd9ec8a1ff1e5a07680d684a036640b231c3a [file] [log] [blame]
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001//===-- LLVMUserExpression.cpp ----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Ryan Brown998c8a1c12015-11-02 19:30:40 +00006//
7//===----------------------------------------------------------------------===//
8
Ryan Brown998c8a1c12015-11-02 19:30:40 +00009
Ryan Brown998c8a1c12015-11-02 19:30:40 +000010#include "lldb/Expression/LLVMUserExpression.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000011#include "lldb/Core/Module.h"
12#include "lldb/Core/StreamFile.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000013#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000014#include "lldb/Expression/DiagnosticManager.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000015#include "lldb/Expression/IRExecutionUnit.h"
16#include "lldb/Expression/IRInterpreter.h"
17#include "lldb/Expression/Materializer.h"
18#include "lldb/Host/HostInfo.h"
19#include "lldb/Symbol/Block.h"
20#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000021#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000022#include "lldb/Symbol/Function.h"
23#include "lldb/Symbol/ObjectFile.h"
24#include "lldb/Symbol/SymbolVendor.h"
25#include "lldb/Symbol/Type.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000026#include "lldb/Symbol/VariableList.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/StackFrame.h"
30#include "lldb/Target/Target.h"
31#include "lldb/Target/ThreadPlan.h"
32#include "lldb/Target/ThreadPlanCallUserExpression.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000033#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000034#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000035#include "lldb/Utility/StreamString.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000036
37using namespace lldb_private;
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
Zachary Turnerc5d7df92016-11-08 04:52:16 +000040 llvm::StringRef expr,
41 llvm::StringRef prefix,
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 lldb::LanguageType language,
43 ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +000044 const EvaluateExpressionOptions &options)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000045 : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000046 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
Jonas Devlieghered5b44032019-02-13 06:25:41 +000047 m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false),
48 m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(),
49 m_materializer_up(), m_jit_module_wp(), m_enforce_valid_object(true),
50 m_in_cplusplus_method(false), m_in_objectivec_method(false),
51 m_in_static_method(false), m_needs_object_ptr(false), m_target(NULL),
52 m_can_interpret(false), m_materialized_address(LLDB_INVALID_ADDRESS) {}
Ryan Brown998c8a1c12015-11-02 19:30:40 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054LLVMUserExpression::~LLVMUserExpression() {
55 if (m_target) {
56 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
57 if (jit_module_sp)
58 m_target->GetImages().Remove(jit_module_sp);
59 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000060}
61
62lldb::ExpressionResults
Kate Stoneb9c1b512016-09-06 20:57:50 +000063LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
64 ExecutionContext &exe_ctx,
65 const EvaluateExpressionOptions &options,
66 lldb::UserExpressionSP &shared_ptr_to_me,
67 lldb::ExpressionVariableSP &result) {
68 // The expression log is quite verbose, and if you're just tracking the
Adrian Prantl05097242018-04-30 16:49:04 +000069 // execution of the expression, it's quite convenient to have these logs come
70 // out with the STEP log as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
72 LIBLLDB_LOG_STEP));
Ryan Brown998c8a1c12015-11-02 19:30:40 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
75 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
78 struct_address)) {
79 diagnostic_manager.Printf(
80 eDiagnosticSeverityError,
81 "errored out in %s, couldn't PrepareToExecuteJITExpression",
82 __FUNCTION__);
83 return lldb::eExpressionSetupError;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085
86 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
87 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
88
89 if (m_can_interpret) {
90 llvm::Module *module = m_execution_unit_sp->GetModule();
91 llvm::Function *function = m_execution_unit_sp->GetFunction();
92
93 if (!module || !function) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000094 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 eDiagnosticSeverityError,
96 "supposed to interpret, but nothing is there");
Ryan Brown998c8a1c12015-11-02 19:30:40 +000097 return lldb::eExpressionSetupError;
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000099
Zachary Turner97206d52017-05-12 04:51:55 +0000100 Status interpreter_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 std::vector<lldb::addr_t> args;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
Sean Callanan579e70c2016-03-19 00:03:59 +0000105 diagnostic_manager.Printf(eDiagnosticSeverityError,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 "errored out in %s, couldn't AddArguments",
107 __FUNCTION__);
108 return lldb::eExpressionSetupError;
109 }
110
111 function_stack_bottom = m_stack_frame_bottom;
112 function_stack_top = m_stack_frame_top;
113
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000114 IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
115 interpreter_error, function_stack_bottom,
116 function_stack_top, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117
118 if (!interpreter_error.Success()) {
119 diagnostic_manager.Printf(eDiagnosticSeverityError,
120 "supposed to interpret, but failed: %s",
121 interpreter_error.AsCString());
122 return lldb::eExpressionDiscarded;
123 }
124 } else {
125 if (!exe_ctx.HasThreadScope()) {
126 diagnostic_manager.Printf(eDiagnosticSeverityError,
127 "%s called with no thread selected",
128 __FUNCTION__);
129 return lldb::eExpressionSetupError;
130 }
131
132 Address wrapper_address(m_jit_start_addr);
133
134 std::vector<lldb::addr_t> args;
135
136 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
137 diagnostic_manager.Printf(eDiagnosticSeverityError,
138 "errored out in %s, couldn't AddArguments",
139 __FUNCTION__);
140 return lldb::eExpressionSetupError;
141 }
142
143 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
144 exe_ctx.GetThreadRef(), wrapper_address, args, options,
145 shared_ptr_to_me));
146
147 StreamString ss;
148 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000149 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 return lldb::eExpressionSetupError;
151 }
152
153 ThreadPlanCallUserExpression *user_expression_plan =
154 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
155
156 lldb::addr_t function_stack_pointer =
157 user_expression_plan->GetFunctionStackPointer();
158
159 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
160 function_stack_top = function_stack_pointer;
161
162 if (log)
163 log->Printf(
164 "-- [UserExpression::Execute] Execution of expression begins --");
165
166 if (exe_ctx.GetProcessPtr())
167 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
168
169 lldb::ExpressionResults execution_result =
170 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
171 diagnostic_manager);
172
173 if (exe_ctx.GetProcessPtr())
174 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
175
176 if (log)
177 log->Printf("-- [UserExpression::Execute] Execution of expression "
178 "completed --");
179
180 if (execution_result == lldb::eExpressionInterrupted ||
181 execution_result == lldb::eExpressionHitBreakpoint) {
182 const char *error_desc = NULL;
183
184 if (call_plan_sp) {
185 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
186 if (real_stop_info_sp)
187 error_desc = real_stop_info_sp->GetDescription();
188 }
189 if (error_desc)
190 diagnostic_manager.Printf(eDiagnosticSeverityError,
191 "Execution was interrupted, reason: %s.",
192 error_desc);
193 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000194 diagnostic_manager.PutString(eDiagnosticSeverityError,
195 "Execution was interrupted.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196
197 if ((execution_result == lldb::eExpressionInterrupted &&
198 options.DoesUnwindOnError()) ||
199 (execution_result == lldb::eExpressionHitBreakpoint &&
200 options.DoesIgnoreBreakpoints()))
201 diagnostic_manager.AppendMessageToDiagnostic(
202 "The process has been returned to the state before expression "
203 "evaluation.");
204 else {
205 if (execution_result == lldb::eExpressionHitBreakpoint)
206 user_expression_plan->TransferExpressionOwnership();
207 diagnostic_manager.AppendMessageToDiagnostic(
208 "The process has been left at the point where it was "
209 "interrupted, "
210 "use \"thread return -x\" to return to the state before "
211 "expression evaluation.");
212 }
213
214 return execution_result;
215 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000216 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 eDiagnosticSeverityRemark,
218 "Execution was halted at the first instruction of the expression "
219 "function because \"debug\" was requested.\n"
220 "Use \"thread return -x\" to return to the state before expression "
221 "evaluation.");
222 return execution_result;
223 } else if (execution_result != lldb::eExpressionCompleted) {
224 diagnostic_manager.Printf(
225 eDiagnosticSeverityError,
226 "Couldn't execute function; result was %s",
227 Process::ExecutionResultAsCString(execution_result));
228 return execution_result;
229 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000230 }
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
233 function_stack_bottom, function_stack_top)) {
234 return lldb::eExpressionCompleted;
235 } else {
236 return lldb::eExpressionResultUnavailable;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000237 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000239 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 eDiagnosticSeverityError,
241 "Expression can't be run, because there is no JIT compiled function");
242 return lldb::eExpressionSetupError;
243 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246bool LLVMUserExpression::FinalizeJITExecution(
247 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
248 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
249 lldb::addr_t function_stack_top) {
250 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 if (log)
253 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
254 "after execution --");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 if (!m_dematerializer_sp) {
257 diagnostic_manager.Printf(eDiagnosticSeverityError,
258 "Couldn't apply expression side effects : no "
259 "dematerializer is present");
260 return false;
261 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000262
Zachary Turner97206d52017-05-12 04:51:55 +0000263 Status dematerialize_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
266 function_stack_top);
Jim Ingham2c381412015-11-04 20:32:27 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 if (!dematerialize_error.Success()) {
269 diagnostic_manager.Printf(eDiagnosticSeverityError,
270 "Couldn't apply expression side effects : %s",
271 dematerialize_error.AsCString("unknown error"));
272 return false;
273 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 result =
276 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (result)
279 result->TransferAddress();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 m_dematerializer_sp.reset();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 return true;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000284}
285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286bool LLVMUserExpression::PrepareToExecuteJITExpression(
287 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
288 lldb::addr_t &struct_address) {
289 lldb::TargetSP target;
290 lldb::ProcessSP process;
291 lldb::StackFrameSP frame;
292
293 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000294 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 eDiagnosticSeverityError,
296 "The context has changed before we could JIT the expression!");
297 return false;
298 }
299
300 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
301 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000302 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303
304 IRMemoryMap::AllocationPolicy policy =
305 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
306 : IRMemoryMap::eAllocationPolicyMirror;
307
308 const bool zero_memory = false;
309
310 m_materialized_address = m_execution_unit_sp->Malloc(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000311 m_materializer_up->GetStructByteSize(),
312 m_materializer_up->GetStructAlignment(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
314 zero_memory, alloc_error);
315
316 if (!alloc_error.Success()) {
317 diagnostic_manager.Printf(
318 eDiagnosticSeverityError,
319 "Couldn't allocate space for materialized struct: %s",
320 alloc_error.AsCString());
321 return false;
322 }
323 }
324
325 struct_address = m_materialized_address;
326
327 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000328 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329
330 const size_t stack_frame_size = 512 * 1024;
331
332 const bool zero_memory = false;
333
334 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
335 stack_frame_size, 8,
336 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
337 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
338
339 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
340
341 if (!alloc_error.Success()) {
342 diagnostic_manager.Printf(
343 eDiagnosticSeverityError,
344 "Couldn't allocate space for the stack frame: %s",
345 alloc_error.AsCString());
346 return false;
347 }
348 }
349
Zachary Turner97206d52017-05-12 04:51:55 +0000350 Status materialize_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000352 m_dematerializer_sp = m_materializer_up->Materialize(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 frame, *m_execution_unit_sp, struct_address, materialize_error);
354
355 if (!materialize_error.Success()) {
356 diagnostic_manager.Printf(eDiagnosticSeverityError,
357 "Couldn't materialize: %s",
358 materialize_error.AsCString());
359 return false;
360 }
361 }
362 return true;
363}
364
365lldb::ModuleSP LLVMUserExpression::GetJITModule() {
366 if (m_execution_unit_sp)
367 return m_execution_unit_sp->GetJITModule();
368 return lldb::ModuleSP();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000369}