blob: 25eb8c4bc13005de460689f40800839acb0f157f [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),
Daniel Dunbar97c89572011-10-31 22:50:49 +000059 m_target (NULL),
Sean Callanan47dc4572011-09-15 02:13:07 +000060 m_evaluated_statically (false),
Daniel Dunbar97c89572011-10-31 22:50:49 +000061 m_const_result ()
Sean Callanan65dafa82010-08-27 01:01:44 +000062{
Sean Callanan5b658cc2011-11-07 23:35:40 +000063 switch (m_language)
64 {
65 case lldb::eLanguageTypeC_plus_plus:
66 m_allow_cxx = true;
67 break;
68 case lldb::eLanguageTypeObjC:
69 m_allow_objc = true;
70 break;
71 case lldb::eLanguageTypeObjC_plus_plus:
72 default:
73 m_allow_cxx = true;
74 m_allow_objc = true;
75 break;
76 }
Sean Callanan65dafa82010-08-27 01:01:44 +000077}
78
Sean Callanan830a9032010-08-27 23:31:21 +000079ClangUserExpression::~ClangUserExpression ()
80{
81}
82
Sean Callanan65dafa82010-08-27 01:01:44 +000083clang::ASTConsumer *
84ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan036fa902011-08-24 22:18:12 +000085{
Sean Callanan24312442011-08-23 21:20:51 +000086 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
87
88 if (!clang_ast_context)
89 return NULL;
90
Sean Callanan060d53f2011-10-08 00:21:35 +000091 if (!m_result_synthesizer.get())
92 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
93 m_desired_type,
94 *m_target->GetScratchClangASTContext()->getASTContext(),
95 m_target->GetPersistentVariables()));
96
97 return m_result_synthesizer.get();
Sean Callanan65dafa82010-08-27 01:01:44 +000098}
99
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000100void
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000101ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000102{
Greg Clayton567e7f32011-09-22 04:58:26 +0000103 m_target = exe_ctx.GetTargetPtr();
Greg Clayton144188b2011-09-12 23:21:58 +0000104
Sean Callanan5b658cc2011-11-07 23:35:40 +0000105 if (!(m_allow_cxx || m_allow_objc))
106 return;
107
Greg Clayton567e7f32011-09-22 04:58:26 +0000108 StackFrame *frame = exe_ctx.GetFramePtr();
109 if (frame == NULL)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000110 return;
111
Greg Clayton567e7f32011-09-22 04:58:26 +0000112 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanane8e55572010-12-01 21:35:54 +0000113
Sean Callananc617a4c2011-08-05 23:43:37 +0000114 if (!sym_ctx.function)
115 return;
116
117 clang::DeclContext *decl_context;
118
119 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
120 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
121 else
122 decl_context = sym_ctx.function->GetClangDeclContext();
123
124 if (!decl_context)
125 return;
Sean Callanan036fa902011-08-24 22:18:12 +0000126
Sean Callananc617a4c2011-08-05 23:43:37 +0000127 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000128 {
Sean Callanan5b658cc2011-11-07 23:35:40 +0000129 if (m_allow_cxx && method_decl->isInstance())
Sean Callanane8e55572010-12-01 21:35:54 +0000130 {
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000131 VariableList *vars = frame->GetVariableList(false);
132
133 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
134
135 if (!vars)
136 {
137 err.SetErrorToGenericError();
138 err.SetErrorString(thisErrorString);
139 return;
140 }
141
142 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
143
144 if (!this_var ||
145 !this_var->IsInScope(frame) ||
146 !this_var->LocationIsValidForFrame (frame))
147 {
148 err.SetErrorToGenericError();
149 err.SetErrorString(thisErrorString);
150 return;
151 }
152
Sean Callananc617a4c2011-08-05 23:43:37 +0000153 m_cplusplus = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000154 m_needs_object_ptr = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000155
Sean Callananc617a4c2011-08-05 23:43:37 +0000156 do {
157 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
158
159 const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
160
161 if (!this_pointer_type)
162 break;
163
164 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
165 } while (0);
Sean Callanane8e55572010-12-01 21:35:54 +0000166 }
167 }
Sean Callananc617a4c2011-08-05 23:43:37 +0000168 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000169 {
Sean Callanan5b658cc2011-11-07 23:35:40 +0000170 if (m_allow_objc && method_decl->isInstanceMethod())
Sean Callanande3d27e2011-09-26 18:45:31 +0000171 {
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000172 VariableList *vars = frame->GetVariableList(false);
173
174 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
175
176 if (!vars)
177 {
178 err.SetErrorToGenericError();
179 err.SetErrorString(selfErrorString);
180 return;
181 }
182
183 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
184
185 if (!self_var ||
186 !self_var->IsInScope(frame) ||
187 !self_var->LocationIsValidForFrame (frame))
188 {
189 err.SetErrorToGenericError();
190 err.SetErrorString(selfErrorString);
191 return;
192 }
193
Sean Callananc617a4c2011-08-05 23:43:37 +0000194 m_objectivec = true;
Sean Callanande3d27e2011-09-26 18:45:31 +0000195 m_needs_object_ptr = true;
196 }
Sean Callanane8e55572010-12-01 21:35:54 +0000197 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000198}
199
Sean Callanan550f2762010-10-22 23:25:16 +0000200// This is a really nasty hack, meant to fix Objective-C expressions of the form
201// (int)[myArray count]. Right now, because the type information for count is
202// not available, [myArray count] returns id, which can't be directly cast to
203// int without causing a clang error.
204static void
205ApplyObjcCastHack(std::string &expr)
206{
207#define OBJC_CAST_HACK_FROM "(int)["
208#define OBJC_CAST_HACK_TO "(int)(long long)["
209
210 size_t from_offset;
211
212 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
213 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
214
215#undef OBJC_CAST_HACK_TO
216#undef OBJC_CAST_HACK_FROM
217}
218
Sean Callanan30892372010-10-24 20:45:49 +0000219// Another hack, meant to allow use of unichar despite it not being available in
220// the type information. Although we could special-case it in type lookup,
221// hopefully we'll figure out a way to #include the same environment as is
222// present in the original source file rather than try to hack specific type
223// definitions in as needed.
224static void
225ApplyUnicharHack(std::string &expr)
226{
227#define UNICHAR_HACK_FROM "unichar"
228#define UNICHAR_HACK_TO "unsigned short"
229
230 size_t from_offset;
231
232 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
233 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
234
235#undef UNICHAR_HACK_TO
236#undef UNICHAR_HACK_FROM
237}
238
Sean Callanan550f2762010-10-22 23:25:16 +0000239bool
Sean Callanana91dd992010-11-19 02:52:21 +0000240ClangUserExpression::Parse (Stream &error_stream,
241 ExecutionContext &exe_ctx,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000242 TypeFromUser desired_type,
Sean Callanan47dc4572011-09-15 02:13:07 +0000243 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000244 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000245{
Greg Claytone005f2c2010-11-06 01:53:30 +0000246 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000247
Sean Callananfa9e6dd2011-11-04 02:09:33 +0000248 Error err;
249
250 ScanContext(exe_ctx, err);
251
252 if (!err.Success())
253 {
254 error_stream.Printf("warning: %s\n", err.AsCString());
255 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000256
257 StreamString m_transformed_stream;
258
259 ////////////////////////////////////
260 // Generate the expression
261 //
Sean Callanan550f2762010-10-22 23:25:16 +0000262
263 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000264 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000265
Sean Callanande3d27e2011-09-26 18:45:31 +0000266 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
267
268 lldb::LanguageType lang_type;
269
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000270 if (m_cplusplus)
Sean Callanande3d27e2011-09-26 18:45:31 +0000271 lang_type = lldb::eLanguageTypeC_plus_plus;
272 else if(m_objectivec)
273 lang_type = lldb::eLanguageTypeObjC;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000274 else
Sean Callanande3d27e2011-09-26 18:45:31 +0000275 lang_type = lldb::eLanguageTypeC;
276
277 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object))
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000278 {
Sean Callanande3d27e2011-09-26 18:45:31 +0000279 error_stream.PutCString ("error: couldn't construct expression body");
280 return false;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000281 }
282
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000283 if (log)
284 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
285
Sean Callanan65dafa82010-08-27 01:01:44 +0000286 ////////////////////////////////////
287 // Set up the target and compiler
288 //
289
Greg Clayton567e7f32011-09-22 04:58:26 +0000290 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan65dafa82010-08-27 01:01:44 +0000291
292 if (!target)
293 {
294 error_stream.PutCString ("error: invalid target\n");
295 return false;
296 }
297
Sean Callanan65dafa82010-08-27 01:01:44 +0000298 //////////////////////////
299 // Parse the expression
300 //
301
Sean Callanana91dd992010-11-19 02:52:21 +0000302 m_desired_type = desired_type;
303
Sean Callanan73b520f2011-10-29 01:58:46 +0000304 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callananaa301c42010-12-03 01:38:59 +0000305
Sean Callanan166ba102011-08-01 18:18:33 +0000306 if (!m_expr_decl_map->WillParse(exe_ctx))
307 {
308 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
309 return false;
310 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000311
Greg Clayton567e7f32011-09-22 04:58:26 +0000312 Process *process = exe_ctx.GetProcessPtr();
313 ClangExpressionParser parser(process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000314
315 unsigned num_errors = parser.Parse (error_stream);
316
317 if (num_errors)
318 {
319 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000320
321 m_expr_decl_map->DidParse();
322
Sean Callanan65dafa82010-08-27 01:01:44 +0000323 return false;
324 }
325
Sean Callanan47dc4572011-09-15 02:13:07 +0000326 //////////////////////////////////////////////////////////////////////////////////////////
327 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan65dafa82010-08-27 01:01:44 +0000328 //
Sean Callanan65dafa82010-08-27 01:01:44 +0000329
Greg Clayton567e7f32011-09-22 04:58:26 +0000330 if (execution_policy != eExecutionPolicyNever && process)
331 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan65dafa82010-08-27 01:01:44 +0000332
Sean Callanan47dc4572011-09-15 02:13:07 +0000333 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
334 m_jit_start_addr,
335 m_jit_end_addr,
336 exe_ctx,
337 m_data_allocator.get(),
338 m_evaluated_statically,
339 m_const_result,
340 execution_policy);
Sean Callanan65dafa82010-08-27 01:01:44 +0000341
Sean Callanan47dc4572011-09-15 02:13:07 +0000342 if (log && m_data_allocator.get())
Sean Callananc0492742011-05-23 21:40:23 +0000343 {
344 StreamString dump_string;
345 m_data_allocator->Dump(dump_string);
346
347 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
348 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000349
Sean Callanan65dafa82010-08-27 01:01:44 +0000350 if (jit_error.Success())
351 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000352 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
353 m_jit_process_sp = process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000354 return true;
355 }
356 else
357 {
Greg Clayton30581972011-05-17 03:51:29 +0000358 const char *error_cstr = jit_error.AsCString();
359 if (error_cstr && error_cstr[0])
360 error_stream.Printf ("error: %s\n", error_cstr);
361 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000362 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan65dafa82010-08-27 01:01:44 +0000363 return false;
364 }
365}
366
367bool
Jim Inghamd1686902010-10-14 23:45:03 +0000368ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000369 ExecutionContext &exe_ctx,
370 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000371 lldb::addr_t &object_ptr,
372 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000373{
Greg Claytone005f2c2010-11-06 01:53:30 +0000374 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000375
Greg Claytond0882d02011-01-19 23:00:49 +0000376 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000377 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000378 Error materialize_error;
379
Sean Callanan3aa7da52010-12-13 22:46:15 +0000380 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000381 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000382 ConstString object_name;
383
384 if (m_cplusplus)
385 {
386 object_name.SetCString("this");
387 }
388 else if (m_objectivec)
389 {
390 object_name.SetCString("self");
391 }
392 else
393 {
394 error_stream.Printf("Need object pointer but don't know the language\n");
395 return false;
396 }
397
398 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
399 {
400 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
401 return false;
402 }
Sean Callanan047923c2010-12-14 00:42:36 +0000403
404 if (m_objectivec)
405 {
406 ConstString cmd_name("_cmd");
407
408 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
409 {
410 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
411 return false;
412 }
413 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000414 }
415
Sean Callananaa301c42010-12-03 01:38:59 +0000416 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000417 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000418 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000419 return false;
420 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000421
422#if 0
423 // jingham: look here
424 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000425 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 +0000426#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000427
428 if (log)
429 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000430 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000431
Greg Claytond0882d02011-01-19 23:00:49 +0000432 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000433
434 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000435 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000436
Sean Callanan33711022010-12-07 10:00:20 +0000437 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000438
439 StreamString args;
440
441 Error dump_error;
442
Sean Callanane8a59a82010-09-13 21:34:21 +0000443 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000444 {
Sean Callananaa301c42010-12-03 01:38:59 +0000445 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000446 {
Sean Callanan33711022010-12-07 10:00:20 +0000447 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000448 }
449 else
450 {
Sean Callanan33711022010-12-07 10:00:20 +0000451 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000452 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000453 }
454 }
Jim Inghamd1686902010-10-14 23:45:03 +0000455 }
456 return true;
457}
458
459ThreadPlan *
460ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000461 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000462{
463 lldb::addr_t struct_address;
464
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000465 lldb::addr_t object_ptr = 0;
466 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000467
Sean Callanan047923c2010-12-14 00:42:36 +0000468 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000469
Jim Ingham360f53f2010-11-30 02:22:11 +0000470 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
471 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000472 // forcing unwind_on_error to be true here, in practical terms that can't happen.
473
Jim Inghamd1686902010-10-14 23:45:03 +0000474 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000475 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000476 struct_address,
477 error_stream,
478 true,
479 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000480 (m_needs_object_ptr ? &object_ptr : NULL),
481 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000482}
483
484bool
485ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
486 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000487 lldb::ClangExpressionVariableSP &result,
488 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000489{
490 Error expr_error;
491
Sean Callanan33711022010-12-07 10:00:20 +0000492 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
493
494 if (log)
495 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000496 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000497
498 StreamString args;
499
500 Error dump_error;
501
502 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
503 {
504 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
505 }
506 else
507 {
508 log->Printf(" Structure contents:\n%s", args.GetData());
509 }
510 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000511
512 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
513
Sean Callanan33711022010-12-07 10:00:20 +0000514
Sean Callanan0ddf8062011-05-09 22:04:36 +0000515 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000516 {
517 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
518 return false;
519 }
Sean Callanan6d284ef2011-10-12 22:20:02 +0000520
Jim Inghamd1686902010-10-14 23:45:03 +0000521 return true;
522}
523
Greg Claytonb3448432011-03-24 21:19:54 +0000524ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000525ClangUserExpression::Execute (Stream &error_stream,
526 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000527 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000528 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000529 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000530{
Jim Ingham7812e012011-01-18 22:20:08 +0000531 // The expression log is quite verbose, and if you're just tracking the execution of the
532 // expression, it's quite convenient to have these logs come out with the STEP log as well.
533 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000534
Sean Callanan47dc4572011-09-15 02:13:07 +0000535 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000536 {
537 lldb::addr_t struct_address;
538
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000539 lldb::addr_t object_ptr = 0;
540 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000541
Sean Callanan047923c2010-12-14 00:42:36 +0000542 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000543 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000544
Jim Inghamea9d4262010-11-05 19:25:48 +0000545 const bool stop_others = true;
546 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000547
Greg Claytond0882d02011-01-19 23:00:49 +0000548 Address wrapper_address (NULL, m_jit_start_addr);
Greg Clayton567e7f32011-09-22 04:58:26 +0000549 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan3aa7da52010-12-13 22:46:15 +0000550 wrapper_address,
551 struct_address,
552 stop_others,
553 discard_on_error,
554 (m_needs_object_ptr ? &object_ptr : NULL),
555 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
556 shared_ptr_to_me));
557
Jim Ingham360f53f2010-11-30 02:22:11 +0000558 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000559 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000560
561 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000562
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000563 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000564
Greg Clayton61468e82011-01-19 07:54:15 +0000565 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000566
567 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000568 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000569
Jim Ingham0296fe72011-11-08 03:00:11 +0000570 if (exe_ctx.GetProcessPtr())
571 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
572
Greg Clayton567e7f32011-09-22 04:58:26 +0000573 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
574 call_plan_sp,
575 stop_others,
576 try_all_threads,
577 discard_on_error,
578 single_thread_timeout_usec,
579 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000580
Jim Ingham0296fe72011-11-08 03:00:11 +0000581 if (exe_ctx.GetProcessPtr())
582 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
583
Sean Callanan33711022010-12-07 10:00:20 +0000584 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000585 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000586
Greg Claytonb3448432011-03-24 21:19:54 +0000587 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000588 {
Jim Ingham2370a972011-05-17 01:10:11 +0000589 const char *error_desc = NULL;
590
591 if (call_plan_sp)
592 {
593 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
594 if (real_stop_info_sp)
595 error_desc = real_stop_info_sp->GetDescription();
596 }
597 if (error_desc)
598 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000599 else
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000600 error_stream.Printf ("Execution was interrupted.");
Jim Ingham2370a972011-05-17 01:10:11 +0000601
602 if (discard_on_error)
603 error_stream.Printf ("\nThe process has been returned to the state before execution.");
604 else
605 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000606
607 return execution_result;
608 }
Greg Claytonb3448432011-03-24 21:19:54 +0000609 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000610 {
611 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
612 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000613 }
614
Sean Callanan0ddf8062011-05-09 22:04:36 +0000615 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000616 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000617 else
Greg Claytonb3448432011-03-24 21:19:54 +0000618 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000619 }
620 else
621 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000622 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytonb3448432011-03-24 21:19:54 +0000623 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000624 }
625}
626
Greg Claytonb3448432011-03-24 21:19:54 +0000627ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000628ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
629 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000630 lldb::LanguageType language,
Jim Inghamea9d4262010-11-05 19:25:48 +0000631 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000632 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000633 const char *expr_prefix,
634 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000635{
Jim Inghamec07c0d2011-08-09 00:00:49 +0000636 Error error;
Sean Callanan5b658cc2011-11-07 23:35:40 +0000637 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 +0000638}
639
640ExecutionResults
Sean Callanan47dc4572011-09-15 02:13:07 +0000641ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
642 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +0000643 lldb::LanguageType language,
Sean Callanan47dc4572011-09-15 02:13:07 +0000644 bool discard_on_error,
645 const char *expr_cstr,
646 const char *expr_prefix,
647 lldb::ValueObjectSP &result_valobj_sp,
648 Error &error)
Jim Inghamec07c0d2011-08-09 00:00:49 +0000649{
Jim Ingham7812e012011-01-18 22:20:08 +0000650 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000651
Greg Claytonb3448432011-03-24 21:19:54 +0000652 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000653
Greg Clayton567e7f32011-09-22 04:58:26 +0000654 Process *process = exe_ctx.GetProcessPtr();
655
656 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +0000657 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000658 if (execution_policy == eExecutionPolicyAlways)
659 {
660 if (log)
661 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Ingham360f53f2010-11-30 02:22:11 +0000662
Sean Callanan47dc4572011-09-15 02:13:07 +0000663 error.SetErrorString ("expression needed to run but couldn't");
664
665 return execution_results;
666 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000667 }
Sean Callananf7649bb2011-09-15 17:43:00 +0000668
Greg Clayton567e7f32011-09-22 04:58:26 +0000669 if (process == NULL || !process->CanJIT())
Sean Callananf7649bb2011-09-15 17:43:00 +0000670 execution_policy = eExecutionPolicyNever;
Greg Clayton0baa3942010-11-04 01:54:29 +0000671
Sean Callanan5b658cc2011-11-07 23:35:40 +0000672 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language));
Jim Ingham360f53f2010-11-30 02:22:11 +0000673
Greg Clayton377e0b42010-10-05 00:31:29 +0000674 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000675
Sean Callanan94d255f2010-12-07 22:55:01 +0000676 if (log)
677 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
678
Sean Callanan47dc4572011-09-15 02:13:07 +0000679 const bool keep_expression_in_memory = true;
680
681 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 +0000682 {
683 if (error_stream.GetString().empty())
684 error.SetErrorString ("expression failed to parse, unknown error");
685 else
686 error.SetErrorString (error_stream.GetString().c_str());
687 }
688 else
689 {
Greg Clayton427f2902010-12-14 02:59:59 +0000690 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000691
Sean Callanan47dc4572011-09-15 02:13:07 +0000692 if (user_expression_sp->EvaluatedStatically())
Greg Clayton377e0b42010-10-05 00:31:29 +0000693 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000694 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000695 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000696
Sean Callanan47dc4572011-09-15 02:13:07 +0000697 if (user_expression_sp->m_const_result)
698 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
699 else
700 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
701
Jim Inghamec07c0d2011-08-09 00:00:49 +0000702 execution_results = eExecutionCompleted;
Greg Clayton377e0b42010-10-05 00:31:29 +0000703 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000704 else if (execution_policy == eExecutionPolicyNever)
705 {
706 if (log)
707 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
708
709 if (error_stream.GetString().empty())
710 error.SetErrorString ("expression needed to run but couldn't");
711 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000712 else
713 {
714 error_stream.GetString().clear();
715
716 if (log)
717 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
718
719 execution_results = user_expression_sp->Execute (error_stream,
720 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000721 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000722 user_expression_sp,
723 expr_result);
724
Greg Claytonb3448432011-03-24 21:19:54 +0000725 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000726 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000727 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000728 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
729
730 if (error_stream.GetString().empty())
731 error.SetErrorString ("expression failed to execute, unknown error");
732 else
733 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000734 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000735 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000736 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000737 if (expr_result)
738 {
739 result_valobj_sp = expr_result->GetValueObject();
740
741 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000742 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000743 }
744 else
745 {
746 if (log)
747 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
748
Sean Callanan24312442011-08-23 21:20:51 +0000749 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000750 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000751 }
752 }
753 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000754
Greg Claytond1719722010-10-05 03:13:51 +0000755 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000756 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000757
Jim Ingham360f53f2010-11-30 02:22:11 +0000758 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000759}