blob: cd28834a9621ad2c01eaa98ff66a0e64177d9eba [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"
15#include "lldb/Core/ConstString.h"
16#include "lldb/Core/Log.h"
17#include "lldb/Core/Module.h"
18#include "lldb/Core/StreamFile.h"
19#include "lldb/Core/StreamString.h"
20#include "lldb/Core/ValueObjectConstResult.h"
21#include "lldb/Expression/ExpressionSourceCode.h"
22#include "lldb/Expression/IRExecutionUnit.h"
23#include "lldb/Expression/IRInterpreter.h"
24#include "lldb/Expression/Materializer.h"
25#include "lldb/Host/HostInfo.h"
26#include "lldb/Symbol/Block.h"
27#include "lldb/Symbol/ClangASTContext.h"
28#include "lldb/Symbol/Function.h"
29#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Symbol/SymbolVendor.h"
31#include "lldb/Symbol/Type.h"
32#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
33#include "lldb/Symbol/VariableList.h"
34#include "lldb/Target/ExecutionContext.h"
35#include "lldb/Target/Process.h"
36#include "lldb/Target/StackFrame.h"
37#include "lldb/Target/Target.h"
38#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
40
41using namespace lldb_private;
42
43LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope, const char *expr, const char *expr_prefix,
44 lldb::LanguageType language, ResultType desired_type)
45 : UserExpression(exe_scope, expr, expr_prefix, language, desired_type),
46 m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
47 m_stack_frame_top(LLDB_INVALID_ADDRESS),
48 m_transformed_text(),
49 m_execution_unit_sp(),
50 m_materializer_ap(),
51 m_jit_module_wp(),
52 m_enforce_valid_object(true),
53 m_in_cplusplus_method(false),
54 m_in_objectivec_method(false),
55 m_in_static_method(false),
56 m_needs_object_ptr(false),
57 m_const_object(false),
58 m_target(NULL),
59 m_can_interpret(false),
60 m_materialized_address(LLDB_INVALID_ADDRESS)
61{
62}
63
64LLVMUserExpression::~LLVMUserExpression()
65{
66 if (m_target)
67 {
68 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
69 if (jit_module_sp)
70 m_target->GetImages().Remove(jit_module_sp);
71 }
72}
73
74lldb::ExpressionResults
75LLVMUserExpression::Execute(Stream &error_stream, ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options,
76 lldb::UserExpressionSP &shared_ptr_to_me, lldb::ExpressionVariableSP &result)
77{
78 // The expression log is quite verbose, and if you're just tracking the execution of the
79 // expression, it's quite convenient to have these logs come out with the STEP log as well.
80 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
81
82 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
83 {
84 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
85
86 if (!PrepareToExecuteJITExpression(error_stream, exe_ctx, struct_address))
87 {
88 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
89 return lldb::eExpressionSetupError;
90 }
91
92 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
93 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
94
95 if (m_can_interpret)
96 {
97 llvm::Module *module = m_execution_unit_sp->GetModule();
98 llvm::Function *function = m_execution_unit_sp->GetFunction();
99
100 if (!module || !function)
101 {
102 error_stream.Printf("Supposed to interpret, but nothing is there");
103 return lldb::eExpressionSetupError;
104 }
105
106 Error interpreter_error;
107
108 std::vector<lldb::addr_t> args;
109
110 if (!AddInitialArguments(exe_ctx, args, error_stream))
111 {
112 error_stream.Printf("Errored out in %s, couldn't AddInitialArguments", __FUNCTION__);
113 return lldb::eExpressionSetupError;
114 }
115
116 args.push_back(struct_address);
117
118 function_stack_bottom = m_stack_frame_bottom;
119 function_stack_top = m_stack_frame_top;
120
121 IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp.get(), interpreter_error,
122 function_stack_bottom, function_stack_top, exe_ctx);
123
124 if (!interpreter_error.Success())
125 {
126 error_stream.Printf("Supposed to interpret, but failed: %s", interpreter_error.AsCString());
127 return lldb::eExpressionDiscarded;
128 }
129 }
130 else
131 {
132 if (!exe_ctx.HasThreadScope())
133 {
134 error_stream.Printf("UserExpression::Execute called with no thread selected.");
135 return lldb::eExpressionSetupError;
136 }
137
138 Address wrapper_address(m_jit_start_addr);
139
140 std::vector<lldb::addr_t> args;
141
142 if (!AddInitialArguments(exe_ctx, args, error_stream))
143 {
144 error_stream.Printf("Errored out in %s, couldn't AddInitialArguments", __FUNCTION__);
145 return lldb::eExpressionSetupError;
146 }
147
148 args.push_back(struct_address);
149
150 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(exe_ctx.GetThreadRef(), wrapper_address,
151 args, options, shared_ptr_to_me));
152
153 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&error_stream))
154 return lldb::eExpressionSetupError;
155
156 ThreadPlanCallUserExpression *user_expression_plan =
157 static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
158
159 lldb::addr_t function_stack_pointer = user_expression_plan->GetFunctionStackPointer();
160
161 function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
162 function_stack_top = function_stack_pointer;
163
164 if (log)
165 log->Printf("-- [UserExpression::Execute] Execution of expression begins --");
166
167 if (exe_ctx.GetProcessPtr())
168 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
169
170 lldb::ExpressionResults execution_result =
171 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, error_stream);
172
173 if (exe_ctx.GetProcessPtr())
174 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
175
176 if (log)
177 log->Printf("-- [UserExpression::Execute] Execution of expression completed --");
178
179 if (execution_result == lldb::eExpressionInterrupted || execution_result == lldb::eExpressionHitBreakpoint)
180 {
181 const char *error_desc = NULL;
182
183 if (call_plan_sp)
184 {
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 error_stream.Printf("Execution was interrupted, reason: %s.", error_desc);
191 else
192 error_stream.PutCString("Execution was interrupted.");
193
194 if ((execution_result == lldb::eExpressionInterrupted && options.DoesUnwindOnError()) ||
195 (execution_result == lldb::eExpressionHitBreakpoint && options.DoesIgnoreBreakpoints()))
196 error_stream.PutCString(
197 "\nThe process has been returned to the state before expression evaluation.");
198 else
199 {
200 if (execution_result == lldb::eExpressionHitBreakpoint)
201 user_expression_plan->TransferExpressionOwnership();
202 error_stream.PutCString(
203 "\nThe process has been left at the point where it was interrupted, "
204 "use \"thread return -x\" to return to the state before expression evaluation.");
205 }
206
207 return execution_result;
208 }
209 else if (execution_result == lldb::eExpressionStoppedForDebug)
210 {
211 error_stream.PutCString(
212 "Execution was halted at the first instruction of the expression "
213 "function because \"debug\" was requested.\n"
214 "Use \"thread return -x\" to return to the state before expression evaluation.");
215 return execution_result;
216 }
217 else if (execution_result != lldb::eExpressionCompleted)
218 {
219 error_stream.Printf("Couldn't execute function; result was %s\n",
220 Process::ExecutionResultAsCString(execution_result));
221 return execution_result;
222 }
223 }
224
225 if (FinalizeJITExecution(error_stream, exe_ctx, result, function_stack_bottom, function_stack_top))
226 {
227 return lldb::eExpressionCompleted;
228 }
229 else
230 {
231 return lldb::eExpressionResultUnavailable;
232 }
233 }
234 else
235 {
236 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
237 return lldb::eExpressionSetupError;
238 }
239}
240
241bool
242LLVMUserExpression::FinalizeJITExecution(Stream &error_stream, ExecutionContext &exe_ctx,
243 lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
244 lldb::addr_t function_stack_top)
245{
246 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
247
248 if (log)
249 log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing after execution --");
250
251 if (!m_dematerializer_sp)
252 {
253 error_stream.Printf("Couldn't apply expression side effects : no dematerializer is present");
254 return false;
255 }
256
257 Error dematerialize_error;
258
259 m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom, function_stack_top);
260
261 if (!dematerialize_error.Success())
262 {
263 error_stream.Printf("Couldn't apply expression side effects : %s\n",
264 dematerialize_error.AsCString("unknown error"));
265 return false;
266 }
267
268 result = GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
269
270 if (result)
271 result->TransferAddress();
272
273 m_dematerializer_sp.reset();
274
275 return true;
276}
277
278bool
279LLVMUserExpression::PrepareToExecuteJITExpression(Stream &error_stream, ExecutionContext &exe_ctx,
280 lldb::addr_t &struct_address)
281{
282 lldb::TargetSP target;
283 lldb::ProcessSP process;
284 lldb::StackFrameSP frame;
285
286 if (!LockAndCheckContext(exe_ctx, target, process, frame))
287 {
288 error_stream.Printf("The context has changed before we could JIT the expression!\n");
289 return false;
290 }
291
292 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
293 {
294 if (m_materialized_address == LLDB_INVALID_ADDRESS)
295 {
296 Error alloc_error;
297
298 IRMemoryMap::AllocationPolicy policy =
299 m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly : IRMemoryMap::eAllocationPolicyMirror;
300
301 m_materialized_address = m_execution_unit_sp->Malloc(
302 m_materializer_ap->GetStructByteSize(), m_materializer_ap->GetStructAlignment(),
303 lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy, alloc_error);
304
305 if (!alloc_error.Success())
306 {
307 error_stream.Printf("Couldn't allocate space for materialized struct: %s\n", alloc_error.AsCString());
308 return false;
309 }
310 }
311
312 struct_address = m_materialized_address;
313
314 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS)
315 {
316 Error alloc_error;
317
318 const size_t stack_frame_size = 512 * 1024;
319
320 m_stack_frame_bottom = m_execution_unit_sp->Malloc(stack_frame_size, 8,
321 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
322 IRMemoryMap::eAllocationPolicyHostOnly, alloc_error);
323
324 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
325
326 if (!alloc_error.Success())
327 {
328 error_stream.Printf("Couldn't allocate space for the stack frame: %s\n", alloc_error.AsCString());
329 return false;
330 }
331 }
332
333 Error materialize_error;
334
335 m_dematerializer_sp =
336 m_materializer_ap->Materialize(frame, *m_execution_unit_sp, struct_address, materialize_error);
337
338 if (!materialize_error.Success())
339 {
340 error_stream.Printf("Couldn't materialize: %s\n", materialize_error.AsCString());
341 return false;
342 }
343 }
344 return true;
345}
346
347lldb::ModuleSP
348LLVMUserExpression::GetJITModule()
349{
350 if (m_execution_unit_sp)
351 return m_execution_unit_sp->GetJITModule();
352 return lldb::ModuleSP();
353}