blob: 5ada385e13ee7e1cc62a46b05f9e820092ad29ff [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ClangFunction.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
11// C Includes
12// C++ Includes
13// Other libraries and framework includes
Chris Lattner24943d22010-06-08 16:52:24 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
Greg Claytonc4f51102010-07-02 18:39:06 +000016#include "clang/CodeGen/CodeGenAction.h"
17#include "clang/CodeGen/ModuleBuilder.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "llvm/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
23// Project includes
Sean Callanan65dafa82010-08-27 01:01:44 +000024#include "lldb/Expression/ASTStructExtractor.h"
25#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Expression/ClangFunction.h"
27#include "lldb/Symbol/Type.h"
28#include "lldb/Core/DataExtractor.h"
29#include "lldb/Core/ValueObject.h"
30#include "lldb/Core/ValueObjectList.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
32#include "lldb/Symbol/ClangASTContext.h"
33#include "lldb/Symbol/Function.h"
34#include "lldb/Target/ExecutionContext.h"
35#include "lldb/Target/Process.h"
Sean Callanan841026f2010-07-23 00:16:21 +000036#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000037#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038#include "lldb/Target/Thread.h"
39#include "lldb/Target/ThreadPlan.h"
40#include "lldb/Target/ThreadPlanCallFunction.h"
41#include "lldb/Core/Log.h"
42
43using namespace lldb_private;
Sean Callanan65dafa82010-08-27 01:01:44 +000044
Chris Lattner24943d22010-06-08 16:52:24 +000045//----------------------------------------------------------------------
46// ClangFunction constructor
47//----------------------------------------------------------------------
48ClangFunction::ClangFunction(const char *target_triple, ClangASTContext *ast_context, void *return_qualtype, const Address& functionAddress, const ValueList &arg_value_list) :
Sean Callanan65dafa82010-08-27 01:01:44 +000049 m_target_triple (target_triple),
Chris Lattner24943d22010-06-08 16:52:24 +000050 m_function_ptr (NULL),
Greg Clayton54e7afa2010-07-09 20:39:50 +000051 m_function_addr (functionAddress),
Chris Lattner24943d22010-06-08 16:52:24 +000052 m_function_return_qual_type(return_qualtype),
Greg Clayton54e7afa2010-07-09 20:39:50 +000053 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_wrapper_function_name ("__lldb_caller_function"),
55 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000056 m_wrapper_function_addr (),
57 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000058 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000059 m_compiled (false),
60 m_JITted (false)
61{
62}
63
64ClangFunction::ClangFunction(const char *target_triple, Function &function, ClangASTContext *ast_context, const ValueList &arg_value_list) :
Sean Callanan65dafa82010-08-27 01:01:44 +000065 m_target_triple (target_triple),
Chris Lattner24943d22010-06-08 16:52:24 +000066 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000067 m_function_addr (),
68 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000070 m_wrapper_function_name ("__lldb_function_caller"),
71 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000072 m_wrapper_function_addr (),
73 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000074 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000075 m_compiled (false),
76 m_JITted (false)
77{
78 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
79 m_function_return_qual_type = m_function_ptr->GetReturnType().GetOpaqueClangQualType();
80}
81
82//----------------------------------------------------------------------
83// Destructor
84//----------------------------------------------------------------------
85ClangFunction::~ClangFunction()
86{
87}
88
89unsigned
90ClangFunction::CompileFunction (Stream &errors)
91{
Sean Callanan65dafa82010-08-27 01:01:44 +000092 if (m_compiled)
93 return 0;
94
Chris Lattner24943d22010-06-08 16:52:24 +000095 // FIXME: How does clang tell us there's no return value? We need to handle that case.
96 unsigned num_errors = 0;
97
Sean Callanan65dafa82010-08-27 01:01:44 +000098 std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type);
99
100 // Cons up the function we're going to wrap our call in, then compile it...
101 // We declare the function "extern "C"" because the compiler might be in C++
102 // mode which would mangle the name and then we couldn't find it again...
103 m_wrapper_function_text.clear();
104 m_wrapper_function_text.append ("extern \"C\" void ");
105 m_wrapper_function_text.append (m_wrapper_function_name);
106 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
107 m_wrapper_function_text.append (m_wrapper_struct_name);
108 m_wrapper_function_text.append (" \n {\n");
109 m_wrapper_function_text.append (" ");
110 m_wrapper_function_text.append (return_type_str);
111 m_wrapper_function_text.append (" (*fn_ptr) (");
112
113 // Get the number of arguments. If we have a function type and it is prototyped,
114 // trust that, otherwise use the values we were given.
115
116 // FIXME: This will need to be extended to handle Variadic functions. We'll need
117 // to pull the defined arguments out of the function, then add the types from the
118 // arguments list for the variable arguments.
119
120 uint32_t num_args = UINT32_MAX;
121 bool trust_function = false;
122 // GetArgumentCount returns -1 for an unprototyped function.
123 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000124 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000125 int num_func_args = m_function_ptr->GetArgumentCount();
126 if (num_func_args >= 0)
127 trust_function = true;
128 else
129 num_args = num_func_args;
130 }
Chris Lattner24943d22010-06-08 16:52:24 +0000131
Sean Callanan65dafa82010-08-27 01:01:44 +0000132 if (num_args == UINT32_MAX)
133 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000134
Sean Callanan65dafa82010-08-27 01:01:44 +0000135 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
136 std::string args_list_buffer; // This one stores the argument list called from the structure.
137 for (size_t i = 0; i < num_args; i++)
138 {
139 const char *type_string;
140 std::string type_stdstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000141
Sean Callanan65dafa82010-08-27 01:01:44 +0000142 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000143 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000144 type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000145 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000146 else
Chris Lattner24943d22010-06-08 16:52:24 +0000147 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000148 Value *arg_value = m_arg_values.GetValueAtIndex(i);
149 void *clang_qual_type = arg_value->GetOpaqueClangQualType ();
150 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000151 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000152 type_stdstr = ClangASTContext::GetTypeName(clang_qual_type);
153 type_string = type_stdstr.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +0000154 }
155 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000156 {
157 errors.Printf("Could not determine type of input value %d.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000158 return 1;
159 }
Chris Lattner24943d22010-06-08 16:52:24 +0000160 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000161
162 m_wrapper_function_text.append (type_string);
163 if (i < num_args - 1)
164 m_wrapper_function_text.append (", ");
165
166 char arg_buf[32];
167 args_buffer.append (" ");
168 args_buffer.append (type_string);
169 snprintf(arg_buf, 31, "arg_%zd", i);
170 args_buffer.push_back (' ');
171 args_buffer.append (arg_buf);
172 args_buffer.append (";\n");
173
174 args_list_buffer.append ("__lldb_fn_data->");
175 args_list_buffer.append (arg_buf);
176 if (i < num_args - 1)
177 args_list_buffer.append (", ");
178
Chris Lattner24943d22010-06-08 16:52:24 +0000179 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000180 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
181
182 m_wrapper_function_text.append (args_buffer);
183
184 m_wrapper_function_text.append (" ");
185 m_wrapper_function_text.append (return_type_str);
186 m_wrapper_function_text.append (" return_value;");
187 m_wrapper_function_text.append ("\n };\n struct ");
188 m_wrapper_function_text.append (m_wrapper_struct_name);
189 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
190 m_wrapper_function_text.append (m_wrapper_struct_name);
191 m_wrapper_function_text.append (" *) input;\n");
192
193 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
194 m_wrapper_function_text.append (args_list_buffer);
195 m_wrapper_function_text.append (");\n}\n");
196
197 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
198 if (log)
199 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
200
201 // Okay, now compile this expression
202
203 m_parser.reset(new ClangExpressionParser(m_target_triple.c_str(), *this));
204
205 num_errors = m_parser->Parse (errors);
206
207 m_compiled = (num_errors == 0);
208
209 if (!m_compiled)
210 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000211
212 return num_errors;
213}
214
215bool
Sean Callananc78d6482010-07-26 22:14:36 +0000216ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000217{
Sean Callananc78d6482010-07-26 22:14:36 +0000218 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000219
Sean Callanan65dafa82010-08-27 01:01:44 +0000220 if (!process)
221 return false;
222
223 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000224 return false;
225
Sean Callanan65dafa82010-08-27 01:01:44 +0000226 if (m_JITted)
227 return true;
228
Sean Callanan830a9032010-08-27 23:31:21 +0000229 lldb::addr_t wrapper_function_end;
230
231 Error jit_error = m_parser->MakeJIT(m_wrapper_function_addr, wrapper_function_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000232
233 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000234 return false;
235
236 return true;
237}
238
239bool
Sean Callananc78d6482010-07-26 22:14:36 +0000240ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000241{
Sean Callananc78d6482010-07-26 22:14:36 +0000242 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000243}
244
245// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
246
247bool
Sean Callananc78d6482010-07-26 22:14:36 +0000248ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Address function_address, ValueList &arg_values, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000249{
Sean Callanan65dafa82010-08-27 01:01:44 +0000250 // All the information to reconstruct the struct is provided by the
251 // StructExtractor.
252 if (!m_struct_valid)
253 {
254 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
255 return false;
256 }
257
Chris Lattner24943d22010-06-08 16:52:24 +0000258 Error error;
259 using namespace clang;
260 ExecutionResults return_value = eExecutionSetupError;
261
Sean Callananc78d6482010-07-26 22:14:36 +0000262 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000263
264 if (process == NULL)
265 return return_value;
Sean Callanan65dafa82010-08-27 01:01:44 +0000266
Chris Lattner24943d22010-06-08 16:52:24 +0000267 if (args_addr_ref == LLDB_INVALID_ADDRESS)
268 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000269 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000270 if (args_addr_ref == LLDB_INVALID_ADDRESS)
271 return false;
272 m_wrapper_args_addrs.push_back (args_addr_ref);
273 }
274 else
275 {
276 // Make sure this is an address that we've already handed out.
277 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
278 {
279 return false;
280 }
281 }
282
283 // FIXME: This is fake, and just assumes that it matches that architecture.
284 // Make a data extractor and put the address into the right byte order & size.
285
Sean Callananc78d6482010-07-26 22:14:36 +0000286 uint64_t fun_addr = function_address.GetLoadAddress(exe_ctx.process);
Sean Callanan65dafa82010-08-27 01:01:44 +0000287 int first_offset = m_member_offsets[0];
Chris Lattner24943d22010-06-08 16:52:24 +0000288 process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error);
289
290 // FIXME: We will need to extend this for Variadic functions.
291
292 Error value_error;
293
294 size_t num_args = arg_values.GetSize();
295 if (num_args != m_arg_values.GetSize())
296 {
297 errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize());
298 return false;
299 }
300
Greg Clayton54e7afa2010-07-09 20:39:50 +0000301 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000302 {
303 // FIXME: We should sanity check sizes.
304
Sean Callanan65dafa82010-08-27 01:01:44 +0000305 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000306 Value *arg_value = arg_values.GetValueAtIndex(i);
307
308 // FIXME: For now just do scalars:
309
310 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
311
312 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
313 arg_value->GetContextType() == Value::eContextTypeOpaqueClangQualType &&
314 ClangASTContext::IsPointerType(arg_value->GetValueOpaqueClangQualType()))
315 continue;
316
Sean Callananc78d6482010-07-26 22:14:36 +0000317 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000318
319 int byte_size = arg_scalar.GetByteSize();
320 std::vector<uint8_t> buffer;
321 buffer.resize(byte_size);
322 DataExtractor value_data;
323 arg_scalar.GetData (value_data);
Greg Clayton53d68e72010-07-20 22:52:08 +0000324 value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), &buffer.front());
325 process->WriteMemory(args_addr_ref + offset, &buffer.front(), byte_size, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000326 }
327
328 return true;
329}
330
331bool
Sean Callananc78d6482010-07-26 22:14:36 +0000332ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000333{
334 using namespace clang;
335
336 if (CompileFunction(errors) != 0)
337 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000338 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000339 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000340 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000341 return false;
342
343 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
344 if (log)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000345 log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_wrapper_function_addr, args_addr_ref);
Chris Lattner24943d22010-06-08 16:52:24 +0000346
347 return true;
348}
349
350ThreadPlan *
Sean Callananc78d6482010-07-26 22:14:36 +0000351ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, lldb::addr_t func_addr, lldb::addr_t &args_addr, Stream &errors, bool stop_others, bool discard_on_error)
Chris Lattner24943d22010-06-08 16:52:24 +0000352{
353 // FIXME: Use the errors Stream for better error reporting.
354
Sean Callananc78d6482010-07-26 22:14:36 +0000355 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000356
357 if (process == NULL)
358 {
359 errors.Printf("Can't call a function without a process.");
360 return NULL;
361 }
362
363 // Okay, now run the function:
364
Sean Callanan841026f2010-07-23 00:16:21 +0000365 Address wrapper_address (NULL, func_addr);
Sean Callananc78d6482010-07-26 22:14:36 +0000366 ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread,
Chris Lattner24943d22010-06-08 16:52:24 +0000367 wrapper_address,
368 args_addr,
369 stop_others, discard_on_error);
370 return new_plan;
371}
372
373bool
Sean Callananc78d6482010-07-26 22:14:36 +0000374ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000375{
376 // Read the return value - it is the last field in the struct:
377 // FIXME: How does clang tell us there's no return value? We need to handle that case.
378
379 std::vector<uint8_t> data_buffer;
380 data_buffer.resize(m_return_size);
Sean Callananc78d6482010-07-26 22:14:36 +0000381 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000382 Error error;
Sean Callanan65dafa82010-08-27 01:01:44 +0000383 size_t bytes_read = process->ReadMemory(args_addr + m_return_offset, &data_buffer.front(), m_return_size, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000384
385 if (bytes_read == 0)
386 {
387 return false;
388 }
389
390 if (bytes_read < m_return_size)
391 return false;
392
Greg Clayton53d68e72010-07-20 22:52:08 +0000393 DataExtractor data(&data_buffer.front(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000394 // FIXME: Assuming an integer scalar for now:
395
396 uint32_t offset = 0;
397 uint64_t return_integer = data.GetMaxU64(&offset, m_return_size);
398
399 ret_value.SetContext (Value::eContextTypeOpaqueClangQualType, m_function_return_qual_type);
400 ret_value.SetValueType(Value::eValueTypeScalar);
401 ret_value.GetScalar() = return_integer;
402 return true;
403}
404
405void
Sean Callananc78d6482010-07-26 22:14:36 +0000406ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000407{
408 std::list<lldb::addr_t>::iterator pos;
409 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
410 if (pos != m_wrapper_args_addrs.end())
411 m_wrapper_args_addrs.erase(pos);
412
Sean Callananc78d6482010-07-26 22:14:36 +0000413 exe_ctx.process->DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000414}
415
416ClangFunction::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000417ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000418{
Sean Callananc78d6482010-07-26 22:14:36 +0000419 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000420}
421
422ClangFunction::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000423ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000424{
Sean Callananc78d6482010-07-26 22:14:36 +0000425 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, false, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000426}
427
428ClangFunction::ExecutionResults
429ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000430 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000431 Stream &errors,
432 uint32_t single_thread_timeout_usec,
433 bool try_all_threads,
434 Value &results)
435{
Sean Callananc78d6482010-07-26 22:14:36 +0000436 return ExecuteFunction (exe_ctx, NULL, errors, true, single_thread_timeout_usec, try_all_threads, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000437}
438
Sean Callanan841026f2010-07-23 00:16:21 +0000439// This is the static function
440ClangFunction::ExecutionResults
441ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000442 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000443 lldb::addr_t function_address,
444 lldb::addr_t &void_arg,
445 bool stop_others,
446 bool try_all_threads,
447 uint32_t single_thread_timeout_usec,
448 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000449{
Sean Callananc78d6482010-07-26 22:14:36 +0000450 // Save this value for restoration of the execution context after we run
451 uint32_t tid = exe_ctx.thread->GetID();
452
Sean Callanan841026f2010-07-23 00:16:21 +0000453 ClangFunction::ExecutionResults return_value = eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000454
Sean Callananc78d6482010-07-26 22:14:36 +0000455 lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg, errors, stop_others, false));
Chris Lattner24943d22010-06-08 16:52:24 +0000456
457 ThreadPlanCallFunction *call_plan_ptr = static_cast<ThreadPlanCallFunction *> (call_plan_sp.get());
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459 if (call_plan_sp == NULL)
Sean Callanan841026f2010-07-23 00:16:21 +0000460 return eExecutionSetupError;
461
Sean Callanan65af7342010-09-08 20:04:08 +0000462//#define SINGLE_STEP_EXPRESSIONS
463
464#ifdef SINGLE_STEP_EXPRESSIONS
465 return eExecutionInterrupted;
466#else
Chris Lattner24943d22010-06-08 16:52:24 +0000467 call_plan_sp->SetPrivate(true);
Sean Callananc78d6482010-07-26 22:14:36 +0000468 exe_ctx.thread->QueueThreadPlan(call_plan_sp, true);
Sean Callanan65af7342010-09-08 20:04:08 +0000469#endif
Sean Callanan841026f2010-07-23 00:16:21 +0000470
Chris Lattner24943d22010-06-08 16:52:24 +0000471 // We need to call the function synchronously, so spin waiting for it to return.
472 // If we get interrupted while executing, we're going to lose our context, and
473 // won't be able to gather the result at this point.
Sean Callanan841026f2010-07-23 00:16:21 +0000474
Chris Lattner24943d22010-06-08 16:52:24 +0000475 TimeValue* timeout_ptr = NULL;
476 TimeValue real_timeout;
Sean Callanan841026f2010-07-23 00:16:21 +0000477
Chris Lattner24943d22010-06-08 16:52:24 +0000478 if (single_thread_timeout_usec != 0)
479 {
480 real_timeout = TimeValue::Now();
481 real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec);
482 timeout_ptr = &real_timeout;
483 }
Sean Callanan841026f2010-07-23 00:16:21 +0000484
Jim Inghama2890f42010-08-17 00:35:35 +0000485 Error resume_error = exe_ctx.process->Resume ();
486 if (!resume_error.Success())
487 {
488 errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString());
489 return eExecutionSetupError;
490 }
Sean Callanan841026f2010-07-23 00:16:21 +0000491
492 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
Chris Lattner24943d22010-06-08 16:52:24 +0000493
494 while (1)
495 {
496 lldb::EventSP event_sp;
Sean Callanan841026f2010-07-23 00:16:21 +0000497
Chris Lattner24943d22010-06-08 16:52:24 +0000498 // Now wait for the process to stop again:
499 // FIXME: Probably want a time out.
Sean Callananc78d6482010-07-26 22:14:36 +0000500 lldb::StateType stop_state = exe_ctx.process->WaitForStateChangedEvents (timeout_ptr, event_sp);
Sean Callanan841026f2010-07-23 00:16:21 +0000501
Chris Lattner24943d22010-06-08 16:52:24 +0000502 if (stop_state == lldb::eStateInvalid && timeout_ptr != NULL)
503 {
504 // Right now this is the only way to tell we've timed out...
505 // We should interrupt the process here...
506 // Not really sure what to do if Halt fails here...
Chris Lattner24943d22010-06-08 16:52:24 +0000507 if (log)
508 log->Printf ("Running function with timeout: %d timed out, trying with all threads enabled.", single_thread_timeout_usec);
Sean Callanan841026f2010-07-23 00:16:21 +0000509
Sean Callananc78d6482010-07-26 22:14:36 +0000510 if (exe_ctx.process->Halt().Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000511 {
512 timeout_ptr = NULL;
513
Sean Callananc78d6482010-07-26 22:14:36 +0000514 stop_state = exe_ctx.process->WaitForStateChangedEvents (timeout_ptr, event_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000515 if (stop_state == lldb::eStateInvalid)
516 {
517 errors.Printf ("Got an invalid stop state after halt.");
518 }
519 else if (stop_state != lldb::eStateStopped)
520 {
521 StreamString s;
522 event_sp->Dump (&s);
523
524 errors.Printf("Didn't get a stopped event after Halting the target, got: \"%s\"", s.GetData());
525 }
526
527 if (try_all_threads)
528 {
529 // Between the time that we got the timeout and the time we halted, but target
530 // might have actually completed the plan. If so, we're done.
Sean Callananc78d6482010-07-26 22:14:36 +0000531 if (exe_ctx.thread->IsThreadPlanDone (call_plan_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000532 {
533 return_value = eExecutionCompleted;
534 break;
535 }
Sean Callanan841026f2010-07-23 00:16:21 +0000536
Chris Lattner24943d22010-06-08 16:52:24 +0000537 call_plan_ptr->SetStopOthers (false);
Sean Callananc78d6482010-07-26 22:14:36 +0000538 exe_ctx.process->Resume();
Chris Lattner24943d22010-06-08 16:52:24 +0000539 continue;
540 }
541 else
542 return eExecutionInterrupted;
543 }
544 }
545 if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping)
546 continue;
Sean Callanan841026f2010-07-23 00:16:21 +0000547
Sean Callananc78d6482010-07-26 22:14:36 +0000548 if (exe_ctx.thread->IsThreadPlanDone (call_plan_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000549 {
550 return_value = eExecutionCompleted;
551 break;
552 }
Sean Callananc78d6482010-07-26 22:14:36 +0000553 else if (exe_ctx.thread->WasThreadPlanDiscarded (call_plan_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000554 {
555 return_value = eExecutionDiscarded;
556 break;
557 }
558 else
559 {
Sean Callanan841026f2010-07-23 00:16:21 +0000560 if (log)
561 {
562 StreamString s;
563 event_sp->Dump (&s);
564 StreamString ts;
565
566 const char *event_explanation;
567
568 do
569 {
570 const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get());
571
572 if (!event_data)
573 {
574 event_explanation = "<no event data>";
575 break;
576 }
577
578 Process *process = event_data->GetProcessSP().get();
579
580 if (!process)
581 {
582 event_explanation = "<no process>";
583 break;
584 }
585
586 ThreadList &thread_list = process->GetThreadList();
587
588 uint32_t num_threads = thread_list.GetSize();
589 uint32_t thread_index;
590
591 ts.Printf("<%u threads> ", num_threads);
592
593 for (thread_index = 0;
594 thread_index < num_threads;
595 ++thread_index)
596 {
597 Thread *thread = thread_list.GetThreadAtIndex(thread_index).get();
598
599 if (!thread)
600 {
601 ts.Printf("<?> ");
602 continue;
603 }
604
Sean Callanan841026f2010-07-23 00:16:21 +0000605 ts.Printf("<");
606 RegisterContext *register_context = thread->GetRegisterContext();
607
608 if (register_context)
609 ts.Printf("[ip 0x%llx] ", register_context->GetPC());
610 else
611 ts.Printf("[ip unknown] ");
612
Greg Clayton643ee732010-08-04 01:40:35 +0000613 StopInfo *stop_info = thread->GetStopInfo();
614 if (stop_info)
615 {
616 const char *stop_desc = stop_info->GetDescription();
617 if (stop_desc)
618 ts.PutCString (stop_desc);
619 }
Sean Callanan841026f2010-07-23 00:16:21 +0000620 ts.Printf(">");
621 }
622
623 event_explanation = ts.GetData();
624 } while (0);
625
626 log->Printf("Execution interrupted: %s %s", s.GetData(), event_explanation);
627 }
628
Chris Lattner24943d22010-06-08 16:52:24 +0000629 return_value = eExecutionInterrupted;
630 break;
631 }
Chris Lattner24943d22010-06-08 16:52:24 +0000632 }
Sean Callanan841026f2010-07-23 00:16:21 +0000633
Sean Callananc78d6482010-07-26 22:14:36 +0000634 // Thread we ran the function in may have gone away because we ran the target
635 // Check that it's still there.
636 exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByID(tid, true).get();
637 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get();
638
Sean Callanan841026f2010-07-23 00:16:21 +0000639 return return_value;
640}
Chris Lattner24943d22010-06-08 16:52:24 +0000641
Sean Callanan841026f2010-07-23 00:16:21 +0000642ClangFunction::ExecutionResults
643ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000644 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000645 lldb::addr_t *args_addr_ptr,
646 Stream &errors,
647 bool stop_others,
648 uint32_t single_thread_timeout_usec,
649 bool try_all_threads,
650 Value &results)
651{
652 using namespace clang;
653 ExecutionResults return_value = eExecutionSetupError;
654
655 lldb::addr_t args_addr;
656
657 if (args_addr_ptr != NULL)
658 args_addr = *args_addr_ptr;
659 else
660 args_addr = LLDB_INVALID_ADDRESS;
661
662 if (CompileFunction(errors) != 0)
663 return eExecutionSetupError;
664
665 if (args_addr == LLDB_INVALID_ADDRESS)
666 {
Sean Callananc78d6482010-07-26 22:14:36 +0000667 if (!InsertFunction(exe_ctx, args_addr, errors))
Sean Callanan841026f2010-07-23 00:16:21 +0000668 return eExecutionSetupError;
669 }
670
Sean Callananc78d6482010-07-26 22:14:36 +0000671 return_value = ClangFunction::ExecuteFunction(exe_ctx, m_wrapper_function_addr, args_addr, stop_others, try_all_threads, single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000672
673 if (args_addr_ptr != NULL)
674 *args_addr_ptr = args_addr;
675
Chris Lattner24943d22010-06-08 16:52:24 +0000676 if (return_value != eExecutionCompleted)
677 return return_value;
678
Sean Callananc78d6482010-07-26 22:14:36 +0000679 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000680
681 if (args_addr_ptr == NULL)
Sean Callananc78d6482010-07-26 22:14:36 +0000682 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000683
684 return eExecutionCompleted;
685}
686
Sean Callanan65dafa82010-08-27 01:01:44 +0000687clang::ASTConsumer *
688ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner24943d22010-06-08 16:52:24 +0000689{
Sean Callanan65dafa82010-08-27 01:01:44 +0000690 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner24943d22010-06-08 16:52:24 +0000691}