blob: 11188d045655edc58a911a8a632917c129b39f5d [file] [log] [blame]
Enrico Granata3f1052b2012-03-13 21:52:00 +00001"""
2LLDB AppKit formatters
3
4part of The LLVM Compiler Infrastructure
5This file is distributed under the University of Illinois Open Source
6License. See LICENSE.TXT for details.
7"""
Enrico Granata3467d802012-09-04 18:47:54 +00008# example summary provider for NSArray
9# the real summary is now C++ code built into LLDB
Enrico Granataeb4a4792012-02-23 23:10:27 +000010import lldb
11import ctypes
Enrico Granata28399ad2012-04-25 01:39:27 +000012import lldb.runtime.objc.objc_runtime
13import lldb.formatters.metrics
14import lldb.formatters.Logger
Enrico Granataeb4a4792012-02-23 23:10:27 +000015
Enrico Granata28399ad2012-04-25 01:39:27 +000016statistics = lldb.formatters.metrics.Metrics()
Enrico Granataeb4a4792012-02-23 23:10:27 +000017statistics.add_metric('invalid_isa')
18statistics.add_metric('invalid_pointer')
19statistics.add_metric('unknown_class')
20statistics.add_metric('code_notrun')
21
22# much less functional than the other two cases below
23# just runs code to get to the count and then returns
24# no children
25class NSArrayKVC_SynthProvider:
26
27 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +000028 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +000029
Enrico Granatacfdafa32012-03-05 19:56:33 +000030 def __init__(self, valobj, dict, params):
Enrico Granata28399ad2012-04-25 01:39:27 +000031 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000032 self.valobj = valobj;
33 self.update()
34
35 def update(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000036 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000037 self.adjust_for_architecture();
Enrico Granataeb4a4792012-02-23 23:10:27 +000038
39 def num_children(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000040 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000041 stream = lldb.SBStream()
42 self.valobj.GetExpressionPath(stream)
43 num_children_vo = self.valobj.CreateValueFromExpression("count","(int)[" + stream.GetData() + " count]");
Enrico Granata3f1052b2012-03-13 21:52:00 +000044 if num_children_vo.IsValid():
45 return num_children_vo.GetValueAsUnsigned(0)
46 return "<variable is not NSArray>"
Enrico Granataeb4a4792012-02-23 23:10:27 +000047
Enrico Granataeb4a4792012-02-23 23:10:27 +000048# much less functional than the other two cases below
49# just runs code to get to the count and then returns
50# no children
51class NSArrayCF_SynthProvider:
52
53 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +000054 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +000055
Enrico Granatacfdafa32012-03-05 19:56:33 +000056 def __init__(self, valobj, dict, params):
Enrico Granata28399ad2012-04-25 01:39:27 +000057 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000058 self.valobj = valobj;
Enrico Granatacfdafa32012-03-05 19:56:33 +000059 self.sys_params = params
60 if not (self.sys_params.types_cache.ulong):
61 self.sys_params.types_cache.ulong = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong)
Enrico Granataeb4a4792012-02-23 23:10:27 +000062 self.update()
63
64 def update(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000065 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000066 self.adjust_for_architecture();
Enrico Granataeb4a4792012-02-23 23:10:27 +000067
68 def num_children(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000069 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000070 num_children_vo = self.valobj.CreateChildAtOffset("count",
Enrico Granatacfdafa32012-03-05 19:56:33 +000071 self.sys_params.cfruntime_size,
72 self.sys_params.types_cache.ulong)
Enrico Granataeb4a4792012-02-23 23:10:27 +000073 return num_children_vo.GetValueAsUnsigned(0)
74
Enrico Granataeb4a4792012-02-23 23:10:27 +000075class NSArrayI_SynthProvider:
Enrico Granataeb4a4792012-02-23 23:10:27 +000076 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +000077 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +000078
Enrico Granatacfdafa32012-03-05 19:56:33 +000079 def __init__(self, valobj, dict, params):
Enrico Granata28399ad2012-04-25 01:39:27 +000080 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000081 self.valobj = valobj;
Enrico Granatacfdafa32012-03-05 19:56:33 +000082 self.sys_params = params
83 if not(self.sys_params.types_cache.long):
84 self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong)
Enrico Granataeb4a4792012-02-23 23:10:27 +000085 self.update()
86
87 def update(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000088 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000089 self.adjust_for_architecture();
Enrico Granataeb4a4792012-02-23 23:10:27 +000090
91 # skip the isa pointer and get at the size
92 def num_children(self):
Enrico Granata28399ad2012-04-25 01:39:27 +000093 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +000094 count = self.valobj.CreateChildAtOffset("count",
Enrico Granatacfdafa32012-03-05 19:56:33 +000095 self.sys_params.pointer_size,
96 self.sys_params.types_cache.long);
97 return count.GetValueAsUnsigned(0)
Enrico Granataeb4a4792012-02-23 23:10:27 +000098
99class NSArrayM_SynthProvider:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000100 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +0000101 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +0000102
Enrico Granatacfdafa32012-03-05 19:56:33 +0000103 def __init__(self, valobj, dict, params):
Enrico Granata28399ad2012-04-25 01:39:27 +0000104 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000105 self.valobj = valobj;
Enrico Granatacfdafa32012-03-05 19:56:33 +0000106 self.sys_params = params
107 if not(self.sys_params.types_cache.long):
108 self.sys_params.types_cache.long = self.valobj.GetType().GetBasicType(lldb.eBasicTypeLong)
109 self.update()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000110
111 def update(self):
Enrico Granata28399ad2012-04-25 01:39:27 +0000112 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000113 self.adjust_for_architecture();
Enrico Granataeb4a4792012-02-23 23:10:27 +0000114
115 # skip the isa pointer and get at the size
116 def num_children(self):
Enrico Granata28399ad2012-04-25 01:39:27 +0000117 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000118 count = self.valobj.CreateChildAtOffset("count",
Enrico Granatacfdafa32012-03-05 19:56:33 +0000119 self.sys_params.pointer_size,
120 self.sys_params.types_cache.long);
121 return count.GetValueAsUnsigned(0)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000122
123# this is the actual synth provider, but is just a wrapper that checks
124# whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an
125# appropriate backend layer to do the computations
126class NSArray_SynthProvider:
Enrico Granataeb4a4792012-02-23 23:10:27 +0000127 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +0000128 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +0000129
130 def __init__(self, valobj, dict):
Enrico Granata28399ad2012-04-25 01:39:27 +0000131 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000132 self.valobj = valobj;
133 self.adjust_for_architecture()
Enrico Granata3f1052b2012-03-13 21:52:00 +0000134 self.error = False
135 self.wrapper = self.make_wrapper()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000136 self.invalid = (self.wrapper == None)
137
Enrico Granataeb4a4792012-02-23 23:10:27 +0000138 def num_children(self):
Enrico Granata28399ad2012-04-25 01:39:27 +0000139 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000140 if self.wrapper == None:
141 return 0;
142 return self.wrapper.num_children()
143
144 def update(self):
Enrico Granata28399ad2012-04-25 01:39:27 +0000145 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000146 if self.wrapper == None:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000147 return
148 self.wrapper.update()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000149
150 # this code acts as our defense against NULL and unitialized
151 # NSArray pointers, which makes it much longer than it would be otherwise
Enrico Granata3f1052b2012-03-13 21:52:00 +0000152 def make_wrapper(self):
Enrico Granata28399ad2012-04-25 01:39:27 +0000153 logger = lldb.formatters.Logger.Logger()
Enrico Granata3f1052b2012-03-13 21:52:00 +0000154 if self.valobj.GetValueAsUnsigned() == 0:
155 self.error = True
Enrico Granatade93b6b2012-04-25 20:59:02 +0000156 return lldb.runtime.objc.objc_runtime.InvalidPointer_Description(True)
Enrico Granata3f1052b2012-03-13 21:52:00 +0000157 else:
158 global statistics
Enrico Granata7d222212012-04-25 17:53:41 +0000159 class_data,wrapper =lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(self.valobj,statistics)
Enrico Granata3f1052b2012-03-13 21:52:00 +0000160 if wrapper:
161 self.error = True
162 return wrapper
Enrico Granataeb4a4792012-02-23 23:10:27 +0000163
164 name_string = class_data.class_name()
Enrico Granata247bd412012-04-02 16:39:29 +0000165
166 logger >> "Class name is " + str(name_string)
167
Enrico Granataeb4a4792012-02-23 23:10:27 +0000168 if name_string == '__NSArrayI':
Enrico Granata3f1052b2012-03-13 21:52:00 +0000169 wrapper = NSArrayI_SynthProvider(self.valobj, dict, class_data.sys_params)
Enrico Granataa20e8632012-04-02 23:43:22 +0000170 statistics.metric_hit('code_notrun',self.valobj.GetName())
Enrico Granataeb4a4792012-02-23 23:10:27 +0000171 elif name_string == '__NSArrayM':
Enrico Granata3f1052b2012-03-13 21:52:00 +0000172 wrapper = NSArrayM_SynthProvider(self.valobj, dict, class_data.sys_params)
Enrico Granataa20e8632012-04-02 23:43:22 +0000173 statistics.metric_hit('code_notrun',self.valobj.GetName())
Enrico Granataeb4a4792012-02-23 23:10:27 +0000174 elif name_string == '__NSCFArray':
Enrico Granata3f1052b2012-03-13 21:52:00 +0000175 wrapper = NSArrayCF_SynthProvider(self.valobj, dict, class_data.sys_params)
Enrico Granataa20e8632012-04-02 23:43:22 +0000176 statistics.metric_hit('code_notrun',self.valobj.GetName())
Enrico Granataeb4a4792012-02-23 23:10:27 +0000177 else:
Enrico Granata3f1052b2012-03-13 21:52:00 +0000178 wrapper = NSArrayKVC_SynthProvider(self.valobj, dict, class_data.sys_params)
Enrico Granataa20e8632012-04-02 23:43:22 +0000179 statistics.metric_hit('unknown_class',str(self.valobj.GetName()) + " seen as " + name_string)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000180 return wrapper;
181
182def CFArray_SummaryProvider (valobj,dict):
Enrico Granata28399ad2012-04-25 01:39:27 +0000183 logger = lldb.formatters.Logger.Logger()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000184 provider = NSArray_SynthProvider(valobj,dict);
185 if provider.invalid == False:
Enrico Granata3f1052b2012-03-13 21:52:00 +0000186 if provider.error == True:
187 return provider.wrapper.message()
188 try:
189 summary = int(provider.num_children());
190 except:
191 summary = None
Enrico Granata247bd412012-04-02 16:39:29 +0000192 logger >> "provider gave me " + str(summary)
Enrico Granata3f1052b2012-03-13 21:52:00 +0000193 if summary == None:
194 summary = '<variable is not NSArray>'
195 elif isinstance(summary,basestring):
196 pass
197 else:
198 # we format it like it were a CFString to make it look the same as the summary from Xcode
199 summary = '@"' + str(summary) + (" objects" if summary != 1 else " object") + '"'
200 return summary
201 return 'Summary Unavailable'
Enrico Granataeb4a4792012-02-23 23:10:27 +0000202
203def __lldb_init_module(debugger,dict):
204 debugger.HandleCommand("type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef")