blob: 7719f0815187339c4bc50fed9c9767d0bf5c7d5c [file] [log] [blame]
Sean Callanan79763a42011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan1a8d4092010-08-27 01:01:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17#include <cstdlib>
18#include <string>
19#include <map>
20
21#include "lldb/Core/ConstString.h"
22#include "lldb/Core/Log.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000023#include "lldb/Core/StreamFile.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000024#include "lldb/Core/StreamString.h"
Greg Claytonb71f3842010-10-05 03:13:51 +000025#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000026#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000027#include "lldb/Expression/ClangExpressionDeclMap.h"
28#include "lldb/Expression/ClangExpressionParser.h"
29#include "lldb/Expression/ClangFunction.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000030#include "lldb/Expression/ClangUserExpression.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000031#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Host/Host.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000033#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000034#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000035#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000036#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000037#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000038#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000040
Sean Callanan72e49402011-08-05 23:43:37 +000041#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
Sean Callanan1a8d4092010-08-27 01:01:44 +000044using namespace lldb_private;
45
Sean Callanan322f5292010-10-29 00:29:03 +000046ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000047 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000048 lldb::LanguageType language,
49 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000050 ClangExpression (),
51 m_expr_text (expr),
52 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000053 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000054 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000055 m_desired_type (desired_type),
Greg Clayton22a939a2011-01-19 23:00:49 +000056 m_cplusplus (false),
57 m_objectivec (false),
58 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000059 m_const_object (false),
Sean Callanand5c17ed2011-11-15 02:11:17 +000060 m_static_method(false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000061 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000062 m_evaluated_statically (false),
Sean Callanand5cc1322011-12-13 01:42:04 +000063 m_const_result (),
64 m_enforce_valid_object (false)
Sean Callanan1a8d4092010-08-27 01:01:44 +000065{
Sean Callananc7b65062011-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 Callanan1a8d4092010-08-27 01:01:44 +000080}
81
Sean Callanane71d5532010-08-27 23:31:21 +000082ClangUserExpression::~ClangUserExpression ()
83{
84}
85
Sean Callanan1a8d4092010-08-27 01:01:44 +000086clang::ASTConsumer *
87ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000088{
Sean Callananbccce812011-08-23 21:20:51 +000089 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
90
91 if (!clang_ast_context)
92 return NULL;
93
Sean Callanan2590b9a2011-10-08 00:21:35 +000094 if (!m_result_synthesizer.get())
95 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanan0eed0d42011-12-06 03:41:14 +000096 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +000097
98 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +000099}
100
Sean Callananfc55f5d2010-09-21 00:44:12 +0000101void
Sean Callanan744756e2011-11-04 02:09:33 +0000102ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000103{
Greg Claytonc14ee322011-09-22 04:58:26 +0000104 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000105
Sean Callananc7b65062011-11-07 23:35:40 +0000106 if (!(m_allow_cxx || m_allow_objc))
107 return;
108
Greg Claytonc14ee322011-09-22 04:58:26 +0000109 StackFrame *frame = exe_ctx.GetFramePtr();
110 if (frame == NULL)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000111 return;
112
Greg Claytonc14ee322011-09-22 04:58:26 +0000113 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanan3670ba52010-12-01 21:35:54 +0000114
Sean Callanan72e49402011-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 Callanan737330c2011-08-24 22:18:12 +0000127
Sean Callanan72e49402011-08-05 23:43:37 +0000128 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000129 {
Sean Callananc7b65062011-11-07 23:35:40 +0000130 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000131 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000132 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000133 {
Sean Callanand5cc1322011-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 Callanan744756e2011-11-04 02:09:33 +0000155 }
156
Sean Callanan72e49402011-08-05 23:43:37 +0000157 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000158 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000159
Sean Callanan72e49402011-08-05 23:43:37 +0000160 do {
161 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
162
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000163 const clang::PointerType *this_pointer_type = this_type->getAs<clang::PointerType>();
Sean Callanan72e49402011-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 Callanan3670ba52010-12-01 21:35:54 +0000170 }
171 }
Sean Callanan72e49402011-08-05 23:43:37 +0000172 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000173 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000174 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000175 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000176 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000177 {
Sean Callanand5cc1322011-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 Callanan744756e2011-11-04 02:09:33 +0000199 }
200
Sean Callanan72e49402011-08-05 23:43:37 +0000201 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000202 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000203
204 if (!method_decl->isInstanceMethod())
205 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000206 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000207 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000208}
209
Sean Callanancf5498f2010-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 Callanan64186e72010-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 Callanancf5498f2010-10-22 23:25:16 +0000249bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000250ClangUserExpression::Parse (Stream &error_stream,
251 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000252 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000253 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000254{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000255 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000256
Sean Callanan744756e2011-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 Callananfc55f5d2010-09-21 00:44:12 +0000265
266 StreamString m_transformed_stream;
267
268 ////////////////////////////////////
269 // Generate the expression
270 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000271
272 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000273 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000274
Sean Callanan9bc83842011-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 Callananfc55f5d2010-09-21 00:44:12 +0000279 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000280 lang_type = lldb::eLanguageTypeC_plus_plus;
281 else if(m_objectivec)
282 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000283 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000284 lang_type = lldb::eLanguageTypeC;
285
Sean Callanand5c17ed2011-11-15 02:11:17 +0000286 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000287 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000288 error_stream.PutCString ("error: couldn't construct expression body");
289 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000290 }
291
Sean Callananfc55f5d2010-09-21 00:44:12 +0000292 if (log)
293 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
294
Sean Callanan1a8d4092010-08-27 01:01:44 +0000295 ////////////////////////////////////
296 // Set up the target and compiler
297 //
298
Greg Claytonc14ee322011-09-22 04:58:26 +0000299 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000300
301 if (!target)
302 {
303 error_stream.PutCString ("error: invalid target\n");
304 return false;
305 }
306
Sean Callanan1a8d4092010-08-27 01:01:44 +0000307 //////////////////////////
308 // Parse the expression
309 //
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000310
Sean Callanan1ee44b72011-10-29 01:58:46 +0000311 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000312
Sean Callananb9951192011-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 Callanan1a8d4092010-08-27 01:01:44 +0000318
Greg Claytonc14ee322011-09-22 04:58:26 +0000319 Process *process = exe_ctx.GetProcessPtr();
320 ClangExpressionParser parser(process, *this);
Sean Callanan1a8d4092010-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 Callanan979f74d2010-12-03 01:38:59 +0000327
328 m_expr_decl_map->DidParse();
329
Sean Callanan1a8d4092010-08-27 01:01:44 +0000330 return false;
331 }
332
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000333 //////////////////////////////////////////////////////////////////////////////////////////
334 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000335 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000336
Greg Claytonc14ee322011-09-22 04:58:26 +0000337 if (execution_policy != eExecutionPolicyNever && process)
338 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000339
Sean Callanan3bfdaa22011-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 Callanan1a8d4092010-08-27 01:01:44 +0000348
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000349 if (log && m_data_allocator.get())
Sean Callanan79763a42011-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 Callanane3aef1d2011-10-12 22:20:02 +0000356
Sean Callanan1a8d4092010-08-27 01:01:44 +0000357 if (jit_error.Success())
358 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000359 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
360 m_jit_process_sp = process->GetSP();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000361 return true;
362 }
363 else
364 {
Greg Claytone6a9e432011-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 Molendafd54b362011-09-20 21:44:10 +0000369 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000370 return false;
371 }
372}
373
374bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000375ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000376 ExecutionContext &exe_ctx,
377 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000378 lldb::addr_t &object_ptr,
379 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000380{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000381 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000382
Greg Clayton22a939a2011-01-19 23:00:49 +0000383 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000384 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000385 Error materialize_error;
386
Sean Callanan17827832010-12-13 22:46:15 +0000387 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000388 {
Sean Callanan17827832010-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 Callanand5cc1322011-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 Callanan17827832010-12-13 22:46:15 +0000409 }
Sean Callanan9d48e802010-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 Callanand5cc1322011-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 Callanan9d48e802010-12-14 00:42:36 +0000419 }
420 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000421 }
422
Sean Callanan979f74d2010-12-03 01:38:59 +0000423 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000424 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000425 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000426 return false;
427 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000428
429#if 0
430 // jingham: look here
431 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Clayton22a939a2011-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 Claytonc4e411f2011-01-18 19:36:39 +0000433#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000434
435 if (log)
436 {
Sean Callanana162eba2010-12-07 22:55:01 +0000437 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000438
Greg Clayton22a939a2011-01-19 23:00:49 +0000439 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000440
441 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000442 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000443
Sean Callananc673a6e2010-12-07 10:00:20 +0000444 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000445
446 StreamString args;
447
448 Error dump_error;
449
Sean Callanan9e6ed532010-09-13 21:34:21 +0000450 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000451 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000452 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000453 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000454 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000455 }
456 else
457 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000458 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000459 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000460 }
461 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000462 }
463 return true;
464}
465
466ThreadPlan *
467ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000468 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000469{
470 lldb::addr_t struct_address;
471
Johnny Chen44805302011-07-19 19:48:13 +0000472 lldb::addr_t object_ptr = 0;
473 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000474
Sean Callanan9d48e802010-12-14 00:42:36 +0000475 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000476
Jim Inghamf48169b2010-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 Callanan17827832010-12-13 22:46:15 +0000479 // forcing unwind_on_error to be true here, in practical terms that can't happen.
480
Jim Ingham36f3b362010-10-14 23:45:03 +0000481 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000482 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000483 struct_address,
484 error_stream,
485 true,
486 true,
Sean Callanan17827832010-12-13 22:46:15 +0000487 (m_needs_object_ptr ? &object_ptr : NULL),
488 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000489}
490
491bool
492ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
493 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000494 lldb::ClangExpressionVariableSP &result,
495 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000496{
497 Error expr_error;
498
Sean Callananc673a6e2010-12-07 10:00:20 +0000499 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
500
501 if (log)
502 {
Sean Callanana162eba2010-12-07 22:55:01 +0000503 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-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 Callanane359d9b2011-05-09 22:04:36 +0000518
519 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
520
Sean Callananc673a6e2010-12-07 10:00:20 +0000521
Sean Callanane359d9b2011-05-09 22:04:36 +0000522 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-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 Callanane3aef1d2011-10-12 22:20:02 +0000527
Johnny Chenb49440f2012-01-06 00:35:38 +0000528 if (result)
529 result->TransferAddress();
530
Jim Ingham36f3b362010-10-14 23:45:03 +0000531 return true;
532}
533
Greg Claytone0d378b2011-03-24 21:19:54 +0000534ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000535ClangUserExpression::Execute (Stream &error_stream,
536 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000537 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000538 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000539 lldb::ClangExpressionVariableSP &result)
Jim Ingham36f3b362010-10-14 23:45:03 +0000540{
Jim Inghamb086ff72011-01-18 22:20:08 +0000541 // The expression log is quite verbose, and if you're just tracking the execution of the
542 // expression, it's quite convenient to have these logs come out with the STEP log as well.
543 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000544
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000545 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000546 {
547 lldb::addr_t struct_address;
548
Johnny Chen44805302011-07-19 19:48:13 +0000549 lldb::addr_t object_ptr = 0;
550 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000551
Sean Callanan9d48e802010-12-14 00:42:36 +0000552 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytone0d378b2011-03-24 21:19:54 +0000553 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000554
Jim Ingham399f1ca2010-11-05 19:25:48 +0000555 const bool stop_others = true;
556 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000557
Greg Clayton22a939a2011-01-19 23:00:49 +0000558 Address wrapper_address (NULL, m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000559 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000560 wrapper_address,
561 struct_address,
562 stop_others,
563 discard_on_error,
564 (m_needs_object_ptr ? &object_ptr : NULL),
565 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
566 shared_ptr_to_me));
567
Jim Inghamf48169b2010-11-30 02:22:11 +0000568 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000569 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000570
571 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Inghamf48169b2010-11-30 02:22:11 +0000572
Jim Ingham17e5c4e2011-05-17 22:24:54 +0000573 call_plan_sp->SetPrivate(true);
Jim Inghamf48169b2010-11-30 02:22:11 +0000574
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000575 uint32_t single_thread_timeout_usec = 500000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000576
577 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000578 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000579
Jim Ingham0faa43f2011-11-08 03:00:11 +0000580 if (exe_ctx.GetProcessPtr())
581 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
582
Greg Claytonc14ee322011-09-22 04:58:26 +0000583 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
584 call_plan_sp,
585 stop_others,
586 try_all_threads,
587 discard_on_error,
588 single_thread_timeout_usec,
589 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000590
Jim Ingham0faa43f2011-11-08 03:00:11 +0000591 if (exe_ctx.GetProcessPtr())
592 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
593
Sean Callananc673a6e2010-12-07 10:00:20 +0000594 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000595 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000596
Greg Claytone0d378b2011-03-24 21:19:54 +0000597 if (execution_result == eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000598 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000599 const char *error_desc = NULL;
600
601 if (call_plan_sp)
602 {
603 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
604 if (real_stop_info_sp)
605 error_desc = real_stop_info_sp->GetDescription();
606 }
607 if (error_desc)
608 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000609 else
Jason Molendafd54b362011-09-20 21:44:10 +0000610 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000611
612 if (discard_on_error)
613 error_stream.Printf ("\nThe process has been returned to the state before execution.");
614 else
615 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000616
617 return execution_result;
618 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000619 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000620 {
621 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
622 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000623 }
624
Sean Callanane359d9b2011-05-09 22:04:36 +0000625 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000626 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000627 else
Greg Claytone0d378b2011-03-24 21:19:54 +0000628 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000629 }
630 else
631 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000632 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000633 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000634 }
635}
636
Greg Claytone0d378b2011-03-24 21:19:54 +0000637ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000638ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
639 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000640 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000641 ResultType desired_type,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000642 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000643 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000644 const char *expr_prefix,
645 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000646{
Jim Ingham41c75912011-08-09 00:00:49 +0000647 Error error;
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000648 return EvaluateWithError (exe_ctx, execution_policy, language, desired_type, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Ingham41c75912011-08-09 00:00:49 +0000649}
650
651ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000652ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
653 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000654 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000655 ResultType desired_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000656 bool discard_on_error,
657 const char *expr_cstr,
658 const char *expr_prefix,
659 lldb::ValueObjectSP &result_valobj_sp,
660 Error &error)
Jim Ingham41c75912011-08-09 00:00:49 +0000661{
Jim Inghamb086ff72011-01-18 22:20:08 +0000662 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000663
Greg Claytone0d378b2011-03-24 21:19:54 +0000664 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000665
Greg Claytonc14ee322011-09-22 04:58:26 +0000666 Process *process = exe_ctx.GetProcessPtr();
667
668 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000669 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000670 if (execution_policy == eExecutionPolicyAlways)
671 {
672 if (log)
673 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000674
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000675 error.SetErrorString ("expression needed to run but couldn't");
676
677 return execution_results;
678 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000679 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000680
Greg Claytonc14ee322011-09-22 04:58:26 +0000681 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000682 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000683
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000684 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000685
Greg Clayton0184f012010-10-05 00:31:29 +0000686 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000687
Sean Callanana162eba2010-12-07 22:55:01 +0000688 if (log)
689 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
690
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000691 const bool keep_expression_in_memory = true;
692
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000693 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000694 {
695 if (error_stream.GetString().empty())
696 error.SetErrorString ("expression failed to parse, unknown error");
697 else
698 error.SetErrorString (error_stream.GetString().c_str());
699 }
700 else
701 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000702 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000703
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000704 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000705 {
Sean Callanana162eba2010-12-07 22:55:01 +0000706 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000707 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000708
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000709 if (user_expression_sp->m_const_result)
710 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
711 else
712 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
713
Jim Ingham41c75912011-08-09 00:00:49 +0000714 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000715 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000716 else if (execution_policy == eExecutionPolicyNever)
717 {
718 if (log)
719 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
720
721 if (error_stream.GetString().empty())
722 error.SetErrorString ("expression needed to run but couldn't");
723 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000724 else
725 {
726 error_stream.GetString().clear();
727
728 if (log)
729 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
730
731 execution_results = user_expression_sp->Execute (error_stream,
732 exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35 +0000733 discard_on_error,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000734 user_expression_sp,
735 expr_result);
736
Greg Claytone0d378b2011-03-24 21:19:54 +0000737 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000738 {
Sean Callanana162eba2010-12-07 22:55:01 +0000739 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000740 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
741
742 if (error_stream.GetString().empty())
743 error.SetErrorString ("expression failed to execute, unknown error");
744 else
745 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000746 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000747 else
Greg Clayton0184f012010-10-05 00:31:29 +0000748 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000749 if (expr_result)
750 {
751 result_valobj_sp = expr_result->GetValueObject();
752
753 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000754 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000755 }
756 else
757 {
758 if (log)
759 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
760
Sean Callananbccce812011-08-23 21:20:51 +0000761 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000762 }
Greg Clayton0184f012010-10-05 00:31:29 +0000763 }
764 }
765 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000766
Greg Claytonb71f3842010-10-05 03:13:51 +0000767 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000768 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000769
Jim Inghamf48169b2010-11-30 02:22:11 +0000770 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000771}