blob: 1403a122eb6df6bebfc3fae124c7a2f6694225f0 [file] [log] [blame]
Enrico Granataeb4a4792012-02-23 23:10:27 +00001# summary provider for CFBag
2import lldb
3import ctypes
4import objc_runtime
5import metrics
6
7statistics = metrics.Metrics()
8statistics.add_metric('invalid_isa')
9statistics.add_metric('invalid_pointer')
10statistics.add_metric('unknown_class')
11statistics.add_metric('code_notrun')
12
13# despite the similary to synthetic children providers, these classes are not
14# trying to provide anything but the length for an CFBag, so they need not
15# obey the interface specification for synthetic children providers
16class CFBagRef_SummaryProvider:
17 def adjust_for_architecture(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +000018 self.is_64_bit = (self.valobj.GetTarget().GetProcess().GetAddressByteSize() == 8)
Enrico Granataeb4a4792012-02-23 23:10:27 +000019 self.is_little = (self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle)
20 self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize()
21
22 def __init__(self, valobj):
23 self.valobj = valobj;
24 self.update();
25
26 def update(self):
27 self.adjust_for_architecture();
28 self.id_type = self.valobj.GetType().GetBasicType(lldb.eBasicTypeObjCID)
Enrico Granata7bc0ec32012-02-29 03:28:49 +000029 if self.is_64_bit:
Enrico Granataeb4a4792012-02-23 23:10:27 +000030 self.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong)
31 else:
32 self.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt)
33
34 # 12 bytes on i386
35 # 20 bytes on x64
36 # most probably 2 pointers and 4 bytes of data
37 def offset(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +000038 if self.is_64_bit:
Enrico Granataeb4a4792012-02-23 23:10:27 +000039 return 20
40 else:
41 return 12
42
43 def length(self):
44 size = self.valobj.CreateChildAtOffset("count",
45 self.offset(),
46 self.NSUInteger)
47 return size.GetValueAsUnsigned(0)
48
49
50class CFBagUnknown_SummaryProvider:
51 def adjust_for_architecture(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +000052 self.is_64_bit = (self.valobj.GetTarget().GetProcess().GetAddressByteSize() == 8)
Enrico Granataeb4a4792012-02-23 23:10:27 +000053 self.is_little = (self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle)
54 self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize()
55
56 def __init__(self, valobj):
57 self.valobj = valobj;
58 self.update()
59
60 def update(self):
61 self.adjust_for_architecture();
62 self.id_type = self.valobj.GetType().GetBasicType(lldb.eBasicTypeObjCID)
63
64 def length(self):
65 stream = lldb.SBStream()
66 self.valobj.GetExpressionPath(stream)
67 num_children_vo = self.valobj.CreateValueFromExpression("count","(int)CFBagGetCount(" + stream.GetData() + " )");
68 return num_children_vo.GetValueAsUnsigned(0)
69
70
71def GetSummary_Impl(valobj):
72 global statistics
73 class_data = objc_runtime.ObjCRuntime(valobj)
74 if class_data.is_valid() == False:
75 statistics.metric_hit('invalid_pointer',valobj)
76 wrapper = None
77 return
78 class_data = class_data.read_class_data()
79 if class_data.is_valid() == False:
80 statistics.metric_hit('invalid_isa',valobj)
81 wrapper = None
82 return
83 if class_data.is_kvo():
84 class_data = class_data.get_superclass()
85 if class_data.is_valid() == False:
86 statistics.metric_hit('invalid_isa',valobj)
87 wrapper = None
88 return
89
90 name_string = class_data.class_name()
91 actual_name = name_string
92 if name_string == '__NSCFType':
93 # CFBag does not expose an actual NSWrapper type, so we have to check that this is
94 # an NSCFType and then check we are a pointer-to __CFBag
95 valobj_type = valobj.GetType()
96 if valobj_type.IsValid() and valobj_type.IsPointerType():
97 pointee_type = valobj_type.GetPointeeType()
98 actual_name = pointee_type.GetName()
99 if actual_name == '__CFBag' or \
100 actual_name == 'const struct __CFBag':
101 wrapper = CFBagRef_SummaryProvider(valobj)
102 statistics.metric_hit('code_notrun',valobj)
103 return wrapper
104 wrapper = CFBagUnknown_SummaryProvider(valobj)
105 statistics.metric_hit('unknown_class',str(valobj) + " seen as " + actual_name)
106 return wrapper;
107
108def CFBag_SummaryProvider (valobj,dict):
109 provider = GetSummary_Impl(valobj);
110 if provider != None:
111 try:
112 summary = provider.length();
113 except:
114 summary = None
115 # for some reason, one needs to clear some bits for the count
116 # to be correct when using CF(Mutable)BagRef on x64
117 # the bit mask was derived through experimentation
118 # (if counts start looking weird, then most probably
119 # the mask needs to be changed)
120 if summary == None:
121 summary = 'no valid set here'
122 else:
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000123 if provider.is_64_bit:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000124 summary = summary & ~0x1fff000000000000
125 if summary == 1:
126 return '1 item'
127 return str(summary) + " items"
128 return ''
129
130def __lldb_init_module(debugger,dict):
131 debugger.HandleCommand("type summary add -F CFBag.CFBag_SummaryProvider CFBagRef CFMutableBagRef")