ClangFunctionCaller.cpp revision 314564
1314564Sdim//===-- ClangFunctionCaller.cpp ---------------------------------*- C++ -*-===// 2292932Sdim// 3292932Sdim// The LLVM Compiler Infrastructure 4292932Sdim// 5292932Sdim// This file is distributed under the University of Illinois Open Source 6292932Sdim// License. See LICENSE.TXT for details. 7292932Sdim// 8292932Sdim//===----------------------------------------------------------------------===// 9292932Sdim 10292932Sdim#include "ClangFunctionCaller.h" 11292932Sdim 12292932Sdim#include "ASTStructExtractor.h" 13292932Sdim#include "ClangExpressionParser.h" 14292932Sdim 15292932Sdim// C Includes 16292932Sdim// C++ Includes 17292932Sdim// Other libraries and framework includes 18292932Sdim#include "clang/AST/ASTContext.h" 19292932Sdim#include "clang/AST/RecordLayout.h" 20292932Sdim#include "clang/CodeGen/CodeGenAction.h" 21292932Sdim#include "clang/CodeGen/ModuleBuilder.h" 22292932Sdim#include "clang/Frontend/CompilerInstance.h" 23292932Sdim#include "llvm/ADT/StringRef.h" 24292932Sdim#include "llvm/ADT/Triple.h" 25292932Sdim#include "llvm/ExecutionEngine/ExecutionEngine.h" 26292932Sdim#include "llvm/IR/Module.h" 27292932Sdim 28292932Sdim// Project includes 29292932Sdim#include "lldb/Core/DataExtractor.h" 30292932Sdim#include "lldb/Core/Log.h" 31292932Sdim#include "lldb/Core/Module.h" 32292932Sdim#include "lldb/Core/State.h" 33292932Sdim#include "lldb/Core/ValueObject.h" 34292932Sdim#include "lldb/Core/ValueObjectList.h" 35292932Sdim#include "lldb/Expression/IRExecutionUnit.h" 36292932Sdim#include "lldb/Interpreter/CommandReturnObject.h" 37292932Sdim#include "lldb/Symbol/ClangASTContext.h" 38292932Sdim#include "lldb/Symbol/Function.h" 39292932Sdim#include "lldb/Symbol/Type.h" 40292932Sdim#include "lldb/Target/ExecutionContext.h" 41292932Sdim#include "lldb/Target/Process.h" 42292932Sdim#include "lldb/Target/RegisterContext.h" 43292932Sdim#include "lldb/Target/Target.h" 44292932Sdim#include "lldb/Target/Thread.h" 45292932Sdim#include "lldb/Target/ThreadPlan.h" 46292932Sdim#include "lldb/Target/ThreadPlanCallFunction.h" 47292932Sdim 48292932Sdimusing namespace lldb_private; 49292932Sdim 50292932Sdim//---------------------------------------------------------------------- 51292932Sdim// ClangFunctionCaller constructor 52292932Sdim//---------------------------------------------------------------------- 53314564SdimClangFunctionCaller::ClangFunctionCaller(ExecutionContextScope &exe_scope, 54314564Sdim const CompilerType &return_type, 55314564Sdim const Address &functionAddress, 56314564Sdim const ValueList &arg_value_list, 57314564Sdim const char *name) 58314564Sdim : FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list, 59314564Sdim name), 60314564Sdim m_type_system_helper(*this) { 61314564Sdim m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess()); 62314564Sdim // Can't make a ClangFunctionCaller without a process. 63314564Sdim assert(m_jit_process_wp.lock()); 64292932Sdim} 65292932Sdim 66292932Sdim//---------------------------------------------------------------------- 67292932Sdim// Destructor 68292932Sdim//---------------------------------------------------------------------- 69314564SdimClangFunctionCaller::~ClangFunctionCaller() {} 70292932Sdim 71292932Sdimunsigned 72309124Sdim 73314564SdimClangFunctionCaller::CompileFunction(lldb::ThreadSP thread_to_use_sp, 74314564Sdim DiagnosticManager &diagnostic_manager) { 75314564Sdim if (m_compiled) 76314564Sdim return 0; 77309124Sdim 78314564Sdim // Compilation might call code, make sure to keep on the thread the caller 79314564Sdim // indicated. 80314564Sdim ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher( 81314564Sdim thread_to_use_sp); 82292932Sdim 83314564Sdim // FIXME: How does clang tell us there's no return value? We need to handle 84314564Sdim // that case. 85314564Sdim unsigned num_errors = 0; 86292932Sdim 87314564Sdim std::string return_type_str( 88314564Sdim m_function_return_type.GetTypeName().AsCString("")); 89292932Sdim 90314564Sdim // Cons up the function we're going to wrap our call in, then compile it... 91314564Sdim // We declare the function "extern "C"" because the compiler might be in C++ 92314564Sdim // mode which would mangle the name and then we couldn't find it again... 93314564Sdim m_wrapper_function_text.clear(); 94314564Sdim m_wrapper_function_text.append("extern \"C\" void "); 95314564Sdim m_wrapper_function_text.append(m_wrapper_function_name); 96314564Sdim m_wrapper_function_text.append(" (void *input)\n{\n struct "); 97314564Sdim m_wrapper_function_text.append(m_wrapper_struct_name); 98314564Sdim m_wrapper_function_text.append(" \n {\n"); 99314564Sdim m_wrapper_function_text.append(" "); 100314564Sdim m_wrapper_function_text.append(return_type_str); 101314564Sdim m_wrapper_function_text.append(" (*fn_ptr) ("); 102314564Sdim 103314564Sdim // Get the number of arguments. If we have a function type and it is 104314564Sdim // prototyped, 105314564Sdim // trust that, otherwise use the values we were given. 106314564Sdim 107314564Sdim // FIXME: This will need to be extended to handle Variadic functions. We'll 108314564Sdim // need 109314564Sdim // to pull the defined arguments out of the function, then add the types from 110314564Sdim // the 111314564Sdim // arguments list for the variable arguments. 112314564Sdim 113314564Sdim uint32_t num_args = UINT32_MAX; 114314564Sdim bool trust_function = false; 115314564Sdim // GetArgumentCount returns -1 for an unprototyped function. 116314564Sdim CompilerType function_clang_type; 117314564Sdim if (m_function_ptr) { 118314564Sdim function_clang_type = m_function_ptr->GetCompilerType(); 119314564Sdim if (function_clang_type) { 120314564Sdim int num_func_args = function_clang_type.GetFunctionArgumentCount(); 121314564Sdim if (num_func_args >= 0) { 122314564Sdim trust_function = true; 123314564Sdim num_args = num_func_args; 124314564Sdim } 125292932Sdim } 126314564Sdim } 127292932Sdim 128314564Sdim if (num_args == UINT32_MAX) 129314564Sdim num_args = m_arg_values.GetSize(); 130292932Sdim 131314564Sdim std::string args_buffer; // This one stores the definition of all the args in 132314564Sdim // "struct caller". 133314564Sdim std::string args_list_buffer; // This one stores the argument list called from 134314564Sdim // the structure. 135314564Sdim for (size_t i = 0; i < num_args; i++) { 136314564Sdim std::string type_name; 137292932Sdim 138314564Sdim if (trust_function) { 139314564Sdim type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i) 140314564Sdim .GetTypeName() 141314564Sdim .AsCString(""); 142314564Sdim } else { 143314564Sdim CompilerType clang_qual_type = 144314564Sdim m_arg_values.GetValueAtIndex(i)->GetCompilerType(); 145314564Sdim if (clang_qual_type) { 146314564Sdim type_name = clang_qual_type.GetTypeName().AsCString(""); 147314564Sdim } else { 148314564Sdim diagnostic_manager.Printf( 149314564Sdim eDiagnosticSeverityError, 150314564Sdim "Could not determine type of input value %" PRIu64 ".", 151314564Sdim (uint64_t)i); 152314564Sdim return 1; 153314564Sdim } 154314564Sdim } 155292932Sdim 156314564Sdim m_wrapper_function_text.append(type_name); 157314564Sdim if (i < num_args - 1) 158314564Sdim m_wrapper_function_text.append(", "); 159292932Sdim 160314564Sdim char arg_buf[32]; 161314564Sdim args_buffer.append(" "); 162314564Sdim args_buffer.append(type_name); 163314564Sdim snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i); 164314564Sdim args_buffer.push_back(' '); 165314564Sdim args_buffer.append(arg_buf); 166314564Sdim args_buffer.append(";\n"); 167292932Sdim 168314564Sdim args_list_buffer.append("__lldb_fn_data->"); 169314564Sdim args_list_buffer.append(arg_buf); 170314564Sdim if (i < num_args - 1) 171314564Sdim args_list_buffer.append(", "); 172314564Sdim } 173314564Sdim m_wrapper_function_text.append( 174314564Sdim ");\n"); // Close off the function calling prototype. 175292932Sdim 176314564Sdim m_wrapper_function_text.append(args_buffer); 177292932Sdim 178314564Sdim m_wrapper_function_text.append(" "); 179314564Sdim m_wrapper_function_text.append(return_type_str); 180314564Sdim m_wrapper_function_text.append(" return_value;"); 181314564Sdim m_wrapper_function_text.append("\n };\n struct "); 182314564Sdim m_wrapper_function_text.append(m_wrapper_struct_name); 183314564Sdim m_wrapper_function_text.append("* __lldb_fn_data = (struct "); 184314564Sdim m_wrapper_function_text.append(m_wrapper_struct_name); 185314564Sdim m_wrapper_function_text.append(" *) input;\n"); 186292932Sdim 187314564Sdim m_wrapper_function_text.append( 188314564Sdim " __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); 189314564Sdim m_wrapper_function_text.append(args_list_buffer); 190314564Sdim m_wrapper_function_text.append(");\n}\n"); 191292932Sdim 192314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 193314564Sdim if (log) 194314564Sdim log->Printf("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str()); 195292932Sdim 196314564Sdim // Okay, now compile this expression 197309124Sdim 198314564Sdim lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock()); 199314564Sdim if (jit_process_sp) { 200314564Sdim const bool generate_debug_info = true; 201314564Sdim m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, 202314564Sdim generate_debug_info)); 203309124Sdim 204314564Sdim num_errors = m_parser->Parse(diagnostic_manager); 205314564Sdim } else { 206314564Sdim diagnostic_manager.PutString(eDiagnosticSeverityError, 207314564Sdim "no process - unable to inject function"); 208314564Sdim num_errors = 1; 209314564Sdim } 210292932Sdim 211314564Sdim m_compiled = (num_errors == 0); 212314564Sdim 213314564Sdim if (!m_compiled) 214292932Sdim return num_errors; 215314564Sdim 216314564Sdim return num_errors; 217292932Sdim} 218292932Sdim 219292932Sdimclang::ASTConsumer * 220314564SdimClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer( 221314564Sdim clang::ASTConsumer *passthrough) { 222314564Sdim m_struct_extractor.reset(new ASTStructExtractor( 223314564Sdim passthrough, m_owner.GetWrapperStructName(), m_owner)); 224314564Sdim 225314564Sdim return m_struct_extractor.get(); 226292932Sdim} 227