Home | History | Annotate | Download | only in cocoa
      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 """
      8 # example summary provider for NSMachPort
      9 # the real summary is now C++ code built into LLDB
     10 import lldb
     11 import ctypes
     12 import lldb.runtime.objc.objc_runtime
     13 import lldb.formatters.metrics
     14 import lldb.formatters.Logger
     15 
     16 statistics = lldb.formatters.metrics.Metrics()
     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 port number of an NSMachPort, so they need not
     24 # obey the interface specification for synthetic children providers
     25 class NSMachPortKnown_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 	# one pointer is the ISA
     45 	# then we have one other internal pointer, plus
     46 	# 4 bytes worth of flags. hence, these values
     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 port(self):
     55 		logger = lldb.formatters.Logger.Logger()
     56 		vport = self.valobj.CreateChildAtOffset("port",
     57 							self.offset(),
     58 							self.sys_params.types_cache.NSUInteger)
     59 		return vport.GetValueAsUnsigned(0)
     60 
     61 
     62 class NSMachPortUnknown_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 port(self):
     77 		logger = lldb.formatters.Logger.Logger()
     78 		stream = lldb.SBStream()
     79 		self.valobj.GetExpressionPath(stream)
     80 		num_children_vo = self.valobj.CreateValueFromExpression("port","(int)[" + stream.GetData() + " machPort]")
     81 		if num_children_vo.IsValid():
     82 			return num_children_vo.GetValueAsUnsigned(0)
     83 		return '<variable is not NSMachPort>'
     84 
     85 
     86 def 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 	logger >> "class name is: " + str(name_string)
     95 
     96 	if name_string == 'NSMachPort':
     97 		wrapper = NSMachPortKnown_SummaryProvider(valobj, class_data.sys_params)
     98 		statistics.metric_hit('code_notrun',valobj)
     99 	else:
    100 		wrapper = NSMachPortUnknown_SummaryProvider(valobj, class_data.sys_params)
    101 		statistics.metric_hit('unknown_class',valobj.GetName() + " seen as " + name_string)
    102 	return wrapper;
    103 
    104 def NSMachPort_SummaryProvider (valobj,dict):
    105 	logger = lldb.formatters.Logger.Logger()
    106 	provider = GetSummary_Impl(valobj);
    107 	if provider != None:
    108 		if isinstance(provider,lldb.runtime.objc.objc_runtime.SpecialSituation_Description):
    109 			return provider.message()
    110 		try:
    111 			summary = provider.port();
    112 		except:
    113 			summary = None
    114 		logger >> "got summary " + str(summary)
    115 		if summary == None:
    116 			summary = '<variable is not NSMachPort>'
    117 		if isinstance(summary, basestring):
    118 			return summay
    119 		return 'mach port: ' + str(summary)
    120 	return 'Summary Unavailable'
    121 
    122 def __lldb_init_module(debugger,dict):
    123 	debugger.HandleCommand("type summary add -F NSMachPort.NSMachPort_SummaryProvider NSMachPort")
    124