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 | # synthetic children provider for NSArray |
| 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 | # much less functional than the other two cases below |
| 21 | # just runs code to get to the count and then returns |
| 22 | # no children |
| 23 | class NSArrayKVC_SynthProvider: |
| 24 | |
| 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, dict, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 29 | self.valobj = valobj; |
| 30 | self.update() |
| 31 | |
| 32 | def update(self): |
| 33 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 34 | |
| 35 | def num_children(self): |
| 36 | stream = lldb.SBStream() |
| 37 | self.valobj.GetExpressionPath(stream) |
| 38 | num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " count]"); |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 39 | if num_children_vo.IsValid(): |
| 40 | return num_children_vo.GetValueAsUnsigned(0) |
| 41 | return "<variable is not NSArray>" |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 42 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 43 | # much less functional than the other two cases below |
| 44 | # just runs code to get to the count and then returns |
| 45 | # no children |
| 46 | class NSArrayCF_SynthProvider: |
| 47 | |
| 48 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 49 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 50 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 51 | def __init__(self, valobj, dict, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 52 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 53 | self.sys_params = params |
| 54 | if not (self.sys_params.types_cache.ulong): |
| 55 | self.sys_params.types_cache.ulong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 56 | self.update() |
| 57 | |
| 58 | def update(self): |
| 59 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 60 | |
| 61 | def num_children(self): |
| 62 | num_children_vo = self.valobj.CreateChildAtOffset("count", |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 63 | self.sys_params.cfruntime_size, |
| 64 | self.sys_params.types_cache.ulong) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 65 | return num_children_vo.GetValueAsUnsigned(0) |
| 66 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 67 | class NSArrayI_SynthProvider: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 68 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 69 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 70 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 71 | def __init__(self, valobj, dict, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 72 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 73 | self.sys_params = params |
| 74 | if not(self.sys_params.types_cache.long): |
| 75 | self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 76 | self.update() |
| 77 | |
| 78 | def update(self): |
| 79 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 80 | |
| 81 | # skip the isa pointer and get at the size |
| 82 | def num_children(self): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 83 | count = self.valobj.CreateChildAtOffset("count", |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 84 | self.sys_params.pointer_size, |
| 85 | self.sys_params.types_cache.long); |
| 86 | return count.GetValueAsUnsigned(0) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 87 | |
| 88 | class NSArrayM_SynthProvider: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 89 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 90 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 91 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 92 | def __init__(self, valobj, dict, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 93 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 94 | self.sys_params = params |
| 95 | if not(self.sys_params.types_cache.long): |
| 96 | self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong) |
| 97 | self.update() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 98 | |
| 99 | def update(self): |
| 100 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 101 | |
| 102 | # skip the isa pointer and get at the size |
| 103 | def num_children(self): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 104 | count = self.valobj.CreateChildAtOffset("count", |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 105 | self.sys_params.pointer_size, |
| 106 | self.sys_params.types_cache.long); |
| 107 | return count.GetValueAsUnsigned(0) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 108 | |
| 109 | # this is the actual synth provider, but is just a wrapper that checks |
| 110 | # whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an |
| 111 | # appropriate backend layer to do the computations |
| 112 | class NSArray_SynthProvider: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 113 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 114 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 115 | |
| 116 | def __init__(self, valobj, dict): |
| 117 | self.valobj = valobj; |
| 118 | self.adjust_for_architecture() |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 119 | self.error = False |
| 120 | self.wrapper = self.make_wrapper() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 121 | self.invalid = (self.wrapper == None) |
| 122 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 123 | def num_children(self): |
| 124 | if self.wrapper == None: |
| 125 | return 0; |
| 126 | return self.wrapper.num_children() |
| 127 | |
| 128 | def update(self): |
| 129 | if self.wrapper == None: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 130 | return |
| 131 | self.wrapper.update() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 132 | |
| 133 | # this code acts as our defense against NULL and unitialized |
| 134 | # NSArray pointers, which makes it much longer than it would be otherwise |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 135 | def make_wrapper(self): |
| 136 | if self.valobj.GetValueAsUnsigned() == 0: |
| 137 | self.error = True |
| 138 | return objc_runtime.InvalidPointer_Description(True) |
| 139 | else: |
| 140 | global statistics |
| 141 | class_data,wrapper = objc_runtime.Utilities.prepare_class_detection(self.valobj,statistics) |
| 142 | if wrapper: |
| 143 | self.error = True |
| 144 | return wrapper |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 145 | |
| 146 | name_string = class_data.class_name() |
| 147 | if name_string == '__NSArrayI': |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 148 | wrapper = NSArrayI_SynthProvider(self.valobj, dict, class_data.sys_params) |
| 149 | statistics.metric_hit('code_notrun',self.valobj) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 150 | elif name_string == '__NSArrayM': |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 151 | wrapper = NSArrayM_SynthProvider(self.valobj, dict, class_data.sys_params) |
| 152 | statistics.metric_hit('code_notrun',self.valobj) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 153 | elif name_string == '__NSCFArray': |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 154 | wrapper = NSArrayCF_SynthProvider(self.valobj, dict, class_data.sys_params) |
| 155 | statistics.metric_hit('code_notrun',self.valobj) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 156 | else: |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 157 | wrapper = NSArrayKVC_SynthProvider(self.valobj, dict, class_data.sys_params) |
| 158 | statistics.metric_hit('unknown_class',str(self.valobj) + " seen as " + name_string) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 159 | return wrapper; |
| 160 | |
| 161 | def CFArray_SummaryProvider (valobj,dict): |
| 162 | provider = NSArray_SynthProvider(valobj,dict); |
| 163 | if provider.invalid == False: |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame^] | 164 | if provider.error == True: |
| 165 | return provider.wrapper.message() |
| 166 | try: |
| 167 | summary = int(provider.num_children()); |
| 168 | except: |
| 169 | summary = None |
| 170 | if summary == None: |
| 171 | summary = '<variable is not NSArray>' |
| 172 | elif isinstance(summary,basestring): |
| 173 | pass |
| 174 | else: |
| 175 | # we format it like it were a CFString to make it look the same as the summary from Xcode |
| 176 | summary = '@"' + str(summary) + (" objects" if summary != 1 else " object") + '"' |
| 177 | return summary |
| 178 | return 'Summary Unavailable' |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 179 | |
| 180 | def __lldb_init_module(debugger,dict): |
| 181 | debugger.HandleCommand("type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef") |