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