Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/ObjC/CF.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- CF.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 "CF.h"
10
11
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12
#include "lldb/Core/ValueObject.h"
13
#include "lldb/Core/ValueObjectConstResult.h"
14
#include "lldb/DataFormatters/FormattersHelpers.h"
15
#include "lldb/Target/Language.h"
16
#include "lldb/Target/StackFrame.h"
17
#include "lldb/Target/Target.h"
18
#include "lldb/Utility/DataBufferHeap.h"
19
#include "lldb/Utility/Endian.h"
20
#include "lldb/Utility/Status.h"
21
#include "lldb/Utility/Stream.h"
22
23
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
24
25
using namespace lldb;
26
using namespace lldb_private;
27
using namespace lldb_private::formatters;
28
29
bool lldb_private::formatters::CFAbsoluteTimeSummaryProvider(
30
30
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
31
30
  time_t epoch = GetOSXEpoch();
32
30
  epoch = epoch + (time_t)valobj.GetValueAsSigned(0);
33
30
  tm *tm_date = localtime(&epoch);
34
30
  if (!tm_date)
35
0
    return false;
36
30
  std::string buffer(1024, 0);
37
30
  if (strftime(&buffer[0], 1023, "%Z", tm_date) == 0)
38
0
    return false;
39
30
  stream.Printf("%04d-%02d-%02d %02d:%02d:%02d %s", tm_date->tm_year + 1900,
40
30
                tm_date->tm_mon + 1, tm_date->tm_mday, tm_date->tm_hour,
41
30
                tm_date->tm_min, tm_date->tm_sec, buffer.c_str());
42
30
  return true;
43
30
}
44
45
bool lldb_private::formatters::CFBagSummaryProvider(
46
0
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
47
0
  static constexpr llvm::StringLiteral g_TypeHint("CFBag");
48
49
0
  ProcessSP process_sp = valobj.GetProcessSP();
50
0
  if (!process_sp)
51
0
    return false;
52
53
0
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
54
55
0
  if (!runtime)
56
0
    return false;
57
58
0
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
59
0
      runtime->GetClassDescriptor(valobj));
60
61
0
  if (!descriptor.get() || !descriptor->IsValid())
62
0
    return false;
63
64
0
  uint32_t ptr_size = process_sp->GetAddressByteSize();
65
66
0
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
67
68
0
  if (!valobj_addr)
69
0
    return false;
70
71
0
  uint32_t count = 0;
72
73
0
  bool is_type_ok = false; // check to see if this is a CFBag we know about
74
0
  if (descriptor->IsCFType()) {
75
0
    ConstString type_name(valobj.GetTypeName());
76
77
0
    static ConstString g_CFBag("__CFBag");
78
0
    static ConstString g_conststruct__CFBag("const struct __CFBag");
79
80
0
    if (type_name == g_CFBag || type_name == g_conststruct__CFBag) {
81
0
      if (valobj.IsPointerType())
82
0
        is_type_ok = true;
83
0
    }
84
0
  }
85
86
0
  if (is_type_ok) {
87
0
    lldb::addr_t offset = 2 * ptr_size + 4 + valobj_addr;
88
0
    Status error;
89
0
    count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
90
0
    if (error.Fail())
91
0
      return false;
92
0
  } else
93
0
    return false;
94
95
0
  llvm::StringRef prefix, suffix;
96
0
  if (Language *language = Language::FindPlugin(options.GetLanguage()))
97
0
    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
98
99
0
  stream << prefix;
100
0
  stream.Printf("\"%u value%s\"", count, (count == 1 ? "" : "s"));
101
0
  stream << suffix;
102
0
  return true;
103
0
}
104
105
bool lldb_private::formatters::CFBitVectorSummaryProvider(
106
6
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
107
6
  ProcessSP process_sp = valobj.GetProcessSP();
108
6
  if (!process_sp)
109
0
    return false;
110
111
6
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
112
113
6
  if (!runtime)
114
0
    return false;
115
116
6
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
117
6
      runtime->GetClassDescriptor(valobj));
118
119
6
  if (!descriptor.get() || !descriptor->IsValid())
120
0
    return false;
121
122
6
  uint32_t ptr_size = process_sp->GetAddressByteSize();
123
124
6
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
125
126
6
  if (!valobj_addr)
127
0
    return false;
128
129
6
  uint32_t count = 0;
130
131
6
  bool is_type_ok = false; // check to see if this is a CFBag we know about
132
6
  if (descriptor->IsCFType()) {
133
6
    ConstString type_name(valobj.GetTypeName());
134
6
    if (type_name == "__CFMutableBitVector" || type_name == "__CFBitVector" ||
135
6
        type_name == "CFMutableBitVectorRef" || 
type_name == "CFBitVectorRef"0
) {
136
6
      if (valobj.IsPointerType())
137
6
        is_type_ok = true;
138
6
    }
139
6
  }
140
141
6
  if (!is_type_ok)
142
0
    return false;
143
144
6
  Status error;
145
6
  count = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + 2 * ptr_size,
146
6
                                                    ptr_size, 0, error);
147
6
  if (error.Fail())
148
0
    return false;
149
6
  uint64_t num_bytes = count / 8 + ((count & 7) ? 1 : 
00
);
150
6
  addr_t data_ptr = process_sp->ReadPointerFromMemory(
151
6
      valobj_addr + 2 * ptr_size + 2 * ptr_size, error);
152
6
  if (error.Fail())
153
0
    return false;
154
  // make sure we do not try to read huge amounts of data
155
6
  if (num_bytes > 1024)
156
0
    num_bytes = 1024;
157
6
  WritableDataBufferSP buffer_sp(new DataBufferHeap(num_bytes, 0));
158
6
  num_bytes =
159
6
      process_sp->ReadMemory(data_ptr, buffer_sp->GetBytes(), num_bytes, error);
160
6
  if (error.Fail() || num_bytes == 0)
161
0
    return false;
162
6
  uint8_t *bytes = buffer_sp->GetBytes();
163
42
  for (uint64_t byte_idx = 0; byte_idx < num_bytes - 1; 
byte_idx++36
) {
164
36
    uint8_t byte = bytes[byte_idx];
165
36
    bool bit0 = (byte & 1) == 1;
166
36
    bool bit1 = (byte & 2) == 2;
167
36
    bool bit2 = (byte & 4) == 4;
168
36
    bool bit3 = (byte & 8) == 8;
169
36
    bool bit4 = (byte & 16) == 16;
170
36
    bool bit5 = (byte & 32) == 32;
171
36
    bool bit6 = (byte & 64) == 64;
172
36
    bool bit7 = (byte & 128) == 128;
173
36
    stream.Printf("%c%c%c%c %c%c%c%c ", (bit7 ? 
'1'30
:
'0'6
), (bit6 ?
'1'18
:
'0'18
),
174
36
                  (bit5 ? 
'1'18
:
'0'18
), (bit4 ?
'1'24
:
'0'12
), (bit3 ?
'1'12
:
'0'24
),
175
36
                  (bit2 ? 
'1'18
:
'0'18
), (bit1 ?
'1'18
:
'0'18
), (bit0 ?
'1'18
:
'0'18
));
176
36
    count -= 8;
177
36
  }
178
6
  {
179
    // print the last byte ensuring we do not print spurious bits
180
6
    uint8_t byte = bytes[num_bytes - 1];
181
6
    bool bit0 = (byte & 1) == 1;
182
6
    bool bit1 = (byte & 2) == 2;
183
6
    bool bit2 = (byte & 4) == 4;
184
6
    bool bit3 = (byte & 8) == 8;
185
6
    bool bit4 = (byte & 16) == 16;
186
6
    bool bit5 = (byte & 32) == 32;
187
6
    bool bit6 = (byte & 64) == 64;
188
6
    bool bit7 = (byte & 128) == 128;
189
6
    if (count) {
190
6
      stream.Printf("%c", bit7 ? 
'1'0
: '0');
191
6
      count -= 1;
192
6
    }
193
6
    if (count) {
194
6
      stream.Printf("%c", bit6 ? 
'1'0
: '0');
195
6
      count -= 1;
196
6
    }
197
6
    if (count) {
198
0
      stream.Printf("%c", bit5 ? '1' : '0');
199
0
      count -= 1;
200
0
    }
201
6
    if (count) {
202
0
      stream.Printf("%c", bit4 ? '1' : '0');
203
0
      count -= 1;
204
0
    }
205
6
    if (count) {
206
0
      stream.Printf("%c", bit3 ? '1' : '0');
207
0
      count -= 1;
208
0
    }
209
6
    if (count) {
210
0
      stream.Printf("%c", bit2 ? '1' : '0');
211
0
      count -= 1;
212
0
    }
213
6
    if (count) {
214
0
      stream.Printf("%c", bit1 ? '1' : '0');
215
0
      count -= 1;
216
0
    }
217
6
    if (count)
218
0
      stream.Printf("%c", bit0 ? '1' : '0');
219
6
  }
220
6
  return true;
221
6
}
222
223
bool lldb_private::formatters::CFBinaryHeapSummaryProvider(
224
10
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
225
10
  static constexpr llvm::StringLiteral g_TypeHint("CFBinaryHeap");
226
227
10
  ProcessSP process_sp = valobj.GetProcessSP();
228
10
  if (!process_sp)
229
0
    return false;
230
231
10
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
232
233
10
  if (!runtime)
234
0
    return false;
235
236
10
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
237
10
      runtime->GetClassDescriptor(valobj));
238
239
10
  if (!descriptor.get() || !descriptor->IsValid())
240
0
    return false;
241
242
10
  uint32_t ptr_size = process_sp->GetAddressByteSize();
243
244
10
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
245
246
10
  if (!valobj_addr)
247
0
    return false;
248
249
10
  uint32_t count = 0;
250
251
10
  bool is_type_ok =
252
10
      false; // check to see if this is a CFBinaryHeap we know about
253
10
  if (descriptor->IsCFType()) {
254
10
    ConstString type_name(valobj.GetTypeName());
255
256
10
    static ConstString g_CFBinaryHeap("__CFBinaryHeap");
257
10
    static ConstString g_conststruct__CFBinaryHeap(
258
10
        "const struct __CFBinaryHeap");
259
10
    static ConstString g_CFBinaryHeapRef("CFBinaryHeapRef");
260
261
10
    if (type_name == g_CFBinaryHeap ||
262
10
        type_name == g_conststruct__CFBinaryHeap ||
263
10
        type_name == g_CFBinaryHeapRef) {
264
10
      if (valobj.IsPointerType())
265
10
        is_type_ok = true;
266
10
    }
267
10
  }
268
269
10
  if (is_type_ok) {
270
10
    lldb::addr_t offset = 2 * ptr_size + valobj_addr;
271
10
    Status error;
272
10
    count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
273
10
    if (error.Fail())
274
0
      return false;
275
10
  } else
276
0
    return false;
277
278
10
  llvm::StringRef prefix, suffix;
279
10
  if (Language *language = Language::FindPlugin(options.GetLanguage()))
280
10
    std::tie(prefix, suffix) = language->GetFormatterPrefixSuffix(g_TypeHint);
281
282
10
  stream << prefix;
283
10
  stream.Printf("\"%u item%s\"", count, (count == 1 ? 
""4
:
"s"6
));
284
10
  stream << suffix;
285
10
  return true;
286
10
}