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