Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/ValueObjectChild.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ValueObjectChild.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/ValueObjectChild.h"
10
11
#include "lldb/Core/Value.h"
12
#include "lldb/Symbol/CompilerType.h"
13
#include "lldb/Target/ExecutionContext.h"
14
#include "lldb/Target/Process.h"
15
#include "lldb/Utility/Flags.h"
16
#include "lldb/Utility/Scalar.h"
17
#include "lldb/Utility/Status.h"
18
#include "lldb/lldb-forward.h"
19
20
#include <functional>
21
#include <memory>
22
#include <vector>
23
24
#include <cstdio>
25
#include <cstring>
26
27
using namespace lldb_private;
28
29
ValueObjectChild::ValueObjectChild(
30
    ValueObject &parent, const CompilerType &compiler_type,
31
    ConstString name, uint64_t byte_size, int32_t byte_offset,
32
    uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
33
    bool is_base_class, bool is_deref_of_parent,
34
    AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
35
    : ValueObject(parent), m_compiler_type(compiler_type),
36
      m_byte_size(byte_size), m_byte_offset(byte_offset),
37
      m_bitfield_bit_size(bitfield_bit_size),
38
      m_bitfield_bit_offset(bitfield_bit_offset),
39
      m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),
40
56.9k
      m_can_update_with_invalid_exe_ctx() {
41
56.9k
  m_name = name;
42
56.9k
  SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
43
56.9k
  SetLanguageFlags(language_flags);
44
56.9k
}
45
46
33.3k
ValueObjectChild::~ValueObjectChild() = default;
47
48
52
lldb::ValueType ValueObjectChild::GetValueType() const {
49
52
  return m_parent->GetValueType();
50
52
}
51
52
44.3k
size_t ValueObjectChild::CalculateNumChildren(uint32_t max) {
53
44.3k
  ExecutionContext exe_ctx(GetExecutionContextRef());
54
44.3k
  auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55
44.3k
  return children_count <= max ? children_count : 
max0
;
56
44.3k
}
57
58
static void AdjustForBitfieldness(ConstString &name,
59
160k
                                  uint8_t bitfield_bit_size) {
60
160k
  if (name && bitfield_bit_size)
61
1.83k
    name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());
62
160k
}
63
64
806
ConstString ValueObjectChild::GetTypeName() {
65
806
  if (m_type_name.IsEmpty()) {
66
437
    m_type_name = GetCompilerType().GetTypeName();
67
437
    AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);
68
437
  }
69
806
  return m_type_name;
70
806
}
71
72
150k
ConstString ValueObjectChild::GetQualifiedTypeName() {
73
150k
  ConstString qualified_name = GetCompilerType().GetTypeName();
74
150k
  AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);
75
150k
  return qualified_name;
76
150k
}
77
78
9.20k
ConstString ValueObjectChild::GetDisplayTypeName() {
79
9.20k
  ConstString display_name = GetCompilerType().GetDisplayTypeName();
80
9.20k
  AdjustForBitfieldness(display_name, m_bitfield_bit_size);
81
9.20k
  return display_name;
82
9.20k
}
83
84
118M
LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {
85
118M
  if (m_can_update_with_invalid_exe_ctx.hasValue())
86
118M
    return m_can_update_with_invalid_exe_ctx.getValue();
87
55.8k
  if (m_parent) {
88
55.8k
    ValueObject *opinionated_parent =
89
118M
        m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
90
118M
          return (valobj->CanUpdateWithInvalidExecutionContext() ==
91
118M
                  eLazyBoolCalculate);
92
118M
        });
93
55.8k
    if (opinionated_parent)
94
847
      return (m_can_update_with_invalid_exe_ctx =
95
847
                  opinionated_parent->CanUpdateWithInvalidExecutionContext())
96
847
          .getValue();
97
55.8k
  }
98
54.9k
  return (m_can_update_with_invalid_exe_ctx =
99
54.9k
              this->ValueObject::CanUpdateWithInvalidExecutionContext())
100
54.9k
      .getValue();
101
55.8k
}
102
103
56.1k
bool ValueObjectChild::UpdateValue() {
104
56.1k
  m_error.Clear();
105
56.1k
  SetValueIsValid(false);
106
56.1k
  ValueObject *parent = m_parent;
107
56.1k
  if (parent) {
108
56.1k
    if (parent->UpdateValueIfNeeded(false)) {
109
56.0k
      m_value.SetCompilerType(GetCompilerType());
110
111
56.0k
      CompilerType parent_type(parent->GetCompilerType());
112
      // Copy the parent scalar value and the scalar value type
113
56.0k
      m_value.GetScalar() = parent->GetValue().GetScalar();
114
56.0k
      m_value.SetValueType(parent->GetValue().GetValueType());
115
116
56.0k
      Flags parent_type_flags(parent_type.GetTypeInfo());
117
56.0k
      const bool is_instance_ptr_base =
118
56.0k
          ((m_is_base_class) &&
119
56.0k
           
(parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer))7.16k
);
120
121
56.0k
      if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {
122
28.6k
        m_value.GetScalar() = parent->GetPointerValue();
123
124
28.6k
        switch (parent->GetAddressTypeOfChildren()) {
125
33
        case eAddressTypeFile: {
126
33
          lldb::ProcessSP process_sp(GetProcessSP());
127
33
          if (process_sp && 
process_sp->IsAlive()9
)
128
0
            m_value.SetValueType(Value::ValueType::LoadAddress);
129
33
          else
130
33
            m_value.SetValueType(Value::ValueType::FileAddress);
131
33
        } break;
132
28.6k
        case eAddressTypeLoad:
133
28.6k
          m_value.SetValueType(is_instance_ptr_base
134
28.6k
                                   ? 
Value::ValueType::Scalar0
135
28.6k
                                   : Value::ValueType::LoadAddress);
136
28.6k
          break;
137
0
        case eAddressTypeHost:
138
0
          m_value.SetValueType(Value::ValueType::HostAddress);
139
0
          break;
140
0
        case eAddressTypeInvalid:
141
          // TODO: does this make sense?
142
0
          m_value.SetValueType(Value::ValueType::Scalar);
143
0
          break;
144
28.6k
        }
145
28.6k
      }
146
56.0k
      switch (m_value.GetValueType()) {
147
0
      case Value::ValueType::Invalid:
148
0
        break;
149
44.4k
      case Value::ValueType::LoadAddress:
150
44.5k
      case Value::ValueType::FileAddress:
151
56.0k
      case Value::ValueType::HostAddress: {
152
56.0k
        lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
153
56.0k
        if (addr == LLDB_INVALID_ADDRESS) {
154
0
          m_error.SetErrorString("parent address is invalid.");
155
56.0k
        } else if (addr == 0) {
156
167
          m_error.SetErrorString("parent is NULL");
157
55.9k
        } else {
158
          // If a bitfield doesn't fit into the child_byte_size'd window at
159
          // child_byte_offset, move the window forward until it fits.  The
160
          // problem here is that Value has no notion of bitfields and thus the
161
          // Value's DataExtractor is sized like the bitfields CompilerType; a
162
          // sequence of bitfields, however, can be larger than their underlying
163
          // type.
164
55.9k
          if (m_bitfield_bit_offset) {
165
280
            const bool thread_and_frame_only_if_stopped = true;
166
280
            ExecutionContext exe_ctx(GetExecutionContextRef().Lock(
167
280
                thread_and_frame_only_if_stopped));
168
280
            if (auto type_bit_size = GetCompilerType().GetBitSize(
169
280
                    exe_ctx.GetBestExecutionContextScope())) {
170
280
              uint64_t bitfield_end =
171
280
                  m_bitfield_bit_size + m_bitfield_bit_offset;
172
280
              if (bitfield_end > *type_bit_size) {
173
15
                uint64_t overhang_bytes =
174
15
                    (bitfield_end - *type_bit_size + 7) / 8;
175
15
                m_byte_offset += overhang_bytes;
176
15
                m_bitfield_bit_offset -= overhang_bytes * 8;
177
15
              }
178
280
            }
179
280
          }
180
181
          // Set this object's scalar value to the address of its value by
182
          // adding its byte offset to the parent address
183
55.9k
          m_value.GetScalar() += m_byte_offset;
184
55.9k
        }
185
56.0k
      } break;
186
187
8
      case Value::ValueType::Scalar:
188
        // try to extract the child value from the parent's scalar value
189
8
        {
190
8
          Scalar scalar(m_value.GetScalar());
191
8
          scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);
192
8
          m_value.GetScalar() = scalar;
193
8
        }
194
8
        break;
195
56.0k
      }
196
197
56.0k
      if (m_error.Success()) {
198
55.9k
        const bool thread_and_frame_only_if_stopped = true;
199
55.9k
        ExecutionContext exe_ctx(
200
55.9k
            GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
201
55.9k
        if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
202
40.4k
          Value &value = is_instance_ptr_base ? 
m_parent->GetValue()0
: m_value;
203
40.4k
          m_error =
204
40.4k
              value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
205
40.4k
        } else {
206
15.4k
          m_error.Clear(); // No value so nothing to read...
207
15.4k
        }
208
55.9k
      }
209
210
56.0k
    } else {
211
42
      m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
212
42
                                       parent->GetError().AsCString());
213
42
    }
214
56.1k
  } else {
215
0
    m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
216
0
  }
217
218
56.1k
  return m_error.Success();
219
56.1k
}
220
221
65.5k
bool ValueObjectChild::IsInScope() {
222
65.5k
  ValueObject *root(GetRoot());
223
65.5k
  if (root)
224
65.5k
    return root->IsInScope();
225
0
  return false;
226
65.5k
}