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