blob: 8669666101969bc1ca2cfa57f5d6e46f0fd2e1a9 [file] [log] [blame]
Sean Callanan1a8d4092010-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 Claytonb71f3842010-10-05 03:13:51 +000024#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan1a8d4092010-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 Callananfc55f5d2010-09-21 00:44:12 +000031#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000033#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000034#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000035#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000036#include "lldb/Target/ThreadPlan.h"
37#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000038
39using namespace lldb_private;
40
Sean Callanan322f5292010-10-29 00:29:03 +000041ClangUserExpression::ClangUserExpression (const char *expr,
42 const char *expr_prefix) :
Sean Callanan1a8d4092010-08-27 01:01:44 +000043 m_expr_text(expr),
Johnny Chendabefd02010-10-29 20:19:44 +000044 m_expr_prefix(expr_prefix ? expr_prefix : ""),
Sean Callananfc55f5d2010-09-21 00:44:12 +000045 m_transformed_text(),
46 m_jit_addr(LLDB_INVALID_ADDRESS),
47 m_cplusplus(false),
48 m_objectivec(false),
Sean Callananf7c3e272010-11-19 02:52:21 +000049 m_needs_object_ptr(false),
Sean Callanan3670ba52010-12-01 21:35:54 +000050 m_const_object(false),
Sean Callananf7c3e272010-11-19 02:52:21 +000051 m_desired_type(NULL, NULL)
Sean Callanan1a8d4092010-08-27 01:01:44 +000052{
Sean Callanan1a8d4092010-08-27 01:01:44 +000053}
54
Sean Callanane71d5532010-08-27 23:31:21 +000055ClangUserExpression::~ClangUserExpression ()
56{
57}
58
Sean Callanan1a8d4092010-08-27 01:01:44 +000059clang::ASTConsumer *
60ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
61{
Sean Callananf7c3e272010-11-19 02:52:21 +000062 return new ASTResultSynthesizer(passthrough,
63 m_desired_type);
Sean Callanan1a8d4092010-08-27 01:01:44 +000064}
65
Sean Callananfc55f5d2010-09-21 00:44:12 +000066void
67ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
68{
69 if (!exe_ctx.frame)
70 return;
71
72 VariableList *vars = exe_ctx.frame->GetVariableList(false);
73
74 if (!vars)
75 return;
76
Sean Callanan3670ba52010-12-01 21:35:54 +000077 lldb::VariableSP this_var(vars->FindVariable(ConstString("this")));
78 lldb::VariableSP self_var(vars->FindVariable(ConstString("self")));
79
80 if (this_var.get())
81 {
82 Type *this_type = this_var->GetType();
83
84 lldb::clang_type_t pointer_target_type;
85
86 if (ClangASTContext::IsPointerType(this_type->GetClangType(),
87 &pointer_target_type))
88 {
89 TypeFromUser target_ast_type(pointer_target_type, this_type->GetClangAST());
90
91 if (target_ast_type.IsDefined())
92 m_cplusplus = true;
93
94 if (target_ast_type.IsConst())
95 m_const_object = true;
96 }
97 }
98 else if (self_var.get())
99 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000100 m_objectivec = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000101 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000102}
103
Sean Callanancf5498f2010-10-22 23:25:16 +0000104// This is a really nasty hack, meant to fix Objective-C expressions of the form
105// (int)[myArray count]. Right now, because the type information for count is
106// not available, [myArray count] returns id, which can't be directly cast to
107// int without causing a clang error.
108static void
109ApplyObjcCastHack(std::string &expr)
110{
111#define OBJC_CAST_HACK_FROM "(int)["
112#define OBJC_CAST_HACK_TO "(int)(long long)["
113
114 size_t from_offset;
115
116 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
117 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
118
119#undef OBJC_CAST_HACK_TO
120#undef OBJC_CAST_HACK_FROM
121}
122
Sean Callanan64186e72010-10-24 20:45:49 +0000123// Another hack, meant to allow use of unichar despite it not being available in
124// the type information. Although we could special-case it in type lookup,
125// hopefully we'll figure out a way to #include the same environment as is
126// present in the original source file rather than try to hack specific type
127// definitions in as needed.
128static void
129ApplyUnicharHack(std::string &expr)
130{
131#define UNICHAR_HACK_FROM "unichar"
132#define UNICHAR_HACK_TO "unsigned short"
133
134 size_t from_offset;
135
136 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
137 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
138
139#undef UNICHAR_HACK_TO
140#undef UNICHAR_HACK_FROM
141}
142
Sean Callanancf5498f2010-10-22 23:25:16 +0000143bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000144ClangUserExpression::Parse (Stream &error_stream,
145 ExecutionContext &exe_ctx,
146 TypeFromUser desired_type)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000147{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000148 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000149
Sean Callananfc55f5d2010-09-21 00:44:12 +0000150 ScanContext(exe_ctx);
151
152 StreamString m_transformed_stream;
153
154 ////////////////////////////////////
155 // Generate the expression
156 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000157
158 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000159 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000160
161 if (m_cplusplus)
162 {
Sean Callanan322f5292010-10-29 00:29:03 +0000163 m_transformed_stream.Printf("%s \n"
164 "typedef unsigned short unichar; \n"
Greg Clayton73b472d2010-10-27 03:32:59 +0000165 "void \n"
Sean Callanan3670ba52010-12-01 21:35:54 +0000166 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
Sean Callananfc55f5d2010-09-21 00:44:12 +0000167 "{ \n"
168 " %s; \n"
169 "} \n",
Sean Callanan322f5292010-10-29 00:29:03 +0000170 m_expr_prefix.c_str(),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000171 FunctionName(),
Sean Callanan3670ba52010-12-01 21:35:54 +0000172 (m_const_object ? "const" : ""),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000173 m_expr_text.c_str());
174
175 m_needs_object_ptr = true;
176 }
Sean Callanan17827832010-12-13 22:46:15 +0000177 else if(m_objectivec)
178 {
179 const char *function_name = FunctionName();
180
181 m_transformed_stream.Printf("%s \n"
182 "@interface $__lldb_objc_class ($__lldb_category) \n"
183 "-(void)%s:(void *)$__lldb_arg; \n"
184 "@end \n"
185 "@implementation $__lldb_objc_class ($__lldb_category) \n"
186 "-(void)%s:(void *)$__lldb_arg \n"
187 "{ \n"
188 " %s; \n"
189 "} \n"
190 "@end \n",
191 m_expr_prefix.c_str(),
192 function_name,
193 function_name,
194 m_expr_text.c_str());
195
196 m_needs_object_ptr = true;
197 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000198 else
199 {
Sean Callanan322f5292010-10-29 00:29:03 +0000200 m_transformed_stream.Printf("%s \n"
201 "typedef unsigned short unichar;\n"
Greg Clayton73b472d2010-10-27 03:32:59 +0000202 "void \n"
Sean Callanancf5498f2010-10-22 23:25:16 +0000203 "%s(void *$__lldb_arg) \n"
Sean Callananfc55f5d2010-09-21 00:44:12 +0000204 "{ \n"
205 " %s; \n"
206 "} \n",
Sean Callanan322f5292010-10-29 00:29:03 +0000207 m_expr_prefix.c_str(),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000208 FunctionName(),
209 m_expr_text.c_str());
210 }
211
212 m_transformed_text = m_transformed_stream.GetData();
213
214
215 if (log)
216 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
217
Sean Callanan1a8d4092010-08-27 01:01:44 +0000218 ////////////////////////////////////
219 // Set up the target and compiler
220 //
221
222 Target *target = exe_ctx.target;
223
224 if (!target)
225 {
226 error_stream.PutCString ("error: invalid target\n");
227 return false;
228 }
229
230 ConstString target_triple;
231
232 target->GetTargetTriple (target_triple);
233
234 if (!target_triple)
235 target_triple = Host::GetTargetTriple ();
236
237 if (!target_triple)
238 {
239 error_stream.PutCString ("error: invalid target triple\n");
240 return false;
241 }
242
243 //////////////////////////
244 // Parse the expression
245 //
246
Sean Callananf7c3e272010-11-19 02:52:21 +0000247 m_desired_type = desired_type;
248
Sean Callanan979f74d2010-12-03 01:38:59 +0000249 m_expr_decl_map.reset(new ClangExpressionDeclMap());
250
251 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000252
253 ClangExpressionParser parser(target_triple.GetCString(), *this);
254
255 unsigned num_errors = parser.Parse (error_stream);
256
257 if (num_errors)
258 {
259 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000260
261 m_expr_decl_map->DidParse();
262
Sean Callanan1a8d4092010-08-27 01:01:44 +0000263 return false;
264 }
265
266 ///////////////////////////////////////////////
267 // Convert the output of the parser to DWARF
268 //
269
270 m_dwarf_opcodes.reset(new StreamString);
271 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
272 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
273
274 m_local_variables.reset(new ClangExpressionVariableStore());
275
276 Error dwarf_error = parser.MakeDWARF ();
277
278 if (dwarf_error.Success())
279 {
280 if (log)
281 log->Printf("Code can be interpreted.");
282
Sean Callanan979f74d2010-12-03 01:38:59 +0000283 m_expr_decl_map->DidParse();
284
Sean Callanan1a8d4092010-08-27 01:01:44 +0000285 return true;
286 }
287
288 //////////////////////////////////
289 // JIT the output of the parser
290 //
291
292 m_dwarf_opcodes.reset();
293
Sean Callanane71d5532010-08-27 23:31:21 +0000294 lldb::addr_t jit_end;
295
296 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000297
Sean Callanan979f74d2010-12-03 01:38:59 +0000298 m_expr_decl_map->DidParse();
299
Sean Callanan1a8d4092010-08-27 01:01:44 +0000300 if (jit_error.Success())
301 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000302 return true;
303 }
304 else
305 {
306 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
307 return false;
308 }
309}
310
311bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000312ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000313 ExecutionContext &exe_ctx,
314 lldb::addr_t &struct_address,
315 lldb::addr_t &object_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000316{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000317 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000318
Jim Ingham36f3b362010-10-14 23:45:03 +0000319 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000320 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000321 Error materialize_error;
322
Sean Callanan17827832010-12-13 22:46:15 +0000323 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000324 {
Sean Callanan17827832010-12-13 22:46:15 +0000325 ConstString object_name;
326
327 if (m_cplusplus)
328 {
329 object_name.SetCString("this");
330 }
331 else if (m_objectivec)
332 {
333 object_name.SetCString("self");
334 }
335 else
336 {
337 error_stream.Printf("Need object pointer but don't know the language\n");
338 return false;
339 }
340
341 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
342 {
343 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
344 return false;
345 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000346 }
347
Sean Callanan979f74d2010-12-03 01:38:59 +0000348 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000349 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000350 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000351 return false;
352 }
353
354 if (log)
355 {
Sean Callanana162eba2010-12-07 22:55:01 +0000356 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000357
358 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000359
360 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000361 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000362
Sean Callananc673a6e2010-12-07 10:00:20 +0000363 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000364
365 StreamString args;
366
367 Error dump_error;
368
Sean Callanan9e6ed532010-09-13 21:34:21 +0000369 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000370 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000371 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000372 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000373 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000374 }
375 else
376 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000377 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000378 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000379 }
380 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000381 }
382 return true;
383}
384
385ThreadPlan *
386ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
387 ExecutionContext &exe_ctx)
388{
389 lldb::addr_t struct_address;
390
391 lldb::addr_t object_ptr = NULL;
Sean Callanan17827832010-12-13 22:46:15 +0000392 lldb::addr_t cmd_ptr = NULL;
Jim Ingham36f3b362010-10-14 23:45:03 +0000393
394 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
395
Jim Inghamf48169b2010-11-30 02:22:11 +0000396 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
397 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000398 // forcing unwind_on_error to be true here, in practical terms that can't happen.
399
Jim Ingham36f3b362010-10-14 23:45:03 +0000400 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000401 m_jit_addr,
402 struct_address,
403 error_stream,
404 true,
405 true,
Sean Callanan17827832010-12-13 22:46:15 +0000406 (m_needs_object_ptr ? &object_ptr : NULL),
407 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000408}
409
410bool
411ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
412 ExecutionContext &exe_ctx,
413 ClangExpressionVariable *&result)
414{
415 Error expr_error;
416
Sean Callananc673a6e2010-12-07 10:00:20 +0000417 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
418
419 if (log)
420 {
Sean Callanana162eba2010-12-07 22:55:01 +0000421 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000422
423 StreamString args;
424
425 Error dump_error;
426
427 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
428 {
429 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
430 }
431 else
432 {
433 log->Printf(" Structure contents:\n%s", args.GetData());
434 }
435 }
436
Sean Callanan979f74d2010-12-03 01:38:59 +0000437 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000438 {
439 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
440 return false;
441 }
442 return true;
443}
444
Jim Inghamf48169b2010-11-30 02:22:11 +0000445Process::ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000446ClangUserExpression::Execute (Stream &error_stream,
447 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000448 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000449 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Jim Ingham36f3b362010-10-14 23:45:03 +0000450 ClangExpressionVariable *&result)
451{
Sean Callananc673a6e2010-12-07 10:00:20 +0000452 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
453
Jim Ingham36f3b362010-10-14 23:45:03 +0000454 if (m_dwarf_opcodes.get())
455 {
456 // TODO execute the JITted opcodes
457
458 error_stream.Printf("We don't currently support executing DWARF expressions");
459
Jim Inghamf48169b2010-11-30 02:22:11 +0000460 return Process::eExecutionSetupError;
Jim Ingham36f3b362010-10-14 23:45:03 +0000461 }
462 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
463 {
464 lldb::addr_t struct_address;
465
466 lldb::addr_t object_ptr = NULL;
Sean Callanan17827832010-12-13 22:46:15 +0000467 lldb::addr_t cmd_ptr = NULL;
Jim Ingham36f3b362010-10-14 23:45:03 +0000468
Sean Callanan17827832010-12-13 22:46:15 +0000469 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr))
470 return Process::eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000471
Jim Ingham399f1ca2010-11-05 19:25:48 +0000472 const bool stop_others = true;
473 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000474
Jim Inghamf48169b2010-11-30 02:22:11 +0000475 Address wrapper_address (NULL, m_jit_addr);
Sean Callanan17827832010-12-13 22:46:15 +0000476 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread),
477 wrapper_address,
478 struct_address,
479 stop_others,
480 discard_on_error,
481 (m_needs_object_ptr ? &object_ptr : NULL),
482 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
483 shared_ptr_to_me));
484
Jim Inghamf48169b2010-11-30 02:22:11 +0000485 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
486 return Process::eExecutionSetupError;
487
488 call_plan_sp->SetPrivate(true);
489
490 uint32_t single_thread_timeout_usec = 10000000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000491
492 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000493 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000494
Jim Inghamf48169b2010-11-30 02:22:11 +0000495 Process::ExecutionResults execution_result =
496 exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
497 single_thread_timeout_usec, error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000498
499 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000500 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000501
502 if (execution_result == Process::eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000503 {
Jim Inghamf48169b2010-11-30 02:22:11 +0000504 if (discard_on_error)
505 error_stream.Printf ("Expression execution was interrupted. The process has been returned to the state before execution.");
506 else
507 error_stream.Printf ("Expression execution was interrupted. The process has been left at the point where it was interrupted.");
508
509 return execution_result;
510 }
511 else if (execution_result != Process::eExecutionCompleted)
512 {
513 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
514 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000515 }
516
Jim Inghamf48169b2010-11-30 02:22:11 +0000517 if (FinalizeJITExecution (error_stream, exe_ctx, result))
518 return Process::eExecutionCompleted;
519 else
520 return Process::eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000521 }
522 else
523 {
Johnny Chenfec456d2010-11-10 19:02:11 +0000524 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Jim Inghamf48169b2010-11-30 02:22:11 +0000525 return Process::eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000526 }
527}
528
529StreamString &
530ClangUserExpression::DwarfOpcodeStream ()
531{
532 if (!m_dwarf_opcodes.get())
533 m_dwarf_opcodes.reset(new StreamString());
534
535 return *m_dwarf_opcodes.get();
536}
Greg Clayton0184f012010-10-05 00:31:29 +0000537
Jim Inghamf48169b2010-11-30 02:22:11 +0000538Process::ExecutionResults
Sean Callanan322f5292010-10-29 00:29:03 +0000539ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000540 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000541 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000542 const char *expr_prefix,
543 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000544{
Sean Callanana162eba2010-12-07 22:55:01 +0000545 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
546
Greg Clayton0184f012010-10-05 00:31:29 +0000547 Error error;
Jim Inghamf48169b2010-11-30 02:22:11 +0000548 Process::ExecutionResults execution_results = Process::eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000549
550 if (exe_ctx.process == NULL)
Jim Inghamf48169b2010-11-30 02:22:11 +0000551 {
552 error.SetErrorString ("Must have a process to evaluate expressions.");
553
554 result_valobj_sp.reset (new ValueObjectConstResult (error));
555 return Process::eExecutionSetupError;
556 }
557
Greg Clayton8f343b02010-11-04 01:54:29 +0000558 if (!exe_ctx.process->GetDynamicCheckers())
559 {
Sean Callanana162eba2010-12-07 22:55:01 +0000560 if (log)
561 log->Printf("== [ClangUserExpression::Evaluate] Installing dynamic checkers ==");
562
Greg Clayton8f343b02010-11-04 01:54:29 +0000563 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
564
565 StreamString install_errors;
566
567 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callanan2a396522010-11-05 00:57:06 +0000568 {
569 if (install_errors.GetString().empty())
570 error.SetErrorString ("couldn't install checkers, unknown error");
571 else
572 error.SetErrorString (install_errors.GetString().c_str());
573
574 result_valobj_sp.reset (new ValueObjectConstResult (error));
Jim Inghamf48169b2010-11-30 02:22:11 +0000575 return Process::eExecutionSetupError;
Sean Callanan2a396522010-11-05 00:57:06 +0000576 }
577
Greg Clayton8f343b02010-11-04 01:54:29 +0000578 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
Sean Callanana162eba2010-12-07 22:55:01 +0000579
580 if (log)
581 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Greg Clayton8f343b02010-11-04 01:54:29 +0000582 }
583
Jim Inghamf48169b2010-11-30 02:22:11 +0000584 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
585
Greg Clayton0184f012010-10-05 00:31:29 +0000586 StreamString error_stream;
587
Sean Callanana162eba2010-12-07 22:55:01 +0000588 if (log)
589 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
590
Jim Inghamf48169b2010-11-30 02:22:11 +0000591 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL)))
Greg Clayton0184f012010-10-05 00:31:29 +0000592 {
593 if (error_stream.GetString().empty())
594 error.SetErrorString ("expression failed to parse, unknown error");
595 else
596 error.SetErrorString (error_stream.GetString().c_str());
597 }
598 else
599 {
600 ClangExpressionVariable *expr_result = NULL;
601
602 error_stream.GetString().clear();
Sean Callanana162eba2010-12-07 22:55:01 +0000603
604 if (log)
605 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
Greg Clayton0184f012010-10-05 00:31:29 +0000606
Jim Inghamf48169b2010-11-30 02:22:11 +0000607 execution_results = user_expression_sp->Execute (error_stream,
608 exe_ctx,
609 discard_on_error,
610 user_expression_sp,
611 expr_result);
612 if (execution_results != Process::eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000613 {
Sean Callanana162eba2010-12-07 22:55:01 +0000614 if (log)
615 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
616
Greg Clayton0184f012010-10-05 00:31:29 +0000617 if (error_stream.GetString().empty())
618 error.SetErrorString ("expression failed to execute, unknown error");
619 else
620 error.SetErrorString (error_stream.GetString().c_str());
621 }
622 else
623 {
624 // TODO: seems weird to get a pointer to a result object back from
625 // a function. Do we own it? Feels like we do, but from looking at the
626 // code we don't. Might be best to make this a reference and state
627 // explicitly that we don't own it when we get a reference back from
628 // the execute?
629 if (expr_result)
630 {
631 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
Sean Callanana162eba2010-12-07 22:55:01 +0000632
633 if (log)
634 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString(exe_ctx.GetBestExecutionContextScope()));
Greg Clayton0184f012010-10-05 00:31:29 +0000635 }
636 else
637 {
Sean Callanana162eba2010-12-07 22:55:01 +0000638 if (log)
639 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
640
Sean Callananc57f64d2010-10-19 20:15:00 +0000641 error.SetErrorString ("Expression did not return a result");
Greg Clayton0184f012010-10-05 00:31:29 +0000642 }
643 }
644 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000645
Greg Claytonb71f3842010-10-05 03:13:51 +0000646 if (result_valobj_sp.get() == NULL)
647 result_valobj_sp.reset (new ValueObjectConstResult (error));
648
Jim Inghamf48169b2010-11-30 02:22:11 +0000649 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000650}