NSDate.py
10 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
"""
LLDB AppKit formatters
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
"""
# example summary provider for NSDate
# the real summary is now C++ code built into LLDB
import lldb
import ctypes
import lldb.runtime.objc.objc_runtime
import lldb.formatters.metrics
import struct
import time
import datetime
import CFString
import lldb.formatters.Logger
statistics = lldb.formatters.metrics.Metrics()
statistics.add_metric('invalid_isa')
statistics.add_metric('invalid_pointer')
statistics.add_metric('unknown_class')
statistics.add_metric('code_notrun')
# Python promises to start counting time at midnight on Jan 1st on the epoch year
# hence, all we need to know is the epoch year
python_epoch = time.gmtime(0).tm_year
osx_epoch = datetime.date(2001, 1, 1).timetuple()
def mkgmtime(t):
logger = lldb.formatters.Logger.Logger()
return time.mktime(t) - time.timezone
osx_epoch = mkgmtime(osx_epoch)
def osx_to_python_time(osx):
logger = lldb.formatters.Logger.Logger()
if python_epoch <= 2001:
return osx + osx_epoch
else:
return osx - osx_epoch
# represent a struct_time as a string in the format used by Xcode
def xcode_format_time(X):
logger = lldb.formatters.Logger.Logger()
return time.strftime('%Y-%m-%d %H:%M:%S %Z', X)
# represent a count-since-epoch as a string in the format used by Xcode
def xcode_format_count(X):
logger = lldb.formatters.Logger.Logger()
return xcode_format_time(time.localtime(X))
# despite the similary to synthetic children providers, these classes are not
# trying to provide anything but the summary for NSDate, so they need not
# obey the interface specification for synthetic children providers
class NSTaggedDate_SummaryProvider:
def adjust_for_architecture(self):
pass
def __init__(self, valobj, info_bits, data, params):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.sys_params = params
self.update()
# NSDate is not using its info_bits for info like NSNumber is
# so we need to regroup info_bits and data
self.data = ((data << 8) | (info_bits << 4))
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
def value(self):
logger = lldb.formatters.Logger.Logger()
# the value of the date-time object is wrapped into the pointer value
# unfortunately, it is made as a time-delta after Jan 1 2001 midnight GMT
# while all Python knows about is the "epoch", which is a platform-dependent
# year (1970 of *nix) whose Jan 1 at midnight is taken as reference
value_double = struct.unpack('d', struct.pack('Q', self.data))[0]
if value_double == -63114076800.0:
return '0001-12-30 00:00:00 +0000'
return xcode_format_count(osx_to_python_time(value_double))
class NSUntaggedDate_SummaryProvider:
def adjust_for_architecture(self):
pass
def __init__(self, valobj, params):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.sys_params = params
if not (self.sys_params.types_cache.double):
self.sys_params.types_cache.double = self.valobj.GetType(
).GetBasicType(lldb.eBasicTypeDouble)
self.update()
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
def offset(self):
logger = lldb.formatters.Logger.Logger()
return self.sys_params.pointer_size
def value(self):
logger = lldb.formatters.Logger.Logger()
value = self.valobj.CreateChildAtOffset(
"value", self.offset(), self.sys_params.types_cache.double)
value_double = struct.unpack(
'd', struct.pack(
'Q', value.GetData().uint64[0]))[0]
if value_double == -63114076800.0:
return '0001-12-30 00:00:00 +0000'
return xcode_format_count(osx_to_python_time(value_double))
class NSCalendarDate_SummaryProvider:
def adjust_for_architecture(self):
pass
def __init__(self, valobj, params):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.sys_params = params
if not (self.sys_params.types_cache.double):
self.sys_params.types_cache.double = self.valobj.GetType(
).GetBasicType(lldb.eBasicTypeDouble)
self.update()
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
def offset(self):
logger = lldb.formatters.Logger.Logger()
return 2 * self.sys_params.pointer_size
def value(self):
logger = lldb.formatters.Logger.Logger()
value = self.valobj.CreateChildAtOffset(
"value", self.offset(), self.sys_params.types_cache.double)
value_double = struct.unpack(
'd', struct.pack(
'Q', value.GetData().uint64[0]))[0]
return xcode_format_count(osx_to_python_time(value_double))
class NSTimeZoneClass_SummaryProvider:
def adjust_for_architecture(self):
pass
def __init__(self, valobj, params):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.sys_params = params
if not (self.sys_params.types_cache.voidptr):
self.sys_params.types_cache.voidptr = self.valobj.GetType(
).GetBasicType(lldb.eBasicTypeVoid).GetPointerType()
self.update()
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
def offset(self):
logger = lldb.formatters.Logger.Logger()
return self.sys_params.pointer_size
def timezone(self):
logger = lldb.formatters.Logger.Logger()
tz_string = self.valobj.CreateChildAtOffset(
"tz_name", self.offset(), self.sys_params.types_cache.voidptr)
return CFString.CFString_SummaryProvider(tz_string, None)
class NSUnknownDate_SummaryProvider:
def adjust_for_architecture(self):
pass
def __init__(self, valobj):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.update()
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
def value(self):
logger = lldb.formatters.Logger.Logger()
stream = lldb.SBStream()
self.valobj.GetExpressionPath(stream)
expr = "(NSString*)[" + stream.GetData() + " description]"
num_children_vo = self.valobj.CreateValueFromExpression("str", expr)
if num_children_vo.IsValid():
return num_children_vo.GetSummary()
return '<variable is not NSDate>'
def GetSummary_Impl(valobj):
logger = lldb.formatters.Logger.Logger()
global statistics
class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(
valobj, statistics)
if wrapper:
return wrapper
name_string = class_data.class_name()
logger >> "class name is: " + str(name_string)
if name_string == 'NSDate' or name_string == '__NSDate' or name_string == '__NSTaggedDate':
if class_data.is_tagged():
wrapper = NSTaggedDate_SummaryProvider(
valobj, class_data.info_bits(), class_data.value(), class_data.sys_params)
statistics.metric_hit('code_notrun', valobj)
else:
wrapper = NSUntaggedDate_SummaryProvider(
valobj, class_data.sys_params)
statistics.metric_hit('code_notrun', valobj)
elif name_string == 'NSCalendarDate':
wrapper = NSCalendarDate_SummaryProvider(valobj, class_data.sys_params)
statistics.metric_hit('code_notrun', valobj)
elif name_string == '__NSTimeZone':
wrapper = NSTimeZoneClass_SummaryProvider(
valobj, class_data.sys_params)
statistics.metric_hit('code_notrun', valobj)
else:
wrapper = NSUnknownDate_SummaryProvider(valobj)
statistics.metric_hit(
'unknown_class',
valobj.GetName() +
" seen as " +
name_string)
return wrapper
def NSDate_SummaryProvider(valobj, dict):
logger = lldb.formatters.Logger.Logger()
provider = GetSummary_Impl(valobj)
if provider is not None:
if isinstance(
provider,
lldb.runtime.objc.objc_runtime.SpecialSituation_Description):
return provider.message()
try:
summary = provider.value()
except:
summary = None
if summary is None:
summary = '<variable is not NSDate>'
return str(summary)
return 'Summary Unavailable'
def NSTimeZone_SummaryProvider(valobj, dict):
logger = lldb.formatters.Logger.Logger()
provider = GetSummary_Impl(valobj)
if provider is not None:
if isinstance(
provider,
lldb.runtime.objc.objc_runtime.SpecialSituation_Description):
return provider.message()
try:
summary = provider.timezone()
except:
summary = None
logger >> "got summary " + str(summary)
if summary is None:
summary = '<variable is not NSTimeZone>'
return str(summary)
return 'Summary Unavailable'
def CFAbsoluteTime_SummaryProvider(valobj, dict):
logger = lldb.formatters.Logger.Logger()
try:
value_double = struct.unpack(
'd', struct.pack(
'Q', valobj.GetData().uint64[0]))[0]
return xcode_format_count(osx_to_python_time(value_double))
except:
return 'Summary Unavailable'
def __lldb_init_module(debugger, dict):
debugger.HandleCommand(
"type summary add -F NSDate.NSDate_SummaryProvider NSDate")
debugger.HandleCommand(
"type summary add -F NSDate.CFAbsoluteTime_SummaryProvider CFAbsoluteTime")
debugger.HandleCommand(
"type summary add -F NSDate.NSTimeZone_SummaryProvider NSTimeZone CFTimeZoneRef")