blob: 4ecd1890e0e50ab03189eec35c5d76d0d3f49f2c [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 Callanan96d27302013-04-11 00:09:05 +000032#include "lldb/Expression/Materializer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033#include "lldb/Host/Host.h"
Greg Clayton1f746072012-08-29 21:13:06 +000034#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000035#include "lldb/Symbol/ClangASTContext.h"
36#include "lldb/Symbol/Function.h"
37#include "lldb/Symbol/Type.h"
38#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000039#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000040#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000041#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000042#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000043#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000044#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000046
Sean Callanan72e49402011-08-05 23:43:37 +000047#include "clang/AST/DeclCXX.h"
48#include "clang/AST/DeclObjC.h"
49
Sean Callanan1a8d4092010-08-27 01:01:44 +000050using namespace lldb_private;
51
Sean Callanan322f5292010-10-29 00:29:03 +000052ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000053 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000054 lldb::LanguageType language,
55 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000056 ClangExpression (),
57 m_expr_text (expr),
58 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000059 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000060 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000061 m_desired_type (desired_type),
Sean Callananeab6cc92012-12-06 01:35:38 +000062 m_enforce_valid_object (true),
Greg Clayton22a939a2011-01-19 23:00:49 +000063 m_cplusplus (false),
64 m_objectivec (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000065 m_static_method(false),
Greg Clayton22a939a2011-01-19 23:00:49 +000066 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000067 m_const_object (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000068 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000069 m_evaluated_statically (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000070 m_const_result ()
Sean Callanan1a8d4092010-08-27 01:01:44 +000071{
Sean Callananc7b65062011-11-07 23:35:40 +000072 switch (m_language)
73 {
74 case lldb::eLanguageTypeC_plus_plus:
75 m_allow_cxx = true;
76 break;
77 case lldb::eLanguageTypeObjC:
78 m_allow_objc = true;
79 break;
80 case lldb::eLanguageTypeObjC_plus_plus:
81 default:
82 m_allow_cxx = true;
83 m_allow_objc = true;
84 break;
85 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000086}
87
Sean Callanane71d5532010-08-27 23:31:21 +000088ClangUserExpression::~ClangUserExpression ()
89{
90}
91
Sean Callanan1a8d4092010-08-27 01:01:44 +000092clang::ASTConsumer *
93ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000094{
Sean Callananbccce812011-08-23 21:20:51 +000095 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
96
97 if (!clang_ast_context)
98 return NULL;
99
Sean Callanan2590b9a2011-10-08 00:21:35 +0000100 if (!m_result_synthesizer.get())
101 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanan0eed0d42011-12-06 03:41:14 +0000102 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +0000103
104 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000105}
106
Sean Callananfc55f5d2010-09-21 00:44:12 +0000107void
Sean Callanan744756e2011-11-04 02:09:33 +0000108ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000109{
Greg Clayton5160ce52013-03-27 23:08:40 +0000110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan70385082012-12-01 00:08:33 +0000111
112 if (log)
113 log->Printf("ClangUserExpression::ScanContext()");
114
Greg Claytonc14ee322011-09-22 04:58:26 +0000115 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000116
Sean Callananc7b65062011-11-07 23:35:40 +0000117 if (!(m_allow_cxx || m_allow_objc))
Sean Callanan70385082012-12-01 00:08:33 +0000118 {
119 if (log)
120 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
Sean Callananc7b65062011-11-07 23:35:40 +0000121 return;
Sean Callanan70385082012-12-01 00:08:33 +0000122 }
Sean Callananc7b65062011-11-07 23:35:40 +0000123
Greg Claytonc14ee322011-09-22 04:58:26 +0000124 StackFrame *frame = exe_ctx.GetFramePtr();
125 if (frame == NULL)
Sean Callanan70385082012-12-01 00:08:33 +0000126 {
127 if (log)
128 log->Printf(" [CUE::SC] Null stack frame");
Sean Callananfc55f5d2010-09-21 00:44:12 +0000129 return;
Sean Callanan70385082012-12-01 00:08:33 +0000130 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000131
Sean Callanan5dd6c3d2012-07-13 21:20:29 +0000132 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
Sean Callanan3670ba52010-12-01 21:35:54 +0000133
Sean Callanan72e49402011-08-05 23:43:37 +0000134 if (!sym_ctx.function)
Sean Callanan70385082012-12-01 00:08:33 +0000135 {
136 if (log)
137 log->Printf(" [CUE::SC] Null function");
Sean Callanan72e49402011-08-05 23:43:37 +0000138 return;
Sean Callanan70385082012-12-01 00:08:33 +0000139 }
Sean Callanan72e49402011-08-05 23:43:37 +0000140
Greg Clayton685c88c2012-07-14 00:53:55 +0000141 // Find the block that defines the function represented by "sym_ctx"
142 Block *function_block = sym_ctx.GetFunctionBlock();
Sean Callanan72e49402011-08-05 23:43:37 +0000143
Greg Clayton685c88c2012-07-14 00:53:55 +0000144 if (!function_block)
Sean Callanan70385082012-12-01 00:08:33 +0000145 {
146 if (log)
147 log->Printf(" [CUE::SC] Null function block");
Greg Clayton685c88c2012-07-14 00:53:55 +0000148 return;
Sean Callanan70385082012-12-01 00:08:33 +0000149 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000150
151 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
152
Sean Callanan72e49402011-08-05 23:43:37 +0000153 if (!decl_context)
Sean Callanan70385082012-12-01 00:08:33 +0000154 {
155 if (log)
156 log->Printf(" [CUE::SC] Null decl context");
Sean Callanan72e49402011-08-05 23:43:37 +0000157 return;
Sean Callanan70385082012-12-01 00:08:33 +0000158 }
159
Sean Callanan72e49402011-08-05 23:43:37 +0000160 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000161 {
Sean Callananc7b65062011-11-07 23:35:40 +0000162 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000163 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000164 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000165 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000166 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000167
168 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
169
Greg Clayton685c88c2012-07-14 00:53:55 +0000170 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000171 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000172 err.SetErrorString(thisErrorString);
173 return;
174 }
175
Greg Clayton685c88c2012-07-14 00:53:55 +0000176 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
Sean Callanand5cc1322011-12-13 01:42:04 +0000177
Greg Clayton685c88c2012-07-14 00:53:55 +0000178 if (!this_var_sp ||
179 !this_var_sp->IsInScope(frame) ||
180 !this_var_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000181 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000182 err.SetErrorString(thisErrorString);
183 return;
184 }
Sean Callanan744756e2011-11-04 02:09:33 +0000185 }
186
Sean Callanan72e49402011-08-05 23:43:37 +0000187 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000188 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000189 }
190 }
Sean Callanan72e49402011-08-05 23:43:37 +0000191 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000192 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000193 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000194 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000195 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000196 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000197 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000198
199 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
200
Greg Clayton685c88c2012-07-14 00:53:55 +0000201 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000202 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000203 err.SetErrorString(selfErrorString);
204 return;
205 }
206
Greg Clayton685c88c2012-07-14 00:53:55 +0000207 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
Sean Callanand5cc1322011-12-13 01:42:04 +0000208
Greg Clayton685c88c2012-07-14 00:53:55 +0000209 if (!self_variable_sp ||
210 !self_variable_sp->IsInScope(frame) ||
211 !self_variable_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000212 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000213 err.SetErrorString(selfErrorString);
214 return;
215 }
Sean Callanan744756e2011-11-04 02:09:33 +0000216 }
217
Sean Callanan72e49402011-08-05 23:43:37 +0000218 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000219 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000220
221 if (!method_decl->isInstanceMethod())
222 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000223 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000224 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000225 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context))
226 {
227 // We might also have a function that said in the debug information that it captured an
228 // object pointer. The best way to deal with getting to the ivars at present it by pretending
229 // that this is a method of a class in whatever runtime the debug info says the object pointer
230 // belongs to. Do that here.
231
Greg Claytond0029442013-03-27 01:48:02 +0000232 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), function_decl);
Jim Ingham5fdeed42012-10-30 23:35:54 +0000233 if (metadata && metadata->HasObjectPtr())
234 {
235 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
236 if (language == lldb::eLanguageTypeC_plus_plus)
237 {
Sean Callanana2868d42013-01-19 01:49:02 +0000238 if (m_enforce_valid_object)
239 {
240 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
241
242 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";
243
244 if (!variable_list_sp)
245 {
246 err.SetErrorString(thisErrorString);
247 return;
248 }
249
250 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
251
252 if (!this_var_sp ||
253 !this_var_sp->IsInScope(frame) ||
254 !this_var_sp->LocationIsValidForFrame (frame))
255 {
256 err.SetErrorString(thisErrorString);
257 return;
258 }
259 }
260
Jim Ingham5fdeed42012-10-30 23:35:54 +0000261 m_cplusplus = true;
262 m_needs_object_ptr = true;
263 }
264 else if (language == lldb::eLanguageTypeObjC)
265 {
Sean Callanana2868d42013-01-19 01:49:02 +0000266 if (m_enforce_valid_object)
267 {
268 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
269
270 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";
271
272 if (!variable_list_sp)
273 {
274 err.SetErrorString(selfErrorString);
275 return;
276 }
277
278 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
279
280 if (!self_variable_sp ||
281 !self_variable_sp->IsInScope(frame) ||
282 !self_variable_sp->LocationIsValidForFrame (frame))
283 {
284 err.SetErrorString(selfErrorString);
285 return;
286 }
287
288 Type *self_type = self_variable_sp->GetType();
289
290 if (!self_type)
291 {
292 err.SetErrorString(selfErrorString);
293 return;
294 }
295
296 lldb::clang_type_t self_opaque_type = self_type->GetClangForwardType();
297
298 if (!self_opaque_type)
299 {
300 err.SetErrorString(selfErrorString);
301 return;
302 }
303
304 clang::QualType self_qual_type = clang::QualType::getFromOpaquePtr(self_opaque_type);
305
306 if (self_qual_type->isObjCClassType())
307 {
308 return;
309 }
310 else if (self_qual_type->isObjCObjectPointerType())
311 {
312 m_objectivec = true;
313 m_needs_object_ptr = true;
314 }
315 else
316 {
317 err.SetErrorString(selfErrorString);
318 return;
319 }
320 }
321 else
322 {
323 m_objectivec = true;
324 m_needs_object_ptr = true;
325 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000326 }
327 }
328 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000329}
330
Sean Callanancf5498f2010-10-22 23:25:16 +0000331// This is a really nasty hack, meant to fix Objective-C expressions of the form
332// (int)[myArray count]. Right now, because the type information for count is
333// not available, [myArray count] returns id, which can't be directly cast to
334// int without causing a clang error.
335static void
336ApplyObjcCastHack(std::string &expr)
337{
338#define OBJC_CAST_HACK_FROM "(int)["
339#define OBJC_CAST_HACK_TO "(int)(long long)["
340
341 size_t from_offset;
342
343 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
344 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
345
346#undef OBJC_CAST_HACK_TO
347#undef OBJC_CAST_HACK_FROM
348}
349
Sean Callanan64186e72010-10-24 20:45:49 +0000350// Another hack, meant to allow use of unichar despite it not being available in
351// the type information. Although we could special-case it in type lookup,
352// hopefully we'll figure out a way to #include the same environment as is
353// present in the original source file rather than try to hack specific type
354// definitions in as needed.
355static void
356ApplyUnicharHack(std::string &expr)
357{
358#define UNICHAR_HACK_FROM "unichar"
359#define UNICHAR_HACK_TO "unsigned short"
360
361 size_t from_offset;
362
363 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
364 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
365
366#undef UNICHAR_HACK_TO
367#undef UNICHAR_HACK_FROM
368}
369
Sean Callanancf5498f2010-10-22 23:25:16 +0000370bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000371ClangUserExpression::Parse (Stream &error_stream,
372 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000373 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000374 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000375{
Greg Clayton5160ce52013-03-27 23:08:40 +0000376 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000377
Sean Callanan744756e2011-11-04 02:09:33 +0000378 Error err;
Sean Callanan933693b2012-02-10 01:22:05 +0000379
380 InstallContext(exe_ctx);
Sean Callanan744756e2011-11-04 02:09:33 +0000381
382 ScanContext(exe_ctx, err);
383
384 if (!err.Success())
385 {
386 error_stream.Printf("warning: %s\n", err.AsCString());
387 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000388
389 StreamString m_transformed_stream;
390
391 ////////////////////////////////////
392 // Generate the expression
393 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000394
395 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000396 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000397
Sean Callanan9bc83842011-09-26 18:45:31 +0000398 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
399
400 lldb::LanguageType lang_type;
401
Sean Callananfc55f5d2010-09-21 00:44:12 +0000402 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000403 lang_type = lldb::eLanguageTypeC_plus_plus;
404 else if(m_objectivec)
405 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000406 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000407 lang_type = lldb::eLanguageTypeC;
408
Sean Callanand5c17ed2011-11-15 02:11:17 +0000409 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000410 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000411 error_stream.PutCString ("error: couldn't construct expression body");
412 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000413 }
414
Sean Callananfc55f5d2010-09-21 00:44:12 +0000415 if (log)
416 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
417
Sean Callanan1a8d4092010-08-27 01:01:44 +0000418 ////////////////////////////////////
419 // Set up the target and compiler
420 //
421
Greg Claytonc14ee322011-09-22 04:58:26 +0000422 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000423
424 if (!target)
425 {
426 error_stream.PutCString ("error: invalid target\n");
427 return false;
428 }
429
Sean Callanan1a8d4092010-08-27 01:01:44 +0000430 //////////////////////////
431 // Parse the expression
432 //
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000433
Sean Callanan96d27302013-04-11 00:09:05 +0000434 m_materializer_ap.reset(new Materializer());
435
Sean Callanan1ee44b72011-10-29 01:58:46 +0000436 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000437
Sean Callanan96d27302013-04-11 00:09:05 +0000438 if (!m_expr_decl_map->WillParse(exe_ctx, m_materializer_ap.get()))
Sean Callananb9951192011-08-01 18:18:33 +0000439 {
440 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
441 return false;
442 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000443
Greg Claytonc14ee322011-09-22 04:58:26 +0000444 Process *process = exe_ctx.GetProcessPtr();
Sean Callananaa719af2012-02-08 18:43:35 +0000445 ExecutionContextScope *exe_scope = process;
446
447 if (!exe_scope)
448 exe_scope = exe_ctx.GetTargetPtr();
449
450 ClangExpressionParser parser(exe_scope, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000451
452 unsigned num_errors = parser.Parse (error_stream);
453
454 if (num_errors)
455 {
456 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000457
458 m_expr_decl_map->DidParse();
459
Sean Callanan1a8d4092010-08-27 01:01:44 +0000460 return false;
461 }
462
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000463 //////////////////////////////////////////////////////////////////////////////////////////
464 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000465 //
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000466
467 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000468 m_jit_end_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000469 m_execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000470 exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000471 m_evaluated_statically,
472 m_const_result,
473 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
492bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000493ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000494 ExecutionContext &exe_ctx,
495 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000496 lldb::addr_t &object_ptr,
497 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000498{
Greg Clayton5160ce52013-03-27 23:08:40 +0000499 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000500
Sean Callanan933693b2012-02-10 01:22:05 +0000501 lldb::TargetSP target;
502 lldb::ProcessSP process;
503 lldb::StackFrameSP frame;
504
505 if (!LockAndCheckContext(exe_ctx,
506 target,
507 process,
508 frame))
509 {
510 error_stream.Printf("The context has changed before we could JIT the expression!");
511 return false;
512 }
513
Greg Clayton22a939a2011-01-19 23:00:49 +0000514 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000515 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000516 Error materialize_error;
517
Sean Callanan17827832010-12-13 22:46:15 +0000518 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000519 {
Sean Callanan17827832010-12-13 22:46:15 +0000520 ConstString object_name;
521
522 if (m_cplusplus)
523 {
524 object_name.SetCString("this");
525 }
526 else if (m_objectivec)
527 {
528 object_name.SetCString("self");
529 }
530 else
531 {
532 error_stream.Printf("Need object pointer but don't know the language\n");
533 return false;
534 }
535
Sean Callanan933693b2012-02-10 01:22:05 +0000536 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, materialize_error)))
Sean Callanan17827832010-12-13 22:46:15 +0000537 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000538 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
539 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000540 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000541
542 if (m_objectivec)
543 {
544 ConstString cmd_name("_cmd");
545
Sean Callanan933693b2012-02-10 01:22:05 +0000546 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, materialize_error, true)))
Sean Callanan9d48e802010-12-14 00:42:36 +0000547 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000548 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
549 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000550 }
551 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000552 }
553
Sean Callanan933693b2012-02-10 01:22:05 +0000554 if (!m_expr_decl_map->Materialize(struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000555 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000556 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000557 return false;
558 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000559
560#if 0
561 // jingham: look here
562 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000563 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 +0000564#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000565
566 if (log)
567 {
Sean Callanana162eba2010-12-07 22:55:01 +0000568 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000569
Daniel Malead01b2952012-11-29 21:49:15 +0000570 log->Printf(" Function address : 0x%" PRIx64, (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000571
572 if (m_needs_object_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +0000573 log->Printf(" Object pointer : 0x%" PRIx64, (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000574
Daniel Malead01b2952012-11-29 21:49:15 +0000575 log->Printf(" Structure address : 0x%" PRIx64, (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000576
577 StreamString args;
578
579 Error dump_error;
580
Sean Callanan9e6ed532010-09-13 21:34:21 +0000581 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000582 {
Sean Callanan933693b2012-02-10 01:22:05 +0000583 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000584 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000585 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000586 }
587 else
588 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000589 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000590 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000591 }
592 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000593 }
594 return true;
595}
596
597ThreadPlan *
598ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000599 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000600{
601 lldb::addr_t struct_address;
602
Johnny Chen44805302011-07-19 19:48:13 +0000603 lldb::addr_t object_ptr = 0;
604 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000605
Sean Callanan9d48e802010-12-14 00:42:36 +0000606 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000607
Jim Inghamf48169b2010-11-30 02:22:11 +0000608 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
609 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000610 // forcing unwind_on_error to be true here, in practical terms that can't happen.
611
Jim Ingham184e9812013-01-15 02:47:48 +0000612 const bool stop_others = true;
613 const bool unwind_on_error = true;
614 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000615 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000616 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000617 struct_address,
618 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000619 stop_others,
620 unwind_on_error,
621 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000622 (m_needs_object_ptr ? &object_ptr : NULL),
623 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000624}
625
626bool
627ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
628 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000629 lldb::ClangExpressionVariableSP &result,
630 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000631{
632 Error expr_error;
633
Greg Clayton5160ce52013-03-27 23:08:40 +0000634 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc673a6e2010-12-07 10:00:20 +0000635
636 if (log)
637 {
Sean Callanana162eba2010-12-07 22:55:01 +0000638 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000639
640 StreamString args;
641
642 Error dump_error;
643
Sean Callanan933693b2012-02-10 01:22:05 +0000644 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callananc673a6e2010-12-07 10:00:20 +0000645 {
646 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
647 }
648 else
649 {
650 log->Printf(" Structure contents:\n%s", args.GetData());
651 }
652 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000653
654 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
655
Sean Callananc673a6e2010-12-07 10:00:20 +0000656
Sean Callanan933693b2012-02-10 01:22:05 +0000657 if (!m_expr_decl_map->Dematerialize(result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000658 {
659 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
660 return false;
661 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000662
Johnny Chenb49440f2012-01-06 00:35:38 +0000663 if (result)
664 result->TransferAddress();
665
Jim Ingham36f3b362010-10-14 23:45:03 +0000666 return true;
667}
668
Greg Claytone0d378b2011-03-24 21:19:54 +0000669ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000670ClangUserExpression::Execute (Stream &error_stream,
671 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000672 bool unwind_on_error,
673 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000674 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000675 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000676 bool run_others,
677 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000678{
Jim Inghamb086ff72011-01-18 22:20:08 +0000679 // The expression log is quite verbose, and if you're just tracking the execution of the
680 // expression, it's quite convenient to have these logs come out with the STEP log as well.
Greg Clayton5160ce52013-03-27 23:08:40 +0000681 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000682
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000683 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000684 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000685 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000686
Johnny Chen44805302011-07-19 19:48:13 +0000687 lldb::addr_t object_ptr = 0;
688 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000689
Johnny Chen8115c6d2012-08-18 04:24:00 +0000690 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
691 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000692 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000693 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000694 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000695
Jim Ingham399f1ca2010-11-05 19:25:48 +0000696 const bool stop_others = true;
Jim Ingham0161b492013-02-09 01:29:05 +0000697 const bool try_all_threads = run_others;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000698
Greg Claytone72dfb32012-02-24 01:59:29 +0000699 Address wrapper_address (m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000700 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000701 wrapper_address,
702 struct_address,
703 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000704 unwind_on_error,
705 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000706 (m_needs_object_ptr ? &object_ptr : NULL),
707 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
708 shared_ptr_to_me));
709
Jim Ingham23460c32013-03-28 00:08:00 +0000710 if (!call_plan_sp || !call_plan_sp->ValidatePlan (&error_stream))
Greg Claytone0d378b2011-03-24 21:19:54 +0000711 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000712
713 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham95afbf52012-12-07 19:04:31 +0000714
Sean Callananc673a6e2010-12-07 10:00:20 +0000715 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000716 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000717
Jim Ingham0faa43f2011-11-08 03:00:11 +0000718 if (exe_ctx.GetProcessPtr())
719 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
720
Greg Claytonc14ee322011-09-22 04:58:26 +0000721 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
722 call_plan_sp,
723 stop_others,
724 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000725 unwind_on_error,
726 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000727 timeout_usec,
Greg Claytonc14ee322011-09-22 04:58:26 +0000728 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000729
Jim Ingham0faa43f2011-11-08 03:00:11 +0000730 if (exe_ctx.GetProcessPtr())
731 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
732
Sean Callananc673a6e2010-12-07 10:00:20 +0000733 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000734 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000735
Jim Ingham184e9812013-01-15 02:47:48 +0000736 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000737 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000738 const char *error_desc = NULL;
739
740 if (call_plan_sp)
741 {
742 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
743 if (real_stop_info_sp)
744 error_desc = real_stop_info_sp->GetDescription();
745 }
746 if (error_desc)
747 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000748 else
Jason Molendafd54b362011-09-20 21:44:10 +0000749 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000750
Jim Ingham184e9812013-01-15 02:47:48 +0000751 if ((execution_result == eExecutionInterrupted && unwind_on_error)
752 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
Jim Ingham93208b82013-01-31 21:46:01 +0000753 error_stream.Printf ("\nThe process has been returned to the state before expression evaluation.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000754 else
Jim Ingham93208b82013-01-31 21:46:01 +0000755 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.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000756
757 return execution_result;
758 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000759 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000760 {
761 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
762 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000763 }
764
Sean Callanane359d9b2011-05-09 22:04:36 +0000765 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000766 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000767 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000768 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000769 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000770 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000771 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000772 }
773 else
774 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000775 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000776 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000777 }
778}
779
Greg Claytone0d378b2011-03-24 21:19:54 +0000780ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000781ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
782 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000783 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000784 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000785 bool unwind_on_error,
786 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000787 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000788 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000789 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000790 bool run_others,
791 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000792{
Jim Ingham41c75912011-08-09 00:00:49 +0000793 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000794 return EvaluateWithError (exe_ctx,
795 execution_policy,
796 language,
797 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000798 unwind_on_error,
799 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000800 expr_cstr,
801 expr_prefix,
802 result_valobj_sp,
803 error,
804 run_others,
805 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000806}
807
808ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000809ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
810 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000811 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000812 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000813 bool unwind_on_error,
814 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000815 const char *expr_cstr,
816 const char *expr_prefix,
817 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000818 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000819 bool run_others,
820 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000821{
Greg Clayton5160ce52013-03-27 23:08:40 +0000822 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000823
Greg Claytone0d378b2011-03-24 21:19:54 +0000824 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000825
Greg Claytonc14ee322011-09-22 04:58:26 +0000826 Process *process = exe_ctx.GetProcessPtr();
827
828 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000829 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000830 if (execution_policy == eExecutionPolicyAlways)
831 {
832 if (log)
833 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000834
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000835 error.SetErrorString ("expression needed to run but couldn't");
836
837 return execution_results;
838 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000839 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000840
Greg Claytonc14ee322011-09-22 04:58:26 +0000841 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000842 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000843
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000844 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000845
Greg Clayton0184f012010-10-05 00:31:29 +0000846 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000847
Sean Callanana162eba2010-12-07 22:55:01 +0000848 if (log)
849 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
850
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000851 const bool keep_expression_in_memory = true;
852
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000853 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000854 {
855 if (error_stream.GetString().empty())
856 error.SetErrorString ("expression failed to parse, unknown error");
857 else
858 error.SetErrorString (error_stream.GetString().c_str());
859 }
860 else
861 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000862 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000863
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000864 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000865 {
Sean Callanana162eba2010-12-07 22:55:01 +0000866 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000867 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000868
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000869 if (user_expression_sp->m_const_result)
870 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
871 else
872 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
873
Jim Ingham41c75912011-08-09 00:00:49 +0000874 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000875 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000876 else if (execution_policy == eExecutionPolicyNever)
877 {
878 if (log)
879 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
880
881 if (error_stream.GetString().empty())
882 error.SetErrorString ("expression needed to run but couldn't");
883 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000884 else
885 {
886 error_stream.GetString().clear();
887
888 if (log)
889 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
890
891 execution_results = user_expression_sp->Execute (error_stream,
892 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000893 unwind_on_error,
894 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000895 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000896 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000897 run_others,
898 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000899
Greg Claytone0d378b2011-03-24 21:19:54 +0000900 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000901 {
Sean Callanana162eba2010-12-07 22:55:01 +0000902 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000903 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
904
905 if (error_stream.GetString().empty())
906 error.SetErrorString ("expression failed to execute, unknown error");
907 else
908 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000909 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000910 else
Greg Clayton0184f012010-10-05 00:31:29 +0000911 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000912 if (expr_result)
913 {
914 result_valobj_sp = expr_result->GetValueObject();
915
916 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000917 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000918 }
919 else
920 {
921 if (log)
922 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
923
Sean Callananbccce812011-08-23 21:20:51 +0000924 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000925 }
Greg Clayton0184f012010-10-05 00:31:29 +0000926 }
927 }
928 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000929
Greg Claytonb71f3842010-10-05 03:13:51 +0000930 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000931 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000932
Jim Inghamf48169b2010-11-30 02:22:11 +0000933 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000934}