Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Expression/ExpressionVariable.h
Line
Count
Source (jump to first uncovered line)
1
//===-- ExpressionVariable.h ------------------------------------*- C++ -*-===//
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
#ifndef LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
10
#define LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
11
12
#include <memory>
13
#include <optional>
14
#include <vector>
15
16
#include "llvm/ADT/DenseMap.h"
17
18
#include "lldb/Core/ValueObject.h"
19
#include "lldb/Utility/ConstString.h"
20
#include "lldb/lldb-public.h"
21
#include "llvm/Support/ExtensibleRTTI.h"
22
23
namespace lldb_private {
24
25
class ExpressionVariable
26
    : public std::enable_shared_from_this<ExpressionVariable>,
27
      public llvm::RTTIExtends<ExpressionVariable, llvm::RTTIRoot> {
28
public:
29
  /// LLVM RTTI support
30
  static char ID;
31
32
  ExpressionVariable();
33
34
27.8k
  virtual ~ExpressionVariable() = default;
35
36
7.03k
  std::optional<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
37
38
246k
  ConstString GetName() { return m_frozen_sp->GetName(); }
39
40
19.8k
  lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
41
42
  uint8_t *GetValueBytes();
43
44
6.95k
  void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
45
46
12.6k
  RegisterInfo *GetRegisterInfo() {
47
12.6k
    return m_frozen_sp->GetValue().GetRegisterInfo();
48
12.6k
  }
49
50
31
  void SetRegisterInfo(const RegisterInfo *reg_info) {
51
31
    return m_frozen_sp->GetValue().SetContext(
52
31
        Value::ContextType::RegisterInfo, const_cast<RegisterInfo *>(reg_info));
53
31
  }
54
55
37
  CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
56
57
14.0k
  void SetCompilerType(const CompilerType &compiler_type) {
58
14.0k
    m_frozen_sp->GetValue().SetCompilerType(compiler_type);
59
14.0k
  }
60
61
14.0k
  void SetName(ConstString name) { m_frozen_sp->SetName(name); }
62
63
  // this function is used to copy the address-of m_live_sp into m_frozen_sp
64
  // this is necessary because the results of certain cast and pointer-
65
  // arithmetic operations (such as those described in bugzilla issues 11588
66
  // and 11618) generate frozen objects that do not have a valid address-of,
67
  // which can be troublesome when using synthetic children providers.
68
  // Transferring the address-of the live object solves these issues and
69
  // provides the expected user-level behavior
70
6.87k
  void TransferAddress(bool force = false) {
71
6.87k
    if (m_live_sp.get() == nullptr)
72
2.03k
      return;
73
74
4.84k
    if (m_frozen_sp.get() == nullptr)
75
0
      return;
76
77
4.84k
    if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
78
4.84k
      m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
79
4.84k
  }
80
81
  enum Flags {
82
    EVNone = 0,
83
    EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
84
                                ///specifically allocated for it by LLDB in the
85
                                ///target process
86
    EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
87
                                   ///(possibly invalid) area managed by the
88
                                   ///target program
89
    EVNeedsAllocation = 1 << 2,    ///< Space for this variable has yet to be
90
                                   ///allocated in the target process
91
    EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
92
                              ///m_frozen_sp (for example, for
93
                              ///statically-computed results)
94
    EVNeedsFreezeDry =
95
        1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
96
    EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
97
                             ///complete rather than freeze drying its contents
98
                             ///and freeing it
99
    EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
100
                                ///reference, so materialize the value rather
101
                                ///than the location
102
    EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
103
                            ///some other entity.
104
  };
105
106
  typedef uint16_t FlagType;
107
108
  FlagType m_flags; // takes elements of Flags
109
110
  // these should be private
111
  lldb::ValueObjectSP m_frozen_sp;
112
  lldb::ValueObjectSP m_live_sp;
113
};
114
115
/// \class ExpressionVariableList ExpressionVariable.h
116
/// "lldb/Expression/ExpressionVariable.h"
117
/// A list of variable references.
118
///
119
/// This class stores variables internally, acting as the permanent store.
120
class ExpressionVariableList {
121
public:
122
  /// Implementation of methods in ExpressionVariableListBase
123
76.2k
  size_t GetSize() { return m_variables.size(); }
124
125
541k
  lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
126
541k
    lldb::ExpressionVariableSP var_sp;
127
541k
    if (index < m_variables.size())
128
541k
      var_sp = m_variables[index];
129
541k
    return var_sp;
130
541k
  }
131
132
12.8k
  size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
133
12.8k
    m_variables.push_back(var_sp);
134
12.8k
    return m_variables.size() - 1;
135
12.8k
  }
136
137
  lldb::ExpressionVariableSP
138
27.9k
  AddNewlyConstructedVariable(ExpressionVariable *var) {
139
27.9k
    lldb::ExpressionVariableSP var_sp(var);
140
27.9k
    m_variables.push_back(var_sp);
141
27.9k
    return m_variables.back();
142
27.9k
  }
143
144
0
  bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
145
0
    const size_t size = m_variables.size();
146
0
    for (size_t index = 0; index < size; ++index) {
147
0
      if (m_variables[index].get() == var_sp.get())
148
0
        return true;
149
0
    }
150
0
    return false;
151
0
  }
152
153
  /// Finds a variable by name in the list.
154
  ///
155
  /// \param[in] name
156
  ///     The name of the requested variable.
157
  ///
158
  /// \return
159
  ///     The variable requested, or nullptr if that variable is not in the
160
  ///     list.
161
7.54k
  lldb::ExpressionVariableSP GetVariable(ConstString name) {
162
7.54k
    lldb::ExpressionVariableSP var_sp;
163
239k
    for (size_t index = 0, size = GetSize(); index < size; 
++index232k
) {
164
232k
      var_sp = GetVariableAtIndex(index);
165
232k
      if (var_sp->GetName() == name)
166
238
        return var_sp;
167
232k
    }
168
7.30k
    var_sp.reset();
169
7.30k
    return var_sp;
170
7.54k
  }
171
172
120
  lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
173
120
    if (name.empty())
174
0
      return nullptr;
175
176
493
    
for (size_t index = 0, size = GetSize(); 120
index < size;
++index373
) {
177
415
      auto var_sp = GetVariableAtIndex(index);
178
415
      llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
179
415
      if (var_name_str == name)
180
42
        return var_sp;
181
415
    }
182
78
    return nullptr;
183
120
  }
184
185
275
  void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
186
275
    for (std::vector<lldb::ExpressionVariableSP>::iterator
187
275
             vi = m_variables.begin(),
188
275
             ve = m_variables.end();
189
473
         vi != ve; 
++vi198
) {
190
473
      if (vi->get() == var_sp.get()) {
191
275
        m_variables.erase(vi);
192
275
        return;
193
275
      }
194
473
    }
195
275
  }
196
197
0
  void Clear() { m_variables.clear(); }
198
199
private:
200
  std::vector<lldb::ExpressionVariableSP> m_variables;
201
};
202
203
class PersistentExpressionState
204
    : public ExpressionVariableList,
205
      public llvm::RTTIExtends<PersistentExpressionState, llvm::RTTIRoot> {
206
public:
207
  /// LLVM RTTI support
208
  static char ID;
209
210
  PersistentExpressionState();
211
212
  virtual ~PersistentExpressionState();
213
214
  virtual lldb::ExpressionVariableSP
215
  CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
216
217
  virtual lldb::ExpressionVariableSP
218
  CreatePersistentVariable(ExecutionContextScope *exe_scope,
219
                           ConstString name, const CompilerType &type,
220
                           lldb::ByteOrder byte_order,
221
                           uint32_t addr_byte_size) = 0;
222
223
  /// Return a new persistent variable name with the specified prefix.
224
  virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
225
226
  virtual void
227
  RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
228
229
  virtual std::optional<CompilerType>
230
  GetCompilerTypeFromPersistentDecl(ConstString type_name) = 0;
231
232
  virtual lldb::addr_t LookupSymbol(ConstString name);
233
234
  void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
235
236
protected:
237
  virtual llvm::StringRef
238
  GetPersistentVariablePrefix(bool is_error = false) const = 0;
239
240
private:
241
  typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
242
  ExecutionUnitSet
243
      m_execution_units; ///< The execution units that contain valuable symbols.
244
245
  typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
246
  SymbolMap
247
      m_symbol_map; ///< The addresses of the symbols in m_execution_units.
248
};
249
250
} // namespace lldb_private
251
252
#endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H