Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/ObjC/Cocoa.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Cocoa.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 "Cocoa.h"
10
#include "NSString.h"
11
#include "ObjCConstants.h"
12
13
#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h"
14
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15
#include "lldb/Core/Mangled.h"
16
#include "lldb/Core/ValueObject.h"
17
#include "lldb/Core/ValueObjectConstResult.h"
18
#include "lldb/DataFormatters/FormattersHelpers.h"
19
#include "lldb/DataFormatters/StringPrinter.h"
20
#include "lldb/DataFormatters/TypeSummary.h"
21
#include "lldb/Host/Time.h"
22
#include "lldb/Target/Language.h"
23
#include "lldb/Target/Process.h"
24
#include "lldb/Target/ProcessStructReader.h"
25
#include "lldb/Target/Target.h"
26
#include "lldb/Utility/DataBufferHeap.h"
27
#include "lldb/Utility/Endian.h"
28
#include "lldb/Utility/Status.h"
29
#include "lldb/Utility/Stream.h"
30
31
#include "llvm/ADT/APInt.h"
32
#include "llvm/ADT/bit.h"
33
34
35
using namespace lldb;
36
using namespace lldb_private;
37
using namespace lldb_private::formatters;
38
39
bool lldb_private::formatters::NSBundleSummaryProvider(
40
36
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
41
36
  ProcessSP process_sp = valobj.GetProcessSP();
42
36
  if (!process_sp)
43
0
    return false;
44
45
36
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
46
47
36
  if (!runtime)
48
0
    return false;
49
50
36
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
51
36
      runtime->GetClassDescriptor(valobj));
52
53
36
  if (!descriptor || !descriptor->IsValid())
54
0
    return false;
55
56
36
  uint32_t ptr_size = process_sp->GetAddressByteSize();
57
58
36
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
59
60
36
  if (!valobj_addr)
61
0
    return false;
62
63
36
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
64
65
36
  if (class_name.empty())
66
0
    return false;
67
68
36
  if (class_name == "NSBundle") {
69
36
    uint64_t offset = 5 * ptr_size;
70
36
    ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
71
36
        offset,
72
36
        valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeObjCID),
73
36
        true));
74
75
36
    if (!text)
76
0
      return false;
77
78
36
    StreamString summary_stream;
79
36
    bool was_nsstring_ok =
80
36
        NSStringSummaryProvider(*text, summary_stream, options);
81
36
    if (was_nsstring_ok && summary_stream.GetSize() > 0) {
82
36
      stream.Printf("%s", summary_stream.GetData());
83
36
      return true;
84
36
    }
85
36
  }
86
87
0
  return false;
88
36
}
89
90
bool lldb_private::formatters::NSTimeZoneSummaryProvider(
91
54
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
92
54
  ProcessSP process_sp = valobj.GetProcessSP();
93
54
  if (!process_sp)
94
0
    return false;
95
96
54
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
97
98
54
  if (!runtime)
99
0
    return false;
100
101
54
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
102
54
      runtime->GetClassDescriptor(valobj));
103
104
54
  if (!descriptor || !descriptor->IsValid())
105
0
    return false;
106
107
54
  uint32_t ptr_size = process_sp->GetAddressByteSize();
108
109
54
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
110
111
54
  if (!valobj_addr)
112
0
    return false;
113
114
54
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
115
116
54
  if (class_name.empty())
117
0
    return false;
118
119
54
  if (class_name == "__NSTimeZone") {
120
54
    uint64_t offset = ptr_size;
121
54
    ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
122
54
        offset, valobj.GetCompilerType(), true));
123
124
54
    if (!text)
125
0
      return false;
126
127
54
    StreamString summary_stream;
128
54
    bool was_nsstring_ok =
129
54
        NSStringSummaryProvider(*text, summary_stream, options);
130
54
    if (was_nsstring_ok && summary_stream.GetSize() > 0) {
131
54
      stream.Printf("%s", summary_stream.GetData());
132
54
      return true;
133
54
    }
134
54
  }
135
136
0
  return false;
137
54
}
138
139
bool lldb_private::formatters::NSNotificationSummaryProvider(
140
0
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
141
0
  ProcessSP process_sp = valobj.GetProcessSP();
142
0
  if (!process_sp)
143
0
    return false;
144
145
0
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
146
147
0
  if (!runtime)
148
0
    return false;
149
150
0
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
151
0
      runtime->GetClassDescriptor(valobj));
152
153
0
  if (!descriptor || !descriptor->IsValid())
154
0
    return false;
155
156
0
  uint32_t ptr_size = process_sp->GetAddressByteSize();
157
158
0
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
159
160
0
  if (!valobj_addr)
161
0
    return false;
162
163
0
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
164
165
0
  if (class_name.empty())
166
0
    return false;
167
168
0
  if (class_name == "NSConcreteNotification") {
169
0
    uint64_t offset = ptr_size;
170
0
    ValueObjectSP text(valobj.GetSyntheticChildAtOffset(
171
0
        offset, valobj.GetCompilerType(), true));
172
173
0
    if (!text)
174
0
      return false;
175
176
0
    StreamString summary_stream;
177
0
    bool was_nsstring_ok =
178
0
        NSStringSummaryProvider(*text, summary_stream, options);
179
0
    if (was_nsstring_ok && summary_stream.GetSize() > 0) {
180
0
      stream.Printf("%s", summary_stream.GetData());
181
0
      return true;
182
0
    }
183
0
  }
184
185
0
  return false;
186
0
}
187
188
bool lldb_private::formatters::NSMachPortSummaryProvider(
189
0
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
190
0
  ProcessSP process_sp = valobj.GetProcessSP();
191
0
  if (!process_sp)
192
0
    return false;
193
194
0
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
195
196
0
  if (!runtime)
197
0
    return false;
198
199
0
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
200
0
      runtime->GetClassDescriptor(valobj));
201
202
0
  if (!descriptor || !descriptor->IsValid())
203
0
    return false;
204
205
0
  uint32_t ptr_size = process_sp->GetAddressByteSize();
206
207
0
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
208
209
0
  if (!valobj_addr)
210
0
    return false;
211
212
0
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
213
214
0
  if (class_name.empty())
215
0
    return false;
216
217
0
  uint64_t port_number = 0;
218
219
0
  if (class_name == "NSMachPort") {
220
0
    uint64_t offset = (ptr_size == 4 ? 12 : 20);
221
0
    Status error;
222
0
    port_number = process_sp->ReadUnsignedIntegerFromMemory(
223
0
        offset + valobj_addr, 4, 0, error);
224
0
    if (error.Success()) {
225
0
      stream.Printf("mach port: %u",
226
0
                    (uint32_t)(port_number & 0x00000000FFFFFFFF));
227
0
      return true;
228
0
    }
229
0
  }
230
231
0
  return false;
232
0
}
233
234
bool lldb_private::formatters::NSIndexSetSummaryProvider(
235
33
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
236
33
  ProcessSP process_sp = valobj.GetProcessSP();
237
33
  if (!process_sp)
238
0
    return false;
239
240
33
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
241
242
33
  if (!runtime)
243
0
    return false;
244
245
33
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
246
33
      runtime->GetClassDescriptor(valobj));
247
248
33
  if (!descriptor || !descriptor->IsValid())
249
0
    return false;
250
251
33
  uint32_t ptr_size = process_sp->GetAddressByteSize();
252
253
33
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
254
255
33
  if (!valobj_addr)
256
0
    return false;
257
258
33
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
259
260
33
  if (class_name.empty())
261
0
    return false;
262
263
33
  uint64_t count = 0;
264
265
33
  do {
266
33
    if (class_name == "NSIndexSet" || 
class_name == "NSMutableIndexSet"15
) {
267
33
      Status error;
268
33
      uint32_t mode = process_sp->ReadUnsignedIntegerFromMemory(
269
33
          valobj_addr + ptr_size, 4, 0, error);
270
33
      if (error.Fail())
271
0
        return false;
272
      // this means the set is empty - count = 0
273
33
      if ((mode & 1) == 1) {
274
0
        count = 0;
275
0
        break;
276
0
      }
277
33
      if ((mode & 2) == 2)
278
24
        mode = 1; // this means the set only has one range
279
9
      else
280
9
        mode = 2; // this means the set has multiple ranges
281
33
      if (mode == 1) {
282
24
        count = process_sp->ReadUnsignedIntegerFromMemory(
283
24
            valobj_addr + 3 * ptr_size, ptr_size, 0, error);
284
24
        if (error.Fail())
285
0
          return false;
286
24
      } else {
287
        // read a pointer to the data at 2*ptr_size
288
9
        count = process_sp->ReadUnsignedIntegerFromMemory(
289
9
            valobj_addr + 2 * ptr_size, ptr_size, 0, error);
290
9
        if (error.Fail())
291
0
          return false;
292
        // read the data at 2*ptr_size from the first location
293
9
        count = process_sp->ReadUnsignedIntegerFromMemory(count + 2 * ptr_size,
294
9
                                                          ptr_size, 0, error);
295
9
        if (error.Fail())
296
0
          return false;
297
9
      }
298
33
    } else
299
0
      return false;
300
33
  } while (false);
301
33
  stream.Printf("%" PRIu64 " index%s", count, (count == 1 ? 
""6
:
"es"27
));
302
33
  return true;
303
33
}
304
305
static void NSNumber_FormatChar(ValueObject &valobj, Stream &stream, char value,
306
33
                                lldb::LanguageType lang) {
307
33
  static ConstString g_TypeHint("NSNumber:char");
308
309
33
  std::string prefix, suffix;
310
33
  if (Language *language = Language::FindPlugin(lang)) {
311
33
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
312
33
                                            suffix)) {
313
0
      prefix.clear();
314
0
      suffix.clear();
315
0
    }
316
33
  }
317
318
33
  stream.Printf("%s%hhd%s", prefix.c_str(), value, suffix.c_str());
319
33
}
320
321
static void NSNumber_FormatShort(ValueObject &valobj, Stream &stream,
322
21
                                 short value, lldb::LanguageType lang) {
323
21
  static ConstString g_TypeHint("NSNumber:short");
324
325
21
  std::string prefix, suffix;
326
21
  if (Language *language = Language::FindPlugin(lang)) {
327
21
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
328
21
                                            suffix)) {
329
0
      prefix.clear();
330
0
      suffix.clear();
331
0
    }
332
21
  }
333
334
21
  stream.Printf("%s%hd%s", prefix.c_str(), value, suffix.c_str());
335
21
}
336
337
static void NSNumber_FormatInt(ValueObject &valobj, Stream &stream, int value,
338
267
                               lldb::LanguageType lang) {
339
267
  static ConstString g_TypeHint("NSNumber:int");
340
341
267
  std::string prefix, suffix;
342
267
  if (Language *language = Language::FindPlugin(lang)) {
343
267
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
344
267
                                            suffix)) {
345
6
      prefix.clear();
346
6
      suffix.clear();
347
6
    }
348
267
  }
349
350
267
  stream.Printf("%s%d%s", prefix.c_str(), value, suffix.c_str());
351
267
}
352
353
static void NSNumber_FormatLong(ValueObject &valobj, Stream &stream,
354
84
                                int64_t value, lldb::LanguageType lang) {
355
84
  static ConstString g_TypeHint("NSNumber:long");
356
357
84
  std::string prefix, suffix;
358
84
  if (Language *language = Language::FindPlugin(lang)) {
359
84
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
360
84
                                            suffix)) {
361
18
      prefix.clear();
362
18
      suffix.clear();
363
18
    }
364
84
  }
365
366
84
  stream.Printf("%s%" PRId64 "%s", prefix.c_str(), value, suffix.c_str());
367
84
}
368
369
static void NSNumber_FormatInt128(ValueObject &valobj, Stream &stream,
370
                                  const llvm::APInt &value,
371
33
                                  lldb::LanguageType lang) {
372
33
  static ConstString g_TypeHint("NSNumber:int128_t");
373
374
33
  std::string prefix, suffix;
375
33
  if (Language *language = Language::FindPlugin(lang)) {
376
33
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
377
33
                                            suffix)) {
378
0
      prefix.clear();
379
0
      suffix.clear();
380
0
    }
381
33
  }
382
383
33
  stream.PutCString(prefix.c_str());
384
33
  const int radix = 10;
385
33
  const bool isSigned = true;
386
33
  std::string str = llvm::toString(value, radix, isSigned);
387
33
  stream.PutCString(str.c_str());
388
33
  stream.PutCString(suffix.c_str());
389
33
}
390
391
static void NSNumber_FormatFloat(ValueObject &valobj, Stream &stream,
392
33
                                 float value, lldb::LanguageType lang) {
393
33
  static ConstString g_TypeHint("NSNumber:float");
394
395
33
  std::string prefix, suffix;
396
33
  if (Language *language = Language::FindPlugin(lang)) {
397
33
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
398
33
                                            suffix)) {
399
0
      prefix.clear();
400
0
      suffix.clear();
401
0
    }
402
33
  }
403
404
33
  stream.Printf("%s%f%s", prefix.c_str(), value, suffix.c_str());
405
33
}
406
407
static void NSNumber_FormatDouble(ValueObject &valobj, Stream &stream,
408
78
                                  double value, lldb::LanguageType lang) {
409
78
  static ConstString g_TypeHint("NSNumber:double");
410
411
78
  std::string prefix, suffix;
412
78
  if (Language *language = Language::FindPlugin(lang)) {
413
78
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
414
78
                                            suffix)) {
415
0
      prefix.clear();
416
0
      suffix.clear();
417
0
    }
418
78
  }
419
420
78
  stream.Printf("%s%g%s", prefix.c_str(), value, suffix.c_str());
421
78
}
422
423
bool lldb_private::formatters::NSNumberSummaryProvider(
424
702
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
425
702
  ProcessSP process_sp = valobj.GetProcessSP();
426
702
  if (!process_sp)
427
0
    return false;
428
429
702
  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS);
430
702
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
431
432
702
  if (!runtime)
433
0
    return false;
434
435
702
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
436
702
      runtime->GetClassDescriptor(valobj));
437
438
702
  if (!descriptor || 
!descriptor->IsValid()667
)
439
35
    return false;
440
441
667
  uint32_t ptr_size = process_sp->GetAddressByteSize();
442
443
667
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
444
445
667
  if (!valobj_addr)
446
0
    return false;
447
448
667
  llvm::StringRef class_name(descriptor->GetClassName().GetCString());
449
450
667
  if (class_name.empty())
451
0
    return false;
452
453
667
  if (class_name == "__NSCFBoolean")
454
66
    return ObjCBooleanSummaryProvider(valobj, stream, options);
455
456
601
  if (class_name == "NSDecimalNumber")
457
30
    return NSDecimalNumberSummaryProvider(valobj, stream, options);
458
459
571
  if (class_name == "NSConstantIntegerNumber") {
460
0
    Status error;
461
0
    int64_t value = process_sp->ReadSignedIntegerFromMemory(
462
0
        valobj_addr + 2 * ptr_size, 8, 0, error);
463
0
    if (error.Fail())
464
0
      return false;
465
0
    uint64_t encoding_addr = process_sp->ReadUnsignedIntegerFromMemory(
466
0
        valobj_addr + ptr_size, ptr_size, 0, error);
467
0
    if (error.Fail())
468
0
      return false;
469
0
    char encoding =
470
0
        process_sp->ReadUnsignedIntegerFromMemory(encoding_addr, 1, 0, error);
471
0
    if (error.Fail())
472
0
      return false;
473
474
0
    switch (encoding) {
475
0
    case _C_CHR:
476
0
      NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
477
0
      return true;
478
0
    case _C_SHT:
479
0
      NSNumber_FormatShort(valobj, stream, (short)value, options.GetLanguage());
480
0
      return true;
481
0
    case _C_INT:
482
0
      NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
483
0
      return true;
484
0
    case _C_LNG:
485
0
    case _C_LNG_LNG:
486
0
      NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
487
0
      return true;
488
489
0
    case _C_UCHR:
490
0
    case _C_USHT:
491
0
    case _C_UINT:
492
0
    case _C_ULNG:
493
0
    case _C_ULNG_LNG:
494
0
      stream.Printf("%" PRIu64, value);
495
0
      return true;
496
0
    }
497
498
0
    return false;
499
0
  }
500
501
571
  if (class_name == "NSConstantFloatNumber") {
502
0
    Status error;
503
0
    uint32_t flt_as_int = process_sp->ReadUnsignedIntegerFromMemory(
504
0
        valobj_addr + ptr_size, 4, 0, error);
505
0
    if (error.Fail())
506
0
      return false;
507
0
    float flt_value = 0.0f;
508
0
    memcpy(&flt_value, &flt_as_int, sizeof(flt_as_int));
509
0
    NSNumber_FormatFloat(valobj, stream, flt_value, options.GetLanguage());
510
0
    return true;
511
0
  }
512
513
571
  if (class_name == "NSConstantDoubleNumber") {
514
0
    Status error;
515
0
    uint64_t dbl_as_lng = process_sp->ReadUnsignedIntegerFromMemory(
516
0
        valobj_addr + ptr_size, 8, 0, error);
517
0
    if (error.Fail())
518
0
      return false;
519
0
    double dbl_value = 0.0;
520
0
    memcpy(&dbl_value, &dbl_as_lng, sizeof(dbl_as_lng));
521
0
    NSNumber_FormatDouble(valobj, stream, dbl_value, options.GetLanguage());
522
0
    return true;
523
0
  }
524
525
571
  if (class_name == "NSNumber" || class_name == "__NSCFNumber") {
526
553
    int64_t value = 0;
527
553
    uint64_t i_bits = 0;
528
553
    if (descriptor->GetTaggedPointerInfoSigned(&i_bits, &value)) {
529
      // Check for "preserved" numbers.  We still don't support them yet.
530
397
      if (i_bits & 0x8) {
531
4
        if (log)
532
0
          log->Printf(
533
0
              "Unsupported (preserved) NSNumber tagged pointer 0x%" PRIu64,
534
0
              valobj_addr);
535
4
        return false;
536
4
      }
537
538
393
      switch (i_bits) {
539
33
      case 0:
540
33
        NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
541
33
        break;
542
21
      case 1:
543
21
      case 4:
544
21
        NSNumber_FormatShort(valobj, stream, (short)value,
545
21
                             options.GetLanguage());
546
21
        break;
547
267
      case 2:
548
267
      case 8:
549
267
        NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
550
267
        break;
551
72
      case 3:
552
72
      case 12:
553
72
        NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
554
72
        break;
555
0
      default:
556
0
        return false;
557
393
      }
558
393
      return true;
559
393
    } else {
560
156
      Status error;
561
562
156
      AppleObjCRuntime *runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
563
156
          ObjCLanguageRuntime::Get(*process_sp));
564
565
156
      const bool new_format =
566
156
          (runtime && runtime->GetFoundationVersion() >= 1400);
567
568
156
      enum class TypeCodes : int {
569
156
        sint8 = 0x0,
570
156
        sint16 = 0x1,
571
156
        sint32 = 0x2,
572
156
        sint64 = 0x3,
573
156
        f32 = 0x4,
574
156
        f64 = 0x5,
575
156
        sint128 = 0x6
576
156
      };
577
578
156
      uint64_t data_location = valobj_addr + 2 * ptr_size;
579
156
      TypeCodes type_code;
580
581
156
      if (new_format) {
582
156
        uint64_t cfinfoa = process_sp->ReadUnsignedIntegerFromMemory(
583
156
            valobj_addr + ptr_size, ptr_size, 0, error);
584
585
156
        if (error.Fail())
586
0
          return false;
587
588
156
        bool is_preserved_number = cfinfoa & 0x8;
589
156
        if (is_preserved_number) {
590
0
          if (log)
591
0
            log->Printf(
592
0
                "Unsupported preserved NSNumber tagged pointer 0x%" PRIu64,
593
0
                valobj_addr);
594
0
          return false;
595
0
        }
596
597
156
        type_code = static_cast<TypeCodes>(cfinfoa & 0x7);
598
156
      } else {
599
0
        uint8_t data_type = process_sp->ReadUnsignedIntegerFromMemory(
600
0
                                valobj_addr + ptr_size, 1, 0, error) &
601
0
                            0x1F;
602
603
0
        if (error.Fail())
604
0
          return false;
605
606
0
        switch (data_type) {
607
0
        case 1:
608
0
          type_code = TypeCodes::sint8;
609
0
          break;
610
0
        case 2:
611
0
          type_code = TypeCodes::sint16;
612
0
          break;
613
0
        case 3:
614
0
          type_code = TypeCodes::sint32;
615
0
          break;
616
0
        case 17:
617
0
          data_location += 8;
618
0
          LLVM_FALLTHROUGH;
619
0
        case 4:
620
0
          type_code = TypeCodes::sint64;
621
0
          break;
622
0
        case 5:
623
0
          type_code = TypeCodes::f32;
624
0
          break;
625
0
        case 6:
626
0
          type_code = TypeCodes::f64;
627
0
          break;
628
0
        default:
629
0
          return false;
630
0
        }
631
0
      }
632
633
156
      uint64_t value = 0;
634
156
      bool success = false;
635
156
      switch (type_code) {
636
0
      case TypeCodes::sint8:
637
0
        value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 1, 0,
638
0
                                                          error);
639
0
        if (error.Fail())
640
0
          return false;
641
0
        NSNumber_FormatChar(valobj, stream, (char)value, options.GetLanguage());
642
0
        success = true;
643
0
        break;
644
0
      case TypeCodes::sint16:
645
0
        value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 2, 0,
646
0
                                                          error);
647
0
        if (error.Fail())
648
0
          return false;
649
0
        NSNumber_FormatShort(valobj, stream, (short)value,
650
0
                             options.GetLanguage());
651
0
        success = true;
652
0
        break;
653
0
      case TypeCodes::sint32:
654
0
        value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 4, 0,
655
0
                                                          error);
656
0
        if (error.Fail())
657
0
          return false;
658
0
        NSNumber_FormatInt(valobj, stream, (int)value, options.GetLanguage());
659
0
        success = true;
660
0
        break;
661
12
      case TypeCodes::sint64:
662
12
        value = process_sp->ReadUnsignedIntegerFromMemory(data_location, 8, 0,
663
12
                                                          error);
664
12
        if (error.Fail())
665
0
          return false;
666
12
        NSNumber_FormatLong(valobj, stream, value, options.GetLanguage());
667
12
        success = true;
668
12
        break;
669
33
      case TypeCodes::f32: {
670
33
        uint32_t flt_as_int = process_sp->ReadUnsignedIntegerFromMemory(
671
33
            data_location, 4, 0, error);
672
33
        if (error.Fail())
673
0
          return false;
674
33
        float flt_value = 0.0f;
675
33
        memcpy(&flt_value, &flt_as_int, sizeof(flt_as_int));
676
33
        NSNumber_FormatFloat(valobj, stream, flt_value, options.GetLanguage());
677
33
        success = true;
678
33
        break;
679
33
      }
680
78
      case TypeCodes::f64: {
681
78
        uint64_t dbl_as_lng = process_sp->ReadUnsignedIntegerFromMemory(
682
78
            data_location, 8, 0, error);
683
78
        if (error.Fail())
684
0
          return false;
685
78
        double dbl_value = 0.0;
686
78
        memcpy(&dbl_value, &dbl_as_lng, sizeof(dbl_as_lng));
687
78
        NSNumber_FormatDouble(valobj, stream, dbl_value, options.GetLanguage());
688
78
        success = true;
689
78
        break;
690
78
      }
691
33
      case TypeCodes::sint128: // internally, this is the same
692
33
      {
693
33
        uint64_t words[2];
694
33
        words[1] = process_sp->ReadUnsignedIntegerFromMemory(data_location, 8,
695
33
                                                             0, error);
696
33
        if (error.Fail())
697
0
          return false;
698
33
        words[0] = process_sp->ReadUnsignedIntegerFromMemory(data_location + 8,
699
33
                                                             8, 0, error);
700
33
        if (error.Fail())
701
0
          return false;
702
33
        llvm::APInt i128_value(128, words);
703
33
        NSNumber_FormatInt128(valobj, stream, i128_value,
704
33
                              options.GetLanguage());
705
33
        success = true;
706
33
        break;
707
33
      }
708
156
      }
709
156
      return success;
710
156
    }
711
553
  }
712
713
18
  return false;
714
571
}
715
716
bool lldb_private::formatters::NSDecimalNumberSummaryProvider(
717
30
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
718
30
  ProcessSP process_sp = valobj.GetProcessSP();
719
30
  if (!process_sp)
720
0
    return false;
721
722
30
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
723
30
  uint32_t ptr_size = process_sp->GetAddressByteSize();
724
725
30
  Status error;
726
30
  int8_t exponent = process_sp->ReadUnsignedIntegerFromMemory(
727
30
      valobj_addr + ptr_size, 1, 0, error);
728
30
  if (error.Fail())
729
0
    return false;
730
731
30
  uint8_t length_and_negative = process_sp->ReadUnsignedIntegerFromMemory(
732
30
      valobj_addr + ptr_size + 1, 1, 0, error);
733
30
  if (error.Fail())
734
0
    return false;
735
736
  // Fifth bit marks negativity.
737
30
  const bool is_negative = (length_and_negative >> 4) & 1;
738
739
  // Zero length and negative means NaN.
740
30
  uint8_t length = length_and_negative & 0xf;
741
30
  const bool is_nan = is_negative && 
(length == 0)15
;
742
743
30
  if (is_nan) {
744
0
    stream.Printf("NaN");
745
0
    return true;
746
0
  }
747
748
30
  if (length == 0) {
749
0
    stream.Printf("0");
750
0
    return true;
751
0
  }
752
753
30
  uint64_t mantissa = process_sp->ReadUnsignedIntegerFromMemory(
754
30
      valobj_addr + ptr_size + 4, 8, 0, error);
755
30
  if (error.Fail())
756
0
    return false;
757
758
30
  if (is_negative)
759
15
    stream.Printf("-");
760
761
30
  stream.Printf("%" PRIu64 " x 10^%" PRIi8, mantissa, exponent);
762
30
  return true;
763
30
}
764
765
bool lldb_private::formatters::NSURLSummaryProvider(
766
162
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
767
162
  ProcessSP process_sp = valobj.GetProcessSP();
768
162
  if (!process_sp)
769
0
    return false;
770
771
162
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
772
773
162
  if (!runtime)
774
0
    return false;
775
776
162
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
777
162
      runtime->GetClassDescriptor(valobj));
778
779
162
  if (!descriptor || 
!descriptor->IsValid()156
)
780
6
    return false;
781
782
156
  uint32_t ptr_size = process_sp->GetAddressByteSize();
783
784
156
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
785
786
156
  if (!valobj_addr)
787
0
    return false;
788
789
156
  llvm::StringRef class_name = descriptor->GetClassName().GetStringRef();
790
791
156
  if (!class_name.equals("NSURL"))
792
0
    return false;
793
794
156
  uint64_t offset_text = ptr_size + ptr_size +
795
156
                         8; // ISA + pointer + 8 bytes of data (even on 32bit)
796
156
  uint64_t offset_base = offset_text + ptr_size;
797
156
  CompilerType type(valobj.GetCompilerType());
798
156
  ValueObjectSP text(valobj.GetSyntheticChildAtOffset(offset_text, type, true));
799
156
  ValueObjectSP base(valobj.GetSyntheticChildAtOffset(offset_base, type, true));
800
156
  if (!text || text->GetValueAsUnsigned(0) == 0)
801
0
    return false;
802
803
156
  StreamString base_summary;
804
156
  if (base && base->GetValueAsUnsigned(0)) {
805
48
    if (!NSURLSummaryProvider(*base, base_summary, options))
806
0
      base_summary.Clear();
807
48
  }
808
156
  if (base_summary.Empty())
809
108
    return NSStringSummaryProvider(*text, stream, options);
810
811
48
  StreamString summary;
812
48
  if (!NSStringSummaryProvider(*text, summary, options) || summary.Empty())
813
0
    return false;
814
815
48
  const char quote_char = '"';
816
48
  std::string prefix, suffix;
817
48
  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
818
48
    if (!language->GetFormatterPrefixSuffix(*text, ConstString("NSString"),
819
48
                                            prefix, suffix)) {
820
0
      prefix.clear();
821
0
      suffix.clear();
822
0
    }
823
48
  }
824
  // @"A" -> @"A
825
48
  llvm::StringRef summary_str = summary.GetString();
826
48
  bool back_consumed = summary_str.consume_back(quote_char + suffix);
827
48
  assert(back_consumed);
828
48
  UNUSED_IF_ASSERT_DISABLED(back_consumed);
829
  // @"B" -> B"
830
48
  llvm::StringRef base_summary_str = base_summary.GetString();
831
48
  bool front_consumed = base_summary_str.consume_front(prefix + quote_char);
832
48
  assert(front_consumed);
833
48
  UNUSED_IF_ASSERT_DISABLED(front_consumed);
834
  // @"A -- B"
835
48
  if (!summary_str.empty() && !base_summary_str.empty()) {
836
48
    stream.Printf("%s -- %s", summary_str.str().c_str(),
837
48
                  base_summary_str.str().c_str());
838
48
    return true;
839
48
  }
840
841
0
  return false;
842
48
}
843
844
/// Bias value for tagged pointer exponents.
845
/// Recommended values:
846
/// 0x3e3: encodes all dates between distantPast and distantFuture
847
///   except for the range within about 1e-28 second of the reference date.
848
/// 0x3ef: encodes all dates for a few million years beyond distantPast and
849
///   distantFuture, except within about 1e-25 second of the reference date.
850
const int TAGGED_DATE_EXPONENT_BIAS = 0x3ef;
851
852
struct DoubleBits {
853
  uint64_t fraction : 52; // unsigned
854
  uint64_t exponent : 11; // signed
855
  uint64_t sign : 1;
856
};
857
858
struct TaggedDoubleBits {
859
  uint64_t fraction : 52; // unsigned
860
  uint64_t exponent : 7;  // signed
861
  uint64_t sign : 1;
862
  uint64_t unused : 4; // placeholder for pointer tag bits
863
};
864
865
138
static uint64_t decodeExponent(uint64_t exp) {
866
  // Tagged exponent field is 7-bit signed. Sign-extend the value to 64 bits
867
  // before performing arithmetic.
868
138
  return llvm::SignExtend64<7>(exp) + TAGGED_DATE_EXPONENT_BIAS;
869
138
}
870
871
138
static double decodeTaggedTimeInterval(uint64_t encodedTimeInterval) {
872
138
  if (encodedTimeInterval == 0)
873
0
    return 0.0;
874
138
  if (encodedTimeInterval == std::numeric_limits<uint64_t>::max())
875
0
    return (uint64_t)-0.0;
876
877
138
  TaggedDoubleBits encodedBits =
878
138
      llvm::bit_cast<TaggedDoubleBits>(encodedTimeInterval);
879
138
  assert(encodedBits.unused == 0);
880
881
  // Sign and fraction are represented exactly.
882
  // Exponent is encoded.
883
0
  DoubleBits decodedBits;
884
138
  decodedBits.sign = encodedBits.sign;
885
138
  decodedBits.fraction = encodedBits.fraction;
886
138
  decodedBits.exponent = decodeExponent(encodedBits.exponent);
887
888
138
  return llvm::bit_cast<double>(decodedBits);
889
138
}
890
891
bool lldb_private::formatters::NSDateSummaryProvider(
892
180
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
893
180
  ProcessSP process_sp = valobj.GetProcessSP();
894
180
  if (!process_sp)
895
0
    return false;
896
897
180
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
898
899
180
  if (!runtime)
900
0
    return false;
901
902
180
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
903
180
      runtime->GetClassDescriptor(valobj));
904
905
180
  if (!descriptor || !descriptor->IsValid())
906
0
    return false;
907
908
180
  uint32_t ptr_size = process_sp->GetAddressByteSize();
909
910
180
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
911
912
180
  if (!valobj_addr)
913
3
    return false;
914
915
177
  uint64_t date_value_bits = 0;
916
177
  double date_value = 0.0;
917
918
177
  ConstString class_name = descriptor->GetClassName();
919
920
177
  static const ConstString g_NSDate("NSDate");
921
177
  static const ConstString g___NSDate("__NSDate");
922
177
  static const ConstString g___NSTaggedDate("__NSTaggedDate");
923
177
  static const ConstString g_NSCalendarDate("NSCalendarDate");
924
177
  static const ConstString g_NSConstantDate("NSConstantDate");
925
926
177
  if (class_name.IsEmpty())
927
0
    return false;
928
929
177
  uint64_t info_bits = 0, value_bits = 0;
930
177
  if ((class_name == g_NSDate) || (class_name == g___NSDate) ||
931
177
      (class_name == g___NSTaggedDate) || 
(class_name == g_NSConstantDate)39
) {
932
168
    if (descriptor->GetTaggedPointerInfo(&info_bits, &value_bits)) {
933
138
      date_value_bits = ((value_bits << 8) | (info_bits << 4));
934
138
      memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
935
138
    } else {
936
30
      llvm::Triple triple(
937
30
          process_sp->GetTarget().GetArchitecture().GetTriple());
938
30
      uint32_t delta =
939
30
          (triple.isWatchOS() && 
triple.isWatchABI()0
) ?
80
: ptr_size;
940
30
      Status error;
941
30
      date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(
942
30
          valobj_addr + delta, 8, 0, error);
943
30
      memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
944
30
      if (error.Fail())
945
0
        return false;
946
30
    }
947
168
  } else 
if (9
class_name == g_NSCalendarDate9
) {
948
9
    Status error;
949
9
    date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(
950
9
        valobj_addr + 2 * ptr_size, 8, 0, error);
951
9
    memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
952
9
    if (error.Fail())
953
0
      return false;
954
9
  } else
955
0
    return false;
956
957
  // FIXME: It seems old dates are not formatted according to NSDate's calendar
958
  // so we hardcode distantPast's value so that it looks like LLDB is doing
959
  // the right thing.
960
961
  // The relative time in seconds from Cocoa Epoch to [NSDate distantPast].
962
177
  const double RelSecondsFromCocoaEpochToNSDateDistantPast = -63114076800;
963
177
  if (date_value == RelSecondsFromCocoaEpochToNSDateDistantPast) {
964
15
    stream.Printf("0001-01-01 00:00:00 UTC");
965
15
    return true;
966
15
  }
967
968
  // Accomodate for the __NSTaggedDate format introduced in Foundation 1600.
969
162
  if (class_name == g___NSTaggedDate) {
970
138
    auto *runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
971
138
        ObjCLanguageRuntime::Get(*process_sp));
972
138
    if (runtime && runtime->GetFoundationVersion() >= 1600)
973
138
      date_value = decodeTaggedTimeInterval(value_bits << 4);
974
138
  }
975
976
  // this snippet of code assumes that time_t == seconds since Jan-1-1970 this
977
  // is generally true and POSIXly happy, but might break if a library vendor
978
  // decides to get creative
979
162
  time_t epoch = GetOSXEpoch();
980
162
  epoch = epoch + static_cast<time_t>(std::floor(date_value));
981
162
  tm *tm_date = gmtime(&epoch);
982
162
  if (!tm_date)
983
0
    return false;
984
162
  std::string buffer(1024, 0);
985
162
  if (strftime(&buffer[0], 1023, "%Z", tm_date) == 0)
986
0
    return false;
987
162
  stream.Printf("%04d-%02d-%02d %02d:%02d:%02d %s", tm_date->tm_year + 1900,
988
162
                tm_date->tm_mon + 1, tm_date->tm_mday, tm_date->tm_hour,
989
162
                tm_date->tm_min, tm_date->tm_sec, buffer.c_str());
990
162
  return true;
991
162
}
992
993
bool lldb_private::formatters::ObjCClassSummaryProvider(
994
185
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
995
185
  ProcessSP process_sp = valobj.GetProcessSP();
996
185
  if (!process_sp)
997
0
    return false;
998
999
185
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
1000
1001
185
  if (!runtime)
1002
0
    return false;
1003
1004
185
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
1005
185
      runtime->GetClassDescriptorFromISA(valobj.GetValueAsUnsigned(0)));
1006
1007
185
  if (!descriptor || !descriptor->IsValid())
1008
0
    return false;
1009
1010
185
  ConstString class_name = descriptor->GetClassName();
1011
1012
185
  if (class_name.IsEmpty())
1013
0
    return false;
1014
1015
185
  if (ConstString cs = Mangled(class_name).GetDemangledName())
1016
185
    class_name = cs;
1017
1018
185
  stream.Printf("%s", class_name.AsCString("<unknown class>"));
1019
185
  return true;
1020
185
}
1021
1022
class ObjCClassSyntheticChildrenFrontEnd : public SyntheticChildrenFrontEnd {
1023
public:
1024
  ObjCClassSyntheticChildrenFrontEnd(lldb::ValueObjectSP valobj_sp)
1025
217
      : SyntheticChildrenFrontEnd(*valobj_sp) {}
1026
1027
217
  ~ObjCClassSyntheticChildrenFrontEnd() override = default;
1028
1029
24
  size_t CalculateNumChildren() override { return 0; }
1030
1031
0
  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
1032
0
    return lldb::ValueObjectSP();
1033
0
  }
1034
1035
197
  bool Update() override { return false; }
1036
1037
180
  bool MightHaveChildren() override { return false; }
1038
1039
0
  size_t GetIndexOfChildWithName(ConstString name) override {
1040
0
    return UINT32_MAX;
1041
0
  }
1042
};
1043
1044
SyntheticChildrenFrontEnd *
1045
lldb_private::formatters::ObjCClassSyntheticFrontEndCreator(
1046
217
    CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
1047
217
  return new ObjCClassSyntheticChildrenFrontEnd(valobj_sp);
1048
217
}
1049
1050
template <bool needs_at>
1051
bool lldb_private::formatters::NSDataSummaryProvider(
1052
78
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1053
78
  ProcessSP process_sp = valobj.GetProcessSP();
1054
78
  if (!process_sp)
1055
0
    return false;
1056
1057
78
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
1058
1059
78
  if (!runtime)
1060
0
    return false;
1061
1062
78
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
1063
78
      runtime->GetClassDescriptor(valobj));
1064
1065
78
  if (!descriptor || !descriptor->IsValid())
1066
0
    return false;
1067
1068
78
  bool is_64bit = (process_sp->GetAddressByteSize() == 8);
1069
78
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
1070
1071
78
  if (!valobj_addr)
1072
0
    return false;
1073
1074
78
  uint64_t value = 0;
1075
1076
78
  llvm::StringRef class_name = descriptor->GetClassName().GetCString();
1077
1078
78
  if (class_name.empty())
1079
0
    return false;
1080
1081
78
  bool isNSConcreteData = class_name == "NSConcreteData";
1082
78
  bool isNSConcreteMutableData = class_name == "NSConcreteMutableData";
1083
78
  bool isNSCFData = class_name == "__NSCFData";
1084
78
  if (isNSConcreteData || 
isNSConcreteMutableData66
||
isNSCFData42
) {
1085
60
    uint32_t offset;
1086
60
    if (isNSConcreteData)
1087
12
      offset = is_64bit ? 8 : 
40
;
1088
48
    else
1089
48
      offset = is_64bit ? 16 : 
80
;
1090
1091
60
    Status error;
1092
60
    value = process_sp->ReadUnsignedIntegerFromMemory(
1093
60
        valobj_addr + offset, is_64bit ? 8 : 
40
, 0, error);
1094
60
    if (error.Fail())
1095
0
      return false;
1096
60
  } else 
if (18
class_name == "_NSInlineData"18
) {
1097
18
    uint32_t offset = (is_64bit ? 8 : 
40
);
1098
18
    Status error;
1099
18
    value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, 2,
1100
18
                                                      0, error);
1101
18
    if (error.Fail())
1102
0
      return false;
1103
18
  } else 
if (0
class_name == "_NSZeroData"0
) {
1104
0
    value = 0;
1105
0
  } else
1106
0
    return false;
1107
1108
78
  stream.Printf("%s%" PRIu64 " byte%s%s", (needs_at ? 
"@\""24
:
""54
), value,
1109
78
                (value != 1 ? 
"s"72
:
""6
), (needs_at ?
"\""24
:
""54
));
1110
1111
78
  return true;
1112
78
}
bool lldb_private::formatters::NSDataSummaryProvider<true>(lldb_private::ValueObject&, lldb_private::Stream&, lldb_private::TypeSummaryOptions const&)
Line
Count
Source
1052
24
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1053
24
  ProcessSP process_sp = valobj.GetProcessSP();
1054
24
  if (!process_sp)
1055
0
    return false;
1056
1057
24
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
1058
1059
24
  if (!runtime)
1060
0
    return false;
1061
1062
24
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
1063
24
      runtime->GetClassDescriptor(valobj));
1064
1065
24
  if (!descriptor || !descriptor->IsValid())
1066
0
    return false;
1067
1068
24
  bool is_64bit = (process_sp->GetAddressByteSize() == 8);
1069
24
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
1070
1071
24
  if (!valobj_addr)
1072
0
    return false;
1073
1074
24
  uint64_t value = 0;
1075
1076
24
  llvm::StringRef class_name = descriptor->GetClassName().GetCString();
1077
1078
24
  if (class_name.empty())
1079
0
    return false;
1080
1081
24
  bool isNSConcreteData = class_name == "NSConcreteData";
1082
24
  bool isNSConcreteMutableData = class_name == "NSConcreteMutableData";
1083
24
  bool isNSCFData = class_name == "__NSCFData";
1084
24
  if (isNSConcreteData || isNSConcreteMutableData || isNSCFData) {
1085
24
    uint32_t offset;
1086
24
    if (isNSConcreteData)
1087
0
      offset = is_64bit ? 8 : 4;
1088
24
    else
1089
24
      offset = is_64bit ? 16 : 
80
;
1090
1091
24
    Status error;
1092
24
    value = process_sp->ReadUnsignedIntegerFromMemory(
1093
24
        valobj_addr + offset, is_64bit ? 8 : 
40
, 0, error);
1094
24
    if (error.Fail())
1095
0
      return false;
1096
24
  } else 
if (0
class_name == "_NSInlineData"0
) {
1097
0
    uint32_t offset = (is_64bit ? 8 : 4);
1098
0
    Status error;
1099
0
    value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, 2,
1100
0
                                                      0, error);
1101
0
    if (error.Fail())
1102
0
      return false;
1103
0
  } else if (class_name == "_NSZeroData") {
1104
0
    value = 0;
1105
0
  } else
1106
0
    return false;
1107
1108
24
  stream.Printf("%s%" PRIu64 " byte%s%s", (needs_at ? "@\"" : 
""0
), value,
1109
24
                (value != 1 ? "s" : 
""0
), (needs_at ? "\"" :
""0
));
1110
1111
24
  return true;
1112
24
}
bool lldb_private::formatters::NSDataSummaryProvider<false>(lldb_private::ValueObject&, lldb_private::Stream&, lldb_private::TypeSummaryOptions const&)
Line
Count
Source
1052
54
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1053
54
  ProcessSP process_sp = valobj.GetProcessSP();
1054
54
  if (!process_sp)
1055
0
    return false;
1056
1057
54
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
1058
1059
54
  if (!runtime)
1060
0
    return false;
1061
1062
54
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
1063
54
      runtime->GetClassDescriptor(valobj));
1064
1065
54
  if (!descriptor || !descriptor->IsValid())
1066
0
    return false;
1067
1068
54
  bool is_64bit = (process_sp->GetAddressByteSize() == 8);
1069
54
  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
1070
1071
54
  if (!valobj_addr)
1072
0
    return false;
1073
1074
54
  uint64_t value = 0;
1075
1076
54
  llvm::StringRef class_name = descriptor->GetClassName().GetCString();
1077
1078
54
  if (class_name.empty())
1079
0
    return false;
1080
1081
54
  bool isNSConcreteData = class_name == "NSConcreteData";
1082
54
  bool isNSConcreteMutableData = class_name == "NSConcreteMutableData";
1083
54
  bool isNSCFData = class_name == "__NSCFData";
1084
54
  if (isNSConcreteData || 
isNSConcreteMutableData42
||
isNSCFData18
) {
1085
36
    uint32_t offset;
1086
36
    if (isNSConcreteData)
1087
12
      offset = is_64bit ? 8 : 
40
;
1088
24
    else
1089
24
      offset = is_64bit ? 16 : 
80
;
1090
1091
36
    Status error;
1092
36
    value = process_sp->ReadUnsignedIntegerFromMemory(
1093
36
        valobj_addr + offset, is_64bit ? 8 : 
40
, 0, error);
1094
36
    if (error.Fail())
1095
0
      return false;
1096
36
  } else 
if (18
class_name == "_NSInlineData"18
) {
1097
18
    uint32_t offset = (is_64bit ? 8 : 
40
);
1098
18
    Status error;
1099
18
    value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + offset, 2,
1100
18
                                                      0, error);
1101
18
    if (error.Fail())
1102
0
      return false;
1103
18
  } else 
if (0
class_name == "_NSZeroData"0
) {
1104
0
    value = 0;
1105
0
  } else
1106
0
    return false;
1107
1108
54
  stream.Printf("%s%" PRIu64 " byte%s%s", (needs_at ? 
"@\""0
: ""), value,
1109
54
                (value != 1 ? 
"s"48
:
""6
), (needs_at ?
"\""0
: ""));
1110
1111
54
  return true;
1112
54
}
1113
1114
bool lldb_private::formatters::ObjCBOOLSummaryProvider(
1115
72
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1116
72
  const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo();
1117
1118
72
  ValueObjectSP real_guy_sp = valobj.GetSP();
1119
1120
72
  if (type_info & eTypeIsPointer) {
1121
9
    Status err;
1122
9
    real_guy_sp = valobj.Dereference(err);
1123
9
    if (err.Fail() || !real_guy_sp)
1124
0
      return false;
1125
63
  } else if (type_info & eTypeIsReference) {
1126
9
    real_guy_sp = valobj.GetChildAtIndex(0, true);
1127
9
    if (!real_guy_sp)
1128
0
      return false;
1129
9
  }
1130
72
  int8_t value = (real_guy_sp->GetValueAsSigned(0) & 0xFF);
1131
72
  switch (value) {
1132
36
  case 0:
1133
36
    stream.Printf("NO");
1134
36
    break;
1135
12
  case 1:
1136
12
    stream.Printf("YES");
1137
12
    break;
1138
24
  default:
1139
24
    stream.Printf("%d", value);
1140
24
    break;
1141
72
  }
1142
72
  return true;
1143
72
}
1144
1145
bool lldb_private::formatters::ObjCBooleanSummaryProvider(
1146
66
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1147
66
  lldb::addr_t valobj_ptr_value =
1148
66
      valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1149
66
  if (valobj_ptr_value == LLDB_INVALID_ADDRESS)
1150
0
    return false;
1151
1152
66
  ProcessSP process_sp(valobj.GetProcessSP());
1153
66
  if (!process_sp)
1154
0
    return false;
1155
1156
66
  if (AppleObjCRuntime *objc_runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
1157
66
          ObjCLanguageRuntime::Get(*process_sp))) {
1158
66
    lldb::addr_t cf_true = LLDB_INVALID_ADDRESS,
1159
66
                 cf_false = LLDB_INVALID_ADDRESS;
1160
66
    objc_runtime->GetValuesForGlobalCFBooleans(cf_true, cf_false);
1161
66
    if (valobj_ptr_value == cf_true) {
1162
33
      stream.PutCString("YES");
1163
33
      return true;
1164
33
    }
1165
33
    if (valobj_ptr_value == cf_false) {
1166
33
      stream.PutCString("NO");
1167
33
      return true;
1168
33
    }
1169
33
  }
1170
1171
0
  return false;
1172
66
}
1173
1174
template <bool is_sel_ptr>
1175
bool lldb_private::formatters::ObjCSELSummaryProvider(
1176
77
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1177
77
  lldb::ValueObjectSP valobj_sp;
1178
1179
77
  CompilerType charstar(valobj.GetCompilerType()
1180
77
                            .GetBasicTypeFromAST(eBasicTypeChar)
1181
77
                            .GetPointerType());
1182
1183
77
  if (!charstar)
1184
0
    return false;
1185
1186
77
  ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1187
1188
77
  if (is_sel_ptr) {
1189
0
    lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1190
0
    if (data_address == LLDB_INVALID_ADDRESS)
1191
0
      return false;
1192
0
    valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address,
1193
0
                                                          exe_ctx, charstar);
1194
77
  } else {
1195
77
    DataExtractor data;
1196
77
    Status error;
1197
77
    valobj.GetData(data, error);
1198
77
    if (error.Fail())
1199
0
      return false;
1200
77
    valobj_sp =
1201
77
        ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1202
77
  }
1203
1204
77
  if (!valobj_sp)
1205
0
    return false;
1206
1207
77
  stream.Printf("%s", valobj_sp->GetSummaryAsCString());
1208
77
  return true;
1209
77
}
Unexecuted instantiation: bool lldb_private::formatters::ObjCSELSummaryProvider<true>(lldb_private::ValueObject&, lldb_private::Stream&, lldb_private::TypeSummaryOptions const&)
bool lldb_private::formatters::ObjCSELSummaryProvider<false>(lldb_private::ValueObject&, lldb_private::Stream&, lldb_private::TypeSummaryOptions const&)
Line
Count
Source
1176
77
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
1177
77
  lldb::ValueObjectSP valobj_sp;
1178
1179
77
  CompilerType charstar(valobj.GetCompilerType()
1180
77
                            .GetBasicTypeFromAST(eBasicTypeChar)
1181
77
                            .GetPointerType());
1182
1183
77
  if (!charstar)
1184
0
    return false;
1185
1186
77
  ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
1187
1188
77
  if (is_sel_ptr) {
1189
0
    lldb::addr_t data_address = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1190
0
    if (data_address == LLDB_INVALID_ADDRESS)
1191
0
      return false;
1192
0
    valobj_sp = ValueObject::CreateValueObjectFromAddress("text", data_address,
1193
0
                                                          exe_ctx, charstar);
1194
77
  } else {
1195
77
    DataExtractor data;
1196
77
    Status error;
1197
77
    valobj.GetData(data, error);
1198
77
    if (error.Fail())
1199
0
      return false;
1200
77
    valobj_sp =
1201
77
        ValueObject::CreateValueObjectFromData("text", data, exe_ctx, charstar);
1202
77
  }
1203
1204
77
  if (!valobj_sp)
1205
0
    return false;
1206
1207
77
  stream.Printf("%s", valobj_sp->GetSummaryAsCString());
1208
77
  return true;
1209
77
}
1210
1211
// POSIX has an epoch on Jan-1-1970, but Cocoa prefers Jan-1-2001
1212
// this call gives the POSIX equivalent of the Cocoa epoch
1213
207
time_t lldb_private::formatters::GetOSXEpoch() {
1214
207
  static time_t epoch = 0;
1215
207
  if (!epoch) {
1216
4
#ifndef _WIN32
1217
4
    tzset();
1218
4
    tm tm_epoch;
1219
4
    tm_epoch.tm_sec = 0;
1220
4
    tm_epoch.tm_hour = 0;
1221
4
    tm_epoch.tm_min = 0;
1222
4
    tm_epoch.tm_mon = 0;
1223
4
    tm_epoch.tm_mday = 1;
1224
4
    tm_epoch.tm_year = 2001 - 1900;
1225
4
    tm_epoch.tm_isdst = -1;
1226
4
    tm_epoch.tm_gmtoff = 0;
1227
4
    tm_epoch.tm_zone = nullptr;
1228
4
    epoch = timegm(&tm_epoch);
1229
4
#endif
1230
4
  }
1231
207
  return epoch;
1232
207
}
1233
1234
template bool lldb_private::formatters::NSDataSummaryProvider<true>(
1235
    ValueObject &, Stream &, const TypeSummaryOptions &);
1236
1237
template bool lldb_private::formatters::NSDataSummaryProvider<false>(
1238
    ValueObject &, Stream &, const TypeSummaryOptions &);
1239
1240
template bool lldb_private::formatters::ObjCSELSummaryProvider<true>(
1241
    ValueObject &, Stream &, const TypeSummaryOptions &);
1242
1243
template bool lldb_private::formatters::ObjCSELSummaryProvider<false>(
1244
    ValueObject &, Stream &, const TypeSummaryOptions &);