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