blob: d228569fa1c4934ddb79c42b6605cc179dbb2e02 [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"
Greg Clayton1f746072012-08-29 21:13:06 +000033#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000034#include "lldb/Symbol/ClangASTContext.h"
35#include "lldb/Symbol/Function.h"
36#include "lldb/Symbol/Type.h"
37#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000038#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000039#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000040#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000041#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000042#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000043#include "lldb/Target/ThreadPlan.h"
44#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000045
Sean Callanan72e49402011-08-05 23:43:37 +000046#include "clang/AST/DeclCXX.h"
47#include "clang/AST/DeclObjC.h"
48
Sean Callanan1a8d4092010-08-27 01:01:44 +000049using namespace lldb_private;
50
Sean Callanan322f5292010-10-29 00:29:03 +000051ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000052 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000053 lldb::LanguageType language,
54 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000055 ClangExpression (),
56 m_expr_text (expr),
57 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000058 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000059 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000060 m_desired_type (desired_type),
Sean Callananeab6cc92012-12-06 01:35:38 +000061 m_enforce_valid_object (true),
Greg Clayton22a939a2011-01-19 23:00:49 +000062 m_cplusplus (false),
63 m_objectivec (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000064 m_static_method(false),
Greg Clayton22a939a2011-01-19 23:00:49 +000065 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000066 m_const_object (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000067 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000068 m_evaluated_statically (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000069 m_const_result ()
Sean Callanan1a8d4092010-08-27 01:01:44 +000070{
Sean Callananc7b65062011-11-07 23:35:40 +000071 switch (m_language)
72 {
73 case lldb::eLanguageTypeC_plus_plus:
74 m_allow_cxx = true;
75 break;
76 case lldb::eLanguageTypeObjC:
77 m_allow_objc = true;
78 break;
79 case lldb::eLanguageTypeObjC_plus_plus:
80 default:
81 m_allow_cxx = true;
82 m_allow_objc = true;
83 break;
84 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000085}
86
Sean Callanane71d5532010-08-27 23:31:21 +000087ClangUserExpression::~ClangUserExpression ()
88{
89}
90
Sean Callanan1a8d4092010-08-27 01:01:44 +000091clang::ASTConsumer *
92ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000093{
Sean Callananbccce812011-08-23 21:20:51 +000094 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
95
96 if (!clang_ast_context)
97 return NULL;
98
Sean Callanan2590b9a2011-10-08 00:21:35 +000099 if (!m_result_synthesizer.get())
100 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanan0eed0d42011-12-06 03:41:14 +0000101 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +0000102
103 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000104}
105
Sean Callananfc55f5d2010-09-21 00:44:12 +0000106void
Sean Callanan744756e2011-11-04 02:09:33 +0000107ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000108{
Sean Callanan70385082012-12-01 00:08:33 +0000109 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
110
111 if (log)
112 log->Printf("ClangUserExpression::ScanContext()");
113
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000115
Sean Callananc7b65062011-11-07 23:35:40 +0000116 if (!(m_allow_cxx || m_allow_objc))
Sean Callanan70385082012-12-01 00:08:33 +0000117 {
118 if (log)
119 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
Sean Callananc7b65062011-11-07 23:35:40 +0000120 return;
Sean Callanan70385082012-12-01 00:08:33 +0000121 }
Sean Callananc7b65062011-11-07 23:35:40 +0000122
Greg Claytonc14ee322011-09-22 04:58:26 +0000123 StackFrame *frame = exe_ctx.GetFramePtr();
124 if (frame == NULL)
Sean Callanan70385082012-12-01 00:08:33 +0000125 {
126 if (log)
127 log->Printf(" [CUE::SC] Null stack frame");
Sean Callananfc55f5d2010-09-21 00:44:12 +0000128 return;
Sean Callanan70385082012-12-01 00:08:33 +0000129 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000130
Sean Callanan5dd6c3d2012-07-13 21:20:29 +0000131 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
Sean Callanan3670ba52010-12-01 21:35:54 +0000132
Sean Callanan72e49402011-08-05 23:43:37 +0000133 if (!sym_ctx.function)
Sean Callanan70385082012-12-01 00:08:33 +0000134 {
135 if (log)
136 log->Printf(" [CUE::SC] Null function");
Sean Callanan72e49402011-08-05 23:43:37 +0000137 return;
Sean Callanan70385082012-12-01 00:08:33 +0000138 }
Sean Callanan72e49402011-08-05 23:43:37 +0000139
Greg Clayton685c88c2012-07-14 00:53:55 +0000140 // Find the block that defines the function represented by "sym_ctx"
141 Block *function_block = sym_ctx.GetFunctionBlock();
Sean Callanan72e49402011-08-05 23:43:37 +0000142
Greg Clayton685c88c2012-07-14 00:53:55 +0000143 if (!function_block)
Sean Callanan70385082012-12-01 00:08:33 +0000144 {
145 if (log)
146 log->Printf(" [CUE::SC] Null function block");
Greg Clayton685c88c2012-07-14 00:53:55 +0000147 return;
Sean Callanan70385082012-12-01 00:08:33 +0000148 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000149
150 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
151
Sean Callanan72e49402011-08-05 23:43:37 +0000152 if (!decl_context)
Sean Callanan70385082012-12-01 00:08:33 +0000153 {
154 if (log)
155 log->Printf(" [CUE::SC] Null decl context");
Sean Callanan72e49402011-08-05 23:43:37 +0000156 return;
Sean Callanan70385082012-12-01 00:08:33 +0000157 }
158
Sean Callanan72e49402011-08-05 23:43:37 +0000159 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000160 {
Sean Callananc7b65062011-11-07 23:35:40 +0000161 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000162 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000163 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000164 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000165 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000166
167 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
168
Greg Clayton685c88c2012-07-14 00:53:55 +0000169 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000170 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000171 err.SetErrorString(thisErrorString);
172 return;
173 }
174
Greg Clayton685c88c2012-07-14 00:53:55 +0000175 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
Sean Callanand5cc1322011-12-13 01:42:04 +0000176
Greg Clayton685c88c2012-07-14 00:53:55 +0000177 if (!this_var_sp ||
178 !this_var_sp->IsInScope(frame) ||
179 !this_var_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000180 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000181 err.SetErrorString(thisErrorString);
182 return;
183 }
Sean Callanan744756e2011-11-04 02:09:33 +0000184 }
185
Sean Callanan72e49402011-08-05 23:43:37 +0000186 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000187 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000188 }
189 }
Sean Callanan72e49402011-08-05 23:43:37 +0000190 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000191 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000192 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000193 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000194 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000195 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000196 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000197
198 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
199
Greg Clayton685c88c2012-07-14 00:53:55 +0000200 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000201 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000202 err.SetErrorString(selfErrorString);
203 return;
204 }
205
Greg Clayton685c88c2012-07-14 00:53:55 +0000206 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
Sean Callanand5cc1322011-12-13 01:42:04 +0000207
Greg Clayton685c88c2012-07-14 00:53:55 +0000208 if (!self_variable_sp ||
209 !self_variable_sp->IsInScope(frame) ||
210 !self_variable_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000211 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000212 err.SetErrorString(selfErrorString);
213 return;
214 }
Sean Callanan744756e2011-11-04 02:09:33 +0000215 }
216
Sean Callanan72e49402011-08-05 23:43:37 +0000217 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000218 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000219
220 if (!method_decl->isInstanceMethod())
221 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000222 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000223 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000224 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context))
225 {
226 // We might also have a function that said in the debug information that it captured an
227 // object pointer. The best way to deal with getting to the ivars at present it by pretending
228 // that this is a method of a class in whatever runtime the debug info says the object pointer
229 // belongs to. Do that here.
230
231 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), (uintptr_t) function_decl);
232 if (metadata && metadata->HasObjectPtr())
233 {
234 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
235 if (language == lldb::eLanguageTypeC_plus_plus)
236 {
237 m_cplusplus = true;
238 m_needs_object_ptr = true;
239 }
240 else if (language == lldb::eLanguageTypeObjC)
241 {
242 m_objectivec = true;
243 m_needs_object_ptr = true;
244 }
245 }
246 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000247}
248
Sean Callanancf5498f2010-10-22 23:25:16 +0000249// This is a really nasty hack, meant to fix Objective-C expressions of the form
250// (int)[myArray count]. Right now, because the type information for count is
251// not available, [myArray count] returns id, which can't be directly cast to
252// int without causing a clang error.
253static void
254ApplyObjcCastHack(std::string &expr)
255{
256#define OBJC_CAST_HACK_FROM "(int)["
257#define OBJC_CAST_HACK_TO "(int)(long long)["
258
259 size_t from_offset;
260
261 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
262 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
263
264#undef OBJC_CAST_HACK_TO
265#undef OBJC_CAST_HACK_FROM
266}
267
Sean Callanan64186e72010-10-24 20:45:49 +0000268// Another hack, meant to allow use of unichar despite it not being available in
269// the type information. Although we could special-case it in type lookup,
270// hopefully we'll figure out a way to #include the same environment as is
271// present in the original source file rather than try to hack specific type
272// definitions in as needed.
273static void
274ApplyUnicharHack(std::string &expr)
275{
276#define UNICHAR_HACK_FROM "unichar"
277#define UNICHAR_HACK_TO "unsigned short"
278
279 size_t from_offset;
280
281 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
282 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
283
284#undef UNICHAR_HACK_TO
285#undef UNICHAR_HACK_FROM
286}
287
Sean Callanancf5498f2010-10-22 23:25:16 +0000288bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000289ClangUserExpression::Parse (Stream &error_stream,
290 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000291 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000292 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000293{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000294 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000295
Sean Callanan744756e2011-11-04 02:09:33 +0000296 Error err;
Sean Callanan933693b2012-02-10 01:22:05 +0000297
298 InstallContext(exe_ctx);
Sean Callanan744756e2011-11-04 02:09:33 +0000299
300 ScanContext(exe_ctx, err);
301
302 if (!err.Success())
303 {
304 error_stream.Printf("warning: %s\n", err.AsCString());
305 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000306
307 StreamString m_transformed_stream;
308
309 ////////////////////////////////////
310 // Generate the expression
311 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000312
313 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000314 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000315
Sean Callanan9bc83842011-09-26 18:45:31 +0000316 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
317
318 lldb::LanguageType lang_type;
319
Sean Callananfc55f5d2010-09-21 00:44:12 +0000320 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000321 lang_type = lldb::eLanguageTypeC_plus_plus;
322 else if(m_objectivec)
323 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000324 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000325 lang_type = lldb::eLanguageTypeC;
326
Sean Callanand5c17ed2011-11-15 02:11:17 +0000327 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000328 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000329 error_stream.PutCString ("error: couldn't construct expression body");
330 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000331 }
332
Sean Callananfc55f5d2010-09-21 00:44:12 +0000333 if (log)
334 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
335
Sean Callanan1a8d4092010-08-27 01:01:44 +0000336 ////////////////////////////////////
337 // Set up the target and compiler
338 //
339
Greg Claytonc14ee322011-09-22 04:58:26 +0000340 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000341
342 if (!target)
343 {
344 error_stream.PutCString ("error: invalid target\n");
345 return false;
346 }
347
Sean Callanan1a8d4092010-08-27 01:01:44 +0000348 //////////////////////////
349 // Parse the expression
350 //
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000351
Sean Callanan1ee44b72011-10-29 01:58:46 +0000352 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000353
Sean Callananb9951192011-08-01 18:18:33 +0000354 if (!m_expr_decl_map->WillParse(exe_ctx))
355 {
356 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
357 return false;
358 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000359
Greg Claytonc14ee322011-09-22 04:58:26 +0000360 Process *process = exe_ctx.GetProcessPtr();
Sean Callananaa719af2012-02-08 18:43:35 +0000361 ExecutionContextScope *exe_scope = process;
362
363 if (!exe_scope)
364 exe_scope = exe_ctx.GetTargetPtr();
365
366 ClangExpressionParser parser(exe_scope, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000367
368 unsigned num_errors = parser.Parse (error_stream);
369
370 if (num_errors)
371 {
372 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000373
374 m_expr_decl_map->DidParse();
375
Sean Callanan1a8d4092010-08-27 01:01:44 +0000376 return false;
377 }
378
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000379 //////////////////////////////////////////////////////////////////////////////////////////
380 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000381 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000382
Greg Claytonc14ee322011-09-22 04:58:26 +0000383 if (execution_policy != eExecutionPolicyNever && process)
384 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000385
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000386 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
387 m_jit_start_addr,
388 m_jit_end_addr,
389 exe_ctx,
390 m_data_allocator.get(),
391 m_evaluated_statically,
392 m_const_result,
393 execution_policy);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000394
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000395 if (log && m_data_allocator.get())
Sean Callanan79763a42011-05-23 21:40:23 +0000396 {
397 StreamString dump_string;
398 m_data_allocator->Dump(dump_string);
399
400 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
401 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000402
Sean Callanan1a8d4092010-08-27 01:01:44 +0000403 if (jit_error.Success())
404 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000405 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000406 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000407 return true;
408 }
409 else
410 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000411 const char *error_cstr = jit_error.AsCString();
412 if (error_cstr && error_cstr[0])
413 error_stream.Printf ("error: %s\n", error_cstr);
414 else
Jason Molendafd54b362011-09-20 21:44:10 +0000415 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000416 return false;
417 }
418}
419
420bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000421ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000422 ExecutionContext &exe_ctx,
423 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000424 lldb::addr_t &object_ptr,
425 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000426{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000427 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000428
Sean Callanan933693b2012-02-10 01:22:05 +0000429 lldb::TargetSP target;
430 lldb::ProcessSP process;
431 lldb::StackFrameSP frame;
432
433 if (!LockAndCheckContext(exe_ctx,
434 target,
435 process,
436 frame))
437 {
438 error_stream.Printf("The context has changed before we could JIT the expression!");
439 return false;
440 }
441
Greg Clayton22a939a2011-01-19 23:00:49 +0000442 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000443 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000444 Error materialize_error;
445
Sean Callanan17827832010-12-13 22:46:15 +0000446 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000447 {
Sean Callanan17827832010-12-13 22:46:15 +0000448 ConstString object_name;
449
450 if (m_cplusplus)
451 {
452 object_name.SetCString("this");
453 }
454 else if (m_objectivec)
455 {
456 object_name.SetCString("self");
457 }
458 else
459 {
460 error_stream.Printf("Need object pointer but don't know the language\n");
461 return false;
462 }
463
Sean Callanan933693b2012-02-10 01:22:05 +0000464 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, materialize_error)))
Sean Callanan17827832010-12-13 22:46:15 +0000465 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000466 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
467 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000468 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000469
470 if (m_objectivec)
471 {
472 ConstString cmd_name("_cmd");
473
Sean Callanan933693b2012-02-10 01:22:05 +0000474 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, materialize_error, true)))
Sean Callanan9d48e802010-12-14 00:42:36 +0000475 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000476 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
477 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000478 }
479 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000480 }
481
Sean Callanan933693b2012-02-10 01:22:05 +0000482 if (!m_expr_decl_map->Materialize(struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000483 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000484 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000485 return false;
486 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000487
488#if 0
489 // jingham: look here
490 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000491 logfile.Printf("0x%16.16" PRIx64 ": 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 +0000492#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000493
494 if (log)
495 {
Sean Callanana162eba2010-12-07 22:55:01 +0000496 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000497
Daniel Malead01b2952012-11-29 21:49:15 +0000498 log->Printf(" Function address : 0x%" PRIx64, (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000499
500 if (m_needs_object_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +0000501 log->Printf(" Object pointer : 0x%" PRIx64, (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000502
Daniel Malead01b2952012-11-29 21:49:15 +0000503 log->Printf(" Structure address : 0x%" PRIx64, (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000504
505 StreamString args;
506
507 Error dump_error;
508
Sean Callanan9e6ed532010-09-13 21:34:21 +0000509 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000510 {
Sean Callanan933693b2012-02-10 01:22:05 +0000511 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000512 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000513 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000514 }
515 else
516 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000517 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000518 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000519 }
520 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000521 }
522 return true;
523}
524
525ThreadPlan *
526ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000527 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000528{
529 lldb::addr_t struct_address;
530
Johnny Chen44805302011-07-19 19:48:13 +0000531 lldb::addr_t object_ptr = 0;
532 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000533
Sean Callanan9d48e802010-12-14 00:42:36 +0000534 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000535
Jim Inghamf48169b2010-11-30 02:22:11 +0000536 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
537 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000538 // forcing unwind_on_error to be true here, in practical terms that can't happen.
539
Jim Ingham184e9812013-01-15 02:47:48 +0000540 const bool stop_others = true;
541 const bool unwind_on_error = true;
542 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000543 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000544 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000545 struct_address,
546 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000547 stop_others,
548 unwind_on_error,
549 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000550 (m_needs_object_ptr ? &object_ptr : NULL),
551 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000552}
553
554bool
555ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
556 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000557 lldb::ClangExpressionVariableSP &result,
558 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000559{
560 Error expr_error;
561
Sean Callananc673a6e2010-12-07 10:00:20 +0000562 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
563
564 if (log)
565 {
Sean Callanana162eba2010-12-07 22:55:01 +0000566 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000567
568 StreamString args;
569
570 Error dump_error;
571
Sean Callanan933693b2012-02-10 01:22:05 +0000572 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callananc673a6e2010-12-07 10:00:20 +0000573 {
574 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
575 }
576 else
577 {
578 log->Printf(" Structure contents:\n%s", args.GetData());
579 }
580 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000581
582 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
583
Sean Callananc673a6e2010-12-07 10:00:20 +0000584
Sean Callanan933693b2012-02-10 01:22:05 +0000585 if (!m_expr_decl_map->Dematerialize(result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000586 {
587 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
588 return false;
589 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000590
Johnny Chenb49440f2012-01-06 00:35:38 +0000591 if (result)
592 result->TransferAddress();
593
Jim Ingham36f3b362010-10-14 23:45:03 +0000594 return true;
595}
596
Greg Claytone0d378b2011-03-24 21:19:54 +0000597ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000598ClangUserExpression::Execute (Stream &error_stream,
599 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000600 bool unwind_on_error,
601 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000602 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000603 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000604 bool run_others,
605 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000606{
Jim Inghamb086ff72011-01-18 22:20:08 +0000607 // The expression log is quite verbose, and if you're just tracking the execution of the
608 // expression, it's quite convenient to have these logs come out with the STEP log as well.
609 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000610
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000611 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000612 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000613 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000614
Johnny Chen44805302011-07-19 19:48:13 +0000615 lldb::addr_t object_ptr = 0;
616 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000617
Johnny Chen8115c6d2012-08-18 04:24:00 +0000618 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
619 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000620 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000621 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000622 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000623
Jim Ingham399f1ca2010-11-05 19:25:48 +0000624 const bool stop_others = true;
625 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000626
Greg Claytone72dfb32012-02-24 01:59:29 +0000627 Address wrapper_address (m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000628 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000629 wrapper_address,
630 struct_address,
631 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000632 unwind_on_error,
633 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000634 (m_needs_object_ptr ? &object_ptr : NULL),
635 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
636 shared_ptr_to_me));
637
Sean Callanan9a028512012-08-09 00:50:26 +0000638 if (!call_plan_sp || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000639 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000640
641 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham95afbf52012-12-07 19:04:31 +0000642
Sean Callananc673a6e2010-12-07 10:00:20 +0000643 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000644 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000645
Jim Ingham0faa43f2011-11-08 03:00:11 +0000646 if (exe_ctx.GetProcessPtr())
647 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
648
Greg Claytonc14ee322011-09-22 04:58:26 +0000649 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
650 call_plan_sp,
651 stop_others,
652 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000653 unwind_on_error,
654 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000655 timeout_usec,
Greg Claytonc14ee322011-09-22 04:58:26 +0000656 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000657
Jim Ingham0faa43f2011-11-08 03:00:11 +0000658 if (exe_ctx.GetProcessPtr())
659 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
660
Sean Callananc673a6e2010-12-07 10:00:20 +0000661 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000662 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000663
Jim Ingham184e9812013-01-15 02:47:48 +0000664 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000665 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000666 const char *error_desc = NULL;
667
668 if (call_plan_sp)
669 {
670 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
671 if (real_stop_info_sp)
672 error_desc = real_stop_info_sp->GetDescription();
673 }
674 if (error_desc)
675 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000676 else
Jason Molendafd54b362011-09-20 21:44:10 +0000677 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000678
Jim Ingham184e9812013-01-15 02:47:48 +0000679 if ((execution_result == eExecutionInterrupted && unwind_on_error)
680 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
Jim Ingham160f78c2011-05-17 01:10:11 +0000681 error_stream.Printf ("\nThe process has been returned to the state before execution.");
682 else
683 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000684
685 return execution_result;
686 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000687 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000688 {
689 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
690 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000691 }
692
Sean Callanane359d9b2011-05-09 22:04:36 +0000693 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000694 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000695 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000696 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000697 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000698 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000699 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000700 }
701 else
702 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000703 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000704 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000705 }
706}
707
Greg Claytone0d378b2011-03-24 21:19:54 +0000708ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000709ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
710 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000711 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000712 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000713 bool unwind_on_error,
714 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000715 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000716 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000717 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000718 bool run_others,
719 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000720{
Jim Ingham41c75912011-08-09 00:00:49 +0000721 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000722 return EvaluateWithError (exe_ctx,
723 execution_policy,
724 language,
725 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000726 unwind_on_error,
727 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000728 expr_cstr,
729 expr_prefix,
730 result_valobj_sp,
731 error,
732 run_others,
733 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000734}
735
736ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000737ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
738 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000739 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000740 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000741 bool unwind_on_error,
742 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000743 const char *expr_cstr,
744 const char *expr_prefix,
745 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000746 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000747 bool run_others,
748 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000749{
Jim Inghamb086ff72011-01-18 22:20:08 +0000750 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000751
Greg Claytone0d378b2011-03-24 21:19:54 +0000752 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000753
Greg Claytonc14ee322011-09-22 04:58:26 +0000754 Process *process = exe_ctx.GetProcessPtr();
755
756 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000757 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000758 if (execution_policy == eExecutionPolicyAlways)
759 {
760 if (log)
761 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000762
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000763 error.SetErrorString ("expression needed to run but couldn't");
764
765 return execution_results;
766 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000767 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000768
Greg Claytonc14ee322011-09-22 04:58:26 +0000769 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000770 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000771
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000772 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000773
Greg Clayton0184f012010-10-05 00:31:29 +0000774 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000775
Sean Callanana162eba2010-12-07 22:55:01 +0000776 if (log)
777 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
778
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000779 const bool keep_expression_in_memory = true;
780
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000781 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000782 {
783 if (error_stream.GetString().empty())
784 error.SetErrorString ("expression failed to parse, unknown error");
785 else
786 error.SetErrorString (error_stream.GetString().c_str());
787 }
788 else
789 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000790 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000791
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000792 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000793 {
Sean Callanana162eba2010-12-07 22:55:01 +0000794 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000795 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000796
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000797 if (user_expression_sp->m_const_result)
798 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
799 else
800 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
801
Jim Ingham41c75912011-08-09 00:00:49 +0000802 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000803 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000804 else if (execution_policy == eExecutionPolicyNever)
805 {
806 if (log)
807 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
808
809 if (error_stream.GetString().empty())
810 error.SetErrorString ("expression needed to run but couldn't");
811 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000812 else
813 {
814 error_stream.GetString().clear();
815
816 if (log)
817 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
818
819 execution_results = user_expression_sp->Execute (error_stream,
820 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000821 unwind_on_error,
822 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000823 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000824 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000825 run_others,
826 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000827
Greg Claytone0d378b2011-03-24 21:19:54 +0000828 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000829 {
Sean Callanana162eba2010-12-07 22:55:01 +0000830 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000831 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
832
833 if (error_stream.GetString().empty())
834 error.SetErrorString ("expression failed to execute, unknown error");
835 else
836 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000837 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000838 else
Greg Clayton0184f012010-10-05 00:31:29 +0000839 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000840 if (expr_result)
841 {
842 result_valobj_sp = expr_result->GetValueObject();
843
844 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000845 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000846 }
847 else
848 {
849 if (log)
850 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
851
Sean Callananbccce812011-08-23 21:20:51 +0000852 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000853 }
Greg Clayton0184f012010-10-05 00:31:29 +0000854 }
855 }
856 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000857
Greg Claytonb71f3842010-10-05 03:13:51 +0000858 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000859 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000860
Jim Inghamf48169b2010-11-30 02:22:11 +0000861 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000862}