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