Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ClangUtilityFunction.cpp ------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "ClangUtilityFunction.h"
10
#include "ClangExpressionDeclMap.h"
11
#include "ClangExpressionParser.h"
12
#include "ClangExpressionSourceCode.h"
13
#include "ClangPersistentVariables.h"
14
15
#include <cstdio>
16
#include <sys/types.h>
17
18
19
#include "lldb/Core/Module.h"
20
#include "lldb/Expression/IRExecutionUnit.h"
21
#include "lldb/Host/Host.h"
22
#include "lldb/Target/ExecutionContext.h"
23
#include "lldb/Target/Target.h"
24
#include "lldb/Utility/ConstString.h"
25
#include "lldb/Utility/Log.h"
26
#include "lldb/Utility/Stream.h"
27
28
using namespace lldb_private;
29
30
char ClangUtilityFunction::ID;
31
32
ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope,
33
                                           std::string text, std::string name,
34
                                           bool enable_debugging)
35
2.66k
    : UtilityFunction(
36
2.66k
          exe_scope,
37
2.66k
          std::string(ClangExpressionSourceCode::g_expression_prefix) + text +
38
2.66k
              std::string(ClangExpressionSourceCode::g_expression_suffix),
39
2.66k
          std::move(name), enable_debugging) {
40
  // Write the source code to a file so that LLDB's source manager can display
41
  // it when debugging the code.
42
2.66k
  if (enable_debugging) {
43
0
    int temp_fd = -1;
44
0
    llvm::SmallString<128> result_path;
45
0
    llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
46
0
    if (temp_fd != -1) {
47
0
      lldb_private::NativeFile file(temp_fd, File::eOpenOptionWriteOnly, true);
48
0
      text = "#line 1 \"" + std::string(result_path) + "\"\n" + text;
49
0
      size_t bytes_written = text.size();
50
0
      file.Write(text.c_str(), bytes_written);
51
0
      if (bytes_written == text.size()) {
52
        // If we successfully wrote the source to a temporary file, replace the
53
        // function text with the next text containing the line directive.
54
0
        m_function_text =
55
0
            std::string(ClangExpressionSourceCode::g_expression_prefix) + text +
56
0
            std::string(ClangExpressionSourceCode::g_expression_suffix);
57
0
      }
58
0
      file.Close();
59
0
    }
60
0
  }
61
2.66k
}
62
63
2.66k
ClangUtilityFunction::~ClangUtilityFunction() = default;
64
65
/// Install the utility function into a process
66
///
67
/// \param[in] diagnostic_manager
68
///     A diagnostic manager to report errors and warnings to.
69
///
70
/// \param[in] exe_ctx
71
///     The execution context to install the utility function to.
72
///
73
/// \return
74
///     True on success (no errors); false otherwise.
75
bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager,
76
2.66k
                                   ExecutionContext &exe_ctx) {
77
2.66k
  if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
78
0
    diagnostic_manager.PutString(eDiagnosticSeverityWarning,
79
0
                                 "already installed");
80
0
    return false;
81
0
  }
82
83
  ////////////////////////////////////
84
  // Set up the target and compiler
85
  //
86
87
2.66k
  Target *target = exe_ctx.GetTargetPtr();
88
89
2.66k
  if (!target) {
90
0
    diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
91
0
    return false;
92
0
  }
93
94
2.66k
  Process *process = exe_ctx.GetProcessPtr();
95
96
2.66k
  if (!process) {
97
0
    diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid process");
98
0
    return false;
99
0
  }
100
101
  // Since we might need to call allocate memory and maybe call code to make
102
  // the caller, we need to be stopped.
103
2.66k
  if (process->GetState() != lldb::eStateStopped) {
104
0
    diagnostic_manager.PutString(eDiagnosticSeverityError, "process running");
105
0
    return false;
106
0
  }
107
  //////////////////////////
108
  // Parse the expression
109
  //
110
111
2.66k
  bool keep_result_in_memory = false;
112
113
2.66k
  ResetDeclMap(exe_ctx, keep_result_in_memory);
114
115
2.66k
  if (!DeclMap()->WillParse(exe_ctx, nullptr)) {
116
0
    diagnostic_manager.PutString(
117
0
        eDiagnosticSeverityError,
118
0
        "current process state is unsuitable for expression parsing");
119
0
    return false;
120
0
  }
121
122
2.66k
  const bool generate_debug_info = true;
123
2.66k
  ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this,
124
2.66k
                               generate_debug_info);
125
126
2.66k
  unsigned num_errors = parser.Parse(diagnostic_manager);
127
128
2.66k
  if (num_errors) {
129
0
    ResetDeclMap();
130
131
0
    return false;
132
0
  }
133
134
  //////////////////////////////////
135
  // JIT the output of the parser
136
  //
137
138
2.66k
  bool can_interpret = false; // should stay that way
139
140
2.66k
  Status jit_error = parser.PrepareForExecution(
141
2.66k
      m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
142
2.66k
      can_interpret, eExecutionPolicyAlways);
143
144
2.66k
  if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
145
2.66k
    m_jit_process_wp = process->shared_from_this();
146
2.66k
    if (parser.GetGenerateDebugInfo()) {
147
2.66k
      lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
148
149
2.66k
      if (jit_module_sp) {
150
2.66k
        ConstString const_func_name(FunctionName());
151
2.66k
        FileSpec jit_file;
152
2.66k
        jit_file.SetFilename(const_func_name);
153
2.66k
        jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
154
2.66k
        m_jit_module_wp = jit_module_sp;
155
2.66k
        target->GetImages().Append(jit_module_sp);
156
2.66k
      }
157
2.66k
    }
158
2.66k
  }
159
160
2.66k
  DeclMap()->DidParse();
161
162
2.66k
  ResetDeclMap();
163
164
2.66k
  if (jit_error.Success()) {
165
2.66k
    return true;
166
2.66k
  } else {
167
0
    const char *error_cstr = jit_error.AsCString();
168
0
    if (error_cstr && error_cstr[0]) {
169
0
      diagnostic_manager.Printf(eDiagnosticSeverityError, "%s", error_cstr);
170
0
    } else {
171
0
      diagnostic_manager.PutString(eDiagnosticSeverityError,
172
0
                                   "expression can't be interpreted or run");
173
0
    }
174
0
    return false;
175
0
  }
176
2.66k
}
177
178
char ClangUtilityFunction::ClangUtilityFunctionHelper::ID;
179
180
void ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap(
181
2.66k
    ExecutionContext &exe_ctx, bool keep_result_in_memory) {
182
2.66k
  std::shared_ptr<ClangASTImporter> ast_importer;
183
2.66k
  auto *state = exe_ctx.GetTargetSP()->GetPersistentExpressionStateForLanguage(
184
2.66k
      lldb::eLanguageTypeC);
185
2.66k
  if (state) {
186
2.66k
    auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
187
2.66k
    ast_importer = persistent_vars->GetClangASTImporter();
188
2.66k
  }
189
2.66k
  m_expr_decl_map_up = std::make_unique<ClangExpressionDeclMap>(
190
2.66k
      keep_result_in_memory, nullptr, exe_ctx.GetTargetSP(), ast_importer,
191
2.66k
      nullptr);
192
2.66k
}