blob: 1a81c24efc017f11001b59e7132f1adeb6f2fe2a [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"
31#include "lldb/Host/Host.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000032#include "lldb/Symbol/VariableList.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000033#include "lldb/Target/ExecutionContext.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000034#include "lldb/Target/Process.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000035#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000036#include "lldb/Target/Target.h"
Jim Ingham360f53f2010-11-30 02:22:11 +000037#include "lldb/Target/ThreadPlan.h"
38#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000039
Sean Callananc617a4c2011-08-05 23:43:37 +000040#include "clang/AST/DeclCXX.h"
41#include "clang/AST/DeclObjC.h"
42
Sean Callanan65dafa82010-08-27 01:01:44 +000043using namespace lldb_private;
44
Sean Callanan77e93942010-10-29 00:29:03 +000045ClangUserExpression::ClangUserExpression (const char *expr,
46 const char *expr_prefix) :
Greg Claytond0882d02011-01-19 23:00:49 +000047 ClangExpression (),
48 m_expr_text (expr),
49 m_expr_prefix (expr_prefix ? expr_prefix : ""),
50 m_transformed_text (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000051 m_desired_type (NULL, NULL),
Greg Claytond0882d02011-01-19 23:00:49 +000052 m_cplusplus (false),
53 m_objectivec (false),
54 m_needs_object_ptr (false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000055 m_const_object (false),
Sean Callanan24312442011-08-23 21:20:51 +000056 m_const_result (),
57 m_target (NULL)
Sean Callanan65dafa82010-08-27 01:01:44 +000058{
Sean Callanan65dafa82010-08-27 01:01:44 +000059}
60
Sean Callanan830a9032010-08-27 23:31:21 +000061ClangUserExpression::~ClangUserExpression ()
62{
63}
64
Sean Callanan65dafa82010-08-27 01:01:44 +000065clang::ASTConsumer *
66ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
Sean Callanan036fa902011-08-24 22:18:12 +000067{
Sean Callanan24312442011-08-23 21:20:51 +000068 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
69
70 if (!clang_ast_context)
71 return NULL;
72
Sean Callanana91dd992010-11-19 02:52:21 +000073 return new ASTResultSynthesizer(passthrough,
Sean Callanan24312442011-08-23 21:20:51 +000074 m_desired_type,
75 *m_target->GetScratchClangASTContext()->getASTContext(),
76 m_target->GetPersistentVariables());
Sean Callanan65dafa82010-08-27 01:01:44 +000077}
78
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000079void
80ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
81{
Greg Clayton144188b2011-09-12 23:21:58 +000082 m_target = exe_ctx.target;
83
Sean Callananc617a4c2011-08-05 23:43:37 +000084 if (!exe_ctx.frame)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000085 return;
86
Sean Callananc617a4c2011-08-05 23:43:37 +000087 SymbolContext sym_ctx = exe_ctx.frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanane8e55572010-12-01 21:35:54 +000088
Sean Callananc617a4c2011-08-05 23:43:37 +000089 if (!sym_ctx.function)
90 return;
91
92 clang::DeclContext *decl_context;
93
94 if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
95 decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
96 else
97 decl_context = sym_ctx.function->GetClangDeclContext();
98
99 if (!decl_context)
100 return;
Sean Callanan036fa902011-08-24 22:18:12 +0000101
Sean Callananc617a4c2011-08-05 23:43:37 +0000102 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000103 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000104 if (method_decl->isInstance())
Sean Callanane8e55572010-12-01 21:35:54 +0000105 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000106 m_cplusplus = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000107
Sean Callananc617a4c2011-08-05 23:43:37 +0000108 do {
109 clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
110
111 const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
112
113 if (!this_pointer_type)
114 break;
115
116 clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
117 } while (0);
Sean Callanane8e55572010-12-01 21:35:54 +0000118 }
119 }
Sean Callananc617a4c2011-08-05 23:43:37 +0000120 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
Sean Callanane8e55572010-12-01 21:35:54 +0000121 {
Sean Callananc617a4c2011-08-05 23:43:37 +0000122 if (method_decl->isInstanceMethod())
123 m_objectivec = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000124 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000125}
126
Sean Callanan550f2762010-10-22 23:25:16 +0000127// This is a really nasty hack, meant to fix Objective-C expressions of the form
128// (int)[myArray count]. Right now, because the type information for count is
129// not available, [myArray count] returns id, which can't be directly cast to
130// int without causing a clang error.
131static void
132ApplyObjcCastHack(std::string &expr)
133{
134#define OBJC_CAST_HACK_FROM "(int)["
135#define OBJC_CAST_HACK_TO "(int)(long long)["
136
137 size_t from_offset;
138
139 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
140 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
141
142#undef OBJC_CAST_HACK_TO
143#undef OBJC_CAST_HACK_FROM
144}
145
Sean Callanan30892372010-10-24 20:45:49 +0000146// Another hack, meant to allow use of unichar despite it not being available in
147// the type information. Although we could special-case it in type lookup,
148// hopefully we'll figure out a way to #include the same environment as is
149// present in the original source file rather than try to hack specific type
150// definitions in as needed.
151static void
152ApplyUnicharHack(std::string &expr)
153{
154#define UNICHAR_HACK_FROM "unichar"
155#define UNICHAR_HACK_TO "unsigned short"
156
157 size_t from_offset;
158
159 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
160 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
161
162#undef UNICHAR_HACK_TO
163#undef UNICHAR_HACK_FROM
164}
165
Sean Callanan550f2762010-10-22 23:25:16 +0000166bool
Sean Callanana91dd992010-11-19 02:52:21 +0000167ClangUserExpression::Parse (Stream &error_stream,
168 ExecutionContext &exe_ctx,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000169 TypeFromUser desired_type,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000170 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000171{
Greg Claytone005f2c2010-11-06 01:53:30 +0000172 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000173
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000174 ScanContext(exe_ctx);
175
176 StreamString m_transformed_stream;
177
178 ////////////////////////////////////
179 // Generate the expression
180 //
Sean Callanan550f2762010-10-22 23:25:16 +0000181
182 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000183 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000184
185 if (m_cplusplus)
186 {
Sean Callanan77e93942010-10-29 00:29:03 +0000187 m_transformed_stream.Printf("%s \n"
188 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000189 "void \n"
Sean Callanane8e55572010-12-01 21:35:54 +0000190 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000191 "{ \n"
192 " %s; \n"
193 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000194 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000195 FunctionName(),
Sean Callanane8e55572010-12-01 21:35:54 +0000196 (m_const_object ? "const" : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000197 m_expr_text.c_str());
198
199 m_needs_object_ptr = true;
200 }
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000201 else if (m_objectivec)
Sean Callanan3aa7da52010-12-13 22:46:15 +0000202 {
203 const char *function_name = FunctionName();
204
205 m_transformed_stream.Printf("%s \n"
Greg Claytonc5157ec2010-12-17 02:26:24 +0000206 "typedef unsigned short unichar; \n"
Sean Callanan3aa7da52010-12-13 22:46:15 +0000207 "@interface $__lldb_objc_class ($__lldb_category) \n"
208 "-(void)%s:(void *)$__lldb_arg; \n"
209 "@end \n"
210 "@implementation $__lldb_objc_class ($__lldb_category) \n"
211 "-(void)%s:(void *)$__lldb_arg \n"
212 "{ \n"
213 " %s; \n"
214 "} \n"
215 "@end \n",
216 m_expr_prefix.c_str(),
217 function_name,
218 function_name,
219 m_expr_text.c_str());
220
221 m_needs_object_ptr = true;
222 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000223 else
224 {
Sean Callanan77e93942010-10-29 00:29:03 +0000225 m_transformed_stream.Printf("%s \n"
226 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000227 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000228 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000229 "{ \n"
230 " %s; \n"
231 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000232 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000233 FunctionName(),
234 m_expr_text.c_str());
235 }
236
237 m_transformed_text = m_transformed_stream.GetData();
238
239
240 if (log)
241 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
242
Sean Callanan65dafa82010-08-27 01:01:44 +0000243 ////////////////////////////////////
244 // Set up the target and compiler
245 //
246
247 Target *target = exe_ctx.target;
248
249 if (!target)
250 {
251 error_stream.PutCString ("error: invalid target\n");
252 return false;
253 }
254
Sean Callanan65dafa82010-08-27 01:01:44 +0000255 //////////////////////////
256 // Parse the expression
257 //
258
Sean Callanana91dd992010-11-19 02:52:21 +0000259 m_desired_type = desired_type;
260
Sean Callanan6a925532011-01-13 08:53:35 +0000261 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +0000262
Sean Callanan166ba102011-08-01 18:18:33 +0000263 if (!m_expr_decl_map->WillParse(exe_ctx))
264 {
265 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
266 return false;
267 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000268
Greg Clayton395fc332011-02-15 21:59:32 +0000269 ClangExpressionParser parser(exe_ctx.process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000270
271 unsigned num_errors = parser.Parse (error_stream);
272
273 if (num_errors)
274 {
275 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000276
277 m_expr_decl_map->DidParse();
278
Sean Callanan65dafa82010-08-27 01:01:44 +0000279 return false;
280 }
281
282 ///////////////////////////////////////////////
283 // Convert the output of the parser to DWARF
284 //
285
286 m_dwarf_opcodes.reset(new StreamString);
Greg Claytoncd548032011-02-01 01:31:41 +0000287 m_dwarf_opcodes->SetByteOrder (lldb::endian::InlHostByteOrder());
Sean Callanan65dafa82010-08-27 01:01:44 +0000288 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
289
Greg Clayton427f2902010-12-14 02:59:59 +0000290 m_local_variables.reset(new ClangExpressionVariableList());
Sean Callanan65dafa82010-08-27 01:01:44 +0000291
292 Error dwarf_error = parser.MakeDWARF ();
293
294 if (dwarf_error.Success())
295 {
296 if (log)
297 log->Printf("Code can be interpreted.");
298
Sean Callananaa301c42010-12-03 01:38:59 +0000299 m_expr_decl_map->DidParse();
300
Sean Callanan65dafa82010-08-27 01:01:44 +0000301 return true;
302 }
303
304 //////////////////////////////////
305 // JIT the output of the parser
306 //
307
308 m_dwarf_opcodes.reset();
309
Sean Callananc0492742011-05-23 21:40:23 +0000310 m_data_allocator.reset(new ProcessDataAllocator(*exe_ctx.process));
311
312 Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, m_data_allocator.get(), m_const_result, true);
313
314 if (log)
315 {
316 StreamString dump_string;
317 m_data_allocator->Dump(dump_string);
318
319 log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
320 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000321
Sean Callananaa301c42010-12-03 01:38:59 +0000322 m_expr_decl_map->DidParse();
323
Sean Callanan65dafa82010-08-27 01:01:44 +0000324 if (jit_error.Success())
325 {
Greg Claytond0882d02011-01-19 23:00:49 +0000326 if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
327 m_jit_process_sp = exe_ctx.process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000328 return true;
329 }
330 else
331 {
Greg Clayton30581972011-05-17 03:51:29 +0000332 const char *error_cstr = jit_error.AsCString();
333 if (error_cstr && error_cstr[0])
334 error_stream.Printf ("error: %s\n", error_cstr);
335 else
336 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
Sean Callanan65dafa82010-08-27 01:01:44 +0000337 return false;
338 }
339}
340
341bool
Jim Inghamd1686902010-10-14 23:45:03 +0000342ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000343 ExecutionContext &exe_ctx,
344 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000345 lldb::addr_t &object_ptr,
346 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000347{
Greg Claytone005f2c2010-11-06 01:53:30 +0000348 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000349
Greg Claytond0882d02011-01-19 23:00:49 +0000350 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000351 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000352 Error materialize_error;
353
Sean Callanan3aa7da52010-12-13 22:46:15 +0000354 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000355 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000356 ConstString object_name;
357
358 if (m_cplusplus)
359 {
360 object_name.SetCString("this");
361 }
362 else if (m_objectivec)
363 {
364 object_name.SetCString("self");
365 }
366 else
367 {
368 error_stream.Printf("Need object pointer but don't know the language\n");
369 return false;
370 }
371
372 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
373 {
374 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
375 return false;
376 }
Sean Callanan047923c2010-12-14 00:42:36 +0000377
378 if (m_objectivec)
379 {
380 ConstString cmd_name("_cmd");
381
382 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
383 {
384 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
385 return false;
386 }
387 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000388 }
389
Sean Callananaa301c42010-12-03 01:38:59 +0000390 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000391 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000392 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000393 return false;
394 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000395
396#if 0
397 // jingham: look here
398 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000399 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 +0000400#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000401
402 if (log)
403 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000404 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000405
Greg Claytond0882d02011-01-19 23:00:49 +0000406 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000407
408 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000409 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000410
Sean Callanan33711022010-12-07 10:00:20 +0000411 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000412
413 StreamString args;
414
415 Error dump_error;
416
Sean Callanane8a59a82010-09-13 21:34:21 +0000417 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000418 {
Sean Callananaa301c42010-12-03 01:38:59 +0000419 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000420 {
Sean Callanan33711022010-12-07 10:00:20 +0000421 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000422 }
423 else
424 {
Sean Callanan33711022010-12-07 10:00:20 +0000425 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000426 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000427 }
428 }
Jim Inghamd1686902010-10-14 23:45:03 +0000429 }
430 return true;
431}
432
433ThreadPlan *
434ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000435 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000436{
437 lldb::addr_t struct_address;
438
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000439 lldb::addr_t object_ptr = 0;
440 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000441
Sean Callanan047923c2010-12-14 00:42:36 +0000442 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000443
Jim Ingham360f53f2010-11-30 02:22:11 +0000444 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
445 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000446 // forcing unwind_on_error to be true here, in practical terms that can't happen.
447
Jim Inghamd1686902010-10-14 23:45:03 +0000448 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000449 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000450 struct_address,
451 error_stream,
452 true,
453 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000454 (m_needs_object_ptr ? &object_ptr : NULL),
455 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000456}
457
458bool
459ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
460 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000461 lldb::ClangExpressionVariableSP &result,
462 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000463{
464 Error expr_error;
465
Sean Callanan33711022010-12-07 10:00:20 +0000466 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
467
468 if (log)
469 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000470 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000471
472 StreamString args;
473
474 Error dump_error;
475
476 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
477 {
478 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
479 }
480 else
481 {
482 log->Printf(" Structure contents:\n%s", args.GetData());
483 }
484 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000485
486 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
487
Sean Callanan33711022010-12-07 10:00:20 +0000488
Sean Callanan0ddf8062011-05-09 22:04:36 +0000489 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000490 {
491 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
492 return false;
493 }
494 return true;
495}
496
Greg Claytonb3448432011-03-24 21:19:54 +0000497ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000498ClangUserExpression::Execute (Stream &error_stream,
499 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000500 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000501 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000502 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000503{
Jim Ingham7812e012011-01-18 22:20:08 +0000504 // The expression log is quite verbose, and if you're just tracking the execution of the
505 // expression, it's quite convenient to have these logs come out with the STEP log as well.
506 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000507
Jim Inghamd1686902010-10-14 23:45:03 +0000508 if (m_dwarf_opcodes.get())
509 {
510 // TODO execute the JITted opcodes
511
512 error_stream.Printf("We don't currently support executing DWARF expressions");
513
Greg Claytonb3448432011-03-24 21:19:54 +0000514 return eExecutionSetupError;
Jim Inghamd1686902010-10-14 23:45:03 +0000515 }
Greg Claytond0882d02011-01-19 23:00:49 +0000516 else if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000517 {
518 lldb::addr_t struct_address;
519
Johnny Chen2bc9eb32011-07-19 19:48:13 +0000520 lldb::addr_t object_ptr = 0;
521 lldb::addr_t cmd_ptr = 0;
Jim Inghamd1686902010-10-14 23:45:03 +0000522
Sean Callanan047923c2010-12-14 00:42:36 +0000523 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000524 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000525
Jim Inghamea9d4262010-11-05 19:25:48 +0000526 const bool stop_others = true;
527 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000528
Greg Claytond0882d02011-01-19 23:00:49 +0000529 Address wrapper_address (NULL, m_jit_start_addr);
Sean Callanan3aa7da52010-12-13 22:46:15 +0000530 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread),
531 wrapper_address,
532 struct_address,
533 stop_others,
534 discard_on_error,
535 (m_needs_object_ptr ? &object_ptr : NULL),
536 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
537 shared_ptr_to_me));
538
Jim Ingham360f53f2010-11-30 02:22:11 +0000539 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000540 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000541
542 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000543
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000544 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000545
Greg Clayton61468e82011-01-19 07:54:15 +0000546 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000547
548 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000549 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000550
Greg Claytonb3448432011-03-24 21:19:54 +0000551 ExecutionResults execution_result = exe_ctx.process->RunThreadPlan (exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000552 call_plan_sp,
553 stop_others,
554 try_all_threads,
555 discard_on_error,
556 single_thread_timeout_usec,
557 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000558
559 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000560 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000561
Greg Claytonb3448432011-03-24 21:19:54 +0000562 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000563 {
Jim Ingham2370a972011-05-17 01:10:11 +0000564 const char *error_desc = NULL;
565
566 if (call_plan_sp)
567 {
568 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
569 if (real_stop_info_sp)
570 error_desc = real_stop_info_sp->GetDescription();
571 }
572 if (error_desc)
573 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000574 else
Jim Ingham2370a972011-05-17 01:10:11 +0000575 error_stream.Printf ("Execution was interrupted.", error_desc);
576
577 if (discard_on_error)
578 error_stream.Printf ("\nThe process has been returned to the state before execution.");
579 else
580 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000581
582 return execution_result;
583 }
Greg Claytonb3448432011-03-24 21:19:54 +0000584 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000585 {
586 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
587 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000588 }
589
Sean Callanan0ddf8062011-05-09 22:04:36 +0000590 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000591 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000592 else
Greg Claytonb3448432011-03-24 21:19:54 +0000593 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000594 }
595 else
596 {
Johnny Chencb395442010-11-10 19:02:11 +0000597 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Greg Claytonb3448432011-03-24 21:19:54 +0000598 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000599 }
600}
601
602StreamString &
603ClangUserExpression::DwarfOpcodeStream ()
604{
605 if (!m_dwarf_opcodes.get())
606 m_dwarf_opcodes.reset(new StreamString());
607
608 return *m_dwarf_opcodes.get();
609}
Greg Clayton377e0b42010-10-05 00:31:29 +0000610
Greg Claytonb3448432011-03-24 21:19:54 +0000611ExecutionResults
Sean Callanan77e93942010-10-29 00:29:03 +0000612ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000613 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000614 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000615 const char *expr_prefix,
616 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000617{
Jim Inghamec07c0d2011-08-09 00:00:49 +0000618 Error error;
619 return EvaluateWithError (exe_ctx, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
620}
621
622ExecutionResults
623ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
624 bool discard_on_error,
625 const char *expr_cstr,
626 const char *expr_prefix,
627 lldb::ValueObjectSP &result_valobj_sp,
628 Error &error)
629{
Jim Ingham7812e012011-01-18 22:20:08 +0000630 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000631
Greg Claytonb3448432011-03-24 21:19:54 +0000632 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000633
Jim Inghame6bd1422011-06-20 17:32:44 +0000634 if (exe_ctx.process == NULL || exe_ctx.process->GetState() != lldb::eStateStopped)
Jim Ingham360f53f2010-11-30 02:22:11 +0000635 {
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000636 error.SetErrorString ("must have a stopped process to evaluate expressions.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000637
Jim Ingham47da8102011-04-22 23:53:53 +0000638 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb3448432011-03-24 21:19:54 +0000639 return eExecutionSetupError;
Jim Ingham360f53f2010-11-30 02:22:11 +0000640 }
641
Greg Clayton0baa3942010-11-04 01:54:29 +0000642 if (!exe_ctx.process->GetDynamicCheckers())
643 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000644 if (log)
645 log->Printf("== [ClangUserExpression::Evaluate] Installing dynamic checkers ==");
646
Greg Clayton0baa3942010-11-04 01:54:29 +0000647 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
648
649 StreamString install_errors;
650
651 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callananf7731452010-11-05 00:57:06 +0000652 {
653 if (install_errors.GetString().empty())
654 error.SetErrorString ("couldn't install checkers, unknown error");
655 else
656 error.SetErrorString (install_errors.GetString().c_str());
657
Jim Ingham47da8102011-04-22 23:53:53 +0000658 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb3448432011-03-24 21:19:54 +0000659 return eExecutionSetupError;
Sean Callananf7731452010-11-05 00:57:06 +0000660 }
661
Greg Clayton0baa3942010-11-04 01:54:29 +0000662 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
Sean Callanan94d255f2010-12-07 22:55:01 +0000663
664 if (log)
665 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Greg Clayton0baa3942010-11-04 01:54:29 +0000666 }
667
Jim Ingham360f53f2010-11-30 02:22:11 +0000668 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
669
Greg Clayton377e0b42010-10-05 00:31:29 +0000670 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000671
Sean Callanan94d255f2010-12-07 22:55:01 +0000672 if (log)
673 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
674
Sean Callanan696cf5f2011-05-07 01:06:41 +0000675 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), true))
Greg Clayton377e0b42010-10-05 00:31:29 +0000676 {
677 if (error_stream.GetString().empty())
678 error.SetErrorString ("expression failed to parse, unknown error");
679 else
680 error.SetErrorString (error_stream.GetString().c_str());
681 }
682 else
683 {
Greg Clayton427f2902010-12-14 02:59:59 +0000684 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000685
Sean Callanan696cf5f2011-05-07 01:06:41 +0000686 if (user_expression_sp->m_const_result.get())
Greg Clayton377e0b42010-10-05 00:31:29 +0000687 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000688 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000689 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000690
Sean Callanan696cf5f2011-05-07 01:06:41 +0000691 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
Jim Inghamec07c0d2011-08-09 00:00:49 +0000692 execution_results = eExecutionCompleted;
Greg Clayton377e0b42010-10-05 00:31:29 +0000693 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000694 else
695 {
696 error_stream.GetString().clear();
697
698 if (log)
699 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
700
701 execution_results = user_expression_sp->Execute (error_stream,
702 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000703 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000704 user_expression_sp,
705 expr_result);
706
Greg Claytonb3448432011-03-24 21:19:54 +0000707 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000708 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000709 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000710 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
711
712 if (error_stream.GetString().empty())
713 error.SetErrorString ("expression failed to execute, unknown error");
714 else
715 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000716 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000717 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000718 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000719 if (expr_result)
720 {
721 result_valobj_sp = expr_result->GetValueObject();
722
723 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000724 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000725 }
726 else
727 {
728 if (log)
729 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
730
Sean Callanan24312442011-08-23 21:20:51 +0000731 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000732 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000733 }
734 }
735 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000736
Greg Claytond1719722010-10-05 03:13:51 +0000737 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000738 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000739
Jim Ingham360f53f2010-11-30 02:22:11 +0000740 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000741}