Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 1 | # summary provider for NSBundle |
| 2 | import lldb |
| 3 | import ctypes |
| 4 | import objc_runtime |
| 5 | import metrics |
| 6 | import NSURL |
| 7 | |
| 8 | statistics = metrics.Metrics() |
| 9 | statistics.add_metric('invalid_isa') |
| 10 | statistics.add_metric('invalid_pointer') |
| 11 | statistics.add_metric('unknown_class') |
| 12 | statistics.add_metric('code_notrun') |
| 13 | |
| 14 | # despite the similary to synthetic children providers, these classes are not |
| 15 | # trying to provide anything but a summary for an NSURL, so they need not |
| 16 | # obey the interface specification for synthetic children providers |
| 17 | class NSBundleKnown_SummaryProvider: |
| 18 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 19 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 20 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 21 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 22 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 23 | self.sys_params = params |
| 24 | if not(self.sys_params.types_cache.NSString): |
| 25 | self.sys_params.types_cache.NSString = self.valobj.GetTarget().FindFirstType('NSString').GetPointerType() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 26 | self.update(); |
| 27 | |
| 28 | def update(self): |
| 29 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 30 | |
| 31 | # we need to skip the ISA, plus four other values |
| 32 | # that are luckily each a pointer in size |
| 33 | # which makes our computation trivial :-) |
| 34 | def offset(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 35 | return 5 * self.sys_params.pointer_size |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 36 | |
| 37 | def url_text(self): |
| 38 | global statistics |
| 39 | text = self.valobj.CreateChildAtOffset("text", |
| 40 | self.offset(), |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 41 | self.sys_params.types_cache.NSString) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 42 | my_string = text.GetSummary() |
| 43 | if (my_string == None) or (my_string == ''): |
| 44 | statistics.metric_hit('unknown_class',str(self.valobj) + " triggered unkown pointer location") |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 45 | return NSBundleUnknown_SummaryProvider(self.valobj, self.sys_params).url_text() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 46 | else: |
| 47 | statistics.metric_hit('code_notrun',self.valobj) |
| 48 | return my_string |
| 49 | |
| 50 | |
| 51 | class NSBundleUnknown_SummaryProvider: |
| 52 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 53 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 54 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 55 | def __init__(self, valobj, params): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 56 | self.valobj = valobj; |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 57 | self.sys_params = params |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 58 | self.update() |
| 59 | |
| 60 | def update(self): |
| 61 | self.adjust_for_architecture(); |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 62 | |
| 63 | def url_text(self): |
| 64 | stream = lldb.SBStream() |
| 65 | self.valobj.GetExpressionPath(stream) |
| 66 | expr = "(NSString*)[" + stream.GetData() + " bundlePath]" |
| 67 | url_text_vo = self.valobj.CreateValueFromExpression("path",expr); |
| 68 | return url_text_vo.GetSummary() |
| 69 | |
| 70 | |
| 71 | def GetSummary_Impl(valobj): |
| 72 | global statistics |
| 73 | class_data = objc_runtime.ObjCRuntime(valobj) |
| 74 | if class_data.is_valid() == False: |
| 75 | statistics.metric_hit('invalid_pointer',valobj) |
| 76 | wrapper = None |
| 77 | return |
| 78 | class_data = class_data.read_class_data() |
| 79 | if class_data.is_valid() == False: |
| 80 | statistics.metric_hit('invalid_isa',valobj) |
| 81 | wrapper = None |
| 82 | return |
| 83 | if class_data.is_kvo(): |
| 84 | class_data = class_data.get_superclass() |
| 85 | if class_data.is_valid() == False: |
| 86 | statistics.metric_hit('invalid_isa',valobj) |
| 87 | wrapper = None |
| 88 | return |
| 89 | |
| 90 | name_string = class_data.class_name() |
| 91 | if name_string == 'NSBundle': |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 92 | wrapper = NSBundleKnown_SummaryProvider(valobj, class_data.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 93 | # [NSBundle mainBundle] does return an object that is |
| 94 | # not correctly filled out for our purposes, so we still |
| 95 | # end up having to run code in that case |
| 96 | #statistics.metric_hit('code_notrun',valobj) |
| 97 | else: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame^] | 98 | wrapper = NSBundleUnknown_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 " + name_string) |
| 100 | return wrapper; |
| 101 | |
| 102 | def NSBundle_SummaryProvider (valobj,dict): |
| 103 | provider = GetSummary_Impl(valobj); |
| 104 | if provider != None: |
| 105 | try: |
| 106 | summary = provider.url_text(); |
| 107 | except: |
| 108 | summary = None |
| 109 | if summary == None or summary == '': |
| 110 | summary = 'no valid NSBundle here' |
| 111 | return summary |
| 112 | return '' |
| 113 | |
| 114 | def __lldb_init_module(debugger,dict): |
| 115 | debugger.HandleCommand("type summary add -F NSBundle.NSBundle_SummaryProvider NSBundle") |