4 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 See https://llvm.org/LICENSE.txt for license information.
6 SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 # example summary provider for NSMachPort
9 # the real summary is now C++ code built into LLDB
12 import lldb
.runtime
.objc
.objc_runtime
13 import lldb
.formatters
.metrics
14 import lldb
.formatters
.Logger
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")
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
27 class NSMachPortKnown_SummaryProvider
:
28 def adjust_for_architecture(self
):
31 def __init__(self
, valobj
, params
):
32 logger
= lldb
.formatters
.Logger
.Logger()
34 self
.sys_params
= params
35 if not (self
.sys_params
.types_cache
.NSUInteger
):
36 if self
.sys_params
.is_64_bit
:
37 self
.sys_params
.types_cache
.NSUInteger
= (
38 self
.valobj
.GetType().GetBasicType(lldb
.eBasicTypeUnsignedLong
)
41 self
.sys_params
.types_cache
.NSUInteger
= (
42 self
.valobj
.GetType().GetBasicType(lldb
.eBasicTypeUnsignedInt
)
47 logger
= lldb
.formatters
.Logger
.Logger()
48 self
.adjust_for_architecture()
50 # one pointer is the ISA
51 # then we have one other internal pointer, plus
52 # 4 bytes worth of flags. hence, these values
54 logger
= lldb
.formatters
.Logger
.Logger()
55 if self
.sys_params
.is_64_bit
:
61 logger
= lldb
.formatters
.Logger
.Logger()
62 vport
= self
.valobj
.CreateChildAtOffset(
63 "port", self
.offset(), self
.sys_params
.types_cache
.NSUInteger
65 return vport
.GetValueAsUnsigned(0)
68 class NSMachPortUnknown_SummaryProvider
:
69 def adjust_for_architecture(self
):
72 def __init__(self
, valobj
, params
):
73 logger
= lldb
.formatters
.Logger
.Logger()
75 self
.sys_params
= params
79 logger
= lldb
.formatters
.Logger
.Logger()
80 self
.adjust_for_architecture()
83 logger
= lldb
.formatters
.Logger
.Logger()
84 stream
= lldb
.SBStream()
85 self
.valobj
.GetExpressionPath(stream
)
86 num_children_vo
= self
.valobj
.CreateValueFromExpression(
87 "port", "(int)[" + stream
.GetData() + " machPort]"
89 if num_children_vo
.IsValid():
90 return num_children_vo
.GetValueAsUnsigned(0)
91 return "<variable is not NSMachPort>"
94 def GetSummary_Impl(valobj
):
95 logger
= lldb
.formatters
.Logger
.Logger()
100 ) = lldb
.runtime
.objc
.objc_runtime
.Utilities
.prepare_class_detection(
106 name_string
= class_data
.class_name()
107 logger
>> "class name is: " + str(name_string
)
109 if name_string
== "NSMachPort":
110 wrapper
= NSMachPortKnown_SummaryProvider(valobj
, class_data
.sys_params
)
111 statistics
.metric_hit("code_notrun", valobj
)
113 wrapper
= NSMachPortUnknown_SummaryProvider(valobj
, class_data
.sys_params
)
114 statistics
.metric_hit(
115 "unknown_class", valobj
.GetName() + " seen as " + name_string
120 def NSMachPort_SummaryProvider(valobj
, dict):
121 logger
= lldb
.formatters
.Logger
.Logger()
122 provider
= GetSummary_Impl(valobj
)
123 if provider
is not None:
125 provider
, lldb
.runtime
.objc
.objc_runtime
.SpecialSituation_Description
127 return provider
.message()
129 summary
= provider
.port()
132 logger
>> "got summary " + str(summary
)
134 summary
= "<variable is not NSMachPort>"
135 if isinstance(summary
, str):
137 return "mach port: " + str(summary
)
138 return "Summary Unavailable"
141 def __lldb_init_module(debugger
, dict):
142 debugger
.HandleCommand(
143 "type summary add -F NSMachPort.NSMachPort_SummaryProvider NSMachPort"