Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/ValueObjectMemory.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ValueObjectMemory.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 "lldb/Core/ValueObjectMemory.h"
10
#include "lldb/Core/Value.h"
11
#include "lldb/Core/ValueObject.h"
12
#include "lldb/Symbol/Type.h"
13
#include "lldb/Target/ExecutionContext.h"
14
#include "lldb/Target/Target.h"
15
#include "lldb/Utility/DataExtractor.h"
16
#include "lldb/Utility/Scalar.h"
17
#include "lldb/Utility/Status.h"
18
#include "lldb/lldb-types.h"
19
#include "llvm/Support/ErrorHandling.h"
20
21
#include <cassert>
22
#include <memory>
23
#include <optional>
24
25
namespace lldb_private {
26
class ExecutionContextScope;
27
}
28
29
using namespace lldb;
30
using namespace lldb_private;
31
32
ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
33
                                        llvm::StringRef name,
34
                                        const Address &address,
35
0
                                        lldb::TypeSP &type_sp) {
36
0
  auto manager_sp = ValueObjectManager::Create();
37
0
  return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
38
0
      ->GetSP();
39
0
}
40
41
ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
42
                                        llvm::StringRef name,
43
                                        const Address &address,
44
253
                                        const CompilerType &ast_type) {
45
253
  auto manager_sp = ValueObjectManager::Create();
46
253
  return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
47
253
                                ast_type))
48
253
      ->GetSP();
49
253
}
50
51
ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
52
                                     ValueObjectManager &manager,
53
                                     llvm::StringRef name,
54
                                     const Address &address,
55
                                     lldb::TypeSP &type_sp)
56
0
    : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
57
0
      m_compiler_type() {
58
  // Do not attempt to construct one of these objects with no variable!
59
0
  assert(m_type_sp.get() != nullptr);
60
0
  SetName(ConstString(name));
61
0
  m_value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
62
0
  TargetSP target_sp(GetTargetSP());
63
0
  lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
64
0
  if (load_address != LLDB_INVALID_ADDRESS) {
65
0
    m_value.SetValueType(Value::ValueType::LoadAddress);
66
0
    m_value.GetScalar() = load_address;
67
0
  } else {
68
0
    lldb::addr_t file_address = m_address.GetFileAddress();
69
0
    if (file_address != LLDB_INVALID_ADDRESS) {
70
0
      m_value.SetValueType(Value::ValueType::FileAddress);
71
0
      m_value.GetScalar() = file_address;
72
0
    } else {
73
0
      m_value.GetScalar() = m_address.GetOffset();
74
0
      m_value.SetValueType(Value::ValueType::Scalar);
75
0
    }
76
0
  }
77
0
}
78
79
ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
80
                                     ValueObjectManager &manager,
81
                                     llvm::StringRef name,
82
                                     const Address &address,
83
                                     const CompilerType &ast_type)
84
253
    : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
85
253
      m_compiler_type(ast_type) {
86
  // Do not attempt to construct one of these objects with no variable!
87
253
  assert(m_compiler_type.IsValid());
88
89
253
  TargetSP target_sp(GetTargetSP());
90
91
253
  SetName(ConstString(name));
92
253
  m_value.SetCompilerType(m_compiler_type);
93
253
  lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
94
253
  if (load_address != LLDB_INVALID_ADDRESS) {
95
253
    m_value.SetValueType(Value::ValueType::LoadAddress);
96
253
    m_value.GetScalar() = load_address;
97
253
  } else {
98
0
    lldb::addr_t file_address = m_address.GetFileAddress();
99
0
    if (file_address != LLDB_INVALID_ADDRESS) {
100
0
      m_value.SetValueType(Value::ValueType::FileAddress);
101
0
      m_value.GetScalar() = file_address;
102
0
    } else {
103
0
      m_value.GetScalar() = m_address.GetOffset();
104
0
      m_value.SetValueType(Value::ValueType::Scalar);
105
0
    }
106
0
  }
107
253
}
108
109
253
ValueObjectMemory::~ValueObjectMemory() = default;
110
111
1.38k
CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
112
1.38k
  if (m_type_sp)
113
0
    return m_type_sp->GetForwardCompilerType();
114
1.38k
  return m_compiler_type;
115
1.38k
}
116
117
0
ConstString ValueObjectMemory::GetTypeName() {
118
0
  if (m_type_sp)
119
0
    return m_type_sp->GetName();
120
0
  return m_compiler_type.GetTypeName();
121
0
}
122
123
19
ConstString ValueObjectMemory::GetDisplayTypeName() {
124
19
  if (m_type_sp)
125
0
    return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
126
19
  return m_compiler_type.GetDisplayTypeName();
127
19
}
128
129
19
size_t ValueObjectMemory::CalculateNumChildren(uint32_t max) {
130
19
  if (m_type_sp) {
131
0
    auto child_count = m_type_sp->GetNumChildren(true);
132
0
    return child_count <= max ? child_count : max;
133
0
  }
134
135
19
  ExecutionContext exe_ctx(GetExecutionContextRef());
136
19
  const bool omit_empty_base_classes = true;
137
19
  auto child_count =
138
19
      m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
139
19
  return child_count <= max ? child_count : 
max0
;
140
19
}
141
142
0
std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
143
0
  ExecutionContext exe_ctx(GetExecutionContextRef());
144
0
  if (m_type_sp)
145
0
    return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
146
0
  return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
147
0
}
148
149
0
lldb::ValueType ValueObjectMemory::GetValueType() const {
150
  // RETHINK: Should this be inherited from somewhere?
151
0
  return lldb::eValueTypeVariableGlobal;
152
0
}
153
154
253
bool ValueObjectMemory::UpdateValue() {
155
253
  SetValueIsValid(false);
156
253
  m_error.Clear();
157
158
253
  ExecutionContext exe_ctx(GetExecutionContextRef());
159
160
253
  Target *target = exe_ctx.GetTargetPtr();
161
253
  if (target) {
162
253
    m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
163
253
    m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
164
253
  }
165
166
253
  Value old_value(m_value);
167
253
  if (m_address.IsValid()) {
168
253
    Value::ValueType value_type = m_value.GetValueType();
169
170
253
    switch (value_type) {
171
0
    case Value::ValueType::Invalid:
172
0
      m_error.SetErrorString("Invalid value");
173
0
      return false;
174
0
    case Value::ValueType::Scalar:
175
      // The variable value is in the Scalar value inside the m_value. We can
176
      // point our m_data right to it.
177
0
      m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
178
0
      break;
179
180
0
    case Value::ValueType::FileAddress:
181
253
    case Value::ValueType::LoadAddress:
182
253
    case Value::ValueType::HostAddress:
183
      // The DWARF expression result was an address in the inferior process. If
184
      // this variable is an aggregate type, we just need the address as the
185
      // main value as all child variable objects will rely upon this location
186
      // and add an offset and then read their own values as needed. If this
187
      // variable is a simple type, we read all data for it into m_data. Make
188
      // sure this type has a value before we try and read it
189
190
      // If we have a file address, convert it to a load address if we can.
191
253
      if (value_type == Value::ValueType::FileAddress &&
192
253
          
exe_ctx.GetProcessPtr()0
) {
193
0
        lldb::addr_t load_addr = m_address.GetLoadAddress(target);
194
0
        if (load_addr != LLDB_INVALID_ADDRESS) {
195
0
          m_value.SetValueType(Value::ValueType::LoadAddress);
196
0
          m_value.GetScalar() = load_addr;
197
0
        }
198
0
      }
199
200
253
      if (!CanProvideValue()) {
201
        // this value object represents an aggregate type whose children have
202
        // values, but this object does not. So we say we are changed if our
203
        // location has changed.
204
14
        SetValueDidChange(value_type != old_value.GetValueType() ||
205
14
                          m_value.GetScalar() != old_value.GetScalar());
206
239
      } else {
207
        // Copy the Value and set the context to use our Variable so it can
208
        // extract read its value into m_data appropriately
209
239
        Value value(m_value);
210
239
        if (m_type_sp)
211
0
          value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
212
239
        else {
213
239
          value.SetCompilerType(m_compiler_type);
214
239
        }
215
216
239
        m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
217
239
      }
218
253
      break;
219
253
    }
220
221
253
    SetValueIsValid(m_error.Success());
222
253
  }
223
253
  return m_error.Success();
224
253
}
225
226
298
bool ValueObjectMemory::IsInScope() {
227
  // FIXME: Maybe try to read the memory address, and if that works, then
228
  // we are in scope?
229
298
  return true;
230
298
}
231
232
516
lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }