reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
//===-- CF.cpp ----------------------------------------------------*- C++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "CF.h"

#include "lldb/Core/ValueObject.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Target/Language.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/Stream.h"

#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;

bool lldb_private::formatters::CFAbsoluteTimeSummaryProvider(
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
  time_t epoch = GetOSXEpoch();
  epoch = epoch + (time_t)valobj.GetValueAsUnsigned(0);
  tm *tm_date = localtime(&epoch);
  if (!tm_date)
    return false;
  std::string buffer(1024, 0);
  if (strftime(&buffer[0], 1023, "%Z", tm_date) == 0)
    return false;
  stream.Printf("%04d-%02d-%02d %02d:%02d:%02d %s", tm_date->tm_year + 1900,
                tm_date->tm_mon + 1, tm_date->tm_mday, tm_date->tm_hour,
                tm_date->tm_min, tm_date->tm_sec, buffer.c_str());
  return true;
}

bool lldb_private::formatters::CFBagSummaryProvider(
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
  static ConstString g_TypeHint("CFBag");

  ProcessSP process_sp = valobj.GetProcessSP();
  if (!process_sp)
    return false;

  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);

  if (!runtime)
    return false;

  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
      runtime->GetClassDescriptor(valobj));

  if (!descriptor.get() || !descriptor->IsValid())
    return false;

  uint32_t ptr_size = process_sp->GetAddressByteSize();

  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);

  if (!valobj_addr)
    return false;

  uint32_t count = 0;

  bool is_type_ok = false; // check to see if this is a CFBag we know about
  if (descriptor->IsCFType()) {
    ConstString type_name(valobj.GetTypeName());

    static ConstString g___CFBag("__CFBag");
    static ConstString g_conststruct__CFBag("const struct __CFBag");

    if (type_name == g___CFBag || type_name == g_conststruct__CFBag) {
      if (valobj.IsPointerType())
        is_type_ok = true;
    }
  }

  if (is_type_ok) {
    lldb::addr_t offset = 2 * ptr_size + 4 + valobj_addr;
    Status error;
    count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
    if (error.Fail())
      return false;
  } else
    return false;

  std::string prefix, suffix;
  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
                                            suffix)) {
      prefix.clear();
      suffix.clear();
    }
  }

  stream.Printf("%s\"%u value%s\"%s", prefix.c_str(), count,
                (count == 1 ? "" : "s"), suffix.c_str());
  return true;
}

bool lldb_private::formatters::CFBitVectorSummaryProvider(
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
  ProcessSP process_sp = valobj.GetProcessSP();
  if (!process_sp)
    return false;

  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);

  if (!runtime)
    return false;

  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
      runtime->GetClassDescriptor(valobj));

  if (!descriptor.get() || !descriptor->IsValid())
    return false;

  uint32_t ptr_size = process_sp->GetAddressByteSize();

  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);

  if (!valobj_addr)
    return false;

  uint32_t count = 0;

  bool is_type_ok = false; // check to see if this is a CFBag we know about
  if (descriptor->IsCFType()) {
    ConstString type_name(valobj.GetTypeName());
    if (type_name == "__CFMutableBitVector" || type_name == "__CFBitVector" ||
        type_name == "CFMutableBitVectorRef" || type_name == "CFBitVectorRef") {
      if (valobj.IsPointerType())
        is_type_ok = true;
    }
  }

  if (!is_type_ok)
    return false;

  Status error;
  count = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + 2 * ptr_size,
                                                    ptr_size, 0, error);
  if (error.Fail())
    return false;
  uint64_t num_bytes = count / 8 + ((count & 7) ? 1 : 0);
  addr_t data_ptr = process_sp->ReadPointerFromMemory(
      valobj_addr + 2 * ptr_size + 2 * ptr_size, error);
  if (error.Fail())
    return false;
  // make sure we do not try to read huge amounts of data
  if (num_bytes > 1024)
    num_bytes = 1024;
  DataBufferSP buffer_sp(new DataBufferHeap(num_bytes, 0));
  num_bytes =
      process_sp->ReadMemory(data_ptr, buffer_sp->GetBytes(), num_bytes, error);
  if (error.Fail() || num_bytes == 0)
    return false;
  uint8_t *bytes = buffer_sp->GetBytes();
  for (uint64_t byte_idx = 0; byte_idx < num_bytes - 1; byte_idx++) {
    uint8_t byte = bytes[byte_idx];
    bool bit0 = (byte & 1) == 1;
    bool bit1 = (byte & 2) == 2;
    bool bit2 = (byte & 4) == 4;
    bool bit3 = (byte & 8) == 8;
    bool bit4 = (byte & 16) == 16;
    bool bit5 = (byte & 32) == 32;
    bool bit6 = (byte & 64) == 64;
    bool bit7 = (byte & 128) == 128;
    stream.Printf("%c%c%c%c %c%c%c%c ", (bit7 ? '1' : '0'), (bit6 ? '1' : '0'),
                  (bit5 ? '1' : '0'), (bit4 ? '1' : '0'), (bit3 ? '1' : '0'),
                  (bit2 ? '1' : '0'), (bit1 ? '1' : '0'), (bit0 ? '1' : '0'));
    count -= 8;
  }
  {
    // print the last byte ensuring we do not print spurious bits
    uint8_t byte = bytes[num_bytes - 1];
    bool bit0 = (byte & 1) == 1;
    bool bit1 = (byte & 2) == 2;
    bool bit2 = (byte & 4) == 4;
    bool bit3 = (byte & 8) == 8;
    bool bit4 = (byte & 16) == 16;
    bool bit5 = (byte & 32) == 32;
    bool bit6 = (byte & 64) == 64;
    bool bit7 = (byte & 128) == 128;
    if (count) {
      stream.Printf("%c", bit7 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit6 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit5 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit4 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit3 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit2 ? '1' : '0');
      count -= 1;
    }
    if (count) {
      stream.Printf("%c", bit1 ? '1' : '0');
      count -= 1;
    }
    if (count)
      stream.Printf("%c", bit0 ? '1' : '0');
  }
  return true;
}

bool lldb_private::formatters::CFBinaryHeapSummaryProvider(
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
  static ConstString g_TypeHint("CFBinaryHeap");

  ProcessSP process_sp = valobj.GetProcessSP();
  if (!process_sp)
    return false;

  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);

  if (!runtime)
    return false;

  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
      runtime->GetClassDescriptor(valobj));

  if (!descriptor.get() || !descriptor->IsValid())
    return false;

  uint32_t ptr_size = process_sp->GetAddressByteSize();

  lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);

  if (!valobj_addr)
    return false;

  uint32_t count = 0;

  bool is_type_ok =
      false; // check to see if this is a CFBinaryHeap we know about
  if (descriptor->IsCFType()) {
    ConstString type_name(valobj.GetTypeName());

    static ConstString g___CFBinaryHeap("__CFBinaryHeap");
    static ConstString g_conststruct__CFBinaryHeap(
        "const struct __CFBinaryHeap");
    static ConstString g_CFBinaryHeapRef("CFBinaryHeapRef");

    if (type_name == g___CFBinaryHeap ||
        type_name == g_conststruct__CFBinaryHeap ||
        type_name == g_CFBinaryHeapRef) {
      if (valobj.IsPointerType())
        is_type_ok = true;
    }
  }

  if (is_type_ok) {
    lldb::addr_t offset = 2 * ptr_size + valobj_addr;
    Status error;
    count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
    if (error.Fail())
      return false;
  } else
    return false;

  std::string prefix, suffix;
  if (Language *language = Language::FindPlugin(options.GetLanguage())) {
    if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
                                            suffix)) {
      prefix.clear();
      suffix.clear();
    }
  }

  stream.Printf("%s\"%u item%s\"%s", prefix.c_str(), count,
                (count == 1 ? "" : "s"), suffix.c_str());
  return true;
}