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