Chris Lattner | 30fdc8d | 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 | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/RecordLayout.h" |
Greg Clayton | d2d60ce | 2010-07-02 18:39:06 +0000 | [diff] [blame] | 16 | #include "clang/CodeGen/CodeGenAction.h" |
| 17 | #include "clang/CodeGen/ModuleBuilder.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Greg Clayton | d2d60ce | 2010-07-02 18:39:06 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chandler Carruth | 1e15758 | 2013-01-02 12:20:07 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | // Project includes |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/ASTStructExtractor.h" |
| 26 | #include "lldb/Expression/ClangExpressionParser.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangFunction.h" |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 28 | #include "lldb/Expression/IRExecutionUnit.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Symbol/Type.h" |
| 30 | #include "lldb/Core/DataExtractor.h" |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 31 | #include "lldb/Core/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Core/ValueObject.h" |
| 33 | #include "lldb/Core/ValueObjectList.h" |
| 34 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 35 | #include "lldb/Symbol/ClangASTContext.h" |
| 36 | #include "lldb/Symbol/Function.h" |
| 37 | #include "lldb/Target/ExecutionContext.h" |
| 38 | #include "lldb/Target/Process.h" |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 39 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | #include "lldb/Target/Thread.h" |
| 42 | #include "lldb/Target/ThreadPlan.h" |
| 43 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 44 | #include "lldb/Core/Log.h" |
| 45 | |
| 46 | using namespace lldb_private; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | //---------------------------------------------------------------------- |
| 49 | // ClangFunction constructor |
| 50 | //---------------------------------------------------------------------- |
Greg Clayton | 7b462cc | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 51 | ClangFunction::ClangFunction |
| 52 | ( |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 53 | ExecutionContextScope &exe_scope, |
Greg Clayton | 7b462cc | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 54 | ClangASTContext *ast_context, |
| 55 | void *return_qualtype, |
| 56 | const Address& functionAddress, |
| 57 | const ValueList &arg_value_list |
| 58 | ) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | m_function_ptr (NULL), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 60 | m_function_addr (functionAddress), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | m_function_return_qual_type(return_qualtype), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 62 | m_clang_ast_context (ast_context), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | m_wrapper_function_name ("__lldb_caller_function"), |
| 64 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 65 | m_wrapper_args_addrs (), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 66 | m_arg_values (arg_value_list), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | m_compiled (false), |
| 68 | m_JITted (false) |
| 69 | { |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 70 | m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess()); |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 71 | // Can't make a ClangFunction without a process. |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 72 | assert (m_jit_process_wp.lock()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Greg Clayton | 7b462cc | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 75 | ClangFunction::ClangFunction |
| 76 | ( |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 77 | ExecutionContextScope &exe_scope, |
Greg Clayton | 7b462cc | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 78 | Function &function, |
| 79 | ClangASTContext *ast_context, |
| 80 | const ValueList &arg_value_list |
| 81 | ) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | m_function_ptr (&function), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 83 | m_function_addr (), |
| 84 | m_function_return_qual_type (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | m_clang_ast_context (ast_context), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | m_wrapper_function_name ("__lldb_function_caller"), |
| 87 | m_wrapper_struct_name ("__lldb_caller_struct"), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 88 | m_wrapper_args_addrs (), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 89 | m_arg_values (arg_value_list), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | m_compiled (false), |
| 91 | m_JITted (false) |
| 92 | { |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 93 | m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess()); |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 94 | // Can't make a ClangFunction without a process. |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 95 | assert (m_jit_process_wp.lock()); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress(); |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 98 | m_function_return_qual_type = m_function_ptr->GetReturnClangType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | //---------------------------------------------------------------------- |
| 102 | // Destructor |
| 103 | //---------------------------------------------------------------------- |
| 104 | ClangFunction::~ClangFunction() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | unsigned |
| 109 | ClangFunction::CompileFunction (Stream &errors) |
| 110 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 111 | if (m_compiled) |
| 112 | return 0; |
| 113 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 115 | unsigned num_errors = 0; |
| 116 | |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 117 | std::string return_type_str (ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(), |
| 118 | m_function_return_qual_type)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 119 | |
| 120 | // Cons up the function we're going to wrap our call in, then compile it... |
| 121 | // We declare the function "extern "C"" because the compiler might be in C++ |
| 122 | // mode which would mangle the name and then we couldn't find it again... |
| 123 | m_wrapper_function_text.clear(); |
| 124 | m_wrapper_function_text.append ("extern \"C\" void "); |
| 125 | m_wrapper_function_text.append (m_wrapper_function_name); |
| 126 | m_wrapper_function_text.append (" (void *input)\n{\n struct "); |
| 127 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 128 | m_wrapper_function_text.append (" \n {\n"); |
| 129 | m_wrapper_function_text.append (" "); |
| 130 | m_wrapper_function_text.append (return_type_str); |
| 131 | m_wrapper_function_text.append (" (*fn_ptr) ("); |
| 132 | |
| 133 | // Get the number of arguments. If we have a function type and it is prototyped, |
| 134 | // trust that, otherwise use the values we were given. |
| 135 | |
| 136 | // FIXME: This will need to be extended to handle Variadic functions. We'll need |
| 137 | // to pull the defined arguments out of the function, then add the types from the |
| 138 | // arguments list for the variable arguments. |
| 139 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 140 | size_t num_args = UINT32_MAX; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 141 | bool trust_function = false; |
| 142 | // GetArgumentCount returns -1 for an unprototyped function. |
| 143 | if (m_function_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 145 | int num_func_args = m_function_ptr->GetArgumentCount(); |
| 146 | if (num_func_args >= 0) |
| 147 | trust_function = true; |
| 148 | else |
| 149 | num_args = num_func_args; |
| 150 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 152 | if (num_args == UINT32_MAX) |
| 153 | num_args = m_arg_values.GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 155 | std::string args_buffer; // This one stores the definition of all the args in "struct caller". |
| 156 | std::string args_list_buffer; // This one stores the argument list called from the structure. |
| 157 | for (size_t i = 0; i < num_args; i++) |
| 158 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 159 | std::string type_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 161 | if (trust_function) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 163 | lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i); |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 164 | type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(), |
| 165 | arg_clang_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 167 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 169 | Value *arg_value = m_arg_values.GetValueAtIndex(i); |
Greg Clayton | e305594 | 2011-06-30 02:28:26 +0000 | [diff] [blame] | 170 | lldb::clang_type_t clang_qual_type = arg_value->GetClangType (); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 171 | if (clang_qual_type != NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | { |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 173 | type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(), |
| 174 | clang_qual_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | } |
| 176 | else |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 177 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 178 | errors.Printf("Could not determine type of input value %lu.", i); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | return 1; |
| 180 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 182 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 183 | m_wrapper_function_text.append (type_name); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 184 | if (i < num_args - 1) |
| 185 | m_wrapper_function_text.append (", "); |
| 186 | |
| 187 | char arg_buf[32]; |
| 188 | args_buffer.append (" "); |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 189 | args_buffer.append (type_name); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 190 | snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 191 | args_buffer.push_back (' '); |
| 192 | args_buffer.append (arg_buf); |
| 193 | args_buffer.append (";\n"); |
| 194 | |
| 195 | args_list_buffer.append ("__lldb_fn_data->"); |
| 196 | args_list_buffer.append (arg_buf); |
| 197 | if (i < num_args - 1) |
| 198 | args_list_buffer.append (", "); |
| 199 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 201 | m_wrapper_function_text.append (");\n"); // Close off the function calling prototype. |
| 202 | |
| 203 | m_wrapper_function_text.append (args_buffer); |
| 204 | |
| 205 | m_wrapper_function_text.append (" "); |
| 206 | m_wrapper_function_text.append (return_type_str); |
| 207 | m_wrapper_function_text.append (" return_value;"); |
| 208 | m_wrapper_function_text.append ("\n };\n struct "); |
| 209 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 210 | m_wrapper_function_text.append ("* __lldb_fn_data = (struct "); |
| 211 | m_wrapper_function_text.append (m_wrapper_struct_name); |
| 212 | m_wrapper_function_text.append (" *) input;\n"); |
| 213 | |
| 214 | m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); |
| 215 | m_wrapper_function_text.append (args_list_buffer); |
| 216 | m_wrapper_function_text.append (");\n}\n"); |
| 217 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 218 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 219 | if (log) |
| 220 | log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str()); |
| 221 | |
| 222 | // Okay, now compile this expression |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 223 | |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 224 | lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); |
| 225 | if (jit_process_sp) |
| 226 | { |
| 227 | m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this)); |
| 228 | |
| 229 | num_errors = m_parser->Parse (errors); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | errors.Printf("no process - unable to inject function"); |
| 234 | num_errors = 1; |
| 235 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 236 | |
| 237 | m_compiled = (num_errors == 0); |
| 238 | |
| 239 | if (!m_compiled) |
| 240 | return num_errors; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | |
| 242 | return num_errors; |
| 243 | } |
| 244 | |
| 245 | bool |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 246 | ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | { |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 248 | Process *process = exe_ctx.GetProcessPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 250 | if (!process) |
| 251 | return false; |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 252 | |
| 253 | lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); |
| 254 | |
| 255 | if (process != jit_process_sp.get()) |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 256 | return false; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 257 | |
| 258 | if (!m_compiled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | return false; |
| 260 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 261 | if (m_JITted) |
| 262 | return true; |
| 263 | |
Sean Callanan | 63697e5 | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 264 | lldb::ClangExpressionVariableSP const_result; |
| 265 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 266 | bool evaluated_statically = false; // should stay that way |
| 267 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 268 | Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr, |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 269 | m_jit_end_addr, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 270 | m_execution_unit_ap, |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 271 | exe_ctx, |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 272 | evaluated_statically, |
| 273 | const_result, |
| 274 | eExecutionPolicyAlways)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 275 | |
| 276 | if (!jit_error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | return false; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 278 | |
| 279 | if (process && m_jit_start_addr) |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 280 | m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); |
Sean Callanan | d14fac1 | 2013-01-14 21:45:38 +0000 | [diff] [blame] | 281 | |
| 282 | m_JITted = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | bool |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 288 | ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | { |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 290 | return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function. |
| 294 | |
| 295 | bool |
Jim Ingham | baae168 | 2010-09-10 23:07:48 +0000 | [diff] [blame] | 296 | ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, |
| 297 | lldb::addr_t &args_addr_ref, |
| 298 | Address function_address, |
| 299 | ValueList &arg_values, |
| 300 | Stream &errors) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 302 | // All the information to reconstruct the struct is provided by the |
| 303 | // StructExtractor. |
| 304 | if (!m_struct_valid) |
| 305 | { |
| 306 | errors.Printf("Argument information was not correctly parsed, so the function cannot be called."); |
| 307 | return false; |
| 308 | } |
| 309 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | Error error; |
| 311 | using namespace clang; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 312 | ExecutionResults return_value = eExecutionSetupError; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 314 | Process *process = exe_ctx.GetProcessPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | |
| 316 | if (process == NULL) |
| 317 | return return_value; |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 318 | |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 319 | lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); |
| 320 | |
| 321 | if (process != jit_process_sp.get()) |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 322 | return false; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 325 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 326 | args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 328 | return false; |
| 329 | m_wrapper_args_addrs.push_back (args_addr_ref); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | // Make sure this is an address that we've already handed out. |
| 334 | if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end()) |
| 335 | { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 340 | // TODO: verify fun_addr needs to be a callable address |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 341 | Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr())); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 342 | uint64_t first_offset = m_member_offsets[0]; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 343 | process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 344 | |
| 345 | // FIXME: We will need to extend this for Variadic functions. |
| 346 | |
| 347 | Error value_error; |
| 348 | |
| 349 | size_t num_args = arg_values.GetSize(); |
| 350 | if (num_args != m_arg_values.GetSize()) |
| 351 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 352 | errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 356 | for (size_t i = 0; i < num_args; i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | { |
| 358 | // FIXME: We should sanity check sizes. |
| 359 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 360 | uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | Value *arg_value = arg_values.GetValueAtIndex(i); |
| 362 | |
| 363 | // FIXME: For now just do scalars: |
| 364 | |
| 365 | // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings) |
| 366 | |
| 367 | if (arg_value->GetValueType() == Value::eValueTypeHostAddress && |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 368 | arg_value->GetContextType() == Value::eContextTypeClangType && |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 369 | ClangASTContext::IsPointerType(arg_value->GetClangType())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | continue; |
| 371 | |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 372 | const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 374 | if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error)) |
| 375 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 382 | ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | { |
| 384 | using namespace clang; |
| 385 | |
| 386 | if (CompileFunction(errors) != 0) |
| 387 | return false; |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 388 | if (!WriteFunctionWrapper(exe_ctx, errors)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | return false; |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 390 | if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 391 | return false; |
| 392 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 393 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 395 | log->Printf ("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n", m_jit_start_addr, args_addr_ref); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | ThreadPlan * |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 401 | ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, |
| 402 | lldb::addr_t func_addr, |
| 403 | lldb::addr_t &args_addr, |
| 404 | Stream &errors, |
| 405 | bool stop_others, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 406 | bool unwind_on_error, |
| 407 | bool ignore_breakpoints, |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 408 | lldb::addr_t *this_arg, |
| 409 | lldb::addr_t *cmd_arg) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 411 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | f58b12d | 2013-03-06 19:57:25 +0000 | [diff] [blame] | 412 | |
| 413 | if (log) |
| 414 | log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function --"); |
| 415 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 416 | // FIXME: Use the errors Stream for better error reporting. |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 417 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 418 | if (thread == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | { |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 420 | errors.Printf("Can't call a function without a valid thread."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | // Okay, now run the function: |
| 425 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 426 | Address wrapper_address (func_addr); |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 427 | ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread, |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 428 | wrapper_address, |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 429 | ClangASTType(), |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 430 | args_addr, |
| 431 | stop_others, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 432 | unwind_on_error, |
| 433 | ignore_breakpoints, |
Sean Callanan | 1782783 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 434 | this_arg, |
| 435 | cmd_arg); |
Jim Ingham | 923886c | 2012-05-11 18:43:38 +0000 | [diff] [blame] | 436 | new_plan->SetIsMasterPlan(true); |
| 437 | new_plan->SetOkayToDiscard (false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 438 | return new_plan; |
| 439 | } |
| 440 | |
| 441 | bool |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 442 | ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | { |
| 444 | // Read the return value - it is the last field in the struct: |
| 445 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 446 | // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject |
| 447 | // to fetch the value. That way we can fetch any values we need. |
Sean Callanan | f58b12d | 2013-03-06 19:57:25 +0000 | [diff] [blame] | 448 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 449 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | f58b12d | 2013-03-06 19:57:25 +0000 | [diff] [blame] | 450 | |
| 451 | if (log) |
| 452 | log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results --"); |
| 453 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 454 | Process *process = exe_ctx.GetProcessPtr(); |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 455 | |
| 456 | if (process == NULL) |
| 457 | return false; |
Enrico Granata | dfc88a0 | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 458 | |
| 459 | lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); |
| 460 | |
| 461 | if (process != jit_process_sp.get()) |
Jim Ingham | 35944dd | 2011-03-17 20:02:56 +0000 | [diff] [blame] | 462 | return false; |
| 463 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | Error error; |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 465 | ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | |
Greg Clayton | f3ef3d2 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 467 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | return false; |
| 469 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 470 | ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | ret_value.SetValueType(Value::eValueTypeScalar); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | |
| 475 | void |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 476 | ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | { |
| 478 | std::list<lldb::addr_t>::iterator pos; |
| 479 | pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr); |
| 480 | if (pos != m_wrapper_args_addrs.end()) |
| 481 | m_wrapper_args_addrs.erase(pos); |
| 482 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 483 | exe_ctx.GetProcessRef().DeallocateMemory(args_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 486 | ExecutionResults |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 487 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | { |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 489 | return ExecuteFunction (exe_ctx, errors, 1000, true, results); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 492 | ExecutionResults |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 493 | ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 494 | { |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 495 | const bool try_all_threads = false; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 496 | const bool unwind_on_error = true; |
| 497 | const bool ignore_breakpoints = true; |
| 498 | return ExecuteFunction (exe_ctx, NULL, errors, stop_others, 0UL, try_all_threads, |
| 499 | unwind_on_error, ignore_breakpoints, results); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 502 | ExecutionResults |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 503 | ClangFunction::ExecuteFunction( |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 504 | ExecutionContext &exe_ctx, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | Stream &errors, |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 506 | uint32_t timeout_usec, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | bool try_all_threads, |
| 508 | Value &results) |
| 509 | { |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 510 | const bool stop_others = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 511 | const bool unwind_on_error = true; |
| 512 | const bool ignore_breakpoints = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 513 | return ExecuteFunction (exe_ctx, NULL, errors, stop_others, timeout_usec, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 514 | try_all_threads, unwind_on_error, ignore_breakpoints, results); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 517 | // This is the static function |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 518 | ExecutionResults |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 519 | ClangFunction::ExecuteFunction ( |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 520 | ExecutionContext &exe_ctx, |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 521 | lldb::addr_t function_address, |
| 522 | lldb::addr_t &void_arg, |
| 523 | bool stop_others, |
| 524 | bool try_all_threads, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 525 | bool unwind_on_error, |
| 526 | bool ignore_breakpoints, |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 527 | uint32_t timeout_usec, |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 528 | Stream &errors, |
| 529 | lldb::addr_t *this_arg) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 530 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 531 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | f58b12d | 2013-03-06 19:57:25 +0000 | [diff] [blame] | 532 | |
| 533 | if (log) |
| 534 | log->Printf("== [ClangFunction::ExecuteFunction] Executing function =="); |
| 535 | |
| 536 | lldb::ThreadPlanSP call_plan_sp (ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 537 | function_address, |
| 538 | void_arg, |
| 539 | errors, |
| 540 | stop_others, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 541 | unwind_on_error, |
| 542 | ignore_breakpoints, |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 543 | this_arg)); |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 544 | if (!call_plan_sp) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 545 | return eExecutionSetupError; |
Jim Ingham | 95afbf5 | 2012-12-07 19:04:31 +0000 | [diff] [blame] | 546 | |
Enrico Granata | e2e091b | 2012-08-03 22:24:48 +0000 | [diff] [blame] | 547 | // <rdar://problem/12027563> we need to make sure we record the fact that we are running an expression here |
| 548 | // otherwise this fact will fail to be recorded when fetching an Objective-C object description |
| 549 | if (exe_ctx.GetProcessPtr()) |
| 550 | exe_ctx.GetProcessPtr()->SetRunningUserExpression(true); |
| 551 | |
| 552 | ExecutionResults results = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, call_plan_sp, |
| 553 | stop_others, |
| 554 | try_all_threads, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 555 | unwind_on_error, |
| 556 | ignore_breakpoints, |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 557 | timeout_usec, |
Enrico Granata | e2e091b | 2012-08-03 22:24:48 +0000 | [diff] [blame] | 558 | errors); |
| 559 | |
Sean Callanan | f58b12d | 2013-03-06 19:57:25 +0000 | [diff] [blame] | 560 | if (log) |
| 561 | { |
| 562 | if (results != eExecutionCompleted) |
| 563 | { |
| 564 | log->Printf("== [ClangFunction::ExecuteFunction] Execution completed abnormally =="); |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | log->Printf("== [ClangFunction::ExecuteFunction] Execution completed normally =="); |
| 569 | } |
| 570 | } |
| 571 | |
Enrico Granata | e2e091b | 2012-08-03 22:24:48 +0000 | [diff] [blame] | 572 | if (exe_ctx.GetProcessPtr()) |
| 573 | exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); |
| 574 | |
| 575 | return results; |
| 576 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 577 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 578 | ExecutionResults |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 579 | ClangFunction::ExecuteFunction( |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 580 | ExecutionContext &exe_ctx, |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 581 | lldb::addr_t *args_addr_ptr, |
| 582 | Stream &errors, |
| 583 | bool stop_others, |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 584 | uint32_t timeout_usec, |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 585 | bool try_all_threads, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 586 | bool unwind_on_error, |
| 587 | bool ignore_breakpoints, |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 588 | Value &results) |
| 589 | { |
| 590 | using namespace clang; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 591 | ExecutionResults return_value = eExecutionSetupError; |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 592 | |
| 593 | lldb::addr_t args_addr; |
| 594 | |
| 595 | if (args_addr_ptr != NULL) |
| 596 | args_addr = *args_addr_ptr; |
| 597 | else |
| 598 | args_addr = LLDB_INVALID_ADDRESS; |
| 599 | |
| 600 | if (CompileFunction(errors) != 0) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 601 | return eExecutionSetupError; |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 602 | |
| 603 | if (args_addr == LLDB_INVALID_ADDRESS) |
| 604 | { |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 605 | if (!InsertFunction(exe_ctx, args_addr, errors)) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 606 | return eExecutionSetupError; |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 609 | return_value = ClangFunction::ExecuteFunction (exe_ctx, |
| 610 | m_jit_start_addr, |
| 611 | args_addr, |
| 612 | stop_others, |
| 613 | try_all_threads, |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 614 | unwind_on_error, |
| 615 | ignore_breakpoints, |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 616 | timeout_usec, |
Greg Clayton | 22a939a | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 617 | errors); |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 618 | |
| 619 | if (args_addr_ptr != NULL) |
| 620 | *args_addr_ptr = args_addr; |
| 621 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 622 | if (return_value != eExecutionCompleted) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | return return_value; |
| 624 | |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 625 | FetchFunctionResults(exe_ctx, args_addr, results); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | |
| 627 | if (args_addr_ptr == NULL) |
Sean Callanan | 138e74e | 2010-07-26 22:14:36 +0000 | [diff] [blame] | 628 | DeallocateFunctionResults(exe_ctx, args_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 630 | return eExecutionCompleted; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 633 | clang::ASTConsumer * |
| 634 | ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 636 | return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | } |