blob: 083f67aa2cf45bd19e4dee8ba88f9373f0264972 [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 Callanan14b1bae2013-04-16 23:25:35 +000032#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000033#include "lldb/Expression/IRInterpreter.h"
Sean Callanan96d27302013-04-11 00:09:05 +000034#include "lldb/Expression/Materializer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000035#include "lldb/Host/Host.h"
Greg Clayton1f746072012-08-29 21:13:06 +000036#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000037#include "lldb/Symbol/ClangASTContext.h"
38#include "lldb/Symbol/Function.h"
39#include "lldb/Symbol/Type.h"
40#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000041#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000042#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000043#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000044#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000045#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000046#include "lldb/Target/ThreadPlan.h"
47#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000048
Sean Callanan72e49402011-08-05 23:43:37 +000049#include "clang/AST/DeclCXX.h"
50#include "clang/AST/DeclObjC.h"
51
Sean Callanan1a8d4092010-08-27 01:01:44 +000052using namespace lldb_private;
53
Sean Callanan322f5292010-10-29 00:29:03 +000054ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000055 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000056 lldb::LanguageType language,
57 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000058 ClangExpression (),
59 m_expr_text (expr),
60 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000061 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000062 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000063 m_desired_type (desired_type),
Sean Callananeab6cc92012-12-06 01:35:38 +000064 m_enforce_valid_object (true),
Greg Clayton22a939a2011-01-19 23:00:49 +000065 m_cplusplus (false),
66 m_objectivec (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000067 m_static_method(false),
Greg Clayton22a939a2011-01-19 23:00:49 +000068 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000069 m_const_object (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000070 m_target (NULL),
Sean Callanan1582ee62013-04-18 22:06:33 +000071 m_can_interpret (false)
Sean Callanan1a8d4092010-08-27 01:01:44 +000072{
Sean Callananc7b65062011-11-07 23:35:40 +000073 switch (m_language)
74 {
75 case lldb::eLanguageTypeC_plus_plus:
76 m_allow_cxx = true;
77 break;
78 case lldb::eLanguageTypeObjC:
79 m_allow_objc = true;
80 break;
81 case lldb::eLanguageTypeObjC_plus_plus:
82 default:
83 m_allow_cxx = true;
84 m_allow_objc = true;
85 break;
86 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000087}
88
Sean Callanane71d5532010-08-27 23:31:21 +000089ClangUserExpression::~ClangUserExpression ()
90{
91}
92
Sean Callanan1a8d4092010-08-27 01:01:44 +000093clang::ASTConsumer *
94ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000095{
Sean Callananbccce812011-08-23 21:20:51 +000096 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
97
98 if (!clang_ast_context)
99 return NULL;
100
Sean Callanan2590b9a2011-10-08 00:21:35 +0000101 if (!m_result_synthesizer.get())
102 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanan0eed0d42011-12-06 03:41:14 +0000103 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +0000104
105 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000106}
107
Sean Callananfc55f5d2010-09-21 00:44:12 +0000108void
Sean Callanan744756e2011-11-04 02:09:33 +0000109ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000110{
Greg Clayton5160ce52013-03-27 23:08:40 +0000111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan70385082012-12-01 00:08:33 +0000112
113 if (log)
114 log->Printf("ClangUserExpression::ScanContext()");
115
Greg Claytonc14ee322011-09-22 04:58:26 +0000116 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000117
Sean Callananc7b65062011-11-07 23:35:40 +0000118 if (!(m_allow_cxx || m_allow_objc))
Sean Callanan70385082012-12-01 00:08:33 +0000119 {
120 if (log)
121 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
Sean Callananc7b65062011-11-07 23:35:40 +0000122 return;
Sean Callanan70385082012-12-01 00:08:33 +0000123 }
Sean Callananc7b65062011-11-07 23:35:40 +0000124
Greg Claytonc14ee322011-09-22 04:58:26 +0000125 StackFrame *frame = exe_ctx.GetFramePtr();
126 if (frame == NULL)
Sean Callanan70385082012-12-01 00:08:33 +0000127 {
128 if (log)
129 log->Printf(" [CUE::SC] Null stack frame");
Sean Callananfc55f5d2010-09-21 00:44:12 +0000130 return;
Sean Callanan70385082012-12-01 00:08:33 +0000131 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000132
Sean Callanan5dd6c3d2012-07-13 21:20:29 +0000133 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
Sean Callanan3670ba52010-12-01 21:35:54 +0000134
Sean Callanan72e49402011-08-05 23:43:37 +0000135 if (!sym_ctx.function)
Sean Callanan70385082012-12-01 00:08:33 +0000136 {
137 if (log)
138 log->Printf(" [CUE::SC] Null function");
Sean Callanan72e49402011-08-05 23:43:37 +0000139 return;
Sean Callanan70385082012-12-01 00:08:33 +0000140 }
Sean Callanan72e49402011-08-05 23:43:37 +0000141
Greg Clayton685c88c2012-07-14 00:53:55 +0000142 // Find the block that defines the function represented by "sym_ctx"
143 Block *function_block = sym_ctx.GetFunctionBlock();
Sean Callanan72e49402011-08-05 23:43:37 +0000144
Greg Clayton685c88c2012-07-14 00:53:55 +0000145 if (!function_block)
Sean Callanan70385082012-12-01 00:08:33 +0000146 {
147 if (log)
148 log->Printf(" [CUE::SC] Null function block");
Greg Clayton685c88c2012-07-14 00:53:55 +0000149 return;
Sean Callanan70385082012-12-01 00:08:33 +0000150 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000151
152 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
153
Sean Callanan72e49402011-08-05 23:43:37 +0000154 if (!decl_context)
Sean Callanan70385082012-12-01 00:08:33 +0000155 {
156 if (log)
157 log->Printf(" [CUE::SC] Null decl context");
Sean Callanan72e49402011-08-05 23:43:37 +0000158 return;
Sean Callanan70385082012-12-01 00:08:33 +0000159 }
160
Sean Callanan72e49402011-08-05 23:43:37 +0000161 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000162 {
Sean Callananc7b65062011-11-07 23:35:40 +0000163 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000164 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000165 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000166 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000167 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000168
169 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
170
Greg Clayton685c88c2012-07-14 00:53:55 +0000171 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000172 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000173 err.SetErrorString(thisErrorString);
174 return;
175 }
176
Greg Clayton685c88c2012-07-14 00:53:55 +0000177 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
Sean Callanand5cc1322011-12-13 01:42:04 +0000178
Greg Clayton685c88c2012-07-14 00:53:55 +0000179 if (!this_var_sp ||
180 !this_var_sp->IsInScope(frame) ||
181 !this_var_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000182 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000183 err.SetErrorString(thisErrorString);
184 return;
185 }
Sean Callanan744756e2011-11-04 02:09:33 +0000186 }
187
Sean Callanan72e49402011-08-05 23:43:37 +0000188 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000189 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000190 }
191 }
Sean Callanan72e49402011-08-05 23:43:37 +0000192 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000193 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000194 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000195 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000196 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000197 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000198 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000199
200 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
201
Greg Clayton685c88c2012-07-14 00:53:55 +0000202 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000203 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000204 err.SetErrorString(selfErrorString);
205 return;
206 }
207
Greg Clayton685c88c2012-07-14 00:53:55 +0000208 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
Sean Callanand5cc1322011-12-13 01:42:04 +0000209
Greg Clayton685c88c2012-07-14 00:53:55 +0000210 if (!self_variable_sp ||
211 !self_variable_sp->IsInScope(frame) ||
212 !self_variable_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000213 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000214 err.SetErrorString(selfErrorString);
215 return;
216 }
Sean Callanan744756e2011-11-04 02:09:33 +0000217 }
218
Sean Callanan72e49402011-08-05 23:43:37 +0000219 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000220 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000221
222 if (!method_decl->isInstanceMethod())
223 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000224 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000225 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000226 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context))
227 {
228 // We might also have a function that said in the debug information that it captured an
229 // object pointer. The best way to deal with getting to the ivars at present it by pretending
230 // that this is a method of a class in whatever runtime the debug info says the object pointer
231 // belongs to. Do that here.
232
Greg Claytond0029442013-03-27 01:48:02 +0000233 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), function_decl);
Jim Ingham5fdeed42012-10-30 23:35:54 +0000234 if (metadata && metadata->HasObjectPtr())
235 {
236 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
237 if (language == lldb::eLanguageTypeC_plus_plus)
238 {
Sean Callanana2868d42013-01-19 01:49:02 +0000239 if (m_enforce_valid_object)
240 {
241 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
242
243 const char *thisErrorString = "Stopped in a context claiming to capture a C++ object pointer, but 'this' isn't available; pretending we are in a generic context";
244
245 if (!variable_list_sp)
246 {
247 err.SetErrorString(thisErrorString);
248 return;
249 }
250
251 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
252
253 if (!this_var_sp ||
254 !this_var_sp->IsInScope(frame) ||
255 !this_var_sp->LocationIsValidForFrame (frame))
256 {
257 err.SetErrorString(thisErrorString);
258 return;
259 }
260 }
261
Jim Ingham5fdeed42012-10-30 23:35:54 +0000262 m_cplusplus = true;
263 m_needs_object_ptr = true;
264 }
265 else if (language == lldb::eLanguageTypeObjC)
266 {
Sean Callanana2868d42013-01-19 01:49:02 +0000267 if (m_enforce_valid_object)
268 {
269 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
270
271 const char *selfErrorString = "Stopped in a context claiming to capture an Objective-C object pointer, but 'self' isn't available; pretending we are in a generic context";
272
273 if (!variable_list_sp)
274 {
275 err.SetErrorString(selfErrorString);
276 return;
277 }
278
279 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
280
281 if (!self_variable_sp ||
282 !self_variable_sp->IsInScope(frame) ||
283 !self_variable_sp->LocationIsValidForFrame (frame))
284 {
285 err.SetErrorString(selfErrorString);
286 return;
287 }
288
289 Type *self_type = self_variable_sp->GetType();
290
291 if (!self_type)
292 {
293 err.SetErrorString(selfErrorString);
294 return;
295 }
296
297 lldb::clang_type_t self_opaque_type = self_type->GetClangForwardType();
298
299 if (!self_opaque_type)
300 {
301 err.SetErrorString(selfErrorString);
302 return;
303 }
304
305 clang::QualType self_qual_type = clang::QualType::getFromOpaquePtr(self_opaque_type);
306
307 if (self_qual_type->isObjCClassType())
308 {
309 return;
310 }
311 else if (self_qual_type->isObjCObjectPointerType())
312 {
313 m_objectivec = true;
314 m_needs_object_ptr = true;
315 }
316 else
317 {
318 err.SetErrorString(selfErrorString);
319 return;
320 }
321 }
322 else
323 {
324 m_objectivec = true;
325 m_needs_object_ptr = true;
326 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000327 }
328 }
329 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000330}
331
Sean Callanancf5498f2010-10-22 23:25:16 +0000332// This is a really nasty hack, meant to fix Objective-C expressions of the form
333// (int)[myArray count]. Right now, because the type information for count is
334// not available, [myArray count] returns id, which can't be directly cast to
335// int without causing a clang error.
336static void
337ApplyObjcCastHack(std::string &expr)
338{
339#define OBJC_CAST_HACK_FROM "(int)["
340#define OBJC_CAST_HACK_TO "(int)(long long)["
341
342 size_t from_offset;
343
344 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
345 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
346
347#undef OBJC_CAST_HACK_TO
348#undef OBJC_CAST_HACK_FROM
349}
350
Sean Callanan64186e72010-10-24 20:45:49 +0000351// Another hack, meant to allow use of unichar despite it not being available in
352// the type information. Although we could special-case it in type lookup,
353// hopefully we'll figure out a way to #include the same environment as is
354// present in the original source file rather than try to hack specific type
355// definitions in as needed.
356static void
357ApplyUnicharHack(std::string &expr)
358{
359#define UNICHAR_HACK_FROM "unichar"
360#define UNICHAR_HACK_TO "unsigned short"
361
362 size_t from_offset;
363
364 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
365 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
366
367#undef UNICHAR_HACK_TO
368#undef UNICHAR_HACK_FROM
369}
370
Sean Callanancf5498f2010-10-22 23:25:16 +0000371bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000372ClangUserExpression::Parse (Stream &error_stream,
373 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000374 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000375 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000376{
Greg Clayton5160ce52013-03-27 23:08:40 +0000377 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000378
Sean Callanan744756e2011-11-04 02:09:33 +0000379 Error err;
Sean Callanan933693b2012-02-10 01:22:05 +0000380
381 InstallContext(exe_ctx);
Sean Callanan744756e2011-11-04 02:09:33 +0000382
383 ScanContext(exe_ctx, err);
384
385 if (!err.Success())
386 {
387 error_stream.Printf("warning: %s\n", err.AsCString());
388 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000389
390 StreamString m_transformed_stream;
391
392 ////////////////////////////////////
393 // Generate the expression
394 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000395
396 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000397 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000398
Greg Claytone01e07b2013-04-18 18:10:51 +0000399 STD_UNIQUE_PTR(ExpressionSourceCode) source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
Sean Callanan9bc83842011-09-26 18:45:31 +0000400
401 lldb::LanguageType lang_type;
402
Sean Callananfc55f5d2010-09-21 00:44:12 +0000403 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000404 lang_type = lldb::eLanguageTypeC_plus_plus;
405 else if(m_objectivec)
406 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000407 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000408 lang_type = lldb::eLanguageTypeC;
409
Sean Callanand5c17ed2011-11-15 02:11:17 +0000410 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000411 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000412 error_stream.PutCString ("error: couldn't construct expression body");
413 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000414 }
415
Sean Callananfc55f5d2010-09-21 00:44:12 +0000416 if (log)
417 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
418
Sean Callanan1a8d4092010-08-27 01:01:44 +0000419 ////////////////////////////////////
420 // Set up the target and compiler
421 //
422
Greg Claytonc14ee322011-09-22 04:58:26 +0000423 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000424
425 if (!target)
426 {
427 error_stream.PutCString ("error: invalid target\n");
428 return false;
429 }
430
Sean Callanan1a8d4092010-08-27 01:01:44 +0000431 //////////////////////////
432 // Parse the expression
433 //
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000434
Sean Callanan96d27302013-04-11 00:09:05 +0000435 m_materializer_ap.reset(new Materializer());
436
Sean Callanan1ee44b72011-10-29 01:58:46 +0000437 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000438
Sean Callanan96d27302013-04-11 00:09:05 +0000439 if (!m_expr_decl_map->WillParse(exe_ctx, m_materializer_ap.get()))
Sean Callananb9951192011-08-01 18:18:33 +0000440 {
441 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
442 return false;
443 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000444
Greg Claytonc14ee322011-09-22 04:58:26 +0000445 Process *process = exe_ctx.GetProcessPtr();
Sean Callananaa719af2012-02-08 18:43:35 +0000446 ExecutionContextScope *exe_scope = process;
447
448 if (!exe_scope)
449 exe_scope = exe_ctx.GetTargetPtr();
450
451 ClangExpressionParser parser(exe_scope, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000452
453 unsigned num_errors = parser.Parse (error_stream);
454
455 if (num_errors)
456 {
457 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000458
459 m_expr_decl_map->DidParse();
460
Sean Callanan1a8d4092010-08-27 01:01:44 +0000461 return false;
462 }
463
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000464 //////////////////////////////////////////////////////////////////////////////////////////
465 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000466 //
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000467
468 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000469 m_jit_end_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000470 m_execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000471 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000472 m_can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000473 execution_policy);
Sean Callanane3aef1d2011-10-12 22:20:02 +0000474
Sean Callanan1a8d4092010-08-27 01:01:44 +0000475 if (jit_error.Success())
476 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000477 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000478 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000479 return true;
480 }
481 else
482 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000483 const char *error_cstr = jit_error.AsCString();
484 if (error_cstr && error_cstr[0])
485 error_stream.Printf ("error: %s\n", error_cstr);
486 else
Jason Molendafd54b362011-09-20 21:44:10 +0000487 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000488 return false;
489 }
490}
491
Sean Callanan1582ee62013-04-18 22:06:33 +0000492static lldb::addr_t
493GetObjectPointer (lldb::StackFrameSP frame_sp,
494 ConstString &object_name,
495 Error &err)
496{
497 err.Clear();
498
499 if (!frame_sp)
500 {
501 err.SetErrorStringWithFormat("Couldn't load '%s' because the context is incomplete", object_name.AsCString());
502 return LLDB_INVALID_ADDRESS;
503 }
504
505 lldb::VariableSP var_sp;
506 lldb::ValueObjectSP valobj_sp;
507
508 valobj_sp = frame_sp->GetValueForVariableExpressionPath(object_name.AsCString(),
509 lldb::eNoDynamicValues,
510 StackFrame::eExpressionPathOptionCheckPtrVsMember ||
511 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess ||
512 StackFrame::eExpressionPathOptionsNoFragileObjcIvar ||
513 StackFrame::eExpressionPathOptionsNoSyntheticChildren ||
514 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
515 var_sp,
516 err);
517
518 if (!err.Success())
519 return LLDB_INVALID_ADDRESS;
520
521 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
522
523 if (ret == LLDB_INVALID_ADDRESS)
524 {
525 err.SetErrorStringWithFormat("Couldn't load '%s' because its value couldn't be evaluated", object_name.AsCString());
526 return LLDB_INVALID_ADDRESS;
527 }
528
529 return ret;
530}
531
Sean Callanan1a8d4092010-08-27 01:01:44 +0000532bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000533ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000534 ExecutionContext &exe_ctx,
535 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000536 lldb::addr_t &object_ptr,
537 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000538{
Sean Callanan933693b2012-02-10 01:22:05 +0000539 lldb::TargetSP target;
540 lldb::ProcessSP process;
541 lldb::StackFrameSP frame;
542
543 if (!LockAndCheckContext(exe_ctx,
544 target,
545 process,
546 frame))
547 {
548 error_stream.Printf("The context has changed before we could JIT the expression!");
549 return false;
550 }
551
Sean Callanan1582ee62013-04-18 22:06:33 +0000552 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
553 {
Sean Callanan17827832010-12-13 22:46:15 +0000554 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000555 {
Sean Callanan17827832010-12-13 22:46:15 +0000556 ConstString object_name;
557
558 if (m_cplusplus)
559 {
560 object_name.SetCString("this");
561 }
562 else if (m_objectivec)
563 {
564 object_name.SetCString("self");
565 }
566 else
567 {
568 error_stream.Printf("Need object pointer but don't know the language\n");
569 return false;
570 }
571
Sean Callanan1582ee62013-04-18 22:06:33 +0000572 Error object_ptr_error;
573
574 object_ptr = GetObjectPointer(frame, object_name, object_ptr_error);
575
576 if (!object_ptr_error.Success())
Sean Callanan17827832010-12-13 22:46:15 +0000577 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000578 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
Sean Callanand5cc1322011-12-13 01:42:04 +0000579 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000580 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000581
582 if (m_objectivec)
583 {
584 ConstString cmd_name("_cmd");
585
Sean Callanan1582ee62013-04-18 22:06:33 +0000586 cmd_ptr = GetObjectPointer(frame, cmd_name, object_ptr_error);
587
588 if (!object_ptr_error.Success())
Sean Callanan9d48e802010-12-14 00:42:36 +0000589 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000590 error_stream.Printf("warning: couldn't get cmd pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
Sean Callanand5cc1322011-12-13 01:42:04 +0000591 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000592 }
593 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000594 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000595
596 Error alloc_error;
597
598 struct_address = m_execution_unit_ap->Malloc(m_materializer_ap->GetStructByteSize(),
599 m_materializer_ap->GetStructAlignment(),
600 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
601 IRMemoryMap::eAllocationPolicyMirror,
602 alloc_error);
603
604 if (!alloc_error.Success())
605 {
606 error_stream.Printf("Couldn't allocate space for materialized struct: %s\n", alloc_error.AsCString());
607 return false;
608 }
609
610 m_materialized_address = struct_address;
611
612 Error materialize_error;
613
614 m_dematerializer_sp = m_materializer_ap->Materialize(frame, *m_execution_unit_ap, struct_address, materialize_error);
615
616 if (!materialize_error.Success())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000617 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000618 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000619 return false;
620 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000621 }
622 return true;
623}
624
625ThreadPlan *
626ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000627 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000628{
629 lldb::addr_t struct_address;
630
Johnny Chen44805302011-07-19 19:48:13 +0000631 lldb::addr_t object_ptr = 0;
632 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000633
Sean Callanan9d48e802010-12-14 00:42:36 +0000634 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000635
Jim Inghamf48169b2010-11-30 02:22:11 +0000636 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
637 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000638 // forcing unwind_on_error to be true here, in practical terms that can't happen.
639
Jim Ingham184e9812013-01-15 02:47:48 +0000640 const bool stop_others = true;
641 const bool unwind_on_error = true;
642 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000643 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000644 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000645 struct_address,
646 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000647 stop_others,
648 unwind_on_error,
649 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000650 (m_needs_object_ptr ? &object_ptr : NULL),
651 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000652}
653
654bool
655ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
656 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000657 lldb::ClangExpressionVariableSP &result,
658 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000659{
660 Error expr_error;
661
Greg Clayton5160ce52013-03-27 23:08:40 +0000662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc673a6e2010-12-07 10:00:20 +0000663
664 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000665 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000666
Sean Callanane359d9b2011-05-09 22:04:36 +0000667 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
668
Sean Callanan1582ee62013-04-18 22:06:33 +0000669 if (!m_dematerializer_sp)
670 {
671 error_stream.Printf ("Couldn't dematerialize struct : no dematerializer is present");
672 return false;
673 }
674
675 Error dematerialize_error;
676
677 m_dematerializer_sp->Dematerialize(dematerialize_error, result, function_stack_pointer, function_stack_bottom);
678
679 if (!dematerialize_error.Success())
Jim Ingham36f3b362010-10-14 23:45:03 +0000680 {
681 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
682 return false;
683 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000684
Johnny Chenb49440f2012-01-06 00:35:38 +0000685 if (result)
686 result->TransferAddress();
687
Sean Callanan1582ee62013-04-18 22:06:33 +0000688 m_dematerializer_sp.reset();
689
Jim Ingham36f3b362010-10-14 23:45:03 +0000690 return true;
691}
692
Greg Claytone0d378b2011-03-24 21:19:54 +0000693ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000694ClangUserExpression::Execute (Stream &error_stream,
695 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000696 bool unwind_on_error,
697 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000698 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000699 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000700 bool run_others,
701 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000702{
Jim Inghamb086ff72011-01-18 22:20:08 +0000703 // The expression log is quite verbose, and if you're just tracking the execution of the
704 // expression, it's quite convenient to have these logs come out with the STEP log as well.
Greg Clayton5160ce52013-03-27 23:08:40 +0000705 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000706
Sean Callanan1582ee62013-04-18 22:06:33 +0000707 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
Jim Ingham36f3b362010-10-14 23:45:03 +0000708 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000709 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000710
Johnny Chen44805302011-07-19 19:48:13 +0000711 lldb::addr_t object_ptr = 0;
712 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000713
Johnny Chen8115c6d2012-08-18 04:24:00 +0000714 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
715 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000716 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000717 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000718 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000719
Sean Callanan1582ee62013-04-18 22:06:33 +0000720 lldb::addr_t function_stack_pointer = NULL;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000721
Sean Callanan1582ee62013-04-18 22:06:33 +0000722 if (m_can_interpret)
723 {
724 llvm::Module *module = m_execution_unit_ap->GetModule();
725 llvm::Function *function = m_execution_unit_ap->GetFunction();
Jim Ingham0faa43f2011-11-08 03:00:11 +0000726
Sean Callanan1582ee62013-04-18 22:06:33 +0000727 if (!module || !function)
Jim Ingham160f78c2011-05-17 01:10:11 +0000728 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000729 error_stream.Printf("Supposed to interpret, but nothing is there");
730 return eExecutionSetupError;
Jim Ingham160f78c2011-05-17 01:10:11 +0000731 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000732
Sean Callanan1582ee62013-04-18 22:06:33 +0000733 Error interpreter_error;
734
735 llvm::SmallVector <lldb::addr_t, 3> args;
736
737 if (m_needs_object_ptr)
738 {
739 args.push_back(object_ptr);
740
741 if (m_objectivec)
742 args.push_back(cmd_ptr);
743 }
744
745 args.push_back(struct_address);
746
747 IRInterpreter::Interpret (*module,
748 *function,
749 args,
750 *m_execution_unit_ap.get(),
751 interpreter_error);
752
753 if (!interpreter_error.Success())
754 {
755 error_stream.Printf("Supposed to interpret, but failed: %s", interpreter_error.AsCString());
756 return eExecutionDiscarded;
757 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000758 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000759 else
Jim Inghamf48169b2010-11-30 02:22:11 +0000760 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000761 const bool stop_others = true;
762 const bool try_all_threads = run_others;
763
764 Address wrapper_address (m_jit_start_addr);
765 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
766 wrapper_address,
767 struct_address,
768 stop_others,
769 unwind_on_error,
770 ignore_breakpoints,
771 (m_needs_object_ptr ? &object_ptr : NULL),
772 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
773 shared_ptr_to_me));
774
775 if (!call_plan_sp || !call_plan_sp->ValidatePlan (&error_stream))
776 return eExecutionSetupError;
777
778 function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
779
780 if (log)
781 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
782
783 if (exe_ctx.GetProcessPtr())
784 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
785
786 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
787 call_plan_sp,
788 stop_others,
789 try_all_threads,
790 unwind_on_error,
791 ignore_breakpoints,
792 timeout_usec,
793 error_stream);
794
795 if (exe_ctx.GetProcessPtr())
796 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
797
798 if (log)
799 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
800
801 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
802 {
803 const char *error_desc = NULL;
804
805 if (call_plan_sp)
806 {
807 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
808 if (real_stop_info_sp)
809 error_desc = real_stop_info_sp->GetDescription();
810 }
811 if (error_desc)
812 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
813 else
814 error_stream.Printf ("Execution was interrupted.");
815
816 if ((execution_result == eExecutionInterrupted && unwind_on_error)
817 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
818 error_stream.Printf ("\nThe process has been returned to the state before expression evaluation.");
819 else
820 error_stream.Printf ("\nThe process has been left at the point where it was interrupted, use \"thread return -x\" to return to the state before expression evaluation.");
821
822 return execution_result;
823 }
824 else if (execution_result != eExecutionCompleted)
825 {
826 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
827 return execution_result;
828 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000829 }
830
Sean Callanane359d9b2011-05-09 22:04:36 +0000831 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000832 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000833 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000834 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000835 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000836 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000837 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000838 }
839 else
840 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000841 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000842 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000843 }
844}
845
Greg Claytone0d378b2011-03-24 21:19:54 +0000846ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000847ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
848 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000849 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000850 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000851 bool unwind_on_error,
852 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000853 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000854 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000855 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000856 bool run_others,
857 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000858{
Jim Ingham41c75912011-08-09 00:00:49 +0000859 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000860 return EvaluateWithError (exe_ctx,
861 execution_policy,
862 language,
863 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000864 unwind_on_error,
865 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000866 expr_cstr,
867 expr_prefix,
868 result_valobj_sp,
869 error,
870 run_others,
871 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000872}
873
874ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000875ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
876 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000877 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000878 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000879 bool unwind_on_error,
880 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000881 const char *expr_cstr,
882 const char *expr_prefix,
883 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000884 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000885 bool run_others,
886 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000887{
Greg Clayton5160ce52013-03-27 23:08:40 +0000888 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000889
Greg Claytone0d378b2011-03-24 21:19:54 +0000890 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000891
Greg Claytonc14ee322011-09-22 04:58:26 +0000892 Process *process = exe_ctx.GetProcessPtr();
893
894 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000895 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000896 if (execution_policy == eExecutionPolicyAlways)
897 {
898 if (log)
899 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000900
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000901 error.SetErrorString ("expression needed to run but couldn't");
902
903 return execution_results;
904 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000905 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000906
Greg Claytonc14ee322011-09-22 04:58:26 +0000907 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000908 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000909
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000910 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000911
Greg Clayton0184f012010-10-05 00:31:29 +0000912 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000913
Sean Callanana162eba2010-12-07 22:55:01 +0000914 if (log)
915 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
916
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000917 const bool keep_expression_in_memory = true;
918
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000919 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000920 {
921 if (error_stream.GetString().empty())
922 error.SetErrorString ("expression failed to parse, unknown error");
923 else
924 error.SetErrorString (error_stream.GetString().c_str());
925 }
926 else
927 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000928 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000929
Sean Callanan1582ee62013-04-18 22:06:33 +0000930 if (execution_policy == eExecutionPolicyNever &&
931 !user_expression_sp->CanInterpret())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000932 {
933 if (log)
934 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
935
936 if (error_stream.GetString().empty())
937 error.SetErrorString ("expression needed to run but couldn't");
938 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000939 else
940 {
941 error_stream.GetString().clear();
942
943 if (log)
944 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
945
946 execution_results = user_expression_sp->Execute (error_stream,
947 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000948 unwind_on_error,
949 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000950 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000951 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000952 run_others,
953 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000954
Greg Claytone0d378b2011-03-24 21:19:54 +0000955 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000956 {
Sean Callanana162eba2010-12-07 22:55:01 +0000957 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000958 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
959
960 if (error_stream.GetString().empty())
961 error.SetErrorString ("expression failed to execute, unknown error");
962 else
963 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000964 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000965 else
Greg Clayton0184f012010-10-05 00:31:29 +0000966 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000967 if (expr_result)
968 {
969 result_valobj_sp = expr_result->GetValueObject();
970
971 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000972 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000973 }
974 else
975 {
976 if (log)
977 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
978
Sean Callananbccce812011-08-23 21:20:51 +0000979 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000980 }
Greg Clayton0184f012010-10-05 00:31:29 +0000981 }
982 }
983 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000984
Greg Claytonb71f3842010-10-05 03:13:51 +0000985 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000986 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000987
Jim Inghamf48169b2010-11-30 02:22:11 +0000988 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000989}