blob: bd67bbdae628dc8c9ab59e80cc42e65f24d30621 [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"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000033#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000034#include "lldb/Target/Target.h"
35
36using namespace lldb_private;
37
Sean Callanan77e93942010-10-29 00:29:03 +000038ClangUserExpression::ClangUserExpression (const char *expr,
39 const char *expr_prefix) :
Sean Callanan65dafa82010-08-27 01:01:44 +000040 m_expr_text(expr),
Sean Callanan77e93942010-10-29 00:29:03 +000041 m_expr_prefix(expr_prefix),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000042 m_transformed_text(),
43 m_jit_addr(LLDB_INVALID_ADDRESS),
44 m_cplusplus(false),
45 m_objectivec(false),
46 m_needs_object_ptr(false)
Sean Callanan65dafa82010-08-27 01:01:44 +000047{
Sean Callanan65dafa82010-08-27 01:01:44 +000048}
49
Sean Callanan830a9032010-08-27 23:31:21 +000050ClangUserExpression::~ClangUserExpression ()
51{
52}
53
Sean Callanan65dafa82010-08-27 01:01:44 +000054clang::ASTConsumer *
55ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
56{
57 return new ASTResultSynthesizer(passthrough);
58}
59
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000060void
61ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
62{
63 if (!exe_ctx.frame)
64 return;
65
66 VariableList *vars = exe_ctx.frame->GetVariableList(false);
67
68 if (!vars)
69 return;
70
71 if (vars->FindVariable(ConstString("this")).get())
72 m_cplusplus = true;
73 else if (vars->FindVariable(ConstString("self")).get())
74 m_objectivec = true;
75}
76
Sean Callanan550f2762010-10-22 23:25:16 +000077// This is a really nasty hack, meant to fix Objective-C expressions of the form
78// (int)[myArray count]. Right now, because the type information for count is
79// not available, [myArray count] returns id, which can't be directly cast to
80// int without causing a clang error.
81static void
82ApplyObjcCastHack(std::string &expr)
83{
84#define OBJC_CAST_HACK_FROM "(int)["
85#define OBJC_CAST_HACK_TO "(int)(long long)["
86
87 size_t from_offset;
88
89 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
90 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
91
92#undef OBJC_CAST_HACK_TO
93#undef OBJC_CAST_HACK_FROM
94}
95
Sean Callanan30892372010-10-24 20:45:49 +000096// Another hack, meant to allow use of unichar despite it not being available in
97// the type information. Although we could special-case it in type lookup,
98// hopefully we'll figure out a way to #include the same environment as is
99// present in the original source file rather than try to hack specific type
100// definitions in as needed.
101static void
102ApplyUnicharHack(std::string &expr)
103{
104#define UNICHAR_HACK_FROM "unichar"
105#define UNICHAR_HACK_TO "unsigned short"
106
107 size_t from_offset;
108
109 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
110 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
111
112#undef UNICHAR_HACK_TO
113#undef UNICHAR_HACK_FROM
114}
115
Sean Callanan550f2762010-10-22 23:25:16 +0000116bool
Sean Callanan65dafa82010-08-27 01:01:44 +0000117ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
118{
119 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
120
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000121 ScanContext(exe_ctx);
122
123 StreamString m_transformed_stream;
124
125 ////////////////////////////////////
126 // Generate the expression
127 //
Sean Callanan550f2762010-10-22 23:25:16 +0000128
129 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000130 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000131
132 if (m_cplusplus)
133 {
Sean Callanan77e93942010-10-29 00:29:03 +0000134 m_transformed_stream.Printf("%s \n"
135 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000136 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000137 "$__lldb_class::%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000138 "{ \n"
139 " %s; \n"
140 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000141 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000142 FunctionName(),
143 m_expr_text.c_str());
144
145 m_needs_object_ptr = true;
146 }
147 else
148 {
Sean Callanan77e93942010-10-29 00:29:03 +0000149 m_transformed_stream.Printf("%s \n"
150 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000151 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000152 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000153 "{ \n"
154 " %s; \n"
155 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000156 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000157 FunctionName(),
158 m_expr_text.c_str());
159 }
160
161 m_transformed_text = m_transformed_stream.GetData();
162
163
164 if (log)
165 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
166
Sean Callanan65dafa82010-08-27 01:01:44 +0000167 ////////////////////////////////////
168 // Set up the target and compiler
169 //
170
171 Target *target = exe_ctx.target;
172
173 if (!target)
174 {
175 error_stream.PutCString ("error: invalid target\n");
176 return false;
177 }
178
179 ConstString target_triple;
180
181 target->GetTargetTriple (target_triple);
182
183 if (!target_triple)
184 target_triple = Host::GetTargetTriple ();
185
186 if (!target_triple)
187 {
188 error_stream.PutCString ("error: invalid target triple\n");
189 return false;
190 }
191
192 //////////////////////////
193 // Parse the expression
194 //
195
196 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
197
198 ClangExpressionParser parser(target_triple.GetCString(), *this);
199
200 unsigned num_errors = parser.Parse (error_stream);
201
202 if (num_errors)
203 {
204 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
205 return false;
206 }
207
208 ///////////////////////////////////////////////
209 // Convert the output of the parser to DWARF
210 //
211
212 m_dwarf_opcodes.reset(new StreamString);
213 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
214 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
215
216 m_local_variables.reset(new ClangExpressionVariableStore());
217
218 Error dwarf_error = parser.MakeDWARF ();
219
220 if (dwarf_error.Success())
221 {
222 if (log)
223 log->Printf("Code can be interpreted.");
224
225 return true;
226 }
227
228 //////////////////////////////////
229 // JIT the output of the parser
230 //
231
232 m_dwarf_opcodes.reset();
233
Sean Callanan830a9032010-08-27 23:31:21 +0000234 lldb::addr_t jit_end;
235
236 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000237
238 if (jit_error.Success())
239 {
240 if (log)
241 {
242 log->Printf("Code can be run in the target.");
243
244 StreamString disassembly_stream;
245
246 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
247
248 if (!err.Success())
249 {
250 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
251 }
252 else
253 {
254 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
255 }
256 }
257
258 return true;
259 }
260 else
261 {
262 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
263 return false;
264 }
265}
266
267bool
Jim Inghamd1686902010-10-14 23:45:03 +0000268ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000269 ExecutionContext &exe_ctx,
270 lldb::addr_t &struct_address,
271 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000272{
273 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
274
Jim Inghamd1686902010-10-14 23:45:03 +0000275 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000276 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000277
278 Error materialize_error;
279
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000280
281 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
282 {
283 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
284 return false;
285 }
286
Sean Callanan65dafa82010-08-27 01:01:44 +0000287 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
288 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000289 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000290 return false;
291 }
292
293 if (log)
294 {
295 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000296
297 if (m_needs_object_ptr)
298 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
299
Sean Callanan65dafa82010-08-27 01:01:44 +0000300 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
301
302 StreamString args;
303
304 Error dump_error;
305
Sean Callanane8a59a82010-09-13 21:34:21 +0000306 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000307 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000308 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
309 {
310 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
311 }
312 else
313 {
314 log->Printf("Structure contents:\n%s", args.GetData());
315 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000316 }
317 }
Jim Inghamd1686902010-10-14 23:45:03 +0000318 }
319 return true;
320}
321
322ThreadPlan *
323ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
324 ExecutionContext &exe_ctx)
325{
326 lldb::addr_t struct_address;
327
328 lldb::addr_t object_ptr = NULL;
329
330 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
331
332 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
333 m_jit_addr,
334 struct_address,
335 error_stream,
336 true,
337 true,
338 (m_needs_object_ptr ? &object_ptr : NULL));
339}
340
341bool
342ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
343 ExecutionContext &exe_ctx,
344 ClangExpressionVariable *&result)
345{
346 Error expr_error;
347
348 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
349 {
350 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
351 return false;
352 }
353 return true;
354}
355
356bool
357ClangUserExpression::Execute (Stream &error_stream,
358 ExecutionContext &exe_ctx,
359 ClangExpressionVariable *&result)
360{
361 if (m_dwarf_opcodes.get())
362 {
363 // TODO execute the JITted opcodes
364
365 error_stream.Printf("We don't currently support executing DWARF expressions");
366
367 return false;
368 }
369 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
370 {
371 lldb::addr_t struct_address;
372
373 lldb::addr_t object_ptr = NULL;
374
375 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000376
377 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000378 ClangFunction::ExecuteFunction (exe_ctx,
379 m_jit_addr,
380 struct_address,
381 true,
382 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000383 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000384 error_stream,
385 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000386
387 if (execution_result != ClangFunction::eExecutionCompleted)
388 {
389 const char *result_name;
390
391 switch (execution_result)
392 {
393 case ClangFunction::eExecutionCompleted:
394 result_name = "eExecutionCompleted";
395 break;
396 case ClangFunction::eExecutionDiscarded:
397 result_name = "eExecutionDiscarded";
398 break;
399 case ClangFunction::eExecutionInterrupted:
400 result_name = "eExecutionInterrupted";
401 break;
402 case ClangFunction::eExecutionSetupError:
403 result_name = "eExecutionSetupError";
404 break;
405 case ClangFunction::eExecutionTimedOut:
406 result_name = "eExecutionTimedOut";
407 break;
408 }
409
410 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
411 return false;
412 }
413
Jim Inghamd1686902010-10-14 23:45:03 +0000414 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000415 }
416 else
417 {
418 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
419 return false;
420 }
421}
422
423StreamString &
424ClangUserExpression::DwarfOpcodeStream ()
425{
426 if (!m_dwarf_opcodes.get())
427 m_dwarf_opcodes.reset(new StreamString());
428
429 return *m_dwarf_opcodes.get();
430}
Greg Clayton377e0b42010-10-05 00:31:29 +0000431
432
Greg Claytond1719722010-10-05 03:13:51 +0000433lldb::ValueObjectSP
Sean Callanan77e93942010-10-29 00:29:03 +0000434ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
435 const char *expr_cstr,
436 const char *expr_prefix)
Greg Clayton377e0b42010-10-05 00:31:29 +0000437{
438 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000439 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan77e93942010-10-29 00:29:03 +0000440 ClangUserExpression user_expression (expr_cstr, expr_prefix);
Greg Clayton377e0b42010-10-05 00:31:29 +0000441
442 StreamString error_stream;
443
444 if (!user_expression.Parse (error_stream, exe_ctx))
445 {
446 if (error_stream.GetString().empty())
447 error.SetErrorString ("expression failed to parse, unknown error");
448 else
449 error.SetErrorString (error_stream.GetString().c_str());
450 }
451 else
452 {
453 ClangExpressionVariable *expr_result = NULL;
454
455 error_stream.GetString().clear();
456
457 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
458 {
459 if (error_stream.GetString().empty())
460 error.SetErrorString ("expression failed to execute, unknown error");
461 else
462 error.SetErrorString (error_stream.GetString().c_str());
463 }
464 else
465 {
466 // TODO: seems weird to get a pointer to a result object back from
467 // a function. Do we own it? Feels like we do, but from looking at the
468 // code we don't. Might be best to make this a reference and state
469 // explicitly that we don't own it when we get a reference back from
470 // the execute?
471 if (expr_result)
472 {
473 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
474 }
475 else
476 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000477 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000478 }
479 }
480 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000481
Greg Claytond1719722010-10-05 03:13:51 +0000482 if (result_valobj_sp.get() == NULL)
483 result_valobj_sp.reset (new ValueObjectConstResult (error));
484
485 return result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000486}