Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/RecordLayout.h" |
Greg Clayton | c4f5110 | 2010-07-02 18:39:06 +0000 | [diff] [blame] | 16 | #include "clang/CodeGen/CodeGenAction.h" |
| 17 | #include "clang/CodeGen/ModuleBuilder.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Greg Clayton | c4f5110 | 2010-07-02 18:39:06 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | // Project includes |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/ASTStructExtractor.h" |
| 26 | #include "lldb/Expression/ClangExpressionParser.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangFunction.h" |
| 28 | #include "lldb/Symbol/Type.h" |
| 29 | #include "lldb/Core/DataExtractor.h" |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 30 | #include "lldb/Core/State.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Core/ValueObject.h" |
| 32 | #include "lldb/Core/ValueObjectList.h" |
| 33 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 34 | #include "lldb/Symbol/ClangASTContext.h" |
| 35 | #include "lldb/Symbol/Function.h" |
| 36 | #include "lldb/Target/ExecutionContext.h" |
| 37 | #include "lldb/Target/Process.h" |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 38 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 39 | #include "lldb/Target/StopInfo.h" |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | #include "lldb/Target/Thread.h" |
| 42 | #include "lldb/Target/ThreadPlan.h" |
| 43 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 44 | #include "lldb/Core/Log.h" |
| 45 | |
| 46 | using namespace lldb_private; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | //---------------------------------------------------------------------- |
| 49 | // ClangFunction constructor |
| 50 | //---------------------------------------------------------------------- |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 51 | ClangFunction::ClangFunction |
| 52 | ( |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 53 | ExecutionContextScope &exe_scope, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 54 | ClangASTContext *ast_context, |
| 55 | void *return_qualtype, |
| 56 | const Address& functionAddress, |
| 57 | const ValueList &arg_value_list |
| 58 | ) : |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | m_function_ptr (NULL), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 60 | m_function_addr (functionAddress), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | m_function_return_qual_type(return_qualtype), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 62 | m_clang_ast_context (ast_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | m_wrapper_function_name ("__lldb_caller_function"), |
| 64 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 65 | m_wrapper_args_addrs (), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 66 | m_arg_values (arg_value_list), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | m_compiled (false), |
| 68 | m_JITted (false) |
| 69 | { |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 70 | Process *process = exe_scope.CalculateProcess(); |
| 71 | // Can't make a ClangFunction without a process. |
| 72 | assert (process != NULL); |
| 73 | |
| 74 | m_jit_process_sp = process->GetSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 77 | ClangFunction::ClangFunction |
| 78 | ( |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 79 | ExecutionContextScope &exe_scope, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 80 | Function &function, |
| 81 | ClangASTContext *ast_context, |
| 82 | const ValueList &arg_value_list |
| 83 | ) : |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | m_function_ptr (&function), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 85 | m_function_addr (), |
| 86 | m_function_return_qual_type (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | m_clang_ast_context (ast_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | m_wrapper_function_name ("__lldb_function_caller"), |
| 89 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 90 | m_wrapper_args_addrs (), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 91 | m_arg_values (arg_value_list), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | m_compiled (false), |
| 93 | m_JITted (false) |
| 94 | { |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 95 | Process *process = exe_scope.CalculateProcess(); |
| 96 | // Can't make a ClangFunction without a process. |
| 97 | assert (process != NULL); |
| 98 | |
| 99 | m_jit_process_sp = process->GetSP(); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress(); |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 102 | m_function_return_qual_type = m_function_ptr->GetReturnClangType(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | //---------------------------------------------------------------------- |
| 106 | // Destructor |
| 107 | //---------------------------------------------------------------------- |
| 108 | ClangFunction::~ClangFunction() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | unsigned |
| 113 | ClangFunction::CompileFunction (Stream &errors) |
| 114 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 115 | if (m_compiled) |
| 116 | return 0; |
| 117 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 119 | unsigned num_errors = 0; |
| 120 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 121 | std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type); |
| 122 | |
| 123 | // Cons up the function we're going to wrap our call in, then compile it... |
| 124 | // We declare the function "extern "C"" because the compiler might be in C++ |
| 125 | // mode which would mangle the name and then we couldn't find it again... |
| 126 | m_wrapper_function_text.clear(); |
| 127 | m_wrapper_function_text.append ("extern \"C\" void "); |
| 128 | m_wrapper_function_text.append (m_wrapper_function_name); |
| 129 | m_wrapper_function_text.append (" (void *input)\n{\n struct "); |
| 130 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 131 | m_wrapper_function_text.append (" \n {\n"); |
| 132 | m_wrapper_function_text.append (" "); |
| 133 | m_wrapper_function_text.append (return_type_str); |
| 134 | m_wrapper_function_text.append (" (*fn_ptr) ("); |
| 135 | |
| 136 | // Get the number of arguments. If we have a function type and it is prototyped, |
| 137 | // trust that, otherwise use the values we were given. |
| 138 | |
| 139 | // FIXME: This will need to be extended to handle Variadic functions. We'll need |
| 140 | // to pull the defined arguments out of the function, then add the types from the |
| 141 | // arguments list for the variable arguments. |
| 142 | |
| 143 | uint32_t num_args = UINT32_MAX; |
| 144 | bool trust_function = false; |
| 145 | // GetArgumentCount returns -1 for an unprototyped function. |
| 146 | if (m_function_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 148 | int num_func_args = m_function_ptr->GetArgumentCount(); |
| 149 | if (num_func_args >= 0) |
| 150 | trust_function = true; |
| 151 | else |
| 152 | num_args = num_func_args; |
| 153 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 155 | if (num_args == UINT32_MAX) |
| 156 | num_args = m_arg_values.GetSize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 158 | std::string args_buffer; // This one stores the definition of all the args in "struct caller". |
| 159 | std::string args_list_buffer; // This one stores the argument list called from the structure. |
| 160 | for (size_t i = 0; i < num_args; i++) |
| 161 | { |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 162 | std::string type_name; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 164 | if (trust_function) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | { |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 166 | lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i); |
| 167 | type_name = ClangASTContext::GetTypeName(arg_clang_type); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 169 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 171 | Value *arg_value = m_arg_values.GetValueAtIndex(i); |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 172 | void *clang_qual_type = arg_value->GetClangType (); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 173 | if (clang_qual_type != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | { |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 175 | type_name = ClangASTContext::GetTypeName(clang_qual_type); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | } |
| 177 | else |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 178 | { |
| 179 | errors.Printf("Could not determine type of input value %d.", i); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | return 1; |
| 181 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 183 | |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 184 | m_wrapper_function_text.append (type_name); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 185 | if (i < num_args - 1) |
| 186 | m_wrapper_function_text.append (", "); |
| 187 | |
| 188 | char arg_buf[32]; |
| 189 | args_buffer.append (" "); |
Greg Clayton | 04c9c7b | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 190 | args_buffer.append (type_name); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 191 | snprintf(arg_buf, 31, "arg_%zd", i); |
| 192 | args_buffer.push_back (' '); |
| 193 | args_buffer.append (arg_buf); |
| 194 | args_buffer.append (";\n"); |
| 195 | |
| 196 | args_list_buffer.append ("__lldb_fn_data->"); |
| 197 | args_list_buffer.append (arg_buf); |
| 198 | if (i < num_args - 1) |
| 199 | args_list_buffer.append (", "); |
| 200 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 201 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 202 | m_wrapper_function_text.append (");\n"); // Close off the function calling prototype. |
| 203 | |
| 204 | m_wrapper_function_text.append (args_buffer); |
| 205 | |
| 206 | m_wrapper_function_text.append (" "); |
| 207 | m_wrapper_function_text.append (return_type_str); |
| 208 | m_wrapper_function_text.append (" return_value;"); |
| 209 | m_wrapper_function_text.append ("\n };\n struct "); |
| 210 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 211 | m_wrapper_function_text.append ("* __lldb_fn_data = (struct "); |
| 212 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 213 | m_wrapper_function_text.append (" *) input;\n"); |
| 214 | |
| 215 | m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); |
| 216 | m_wrapper_function_text.append (args_list_buffer); |
| 217 | m_wrapper_function_text.append (");\n}\n"); |
| 218 | |
Jim Ingham | 7812e01 | 2011-01-18 22:20:08 +0000 | [diff] [blame] | 219 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 220 | if (log) |
| 221 | log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str()); |
| 222 | |
| 223 | // Okay, now compile this expression |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 224 | |
| 225 | m_parser.reset(new ClangExpressionParser(m_jit_process_sp.get(), *this)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 226 | |
| 227 | num_errors = m_parser->Parse (errors); |
| 228 | |
| 229 | m_compiled = (num_errors == 0); |
| 230 | |
| 231 | if (!m_compiled) |
| 232 | return num_errors; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | |
| 234 | return num_errors; |
| 235 | } |
| 236 | |
| 237 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 238 | ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 240 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 242 | if (!process) |
| 243 | return false; |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 244 | |
| 245 | if (process != m_jit_process_sp.get()) |
| 246 | return false; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 247 | |
| 248 | if (!m_compiled) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | return false; |
| 250 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 251 | if (m_JITted) |
| 252 | return true; |
| 253 | |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 254 | lldb::ClangExpressionVariableSP const_result; |
| 255 | |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame^] | 256 | Error jit_error (m_parser->MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, NULL, const_result)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 257 | |
| 258 | if (!jit_error.Success()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | return false; |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 260 | if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS) |
| 261 | m_jit_process_sp = exe_ctx.process->GetSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 267 | ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 269 | return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function. |
| 273 | |
| 274 | bool |
Jim Ingham | 681778e | 2010-09-10 23:07:48 +0000 | [diff] [blame] | 275 | ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, |
| 276 | lldb::addr_t &args_addr_ref, |
| 277 | Address function_address, |
| 278 | ValueList &arg_values, |
| 279 | Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 281 | // All the information to reconstruct the struct is provided by the |
| 282 | // StructExtractor. |
| 283 | if (!m_struct_valid) |
| 284 | { |
| 285 | errors.Printf("Argument information was not correctly parsed, so the function cannot be called."); |
| 286 | return false; |
| 287 | } |
| 288 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | Error error; |
| 290 | using namespace clang; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 291 | ExecutionResults return_value = eExecutionSetupError; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 293 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | |
| 295 | if (process == NULL) |
| 296 | return return_value; |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 297 | |
| 298 | if (process != m_jit_process_sp.get()) |
| 299 | return false; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 302 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 303 | args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 304 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 305 | return false; |
| 306 | m_wrapper_args_addrs.push_back (args_addr_ref); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | // Make sure this is an address that we've already handed out. |
| 311 | if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end()) |
| 312 | { |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 317 | // TODO: verify fun_addr needs to be a callable address |
| 318 | Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.target)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 319 | int first_offset = m_member_offsets[0]; |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 320 | process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | |
| 322 | // FIXME: We will need to extend this for Variadic functions. |
| 323 | |
| 324 | Error value_error; |
| 325 | |
| 326 | size_t num_args = arg_values.GetSize(); |
| 327 | if (num_args != m_arg_values.GetSize()) |
| 328 | { |
| 329 | errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize()); |
| 330 | return false; |
| 331 | } |
| 332 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 333 | for (size_t i = 0; i < num_args; i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | { |
| 335 | // FIXME: We should sanity check sizes. |
| 336 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 337 | int offset = m_member_offsets[i+1]; // Clang sizes are in bytes. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 338 | Value *arg_value = arg_values.GetValueAtIndex(i); |
| 339 | |
| 340 | // FIXME: For now just do scalars: |
| 341 | |
| 342 | // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings) |
| 343 | |
| 344 | if (arg_value->GetValueType() == Value::eValueTypeHostAddress && |
Greg Clayton | 6916e35 | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 345 | arg_value->GetContextType() == Value::eContextTypeClangType && |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 346 | ClangASTContext::IsPointerType(arg_value->GetClangType())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 347 | continue; |
| 348 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 349 | const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 351 | if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error)) |
| 352 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 359 | ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | { |
| 361 | using namespace clang; |
| 362 | |
| 363 | if (CompileFunction(errors) != 0) |
| 364 | return false; |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 365 | if (!WriteFunctionWrapper(exe_ctx, errors)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | return false; |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 367 | if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | return false; |
| 369 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 370 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 371 | if (log) |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 372 | log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_jit_start_addr, args_addr_ref); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | ThreadPlan * |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 378 | ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, |
| 379 | lldb::addr_t func_addr, |
| 380 | lldb::addr_t &args_addr, |
| 381 | Stream &errors, |
| 382 | bool stop_others, |
| 383 | bool discard_on_error, |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 384 | lldb::addr_t *this_arg, |
| 385 | lldb::addr_t *cmd_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | { |
| 387 | // FIXME: Use the errors Stream for better error reporting. |
| 388 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 389 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | |
| 391 | if (process == NULL) |
| 392 | { |
| 393 | errors.Printf("Can't call a function without a process."); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | // Okay, now run the function: |
| 398 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 399 | Address wrapper_address (NULL, func_addr); |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 400 | ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread, |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 401 | wrapper_address, |
| 402 | args_addr, |
| 403 | stop_others, |
| 404 | discard_on_error, |
| 405 | this_arg, |
| 406 | cmd_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | return new_plan; |
| 408 | } |
| 409 | |
| 410 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 411 | ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 412 | { |
| 413 | // Read the return value - it is the last field in the struct: |
| 414 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 415 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 416 | Process *process = exe_ctx.process; |
Jim Ingham | 53bfd06 | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 417 | |
| 418 | if (process == NULL) |
| 419 | return false; |
| 420 | if (process != m_jit_process_sp.get()) |
| 421 | return false; |
| 422 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | Error error; |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 424 | ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 426 | if (error.Fail()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 427 | return false; |
| 428 | |
Greg Clayton | 6916e35 | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 429 | ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | ret_value.SetValueType(Value::eValueTypeScalar); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | |
| 434 | void |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 435 | ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 436 | { |
| 437 | std::list<lldb::addr_t>::iterator pos; |
| 438 | pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr); |
| 439 | if (pos != m_wrapper_args_addrs.end()) |
| 440 | m_wrapper_args_addrs.erase(pos); |
| 441 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 442 | exe_ctx.process->DeallocateMemory(args_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 445 | ExecutionResults |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 446 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 448 | return ExecuteFunction (exe_ctx, errors, 1000, true, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 451 | ExecutionResults |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 452 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | { |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 454 | const bool try_all_threads = false; |
| 455 | const bool discard_on_error = true; |
| 456 | return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 459 | ExecutionResults |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | ClangFunction::ExecuteFunction( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 461 | ExecutionContext &exe_ctx, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 462 | Stream &errors, |
| 463 | uint32_t single_thread_timeout_usec, |
| 464 | bool try_all_threads, |
| 465 | Value &results) |
| 466 | { |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 467 | const bool stop_others = true; |
| 468 | const bool discard_on_error = true; |
| 469 | return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec, |
| 470 | try_all_threads, discard_on_error, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 473 | // This is the static function |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 474 | ExecutionResults |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 475 | ClangFunction::ExecuteFunction ( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 476 | ExecutionContext &exe_ctx, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 477 | lldb::addr_t function_address, |
| 478 | lldb::addr_t &void_arg, |
| 479 | bool stop_others, |
| 480 | bool try_all_threads, |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 481 | bool discard_on_error, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 482 | uint32_t single_thread_timeout_usec, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 483 | Stream &errors, |
| 484 | lldb::addr_t *this_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | { |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 486 | lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg, |
| 487 | errors, stop_others, discard_on_error, |
| 488 | this_arg)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | if (call_plan_sp == NULL) |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 490 | return eExecutionSetupError; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | call_plan_sp->SetPrivate(true); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 493 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 494 | return exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error, |
| 495 | single_thread_timeout_usec, errors); |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 496 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 498 | ExecutionResults |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 499 | ClangFunction::ExecuteFunction( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 500 | ExecutionContext &exe_ctx, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 501 | lldb::addr_t *args_addr_ptr, |
| 502 | Stream &errors, |
| 503 | bool stop_others, |
| 504 | uint32_t single_thread_timeout_usec, |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 505 | bool try_all_threads, |
| 506 | bool discard_on_error, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 507 | Value &results) |
| 508 | { |
| 509 | using namespace clang; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 510 | ExecutionResults return_value = eExecutionSetupError; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 511 | |
| 512 | lldb::addr_t args_addr; |
| 513 | |
| 514 | if (args_addr_ptr != NULL) |
| 515 | args_addr = *args_addr_ptr; |
| 516 | else |
| 517 | args_addr = LLDB_INVALID_ADDRESS; |
| 518 | |
| 519 | if (CompileFunction(errors) != 0) |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 520 | return eExecutionSetupError; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 521 | |
| 522 | if (args_addr == LLDB_INVALID_ADDRESS) |
| 523 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 524 | if (!InsertFunction(exe_ctx, args_addr, errors)) |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 525 | return eExecutionSetupError; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 528 | return_value = ClangFunction::ExecuteFunction (exe_ctx, |
| 529 | m_jit_start_addr, |
| 530 | args_addr, |
| 531 | stop_others, |
| 532 | try_all_threads, |
| 533 | discard_on_error, |
| 534 | single_thread_timeout_usec, |
| 535 | errors); |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 536 | |
| 537 | if (args_addr_ptr != NULL) |
| 538 | *args_addr_ptr = args_addr; |
| 539 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 540 | if (return_value != eExecutionCompleted) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | return return_value; |
| 542 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 543 | FetchFunctionResults(exe_ctx, args_addr, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | |
| 545 | if (args_addr_ptr == NULL) |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 546 | DeallocateFunctionResults(exe_ctx, args_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 548 | return eExecutionCompleted; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 551 | clang::ASTConsumer * |
| 552 | ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 553 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 554 | return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | } |