1 //===-- CF.cpp ----------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Host/Endian.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
22 #include "lldb/Target/Target.h"
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
29 lldb_private::formatters::CFAbsoluteTimeSummaryProvider (ValueObject& valobj, Stream& stream)
31 time_t epoch = GetOSXEpoch();
32 epoch = epoch + (time_t)valobj.GetValueAsUnsigned(0);
33 tm *tm_date = localtime(&epoch);
36 std::string buffer(1024,0);
37 if (strftime (&buffer[0], 1023, "%Z", tm_date) == 0)
39 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());
44 lldb_private::formatters::CFBagSummaryProvider (ValueObject& valobj, Stream& stream)
46 ProcessSP process_sp = valobj.GetProcessSP();
50 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
55 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
57 if (!descriptor.get() || !descriptor->IsValid())
60 uint32_t ptr_size = process_sp->GetAddressByteSize();
62 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
69 bool is_type_ok = false; // check to see if this is a CFBag we know about
70 if (descriptor->IsCFType())
72 ConstString type_name(valobj.GetTypeName());
73 if (type_name == ConstString("__CFBag") || type_name == ConstString("const struct __CFBag"))
75 if (valobj.IsPointerType())
80 if (is_type_ok == false)
82 StackFrameSP frame_sp(valobj.GetFrameSP());
85 ValueObjectSP count_sp;
87 expr.Printf("(int)CFBagGetCount((void*)0x%" PRIx64 ")",valobj.GetPointerValue());
88 if (process_sp->GetTarget().EvaluateExpression(expr.GetData(), frame_sp.get(), count_sp) != eExecutionCompleted)
92 count = count_sp->GetValueAsUnsigned(0);
96 uint32_t offset = 2*ptr_size+4 + valobj_addr;
98 count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
102 stream.Printf("@\"%u value%s\"",
103 count,(count == 1 ? "" : "s"));
108 lldb_private::formatters::CFBitVectorSummaryProvider (ValueObject& valobj, Stream& stream)
110 ProcessSP process_sp = valobj.GetProcessSP();
114 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
119 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
121 if (!descriptor.get() || !descriptor->IsValid())
124 uint32_t ptr_size = process_sp->GetAddressByteSize();
126 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
133 bool is_type_ok = false; // check to see if this is a CFBag we know about
134 if (descriptor->IsCFType())
136 ConstString type_name(valobj.GetTypeName());
137 if (type_name == ConstString("__CFMutableBitVector") || type_name == ConstString("__CFBitVector") || type_name == ConstString("CFMutableBitVectorRef") || type_name == ConstString("CFBitVectorRef"))
139 if (valobj.IsPointerType())
144 if (is_type_ok == false)
148 count = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr+2*ptr_size, ptr_size, 0, error);
151 uint64_t num_bytes = count / 8 + ((count & 7) ? 1 : 0);
152 addr_t data_ptr = process_sp->ReadPointerFromMemory(valobj_addr+2*ptr_size+2*ptr_size, error);
155 // make sure we do not try to read huge amounts of data
156 if (num_bytes > 1024)
158 DataBufferSP buffer_sp(new DataBufferHeap(num_bytes,0));
159 num_bytes = process_sp->ReadMemory(data_ptr, buffer_sp->GetBytes(), num_bytes, error);
160 if (error.Fail() || num_bytes == 0)
162 uint8_t *bytes = buffer_sp->GetBytes();
163 for (int byte_idx = 0; byte_idx < num_bytes-1; byte_idx++)
165 uint8_t byte = bytes[byte_idx];
166 bool bit0 = (byte & 1) == 1;
167 bool bit1 = (byte & 2) == 2;
168 bool bit2 = (byte & 4) == 4;
169 bool bit3 = (byte & 8) == 8;
170 bool bit4 = (byte & 16) == 16;
171 bool bit5 = (byte & 32) == 32;
172 bool bit6 = (byte & 64) == 64;
173 bool bit7 = (byte & 128) == 128;
174 stream.Printf("%c%c%c%c %c%c%c%c ",
186 // print the last byte ensuring we do not print spurious bits
187 uint8_t byte = bytes[num_bytes-1];
188 bool bit0 = (byte & 1) == 1;
189 bool bit1 = (byte & 2) == 2;
190 bool bit2 = (byte & 4) == 4;
191 bool bit3 = (byte & 8) == 8;
192 bool bit4 = (byte & 16) == 16;
193 bool bit5 = (byte & 32) == 32;
194 bool bit6 = (byte & 64) == 64;
195 bool bit7 = (byte & 128) == 128;
198 stream.Printf("%c",bit7 ? '1' : '0');
203 stream.Printf("%c",bit6 ? '1' : '0');
208 stream.Printf("%c",bit5 ? '1' : '0');
213 stream.Printf("%c",bit4 ? '1' : '0');
218 stream.Printf("%c",bit3 ? '1' : '0');
223 stream.Printf("%c",bit2 ? '1' : '0');
228 stream.Printf("%c",bit1 ? '1' : '0');
232 stream.Printf("%c",bit0 ? '1' : '0');
238 lldb_private::formatters::CFBinaryHeapSummaryProvider (ValueObject& valobj, Stream& stream)
240 ProcessSP process_sp = valobj.GetProcessSP();
244 ObjCLanguageRuntime* runtime = (ObjCLanguageRuntime*)process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
249 ObjCLanguageRuntime::ClassDescriptorSP descriptor(runtime->GetClassDescriptor(valobj));
251 if (!descriptor.get() || !descriptor->IsValid())
254 uint32_t ptr_size = process_sp->GetAddressByteSize();
256 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
263 bool is_type_ok = false; // check to see if this is a CFBinaryHeap we know about
264 if (descriptor->IsCFType())
266 ConstString type_name(valobj.GetTypeName());
267 if (type_name == ConstString("__CFBinaryHeap") || type_name == ConstString("const struct __CFBinaryHeap"))
269 if (valobj.IsPointerType())
274 if (is_type_ok == false)
276 StackFrameSP frame_sp(valobj.GetFrameSP());
279 ValueObjectSP count_sp;
281 expr.Printf("(int)CFBinaryHeapGetCount((void*)0x%" PRIx64 ")",valobj.GetPointerValue());
282 if (process_sp->GetTarget().EvaluateExpression(expr.GetData(), frame_sp.get(), count_sp) != eExecutionCompleted)
286 count = count_sp->GetValueAsUnsigned(0);
290 uint32_t offset = 2*ptr_size;
292 count = process_sp->ReadUnsignedIntegerFromMemory(offset, 4, 0, error);
296 stream.Printf("@\"%u item%s\"",
297 count,(count == 1 ? "" : "s"));