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" |
| 20 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/JIT.h" |
| 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" |
| 30 | #include "lldb/Core/ValueObject.h" |
| 31 | #include "lldb/Core/ValueObjectList.h" |
| 32 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 33 | #include "lldb/Symbol/ClangASTContext.h" |
| 34 | #include "lldb/Symbol/Function.h" |
| 35 | #include "lldb/Target/ExecutionContext.h" |
| 36 | #include "lldb/Target/Process.h" |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 37 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 38 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Target/Thread.h" |
| 40 | #include "lldb/Target/ThreadPlan.h" |
| 41 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 42 | #include "lldb/Core/Log.h" |
| 43 | |
| 44 | using namespace lldb_private; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | //---------------------------------------------------------------------- |
| 47 | // ClangFunction constructor |
| 48 | //---------------------------------------------------------------------- |
| 49 | ClangFunction::ClangFunction(const char *target_triple, ClangASTContext *ast_context, void *return_qualtype, const Address& functionAddress, const ValueList &arg_value_list) : |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 50 | m_target_triple (target_triple), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | m_function_ptr (NULL), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 52 | m_function_addr (functionAddress), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | m_function_return_qual_type(return_qualtype), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 54 | m_clang_ast_context (ast_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | m_wrapper_function_name ("__lldb_caller_function"), |
| 56 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 57 | m_wrapper_function_addr (), |
| 58 | m_wrapper_args_addrs (), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 59 | m_arg_values (arg_value_list), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | m_compiled (false), |
| 61 | m_JITted (false) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | ClangFunction::ClangFunction(const char *target_triple, Function &function, ClangASTContext *ast_context, const ValueList &arg_value_list) : |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 66 | m_target_triple (target_triple), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | m_function_ptr (&function), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 68 | m_function_addr (), |
| 69 | m_function_return_qual_type (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | m_clang_ast_context (ast_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | m_wrapper_function_name ("__lldb_function_caller"), |
| 72 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 73 | m_wrapper_function_addr (), |
| 74 | m_wrapper_args_addrs (), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 75 | m_arg_values (arg_value_list), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | m_compiled (false), |
| 77 | m_JITted (false) |
| 78 | { |
| 79 | m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress(); |
| 80 | m_function_return_qual_type = m_function_ptr->GetReturnType().GetOpaqueClangQualType(); |
| 81 | } |
| 82 | |
| 83 | //---------------------------------------------------------------------- |
| 84 | // Destructor |
| 85 | //---------------------------------------------------------------------- |
| 86 | ClangFunction::~ClangFunction() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | unsigned |
| 91 | ClangFunction::CompileFunction (Stream &errors) |
| 92 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 93 | if (m_compiled) |
| 94 | return 0; |
| 95 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 97 | unsigned num_errors = 0; |
| 98 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 99 | std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type); |
| 100 | |
| 101 | // Cons up the function we're going to wrap our call in, then compile it... |
| 102 | // We declare the function "extern "C"" because the compiler might be in C++ |
| 103 | // mode which would mangle the name and then we couldn't find it again... |
| 104 | m_wrapper_function_text.clear(); |
| 105 | m_wrapper_function_text.append ("extern \"C\" void "); |
| 106 | m_wrapper_function_text.append (m_wrapper_function_name); |
| 107 | m_wrapper_function_text.append (" (void *input)\n{\n struct "); |
| 108 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 109 | m_wrapper_function_text.append (" \n {\n"); |
| 110 | m_wrapper_function_text.append (" "); |
| 111 | m_wrapper_function_text.append (return_type_str); |
| 112 | m_wrapper_function_text.append (" (*fn_ptr) ("); |
| 113 | |
| 114 | // Get the number of arguments. If we have a function type and it is prototyped, |
| 115 | // trust that, otherwise use the values we were given. |
| 116 | |
| 117 | // FIXME: This will need to be extended to handle Variadic functions. We'll need |
| 118 | // to pull the defined arguments out of the function, then add the types from the |
| 119 | // arguments list for the variable arguments. |
| 120 | |
| 121 | uint32_t num_args = UINT32_MAX; |
| 122 | bool trust_function = false; |
| 123 | // GetArgumentCount returns -1 for an unprototyped function. |
| 124 | if (m_function_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 126 | int num_func_args = m_function_ptr->GetArgumentCount(); |
| 127 | if (num_func_args >= 0) |
| 128 | trust_function = true; |
| 129 | else |
| 130 | num_args = num_func_args; |
| 131 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 133 | if (num_args == UINT32_MAX) |
| 134 | num_args = m_arg_values.GetSize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 136 | std::string args_buffer; // This one stores the definition of all the args in "struct caller". |
| 137 | std::string args_list_buffer; // This one stores the argument list called from the structure. |
| 138 | for (size_t i = 0; i < num_args; i++) |
| 139 | { |
| 140 | const char *type_string; |
| 141 | std::string type_stdstr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 143 | if (trust_function) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 145 | type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 147 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 149 | Value *arg_value = m_arg_values.GetValueAtIndex(i); |
| 150 | void *clang_qual_type = arg_value->GetOpaqueClangQualType (); |
| 151 | if (clang_qual_type != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 153 | type_stdstr = ClangASTContext::GetTypeName(clang_qual_type); |
| 154 | type_string = type_stdstr.c_str(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | } |
| 156 | else |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 157 | { |
| 158 | errors.Printf("Could not determine type of input value %d.", i); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | return 1; |
| 160 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 162 | |
| 163 | m_wrapper_function_text.append (type_string); |
| 164 | if (i < num_args - 1) |
| 165 | m_wrapper_function_text.append (", "); |
| 166 | |
| 167 | char arg_buf[32]; |
| 168 | args_buffer.append (" "); |
| 169 | args_buffer.append (type_string); |
| 170 | snprintf(arg_buf, 31, "arg_%zd", i); |
| 171 | args_buffer.push_back (' '); |
| 172 | args_buffer.append (arg_buf); |
| 173 | args_buffer.append (";\n"); |
| 174 | |
| 175 | args_list_buffer.append ("__lldb_fn_data->"); |
| 176 | args_list_buffer.append (arg_buf); |
| 177 | if (i < num_args - 1) |
| 178 | args_list_buffer.append (", "); |
| 179 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 181 | m_wrapper_function_text.append (");\n"); // Close off the function calling prototype. |
| 182 | |
| 183 | m_wrapper_function_text.append (args_buffer); |
| 184 | |
| 185 | m_wrapper_function_text.append (" "); |
| 186 | m_wrapper_function_text.append (return_type_str); |
| 187 | m_wrapper_function_text.append (" return_value;"); |
| 188 | m_wrapper_function_text.append ("\n };\n struct "); |
| 189 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 190 | m_wrapper_function_text.append ("* __lldb_fn_data = (struct "); |
| 191 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 192 | m_wrapper_function_text.append (" *) input;\n"); |
| 193 | |
| 194 | m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); |
| 195 | m_wrapper_function_text.append (args_list_buffer); |
| 196 | m_wrapper_function_text.append (");\n}\n"); |
| 197 | |
| 198 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 199 | if (log) |
| 200 | log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str()); |
| 201 | |
| 202 | // Okay, now compile this expression |
| 203 | |
| 204 | m_parser.reset(new ClangExpressionParser(m_target_triple.c_str(), *this)); |
| 205 | |
| 206 | num_errors = m_parser->Parse (errors); |
| 207 | |
| 208 | m_compiled = (num_errors == 0); |
| 209 | |
| 210 | if (!m_compiled) |
| 211 | return num_errors; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | |
| 213 | return num_errors; |
| 214 | } |
| 215 | |
| 216 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 217 | ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 219 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 221 | if (!process) |
| 222 | return false; |
| 223 | |
| 224 | if (!m_compiled) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | return false; |
| 226 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 227 | if (m_JITted) |
| 228 | return true; |
| 229 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame^] | 230 | lldb::addr_t wrapper_function_end; |
| 231 | |
| 232 | Error jit_error = m_parser->MakeJIT(m_wrapper_function_addr, wrapper_function_end, exe_ctx); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 233 | |
| 234 | if (!jit_error.Success()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | return false; |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 241 | 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] | 242 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 243 | 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] | 244 | } |
| 245 | |
| 246 | // FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function. |
| 247 | |
| 248 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 249 | ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Address function_address, ValueList &arg_values, Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 251 | // All the information to reconstruct the struct is provided by the |
| 252 | // StructExtractor. |
| 253 | if (!m_struct_valid) |
| 254 | { |
| 255 | errors.Printf("Argument information was not correctly parsed, so the function cannot be called."); |
| 256 | return false; |
| 257 | } |
| 258 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | Error error; |
| 260 | using namespace clang; |
| 261 | ExecutionResults return_value = eExecutionSetupError; |
| 262 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 263 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | |
| 265 | if (process == NULL) |
| 266 | return return_value; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 269 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 270 | 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] | 271 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 272 | return false; |
| 273 | m_wrapper_args_addrs.push_back (args_addr_ref); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | // Make sure this is an address that we've already handed out. |
| 278 | if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end()) |
| 279 | { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // FIXME: This is fake, and just assumes that it matches that architecture. |
| 285 | // Make a data extractor and put the address into the right byte order & size. |
| 286 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 287 | uint64_t fun_addr = function_address.GetLoadAddress(exe_ctx.process); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 288 | int first_offset = m_member_offsets[0]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error); |
| 290 | |
| 291 | // FIXME: We will need to extend this for Variadic functions. |
| 292 | |
| 293 | Error value_error; |
| 294 | |
| 295 | size_t num_args = arg_values.GetSize(); |
| 296 | if (num_args != m_arg_values.GetSize()) |
| 297 | { |
| 298 | errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize()); |
| 299 | return false; |
| 300 | } |
| 301 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 302 | for (size_t i = 0; i < num_args; i++) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | { |
| 304 | // FIXME: We should sanity check sizes. |
| 305 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 306 | int offset = m_member_offsets[i+1]; // Clang sizes are in bytes. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | Value *arg_value = arg_values.GetValueAtIndex(i); |
| 308 | |
| 309 | // FIXME: For now just do scalars: |
| 310 | |
| 311 | // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings) |
| 312 | |
| 313 | if (arg_value->GetValueType() == Value::eValueTypeHostAddress && |
| 314 | arg_value->GetContextType() == Value::eContextTypeOpaqueClangQualType && |
| 315 | ClangASTContext::IsPointerType(arg_value->GetValueOpaqueClangQualType())) |
| 316 | continue; |
| 317 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 318 | 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] | 319 | |
| 320 | int byte_size = arg_scalar.GetByteSize(); |
| 321 | std::vector<uint8_t> buffer; |
| 322 | buffer.resize(byte_size); |
| 323 | DataExtractor value_data; |
| 324 | arg_scalar.GetData (value_data); |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 325 | value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), &buffer.front()); |
| 326 | process->WriteMemory(args_addr_ref + offset, &buffer.front(), byte_size, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 333 | 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] | 334 | { |
| 335 | using namespace clang; |
| 336 | |
| 337 | if (CompileFunction(errors) != 0) |
| 338 | return false; |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 339 | if (!WriteFunctionWrapper(exe_ctx, errors)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 340 | return false; |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 341 | if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 342 | return false; |
| 343 | |
| 344 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 345 | if (log) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 346 | log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_wrapper_function_addr, args_addr_ref); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | ThreadPlan * |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 352 | ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, lldb::addr_t func_addr, lldb::addr_t &args_addr, Stream &errors, bool stop_others, bool discard_on_error) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | { |
| 354 | // FIXME: Use the errors Stream for better error reporting. |
| 355 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 356 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | |
| 358 | if (process == NULL) |
| 359 | { |
| 360 | errors.Printf("Can't call a function without a process."); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | // Okay, now run the function: |
| 365 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 366 | Address wrapper_address (NULL, func_addr); |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 367 | ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | wrapper_address, |
| 369 | args_addr, |
| 370 | stop_others, discard_on_error); |
| 371 | return new_plan; |
| 372 | } |
| 373 | |
| 374 | bool |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 375 | 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] | 376 | { |
| 377 | // Read the return value - it is the last field in the struct: |
| 378 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 379 | |
| 380 | std::vector<uint8_t> data_buffer; |
| 381 | data_buffer.resize(m_return_size); |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 382 | Process *process = exe_ctx.process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | Error error; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 384 | size_t bytes_read = process->ReadMemory(args_addr + m_return_offset, &data_buffer.front(), m_return_size, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | |
| 386 | if (bytes_read == 0) |
| 387 | { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | if (bytes_read < m_return_size) |
| 392 | return false; |
| 393 | |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 394 | DataExtractor data(&data_buffer.front(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | // FIXME: Assuming an integer scalar for now: |
| 396 | |
| 397 | uint32_t offset = 0; |
| 398 | uint64_t return_integer = data.GetMaxU64(&offset, m_return_size); |
| 399 | |
| 400 | ret_value.SetContext (Value::eContextTypeOpaqueClangQualType, m_function_return_qual_type); |
| 401 | ret_value.SetValueType(Value::eValueTypeScalar); |
| 402 | ret_value.GetScalar() = return_integer; |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | void |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 407 | ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | { |
| 409 | std::list<lldb::addr_t>::iterator pos; |
| 410 | pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr); |
| 411 | if (pos != m_wrapper_args_addrs.end()) |
| 412 | m_wrapper_args_addrs.erase(pos); |
| 413 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 414 | exe_ctx.process->DeallocateMemory(args_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | ClangFunction::ExecutionResults |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 418 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 420 | return ExecuteFunction (exe_ctx, errors, 1000, true, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | ClangFunction::ExecutionResults |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 424 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 426 | return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, false, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | ClangFunction::ExecutionResults |
| 430 | ClangFunction::ExecuteFunction( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 431 | ExecutionContext &exe_ctx, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | Stream &errors, |
| 433 | uint32_t single_thread_timeout_usec, |
| 434 | bool try_all_threads, |
| 435 | Value &results) |
| 436 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 437 | return ExecuteFunction (exe_ctx, NULL, errors, true, single_thread_timeout_usec, try_all_threads, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 440 | // This is the static function |
| 441 | ClangFunction::ExecutionResults |
| 442 | ClangFunction::ExecuteFunction ( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 443 | ExecutionContext &exe_ctx, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 444 | lldb::addr_t function_address, |
| 445 | lldb::addr_t &void_arg, |
| 446 | bool stop_others, |
| 447 | bool try_all_threads, |
| 448 | uint32_t single_thread_timeout_usec, |
| 449 | Stream &errors) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 450 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 451 | // Save this value for restoration of the execution context after we run |
| 452 | uint32_t tid = exe_ctx.thread->GetID(); |
| 453 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 454 | ClangFunction::ExecutionResults return_value = eExecutionSetupError; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 456 | lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg, errors, stop_others, false)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | |
| 458 | ThreadPlanCallFunction *call_plan_ptr = static_cast<ThreadPlanCallFunction *> (call_plan_sp.get()); |
| 459 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | if (call_plan_sp == NULL) |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 461 | return eExecutionSetupError; |
| 462 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | call_plan_sp->SetPrivate(true); |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 464 | exe_ctx.thread->QueueThreadPlan(call_plan_sp, true); |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | // We need to call the function synchronously, so spin waiting for it to return. |
| 467 | // If we get interrupted while executing, we're going to lose our context, and |
| 468 | // won't be able to gather the result at this point. |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 470 | TimeValue* timeout_ptr = NULL; |
| 471 | TimeValue real_timeout; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | if (single_thread_timeout_usec != 0) |
| 474 | { |
| 475 | real_timeout = TimeValue::Now(); |
| 476 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 477 | timeout_ptr = &real_timeout; |
| 478 | } |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 479 | |
Jim Ingham | a2890f4 | 2010-08-17 00:35:35 +0000 | [diff] [blame] | 480 | Error resume_error = exe_ctx.process->Resume (); |
| 481 | if (!resume_error.Success()) |
| 482 | { |
| 483 | errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString()); |
| 484 | return eExecutionSetupError; |
| 485 | } |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 486 | |
| 487 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | |
| 489 | while (1) |
| 490 | { |
| 491 | lldb::EventSP event_sp; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | // Now wait for the process to stop again: |
| 494 | // FIXME: Probably want a time out. |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 495 | lldb::StateType stop_state = exe_ctx.process->WaitForStateChangedEvents (timeout_ptr, event_sp); |
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 | if (stop_state == lldb::eStateInvalid && timeout_ptr != NULL) |
| 498 | { |
| 499 | // Right now this is the only way to tell we've timed out... |
| 500 | // We should interrupt the process here... |
| 501 | // Not really sure what to do if Halt fails here... |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | if (log) |
| 503 | log->Printf ("Running function with timeout: %d timed out, trying with all threads enabled.", single_thread_timeout_usec); |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 504 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 505 | if (exe_ctx.process->Halt().Success()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 506 | { |
| 507 | timeout_ptr = NULL; |
| 508 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 509 | stop_state = exe_ctx.process->WaitForStateChangedEvents (timeout_ptr, event_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 510 | if (stop_state == lldb::eStateInvalid) |
| 511 | { |
| 512 | errors.Printf ("Got an invalid stop state after halt."); |
| 513 | } |
| 514 | else if (stop_state != lldb::eStateStopped) |
| 515 | { |
| 516 | StreamString s; |
| 517 | event_sp->Dump (&s); |
| 518 | |
| 519 | errors.Printf("Didn't get a stopped event after Halting the target, got: \"%s\"", s.GetData()); |
| 520 | } |
| 521 | |
| 522 | if (try_all_threads) |
| 523 | { |
| 524 | // Between the time that we got the timeout and the time we halted, but target |
| 525 | // might have actually completed the plan. If so, we're done. |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 526 | if (exe_ctx.thread->IsThreadPlanDone (call_plan_sp.get())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | { |
| 528 | return_value = eExecutionCompleted; |
| 529 | break; |
| 530 | } |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | call_plan_ptr->SetStopOthers (false); |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 533 | exe_ctx.process->Resume(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | else |
| 537 | return eExecutionInterrupted; |
| 538 | } |
| 539 | } |
| 540 | if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping) |
| 541 | continue; |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 542 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 543 | if (exe_ctx.thread->IsThreadPlanDone (call_plan_sp.get())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | { |
| 545 | return_value = eExecutionCompleted; |
| 546 | break; |
| 547 | } |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 548 | else if (exe_ctx.thread->WasThreadPlanDiscarded (call_plan_sp.get())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | { |
| 550 | return_value = eExecutionDiscarded; |
| 551 | break; |
| 552 | } |
| 553 | else |
| 554 | { |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 555 | if (log) |
| 556 | { |
| 557 | StreamString s; |
| 558 | event_sp->Dump (&s); |
| 559 | StreamString ts; |
| 560 | |
| 561 | const char *event_explanation; |
| 562 | |
| 563 | do |
| 564 | { |
| 565 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 566 | |
| 567 | if (!event_data) |
| 568 | { |
| 569 | event_explanation = "<no event data>"; |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | Process *process = event_data->GetProcessSP().get(); |
| 574 | |
| 575 | if (!process) |
| 576 | { |
| 577 | event_explanation = "<no process>"; |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | ThreadList &thread_list = process->GetThreadList(); |
| 582 | |
| 583 | uint32_t num_threads = thread_list.GetSize(); |
| 584 | uint32_t thread_index; |
| 585 | |
| 586 | ts.Printf("<%u threads> ", num_threads); |
| 587 | |
| 588 | for (thread_index = 0; |
| 589 | thread_index < num_threads; |
| 590 | ++thread_index) |
| 591 | { |
| 592 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 593 | |
| 594 | if (!thread) |
| 595 | { |
| 596 | ts.Printf("<?> "); |
| 597 | continue; |
| 598 | } |
| 599 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 600 | ts.Printf("<"); |
| 601 | RegisterContext *register_context = thread->GetRegisterContext(); |
| 602 | |
| 603 | if (register_context) |
| 604 | ts.Printf("[ip 0x%llx] ", register_context->GetPC()); |
| 605 | else |
| 606 | ts.Printf("[ip unknown] "); |
| 607 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 608 | StopInfo *stop_info = thread->GetStopInfo(); |
| 609 | if (stop_info) |
| 610 | { |
| 611 | const char *stop_desc = stop_info->GetDescription(); |
| 612 | if (stop_desc) |
| 613 | ts.PutCString (stop_desc); |
| 614 | } |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 615 | ts.Printf(">"); |
| 616 | } |
| 617 | |
| 618 | event_explanation = ts.GetData(); |
| 619 | } while (0); |
| 620 | |
| 621 | log->Printf("Execution interrupted: %s %s", s.GetData(), event_explanation); |
| 622 | } |
| 623 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 624 | return_value = eExecutionInterrupted; |
| 625 | break; |
| 626 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | } |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 628 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 629 | // Thread we ran the function in may have gone away because we ran the target |
| 630 | // Check that it's still there. |
| 631 | exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByID(tid, true).get(); |
| 632 | exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get(); |
| 633 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 634 | return return_value; |
| 635 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 636 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 637 | ClangFunction::ExecutionResults |
| 638 | ClangFunction::ExecuteFunction( |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 639 | ExecutionContext &exe_ctx, |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 640 | lldb::addr_t *args_addr_ptr, |
| 641 | Stream &errors, |
| 642 | bool stop_others, |
| 643 | uint32_t single_thread_timeout_usec, |
| 644 | bool try_all_threads, |
| 645 | Value &results) |
| 646 | { |
| 647 | using namespace clang; |
| 648 | ExecutionResults return_value = eExecutionSetupError; |
| 649 | |
| 650 | lldb::addr_t args_addr; |
| 651 | |
| 652 | if (args_addr_ptr != NULL) |
| 653 | args_addr = *args_addr_ptr; |
| 654 | else |
| 655 | args_addr = LLDB_INVALID_ADDRESS; |
| 656 | |
| 657 | if (CompileFunction(errors) != 0) |
| 658 | return eExecutionSetupError; |
| 659 | |
| 660 | if (args_addr == LLDB_INVALID_ADDRESS) |
| 661 | { |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 662 | if (!InsertFunction(exe_ctx, args_addr, errors)) |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 663 | return eExecutionSetupError; |
| 664 | } |
| 665 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 666 | return_value = ClangFunction::ExecuteFunction(exe_ctx, m_wrapper_function_addr, args_addr, stop_others, try_all_threads, single_thread_timeout_usec, errors); |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 667 | |
| 668 | if (args_addr_ptr != NULL) |
| 669 | *args_addr_ptr = args_addr; |
| 670 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 671 | if (return_value != eExecutionCompleted) |
| 672 | return return_value; |
| 673 | |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 674 | FetchFunctionResults(exe_ctx, args_addr, results); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 675 | |
| 676 | if (args_addr_ptr == NULL) |
Sean Callanan | c78d648 | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 677 | DeallocateFunctionResults(exe_ctx, args_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 678 | |
| 679 | return eExecutionCompleted; |
| 680 | } |
| 681 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 682 | clang::ASTConsumer * |
| 683 | ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 684 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 685 | return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | } |