blob: 5181c98c9aa3a1f5cfc17a63132a60751fdb4321 [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{
120 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
121
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 {
241 if (log)
242 {
243 log->Printf("Code can be run in the target.");
244
245 StreamString disassembly_stream;
246
247 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
248
249 if (!err.Success())
250 {
251 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
252 }
253 else
254 {
255 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
256 }
257 }
258
259 return true;
260 }
261 else
262 {
263 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
264 return false;
265 }
266}
267
268bool
Jim Inghamd1686902010-10-14 23:45:03 +0000269ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000270 ExecutionContext &exe_ctx,
271 lldb::addr_t &struct_address,
272 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000273{
274 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
275
Jim Inghamd1686902010-10-14 23:45:03 +0000276 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000277 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000278
279 Error materialize_error;
280
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000281
282 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
283 {
284 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
285 return false;
286 }
287
Sean Callanan65dafa82010-08-27 01:01:44 +0000288 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
289 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000290 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000291 return false;
292 }
293
294 if (log)
295 {
296 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000297
298 if (m_needs_object_ptr)
299 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
300
Sean Callanan65dafa82010-08-27 01:01:44 +0000301 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
302
303 StreamString args;
304
305 Error dump_error;
306
Sean Callanane8a59a82010-09-13 21:34:21 +0000307 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000308 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000309 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
310 {
311 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
312 }
313 else
314 {
315 log->Printf("Structure contents:\n%s", args.GetData());
316 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000317 }
318 }
Jim Inghamd1686902010-10-14 23:45:03 +0000319 }
320 return true;
321}
322
323ThreadPlan *
324ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
325 ExecutionContext &exe_ctx)
326{
327 lldb::addr_t struct_address;
328
329 lldb::addr_t object_ptr = NULL;
330
331 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
332
333 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
334 m_jit_addr,
335 struct_address,
336 error_stream,
337 true,
338 true,
339 (m_needs_object_ptr ? &object_ptr : NULL));
340}
341
342bool
343ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
344 ExecutionContext &exe_ctx,
345 ClangExpressionVariable *&result)
346{
347 Error expr_error;
348
349 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
350 {
351 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
352 return false;
353 }
354 return true;
355}
356
357bool
358ClangUserExpression::Execute (Stream &error_stream,
359 ExecutionContext &exe_ctx,
360 ClangExpressionVariable *&result)
361{
362 if (m_dwarf_opcodes.get())
363 {
364 // TODO execute the JITted opcodes
365
366 error_stream.Printf("We don't currently support executing DWARF expressions");
367
368 return false;
369 }
370 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
371 {
372 lldb::addr_t struct_address;
373
374 lldb::addr_t object_ptr = NULL;
375
376 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000377
378 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000379 ClangFunction::ExecuteFunction (exe_ctx,
380 m_jit_addr,
381 struct_address,
382 true,
383 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000384 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000385 error_stream,
386 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000387
388 if (execution_result != ClangFunction::eExecutionCompleted)
389 {
390 const char *result_name;
391
392 switch (execution_result)
393 {
394 case ClangFunction::eExecutionCompleted:
395 result_name = "eExecutionCompleted";
396 break;
397 case ClangFunction::eExecutionDiscarded:
398 result_name = "eExecutionDiscarded";
399 break;
400 case ClangFunction::eExecutionInterrupted:
401 result_name = "eExecutionInterrupted";
402 break;
403 case ClangFunction::eExecutionSetupError:
404 result_name = "eExecutionSetupError";
405 break;
406 case ClangFunction::eExecutionTimedOut:
407 result_name = "eExecutionTimedOut";
408 break;
409 }
410
411 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
412 return false;
413 }
414
Jim Inghamd1686902010-10-14 23:45:03 +0000415 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000416 }
417 else
418 {
419 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
420 return false;
421 }
422}
423
424StreamString &
425ClangUserExpression::DwarfOpcodeStream ()
426{
427 if (!m_dwarf_opcodes.get())
428 m_dwarf_opcodes.reset(new StreamString());
429
430 return *m_dwarf_opcodes.get();
431}
Greg Clayton377e0b42010-10-05 00:31:29 +0000432
433
Greg Claytond1719722010-10-05 03:13:51 +0000434lldb::ValueObjectSP
Sean Callanan77e93942010-10-29 00:29:03 +0000435ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
436 const char *expr_cstr,
437 const char *expr_prefix)
Greg Clayton377e0b42010-10-05 00:31:29 +0000438{
439 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000440 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton0baa3942010-11-04 01:54:29 +0000441
442 if (exe_ctx.process == NULL)
443 return result_valobj_sp;
444
445 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))
452 return result_valobj_sp;
453
454 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
455 }
456
Sean Callanan77e93942010-10-29 00:29:03 +0000457 ClangUserExpression user_expression (expr_cstr, expr_prefix);
Greg Clayton377e0b42010-10-05 00:31:29 +0000458
459 StreamString error_stream;
460
461 if (!user_expression.Parse (error_stream, exe_ctx))
462 {
463 if (error_stream.GetString().empty())
464 error.SetErrorString ("expression failed to parse, unknown error");
465 else
466 error.SetErrorString (error_stream.GetString().c_str());
467 }
468 else
469 {
470 ClangExpressionVariable *expr_result = NULL;
471
472 error_stream.GetString().clear();
473
474 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
475 {
476 if (error_stream.GetString().empty())
477 error.SetErrorString ("expression failed to execute, unknown error");
478 else
479 error.SetErrorString (error_stream.GetString().c_str());
480 }
481 else
482 {
483 // TODO: seems weird to get a pointer to a result object back from
484 // a function. Do we own it? Feels like we do, but from looking at the
485 // code we don't. Might be best to make this a reference and state
486 // explicitly that we don't own it when we get a reference back from
487 // the execute?
488 if (expr_result)
489 {
490 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
491 }
492 else
493 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000494 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000495 }
496 }
497 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000498
Greg Claytond1719722010-10-05 03:13:51 +0000499 if (result_valobj_sp.get() == NULL)
500 result_valobj_sp.reset (new ValueObjectConstResult (error));
501
502 return result_valobj_sp;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000503}