Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.cpp
39648 views
//===-- ClangFunctionCaller.cpp -------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ClangFunctionCaller.h"910#include "ASTStructExtractor.h"11#include "ClangExpressionParser.h"1213#include "clang/AST/ASTContext.h"14#include "clang/AST/RecordLayout.h"15#include "clang/CodeGen/CodeGenAction.h"16#include "clang/CodeGen/ModuleBuilder.h"17#include "clang/Frontend/CompilerInstance.h"18#include "llvm/ADT/StringRef.h"19#include "llvm/ExecutionEngine/ExecutionEngine.h"20#include "llvm/IR/Module.h"21#include "llvm/TargetParser/Triple.h"2223#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"24#include "lldb/Core/Module.h"25#include "lldb/Core/ValueObject.h"26#include "lldb/Core/ValueObjectList.h"27#include "lldb/Expression/IRExecutionUnit.h"28#include "lldb/Interpreter/CommandReturnObject.h"29#include "lldb/Symbol/Function.h"30#include "lldb/Symbol/Type.h"31#include "lldb/Target/ExecutionContext.h"32#include "lldb/Target/Process.h"33#include "lldb/Target/RegisterContext.h"34#include "lldb/Target/Target.h"35#include "lldb/Target/Thread.h"36#include "lldb/Target/ThreadPlan.h"37#include "lldb/Target/ThreadPlanCallFunction.h"38#include "lldb/Utility/DataExtractor.h"39#include "lldb/Utility/LLDBLog.h"40#include "lldb/Utility/Log.h"41#include "lldb/Utility/State.h"4243using namespace lldb_private;4445char ClangFunctionCaller::ID;4647// ClangFunctionCaller constructor48ClangFunctionCaller::ClangFunctionCaller(ExecutionContextScope &exe_scope,49const CompilerType &return_type,50const Address &functionAddress,51const ValueList &arg_value_list,52const char *name)53: FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list,54name),55m_type_system_helper(*this) {56m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());57// Can't make a ClangFunctionCaller without a process.58assert(m_jit_process_wp.lock());59}6061// Destructor62ClangFunctionCaller::~ClangFunctionCaller() = default;6364unsigned6566ClangFunctionCaller::CompileFunction(lldb::ThreadSP thread_to_use_sp,67DiagnosticManager &diagnostic_manager) {68if (m_compiled)69return 0;7071// Compilation might call code, make sure to keep on the thread the caller72// indicated.73ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(74thread_to_use_sp);7576// FIXME: How does clang tell us there's no return value? We need to handle77// that case.78unsigned num_errors = 0;7980std::string return_type_str(81m_function_return_type.GetTypeName().AsCString(""));8283// Cons up the function we're going to wrap our call in, then compile it...84// We declare the function "extern "C"" because the compiler might be in C++85// mode which would mangle the name and then we couldn't find it again...86m_wrapper_function_text.clear();87m_wrapper_function_text.append("extern \"C\" void ");88m_wrapper_function_text.append(m_wrapper_function_name);89m_wrapper_function_text.append(" (void *input)\n{\n struct ");90m_wrapper_function_text.append(m_wrapper_struct_name);91m_wrapper_function_text.append(" \n {\n");92m_wrapper_function_text.append(" ");93m_wrapper_function_text.append(return_type_str);94m_wrapper_function_text.append(" (*fn_ptr) (");9596// Get the number of arguments. If we have a function type and it is97// prototyped, trust that, otherwise use the values we were given.9899// FIXME: This will need to be extended to handle Variadic functions. We'll100// need101// to pull the defined arguments out of the function, then add the types from102// the arguments list for the variable arguments.103104uint32_t num_args = UINT32_MAX;105bool trust_function = false;106// GetArgumentCount returns -1 for an unprototyped function.107CompilerType function_clang_type;108if (m_function_ptr) {109function_clang_type = m_function_ptr->GetCompilerType();110if (function_clang_type) {111int num_func_args = function_clang_type.GetFunctionArgumentCount();112if (num_func_args >= 0) {113trust_function = true;114num_args = num_func_args;115}116}117}118119if (num_args == UINT32_MAX)120num_args = m_arg_values.GetSize();121122std::string args_buffer; // This one stores the definition of all the args in123// "struct caller".124std::string args_list_buffer; // This one stores the argument list called from125// the structure.126for (size_t i = 0; i < num_args; i++) {127std::string type_name;128129if (trust_function) {130type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i)131.GetTypeName()132.AsCString("");133} else {134CompilerType clang_qual_type =135m_arg_values.GetValueAtIndex(i)->GetCompilerType();136if (clang_qual_type) {137type_name = clang_qual_type.GetTypeName().AsCString("");138} else {139diagnostic_manager.Printf(140lldb::eSeverityError,141"Could not determine type of input value %" PRIu64 ".",142(uint64_t)i);143return 1;144}145}146147m_wrapper_function_text.append(type_name);148if (i < num_args - 1)149m_wrapper_function_text.append(", ");150151char arg_buf[32];152args_buffer.append(" ");153args_buffer.append(type_name);154snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);155args_buffer.push_back(' ');156args_buffer.append(arg_buf);157args_buffer.append(";\n");158159args_list_buffer.append("__lldb_fn_data->");160args_list_buffer.append(arg_buf);161if (i < num_args - 1)162args_list_buffer.append(", ");163}164m_wrapper_function_text.append(165");\n"); // Close off the function calling prototype.166167m_wrapper_function_text.append(args_buffer);168169m_wrapper_function_text.append(" ");170m_wrapper_function_text.append(return_type_str);171m_wrapper_function_text.append(" return_value;");172m_wrapper_function_text.append("\n };\n struct ");173m_wrapper_function_text.append(m_wrapper_struct_name);174m_wrapper_function_text.append("* __lldb_fn_data = (struct ");175m_wrapper_function_text.append(m_wrapper_struct_name);176m_wrapper_function_text.append(" *) input;\n");177178m_wrapper_function_text.append(179" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");180m_wrapper_function_text.append(args_list_buffer);181m_wrapper_function_text.append(");\n}\n");182183Log *log = GetLog(LLDBLog::Expressions);184LLDB_LOGF(log, "Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());185186// Okay, now compile this expression187188lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());189if (jit_process_sp) {190const bool generate_debug_info = true;191auto *clang_parser = new ClangExpressionParser(jit_process_sp.get(), *this,192generate_debug_info);193num_errors = clang_parser->Parse(diagnostic_manager);194m_parser.reset(clang_parser);195} else {196diagnostic_manager.PutString(lldb::eSeverityError,197"no process - unable to inject function");198num_errors = 1;199}200201m_compiled = (num_errors == 0);202203if (!m_compiled)204return num_errors;205206return num_errors;207}208209char ClangFunctionCaller::ClangFunctionCallerHelper::ID;210211clang::ASTConsumer *212ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer(213clang::ASTConsumer *passthrough) {214m_struct_extractor = std::make_unique<ASTStructExtractor>(215passthrough, m_owner.GetWrapperStructName(), m_owner);216217return m_struct_extractor.get();218}219220221