]> git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/ntstat.py
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tools / lldbmacros / ntstat.py
1 """ Please make sure you read the README COMPLETELY BEFORE reading anything below.
2 It is very critical that you read coding guidelines in Section E in README file.
3 """
4
5 from xnu import *
6 from utils import *
7 from string import *
8 from socket import *
9
10 import xnudefines
11 from netdefines import *
12 from routedefines import *
13
14 def ShowNstatTUShadow(inshadow):
15 """ Display summary for an nstat_tu_shadow struct
16 params:
17 inshadow : cvalue object which points to 'struct nstat_tu_shadow *'
18 """
19 shad = Cast(inshadow, 'struct nstat_tu_shadow *')
20 procdetails = shad.shad_procdetails
21 out_string = ""
22 if shad :
23 format_string = "nstat_tu_shadow {0: <s}: next={1: <s} prev={2: <s} context (necp_client *)={3: <s} live={4: <d}"
24 out_string += format_string.format(hex(shad), hex(shad.shad_link.tqe_next), hex(shad.shad_link.tqe_prev), hex(shad.shad_provider_context),shad.shad_live)
25
26 magic = unsigned(shad.shad_magic)
27 if (magic != 0xfeedf00d) :
28 format_string = " INVALID shad magic {0: <s}"
29 out_string += format_string.format(hex(magic))
30
31 if (procdetails) :
32 format_string = " --> procdetails {0: <s}: pid={1: <d} name={2: <s} numflows={3: <d}"
33 out_string += format_string.format(hex(procdetails), procdetails.pdet_pid, procdetails.pdet_procname, procdetails.pdet_numflows)
34
35 procmagic = unsigned(procdetails.pdet_magic)
36 if (procmagic != 0xfeedc001) :
37 format_string = " INVALID proc magic {0: <s}"
38 out_string += format_string.format(hex(procmagic))
39
40 print out_string
41
42 def GetNstatProcdetailsBrief(procdetails):
43 """ Display a brief summary for an nstat_procdetails struct
44 params:
45 procdetails : cvalue object which points to 'struct nstat_procdetails *'
46 returns:
47 str : A string describing various information for the nstat_procdetails structure
48 """
49 procdetails = Cast(procdetails, 'struct nstat_procdetails *')
50 out_string = ""
51 if (procdetails) :
52 format_string = " --> pid={0: <d} name={1: <s} numflows={2: <d}"
53 out_string += format_string.format(procdetails.pdet_pid, procdetails.pdet_procname, procdetails.pdet_numflows)
54
55 procmagic = unsigned(procdetails.pdet_magic)
56 if (procmagic != 0xfeedc001) :
57 format_string = " INVALID proc magic {0: <s}"
58 out_string += format_string.format(hex(procmagic))
59
60 return out_string
61
62 def ShowNstatProcdetails(procdetails):
63 """ Display a summary for an nstat_procdetails struct
64 params:
65 procdetails : cvalue object which points to 'struct nstat_procdetails *'
66 """
67 procdetails = Cast(procdetails, 'struct nstat_procdetails *')
68 out_string = ""
69 if (procdetails) :
70 format_string = "nstat_procdetails: {0: <s} next={1: <s} prev={2: <s} "
71 out_string += format_string.format(hex(procdetails), hex(procdetails.pdet_link.tqe_next), hex(procdetails.pdet_link.tqe_prev))
72 out_string += GetNstatProcdetailsBrief(procdetails)
73
74 print out_string
75
76 def GetNstatTUShadowBrief(shadow):
77 """ Display a summary for an nstat_tu_shadow struct
78 params:
79 shadow : cvalue object which points to 'struct nstat_tu_shadow *'
80 returns:
81 str : A string describing various information for the nstat_tu_shadow structure
82 """
83 out_string = ""
84 shad = Cast(shadow, 'struct nstat_tu_shadow *')
85 procdetails = shad.shad_procdetails
86 procdetails = Cast(procdetails, 'struct nstat_procdetails *')
87 out_string = ""
88 if shad :
89 format_string = " shadow {0: <s}: necp_client ={1: <s} live={2: <d}"
90 out_string += format_string.format(hex(shad),hex(shad.shad_provider_context),shad.shad_live)
91 magic = unsigned(shad.shad_magic)
92 if (magic != 0xfeedf00d) :
93 format_string = " INVALID shad magic {0: <s}"
94 out_string += format_string.format(hex(magic))
95 elif (procdetails) :
96 out_string += GetNstatProcdetailsBrief(procdetails)
97
98 return out_string
99
100 def ShowNstatSrc(insrc):
101 """ Display summary for an nstat_src struct
102 params:
103 insrc : cvalue object which points to 'struct nstat_src *'
104 """
105 src = Cast(insrc, 'nstat_src *')
106 prov = src.provider
107 prov = Cast(prov, 'nstat_provider *')
108 prov_string = "?"
109 if (prov.nstat_provider_id == 2):
110 prov_string = "TCP k"
111 elif (prov.nstat_provider_id == 3):
112 prov_string = "TCP u"
113 elif (prov.nstat_provider_id == 4):
114 prov_string = "UDP k"
115 elif (prov.nstat_provider_id == 5):
116 prov_string = "UDP u"
117 elif (prov.nstat_provider_id == 1):
118 prov_string = "Route"
119 elif (prov.nstat_provider_id == 6):
120 prov_string = "ifnet"
121 elif (prov.nstat_provider_id == 7):
122 prov_string = "sysinfo"
123 else:
124 prov_string = "unknown-provider"
125
126 out_string = ""
127 if src :
128 format_string = " nstat_src {0: <s}: prov={1: <s} next={2: <s} prev={3: <s} ref={4: <d}"
129 out_string += format_string.format(hex(src), prov_string, hex(src.ns_control_link.tqe_next), hex(src.ns_control_link.tqe_prev), src.srcref)
130
131 if (prov.nstat_provider_id == 3):
132 out_string += GetNstatTUShadowBrief(src.cookie);
133
134 print out_string
135
136 def ShowNstatCtrl(inctrl):
137 """ Display an nstat_control_state struct
138 params:
139 ctrl : value object representing an nstat_control_state in the kernel
140 """
141 ctrl = Cast(inctrl, 'nstat_control_state *')
142 out_string = ""
143 if ctrl :
144 format_string = "nstat_control_state {0: <s}: next={1: <s} src head={2: <s} tail={3: <s}"
145 out_string += format_string.format(hex(ctrl), hex(ctrl.ncs_next), hex(ctrl.ncs_src_queue.tqh_first), hex(ctrl.ncs_src_queue.tqh_last))
146
147 print out_string
148
149 for src in IterateTAILQ_HEAD(ctrl.ncs_src_queue, 'ns_control_link'):
150 ShowNstatSrc(src)
151
152 # Macro: showallntstat
153
154 @lldb_command('showallntstat')
155 def ShowAllNtstat(cmd_args=None) :
156 """ Show the contents of various ntstat (network statistics) data structures
157 """
158 print "nstat_controls list:\n"
159 ctrl = kern.globals.nstat_controls
160 ctrl = cast(ctrl, 'nstat_control_state *')
161 while ctrl != 0:
162 ShowNstatCtrl(ctrl)
163 ctrl = cast(ctrl.ncs_next, 'nstat_control_state *')
164
165 print "\nnstat_userprot_shad list:\n"
166 shadows = kern.globals.nstat_userprot_shad_head
167 for shad in IterateTAILQ_HEAD(shadows, 'shad_link'):
168 ShowNstatTUShadow(shad)
169
170 print "\nnstat_procdetails list:\n"
171 procdetails_head = kern.globals.nstat_procdetails_head
172 for procdetails in IterateTAILQ_HEAD(procdetails_head, 'pdet_link'):
173 ShowNstatProcdetails(procdetails)
174
175 # EndMacro: showallntstat