blob: 993b7805c41da9326ce3422ea3656f4399d7a5a9 [file] [log] [blame]
Sean Callanan65dafa82010-08-27 01:01:44 +00001//===-- ClangUserExpression.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#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17#include <cstdlib>
18#include <string>
19#include <map>
20
21#include "lldb/Core/ConstString.h"
22#include "lldb/Core/Log.h"
23#include "lldb/Core/StreamString.h"
Greg Claytond1719722010-10-05 03:13:51 +000024#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000025#include "lldb/Expression/ClangExpressionDeclMap.h"
26#include "lldb/Expression/ClangExpressionParser.h"
27#include "lldb/Expression/ClangFunction.h"
28#include "lldb/Expression/ASTResultSynthesizer.h"
29#include "lldb/Expression/ClangUserExpression.h"
30#include "lldb/Host/Host.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000031#include "lldb/Symbol/VariableList.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000032#include "lldb/Target/ExecutionContext.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000033#include "lldb/Target/Process.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000034#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000035#include "lldb/Target/Target.h"
Jim Ingham360f53f2010-11-30 02:22:11 +000036#include "lldb/Target/ThreadPlan.h"
37#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000038
39using namespace lldb_private;
40
Sean Callanan77e93942010-10-29 00:29:03 +000041ClangUserExpression::ClangUserExpression (const char *expr,
42 const char *expr_prefix) :
Sean Callanan65dafa82010-08-27 01:01:44 +000043 m_expr_text(expr),
Johnny Chenb4c0f022010-10-29 20:19:44 +000044 m_expr_prefix(expr_prefix ? expr_prefix : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000045 m_transformed_text(),
46 m_jit_addr(LLDB_INVALID_ADDRESS),
47 m_cplusplus(false),
48 m_objectivec(false),
Sean Callanana91dd992010-11-19 02:52:21 +000049 m_needs_object_ptr(false),
50 m_desired_type(NULL, NULL)
Sean Callanan65dafa82010-08-27 01:01:44 +000051{
Sean Callanan65dafa82010-08-27 01:01:44 +000052}
53
Sean Callanan830a9032010-08-27 23:31:21 +000054ClangUserExpression::~ClangUserExpression ()
55{
56}
57
Sean Callanan65dafa82010-08-27 01:01:44 +000058clang::ASTConsumer *
59ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
60{
Sean Callanana91dd992010-11-19 02:52:21 +000061 return new ASTResultSynthesizer(passthrough,
62 m_desired_type);
Sean Callanan65dafa82010-08-27 01:01:44 +000063}
64
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000065void
66ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
67{
68 if (!exe_ctx.frame)
69 return;
70
71 VariableList *vars = exe_ctx.frame->GetVariableList(false);
72
73 if (!vars)
74 return;
75
76 if (vars->FindVariable(ConstString("this")).get())
77 m_cplusplus = true;
78 else if (vars->FindVariable(ConstString("self")).get())
79 m_objectivec = true;
80}
81
Sean Callanan550f2762010-10-22 23:25:16 +000082// This is a really nasty hack, meant to fix Objective-C expressions of the form
83// (int)[myArray count]. Right now, because the type information for count is
84// not available, [myArray count] returns id, which can't be directly cast to
85// int without causing a clang error.
86static void
87ApplyObjcCastHack(std::string &expr)
88{
89#define OBJC_CAST_HACK_FROM "(int)["
90#define OBJC_CAST_HACK_TO "(int)(long long)["
91
92 size_t from_offset;
93
94 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
95 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
96
97#undef OBJC_CAST_HACK_TO
98#undef OBJC_CAST_HACK_FROM
99}
100
Sean Callanan30892372010-10-24 20:45:49 +0000101// Another hack, meant to allow use of unichar despite it not being available in
102// the type information. Although we could special-case it in type lookup,
103// hopefully we'll figure out a way to #include the same environment as is
104// present in the original source file rather than try to hack specific type
105// definitions in as needed.
106static void
107ApplyUnicharHack(std::string &expr)
108{
109#define UNICHAR_HACK_FROM "unichar"
110#define UNICHAR_HACK_TO "unsigned short"
111
112 size_t from_offset;
113
114 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
115 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
116
117#undef UNICHAR_HACK_TO
118#undef UNICHAR_HACK_FROM
119}
120
Sean Callanan550f2762010-10-22 23:25:16 +0000121bool
Sean Callanana91dd992010-11-19 02:52:21 +0000122ClangUserExpression::Parse (Stream &error_stream,
123 ExecutionContext &exe_ctx,
124 TypeFromUser desired_type)
Sean Callanan65dafa82010-08-27 01:01:44 +0000125{
Greg Claytone005f2c2010-11-06 01:53:30 +0000126 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000127
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000128 ScanContext(exe_ctx);
129
130 StreamString m_transformed_stream;
131
132 ////////////////////////////////////
133 // Generate the expression
134 //
Sean Callanan550f2762010-10-22 23:25:16 +0000135
136 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000137 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000138
139 if (m_cplusplus)
140 {
Sean Callanan77e93942010-10-29 00:29:03 +0000141 m_transformed_stream.Printf("%s \n"
142 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000143 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000144 "$__lldb_class::%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000145 "{ \n"
146 " %s; \n"
147 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000148 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000149 FunctionName(),
150 m_expr_text.c_str());
151
152 m_needs_object_ptr = true;
153 }
154 else
155 {
Sean Callanan77e93942010-10-29 00:29:03 +0000156 m_transformed_stream.Printf("%s \n"
157 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000158 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000159 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000160 "{ \n"
161 " %s; \n"
162 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000163 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000164 FunctionName(),
165 m_expr_text.c_str());
166 }
167
168 m_transformed_text = m_transformed_stream.GetData();
169
170
171 if (log)
172 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
173
Sean Callanan65dafa82010-08-27 01:01:44 +0000174 ////////////////////////////////////
175 // Set up the target and compiler
176 //
177
178 Target *target = exe_ctx.target;
179
180 if (!target)
181 {
182 error_stream.PutCString ("error: invalid target\n");
183 return false;
184 }
185
186 ConstString target_triple;
187
188 target->GetTargetTriple (target_triple);
189
190 if (!target_triple)
191 target_triple = Host::GetTargetTriple ();
192
193 if (!target_triple)
194 {
195 error_stream.PutCString ("error: invalid target triple\n");
196 return false;
197 }
198
199 //////////////////////////
200 // Parse the expression
201 //
202
Sean Callanana91dd992010-11-19 02:52:21 +0000203 m_desired_type = desired_type;
204
Sean Callanan65dafa82010-08-27 01:01:44 +0000205 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
206
207 ClangExpressionParser parser(target_triple.GetCString(), *this);
208
209 unsigned num_errors = parser.Parse (error_stream);
210
211 if (num_errors)
212 {
213 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
214 return false;
215 }
216
217 ///////////////////////////////////////////////
218 // Convert the output of the parser to DWARF
219 //
220
221 m_dwarf_opcodes.reset(new StreamString);
222 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
223 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
224
225 m_local_variables.reset(new ClangExpressionVariableStore());
226
227 Error dwarf_error = parser.MakeDWARF ();
228
229 if (dwarf_error.Success())
230 {
231 if (log)
232 log->Printf("Code can be interpreted.");
233
234 return true;
235 }
236
237 //////////////////////////////////
238 // JIT the output of the parser
239 //
240
241 m_dwarf_opcodes.reset();
242
Sean Callanan830a9032010-08-27 23:31:21 +0000243 lldb::addr_t jit_end;
244
245 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000246
247 if (jit_error.Success())
248 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000249 return true;
250 }
251 else
252 {
253 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
254 return false;
255 }
256}
257
258bool
Jim Inghamd1686902010-10-14 23:45:03 +0000259ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000260 ExecutionContext &exe_ctx,
261 lldb::addr_t &struct_address,
262 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000263{
Greg Claytone005f2c2010-11-06 01:53:30 +0000264 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000265
Jim Inghamd1686902010-10-14 23:45:03 +0000266 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000267 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000268
269 Error materialize_error;
270
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000271
272 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
273 {
274 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
275 return false;
276 }
277
Sean Callanan65dafa82010-08-27 01:01:44 +0000278 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
279 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000280 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000281 return false;
282 }
283
284 if (log)
285 {
286 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000287
288 if (m_needs_object_ptr)
289 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
290
Sean Callanan65dafa82010-08-27 01:01:44 +0000291 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
292
293 StreamString args;
294
295 Error dump_error;
296
Sean Callanane8a59a82010-09-13 21:34:21 +0000297 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000298 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000299 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
300 {
301 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
302 }
303 else
304 {
305 log->Printf("Structure contents:\n%s", args.GetData());
306 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000307 }
308 }
Jim Inghamd1686902010-10-14 23:45:03 +0000309 }
310 return true;
311}
312
313ThreadPlan *
314ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
315 ExecutionContext &exe_ctx)
316{
317 lldb::addr_t struct_address;
318
319 lldb::addr_t object_ptr = NULL;
320
321 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
322
Jim Ingham360f53f2010-11-30 02:22:11 +0000323 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
324 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
325 // forcing unwind_on_error to be true here, in practical terms that can't happen.
Jim Inghamd1686902010-10-14 23:45:03 +0000326 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
327 m_jit_addr,
328 struct_address,
329 error_stream,
330 true,
331 true,
332 (m_needs_object_ptr ? &object_ptr : NULL));
333}
334
335bool
336ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
337 ExecutionContext &exe_ctx,
338 ClangExpressionVariable *&result)
339{
340 Error expr_error;
341
342 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
343 {
344 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
345 return false;
346 }
347 return true;
348}
349
Jim Ingham360f53f2010-11-30 02:22:11 +0000350Process::ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000351ClangUserExpression::Execute (Stream &error_stream,
352 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000353 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000354 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Jim Inghamd1686902010-10-14 23:45:03 +0000355 ClangExpressionVariable *&result)
356{
357 if (m_dwarf_opcodes.get())
358 {
359 // TODO execute the JITted opcodes
360
361 error_stream.Printf("We don't currently support executing DWARF expressions");
362
Jim Ingham360f53f2010-11-30 02:22:11 +0000363 return Process::eExecutionSetupError;
Jim Inghamd1686902010-10-14 23:45:03 +0000364 }
365 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
366 {
367 lldb::addr_t struct_address;
368
369 lldb::addr_t object_ptr = NULL;
370
371 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000372
Jim Inghamea9d4262010-11-05 19:25:48 +0000373 const bool stop_others = true;
374 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000375
Jim Ingham360f53f2010-11-30 02:22:11 +0000376 Address wrapper_address (NULL, m_jit_addr);
377 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread), wrapper_address, struct_address,
378 stop_others, discard_on_error,
379 (m_needs_object_ptr ? &object_ptr : NULL),
380 shared_ptr_to_me));
381 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
382 return Process::eExecutionSetupError;
383
384 call_plan_sp->SetPrivate(true);
385
386 uint32_t single_thread_timeout_usec = 10000000;
387 Process::ExecutionResults execution_result =
388 exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
389 single_thread_timeout_usec, error_stream);
390
391 if (execution_result == Process::eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000392 {
Jim Ingham360f53f2010-11-30 02:22:11 +0000393 if (discard_on_error)
394 error_stream.Printf ("Expression execution was interrupted. The process has been returned to the state before execution.");
395 else
396 error_stream.Printf ("Expression execution was interrupted. The process has been left at the point where it was interrupted.");
397
398 return execution_result;
399 }
400 else if (execution_result != Process::eExecutionCompleted)
401 {
402 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
403 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000404 }
405
Jim Ingham360f53f2010-11-30 02:22:11 +0000406 if (FinalizeJITExecution (error_stream, exe_ctx, result))
407 return Process::eExecutionCompleted;
408 else
409 return Process::eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000410 }
411 else
412 {
Johnny Chencb395442010-11-10 19:02:11 +0000413 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Jim Ingham360f53f2010-11-30 02:22:11 +0000414 return Process::eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000415 }
416}
417
418StreamString &
419ClangUserExpression::DwarfOpcodeStream ()
420{
421 if (!m_dwarf_opcodes.get())
422 m_dwarf_opcodes.reset(new StreamString());
423
424 return *m_dwarf_opcodes.get();
425}
Greg Clayton377e0b42010-10-05 00:31:29 +0000426
Jim Ingham360f53f2010-11-30 02:22:11 +0000427Process::ExecutionResults
Sean Callanan77e93942010-10-29 00:29:03 +0000428ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000429 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000430 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000431 const char *expr_prefix,
432 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000433{
434 Error error;
Jim Ingham360f53f2010-11-30 02:22:11 +0000435 Process::ExecutionResults execution_results = Process::eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000436
437 if (exe_ctx.process == NULL)
Jim Ingham360f53f2010-11-30 02:22:11 +0000438 {
439 error.SetErrorString ("Must have a process to evaluate expressions.");
440
441 result_valobj_sp.reset (new ValueObjectConstResult (error));
442 return Process::eExecutionSetupError;
443 }
444
Greg Clayton0baa3942010-11-04 01:54:29 +0000445 if (!exe_ctx.process->GetDynamicCheckers())
446 {
447 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
448
449 StreamString install_errors;
450
451 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callananf7731452010-11-05 00:57:06 +0000452 {
453 if (install_errors.GetString().empty())
454 error.SetErrorString ("couldn't install checkers, unknown error");
455 else
456 error.SetErrorString (install_errors.GetString().c_str());
457
458 result_valobj_sp.reset (new ValueObjectConstResult (error));
Jim Ingham360f53f2010-11-30 02:22:11 +0000459 return Process::eExecutionSetupError;
Sean Callananf7731452010-11-05 00:57:06 +0000460 }
461
Greg Clayton0baa3942010-11-04 01:54:29 +0000462 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
463 }
464
Jim Ingham360f53f2010-11-30 02:22:11 +0000465 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
466
Greg Clayton377e0b42010-10-05 00:31:29 +0000467 StreamString error_stream;
468
Jim Ingham360f53f2010-11-30 02:22:11 +0000469 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL)))
Greg Clayton377e0b42010-10-05 00:31:29 +0000470 {
471 if (error_stream.GetString().empty())
472 error.SetErrorString ("expression failed to parse, unknown error");
473 else
474 error.SetErrorString (error_stream.GetString().c_str());
475 }
476 else
477 {
478 ClangExpressionVariable *expr_result = NULL;
479
480 error_stream.GetString().clear();
481
Jim Ingham360f53f2010-11-30 02:22:11 +0000482 execution_results = user_expression_sp->Execute (error_stream,
483 exe_ctx,
484 discard_on_error,
485 user_expression_sp,
486 expr_result);
487 if (execution_results != Process::eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000488 {
489 if (error_stream.GetString().empty())
490 error.SetErrorString ("expression failed to execute, unknown error");
491 else
492 error.SetErrorString (error_stream.GetString().c_str());
493 }
494 else
495 {
496 // TODO: seems weird to get a pointer to a result object back from
497 // a function. Do we own it? Feels like we do, but from looking at the
498 // code we don't. Might be best to make this a reference and state
499 // explicitly that we don't own it when we get a reference back from
500 // the execute?
501 if (expr_result)
502 {
503 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
504 }
505 else
506 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000507 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000508 }
509 }
510 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000511
Greg Claytond1719722010-10-05 03:13:51 +0000512 if (result_valobj_sp.get() == NULL)
513 result_valobj_sp.reset (new ValueObjectConstResult (error));
514
Jim Ingham360f53f2010-11-30 02:22:11 +0000515 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000516}