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 | 50b5ee5 | 2013-03-16 00:50:25 +0000 | [diff] [blame] | 8 | # example summary provider for CFBag |
| 9 | # the real summary is now C++ code built into LLDB |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 10 | import lldb |
| 11 | import ctypes |
Enrico Granata | 28399ad | 2012-04-25 01:39:27 +0000 | [diff] [blame] | 12 | import lldb.runtime.objc.objc_runtime |
| 13 | import lldb.formatters.metrics |
| 14 | import lldb.formatters.Logger |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 15 | |
Enrico Granata | 28399ad | 2012-04-25 01:39:27 +0000 | [diff] [blame] | 16 | statistics = lldb.formatters.metrics.Metrics() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 17 | statistics.add_metric('invalid_isa') |
| 18 | statistics.add_metric('invalid_pointer') |
| 19 | statistics.add_metric('unknown_class') |
| 20 | statistics.add_metric('code_notrun') |
| 21 | |
| 22 | # despite the similary to synthetic children providers, these classes are not |
| 23 | # trying to provide anything but the length for an CFBag, so they need not |
| 24 | # obey the interface specification for synthetic children providers |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | |
| 26 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 27 | class CFBagRef_SummaryProvider: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | def adjust_for_architecture(self): |
| 30 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | def __init__(self, valobj, params): |
| 33 | logger = lldb.formatters.Logger.Logger() |
| 34 | self.valobj = valobj |
| 35 | self.sys_params = params |
| 36 | if not(self.sys_params.types_cache.NSUInteger): |
| 37 | if self.sys_params.is_64_bit: |
| 38 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType( |
| 39 | ).GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 40 | else: |
| 41 | self.sys_params.types_cache.NSUInteger = self.valobj.GetType( |
| 42 | ).GetBasicType(lldb.eBasicTypeUnsignedInt) |
| 43 | self.update() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | def update(self): |
| 46 | logger = lldb.formatters.Logger.Logger() |
| 47 | self.adjust_for_architecture() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | # 12 bytes on i386 |
| 50 | # 20 bytes on x64 |
| 51 | # most probably 2 pointers and 4 bytes of data |
| 52 | def offset(self): |
| 53 | logger = lldb.formatters.Logger.Logger() |
| 54 | if self.sys_params.is_64_bit: |
| 55 | return 20 |
| 56 | else: |
| 57 | return 12 |
| 58 | |
| 59 | def length(self): |
| 60 | logger = lldb.formatters.Logger.Logger() |
| 61 | size = self.valobj.CreateChildAtOffset( |
| 62 | "count", self.offset(), self.sys_params.types_cache.NSUInteger) |
| 63 | return size.GetValueAsUnsigned(0) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | class CFBagUnknown_SummaryProvider: |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | def adjust_for_architecture(self): |
| 69 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | def __init__(self, valobj, params): |
| 72 | logger = lldb.formatters.Logger.Logger() |
| 73 | self.valobj = valobj |
| 74 | self.sys_params = params |
| 75 | self.update() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | def update(self): |
| 78 | logger = lldb.formatters.Logger.Logger() |
| 79 | self.adjust_for_architecture() |
| 80 | |
| 81 | def length(self): |
| 82 | logger = lldb.formatters.Logger.Logger() |
| 83 | stream = lldb.SBStream() |
| 84 | self.valobj.GetExpressionPath(stream) |
| 85 | num_children_vo = self.valobj.CreateValueFromExpression( |
| 86 | "count", "(int)CFBagGetCount(" + stream.GetData() + " )") |
| 87 | if num_children_vo.IsValid(): |
| 88 | return num_children_vo.GetValueAsUnsigned(0) |
| 89 | return "<variable is not CFBag>" |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def GetSummary_Impl(valobj): |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | logger = lldb.formatters.Logger.Logger() |
| 94 | global statistics |
| 95 | class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection( |
| 96 | valobj, statistics) |
| 97 | if wrapper: |
| 98 | return wrapper |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | name_string = class_data.class_name() |
| 101 | actual_name = name_string |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | logger >> "name string got was " + \ |
| 104 | str(name_string) + " but actual name is " + str(actual_name) |
| 105 | |
| 106 | if class_data.is_cftype(): |
| 107 | # CFBag does not expose an actual NSWrapper type, so we have to check that this is |
| 108 | # an NSCFType and then check we are a pointer-to __CFBag |
| 109 | valobj_type = valobj.GetType() |
| 110 | if valobj_type.IsValid() and valobj_type.IsPointerType(): |
| 111 | valobj_type = valobj_type.GetPointeeType() |
| 112 | if valobj_type.IsValid(): |
| 113 | actual_name = valobj_type.GetName() |
| 114 | if actual_name == '__CFBag' or \ |
| 115 | actual_name == 'const struct __CFBag': |
| 116 | wrapper = CFBagRef_SummaryProvider(valobj, class_data.sys_params) |
| 117 | statistics.metric_hit('code_notrun', valobj) |
| 118 | return wrapper |
| 119 | wrapper = CFBagUnknown_SummaryProvider(valobj, class_data.sys_params) |
| 120 | statistics.metric_hit( |
| 121 | 'unknown_class', |
| 122 | valobj.GetName() + |
| 123 | " seen as " + |
| 124 | actual_name) |
| 125 | return wrapper |
| 126 | |
| 127 | |
| 128 | def CFBag_SummaryProvider(valobj, dict): |
| 129 | logger = lldb.formatters.Logger.Logger() |
| 130 | provider = GetSummary_Impl(valobj) |
| 131 | if provider is not None: |
| 132 | if isinstance( |
| 133 | provider, |
| 134 | lldb.runtime.objc.objc_runtime.SpecialSituation_Description): |
| 135 | return provider.message() |
| 136 | try: |
| 137 | summary = provider.length() |
| 138 | except: |
| 139 | summary = None |
| 140 | logger >> "summary got from provider: " + str(summary) |
| 141 | # for some reason, one needs to clear some bits for the count |
| 142 | # to be correct when using CF(Mutable)BagRef on x64 |
| 143 | # the bit mask was derived through experimentation |
| 144 | # (if counts start looking weird, then most probably |
| 145 | # the mask needs to be changed) |
| 146 | if summary is None: |
| 147 | summary = '<variable is not CFBag>' |
| 148 | elif isinstance(summary, basestring): |
| 149 | pass |
| 150 | else: |
| 151 | if provider.sys_params.is_64_bit: |
| 152 | summary = summary & ~0x1fff000000000000 |
| 153 | if summary == 1: |
| 154 | summary = '@"1 value"' |
| 155 | else: |
| 156 | summary = '@"' + str(summary) + ' values"' |
| 157 | return summary |
| 158 | return 'Summary Unavailable' |
| 159 | |
| 160 | |
| 161 | def __lldb_init_module(debugger, dict): |
| 162 | debugger.HandleCommand( |
| 163 | "type summary add -F CFBag.CFBag_SummaryProvider CFBagRef CFMutableBagRef") |