blob: 3b0a45a96a9cfa2c70e6d2ae583363e65e493196 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-06-08 16:52:24 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
Greg Claytond2d60ce2010-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"
Greg Clayton514487e2011-02-15 21:59:32 +000020#include "llvm/ADT/Triple.h"
Greg Claytond2d60ce2010-07-02 18:39:06 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000022#include "llvm/IR/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24// Project includes
Greg Clayton23f8c952014-03-24 23:10:19 +000025#include "lldb/Core/DataExtractor.h"
26#include "lldb/Core/Log.h"
27#include "lldb/Core/Module.h"
28#include "lldb/Core/State.h"
29#include "lldb/Core/ValueObject.h"
30#include "lldb/Core/ValueObjectList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000031#include "lldb/Expression/ASTStructExtractor.h"
32#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Expression/ClangFunction.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000034#include "lldb/Expression/IRExecutionUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Interpreter/CommandReturnObject.h"
36#include "lldb/Symbol/ClangASTContext.h"
37#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000038#include "lldb/Symbol/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/ExecutionContext.h"
40#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000041#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000042#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000048
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049//----------------------------------------------------------------------
50// ClangFunction constructor
51//----------------------------------------------------------------------
Greg Clayton7b462cc2010-10-15 22:48:33 +000052ClangFunction::ClangFunction
53(
Jim Ingham35944dd2011-03-17 20:02:56 +000054 ExecutionContextScope &exe_scope,
Greg Clayton57ee3062013-07-11 22:46:58 +000055 const ClangASTType &return_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +000056 const Address& functionAddress,
Jim Ingham23ef27c2014-04-22 01:42:22 +000057 const ValueList &arg_value_list,
58 const char *name
Greg Clayton7b462cc2010-10-15 22:48:33 +000059) :
Greg Clayton23f8c952014-03-24 23:10:19 +000060 m_parser(),
61 m_execution_unit_sp(),
62 m_jit_module_wp(),
Jim Ingham23ef27c2014-04-22 01:42:22 +000063 m_name (name ? name : "<unknown>"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064 m_function_ptr (NULL),
Greg Claytonc982c762010-07-09 20:39:50 +000065 m_function_addr (functionAddress),
Greg Clayton57ee3062013-07-11 22:46:58 +000066 m_function_return_type(return_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067 m_wrapper_function_name ("__lldb_caller_function"),
68 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000069 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000070 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 m_compiled (false),
72 m_JITted (false)
73{
Enrico Granatadfc88a02012-09-18 00:08:47 +000074 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000075 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000076 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
Greg Clayton7b462cc2010-10-15 22:48:33 +000079ClangFunction::ClangFunction
80(
Jim Ingham35944dd2011-03-17 20:02:56 +000081 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000082 Function &function,
83 ClangASTContext *ast_context,
Jim Ingham23ef27c2014-04-22 01:42:22 +000084 const ValueList &arg_value_list,
85 const char *name
Greg Clayton7b462cc2010-10-15 22:48:33 +000086) :
Jim Ingham23ef27c2014-04-22 01:42:22 +000087 m_name (name ? name : "<unknown>"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 m_function_ptr (&function),
Greg Claytonc982c762010-07-09 20:39:50 +000089 m_function_addr (),
Greg Clayton57ee3062013-07-11 22:46:58 +000090 m_function_return_type (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 m_wrapper_function_name ("__lldb_function_caller"),
92 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000093 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000094 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 m_compiled (false),
96 m_JITted (false)
97{
Greg Clayton23f8c952014-03-24 23:10:19 +000098 m_jit_process_wp = exe_scope.CalculateProcess();
Jim Ingham35944dd2011-03-17 20:02:56 +000099 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +0000100 assert (m_jit_process_wp.lock());
Greg Clayton514487e2011-02-15 21:59:32 +0000101
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +0000103 m_function_return_type = m_function_ptr->GetClangType().GetFunctionReturnType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
106//----------------------------------------------------------------------
107// Destructor
108//----------------------------------------------------------------------
109ClangFunction::~ClangFunction()
110{
Greg Clayton23f8c952014-03-24 23:10:19 +0000111 lldb::ProcessSP process_sp (m_jit_process_wp.lock());
112 if (process_sp)
113 {
114 lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
115 if (jit_module_sp)
116 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
120unsigned
121ClangFunction::CompileFunction (Stream &errors)
122{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000123 if (m_compiled)
124 return 0;
125
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 // FIXME: How does clang tell us there's no return value? We need to handle that case.
127 unsigned num_errors = 0;
128
Jim Inghamfb6fc0d2013-09-27 20:59:37 +0000129 std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000130
131 // Cons up the function we're going to wrap our call in, then compile it...
132 // We declare the function "extern "C"" because the compiler might be in C++
133 // mode which would mangle the name and then we couldn't find it again...
134 m_wrapper_function_text.clear();
135 m_wrapper_function_text.append ("extern \"C\" void ");
136 m_wrapper_function_text.append (m_wrapper_function_name);
137 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
138 m_wrapper_function_text.append (m_wrapper_struct_name);
139 m_wrapper_function_text.append (" \n {\n");
140 m_wrapper_function_text.append (" ");
141 m_wrapper_function_text.append (return_type_str);
142 m_wrapper_function_text.append (" (*fn_ptr) (");
143
144 // Get the number of arguments. If we have a function type and it is prototyped,
145 // trust that, otherwise use the values we were given.
146
147 // FIXME: This will need to be extended to handle Variadic functions. We'll need
148 // to pull the defined arguments out of the function, then add the types from the
149 // arguments list for the variable arguments.
150
Greg Clayton57ee3062013-07-11 22:46:58 +0000151 uint32_t num_args = UINT32_MAX;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000152 bool trust_function = false;
153 // GetArgumentCount returns -1 for an unprototyped function.
Greg Clayton57ee3062013-07-11 22:46:58 +0000154 ClangASTType function_clang_type;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000155 if (m_function_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000157 function_clang_type = m_function_ptr->GetClangType();
158 if (function_clang_type)
159 {
160 int num_func_args = function_clang_type.GetFunctionArgumentCount();
161 if (num_func_args >= 0)
162 {
163 trust_function = true;
164 num_args = num_func_args;
165 }
166 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000167 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
Sean Callanan1a8d4092010-08-27 01:01:44 +0000169 if (num_args == UINT32_MAX)
170 num_args = m_arg_values.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
Sean Callanan1a8d4092010-08-27 01:01:44 +0000172 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
173 std::string args_list_buffer; // This one stores the argument list called from the structure.
174 for (size_t i = 0; i < num_args; i++)
175 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000176 std::string type_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177
Sean Callanan1a8d4092010-08-27 01:01:44 +0000178 if (trust_function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000180 type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000182 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000184 ClangASTType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetClangType ();
185 if (clang_qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000187 type_name = clang_qual_type.GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 }
189 else
Sean Callanan1a8d4092010-08-27 01:01:44 +0000190 {
Greg Clayton6fea17e2014-03-03 19:15:20 +0000191 errors.Printf("Could not determine type of input value %" PRIu64 ".", (uint64_t)i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 return 1;
193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000195
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000196 m_wrapper_function_text.append (type_name);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000197 if (i < num_args - 1)
198 m_wrapper_function_text.append (", ");
199
200 char arg_buf[32];
201 args_buffer.append (" ");
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000202 args_buffer.append (type_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000203 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000204 args_buffer.push_back (' ');
205 args_buffer.append (arg_buf);
206 args_buffer.append (";\n");
207
208 args_list_buffer.append ("__lldb_fn_data->");
209 args_list_buffer.append (arg_buf);
210 if (i < num_args - 1)
211 args_list_buffer.append (", ");
212
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000214 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
215
216 m_wrapper_function_text.append (args_buffer);
217
218 m_wrapper_function_text.append (" ");
219 m_wrapper_function_text.append (return_type_str);
220 m_wrapper_function_text.append (" return_value;");
221 m_wrapper_function_text.append ("\n };\n struct ");
222 m_wrapper_function_text.append (m_wrapper_struct_name);
223 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
224 m_wrapper_function_text.append (m_wrapper_struct_name);
225 m_wrapper_function_text.append (" *) input;\n");
226
227 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
228 m_wrapper_function_text.append (args_list_buffer);
229 m_wrapper_function_text.append (");\n}\n");
230
Greg Clayton5160ce52013-03-27 23:08:40 +0000231 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000232 if (log)
233 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
234
235 // Okay, now compile this expression
Sean Callanan1a8d4092010-08-27 01:01:44 +0000236
Enrico Granatadfc88a02012-09-18 00:08:47 +0000237 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
238 if (jit_process_sp)
239 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000240 const bool generate_debug_info = true;
241 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, generate_debug_info));
Enrico Granatadfc88a02012-09-18 00:08:47 +0000242
243 num_errors = m_parser->Parse (errors);
244 }
245 else
246 {
247 errors.Printf("no process - unable to inject function");
248 num_errors = 1;
249 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000250
251 m_compiled = (num_errors == 0);
252
253 if (!m_compiled)
254 return num_errors;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
256 return num_errors;
257}
258
259bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000260ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261{
Greg Claytonc14ee322011-09-22 04:58:26 +0000262 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Sean Callanan1a8d4092010-08-27 01:01:44 +0000264 if (!process)
265 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000266
267 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
268
269 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000270 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000271
272 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 return false;
274
Sean Callanan1a8d4092010-08-27 01:01:44 +0000275 if (m_JITted)
276 return true;
Sean Callanan1582ee62013-04-18 22:06:33 +0000277
278 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000279
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000280 Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000281 m_jit_end_addr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000282 m_execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000283 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000284 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000285 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000286
287 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 return false;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000289
Greg Clayton23f8c952014-03-24 23:10:19 +0000290 if (m_parser->GetGenerateDebugInfo())
291 {
292 lldb::ModuleSP jit_module_sp ( m_execution_unit_sp->GetJITModule());
293
294 if (jit_module_sp)
295 {
296 ConstString const_func_name(FunctionName());
297 FileSpec jit_file;
298 jit_file.GetFilename() = const_func_name;
299 jit_module_sp->SetFileSpecAndObjectName (jit_file, ConstString());
300 m_jit_module_wp = jit_module_sp;
301 process->GetTarget().GetImages().Append(jit_module_sp);
302 }
303 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000304 if (process && m_jit_start_addr)
Greg Clayton23f8c952014-03-24 23:10:19 +0000305 m_jit_process_wp = process->shared_from_this();
Sean Callanand14fac12013-01-14 21:45:38 +0000306
307 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
309 return true;
310}
311
312bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000313ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314{
Sean Callanan138e74e2010-07-26 22:14:36 +0000315 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316}
317
318// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
319
320bool
Jim Inghambaae1682010-09-10 23:07:48 +0000321ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
322 lldb::addr_t &args_addr_ref,
323 Address function_address,
324 ValueList &arg_values,
325 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000327 // All the information to reconstruct the struct is provided by the
328 // StructExtractor.
329 if (!m_struct_valid)
330 {
331 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
332 return false;
333 }
334
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 Error error;
336 using namespace clang;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000337 lldb::ExpressionResults return_value = lldb::eExecutionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
Greg Claytonc14ee322011-09-22 04:58:26 +0000339 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340
341 if (process == NULL)
342 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000343
Enrico Granatadfc88a02012-09-18 00:08:47 +0000344 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
345
346 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000347 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000348
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 if (args_addr_ref == LLDB_INVALID_ADDRESS)
350 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000351 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 if (args_addr_ref == LLDB_INVALID_ADDRESS)
353 return false;
354 m_wrapper_args_addrs.push_back (args_addr_ref);
355 }
356 else
357 {
358 // Make sure this is an address that we've already handed out.
359 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
360 {
361 return false;
362 }
363 }
364
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000365 // TODO: verify fun_addr needs to be a callable address
Greg Claytonc14ee322011-09-22 04:58:26 +0000366 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Greg Claytonc7bece562013-01-25 18:06:21 +0000367 uint64_t first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000368 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369
370 // FIXME: We will need to extend this for Variadic functions.
371
372 Error value_error;
373
374 size_t num_args = arg_values.GetSize();
375 if (num_args != m_arg_values.GetSize())
376 {
Greg Clayton6fea17e2014-03-03 19:15:20 +0000377 errors.Printf ("Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "", (uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 return false;
379 }
380
Greg Claytonc982c762010-07-09 20:39:50 +0000381 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
383 // FIXME: We should sanity check sizes.
384
Greg Claytonc7bece562013-01-25 18:06:21 +0000385 uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 Value *arg_value = arg_values.GetValueAtIndex(i);
387
388 // FIXME: For now just do scalars:
389
390 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
391
392 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000393 arg_value->GetContextType() == Value::eContextTypeInvalid &&
394 arg_value->GetClangType().IsPointerType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 continue;
396
Greg Clayton57ee3062013-07-11 22:46:58 +0000397 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000399 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
400 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 }
402
403 return true;
404}
405
406bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000407ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408{
409 using namespace clang;
410
411 if (CompileFunction(errors) != 0)
412 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000413 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000415 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 return false;
417
Greg Clayton5160ce52013-03-27 23:08:40 +0000418 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000420 log->Printf ("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n", m_jit_start_addr, args_addr_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
422 return true;
423}
424
425ThreadPlan *
Jim Ingham399f1ca2010-11-05 19:25:48 +0000426ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
Sean Callanana464f3d2013-11-08 01:14:26 +0000427 lldb::addr_t args_addr,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000428 const EvaluateExpressionOptions &options,
429 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430{
Greg Clayton5160ce52013-03-27 23:08:40 +0000431 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000432
433 if (log)
Jim Ingham23ef27c2014-04-22 01:42:22 +0000434 log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function \"%s\" --", m_name.c_str());
Sean Callananf58b12d2013-03-06 19:57:25 +0000435
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000437 Thread *thread = exe_ctx.GetThreadPtr();
438 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000440 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 return NULL;
442 }
443
444 // Okay, now run the function:
445
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000446 Address wrapper_address (m_jit_start_addr);
Sean Callanana464f3d2013-11-08 01:14:26 +0000447
448 lldb::addr_t args = { args_addr };
449
Greg Claytonc14ee322011-09-22 04:58:26 +0000450 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000451 wrapper_address,
Jim Inghamef651602011-12-22 19:12:40 +0000452 ClangASTType(),
Sean Callanana464f3d2013-11-08 01:14:26 +0000453 args,
454 options);
Jim Ingham923886c2012-05-11 18:43:38 +0000455 new_plan->SetIsMasterPlan(true);
456 new_plan->SetOkayToDiscard (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 return new_plan;
458}
459
460bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000461ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462{
463 // Read the return value - it is the last field in the struct:
464 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Inghamef651602011-12-22 19:12:40 +0000465 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
466 // to fetch the value. That way we can fetch any values we need.
Sean Callananf58b12d2013-03-06 19:57:25 +0000467
Greg Clayton5160ce52013-03-27 23:08:40 +0000468 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000469
470 if (log)
Jim Ingham23ef27c2014-04-22 01:42:22 +0000471 log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results for \"%s\"--", m_name.c_str());
Sean Callananf58b12d2013-03-06 19:57:25 +0000472
Greg Claytonc14ee322011-09-22 04:58:26 +0000473 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000474
475 if (process == NULL)
476 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000477
478 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
479
480 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000481 return false;
482
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000484 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000486 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 return false;
488
Greg Clayton57ee3062013-07-11 22:46:58 +0000489 ret_value.SetClangType(m_function_return_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 return true;
492}
493
494void
Sean Callanan138e74e2010-07-26 22:14:36 +0000495ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496{
497 std::list<lldb::addr_t>::iterator pos;
498 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
499 if (pos != m_wrapper_args_addrs.end())
500 m_wrapper_args_addrs.erase(pos);
501
Greg Claytonc14ee322011-09-22 04:58:26 +0000502 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
Jim Ingham1624a2d2014-05-05 02:26:40 +0000505lldb::ExpressionResults
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000507 ExecutionContext &exe_ctx,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000508 lldb::addr_t *args_addr_ptr,
509 const EvaluateExpressionOptions &options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 Stream &errors,
Sean Callananebf77072010-07-23 00:16:21 +0000511 Value &results)
512{
513 using namespace clang;
Jim Ingham1624a2d2014-05-05 02:26:40 +0000514 lldb::ExpressionResults return_value = lldb::eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000515
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000516 // ClangFunction::ExecuteFunction execution is always just to get the result. Do make sure we ignore
517 // breakpoints, unwind on error, and don't try to debug it.
518 EvaluateExpressionOptions real_options = options;
519 real_options.SetDebug(false);
520 real_options.SetUnwindOnError(true);
521 real_options.SetIgnoreBreakpoints(true);
522
Sean Callananebf77072010-07-23 00:16:21 +0000523 lldb::addr_t args_addr;
524
525 if (args_addr_ptr != NULL)
526 args_addr = *args_addr_ptr;
527 else
528 args_addr = LLDB_INVALID_ADDRESS;
529
530 if (CompileFunction(errors) != 0)
Jim Ingham1624a2d2014-05-05 02:26:40 +0000531 return lldb::eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000532
533 if (args_addr == LLDB_INVALID_ADDRESS)
534 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000535 if (!InsertFunction(exe_ctx, args_addr, errors))
Jim Ingham1624a2d2014-05-05 02:26:40 +0000536 return lldb::eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000537 }
Sean Callananebf77072010-07-23 00:16:21 +0000538
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000539 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
540
541 if (log)
Jim Ingham23ef27c2014-04-22 01:42:22 +0000542 log->Printf("== [ClangFunction::ExecuteFunction] Executing function \"%s\" ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000543
544 lldb::ThreadPlanSP call_plan_sp (GetThreadPlanToCallFunction (exe_ctx,
545 args_addr,
546 real_options,
547 errors));
548 if (!call_plan_sp)
Jim Ingham1624a2d2014-05-05 02:26:40 +0000549 return lldb::eExecutionSetupError;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000550
Jim Ingham23b95f02014-03-12 23:43:15 +0000551 // We need to make sure we record the fact that we are running an expression here
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000552 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
553 if (exe_ctx.GetProcessPtr())
554 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
555
556 return_value = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
557 call_plan_sp,
558 real_options,
559 errors);
560
561 if (log)
562 {
Jim Ingham1624a2d2014-05-05 02:26:40 +0000563 if (return_value != lldb::eExecutionCompleted)
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000564 {
Jim Ingham23ef27c2014-04-22 01:42:22 +0000565 log->Printf("== [ClangFunction::ExecuteFunction] Execution of \"%s\" completed abnormally ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000566 }
567 else
568 {
Jim Ingham23ef27c2014-04-22 01:42:22 +0000569 log->Printf("== [ClangFunction::ExecuteFunction] Execution of \"%s\" completed normally ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000570 }
571 }
572
573 if (exe_ctx.GetProcessPtr())
574 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
575
Sean Callananebf77072010-07-23 00:16:21 +0000576 if (args_addr_ptr != NULL)
577 *args_addr_ptr = args_addr;
578
Jim Ingham1624a2d2014-05-05 02:26:40 +0000579 if (return_value != lldb::eExecutionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 return return_value;
581
Sean Callanan138e74e2010-07-26 22:14:36 +0000582 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583
584 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000585 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586
Jim Ingham1624a2d2014-05-05 02:26:40 +0000587 return lldb::eExecutionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Sean Callanan1a8d4092010-08-27 01:01:44 +0000590clang::ASTConsumer *
591ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592{
Sean Callanan394e36d2013-10-10 00:39:23 +0000593 m_struct_extractor.reset(new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this));
594
595 return m_struct_extractor.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596}