blob: 66783812508b5be757e2a4e0284473f55d6bc523 [file] [log] [blame]
Sean Callanan79763a42011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan1a8d4092010-08-27 01:01:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17#include <cstdlib>
18#include <string>
19#include <map>
20
21#include "lldb/Core/ConstString.h"
22#include "lldb/Core/Log.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000023#include "lldb/Core/StreamFile.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000024#include "lldb/Core/StreamString.h"
Greg Claytonb71f3842010-10-05 03:13:51 +000025#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000026#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000027#include "lldb/Expression/ClangExpressionDeclMap.h"
28#include "lldb/Expression/ClangExpressionParser.h"
29#include "lldb/Expression/ClangFunction.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000030#include "lldb/Expression/ClangUserExpression.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000031#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Host/Host.h"
Greg Clayton1f746072012-08-29 21:13:06 +000033#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000034#include "lldb/Symbol/ClangASTContext.h"
35#include "lldb/Symbol/Function.h"
36#include "lldb/Symbol/Type.h"
37#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000038#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000039#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000040#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000041#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000042#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000043#include "lldb/Target/ThreadPlan.h"
44#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000045
Sean Callanan72e49402011-08-05 23:43:37 +000046#include "clang/AST/DeclCXX.h"
47#include "clang/AST/DeclObjC.h"
48
Sean Callanan1a8d4092010-08-27 01:01:44 +000049using namespace lldb_private;
50
Sean Callanan322f5292010-10-29 00:29:03 +000051ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000052 const char *expr_prefix,
Sean Callanan20bb3aa2011-12-21 22:22:58 +000053 lldb::LanguageType language,
54 ResultType desired_type) :
Greg Clayton22a939a2011-01-19 23:00:49 +000055 ClangExpression (),
56 m_expr_text (expr),
57 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000058 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000059 m_transformed_text (),
Sean Callanan20bb3aa2011-12-21 22:22:58 +000060 m_desired_type (desired_type),
Sean Callananeab6cc92012-12-06 01:35:38 +000061 m_enforce_valid_object (true),
Greg Clayton22a939a2011-01-19 23:00:49 +000062 m_cplusplus (false),
63 m_objectivec (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000064 m_static_method(false),
Greg Clayton22a939a2011-01-19 23:00:49 +000065 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000066 m_const_object (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000067 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000068 m_evaluated_statically (false),
Bill Wendlingd53b5de2012-04-03 08:46:13 +000069 m_const_result ()
Sean Callanan1a8d4092010-08-27 01:01:44 +000070{
Sean Callananc7b65062011-11-07 23:35:40 +000071 switch (m_language)
72 {
73 case lldb::eLanguageTypeC_plus_plus:
74 m_allow_cxx = true;
75 break;
76 case lldb::eLanguageTypeObjC:
77 m_allow_objc = true;
78 break;
79 case lldb::eLanguageTypeObjC_plus_plus:
80 default:
81 m_allow_cxx = true;
82 m_allow_objc = true;
83 break;
84 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000085}
86
Sean Callanane71d5532010-08-27 23:31:21 +000087ClangUserExpression::~ClangUserExpression ()
88{
89}
90
Sean Callanan1a8d4092010-08-27 01:01:44 +000091clang::ASTConsumer *
92ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000093{
Sean Callananbccce812011-08-23 21:20:51 +000094 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
95
96 if (!clang_ast_context)
97 return NULL;
98
Sean Callanan2590b9a2011-10-08 00:21:35 +000099 if (!m_result_synthesizer.get())
100 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
Sean Callanan0eed0d42011-12-06 03:41:14 +0000101 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +0000102
103 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000104}
105
Sean Callananfc55f5d2010-09-21 00:44:12 +0000106void
Sean Callanan744756e2011-11-04 02:09:33 +0000107ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000108{
Sean Callanan70385082012-12-01 00:08:33 +0000109 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
110
111 if (log)
112 log->Printf("ClangUserExpression::ScanContext()");
113
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000115
Sean Callananc7b65062011-11-07 23:35:40 +0000116 if (!(m_allow_cxx || m_allow_objc))
Sean Callanan70385082012-12-01 00:08:33 +0000117 {
118 if (log)
119 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
Sean Callananc7b65062011-11-07 23:35:40 +0000120 return;
Sean Callanan70385082012-12-01 00:08:33 +0000121 }
Sean Callananc7b65062011-11-07 23:35:40 +0000122
Greg Claytonc14ee322011-09-22 04:58:26 +0000123 StackFrame *frame = exe_ctx.GetFramePtr();
124 if (frame == NULL)
Sean Callanan70385082012-12-01 00:08:33 +0000125 {
126 if (log)
127 log->Printf(" [CUE::SC] Null stack frame");
Sean Callananfc55f5d2010-09-21 00:44:12 +0000128 return;
Sean Callanan70385082012-12-01 00:08:33 +0000129 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000130
Sean Callanan5dd6c3d2012-07-13 21:20:29 +0000131 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
Sean Callanan3670ba52010-12-01 21:35:54 +0000132
Sean Callanan72e49402011-08-05 23:43:37 +0000133 if (!sym_ctx.function)
Sean Callanan70385082012-12-01 00:08:33 +0000134 {
135 if (log)
136 log->Printf(" [CUE::SC] Null function");
Sean Callanan72e49402011-08-05 23:43:37 +0000137 return;
Sean Callanan70385082012-12-01 00:08:33 +0000138 }
Sean Callanan72e49402011-08-05 23:43:37 +0000139
Greg Clayton685c88c2012-07-14 00:53:55 +0000140 // Find the block that defines the function represented by "sym_ctx"
141 Block *function_block = sym_ctx.GetFunctionBlock();
Sean Callanan72e49402011-08-05 23:43:37 +0000142
Greg Clayton685c88c2012-07-14 00:53:55 +0000143 if (!function_block)
Sean Callanan70385082012-12-01 00:08:33 +0000144 {
145 if (log)
146 log->Printf(" [CUE::SC] Null function block");
Greg Clayton685c88c2012-07-14 00:53:55 +0000147 return;
Sean Callanan70385082012-12-01 00:08:33 +0000148 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000149
150 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
151
Sean Callanan72e49402011-08-05 23:43:37 +0000152 if (!decl_context)
Sean Callanan70385082012-12-01 00:08:33 +0000153 {
154 if (log)
155 log->Printf(" [CUE::SC] Null decl context");
Sean Callanan72e49402011-08-05 23:43:37 +0000156 return;
Sean Callanan70385082012-12-01 00:08:33 +0000157 }
158
Sean Callanan72e49402011-08-05 23:43:37 +0000159 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000160 {
Sean Callananc7b65062011-11-07 23:35:40 +0000161 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000162 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000163 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000164 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000165 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000166
167 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
168
Greg Clayton685c88c2012-07-14 00:53:55 +0000169 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000170 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000171 err.SetErrorString(thisErrorString);
172 return;
173 }
174
Greg Clayton685c88c2012-07-14 00:53:55 +0000175 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
Sean Callanand5cc1322011-12-13 01:42:04 +0000176
Greg Clayton685c88c2012-07-14 00:53:55 +0000177 if (!this_var_sp ||
178 !this_var_sp->IsInScope(frame) ||
179 !this_var_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000180 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000181 err.SetErrorString(thisErrorString);
182 return;
183 }
Sean Callanan744756e2011-11-04 02:09:33 +0000184 }
185
Sean Callanan72e49402011-08-05 23:43:37 +0000186 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000187 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000188 }
189 }
Sean Callanan72e49402011-08-05 23:43:37 +0000190 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000191 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000192 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000193 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000194 if (m_enforce_valid_object)
Sean Callanan744756e2011-11-04 02:09:33 +0000195 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000196 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
Sean Callanand5cc1322011-12-13 01:42:04 +0000197
198 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
199
Greg Clayton685c88c2012-07-14 00:53:55 +0000200 if (!variable_list_sp)
Sean Callanand5cc1322011-12-13 01:42:04 +0000201 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000202 err.SetErrorString(selfErrorString);
203 return;
204 }
205
Greg Clayton685c88c2012-07-14 00:53:55 +0000206 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
Sean Callanand5cc1322011-12-13 01:42:04 +0000207
Greg Clayton685c88c2012-07-14 00:53:55 +0000208 if (!self_variable_sp ||
209 !self_variable_sp->IsInScope(frame) ||
210 !self_variable_sp->LocationIsValidForFrame (frame))
Sean Callanand5cc1322011-12-13 01:42:04 +0000211 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000212 err.SetErrorString(selfErrorString);
213 return;
214 }
Sean Callanan744756e2011-11-04 02:09:33 +0000215 }
216
Sean Callanan72e49402011-08-05 23:43:37 +0000217 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000218 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000219
220 if (!method_decl->isInstanceMethod())
221 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000222 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000223 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000224 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context))
225 {
226 // We might also have a function that said in the debug information that it captured an
227 // object pointer. The best way to deal with getting to the ivars at present it by pretending
228 // that this is a method of a class in whatever runtime the debug info says the object pointer
229 // belongs to. Do that here.
230
231 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), (uintptr_t) function_decl);
232 if (metadata && metadata->HasObjectPtr())
233 {
234 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
235 if (language == lldb::eLanguageTypeC_plus_plus)
236 {
Sean Callanana2868d42013-01-19 01:49:02 +0000237 if (m_enforce_valid_object)
238 {
239 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
240
241 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";
242
243 if (!variable_list_sp)
244 {
245 err.SetErrorString(thisErrorString);
246 return;
247 }
248
249 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
250
251 if (!this_var_sp ||
252 !this_var_sp->IsInScope(frame) ||
253 !this_var_sp->LocationIsValidForFrame (frame))
254 {
255 err.SetErrorString(thisErrorString);
256 return;
257 }
258 }
259
Jim Ingham5fdeed42012-10-30 23:35:54 +0000260 m_cplusplus = true;
261 m_needs_object_ptr = true;
262 }
263 else if (language == lldb::eLanguageTypeObjC)
264 {
Sean Callanana2868d42013-01-19 01:49:02 +0000265 if (m_enforce_valid_object)
266 {
267 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
268
269 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";
270
271 if (!variable_list_sp)
272 {
273 err.SetErrorString(selfErrorString);
274 return;
275 }
276
277 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
278
279 if (!self_variable_sp ||
280 !self_variable_sp->IsInScope(frame) ||
281 !self_variable_sp->LocationIsValidForFrame (frame))
282 {
283 err.SetErrorString(selfErrorString);
284 return;
285 }
286
287 Type *self_type = self_variable_sp->GetType();
288
289 if (!self_type)
290 {
291 err.SetErrorString(selfErrorString);
292 return;
293 }
294
295 lldb::clang_type_t self_opaque_type = self_type->GetClangForwardType();
296
297 if (!self_opaque_type)
298 {
299 err.SetErrorString(selfErrorString);
300 return;
301 }
302
303 clang::QualType self_qual_type = clang::QualType::getFromOpaquePtr(self_opaque_type);
304
305 if (self_qual_type->isObjCClassType())
306 {
307 return;
308 }
309 else if (self_qual_type->isObjCObjectPointerType())
310 {
311 m_objectivec = true;
312 m_needs_object_ptr = true;
313 }
314 else
315 {
316 err.SetErrorString(selfErrorString);
317 return;
318 }
319 }
320 else
321 {
322 m_objectivec = true;
323 m_needs_object_ptr = true;
324 }
Jim Ingham5fdeed42012-10-30 23:35:54 +0000325 }
326 }
327 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000328}
329
Sean Callanancf5498f2010-10-22 23:25:16 +0000330// This is a really nasty hack, meant to fix Objective-C expressions of the form
331// (int)[myArray count]. Right now, because the type information for count is
332// not available, [myArray count] returns id, which can't be directly cast to
333// int without causing a clang error.
334static void
335ApplyObjcCastHack(std::string &expr)
336{
337#define OBJC_CAST_HACK_FROM "(int)["
338#define OBJC_CAST_HACK_TO "(int)(long long)["
339
340 size_t from_offset;
341
342 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
343 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
344
345#undef OBJC_CAST_HACK_TO
346#undef OBJC_CAST_HACK_FROM
347}
348
Sean Callanan64186e72010-10-24 20:45:49 +0000349// Another hack, meant to allow use of unichar despite it not being available in
350// the type information. Although we could special-case it in type lookup,
351// hopefully we'll figure out a way to #include the same environment as is
352// present in the original source file rather than try to hack specific type
353// definitions in as needed.
354static void
355ApplyUnicharHack(std::string &expr)
356{
357#define UNICHAR_HACK_FROM "unichar"
358#define UNICHAR_HACK_TO "unsigned short"
359
360 size_t from_offset;
361
362 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
363 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
364
365#undef UNICHAR_HACK_TO
366#undef UNICHAR_HACK_FROM
367}
368
Sean Callanancf5498f2010-10-22 23:25:16 +0000369bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000370ClangUserExpression::Parse (Stream &error_stream,
371 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000372 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000373 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000374{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000375 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000376
Sean Callanan744756e2011-11-04 02:09:33 +0000377 Error err;
Sean Callanan933693b2012-02-10 01:22:05 +0000378
379 InstallContext(exe_ctx);
Sean Callanan744756e2011-11-04 02:09:33 +0000380
381 ScanContext(exe_ctx, err);
382
383 if (!err.Success())
384 {
385 error_stream.Printf("warning: %s\n", err.AsCString());
386 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000387
388 StreamString m_transformed_stream;
389
390 ////////////////////////////////////
391 // Generate the expression
392 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000393
394 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000395 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000396
Sean Callanan9bc83842011-09-26 18:45:31 +0000397 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
398
399 lldb::LanguageType lang_type;
400
Sean Callananfc55f5d2010-09-21 00:44:12 +0000401 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000402 lang_type = lldb::eLanguageTypeC_plus_plus;
403 else if(m_objectivec)
404 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000405 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000406 lang_type = lldb::eLanguageTypeC;
407
Sean Callanand5c17ed2011-11-15 02:11:17 +0000408 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000409 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000410 error_stream.PutCString ("error: couldn't construct expression body");
411 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000412 }
413
Sean Callananfc55f5d2010-09-21 00:44:12 +0000414 if (log)
415 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
416
Sean Callanan1a8d4092010-08-27 01:01:44 +0000417 ////////////////////////////////////
418 // Set up the target and compiler
419 //
420
Greg Claytonc14ee322011-09-22 04:58:26 +0000421 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000422
423 if (!target)
424 {
425 error_stream.PutCString ("error: invalid target\n");
426 return false;
427 }
428
Sean Callanan1a8d4092010-08-27 01:01:44 +0000429 //////////////////////////
430 // Parse the expression
431 //
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000432
Sean Callanan1ee44b72011-10-29 01:58:46 +0000433 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000434
Sean Callananb9951192011-08-01 18:18:33 +0000435 if (!m_expr_decl_map->WillParse(exe_ctx))
436 {
437 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
438 return false;
439 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000440
Greg Claytonc14ee322011-09-22 04:58:26 +0000441 Process *process = exe_ctx.GetProcessPtr();
Sean Callananaa719af2012-02-08 18:43:35 +0000442 ExecutionContextScope *exe_scope = process;
443
444 if (!exe_scope)
445 exe_scope = exe_ctx.GetTargetPtr();
446
447 ClangExpressionParser parser(exe_scope, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000448
449 unsigned num_errors = parser.Parse (error_stream);
450
451 if (num_errors)
452 {
453 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000454
455 m_expr_decl_map->DidParse();
456
Sean Callanan1a8d4092010-08-27 01:01:44 +0000457 return false;
458 }
459
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000460 //////////////////////////////////////////////////////////////////////////////////////////
461 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000462 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000463
Greg Claytonc14ee322011-09-22 04:58:26 +0000464 if (execution_policy != eExecutionPolicyNever && process)
465 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000466
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000467 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
468 m_jit_start_addr,
469 m_jit_end_addr,
470 exe_ctx,
471 m_data_allocator.get(),
472 m_evaluated_statically,
473 m_const_result,
474 execution_policy);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000475
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000476 if (log && m_data_allocator.get())
Sean Callanan79763a42011-05-23 21:40:23 +0000477 {
478 StreamString dump_string;
479 m_data_allocator->Dump(dump_string);
480
481 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
482 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000483
Sean Callanan1a8d4092010-08-27 01:01:44 +0000484 if (jit_error.Success())
485 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000486 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000487 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000488 return true;
489 }
490 else
491 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000492 const char *error_cstr = jit_error.AsCString();
493 if (error_cstr && error_cstr[0])
494 error_stream.Printf ("error: %s\n", error_cstr);
495 else
Jason Molendafd54b362011-09-20 21:44:10 +0000496 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000497 return false;
498 }
499}
500
501bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000502ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000503 ExecutionContext &exe_ctx,
504 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000505 lldb::addr_t &object_ptr,
506 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000507{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000508 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000509
Sean Callanan933693b2012-02-10 01:22:05 +0000510 lldb::TargetSP target;
511 lldb::ProcessSP process;
512 lldb::StackFrameSP frame;
513
514 if (!LockAndCheckContext(exe_ctx,
515 target,
516 process,
517 frame))
518 {
519 error_stream.Printf("The context has changed before we could JIT the expression!");
520 return false;
521 }
522
Greg Clayton22a939a2011-01-19 23:00:49 +0000523 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000524 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000525 Error materialize_error;
526
Sean Callanan17827832010-12-13 22:46:15 +0000527 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000528 {
Sean Callanan17827832010-12-13 22:46:15 +0000529 ConstString object_name;
530
531 if (m_cplusplus)
532 {
533 object_name.SetCString("this");
534 }
535 else if (m_objectivec)
536 {
537 object_name.SetCString("self");
538 }
539 else
540 {
541 error_stream.Printf("Need object pointer but don't know the language\n");
542 return false;
543 }
544
Sean Callanan933693b2012-02-10 01:22:05 +0000545 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, materialize_error)))
Sean Callanan17827832010-12-13 22:46:15 +0000546 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000547 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
548 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000549 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000550
551 if (m_objectivec)
552 {
553 ConstString cmd_name("_cmd");
554
Sean Callanan933693b2012-02-10 01:22:05 +0000555 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, materialize_error, true)))
Sean Callanan9d48e802010-12-14 00:42:36 +0000556 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000557 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
558 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000559 }
560 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000561 }
562
Sean Callanan933693b2012-02-10 01:22:05 +0000563 if (!m_expr_decl_map->Materialize(struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000564 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000565 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000566 return false;
567 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000568
569#if 0
570 // jingham: look here
571 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000572 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 +0000573#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000574
575 if (log)
576 {
Sean Callanana162eba2010-12-07 22:55:01 +0000577 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000578
Daniel Malead01b2952012-11-29 21:49:15 +0000579 log->Printf(" Function address : 0x%" PRIx64, (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000580
581 if (m_needs_object_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +0000582 log->Printf(" Object pointer : 0x%" PRIx64, (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000583
Daniel Malead01b2952012-11-29 21:49:15 +0000584 log->Printf(" Structure address : 0x%" PRIx64, (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000585
586 StreamString args;
587
588 Error dump_error;
589
Sean Callanan9e6ed532010-09-13 21:34:21 +0000590 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000591 {
Sean Callanan933693b2012-02-10 01:22:05 +0000592 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000593 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000594 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000595 }
596 else
597 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000598 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000599 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000600 }
601 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000602 }
603 return true;
604}
605
606ThreadPlan *
607ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000608 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000609{
610 lldb::addr_t struct_address;
611
Johnny Chen44805302011-07-19 19:48:13 +0000612 lldb::addr_t object_ptr = 0;
613 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000614
Sean Callanan9d48e802010-12-14 00:42:36 +0000615 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000616
Jim Inghamf48169b2010-11-30 02:22:11 +0000617 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
618 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000619 // forcing unwind_on_error to be true here, in practical terms that can't happen.
620
Jim Ingham184e9812013-01-15 02:47:48 +0000621 const bool stop_others = true;
622 const bool unwind_on_error = true;
623 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000624 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000625 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000626 struct_address,
627 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000628 stop_others,
629 unwind_on_error,
630 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000631 (m_needs_object_ptr ? &object_ptr : NULL),
632 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000633}
634
635bool
636ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
637 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000638 lldb::ClangExpressionVariableSP &result,
639 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000640{
641 Error expr_error;
642
Sean Callananc673a6e2010-12-07 10:00:20 +0000643 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
644
645 if (log)
646 {
Sean Callanana162eba2010-12-07 22:55:01 +0000647 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000648
649 StreamString args;
650
651 Error dump_error;
652
Sean Callanan933693b2012-02-10 01:22:05 +0000653 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callananc673a6e2010-12-07 10:00:20 +0000654 {
655 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
656 }
657 else
658 {
659 log->Printf(" Structure contents:\n%s", args.GetData());
660 }
661 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000662
663 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
664
Sean Callananc673a6e2010-12-07 10:00:20 +0000665
Sean Callanan933693b2012-02-10 01:22:05 +0000666 if (!m_expr_decl_map->Dematerialize(result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000667 {
668 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
669 return false;
670 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000671
Johnny Chenb49440f2012-01-06 00:35:38 +0000672 if (result)
673 result->TransferAddress();
674
Jim Ingham36f3b362010-10-14 23:45:03 +0000675 return true;
676}
677
Greg Claytone0d378b2011-03-24 21:19:54 +0000678ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000679ClangUserExpression::Execute (Stream &error_stream,
680 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000681 bool unwind_on_error,
682 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000683 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000684 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000685 bool run_others,
686 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000687{
Jim Inghamb086ff72011-01-18 22:20:08 +0000688 // The expression log is quite verbose, and if you're just tracking the execution of the
689 // expression, it's quite convenient to have these logs come out with the STEP log as well.
690 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000691
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000692 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000693 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000694 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000695
Johnny Chen44805302011-07-19 19:48:13 +0000696 lldb::addr_t object_ptr = 0;
697 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000698
Johnny Chen8115c6d2012-08-18 04:24:00 +0000699 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
700 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000701 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000702 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000703 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000704
Jim Ingham399f1ca2010-11-05 19:25:48 +0000705 const bool stop_others = true;
706 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000707
Greg Claytone72dfb32012-02-24 01:59:29 +0000708 Address wrapper_address (m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000709 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000710 wrapper_address,
711 struct_address,
712 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000713 unwind_on_error,
714 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000715 (m_needs_object_ptr ? &object_ptr : NULL),
716 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
717 shared_ptr_to_me));
718
Sean Callanan9a028512012-08-09 00:50:26 +0000719 if (!call_plan_sp || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000720 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000721
722 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham95afbf52012-12-07 19:04:31 +0000723
Sean Callananc673a6e2010-12-07 10:00:20 +0000724 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000725 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000726
Jim Ingham0faa43f2011-11-08 03:00:11 +0000727 if (exe_ctx.GetProcessPtr())
728 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
729
Greg Claytonc14ee322011-09-22 04:58:26 +0000730 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
731 call_plan_sp,
732 stop_others,
733 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000734 unwind_on_error,
735 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000736 timeout_usec,
Greg Claytonc14ee322011-09-22 04:58:26 +0000737 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000738
Jim Ingham0faa43f2011-11-08 03:00:11 +0000739 if (exe_ctx.GetProcessPtr())
740 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
741
Sean Callananc673a6e2010-12-07 10:00:20 +0000742 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000743 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000744
Jim Ingham184e9812013-01-15 02:47:48 +0000745 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000746 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000747 const char *error_desc = NULL;
748
749 if (call_plan_sp)
750 {
751 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
752 if (real_stop_info_sp)
753 error_desc = real_stop_info_sp->GetDescription();
754 }
755 if (error_desc)
756 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000757 else
Jason Molendafd54b362011-09-20 21:44:10 +0000758 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000759
Jim Ingham184e9812013-01-15 02:47:48 +0000760 if ((execution_result == eExecutionInterrupted && unwind_on_error)
761 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
Jim Ingham160f78c2011-05-17 01:10:11 +0000762 error_stream.Printf ("\nThe process has been returned to the state before execution.");
763 else
764 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000765
766 return execution_result;
767 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000768 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000769 {
770 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
771 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000772 }
773
Sean Callanane359d9b2011-05-09 22:04:36 +0000774 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000775 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000776 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000777 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000778 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000779 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000780 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000781 }
782 else
783 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000784 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000785 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000786 }
787}
788
Greg Claytone0d378b2011-03-24 21:19:54 +0000789ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000790ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
791 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000792 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000793 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000794 bool unwind_on_error,
795 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000796 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000797 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000798 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000799 bool run_others,
800 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000801{
Jim Ingham41c75912011-08-09 00:00:49 +0000802 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000803 return EvaluateWithError (exe_ctx,
804 execution_policy,
805 language,
806 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000807 unwind_on_error,
808 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000809 expr_cstr,
810 expr_prefix,
811 result_valobj_sp,
812 error,
813 run_others,
814 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000815}
816
817ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000818ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
819 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000820 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000821 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000822 bool unwind_on_error,
823 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000824 const char *expr_cstr,
825 const char *expr_prefix,
826 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000827 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000828 bool run_others,
829 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000830{
Jim Inghamb086ff72011-01-18 22:20:08 +0000831 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000832
Greg Claytone0d378b2011-03-24 21:19:54 +0000833 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000834
Greg Claytonc14ee322011-09-22 04:58:26 +0000835 Process *process = exe_ctx.GetProcessPtr();
836
837 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000838 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000839 if (execution_policy == eExecutionPolicyAlways)
840 {
841 if (log)
842 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000843
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000844 error.SetErrorString ("expression needed to run but couldn't");
845
846 return execution_results;
847 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000848 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000849
Greg Claytonc14ee322011-09-22 04:58:26 +0000850 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000851 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000852
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000853 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000854
Greg Clayton0184f012010-10-05 00:31:29 +0000855 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000856
Sean Callanana162eba2010-12-07 22:55:01 +0000857 if (log)
858 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
859
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000860 const bool keep_expression_in_memory = true;
861
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000862 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000863 {
864 if (error_stream.GetString().empty())
865 error.SetErrorString ("expression failed to parse, unknown error");
866 else
867 error.SetErrorString (error_stream.GetString().c_str());
868 }
869 else
870 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000871 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000872
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000873 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000874 {
Sean Callanana162eba2010-12-07 22:55:01 +0000875 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000876 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000877
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000878 if (user_expression_sp->m_const_result)
879 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
880 else
881 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
882
Jim Ingham41c75912011-08-09 00:00:49 +0000883 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000884 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000885 else if (execution_policy == eExecutionPolicyNever)
886 {
887 if (log)
888 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
889
890 if (error_stream.GetString().empty())
891 error.SetErrorString ("expression needed to run but couldn't");
892 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000893 else
894 {
895 error_stream.GetString().clear();
896
897 if (log)
898 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
899
900 execution_results = user_expression_sp->Execute (error_stream,
901 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000902 unwind_on_error,
903 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000904 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000905 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000906 run_others,
907 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000908
Greg Claytone0d378b2011-03-24 21:19:54 +0000909 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000910 {
Sean Callanana162eba2010-12-07 22:55:01 +0000911 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000912 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
913
914 if (error_stream.GetString().empty())
915 error.SetErrorString ("expression failed to execute, unknown error");
916 else
917 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000918 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000919 else
Greg Clayton0184f012010-10-05 00:31:29 +0000920 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000921 if (expr_result)
922 {
923 result_valobj_sp = expr_result->GetValueObject();
924
925 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000926 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000927 }
928 else
929 {
930 if (log)
931 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
932
Sean Callananbccce812011-08-23 21:20:51 +0000933 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000934 }
Greg Clayton0184f012010-10-05 00:31:29 +0000935 }
936 }
937 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000938
Greg Claytonb71f3842010-10-05 03:13:51 +0000939 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000940 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000941
Jim Inghamf48169b2010-11-30 02:22:11 +0000942 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000943}