blob: 8f011c089d9e37ba194b2db0adb25675088368a3 [file] [log] [blame]
Sean Callananc0492742011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan65dafa82010-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 Claytonc71899e2011-01-18 19:36:39 +000023#include "lldb/Core/StreamFile.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000024#include "lldb/Core/StreamString.h"
Greg Claytond1719722010-10-05 03:13:51 +000025#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan05a5a1b2010-12-16 03:17:46 +000026#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000027#include "lldb/Expression/ClangExpressionDeclMap.h"
28#include "lldb/Expression/ClangExpressionParser.h"
29#include "lldb/Expression/ClangFunction.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000030#include "lldb/Expression/ClangUserExpression.h"
Sean Callanande3d27e2011-09-26 18:45:31 +000031#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000032#include "lldb/Host/Host.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000033#include "lldb/Symbol/VariableList.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000034#include "lldb/Target/ExecutionContext.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000035#include "lldb/Target/Process.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000036#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000037#include "lldb/Target/Target.h"
Jim Ingham360f53f2010-11-30 02:22:11 +000038#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000040
Sean Callananc617a4c2011-08-05 23:43:37 +000041#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
Sean Callanan65dafa82010-08-27 01:01:44 +000044using namespace lldb_private;
45
Sean Callanan77e93942010-10-29 00:29:03 +000046ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callanan5b658cc2011-11-07 23:35:40 +000047 const char *expr_prefix,
48 lldb::LanguageType language) :
Greg Claytond0882d02011-01-19 23:00:49 +000049 ClangExpression (),
50 m_expr_text (expr),
51 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callanan5b658cc2011-11-07 23:35:40 +000052 m_language (language),
Greg Claytond0882d02011-01-19 23:00:49 +000053 m_transformed_text (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000054 m_desired_type (NULL, NULL),
Greg Claytond0882d02011-01-19 23:00:49 +000055 m_cplusplus (false),
56 m_objectivec (false),
57 m_needs_object_ptr (false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000058 m_const_object (false),
Sean Callanane6ea5fe2011-11-15 02:11:17 +000059 m_static_method(false),
Daniel Dunbar97c89572011-10-31 22:50:49 +000060 m_target (NULL),
Sean Callanan47dc4572011-09-15 02:13:07 +000061 m_evaluated_statically (false),
Sean Callanan5ed59a72011-12-13 01:42:04 +000062 m_const_result (),
63 m_enforce_valid_object (false)
Sean Callanan65dafa82010-08-27 01:01:44 +000064{
Sean Callanan5b658cc2011-11-07 23:35:40 +000065 switch (m_language)
66 {
67 case lldb::eLanguageTypeC_plus_plus:
68 m_allow_cxx = true;
69 break;
70 case lldb::eLanguageTypeObjC:
71 m_allow_objc = true;
72 break;
73 case lldb::eLanguageTypeObjC_plus_plus:
74 default:
75 m_allow_cxx = true;
76 m_allow_objc = true;
77 break;
78 }
Sean Callanan65dafa82010-08-27 01:01:44 +000079}
80
Sean Callanan830a9032010-08-27 23:31:21 +000081ClangUserExpression::~ClangUserExpression ()
82{
83}
84
Sean Callanan65dafa82010-08-27 01:01:44 +000085clang::ASTConsumer *
86ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan036fa902011-08-24 22:18:12 +000087{
Sean Callanan24312442011-08-23 21:20:51 +000088 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
89
90 if (!clang_ast_context)
91 return NULL;
92
Sean Callanan060d53f2011-10-08 00:21:35 +000093 if (!m_result_synthesizer.get())
94 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
95 m_desired_type,
Sean Callanane1301a62011-12-06 03:41:14 +000096 *m_target));
Sean Callanan060d53f2011-10-08 00:21:35 +000097
98 return m_result_synthesizer.get();
Sean Callanan65dafa82010-08-27 01:01:44 +000099}
100
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000101void
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000102ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000103{
Greg Clayton567e7f32011-09-22 04:58:26 +0000104 m_target = exe_ctx.GetTargetPtr();
Greg Clayton144188b2011-09-12 23:21:58 +0000105
Sean Callanan5b658cc2011-11-07 23:35:40 +0000106 if (!(m_allow_cxx || m_allow_objc))
107 return;
108
Greg Clayton567e7f32011-09-22 04:58:26 +0000109 StackFrame *frame = exe_ctx.GetFramePtr();
110 if (frame == NULL)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000111 return;
112
Greg Clayton567e7f32011-09-22 04:58:26 +0000113 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanane8e55572010-12-01 21:35:54 +0000114
Sean Callananc617a4c2011-08-05 23:43:37 +0000115 if (!sym_ctx.function)
116 return;
117
118 clang::DeclContext *decl_context;
119
120 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
121 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
122 else
123 decl_context = sym_ctx.function->GetClangDeclContext();
124
125 if (!decl_context)
126 return;
Sean Callanan036fa902011-08-24 22:18:12 +0000127
Sean Callananc617a4c2011-08-05 23:43:37 +0000128 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000129 {
Sean Callanan5b658cc2011-11-07 23:35:40 +0000130 if (m_allow_cxx && method_decl->isInstance())
Sean Callanane8e55572010-12-01 21:35:54 +0000131 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000132 if (m_enforce_valid_object)
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000133 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000134 VariableList *vars = frame->GetVariableList(false);
135
136 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
137
138 if (!vars)
139 {
140 err.SetErrorToGenericError();
141 err.SetErrorString(thisErrorString);
142 return;
143 }
144
145 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
146
147 if (!this_var ||
148 !this_var->IsInScope(frame) ||
149 !this_var->LocationIsValidForFrame (frame))
150 {
151 err.SetErrorToGenericError();
152 err.SetErrorString(thisErrorString);
153 return;
154 }
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000155 }
156
Sean Callananc617a4c2011-08-05 23:43:37 +0000157 m_cplusplus = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000158 m_needs_object_ptr = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000159
Sean Callananc617a4c2011-08-05 23:43:37 +0000160 do {
161 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
162
Sean Callanan21f2e192011-12-14 01:13:04 +0000163 const clang::PointerType *this_pointer_type = this_type->getAs<clang::PointerType>();
Sean Callananc617a4c2011-08-05 23:43:37 +0000164
165 if (!this_pointer_type)
166 break;
167
168 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
169 } while (0);
Sean Callanane8e55572010-12-01 21:35:54 +0000170 }
171 }
Sean Callananc617a4c2011-08-05 23:43:37 +0000172 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000173 {
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000174 if (m_allow_objc)
Sean Callanande3d27e2011-09-26 18:45:31 +0000175 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000176 if (m_enforce_valid_object)
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000177 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000178 VariableList *vars = frame->GetVariableList(false);
179
180 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
181
182 if (!vars)
183 {
184 err.SetErrorToGenericError();
185 err.SetErrorString(selfErrorString);
186 return;
187 }
188
189 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
190
191 if (!self_var ||
192 !self_var->IsInScope(frame) ||
193 !self_var->LocationIsValidForFrame (frame))
194 {
195 err.SetErrorToGenericError();
196 err.SetErrorString(selfErrorString);
197 return;
198 }
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000199 }
200
Sean Callananc617a4c2011-08-05 23:43:37 +0000201 m_objectivec = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000202 m_needs_object_ptr = true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000203
204 if (!method_decl->isInstanceMethod())
205 m_static_method = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000206 }
Sean Callanane8e55572010-12-01 21:35:54 +0000207 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000208}
209
Sean Callanan550f2762010-10-22 23:25:16 +0000210// This is a really nasty hack, meant to fix Objective-C expressions of the form
211// (int)[myArray count]. Right now, because the type information for count is
212// not available, [myArray count] returns id, which can't be directly cast to
213// int without causing a clang error.
214static void
215ApplyObjcCastHack(std::string &expr)
216{
217#define OBJC_CAST_HACK_FROM "(int)["
218#define OBJC_CAST_HACK_TO "(int)(long long)["
219
220 size_t from_offset;
221
222 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
223 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
224
225#undef OBJC_CAST_HACK_TO
226#undef OBJC_CAST_HACK_FROM
227}
228
Sean Callanan30892372010-10-24 20:45:49 +0000229// Another hack, meant to allow use of unichar despite it not being available in
230// the type information. Although we could special-case it in type lookup,
231// hopefully we'll figure out a way to #include the same environment as is
232// present in the original source file rather than try to hack specific type
233// definitions in as needed.
234static void
235ApplyUnicharHack(std::string &expr)
236{
237#define UNICHAR_HACK_FROM "unichar"
238#define UNICHAR_HACK_TO "unsigned short"
239
240 size_t from_offset;
241
242 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
243 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
244
245#undef UNICHAR_HACK_TO
246#undef UNICHAR_HACK_FROM
247}
248
Sean Callanan550f2762010-10-22 23:25:16 +0000249bool
Sean Callanana91dd992010-11-19 02:52:21 +0000250ClangUserExpression::Parse (Stream &error_stream,
251 ExecutionContext &exe_ctx,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000252 TypeFromUser desired_type,
Sean Callanan47dc4572011-09-15 02:13:07 +0000253 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000254 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000255{
Greg Claytone005f2c2010-11-06 01:53:30 +0000256 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000257
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000258 Error err;
259
260 ScanContext(exe_ctx, err);
261
262 if (!err.Success())
263 {
264 error_stream.Printf("warning: %s\n", err.AsCString());
265 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000266
267 StreamString m_transformed_stream;
268
269 ////////////////////////////////////
270 // Generate the expression
271 //
Sean Callanan550f2762010-10-22 23:25:16 +0000272
273 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000274 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000275
Sean Callanande3d27e2011-09-26 18:45:31 +0000276 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
277
278 lldb::LanguageType lang_type;
279
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000280 if (m_cplusplus)
Sean Callanande3d27e2011-09-26 18:45:31 +0000281 lang_type = lldb::eLanguageTypeC_plus_plus;
282 else if(m_objectivec)
283 lang_type = lldb::eLanguageTypeObjC;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000284 else
Sean Callanande3d27e2011-09-26 18:45:31 +0000285 lang_type = lldb::eLanguageTypeC;
286
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000287 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000288 {
Sean Callanande3d27e2011-09-26 18:45:31 +0000289 error_stream.PutCString ("error: couldn't construct expression body");
290 return false;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000291 }
292
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000293 if (log)
294 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
295
Sean Callanan65dafa82010-08-27 01:01:44 +0000296 ////////////////////////////////////
297 // Set up the target and compiler
298 //
299
Greg Clayton567e7f32011-09-22 04:58:26 +0000300 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan65dafa82010-08-27 01:01:44 +0000301
302 if (!target)
303 {
304 error_stream.PutCString ("error: invalid target\n");
305 return false;
306 }
307
Sean Callanan65dafa82010-08-27 01:01:44 +0000308 //////////////////////////
309 // Parse the expression
310 //
311
Sean Callanana91dd992010-11-19 02:52:21 +0000312 m_desired_type = desired_type;
313
Sean Callanan73b520f2011-10-29 01:58:46 +0000314 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callananaa301c42010-12-03 01:38:59 +0000315
Sean Callanan166ba102011-08-01 18:18:33 +0000316 if (!m_expr_decl_map->WillParse(exe_ctx))
317 {
318 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
319 return false;
320 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000321
Greg Clayton567e7f32011-09-22 04:58:26 +0000322 Process *process = exe_ctx.GetProcessPtr();
323 ClangExpressionParser parser(process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000324
325 unsigned num_errors = parser.Parse (error_stream);
326
327 if (num_errors)
328 {
329 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000330
331 m_expr_decl_map->DidParse();
332
Sean Callanan65dafa82010-08-27 01:01:44 +0000333 return false;
334 }
335
Sean Callanan47dc4572011-09-15 02:13:07 +0000336 //////////////////////////////////////////////////////////////////////////////////////////
337 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan65dafa82010-08-27 01:01:44 +0000338 //
Sean Callanan65dafa82010-08-27 01:01:44 +0000339
Greg Clayton567e7f32011-09-22 04:58:26 +0000340 if (execution_policy != eExecutionPolicyNever && process)
341 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan65dafa82010-08-27 01:01:44 +0000342
Sean Callanan47dc4572011-09-15 02:13:07 +0000343 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
344 m_jit_start_addr,
345 m_jit_end_addr,
346 exe_ctx,
347 m_data_allocator.get(),
348 m_evaluated_statically,
349 m_const_result,
350 execution_policy);
Sean Callanan65dafa82010-08-27 01:01:44 +0000351
Sean Callanan47dc4572011-09-15 02:13:07 +0000352 if (log && m_data_allocator.get())
Sean Callananc0492742011-05-23 21:40:23 +0000353 {
354 StreamString dump_string;
355 m_data_allocator->Dump(dump_string);
356
357 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
358 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000359
Sean Callanan65dafa82010-08-27 01:01:44 +0000360 if (jit_error.Success())
361 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000362 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
363 m_jit_process_sp = process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000364 return true;
365 }
366 else
367 {
Greg Clayton30581972011-05-17 03:51:29 +0000368 const char *error_cstr = jit_error.AsCString();
369 if (error_cstr && error_cstr[0])
370 error_stream.Printf ("error: %s\n", error_cstr);
371 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000372 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan65dafa82010-08-27 01:01:44 +0000373 return false;
374 }
375}
376
377bool
Jim Inghamd1686902010-10-14 23:45:03 +0000378ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000379 ExecutionContext &exe_ctx,
380 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000381 lldb::addr_t &object_ptr,
382 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000383{
Greg Claytone005f2c2010-11-06 01:53:30 +0000384 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000385
Greg Claytond0882d02011-01-19 23:00:49 +0000386 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000387 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000388 Error materialize_error;
389
Sean Callanan3aa7da52010-12-13 22:46:15 +0000390 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000391 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000392 ConstString object_name;
393
394 if (m_cplusplus)
395 {
396 object_name.SetCString("this");
397 }
398 else if (m_objectivec)
399 {
400 object_name.SetCString("self");
401 }
402 else
403 {
404 error_stream.Printf("Need object pointer but don't know the language\n");
405 return false;
406 }
407
408 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
409 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000410 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", materialize_error.AsCString());
411 object_ptr = 0;
Sean Callanan3aa7da52010-12-13 22:46:15 +0000412 }
Sean Callanan047923c2010-12-14 00:42:36 +0000413
414 if (m_objectivec)
415 {
416 ConstString cmd_name("_cmd");
417
418 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
419 {
Sean Callanan5ed59a72011-12-13 01:42:04 +0000420 error_stream.Printf("warning: couldn't get object pointer (substituting NULL): %s\n", materialize_error.AsCString());
421 cmd_ptr = 0;
Sean Callanan047923c2010-12-14 00:42:36 +0000422 }
423 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000424 }
425
Sean Callananaa301c42010-12-03 01:38:59 +0000426 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000427 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000428 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000429 return false;
430 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000431
432#if 0
433 // jingham: look here
434 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000435 logfile.Printf("0x%16.16llx: 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 Claytonc71899e2011-01-18 19:36:39 +0000436#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000437
438 if (log)
439 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000440 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000441
Greg Claytond0882d02011-01-19 23:00:49 +0000442 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000443
444 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000445 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000446
Sean Callanan33711022010-12-07 10:00:20 +0000447 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000448
449 StreamString args;
450
451 Error dump_error;
452
Sean Callanane8a59a82010-09-13 21:34:21 +0000453 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000454 {
Sean Callananaa301c42010-12-03 01:38:59 +0000455 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000456 {
Sean Callanan33711022010-12-07 10:00:20 +0000457 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000458 }
459 else
460 {
Sean Callanan33711022010-12-07 10:00:20 +0000461 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000462 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000463 }
464 }
Jim Inghamd1686902010-10-14 23:45:03 +0000465 }
466 return true;
467}
468
469ThreadPlan *
470ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000471 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000472{
473 lldb::addr_t struct_address;
474
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000475 lldb::addr_t object_ptr = 0;
476 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000477
Sean Callanan047923c2010-12-14 00:42:36 +0000478 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000479
Jim Ingham360f53f2010-11-30 02:22:11 +0000480 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
481 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000482 // forcing unwind_on_error to be true here, in practical terms that can't happen.
483
Jim Inghamd1686902010-10-14 23:45:03 +0000484 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000485 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000486 struct_address,
487 error_stream,
488 true,
489 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000490 (m_needs_object_ptr ? &object_ptr : NULL),
491 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000492}
493
494bool
495ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
496 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000497 lldb::ClangExpressionVariableSP &result,
498 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000499{
500 Error expr_error;
501
Sean Callanan33711022010-12-07 10:00:20 +0000502 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
503
504 if (log)
505 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000506 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000507
508 StreamString args;
509
510 Error dump_error;
511
512 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
513 {
514 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
515 }
516 else
517 {
518 log->Printf(" Structure contents:\n%s", args.GetData());
519 }
520 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000521
522 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
523
Sean Callanan33711022010-12-07 10:00:20 +0000524
Sean Callanan0ddf8062011-05-09 22:04:36 +0000525 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000526 {
527 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
528 return false;
529 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000530
Jim Inghamd1686902010-10-14 23:45:03 +0000531 return true;
532}
533
Greg Claytonb3448432011-03-24 21:19:54 +0000534ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000535ClangUserExpression::Execute (Stream &error_stream,
536 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000537 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000538 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000539 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000540{
Jim Ingham7812e012011-01-18 22:20:08 +0000541 // The expression log is quite verbose, and if you're just tracking the execution of the
542 // expression, it's quite convenient to have these logs come out with the STEP log as well.
543 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000544
Sean Callanan47dc4572011-09-15 02:13:07 +0000545 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000546 {
547 lldb::addr_t struct_address;
548
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000549 lldb::addr_t object_ptr = 0;
550 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000551
Sean Callanan047923c2010-12-14 00:42:36 +0000552 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000553 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000554
Jim Inghamea9d4262010-11-05 19:25:48 +0000555 const bool stop_others = true;
556 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000557
Greg Claytond0882d02011-01-19 23:00:49 +0000558 Address wrapper_address (NULL, m_jit_start_addr);
Greg Clayton567e7f32011-09-22 04:58:26 +0000559 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan3aa7da52010-12-13 22:46:15 +0000560 wrapper_address,
561 struct_address,
562 stop_others,
563 discard_on_error,
564 (m_needs_object_ptr ? &object_ptr : NULL),
565 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
566 shared_ptr_to_me));
567
Jim Ingham360f53f2010-11-30 02:22:11 +0000568 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000569 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000570
571 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000572
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000573 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000574
Greg Clayton61468e82011-01-19 07:54:15 +0000575 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000576
577 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000578 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000579
Jim Ingham0296fe72011-11-08 03:00:11 +0000580 if (exe_ctx.GetProcessPtr())
581 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
582
Greg Clayton567e7f32011-09-22 04:58:26 +0000583 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
584 call_plan_sp,
585 stop_others,
586 try_all_threads,
587 discard_on_error,
588 single_thread_timeout_usec,
589 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000590
Jim Ingham0296fe72011-11-08 03:00:11 +0000591 if (exe_ctx.GetProcessPtr())
592 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
593
Sean Callanan33711022010-12-07 10:00:20 +0000594 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000595 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000596
Greg Claytonb3448432011-03-24 21:19:54 +0000597 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000598 {
Jim Ingham2370a972011-05-17 01:10:11 +0000599 const char *error_desc = NULL;
600
601 if (call_plan_sp)
602 {
603 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
604 if (real_stop_info_sp)
605 error_desc = real_stop_info_sp->GetDescription();
606 }
607 if (error_desc)
608 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000609 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000610 error_stream.Printf ("Execution was interrupted.");
Jim Ingham2370a972011-05-17 01:10:11 +0000611
612 if (discard_on_error)
613 error_stream.Printf ("\nThe process has been returned to the state before execution.");
614 else
615 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000616
617 return execution_result;
618 }
Greg Claytonb3448432011-03-24 21:19:54 +0000619 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000620 {
621 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
622 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000623 }
624
Sean Callanan0ddf8062011-05-09 22:04:36 +0000625 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000626 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000627 else
Greg Claytonb3448432011-03-24 21:19:54 +0000628 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000629 }
630 else
631 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000632 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytonb3448432011-03-24 21:19:54 +0000633 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000634 }
635}
636
Greg Claytonb3448432011-03-24 21:19:54 +0000637ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000638ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
639 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000640 lldb::LanguageType language,
Jim Inghamea9d4262010-11-05 19:25:48 +0000641 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000642 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000643 const char *expr_prefix,
644 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000645{
Jim Inghamec07c0d2011-08-09 00:00:49 +0000646 Error error;
Sean Callanan5b658cc2011-11-07 23:35:40 +0000647 return EvaluateWithError (exe_ctx, execution_policy, language, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Inghamec07c0d2011-08-09 00:00:49 +0000648}
649
650ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000651ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
652 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000653 lldb::LanguageType language,
Sean Callanan47dc4572011-09-15 02:13:07 +0000654 bool discard_on_error,
655 const char *expr_cstr,
656 const char *expr_prefix,
657 lldb::ValueObjectSP &result_valobj_sp,
658 Error &error)
Jim Inghamec07c0d2011-08-09 00:00:49 +0000659{
Jim Ingham7812e012011-01-18 22:20:08 +0000660 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000661
Greg Claytonb3448432011-03-24 21:19:54 +0000662 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000663
Greg Clayton567e7f32011-09-22 04:58:26 +0000664 Process *process = exe_ctx.GetProcessPtr();
665
666 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +0000667 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000668 if (execution_policy == eExecutionPolicyAlways)
669 {
670 if (log)
671 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Ingham360f53f2010-11-30 02:22:11 +0000672
Sean Callanan47dc4572011-09-15 02:13:07 +0000673 error.SetErrorString ("expression needed to run but couldn't");
674
675 return execution_results;
676 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000677 }
Sean Callananf7649bb2011-09-15 17:43:00 +0000678
Greg Clayton567e7f32011-09-22 04:58:26 +0000679 if (process == NULL || !process->CanJIT())
Sean Callananf7649bb2011-09-15 17:43:00 +0000680 execution_policy = eExecutionPolicyNever;
Greg Clayton0baa3942010-11-04 01:54:29 +0000681
Sean Callanan5b658cc2011-11-07 23:35:40 +0000682 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language));
Jim Ingham360f53f2010-11-30 02:22:11 +0000683
Greg Clayton377e0b42010-10-05 00:31:29 +0000684 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000685
Sean Callanan94d255f2010-12-07 22:55:01 +0000686 if (log)
687 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
688
Sean Callanan47dc4572011-09-15 02:13:07 +0000689 const bool keep_expression_in_memory = true;
690
691 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), execution_policy, keep_expression_in_memory))
Greg Clayton377e0b42010-10-05 00:31:29 +0000692 {
693 if (error_stream.GetString().empty())
694 error.SetErrorString ("expression failed to parse, unknown error");
695 else
696 error.SetErrorString (error_stream.GetString().c_str());
697 }
698 else
699 {
Greg Clayton427f2902010-12-14 02:59:59 +0000700 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000701
Sean Callanan47dc4572011-09-15 02:13:07 +0000702 if (user_expression_sp->EvaluatedStatically())
Greg Clayton377e0b42010-10-05 00:31:29 +0000703 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000704 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000705 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000706
Sean Callanan47dc4572011-09-15 02:13:07 +0000707 if (user_expression_sp->m_const_result)
708 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
709 else
710 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
711
Jim Inghamec07c0d2011-08-09 00:00:49 +0000712 execution_results = eExecutionCompleted;
Greg Clayton377e0b42010-10-05 00:31:29 +0000713 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000714 else if (execution_policy == eExecutionPolicyNever)
715 {
716 if (log)
717 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
718
719 if (error_stream.GetString().empty())
720 error.SetErrorString ("expression needed to run but couldn't");
721 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000722 else
723 {
724 error_stream.GetString().clear();
725
726 if (log)
727 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
728
729 execution_results = user_expression_sp->Execute (error_stream,
730 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000731 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000732 user_expression_sp,
733 expr_result);
734
Greg Claytonb3448432011-03-24 21:19:54 +0000735 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000736 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000737 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000738 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
739
740 if (error_stream.GetString().empty())
741 error.SetErrorString ("expression failed to execute, unknown error");
742 else
743 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000744 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000745 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000746 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000747 if (expr_result)
748 {
749 result_valobj_sp = expr_result->GetValueObject();
750
751 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000752 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000753 }
754 else
755 {
756 if (log)
757 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
758
Sean Callanan24312442011-08-23 21:20:51 +0000759 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000760 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000761 }
762 }
763 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000764
Greg Claytond1719722010-10-05 03:13:51 +0000765 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000766 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000767
Jim Ingham360f53f2010-11-30 02:22:11 +0000768 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000769}