blob: 0386e94e3315a5da99d86287716b1eda5b33e717 [file] [log] [blame]
Sean Callanan65dafa82010-08-27 01:01:44 +00001//===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===//
2//
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
40using namespace lldb_private;
41
Sean Callanan77e93942010-10-29 00:29:03 +000042ClangUserExpression::ClangUserExpression (const char *expr,
43 const char *expr_prefix) :
Greg Claytond0882d02011-01-19 23:00:49 +000044 ClangExpression (),
45 m_expr_text (expr),
46 m_expr_prefix (expr_prefix ? expr_prefix : ""),
47 m_transformed_text (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000048 m_desired_type (NULL, NULL),
Greg Claytond0882d02011-01-19 23:00:49 +000049 m_cplusplus (false),
50 m_objectivec (false),
51 m_needs_object_ptr (false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000052 m_const_object (false),
53 m_const_result ()
Sean Callanan65dafa82010-08-27 01:01:44 +000054{
Sean Callanan65dafa82010-08-27 01:01:44 +000055}
56
Sean Callanan830a9032010-08-27 23:31:21 +000057ClangUserExpression::~ClangUserExpression ()
58{
59}
60
Sean Callanan65dafa82010-08-27 01:01:44 +000061clang::ASTConsumer *
62ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
63{
Sean Callanana91dd992010-11-19 02:52:21 +000064 return new ASTResultSynthesizer(passthrough,
65 m_desired_type);
Sean Callanan65dafa82010-08-27 01:01:44 +000066}
67
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000068void
69ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
70{
71 if (!exe_ctx.frame)
72 return;
73
74 VariableList *vars = exe_ctx.frame->GetVariableList(false);
75
76 if (!vars)
77 return;
78
Sean Callanane8e55572010-12-01 21:35:54 +000079 lldb::VariableSP this_var(vars->FindVariable(ConstString("this")));
80 lldb::VariableSP self_var(vars->FindVariable(ConstString("self")));
81
82 if (this_var.get())
83 {
84 Type *this_type = this_var->GetType();
85
86 lldb::clang_type_t pointer_target_type;
87
Greg Clayton04c9c7b2011-02-16 23:00:21 +000088 if (ClangASTContext::IsPointerType(this_type->GetClangForwardType(),
Sean Callanane8e55572010-12-01 21:35:54 +000089 &pointer_target_type))
90 {
91 TypeFromUser target_ast_type(pointer_target_type, this_type->GetClangAST());
92
Greg Clayton04c9c7b2011-02-16 23:00:21 +000093 if (ClangASTContext::IsCXXClassType(target_ast_type.GetOpaqueQualType()))
Sean Callanan14404f82011-01-24 08:11:45 +000094 {
Sean Callanane8e55572010-12-01 21:35:54 +000095 m_cplusplus = true;
96
Sean Callanan14404f82011-01-24 08:11:45 +000097 if (target_ast_type.IsConst())
98 m_const_object = true;
99 }
Sean Callanane8e55572010-12-01 21:35:54 +0000100 }
101 }
102 else if (self_var.get())
103 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000104 m_objectivec = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000105 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000106}
107
Sean Callanan550f2762010-10-22 23:25:16 +0000108// This is a really nasty hack, meant to fix Objective-C expressions of the form
109// (int)[myArray count]. Right now, because the type information for count is
110// not available, [myArray count] returns id, which can't be directly cast to
111// int without causing a clang error.
112static void
113ApplyObjcCastHack(std::string &expr)
114{
115#define OBJC_CAST_HACK_FROM "(int)["
116#define OBJC_CAST_HACK_TO "(int)(long long)["
117
118 size_t from_offset;
119
120 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
121 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
122
123#undef OBJC_CAST_HACK_TO
124#undef OBJC_CAST_HACK_FROM
125}
126
Sean Callanan30892372010-10-24 20:45:49 +0000127// Another hack, meant to allow use of unichar despite it not being available in
128// the type information. Although we could special-case it in type lookup,
129// hopefully we'll figure out a way to #include the same environment as is
130// present in the original source file rather than try to hack specific type
131// definitions in as needed.
132static void
133ApplyUnicharHack(std::string &expr)
134{
135#define UNICHAR_HACK_FROM "unichar"
136#define UNICHAR_HACK_TO "unsigned short"
137
138 size_t from_offset;
139
140 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
141 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
142
143#undef UNICHAR_HACK_TO
144#undef UNICHAR_HACK_FROM
145}
146
Sean Callanan550f2762010-10-22 23:25:16 +0000147bool
Sean Callanana91dd992010-11-19 02:52:21 +0000148ClangUserExpression::Parse (Stream &error_stream,
149 ExecutionContext &exe_ctx,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000150 TypeFromUser desired_type,
Sean Callanan696cf5f2011-05-07 01:06:41 +0000151 bool keep_result_in_memory)
Sean Callanan65dafa82010-08-27 01:01:44 +0000152{
Greg Claytone005f2c2010-11-06 01:53:30 +0000153 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000154
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000155 ScanContext(exe_ctx);
156
157 StreamString m_transformed_stream;
158
159 ////////////////////////////////////
160 // Generate the expression
161 //
Sean Callanan550f2762010-10-22 23:25:16 +0000162
163 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000164 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000165
166 if (m_cplusplus)
167 {
Sean Callanan77e93942010-10-29 00:29:03 +0000168 m_transformed_stream.Printf("%s \n"
169 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000170 "void \n"
Sean Callanane8e55572010-12-01 21:35:54 +0000171 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000172 "{ \n"
173 " %s; \n"
174 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000175 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000176 FunctionName(),
Sean Callanane8e55572010-12-01 21:35:54 +0000177 (m_const_object ? "const" : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000178 m_expr_text.c_str());
179
180 m_needs_object_ptr = true;
181 }
Sean Callanan3aa7da52010-12-13 22:46:15 +0000182 else if(m_objectivec)
183 {
184 const char *function_name = FunctionName();
185
186 m_transformed_stream.Printf("%s \n"
Greg Claytonc5157ec2010-12-17 02:26:24 +0000187 "typedef unsigned short unichar; \n"
Sean Callanan3aa7da52010-12-13 22:46:15 +0000188 "@interface $__lldb_objc_class ($__lldb_category) \n"
189 "-(void)%s:(void *)$__lldb_arg; \n"
190 "@end \n"
191 "@implementation $__lldb_objc_class ($__lldb_category) \n"
192 "-(void)%s:(void *)$__lldb_arg \n"
193 "{ \n"
194 " %s; \n"
195 "} \n"
196 "@end \n",
197 m_expr_prefix.c_str(),
198 function_name,
199 function_name,
200 m_expr_text.c_str());
201
202 m_needs_object_ptr = true;
203 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000204 else
205 {
Sean Callanan77e93942010-10-29 00:29:03 +0000206 m_transformed_stream.Printf("%s \n"
207 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000208 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000209 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000210 "{ \n"
211 " %s; \n"
212 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000213 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000214 FunctionName(),
215 m_expr_text.c_str());
216 }
217
218 m_transformed_text = m_transformed_stream.GetData();
219
220
221 if (log)
222 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
223
Sean Callanan65dafa82010-08-27 01:01:44 +0000224 ////////////////////////////////////
225 // Set up the target and compiler
226 //
227
228 Target *target = exe_ctx.target;
229
230 if (!target)
231 {
232 error_stream.PutCString ("error: invalid target\n");
233 return false;
234 }
235
Sean Callanan65dafa82010-08-27 01:01:44 +0000236 //////////////////////////
237 // Parse the expression
238 //
239
Sean Callanana91dd992010-11-19 02:52:21 +0000240 m_desired_type = desired_type;
241
Sean Callanan6a925532011-01-13 08:53:35 +0000242 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory));
Sean Callananaa301c42010-12-03 01:38:59 +0000243
244 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000245
Greg Clayton395fc332011-02-15 21:59:32 +0000246 ClangExpressionParser parser(exe_ctx.process, *this);
Sean Callanan65dafa82010-08-27 01:01:44 +0000247
248 unsigned num_errors = parser.Parse (error_stream);
249
250 if (num_errors)
251 {
252 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000253
254 m_expr_decl_map->DidParse();
255
Sean Callanan65dafa82010-08-27 01:01:44 +0000256 return false;
257 }
258
259 ///////////////////////////////////////////////
260 // Convert the output of the parser to DWARF
261 //
262
263 m_dwarf_opcodes.reset(new StreamString);
Greg Claytoncd548032011-02-01 01:31:41 +0000264 m_dwarf_opcodes->SetByteOrder (lldb::endian::InlHostByteOrder());
Sean Callanan65dafa82010-08-27 01:01:44 +0000265 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
266
Greg Clayton427f2902010-12-14 02:59:59 +0000267 m_local_variables.reset(new ClangExpressionVariableList());
Sean Callanan65dafa82010-08-27 01:01:44 +0000268
269 Error dwarf_error = parser.MakeDWARF ();
270
271 if (dwarf_error.Success())
272 {
273 if (log)
274 log->Printf("Code can be interpreted.");
275
Sean Callananaa301c42010-12-03 01:38:59 +0000276 m_expr_decl_map->DidParse();
277
Sean Callanan65dafa82010-08-27 01:01:44 +0000278 return true;
279 }
280
281 //////////////////////////////////
282 // JIT the output of the parser
283 //
284
285 m_dwarf_opcodes.reset();
286
Sean Callanan696cf5f2011-05-07 01:06:41 +0000287 Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, m_const_result, true);
Sean Callanan65dafa82010-08-27 01:01:44 +0000288
Sean Callananaa301c42010-12-03 01:38:59 +0000289 m_expr_decl_map->DidParse();
290
Sean Callanan65dafa82010-08-27 01:01:44 +0000291 if (jit_error.Success())
292 {
Greg Claytond0882d02011-01-19 23:00:49 +0000293 if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
294 m_jit_process_sp = exe_ctx.process->GetSP();
Sean Callanan65dafa82010-08-27 01:01:44 +0000295 return true;
296 }
297 else
298 {
Greg Clayton30581972011-05-17 03:51:29 +0000299 const char *error_cstr = jit_error.AsCString();
300 if (error_cstr && error_cstr[0])
301 error_stream.Printf ("error: %s\n", error_cstr);
302 else
303 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
Sean Callanan65dafa82010-08-27 01:01:44 +0000304 return false;
305 }
306}
307
308bool
Jim Inghamd1686902010-10-14 23:45:03 +0000309ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000310 ExecutionContext &exe_ctx,
311 lldb::addr_t &struct_address,
Sean Callanan047923c2010-12-14 00:42:36 +0000312 lldb::addr_t &object_ptr,
313 lldb::addr_t &cmd_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000314{
Greg Claytone005f2c2010-11-06 01:53:30 +0000315 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000316
Greg Claytond0882d02011-01-19 23:00:49 +0000317 if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000318 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000319 Error materialize_error;
320
Sean Callanan3aa7da52010-12-13 22:46:15 +0000321 if (m_needs_object_ptr)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000322 {
Sean Callanan3aa7da52010-12-13 22:46:15 +0000323 ConstString object_name;
324
325 if (m_cplusplus)
326 {
327 object_name.SetCString("this");
328 }
329 else if (m_objectivec)
330 {
331 object_name.SetCString("self");
332 }
333 else
334 {
335 error_stream.Printf("Need object pointer but don't know the language\n");
336 return false;
337 }
338
339 if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
340 {
341 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
342 return false;
343 }
Sean Callanan047923c2010-12-14 00:42:36 +0000344
345 if (m_objectivec)
346 {
347 ConstString cmd_name("_cmd");
348
349 if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
350 {
351 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
352 return false;
353 }
354 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000355 }
356
Sean Callananaa301c42010-12-03 01:38:59 +0000357 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000358 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000359 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000360 return false;
361 }
Greg Claytonc71899e2011-01-18 19:36:39 +0000362
363#if 0
364 // jingham: look here
365 StreamFile logfile ("/tmp/exprs.txt", "a");
Greg Claytond0882d02011-01-19 23:00:49 +0000366 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 +0000367#endif
Sean Callanan65dafa82010-08-27 01:01:44 +0000368
369 if (log)
370 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000371 log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000372
Greg Claytond0882d02011-01-19 23:00:49 +0000373 log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000374
375 if (m_needs_object_ptr)
Sean Callanan33711022010-12-07 10:00:20 +0000376 log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000377
Sean Callanan33711022010-12-07 10:00:20 +0000378 log->Printf(" Structure address : 0x%llx", (uint64_t)struct_address);
Sean Callanan65dafa82010-08-27 01:01:44 +0000379
380 StreamString args;
381
382 Error dump_error;
383
Sean Callanane8a59a82010-09-13 21:34:21 +0000384 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000385 {
Sean Callananaa301c42010-12-03 01:38:59 +0000386 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000387 {
Sean Callanan33711022010-12-07 10:00:20 +0000388 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
Sean Callanane8a59a82010-09-13 21:34:21 +0000389 }
390 else
391 {
Sean Callanan33711022010-12-07 10:00:20 +0000392 log->Printf(" Structure contents:\n%s", args.GetData());
Sean Callanane8a59a82010-09-13 21:34:21 +0000393 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000394 }
395 }
Jim Inghamd1686902010-10-14 23:45:03 +0000396 }
397 return true;
398}
399
400ThreadPlan *
401ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
Sean Callanan6a925532011-01-13 08:53:35 +0000402 ExecutionContext &exe_ctx)
Jim Inghamd1686902010-10-14 23:45:03 +0000403{
404 lldb::addr_t struct_address;
405
406 lldb::addr_t object_ptr = NULL;
Sean Callanan3aa7da52010-12-13 22:46:15 +0000407 lldb::addr_t cmd_ptr = NULL;
Jim Inghamd1686902010-10-14 23:45:03 +0000408
Sean Callanan047923c2010-12-14 00:42:36 +0000409 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
Jim Inghamd1686902010-10-14 23:45:03 +0000410
Jim Ingham360f53f2010-11-30 02:22:11 +0000411 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
412 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
Sean Callanan3aa7da52010-12-13 22:46:15 +0000413 // forcing unwind_on_error to be true here, in practical terms that can't happen.
414
Jim Inghamd1686902010-10-14 23:45:03 +0000415 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytond0882d02011-01-19 23:00:49 +0000416 m_jit_start_addr,
Sean Callanana65b5272010-12-01 01:28:23 +0000417 struct_address,
418 error_stream,
419 true,
420 true,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000421 (m_needs_object_ptr ? &object_ptr : NULL),
422 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
Jim Inghamd1686902010-10-14 23:45:03 +0000423}
424
425bool
426ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
427 ExecutionContext &exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000428 lldb::ClangExpressionVariableSP &result,
429 lldb::addr_t function_stack_pointer)
Jim Inghamd1686902010-10-14 23:45:03 +0000430{
431 Error expr_error;
432
Sean Callanan33711022010-12-07 10:00:20 +0000433 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
434
435 if (log)
436 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000437 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
Sean Callanan33711022010-12-07 10:00:20 +0000438
439 StreamString args;
440
441 Error dump_error;
442
443 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
444 {
445 log->Printf(" Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
446 }
447 else
448 {
449 log->Printf(" Structure contents:\n%s", args.GetData());
450 }
451 }
Sean Callanan0ddf8062011-05-09 22:04:36 +0000452
453 lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
454
Sean Callanan33711022010-12-07 10:00:20 +0000455
Sean Callanan0ddf8062011-05-09 22:04:36 +0000456 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000457 {
458 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
459 return false;
460 }
461 return true;
462}
463
Greg Claytonb3448432011-03-24 21:19:54 +0000464ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000465ClangUserExpression::Execute (Stream &error_stream,
466 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000467 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000468 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Greg Clayton427f2902010-12-14 02:59:59 +0000469 lldb::ClangExpressionVariableSP &result)
Jim Inghamd1686902010-10-14 23:45:03 +0000470{
Jim Ingham7812e012011-01-18 22:20:08 +0000471 // The expression log is quite verbose, and if you're just tracking the execution of the
472 // expression, it's quite convenient to have these logs come out with the STEP log as well.
473 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan33711022010-12-07 10:00:20 +0000474
Jim Inghamd1686902010-10-14 23:45:03 +0000475 if (m_dwarf_opcodes.get())
476 {
477 // TODO execute the JITted opcodes
478
479 error_stream.Printf("We don't currently support executing DWARF expressions");
480
Greg Claytonb3448432011-03-24 21:19:54 +0000481 return eExecutionSetupError;
Jim Inghamd1686902010-10-14 23:45:03 +0000482 }
Greg Claytond0882d02011-01-19 23:00:49 +0000483 else if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
Jim Inghamd1686902010-10-14 23:45:03 +0000484 {
485 lldb::addr_t struct_address;
486
487 lldb::addr_t object_ptr = NULL;
Sean Callanan3aa7da52010-12-13 22:46:15 +0000488 lldb::addr_t cmd_ptr = NULL;
Jim Inghamd1686902010-10-14 23:45:03 +0000489
Sean Callanan047923c2010-12-14 00:42:36 +0000490 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
Greg Claytonb3448432011-03-24 21:19:54 +0000491 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000492
Jim Inghamea9d4262010-11-05 19:25:48 +0000493 const bool stop_others = true;
494 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000495
Greg Claytond0882d02011-01-19 23:00:49 +0000496 Address wrapper_address (NULL, m_jit_start_addr);
Sean Callanan3aa7da52010-12-13 22:46:15 +0000497 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread),
498 wrapper_address,
499 struct_address,
500 stop_others,
501 discard_on_error,
502 (m_needs_object_ptr ? &object_ptr : NULL),
503 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
504 shared_ptr_to_me));
505
Jim Ingham360f53f2010-11-30 02:22:11 +0000506 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
Greg Claytonb3448432011-03-24 21:19:54 +0000507 return eExecutionSetupError;
Sean Callanan0ddf8062011-05-09 22:04:36 +0000508
509 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
Jim Ingham360f53f2010-11-30 02:22:11 +0000510
Jim Ingham5ab7fba2011-05-17 22:24:54 +0000511 call_plan_sp->SetPrivate(true);
Jim Ingham360f53f2010-11-30 02:22:11 +0000512
Greg Clayton61468e82011-01-19 07:54:15 +0000513 uint32_t single_thread_timeout_usec = 500000;
Sean Callanan33711022010-12-07 10:00:20 +0000514
515 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000516 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
Sean Callanan33711022010-12-07 10:00:20 +0000517
Greg Claytonb3448432011-03-24 21:19:54 +0000518 ExecutionResults execution_result = exe_ctx.process->RunThreadPlan (exe_ctx,
Sean Callanan0ddf8062011-05-09 22:04:36 +0000519 call_plan_sp,
520 stop_others,
521 try_all_threads,
522 discard_on_error,
523 single_thread_timeout_usec,
524 error_stream);
Sean Callanan33711022010-12-07 10:00:20 +0000525
526 if (log)
Sean Callanan94d255f2010-12-07 22:55:01 +0000527 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
Jim Ingham360f53f2010-11-30 02:22:11 +0000528
Greg Claytonb3448432011-03-24 21:19:54 +0000529 if (execution_result == eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000530 {
Jim Ingham2370a972011-05-17 01:10:11 +0000531 const char *error_desc = NULL;
532
533 if (call_plan_sp)
534 {
535 lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
536 if (real_stop_info_sp)
537 error_desc = real_stop_info_sp->GetDescription();
538 }
539 if (error_desc)
540 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
Jim Ingham360f53f2010-11-30 02:22:11 +0000541 else
Jim Ingham2370a972011-05-17 01:10:11 +0000542 error_stream.Printf ("Execution was interrupted.", error_desc);
543
544 if (discard_on_error)
545 error_stream.Printf ("\nThe process has been returned to the state before execution.");
546 else
547 error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
Jim Ingham360f53f2010-11-30 02:22:11 +0000548
549 return execution_result;
550 }
Greg Claytonb3448432011-03-24 21:19:54 +0000551 else if (execution_result != eExecutionCompleted)
Jim Ingham360f53f2010-11-30 02:22:11 +0000552 {
553 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
554 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000555 }
556
Sean Callanan0ddf8062011-05-09 22:04:36 +0000557 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
Greg Claytonb3448432011-03-24 21:19:54 +0000558 return eExecutionCompleted;
Jim Ingham360f53f2010-11-30 02:22:11 +0000559 else
Greg Claytonb3448432011-03-24 21:19:54 +0000560 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000561 }
562 else
563 {
Johnny Chencb395442010-11-10 19:02:11 +0000564 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Greg Claytonb3448432011-03-24 21:19:54 +0000565 return eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000566 }
567}
568
569StreamString &
570ClangUserExpression::DwarfOpcodeStream ()
571{
572 if (!m_dwarf_opcodes.get())
573 m_dwarf_opcodes.reset(new StreamString());
574
575 return *m_dwarf_opcodes.get();
576}
Greg Clayton377e0b42010-10-05 00:31:29 +0000577
Greg Claytonb3448432011-03-24 21:19:54 +0000578ExecutionResults
Sean Callanan77e93942010-10-29 00:29:03 +0000579ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000580 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000581 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000582 const char *expr_prefix,
583 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000584{
Jim Ingham7812e012011-01-18 22:20:08 +0000585 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callanan94d255f2010-12-07 22:55:01 +0000586
Greg Clayton377e0b42010-10-05 00:31:29 +0000587 Error error;
Greg Claytonb3448432011-03-24 21:19:54 +0000588 ExecutionResults execution_results = eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000589
590 if (exe_ctx.process == NULL)
Jim Ingham360f53f2010-11-30 02:22:11 +0000591 {
592 error.SetErrorString ("Must have a process to evaluate expressions.");
593
Jim Ingham47da8102011-04-22 23:53:53 +0000594 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb3448432011-03-24 21:19:54 +0000595 return eExecutionSetupError;
Jim Ingham360f53f2010-11-30 02:22:11 +0000596 }
597
Greg Clayton0baa3942010-11-04 01:54:29 +0000598 if (!exe_ctx.process->GetDynamicCheckers())
599 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000600 if (log)
601 log->Printf("== [ClangUserExpression::Evaluate] Installing dynamic checkers ==");
602
Greg Clayton0baa3942010-11-04 01:54:29 +0000603 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
604
605 StreamString install_errors;
606
607 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callananf7731452010-11-05 00:57:06 +0000608 {
609 if (install_errors.GetString().empty())
610 error.SetErrorString ("couldn't install checkers, unknown error");
611 else
612 error.SetErrorString (install_errors.GetString().c_str());
613
Jim Ingham47da8102011-04-22 23:53:53 +0000614 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytonb3448432011-03-24 21:19:54 +0000615 return eExecutionSetupError;
Sean Callananf7731452010-11-05 00:57:06 +0000616 }
617
Greg Clayton0baa3942010-11-04 01:54:29 +0000618 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
Sean Callanan94d255f2010-12-07 22:55:01 +0000619
620 if (log)
621 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Greg Clayton0baa3942010-11-04 01:54:29 +0000622 }
623
Jim Ingham360f53f2010-11-30 02:22:11 +0000624 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
625
Greg Clayton377e0b42010-10-05 00:31:29 +0000626 StreamString error_stream;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000627
Sean Callanan94d255f2010-12-07 22:55:01 +0000628 if (log)
629 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
630
Sean Callanan696cf5f2011-05-07 01:06:41 +0000631 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), true))
Greg Clayton377e0b42010-10-05 00:31:29 +0000632 {
633 if (error_stream.GetString().empty())
634 error.SetErrorString ("expression failed to parse, unknown error");
635 else
636 error.SetErrorString (error_stream.GetString().c_str());
637 }
638 else
639 {
Greg Clayton427f2902010-12-14 02:59:59 +0000640 lldb::ClangExpressionVariableSP expr_result;
Greg Clayton377e0b42010-10-05 00:31:29 +0000641
Sean Callanan696cf5f2011-05-07 01:06:41 +0000642 if (user_expression_sp->m_const_result.get())
Greg Clayton377e0b42010-10-05 00:31:29 +0000643 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000644 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000645 log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
Sean Callanan94d255f2010-12-07 22:55:01 +0000646
Sean Callanan696cf5f2011-05-07 01:06:41 +0000647 result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
Greg Clayton377e0b42010-10-05 00:31:29 +0000648 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000649 else
650 {
651 error_stream.GetString().clear();
652
653 if (log)
654 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
655
656 execution_results = user_expression_sp->Execute (error_stream,
657 exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000658 discard_on_error,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000659 user_expression_sp,
660 expr_result);
661
Greg Claytonb3448432011-03-24 21:19:54 +0000662 if (execution_results != eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000663 {
Sean Callanan94d255f2010-12-07 22:55:01 +0000664 if (log)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000665 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
666
667 if (error_stream.GetString().empty())
668 error.SetErrorString ("expression failed to execute, unknown error");
669 else
670 error.SetErrorString (error_stream.GetString().c_str());
Greg Clayton377e0b42010-10-05 00:31:29 +0000671 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000672 else
Greg Clayton377e0b42010-10-05 00:31:29 +0000673 {
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000674 if (expr_result)
675 {
676 result_valobj_sp = expr_result->GetValueObject();
677
678 if (log)
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000679 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000680 }
681 else
682 {
683 if (log)
684 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
685
686 error.SetErrorString ("Expression did not return a result");
687 }
Greg Clayton377e0b42010-10-05 00:31:29 +0000688 }
689 }
690 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000691
Greg Claytond1719722010-10-05 03:13:51 +0000692 if (result_valobj_sp.get() == NULL)
Jim Ingham47da8102011-04-22 23:53:53 +0000693 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
Greg Claytond1719722010-10-05 03:13:51 +0000694
Jim Ingham360f53f2010-11-30 02:22:11 +0000695 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000696}