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