blob: 98b5c63efd240fbe9edc2cfb0569347c149ffb01 [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 Callanan8dfb68e2013-03-19 00:10:07 +0000463
464 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000465 m_jit_end_addr,
466 exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000467 m_evaluated_statically,
468 m_const_result,
469 execution_policy);
Sean Callanane3aef1d2011-10-12 22:20:02 +0000470
Sean Callanan1a8d4092010-08-27 01:01:44 +0000471 if (jit_error.Success())
472 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000473 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000474 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000475 return true;
476 }
477 else
478 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000479 const char *error_cstr = jit_error.AsCString();
480 if (error_cstr && error_cstr[0])
481 error_stream.Printf ("error: %s\n", error_cstr);
482 else
Jason Molendafd54b362011-09-20 21:44:10 +0000483 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000484 return false;
485 }
486}
487
488bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000489ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000490 ExecutionContext &exe_ctx,
491 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000492 lldb::addr_t &object_ptr,
493 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000494{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000495 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000496
Sean Callanan933693b2012-02-10 01:22:05 +0000497 lldb::TargetSP target;
498 lldb::ProcessSP process;
499 lldb::StackFrameSP frame;
500
501 if (!LockAndCheckContext(exe_ctx,
502 target,
503 process,
504 frame))
505 {
506 error_stream.Printf("The context has changed before we could JIT the expression!");
507 return false;
508 }
509
Greg Clayton22a939a2011-01-19 23:00:49 +0000510 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000511 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000512 Error materialize_error;
513
Sean Callanan17827832010-12-13 22:46:15 +0000514 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000515 {
Sean Callanan17827832010-12-13 22:46:15 +0000516 ConstString object_name;
517
518 if (m_cplusplus)
519 {
520 object_name.SetCString("this");
521 }
522 else if (m_objectivec)
523 {
524 object_name.SetCString("self");
525 }
526 else
527 {
528 error_stream.Printf("Need object pointer but don't know the language\n");
529 return false;
530 }
531
Sean Callanan933693b2012-02-10 01:22:05 +0000532 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, materialize_error)))
Sean Callanan17827832010-12-13 22:46:15 +0000533 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000534 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
535 object_ptr = 0;
Sean Callanan17827832010-12-13 22:46:15 +0000536 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000537
538 if (m_objectivec)
539 {
540 ConstString cmd_name("_cmd");
541
Sean Callanan933693b2012-02-10 01:22:05 +0000542 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, materialize_error, true)))
Sean Callanan9d48e802010-12-14 00:42:36 +0000543 {
Sean Callanand5cc1322011-12-13 01:42:04 +0000544 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
545 cmd_ptr = 0;
Sean Callanan9d48e802010-12-14 00:42:36 +0000546 }
547 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000548 }
549
Sean Callanan933693b2012-02-10 01:22:05 +0000550 if (!m_expr_decl_map->Materialize(struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000551 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000552 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000553 return false;
554 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000555
556#if 0
557 // jingham: look here
558 StreamFile logfile ("/tmp/exprs.txt", "a");
Daniel Malead01b2952012-11-29 21:49:15 +0000559 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 +0000560#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000561
562 if (log)
563 {
Sean Callanana162eba2010-12-07 22:55:01 +0000564 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000565
Daniel Malead01b2952012-11-29 21:49:15 +0000566 log->Printf(" Function address : 0x%" PRIx64, (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000567
568 if (m_needs_object_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +0000569 log->Printf(" Object pointer : 0x%" PRIx64, (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000570
Daniel Malead01b2952012-11-29 21:49:15 +0000571 log->Printf(" Structure address : 0x%" PRIx64, (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000572
573 StreamString args;
574
575 Error dump_error;
576
Sean Callanan9e6ed532010-09-13 21:34:21 +0000577 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000578 {
Sean Callanan933693b2012-02-10 01:22:05 +0000579 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000580 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000581 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000582 }
583 else
584 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000585 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000586 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000587 }
588 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000589 }
590 return true;
591}
592
593ThreadPlan *
594ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000595 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000596{
597 lldb::addr_t struct_address;
598
Johnny Chen44805302011-07-19 19:48:13 +0000599 lldb::addr_t object_ptr = 0;
600 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000601
Sean Callanan9d48e802010-12-14 00:42:36 +0000602 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000603
Jim Inghamf48169b2010-11-30 02:22:11 +0000604 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
605 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000606 // forcing unwind_on_error to be true here, in practical terms that can't happen.
607
Jim Ingham184e9812013-01-15 02:47:48 +0000608 const bool stop_others = true;
609 const bool unwind_on_error = true;
610 const bool ignore_breakpoints = false;
Jim Ingham36f3b362010-10-14 23:45:03 +0000611 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000612 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000613 struct_address,
614 error_stream,
Jim Ingham184e9812013-01-15 02:47:48 +0000615 stop_others,
616 unwind_on_error,
617 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000618 (m_needs_object_ptr ? &object_ptr : NULL),
619 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000620}
621
622bool
623ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
624 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000625 lldb::ClangExpressionVariableSP &result,
626 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000627{
628 Error expr_error;
629
Sean Callananc673a6e2010-12-07 10:00:20 +0000630 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
631
632 if (log)
633 {
Sean Callanana162eba2010-12-07 22:55:01 +0000634 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000635
636 StreamString args;
637
638 Error dump_error;
639
Sean Callanan933693b2012-02-10 01:22:05 +0000640 if (!m_expr_decl_map->DumpMaterializedStruct(args, dump_error))
Sean Callananc673a6e2010-12-07 10:00:20 +0000641 {
642 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
643 }
644 else
645 {
646 log->Printf(" Structure contents:\n%s", args.GetData());
647 }
648 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000649
650 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
651
Sean Callananc673a6e2010-12-07 10:00:20 +0000652
Sean Callanan933693b2012-02-10 01:22:05 +0000653 if (!m_expr_decl_map->Dematerialize(result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000654 {
655 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
656 return false;
657 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000658
Johnny Chenb49440f2012-01-06 00:35:38 +0000659 if (result)
660 result->TransferAddress();
661
Jim Ingham36f3b362010-10-14 23:45:03 +0000662 return true;
663}
664
Greg Claytone0d378b2011-03-24 21:19:54 +0000665ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000666ClangUserExpression::Execute (Stream &error_stream,
667 ExecutionContext &exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000668 bool unwind_on_error,
669 bool ignore_breakpoints,
Jim Inghamf48169b2010-11-30 02:22:11 +0000670 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Enrico Granata3372f582012-07-16 23:10:35 +0000671 lldb::ClangExpressionVariableSP &result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000672 bool run_others,
673 uint32_t timeout_usec)
Jim Ingham36f3b362010-10-14 23:45:03 +0000674{
Jim Inghamb086ff72011-01-18 22:20:08 +0000675 // The expression log is quite verbose, and if you're just tracking the execution of the
676 // expression, it's quite convenient to have these logs come out with the STEP log as well.
677 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000678
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000679 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000680 {
Jim Ingham28eb5712012-10-12 17:34:26 +0000681 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
Jim Ingham36f3b362010-10-14 23:45:03 +0000682
Johnny Chen44805302011-07-19 19:48:13 +0000683 lldb::addr_t object_ptr = 0;
684 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000685
Johnny Chen8115c6d2012-08-18 04:24:00 +0000686 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
687 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000688 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000689 return eExecutionSetupError;
Johnny Chen2c90e992012-08-18 04:14:54 +0000690 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000691
Jim Ingham399f1ca2010-11-05 19:25:48 +0000692 const bool stop_others = true;
Jim Ingham0161b492013-02-09 01:29:05 +0000693 const bool try_all_threads = run_others;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000694
Greg Claytone72dfb32012-02-24 01:59:29 +0000695 Address wrapper_address (m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000696 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000697 wrapper_address,
698 struct_address,
699 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000700 unwind_on_error,
701 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000702 (m_needs_object_ptr ? &object_ptr : NULL),
703 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
704 shared_ptr_to_me));
705
Sean Callanan9a028512012-08-09 00:50:26 +0000706 if (!call_plan_sp || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000707 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000708
709 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham95afbf52012-12-07 19:04:31 +0000710
Sean Callananc673a6e2010-12-07 10:00:20 +0000711 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000712 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000713
Jim Ingham0faa43f2011-11-08 03:00:11 +0000714 if (exe_ctx.GetProcessPtr())
715 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
716
Greg Claytonc14ee322011-09-22 04:58:26 +0000717 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
718 call_plan_sp,
719 stop_others,
720 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000721 unwind_on_error,
722 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000723 timeout_usec,
Greg Claytonc14ee322011-09-22 04:58:26 +0000724 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000725
Jim Ingham0faa43f2011-11-08 03:00:11 +0000726 if (exe_ctx.GetProcessPtr())
727 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
728
Sean Callananc673a6e2010-12-07 10:00:20 +0000729 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000730 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000731
Jim Ingham184e9812013-01-15 02:47:48 +0000732 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000733 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000734 const char *error_desc = NULL;
735
736 if (call_plan_sp)
737 {
738 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
739 if (real_stop_info_sp)
740 error_desc = real_stop_info_sp->GetDescription();
741 }
742 if (error_desc)
743 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000744 else
Jason Molendafd54b362011-09-20 21:44:10 +0000745 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000746
Jim Ingham184e9812013-01-15 02:47:48 +0000747 if ((execution_result == eExecutionInterrupted && unwind_on_error)
748 || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints))
Jim Ingham93208b82013-01-31 21:46:01 +0000749 error_stream.Printf ("\nThe process has been returned to the state before expression evaluation.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000750 else
Jim Ingham93208b82013-01-31 21:46:01 +0000751 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 +0000752
753 return execution_result;
754 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000755 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000756 {
757 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
758 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000759 }
760
Sean Callanane359d9b2011-05-09 22:04:36 +0000761 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000762 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000763 else
Johnny Chen8115c6d2012-08-18 04:24:00 +0000764 {
Johnny Chen2c90e992012-08-18 04:14:54 +0000765 error_stream.Printf("Errored out in %s: Couldn't FinalizeJITExpression", __FUNCTION__);
Greg Claytone0d378b2011-03-24 21:19:54 +0000766 return eExecutionSetupError;
Johnny Chen8115c6d2012-08-18 04:24:00 +0000767 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000768 }
769 else
770 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000771 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000772 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000773 }
774}
775
Greg Claytone0d378b2011-03-24 21:19:54 +0000776ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000777ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
778 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000779 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000780 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000781 bool unwind_on_error,
782 bool ignore_breakpoints,
Sean Callanan322f5292010-10-29 00:29:03 +0000783 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000784 const char *expr_prefix,
Enrico Granata3372f582012-07-16 23:10:35 +0000785 lldb::ValueObjectSP &result_valobj_sp,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000786 bool run_others,
787 uint32_t timeout_usec)
Greg Clayton0184f012010-10-05 00:31:29 +0000788{
Jim Ingham41c75912011-08-09 00:00:49 +0000789 Error error;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000790 return EvaluateWithError (exe_ctx,
791 execution_policy,
792 language,
793 desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000794 unwind_on_error,
795 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000796 expr_cstr,
797 expr_prefix,
798 result_valobj_sp,
799 error,
800 run_others,
801 timeout_usec);
Jim Ingham41c75912011-08-09 00:00:49 +0000802}
803
804ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000805ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
806 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000807 lldb::LanguageType language,
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000808 ResultType desired_type,
Jim Ingham184e9812013-01-15 02:47:48 +0000809 bool unwind_on_error,
810 bool ignore_breakpoints,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000811 const char *expr_cstr,
812 const char *expr_prefix,
813 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000814 Error &error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000815 bool run_others,
816 uint32_t timeout_usec)
Jim Ingham41c75912011-08-09 00:00:49 +0000817{
Jim Inghamb086ff72011-01-18 22:20:08 +0000818 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000819
Greg Claytone0d378b2011-03-24 21:19:54 +0000820 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000821
Greg Claytonc14ee322011-09-22 04:58:26 +0000822 Process *process = exe_ctx.GetProcessPtr();
823
824 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000825 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000826 if (execution_policy == eExecutionPolicyAlways)
827 {
828 if (log)
829 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000830
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000831 error.SetErrorString ("expression needed to run but couldn't");
832
833 return execution_results;
834 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000835 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000836
Greg Claytonc14ee322011-09-22 04:58:26 +0000837 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000838 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000839
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000840 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
Jim Inghamf48169b2010-11-30 02:22:11 +0000841
Greg Clayton0184f012010-10-05 00:31:29 +0000842 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000843
Sean Callanana162eba2010-12-07 22:55:01 +0000844 if (log)
845 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
846
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000847 const bool keep_expression_in_memory = true;
848
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000849 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000850 {
851 if (error_stream.GetString().empty())
852 error.SetErrorString ("expression failed to parse, unknown error");
853 else
854 error.SetErrorString (error_stream.GetString().c_str());
855 }
856 else
857 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000858 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000859
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000860 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000861 {
Sean Callanana162eba2010-12-07 22:55:01 +0000862 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000863 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000864
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000865 if (user_expression_sp->m_const_result)
866 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
867 else
868 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
869
Jim Ingham41c75912011-08-09 00:00:49 +0000870 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000871 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000872 else if (execution_policy == eExecutionPolicyNever)
873 {
874 if (log)
875 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
876
877 if (error_stream.GetString().empty())
878 error.SetErrorString ("expression needed to run but couldn't");
879 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000880 else
881 {
882 error_stream.GetString().clear();
883
884 if (log)
885 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
886
887 execution_results = user_expression_sp->Execute (error_stream,
888 exe_ctx,
Jim Ingham184e9812013-01-15 02:47:48 +0000889 unwind_on_error,
890 ignore_breakpoints,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000891 user_expression_sp,
Enrico Granata3372f582012-07-16 23:10:35 +0000892 expr_result,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000893 run_others,
894 timeout_usec);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000895
Greg Claytone0d378b2011-03-24 21:19:54 +0000896 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000897 {
Sean Callanana162eba2010-12-07 22:55:01 +0000898 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000899 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
900
901 if (error_stream.GetString().empty())
902 error.SetErrorString ("expression failed to execute, unknown error");
903 else
904 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000905 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000906 else
Greg Clayton0184f012010-10-05 00:31:29 +0000907 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000908 if (expr_result)
909 {
910 result_valobj_sp = expr_result->GetValueObject();
911
912 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000913 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000914 }
915 else
916 {
917 if (log)
918 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
919
Sean Callananbccce812011-08-23 21:20:51 +0000920 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000921 }
Greg Clayton0184f012010-10-05 00:31:29 +0000922 }
923 }
924 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000925
Greg Claytonb71f3842010-10-05 03:13:51 +0000926 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000927 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000928
Jim Inghamf48169b2010-11-30 02:22:11 +0000929 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000930}