blob: 4e49c4875a22a98fdc3172c1eaacbe5924def41b [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"
24#include "lldb/Expression/ClangExpressionDeclMap.h"
25#include "lldb/Expression/ClangExpressionParser.h"
26#include "lldb/Expression/ClangFunction.h"
27#include "lldb/Expression/ASTResultSynthesizer.h"
28#include "lldb/Expression/ClangUserExpression.h"
29#include "lldb/Host/Host.h"
30#include "lldb/Target/ExecutionContext.h"
31#include "lldb/Target/Target.h"
32
33using namespace lldb_private;
34
35ClangUserExpression::ClangUserExpression (const char *expr) :
36 m_expr_text(expr),
37 m_jit_addr(LLDB_INVALID_ADDRESS)
38{
39 StreamString m_transformed_stream;
40
41 m_transformed_stream.Printf("extern \"C\" void %s(void *___clang_arg) { %s; }\n",
42 FunctionName(),
43 m_expr_text.c_str());
44
45 m_transformed_text = m_transformed_stream.GetData();
46}
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
58bool
59ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx)
60{
61 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
62
63 ////////////////////////////////////
64 // Set up the target and compiler
65 //
66
67 Target *target = exe_ctx.target;
68
69 if (!target)
70 {
71 error_stream.PutCString ("error: invalid target\n");
72 return false;
73 }
74
75 ConstString target_triple;
76
77 target->GetTargetTriple (target_triple);
78
79 if (!target_triple)
80 target_triple = Host::GetTargetTriple ();
81
82 if (!target_triple)
83 {
84 error_stream.PutCString ("error: invalid target triple\n");
85 return false;
86 }
87
88 //////////////////////////
89 // Parse the expression
90 //
91
92 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
93
94 ClangExpressionParser parser(target_triple.GetCString(), *this);
95
96 unsigned num_errors = parser.Parse (error_stream);
97
98 if (num_errors)
99 {
100 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
101 return false;
102 }
103
104 ///////////////////////////////////////////////
105 // Convert the output of the parser to DWARF
106 //
107
108 m_dwarf_opcodes.reset(new StreamString);
109 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
110 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
111
112 m_local_variables.reset(new ClangExpressionVariableStore());
113
114 Error dwarf_error = parser.MakeDWARF ();
115
116 if (dwarf_error.Success())
117 {
118 if (log)
119 log->Printf("Code can be interpreted.");
120
121 return true;
122 }
123
124 //////////////////////////////////
125 // JIT the output of the parser
126 //
127
128 m_dwarf_opcodes.reset();
129
Sean Callanan830a9032010-08-27 23:31:21 +0000130 lldb::addr_t jit_end;
131
132 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000133
134 if (jit_error.Success())
135 {
136 if (log)
137 {
138 log->Printf("Code can be run in the target.");
139
140 StreamString disassembly_stream;
141
142 Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx);
143
144 if (!err.Success())
145 {
146 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
147 }
148 else
149 {
150 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
151 }
152 }
153
154 return true;
155 }
156 else
157 {
158 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
159 return false;
160 }
161}
162
163bool
164ClangUserExpression::Execute (Stream &error_stream,
165 ExecutionContext &exe_ctx,
166 ClangExpressionVariable *&result)
167{
168 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
169
170 if (m_dwarf_opcodes.get())
171 {
172 // TODO execute the JITted opcodes
173
174 error_stream.Printf("We don't currently support executing DWARF expressions");
175
176 return false;
177 }
178 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
179 {
180 lldb::addr_t struct_address;
181
182 Error materialize_error;
183
184 if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error))
185 {
186 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString("unknown error"));
187 return false;
188 }
189
190 if (log)
191 {
192 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
193 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
194
195 StreamString args;
196
197 Error dump_error;
198
Sean Callanane8a59a82010-09-13 21:34:21 +0000199 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000200 {
Sean Callanane8a59a82010-09-13 21:34:21 +0000201 if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error))
202 {
203 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
204 }
205 else
206 {
207 log->Printf("Structure contents:\n%s", args.GetData());
208 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000209 }
210 }
211
212 ClangFunction::ExecutionResults execution_result =
213 ClangFunction::ExecuteFunction (exe_ctx, m_jit_addr, struct_address, true, true, 10000, error_stream);
214
215 if (execution_result != ClangFunction::eExecutionCompleted)
216 {
217 const char *result_name;
218
219 switch (execution_result)
220 {
221 case ClangFunction::eExecutionCompleted:
222 result_name = "eExecutionCompleted";
223 break;
224 case ClangFunction::eExecutionDiscarded:
225 result_name = "eExecutionDiscarded";
226 break;
227 case ClangFunction::eExecutionInterrupted:
228 result_name = "eExecutionInterrupted";
229 break;
230 case ClangFunction::eExecutionSetupError:
231 result_name = "eExecutionSetupError";
232 break;
233 case ClangFunction::eExecutionTimedOut:
234 result_name = "eExecutionTimedOut";
235 break;
236 }
237
238 error_stream.Printf ("Couldn't execute function; result was %s\n", result_name);
239 return false;
240 }
241
242 Error expr_error;
243
244 if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error))
245 {
246 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
247 return false;
248 }
249
250 return true;
251 }
252 else
253 {
254 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present");
255 return false;
256 }
257}
258
259StreamString &
260ClangUserExpression::DwarfOpcodeStream ()
261{
262 if (!m_dwarf_opcodes.get())
263 m_dwarf_opcodes.reset(new StreamString());
264
265 return *m_dwarf_opcodes.get();
266}