blob: 6f65b11a6869cf1d86d6a154abbdefc36e0b0592 [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 Callanan96d27302013-04-11 00:09:05 +000033#include "lldb/Expression/Materializer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000034#include "lldb/Host/Host.h"
Greg Clayton1f746072012-08-29 21:13:06 +000035#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000036#include "lldb/Symbol/ClangASTContext.h"
37#include "lldb/Symbol/Function.h"
38#include "lldb/Symbol/Type.h"
39#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000040#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000041#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000042#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000043#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000044#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000045#include "lldb/Target/ThreadPlan.h"
46#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000047
Sean Callanan72e49402011-08-05 23:43:37 +000048#include "clang/AST/DeclCXX.h"
49#include "clang/AST/DeclObjC.h"
50
Sean Callanan1a8d4092010-08-27 01:01:44 +000051using namespace lldb_private;
52
Sean Callanan322f5292010-10-29 00:29:03 +000053ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000054 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000055 lldb::LanguageType language,
56 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000057 ClangExpression (),
58 m_expr_text (expr),
59 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000060 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000061 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000062 m_desired_type (desired_type),
Sean Callananeab6cc92012-12-06 01:35:38 +000063 m_enforce_valid_object (true),
Greg Clayton22a939a2011-01-19 23:00:49 +000064 m_cplusplus (false),
65 m_objectivec (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000066 m_static_method(false),
Greg Clayton22a939a2011-01-19 23:00:49 +000067 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000068 m_const_object (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000069 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000070 m_evaluated_statically (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000071 m_const_result ()
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 Callanan3bfdaa22011-09-15 02:13:07 +0000472 m_evaluated_statically,
473 m_const_result,
474 execution_policy);
Sean Callanane3aef1d2011-10-12 22:20:02 +0000475
Sean Callanan1a8d4092010-08-27 01:01:44 +0000476 if (jit_error.Success())
477 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000478 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000479 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000480 return true;
481 }
482 else
483 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000484 const char *error_cstr = jit_error.AsCString();
485 if (error_cstr && error_cstr[0])
486 error_stream.Printf ("error: %s\n", error_cstr);
487 else
Jason Molendafd54b362011-09-20 21:44:10 +0000488 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000489 return false;
490 }
491}
492
493bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000494ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000495 ExecutionContext &exe_ctx,
496 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000497 lldb::addr_t &object_ptr,
498 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000499{
Greg Clayton5160ce52013-03-27 23:08:40 +0000500 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000501
Sean Callanan933693b2012-02-10 01:22:05 +0000502 lldb::TargetSP target;
503 lldb::ProcessSP process;
504 lldb::StackFrameSP frame;
505
506 if (!LockAndCheckContext(exe_ctx,
507 target,
508 process,
509 frame))
510 {
511 error_stream.Printf("The context has changed before we could JIT the expression!");
512 return false;
513 }
514
Greg Clayton22a939a2011-01-19 23:00:49 +0000515 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000516 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000517 Error materialize_error;
518
Sean Callanan17827832010-12-13 22:46:15 +0000519 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000520 {
Sean Callanan17827832010-12-13 22:46:15 +0000521 ConstString object_name;
522
523 if (m_cplusplus)
524 {
525 object_name.SetCString("this");
526 }
527 else if (m_objectivec)
528 {
529 object_name.SetCString("self");
530 }
531 else
532 {
533 error_stream.Printf("Need object pointer but don't know the language\n");
534 return false;
535 }
536
Sean Callanan933693b2012-02-10 01:22:05 +0000537 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, materialize_error)))
Sean Callanan17827832010-12-13 22:46:15 +0000538 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000539 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
540 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000541 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000542
543 if (m_objectivec)
544 {
545 ConstString cmd_name("_cmd");
546
Sean Callanan933693b2012-02-10 01:22:05 +0000547 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, materialize_error, true)))
Sean Callanan9d48e802010-12-14 00:42:36 +0000548 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000549 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
550 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000551 }
552 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000553 }
554
Sean Callanan14b1bae2013-04-16 23:25:35 +0000555 if (!m_expr_decl_map->Materialize(*m_execution_unit_ap, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000556 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000557 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000558 return false;
559 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000560
561#if 0
562 // jingham: look here
563 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000564 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 +0000565#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000566
567 if (log)
568 {
Sean Callanana162eba2010-12-07 22:55:01 +0000569 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000570
Daniel Malead01b2952012-11-29 21:49:15 +0000571 log->Printf(" Function address : 0x%" PRIx64, (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000572
573 if (m_needs_object_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +0000574 log->Printf(" Object pointer : 0x%" PRIx64, (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000575
Daniel Malead01b2952012-11-29 21:49:15 +0000576 log->Printf(" Structure address : 0x%" PRIx64, (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000577
578 StreamString args;
579
580 Error dump_error;
581
Sean Callanan9e6ed532010-09-13 21:34:21 +0000582 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000583 {
Sean Callanan933693b2012-02-10 01:22:05 +0000584 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000585 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000586 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000587 }
588 else
589 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000590 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000591 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000592 }
593 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000594 }
595 return true;
596}
597
598ThreadPlan *
599ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000600 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000601{
602 lldb::addr_t struct_address;
603
Johnny Chen44805302011-07-19 19:48:13 +0000604 lldb::addr_t object_ptr = 0;
605 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000606
Sean Callanan9d48e802010-12-14 00:42:36 +0000607 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000608
Jim Inghamf48169b2010-11-30 02:22:11 +0000609 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
610 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000611 // forcing unwind_on_error to be true here, in practical terms that can't happen.
612
Jim Ingham184e9812013-01-15 02:47:48 +0000613 const bool stop_others = true;
614 const bool unwind_on_error = true;
615 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000616 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000617 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000618 struct_address,
619 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000620 stop_others,
621 unwind_on_error,
622 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000623 (m_needs_object_ptr ? &object_ptr : NULL),
624 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000625}
626
627bool
628ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
629 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000630 lldb::ClangExpressionVariableSP &result,
631 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000632{
633 Error expr_error;
634
Greg Clayton5160ce52013-03-27 23:08:40 +0000635 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc673a6e2010-12-07 10:00:20 +0000636
637 if (log)
638 {
Sean Callanana162eba2010-12-07 22:55:01 +0000639 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000640
641 StreamString args;
642
643 Error dump_error;
644
Sean Callanan933693b2012-02-10 01:22:05 +0000645 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callananc673a6e2010-12-07 10:00:20 +0000646 {
647 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
648 }
649 else
650 {
651 log->Printf(" Structure contents:\n%s", args.GetData());
652 }
653 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000654
655 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
656
Sean Callananc673a6e2010-12-07 10:00:20 +0000657
Sean Callanan14b1bae2013-04-16 23:25:35 +0000658 if (!m_expr_decl_map->Dematerialize(result, *m_execution_unit_ap, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000659 {
660 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
661 return false;
662 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000663
Johnny Chenb49440f2012-01-06 00:35:38 +0000664 if (result)
665 result->TransferAddress();
666
Jim Ingham36f3b362010-10-14 23:45:03 +0000667 return true;
668}
669
Greg Claytone0d378b2011-03-24 21:19:54 +0000670ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000671ClangUserExpression::Execute (Stream &error_stream,
672 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000673 bool unwind_on_error,
674 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000675 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000676 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000677 bool run_others,
678 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000679{
Jim Inghamb086ff72011-01-18 22:20:08 +0000680 // The expression log is quite verbose, and if you're just tracking the execution of the
681 // expression, it's quite convenient to have these logs come out with the STEP log as well.
Greg Clayton5160ce52013-03-27 23:08:40 +0000682 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000683
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000684 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000685 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000686 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000687
Johnny Chen44805302011-07-19 19:48:13 +0000688 lldb::addr_t object_ptr = 0;
689 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000690
Johnny Chen8115c6d2012-08-18 04:24:00 +0000691 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
692 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000693 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000694 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000695 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000696
Jim Ingham399f1ca2010-11-05 19:25:48 +0000697 const bool stop_others = true;
Jim Ingham0161b492013-02-09 01:29:05 +0000698 const bool try_all_threads = run_others;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000699
Greg Claytone72dfb32012-02-24 01:59:29 +0000700 Address wrapper_address (m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000701 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000702 wrapper_address,
703 struct_address,
704 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000705 unwind_on_error,
706 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000707 (m_needs_object_ptr ? &object_ptr : NULL),
708 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
709 shared_ptr_to_me));
710
Jim Ingham23460c32013-03-28 00:08:00 +0000711 if (!call_plan_sp || !call_plan_sp->ValidatePlan (&error_stream))
Greg Claytone0d378b2011-03-24 21:19:54 +0000712 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000713
714 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham95afbf52012-12-07 19:04:31 +0000715
Sean Callananc673a6e2010-12-07 10:00:20 +0000716 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000717 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000718
Jim Ingham0faa43f2011-11-08 03:00:11 +0000719 if (exe_ctx.GetProcessPtr())
720 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
721
Greg Claytonc14ee322011-09-22 04:58:26 +0000722 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
723 call_plan_sp,
724 stop_others,
725 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000726 unwind_on_error,
727 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000728 timeout_usec,
Greg Claytonc14ee322011-09-22 04:58:26 +0000729 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000730
Jim Ingham0faa43f2011-11-08 03:00:11 +0000731 if (exe_ctx.GetProcessPtr())
732 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
733
Sean Callananc673a6e2010-12-07 10:00:20 +0000734 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000735 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000736
Jim Ingham184e9812013-01-15 02:47:48 +0000737 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000738 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000739 const char *error_desc = NULL;
740
741 if (call_plan_sp)
742 {
743 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
744 if (real_stop_info_sp)
745 error_desc = real_stop_info_sp->GetDescription();
746 }
747 if (error_desc)
748 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000749 else
Jason Molendafd54b362011-09-20 21:44:10 +0000750 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000751
Jim Ingham184e9812013-01-15 02:47:48 +0000752 if ((execution_result == eExecutionInterrupted && unwind_on_error)
753 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
Jim Ingham93208b82013-01-31 21:46:01 +0000754 error_stream.Printf ("\nThe process has been returned to the state before expression evaluation.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000755 else
Jim Ingham93208b82013-01-31 21:46:01 +0000756 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 +0000757
758 return execution_result;
759 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000760 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000761 {
762 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
763 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000764 }
765
Sean Callanane359d9b2011-05-09 22:04:36 +0000766 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000767 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000768 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000769 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000770 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000771 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000772 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000773 }
774 else
775 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000776 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000777 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000778 }
779}
780
Greg Claytone0d378b2011-03-24 21:19:54 +0000781ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000782ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
783 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000784 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000785 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000786 bool unwind_on_error,
787 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000788 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000789 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000790 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000791 bool run_others,
792 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000793{
Jim Ingham41c75912011-08-09 00:00:49 +0000794 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000795 return EvaluateWithError (exe_ctx,
796 execution_policy,
797 language,
798 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000799 unwind_on_error,
800 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000801 expr_cstr,
802 expr_prefix,
803 result_valobj_sp,
804 error,
805 run_others,
806 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000807}
808
809ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000810ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
811 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000812 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000813 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000814 bool unwind_on_error,
815 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000816 const char *expr_cstr,
817 const char *expr_prefix,
818 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000819 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000820 bool run_others,
821 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000822{
Greg Clayton5160ce52013-03-27 23:08:40 +0000823 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000824
Greg Claytone0d378b2011-03-24 21:19:54 +0000825 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000826
Greg Claytonc14ee322011-09-22 04:58:26 +0000827 Process *process = exe_ctx.GetProcessPtr();
828
829 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000830 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000831 if (execution_policy == eExecutionPolicyAlways)
832 {
833 if (log)
834 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000835
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000836 error.SetErrorString ("expression needed to run but couldn't");
837
838 return execution_results;
839 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000840 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000841
Greg Claytonc14ee322011-09-22 04:58:26 +0000842 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000843 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000844
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000845 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000846
Greg Clayton0184f012010-10-05 00:31:29 +0000847 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000848
Sean Callanana162eba2010-12-07 22:55:01 +0000849 if (log)
850 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
851
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000852 const bool keep_expression_in_memory = true;
853
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000854 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000855 {
856 if (error_stream.GetString().empty())
857 error.SetErrorString ("expression failed to parse, unknown error");
858 else
859 error.SetErrorString (error_stream.GetString().c_str());
860 }
861 else
862 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000863 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000864
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000865 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000866 {
Sean Callanana162eba2010-12-07 22:55:01 +0000867 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000868 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000869
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000870 if (user_expression_sp->m_const_result)
871 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
872 else
873 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
874
Jim Ingham41c75912011-08-09 00:00:49 +0000875 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000876 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000877 else if (execution_policy == eExecutionPolicyNever)
878 {
879 if (log)
880 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
881
882 if (error_stream.GetString().empty())
883 error.SetErrorString ("expression needed to run but couldn't");
884 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000885 else
886 {
887 error_stream.GetString().clear();
888
889 if (log)
890 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
891
892 execution_results = user_expression_sp->Execute (error_stream,
893 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000894 unwind_on_error,
895 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000896 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000897 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000898 run_others,
899 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000900
Greg Claytone0d378b2011-03-24 21:19:54 +0000901 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000902 {
Sean Callanana162eba2010-12-07 22:55:01 +0000903 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000904 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
905
906 if (error_stream.GetString().empty())
907 error.SetErrorString ("expression failed to execute, unknown error");
908 else
909 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000910 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000911 else
Greg Clayton0184f012010-10-05 00:31:29 +0000912 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000913 if (expr_result)
914 {
915 result_valobj_sp = expr_result->GetValueObject();
916
917 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000918 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000919 }
920 else
921 {
922 if (log)
923 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
924
Sean Callananbccce812011-08-23 21:20:51 +0000925 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000926 }
Greg Clayton0184f012010-10-05 00:31:29 +0000927 }
928 }
929 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000930
Greg Claytonb71f3842010-10-05 03:13:51 +0000931 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000932 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000933
Jim Inghamf48169b2010-11-30 02:22:11 +0000934 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000935}