blob: f35152897411d75a5717ba22201183933cb52c3b [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"
36
37using namespace lldb_private;
38
Sean Callanan77e93942010-10-29 00:29:03 +000039ClangUserExpression::ClangUserExpression (const char *expr,
40 const char *expr_prefix) :
Sean Callanan65dafa82010-08-27 01:01:44 +000041 m_expr_text(expr),
Johnny Chenb4c0f022010-10-29 20:19:44 +000042 m_expr_prefix(expr_prefix ? expr_prefix : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000043 m_transformed_text(),
44 m_jit_addr(LLDB_INVALID_ADDRESS),
45 m_cplusplus(false),
46 m_objectivec(false),
47 m_needs_object_ptr(false)
Sean Callanan65dafa82010-08-27 01:01:44 +000048{
Sean Callanan65dafa82010-08-27 01:01:44 +000049}
50
Sean Callanan830a9032010-08-27 23:31:21 +000051ClangUserExpression::~ClangUserExpression ()
52{
53}
54
Sean Callanan65dafa82010-08-27 01:01:44 +000055clang::ASTConsumer *
56ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
57{
58 return new ASTResultSynthesizer(passthrough);
59}
60
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000061void
62ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
63{
64 if (!exe_ctx.frame)
65 return;
66
67 VariableList *vars = exe_ctx.frame->GetVariableList(false);
68
69 if (!vars)
70 return;
71
72 if (vars->FindVariable(ConstString("this")).get())
73 m_cplusplus = true;
74 else if (vars->FindVariable(ConstString("self")).get())
75 m_objectivec = true;
76}
77
Sean Callanan550f2762010-10-22 23:25:16 +000078// This is a really nasty hack, meant to fix Objective-C expressions of the form
79// (int)[myArray count]. Right now, because the type information for count is
80// not available, [myArray count] returns id, which can't be directly cast to
81// int without causing a clang error.
82static void
83ApplyObjcCastHack(std::string &expr)
84{
85#define OBJC_CAST_HACK_FROM "(int)["
86#define OBJC_CAST_HACK_TO "(int)(long long)["
87
88 size_t from_offset;
89
90 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
91 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
92
93#undef OBJC_CAST_HACK_TO
94#undef OBJC_CAST_HACK_FROM
95}
96
Sean Callanan30892372010-10-24 20:45:49 +000097// Another hack, meant to allow use of unichar despite it not being available in
98// the type information. Although we could special-case it in type lookup,
99// hopefully we'll figure out a way to #include the same environment as is
100// present in the original source file rather than try to hack specific type
101// definitions in as needed.
102static void
103ApplyUnicharHack(std::string &expr)
104{
105#define UNICHAR_HACK_FROM "unichar"
106#define UNICHAR_HACK_TO "unsigned short"
107
108 size_t from_offset;
109
110 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
111 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
112
113#undef UNICHAR_HACK_TO
114#undef UNICHAR_HACK_FROM
115}
116
Sean Callanan550f2762010-10-22 23:25:16 +0000117bool
Sean Callanan65dafa82010-08-27 01:01:44 +0000118ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
119{
Greg Claytone005f2c2010-11-06 01:53:30 +0000120 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000121
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000122 ScanContext(exe_ctx);
123
124 StreamString m_transformed_stream;
125
126 ////////////////////////////////////
127 // Generate the expression
128 //
Sean Callanan550f2762010-10-22 23:25:16 +0000129
130 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000131 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000132
133 if (m_cplusplus)
134 {
Sean Callanan77e93942010-10-29 00:29:03 +0000135 m_transformed_stream.Printf("%s \n"
136 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000137 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000138 "$__lldb_class::%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000139 "{ \n"
140 " %s; \n"
141 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000142 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000143 FunctionName(),
144 m_expr_text.c_str());
145
146 m_needs_object_ptr = true;
147 }
148 else
149 {
Sean Callanan77e93942010-10-29 00:29:03 +0000150 m_transformed_stream.Printf("%s \n"
151 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000152 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000153 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000154 "{ \n"
155 " %s; \n"
156 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000157 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000158 FunctionName(),
159 m_expr_text.c_str());
160 }
161
162 m_transformed_text = m_transformed_stream.GetData();
163
164
165 if (log)
166 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
167
Sean Callanan65dafa82010-08-27 01:01:44 +0000168 ////////////////////////////////////
169 // Set up the target and compiler
170 //
171
172 Target *target = exe_ctx.target;
173
174 if (!target)
175 {
176 error_stream.PutCString ("error: invalid target\n");
177 return false;
178 }
179
180 ConstString target_triple;
181
182 target->GetTargetTriple (target_triple);
183
184 if (!target_triple)
185 target_triple = Host::GetTargetTriple ();
186
187 if (!target_triple)
188 {
189 error_stream.PutCString ("error: invalid target triple\n");
190 return false;
191 }
192
193 //////////////////////////
194 // Parse the expression
195 //
196
197 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
198
199 ClangExpressionParser parser(target_triple.GetCString(), *this);
200
201 unsigned num_errors = parser.Parse (error_stream);
202
203 if (num_errors)
204 {
205 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
206 return false;
207 }
208
209 ///////////////////////////////////////////////
210 // Convert the output of the parser to DWARF
211 //
212
213 m_dwarf_opcodes.reset(new StreamString);
214 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
215 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
216
217 m_local_variables.reset(new ClangExpressionVariableStore());
218
219 Error dwarf_error = parser.MakeDWARF ();
220
221 if (dwarf_error.Success())
222 {
223 if (log)
224 log->Printf("Code can be interpreted.");
225
226 return true;
227 }
228
229 //////////////////////////////////
230 // JIT the output of the parser
231 //
232
233 m_dwarf_opcodes.reset();
234
Sean Callanan830a9032010-08-27 23:31:21 +0000235 lldb::addr_t jit_end;
236
237 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000238
239 if (jit_error.Success())
240 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000241 return true;
242 }
243 else
244 {
245 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
246 return false;
247 }
248}
249
250bool
Jim Inghamd1686902010-10-14 23:45:03 +0000251ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000252 ExecutionContext &exe_ctx,
253 lldb::addr_t &struct_address,
254 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000255{
Greg Claytone005f2c2010-11-06 01:53:30 +0000256 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000257
Jim Inghamd1686902010-10-14 23:45:03 +0000258 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000259 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000260
261 Error materialize_error;
262
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000263
264 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
265 {
266 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
267 return false;
268 }
269
Sean Callanan65dafa82010-08-27 01:01:44 +0000270 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
271 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000272 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000273 return false;
274 }
275
276 if (log)
277 {
278 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000279
280 if (m_needs_object_ptr)
281 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
282
Sean Callanan65dafa82010-08-27 01:01:44 +0000283 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
284
285 StreamString args;
286
287 Error dump_error;
288
Sean Callanane8a59a82010-09-13 21:34:21 +0000289 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000290 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000291 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
292 {
293 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
294 }
295 else
296 {
297 log->Printf("Structure contents:\n%s", args.GetData());
298 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000299 }
300 }
Jim Inghamd1686902010-10-14 23:45:03 +0000301 }
302 return true;
303}
304
305ThreadPlan *
306ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
307 ExecutionContext &exe_ctx)
308{
309 lldb::addr_t struct_address;
310
311 lldb::addr_t object_ptr = NULL;
312
313 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
314
315 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
316 m_jit_addr,
317 struct_address,
318 error_stream,
319 true,
320 true,
321 (m_needs_object_ptr ? &object_ptr : NULL));
322}
323
324bool
325ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
326 ExecutionContext &exe_ctx,
327 ClangExpressionVariable *&result)
328{
329 Error expr_error;
330
331 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
332 {
333 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
334 return false;
335 }
336 return true;
337}
338
339bool
340ClangUserExpression::Execute (Stream &error_stream,
341 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000342 bool discard_on_error,
Jim Inghamd1686902010-10-14 23:45:03 +0000343 ClangExpressionVariable *&result)
344{
345 if (m_dwarf_opcodes.get())
346 {
347 // TODO execute the JITted opcodes
348
349 error_stream.Printf("We don't currently support executing DWARF expressions");
350
351 return false;
352 }
353 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
354 {
355 lldb::addr_t struct_address;
356
357 lldb::addr_t object_ptr = NULL;
358
359 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000360
Jim Inghamea9d4262010-11-05 19:25:48 +0000361 const bool stop_others = true;
362 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000363 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000364 ClangFunction::ExecuteFunction (exe_ctx,
365 m_jit_addr,
366 struct_address,
Jim Inghamea9d4262010-11-05 19:25:48 +0000367 stop_others,
368 try_all_threads,
369 discard_on_error,
Sean Callanan0027cec2010-10-07 18:17:31 +0000370 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000371 error_stream,
372 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000373
374 if (execution_result != ClangFunction::eExecutionCompleted)
375 {
376 const char *result_name;
377
378 switch (execution_result)
379 {
380 case ClangFunction::eExecutionCompleted:
381 result_name = "eExecutionCompleted";
382 break;
383 case ClangFunction::eExecutionDiscarded:
384 result_name = "eExecutionDiscarded";
385 break;
386 case ClangFunction::eExecutionInterrupted:
387 result_name = "eExecutionInterrupted";
388 break;
389 case ClangFunction::eExecutionSetupError:
390 result_name = "eExecutionSetupError";
391 break;
392 case ClangFunction::eExecutionTimedOut:
393 result_name = "eExecutionTimedOut";
394 break;
395 }
396
397 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
398 return false;
399 }
400
Jim Inghamd1686902010-10-14 23:45:03 +0000401 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000402 }
403 else
404 {
Johnny Chencb395442010-11-10 19:02:11 +0000405 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Sean Callanan65dafa82010-08-27 01:01:44 +0000406 return false;
407 }
408}
409
410StreamString &
411ClangUserExpression::DwarfOpcodeStream ()
412{
413 if (!m_dwarf_opcodes.get())
414 m_dwarf_opcodes.reset(new StreamString());
415
416 return *m_dwarf_opcodes.get();
417}
Greg Clayton377e0b42010-10-05 00:31:29 +0000418
Greg Claytond1719722010-10-05 03:13:51 +0000419lldb::ValueObjectSP
Sean Callanan77e93942010-10-29 00:29:03 +0000420ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000421 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000422 const char *expr_cstr,
423 const char *expr_prefix)
Greg Clayton377e0b42010-10-05 00:31:29 +0000424{
425 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000426 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton0baa3942010-11-04 01:54:29 +0000427
428 if (exe_ctx.process == NULL)
429 return result_valobj_sp;
430
431 if (!exe_ctx.process->GetDynamicCheckers())
432 {
433 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
434
435 StreamString install_errors;
436
437 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callananf7731452010-11-05 00:57:06 +0000438 {
439 if (install_errors.GetString().empty())
440 error.SetErrorString ("couldn't install checkers, unknown error");
441 else
442 error.SetErrorString (install_errors.GetString().c_str());
443
444 result_valobj_sp.reset (new ValueObjectConstResult (error));
Greg Clayton0baa3942010-11-04 01:54:29 +0000445 return result_valobj_sp;
Sean Callananf7731452010-11-05 00:57:06 +0000446 }
447
Greg Clayton0baa3942010-11-04 01:54:29 +0000448 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
449 }
450
Sean Callanan77e93942010-10-29 00:29:03 +0000451 ClangUserExpression user_expression (expr_cstr, expr_prefix);
Greg Clayton377e0b42010-10-05 00:31:29 +0000452
453 StreamString error_stream;
454
455 if (!user_expression.Parse (error_stream, exe_ctx))
456 {
457 if (error_stream.GetString().empty())
458 error.SetErrorString ("expression failed to parse, unknown error");
459 else
460 error.SetErrorString (error_stream.GetString().c_str());
461 }
462 else
463 {
464 ClangExpressionVariable *expr_result = NULL;
465
466 error_stream.GetString().clear();
467
Jim Inghamea9d4262010-11-05 19:25:48 +0000468 if (!user_expression.Execute (error_stream, exe_ctx, discard_on_error, expr_result))
Greg Clayton377e0b42010-10-05 00:31:29 +0000469 {
470 if (error_stream.GetString().empty())
471 error.SetErrorString ("expression failed to execute, unknown error");
472 else
473 error.SetErrorString (error_stream.GetString().c_str());
474 }
475 else
476 {
477 // TODO: seems weird to get a pointer to a result object back from
478 // a function. Do we own it? Feels like we do, but from looking at the
479 // code we don't. Might be best to make this a reference and state
480 // explicitly that we don't own it when we get a reference back from
481 // the execute?
482 if (expr_result)
483 {
484 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
485 }
486 else
487 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000488 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000489 }
490 }
491 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000492
Greg Claytond1719722010-10-05 03:13:51 +0000493 if (result_valobj_sp.get() == NULL)
494 result_valobj_sp.reset (new ValueObjectConstResult (error));
495
496 return result_valobj_sp;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000497}