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