blob: b5bad503155c489a64557c39c2079ad6b2c70cc6 [file] [log] [blame]
Sean Callananc0492742011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan65dafa82010-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 Claytonc71899e2011-01-18 19:36:39 +000023#include "lldb/Core/StreamFile.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000024#include "lldb/Core/StreamString.h"
Greg Claytond1719722010-10-05 03:13:51 +000025#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan05a5a1b2010-12-16 03:17:46 +000026#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000027#include "lldb/Expression/ClangExpressionDeclMap.h"
28#include "lldb/Expression/ClangExpressionParser.h"
29#include "lldb/Expression/ClangFunction.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000030#include "lldb/Expression/ClangUserExpression.h"
Sean Callanande3d27e2011-09-26 18:45:31 +000031#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000032#include "lldb/Host/Host.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000033#include "lldb/Symbol/VariableList.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000034#include "lldb/Target/ExecutionContext.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000035#include "lldb/Target/Process.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000036#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000037#include "lldb/Target/Target.h"
Jim Ingham360f53f2010-11-30 02:22:11 +000038#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000040
Sean Callananc617a4c2011-08-05 23:43:37 +000041#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
Sean Callanan65dafa82010-08-27 01:01:44 +000044using namespace lldb_private;
45
Sean Callanan77e93942010-10-29 00:29:03 +000046ClangUserExpression::ClangUserExpression (const char *expr,
47 const char *expr_prefix) :
Greg Claytond0882d02011-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 Wilsondbeb3e12011-04-11 19:41:40 +000052 m_desired_type (NULL, NULL),
Greg Claytond0882d02011-01-19 23:00:49 +000053 m_cplusplus (false),
54 m_objectivec (false),
55 m_needs_object_ptr (false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000056 m_const_object (false),
Sean Callanan47dc4572011-09-15 02:13:07 +000057 m_evaluated_statically (false),
Sean Callanan24312442011-08-23 21:20:51 +000058 m_const_result (),
59 m_target (NULL)
Sean Callanan65dafa82010-08-27 01:01:44 +000060{
Sean Callanan65dafa82010-08-27 01:01:44 +000061}
62
Sean Callanan830a9032010-08-27 23:31:21 +000063ClangUserExpression::~ClangUserExpression ()
64{
65}
66
Sean Callanan65dafa82010-08-27 01:01:44 +000067clang::ASTConsumer *
68ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan036fa902011-08-24 22:18:12 +000069{
Sean Callanan24312442011-08-23 21:20:51 +000070 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
71
72 if (!clang_ast_context)
73 return NULL;
74
Sean Callanan060d53f2011-10-08 00:21:35 +000075 if (!m_result_synthesizer.get())
76 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
77 m_desired_type,
78 *m_target->GetScratchClangASTContext()->getASTContext(),
79 m_target->GetPersistentVariables()));
80
81 return m_result_synthesizer.get();
Sean Callanan65dafa82010-08-27 01:01:44 +000082}
83
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000084void
85ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
86{
Greg Clayton567e7f32011-09-22 04:58:26 +000087 m_target = exe_ctx.GetTargetPtr();
Greg Clayton144188b2011-09-12 23:21:58 +000088
Greg Clayton567e7f32011-09-22 04:58:26 +000089 StackFrame *frame = exe_ctx.GetFramePtr();
90 if (frame == NULL)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000091 return;
92
Greg Clayton567e7f32011-09-22 04:58:26 +000093 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanane8e55572010-12-01 21:35:54 +000094
Sean Callananc617a4c2011-08-05 23:43:37 +000095 if (!sym_ctx.function)
96 return;
97
98 clang::DeclContext *decl_context;
99
100 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
101 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
102 else
103 decl_context = sym_ctx.function->GetClangDeclContext();
104
105 if (!decl_context)
106 return;
Sean Callanan036fa902011-08-24 22:18:12 +0000107
Sean Callananc617a4c2011-08-05 23:43:37 +0000108 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000109 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000110 if (method_decl->isInstance())
Sean Callanane8e55572010-12-01 21:35:54 +0000111 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000112 m_cplusplus = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000113 m_needs_object_ptr = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000114
Sean Callananc617a4c2011-08-05 23:43:37 +0000115 do {
116 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
117
118 const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
119
120 if (!this_pointer_type)
121 break;
122
123 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
124 } while (0);
Sean Callanane8e55572010-12-01 21:35:54 +0000125 }
126 }
Sean Callananc617a4c2011-08-05 23:43:37 +0000127 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000128 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000129 if (method_decl->isInstanceMethod())
Sean Callanande3d27e2011-09-26 18:45:31 +0000130 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000131 m_objectivec = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000132 m_needs_object_ptr = true;
133 }
Sean Callanane8e55572010-12-01 21:35:54 +0000134 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000135}
136
Sean Callanan550f2762010-10-22 23:25:16 +0000137// This is a really nasty hack, meant to fix Objective-C expressions of the form
138// (int)[myArray count]. Right now, because the type information for count is
139// not available, [myArray count] returns id, which can't be directly cast to
140// int without causing a clang error.
141static void
142ApplyObjcCastHack(std::string &expr)
143{
144#define OBJC_CAST_HACK_FROM "(int)["
145#define OBJC_CAST_HACK_TO "(int)(long long)["
146
147 size_t from_offset;
148
149 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
150 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
151
152#undef OBJC_CAST_HACK_TO
153#undef OBJC_CAST_HACK_FROM
154}
155
Sean Callanan30892372010-10-24 20:45:49 +0000156// Another hack, meant to allow use of unichar despite it not being available in
157// the type information. Although we could special-case it in type lookup,
158// hopefully we'll figure out a way to #include the same environment as is
159// present in the original source file rather than try to hack specific type
160// definitions in as needed.
161static void
162ApplyUnicharHack(std::string &expr)
163{
164#define UNICHAR_HACK_FROM "unichar"
165#define UNICHAR_HACK_TO "unsigned short"
166
167 size_t from_offset;
168
169 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
170 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
171
172#undef UNICHAR_HACK_TO
173#undef UNICHAR_HACK_FROM
174}
175
Sean Callanan550f2762010-10-22 23:25:16 +0000176bool
Sean Callanana91dd992010-11-19 02:52:21 +0000177ClangUserExpression::Parse (Stream &error_stream,
178 ExecutionContext &exe_ctx,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000179 TypeFromUser desired_type,
Sean Callanan47dc4572011-09-15 02:13:07 +0000180 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000181 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000182{
Greg Claytone005f2c2010-11-06 01:53:30 +0000183 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000184
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000185 ScanContext(exe_ctx);
186
187 StreamString m_transformed_stream;
188
189 ////////////////////////////////////
190 // Generate the expression
191 //
Sean Callanan550f2762010-10-22 23:25:16 +0000192
193 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000194 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000195
Sean Callanande3d27e2011-09-26 18:45:31 +0000196 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
197
198 lldb::LanguageType lang_type;
199
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000200 if (m_cplusplus)
Sean Callanande3d27e2011-09-26 18:45:31 +0000201 lang_type = lldb::eLanguageTypeC_plus_plus;
202 else if(m_objectivec)
203 lang_type = lldb::eLanguageTypeObjC;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000204 else
Sean Callanande3d27e2011-09-26 18:45:31 +0000205 lang_type = lldb::eLanguageTypeC;
206
207 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object))
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000208 {
Sean Callanande3d27e2011-09-26 18:45:31 +0000209 error_stream.PutCString ("error: couldn't construct expression body");
210 return false;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000211 }
212
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000213 if (log)
214 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
215
Sean Callanan65dafa82010-08-27 01:01:44 +0000216 ////////////////////////////////////
217 // Set up the target and compiler
218 //
219
Greg Clayton567e7f32011-09-22 04:58:26 +0000220 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan65dafa82010-08-27 01:01:44 +0000221
222 if (!target)
223 {
224 error_stream.PutCString ("error: invalid target\n");
225 return false;
226 }
227
Sean Callanan65dafa82010-08-27 01:01:44 +0000228 //////////////////////////
229 // Parse the expression
230 //
231
Sean Callanana91dd992010-11-19 02:52:21 +0000232 m_desired_type = desired_type;
233
Sean Callanan6a925532011-01-13 08:53:35 +0000234 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +0000235
Sean Callanan166ba102011-08-01 18:18:33 +0000236 if (!m_expr_decl_map->WillParse(exe_ctx))
237 {
238 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
239 return false;
240 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000241
Greg Clayton567e7f32011-09-22 04:58:26 +0000242 Process *process = exe_ctx.GetProcessPtr();
243 ClangExpressionParser parser(process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000244
245 unsigned num_errors = parser.Parse (error_stream);
246
247 if (num_errors)
248 {
249 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000250
251 m_expr_decl_map->DidParse();
252
Sean Callanan65dafa82010-08-27 01:01:44 +0000253 return false;
254 }
255
Sean Callanan47dc4572011-09-15 02:13:07 +0000256 //////////////////////////////////////////////////////////////////////////////////////////
257 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan65dafa82010-08-27 01:01:44 +0000258 //
Sean Callanan65dafa82010-08-27 01:01:44 +0000259
Greg Clayton567e7f32011-09-22 04:58:26 +0000260 if (execution_policy != eExecutionPolicyNever && process)
261 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan65dafa82010-08-27 01:01:44 +0000262
Sean Callanan47dc4572011-09-15 02:13:07 +0000263 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
264 m_jit_start_addr,
265 m_jit_end_addr,
266 exe_ctx,
267 m_data_allocator.get(),
268 m_evaluated_statically,
269 m_const_result,
270 execution_policy);
Sean Callanan65dafa82010-08-27 01:01:44 +0000271
Sean Callanan47dc4572011-09-15 02:13:07 +0000272 if (log && m_data_allocator.get())
Sean Callananc0492742011-05-23 21:40:23 +0000273 {
274 StreamString dump_string;
275 m_data_allocator->Dump(dump_string);
276
277 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
278 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000279
Sean Callanan65dafa82010-08-27 01:01:44 +0000280 if (jit_error.Success())
281 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000282 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
283 m_jit_process_sp = process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000284 return true;
285 }
286 else
287 {
Greg Clayton30581972011-05-17 03:51:29 +0000288 const char *error_cstr = jit_error.AsCString();
289 if (error_cstr && error_cstr[0])
290 error_stream.Printf ("error: %s\n", error_cstr);
291 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000292 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan65dafa82010-08-27 01:01:44 +0000293 return false;
294 }
295}
296
297bool
Jim Inghamd1686902010-10-14 23:45:03 +0000298ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000299 ExecutionContext &exe_ctx,
300 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000301 lldb::addr_t &object_ptr,
302 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000303{
Greg Claytone005f2c2010-11-06 01:53:30 +0000304 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000305
Greg Claytond0882d02011-01-19 23:00:49 +0000306 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000307 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000308 Error materialize_error;
309
Sean Callanan3aa7da52010-12-13 22:46:15 +0000310 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000311 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000312 ConstString object_name;
313
314 if (m_cplusplus)
315 {
316 object_name.SetCString("this");
317 }
318 else if (m_objectivec)
319 {
320 object_name.SetCString("self");
321 }
322 else
323 {
324 error_stream.Printf("Need object pointer but don't know the language\n");
325 return false;
326 }
327
328 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
329 {
330 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
331 return false;
332 }
Sean Callanan047923c2010-12-14 00:42:36 +0000333
334 if (m_objectivec)
335 {
336 ConstString cmd_name("_cmd");
337
338 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
339 {
340 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
341 return false;
342 }
343 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000344 }
345
Sean Callananaa301c42010-12-03 01:38:59 +0000346 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000347 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000348 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000349 return false;
350 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000351
352#if 0
353 // jingham: look here
354 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000355 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 Claytonc71899e2011-01-18 19:36:39 +0000356#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000357
358 if (log)
359 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000360 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000361
Greg Claytond0882d02011-01-19 23:00:49 +0000362 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000363
364 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000365 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000366
Sean Callanan33711022010-12-07 10:00:20 +0000367 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000368
369 StreamString args;
370
371 Error dump_error;
372
Sean Callanane8a59a82010-09-13 21:34:21 +0000373 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000374 {
Sean Callananaa301c42010-12-03 01:38:59 +0000375 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000376 {
Sean Callanan33711022010-12-07 10:00:20 +0000377 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000378 }
379 else
380 {
Sean Callanan33711022010-12-07 10:00:20 +0000381 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000382 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000383 }
384 }
Jim Inghamd1686902010-10-14 23:45:03 +0000385 }
386 return true;
387}
388
389ThreadPlan *
390ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000391 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000392{
393 lldb::addr_t struct_address;
394
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000395 lldb::addr_t object_ptr = 0;
396 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000397
Sean Callanan047923c2010-12-14 00:42:36 +0000398 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000399
Jim Ingham360f53f2010-11-30 02:22:11 +0000400 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
401 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000402 // forcing unwind_on_error to be true here, in practical terms that can't happen.
403
Jim Inghamd1686902010-10-14 23:45:03 +0000404 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000405 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000406 struct_address,
407 error_stream,
408 true,
409 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000410 (m_needs_object_ptr ? &object_ptr : NULL),
411 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000412}
413
414bool
415ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
416 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000417 lldb::ClangExpressionVariableSP &result,
418 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000419{
420 Error expr_error;
421
Sean Callanan33711022010-12-07 10:00:20 +0000422 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
423
424 if (log)
425 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000426 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000427
428 StreamString args;
429
430 Error dump_error;
431
432 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
433 {
434 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
435 }
436 else
437 {
438 log->Printf(" Structure contents:\n%s", args.GetData());
439 }
440 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000441
442 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
443
Sean Callanan33711022010-12-07 10:00:20 +0000444
Sean Callanan0ddf8062011-05-09 22:04:36 +0000445 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000446 {
447 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
448 return false;
449 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000450
Jim Inghamd1686902010-10-14 23:45:03 +0000451 return true;
452}
453
Greg Claytonb3448432011-03-24 21:19:54 +0000454ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000455ClangUserExpression::Execute (Stream &error_stream,
456 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000457 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000458 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000459 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000460{
Jim Ingham7812e012011-01-18 22:20:08 +0000461 // The expression log is quite verbose, and if you're just tracking the execution of the
462 // expression, it's quite convenient to have these logs come out with the STEP log as well.
463 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000464
Sean Callanan47dc4572011-09-15 02:13:07 +0000465 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000466 {
467 lldb::addr_t struct_address;
468
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000469 lldb::addr_t object_ptr = 0;
470 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000471
Sean Callanan047923c2010-12-14 00:42:36 +0000472 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000473 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000474
Jim Inghamea9d4262010-11-05 19:25:48 +0000475 const bool stop_others = true;
476 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000477
Greg Claytond0882d02011-01-19 23:00:49 +0000478 Address wrapper_address (NULL, m_jit_start_addr);
Greg Clayton567e7f32011-09-22 04:58:26 +0000479 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan3aa7da52010-12-13 22:46:15 +0000480 wrapper_address,
481 struct_address,
482 stop_others,
483 discard_on_error,
484 (m_needs_object_ptr ? &object_ptr : NULL),
485 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
486 shared_ptr_to_me));
487
Jim Ingham360f53f2010-11-30 02:22:11 +0000488 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000489 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000490
491 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000492
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000493 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000494
Greg Clayton61468e82011-01-19 07:54:15 +0000495 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000496
497 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000498 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000499
Greg Clayton567e7f32011-09-22 04:58:26 +0000500 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
501 call_plan_sp,
502 stop_others,
503 try_all_threads,
504 discard_on_error,
505 single_thread_timeout_usec,
506 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000507
508 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000509 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000510
Greg Claytonb3448432011-03-24 21:19:54 +0000511 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000512 {
Jim Ingham2370a972011-05-17 01:10:11 +0000513 const char *error_desc = NULL;
514
515 if (call_plan_sp)
516 {
517 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
518 if (real_stop_info_sp)
519 error_desc = real_stop_info_sp->GetDescription();
520 }
521 if (error_desc)
522 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000523 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000524 error_stream.Printf ("Execution was interrupted.");
Jim Ingham2370a972011-05-17 01:10:11 +0000525
526 if (discard_on_error)
527 error_stream.Printf ("\nThe process has been returned to the state before execution.");
528 else
529 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000530
531 return execution_result;
532 }
Greg Claytonb3448432011-03-24 21:19:54 +0000533 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000534 {
535 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
536 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000537 }
538
Sean Callanan0ddf8062011-05-09 22:04:36 +0000539 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000540 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000541 else
Greg Claytonb3448432011-03-24 21:19:54 +0000542 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000543 }
544 else
545 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000546 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytonb3448432011-03-24 21:19:54 +0000547 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000548 }
549}
550
Greg Claytonb3448432011-03-24 21:19:54 +0000551ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000552ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
553 lldb_private::ExecutionPolicy execution_policy,
Jim Inghamea9d4262010-11-05 19:25:48 +0000554 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000555 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000556 const char *expr_prefix,
557 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000558{
Jim Inghamec07c0d2011-08-09 00:00:49 +0000559 Error error;
Sean Callanan47dc4572011-09-15 02:13:07 +0000560 return EvaluateWithError (exe_ctx, execution_policy, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Inghamec07c0d2011-08-09 00:00:49 +0000561}
562
563ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000564ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
565 lldb_private::ExecutionPolicy execution_policy,
566 bool discard_on_error,
567 const char *expr_cstr,
568 const char *expr_prefix,
569 lldb::ValueObjectSP &result_valobj_sp,
570 Error &error)
Jim Inghamec07c0d2011-08-09 00:00:49 +0000571{
Jim Ingham7812e012011-01-18 22:20:08 +0000572 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000573
Greg Claytonb3448432011-03-24 21:19:54 +0000574 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000575
Greg Clayton567e7f32011-09-22 04:58:26 +0000576 Process *process = exe_ctx.GetProcessPtr();
577
578 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +0000579 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000580 if (execution_policy == eExecutionPolicyAlways)
581 {
582 if (log)
583 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Ingham360f53f2010-11-30 02:22:11 +0000584
Sean Callanan47dc4572011-09-15 02:13:07 +0000585 error.SetErrorString ("expression needed to run but couldn't");
586
587 return execution_results;
588 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000589 }
Sean Callananf7649bb2011-09-15 17:43:00 +0000590
Greg Clayton567e7f32011-09-22 04:58:26 +0000591 if (process == NULL || !process->CanJIT())
Sean Callananf7649bb2011-09-15 17:43:00 +0000592 execution_policy = eExecutionPolicyNever;
Greg Clayton0baa3942010-11-04 01:54:29 +0000593
Jim Ingham360f53f2010-11-30 02:22:11 +0000594 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
595
Greg Clayton377e0b42010-10-05 00:31:29 +0000596 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000597
Sean Callanan94d255f2010-12-07 22:55:01 +0000598 if (log)
599 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
600
Sean Callanan47dc4572011-09-15 02:13:07 +0000601 const bool keep_expression_in_memory = true;
602
603 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), execution_policy, keep_expression_in_memory))
Greg Clayton377e0b42010-10-05 00:31:29 +0000604 {
605 if (error_stream.GetString().empty())
606 error.SetErrorString ("expression failed to parse, unknown error");
607 else
608 error.SetErrorString (error_stream.GetString().c_str());
609 }
610 else
611 {
Greg Clayton427f2902010-12-14 02:59:59 +0000612 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000613
Sean Callanan47dc4572011-09-15 02:13:07 +0000614 if (user_expression_sp->EvaluatedStatically())
Greg Clayton377e0b42010-10-05 00:31:29 +0000615 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000616 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000617 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000618
Sean Callanan47dc4572011-09-15 02:13:07 +0000619 if (user_expression_sp->m_const_result)
620 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
621 else
622 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
623
Jim Inghamec07c0d2011-08-09 00:00:49 +0000624 execution_results = eExecutionCompleted;
Greg Clayton377e0b42010-10-05 00:31:29 +0000625 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000626 else if (execution_policy == eExecutionPolicyNever)
627 {
628 if (log)
629 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
630
631 if (error_stream.GetString().empty())
632 error.SetErrorString ("expression needed to run but couldn't");
633 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000634 else
635 {
636 error_stream.GetString().clear();
637
638 if (log)
639 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
640
641 execution_results = user_expression_sp->Execute (error_stream,
642 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000643 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000644 user_expression_sp,
645 expr_result);
646
Greg Claytonb3448432011-03-24 21:19:54 +0000647 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000648 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000649 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000650 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
651
652 if (error_stream.GetString().empty())
653 error.SetErrorString ("expression failed to execute, unknown error");
654 else
655 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000656 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000657 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000658 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000659 if (expr_result)
660 {
661 result_valobj_sp = expr_result->GetValueObject();
662
663 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000664 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000665 }
666 else
667 {
668 if (log)
669 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
670
Sean Callanan24312442011-08-23 21:20:51 +0000671 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000672 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000673 }
674 }
675 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000676
Greg Claytond1719722010-10-05 03:13:51 +0000677 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000678 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000679
Jim Ingham360f53f2010-11-30 02:22:11 +0000680 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000681}