Alexander Shaposhnikov | 696bd63 | 2016-11-26 05:23:44 +0000 | [diff] [blame] | 1 | //===-- ClangFunctionCaller.cpp ---------------------------------*- C++ -*-===// |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 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 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 10 | #include "ClangFunctionCaller.h" |
| 11 | |
| 12 | #include "ASTStructExtractor.h" |
| 13 | #include "ClangExpressionParser.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 14 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/RecordLayout.h" |
| 17 | #include "clang/CodeGen/CodeGenAction.h" |
| 18 | #include "clang/CodeGen/ModuleBuilder.h" |
| 19 | #include "clang/Frontend/CompilerInstance.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/Triple.h" |
| 22 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Module.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 26 | #include "lldb/Core/ValueObject.h" |
| 27 | #include "lldb/Core/ValueObjectList.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 28 | #include "lldb/Expression/IRExecutionUnit.h" |
| 29 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 30 | #include "lldb/Symbol/ClangASTContext.h" |
| 31 | #include "lldb/Symbol/Function.h" |
| 32 | #include "lldb/Symbol/Type.h" |
| 33 | #include "lldb/Target/ExecutionContext.h" |
| 34 | #include "lldb/Target/Process.h" |
| 35 | #include "lldb/Target/RegisterContext.h" |
| 36 | #include "lldb/Target/Target.h" |
| 37 | #include "lldb/Target/Thread.h" |
| 38 | #include "lldb/Target/ThreadPlan.h" |
| 39 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 40 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 41 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/State.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace lldb_private; |
| 45 | |
| 46 | //---------------------------------------------------------------------- |
| 47 | // ClangFunctionCaller constructor |
| 48 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | ClangFunctionCaller::ClangFunctionCaller(ExecutionContextScope &exe_scope, |
| 50 | const CompilerType &return_type, |
| 51 | const Address &functionAddress, |
| 52 | const ValueList &arg_value_list, |
| 53 | const char *name) |
| 54 | : FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list, |
| 55 | name), |
| 56 | m_type_system_helper(*this) { |
| 57 | m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess()); |
| 58 | // Can't make a ClangFunctionCaller without a process. |
| 59 | assert(m_jit_process_wp.lock()); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | //---------------------------------------------------------------------- |
| 63 | // Destructor |
| 64 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | ClangFunctionCaller::~ClangFunctionCaller() {} |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 66 | |
| 67 | unsigned |
Jim Ingham | 6896b35 | 2016-03-21 19:21:13 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | ClangFunctionCaller::CompileFunction(lldb::ThreadSP thread_to_use_sp, |
| 70 | DiagnosticManager &diagnostic_manager) { |
| 71 | if (m_compiled) |
| 72 | return 0; |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | // Compilation might call code, make sure to keep on the thread the caller |
| 75 | // indicated. |
| 76 | ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher( |
| 77 | thread_to_use_sp); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | // FIXME: How does clang tell us there's no return value? We need to handle |
| 80 | // that case. |
| 81 | unsigned num_errors = 0; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | std::string return_type_str( |
| 84 | m_function_return_type.GetTypeName().AsCString("")); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | // Cons up the function we're going to wrap our call in, then compile it... |
| 87 | // We declare the function "extern "C"" because the compiler might be in C++ |
| 88 | // mode which would mangle the name and then we couldn't find it again... |
| 89 | m_wrapper_function_text.clear(); |
| 90 | m_wrapper_function_text.append("extern \"C\" void "); |
| 91 | m_wrapper_function_text.append(m_wrapper_function_name); |
| 92 | m_wrapper_function_text.append(" (void *input)\n{\n struct "); |
| 93 | m_wrapper_function_text.append(m_wrapper_struct_name); |
| 94 | m_wrapper_function_text.append(" \n {\n"); |
| 95 | m_wrapper_function_text.append(" "); |
| 96 | m_wrapper_function_text.append(return_type_str); |
| 97 | m_wrapper_function_text.append(" (*fn_ptr) ("); |
| 98 | |
| 99 | // Get the number of arguments. If we have a function type and it is |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 100 | // prototyped, trust that, otherwise use the values we were given. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | |
| 102 | // FIXME: This will need to be extended to handle Variadic functions. We'll |
| 103 | // need |
| 104 | // to pull the defined arguments out of the function, then add the types from |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 105 | // the arguments list for the variable arguments. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | |
| 107 | uint32_t num_args = UINT32_MAX; |
| 108 | bool trust_function = false; |
| 109 | // GetArgumentCount returns -1 for an unprototyped function. |
| 110 | CompilerType function_clang_type; |
| 111 | if (m_function_ptr) { |
| 112 | function_clang_type = m_function_ptr->GetCompilerType(); |
| 113 | if (function_clang_type) { |
| 114 | int num_func_args = function_clang_type.GetFunctionArgumentCount(); |
| 115 | if (num_func_args >= 0) { |
| 116 | trust_function = true; |
| 117 | num_args = num_func_args; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (num_args == UINT32_MAX) |
| 123 | num_args = m_arg_values.GetSize(); |
| 124 | |
| 125 | std::string args_buffer; // This one stores the definition of all the args in |
| 126 | // "struct caller". |
| 127 | std::string args_list_buffer; // This one stores the argument list called from |
| 128 | // the structure. |
| 129 | for (size_t i = 0; i < num_args; i++) { |
| 130 | std::string type_name; |
| 131 | |
| 132 | if (trust_function) { |
| 133 | type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i) |
| 134 | .GetTypeName() |
| 135 | .AsCString(""); |
| 136 | } else { |
| 137 | CompilerType clang_qual_type = |
| 138 | m_arg_values.GetValueAtIndex(i)->GetCompilerType(); |
| 139 | if (clang_qual_type) { |
| 140 | type_name = clang_qual_type.GetTypeName().AsCString(""); |
| 141 | } else { |
| 142 | diagnostic_manager.Printf( |
| 143 | eDiagnosticSeverityError, |
| 144 | "Could not determine type of input value %" PRIu64 ".", |
| 145 | (uint64_t)i); |
| 146 | return 1; |
| 147 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | m_wrapper_function_text.append(type_name); |
| 151 | if (i < num_args - 1) |
| 152 | m_wrapper_function_text.append(", "); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | char arg_buf[32]; |
| 155 | args_buffer.append(" "); |
| 156 | args_buffer.append(type_name); |
| 157 | snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i); |
| 158 | args_buffer.push_back(' '); |
| 159 | args_buffer.append(arg_buf); |
| 160 | args_buffer.append(";\n"); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | args_list_buffer.append("__lldb_fn_data->"); |
| 163 | args_list_buffer.append(arg_buf); |
| 164 | if (i < num_args - 1) |
| 165 | args_list_buffer.append(", "); |
| 166 | } |
| 167 | m_wrapper_function_text.append( |
| 168 | ");\n"); // Close off the function calling prototype. |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | m_wrapper_function_text.append(args_buffer); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | m_wrapper_function_text.append(" "); |
| 173 | m_wrapper_function_text.append(return_type_str); |
| 174 | m_wrapper_function_text.append(" return_value;"); |
| 175 | m_wrapper_function_text.append("\n };\n struct "); |
| 176 | m_wrapper_function_text.append(m_wrapper_struct_name); |
| 177 | m_wrapper_function_text.append("* __lldb_fn_data = (struct "); |
| 178 | m_wrapper_function_text.append(m_wrapper_struct_name); |
| 179 | m_wrapper_function_text.append(" *) input;\n"); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | m_wrapper_function_text.append( |
| 182 | " __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); |
| 183 | m_wrapper_function_text.append(args_list_buffer); |
| 184 | m_wrapper_function_text.append(");\n}\n"); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 187 | if (log) |
| 188 | log->Printf("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str()); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | // Okay, now compile this expression |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 191 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); |
| 193 | if (jit_process_sp) { |
| 194 | const bool generate_debug_info = true; |
| 195 | m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, |
| 196 | generate_debug_info)); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | num_errors = m_parser->Parse(diagnostic_manager); |
| 199 | } else { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 200 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 201 | "no process - unable to inject function"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | num_errors = 1; |
| 203 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | m_compiled = (num_errors == 0); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | if (!m_compiled) |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 208 | return num_errors; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | |
| 210 | return num_errors; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | clang::ASTConsumer * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer( |
| 215 | clang::ASTConsumer *passthrough) { |
| 216 | m_struct_extractor.reset(new ASTStructExtractor( |
| 217 | passthrough, m_owner.GetWrapperStructName(), m_owner)); |
| 218 | |
| 219 | return m_struct_extractor.get(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 220 | } |