blob: 04d5c32d1f2c057bd7649972f9d27c8e016797b1 [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"
Sean Callananfc55f5d2010-09-21 00:44:12 +000033#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000034#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000035#include "lldb/Target/Process.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000036#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000037#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000038#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000040
Sean Callanan72e49402011-08-05 23:43:37 +000041#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
Sean Callanan1a8d4092010-08-27 01:01:44 +000044using namespace lldb_private;
45
Sean Callanan322f5292010-10-29 00:29:03 +000046ClangUserExpression::ClangUserExpression (const char *expr,
Sean Callananc7b65062011-11-07 23:35:40 +000047 const char *expr_prefix,
48 lldb::LanguageType language) :
Greg Clayton22a939a2011-01-19 23:00:49 +000049 ClangExpression (),
50 m_expr_text (expr),
51 m_expr_prefix (expr_prefix ? expr_prefix : ""),
Sean Callananc7b65062011-11-07 23:35:40 +000052 m_language (language),
Greg Clayton22a939a2011-01-19 23:00:49 +000053 m_transformed_text (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000054 m_desired_type (NULL, NULL),
Greg Clayton22a939a2011-01-19 23:00:49 +000055 m_cplusplus (false),
56 m_objectivec (false),
57 m_needs_object_ptr (false),
Sean Callanan63697e52011-05-07 01:06:41 +000058 m_const_object (false),
Sean Callanand5c17ed2011-11-15 02:11:17 +000059 m_static_method(false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000060 m_target (NULL),
Sean Callanan3bfdaa22011-09-15 02:13:07 +000061 m_evaluated_statically (false),
Daniel Dunbara08823f2011-10-31 22:50:49 +000062 m_const_result ()
Sean Callanan1a8d4092010-08-27 01:01:44 +000063{
Sean Callananc7b65062011-11-07 23:35:40 +000064 switch (m_language)
65 {
66 case lldb::eLanguageTypeC_plus_plus:
67 m_allow_cxx = true;
68 break;
69 case lldb::eLanguageTypeObjC:
70 m_allow_objc = true;
71 break;
72 case lldb::eLanguageTypeObjC_plus_plus:
73 default:
74 m_allow_cxx = true;
75 m_allow_objc = true;
76 break;
77 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000078}
79
Sean Callanane71d5532010-08-27 23:31:21 +000080ClangUserExpression::~ClangUserExpression ()
81{
82}
83
Sean Callanan1a8d4092010-08-27 01:01:44 +000084clang::ASTConsumer *
85ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan737330c2011-08-24 22:18:12 +000086{
Sean Callananbccce812011-08-23 21:20:51 +000087 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
88
89 if (!clang_ast_context)
90 return NULL;
91
Sean Callanan2590b9a2011-10-08 00:21:35 +000092 if (!m_result_synthesizer.get())
93 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
94 m_desired_type,
Sean Callanan0eed0d42011-12-06 03:41:14 +000095 *m_target));
Sean Callanan2590b9a2011-10-08 00:21:35 +000096
97 return m_result_synthesizer.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +000098}
99
Sean Callananfc55f5d2010-09-21 00:44:12 +0000100void
Sean Callanan744756e2011-11-04 02:09:33 +0000101ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000102{
Greg Claytonc14ee322011-09-22 04:58:26 +0000103 m_target = exe_ctx.GetTargetPtr();
Greg Claytond4a2b372011-09-12 23:21:58 +0000104
Sean Callananc7b65062011-11-07 23:35:40 +0000105 if (!(m_allow_cxx || m_allow_objc))
106 return;
107
Greg Claytonc14ee322011-09-22 04:58:26 +0000108 StackFrame *frame = exe_ctx.GetFramePtr();
109 if (frame == NULL)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000110 return;
111
Greg Claytonc14ee322011-09-22 04:58:26 +0000112 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanan3670ba52010-12-01 21:35:54 +0000113
Sean Callanan72e49402011-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 Callanan737330c2011-08-24 22:18:12 +0000126
Sean Callanan72e49402011-08-05 23:43:37 +0000127 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanan3670ba52010-12-01 21:35:54 +0000128 {
Sean Callananc7b65062011-11-07 23:35:40 +0000129 if (m_allow_cxx && method_decl->isInstance())
Sean Callanan3670ba52010-12-01 21:35:54 +0000130 {
Sean Callanan744756e2011-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 Callanan72e49402011-08-05 23:43:37 +0000153 m_cplusplus = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000154 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000155
Sean Callanan72e49402011-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 Callanan3670ba52010-12-01 21:35:54 +0000166 }
167 }
Sean Callanan72e49402011-08-05 23:43:37 +0000168 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanan744756e2011-11-04 02:09:33 +0000169 {
Sean Callanand5c17ed2011-11-15 02:11:17 +0000170 if (m_allow_objc)
Sean Callanan9bc83842011-09-26 18:45:31 +0000171 {
Sean Callanan744756e2011-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 Callanan72e49402011-08-05 23:43:37 +0000194 m_objectivec = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000195 m_needs_object_ptr = true;
Sean Callanand5c17ed2011-11-15 02:11:17 +0000196
197 if (!method_decl->isInstanceMethod())
198 m_static_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000199 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000200 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000201}
202
Sean Callanancf5498f2010-10-22 23:25:16 +0000203// This is a really nasty hack, meant to fix Objective-C expressions of the form
204// (int)[myArray count]. Right now, because the type information for count is
205// not available, [myArray count] returns id, which can't be directly cast to
206// int without causing a clang error.
207static void
208ApplyObjcCastHack(std::string &expr)
209{
210#define OBJC_CAST_HACK_FROM "(int)["
211#define OBJC_CAST_HACK_TO "(int)(long long)["
212
213 size_t from_offset;
214
215 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
216 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
217
218#undef OBJC_CAST_HACK_TO
219#undef OBJC_CAST_HACK_FROM
220}
221
Sean Callanan64186e72010-10-24 20:45:49 +0000222// Another hack, meant to allow use of unichar despite it not being available in
223// the type information. Although we could special-case it in type lookup,
224// hopefully we'll figure out a way to #include the same environment as is
225// present in the original source file rather than try to hack specific type
226// definitions in as needed.
227static void
228ApplyUnicharHack(std::string &expr)
229{
230#define UNICHAR_HACK_FROM "unichar"
231#define UNICHAR_HACK_TO "unsigned short"
232
233 size_t from_offset;
234
235 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
236 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
237
238#undef UNICHAR_HACK_TO
239#undef UNICHAR_HACK_FROM
240}
241
Sean Callanancf5498f2010-10-22 23:25:16 +0000242bool
Sean Callananf7c3e272010-11-19 02:52:21 +0000243ClangUserExpression::Parse (Stream &error_stream,
244 ExecutionContext &exe_ctx,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000245 TypeFromUser desired_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000246 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan63697e52011-05-07 01:06:41 +0000247 bool keep_result_in_memory)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000248{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000249 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000250
Sean Callanan744756e2011-11-04 02:09:33 +0000251 Error err;
252
253 ScanContext(exe_ctx, err);
254
255 if (!err.Success())
256 {
257 error_stream.Printf("warning: %s\n", err.AsCString());
258 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000259
260 StreamString m_transformed_stream;
261
262 ////////////////////////////////////
263 // Generate the expression
264 //
Sean Callanancf5498f2010-10-22 23:25:16 +0000265
266 ApplyObjcCastHack(m_expr_text);
Greg Clayton73b472d2010-10-27 03:32:59 +0000267 //ApplyUnicharHack(m_expr_text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000268
Sean Callanan9bc83842011-09-26 18:45:31 +0000269 std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
270
271 lldb::LanguageType lang_type;
272
Sean Callananfc55f5d2010-09-21 00:44:12 +0000273 if (m_cplusplus)
Sean Callanan9bc83842011-09-26 18:45:31 +0000274 lang_type = lldb::eLanguageTypeC_plus_plus;
275 else if(m_objectivec)
276 lang_type = lldb::eLanguageTypeObjC;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000277 else
Sean Callanan9bc83842011-09-26 18:45:31 +0000278 lang_type = lldb::eLanguageTypeC;
279
Sean Callanand5c17ed2011-11-15 02:11:17 +0000280 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000281 {
Sean Callanan9bc83842011-09-26 18:45:31 +0000282 error_stream.PutCString ("error: couldn't construct expression body");
283 return false;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000284 }
285
Sean Callananfc55f5d2010-09-21 00:44:12 +0000286 if (log)
287 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
288
Sean Callanan1a8d4092010-08-27 01:01:44 +0000289 ////////////////////////////////////
290 // Set up the target and compiler
291 //
292
Greg Claytonc14ee322011-09-22 04:58:26 +0000293 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000294
295 if (!target)
296 {
297 error_stream.PutCString ("error: invalid target\n");
298 return false;
299 }
300
Sean Callanan1a8d4092010-08-27 01:01:44 +0000301 //////////////////////////
302 // Parse the expression
303 //
304
Sean Callananf7c3e272010-11-19 02:52:21 +0000305 m_desired_type = desired_type;
306
Sean Callanan1ee44b72011-10-29 01:58:46 +0000307 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
Sean Callanan979f74d2010-12-03 01:38:59 +0000308
Sean Callananb9951192011-08-01 18:18:33 +0000309 if (!m_expr_decl_map->WillParse(exe_ctx))
310 {
311 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
312 return false;
313 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000314
Greg Claytonc14ee322011-09-22 04:58:26 +0000315 Process *process = exe_ctx.GetProcessPtr();
316 ClangExpressionParser parser(process, *this);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000317
318 unsigned num_errors = parser.Parse (error_stream);
319
320 if (num_errors)
321 {
322 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanan979f74d2010-12-03 01:38:59 +0000323
324 m_expr_decl_map->DidParse();
325
Sean Callanan1a8d4092010-08-27 01:01:44 +0000326 return false;
327 }
328
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000329 //////////////////////////////////////////////////////////////////////////////////////////
330 // Prepare the output of the parser for execution, evaluating it statically if possible
Sean Callanan1a8d4092010-08-27 01:01:44 +0000331 //
Sean Callanan1a8d4092010-08-27 01:01:44 +0000332
Greg Claytonc14ee322011-09-22 04:58:26 +0000333 if (execution_policy != eExecutionPolicyNever && process)
334 m_data_allocator.reset(new ProcessDataAllocator(*process));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000335
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000336 Error jit_error = parser.PrepareForExecution (m_jit_alloc,
337 m_jit_start_addr,
338 m_jit_end_addr,
339 exe_ctx,
340 m_data_allocator.get(),
341 m_evaluated_statically,
342 m_const_result,
343 execution_policy);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000345 if (log && m_data_allocator.get())
Sean Callanan79763a42011-05-23 21:40:23 +0000346 {
347 StreamString dump_string;
348 m_data_allocator->Dump(dump_string);
349
350 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
351 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000352
Sean Callanan1a8d4092010-08-27 01:01:44 +0000353 if (jit_error.Success())
354 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000355 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
356 m_jit_process_sp = process->GetSP();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000357 return true;
358 }
359 else
360 {
Greg Claytone6a9e432011-05-17 03:51:29 +0000361 const char *error_cstr = jit_error.AsCString();
362 if (error_cstr && error_cstr[0])
363 error_stream.Printf ("error: %s\n", error_cstr);
364 else
Jason Molendafd54b362011-09-20 21:44:10 +0000365 error_stream.Printf ("error: expression can't be interpreted or run\n");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000366 return false;
367 }
368}
369
370bool
Jim Ingham36f3b362010-10-14 23:45:03 +0000371ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callanan104a6e92010-10-19 23:57:21 +0000372 ExecutionContext &exe_ctx,
373 lldb::addr_t &struct_address,
Sean Callanan9d48e802010-12-14 00:42:36 +0000374 lldb::addr_t &object_ptr,
375 lldb::addr_t &cmd_ptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000376{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000377 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000378
Greg Clayton22a939a2011-01-19 23:00:49 +0000379 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000380 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000381 Error materialize_error;
382
Sean Callanan17827832010-12-13 22:46:15 +0000383 if (m_needs_object_ptr)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000384 {
Sean Callanan17827832010-12-13 22:46:15 +0000385 ConstString object_name;
386
387 if (m_cplusplus)
388 {
389 object_name.SetCString("this");
390 }
391 else if (m_objectivec)
392 {
393 object_name.SetCString("self");
394 }
395 else
396 {
397 error_stream.Printf("Need object pointer but don't know the language\n");
398 return false;
399 }
400
401 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
402 {
403 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
404 return false;
405 }
Sean Callanan9d48e802010-12-14 00:42:36 +0000406
407 if (m_objectivec)
408 {
409 ConstString cmd_name("_cmd");
410
411 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
412 {
413 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
414 return false;
415 }
416 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000417 }
418
Sean Callanan979f74d2010-12-03 01:38:59 +0000419 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan1a8d4092010-08-27 01:01:44 +0000420 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000421 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000422 return false;
423 }
Greg Claytonc4e411f2011-01-18 19:36:39 +0000424
425#if 0
426 // jingham: look here
427 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Clayton22a939a2011-01-19 23:00:49 +0000428 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 Claytonc4e411f2011-01-18 19:36:39 +0000429#endif
Sean Callanan1a8d4092010-08-27 01:01:44 +0000430
431 if (log)
432 {
Sean Callanana162eba2010-12-07 22:55:01 +0000433 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000434
Greg Clayton22a939a2011-01-19 23:00:49 +0000435 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000436
437 if (m_needs_object_ptr)
Sean Callananc673a6e2010-12-07 10:00:20 +0000438 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000439
Sean Callananc673a6e2010-12-07 10:00:20 +0000440 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000441
442 StreamString args;
443
444 Error dump_error;
445
Sean Callanan9e6ed532010-09-13 21:34:21 +0000446 if (struct_address)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000447 {
Sean Callanan979f74d2010-12-03 01:38:59 +0000448 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000449 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000450 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanan9e6ed532010-09-13 21:34:21 +0000451 }
452 else
453 {
Sean Callananc673a6e2010-12-07 10:00:20 +0000454 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000455 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000456 }
457 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000458 }
459 return true;
460}
461
462ThreadPlan *
463ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan92adcac2011-01-13 08:53:35 +0000464 ExecutionContext &exe_ctx)
Jim Ingham36f3b362010-10-14 23:45:03 +0000465{
466 lldb::addr_t struct_address;
467
Johnny Chen44805302011-07-19 19:48:13 +0000468 lldb::addr_t object_ptr = 0;
469 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000470
Sean Callanan9d48e802010-12-14 00:42:36 +0000471 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Ingham36f3b362010-10-14 23:45:03 +0000472
Jim Inghamf48169b2010-11-30 02:22:11 +0000473 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
474 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan17827832010-12-13 22:46:15 +0000475 // forcing unwind_on_error to be true here, in practical terms that can't happen.
476
Jim Ingham36f3b362010-10-14 23:45:03 +0000477 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Clayton22a939a2011-01-19 23:00:49 +0000478 m_jit_start_addr,
Sean Callanan1d47caf2010-12-01 01:28:23 +0000479 struct_address,
480 error_stream,
481 true,
482 true,
Sean Callanan17827832010-12-13 22:46:15 +0000483 (m_needs_object_ptr ? &object_ptr : NULL),
484 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Ingham36f3b362010-10-14 23:45:03 +0000485}
486
487bool
488ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
489 ExecutionContext &exe_ctx,
Sean Callanane359d9b2011-05-09 22:04:36 +0000490 lldb::ClangExpressionVariableSP &result,
491 lldb::addr_t function_stack_pointer)
Jim Ingham36f3b362010-10-14 23:45:03 +0000492{
493 Error expr_error;
494
Sean Callananc673a6e2010-12-07 10:00:20 +0000495 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
496
497 if (log)
498 {
Sean Callanana162eba2010-12-07 22:55:01 +0000499 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000500
501 StreamString args;
502
503 Error dump_error;
504
505 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
506 {
507 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
508 }
509 else
510 {
511 log->Printf(" Structure contents:\n%s", args.GetData());
512 }
513 }
Sean Callanane359d9b2011-05-09 22:04:36 +0000514
515 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
516
Sean Callananc673a6e2010-12-07 10:00:20 +0000517
Sean Callanane359d9b2011-05-09 22:04:36 +0000518 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Ingham36f3b362010-10-14 23:45:03 +0000519 {
520 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
521 return false;
522 }
Sean Callanane3aef1d2011-10-12 22:20:02 +0000523
Jim Ingham36f3b362010-10-14 23:45:03 +0000524 return true;
525}
526
Greg Claytone0d378b2011-03-24 21:19:54 +0000527ExecutionResults
Jim Ingham36f3b362010-10-14 23:45:03 +0000528ClangUserExpression::Execute (Stream &error_stream,
529 ExecutionContext &exe_ctx,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000530 bool discard_on_error,
Jim Inghamf48169b2010-11-30 02:22:11 +0000531 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000532 lldb::ClangExpressionVariableSP &result)
Jim Ingham36f3b362010-10-14 23:45:03 +0000533{
Jim Inghamb086ff72011-01-18 22:20:08 +0000534 // The expression log is quite verbose, and if you're just tracking the execution of the
535 // expression, it's quite convenient to have these logs come out with the STEP log as well.
536 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananc673a6e2010-12-07 10:00:20 +0000537
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000538 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Ingham36f3b362010-10-14 23:45:03 +0000539 {
540 lldb::addr_t struct_address;
541
Johnny Chen44805302011-07-19 19:48:13 +0000542 lldb::addr_t object_ptr = 0;
543 lldb::addr_t cmd_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000544
Sean Callanan9d48e802010-12-14 00:42:36 +0000545 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytone0d378b2011-03-24 21:19:54 +0000546 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000547
Jim Ingham399f1ca2010-11-05 19:25:48 +0000548 const bool stop_others = true;
549 const bool try_all_threads = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000550
Greg Clayton22a939a2011-01-19 23:00:49 +0000551 Address wrapper_address (NULL, m_jit_start_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000552 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
Sean Callanan17827832010-12-13 22:46:15 +0000553 wrapper_address,
554 struct_address,
555 stop_others,
556 discard_on_error,
557 (m_needs_object_ptr ? &object_ptr : NULL),
558 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
559 shared_ptr_to_me));
560
Jim Inghamf48169b2010-11-30 02:22:11 +0000561 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytone0d378b2011-03-24 21:19:54 +0000562 return eExecutionSetupError;
Sean Callanane359d9b2011-05-09 22:04:36 +0000563
564 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Inghamf48169b2010-11-30 02:22:11 +0000565
Jim Ingham17e5c4e2011-05-17 22:24:54 +0000566 call_plan_sp->SetPrivate(true);
Jim Inghamf48169b2010-11-30 02:22:11 +0000567
Greg Claytone2d4f0d2011-01-19 07:54:15 +0000568 uint32_t single_thread_timeout_usec = 500000;
Sean Callananc673a6e2010-12-07 10:00:20 +0000569
570 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000571 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callananc673a6e2010-12-07 10:00:20 +0000572
Jim Ingham0faa43f2011-11-08 03:00:11 +0000573 if (exe_ctx.GetProcessPtr())
574 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
575
Greg Claytonc14ee322011-09-22 04:58:26 +0000576 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
577 call_plan_sp,
578 stop_others,
579 try_all_threads,
580 discard_on_error,
581 single_thread_timeout_usec,
582 error_stream);
Sean Callananc673a6e2010-12-07 10:00:20 +0000583
Jim Ingham0faa43f2011-11-08 03:00:11 +0000584 if (exe_ctx.GetProcessPtr())
585 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
586
Sean Callananc673a6e2010-12-07 10:00:20 +0000587 if (log)
Sean Callanana162eba2010-12-07 22:55:01 +0000588 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Inghamf48169b2010-11-30 02:22:11 +0000589
Greg Claytone0d378b2011-03-24 21:19:54 +0000590 if (execution_result == eExecutionInterrupted)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000591 {
Jim Ingham160f78c2011-05-17 01:10:11 +0000592 const char *error_desc = NULL;
593
594 if (call_plan_sp)
595 {
596 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
597 if (real_stop_info_sp)
598 error_desc = real_stop_info_sp->GetDescription();
599 }
600 if (error_desc)
601 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Inghamf48169b2010-11-30 02:22:11 +0000602 else
Jason Molendafd54b362011-09-20 21:44:10 +0000603 error_stream.Printf ("Execution was interrupted.");
Jim Ingham160f78c2011-05-17 01:10:11 +0000604
605 if (discard_on_error)
606 error_stream.Printf ("\nThe process has been returned to the state before execution.");
607 else
608 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Inghamf48169b2010-11-30 02:22:11 +0000609
610 return execution_result;
611 }
Greg Claytone0d378b2011-03-24 21:19:54 +0000612 else if (execution_result != eExecutionCompleted)
Jim Inghamf48169b2010-11-30 02:22:11 +0000613 {
614 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
615 return execution_result;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000616 }
617
Sean Callanane359d9b2011-05-09 22:04:36 +0000618 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytone0d378b2011-03-24 21:19:54 +0000619 return eExecutionCompleted;
Jim Inghamf48169b2010-11-30 02:22:11 +0000620 else
Greg Claytone0d378b2011-03-24 21:19:54 +0000621 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000622 }
623 else
624 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000625 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
Greg Claytone0d378b2011-03-24 21:19:54 +0000626 return eExecutionSetupError;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000627 }
628}
629
Greg Claytone0d378b2011-03-24 21:19:54 +0000630ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000631ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
632 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000633 lldb::LanguageType language,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000634 bool discard_on_error,
Sean Callanan322f5292010-10-29 00:29:03 +0000635 const char *expr_cstr,
Jim Inghamf48169b2010-11-30 02:22:11 +0000636 const char *expr_prefix,
637 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton0184f012010-10-05 00:31:29 +0000638{
Jim Ingham41c75912011-08-09 00:00:49 +0000639 Error error;
Sean Callananc7b65062011-11-07 23:35:40 +0000640 return EvaluateWithError (exe_ctx, execution_policy, language, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
Jim Ingham41c75912011-08-09 00:00:49 +0000641}
642
643ExecutionResults
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000644ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
645 lldb_private::ExecutionPolicy execution_policy,
Sean Callananc7b65062011-11-07 23:35:40 +0000646 lldb::LanguageType language,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000647 bool discard_on_error,
648 const char *expr_cstr,
649 const char *expr_prefix,
650 lldb::ValueObjectSP &result_valobj_sp,
651 Error &error)
Jim Ingham41c75912011-08-09 00:00:49 +0000652{
Jim Inghamb086ff72011-01-18 22:20:08 +0000653 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanana162eba2010-12-07 22:55:01 +0000654
Greg Claytone0d378b2011-03-24 21:19:54 +0000655 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton8f343b02010-11-04 01:54:29 +0000656
Greg Claytonc14ee322011-09-22 04:58:26 +0000657 Process *process = exe_ctx.GetProcessPtr();
658
659 if (process == NULL || process->GetState() != lldb::eStateStopped)
Jim Inghamf48169b2010-11-30 02:22:11 +0000660 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000661 if (execution_policy == eExecutionPolicyAlways)
662 {
663 if (log)
664 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
Jim Inghamf48169b2010-11-30 02:22:11 +0000665
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000666 error.SetErrorString ("expression needed to run but couldn't");
667
668 return execution_results;
669 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000670 }
Sean Callanan64fe1992011-09-15 17:43:00 +0000671
Greg Claytonc14ee322011-09-22 04:58:26 +0000672 if (process == NULL || !process->CanJIT())
Sean Callanan64fe1992011-09-15 17:43:00 +0000673 execution_policy = eExecutionPolicyNever;
Greg Clayton8f343b02010-11-04 01:54:29 +0000674
Sean Callananc7b65062011-11-07 23:35:40 +0000675 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language));
Jim Inghamf48169b2010-11-30 02:22:11 +0000676
Greg Clayton0184f012010-10-05 00:31:29 +0000677 StreamString error_stream;
Sean Callanan63697e52011-05-07 01:06:41 +0000678
Sean Callanana162eba2010-12-07 22:55:01 +0000679 if (log)
680 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
681
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000682 const bool keep_expression_in_memory = true;
683
684 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), execution_policy, keep_expression_in_memory))
Greg Clayton0184f012010-10-05 00:31:29 +0000685 {
686 if (error_stream.GetString().empty())
687 error.SetErrorString ("expression failed to parse, unknown error");
688 else
689 error.SetErrorString (error_stream.GetString().c_str());
690 }
691 else
692 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000693 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton0184f012010-10-05 00:31:29 +0000694
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000695 if (user_expression_sp->EvaluatedStatically())
Greg Clayton0184f012010-10-05 00:31:29 +0000696 {
Sean Callanana162eba2010-12-07 22:55:01 +0000697 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000698 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanana162eba2010-12-07 22:55:01 +0000699
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000700 if (user_expression_sp->m_const_result)
701 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
702 else
703 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
704
Jim Ingham41c75912011-08-09 00:00:49 +0000705 execution_results = eExecutionCompleted;
Greg Clayton0184f012010-10-05 00:31:29 +0000706 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000707 else if (execution_policy == eExecutionPolicyNever)
708 {
709 if (log)
710 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
711
712 if (error_stream.GetString().empty())
713 error.SetErrorString ("expression needed to run but couldn't");
714 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000715 else
716 {
717 error_stream.GetString().clear();
718
719 if (log)
720 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
721
722 execution_results = user_expression_sp->Execute (error_stream,
723 exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35 +0000724 discard_on_error,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000725 user_expression_sp,
726 expr_result);
727
Greg Claytone0d378b2011-03-24 21:19:54 +0000728 if (execution_results != eExecutionCompleted)
Greg Clayton0184f012010-10-05 00:31:29 +0000729 {
Sean Callanana162eba2010-12-07 22:55:01 +0000730 if (log)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000731 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
732
733 if (error_stream.GetString().empty())
734 error.SetErrorString ("expression failed to execute, unknown error");
735 else
736 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton0184f012010-10-05 00:31:29 +0000737 }
Sean Callanane4ec90e2010-12-16 03:17:46 +0000738 else
Greg Clayton0184f012010-10-05 00:31:29 +0000739 {
Sean Callanane4ec90e2010-12-16 03:17:46 +0000740 if (expr_result)
741 {
742 result_valobj_sp = expr_result->GetValueObject();
743
744 if (log)
Jim Ingham6035b672011-03-31 00:19:25 +0000745 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanane4ec90e2010-12-16 03:17:46 +0000746 }
747 else
748 {
749 if (log)
750 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
751
Sean Callananbccce812011-08-23 21:20:51 +0000752 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000753 }
Greg Clayton0184f012010-10-05 00:31:29 +0000754 }
755 }
756 }
Sean Callananc57f64d2010-10-19 20:15:00 +0000757
Greg Claytonb71f3842010-10-05 03:13:51 +0000758 if (result_valobj_sp.get() == NULL)
Jim Ingham58b59f92011-04-22 23:53:53 +0000759 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb71f3842010-10-05 03:13:51 +0000760
Jim Inghamf48169b2010-11-30 02:22:11 +0000761 return execution_results;
Johnny Chendabefd02010-10-29 20:19:44 +0000762}