blob: 6857634a0a8e4721c7468e9e5f5e0425e05f08d2 [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 }
177 else
178 {
Sean Callanan322f5292010-10-29 00:29:03 +0000179 m_transformed_stream.Printf("%s \n"
180 "typedef unsigned short unichar;\n"
Greg Clayton73b472d2010-10-27 03:32:59 +0000181 "void \n"
Sean Callanancf5498f2010-10-22 23:25:16 +0000182 "%s(void *$__lldb_arg) \n"
Sean Callananfc55f5d2010-09-21 00:44:12 +0000183 "{ \n"
184 " %s; \n"
185 "} \n",
Sean Callanan322f5292010-10-29 00:29:03 +0000186 m_expr_prefix.c_str(),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000187 FunctionName(),
188 m_expr_text.c_str());
189 }
190
191 m_transformed_text = m_transformed_stream.GetData();
192
193
194 if (log)
195 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
196
Sean Callanan1a8d4092010-08-27 01:01:44 +0000197 ////////////////////////////////////
198 // Set up the target and compiler
199 //
200
201 Target *target = exe_ctx.target;
202
203 if (!target)
204 {
205 error_stream.PutCString ("error: invalid target\n");
206 return false;
207 }
208
209 ConstString target_triple;
210
211 target->GetTargetTriple (target_triple);
212
213 if (!target_triple)
214 target_triple = Host::GetTargetTriple ();
215
216 if (!target_triple)
217 {
218 error_stream.PutCString ("error: invalid target triple\n");
219 return false;
220 }
221
222 //////////////////////////
223 // Parse the expression
224 //
225
Sean Callananf7c3e272010-11-19 02:52:21 +0000226 m_desired_type = desired_type;
227
Sean Callanan979f74d2010-12-03 01:38:59 +0000228 m_expr_decl_map.reset(new ClangExpressionDeclMap());
229
230 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000231
232 ClangExpressionParser parser(target_triple.GetCString(), *this);
233
234 unsigned num_errors = parser.Parse (error_stream);
235
236 if (num_errors)
237 {
238 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000239
240 m_expr_decl_map->DidParse();
241
Sean Callanan1a8d4092010-08-27 01:01:44 +0000242 return false;
243 }
244
245 ///////////////////////////////////////////////
246 // Convert the output of the parser to DWARF
247 //
248
249 m_dwarf_opcodes.reset(new StreamString);
250 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
251 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
252
253 m_local_variables.reset(new ClangExpressionVariableStore());
254
255 Error dwarf_error = parser.MakeDWARF ();
256
257 if (dwarf_error.Success())
258 {
259 if (log)
260 log->Printf("Code can be interpreted.");
261
Sean Callanan979f74d2010-12-03 01:38:59 +0000262 m_expr_decl_map->DidParse();
263
Sean Callanan1a8d4092010-08-27 01:01:44 +0000264 return true;
265 }
266
267 //////////////////////////////////
268 // JIT the output of the parser
269 //
270
271 m_dwarf_opcodes.reset();
272
Sean Callanane71d5532010-08-27 23:31:21 +0000273 lldb::addr_t jit_end;
274
275 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000276
Sean Callanan979f74d2010-12-03 01:38:59 +0000277 m_expr_decl_map->DidParse();
278
Sean Callanan1a8d4092010-08-27 01:01:44 +0000279 if (jit_error.Success())
280 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000281 return true;
282 }
283 else
284 {
285 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
286 return false;
287 }
288}
289
290bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000291ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000292 ExecutionContext &exe_ctx,
293 lldb::addr_t &struct_address,
294 lldb::addr_t &object_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000295{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000296 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000297
Jim Ingham36f3b362010-10-14 23:45:03 +0000298 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000299 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000300
301 Error materialize_error;
302
Sean Callananfc55f5d2010-09-21 00:44:12 +0000303
Sean Callanan979f74d2010-12-03 01:38:59 +0000304 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, exe_ctx, materialize_error)))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000305 {
306 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
307 return false;
308 }
309
Sean Callanan979f74d2010-12-03 01:38:59 +0000310 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000311 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000312 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000313 return false;
314 }
315
316 if (log)
317 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000318 log->Printf("-- Materializing for execution --");
319
320 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000321
322 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000323 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000324
Sean Callananc673a6e2010-12-07 10:00:20 +0000325 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000326
327 StreamString args;
328
329 Error dump_error;
330
Sean Callanan9e6ed532010-09-13 21:34:21 +0000331 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000332 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000333 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000334 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000335 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000336 }
337 else
338 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000339 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000340 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000341 }
342 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000343 }
344 return true;
345}
346
347ThreadPlan *
348ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
349 ExecutionContext &exe_ctx)
350{
351 lldb::addr_t struct_address;
352
353 lldb::addr_t object_ptr = NULL;
354
355 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
356
Jim Inghamf48169b2010-11-30 02:22:11 +0000357 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
358 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
359 // forcing unwind_on_error to be true here, in practical terms that can't happen.
Jim Ingham36f3b362010-10-14 23:45:03 +0000360 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000361 m_jit_addr,
362 struct_address,
363 error_stream,
364 true,
365 true,
366 (m_needs_object_ptr ? &object_ptr : NULL));
Jim Ingham36f3b362010-10-14 23:45:03 +0000367}
368
369bool
370ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
371 ExecutionContext &exe_ctx,
372 ClangExpressionVariable *&result)
373{
374 Error expr_error;
375
Sean Callananc673a6e2010-12-07 10:00:20 +0000376 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
377
378 if (log)
379 {
380 log->Printf("-- Dematerializing after execution --");
381
382 StreamString args;
383
384 Error dump_error;
385
386 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
387 {
388 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
389 }
390 else
391 {
392 log->Printf(" Structure contents:\n%s", args.GetData());
393 }
394 }
395
Sean Callanan979f74d2010-12-03 01:38:59 +0000396 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000397 {
398 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
399 return false;
400 }
401 return true;
402}
403
Jim Inghamf48169b2010-11-30 02:22:11 +0000404Process::ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000405ClangUserExpression::Execute (Stream &error_stream,
406 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000407 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000408 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Jim Ingham36f3b362010-10-14 23:45:03 +0000409 ClangExpressionVariable *&result)
410{
Sean Callananc673a6e2010-12-07 10:00:20 +0000411 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
412
Jim Ingham36f3b362010-10-14 23:45:03 +0000413 if (m_dwarf_opcodes.get())
414 {
415 // TODO execute the JITted opcodes
416
417 error_stream.Printf("We don't currently support executing DWARF expressions");
418
Jim Inghamf48169b2010-11-30 02:22:11 +0000419 return Process::eExecutionSetupError;
Jim Ingham36f3b362010-10-14 23:45:03 +0000420 }
421 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
422 {
423 lldb::addr_t struct_address;
424
425 lldb::addr_t object_ptr = NULL;
426
427 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000428
Jim Ingham399f1ca2010-11-05 19:25:48 +0000429 const bool stop_others = true;
430 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000431
Jim Inghamf48169b2010-11-30 02:22:11 +0000432 Address wrapper_address (NULL, m_jit_addr);
433 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread), wrapper_address, struct_address,
434 stop_others, discard_on_error,
435 (m_needs_object_ptr ? &object_ptr : NULL),
436 shared_ptr_to_me));
437 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
438 return Process::eExecutionSetupError;
439
440 call_plan_sp->SetPrivate(true);
441
442 uint32_t single_thread_timeout_usec = 10000000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000443
444 if (log)
445 log->Printf("-- Execution of expression begins --");
446
Jim Inghamf48169b2010-11-30 02:22:11 +0000447 Process::ExecutionResults execution_result =
448 exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
449 single_thread_timeout_usec, error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000450
451 if (log)
452 log->Printf("-- Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000453
454 if (execution_result == Process::eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000455 {
Jim Inghamf48169b2010-11-30 02:22:11 +0000456 if (discard_on_error)
457 error_stream.Printf ("Expression execution was interrupted. The process has been returned to the state before execution.");
458 else
459 error_stream.Printf ("Expression execution was interrupted. The process has been left at the point where it was interrupted.");
460
461 return execution_result;
462 }
463 else if (execution_result != Process::eExecutionCompleted)
464 {
465 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
466 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000467 }
468
Jim Inghamf48169b2010-11-30 02:22:11 +0000469 if (FinalizeJITExecution (error_stream, exe_ctx, result))
470 return Process::eExecutionCompleted;
471 else
472 return Process::eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000473 }
474 else
475 {
Johnny Chenfec456d2010-11-10 19:02:11 +0000476 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Jim Inghamf48169b2010-11-30 02:22:11 +0000477 return Process::eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000478 }
479}
480
481StreamString &
482ClangUserExpression::DwarfOpcodeStream ()
483{
484 if (!m_dwarf_opcodes.get())
485 m_dwarf_opcodes.reset(new StreamString());
486
487 return *m_dwarf_opcodes.get();
488}
Greg Clayton0184f012010-10-05 00:31:29 +0000489
Jim Inghamf48169b2010-11-30 02:22:11 +0000490Process::ExecutionResults
Sean Callanan322f5292010-10-29 00:29:03 +0000491ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000492 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000493 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000494 const char *expr_prefix,
495 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000496{
497 Error error;
Jim Inghamf48169b2010-11-30 02:22:11 +0000498 Process::ExecutionResults execution_results = Process::eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000499
500 if (exe_ctx.process == NULL)
Jim Inghamf48169b2010-11-30 02:22:11 +0000501 {
502 error.SetErrorString ("Must have a process to evaluate expressions.");
503
504 result_valobj_sp.reset (new ValueObjectConstResult (error));
505 return Process::eExecutionSetupError;
506 }
507
Greg Clayton8f343b02010-11-04 01:54:29 +0000508 if (!exe_ctx.process->GetDynamicCheckers())
509 {
510 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
511
512 StreamString install_errors;
513
514 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callanan2a396522010-11-05 00:57:06 +0000515 {
516 if (install_errors.GetString().empty())
517 error.SetErrorString ("couldn't install checkers, unknown error");
518 else
519 error.SetErrorString (install_errors.GetString().c_str());
520
521 result_valobj_sp.reset (new ValueObjectConstResult (error));
Jim Inghamf48169b2010-11-30 02:22:11 +0000522 return Process::eExecutionSetupError;
Sean Callanan2a396522010-11-05 00:57:06 +0000523 }
524
Greg Clayton8f343b02010-11-04 01:54:29 +0000525 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
526 }
527
Jim Inghamf48169b2010-11-30 02:22:11 +0000528 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
529
Greg Clayton0184f012010-10-05 00:31:29 +0000530 StreamString error_stream;
531
Jim Inghamf48169b2010-11-30 02:22:11 +0000532 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL)))
Greg Clayton0184f012010-10-05 00:31:29 +0000533 {
534 if (error_stream.GetString().empty())
535 error.SetErrorString ("expression failed to parse, unknown error");
536 else
537 error.SetErrorString (error_stream.GetString().c_str());
538 }
539 else
540 {
541 ClangExpressionVariable *expr_result = NULL;
542
543 error_stream.GetString().clear();
544
Jim Inghamf48169b2010-11-30 02:22:11 +0000545 execution_results = user_expression_sp->Execute (error_stream,
546 exe_ctx,
547 discard_on_error,
548 user_expression_sp,
549 expr_result);
550 if (execution_results != Process::eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000551 {
552 if (error_stream.GetString().empty())
553 error.SetErrorString ("expression failed to execute, unknown error");
554 else
555 error.SetErrorString (error_stream.GetString().c_str());
556 }
557 else
558 {
559 // TODO: seems weird to get a pointer to a result object back from
560 // a function. Do we own it? Feels like we do, but from looking at the
561 // code we don't. Might be best to make this a reference and state
562 // explicitly that we don't own it when we get a reference back from
563 // the execute?
564 if (expr_result)
565 {
566 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
567 }
568 else
569 {
Sean Callananc57f64d2010-10-19 20:15:00 +0000570 error.SetErrorString ("Expression did not return a result");
Greg Clayton0184f012010-10-05 00:31:29 +0000571 }
572 }
573 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000574
Greg Claytonb71f3842010-10-05 03:13:51 +0000575 if (result_valobj_sp.get() == NULL)
576 result_valobj_sp.reset (new ValueObjectConstResult (error));
577
Jim Inghamf48169b2010-11-30 02:22:11 +0000578 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000579}