blob: 4aabf521511ca84163d1730b3d669b74f6ccba28 [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
218ClangUserExpression::Execute (Stream &error_stream,
219 ExecutionContext &exe_ctx,
220 ClangExpressionVariable *&result)
221{
222 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
223
224 if (m_dwarf_opcodes.get())
225 {
226 // TODO execute the JITted opcodes
227
228 error_stream.Printf("We don't currently support executing DWARF expressions");
229
230 return false;
231 }
232 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
233 {
234 lldb::addr_t struct_address;
235
236 Error materialize_error;
237
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000238 lldb::addr_t object_ptr = NULL;
239
240 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error)))
241 {
242 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
243 return false;
244 }
245
Sean Callanan65dafa82010-08-27 01:01:44 +0000246 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
247 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000248 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000249 return false;
250 }
251
252 if (log)
253 {
254 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000255
256 if (m_needs_object_ptr)
257 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
258
Sean Callanan65dafa82010-08-27 01:01:44 +0000259 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
260
261 StreamString args;
262
263 Error dump_error;
264
Sean Callanane8a59a82010-09-13 21:34:21 +0000265 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000266 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000267 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
268 {
269 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
270 }
271 else
272 {
273 log->Printf("Structure contents:\n%s", args.GetData());
274 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000275 }
276 }
277
278 ClangFunction::ExecutionResults execution_result =
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000279 ClangFunction::ExecuteFunction (exe_ctx,
280 m_jit_addr,
281 struct_address,
282 true,
283 true,
Sean Callanan0027cec2010-10-07 18:17:31 +0000284 10000000,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000285 error_stream,
286 (m_needs_object_ptr ? &object_ptr : NULL));
Sean Callanan65dafa82010-08-27 01:01:44 +0000287
288 if (execution_result != ClangFunction::eExecutionCompleted)
289 {
290 const char *result_name;
291
292 switch (execution_result)
293 {
294 case ClangFunction::eExecutionCompleted:
295 result_name = "eExecutionCompleted";
296 break;
297 case ClangFunction::eExecutionDiscarded:
298 result_name = "eExecutionDiscarded";
299 break;
300 case ClangFunction::eExecutionInterrupted:
301 result_name = "eExecutionInterrupted";
302 break;
303 case ClangFunction::eExecutionSetupError:
304 result_name = "eExecutionSetupError";
305 break;
306 case ClangFunction::eExecutionTimedOut:
307 result_name = "eExecutionTimedOut";
308 break;
309 }
310
311 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
312 return false;
313 }
314
315 Error expr_error;
316
317 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
318 {
319 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
320 return false;
321 }
322
323 return true;
324 }
325 else
326 {
327 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
328 return false;
329 }
330}
331
332StreamString &
333ClangUserExpression::DwarfOpcodeStream ()
334{
335 if (!m_dwarf_opcodes.get())
336 m_dwarf_opcodes.reset(new StreamString());
337
338 return *m_dwarf_opcodes.get();
339}
Greg Clayton377e0b42010-10-05 00:31:29 +0000340
341
Greg Claytond1719722010-10-05 03:13:51 +0000342lldb::ValueObjectSP
343ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, const char *expr_cstr)
Greg Clayton377e0b42010-10-05 00:31:29 +0000344{
345 Error error;
Greg Claytond1719722010-10-05 03:13:51 +0000346 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000347 ClangUserExpression user_expression (expr_cstr);
348
349 StreamString error_stream;
350
351 if (!user_expression.Parse (error_stream, exe_ctx))
352 {
353 if (error_stream.GetString().empty())
354 error.SetErrorString ("expression failed to parse, unknown error");
355 else
356 error.SetErrorString (error_stream.GetString().c_str());
357 }
358 else
359 {
360 ClangExpressionVariable *expr_result = NULL;
361
362 error_stream.GetString().clear();
363
364 if (!user_expression.Execute (error_stream, exe_ctx, expr_result))
365 {
366 if (error_stream.GetString().empty())
367 error.SetErrorString ("expression failed to execute, unknown error");
368 else
369 error.SetErrorString (error_stream.GetString().c_str());
370 }
371 else
372 {
373 // TODO: seems weird to get a pointer to a result object back from
374 // a function. Do we own it? Feels like we do, but from looking at the
375 // code we don't. Might be best to make this a reference and state
376 // explicitly that we don't own it when we get a reference back from
377 // the execute?
378 if (expr_result)
379 {
380 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
381 }
382 else
383 {
384 error.SetErrorString ("NULL expression result");
385 }
386 }
387 }
Greg Claytond1719722010-10-05 03:13:51 +0000388 if (result_valobj_sp.get() == NULL)
389 result_valobj_sp.reset (new ValueObjectConstResult (error));
390
391 return result_valobj_sp;
Greg Clayton377e0b42010-10-05 00:31:29 +0000392
393}