blob: 3b6282435072854d3b2b6e147e26c1df69fe0a05 [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/ExpressionSourceCode.h"
16#include "lldb/Expression/IRExecutionUnit.h"
17#include "lldb/Expression/IRInterpreter.h"
18#include "lldb/Expression/Materializer.h"
19#include "lldb/Host/HostInfo.h"
20#include "lldb/Symbol/Block.h"
21#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000022#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000023#include "lldb/Symbol/Function.h"
24#include "lldb/Symbol/ObjectFile.h"
25#include "lldb/Symbol/SymbolVendor.h"
26#include "lldb/Symbol/Type.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000027#include "lldb/Symbol/VariableList.h"
28#include "lldb/Target/ExecutionContext.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/StackFrame.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/ThreadPlan.h"
33#include "lldb/Target/ThreadPlanCallUserExpression.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000034#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000035#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000036#include "lldb/Utility/StreamString.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000037
38using namespace lldb_private;
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
Zachary Turnerc5d7df92016-11-08 04:52:16 +000041 llvm::StringRef expr,
42 llvm::StringRef prefix,
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 lldb::LanguageType language,
44 ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +000045 const EvaluateExpressionOptions &options)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000046 : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
Ryan Brown998c8a1c12015-11-02 19:30:40 +000047 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
Bruce Mitcheneref4536c2017-03-23 09:52:26 +000048 m_stack_frame_top(LLDB_INVALID_ADDRESS),
Jason Molendaab2dae02017-03-21 02:59:15 +000049 m_allow_cxx(false),
50 m_allow_objc(false),
Bruce Mitcheneref4536c2017-03-23 09:52:26 +000051 m_transformed_text(),
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 m_execution_unit_sp(), m_materializer_ap(), m_jit_module_wp(),
53 m_enforce_valid_object(true), m_in_cplusplus_method(false),
54 m_in_objectivec_method(false), m_in_static_method(false),
55 m_needs_object_ptr(false), m_target(NULL), m_can_interpret(false),
56 m_materialized_address(LLDB_INVALID_ADDRESS) {}
Ryan Brown998c8a1c12015-11-02 19:30:40 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058LLVMUserExpression::~LLVMUserExpression() {
59 if (m_target) {
60 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
61 if (jit_module_sp)
62 m_target->GetImages().Remove(jit_module_sp);
63 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000064}
65
66lldb::ExpressionResults
Kate Stoneb9c1b512016-09-06 20:57:50 +000067LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
68 ExecutionContext &exe_ctx,
69 const EvaluateExpressionOptions &options,
70 lldb::UserExpressionSP &shared_ptr_to_me,
71 lldb::ExpressionVariableSP &result) {
72 // The expression log is quite verbose, and if you're just tracking the
Adrian Prantl05097242018-04-30 16:49:04 +000073 // execution of the expression, it's quite convenient to have these logs come
74 // out with the STEP log as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
76 LIBLLDB_LOG_STEP));
Ryan Brown998c8a1c12015-11-02 19:30:40 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
79 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
82 struct_address)) {
83 diagnostic_manager.Printf(
84 eDiagnosticSeverityError,
85 "errored out in %s, couldn't PrepareToExecuteJITExpression",
86 __FUNCTION__);
87 return lldb::eExpressionSetupError;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000088 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000089
90 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
91 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
92
93 if (m_can_interpret) {
94 llvm::Module *module = m_execution_unit_sp->GetModule();
95 llvm::Function *function = m_execution_unit_sp->GetFunction();
96
97 if (!module || !function) {
Zachary Turnere2411fa2016-11-12 19:12:56 +000098 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 eDiagnosticSeverityError,
100 "supposed to interpret, but nothing is there");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000101 return lldb::eExpressionSetupError;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000103
Zachary Turner97206d52017-05-12 04:51:55 +0000104 Status interpreter_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 std::vector<lldb::addr_t> args;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
Sean Callanan579e70c2016-03-19 00:03:59 +0000109 diagnostic_manager.Printf(eDiagnosticSeverityError,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 "errored out in %s, couldn't AddArguments",
111 __FUNCTION__);
112 return lldb::eExpressionSetupError;
113 }
114
115 function_stack_bottom = m_stack_frame_bottom;
116 function_stack_top = m_stack_frame_top;
117
118 IRInterpreter::Interpret(*module, *function, args,
119 *m_execution_unit_sp.get(), interpreter_error,
120 function_stack_bottom, function_stack_top,
121 exe_ctx);
122
123 if (!interpreter_error.Success()) {
124 diagnostic_manager.Printf(eDiagnosticSeverityError,
125 "supposed to interpret, but failed: %s",
126 interpreter_error.AsCString());
127 return lldb::eExpressionDiscarded;
128 }
129 } else {
130 if (!exe_ctx.HasThreadScope()) {
131 diagnostic_manager.Printf(eDiagnosticSeverityError,
132 "%s called with no thread selected",
133 __FUNCTION__);
134 return lldb::eExpressionSetupError;
135 }
136
137 Address wrapper_address(m_jit_start_addr);
138
139 std::vector<lldb::addr_t> args;
140
141 if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
142 diagnostic_manager.Printf(eDiagnosticSeverityError,
143 "errored out in %s, couldn't AddArguments",
144 __FUNCTION__);
145 return lldb::eExpressionSetupError;
146 }
147
148 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
149 exe_ctx.GetThreadRef(), wrapper_address, args, options,
150 shared_ptr_to_me));
151
152 StreamString ss;
153 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
Zachary Turnerc1564272016-11-16 21:15:24 +0000154 diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 return lldb::eExpressionSetupError;
156 }
157
158 ThreadPlanCallUserExpression *user_expression_plan =
159 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
160
161 lldb::addr_t function_stack_pointer =
162 user_expression_plan->GetFunctionStackPointer();
163
164 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
165 function_stack_top = function_stack_pointer;
166
167 if (log)
168 log->Printf(
169 "-- [UserExpression::Execute] Execution of expression begins --");
170
171 if (exe_ctx.GetProcessPtr())
172 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
173
174 lldb::ExpressionResults execution_result =
175 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
176 diagnostic_manager);
177
178 if (exe_ctx.GetProcessPtr())
179 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
180
181 if (log)
182 log->Printf("-- [UserExpression::Execute] Execution of expression "
183 "completed --");
184
185 if (execution_result == lldb::eExpressionInterrupted ||
186 execution_result == lldb::eExpressionHitBreakpoint) {
187 const char *error_desc = NULL;
188
189 if (call_plan_sp) {
190 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
191 if (real_stop_info_sp)
192 error_desc = real_stop_info_sp->GetDescription();
193 }
194 if (error_desc)
195 diagnostic_manager.Printf(eDiagnosticSeverityError,
196 "Execution was interrupted, reason: %s.",
197 error_desc);
198 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000199 diagnostic_manager.PutString(eDiagnosticSeverityError,
200 "Execution was interrupted.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201
202 if ((execution_result == lldb::eExpressionInterrupted &&
203 options.DoesUnwindOnError()) ||
204 (execution_result == lldb::eExpressionHitBreakpoint &&
205 options.DoesIgnoreBreakpoints()))
206 diagnostic_manager.AppendMessageToDiagnostic(
207 "The process has been returned to the state before expression "
208 "evaluation.");
209 else {
210 if (execution_result == lldb::eExpressionHitBreakpoint)
211 user_expression_plan->TransferExpressionOwnership();
212 diagnostic_manager.AppendMessageToDiagnostic(
213 "The process has been left at the point where it was "
214 "interrupted, "
215 "use \"thread return -x\" to return to the state before "
216 "expression evaluation.");
217 }
218
219 return execution_result;
220 } else if (execution_result == lldb::eExpressionStoppedForDebug) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000221 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 eDiagnosticSeverityRemark,
223 "Execution was halted at the first instruction of the expression "
224 "function because \"debug\" was requested.\n"
225 "Use \"thread return -x\" to return to the state before expression "
226 "evaluation.");
227 return execution_result;
228 } else if (execution_result != lldb::eExpressionCompleted) {
229 diagnostic_manager.Printf(
230 eDiagnosticSeverityError,
231 "Couldn't execute function; result was %s",
232 Process::ExecutionResultAsCString(execution_result));
233 return execution_result;
234 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000235 }
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
238 function_stack_bottom, function_stack_top)) {
239 return lldb::eExpressionCompleted;
240 } else {
241 return lldb::eExpressionResultUnavailable;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000242 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000244 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 eDiagnosticSeverityError,
246 "Expression can't be run, because there is no JIT compiled function");
247 return lldb::eExpressionSetupError;
248 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000249}
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251bool LLVMUserExpression::FinalizeJITExecution(
252 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
253 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
254 lldb::addr_t function_stack_top) {
255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 if (log)
258 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
259 "after execution --");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 if (!m_dematerializer_sp) {
262 diagnostic_manager.Printf(eDiagnosticSeverityError,
263 "Couldn't apply expression side effects : no "
264 "dematerializer is present");
265 return false;
266 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000267
Zachary Turner97206d52017-05-12 04:51:55 +0000268 Status dematerialize_error;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
271 function_stack_top);
Jim Ingham2c381412015-11-04 20:32:27 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (!dematerialize_error.Success()) {
274 diagnostic_manager.Printf(eDiagnosticSeverityError,
275 "Couldn't apply expression side effects : %s",
276 dematerialize_error.AsCString("unknown error"));
277 return false;
278 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 result =
281 GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 if (result)
284 result->TransferAddress();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 m_dematerializer_sp.reset();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 return true;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000289}
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291bool LLVMUserExpression::PrepareToExecuteJITExpression(
292 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
293 lldb::addr_t &struct_address) {
294 lldb::TargetSP target;
295 lldb::ProcessSP process;
296 lldb::StackFrameSP frame;
297
298 if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000299 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 eDiagnosticSeverityError,
301 "The context has changed before we could JIT the expression!");
302 return false;
303 }
304
305 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
306 if (m_materialized_address == LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000307 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308
309 IRMemoryMap::AllocationPolicy policy =
310 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
311 : IRMemoryMap::eAllocationPolicyMirror;
312
313 const bool zero_memory = false;
314
315 m_materialized_address = m_execution_unit_sp->Malloc(
316 m_materializer_ap->GetStructByteSize(),
317 m_materializer_ap->GetStructAlignment(),
318 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
319 zero_memory, alloc_error);
320
321 if (!alloc_error.Success()) {
322 diagnostic_manager.Printf(
323 eDiagnosticSeverityError,
324 "Couldn't allocate space for materialized struct: %s",
325 alloc_error.AsCString());
326 return false;
327 }
328 }
329
330 struct_address = m_materialized_address;
331
332 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +0000333 Status alloc_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334
335 const size_t stack_frame_size = 512 * 1024;
336
337 const bool zero_memory = false;
338
339 m_stack_frame_bottom = m_execution_unit_sp->Malloc(
340 stack_frame_size, 8,
341 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
342 IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
343
344 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
345
346 if (!alloc_error.Success()) {
347 diagnostic_manager.Printf(
348 eDiagnosticSeverityError,
349 "Couldn't allocate space for the stack frame: %s",
350 alloc_error.AsCString());
351 return false;
352 }
353 }
354
Zachary Turner97206d52017-05-12 04:51:55 +0000355 Status materialize_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356
357 m_dematerializer_sp = m_materializer_ap->Materialize(
358 frame, *m_execution_unit_sp, struct_address, materialize_error);
359
360 if (!materialize_error.Success()) {
361 diagnostic_manager.Printf(eDiagnosticSeverityError,
362 "Couldn't materialize: %s",
363 materialize_error.AsCString());
364 return false;
365 }
366 }
367 return true;
368}
369
370lldb::ModuleSP LLVMUserExpression::GetJITModule() {
371 if (m_execution_unit_sp)
372 return m_execution_unit_sp->GetJITModule();
373 return lldb::ModuleSP();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000374}