blob: b4f4901a197e845959de77ef6216c562af94e45a [file] [log] [blame]
Sean Callanan79763a42011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan1a8d4092010-08-27 01:01:44 +00002//
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"
Greg Claytonc4e411f2011-01-18 19:36:39 +000023#include "lldb/Core/StreamFile.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000024#include "lldb/Core/StreamString.h"
Greg Claytonb71f3842010-10-05 03:13:51 +000025#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000026#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000027#include "lldb/Expression/ClangExpressionDeclMap.h"
28#include "lldb/Expression/ClangExpressionParser.h"
29#include "lldb/Expression/ClangFunction.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000030#include "lldb/Expression/ClangUserExpression.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000031#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Host/Host.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000033#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000034#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000035#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000036#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000037#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000038#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000040
Sean Callanan72e49402011-08-05 23:43:37 +000041#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
Sean Callanan1a8d4092010-08-27 01:01:44 +000044using namespace lldb_private;
45
Sean Callanan322f5292010-10-29 00:29:03 +000046ClangUserExpression::ClangUserExpression (const char *expr,
47 const char *expr_prefix) :
Greg Clayton22a939a2011-01-19 23:00:49 +000048 ClangExpression (),
49 m_expr_text (expr),
50 m_expr_prefix (expr_prefix ? expr_prefix : ""),
51 m_transformed_text (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000052 m_desired_type (NULL, NULL),
Greg Clayton22a939a2011-01-19 23:00:49 +000053 m_cplusplus (false),
54 m_objectivec (false),
55 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000056 m_const_object (false),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000057 m_evaluated_statically (false),
Sean Callananbccce812011-08-23 21:20:51 +000058 m_const_result (),
59 m_target (NULL)
Sean Callanan1a8d4092010-08-27 01:01:44 +000060{
Sean Callanan1a8d4092010-08-27 01:01:44 +000061}
62
Sean Callanane71d5532010-08-27 23:31:21 +000063ClangUserExpression::~ClangUserExpression ()
64{
65}
66
Sean Callanan1a8d4092010-08-27 01:01:44 +000067clang::ASTConsumer *
68ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000069{
Sean Callananbccce812011-08-23 21:20:51 +000070 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
71
72 if (!clang_ast_context)
73 return NULL;
74
Sean Callananf7c3e272010-11-19 02:52:21 +000075 return new ASTResultSynthesizer(passthrough,
Sean Callananbccce812011-08-23 21:20:51 +000076 m_desired_type,
77 *m_target->GetScratchClangASTContext()->getASTContext(),
78 m_target->GetPersistentVariables());
Sean Callanan1a8d4092010-08-27 01:01:44 +000079}
80
Sean Callananfc55f5d2010-09-21 00:44:12 +000081void
82ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
83{
Greg Claytonc14ee322011-09-22 04:58:26 +000084 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +000085
Greg Claytonc14ee322011-09-22 04:58:26 +000086 StackFrame *frame = exe_ctx.GetFramePtr();
87 if (frame == NULL)
Sean Callananfc55f5d2010-09-21 00:44:12 +000088 return;
89
Greg Claytonc14ee322011-09-22 04:58:26 +000090 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanan3670ba52010-12-01 21:35:54 +000091
Sean Callanan72e49402011-08-05 23:43:37 +000092 if (!sym_ctx.function)
93 return;
94
95 clang::DeclContext *decl_context;
96
97 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
98 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
99 else
100 decl_context = sym_ctx.function->GetClangDeclContext();
101
102 if (!decl_context)
103 return;
Sean Callanan737330c2011-08-24 22:18:12 +0000104
Sean Callanan72e49402011-08-05 23:43:37 +0000105 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000106 {
Sean Callanan72e49402011-08-05 23:43:37 +0000107 if (method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000108 {
Sean Callanan72e49402011-08-05 23:43:37 +0000109 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000110 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000111
Sean Callanan72e49402011-08-05 23:43:37 +0000112 do {
113 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
114
115 const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
116
117 if (!this_pointer_type)
118 break;
119
120 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
121 } while (0);
Sean Callanan3670ba52010-12-01 21:35:54 +0000122 }
123 }
Sean Callanan72e49402011-08-05 23:43:37 +0000124 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000125 {
Sean Callanan72e49402011-08-05 23:43:37 +0000126 if (method_decl->isInstanceMethod())
Sean Callanan9bc83842011-09-26 18:45:31 +0000127 {
Sean Callanan72e49402011-08-05 23:43:37 +0000128 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000129 m_needs_object_ptr = true;
130 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000131 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000132}
133
Sean Callanancf5498f2010-10-22 23:25:16 +0000134// This is a really nasty hack, meant to fix Objective-C expressions of the form
135// (int)[myArray count]. Right now, because the type information for count is
136// not available, [myArray count] returns id, which can't be directly cast to
137// int without causing a clang error.
138static void
139ApplyObjcCastHack(std::string &expr)
140{
141#define OBJC_CAST_HACK_FROM "(int)["
142#define OBJC_CAST_HACK_TO "(int)(long long)["
143
144 size_t from_offset;
145
146 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
147 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
148
149#undef OBJC_CAST_HACK_TO
150#undef OBJC_CAST_HACK_FROM
151}
152
Sean Callanan64186e72010-10-24 20:45:49 +0000153// Another hack, meant to allow use of unichar despite it not being available in
154// the type information. Although we could special-case it in type lookup,
155// hopefully we'll figure out a way to #include the same environment as is
156// present in the original source file rather than try to hack specific type
157// definitions in as needed.
158static void
159ApplyUnicharHack(std::string &expr)
160{
161#define UNICHAR_HACK_FROM "unichar"
162#define UNICHAR_HACK_TO "unsigned short"
163
164 size_t from_offset;
165
166 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
167 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
168
169#undef UNICHAR_HACK_TO
170#undef UNICHAR_HACK_FROM
171}
172
Sean Callanancf5498f2010-10-22 23:25:16 +0000173bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000174ClangUserExpression::Parse (Stream &error_stream,
175 ExecutionContext &exe_ctx,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000176 TypeFromUser desired_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000177 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000178 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000179{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000180 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000181
Sean Callananfc55f5d2010-09-21 00:44:12 +0000182 ScanContext(exe_ctx);
183
184 StreamString m_transformed_stream;
185
186 ////////////////////////////////////
187 // Generate the expression
188 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000189
190 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000191 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000192
Sean Callanan9bc83842011-09-26 18:45:31 +0000193 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
194
195 lldb::LanguageType lang_type;
196
Sean Callananfc55f5d2010-09-21 00:44:12 +0000197 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000198 lang_type = lldb::eLanguageTypeC_plus_plus;
199 else if(m_objectivec)
200 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000201 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000202 lang_type = lldb::eLanguageTypeC;
203
204 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000205 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000206 error_stream.PutCString ("error: couldn't construct expression body");
207 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000208 }
209
Sean Callananfc55f5d2010-09-21 00:44:12 +0000210 if (log)
211 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
212
Sean Callanan1a8d4092010-08-27 01:01:44 +0000213 ////////////////////////////////////
214 // Set up the target and compiler
215 //
216
Greg Claytonc14ee322011-09-22 04:58:26 +0000217 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000218
219 if (!target)
220 {
221 error_stream.PutCString ("error: invalid target\n");
222 return false;
223 }
224
Sean Callanan1a8d4092010-08-27 01:01:44 +0000225 //////////////////////////
226 // Parse the expression
227 //
228
Sean Callananf7c3e272010-11-19 02:52:21 +0000229 m_desired_type = desired_type;
230
Sean Callanan92adcac2011-01-13 08:53:35 +0000231 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callanan979f74d2010-12-03 01:38:59 +0000232
Sean Callananb9951192011-08-01 18:18:33 +0000233 if (!m_expr_decl_map->WillParse(exe_ctx))
234 {
235 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
236 return false;
237 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000238
Greg Claytonc14ee322011-09-22 04:58:26 +0000239 Process *process = exe_ctx.GetProcessPtr();
240 ClangExpressionParser parser(process, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000241
242 unsigned num_errors = parser.Parse (error_stream);
243
244 if (num_errors)
245 {
246 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000247
248 m_expr_decl_map->DidParse();
249
Sean Callanan1a8d4092010-08-27 01:01:44 +0000250 return false;
251 }
252
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000253 //////////////////////////////////////////////////////////////////////////////////////////
254 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000255 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000256
Greg Claytonc14ee322011-09-22 04:58:26 +0000257 if (execution_policy != eExecutionPolicyNever && process)
258 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000259
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000260 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
261 m_jit_start_addr,
262 m_jit_end_addr,
263 exe_ctx,
264 m_data_allocator.get(),
265 m_evaluated_statically,
266 m_const_result,
267 execution_policy);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000268
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000269 if (log && m_data_allocator.get())
Sean Callanan79763a42011-05-23 21:40:23 +0000270 {
271 StreamString dump_string;
272 m_data_allocator->Dump(dump_string);
273
274 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
275 }
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 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000281 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
282 m_jit_process_sp = process->GetSP();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000283 return true;
284 }
285 else
286 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000287 const char *error_cstr = jit_error.AsCString();
288 if (error_cstr && error_cstr[0])
289 error_stream.Printf ("error: %s\n", error_cstr);
290 else
Jason Molendafd54b362011-09-20 21:44:10 +0000291 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000292 return false;
293 }
294}
295
296bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000297ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000298 ExecutionContext &exe_ctx,
299 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000300 lldb::addr_t &object_ptr,
301 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000302{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000303 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000304
Greg Clayton22a939a2011-01-19 23:00:49 +0000305 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000306 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000307 Error materialize_error;
308
Sean Callanan17827832010-12-13 22:46:15 +0000309 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000310 {
Sean Callanan17827832010-12-13 22:46:15 +0000311 ConstString object_name;
312
313 if (m_cplusplus)
314 {
315 object_name.SetCString("this");
316 }
317 else if (m_objectivec)
318 {
319 object_name.SetCString("self");
320 }
321 else
322 {
323 error_stream.Printf("Need object pointer but don't know the language\n");
324 return false;
325 }
326
327 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
328 {
329 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
330 return false;
331 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000332
333 if (m_objectivec)
334 {
335 ConstString cmd_name("_cmd");
336
337 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
338 {
339 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
340 return false;
341 }
342 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000343 }
344
Sean Callanan979f74d2010-12-03 01:38:59 +0000345 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000346 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000347 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000348 return false;
349 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000350
351#if 0
352 // jingham: look here
353 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Clayton22a939a2011-01-19 23:00:49 +0000354 logfile.Printf("0x%16.16llx: thread = 0x%4.4x, expr = '%s'\n", m_jit_start_addr, exe_ctx.thread ? exe_ctx.thread->GetID() : -1, m_expr_text.c_str());
Greg Claytonc4e411f2011-01-18 19:36:39 +0000355#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000356
357 if (log)
358 {
Sean Callanana162eba2010-12-07 22:55:01 +0000359 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000360
Greg Clayton22a939a2011-01-19 23:00:49 +0000361 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000362
363 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000364 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000365
Sean Callananc673a6e2010-12-07 10:00:20 +0000366 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000367
368 StreamString args;
369
370 Error dump_error;
371
Sean Callanan9e6ed532010-09-13 21:34:21 +0000372 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000373 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000374 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000375 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000376 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000377 }
378 else
379 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000380 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000381 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000382 }
383 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000384 }
385 return true;
386}
387
388ThreadPlan *
389ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000390 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000391{
392 lldb::addr_t struct_address;
393
Johnny Chen44805302011-07-19 19:48:13 +0000394 lldb::addr_t object_ptr = 0;
395 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000396
Sean Callanan9d48e802010-12-14 00:42:36 +0000397 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000398
Jim Inghamf48169b2010-11-30 02:22:11 +0000399 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
400 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000401 // forcing unwind_on_error to be true here, in practical terms that can't happen.
402
Jim Ingham36f3b362010-10-14 23:45:03 +0000403 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000404 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000405 struct_address,
406 error_stream,
407 true,
408 true,
Sean Callanan17827832010-12-13 22:46:15 +0000409 (m_needs_object_ptr ? &object_ptr : NULL),
410 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000411}
412
413bool
414ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
415 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000416 lldb::ClangExpressionVariableSP &result,
417 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000418{
419 Error expr_error;
420
Sean Callananc673a6e2010-12-07 10:00:20 +0000421 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
422
423 if (log)
424 {
Sean Callanana162eba2010-12-07 22:55:01 +0000425 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000426
427 StreamString args;
428
429 Error dump_error;
430
431 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
432 {
433 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
434 }
435 else
436 {
437 log->Printf(" Structure contents:\n%s", args.GetData());
438 }
439 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000440
441 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
442
Sean Callananc673a6e2010-12-07 10:00:20 +0000443
Sean Callanane359d9b2011-05-09 22:04:36 +0000444 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000445 {
446 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
447 return false;
448 }
449 return true;
450}
451
Greg Claytone0d378b2011-03-24 21:19:54 +0000452ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000453ClangUserExpression::Execute (Stream &error_stream,
454 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000455 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000456 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000457 lldb::ClangExpressionVariableSP &result)
Jim Ingham36f3b362010-10-14 23:45:03 +0000458{
Jim Inghamb086ff72011-01-18 22:20:08 +0000459 // The expression log is quite verbose, and if you're just tracking the execution of the
460 // expression, it's quite convenient to have these logs come out with the STEP log as well.
461 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000462
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000463 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000464 {
465 lldb::addr_t struct_address;
466
Johnny Chen44805302011-07-19 19:48:13 +0000467 lldb::addr_t object_ptr = 0;
468 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000469
Sean Callanan9d48e802010-12-14 00:42:36 +0000470 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytone0d378b2011-03-24 21:19:54 +0000471 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000472
Jim Ingham399f1ca2010-11-05 19:25:48 +0000473 const bool stop_others = true;
474 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000475
Greg Clayton22a939a2011-01-19 23:00:49 +0000476 Address wrapper_address (NULL, m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000477 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000478 wrapper_address,
479 struct_address,
480 stop_others,
481 discard_on_error,
482 (m_needs_object_ptr ? &object_ptr : NULL),
483 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
484 shared_ptr_to_me));
485
Jim Inghamf48169b2010-11-30 02:22:11 +0000486 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000487 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000488
489 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Inghamf48169b2010-11-30 02:22:11 +0000490
Jim Ingham17e5c4e2011-05-17 22:24:54 +0000491 call_plan_sp->SetPrivate(true);
Jim Inghamf48169b2010-11-30 02:22:11 +0000492
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000493 uint32_t single_thread_timeout_usec = 500000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000494
495 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000496 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000497
Greg Claytonc14ee322011-09-22 04:58:26 +0000498 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
499 call_plan_sp,
500 stop_others,
501 try_all_threads,
502 discard_on_error,
503 single_thread_timeout_usec,
504 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000505
506 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000507 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000508
Greg Claytone0d378b2011-03-24 21:19:54 +0000509 if (execution_result == eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000510 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000511 const char *error_desc = NULL;
512
513 if (call_plan_sp)
514 {
515 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
516 if (real_stop_info_sp)
517 error_desc = real_stop_info_sp->GetDescription();
518 }
519 if (error_desc)
520 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000521 else
Jason Molendafd54b362011-09-20 21:44:10 +0000522 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000523
524 if (discard_on_error)
525 error_stream.Printf ("\nThe process has been returned to the state before execution.");
526 else
527 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000528
529 return execution_result;
530 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000531 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000532 {
533 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
534 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000535 }
536
Sean Callanane359d9b2011-05-09 22:04:36 +0000537 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000538 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000539 else
Greg Claytone0d378b2011-03-24 21:19:54 +0000540 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000541 }
542 else
543 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000544 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000545 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000546 }
547}
548
Greg Claytone0d378b2011-03-24 21:19:54 +0000549ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000550ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
551 lldb_private::ExecutionPolicy execution_policy,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000552 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000553 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000554 const char *expr_prefix,
555 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000556{
Jim Ingham41c75912011-08-09 00:00:49 +0000557 Error error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000558 return EvaluateWithError (exe_ctx, execution_policy, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Ingham41c75912011-08-09 00:00:49 +0000559}
560
561ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000562ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
563 lldb_private::ExecutionPolicy execution_policy,
564 bool discard_on_error,
565 const char *expr_cstr,
566 const char *expr_prefix,
567 lldb::ValueObjectSP &result_valobj_sp,
568 Error &error)
Jim Ingham41c75912011-08-09 00:00:49 +0000569{
Jim Inghamb086ff72011-01-18 22:20:08 +0000570 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000571
Greg Claytone0d378b2011-03-24 21:19:54 +0000572 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000573
Greg Claytonc14ee322011-09-22 04:58:26 +0000574 Process *process = exe_ctx.GetProcessPtr();
575
576 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000577 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000578 if (execution_policy == eExecutionPolicyAlways)
579 {
580 if (log)
581 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000582
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000583 error.SetErrorString ("expression needed to run but couldn't");
584
585 return execution_results;
586 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000587 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000588
Greg Claytonc14ee322011-09-22 04:58:26 +0000589 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000590 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000591
Jim Inghamf48169b2010-11-30 02:22:11 +0000592 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
593
Greg Clayton0184f012010-10-05 00:31:29 +0000594 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000595
Sean Callanana162eba2010-12-07 22:55:01 +0000596 if (log)
597 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
598
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000599 const bool keep_expression_in_memory = true;
600
601 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000602 {
603 if (error_stream.GetString().empty())
604 error.SetErrorString ("expression failed to parse, unknown error");
605 else
606 error.SetErrorString (error_stream.GetString().c_str());
607 }
608 else
609 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000610 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000611
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000612 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000613 {
Sean Callanana162eba2010-12-07 22:55:01 +0000614 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000615 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000616
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000617 if (user_expression_sp->m_const_result)
618 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
619 else
620 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
621
Jim Ingham41c75912011-08-09 00:00:49 +0000622 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000623 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000624 else if (execution_policy == eExecutionPolicyNever)
625 {
626 if (log)
627 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
628
629 if (error_stream.GetString().empty())
630 error.SetErrorString ("expression needed to run but couldn't");
631 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000632 else
633 {
634 error_stream.GetString().clear();
635
636 if (log)
637 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
638
639 execution_results = user_expression_sp->Execute (error_stream,
640 exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35 +0000641 discard_on_error,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000642 user_expression_sp,
643 expr_result);
644
Greg Claytone0d378b2011-03-24 21:19:54 +0000645 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000646 {
Sean Callanana162eba2010-12-07 22:55:01 +0000647 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000648 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
649
650 if (error_stream.GetString().empty())
651 error.SetErrorString ("expression failed to execute, unknown error");
652 else
653 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000654 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000655 else
Greg Clayton0184f012010-10-05 00:31:29 +0000656 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000657 if (expr_result)
658 {
659 result_valobj_sp = expr_result->GetValueObject();
660
661 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000662 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000663 }
664 else
665 {
666 if (log)
667 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
668
Sean Callananbccce812011-08-23 21:20:51 +0000669 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000670 }
Greg Clayton0184f012010-10-05 00:31:29 +0000671 }
672 }
673 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000674
Greg Claytonb71f3842010-10-05 03:13:51 +0000675 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000676 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000677
Jim Inghamf48169b2010-11-30 02:22:11 +0000678 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000679}