Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 1 | # summary provider for NSDictionary |
| 2 | import lldb |
| 3 | import ctypes |
| 4 | import objc_runtime |
| 5 | import metrics |
| 6 | |
| 7 | statistics = metrics.Metrics() |
| 8 | statistics.add_metric('invalid_isa') |
| 9 | statistics.add_metric('invalid_pointer') |
| 10 | statistics.add_metric('unknown_class') |
| 11 | statistics.add_metric('code_notrun') |
| 12 | |
| 13 | # despite the similary to synthetic children providers, these classes are not |
| 14 | # trying to provide anything but the count for an NSDictionary, so they need not |
| 15 | # obey the interface specification for synthetic children providers |
| 16 | class NSCFDictionary_SummaryProvider: |
| 17 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 18 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 19 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 20 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 21 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 22 | self.sys_params = params |
| 23 | if not(self.sys_params.types_cache.NSUInteger): |
| 24 | if self.sys_params.is_64_bit: |
| 25 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 26 | else: |
| 27 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 28 | self.update(); |
| 29 | |
| 30 | def update(self): |
| 31 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 32 | |
| 33 | # empirically determined on both 32 and 64bit desktop Mac OS X |
| 34 | # probably boils down to 2 pointers and 4 bytes of data, but |
| 35 | # the description of __CFDictionary is not readily available so most |
| 36 | # of this is guesswork, plain and simple |
| 37 | def offset(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 38 | if self.sys_params.is_64_bit: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 39 | return 20 |
| 40 | else: |
| 41 | return 12 |
| 42 | |
| 43 | def num_children(self): |
| 44 | num_children_vo = self.valobj.CreateChildAtOffset("count", |
| 45 | self.offset(), |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 46 | self.sys_params.types_cache.NSUInteger) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 47 | return num_children_vo.GetValueAsUnsigned(0) |
| 48 | |
| 49 | |
| 50 | class NSDictionaryI_SummaryProvider: |
| 51 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 52 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 53 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 54 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 55 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 56 | self.sys_params = params |
| 57 | if not(self.sys_params.types_cache.NSUInteger): |
| 58 | if self.sys_params.is_64_bit: |
| 59 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 60 | else: |
| 61 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 62 | self.update(); |
| 63 | |
| 64 | def update(self): |
| 65 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 66 | |
| 67 | # we just need to skip the ISA and the count immediately follows |
| 68 | def offset(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 69 | return self.sys_params.pointer_size |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 70 | |
| 71 | def num_children(self): |
| 72 | num_children_vo = self.valobj.CreateChildAtOffset("count", |
| 73 | self.offset(), |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 74 | self.sys_params.types_cache.NSUInteger) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 75 | value = num_children_vo.GetValueAsUnsigned(0) |
| 76 | if value != None: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 77 | # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity |
| 78 | # not sure if it is a bug or some weird sort of feature, but masking that out |
| 79 | # gets the count right |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 80 | if self.sys_params.is_64_bit: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 81 | value = value & ~0xFC00000000000000 |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 82 | else: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 83 | value = value & ~0xFC000000 |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 84 | return value |
| 85 | |
| 86 | class NSDictionaryM_SummaryProvider: |
| 87 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 88 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 89 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 90 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 91 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 92 | self.sys_params = params |
| 93 | if not(self.sys_params.types_cache.NSUInteger): |
| 94 | if self.sys_params.is_64_bit: |
| 95 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 96 | else: |
| 97 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 98 | self.update(); |
| 99 | |
| 100 | def update(self): |
| 101 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 102 | |
| 103 | # we just need to skip the ISA and the count immediately follows |
| 104 | def offset(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 105 | return self.sys_params.pointer_size |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 106 | |
| 107 | def num_children(self): |
| 108 | num_children_vo = self.valobj.CreateChildAtOffset("count", |
| 109 | self.offset(), |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 110 | self.sys_params.types_cache.NSUInteger) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 111 | value = num_children_vo.GetValueAsUnsigned(0) |
| 112 | if value != None: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 113 | # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity |
| 114 | # not sure if it is a bug or some weird sort of feature, but masking that out |
| 115 | # gets the count right |
| 116 | if self.sys_params.is_64_bit: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 117 | value = value & ~0xFC00000000000000 |
| 118 | else: |
| 119 | value = value & ~0xFC000000 |
| 120 | return value |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 121 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 122 | class NSDictionaryUnknown_SummaryProvider: |
| 123 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 124 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 125 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 126 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 127 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 128 | self.sys_params = params |
| 129 | self.update(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 130 | |
| 131 | def update(self): |
| 132 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 133 | |
| 134 | def num_children(self): |
| 135 | stream = lldb.SBStream() |
| 136 | self.valobj.GetExpressionPath(stream) |
| 137 | num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " count]"); |
| 138 | return num_children_vo.GetValueAsUnsigned(0) |
| 139 | |
| 140 | |
| 141 | def GetSummary_Impl(valobj): |
| 142 | global statistics |
| 143 | class_data = objc_runtime.ObjCRuntime(valobj) |
| 144 | if class_data.is_valid() == False: |
| 145 | statistics.metric_hit('invalid_pointer',valobj) |
| 146 | wrapper = None |
| 147 | return |
| 148 | class_data = class_data.read_class_data() |
| 149 | if class_data.is_valid() == False: |
| 150 | statistics.metric_hit('invalid_isa',valobj) |
| 151 | wrapper = None |
| 152 | return |
| 153 | if class_data.is_kvo(): |
| 154 | class_data = class_data.get_superclass() |
| 155 | if class_data.is_valid() == False: |
| 156 | statistics.metric_hit('invalid_isa',valobj) |
| 157 | wrapper = None |
| 158 | return |
| 159 | |
| 160 | name_string = class_data.class_name() |
| 161 | if name_string == '__NSCFDictionary': |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 162 | wrapper = NSCFDictionary_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 163 | statistics.metric_hit('code_notrun',valobj) |
| 164 | elif name_string == '__NSDictionaryI': |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 165 | wrapper = NSDictionaryI_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 166 | statistics.metric_hit('code_notrun',valobj) |
| 167 | elif name_string == '__NSDictionaryM': |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 168 | wrapper = NSDictionaryM_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 169 | statistics.metric_hit('code_notrun',valobj) |
| 170 | else: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 171 | wrapper = NSDictionaryUnknown_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 172 | statistics.metric_hit('unknown_class',str(valobj) + " seen as " + name_string) |
| 173 | return wrapper; |
| 174 | |
| 175 | def CFDictionary_SummaryProvider (valobj,dict): |
| 176 | provider = GetSummary_Impl(valobj); |
| 177 | if provider != None: |
| 178 | try: |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 179 | summary = provider.num_children(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 180 | except: |
| 181 | summary = None |
| 182 | if summary == None: |
| 183 | summary = 'no valid dictionary here' |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 184 | return str(summary) + (" key/value pairs" if summary > 1 else " key/value pair") |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 185 | return '' |
| 186 | |
| 187 | def CFDictionary_SummaryProvider2 (valobj,dict): |
| 188 | provider = GetSummary_Impl(valobj); |
| 189 | if provider != None: |
| 190 | try: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 191 | summary = provider.num_children(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 192 | except: |
| 193 | summary = None |
| 194 | if summary == None: |
| 195 | summary = 'no valid dictionary here' |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 196 | else: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 197 | # needed on OSX Mountain Lion |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 198 | if provider.sys_params.is_64_bit: |
| 199 | summary = summary & ~0x0f1f000000000000 |
Enrico Granata | 8c69c96 | 2012-03-13 00:25:59 +0000 | [diff] [blame] | 200 | summary = '@"' + str(summary) + (' entries"' if summary > 1 else ' entry"') |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 201 | return summary |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 202 | return '' |
| 203 | |
| 204 | def __lldb_init_module(debugger,dict): |
| 205 | debugger.HandleCommand("type summary add -F CFDictionary.CFDictionary_SummaryProvider NSDictionary") |
| 206 | debugger.HandleCommand("type summary add -F CFDictionary.CFDictionary_SummaryProvider2 CFDictionaryRef CFMutableDictionaryRef") |