blob: 73ed043c26ab4a8e974b3718adf7c45d07827a7b [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"
31#include "lldb/Host/Host.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000032#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000034#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000035#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000036#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000037#include "lldb/Target/ThreadPlan.h"
38#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000039
Sean Callanan72e49402011-08-05 23:43:37 +000040#include "clang/AST/DeclCXX.h"
41#include "clang/AST/DeclObjC.h"
42
Sean Callanan1a8d4092010-08-27 01:01:44 +000043using namespace lldb_private;
44
Sean Callanan322f5292010-10-29 00:29:03 +000045ClangUserExpression::ClangUserExpression (const char *expr,
46 const char *expr_prefix) :
Greg Clayton22a939a2011-01-19 23:00:49 +000047 ClangExpression (),
48 m_expr_text (expr),
49 m_expr_prefix (expr_prefix ? expr_prefix : ""),
50 m_transformed_text (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000051 m_desired_type (NULL, NULL),
Greg Clayton22a939a2011-01-19 23:00:49 +000052 m_cplusplus (false),
53 m_objectivec (false),
54 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000055 m_const_object (false),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000056 m_evaluated_statically (false),
Sean Callananbccce812011-08-23 21:20:51 +000057 m_const_result (),
58 m_target (NULL)
Sean Callanan1a8d4092010-08-27 01:01:44 +000059{
Sean Callanan1a8d4092010-08-27 01:01:44 +000060}
61
Sean Callanane71d5532010-08-27 23:31:21 +000062ClangUserExpression::~ClangUserExpression ()
63{
64}
65
Sean Callanan1a8d4092010-08-27 01:01:44 +000066clang::ASTConsumer *
67ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000068{
Sean Callananbccce812011-08-23 21:20:51 +000069 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
70
71 if (!clang_ast_context)
72 return NULL;
73
Sean Callananf7c3e272010-11-19 02:52:21 +000074 return new ASTResultSynthesizer(passthrough,
Sean Callananbccce812011-08-23 21:20:51 +000075 m_desired_type,
76 *m_target->GetScratchClangASTContext()->getASTContext(),
77 m_target->GetPersistentVariables());
Sean Callanan1a8d4092010-08-27 01:01:44 +000078}
79
Sean Callananfc55f5d2010-09-21 00:44:12 +000080void
81ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
82{
Greg Claytonc14ee322011-09-22 04:58:26 +000083 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +000084
Greg Claytonc14ee322011-09-22 04:58:26 +000085 StackFrame *frame = exe_ctx.GetFramePtr();
86 if (frame == NULL)
Sean Callananfc55f5d2010-09-21 00:44:12 +000087 return;
88
Greg Claytonc14ee322011-09-22 04:58:26 +000089 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanan3670ba52010-12-01 21:35:54 +000090
Sean Callanan72e49402011-08-05 23:43:37 +000091 if (!sym_ctx.function)
92 return;
93
94 clang::DeclContext *decl_context;
95
96 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
97 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
98 else
99 decl_context = sym_ctx.function->GetClangDeclContext();
100
101 if (!decl_context)
102 return;
Sean Callanan737330c2011-08-24 22:18:12 +0000103
Sean Callanan72e49402011-08-05 23:43:37 +0000104 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000105 {
Sean Callanan72e49402011-08-05 23:43:37 +0000106 if (method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000107 {
Sean Callanan72e49402011-08-05 23:43:37 +0000108 m_cplusplus = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000109
Sean Callanan72e49402011-08-05 23:43:37 +0000110 do {
111 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
112
113 const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
114
115 if (!this_pointer_type)
116 break;
117
118 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
119 } while (0);
Sean Callanan3670ba52010-12-01 21:35:54 +0000120 }
121 }
Sean Callanan72e49402011-08-05 23:43:37 +0000122 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000123 {
Sean Callanan72e49402011-08-05 23:43:37 +0000124 if (method_decl->isInstanceMethod())
125 m_objectivec = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000126 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000127}
128
Sean Callanancf5498f2010-10-22 23:25:16 +0000129// This is a really nasty hack, meant to fix Objective-C expressions of the form
130// (int)[myArray count]. Right now, because the type information for count is
131// not available, [myArray count] returns id, which can't be directly cast to
132// int without causing a clang error.
133static void
134ApplyObjcCastHack(std::string &expr)
135{
136#define OBJC_CAST_HACK_FROM "(int)["
137#define OBJC_CAST_HACK_TO "(int)(long long)["
138
139 size_t from_offset;
140
141 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
142 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
143
144#undef OBJC_CAST_HACK_TO
145#undef OBJC_CAST_HACK_FROM
146}
147
Sean Callanan64186e72010-10-24 20:45:49 +0000148// Another hack, meant to allow use of unichar despite it not being available in
149// the type information. Although we could special-case it in type lookup,
150// hopefully we'll figure out a way to #include the same environment as is
151// present in the original source file rather than try to hack specific type
152// definitions in as needed.
153static void
154ApplyUnicharHack(std::string &expr)
155{
156#define UNICHAR_HACK_FROM "unichar"
157#define UNICHAR_HACK_TO "unsigned short"
158
159 size_t from_offset;
160
161 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
162 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
163
164#undef UNICHAR_HACK_TO
165#undef UNICHAR_HACK_FROM
166}
167
Sean Callanancf5498f2010-10-22 23:25:16 +0000168bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000169ClangUserExpression::Parse (Stream &error_stream,
170 ExecutionContext &exe_ctx,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000171 TypeFromUser desired_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000172 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000173 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000174{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000175 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000176
Sean Callananfc55f5d2010-09-21 00:44:12 +0000177 ScanContext(exe_ctx);
178
179 StreamString m_transformed_stream;
180
181 ////////////////////////////////////
182 // Generate the expression
183 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000184
185 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000186 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000187
188 if (m_cplusplus)
189 {
Sean Callanan322f5292010-10-29 00:29:03 +0000190 m_transformed_stream.Printf("%s \n"
191 "typedef unsigned short unichar; \n"
Greg Clayton73b472d2010-10-27 03:32:59 +0000192 "void \n"
Sean Callanan3670ba52010-12-01 21:35:54 +0000193 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
Sean Callananfc55f5d2010-09-21 00:44:12 +0000194 "{ \n"
195 " %s; \n"
196 "} \n",
Sean Callanan322f5292010-10-29 00:29:03 +0000197 m_expr_prefix.c_str(),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000198 FunctionName(),
Sean Callanan3670ba52010-12-01 21:35:54 +0000199 (m_const_object ? "const" : ""),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000200 m_expr_text.c_str());
201
202 m_needs_object_ptr = true;
203 }
Enrico Granata20edcdb2011-07-19 18:03:25 +0000204 else if (m_objectivec)
Sean Callanan17827832010-12-13 22:46:15 +0000205 {
206 const char *function_name = FunctionName();
207
208 m_transformed_stream.Printf("%s \n"
Greg Claytonf028a1f2010-12-17 02:26:24 +0000209 "typedef unsigned short unichar; \n"
Sean Callanan17827832010-12-13 22:46:15 +0000210 "@interface $__lldb_objc_class ($__lldb_category) \n"
211 "-(void)%s:(void *)$__lldb_arg; \n"
212 "@end \n"
213 "@implementation $__lldb_objc_class ($__lldb_category) \n"
214 "-(void)%s:(void *)$__lldb_arg \n"
215 "{ \n"
216 " %s; \n"
217 "} \n"
218 "@end \n",
219 m_expr_prefix.c_str(),
220 function_name,
221 function_name,
222 m_expr_text.c_str());
223
224 m_needs_object_ptr = true;
225 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000226 else
227 {
Sean Callanan322f5292010-10-29 00:29:03 +0000228 m_transformed_stream.Printf("%s \n"
229 "typedef unsigned short unichar;\n"
Greg Clayton73b472d2010-10-27 03:32:59 +0000230 "void \n"
Sean Callanancf5498f2010-10-22 23:25:16 +0000231 "%s(void *$__lldb_arg) \n"
Sean Callananfc55f5d2010-09-21 00:44:12 +0000232 "{ \n"
233 " %s; \n"
234 "} \n",
Sean Callanan322f5292010-10-29 00:29:03 +0000235 m_expr_prefix.c_str(),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000236 FunctionName(),
237 m_expr_text.c_str());
238 }
239
240 m_transformed_text = m_transformed_stream.GetData();
241
242
243 if (log)
244 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
245
Sean Callanan1a8d4092010-08-27 01:01:44 +0000246 ////////////////////////////////////
247 // Set up the target and compiler
248 //
249
Greg Claytonc14ee322011-09-22 04:58:26 +0000250 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000251
252 if (!target)
253 {
254 error_stream.PutCString ("error: invalid target\n");
255 return false;
256 }
257
Sean Callanan1a8d4092010-08-27 01:01:44 +0000258 //////////////////////////
259 // Parse the expression
260 //
261
Sean Callananf7c3e272010-11-19 02:52:21 +0000262 m_desired_type = desired_type;
263
Sean Callanan92adcac2011-01-13 08:53:35 +0000264 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callanan979f74d2010-12-03 01:38:59 +0000265
Sean Callananb9951192011-08-01 18:18:33 +0000266 if (!m_expr_decl_map->WillParse(exe_ctx))
267 {
268 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
269 return false;
270 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000271
Greg Claytonc14ee322011-09-22 04:58:26 +0000272 Process *process = exe_ctx.GetProcessPtr();
273 ClangExpressionParser parser(process, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000274
275 unsigned num_errors = parser.Parse (error_stream);
276
277 if (num_errors)
278 {
279 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000280
281 m_expr_decl_map->DidParse();
282
Sean Callanan1a8d4092010-08-27 01:01:44 +0000283 return false;
284 }
285
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000286 //////////////////////////////////////////////////////////////////////////////////////////
287 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000288 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000289
Greg Claytonc14ee322011-09-22 04:58:26 +0000290 if (execution_policy != eExecutionPolicyNever && process)
291 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000292
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000293 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
294 m_jit_start_addr,
295 m_jit_end_addr,
296 exe_ctx,
297 m_data_allocator.get(),
298 m_evaluated_statically,
299 m_const_result,
300 execution_policy);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000301
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000302 if (log && m_data_allocator.get())
Sean Callanan79763a42011-05-23 21:40:23 +0000303 {
304 StreamString dump_string;
305 m_data_allocator->Dump(dump_string);
306
307 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
308 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000309
Sean Callanan979f74d2010-12-03 01:38:59 +0000310 m_expr_decl_map->DidParse();
311
Sean Callanan1a8d4092010-08-27 01:01:44 +0000312 if (jit_error.Success())
313 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000314 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
315 m_jit_process_sp = process->GetSP();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000316 return true;
317 }
318 else
319 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000320 const char *error_cstr = jit_error.AsCString();
321 if (error_cstr && error_cstr[0])
322 error_stream.Printf ("error: %s\n", error_cstr);
323 else
Jason Molendafd54b362011-09-20 21:44:10 +0000324 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000325 return false;
326 }
327}
328
329bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000330ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000331 ExecutionContext &exe_ctx,
332 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000333 lldb::addr_t &object_ptr,
334 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000335{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000336 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000337
Greg Clayton22a939a2011-01-19 23:00:49 +0000338 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000339 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000340 Error materialize_error;
341
Sean Callanan17827832010-12-13 22:46:15 +0000342 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000343 {
Sean Callanan17827832010-12-13 22:46:15 +0000344 ConstString object_name;
345
346 if (m_cplusplus)
347 {
348 object_name.SetCString("this");
349 }
350 else if (m_objectivec)
351 {
352 object_name.SetCString("self");
353 }
354 else
355 {
356 error_stream.Printf("Need object pointer but don't know the language\n");
357 return false;
358 }
359
360 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
361 {
362 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
363 return false;
364 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000365
366 if (m_objectivec)
367 {
368 ConstString cmd_name("_cmd");
369
370 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
371 {
372 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
373 return false;
374 }
375 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000376 }
377
Sean Callanan979f74d2010-12-03 01:38:59 +0000378 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000379 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000380 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000381 return false;
382 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000383
384#if 0
385 // jingham: look here
386 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Clayton22a939a2011-01-19 23:00:49 +0000387 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 +0000388#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000389
390 if (log)
391 {
Sean Callanana162eba2010-12-07 22:55:01 +0000392 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000393
Greg Clayton22a939a2011-01-19 23:00:49 +0000394 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000395
396 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000397 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000398
Sean Callananc673a6e2010-12-07 10:00:20 +0000399 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000400
401 StreamString args;
402
403 Error dump_error;
404
Sean Callanan9e6ed532010-09-13 21:34:21 +0000405 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000406 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000407 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000408 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000409 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000410 }
411 else
412 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000413 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000414 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000415 }
416 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000417 }
418 return true;
419}
420
421ThreadPlan *
422ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000423 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000424{
425 lldb::addr_t struct_address;
426
Johnny Chen44805302011-07-19 19:48:13 +0000427 lldb::addr_t object_ptr = 0;
428 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000429
Sean Callanan9d48e802010-12-14 00:42:36 +0000430 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000431
Jim Inghamf48169b2010-11-30 02:22:11 +0000432 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
433 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000434 // forcing unwind_on_error to be true here, in practical terms that can't happen.
435
Jim Ingham36f3b362010-10-14 23:45:03 +0000436 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000437 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000438 struct_address,
439 error_stream,
440 true,
441 true,
Sean Callanan17827832010-12-13 22:46:15 +0000442 (m_needs_object_ptr ? &object_ptr : NULL),
443 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000444}
445
446bool
447ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
448 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000449 lldb::ClangExpressionVariableSP &result,
450 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000451{
452 Error expr_error;
453
Sean Callananc673a6e2010-12-07 10:00:20 +0000454 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
455
456 if (log)
457 {
Sean Callanana162eba2010-12-07 22:55:01 +0000458 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000459
460 StreamString args;
461
462 Error dump_error;
463
464 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
465 {
466 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
467 }
468 else
469 {
470 log->Printf(" Structure contents:\n%s", args.GetData());
471 }
472 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000473
474 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
475
Sean Callananc673a6e2010-12-07 10:00:20 +0000476
Sean Callanane359d9b2011-05-09 22:04:36 +0000477 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000478 {
479 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
480 return false;
481 }
482 return true;
483}
484
Greg Claytone0d378b2011-03-24 21:19:54 +0000485ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000486ClangUserExpression::Execute (Stream &error_stream,
487 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000488 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000489 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000490 lldb::ClangExpressionVariableSP &result)
Jim Ingham36f3b362010-10-14 23:45:03 +0000491{
Jim Inghamb086ff72011-01-18 22:20:08 +0000492 // The expression log is quite verbose, and if you're just tracking the execution of the
493 // expression, it's quite convenient to have these logs come out with the STEP log as well.
494 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000495
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000496 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000497 {
498 lldb::addr_t struct_address;
499
Johnny Chen44805302011-07-19 19:48:13 +0000500 lldb::addr_t object_ptr = 0;
501 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000502
Sean Callanan9d48e802010-12-14 00:42:36 +0000503 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytone0d378b2011-03-24 21:19:54 +0000504 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000505
Jim Ingham399f1ca2010-11-05 19:25:48 +0000506 const bool stop_others = true;
507 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000508
Greg Clayton22a939a2011-01-19 23:00:49 +0000509 Address wrapper_address (NULL, m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000510 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000511 wrapper_address,
512 struct_address,
513 stop_others,
514 discard_on_error,
515 (m_needs_object_ptr ? &object_ptr : NULL),
516 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
517 shared_ptr_to_me));
518
Jim Inghamf48169b2010-11-30 02:22:11 +0000519 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000520 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000521
522 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Inghamf48169b2010-11-30 02:22:11 +0000523
Jim Ingham17e5c4e2011-05-17 22:24:54 +0000524 call_plan_sp->SetPrivate(true);
Jim Inghamf48169b2010-11-30 02:22:11 +0000525
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000526 uint32_t single_thread_timeout_usec = 500000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000527
528 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000529 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000530
Greg Claytonc14ee322011-09-22 04:58:26 +0000531 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
532 call_plan_sp,
533 stop_others,
534 try_all_threads,
535 discard_on_error,
536 single_thread_timeout_usec,
537 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000538
539 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000540 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000541
Greg Claytone0d378b2011-03-24 21:19:54 +0000542 if (execution_result == eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000543 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000544 const char *error_desc = NULL;
545
546 if (call_plan_sp)
547 {
548 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
549 if (real_stop_info_sp)
550 error_desc = real_stop_info_sp->GetDescription();
551 }
552 if (error_desc)
553 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000554 else
Jason Molendafd54b362011-09-20 21:44:10 +0000555 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000556
557 if (discard_on_error)
558 error_stream.Printf ("\nThe process has been returned to the state before execution.");
559 else
560 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000561
562 return execution_result;
563 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000564 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000565 {
566 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
567 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000568 }
569
Sean Callanane359d9b2011-05-09 22:04:36 +0000570 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000571 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000572 else
Greg Claytone0d378b2011-03-24 21:19:54 +0000573 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000574 }
575 else
576 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000577 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000578 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000579 }
580}
581
Greg Claytone0d378b2011-03-24 21:19:54 +0000582ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000583ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
584 lldb_private::ExecutionPolicy execution_policy,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000585 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000586 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000587 const char *expr_prefix,
588 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000589{
Jim Ingham41c75912011-08-09 00:00:49 +0000590 Error error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000591 return EvaluateWithError (exe_ctx, execution_policy, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Ingham41c75912011-08-09 00:00:49 +0000592}
593
594ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000595ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
596 lldb_private::ExecutionPolicy execution_policy,
597 bool discard_on_error,
598 const char *expr_cstr,
599 const char *expr_prefix,
600 lldb::ValueObjectSP &result_valobj_sp,
601 Error &error)
Jim Ingham41c75912011-08-09 00:00:49 +0000602{
Jim Inghamb086ff72011-01-18 22:20:08 +0000603 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000604
Greg Claytone0d378b2011-03-24 21:19:54 +0000605 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000606
Greg Claytonc14ee322011-09-22 04:58:26 +0000607 Process *process = exe_ctx.GetProcessPtr();
608
609 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000610 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000611 if (execution_policy == eExecutionPolicyAlways)
612 {
613 if (log)
614 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000615
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000616 error.SetErrorString ("expression needed to run but couldn't");
617
618 return execution_results;
619 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000620 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000621
Greg Claytonc14ee322011-09-22 04:58:26 +0000622 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000623 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000624
Jim Inghamf48169b2010-11-30 02:22:11 +0000625 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
626
Greg Clayton0184f012010-10-05 00:31:29 +0000627 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000628
Sean Callanana162eba2010-12-07 22:55:01 +0000629 if (log)
630 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
631
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000632 const bool keep_expression_in_memory = true;
633
634 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 +0000635 {
636 if (error_stream.GetString().empty())
637 error.SetErrorString ("expression failed to parse, unknown error");
638 else
639 error.SetErrorString (error_stream.GetString().c_str());
640 }
641 else
642 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000643 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000644
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000645 if (user_expression_sp->EvaluatedStatically())
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] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000649
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000650 if (user_expression_sp->m_const_result)
651 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
652 else
653 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
654
Jim Ingham41c75912011-08-09 00:00:49 +0000655 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000656 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000657 else if (execution_policy == eExecutionPolicyNever)
658 {
659 if (log)
660 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
661
662 if (error_stream.GetString().empty())
663 error.SetErrorString ("expression needed to run but couldn't");
664 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000665 else
666 {
667 error_stream.GetString().clear();
668
669 if (log)
670 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
671
672 execution_results = user_expression_sp->Execute (error_stream,
673 exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35 +0000674 discard_on_error,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000675 user_expression_sp,
676 expr_result);
677
Greg Claytone0d378b2011-03-24 21:19:54 +0000678 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000679 {
Sean Callanana162eba2010-12-07 22:55:01 +0000680 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000681 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
682
683 if (error_stream.GetString().empty())
684 error.SetErrorString ("expression failed to execute, unknown error");
685 else
686 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000687 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000688 else
Greg Clayton0184f012010-10-05 00:31:29 +0000689 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000690 if (expr_result)
691 {
692 result_valobj_sp = expr_result->GetValueObject();
693
694 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000695 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000696 }
697 else
698 {
699 if (log)
700 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
701
Sean Callananbccce812011-08-23 21:20:51 +0000702 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000703 }
Greg Clayton0184f012010-10-05 00:31:29 +0000704 }
705 }
706 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000707
Greg Claytonb71f3842010-10-05 03:13:51 +0000708 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000709 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000710
Jim Inghamf48169b2010-11-30 02:22:11 +0000711 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000712}