blob: c2164c80037c4fd9cd3e2a14ab5021a056961e1e [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,
Sean Callanan5b658cc2011-11-07 23:35:40 +000047 const char *expr_prefix,
Sean Callanandaa6efe2011-12-21 22:22:58 +000048 lldb::LanguageType language,
49 ResultType desired_type) :
Greg Claytond0882d02011-01-19 23:00:49 +000050 ClangExpression (),
51 m_expr_text (expr),
52 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callanan5b658cc2011-11-07 23:35:40 +000053 m_language (language),
Greg Claytond0882d02011-01-19 23:00:49 +000054 m_transformed_text (),
Sean Callanandaa6efe2011-12-21 22:22:58 +000055 m_desired_type (desired_type),
Greg Claytond0882d02011-01-19 23:00:49 +000056 m_cplusplus (false),
57 m_objectivec (false),
58 m_needs_object_ptr (false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000059 m_const_object (false),
Sean Callanane6ea5fe2011-11-15 02:11:17 +000060 m_static_method(false),
Daniel Dunbar97c89572011-10-31 22:50:49 +000061 m_target (NULL),
Sean Callanan47dc4572011-09-15 02:13:07 +000062 m_evaluated_statically (false),
Sean Callanan5ed59a72011-12-13 01:42:04 +000063 m_const_result (),
64 m_enforce_valid_object (false)
Sean Callanan65dafa82010-08-27 01:01:44 +000065{
Sean Callanan5b658cc2011-11-07 23:35:40 +000066 switch (m_language)
67 {
68 case lldb::eLanguageTypeC_plus_plus:
69 m_allow_cxx = true;
70 break;
71 case lldb::eLanguageTypeObjC:
72 m_allow_objc = true;
73 break;
74 case lldb::eLanguageTypeObjC_plus_plus:
75 default:
76 m_allow_cxx = true;
77 m_allow_objc = true;
78 break;
79 }
Sean Callanan65dafa82010-08-27 01:01:44 +000080}
81
Sean Callanan830a9032010-08-27 23:31:21 +000082ClangUserExpression::~ClangUserExpression ()
83{
84}
85
Sean Callanan65dafa82010-08-27 01:01:44 +000086clang::ASTConsumer *
87ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan036fa902011-08-24 22:18:12 +000088{
Sean Callanan24312442011-08-23 21:20:51 +000089 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
90
91 if (!clang_ast_context)
92 return NULL;
93
Sean Callanan060d53f2011-10-08 00:21:35 +000094 if (!m_result_synthesizer.get())
95 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanane1301a62011-12-06 03:41:14 +000096 *m_target));
Sean Callanan060d53f2011-10-08 00:21:35 +000097
98 return m_result_synthesizer.get();
Sean Callanan65dafa82010-08-27 01:01:44 +000099}
100
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000101void
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000102ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000103{
Greg Clayton567e7f32011-09-22 04:58:26 +0000104 m_target = exe_ctx.GetTargetPtr();
Greg Clayton144188b2011-09-12 23:21:58 +0000105
Sean Callanan5b658cc2011-11-07 23:35:40 +0000106 if (!(m_allow_cxx || m_allow_objc))
107 return;
108
Greg Clayton567e7f32011-09-22 04:58:26 +0000109 StackFrame *frame = exe_ctx.GetFramePtr();
110 if (frame == NULL)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000111 return;
112
Greg Clayton567e7f32011-09-22 04:58:26 +0000113 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanane8e55572010-12-01 21:35:54 +0000114
Sean Callananc617a4c2011-08-05 23:43:37 +0000115 if (!sym_ctx.function)
116 return;
117
118 clang::DeclContext *decl_context;
119
120 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
121 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
122 else
123 decl_context = sym_ctx.function->GetClangDeclContext();
124
125 if (!decl_context)
126 return;
Sean Callanan036fa902011-08-24 22:18:12 +0000127
Sean Callananc617a4c2011-08-05 23:43:37 +0000128 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000129 {
Sean Callanan5b658cc2011-11-07 23:35:40 +0000130 if (m_allow_cxx && method_decl->isInstance())
Sean Callanane8e55572010-12-01 21:35:54 +0000131 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000132 if (m_enforce_valid_object)
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000133 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000134 VariableList *vars = frame->GetVariableList(false);
135
136 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
137
138 if (!vars)
139 {
140 err.SetErrorToGenericError();
141 err.SetErrorString(thisErrorString);
142 return;
143 }
144
145 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
146
147 if (!this_var ||
148 !this_var->IsInScope(frame) ||
149 !this_var->LocationIsValidForFrame (frame))
150 {
151 err.SetErrorToGenericError();
152 err.SetErrorString(thisErrorString);
153 return;
154 }
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000155 }
156
Sean Callananc617a4c2011-08-05 23:43:37 +0000157 m_cplusplus = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000158 m_needs_object_ptr = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000159
Sean Callananc617a4c2011-08-05 23:43:37 +0000160 do {
161 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
162
Sean Callanan21f2e192011-12-14 01:13:04 +0000163 const clang::PointerType *this_pointer_type = this_type->getAs<clang::PointerType>();
Sean Callananc617a4c2011-08-05 23:43:37 +0000164
165 if (!this_pointer_type)
166 break;
167
168 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
169 } while (0);
Sean Callanane8e55572010-12-01 21:35:54 +0000170 }
171 }
Sean Callananc617a4c2011-08-05 23:43:37 +0000172 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000173 {
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000174 if (m_allow_objc)
Sean Callanande3d27e2011-09-26 18:45:31 +0000175 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000176 if (m_enforce_valid_object)
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000177 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000178 VariableList *vars = frame->GetVariableList(false);
179
180 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
181
182 if (!vars)
183 {
184 err.SetErrorToGenericError();
185 err.SetErrorString(selfErrorString);
186 return;
187 }
188
189 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
190
191 if (!self_var ||
192 !self_var->IsInScope(frame) ||
193 !self_var->LocationIsValidForFrame (frame))
194 {
195 err.SetErrorToGenericError();
196 err.SetErrorString(selfErrorString);
197 return;
198 }
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000199 }
200
Sean Callananc617a4c2011-08-05 23:43:37 +0000201 m_objectivec = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000202 m_needs_object_ptr = true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000203
204 if (!method_decl->isInstanceMethod())
205 m_static_method = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000206 }
Sean Callanane8e55572010-12-01 21:35:54 +0000207 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000208}
209
Sean Callanan550f2762010-10-22 23:25:16 +0000210// This is a really nasty hack, meant to fix Objective-C expressions of the form
211// (int)[myArray count]. Right now, because the type information for count is
212// not available, [myArray count] returns id, which can't be directly cast to
213// int without causing a clang error.
214static void
215ApplyObjcCastHack(std::string &expr)
216{
217#define OBJC_CAST_HACK_FROM "(int)["
218#define OBJC_CAST_HACK_TO "(int)(long long)["
219
220 size_t from_offset;
221
222 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
223 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
224
225#undef OBJC_CAST_HACK_TO
226#undef OBJC_CAST_HACK_FROM
227}
228
Sean Callanan30892372010-10-24 20:45:49 +0000229// Another hack, meant to allow use of unichar despite it not being available in
230// the type information. Although we could special-case it in type lookup,
231// hopefully we'll figure out a way to #include the same environment as is
232// present in the original source file rather than try to hack specific type
233// definitions in as needed.
234static void
235ApplyUnicharHack(std::string &expr)
236{
237#define UNICHAR_HACK_FROM "unichar"
238#define UNICHAR_HACK_TO "unsigned short"
239
240 size_t from_offset;
241
242 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
243 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
244
245#undef UNICHAR_HACK_TO
246#undef UNICHAR_HACK_FROM
247}
248
Sean Callanan550f2762010-10-22 23:25:16 +0000249bool
Sean Callanana91dd992010-11-19 02:52:21 +0000250ClangUserExpression::Parse (Stream &error_stream,
251 ExecutionContext &exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +0000252 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000253 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000254{
Greg Claytone005f2c2010-11-06 01:53:30 +0000255 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000256
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000257 Error err;
258
259 ScanContext(exe_ctx, err);
260
261 if (!err.Success())
262 {
263 error_stream.Printf("warning: %s\n", err.AsCString());
264 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000265
266 StreamString m_transformed_stream;
267
268 ////////////////////////////////////
269 // Generate the expression
270 //
Sean Callanan550f2762010-10-22 23:25:16 +0000271
272 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000273 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000274
Sean Callanande3d27e2011-09-26 18:45:31 +0000275 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
276
277 lldb::LanguageType lang_type;
278
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000279 if (m_cplusplus)
Sean Callanande3d27e2011-09-26 18:45:31 +0000280 lang_type = lldb::eLanguageTypeC_plus_plus;
281 else if(m_objectivec)
282 lang_type = lldb::eLanguageTypeObjC;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000283 else
Sean Callanande3d27e2011-09-26 18:45:31 +0000284 lang_type = lldb::eLanguageTypeC;
285
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000286 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000287 {
Sean Callanande3d27e2011-09-26 18:45:31 +0000288 error_stream.PutCString ("error: couldn't construct expression body");
289 return false;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000290 }
291
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000292 if (log)
293 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
294
Sean Callanan65dafa82010-08-27 01:01:44 +0000295 ////////////////////////////////////
296 // Set up the target and compiler
297 //
298
Greg Clayton567e7f32011-09-22 04:58:26 +0000299 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan65dafa82010-08-27 01:01:44 +0000300
301 if (!target)
302 {
303 error_stream.PutCString ("error: invalid target\n");
304 return false;
305 }
306
Sean Callanan65dafa82010-08-27 01:01:44 +0000307 //////////////////////////
308 // Parse the expression
309 //
Sean Callanandaa6efe2011-12-21 22:22:58 +0000310
Sean Callanan73b520f2011-10-29 01:58:46 +0000311 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callananaa301c42010-12-03 01:38:59 +0000312
Sean Callanan166ba102011-08-01 18:18:33 +0000313 if (!m_expr_decl_map->WillParse(exe_ctx))
314 {
315 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
316 return false;
317 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000318
Greg Clayton567e7f32011-09-22 04:58:26 +0000319 Process *process = exe_ctx.GetProcessPtr();
320 ClangExpressionParser parser(process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000321
322 unsigned num_errors = parser.Parse (error_stream);
323
324 if (num_errors)
325 {
326 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000327
328 m_expr_decl_map->DidParse();
329
Sean Callanan65dafa82010-08-27 01:01:44 +0000330 return false;
331 }
332
Sean Callanan47dc4572011-09-15 02:13:07 +0000333 //////////////////////////////////////////////////////////////////////////////////////////
334 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan65dafa82010-08-27 01:01:44 +0000335 //
Sean Callanan65dafa82010-08-27 01:01:44 +0000336
Greg Clayton567e7f32011-09-22 04:58:26 +0000337 if (execution_policy != eExecutionPolicyNever && process)
338 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan65dafa82010-08-27 01:01:44 +0000339
Sean Callanan47dc4572011-09-15 02:13:07 +0000340 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
341 m_jit_start_addr,
342 m_jit_end_addr,
343 exe_ctx,
344 m_data_allocator.get(),
345 m_evaluated_statically,
346 m_const_result,
347 execution_policy);
Sean Callanan65dafa82010-08-27 01:01:44 +0000348
Sean Callanan47dc4572011-09-15 02:13:07 +0000349 if (log && m_data_allocator.get())
Sean Callananc0492742011-05-23 21:40:23 +0000350 {
351 StreamString dump_string;
352 m_data_allocator->Dump(dump_string);
353
354 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
355 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000356
Sean Callanan65dafa82010-08-27 01:01:44 +0000357 if (jit_error.Success())
358 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000359 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
360 m_jit_process_sp = process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000361 return true;
362 }
363 else
364 {
Greg Clayton30581972011-05-17 03:51:29 +0000365 const char *error_cstr = jit_error.AsCString();
366 if (error_cstr && error_cstr[0])
367 error_stream.Printf ("error: %s\n", error_cstr);
368 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000369 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan65dafa82010-08-27 01:01:44 +0000370 return false;
371 }
372}
373
374bool
Jim Inghamd1686902010-10-14 23:45:03 +0000375ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000376 ExecutionContext &exe_ctx,
377 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000378 lldb::addr_t &object_ptr,
379 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000380{
Greg Claytone005f2c2010-11-06 01:53:30 +0000381 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000382
Greg Claytond0882d02011-01-19 23:00:49 +0000383 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000384 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000385 Error materialize_error;
386
Sean Callanan3aa7da52010-12-13 22:46:15 +0000387 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000388 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000389 ConstString object_name;
390
391 if (m_cplusplus)
392 {
393 object_name.SetCString("this");
394 }
395 else if (m_objectivec)
396 {
397 object_name.SetCString("self");
398 }
399 else
400 {
401 error_stream.Printf("Need object pointer but don't know the language\n");
402 return false;
403 }
404
405 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
406 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000407 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
408 object_ptr = 0;
Sean Callanan3aa7da52010-12-13 22:46:15 +0000409 }
Sean Callanan047923c2010-12-14 00:42:36 +0000410
411 if (m_objectivec)
412 {
413 ConstString cmd_name("_cmd");
414
415 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
416 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000417 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
418 cmd_ptr = 0;
Sean Callanan047923c2010-12-14 00:42:36 +0000419 }
420 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000421 }
422
Sean Callananaa301c42010-12-03 01:38:59 +0000423 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000424 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000425 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000426 return false;
427 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000428
429#if 0
430 // jingham: look here
431 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000432 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 +0000433#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000434
435 if (log)
436 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000437 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000438
Greg Claytond0882d02011-01-19 23:00:49 +0000439 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000440
441 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000442 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000443
Sean Callanan33711022010-12-07 10:00:20 +0000444 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000445
446 StreamString args;
447
448 Error dump_error;
449
Sean Callanane8a59a82010-09-13 21:34:21 +0000450 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000451 {
Sean Callananaa301c42010-12-03 01:38:59 +0000452 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000453 {
Sean Callanan33711022010-12-07 10:00:20 +0000454 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000455 }
456 else
457 {
Sean Callanan33711022010-12-07 10:00:20 +0000458 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000459 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000460 }
461 }
Jim Inghamd1686902010-10-14 23:45:03 +0000462 }
463 return true;
464}
465
466ThreadPlan *
467ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000468 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000469{
470 lldb::addr_t struct_address;
471
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000472 lldb::addr_t object_ptr = 0;
473 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000474
Sean Callanan047923c2010-12-14 00:42:36 +0000475 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000476
Jim Ingham360f53f2010-11-30 02:22:11 +0000477 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
478 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000479 // forcing unwind_on_error to be true here, in practical terms that can't happen.
480
Jim Inghamd1686902010-10-14 23:45:03 +0000481 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000482 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000483 struct_address,
484 error_stream,
485 true,
486 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000487 (m_needs_object_ptr ? &object_ptr : NULL),
488 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000489}
490
491bool
492ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
493 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000494 lldb::ClangExpressionVariableSP &result,
495 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000496{
497 Error expr_error;
498
Sean Callanan33711022010-12-07 10:00:20 +0000499 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
500
501 if (log)
502 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000503 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000504
505 StreamString args;
506
507 Error dump_error;
508
509 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
510 {
511 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
512 }
513 else
514 {
515 log->Printf(" Structure contents:\n%s", args.GetData());
516 }
517 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000518
519 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
520
Sean Callanan33711022010-12-07 10:00:20 +0000521
Sean Callanan0ddf8062011-05-09 22:04:36 +0000522 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000523 {
524 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
525 return false;
526 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000527
Jim Inghamd1686902010-10-14 23:45:03 +0000528 return true;
529}
530
Greg Claytonb3448432011-03-24 21:19:54 +0000531ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000532ClangUserExpression::Execute (Stream &error_stream,
533 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000534 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000535 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000536 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000537{
Jim Ingham7812e012011-01-18 22:20:08 +0000538 // The expression log is quite verbose, and if you're just tracking the execution of the
539 // expression, it's quite convenient to have these logs come out with the STEP log as well.
540 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000541
Sean Callanan47dc4572011-09-15 02:13:07 +0000542 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000543 {
544 lldb::addr_t struct_address;
545
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000546 lldb::addr_t object_ptr = 0;
547 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000548
Sean Callanan047923c2010-12-14 00:42:36 +0000549 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000550 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000551
Jim Inghamea9d4262010-11-05 19:25:48 +0000552 const bool stop_others = true;
553 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000554
Greg Claytond0882d02011-01-19 23:00:49 +0000555 Address wrapper_address (NULL, m_jit_start_addr);
Greg Clayton567e7f32011-09-22 04:58:26 +0000556 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan3aa7da52010-12-13 22:46:15 +0000557 wrapper_address,
558 struct_address,
559 stop_others,
560 discard_on_error,
561 (m_needs_object_ptr ? &object_ptr : NULL),
562 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
563 shared_ptr_to_me));
564
Jim Ingham360f53f2010-11-30 02:22:11 +0000565 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000566 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000567
568 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000569
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000570 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000571
Greg Clayton61468e82011-01-19 07:54:15 +0000572 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000573
574 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000575 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000576
Jim Ingham0296fe72011-11-08 03:00:11 +0000577 if (exe_ctx.GetProcessPtr())
578 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
579
Greg Clayton567e7f32011-09-22 04:58:26 +0000580 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
581 call_plan_sp,
582 stop_others,
583 try_all_threads,
584 discard_on_error,
585 single_thread_timeout_usec,
586 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000587
Jim Ingham0296fe72011-11-08 03:00:11 +0000588 if (exe_ctx.GetProcessPtr())
589 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
590
Sean Callanan33711022010-12-07 10:00:20 +0000591 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000592 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000593
Greg Claytonb3448432011-03-24 21:19:54 +0000594 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000595 {
Jim Ingham2370a972011-05-17 01:10:11 +0000596 const char *error_desc = NULL;
597
598 if (call_plan_sp)
599 {
600 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
601 if (real_stop_info_sp)
602 error_desc = real_stop_info_sp->GetDescription();
603 }
604 if (error_desc)
605 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000606 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000607 error_stream.Printf ("Execution was interrupted.");
Jim Ingham2370a972011-05-17 01:10:11 +0000608
609 if (discard_on_error)
610 error_stream.Printf ("\nThe process has been returned to the state before execution.");
611 else
612 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000613
614 return execution_result;
615 }
Greg Claytonb3448432011-03-24 21:19:54 +0000616 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000617 {
618 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
619 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000620 }
621
Sean Callanan0ddf8062011-05-09 22:04:36 +0000622 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000623 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000624 else
Greg Claytonb3448432011-03-24 21:19:54 +0000625 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000626 }
627 else
628 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000629 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytonb3448432011-03-24 21:19:54 +0000630 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000631 }
632}
633
Greg Claytonb3448432011-03-24 21:19:54 +0000634ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000635ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
636 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000637 lldb::LanguageType language,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000638 ResultType desired_type,
Jim Inghamea9d4262010-11-05 19:25:48 +0000639 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000640 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000641 const char *expr_prefix,
642 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000643{
Jim Inghamec07c0d2011-08-09 00:00:49 +0000644 Error error;
Sean Callanandaa6efe2011-12-21 22:22:58 +0000645 return EvaluateWithError (exe_ctx, execution_policy, language, desired_type, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Inghamec07c0d2011-08-09 00:00:49 +0000646}
647
648ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000649ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
650 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000651 lldb::LanguageType language,
Sean Callanandaa6efe2011-12-21 22:22:58 +0000652 ResultType desired_type,
Sean Callanan47dc4572011-09-15 02:13:07 +0000653 bool discard_on_error,
654 const char *expr_cstr,
655 const char *expr_prefix,
656 lldb::ValueObjectSP &result_valobj_sp,
657 Error &error)
Jim Inghamec07c0d2011-08-09 00:00:49 +0000658{
Jim Ingham7812e012011-01-18 22:20:08 +0000659 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000660
Greg Claytonb3448432011-03-24 21:19:54 +0000661 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000662
Greg Clayton567e7f32011-09-22 04:58:26 +0000663 Process *process = exe_ctx.GetProcessPtr();
664
665 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +0000666 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000667 if (execution_policy == eExecutionPolicyAlways)
668 {
669 if (log)
670 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Ingham360f53f2010-11-30 02:22:11 +0000671
Sean Callanan47dc4572011-09-15 02:13:07 +0000672 error.SetErrorString ("expression needed to run but couldn't");
673
674 return execution_results;
675 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000676 }
Sean Callananf7649bb2011-09-15 17:43:00 +0000677
Greg Clayton567e7f32011-09-22 04:58:26 +0000678 if (process == NULL || !process->CanJIT())
Sean Callananf7649bb2011-09-15 17:43:00 +0000679 execution_policy = eExecutionPolicyNever;
Greg Clayton0baa3942010-11-04 01:54:29 +0000680
Sean Callanandaa6efe2011-12-21 22:22:58 +0000681 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Ingham360f53f2010-11-30 02:22:11 +0000682
Greg Clayton377e0b42010-10-05 00:31:29 +0000683 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000684
Sean Callanan94d255f2010-12-07 22:55:01 +0000685 if (log)
686 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
687
Sean Callanan47dc4572011-09-15 02:13:07 +0000688 const bool keep_expression_in_memory = true;
689
Sean Callanandaa6efe2011-12-21 22:22:58 +0000690 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton377e0b42010-10-05 00:31:29 +0000691 {
692 if (error_stream.GetString().empty())
693 error.SetErrorString ("expression failed to parse, unknown error");
694 else
695 error.SetErrorString (error_stream.GetString().c_str());
696 }
697 else
698 {
Greg Clayton427f2902010-12-14 02:59:59 +0000699 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000700
Sean Callanan47dc4572011-09-15 02:13:07 +0000701 if (user_expression_sp->EvaluatedStatically())
Greg Clayton377e0b42010-10-05 00:31:29 +0000702 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000703 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000704 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000705
Sean Callanan47dc4572011-09-15 02:13:07 +0000706 if (user_expression_sp->m_const_result)
707 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
708 else
709 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
710
Jim Inghamec07c0d2011-08-09 00:00:49 +0000711 execution_results = eExecutionCompleted;
Greg Clayton377e0b42010-10-05 00:31:29 +0000712 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000713 else if (execution_policy == eExecutionPolicyNever)
714 {
715 if (log)
716 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
717
718 if (error_stream.GetString().empty())
719 error.SetErrorString ("expression needed to run but couldn't");
720 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000721 else
722 {
723 error_stream.GetString().clear();
724
725 if (log)
726 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
727
728 execution_results = user_expression_sp->Execute (error_stream,
729 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000730 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000731 user_expression_sp,
732 expr_result);
733
Greg Claytonb3448432011-03-24 21:19:54 +0000734 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000735 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000736 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000737 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
738
739 if (error_stream.GetString().empty())
740 error.SetErrorString ("expression failed to execute, unknown error");
741 else
742 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000743 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000744 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000745 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000746 if (expr_result)
747 {
748 result_valobj_sp = expr_result->GetValueObject();
749
750 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000751 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000752 }
753 else
754 {
755 if (log)
756 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
757
Sean Callanan24312442011-08-23 21:20:51 +0000758 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000759 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000760 }
761 }
762 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000763
Greg Claytond1719722010-10-05 03:13:51 +0000764 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000765 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000766
Jim Ingham360f53f2010-11-30 02:22:11 +0000767 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000768}