Coverage Report

Created: 2022-01-15 10:30

/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 <vector>
14
15
#include "llvm/ADT/DenseMap.h"
16
17
#include "lldb/Core/ValueObject.h"
18
#include "lldb/Utility/ConstString.h"
19
#include "lldb/lldb-public.h"
20
21
namespace lldb_private {
22
23
class ExpressionVariable
24
    : public std::enable_shared_from_this<ExpressionVariable> {
25
public:
26
  // See TypeSystem.h for how to add subclasses to this.
27
  enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
28
29
837k
  LLVMCastKind getKind() const { return m_kind; }
30
31
36.7k
  ExpressionVariable(LLVMCastKind kind) : m_flags(0), m_kind(kind) {}
32
33
  virtual ~ExpressionVariable();
34
35
8.92k
  llvm::Optional<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
36
37
349k
  ConstString GetName() { return m_frozen_sp->GetName(); }
38
39
25.6k
  lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
40
41
  uint8_t *GetValueBytes();
42
43
8.82k
  void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
44
45
16.5k
  RegisterInfo *GetRegisterInfo() {
46
16.5k
    return m_frozen_sp->GetValue().GetRegisterInfo();
47
16.5k
  }
48
49
33
  void SetRegisterInfo(const RegisterInfo *reg_info) {
50
33
    return m_frozen_sp->GetValue().SetContext(
51
33
        Value::ContextType::RegisterInfo, const_cast<RegisterInfo *>(reg_info));
52
33
  }
53
54
49
  CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
55
56
18.0k
  void SetCompilerType(const CompilerType &compiler_type) {
57
18.0k
    m_frozen_sp->GetValue().SetCompilerType(compiler_type);
58
18.0k
  }
59
60
18.0k
  void SetName(ConstString name) { m_frozen_sp->SetName(name); }
61
62
  // this function is used to copy the address-of m_live_sp into m_frozen_sp
63
  // this is necessary because the results of certain cast and pointer-
64
  // arithmetic operations (such as those described in bugzilla issues 11588
65
  // and 11618) generate frozen objects that do not have a valid address-of,
66
  // which can be troublesome when using synthetic children providers.
67
  // Transferring the address-of the live object solves these issues and
68
  // provides the expected user-level behavior
69
8.72k
  void TransferAddress(bool force = false) {
70
8.72k
    if (m_live_sp.get() == nullptr)
71
2.23k
      return;
72
73
6.49k
    if (m_frozen_sp.get() == nullptr)
74
0
      return;
75
76
6.49k
    if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
77
6.49k
      m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
78
6.49k
  }
79
80
  enum Flags {
81
    EVNone = 0,
82
    EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
83
                                ///specifically allocated for it by LLDB in the
84
                                ///target process
85
    EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
86
                                   ///(possibly invalid) area managed by the
87
                                   ///target program
88
    EVNeedsAllocation = 1 << 2,    ///< Space for this variable has yet to be
89
                                   ///allocated in the target process
90
    EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
91
                              ///m_frozen_sp (for example, for
92
                              ///statically-computed results)
93
    EVNeedsFreezeDry =
94
        1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
95
    EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
96
                             ///complete rather than freeze drying its contents
97
                             ///and freeing it
98
    EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
99
                                ///reference, so materialize the value rather
100
                                ///than the location
101
    EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
102
                            ///some other entity.
103
  };
104
105
  typedef uint16_t FlagType;
106
107
  FlagType m_flags; // takes elements of Flags
108
109
  // these should be private
110
  lldb::ValueObjectSP m_frozen_sp;
111
  lldb::ValueObjectSP m_live_sp;
112
  LLVMCastKind m_kind;
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
98.3k
  size_t GetSize() { return m_variables.size(); }
124
125
765k
  lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
126
765k
    lldb::ExpressionVariableSP var_sp;
127
765k
    if (index < m_variables.size())
128
765k
      var_sp = m_variables[index];
129
765k
    return var_sp;
130
765k
  }
131
132
16.7k
  size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
133
16.7k
    m_variables.push_back(var_sp);
134
16.7k
    return m_variables.size() - 1;
135
16.7k
  }
136
137
  lldb::ExpressionVariableSP
138
36.7k
  AddNewlyConstructedVariable(ExpressionVariable *var) {
139
36.7k
    lldb::ExpressionVariableSP var_sp(var);
140
36.7k
    m_variables.push_back(var_sp);
141
36.7k
    return m_variables.back();
142
36.7k
  }
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
9.56k
  lldb::ExpressionVariableSP GetVariable(ConstString name) {
162
9.56k
    lldb::ExpressionVariableSP var_sp;
163
341k
    for (size_t index = 0, size = GetSize(); index < size; 
++index332k
) {
164
332k
      var_sp = GetVariableAtIndex(index);
165
332k
      if (var_sp->GetName() == name)
166
150
        return var_sp;
167
332k
    }
168
9.41k
    var_sp.reset();
169
9.41k
    return var_sp;
170
9.56k
  }
171
172
144
  lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
173
144
    if (name.empty())
174
0
      return nullptr;
175
176
637
    
for (size_t index = 0, size = GetSize(); 144
index < size;
++index493
) {
177
543
      auto var_sp = GetVariableAtIndex(index);
178
543
      llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
179
543
      if (var_name_str == name)
180
50
        return var_sp;
181
543
    }
182
94
    return nullptr;
183
144
  }
184
185
77
  void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
186
77
    for (std::vector<lldb::ExpressionVariableSP>::iterator
187
77
             vi = m_variables.begin(),
188
77
             ve = m_variables.end();
189
78
         vi != ve; 
++vi1
) {
190
78
      if (vi->get() == var_sp.get()) {
191
77
        m_variables.erase(vi);
192
77
        return;
193
77
      }
194
78
    }
195
77
  }
196
197
0
  void Clear() { m_variables.clear(); }
198
199
private:
200
  std::vector<lldb::ExpressionVariableSP> m_variables;
201
};
202
203
class PersistentExpressionState : public ExpressionVariableList {
204
public:
205
  // See TypeSystem.h for how to add subclasses to this.
206
  enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
207
208
98.2k
  LLVMCastKind getKind() const { return m_kind; }
209
210
2.46k
  PersistentExpressionState(LLVMCastKind kind) : m_kind(kind) {}
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 llvm::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
  LLVMCastKind m_kind;
242
243
  typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
244
  ExecutionUnitSet
245
      m_execution_units; ///< The execution units that contain valuable symbols.
246
247
  typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
248
  SymbolMap
249
      m_symbol_map; ///< The addresses of the symbols in m_execution_units.
250
};
251
252
} // namespace lldb_private
253
254
#endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H