Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
39648 views
//===-- ClangUtilityFunction.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 "ClangUtilityFunction.h"9#include "ClangExpressionDeclMap.h"10#include "ClangExpressionParser.h"11#include "ClangExpressionSourceCode.h"12#include "ClangPersistentVariables.h"1314#include <cstdio>15#include <sys/types.h>161718#include "lldb/Core/Module.h"19#include "lldb/Expression/IRExecutionUnit.h"20#include "lldb/Host/Host.h"21#include "lldb/Target/ExecutionContext.h"22#include "lldb/Target/Target.h"23#include "lldb/Utility/ConstString.h"24#include "lldb/Utility/Log.h"25#include "lldb/Utility/Stream.h"2627using namespace lldb_private;2829char ClangUtilityFunction::ID;3031ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope,32std::string text, std::string name,33bool enable_debugging)34: UtilityFunction(35exe_scope,36std::string(ClangExpressionSourceCode::g_expression_prefix) + text +37std::string(ClangExpressionSourceCode::g_expression_suffix),38std::move(name), enable_debugging) {39// Write the source code to a file so that LLDB's source manager can display40// it when debugging the code.41if (enable_debugging) {42int temp_fd = -1;43llvm::SmallString<128> result_path;44llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);45if (temp_fd != -1) {46lldb_private::NativeFile file(temp_fd, File::eOpenOptionWriteOnly, true);47text = "#line 1 \"" + std::string(result_path) + "\"\n" + text;48size_t bytes_written = text.size();49file.Write(text.c_str(), bytes_written);50if (bytes_written == text.size()) {51// If we successfully wrote the source to a temporary file, replace the52// function text with the next text containing the line directive.53m_function_text =54std::string(ClangExpressionSourceCode::g_expression_prefix) + text +55std::string(ClangExpressionSourceCode::g_expression_suffix);56}57file.Close();58}59}60}6162ClangUtilityFunction::~ClangUtilityFunction() = default;6364/// Install the utility function into a process65///66/// \param[in] diagnostic_manager67/// A diagnostic manager to report errors and warnings to.68///69/// \param[in] exe_ctx70/// The execution context to install the utility function to.71///72/// \return73/// True on success (no errors); false otherwise.74bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager,75ExecutionContext &exe_ctx) {76if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {77diagnostic_manager.PutString(lldb::eSeverityWarning, "already installed");78return false;79}8081////////////////////////////////////82// Set up the target and compiler83//8485Target *target = exe_ctx.GetTargetPtr();8687if (!target) {88diagnostic_manager.PutString(lldb::eSeverityError, "invalid target");89return false;90}9192Process *process = exe_ctx.GetProcessPtr();9394if (!process) {95diagnostic_manager.PutString(lldb::eSeverityError, "invalid process");96return false;97}9899// Since we might need to call allocate memory and maybe call code to make100// the caller, we need to be stopped.101if (process->GetState() != lldb::eStateStopped) {102diagnostic_manager.PutString(lldb::eSeverityError, "process running");103return false;104}105//////////////////////////106// Parse the expression107//108109bool keep_result_in_memory = false;110111ResetDeclMap(exe_ctx, keep_result_in_memory);112113if (!DeclMap()->WillParse(exe_ctx, nullptr)) {114diagnostic_manager.PutString(115lldb::eSeverityError,116"current process state is unsuitable for expression parsing");117return false;118}119120const bool generate_debug_info = true;121ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this,122generate_debug_info);123124unsigned num_errors = parser.Parse(diagnostic_manager);125126if (num_errors) {127ResetDeclMap();128129return false;130}131132//////////////////////////////////133// JIT the output of the parser134//135136bool can_interpret = false; // should stay that way137138Status jit_error = parser.PrepareForExecution(139m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,140can_interpret, eExecutionPolicyAlways);141142if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {143m_jit_process_wp = process->shared_from_this();144if (parser.GetGenerateDebugInfo()) {145lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());146147if (jit_module_sp) {148ConstString const_func_name(FunctionName());149FileSpec jit_file;150jit_file.SetFilename(const_func_name);151jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());152m_jit_module_wp = jit_module_sp;153target->GetImages().Append(jit_module_sp);154}155}156}157158DeclMap()->DidParse();159160ResetDeclMap();161162if (jit_error.Success()) {163return true;164} else {165const char *error_cstr = jit_error.AsCString();166if (error_cstr && error_cstr[0]) {167diagnostic_manager.Printf(lldb::eSeverityError, "%s", error_cstr);168} else {169diagnostic_manager.PutString(lldb::eSeverityError,170"expression can't be interpreted or run");171}172return false;173}174}175176char ClangUtilityFunction::ClangUtilityFunctionHelper::ID;177178void ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap(179ExecutionContext &exe_ctx, bool keep_result_in_memory) {180std::shared_ptr<ClangASTImporter> ast_importer;181auto *state = exe_ctx.GetTargetSP()->GetPersistentExpressionStateForLanguage(182lldb::eLanguageTypeC);183if (state) {184auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);185ast_importer = persistent_vars->GetClangASTImporter();186}187m_expr_decl_map_up = std::make_unique<ClangExpressionDeclMap>(188keep_result_in_memory, nullptr, exe_ctx.GetTargetSP(), ast_importer,189nullptr);190}191192193