blob: 60f3e754a26d83f454272a4161638fb209925726 [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
38ClangUserExpression::ClangUserExpression (const char *expr) :
39 m_expr_text(expr),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000040 m_transformed_text(),
41 m_jit_addr(LLDB_INVALID_ADDRESS),
42 m_cplusplus(false),
43 m_objectivec(false),
44 m_needs_object_ptr(false)
Sean Callanan65dafa82010-08-27 01:01:44 +000045{
Sean Callanan65dafa82010-08-27 01:01:44 +000046}
47
Sean Callanan830a9032010-08-27 23:31:21 +000048ClangUserExpression::~ClangUserExpression ()
49{
50}
51
Sean Callanan65dafa82010-08-27 01:01:44 +000052clang::ASTConsumer *
53ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
54{
55 return new ASTResultSynthesizer(passthrough);
56}
57
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000058void
59ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
60{
61 if (!exe_ctx.frame)
62 return;
63
64 VariableList *vars = exe_ctx.frame->GetVariableList(false);
65
66 if (!vars)
67 return;
68
69 if (vars->FindVariable(ConstString("this")).get())
70 m_cplusplus = true;
71 else if (vars->FindVariable(ConstString("self")).get())
72 m_objectivec = true;
73}
74
Sean Callanan550f2762010-10-22 23:25:16 +000075// This is a really nasty hack, meant to fix Objective-C expressions of the form
76// (int)[myArray count]. Right now, because the type information for count is
77// not available, [myArray count] returns id, which can't be directly cast to
78// int without causing a clang error.
79static void
80ApplyObjcCastHack(std::string &expr)
81{
82#define OBJC_CAST_HACK_FROM "(int)["
83#define OBJC_CAST_HACK_TO "(int)(long long)["
84
85 size_t from_offset;
86
87 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
88 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
89
90#undef OBJC_CAST_HACK_TO
91#undef OBJC_CAST_HACK_FROM
92}
93
Sean Callanan30892372010-10-24 20:45:49 +000094// Another hack, meant to allow use of unichar despite it not being available in
95// the type information. Although we could special-case it in type lookup,
96// hopefully we'll figure out a way to #include the same environment as is
97// present in the original source file rather than try to hack specific type
98// definitions in as needed.
99static void
100ApplyUnicharHack(std::string &expr)
101{
102#define UNICHAR_HACK_FROM "unichar"
103#define UNICHAR_HACK_TO "unsigned short"
104
105 size_t from_offset;
106
107 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
108 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
109
110#undef UNICHAR_HACK_TO
111#undef UNICHAR_HACK_FROM
112}
113
Sean Callanan550f2762010-10-22 23:25:16 +0000114bool
Sean Callanan65dafa82010-08-27 01:01:44 +0000115ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
116{
117 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
118
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000119 ScanContext(exe_ctx);
120
121 StreamString m_transformed_stream;
122
123 ////////////////////////////////////
124 // Generate the expression
125 //
Sean Callanan550f2762010-10-22 23:25:16 +0000126
127 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000128 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000129
130 if (m_cplusplus)
131 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000132 m_transformed_stream.Printf("typedef unsigned short unichar; \n"
133 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000134 "$__lldb_class::%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000135 "{ \n"
136 " %s; \n"
137 "} \n",
138 FunctionName(),
139 m_expr_text.c_str());
140
141 m_needs_object_ptr = true;
142 }
143 else
144 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000145 m_transformed_stream.Printf("typedef unsigned short unichar;\n"
146 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000147 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000148 "{ \n"
149 " %s; \n"
150 "} \n",
151 FunctionName(),
152 m_expr_text.c_str());
153 }
154
155 m_transformed_text = m_transformed_stream.GetData();
156
157
158 if (log)
159 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
160
Sean Callanan65dafa82010-08-27 01:01:44 +0000161 ////////////////////////////////////
162 // Set up the target and compiler
163 //
164
165 Target *target = exe_ctx.target;
166
167 if (!target)
168 {
169 error_stream.PutCString ("error: invalid target\n");
170 return false;
171 }
172
173 ConstString target_triple;
174
175 target->GetTargetTriple (target_triple);
176
177 if (!target_triple)
178 target_triple = Host::GetTargetTriple ();
179
180 if (!target_triple)
181 {
182 error_stream.PutCString ("error: invalid target triple\n");
183 return false;
184 }
185
186 //////////////////////////
187 // Parse the expression
188 //
189
190 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
191
192 ClangExpressionParser parser(target_triple.GetCString(), *this);
193
194 unsigned num_errors = parser.Parse (error_stream);
195
196 if (num_errors)
197 {
198 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
199 return false;
200 }
201
202 ///////////////////////////////////////////////
203 // Convert the output of the parser to DWARF
204 //
205
206 m_dwarf_opcodes.reset(new StreamString);
207 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
208 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
209
210 m_local_variables.reset(new ClangExpressionVariableStore());
211
212 Error dwarf_error = parser.MakeDWARF ();
213
214 if (dwarf_error.Success())
215 {
216 if (log)
217 log->Printf("Code can be interpreted.");
218
219 return true;
220 }
221
222 //////////////////////////////////
223 // JIT the output of the parser
224 //
225
226 m_dwarf_opcodes.reset();
227
Sean Callanan830a9032010-08-27 23:31:21 +0000228 lldb::addr_t jit_end;
229
230 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000231
232 if (jit_error.Success())
233 {
234 if (log)
235 {
236 log->Printf("Code can be run in the target.");
237
238 StreamString disassembly_stream;
239
240 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
241
242 if (!err.Success())
243 {
244 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
245 }
246 else
247 {
248 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
249 }
250 }
251
252 return true;
253 }
254 else
255 {
256 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
257 return false;
258 }
259}
260
261bool
Jim Inghamd1686902010-10-14 23:45:03 +0000262ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000263 ExecutionContext &exe_ctx,
264 lldb::addr_t &struct_address,
265 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000266{
267 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
268
Jim Inghamd1686902010-10-14 23:45:03 +0000269 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000270 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000271
272 Error materialize_error;
273
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000274
275 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
276 {
277 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
278 return false;
279 }
280
Sean Callanan65dafa82010-08-27 01:01:44 +0000281 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
282 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000283 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000284 return false;
285 }
286
287 if (log)
288 {
289 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000290
291 if (m_needs_object_ptr)
292 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
293
Sean Callanan65dafa82010-08-27 01:01:44 +0000294 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
295
296 StreamString args;
297
298 Error dump_error;
299
Sean Callanane8a59a82010-09-13 21:34:21 +0000300 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000301 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000302 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
303 {
304 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
305 }
306 else
307 {
308 log->Printf("Structure contents:\n%s", args.GetData());
309 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000310 }
311 }
Jim Inghamd1686902010-10-14 23:45:03 +0000312 }
313 return true;
314}
315
316ThreadPlan *
317ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
318 ExecutionContext &exe_ctx)
319{
320 lldb::addr_t struct_address;
321
322 lldb::addr_t object_ptr = NULL;
323
324 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
325
326 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
350bool
351ClangUserExpression::Execute (Stream &error_stream,
352 ExecutionContext &exe_ctx,
353 ClangExpressionVariable *&result)
354{
355 if (m_dwarf_opcodes.get())
356 {
357 // TODO execute the JITted opcodes
358
359 error_stream.Printf("We don't currently support executing DWARF expressions");
360
361 return false;
362 }
363 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
364 {
365 lldb::addr_t struct_address;
366
367 lldb::addr_t object_ptr = NULL;
368
369 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000370
371 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000372 ClangFunction::ExecuteFunction (exe_ctx,
373 m_jit_addr,
374 struct_address,
375 true,
376 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000377 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000378 error_stream,
379 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000380
381 if (execution_result != ClangFunction::eExecutionCompleted)
382 {
383 const char *result_name;
384
385 switch (execution_result)
386 {
387 case ClangFunction::eExecutionCompleted:
388 result_name = "eExecutionCompleted";
389 break;
390 case ClangFunction::eExecutionDiscarded:
391 result_name = "eExecutionDiscarded";
392 break;
393 case ClangFunction::eExecutionInterrupted:
394 result_name = "eExecutionInterrupted";
395 break;
396 case ClangFunction::eExecutionSetupError:
397 result_name = "eExecutionSetupError";
398 break;
399 case ClangFunction::eExecutionTimedOut:
400 result_name = "eExecutionTimedOut";
401 break;
402 }
403
404 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
405 return false;
406 }
407
Jim Inghamd1686902010-10-14 23:45:03 +0000408 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000409 }
410 else
411 {
412 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
413 return false;
414 }
415}
416
417StreamString &
418ClangUserExpression::DwarfOpcodeStream ()
419{
420 if (!m_dwarf_opcodes.get())
421 m_dwarf_opcodes.reset(new StreamString());
422
423 return *m_dwarf_opcodes.get();
424}
Greg Clayton377e0b42010-10-05 00:31:29 +0000425
426
Greg Claytond1719722010-10-05 03:13:51 +0000427lldb::ValueObjectSP
428ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, const char *expr_cstr)
Greg Clayton377e0b42010-10-05 00:31:29 +0000429{
430 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000431 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000432 ClangUserExpression user_expression (expr_cstr);
433
434 StreamString error_stream;
435
436 if (!user_expression.Parse (error_stream, exe_ctx))
437 {
438 if (error_stream.GetString().empty())
439 error.SetErrorString ("expression failed to parse, unknown error");
440 else
441 error.SetErrorString (error_stream.GetString().c_str());
442 }
443 else
444 {
445 ClangExpressionVariable *expr_result = NULL;
446
447 error_stream.GetString().clear();
448
449 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
450 {
451 if (error_stream.GetString().empty())
452 error.SetErrorString ("expression failed to execute, unknown error");
453 else
454 error.SetErrorString (error_stream.GetString().c_str());
455 }
456 else
457 {
458 // TODO: seems weird to get a pointer to a result object back from
459 // a function. Do we own it? Feels like we do, but from looking at the
460 // code we don't. Might be best to make this a reference and state
461 // explicitly that we don't own it when we get a reference back from
462 // the execute?
463 if (expr_result)
464 {
465 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
466 }
467 else
468 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000469 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000470 }
471 }
472 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000473
Greg Claytond1719722010-10-05 03:13:51 +0000474 if (result_valobj_sp.get() == NULL)
475 result_valobj_sp.reset (new ValueObjectConstResult (error));
476
477 return result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000478}