blob: 3720560de59732fca21ef8884d9f398f4978f7af [file] [log] [blame]
Eugene Zelenko8d15f332015-10-20 01:10:59 +00001//===-- Cocoa.cpp -----------------------------------------------*- C++ -*-===//
Enrico Granata92373532013-03-19 22:58:48 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Enrico Granata92373532013-03-19 22:58:48 +00006//
7//===----------------------------------------------------------------------===//
8
Enrico Granata170c3952015-09-14 22:18:32 +00009#include "Cocoa.h"
Enrico Granata92373532013-03-19 22:58:48 +000010
Enrico Granata7de855c2015-10-02 20:59:58 +000011#include "lldb/Core/Mangled.h"
Enrico Granata92373532013-03-19 22:58:48 +000012#include "lldb/Core/ValueObject.h"
13#include "lldb/Core/ValueObjectConstResult.h"
Enrico Granata419d7912015-09-04 00:33:51 +000014#include "lldb/DataFormatters/FormattersHelpers.h"
15#include "lldb/DataFormatters/StringPrinter.h"
16#include "lldb/DataFormatters/TypeSummary.h"
Pavel Labath46897a42017-01-25 11:19:49 +000017#include "lldb/Host/Time.h"
Enrico Granata92373532013-03-19 22:58:48 +000018#include "lldb/Symbol/ClangASTContext.h"
Enrico Granata675f49b2015-10-07 18:36:53 +000019#include "lldb/Target/Language.h"
Enrico Granata92373532013-03-19 22:58:48 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Enrico Granata419d7912015-09-04 00:33:51 +000021#include "lldb/Target/Process.h"
Zachary Turner01c32432017-02-14 19:06:07 +000022#include "lldb/Target/ProcessStructReader.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000024#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner01c32432017-02-14 19:06:07 +000025#include "lldb/Utility/Endian.h"
Zachary Turner97206d52017-05-12 04:51:55 +000026#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000027#include "lldb/Utility/Stream.h"
Enrico Granatad87cc312015-09-03 01:29:42 +000028
Sean Callanan18b5d922017-06-19 18:32:22 +000029#include "llvm/ADT/APInt.h"
Davide Italiano0d98b772019-02-05 17:30:53 +000030#include "llvm/ADT/bit.h"
Sean Callanan18b5d922017-06-19 18:32:22 +000031
Enrico Granata2094e442016-08-22 18:07:52 +000032#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h"
33
Enrico Granata7de855c2015-10-02 20:59:58 +000034#include "NSString.h"
35
Enrico Granata92373532013-03-19 22:58:48 +000036using namespace lldb;
37using namespace lldb_private;
38using namespace lldb_private::formatters;
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040bool lldb_private::formatters::NSBundleSummaryProvider(
41 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
42 ProcessSP process_sp = valobj.GetProcessSP();
43 if (!process_sp)
Enrico Granataaa05cf92016-04-01 20:33:22 +000044 return false;
Enrico Granata92373532013-03-19 22:58:48 +000045
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 ObjCLanguageRuntime *runtime =
47 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
48 lldb::eLanguageTypeObjC);
49
50 if (!runtime)
Enrico Granataaa05cf92016-04-01 20:33:22 +000051 return false;
Enrico Granata92373532013-03-19 22:58:48 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
54 runtime->GetClassDescriptor(valobj));
55
56 if (!descriptor || !descriptor->IsValid())
Enrico Granataaa05cf92016-04-01 20:33:22 +000057 return false;
Enrico Granata92373532013-03-19 22:58:48 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 uint32_t ptr_size = process_sp->GetAddressByteSize();
60
61 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
62
63 if (!valobj_addr)
Enrico Granataaa05cf92016-04-01 20:33:22 +000064 return false;
Enrico Granata92373532013-03-19 22:58:48 +000065
Jonas Devliegherecb38fd62018-06-13 18:15:14 +000066 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Enrico Granata92373532013-03-19 22:58:48 +000067
Jonas Devliegherecb38fd62018-06-13 18:15:14 +000068 if (class_name.empty())
Enrico Granataaa05cf92016-04-01 20:33:22 +000069 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000070
Jonas Devliegherecb38fd62018-06-13 18:15:14 +000071 if (class_name == "NSBundle") {
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 uint64_t offset = 5 * ptr_size;
73 ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
74 offset,
75 valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeObjCID),
76 true));
77
78 StreamString summary_stream;
79 bool was_nsstring_ok =
80 NSStringSummaryProvider(*text, summary_stream, options);
81 if (was_nsstring_ok && summary_stream.GetSize() > 0) {
82 stream.Printf("%s", summary_stream.GetData());
83 return true;
84 }
85 }
86
87 return false;
Enrico Granata92373532013-03-19 22:58:48 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090bool lldb_private::formatters::NSTimeZoneSummaryProvider(
91 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
92 ProcessSP process_sp = valobj.GetProcessSP();
93 if (!process_sp)
Enrico Granata92373532013-03-19 22:58:48 +000094 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000095
96 ObjCLanguageRuntime *runtime =
97 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
98 lldb::eLanguageTypeObjC);
99
100 if (!runtime)
101 return false;
102
103 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
104 runtime->GetClassDescriptor(valobj));
105
106 if (!descriptor || !descriptor->IsValid())
107 return false;
108
109 uint32_t ptr_size = process_sp->GetAddressByteSize();
110
111 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
112
113 if (!valobj_addr)
114 return false;
115
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000116 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000118 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 return false;
120
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000121 if (class_name == "__NSTimeZone") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 uint64_t offset = ptr_size;
123 ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
124 offset, valobj.GetCompilerType(), true));
125 StreamString summary_stream;
126 bool was_nsstring_ok =
127 NSStringSummaryProvider(*text, summary_stream, options);
128 if (was_nsstring_ok && summary_stream.GetSize() > 0) {
129 stream.Printf("%s", summary_stream.GetData());
130 return true;
131 }
132 }
133
134 return false;
Enrico Granata92373532013-03-19 22:58:48 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137bool lldb_private::formatters::NSNotificationSummaryProvider(
138 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
139 ProcessSP process_sp = valobj.GetProcessSP();
140 if (!process_sp)
141 return false;
Enrico Granataaa05cf92016-04-01 20:33:22 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 ObjCLanguageRuntime *runtime =
144 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
145 lldb::eLanguageTypeObjC);
146
147 if (!runtime)
148 return false;
149
150 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
151 runtime->GetClassDescriptor(valobj));
152
153 if (!descriptor || !descriptor->IsValid())
154 return false;
155
156 uint32_t ptr_size = process_sp->GetAddressByteSize();
157
158 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
159
160 if (!valobj_addr)
161 return false;
162
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000163 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000165 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 return false;
167
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000168 if (class_name == "NSConcreteNotification") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 uint64_t offset = ptr_size;
170 ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
171 offset, valobj.GetCompilerType(), true));
172 StreamString summary_stream;
173 bool was_nsstring_ok =
174 NSStringSummaryProvider(*text, summary_stream, options);
175 if (was_nsstring_ok && summary_stream.GetSize() > 0) {
176 stream.Printf("%s", summary_stream.GetData());
177 return true;
Enrico Granata92373532013-03-19 22:58:48 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 }
180
181 return false;
182}
183
184bool lldb_private::formatters::NSMachPortSummaryProvider(
185 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
186 ProcessSP process_sp = valobj.GetProcessSP();
187 if (!process_sp)
188 return false;
189
190 ObjCLanguageRuntime *runtime =
191 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
192 lldb::eLanguageTypeObjC);
193
194 if (!runtime)
195 return false;
196
197 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
198 runtime->GetClassDescriptor(valobj));
199
200 if (!descriptor || !descriptor->IsValid())
201 return false;
202
203 uint32_t ptr_size = process_sp->GetAddressByteSize();
204
205 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
206
207 if (!valobj_addr)
208 return false;
209
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000210 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000212 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return false;
214
215 uint64_t port_number = 0;
216
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000217 if (class_name == "NSMachPort") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 uint64_t offset = (ptr_size == 4 ? 12 : 20);
Zachary Turner97206d52017-05-12 04:51:55 +0000219 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 port_number = process_sp->ReadUnsignedIntegerFromMemory(
221 offset + valobj_addr, 4, 0, error);
222 if (error.Success()) {
223 stream.Printf("mach port: %u",
224 (uint32_t)(port_number & 0x00000000FFFFFFFF));
225 return true;
226 }
227 }
228
229 return false;
230}
231
232bool lldb_private::formatters::NSIndexSetSummaryProvider(
233 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
234 ProcessSP process_sp = valobj.GetProcessSP();
235 if (!process_sp)
236 return false;
237
238 ObjCLanguageRuntime *runtime =
239 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
240 lldb::eLanguageTypeObjC);
241
242 if (!runtime)
243 return false;
244
245 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
246 runtime->GetClassDescriptor(valobj));
247
248 if (!descriptor || !descriptor->IsValid())
249 return false;
250
251 uint32_t ptr_size = process_sp->GetAddressByteSize();
252
253 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
254
255 if (!valobj_addr)
256 return false;
257
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000258 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000260 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 return false;
262
263 uint64_t count = 0;
264
265 do {
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000266 if (class_name == "NSIndexSet" || class_name == "NSMutableIndexSet") {
Zachary Turner97206d52017-05-12 04:51:55 +0000267 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 uint32_t mode = process_sp->ReadUnsignedIntegerFromMemory(
269 valobj_addr + ptr_size, 4, 0, error);
270 if (error.Fail())
271 return false;
272 // this means the set is empty - count = 0
273 if ((mode & 1) == 1) {
274 count = 0;
275 break;
276 }
277 if ((mode & 2) == 2)
278 mode = 1; // this means the set only has one range
279 else
280 mode = 2; // this means the set has multiple ranges
281 if (mode == 1) {
282 count = process_sp->ReadUnsignedIntegerFromMemory(
283 valobj_addr + 3 * ptr_size, ptr_size, 0, error);
Enrico Granata92373532013-03-19 22:58:48 +0000284 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 return false;
286 } else {
287 // read a pointer to the data at 2*ptr_size
288 count = process_sp->ReadUnsignedIntegerFromMemory(
289 valobj_addr + 2 * ptr_size, ptr_size, 0, error);
290 if (error.Fail())
291 return false;
292 // read the data at 2*ptr_size from the first location
293 count = process_sp->ReadUnsignedIntegerFromMemory(count + 2 * ptr_size,
294 ptr_size, 0, error);
295 if (error.Fail())
296 return false;
297 }
298 } else
299 return false;
300 } while (false);
301 stream.Printf("%" PRIu64 " index%s", count, (count == 1 ? "" : "es"));
302 return true;
Enrico Granata92373532013-03-19 22:58:48 +0000303}
Enrico Granatad87cc312015-09-03 01:29:42 +0000304
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305static void NSNumber_FormatChar(ValueObject &valobj, Stream &stream, char value,
306 lldb::LanguageType lang) {
307 static ConstString g_TypeHint("NSNumber:char");
308
309 std::string prefix, suffix;
310 if (Language *language = Language::FindPlugin(lang)) {
311 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
312 suffix)) {
313 prefix.clear();
314 suffix.clear();
315 }
316 }
317
318 stream.Printf("%s%hhd%s", prefix.c_str(), value, suffix.c_str());
Enrico Granatad87cc312015-09-03 01:29:42 +0000319}
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321static void NSNumber_FormatShort(ValueObject &valobj, Stream &stream,
322 short value, lldb::LanguageType lang) {
323 static ConstString g_TypeHint("NSNumber:short");
324
325 std::string prefix, suffix;
326 if (Language *language = Language::FindPlugin(lang)) {
327 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
328 suffix)) {
329 prefix.clear();
330 suffix.clear();
331 }
332 }
333
334 stream.Printf("%s%hd%s", prefix.c_str(), value, suffix.c_str());
335}
336
337static void NSNumber_FormatInt(ValueObject &valobj, Stream &stream, int value,
338 lldb::LanguageType lang) {
339 static ConstString g_TypeHint("NSNumber:int");
340
341 std::string prefix, suffix;
342 if (Language *language = Language::FindPlugin(lang)) {
343 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
344 suffix)) {
345 prefix.clear();
346 suffix.clear();
347 }
348 }
349
350 stream.Printf("%s%d%s", prefix.c_str(), value, suffix.c_str());
351}
352
353static void NSNumber_FormatLong(ValueObject &valobj, Stream &stream,
354 uint64_t value, lldb::LanguageType lang) {
355 static ConstString g_TypeHint("NSNumber:long");
356
357 std::string prefix, suffix;
358 if (Language *language = Language::FindPlugin(lang)) {
359 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
360 suffix)) {
361 prefix.clear();
362 suffix.clear();
363 }
364 }
365
366 stream.Printf("%s%" PRId64 "%s", prefix.c_str(), value, suffix.c_str());
367}
368
Sean Callanan18b5d922017-06-19 18:32:22 +0000369static void NSNumber_FormatInt128(ValueObject &valobj, Stream &stream,
370 const llvm::APInt &value,
371 lldb::LanguageType lang) {
372 static ConstString g_TypeHint("NSNumber:int128_t");
373
374 std::string prefix, suffix;
375 if (Language *language = Language::FindPlugin(lang)) {
376 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
377 suffix)) {
378 prefix.clear();
379 suffix.clear();
380 }
381 }
382
383 stream.PutCString(prefix.c_str());
384 const int radix = 10;
385 const bool isSigned = true;
386 std::string str = value.toString(radix, isSigned);
387 stream.PutCString(str.c_str());
388 stream.PutCString(suffix.c_str());
389}
390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391static void NSNumber_FormatFloat(ValueObject &valobj, Stream &stream,
392 float value, lldb::LanguageType lang) {
393 static ConstString g_TypeHint("NSNumber:float");
394
395 std::string prefix, suffix;
396 if (Language *language = Language::FindPlugin(lang)) {
397 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
398 suffix)) {
399 prefix.clear();
400 suffix.clear();
401 }
402 }
403
404 stream.Printf("%s%f%s", prefix.c_str(), value, suffix.c_str());
405}
406
407static void NSNumber_FormatDouble(ValueObject &valobj, Stream &stream,
408 double value, lldb::LanguageType lang) {
409 static ConstString g_TypeHint("NSNumber:double");
410
411 std::string prefix, suffix;
412 if (Language *language = Language::FindPlugin(lang)) {
413 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
414 suffix)) {
415 prefix.clear();
416 suffix.clear();
417 }
418 }
419
420 stream.Printf("%s%g%s", prefix.c_str(), value, suffix.c_str());
421}
422
423bool lldb_private::formatters::NSNumberSummaryProvider(
424 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
425 ProcessSP process_sp = valobj.GetProcessSP();
426 if (!process_sp)
427 return false;
428
429 ObjCLanguageRuntime *runtime =
430 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
431 lldb::eLanguageTypeObjC);
432
433 if (!runtime)
434 return false;
435
436 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
437 runtime->GetClassDescriptor(valobj));
438
439 if (!descriptor || !descriptor->IsValid())
440 return false;
441
442 uint32_t ptr_size = process_sp->GetAddressByteSize();
443
444 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
445
446 if (!valobj_addr)
447 return false;
448
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000449 llvm::StringRef class_name(descriptor->GetClassName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000451 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 return false;
453
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000454 if (class_name == "__NSCFBoolean")
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 return ObjCBooleanSummaryProvider(valobj, stream, options);
456
Jonas Devlieghere20b051b2018-06-13 18:47:04 +0000457 if (class_name == "NSDecimalNumber")
458 return NSDecimalNumberSummaryProvider(valobj, stream, options);
459
Jonas Devliegherecb38fd62018-06-13 18:15:14 +0000460 if (class_name == "NSNumber" || class_name == "__NSCFNumber") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 uint64_t value = 0;
462 uint64_t i_bits = 0;
463 if (descriptor->GetTaggedPointerInfo(&i_bits, &value)) {
464 switch (i_bits) {
465 case 0:
466 NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
467 break;
468 case 1:
469 case 4:
470 NSNumber_FormatShort(valobj, stream, (short)value,
471 options.GetLanguage());
472 break;
473 case 2:
474 case 8:
475 NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
476 break;
477 case 3:
478 case 12:
479 NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
480 break;
481 default:
482 return false;
483 }
484 return true;
485 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000486 Status error;
Sean Callanan18b5d922017-06-19 18:32:22 +0000487
488 AppleObjCRuntime *runtime =
489 llvm::dyn_cast_or_null<AppleObjCRuntime>(
490 process_sp->GetObjCLanguageRuntime());
491
492 const bool new_format =
493 (runtime && runtime->GetFoundationVersion() >= 1400);
494
495 enum class TypeCodes : int {
496 sint8 = 0x0,
497 sint16 = 0x1,
498 sint32 = 0x2,
499 sint64 = 0x3,
500 f32 = 0x4,
501 f64 = 0x5,
502 sint128 = 0x6
503 };
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 uint64_t data_location = valobj_addr + 2 * ptr_size;
Sean Callanan18b5d922017-06-19 18:32:22 +0000506 TypeCodes type_code;
507
508 if (new_format) {
509 uint64_t cfinfoa =
510 process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + ptr_size,
511 ptr_size, 0, error);
512
513 if (error.Fail())
514 return false;
515
516 bool is_preserved_number = cfinfoa & 0x8;
517 if (is_preserved_number) {
Tim Hammerquist0a940722017-06-29 23:33:40 +0000518 lldbassert(!static_cast<bool>("We should handle preserved numbers!"));
Sean Callanan18b5d922017-06-19 18:32:22 +0000519 return false;
520 }
521
Tim Hammerquist0a940722017-06-29 23:33:40 +0000522 type_code = static_cast<TypeCodes>(cfinfoa & 0x7);
Sean Callanan18b5d922017-06-19 18:32:22 +0000523 } else {
524 uint8_t data_type =
525 process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + ptr_size, 1,
526 0, error) & 0x1F;
527
528 if (error.Fail())
529 return false;
530
531 switch (data_type) {
532 case 1: type_code = TypeCodes::sint8; break;
533 case 2: type_code = TypeCodes::sint16; break;
534 case 3: type_code = TypeCodes::sint32; break;
535 case 17: data_location += 8; LLVM_FALLTHROUGH;
536 case 4: type_code = TypeCodes::sint64; break;
537 case 5: type_code = TypeCodes::f32; break;
538 case 6: type_code = TypeCodes::f64; break;
539 default: return false;
540 }
541 }
542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 uint64_t value = 0;
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000544 bool success = false;
Sean Callanan18b5d922017-06-19 18:32:22 +0000545 switch (type_code) {
546 case TypeCodes::sint8:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 1, 0,
548 error);
549 if (error.Fail())
550 return false;
551 NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000552 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 break;
Sean Callanan18b5d922017-06-19 18:32:22 +0000554 case TypeCodes::sint16:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 2, 0,
556 error);
557 if (error.Fail())
558 return false;
559 NSNumber_FormatShort(valobj, stream, (short)value,
560 options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000561 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 break;
Sean Callanan18b5d922017-06-19 18:32:22 +0000563 case TypeCodes::sint32:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 4, 0,
565 error);
566 if (error.Fail())
567 return false;
568 NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000569 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 break;
Sean Callanan18b5d922017-06-19 18:32:22 +0000571 case TypeCodes::sint64:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 8, 0,
573 error);
574 if (error.Fail())
575 return false;
576 NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000577 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 break;
Sean Callanan18b5d922017-06-19 18:32:22 +0000579 case TypeCodes::f32:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 {
581 uint32_t flt_as_int = process_sp->ReadUnsignedIntegerFromMemory(
582 data_location, 4, 0, error);
583 if (error.Fail())
584 return false;
585 float flt_value = 0.0f;
586 memcpy(&flt_value, &flt_as_int, sizeof(flt_as_int));
587 NSNumber_FormatFloat(valobj, stream, flt_value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000588 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 break;
590 }
Sean Callanan18b5d922017-06-19 18:32:22 +0000591 case TypeCodes::f64:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 {
593 uint64_t dbl_as_lng = process_sp->ReadUnsignedIntegerFromMemory(
594 data_location, 8, 0, error);
595 if (error.Fail())
596 return false;
597 double dbl_value = 0.0;
598 memcpy(&dbl_value, &dbl_as_lng, sizeof(dbl_as_lng));
599 NSNumber_FormatDouble(valobj, stream, dbl_value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000600 success = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 break;
602 }
Sean Callanan18b5d922017-06-19 18:32:22 +0000603 case TypeCodes::sint128: // internally, this is the same
604 {
605 uint64_t words[2];
606 words[1] = process_sp->ReadUnsignedIntegerFromMemory(
607 data_location, 8, 0, error);
608 if (error.Fail())
609 return false;
610 words[0] = process_sp->ReadUnsignedIntegerFromMemory(
611 data_location + 8, 8, 0, error);
612 if (error.Fail())
613 return false;
614 llvm::APInt i128_value(128, words);
615 NSNumber_FormatInt128(valobj, stream, i128_value, options.GetLanguage());
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000616 success = true;
Sean Callanan18b5d922017-06-19 18:32:22 +0000617 break;
618 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 }
Tim Hammerquist95c003b2017-07-11 21:06:20 +0000620 return success;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 }
622 }
623
624 return false;
625}
626
Jonas Devlieghere20b051b2018-06-13 18:47:04 +0000627bool lldb_private::formatters::NSDecimalNumberSummaryProvider(
628 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
629 ProcessSP process_sp = valobj.GetProcessSP();
630 if (!process_sp)
631 return false;
632
633 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
634 uint32_t ptr_size = process_sp->GetAddressByteSize();
635
636 Status error;
637 int8_t exponent = process_sp->ReadUnsignedIntegerFromMemory(
638 valobj_addr + ptr_size, 1, 0, error);
639 if (error.Fail())
640 return false;
641
642 uint8_t length_and_negative = process_sp->ReadUnsignedIntegerFromMemory(
643 valobj_addr + ptr_size + 1, 1, 0, error);
644 if (error.Fail())
645 return false;
646
647 // Fifth bit marks negativity.
648 const bool is_negative = (length_and_negative >> 4) & 1;
649
650 // Zero length and negative means NaN.
651 uint8_t length = length_and_negative & 0xf;
652 const bool is_nan = is_negative && (length == 0);
653
654 if (is_nan) {
655 stream.Printf("NaN");
656 return true;
657 }
658
659 if (length == 0) {
660 stream.Printf("0");
661 return true;
662 }
663
664 uint64_t mantissa = process_sp->ReadUnsignedIntegerFromMemory(
665 valobj_addr + ptr_size + 4, 8, 0, error);
666 if (error.Fail())
667 return false;
668
669 if (is_negative)
670 stream.Printf("-");
671
672 stream.Printf("%" PRIu64 " x 10^%" PRIi8, mantissa, exponent);
673 return true;
674}
675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676bool lldb_private::formatters::NSURLSummaryProvider(
677 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
678 ProcessSP process_sp = valobj.GetProcessSP();
679 if (!process_sp)
680 return false;
681
682 ObjCLanguageRuntime *runtime =
683 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
684 lldb::eLanguageTypeObjC);
685
686 if (!runtime)
687 return false;
688
689 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
690 runtime->GetClassDescriptor(valobj));
691
692 if (!descriptor || !descriptor->IsValid())
693 return false;
694
695 uint32_t ptr_size = process_sp->GetAddressByteSize();
696
697 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
698
699 if (!valobj_addr)
700 return false;
701
Zachary Turner2dd214a2016-11-15 20:13:14 +0000702 llvm::StringRef class_name = descriptor->GetClassName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703
Zachary Turner2dd214a2016-11-15 20:13:14 +0000704 if (!class_name.equals("NSURL"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 return false;
706
Zachary Turner2dd214a2016-11-15 20:13:14 +0000707 uint64_t offset_text = ptr_size + ptr_size +
708 8; // ISA + pointer + 8 bytes of data (even on 32bit)
709 uint64_t offset_base = offset_text + ptr_size;
710 CompilerType type(valobj.GetCompilerType());
711 ValueObjectSP text(valobj.GetSyntheticChildAtOffset(offset_text, type, true));
712 ValueObjectSP base(valobj.GetSyntheticChildAtOffset(offset_base, type, true));
713 if (!text)
714 return false;
715 if (text->GetValueAsUnsigned(0) == 0)
716 return false;
717 StreamString summary;
718 if (!NSStringSummaryProvider(*text, summary, options))
719 return false;
720 if (base && base->GetValueAsUnsigned(0)) {
721 std::string summary_str = summary.GetString();
722
723 if (!summary_str.empty())
724 summary_str.pop_back();
725 summary_str += " -- ";
726 StreamString base_summary;
727 if (NSURLSummaryProvider(*base, base_summary, options) &&
728 !base_summary.Empty()) {
729 llvm::StringRef base_str = base_summary.GetString();
730 if (base_str.size() > 2)
731 base_str = base_str.drop_front(2);
732 summary_str += base_str;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
Zachary Turner2dd214a2016-11-15 20:13:14 +0000734 summary.Clear();
735 summary.PutCString(summary_str);
736 }
737 if (!summary.Empty()) {
738 stream.PutCString(summary.GetString());
739 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 }
741
742 return false;
743}
744
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000745/// Bias value for tagged pointer exponents.
746/// Recommended values:
747/// 0x3e3: encodes all dates between distantPast and distantFuture
748/// except for the range within about 1e-28 second of the reference date.
749/// 0x3ef: encodes all dates for a few million years beyond distantPast and
750/// distantFuture, except within about 1e-25 second of the reference date.
751const int TAGGED_DATE_EXPONENT_BIAS = 0x3ef;
752
Davide Italiano0d98b772019-02-05 17:30:53 +0000753struct DoubleBits {
754 uint64_t fraction : 52; // unsigned
755 uint64_t exponent : 11; // signed
756 uint64_t sign : 1;
757};
758
759struct TaggedDoubleBits {
760 uint64_t fraction : 52; // unsigned
761 uint64_t exponent : 7; // signed
762 uint64_t sign : 1;
763 uint64_t unused : 4; // placeholder for pointer tag bits
764};
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000765
766static uint64_t decodeExponent(uint64_t exp) {
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000767 // Tagged exponent field is 7-bit signed. Sign-extend the value to 64 bits
768 // before performing arithmetic.
Davide Italiano9413be92018-11-16 19:53:00 +0000769 return llvm::SignExtend64<7>(exp) + TAGGED_DATE_EXPONENT_BIAS;
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000770}
771
Davide Italiano0d98b772019-02-05 17:30:53 +0000772static double decodeTaggedTimeInterval(uint64_t encodedTimeInterval) {
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000773 if (encodedTimeInterval == 0)
774 return 0.0;
775 if (encodedTimeInterval == std::numeric_limits<uint64_t>::max())
Haojian Wu59fa37d2018-11-14 09:53:45 +0000776 return (uint64_t)-0.0;
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000777
Davide Italiano0d98b772019-02-05 17:30:53 +0000778 TaggedDoubleBits encodedBits =
779 llvm::bit_cast<TaggedDoubleBits>(encodedTimeInterval);
780 assert(encodedBits.unused == 0);
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000781
782 // Sign and fraction are represented exactly.
783 // Exponent is encoded.
Davide Italiano0d98b772019-02-05 17:30:53 +0000784 DoubleBits decodedBits;
785 decodedBits.sign = encodedBits.sign;
786 decodedBits.fraction = encodedBits.fraction;
787 decodedBits.exponent = decodeExponent(encodedBits.exponent);
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000788
Davide Italiano0d98b772019-02-05 17:30:53 +0000789 return llvm::bit_cast<double>(decodedBits);
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000790}
791
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792bool lldb_private::formatters::NSDateSummaryProvider(
793 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
794 ProcessSP process_sp = valobj.GetProcessSP();
795 if (!process_sp)
796 return false;
797
798 ObjCLanguageRuntime *runtime =
799 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
800 lldb::eLanguageTypeObjC);
801
802 if (!runtime)
803 return false;
804
805 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
806 runtime->GetClassDescriptor(valobj));
807
808 if (!descriptor || !descriptor->IsValid())
809 return false;
810
811 uint32_t ptr_size = process_sp->GetAddressByteSize();
812
813 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
814
815 if (!valobj_addr)
816 return false;
817
818 uint64_t date_value_bits = 0;
819 double date_value = 0.0;
820
821 ConstString class_name = descriptor->GetClassName();
822
823 static const ConstString g_NSDate("NSDate");
824 static const ConstString g___NSDate("__NSDate");
825 static const ConstString g___NSTaggedDate("__NSTaggedDate");
826 static const ConstString g_NSCalendarDate("NSCalendarDate");
827
828 if (class_name.IsEmpty())
829 return false;
830
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000831 uint64_t info_bits = 0, value_bits = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 if ((class_name == g_NSDate) || (class_name == g___NSDate) ||
833 (class_name == g___NSTaggedDate)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 if (descriptor->GetTaggedPointerInfo(&info_bits, &value_bits)) {
835 date_value_bits = ((value_bits << 8) | (info_bits << 4));
836 memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
837 } else {
838 llvm::Triple triple(
839 process_sp->GetTarget().GetArchitecture().GetTriple());
840 uint32_t delta =
841 (triple.isWatchOS() && triple.isWatchABI()) ? 8 : ptr_size;
Zachary Turner97206d52017-05-12 04:51:55 +0000842 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(
844 valobj_addr + delta, 8, 0, error);
845 memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
846 if (error.Fail())
847 return false;
848 }
849 } else if (class_name == g_NSCalendarDate) {
Zachary Turner97206d52017-05-12 04:51:55 +0000850 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(
852 valobj_addr + 2 * ptr_size, 8, 0, error);
853 memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
854 if (error.Fail())
855 return false;
856 } else
857 return false;
858
859 if (date_value == -63114076800) {
860 stream.Printf("0001-12-30 00:00:00 +0000");
861 return true;
862 }
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000863
864 // Accomodate for the __NSTaggedDate format introduced in Foundation 1600.
865 if (class_name == g___NSTaggedDate) {
Davide Italiano0d98b772019-02-05 17:30:53 +0000866 auto *runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
867 process_sp->GetObjCLanguageRuntime());
Davide Italianocdcfa5d2018-11-13 19:43:43 +0000868 if (runtime && runtime->GetFoundationVersion() >= 1600)
869 date_value = decodeTaggedTimeInterval(value_bits << 4);
870 }
871
Adrian Prantl05097242018-04-30 16:49:04 +0000872 // this snippet of code assumes that time_t == seconds since Jan-1-1970 this
873 // is generally true and POSIXly happy, but might break if a library vendor
874 // decides to get creative
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 time_t epoch = GetOSXEpoch();
876 epoch = epoch + (time_t)date_value;
877 tm *tm_date = gmtime(&epoch);
878 if (!tm_date)
879 return false;
880 std::string buffer(1024, 0);
881 if (strftime(&buffer[0], 1023, "%Z", tm_date) == 0)
882 return false;
883 stream.Printf("%04d-%02d-%02d %02d:%02d:%02d %s", tm_date->tm_year + 1900,
884 tm_date->tm_mon + 1, tm_date->tm_mday, tm_date->tm_hour,
885 tm_date->tm_min, tm_date->tm_sec, buffer.c_str());
886 return true;
887}
888
889bool lldb_private::formatters::ObjCClassSummaryProvider(
890 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
891 ProcessSP process_sp = valobj.GetProcessSP();
892 if (!process_sp)
893 return false;
894
895 ObjCLanguageRuntime *runtime =
896 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
897 lldb::eLanguageTypeObjC);
898
899 if (!runtime)
900 return false;
901
902 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
903 runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0)));
904
905 if (!descriptor || !descriptor->IsValid())
906 return false;
907
908 ConstString class_name = descriptor->GetClassName();
909
910 if (class_name.IsEmpty())
911 return false;
912
913 if (ConstString cs =
914 Mangled(class_name).GetDemangledName(lldb::eLanguageTypeUnknown))
915 class_name = cs;
916
917 stream.Printf("%s", class_name.AsCString("<unknown class>"));
918 return true;
919}
920
921class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd {
Enrico Granatad87cc312015-09-03 01:29:42 +0000922public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 ObjCClassSyntheticChildrenFrontEnd(lldb::ValueObjectSP valobj_sp)
924 : SyntheticChildrenFrontEnd(*valobj_sp) {}
Eugene Zelenko8d15f332015-10-20 01:10:59 +0000925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 ~ObjCClassSyntheticChildrenFrontEnd() override = default;
927
928 size_t CalculateNumChildren() override { return 0; }
929
930 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
931 return lldb::ValueObjectSP();
932 }
933
934 bool Update() override { return false; }
935
936 bool MightHaveChildren() override { return false; }
937
938 size_t GetIndexOfChildWithName(const ConstString &name) override {
939 return UINT32_MAX;
940 }
Enrico Granatad87cc312015-09-03 01:29:42 +0000941};
942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943SyntheticChildrenFrontEnd *
944lldb_private::formatters::ObjCClassSyntheticFrontEndCreator(
945 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
946 return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp);
Enrico Granatad87cc312015-09-03 01:29:42 +0000947}
948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949template <bool needs_at>
950bool lldb_private::formatters::NSDataSummaryProvider(
951 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
952 ProcessSP process_sp = valobj.GetProcessSP();
953 if (!process_sp)
Enrico Granata2094e442016-08-22 18:07:52 +0000954 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955
956 ObjCLanguageRuntime *runtime =
957 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
958 lldb::eLanguageTypeObjC);
959
960 if (!runtime)
961 return false;
962
963 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
964 runtime->GetClassDescriptor(valobj));
965
966 if (!descriptor || !descriptor->IsValid())
967 return false;
968
969 bool is_64bit = (process_sp->GetAddressByteSize() == 8);
970 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
971
972 if (!valobj_addr)
973 return false;
974
975 uint64_t value = 0;
976
Vedant Kumar6d2b4352018-02-22 23:48:21 +0000977 llvm::StringRef class_name = descriptor->GetClassName().GetCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978
Vedant Kumar6d2b4352018-02-22 23:48:21 +0000979 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 return false;
981
Vedant Kumar6d2b4352018-02-22 23:48:21 +0000982 bool isNSConcreteData = class_name == "NSConcreteData";
983 bool isNSConcreteMutableData = class_name == "NSConcreteMutableData";
984 bool isNSCFData = class_name == "__NSCFData";
985 if (isNSConcreteData || isNSConcreteMutableData || isNSCFData) {
986 uint32_t offset;
987 if (isNSConcreteData)
988 offset = is_64bit ? 8 : 4;
989 else
990 offset = is_64bit ? 16 : 8;
991
Zachary Turner97206d52017-05-12 04:51:55 +0000992 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 value = process_sp->ReadUnsignedIntegerFromMemory(
994 valobj_addr + offset, is_64bit ? 8 : 4, 0, error);
995 if (error.Fail())
996 return false;
Vedant Kumar6d2b4352018-02-22 23:48:21 +0000997 } else if (class_name == "_NSInlineData") {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 uint32_t offset = (is_64bit ? 8 : 4);
Zachary Turner97206d52017-05-12 04:51:55 +0000999 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, 2,
1001 0, error);
1002 if (error.Fail())
1003 return false;
Vedant Kumar6d2b4352018-02-22 23:48:21 +00001004 } else if (class_name == "_NSZeroData") {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 value = 0;
1006 } else
1007 return false;
1008
1009 stream.Printf("%s%" PRIu64 " byte%s%s", (needs_at ? "@\"" : ""), value,
1010 (value != 1 ? "s" : ""), (needs_at ? "\"" : ""));
1011
1012 return true;
1013}
1014
1015bool lldb_private::formatters::ObjCBOOLSummaryProvider(
1016 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1017 const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo();
1018
1019 ValueObjectSP real_guy_sp = valobj.GetSP();
1020
1021 if (type_info & eTypeIsPointer) {
Zachary Turner97206d52017-05-12 04:51:55 +00001022 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 real_guy_sp = valobj.Dereference(err);
1024 if (err.Fail() || !real_guy_sp)
1025 return false;
1026 } else if (type_info & eTypeIsReference) {
1027 real_guy_sp = valobj.GetChildAtIndex(0, true);
1028 if (!real_guy_sp)
1029 return false;
1030 }
1031 uint8_t value = (real_guy_sp->GetValueAsUnsigned(0) & 0xFF);
1032 switch (value) {
1033 case 0:
1034 stream.Printf("NO");
1035 break;
1036 case 1:
1037 stream.Printf("YES");
1038 break;
1039 default:
1040 stream.Printf("%u", value);
1041 break;
1042 }
1043 return true;
1044}
1045
1046bool lldb_private::formatters::ObjCBooleanSummaryProvider(
1047 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1048 lldb::addr_t valobj_ptr_value =
1049 valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1050 if (valobj_ptr_value == LLDB_INVALID_ADDRESS)
1051 return false;
1052
1053 ProcessSP process_sp(valobj.GetProcessSP());
1054 if (!process_sp)
1055 return false;
1056
1057 if (AppleObjCRuntime *objc_runtime =
1058 (AppleObjCRuntime *)process_sp->GetObjCLanguageRuntime()) {
1059 lldb::addr_t cf_true = LLDB_INVALID_ADDRESS,
1060 cf_false = LLDB_INVALID_ADDRESS;
1061 objc_runtime->GetValuesForGlobalCFBooleans(cf_true, cf_false);
1062 if (valobj_ptr_value == cf_true) {
1063 stream.PutCString("YES");
1064 return true;
1065 }
1066 if (valobj_ptr_value == cf_false) {
1067 stream.PutCString("NO");
1068 return true;
1069 }
1070 }
1071
1072 return false;
Enrico Granata2094e442016-08-22 18:07:52 +00001073}
1074
Enrico Granatad87cc312015-09-03 01:29:42 +00001075template <bool is_sel_ptr>
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076bool lldb_private::formatters::ObjCSELSummaryProvider(
1077 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1078 lldb::ValueObjectSP valobj_sp;
1079
1080 CompilerType charstar(valobj.GetCompilerType()
1081 .GetBasicTypeFromAST(eBasicTypeChar)
1082 .GetPointerType());
1083
1084 if (!charstar)
1085 return false;
1086
1087 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1088
1089 if (is_sel_ptr) {
1090 lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1091 if (data_address == LLDB_INVALID_ADDRESS)
1092 return false;
1093 valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address,
1094 exe_ctx, charstar);
1095 } else {
1096 DataExtractor data;
Zachary Turner97206d52017-05-12 04:51:55 +00001097 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 valobj.GetData(data, error);
1099 if (error.Fail())
1100 return false;
1101 valobj_sp =
1102 ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1103 }
1104
1105 if (!valobj_sp)
1106 return false;
1107
1108 stream.Printf("%s", valobj_sp->GetSummaryAsCString());
1109 return true;
Enrico Granatad87cc312015-09-03 01:29:42 +00001110}
1111
1112// POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001
1113// this call gives the POSIX equivalent of the Cocoa epoch
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114time_t lldb_private::formatters::GetOSXEpoch() {
1115 static time_t epoch = 0;
1116 if (!epoch) {
Enrico Granatad87cc312015-09-03 01:29:42 +00001117#ifndef _WIN32
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 tzset();
1119 tm tm_epoch;
1120 tm_epoch.tm_sec = 0;
1121 tm_epoch.tm_hour = 0;
1122 tm_epoch.tm_min = 0;
1123 tm_epoch.tm_mon = 0;
1124 tm_epoch.tm_mday = 1;
1125 tm_epoch.tm_year = 2001 - 1900;
1126 tm_epoch.tm_isdst = -1;
1127 tm_epoch.tm_gmtoff = 0;
1128 tm_epoch.tm_zone = nullptr;
1129 epoch = timegm(&tm_epoch);
Enrico Granatad87cc312015-09-03 01:29:42 +00001130#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 }
1132 return epoch;
Enrico Granatad87cc312015-09-03 01:29:42 +00001133}
1134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135template bool lldb_private::formatters::NSDataSummaryProvider<true>(
1136 ValueObject &, Stream &, const TypeSummaryOptions &);
Enrico Granatad87cc312015-09-03 01:29:42 +00001137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138template bool lldb_private::formatters::NSDataSummaryProvider<false>(
1139 ValueObject &, Stream &, const TypeSummaryOptions &);
Enrico Granatad87cc312015-09-03 01:29:42 +00001140
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141template bool lldb_private::formatters::ObjCSELSummaryProvider<true>(
1142 ValueObject &, Stream &, const TypeSummaryOptions &);
Enrico Granatad87cc312015-09-03 01:29:42 +00001143
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144template bool lldb_private::formatters::ObjCSELSummaryProvider<false>(
1145 ValueObject &, Stream &, const TypeSummaryOptions &);