blob: c1eeb24e27390a08cc68c7d90ba1dfbd93bf5d54 [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 Callanan65dafa82010-08-27 01:01:44 +000075bool
76ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
77{
78 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
79
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000080 ScanContext(exe_ctx);
81
82 StreamString m_transformed_stream;
83
84 ////////////////////////////////////
85 // Generate the expression
86 //
87
88 if (m_cplusplus)
89 {
90 m_transformed_stream.Printf("void \n"
91 "___clang_class::%s(void *___clang_arg) \n"
92 "{ \n"
93 " %s; \n"
94 "} \n",
95 FunctionName(),
96 m_expr_text.c_str());
97
98 m_needs_object_ptr = true;
99 }
100 else
101 {
102 m_transformed_stream.Printf("void \n"
103 "%s(void *___clang_arg) \n"
104 "{ \n"
105 " %s; \n"
106 "} \n",
107 FunctionName(),
108 m_expr_text.c_str());
109 }
110
111 m_transformed_text = m_transformed_stream.GetData();
112
113
114 if (log)
115 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
116
Sean Callanan65dafa82010-08-27 01:01:44 +0000117 ////////////////////////////////////
118 // Set up the target and compiler
119 //
120
121 Target *target = exe_ctx.target;
122
123 if (!target)
124 {
125 error_stream.PutCString ("error: invalid target\n");
126 return false;
127 }
128
129 ConstString target_triple;
130
131 target->GetTargetTriple (target_triple);
132
133 if (!target_triple)
134 target_triple = Host::GetTargetTriple ();
135
136 if (!target_triple)
137 {
138 error_stream.PutCString ("error: invalid target triple\n");
139 return false;
140 }
141
142 //////////////////////////
143 // Parse the expression
144 //
145
146 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
147
148 ClangExpressionParser parser(target_triple.GetCString(), *this);
149
150 unsigned num_errors = parser.Parse (error_stream);
151
152 if (num_errors)
153 {
154 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
155 return false;
156 }
157
158 ///////////////////////////////////////////////
159 // Convert the output of the parser to DWARF
160 //
161
162 m_dwarf_opcodes.reset(new StreamString);
163 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
164 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
165
166 m_local_variables.reset(new ClangExpressionVariableStore());
167
168 Error dwarf_error = parser.MakeDWARF ();
169
170 if (dwarf_error.Success())
171 {
172 if (log)
173 log->Printf("Code can be interpreted.");
174
175 return true;
176 }
177
178 //////////////////////////////////
179 // JIT the output of the parser
180 //
181
182 m_dwarf_opcodes.reset();
183
Sean Callanan830a9032010-08-27 23:31:21 +0000184 lldb::addr_t jit_end;
185
186 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000187
188 if (jit_error.Success())
189 {
190 if (log)
191 {
192 log->Printf("Code can be run in the target.");
193
194 StreamString disassembly_stream;
195
196 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
197
198 if (!err.Success())
199 {
200 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
201 }
202 else
203 {
204 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
205 }
206 }
207
208 return true;
209 }
210 else
211 {
212 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
213 return false;
214 }
215}
216
217bool
Jim Inghamd1686902010-10-14 23:45:03 +0000218ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
219 ExecutionContext &exe_ctx,
220 lldb::addr_t &struct_address,
221 lldb::addr_t object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000222{
223 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
224
Jim Inghamd1686902010-10-14 23:45:03 +0000225 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000226 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000227
228 Error materialize_error;
229
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000230
231 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
232 {
233 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
234 return false;
235 }
236
Sean Callanan65dafa82010-08-27 01:01:44 +0000237 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
238 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000239 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000240 return false;
241 }
242
243 if (log)
244 {
245 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000246
247 if (m_needs_object_ptr)
248 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
249
Sean Callanan65dafa82010-08-27 01:01:44 +0000250 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
251
252 StreamString args;
253
254 Error dump_error;
255
Sean Callanane8a59a82010-09-13 21:34:21 +0000256 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000257 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000258 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
259 {
260 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
261 }
262 else
263 {
264 log->Printf("Structure contents:\n%s", args.GetData());
265 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000266 }
267 }
Jim Inghamd1686902010-10-14 23:45:03 +0000268 }
269 return true;
270}
271
272ThreadPlan *
273ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
274 ExecutionContext &exe_ctx)
275{
276 lldb::addr_t struct_address;
277
278 lldb::addr_t object_ptr = NULL;
279
280 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
281
282 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
283 m_jit_addr,
284 struct_address,
285 error_stream,
286 true,
287 true,
288 (m_needs_object_ptr ? &object_ptr : NULL));
289}
290
291bool
292ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
293 ExecutionContext &exe_ctx,
294 ClangExpressionVariable *&result)
295{
296 Error expr_error;
297
298 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
299 {
300 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
301 return false;
302 }
303 return true;
304}
305
306bool
307ClangUserExpression::Execute (Stream &error_stream,
308 ExecutionContext &exe_ctx,
309 ClangExpressionVariable *&result)
310{
311 if (m_dwarf_opcodes.get())
312 {
313 // TODO execute the JITted opcodes
314
315 error_stream.Printf("We don't currently support executing DWARF expressions");
316
317 return false;
318 }
319 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
320 {
321 lldb::addr_t struct_address;
322
323 lldb::addr_t object_ptr = NULL;
324
325 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000326
327 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000328 ClangFunction::ExecuteFunction (exe_ctx,
329 m_jit_addr,
330 struct_address,
331 true,
332 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000333 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000334 error_stream,
335 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000336
337 if (execution_result != ClangFunction::eExecutionCompleted)
338 {
339 const char *result_name;
340
341 switch (execution_result)
342 {
343 case ClangFunction::eExecutionCompleted:
344 result_name = "eExecutionCompleted";
345 break;
346 case ClangFunction::eExecutionDiscarded:
347 result_name = "eExecutionDiscarded";
348 break;
349 case ClangFunction::eExecutionInterrupted:
350 result_name = "eExecutionInterrupted";
351 break;
352 case ClangFunction::eExecutionSetupError:
353 result_name = "eExecutionSetupError";
354 break;
355 case ClangFunction::eExecutionTimedOut:
356 result_name = "eExecutionTimedOut";
357 break;
358 }
359
360 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
361 return false;
362 }
363
Jim Inghamd1686902010-10-14 23:45:03 +0000364 return FinalizeJITExecution (error_stream, exe_ctx, result);
Sean Callanan65dafa82010-08-27 01:01:44 +0000365 }
366 else
367 {
368 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
369 return false;
370 }
371}
372
373StreamString &
374ClangUserExpression::DwarfOpcodeStream ()
375{
376 if (!m_dwarf_opcodes.get())
377 m_dwarf_opcodes.reset(new StreamString());
378
379 return *m_dwarf_opcodes.get();
380}
Greg Clayton377e0b42010-10-05 00:31:29 +0000381
382
Greg Claytond1719722010-10-05 03:13:51 +0000383lldb::ValueObjectSP
384ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, const char *expr_cstr)
Greg Clayton377e0b42010-10-05 00:31:29 +0000385{
386 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000387 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000388 ClangUserExpression user_expression (expr_cstr);
389
390 StreamString error_stream;
391
392 if (!user_expression.Parse (error_stream, exe_ctx))
393 {
394 if (error_stream.GetString().empty())
395 error.SetErrorString ("expression failed to parse, unknown error");
396 else
397 error.SetErrorString (error_stream.GetString().c_str());
398 }
399 else
400 {
401 ClangExpressionVariable *expr_result = NULL;
402
403 error_stream.GetString().clear();
404
405 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
406 {
407 if (error_stream.GetString().empty())
408 error.SetErrorString ("expression failed to execute, unknown error");
409 else
410 error.SetErrorString (error_stream.GetString().c_str());
411 }
412 else
413 {
414 // TODO: seems weird to get a pointer to a result object back from
415 // a function. Do we own it? Feels like we do, but from looking at the
416 // code we don't. Might be best to make this a reference and state
417 // explicitly that we don't own it when we get a reference back from
418 // the execute?
419 if (expr_result)
420 {
421 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
422 }
423 else
424 {
425 error.SetErrorString ("NULL expression result");
426 }
427 }
428 }
Greg Claytond1719722010-10-05 03:13:51 +0000429 if (result_valobj_sp.get() == NULL)
430 result_valobj_sp.reset (new ValueObjectConstResult (error));
431
432 return result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000433
434}