blob: d2b35bae912aea6c903a611490fe76c357337568 [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
94bool
Sean Callanan65dafa82010-08-27 01:01:44 +000095ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
96{
97 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
98
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000099 ScanContext(exe_ctx);
100
101 StreamString m_transformed_stream;
102
103 ////////////////////////////////////
104 // Generate the expression
105 //
Sean Callanan550f2762010-10-22 23:25:16 +0000106
107 ApplyObjcCastHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000108
109 if (m_cplusplus)
110 {
111 m_transformed_stream.Printf("void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000112 "$__lldb_class::%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000113 "{ \n"
114 " %s; \n"
115 "} \n",
116 FunctionName(),
117 m_expr_text.c_str());
118
119 m_needs_object_ptr = true;
120 }
121 else
122 {
123 m_transformed_stream.Printf("void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000124 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000125 "{ \n"
126 " %s; \n"
127 "} \n",
128 FunctionName(),
129 m_expr_text.c_str());
130 }
131
132 m_transformed_text = m_transformed_stream.GetData();
133
134
135 if (log)
136 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
137
Sean Callanan65dafa82010-08-27 01:01:44 +0000138 ////////////////////////////////////
139 // Set up the target and compiler
140 //
141
142 Target *target = exe_ctx.target;
143
144 if (!target)
145 {
146 error_stream.PutCString ("error: invalid target\n");
147 return false;
148 }
149
150 ConstString target_triple;
151
152 target->GetTargetTriple (target_triple);
153
154 if (!target_triple)
155 target_triple = Host::GetTargetTriple ();
156
157 if (!target_triple)
158 {
159 error_stream.PutCString ("error: invalid target triple\n");
160 return false;
161 }
162
163 //////////////////////////
164 // Parse the expression
165 //
166
167 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
168
169 ClangExpressionParser parser(target_triple.GetCString(), *this);
170
171 unsigned num_errors = parser.Parse (error_stream);
172
173 if (num_errors)
174 {
175 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
176 return false;
177 }
178
179 ///////////////////////////////////////////////
180 // Convert the output of the parser to DWARF
181 //
182
183 m_dwarf_opcodes.reset(new StreamString);
184 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
185 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
186
187 m_local_variables.reset(new ClangExpressionVariableStore());
188
189 Error dwarf_error = parser.MakeDWARF ();
190
191 if (dwarf_error.Success())
192 {
193 if (log)
194 log->Printf("Code can be interpreted.");
195
196 return true;
197 }
198
199 //////////////////////////////////
200 // JIT the output of the parser
201 //
202
203 m_dwarf_opcodes.reset();
204
Sean Callanan830a9032010-08-27 23:31:21 +0000205 lldb::addr_t jit_end;
206
207 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000208
209 if (jit_error.Success())
210 {
211 if (log)
212 {
213 log->Printf("Code can be run in the target.");
214
215 StreamString disassembly_stream;
216
217 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
218
219 if (!err.Success())
220 {
221 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
222 }
223 else
224 {
225 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
226 }
227 }
228
229 return true;
230 }
231 else
232 {
233 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
234 return false;
235 }
236}
237
238bool
Jim Inghamd1686902010-10-14 23:45:03 +0000239ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000240 ExecutionContext &exe_ctx,
241 lldb::addr_t &struct_address,
242 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000243{
244 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
245
Jim Inghamd1686902010-10-14 23:45:03 +0000246 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000247 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000248
249 Error materialize_error;
250
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000251
252 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
253 {
254 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
255 return false;
256 }
257
Sean Callanan65dafa82010-08-27 01:01:44 +0000258 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
259 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000260 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000261 return false;
262 }
263
264 if (log)
265 {
266 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000267
268 if (m_needs_object_ptr)
269 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
270
Sean Callanan65dafa82010-08-27 01:01:44 +0000271 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
272
273 StreamString args;
274
275 Error dump_error;
276
Sean Callanane8a59a82010-09-13 21:34:21 +0000277 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000278 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000279 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
280 {
281 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
282 }
283 else
284 {
285 log->Printf("Structure contents:\n%s", args.GetData());
286 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000287 }
288 }
Jim Inghamd1686902010-10-14 23:45:03 +0000289 }
290 return true;
291}
292
293ThreadPlan *
294ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
295 ExecutionContext &exe_ctx)
296{
297 lldb::addr_t struct_address;
298
299 lldb::addr_t object_ptr = NULL;
300
301 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
302
303 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
304 m_jit_addr,
305 struct_address,
306 error_stream,
307 true,
308 true,
309 (m_needs_object_ptr ? &object_ptr : NULL));
310}
311
312bool
313ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
314 ExecutionContext &exe_ctx,
315 ClangExpressionVariable *&result)
316{
317 Error expr_error;
318
319 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
320 {
321 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
322 return false;
323 }
324 return true;
325}
326
327bool
328ClangUserExpression::Execute (Stream &error_stream,
329 ExecutionContext &exe_ctx,
330 ClangExpressionVariable *&result)
331{
332 if (m_dwarf_opcodes.get())
333 {
334 // TODO execute the JITted opcodes
335
336 error_stream.Printf("We don't currently support executing DWARF expressions");
337
338 return false;
339 }
340 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
341 {
342 lldb::addr_t struct_address;
343
344 lldb::addr_t object_ptr = NULL;
345
346 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000347
348 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000349 ClangFunction::ExecuteFunction (exe_ctx,
350 m_jit_addr,
351 struct_address,
352 true,
353 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000354 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000355 error_stream,
356 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000357
358 if (execution_result != ClangFunction::eExecutionCompleted)
359 {
360 const char *result_name;
361
362 switch (execution_result)
363 {
364 case ClangFunction::eExecutionCompleted:
365 result_name = "eExecutionCompleted";
366 break;
367 case ClangFunction::eExecutionDiscarded:
368 result_name = "eExecutionDiscarded";
369 break;
370 case ClangFunction::eExecutionInterrupted:
371 result_name = "eExecutionInterrupted";
372 break;
373 case ClangFunction::eExecutionSetupError:
374 result_name = "eExecutionSetupError";
375 break;
376 case ClangFunction::eExecutionTimedOut:
377 result_name = "eExecutionTimedOut";
378 break;
379 }
380
381 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
382 return false;
383 }
384
Jim Inghamd1686902010-10-14 23:45:03 +0000385 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000386 }
387 else
388 {
389 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
390 return false;
391 }
392}
393
394StreamString &
395ClangUserExpression::DwarfOpcodeStream ()
396{
397 if (!m_dwarf_opcodes.get())
398 m_dwarf_opcodes.reset(new StreamString());
399
400 return *m_dwarf_opcodes.get();
401}
Greg Clayton377e0b42010-10-05 00:31:29 +0000402
403
Greg Claytond1719722010-10-05 03:13:51 +0000404lldb::ValueObjectSP
405ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, const char *expr_cstr)
Greg Clayton377e0b42010-10-05 00:31:29 +0000406{
407 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000408 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000409 ClangUserExpression user_expression (expr_cstr);
410
411 StreamString error_stream;
412
413 if (!user_expression.Parse (error_stream, exe_ctx))
414 {
415 if (error_stream.GetString().empty())
416 error.SetErrorString ("expression failed to parse, unknown error");
417 else
418 error.SetErrorString (error_stream.GetString().c_str());
419 }
420 else
421 {
422 ClangExpressionVariable *expr_result = NULL;
423
424 error_stream.GetString().clear();
425
426 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
427 {
428 if (error_stream.GetString().empty())
429 error.SetErrorString ("expression failed to execute, unknown error");
430 else
431 error.SetErrorString (error_stream.GetString().c_str());
432 }
433 else
434 {
435 // TODO: seems weird to get a pointer to a result object back from
436 // a function. Do we own it? Feels like we do, but from looking at the
437 // code we don't. Might be best to make this a reference and state
438 // explicitly that we don't own it when we get a reference back from
439 // the execute?
440 if (expr_result)
441 {
442 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
443 }
444 else
445 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000446 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000447 }
448 }
449 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000450
Greg Claytond1719722010-10-05 03:13:51 +0000451 if (result_valobj_sp.get() == NULL)
452 result_valobj_sp.reset (new ValueObjectConstResult (error));
453
454 return result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000455}