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 CFBag |
| 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 length for an CFBag, so they need not |
| 22 | # obey the interface specification for synthetic children providers |
| 23 | class CFBagRef_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 | # 12 bytes on i386 |
| 41 | # 20 bytes on x64 |
| 42 | # most probably 2 pointers and 4 bytes of data |
| 43 | def offset(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 44 | if self.sys_params.is_64_bit: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 45 | return 20 |
| 46 | else: |
| 47 | return 12 |
| 48 | |
| 49 | def length(self): |
| 50 | size = self.valobj.CreateChildAtOffset("count", |
| 51 | self.offset(), |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 52 | self.sys_params.types_cache.NSUInteger) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 53 | return size.GetValueAsUnsigned(0) |
| 54 | |
| 55 | |
| 56 | class CFBagUnknown_SummaryProvider: |
| 57 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 58 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 59 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 60 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 61 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 62 | self.sys_params = params |
| 63 | self.update(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 64 | |
| 65 | def update(self): |
| 66 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 67 | |
| 68 | def length(self): |
| 69 | stream = lldb.SBStream() |
| 70 | self.valobj.GetExpressionPath(stream) |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 71 | num_children_vo = self.valobj.CreateValueFromExpression("count","(int)CFBagGetCount(" + stream.GetData() + " )") |
| 72 | if num_children_vo.IsValid(): |
| 73 | return num_children_vo.GetValueAsUnsigned(0) |
| 74 | return "<variable is not CFBag>" |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def GetSummary_Impl(valobj): |
| 78 | global statistics |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 79 | class_data,wrapper = objc_runtime.Utilities.prepare_class_detection(valobj,statistics) |
| 80 | if wrapper: |
| 81 | return wrapper |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 82 | |
| 83 | name_string = class_data.class_name() |
| 84 | actual_name = name_string |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 85 | if class_data.is_cftype(): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 86 | # CFBag does not expose an actual NSWrapper type, so we have to check that this is |
| 87 | # an NSCFType and then check we are a pointer-to __CFBag |
| 88 | valobj_type = valobj.GetType() |
| 89 | if valobj_type.IsValid() and valobj_type.IsPointerType(): |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 90 | valobj_type = valobj_type.GetPointeeType() |
| 91 | if valobj_type.IsValid(): |
| 92 | actual_name = valobj_type.GetName() |
| 93 | if actual_name == '__CFBag' or \ |
| 94 | actual_name == 'const struct __CFBag': |
| 95 | wrapper = CFBagRef_SummaryProvider(valobj, class_data.sys_params) |
| 96 | statistics.metric_hit('code_notrun',valobj) |
| 97 | return wrapper |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 98 | wrapper = CFBagUnknown_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 99 | statistics.metric_hit('unknown_class',str(valobj) + " seen as " + actual_name) |
| 100 | return wrapper; |
| 101 | |
| 102 | def CFBag_SummaryProvider (valobj,dict): |
| 103 | provider = GetSummary_Impl(valobj); |
| 104 | if provider != None: |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 105 | if isinstance(provider,objc_runtime.SpecialSituation_Description): |
| 106 | return provider.message() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 107 | try: |
| 108 | summary = provider.length(); |
| 109 | except: |
| 110 | summary = None |
| 111 | # for some reason, one needs to clear some bits for the count |
| 112 | # to be correct when using CF(Mutable)BagRef on x64 |
| 113 | # the bit mask was derived through experimentation |
| 114 | # (if counts start looking weird, then most probably |
| 115 | # the mask needs to be changed) |
| 116 | if summary == None: |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 117 | summary = '<variable is not CFBag>' |
| 118 | elif isinstance(summary,basestring): |
| 119 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 120 | else: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 121 | if provider.sys_params.is_64_bit: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 122 | summary = summary & ~0x1fff000000000000 |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 123 | if summary == 1: |
Enrico Granata | 8c69c96 | 2012-03-13 00:25:59 +0000 | [diff] [blame] | 124 | summary = '@"1 value"' |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 125 | else: |
Enrico Granata | 8c69c96 | 2012-03-13 00:25:59 +0000 | [diff] [blame] | 126 | summary = '@"' + str(summary) + ' values"' |
Enrico Granata | eb06e25 | 2012-03-07 00:56:09 +0000 | [diff] [blame] | 127 | return summary |
Enrico Granata | 3f1052b | 2012-03-13 21:52:00 +0000 | [diff] [blame] | 128 | return 'Summary Unavailable' |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 129 | |
| 130 | def __lldb_init_module(debugger,dict): |
| 131 | debugger.HandleCommand("type summary add -F CFBag.CFBag_SummaryProvider CFBagRef CFMutableBagRef") |