xref: /xnu-8020.140.41/tools/lldbmacros/net.py (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1
2""" Please make sure you read the README COMPLETELY BEFORE reading anything below.
3    It is very critical that you read coding guidelines in Section E in README file.
4"""
5from __future__ import absolute_import, print_function
6
7from builtins import hex
8from builtins import range
9
10from xnu import *
11from utils import *
12from string import *
13from socket import *
14import tempfile
15
16import xnudefines
17from netdefines import *
18from routedefines import *
19from mbufdefines import *
20
21def GetDlilIfFlagsAsString(dlil_if_flags):
22    """ Return a formatted string description of the dlil interface flags
23    """
24    out_string = ""
25    flags = (unsigned)(dlil_if_flags & 0xffff)
26    i = 0
27    num = 1
28    while num <= flags:
29        if flags & num:
30            out_string += dlil_if_flags_strings[i] + ","
31        i += 1
32        num = num << 1
33    return out_string.rstrip(",")
34
35def GetIfFlagsAsString(if_flags):
36    """ Return a formatted string description of the interface flags
37    """
38    out_string = ""
39    flags = (unsigned)(if_flags & 0xffff)
40    i = 0
41    num = 1
42    while num <= flags:
43        if flags & num:
44            out_string += if_flags_strings[i] + ","
45        i += 1
46        num = num << 1
47    return out_string.rstrip(",")
48
49
50def ShowIfConfiguration(ifnet):
51    """ Display ifconfig-like output for the ifnet
52    """
53    iface = Cast(ifnet, 'ifnet *')
54    dlifnet = Cast(ifnet, 'dlil_ifnet *')
55    out_string = ""
56    format_string = "{0: <s}: flags={1: <x} <{2: <s}> index {3: <d} mtu {4: <d}"
57    if iface :
58        out_string += format_string.format(iface.if_xname, (iface.if_flags & 0xffff), GetIfFlagsAsString(iface.if_flags), iface.if_index, iface.if_data.ifi_mtu)
59        out_string += "\n\tdlil flags=" + hex(dlifnet.dl_if_flags)+ " <" + GetDlilIfFlagsAsString(dlifnet.dl_if_flags) + ">"
60        out_string += "\n\t(struct ifnet *)" + hex(ifnet)
61        if iface.if_snd and iface.if_snd.ifcq_len :
62            out_string += "\n\t" + str(iface.if_snd.ifcq_len)
63        if dlifnet.dl_if_inpstorage.dlth_pkts.qlen :
64            out_string += "\n\t" + str(dlifnet.dl_if_inpstorage.dlth_pkts.qlen)
65    print(out_string)
66
67def GetIfConfiguration(ifname):
68    """ Return ifnet structure corresponding to the ifname passed in
69    """
70    global kern
71    ifnets = kern.globals.ifnet_head
72    for ifnet in IterateTAILQ_HEAD(ifnets, "if_link") :
73        if str(ifnet.if_xname) == ifname :
74            return ifnet
75    return None
76
77# Macro: net_get_always_on_pktap
78@lldb_command('net_get_always_on_pktap')
79def NetGetAlwaysOnPktap(cmd_args=None):
80    """ Dump the always-on packet capture to /tmp/dump.pktap
81    """
82    for i in range(0, 10):
83        ifnet = GetIfConfiguration("pktap"+str(i))
84        if not ifnet:
85            continue
86        if ifnet.if_bpf == 0:
87            ifnet = None
88            continue
89        if ifnet.if_bpf.bif_dlist.bd_headdrop == 0:
90            ifnet = None
91            continue
92
93        break
94
95    if not ifnet:
96        print("Could not find a pktap interface")
97        return
98
99    bpf_d = ifnet.if_bpf.bif_dlist
100
101    f = tempfile.NamedTemporaryFile(prefix="dump-", suffix=".pktap", dir="/tmp/", mode="wb", delete=False)
102
103    err = lldb.SBError()
104
105    if bpf_d.bd_hbuf != 0:
106        addr = bpf_d.bd_hbuf[0]._sbval19k84obscure747.AddressOf().GetValueAsUnsigned()
107        buf = LazyTarget.GetProcess().ReadMemory(addr, unsigned(bpf_d.bd_hlen), err)
108        if err.fail:
109            print("Error, getting sbuf")
110        f.write(buf)
111
112    addr = bpf_d.bd_sbuf[0]._sbval19k84obscure747.AddressOf().GetValueAsUnsigned()
113    buf = LazyTarget.GetProcess().ReadMemory(addr, unsigned(bpf_d.bd_slen), err)
114    if err.fail:
115        print("Error, getting sbuf")
116    f.write(buf)
117
118    print(f.name)
119    f.close()
120# EndMacro: net_get_always_on_pktap
121
122# Macro: ifconfig
123@lldb_command('ifconfig')
124def ShowIfconfig(cmd_args=None) :
125    """ Display ifconfig-like output, and print the (struct ifnet *) pointers for further inspection
126    """
127    if cmd_args != None and len(cmd_args) > 0:
128        showall = 1
129    else:
130        showall = 0
131
132    ifnets = kern.globals.ifnet_head
133    for ifnet in IterateTAILQ_HEAD(ifnets, "if_link"):
134        ShowIfConfiguration(ifnet)
135        if (showall == 1):
136            print(GetIfaddrs(ifnet))
137# EndMacro: ifconfig
138
139#Macro: ifconfig_dlil
140@lldb_command('ifconfig_dlil')
141def ShowIfconfigDlil(cmd_args=None) :
142    """ Display ifconfig-like output for DLIL interface list, print (struct ifnet *) pointer and dlil info for further inspection
143    """
144    dlil_ifnets = kern.globals.dlil_ifnet_head
145    for dlil_ifnet in IterateTAILQ_HEAD(dlil_ifnets, "dl_if_link"):
146        ShowIfConfiguration(dlil_ifnet)
147        print(GetIfaddrs(Cast(dlil_ifnet, 'ifnet *')))
148# EndMacro: ifconfig_dlil
149
150def GetAddressAsStringColonHex(addr, count):
151    out_string = ""
152    i = 0
153    addr_format_string = "{0:02x}"
154    while (i < count):
155        if (i == 0):
156            out_string += addr_format_string.format(unsigned(addr[i]))[-2:]
157        else:
158            out_string += ":" + addr_format_string.format(unsigned(addr[i]))[-2:]
159        i += 1
160    return out_string
161
162def GetSocketAddrAsStringUnspec(sockaddr):
163    out_string = ""
164    out_string += GetAddressAsStringColonHex(sockaddr.sa_data, sockaddr.sa_len - 2)
165    return out_string
166
167def GetSocketAddrAsStringUnix(sockaddr):
168    sock_unix = Cast(sockaddr, 'sockaddr_un *')
169    if (sock_unix == 0):
170        return "(null)"
171    else:
172        if (len(str(sock_unix.sun_path)) > 0):
173            return str(sock_unix.sun_path)
174        else:
175            return "\"\""
176
177def GetInAddrAsString(ia):
178    out_string = ""
179    inaddr = Cast(ia, 'in_addr *')
180
181    packed_value = struct.pack('I', unsigned(ia.s_addr))
182    out_string = inet_ntoa(packed_value)
183    return out_string
184
185def GetIn6AddrAsString(ia):
186    out_string = ""
187    addr = ia
188
189    addr_format_string = "{0:02x}:{1:02x}:{2:02x}:{3:02x}{4:02x}:{5:02x}:{6:02x}:{7:02x}{8:02x}:{9:02x}:{10:02x}:{11:02x}{12:02x}:{13:02x}:{14:02x}:{15:02x}"
190    out_string += addr_format_string.format(unsigned(addr[0]), unsigned(addr[1]), unsigned(addr[2]), unsigned(addr[3]), unsigned(addr[4]), unsigned(addr[5]), unsigned(addr[6]), unsigned(addr[7]), unsigned(addr[8]), unsigned(addr[9]), unsigned(addr[10]), unsigned(addr[11]), unsigned(addr[12]), unsigned(addr[13]), unsigned(addr[14]), unsigned(addr[15]))
191    return out_string
192
193def GetSocketAddrAsStringInet(sockaddr):
194    sock_in = Cast(sockaddr, 'sockaddr_in *')
195    return GetInAddrAsString(addressof(sock_in.sin_addr))
196
197def GetSocketAddrAsStringInet6(sockaddr):
198    sock_in6 = Cast(sockaddr, 'sockaddr_in6 *')
199    return GetIn6AddrAsString(sock_in6.sin6_addr.__u6_addr.__u6_addr8)
200
201def GetSocketAddrAsStringLink(sockaddr):
202    sock_link = Cast(sockaddr, 'sockaddr_dl *')
203    if sock_link is None:
204        return "(null)"
205    else:
206        out_string = ""
207        if (sock_link.sdl_nlen == 0 and sock_link.sdl_alen == 0 and sock_link.sdl_slen == 0):
208            out_string = "link#" + str(int(sock_link.sdl_index))
209        else:
210            out_string += GetAddressAsStringColonHex(addressof(sock_link.sdl_data[sock_link.sdl_nlen]), sock_link.sdl_alen)
211    return out_string
212
213def GetSocketAddrAsStringAT(sockaddr):
214    out_string = ""
215    sock_addr = Cast(sockaddr, 'sockaddr *')
216    out_string += GetAddressAsStringColonHex(sockaddr.sa_data, sockaddr.sa_len - 2)
217    return out_string
218
219def GetSocketAddrAsString(sockaddr):
220    if sockaddr is None :
221        return "(null)"
222    out_string = ""
223    if (sockaddr.sa_family == 0):
224        out_string += "UNSPC "
225        GetSocketAddrAsStringUnspec(sockaddr)
226    elif (sockaddr.sa_family == 1):
227        out_string += "UNIX "
228        out_string += GetSocketAddrAsStringUnix(sockaddr)
229    elif (sockaddr.sa_family == 2):
230        out_string += "INET "
231        out_string += GetSocketAddrAsStringInet(sockaddr)
232    elif (sockaddr.sa_family == 30):
233        out_string += "INET6 "
234        out_string += GetSocketAddrAsStringInet6(sockaddr)
235    elif (sockaddr.sa_family == 18):
236        out_string += "LINK "
237        out_string += GetSocketAddrAsStringLink(sockaddr)
238    elif (sockaddr.sa_family == 16):
239        out_string += "ATLK "
240        out_string += GetSocketAddrAsStringAT(sockaddr)
241    else:
242        out_string += "FAM " + str(sockaddr.sa_family)
243        out_string += GetAddressAsStringColonHex(sockaddr.sa_data, sockaddr.sa_len)
244    return out_string
245
246# Macro: showifaddrs
247@lldb_command('showifaddrs')
248def ShowIfaddrs(cmd_args=None):
249    """ Show the (struct ifnet).if_addrhead list of addresses for the given ifp
250    """
251    if cmd_args != None and len(cmd_args) > 0 :
252        ifp = kern.GetValueFromAddress(cmd_args[0], 'ifnet *')
253        if not ifp:
254            print("Unknown value passed as argument.")
255            return
256        i = 1
257        for ifaddr in IterateTAILQ_HEAD(ifp.if_addrhead, "ifa_link"):
258            format_string = "\t{0: <d}: 0x{1: <x} {2: <s} [{3: <d}]"
259            print(format_string.format(i, ifaddr, GetSocketAddrAsString(ifaddr.ifa_addr), ifaddr.ifa_refcnt))
260            i += 1
261    else :
262        print("Missing argument 0 in user function.")
263# EndMacro: showifaddrs
264
265def GetIfaddrs(ifp):
266    out_string = ""
267    if (ifp != 0):
268        i = 1
269        for ifaddr in IterateTAILQ_HEAD(ifp.if_addrhead, "ifa_link"):
270            format_string = "\t{0: <d}: 0x{1: <x} {2: <s} [{3: <d}]"
271            out_string += format_string.format(i, ifaddr, GetSocketAddrAsString(ifaddr.ifa_addr), ifaddr.ifa_refcnt) + "\n"
272            i += 1
273    else:
274        out_string += "Missing argument 0 in user function."
275    return out_string
276
277
278def GetCapabilitiesAsString(flags):
279    """ Return a formatted string description of the interface flags
280    """
281    out_string = ""
282    i = 0
283    num = 1
284    while num <= flags:
285        if flags & num:
286            out_string += if_capenable_strings[i] + ","
287        i += 1
288        num = num << 1
289    return out_string.rstrip(",")
290
291def GetIfEflagsAsString(if_eflags):
292    """ Return a formatted string description of the interface extra flags
293    """
294    out_string = ""
295    flags = unsigned(if_eflags)
296    i = 0
297    num = 1
298    while num <= flags:
299        if flags & num:
300            out_string += if_eflags_strings[i] + ","
301        i += 1
302        num = num << 1
303    return out_string.rstrip(",")
304
305def GetIfXflagsAsString(if_xflags):
306    """ Return a formatted string description of the interface extended flags
307    """
308    out_string = ""
309    flags = unsigned(if_xflags)
310    i = 0
311    num = 1
312    while num <= flags:
313        if flags & num:
314            out_string += if_xflags_strings[i] + ","
315        i += 1
316        num = num << 1
317    return out_string.rstrip(",")
318
319def ShowDlilIfnetConfiguration(dlil_ifnet, show_all) :
320    """ Formatted display of dlil_ifnet structures
321    """
322    DLIF_INUSE = 0x1
323    DLIF_REUSE = 0x2
324
325    if dlil_ifnet is None :
326        return
327
328    dlil_iface = Cast(dlil_ifnet, 'dlil_ifnet *')
329    iface = Cast(dlil_ifnet, 'ifnet *')
330    out_string = ""
331    if (dlil_iface.dl_if_flags & DLIF_REUSE) :
332        out_string  += "*"
333    format_string = "{0: <s}: flags={1: <x} <{2: <s}> index {3: <d} mtu {4: <d}"
334    extended_flags_format_string = "\n\teflags={0: <x} <{1: <s}>"
335    extra_flags_format_string = "\n\txflags={0: <x} <{1: <s}>"
336    capenabled_format_string = "\n\toptions={0: <x} <{1: <s}>"
337    if (dlil_iface.dl_if_flags & DLIF_INUSE) :
338        out_string += format_string.format(iface.if_xname, (iface.if_flags & 0xffff), GetIfFlagsAsString(iface.if_flags), iface.if_index, iface.if_data.ifi_mtu)
339    else :
340        out_string += format_string.format("[" + str(iface.if_name) + str(int(iface.if_unit)) + "]", (iface.if_flags & 0xffff), GetIfFlagsAsString(iface.if_flags), iface.if_index, iface.if_data.ifi_mtu)
341    if (iface.if_eflags) :
342        out_string += extended_flags_format_string.format(iface.if_eflags, GetIfEflagsAsString(iface.if_eflags))
343    if (iface.if_xflags) :
344        out_string += extra_flags_format_string.format(iface.if_xflags, GetIfXflagsAsString(iface.if_xflags))
345    if (iface.if_capenable) :
346        out_string += capenabled_format_string.format(iface.if_capenable, GetCapabilitiesAsString(iface.if_capenable))
347    out_string += "\n\t(struct ifnet *)" + hex(dlil_ifnet) + "\n"
348    if show_all :
349        out_string += GetIfaddrs(iface)
350        out_string += "\n"
351    print(out_string)
352
353# Macro: showifnets
354@lldb_command('showifnets')
355def ShowIfnets(cmd_args=None) :
356    """ Display ifconfig-like output for all attached and detached interfaces
357    """
358    showall = 0
359    if cmd_args != None and len(cmd_args) > 0 :
360        showall = 1
361    dlil_ifnets = kern.globals.dlil_ifnet_head
362    for dlil_ifnet in IterateTAILQ_HEAD(dlil_ifnets, "dl_if_link"):
363        ShowDlilIfnetConfiguration(dlil_ifnet, showall)
364# EndMacro: showifnets
365
366# Macro: showifmultiaddrs
367@lldb_command('showifmultiaddrs')
368def ShowIfMultiAddrs(cmd_args=None) :
369    """ Show the list of multicast addresses for the given ifp
370    """
371    out_string = ""
372    if cmd_args != None and len(cmd_args) > 0 :
373        ifp = kern.GetValueFromAddress(cmd_args[0], 'ifnet *')
374        if not ifp:
375            print("Unknown value passed as argument.")
376            return
377        ifmulti = cast(ifp.if_multiaddrs.lh_first, 'ifmultiaddr *')
378        i = 0
379        while ifmulti != 0:
380            ifma_format_string = "\t{0: <d}: 0x{1: <x} "
381            out_string += (ifma_format_string.format(i + 1, ifmulti))
382            if (ifmulti.ifma_addr.sa_family == 2):
383                if (ifmulti.ifma_ll != 0):
384                    out_string += GetSocketAddrAsStringLink(ifmulti.ifma_ll.ifma_addr) + " "
385                out_string += GetSocketAddrAsStringInet(ifmulti.ifma_addr)
386            if (ifmulti.ifma_addr.sa_family == 30):
387                if (ifmulti.ifma_ll != 0):
388                    out_string += GetSocketAddrAsStringLink(ifmulti.ifma_ll.ifma_addr) + " "
389                out_string += GetSocketAddrAsStringInet6(ifmulti.ifma_addr) + " "
390            if (ifmulti.ifma_addr.sa_family == 18):
391                out_string += GetSocketAddrAsStringLink(ifmulti.ifma_addr) + " "
392            if (ifmulti.ifma_addr.sa_family == 0):
393                out_string += GetSocketAddrAsStringUnspec(ifmulti.ifma_addr) + " "
394            out_string += "[" + str(int(ifmulti.ifma_refcount)) + "]\n"
395            ifmulti = cast(ifmulti.ifma_link.le_next, 'ifmultiaddr *')
396            i += 1
397        print(out_string)
398    else :
399        print("Missing argument 0 in user function.")
400# EndMacro: showifmultiaddrs
401
402# Macro: showinmultiaddrs
403@lldb_command('showinmultiaddrs')
404def ShowInMultiAddrs(cmd_args=None) :
405    """ Show the contents of IPv4 multicast address records
406    """
407    out_string = ""
408    inmultihead = kern.globals.in_multihead
409    inmulti = cast(inmultihead.lh_first, 'in_multi *')
410    i = 0
411    while inmulti != 0:
412        ifp = inmulti.inm_ifp
413        inma_format_string = "\t{0: <d}: 0x{1: <x} "
414        out_string += inma_format_string.format(i + 1, inmulti) + " "
415        out_string += GetInAddrAsString(addressof(inmulti.inm_addr)) + " "
416        ifma_format_string = "(ifp 0x{0: <x} [{1: <s}] ifma {2: <x})"
417        out_string += ifma_format_string.format(ifp, ifp.if_xname, inmulti.inm_ifma) + "\n"
418        inmulti = cast(inmulti.inm_link.le_next, 'in_multi *')
419        i += 1
420    print(out_string)
421# EndMacro: showinmultiaddrs
422
423# Macro: showin6multiaddrs
424@lldb_command('showin6multiaddrs')
425def ShowIn6MultiAddrs(cmd_args=None) :
426    """ Show the contents of IPv6 multicast address records
427    """
428    out_string = ""
429    in6multihead = kern.globals.in6_multihead
430    in6multi = cast(in6multihead.lh_first, 'in6_multi *')
431    i = 0
432    while in6multi != 0:
433        ifp = in6multi.in6m_ifp
434        inma_format_string = "\t{0: <d}: 0x{1: <x} "
435        out_string += inma_format_string.format(i + 1, in6multi) + " "
436        out_string += GetIn6AddrAsString((in6multi.in6m_addr.__u6_addr.__u6_addr8)) + " "
437        ifma_format_string = "(ifp 0x{0: <x} [{1: <s}] ifma {2: <x})"
438        out_string += ifma_format_string.format(ifp, ifp.if_xname, in6multi.in6m_ifma) + "\n"
439        in6multi = cast(in6multi.in6m_entry.le_next, 'in6_multi *')
440        i += 1
441    print(out_string)
442# EndMacro: showin6multiaddrs
443
444def GetTcpState(tcpcb):
445    out_string = ""
446    tp = Cast(tcpcb, 'tcpcb *')
447    if (int(tp) != 0):
448        if tp.t_state == 0:
449            out_string += "CLOSED\t"
450        if tp.t_state == 1:
451            out_string += "LISTEN\t"
452        if tp.t_state == 2:
453            out_string += "SYN_SENT\t"
454        if tp.t_state == 3:
455            out_string += "SYN_RCVD\t"
456        if tp.t_state == 4:
457            out_string += "ESTABLISHED\t"
458        if tp.t_state == 5:
459            out_string += "CLOSE_WAIT\t"
460        if tp.t_state == 6:
461            out_string += "FIN_WAIT_1\t"
462        if tp.t_state == 7:
463            out_string += "CLOSING\t"
464        if tp.t_state == 8:
465            out_string += "LAST_ACK\t"
466        if tp.t_state == 9:
467            out_string += "FIN_WAIT_2\t"
468        if tp.t_state == 10:
469            out_string += "TIME_WAIT\t"
470    return out_string
471
472def GetSocketProtocolAsString(sock):
473    out_string = ""
474    inpcb = Cast(sock.so_pcb, 'inpcb *')
475    if sock.so_proto.pr_protocol == 6:
476        out_string += " TCP "
477        out_string += GetTcpState(inpcb.inp_ppcb)
478    if sock.so_proto.pr_protocol == 17:
479        out_string += " UDP "
480    if sock.so_proto.pr_protocol == 1:
481        out_string += " ICMP "
482    if sock.so_proto.pr_protocol == 254:
483        out_string += " DIVERT "
484    if sock.so_proto.pr_protocol == 255:
485        out_string += " RAW "
486    return out_string
487
488def GetInAddr4to6AsString(inaddr):
489    out_string = ""
490    if (inaddr is not None):
491        ia = Cast(inaddr, 'unsigned char *')
492        inaddr_format_string = "{0:d}:{1:d}:{2:d}:{3:d}"
493        out_string += inaddr_format_string.format(unsigned(ia[0]), unsigned(ia[1]), unsigned(ia[2]), unsigned(ia[3]))
494    return out_string
495
496def GetInPortAsString(port):
497    out_string = ""
498    port_string = Cast(port, 'char *')
499    port_unsigned = dereference(Cast(port, 'unsigned short *'))
500
501    if ((((port_unsigned & 0xff00) >> 8) == port_string[0])) and (((port_unsigned & 0x00ff) == port_string[1])):
502        out_string += ":" + str(int(port_unsigned))
503    else:
504        out_string += ":" + str(int(((port_unsigned & 0xff00) >> 8) | ((port_unsigned & 0x00ff) << 8)))
505
506    return out_string
507
508def GetIPv4SocketAsString(sock) :
509    out_string = ""
510    pcb = Cast(sock.so_pcb, 'inpcb *')
511    if (pcb == 0):
512        out_string += "inpcb: (null) "
513    else:
514        out_string += "inpcb: " + hex(pcb)
515        out_string += GetSocketProtocolAsString(sock)
516
517        out_string += GetInAddr4to6AsString(addressof(pcb.inp_dependladdr.inp46_local.ia46_addr4))
518        out_string += GetInPortAsString(addressof(pcb.inp_lport))
519        out_string += " -> "
520        out_string += GetInAddr4to6AsString(addressof(pcb.inp_dependfaddr.inp46_foreign.ia46_addr4))
521        out_string += GetInPortAsString(addressof(pcb.inp_fport))
522    return out_string
523
524def GetIPv6SocketAsString(sock) :
525    out_string = ""
526    pcb = Cast(sock.so_pcb, 'inpcb *')
527    if (pcb == 0):
528        out_string += "inpcb: (null) "
529    else:
530        out_string += "inpcb: " + hex(pcb) + " "
531        out_string += GetSocketProtocolAsString(sock)
532
533        out_string += GetIn6AddrAsString((pcb.inp_dependladdr.inp6_local.__u6_addr.__u6_addr8))
534        out_string += GetInPortAsString(addressof(pcb.inp_lport))
535        out_string += " -> "
536        out_string += GetIn6AddrAsString((pcb.inp_dependfaddr.inp6_foreign.__u6_addr.__u6_addr8))
537        out_string += GetInPortAsString(addressof(pcb.inp_fport))
538    return out_string
539
540def GetUnixDomainSocketAsString(sock) :
541    out_string = ""
542    pcb = Cast(sock.so_pcb, 'unpcb *')
543    if (pcb == 0):
544        out_string += "unpcb: (null) "
545    else:
546        out_string += "unpcb: " + hex(pcb)  + " "
547        out_string += "unp_vnode: " + hex(pcb.unp_vnode) + " "
548        out_string += "unp_conn: " + hex(pcb.unp_conn) + " "
549        out_string += "unp_addr: " + GetSocketAddrAsStringUnix(pcb.unp_addr)
550    return out_string
551
552def GetVsockSocketAsString(sock) :
553    out_string = ""
554    pcb = Cast(sock.so_pcb, 'vsockpcb *')
555    if (pcb == 0):
556        out_string += "vsockpcb: (null) "
557    else:
558        out_string += "vsockpcb: " + hex(pcb) + " "
559        out_string += str(pcb.local_address) + " "
560        out_string += str(pcb.remote_address)
561    return out_string
562
563def GetSocket(socket) :
564    """ Show the contents of a socket
565    """
566    so = kern.GetValueFromAddress(unsigned(socket), 'socket *')
567    if (so):
568        out_string = ""
569        sock_format_string = "so: 0x{0:<x} options 0x{1:<x} state 0x{2:<x}"
570        out_string += sock_format_string.format(so, so.so_options, so.so_state)
571        domain = so.so_proto.pr_domain
572        domain_name_format_string = " {0:<s} "
573        out_string += domain_name_format_string.format(domain.dom_name)
574        if (domain.dom_family == 1):
575            out_string += GetUnixDomainSocketAsString(so)
576        if (domain.dom_family == 2):
577            out_string += GetIPv4SocketAsString(so)
578        if (domain.dom_family == 30):
579            out_string += GetIPv6SocketAsString(so)
580        if (domain.dom_family == 40):
581            out_string += GetVsockSocketAsString(so)
582        out_string += " s=" + str(int(so.so_snd.sb_cc)) + " r=" + str(int(so.so_rcv.sb_cc)) + " usecnt=" + str(int(so.so_usecount))
583    else:
584        out_string += "(null)"
585    return out_string
586# EndMacro: showsocket
587
588
589# Macro: showsocket
590@lldb_command('showsocket')
591def ShowSocket(cmd_args=None) :
592    """ Show the contents of a socket
593    """
594    if (cmd_args == None or len(cmd_args) == 0):
595            print("Missing argument 0 in user function.")
596            return
597    so = kern.GetValueFromAddress(cmd_args[0], 'socket *')
598    if (len(str(cmd_args[0])) > 0):
599        out_string = ""
600        sock_format_string = "so: 0x{0:<x} options 0x{1:<x} state 0x{2:<x}"
601        out_string += sock_format_string.format(so, so.so_options, so.so_state)
602        domain = so.so_proto.pr_domain
603        domain_name_format_string = " {0:<s} "
604        out_string += domain_name_format_string.format(domain.dom_name)
605        if (domain.dom_family == 1):
606            out_string += GetUnixDomainSocketAsString(so)
607        if (domain.dom_family == 2):
608            out_string += GetIPv4SocketAsString(so)
609        if (domain.dom_family == 30):
610            out_string += GetIPv6SocketAsString(so)
611        if (domain.dom_family == 40):
612            out_string += GetVsockSocketAsString(so)
613        print(out_string)
614    else:
615        print("Unknown value passed as argument.")
616        return
617# EndMacro: showsocket
618
619def GetProcSockets(proc, total_snd_cc, total_rcv_cc, total_sock_fd):
620    """ Given a proc_t pointer, display information about its sockets
621    """
622    out_string = ""
623
624    if proc is None:
625        out_string += "Unknown value passed as argument."
626    else:
627        snd_cc = 0
628        rcv_cc = 0
629        sock_fd_seen = 0
630        """struct  filedesc *"""
631        proc_filedesc = addressof(proc.p_fd)
632        """struct  fileproc **"""
633        proc_ofiles = proc_filedesc.fd_ofiles
634        """ high-water mark of fd_ofiles """
635        if proc_filedesc.fd_nfiles != 0:
636            for fd in range(0, unsigned(proc_filedesc.fd_afterlast)):
637                if (unsigned(proc_ofiles[fd]) != 0 and proc_ofiles[fd].fp_glob != 0):
638                        fg = proc_ofiles[fd].fp_glob
639                        fg_data = Cast(fg.fg_data, 'void *')
640                        if (int(fg.fg_ops.fo_type) == 2):
641                            if (proc_filedesc.fd_ofileflags[fd] & 4):
642                                out_string += "U: "
643                            else:
644                                out_string += " "
645                            out_string += "fd = " + str(fd) + " "
646                            if (fg_data != 0):
647                                out_string += GetSocket(fg_data)
648                                out_string += "\n"
649
650                                so = Cast(fg_data, 'socket *')
651                                snd_cc += int(so.so_snd.sb_cc)
652                                total_snd_cc[0] += int(so.so_snd.sb_cc)
653                                rcv_cc += int(so.so_rcv.sb_cc)
654                                total_rcv_cc[0] += int(so.so_rcv.sb_cc)
655                                sock_fd_seen += 1
656                            else:
657                                out_string += ""
658        out_string += "total sockets " + str(int(sock_fd_seen)) + " snd_cc " + str(int(snd_cc)) + " rcv_cc " + str(int(rcv_cc)) + "\n"
659        total_sock_fd[0] = sock_fd_seen
660    return out_string
661
662
663# Macro: showprocsockets
664@lldb_command('showprocsockets')
665def ShowProcSockets(cmd_args=None):
666    """ Given a proc_t pointer, display information about its sockets
667    """
668    total_snd_cc = [0]
669    total_rcv_cc = [0]
670    sock_fd_seen = [0]
671    out_string = ""
672    if cmd_args != None and len(cmd_args) > 0 :
673        proc = kern.GetValueFromAddress(cmd_args[0], 'proc *')
674
675        if not proc:
676            print("Unknown value passed as argument.")
677            return
678        else:
679            print(GetProcInfo(proc))
680            print(GetProcSockets(proc, total_snd_cc, total_rcv_cc, sock_fd_seen))
681    else:
682        print("Missing argument 0 in user function.")
683# EndMacro: showprocsockets
684
685# Macro: showallprocsockets
686@lldb_command('showallprocsockets')
687def ShowAllProcSockets(cmd_args=None):
688    """Display information about the sockets of all the processes
689    """
690    total_snd_cc = [0]
691    total_rcv_cc = [0]
692    for proc in kern.procs:
693        sock_fd_seen = [0]
694        out_str = ""
695        out_str += GetProcSockets(proc, total_snd_cc, total_rcv_cc, sock_fd_seen)
696        if sock_fd_seen[0] != 0:
697            print("================================================================================")
698            print(GetProcInfo(proc))
699            print(out_str)
700    print ("total_snd_cc: " + str(int(total_snd_cc[0])) + " total_rcv_cc: " + str(int(total_rcv_cc[0])) + "\n")
701# EndMacro: showallprocsockets
702
703
704def GetRtEntryPrDetailsAsString(rte):
705    out_string = ""
706    rt = Cast(rte, 'rtentry *')
707    dst = Cast(rt.rt_nodes[0].rn_u.rn_leaf.rn_Key, 'sockaddr *')
708    isv6 = 0
709    dst_string_format = "{0:<18s}"
710    if (dst.sa_family == AF_INET):
711        out_string += dst_string_format.format(GetSocketAddrAsStringInet(dst)) + " "
712    else:
713        if (dst.sa_family == AF_INET6):
714            out_string += dst_string_format.format(GetSocketAddrAsStringInet6(dst)) + " "
715            isv6 = 1
716        else:
717            if (dst.sa_family == AF_LINK):
718                out_string += dst_string_format.format(GetSocketAddrAsStringLink(dst))
719                if (isv6 == 1):
720                    out_string += "                       "
721                else:
722                    out_string += " "
723            else:
724                out_string += dst_string_format.format(GetSocketAddrAsStringUnspec(dst)) + " "
725
726    gw = Cast(rt.rt_gateway, 'sockaddr *')
727    if (gw.sa_family == AF_INET):
728        out_string += dst_string_format.format(GetSocketAddrAsStringInet(gw)) + " "
729    else:
730        if (gw.sa_family == 30):
731            out_string += dst_string_format.format(GetSocketAddrAsStringInet6(gw)) + " "
732            isv6 = 1
733        else:
734            if (gw.sa_family == 18):
735                out_string += dst_string_format.format(GetSocketAddrAsStringLink(gw)) + " "
736                if (isv6 == 1):
737                    out_string += "                       "
738                else:
739                    out_string += " "
740            else:
741                dst_string_format.format(GetSocketAddrAsStringUnspec(gw))
742
743    if (rt.rt_flags & RTF_WASCLONED):
744        if (kern.ptrsize == 8):
745            rt_flags_string_format = "0x{0:<16x}"
746            out_string += rt_flags_string_format.format(rt.rt_parent) + " "
747        else:
748            rt_flags_string_format = "0x{0:<8x}"
749            out_string += rt_flags_string_format.format(rt.rt_parent) + " "
750    else:
751        if (kern.ptrsize == 8):
752            out_string += "                   "
753        else:
754            out_string += "           "
755
756    rt_refcnt_rmx_string_format = "{0:<d} {1:>10d}  "
757    out_string += rt_refcnt_rmx_string_format.format(rt.rt_refcnt, rt.rt_rmx.rmx_pksent) + "   "
758
759    rtf_string_format = "{0:>s}"
760    if (rt.rt_flags & RTF_UP):
761        out_string += rtf_string_format.format("U")
762    if (rt.rt_flags & RTF_GATEWAY):
763        out_string += rtf_string_format.format("G")
764    if (rt.rt_flags & RTF_HOST):
765        out_string += rtf_string_format.format("H")
766    if (rt.rt_flags & RTF_REJECT):
767        out_string += rtf_string_format.format("R")
768    if (rt.rt_flags & RTF_DYNAMIC):
769        out_string += rtf_string_format.format("D")
770    if (rt.rt_flags & RTF_MODIFIED):
771        out_string += rtf_string_format.format("M")
772    if (rt.rt_flags & RTF_CLONING):
773        out_string += rtf_string_format.format("C")
774    if (rt.rt_flags & RTF_PRCLONING):
775        out_string += rtf_string_format.format("c")
776    if (rt.rt_flags & RTF_LLINFO):
777        out_string += rtf_string_format.format("L")
778    if (rt.rt_flags & RTF_STATIC):
779        out_string += rtf_string_format.format("S")
780    if (rt.rt_flags & RTF_PROTO1):
781        out_string += rtf_string_format.format("1")
782    if (rt.rt_flags & RTF_PROTO2):
783        out_string += rtf_string_format.format("2")
784    if (rt.rt_flags & RTF_PROTO3):
785        out_string += rtf_string_format.format("3")
786    if (rt.rt_flags & RTF_WASCLONED):
787        out_string += rtf_string_format.format("W")
788    if (rt.rt_flags & RTF_BROADCAST):
789        out_string += rtf_string_format.format("b")
790    if (rt.rt_flags & RTF_MULTICAST):
791        out_string += rtf_string_format.format("m")
792    if (rt.rt_flags & RTF_XRESOLVE):
793        out_string += rtf_string_format.format("X")
794    if (rt.rt_flags & RTF_BLACKHOLE):
795        out_string += rtf_string_format.format("B")
796    if (rt.rt_flags & RTF_IFSCOPE):
797        out_string += rtf_string_format.format("I")
798    if (rt.rt_flags & RTF_CONDEMNED):
799        out_string += rtf_string_format.format("Z")
800    if (rt.rt_flags & RTF_IFREF):
801        out_string += rtf_string_format.format("i")
802    if (rt.rt_flags & RTF_PROXY):
803        out_string += rtf_string_format.format("Y")
804    if (rt.rt_flags & RTF_ROUTER):
805        out_string += rtf_string_format.format("r")
806
807    out_string +=  "/"
808    out_string += str(rt.rt_ifp.if_name)
809    out_string += str(int(rt.rt_ifp.if_unit))
810    out_string += "\n"
811    return out_string
812
813
814RNF_ROOT = 2
815def GetRtTableAsString(rt_tables):
816    out_string = ""
817    rn = Cast(rt_tables.rnh_treetop, 'radix_node *')
818    rnh_cnt = rt_tables.rnh_cnt
819
820    while (rn.rn_bit >= 0):
821        rn = rn.rn_u.rn_node.rn_L
822
823    while 1:
824        base = Cast(rn, 'radix_node *')
825        while ((rn.rn_parent.rn_u.rn_node.rn_R == rn) and (rn.rn_flags & RNF_ROOT == 0)):
826            rn = rn.rn_parent
827        rn = rn.rn_parent.rn_u.rn_node.rn_R
828        while (rn.rn_bit >= 0):
829            rn = rn.rn_u.rn_node.rn_L
830        next_rn = rn
831        while (base != 0):
832            rn = base
833            base = rn.rn_u.rn_leaf.rn_Dupedkey
834            if ((rn.rn_flags & RNF_ROOT) == 0):
835                rt = Cast(rn, 'rtentry *')
836                if (kern.ptrsize == 8):
837                    rtentry_string_format = "0x{0:<18x}"
838                    out_string += rtentry_string_format.format(rt) + " "
839                else:
840                    rtentry_string_format = "0x{0:<10x}"
841                    out_string += rtentry_string_format.format(rt) + " "
842                out_string += GetRtEntryPrDetailsAsString(rt) + " "
843
844        rn = next_rn
845        if ((rn.rn_flags & RNF_ROOT) != 0):
846            break
847    return out_string
848
849def GetRtInetAsString():
850    rt_tables = kern.globals.rt_tables[2]
851    if (kern.ptrsize == 8):
852        rt_table_header_format_string = "{0:<18s} {1: <16s} {2:<20s} {3:<16s} {4:<8s} {5:<8s} {6:<8s}"
853        print(rt_table_header_format_string.format("rtentry", " dst", "gw", "parent", "Refs", "Use", "flags/if"))
854        print(rt_table_header_format_string.format("-" * 18, "-" * 16, "-" * 16, "-" * 16, "-" * 8, "-" * 8, "-" * 8))
855        print(GetRtTableAsString(rt_tables))
856    else:
857        rt_table_header_format_string = "{0:<8s} {1:<16s} {2:<18s} {3:<8s} {4:<8s} {5:<8s} {6:<8s}"
858        print(rt_table_header_format_string.format("rtentry", "dst", "gw", "parent", "Refs", "Use", "flags/if"))
859        print(rt_table_header_format_string.format("-" * 8, "-" * 16, "-" * 16, "-" * 8, "-" * 8, "-" * 8, "-" * 8))
860        print(GetRtTableAsString(rt_tables))
861
862def GetRtInet6AsString():
863    rt_tables = kern.globals.rt_tables[30]
864    if (kern.ptrsize == 8):
865        rt_table_header_format_string = "{0:<18s} {1: <16s} {2:<20s} {3:<16s} {4:<8s} {5:<8s} {6:<8s}"
866        print(rt_table_header_format_string.format("rtentry", " dst", "gw", "parent", "Refs", "Use", "flags/if"))
867        print(rt_table_header_format_string.format("-" * 18, "-" * 16, "-" * 16, "-" * 16, "-" * 8, "-" * 8, "-" * 8))
868        print(GetRtTableAsString(rt_tables))
869    else:
870        rt_table_header_format_string = "{0:<8s} {1:<16s} {2:<18s} {3:<8s} {4:<8s} {5:<8s} {6:<8s}"
871        print(rt_table_header_format_string.format("rtentry", "dst", "gw", "parent", "Refs", "Use", "flags/if"))
872        print(rt_table_header_format_string.format("-" * 8, "-" * 16, "-" * 18, "-" * 8, "-" * 8, "-" * 8, "-" * 8))
873        print(GetRtTableAsString(rt_tables))
874
875# Macro: show_rt_inet
876@lldb_command('show_rt_inet')
877def ShowRtInet(cmd_args=None):
878    """ Display the IPv4 routing table
879    """
880    print(GetRtInetAsString())
881# EndMacro: show_rt_inet
882
883# Macro: show_rt_inet6
884@lldb_command('show_rt_inet6')
885def ShowRtInet6(cmd_args=None):
886    """ Display the IPv6 routing table
887    """
888    print(GetRtInet6AsString())
889# EndMacro: show_rt_inet6
890
891# Macro: rtentry_showdbg
892@lldb_command('rtentry_showdbg')
893def ShowRtEntryDebug(cmd_args=None):
894    """ Print the debug information of a route entry
895    """
896    if (cmd_args == None or len(cmd_args) == 0):
897            print("Missing argument 0 in user function.")
898            return
899    out_string = ""
900    cnt = 0
901    rtd = kern.GetValueFromAddress(cmd_args[0], 'rtentry_dbg *')
902    rtd_summary_format_string = "{0:s} {1:d}"
903    out_string += rtd_summary_format_string.format("Total holds : ", rtd.rtd_refhold_cnt) + "\n"
904    out_string += rtd_summary_format_string.format("Total releases : ", rtd.rtd_refrele_cnt) + "\n"
905
906    ix = 0
907    while (ix < CTRACE_STACK_SIZE):
908        kgm_pc = rtd.rtd_alloc.pc[ix]
909        if (kgm_pc != 0):
910            if (ix == 0):
911                out_string += "\nAlloc: (thread " + hex(rtd.rtd_alloc.th) + "):\n"
912            out_string += str(int(ix + 1)) + ": "
913            out_string += GetSourceInformationForAddress(kgm_pc)
914            out_string += "\n"
915        ix += 1
916
917    ix = 0
918    while (ix < CTRACE_STACK_SIZE):
919        kgm_pc = rtd.rtd_free.pc[ix]
920        if (kgm_pc != 0):
921            if (ix == 0):
922                out_string += "\nFree: (thread " + hex(rtd.rtd_free.th) + "):\n"
923            out_string += str(int(ix + 1)) + ": "
924            out_string += GetSourceInformationForAddress(kgm_pc)
925            out_string += "\n"
926        ix += 1
927
928    while (cnt < RTD_TRACE_HIST_SIZE):
929        ix = 0
930        while (ix < CTRACE_STACK_SIZE):
931            kgm_pc = rtd.rtd_refhold[cnt].pc[ix]
932            if (kgm_pc != 0):
933                if (ix == 0):
934                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(rtd.rtd_refhold[cnt].th) + "):\n"
935                out_string += str(int(ix + 1)) + ": "
936                out_string += GetSourceInformationForAddress(kgm_pc)
937                out_string += "\n"
938            ix += 1
939        cnt += 1
940
941    cnt = 0
942    while (cnt < RTD_TRACE_HIST_SIZE):
943        ix = 0
944        while (ix < CTRACE_STACK_SIZE):
945            kgm_pc = rtd.rtd_refrele[cnt].pc[ix]
946            if (kgm_pc != 0):
947                if (ix == 0):
948                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(rtd.rtd_refrele[cnt].th) + "):\n"
949                out_string += str(int(ix + 1)) + ": "
950                out_string += GetSourceInformationForAddress(kgm_pc)
951                out_string += "\n"
952            ix += 1
953        cnt += 1
954
955    out_string += "\nTotal locks : " + str(int(rtd.rtd_lock_cnt))
956    out_string += "\nTotal unlocks : " + str(int(rtd.rtd_unlock_cnt))
957
958    cnt = 0
959    while (cnt < RTD_TRACE_HIST_SIZE):
960        ix = 0
961        while (ix < CTRACE_STACK_SIZE):
962            kgm_pc = rtd.rtd_lock[cnt].pc[ix]
963            if (kgm_pc != 0):
964                if (ix == 0):
965                    out_string += "\nLock [" + str(int(cnt)) + "] (thread " + hex(rtd.rtd_lock[cnt].th) + "):\n"
966                out_string += str(int(ix + 1)) + ": "
967                out_string += GetSourceInformationForAddress(kgm_pc)
968                out_string += "\n"
969            ix += 1
970        cnt += 1
971
972    cnt = 0
973    while (cnt < RTD_TRACE_HIST_SIZE):
974        ix = 0
975        while (ix < CTRACE_STACK_SIZE):
976            kgm_pc = rtd.rtd_unlock[cnt].pc[ix]
977            if (kgm_pc != 0):
978                if (ix == 0):
979                    out_string += "\nUnlock [" + str(int(cnt)) + "] (thread " + hex(rtd.rtd_unlock[cnt].th) + "):\n"
980                out_string += str(int(ix + 1)) + ": "
981                out_string += GetSourceInformationForAddress(kgm_pc)
982                out_string += "\n"
983            ix += 1
984        cnt += 1
985
986    print(out_string)
987# EndMacro: rtentry_showdbg
988
989# Macro: inifa_showdbg
990@lldb_command('inifa_showdbg')
991def InIfaShowDebug(cmd_args=None):
992    """ Print the debug information of an IPv4 interface address
993    """
994    if (cmd_args == None or len(cmd_args) == 0):
995            print("Missing argument 0 in user function.")
996            return
997    out_string = ""
998    cnt = 0
999    inifa = kern.GetValueFromAddress(cmd_args[0], 'in_ifaddr_dbg *')
1000    in_ifaddr_summary_format_string = "{0:s} {1:d}"
1001    out_string += in_ifaddr_summary_format_string.format("Total holds : ", inifa.inifa_refhold_cnt) + "\n"
1002    out_string += in_ifaddr_summary_format_string.format("Total releases : ", inifa.inifa_refrele_cnt) + "\n"
1003
1004    ix = 0
1005    while (ix < CTRACE_STACK_SIZE):
1006        kgm_pc = inifa.inifa_alloc.pc[ix]
1007        if (kgm_pc != 0):
1008            if (ix == 0):
1009                out_string += "\nAlloc: (thread " + hex(inifa.inifa_alloc.th) + "):\n"
1010            out_string += str(int(ix + 1)) + ": "
1011            out_string += GetSourceInformationForAddress(kgm_pc)
1012            out_string += "\n"
1013        ix += 1
1014
1015    ix = 0
1016    while (ix < CTRACE_STACK_SIZE):
1017        kgm_pc = inifa.inifa_free.pc[ix]
1018        if (kgm_pc != 0):
1019            if (ix == 0):
1020                out_string += "\nFree: (thread " + hex(inifa.inifa_free.th) + "):\n"
1021            out_string += str(int(ix + 1)) + ": "
1022            out_string += GetSourceInformationForAddress(kgm_pc)
1023            out_string += "\n"
1024        ix += 1
1025
1026    while (cnt < INIFA_TRACE_HIST_SIZE):
1027        ix = 0
1028        while (ix < CTRACE_STACK_SIZE):
1029            kgm_pc = inifa.inifa_refhold[cnt].pc[ix]
1030            if (kgm_pc != 0):
1031                if (ix == 0):
1032                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(inifa.inifa_refhold[cnt].th) + "):\n"
1033                out_string += str(int(ix + 1)) + ": "
1034                out_string += GetSourceInformationForAddress(kgm_pc)
1035                out_string += "\n"
1036            ix += 1
1037        cnt += 1
1038    cnt = 0
1039
1040    while (cnt < INIFA_TRACE_HIST_SIZE):
1041        ix = 0
1042        while (ix < CTRACE_STACK_SIZE):
1043            kgm_pc = inifa.inifa_refrele[cnt].pc[ix]
1044            if (kgm_pc != 0):
1045                if (ix == 0):
1046                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(inifa.inifa_refrele[cnt].th) + "):\n"
1047                out_string += str(int(ix + 1)) + ": "
1048                out_string += GetSourceInformationForAddress(kgm_pc)
1049                out_string += "\n"
1050            ix += 1
1051        cnt += 1
1052    print(out_string)
1053# EndMacro: inifa_showdbg
1054
1055# Macro: in6ifa_showdbg
1056@lldb_command('in6ifa_showdbg')
1057def In6IfaShowDebug(cmd_args=None):
1058    """ Print the debug information of an IPv6 interface address
1059    """
1060    if (cmd_args == None or len(cmd_args) == 0):
1061            print("Missing argument 0 in user function.")
1062            return
1063    out_string = ""
1064    cnt = 0
1065    in6ifa = kern.GetValueFromAddress(cmd_args[0], 'in6_ifaddr_dbg *')
1066    in6_ifaddr_summary_format_string = "{0:s} {1:d}"
1067    print(in6_ifaddr_summary_format_string.format("Total holds : ", in6ifa.in6ifa_refhold_cnt))
1068    print(in6_ifaddr_summary_format_string.format("Total releases : ", in6ifa.in6ifa_refrele_cnt))
1069
1070    ix = 0
1071    while (ix < CTRACE_STACK_SIZE):
1072        kgm_pc = in6ifa.in6ifa_alloc.pc[ix]
1073        if (kgm_pc != 0):
1074            if (ix == 0):
1075                out_string += "\nAlloc: (thread " + hex(in6ifa.in6ifa_alloc.th) + "):\n"
1076            out_string += str(int(ix + 1)) + ": "
1077            out_string += GetSourceInformationForAddress(kgm_pc)
1078            out_string += "\n"
1079        ix += 1
1080
1081    ix = 0
1082    while (ix < CTRACE_STACK_SIZE):
1083        kgm_pc = in6ifa.in6ifa_free.pc[ix]
1084        if (kgm_pc != 0):
1085            if (ix == 0):
1086                out_string += "\nFree: (thread " + hex(in6ifa.in6ifa_free.th) + "):\n"
1087            out_string += str(int(ix + 1)) + ": "
1088            out_string += GetSourceInformationForAddress(kgm_pc)
1089            out_string += "\n"
1090        ix += 1
1091
1092    while (cnt < IN6IFA_TRACE_HIST_SIZE):
1093        ix = 0
1094        while (ix < CTRACE_STACK_SIZE):
1095            kgm_pc = in6ifa.in6ifa_refhold[cnt].pc[ix]
1096            if (kgm_pc != 0):
1097                if (ix == 0):
1098                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(in6ifa.in6ifa_refhold[cnt].th) + "):\n"
1099                out_string += str(int(ix + 1)) + ": "
1100                out_string += GetSourceInformationForAddress(kgm_pc)
1101                out_string += "\n"
1102            ix += 1
1103        cnt += 1
1104    cnt = 0
1105
1106    while (cnt < IN6IFA_TRACE_HIST_SIZE):
1107        ix = 0
1108        while (ix < CTRACE_STACK_SIZE):
1109            kgm_pc = in6ifa.in6ifa_refrele[cnt].pc[ix]
1110            if (kgm_pc != 0):
1111                if (ix == 0):
1112                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(in6ifa.in6ifa_refrele[cnt].th) + "):\n"
1113                out_string += str(int(ix + 1)) + ": "
1114                out_string += GetSourceInformationForAddress(kgm_pc)
1115                out_string += "\n"
1116            ix += 1
1117        cnt += 1
1118    print(out_string)
1119# EndMacro: in6ifa_showdbg
1120
1121# Macro: inm_showdbg
1122@lldb_command('inm_showdbg')
1123def InmShowDebug(cmd_args=None):
1124    """ Print the debug information of an IPv4 multicast address
1125    """
1126    if (cmd_args == None or len(cmd_args) == 0):
1127            print("Missing argument 0 in user function.")
1128            return
1129    out_string = ""
1130    cnt = 0
1131    inm = kern.GetValueFromAddress(cmd_args[0], 'in_multi_dbg *')
1132    in_multi_summary_format_string = "{0:s} {1:d}"
1133    out_string += in_multi_summary_format_string.format("Total holds : ", inm.inm_refhold_cnt)
1134    out_string += in_multi_summary_format_string.format("Total releases : ", inm.inm_refrele_cnt)
1135
1136    while (cnt < INM_TRACE_HIST_SIZE):
1137        ix = 0
1138        while (ix < CTRACE_STACK_SIZE):
1139            kgm_pc = inm.inm_refhold[cnt].pc[ix]
1140            if (kgm_pc != 0):
1141                if (ix == 0):
1142                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(inm.inm_refhold[cnt].th) + "):\n"
1143                out_string += str(int(ix + 1)) + ": "
1144                out_string += GetSourceInformationForAddress(kgm_pc)
1145                out_string += "\n"
1146            ix += 1
1147        cnt += 1
1148    cnt = 0
1149    while (cnt < INM_TRACE_HIST_SIZE):
1150        ix = 0
1151        while (ix < CTRACE_STACK_SIZE):
1152            kgm_pc = inm.inm_refrele[cnt].pc[ix]
1153            if (kgm_pc != 0):
1154                if (ix == 0):
1155                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(inm.inm_refrele[cnt].th) + "):\n"
1156                out_string += str(int(ix + 1)) + ": "
1157                out_string += GetSourceInformationForAddress(kgm_pc)
1158                out_string += "\n"
1159            ix += 1
1160        cnt += 1
1161    print(out_string)
1162# EndMacro: inm_showdbg
1163
1164# Macro: ifma_showdbg
1165@lldb_command('ifma_showdbg')
1166def IfmaShowDebug(cmd_args=None):
1167    """ Print the debug information of a link multicast address
1168    """
1169    if (cmd_args == None or len(cmd_args) == 0):
1170            print("Missing argument 0 in user function.")
1171            return
1172    out_string = ""
1173    cnt = 0
1174    ifma = kern.GetValueFromAddress(cmd_args[0], 'ifmultiaddr_dbg *')
1175    link_multi_summary_format_string = "{0:s} {1:d}"
1176    out_string += link_multi_summary_format_string.format("Total holds : ", ifma.ifma_refhold_cnt) + "\n"
1177    out_string += link_multi_summary_format_string.format("Total releases : ", ifma.ifma_refrele_cnt) + "\n"
1178
1179    while (cnt < IFMA_TRACE_HIST_SIZE):
1180        ix = 0
1181        while (ix < CTRACE_STACK_SIZE):
1182            kgm_pc = ifma.ifma_refhold[cnt].pc[ix]
1183            if (kgm_pc != 0):
1184                if (ix == 0):
1185                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(ifma.ifma_refhold[cnt].th) + "):\n"
1186                out_string += str(int(ix + 1)) + ": "
1187                out_string += GetSourceInformationForAddress(kgm_pc)
1188                out_string += "\n"
1189            ix += 1
1190        cnt += 1
1191    cnt = 0
1192    while (cnt < IFMA_TRACE_HIST_SIZE):
1193        ix = 0
1194        while (ix < CTRACE_STACK_SIZE):
1195            kgm_pc = ifma.ifma_refrele[cnt].pc[ix]
1196            if (kgm_pc != 0):
1197                if (ix == 0):
1198                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(ifma.ifma_refrele[cnt].th) + "):\n"
1199                out_string += str(int(ix + 1)) + ": "
1200                out_string += GetSourceInformationForAddress(kgm_pc)
1201                out_string += "\n"
1202            ix += 1
1203        cnt += 1
1204    print(out_string)
1205# EndMacro: ifma_showdbg
1206
1207# Macro: ifpref_showdbg
1208@lldb_command('ifpref_showdbg')
1209def IfpRefShowDebug(cmd_args=None):
1210    """ Print the debug information of an interface ref count
1211    """
1212    if (cmd_args == None or len(cmd_args) == 0):
1213            print("Missing argument 0 in user function.")
1214            return
1215    out_string = ""
1216    cnt = 0
1217    dl_if = kern.GetValueFromAddress(cmd_args[0], 'dlil_ifnet_dbg *')
1218    dl_if_summary_format_string = "{0:s} {1:d}"
1219    out_string +=  dl_if_summary_format_string.format("Total holds : ", dl_if.dldbg_if_refhold_cnt)
1220    out_string += dl_if_summary_format_string.format("Total releases : ", dl_if.dldbg_if_refrele_cnt)
1221
1222    while (cnt < IF_REF_TRACE_HIST_SIZE):
1223        ix = 0
1224        while (ix < CTRACE_STACK_SIZE):
1225            kgm_pc = dl_if.dldbg_if_refhold[cnt].pc[ix]
1226            if (kgm_pc != 0):
1227                if (ix == 0):
1228                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(dl_if.dldbg_if_refhold[cnt].th) + "):\n"
1229                out_string += str(int(ix + 1)) + ": "
1230                out_string += GetSourceInformationForAddress(kgm_pc)
1231                out_string += "\n"
1232            ix += 1
1233        cnt += 1
1234    cnt = 0
1235    while (cnt < IF_REF_TRACE_HIST_SIZE):
1236        ix = 0
1237        while (ix < CTRACE_STACK_SIZE):
1238            kgm_pc = dl_if.dldbg_if_refrele[cnt].pc[ix]
1239            if (kgm_pc != 0):
1240                if (ix == 0):
1241                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(dl_if.dldbg_if_refrele[cnt].th) + "):\n"
1242                out_string += str(int(ix + 1)) + ": "
1243                out_string += GetSourceInformationForAddress(kgm_pc)
1244                out_string += "\n"
1245            ix += 1
1246        cnt += 1
1247    print(out_string)
1248# EndMacro: ifpref_showdbg
1249
1250# Macro: ndpr_showdbg
1251@lldb_command('ndpr_showdbg')
1252def ndprShowDebug(cmd_args=None):
1253    """ Print the debug information of a nd_prefix structure
1254    """
1255    if (cmd_args == None or len(cmd_args) == 0):
1256            print("Missing argument 0 in user function.")
1257            return
1258    out_string = ""
1259    cnt = 0
1260    ndpr = kern.GetValueFromAddress(cmd_args[0], 'nd_prefix_dbg *')
1261    ndpr_summary_format_string = "{0:s} {1:d}"
1262    out_string += ndpr_summary_format_string.format("Total holds : ", ndpr.ndpr_refhold_cnt)
1263    out_string += ndpr_summary_format_string.format("Total releases : ", ndpr.ndpr_refrele_cnt)
1264
1265    while (cnt < NDPR_TRACE_HIST_SIZE):
1266        ix = 0
1267        while (ix < CTRACE_STACK_SIZE):
1268            kgm_pc = ndpr.ndpr_refhold[cnt].pc[ix]
1269            if (kgm_pc != 0):
1270                if (ix == 0):
1271                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(ndpr.ndpr_refhold[cnt].th) + "):\n"
1272                out_string += str(int(ix + 1)) + ": "
1273                out_string += GetSourceInformationForAddress(kgm_pc)
1274                out_string += "\n"
1275            ix += 1
1276        cnt += 1
1277    cnt = 0
1278    while (cnt < NDPR_TRACE_HIST_SIZE):
1279        ix = 0
1280        while (ix < CTRACE_STACK_SIZE):
1281            kgm_pc = ndpr.ndpr_refrele[cnt].pc[ix]
1282            if (kgm_pc != 0):
1283                if (ix == 0):
1284                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(ndpr.ndpr_refrele[cnt].th) + "):\n"
1285                out_string += str(int(ix + 1)) + ": "
1286                out_string += GetSourceInformationForAddress(kgm_pc)
1287                out_string += "\n"
1288            ix += 1
1289        cnt += 1
1290    print(out_string)
1291# EndMacro: ndpr_showdbg
1292
1293# Macro: nddr_showdbg
1294@lldb_command('nddr_showdbg')
1295def nddrShowDebug(cmd_args=None):
1296    """ Print the debug information of a nd_defrouter structure
1297    """
1298    if (cmd_args == None or len(cmd_args) == 0):
1299            print("Missing argument 0 in user function.")
1300            return
1301    out_string = ""
1302    cnt = 0
1303    nddr = kern.GetValueFromAddress(cmd_args[0], 'nd_defrouter_dbg *')
1304    nddr_summary_format_string = "{0:s} {1:d}"
1305    out_string += nddr_summary_format_string.format("Total holds : ", nddr.nddr_refhold_cnt)
1306    out_string += nddr_summary_format_string.format("Total releases : ", nddr.nddr_refrele_cnt)
1307
1308    while (cnt < NDDR_TRACE_HIST_SIZE):
1309        ix = 0
1310        while (ix < CTRACE_STACK_SIZE):
1311            kgm_pc = nddr.nddr_refhold[cnt].pc[ix]
1312            if (kgm_pc != 0):
1313                if (ix == 0):
1314                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(nddr.nddr_refhold[cnt].th) + "):\n"
1315                out_string += str(int(ix + 1)) + ": "
1316                out_string += GetSourceInformationForAddress(kgm_pc)
1317                out_string += "\n"
1318            ix += 1
1319        cnt += 1
1320    cnt = 0
1321    while (cnt < NDDR_TRACE_HIST_SIZE):
1322        ix = 0
1323        while (ix < CTRACE_STACK_SIZE):
1324            kgm_pc = nddr.nddr_refrele[cnt].pc[ix]
1325            if (kgm_pc != 0):
1326                if (ix == 0):
1327                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(nddr.nddr_refrele[cnt].th) + "):\n"
1328                out_string += str(int(ix + 1)) + ": "
1329                out_string += GetSourceInformationForAddress(kgm_pc)
1330                out_string += "\n"
1331            ix += 1
1332        cnt += 1
1333    print(out_string)
1334# EndMacro: nddr_showdbg
1335
1336# Macro: imo_showdbg
1337@lldb_command('imo_showdbg')
1338def IpmOptions(cmd_args=None):
1339    """ Print the debug information of a ip_moptions structure
1340    """
1341    if (cmd_args == None or len(cmd_args) == 0):
1342            print("Missing argument 0 in user function.")
1343            return
1344    out_string = ""
1345    cnt = 0
1346    imo = kern.GetValueFromAddress(cmd_args[0], 'ip_moptions_dbg *')
1347    imo_summary_format_string = "{0:s} {1:d}"
1348    out_string += imo_summary_format_string.format("Total holds : ", imo.imo_refhold_cnt)
1349    out_string += imo_summary_format_string.format("Total releases : ", imo.imo_refrele_cnt)
1350
1351    while (cnt < IMO_TRACE_HIST_SIZE):
1352        ix = 0
1353        while (ix < CTRACE_STACK_SIZE):
1354            kgm_pc = imo.imo_refhold[cnt].pc[ix]
1355            if (kgm_pc != 0):
1356                if (ix == 0):
1357                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(imo.imo_refhold[cnt].th) + "):\n"
1358                out_string += str(int(ix + 1)) + ": "
1359                out_string += GetSourceInformationForAddress(kgm_pc)
1360                out_string += "\n"
1361            ix += 1
1362        cnt += 1
1363    cnt = 0
1364    while (cnt < IMO_TRACE_HIST_SIZE):
1365        ix = 0
1366        while (ix < CTRACE_STACK_SIZE):
1367            kgm_pc = imo.imo_refrele[cnt].pc[ix]
1368            if (kgm_pc != 0):
1369                if (ix == 0):
1370                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(imo.imo_refrele[cnt].th) + "):\n"
1371                out_string += str(int(ix + 1)) + ": "
1372                out_string += GetSourceInformationForAddress(kgm_pc)
1373                out_string += "\n"
1374            ix += 1
1375        cnt += 1
1376    print(out_string)
1377# EndMacro: imo_showdbg
1378
1379# Macro: im6o_showdbg
1380@lldb_command('im6o_showdbg')
1381def IpmOptions(cmd_args=None):
1382    """ Print the debug information of a ip6_moptions structure
1383    """
1384    if (cmd_args == None or len(cmd_args) == 0):
1385            print("Missing argument 0 in user function.")
1386            return
1387    out_string = ""
1388    cnt = 0
1389    im6o = kern.GetValueFromAddress(cmd_args[0], 'ip6_moptions_dbg *')
1390    im6o_summary_format_string = "{0:s} {1:d}"
1391    out_string += im6o_summary_format_string.format("Total holds : ", im6o.im6o_refhold_cnt)
1392    out_string += im6o_summary_format_string.format("Total releases : ", im6o.im6o_refrele_cnt)
1393
1394    while (cnt < IM6O_TRACE_HIST_SIZE):
1395        ix = 0
1396        while (ix < CTRACE_STACK_SIZE):
1397            kgm_pc = im6o.im6o_refhold[cnt].pc[ix]
1398            if (kgm_pc != 0):
1399                if (ix == 0):
1400                    out_string += "\nHold [" + str(int(cnt)) + "] (thread " + hex(im6o.im6o_refhold[cnt].th) + "):\n"
1401                out_string += str(int(ix + 1)) + ": "
1402                out_string += GetSourceInformationForAddress(kgm_pc)
1403                out_string += "\n"
1404            ix += 1
1405        cnt += 1
1406    cnt = 0
1407    while (cnt < IM6O_TRACE_HIST_SIZE):
1408        ix = 0
1409        while (ix < CTRACE_STACK_SIZE):
1410            kgm_pc = im6o.im6o_refrele[cnt].pc[ix]
1411            if (kgm_pc != 0):
1412                if (ix == 0):
1413                    out_string += "\nRelease [" + str(int(cnt)) + "] (thread " + hex(im6o.im6o_refrele[cnt].th) + "):\n"
1414                out_string += str(int(ix + 1)) + ": "
1415                out_string += GetSourceInformationForAddress(kgm_pc)
1416                out_string += "\n"
1417            ix += 1
1418        cnt += 1
1419    print(out_string)
1420# EndMacro: im6o_showdbg
1421
1422# Macro: rtentry_trash
1423@lldb_command('rtentry_trash')
1424def RtEntryTrash(cmd_args=None):
1425    """ Walk the list of trash route entries
1426    """
1427    out_string = ""
1428    rt_trash_head = kern.globals.rttrash_head
1429    rtd = Cast(rt_trash_head.tqh_first, 'rtentry_dbg *')
1430    rt_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
1431    cnt = 0
1432    while (int(rtd) != 0):
1433        if (cnt == 0):
1434            if (kern.ptrsize == 8):
1435                print("                rtentry ref   hold   rele             dst    gw             parent flags/if\n")
1436                print("      ----------------- --- ------ ------ --------------- ----- ------------------ -----------\n")
1437            else:
1438                print("        rtentry ref   hold   rele             dst    gw     parent flags/if\n")
1439                print("      --------- --- ------ ------ --------------- ----- ---------- -----------\n")
1440        out_string += rt_trash_format_string.format(cnt, rtd, rtd.rtd_refhold_cnt - rtd.rtd_refrele_cnt, rtd.rtd_refhold_cnt, rtd.rtd_refrele_cnt) + "   "
1441        out_string += GetRtEntryPrDetailsAsString(rtd) + "\n"
1442        rtd = rtd.rtd_trash_link.tqe_next
1443        cnt += 1
1444    print(out_string)
1445# EndMacro: rtentry_trash
1446
1447# Macro: show_rtentry
1448@lldb_command('show_rtentry')
1449def ShRtEntry(cmd_args=None):
1450    """ Print rtentry.
1451    """
1452    out_string = ""
1453    rt = kern.GetValueFromAddress(cmd_args[0], 'rtentry *')
1454    out_string += GetRtEntryPrDetailsAsString(rt) + "\n"
1455    print(out_string)
1456# EndMacro: show_rtentry
1457
1458# Macro: inifa_trash
1459@lldb_command('inifa_trash')
1460def InIfaTrash(cmd_args=None):
1461    """ Walk the list of trash in_ifaddr entries
1462    """
1463    out_string = ""
1464    ifa_trash_head = kern.globals.inifa_trash_head
1465    ifa = Cast(ifa_trash_head.tqh_first, 'in_ifaddr_dbg *')
1466    inifa_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
1467    cnt = 0
1468    while (int(ifa) != 0):
1469        if (cnt == 0):
1470            if (kern.ptrsize == 8):
1471                print("                  in_ifa  ref   hold   rele")
1472                print("      ------------------  ---  ------  ----")
1473            else:
1474                print("          in_ifa  ref   hold   rele")
1475                print("      ----------  ---  ----- ------")
1476        out_string += inifa_trash_format_string.format(cnt + 1, ifa, ifa.inifa_refhold_cnt - ifa.inifa_refrele_cnt, ifa.inifa_refhold_cnt, ifa.inifa_refrele_cnt) + "   "
1477        out_string += GetSocketAddrAsStringInet(ifa.inifa.ia_ifa.ifa_addr) + "\n"
1478        ifa = ifa.inifa_trash_link.tqe_next
1479        cnt += 1
1480    print(out_string)
1481# EndMacro: inifa_trash
1482
1483# Macro: in6ifa_trash
1484@lldb_command('in6ifa_trash')
1485def In6IfaTrash(cmd_args=None):
1486    """ Walk the list of trash in6_ifaddr entries
1487    """
1488    out_string = ""
1489    in6ifa_trash_head = kern.globals.in6ifa_trash_head
1490    ifa = Cast(in6ifa_trash_head.tqh_first, 'in6_ifaddr_dbg *')
1491    in6ifa_trash_format_string = "{0:4d}: 0x{1:x} {2:3d} {3:6d} {4:6d}"
1492    cnt = 0
1493    while (int(ifa) != 0):
1494        if (cnt == 0):
1495            if (kern.ptrsize == 8):
1496                print("                 in6_ifa  ref   hold   rele")
1497                print("      ------------------  --- ------ ------")
1498            else:
1499                print("         in6_ifa  ref   hold   rele")
1500                print("      ----------  --- ------ ------")
1501        out_string += in6ifa_trash_format_string.format(cnt + 1, ifa, ifa.in6ifa_refhold_cnt - ifa.in6ifa_refrele_cnt, ifa.in6ifa_refhold_cnt, ifa.in6ifa_refrele_cnt) + "   "
1502        out_string += GetSocketAddrAsStringInet6(ifa.in6ifa.ia_ifa.ifa_addr) + "\n"
1503        ifa = ifa.in6ifa_trash_link.tqe_next
1504        cnt += 1
1505    print(out_string)
1506# EndMacro: in6ifa_trash
1507
1508# Macro: inm_trash
1509@lldb_command('inm_trash')
1510def InmTrash(cmd_args=None):
1511    """ Walk the list of trash in_multi entries
1512    """
1513    out_string = ""
1514    inm_trash_head = kern.globals.inm_trash_head
1515    inm = Cast(inm_trash_head.tqh_first, 'in_multi_dbg *')
1516    inm_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
1517    cnt = 0
1518    while (int(inm) != 0):
1519        if (cnt == 0):
1520            if (kern.ptrsize == 8):
1521                print("                     inm  ref   hold   rele")
1522                print("      ------------------  --- ------ ------")
1523            else:
1524                print("             inm  ref   hold   rele")
1525                print("      ----------  --- ------ ------")
1526        out_string += inm_trash_format_string.format(cnt + 1, inm, inm.inm_refhold_cnt - inm.inm_refrele_cnt, inm.inm_refhold_cnt, inm.inm_refrele_cnt) + "   "
1527        out_string += GetInAddrAsString(addressof(inm.inm.inm_addr)) + "\n"
1528        inm = inm.inm_trash_link.tqe_next
1529        cnt += 1
1530    print(out_string)
1531# EndMacro: inm_trash
1532
1533# Macro: in6m_trash
1534@lldb_command('in6m_trash')
1535def In6mTrash(cmd_args=None):
1536    """ Walk the list of trash in6_multi entries
1537    """
1538    out_string = ""
1539    in6m_trash_head = kern.globals.in6m_trash_head
1540    in6m = Cast(in6m_trash_head.tqh_first, 'in6_multi_dbg *')
1541    in6m_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
1542    cnt = 0
1543    while (int(in6m) != 0):
1544        if (cnt == 0):
1545            if (kern.ptrsize == 8):
1546                print("                    in6m  ref   hold   rele")
1547                print("      ------------------  --- ------ ------")
1548            else:
1549                print("            in6m  ref   hold   rele")
1550                print("      ----------  --- ------ ------")
1551        out_string += in6m_trash_format_string.format(cnt + 1, in6m, in6m.in6m_refhold_cnt - in6m.in6m_refrele_cnt, in6m.in6m_refhold_cnt, in6m.in6m_refrele_cnt) + "   "
1552        out_string += GetIn6AddrAsString(addressof(in6m.in6m.in6m_addr)) + "\n"
1553        in6m = in6m.in6m_trash_link.tqe_next
1554        cnt += 1
1555    print(out_string)
1556# EndMacro: in6m_trash
1557
1558# Macro: ifma_trash
1559@lldb_command('ifma_trash')
1560def IfmaTrash(cmd_args=None):
1561    """ Walk the list of trash ifmultiaddr entries
1562    """
1563    out_string = ""
1564    ifma_trash_head = kern.globals.ifma_trash_head
1565    ifma = Cast(ifma_trash_head.tqh_first, 'ifmultiaddr_dbg *')
1566    ifma_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
1567    cnt = 0
1568    while (int(ifma) != 0):
1569        if (cnt == 0):
1570            if (kern.ptrsize == 8):
1571                print("                    ifma  ref   hold   rele")
1572                print("      ------------------  --- ------ ------")
1573            else:
1574                print("            ifma  ref   hold   rele")
1575                print("      ----------  --- ------ ------")
1576        out_string += ifma_trash_format_string.format(cnt + 1, ifma, ifma.ifma_refhold_cnt - ifma.ifma_refrele_cnt, ifma.ifma_refhold_cnt, ifma.ifma_refrele_cnt) + "   "
1577        out_string += GetSocketAddrAsString(ifma.ifma.ifma_addr) + "\n"
1578        out_string += " @ " + ifma.ifma.ifma_ifp.if_xname
1579        ifma = ifma.ifma_trash_link.tqe_next
1580        cnt += 1
1581    print(out_string)
1582# EndMacro: ifma_trash
1583
1584def GetInPcb(pcb, proto):
1585    out_string = ""
1586    out_string += hex(pcb)
1587
1588    if (proto == IPPROTO_TCP):
1589        out_string +=  " tcp"
1590    elif (proto == IPPROTO_UDP):
1591        out_string += " udp"
1592    elif (proto == IPPROTO_RAW):
1593        out_string += " raw"
1594    else:
1595        out_string += str(proto) +  "."
1596
1597    if (pcb.inp_vflag & INP_IPV4):
1598        out_string += "4 "
1599    if (pcb.inp_vflag & INP_IPV6):
1600        out_string += "6 "
1601
1602    if (pcb.inp_vflag & INP_IPV4):
1603        out_string += "                                       "
1604        out_string += GetInAddrAsString(addressof(pcb.inp_dependladdr.inp46_local.ia46_addr4))
1605    else:
1606        out_string += "  "
1607        out_string += GetIn6AddrAsString((pcb.inp_dependladdr.inp6_local.__u6_addr.__u6_addr8))
1608
1609    out_string += " "
1610    out_string += Getntohs(pcb.inp_lport)
1611    out_string += " "
1612
1613    if (pcb.inp_vflag & INP_IPV4):
1614        out_string += "                                 "
1615        out_string += GetInAddrAsString(addressof(pcb.inp_dependfaddr.inp46_foreign.ia46_addr4))
1616    else:
1617        out_string += GetIn6AddrAsString((pcb.inp_dependfaddr.inp6_foreign.__u6_addr.__u6_addr8))
1618
1619    out_string += " "
1620    out_string += Getntohs(pcb.inp_fport)
1621    out_string += " "
1622
1623    if (proto == IPPROTO_TCP):
1624        out_string += GetTcpState(pcb.inp_ppcb)
1625
1626    out_string += "\n\t"
1627    if (pcb.inp_flags & INP_RECVOPTS):
1628        out_string += "recvopts "
1629    if (pcb.inp_flags & INP_RECVRETOPTS):
1630        out_string += "recvretopts "
1631    if (pcb.inp_flags & INP_RECVDSTADDR):
1632        out_string += "recvdstaddr "
1633    if (pcb.inp_flags & INP_HDRINCL):
1634        out_string += "hdrincl "
1635    if (pcb.inp_flags & INP_HIGHPORT):
1636        out_string += "highport "
1637    if (pcb.inp_flags & INP_LOWPORT):
1638        out_string += "lowport "
1639    if (pcb.inp_flags & INP_ANONPORT):
1640        out_string += "anonport "
1641    if (pcb.inp_flags & INP_RECVIF):
1642        out_string += "recvif "
1643    if (pcb.inp_flags & INP_MTUDISC):
1644        out_string += "mtudisc "
1645    if (pcb.inp_flags & INP_STRIPHDR):
1646        out_string += "striphdr "
1647    if (pcb.inp_flags & INP_RECV_ANYIF):
1648        out_string += "recv_anyif "
1649    if (pcb.inp_flags & INP_INADDR_ANY):
1650        out_string += "inaddr_any "
1651    if (pcb.inp_flags & INP_RECVTTL):
1652        out_string += "recvttl "
1653    if (pcb.inp_flags & INP_UDP_NOCKSUM):
1654        out_string += "nocksum "
1655    if (pcb.inp_flags & INP_BOUND_IF):
1656        out_string += "boundif "
1657    if (pcb.inp_flags & IN6P_IPV6_V6ONLY):
1658        out_string += "v6only "
1659    if (pcb.inp_flags & IN6P_PKTINFO):
1660        out_string += "pktinfo "
1661    if (pcb.inp_flags & IN6P_HOPLIMIT):
1662        out_string += "hoplimit "
1663    if (pcb.inp_flags & IN6P_HOPOPTS):
1664        out_string += "hopopts "
1665    if (pcb.inp_flags & IN6P_DSTOPTS):
1666        out_string += "dstopts "
1667    if (pcb.inp_flags & IN6P_RTHDR):
1668        out_string += "rthdr "
1669    if (pcb.inp_flags & IN6P_RTHDRDSTOPTS):
1670        out_string += "rthdrdstopts "
1671    if (pcb.inp_flags & IN6P_TCLASS):
1672        out_string += "rcv_tclass "
1673    if (pcb.inp_flags & IN6P_AUTOFLOWLABEL):
1674        out_string += "autoflowlabel "
1675    if (pcb.inp_flags & IN6P_BINDV6ONLY):
1676        out_string += "bindv6only "
1677    if (pcb.inp_flags & IN6P_RFC2292):
1678        out_string += "RFC2292 "
1679    if (pcb.inp_flags & IN6P_MTU):
1680        out_string += "rcv_pmtu "
1681    if (pcb.inp_flags & INP_PKTINFO):
1682        out_string += "pktinfo "
1683    if (pcb.inp_flags & INP_FLOW_SUSPENDED):
1684        out_string += "suspended "
1685    if (pcb.inp_flags & INP_NO_IFT_CELLULAR):
1686        out_string += "nocellular "
1687    if (pcb.inp_flags & INP_FLOW_CONTROLLED):
1688        out_string += "flowctld "
1689    if (pcb.inp_flags & INP_FC_FEEDBACK):
1690        out_string += "fcfeedback "
1691    if (pcb.inp_flags2 & INP2_TIMEWAIT):
1692        out_string += "timewait "
1693    if (pcb.inp_flags2 & INP2_IN_FCTREE):
1694        out_string += "in_fctree "
1695    if (pcb.inp_flags2 & INP2_WANT_APP_POLICY):
1696        out_string += "want_app_policy "
1697
1698    out_string += "\n\t"
1699    so = pcb.inp_socket
1700    if (so != 0):
1701        out_string += "so=" + str(so) + " s=" + str(int(so.so_snd.sb_cc)) + " r=" + str(int(so.so_rcv.sb_cc))
1702        if proto == IPPROTO_TCP :
1703            tcpcb = cast(pcb.inp_ppcb, 'tcpcb *')
1704            out_string += " reass=" + str(int(tcpcb.t_reassqlen))
1705
1706        out_string += " usecnt=" + str(int(so.so_usecount)) + ", "
1707
1708    if (pcb.inp_state == 0 or pcb.inp_state == INPCB_STATE_INUSE):
1709        out_string += "inuse"
1710    else:
1711        if (pcb.inp_state == INPCB_STATE_DEAD):
1712            out_string += "dead"
1713        else:
1714            out_string += "unknown (" + str(int(pcb.inp_state)) + ")"
1715
1716    return out_string
1717
1718def CalcMbufInList(mpkt, pkt_cnt, buf_byte_cnt, mbuf_cnt, mbuf_cluster_cnt):
1719    while (mpkt != 0):
1720        mp = mpkt
1721        mpkt = mpkt.m_hdr.mh_nextpkt
1722        pkt_cnt[0] +=1
1723        while (mp != 0):
1724            mbuf_cnt[0] += 1
1725            buf_byte_cnt[int(mp.m_hdr.mh_type)] += 256
1726            buf_byte_cnt[Mbuf_Type.MT_LAST] += 256
1727            if (mp.m_hdr.mh_flags & 0x01):
1728                mbuf_cluster_cnt[0] += 1
1729                buf_byte_cnt[int(mp.m_hdr.mh_type)] += mp.M_dat.MH.MH_dat.MH_ext.ext_size
1730                buf_byte_cnt[Mbuf_Type.MT_LAST] += mp.M_dat.MH.MH_dat.MH_ext.ext_size
1731            mp = mp.m_hdr.mh_next
1732
1733def CalcMbufInSB(so, snd_cc, snd_buf, rcv_cc, rcv_buf, snd_record_cnt, rcv_record_cnt, snd_mbuf_cnt, rcv_mbuf_cnt, snd_mbuf_cluster_cnt, rcv_mbuf_cluster_cnt):
1734    snd_cc[0] += so.so_snd.sb_cc
1735    mpkt = so.so_snd.sb_mb
1736    CalcMbufInList(mpkt, snd_record_cnt, snd_buf, snd_mbuf_cnt, snd_mbuf_cluster_cnt)
1737    rcv_cc[0] += so.so_rcv.sb_cc
1738    mpkt = so.so_rcv.sb_mb
1739    CalcMbufInList(mpkt, rcv_record_cnt, rcv_buf, rcv_mbuf_cnt, rcv_mbuf_cluster_cnt)
1740
1741def GetMptcpInfo():
1742    mptcp = kern.globals.mtcbinfo
1743
1744    mppcb = cast(mptcp.mppi_pcbs.tqh_first, 'mppcb *')
1745    pcbseen = 0
1746    reinject_cnt=[0]
1747    reinject_byte_cnt=[0] * (Mbuf_Type.MT_LAST + 1)
1748    reinject_mbuf_cnt=[0]
1749    reinject_mbuf_cluster_cnt=[0]
1750
1751    snd_mbuf_cnt = [0]
1752    snd_mbuf_cluster_cnt = [0]
1753    snd_record_cnt = [0]
1754    snd_cc = [0]
1755    snd_buf = [0] * (Mbuf_Type.MT_LAST + 1)
1756    rcv_mbuf_cnt = [0]
1757    rcv_mbuf_cluster_cnt = [0]
1758    rcv_record_cnt = [0]
1759    rcv_cc = [0]
1760    rcv_buf = [0] * (Mbuf_Type.MT_LAST + 1)
1761    total_mbuf_bytes = 0
1762    while mppcb != 0:
1763        mpte = mppcb.mpp_pcbe
1764        pcbseen += 1
1765        CalcMbufInList(mpte.mpte_reinjectq, reinject_cnt, reinject_byte_cnt, reinject_mbuf_cnt, reinject_mbuf_cluster_cnt)
1766
1767        socket = mppcb.mpp_socket
1768        if socket != 0:
1769            CalcMbufInSB(socket, snd_cc, snd_buf, rcv_cc, rcv_buf, snd_record_cnt, rcv_record_cnt, snd_mbuf_cnt, rcv_mbuf_cnt, snd_mbuf_cluster_cnt, rcv_mbuf_cluster_cnt)
1770
1771        mppcb = cast(mppcb.mpp_entry.tqe_next, 'mppcb *')
1772
1773    out_string = ""
1774    out_string += "total pcbs seen: " + str(int(pcbseen)) + "\n"
1775    out_string += "total reinject mbuf count: " + str(int(reinject_mbuf_cnt[0])) + "\n"
1776    out_string += "total reinject mbuf cluster count: " + str(int(reinject_mbuf_cluster_cnt[0])) + "\n"
1777    out_string += "total reinject record count: " + str(int(reinject_cnt[0])) + "\n"
1778    for x in range(Mbuf_Type.MT_LAST):
1779        if (reinject_byte_cnt[x] != 0):
1780            out_string += "total reinject bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(reinject_byte_cnt[x])) + "\n"
1781            total_mbuf_bytes += reinject_byte_cnt[x]
1782
1783
1784    out_string += "total send mbuf count: " + str(int(snd_mbuf_cnt[0])) + " receive mbuf count: " + str(int(rcv_mbuf_cnt[0])) + "\n"
1785    out_string += "total send mbuf cluster count: " + str(int(snd_mbuf_cluster_cnt[0])) + " receive mbuf cluster count: " + str(int(rcv_mbuf_cluster_cnt[0])) + "\n"
1786    out_string += "total send record count: " + str(int(snd_record_cnt[0])) + " receive record count: " + str(int(rcv_record_cnt[0])) + "\n"
1787    out_string += "total snd_cc (total bytes in send buffers): " + str(int(snd_cc[0])) + " rcv_cc (total bytes in receive buffers): " + str(int(rcv_cc[0])) + "\n"
1788    out_string += "total snd_buf bytes " + str(int(snd_buf[Mbuf_Type.MT_LAST])) + " rcv_buf bytes " + str(int(rcv_buf[Mbuf_Type.MT_LAST])) + "\n"
1789    for x in range(Mbuf_Type.MT_LAST):
1790        if (snd_buf[x] != 0 or rcv_buf[x] != 0):
1791            out_string += "total snd_buf bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(snd_buf[x])) + " total recv_buf bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(rcv_buf[x])) + "\n"
1792            total_mbuf_bytes += snd_buf[x] + rcv_buf[x]
1793
1794    out_string += "total mbuf bytes used by MPTCP: "+ str(total_mbuf_bytes) + "\n"
1795    print(out_string)
1796
1797    #pcb =
1798
1799def GetPcbInfo(pcbi, proto):
1800    tcp_reassqlen = [0]
1801    tcp_reassq_bytes = 0
1802    mbuf_reassq_cnt = [0]
1803    mbuf_reassq_bytes = [0] * (Mbuf_Type.MT_LAST + 1)
1804    mbuf_reassq_cluster = [0]
1805    out_string = ""
1806    snd_mbuf_cnt = [0]
1807    snd_mbuf_cluster_cnt = [0]
1808    snd_record_cnt = [0]
1809    snd_cc = [0]
1810    snd_buf = [0] * (Mbuf_Type.MT_LAST + 1)
1811    rcv_mbuf_cnt = [0]
1812    rcv_mbuf_cluster_cnt = [0]
1813    rcv_record_cnt = [0]
1814    rcv_cc = [0]
1815    rcv_buf = [0] * (Mbuf_Type.MT_LAST + 1)
1816    pcbseen = 0
1817    out_string += "lastport " + str(int(pcbi.ipi_lastport)) + " lastlow " + str(int(pcbi.ipi_lastlow)) + " lasthi " + str(int(pcbi.ipi_lasthi)) + "\n"
1818    out_string += "active pcb count is " + str(int(pcbi.ipi_count)) + "\n"
1819    hashsize = pcbi.ipi_hashmask + 1
1820    out_string += "hash size is " + str(int(hashsize)) + "\n"
1821    out_string += str(pcbi.ipi_hashbase) + " has the following inpcb(s):\n"
1822    if (kern.ptrsize == 8):
1823        out_string += "pcb                proto  source                                        port  destination                                 port\n"
1824    else:
1825        out_string += "pcb            proto  source           address  port  destination         address  port\n\n"
1826
1827    if proto == IPPROTO_RAW:
1828        head = cast(pcbi.ipi_listhead, 'inpcbhead *')
1829        pcb = cast(head.lh_first, 'inpcb *')
1830        while pcb != 0:
1831            pcbseen += 1
1832            out_string += GetInPcb(pcb, proto) + "\n"
1833            so = pcb.inp_socket
1834            if so != 0:
1835                CalcMbufInSB(so, snd_cc, snd_buf, rcv_cc, rcv_buf, snd_record_cnt, rcv_record_cnt, snd_mbuf_cnt, rcv_mbuf_cnt, snd_mbuf_cluster_cnt, rcv_mbuf_cluster_cnt)
1836            pcb = cast(pcb.inp_list.le_next, 'inpcb *')
1837    else:
1838        i = 0
1839        hashbase = pcbi.ipi_hashbase
1840        while (i < hashsize):
1841            head = hashbase[i]
1842            pcb = cast(head.lh_first, 'inpcb *')
1843            while pcb != 0:
1844                pcbseen += 1
1845                out_string += GetInPcb(pcb, proto) + "\n"
1846                so = pcb.inp_socket
1847                if so != 0:
1848                    CalcMbufInSB(so, snd_cc, snd_buf, rcv_cc, rcv_buf, snd_record_cnt, rcv_record_cnt, snd_mbuf_cnt, rcv_mbuf_cnt, snd_mbuf_cluster_cnt, rcv_mbuf_cluster_cnt)
1849                if proto == IPPROTO_TCP and pcb.inp_ppcb:
1850                    tcpcb = cast(pcb.inp_ppcb, 'tcpcb *')
1851                    reass_entry = cast(tcpcb.t_segq.lh_first, 'tseg_qent *')
1852                    curr_reass = 0
1853                    while reass_entry != 0:
1854                        CalcMbufInList(reass_entry.tqe_m, tcp_reassqlen, mbuf_reassq_bytes, mbuf_reassq_cnt, mbuf_reassq_cluster)
1855                        tcp_reassq_bytes += reass_entry.tqe_len
1856                        curr_reass += reass_entry.tqe_len
1857
1858                        reass_entry = reass_entry.tqe_q.le_next
1859
1860                pcb = cast(pcb.inp_hash.le_next, 'inpcb *')
1861            i += 1
1862
1863    out_string += "total pcbs seen: " + str(int(pcbseen)) + "\n"
1864    out_string += "total send mbuf count: " + str(int(snd_mbuf_cnt[0])) + " receive mbuf count: " + str(int(rcv_mbuf_cnt[0])) + "\n"
1865    out_string += "total send mbuf cluster count: " + str(int(snd_mbuf_cluster_cnt[0])) + " receive mbuf cluster count: " + str(int(rcv_mbuf_cluster_cnt[0])) + "\n"
1866    out_string += "total send record count: " + str(int(snd_record_cnt[0])) + " receive record count: " + str(int(rcv_record_cnt[0])) + "\n"
1867    out_string += "total snd_cc (total bytes in send buffers): " + str(int(snd_cc[0])) + " rcv_cc (total bytes in receive buffers): " + str(int(rcv_cc[0])) + "\n"
1868    out_string += "total snd_buf bytes " + str(int(snd_buf[Mbuf_Type.MT_LAST])) + " rcv_buf bytes " + str(int(rcv_buf[Mbuf_Type.MT_LAST])) + "\n"
1869    for x in range(Mbuf_Type.MT_LAST):
1870        if (snd_buf[x] != 0 or rcv_buf[x] != 0):
1871            out_string += "total snd_buf bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(snd_buf[x])) + " total recv_buf bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(rcv_buf[x])) + "\n"
1872    out_string += "port hash base is " + hex(pcbi.ipi_porthashbase) + "\n"
1873    if proto == IPPROTO_TCP:
1874        out_string += "TCP reassembly queue length: " + str(tcp_reassqlen[0]) + " TCP-payload bytes: " + str(tcp_reassq_bytes) + "\n"
1875
1876        for x in range(Mbuf_Type.MT_LAST):
1877            if mbuf_reassq_bytes[x] != 0:
1878                out_string += "total reassq bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(mbuf_reassq_bytes[x]) + "\n"
1879
1880    i = 0
1881    hashbase = pcbi.ipi_porthashbase
1882    while (i < hashsize):
1883        head = hashbase[i]
1884        pcb = cast(head.lh_first, 'inpcbport *')
1885        while pcb != 0:
1886            out_string += "\t"
1887            out_string += GetInPcbPort(pcb)
1888            out_string += "\n"
1889            pcb = cast(pcb.phd_hash.le_next, 'inpcbport *')
1890        i += 1
1891
1892    return out_string
1893
1894def GetInPcbPort(ppcb):
1895    out_string = ""
1896    out_string += hex(ppcb) + ": lport "
1897    out_string += Getntohs(ppcb.phd_port)
1898    return out_string
1899
1900
1901def Getntohs(port):
1902    out_string = ""
1903    #p = unsigned(int(port) & 0x0000ffff)
1904    p = ((port & 0x0000ff00) >> 8)
1905    p |= ((port & 0x000000ff) << 8)
1906    return str(p)
1907
1908
1909# Macro: mbuf_list_usage_summary
1910@lldb_command('mbuf_list_usage_summary')
1911def ShowMbufListUsageSummary(cmd_args=None):
1912    """ Print mbuf list usage summary
1913    """
1914    out_string = ""
1915    pkt_cnt = [0]
1916    buf_byte_cnt = [0] * (Mbuf_Type.MT_LAST + 1)
1917    mbuf_cnt = [0]
1918    mbuf_cluster_cnt = [0]
1919
1920    mpkt = kern.GetValueFromAddress(cmd_args[0], 'struct mbuf *')
1921    CalcMbufInList(mpkt, pkt_cnt, buf_byte_cnt, mbuf_cnt, mbuf_cluster_cnt)
1922
1923    out_string += "Total packet count is " + str(int(pkt_cnt[0])) + "\n"
1924    for x in range(Mbuf_Type.MT_LAST):
1925        if (buf_byte_cnt[x] != 0):
1926            out_string += "Total buf bytes of type " + Mbuf_Type.reverse_mapping[x] + " : " + str(int(buf_byte_cnt[x])) + "\n"
1927    out_string += "Total mbuf count " + str(int(mbuf_cnt[0])) + "\n"
1928    out_string += "Total mbuf cluster count " + str(int(mbuf_cluster_cnt[0])) + "\n"
1929    print(out_string)
1930
1931# Macro: show_kern_event_pcbinfo
1932def GetKernEventPcbInfo(kev_pcb_head):
1933    out_string = ""
1934    pcb = Cast(kev_pcb_head.lh_first, 'kern_event_pcb *')
1935    if (kern.ptrsize == 8):
1936        kev_pcb_format_string = "0x{0:<16x} {1:12d} {2:16d} {3:16d} {4:16d} {5:16d}"
1937        out_string += "  evp socket         vendor code      class filter      subclass filter     so_rcv.sb_cc      so_rcv.sb_mbcnt\n"
1938        out_string += "--------------       -----------      ------------      ---------------     ------------      ---------------\n"
1939    else:
1940        kev_pcb_format_string = "0x{0:<8x} {1:12d} {2:16d} {3:16d} {4:16d} {5:16d}"
1941        out_string += "evp socket       vendor code      class filter      subclass filter     so_rcv.sb_cc      so_rcv.sb_mbcnt\n"
1942        out_string += "----------       -----------      ------------      ---------------     ------------      ---------------\n"
1943    while (pcb != 0):
1944        out_string += kev_pcb_format_string.format(pcb.evp_socket, pcb.evp_vendor_code_filter, pcb.evp_class_filter, pcb.evp_subclass_filter, pcb.evp_socket.so_rcv.sb_cc, pcb.evp_socket.so_rcv.sb_mbcnt)
1945        out_string += "\n"
1946        pcb = pcb.evp_link.le_next
1947    return out_string
1948
1949@lldb_command('show_kern_event_pcbinfo')
1950def ShowKernEventPcbInfo(cmd_args=None):
1951    """ Display the list of Kernel Event protocol control block information
1952    """
1953    print(GetKernEventPcbInfo(addressof(kern.globals.kern_event_head)))
1954# EndMacro:  show_kern_event_pcbinfo
1955
1956# Macro: show_kern_control_pcbinfo
1957def GetKernControlPcbInfo(ctl_head):
1958    out_string = ""
1959    kctl = Cast(ctl_head.tqh_first, 'kctl *')
1960    if (kern.ptrsize == 8):
1961        kcb_format_string = "0x{0:<16x} {1:10d} {2:10d} {3:10d}\n"
1962    else:
1963        kcb_format_string = "0x{0:<8x} {1:10d} {2:10d} {3:10d}\n"
1964    while unsigned(kctl) != 0:
1965        kctl_name = "controller: " + str(kctl.name) + "\n"
1966        out_string += kctl_name
1967        kcb = Cast(kctl.kcb_head.tqh_first, 'ctl_cb *')
1968        if unsigned(kcb) != 0:
1969            if (kern.ptrsize == 8):
1970                out_string += "socket               usecount     snd_cc     rcv_cc\n"
1971                out_string += "------               --------     ------     ------\n"
1972            else:
1973                out_string += "socket       usecount     snd_cc     rcv_cc\n"
1974                out_string += "------       --------     ------     ------\n"
1975        while unsigned(kcb) != 0:
1976            so = Cast(kcb.so, 'socket *')
1977            snd_cc = so.so_snd.sb_cc
1978            rcv_cc = so.so_rcv.sb_cc
1979            out_string += kcb_format_string.format(kcb.so, kcb.usecount, snd_cc, rcv_cc)
1980            kcb = kcb.next.tqe_next
1981        out_string += "\n"
1982        kctl = kctl.next.tqe_next
1983    return out_string
1984
1985@lldb_command('show_kern_control_pcbinfo')
1986def ShowKernControlPcbInfo(cmd_args=None):
1987    """ Display the list of Kernel Control protocol control block information
1988    """
1989    print(GetKernControlPcbInfo(addressof(kern.globals.ctl_head)))
1990# EndMacro:  show_kern_control_pcbinfo
1991
1992# Macro: show_unix_domain_pcbinfo
1993def GetUnixDomainPCBAsString(unp, type) :
1994    out_string = ""
1995    pcb = Cast(unp, 'unpcb *')
1996    out_string += "unpcb: " + hex(pcb)  + " " + str(type)
1997    out_string += " unp_socket: " + hex(pcb.unp_socket)
1998    out_string += " unp_vnode: " + hex(pcb.unp_vnode)
1999    out_string += " unp_conn: " + hex(pcb.unp_conn)
2000    out_string += " unp_addr: " + GetSocketAddrAsStringUnix(pcb.unp_addr)
2001    out_string += " unp_gencnt: " + str(int(pcb.unp_gencnt))
2002    out_string += " unp_flags: " + hex(pcb.unp_flags)
2003    if pcb.unp_socket != 0:
2004        so = Cast(pcb.unp_socket, 'socket *')
2005        out_string += " s=" + str(int(so.so_snd.sb_cc)) + " r=" + str(int(so.so_rcv.sb_cc)) + " usecnt=" + str(int(so.so_usecount))
2006    return out_string
2007
2008def GetUnixDomainPcbInfo(unp_head, type):
2009    out_string = ""
2010    unp = Cast(unp_head.lh_first, 'unpcb *')
2011    while unsigned(unp) != 0:
2012        out_string += GetUnixDomainPCBAsString(unp, type)
2013        out_string += "\n"
2014        unp = unp.unp_link.le_next
2015    return out_string
2016
2017@lldb_command('show_unix_domain_pcbinfo')
2018def ShowUnixDomainPcbInfo(cmd_args=None):
2019    """ Display the list of unix domain pcb
2020    """
2021    print(GetUnixDomainPcbInfo(addressof(kern.globals.unp_dhead), "dgram"))
2022    print(GetUnixDomainPcbInfo(addressof(kern.globals.unp_shead), "stream"))
2023# EndMacro:  show_kern_control_pcbinfo
2024
2025# Macro: show_tcp_pcbinfo
2026@lldb_command('show_tcp_pcbinfo')
2027def ShowTcpPcbInfo(cmd_args=None):
2028    """ Display the list of TCP protocol control block information
2029    """
2030    print(GetPcbInfo(addressof(kern.globals.tcbinfo), IPPROTO_TCP))
2031# EndMacro:  show_tcp_pcbinfo
2032
2033# Macro: show_udp_pcbinfo
2034@lldb_command('show_udp_pcbinfo')
2035def ShowUdpPcbInfo(cmd_args=None):
2036    """ Display the list of UDP protocol control block information
2037    """
2038    print(GetPcbInfo(addressof(kern.globals.udbinfo), IPPROTO_UDP))
2039# EndMacro:  show_udp_pcbinfo
2040
2041# Macro: show_rip_pcbinfo
2042@lldb_command('show_rip_pcbinfo')
2043def ShowRipPcbInfo(cmd_args=None):
2044    """ Display the list of Raw IP protocol control block information
2045    """
2046    print(GetPcbInfo(addressof(kern.globals.ripcbinfo), IPPROTO_RAW))
2047# EndMacro:  show_rip_pcbinfo
2048
2049# Macro: show_mptcp_pcbinfo
2050@lldb_command('show_mptcp_pcbinfo')
2051def ShowMptcpPcbInfo(cmd_args=None):
2052    """ Display the list of MPTCP protocol control block information
2053    """
2054    GetMptcpInfo()
2055# EndMacro:  show_mptcp_pcbinfo
2056
2057# Macro: show_domains
2058@lldb_command('show_domains')
2059def ShowDomains(cmd_args=None):
2060    """ Display the list of the domains
2061    """
2062    out_string = ""
2063    domains = kern.globals.domains
2064    dp = Cast(domains.tqh_first, 'domain *')
2065    ifma_trash_format_string = "{0:4d}: {1:x} {2:3d} {3:6d} {4:6d}"
2066    cnt = 0
2067    while (dp != 0):
2068        out_string += "\"" + str(dp.dom_name) + "\"" + "[" + str(int(dp.dom_refs)) + " refs] domain " + hex(dp) + "\n"
2069        out_string += "    family:\t" + str(int(dp.dom_family)) + "\n"
2070        out_string += "    flags:0x\t" + str(int(dp.dom_flags)) + "\n"
2071        out_string += "    rtparams:\toff=" + str(int(dp.dom_rtoffset)) + ", maxrtkey=" + str(int(dp.dom_maxrtkey)) + "\n"
2072
2073        if (dp.dom_init):
2074            out_string += "    init:\t"
2075            out_string += GetSourceInformationForAddress(dp.dom_init) + "\n"
2076        if (dp.dom_externalize):
2077            out_string += "    externalize:\t"
2078            out_string += GetSourceInformationForAddress(dp.dom_externalize) + "\n"
2079        if (dp.dom_dispose):
2080            out_string += "    dispose:\t"
2081            out_string += GetSourceInformationForAddress(dp.dom_dispose) + "\n"
2082        if (dp.dom_rtattach):
2083            out_string += "    rtattach:\t"
2084            out_string += GetSourceInformationForAddress(dp.dom_rtattach) + "\n"
2085        if (dp.dom_old):
2086            out_string += "    old:\t"
2087            out_string += GetSourceInformationForAddress(dp.dom_old) + "\n"
2088
2089        pr = Cast(dp.dom_protosw.tqh_first, 'protosw *')
2090        while pr != 0:
2091            pru = pr.pr_usrreqs
2092            out_string += "\ttype " + str(int(pr.pr_type)) + ", protocol " + str(int(pr.pr_protocol)) + ", protosw " + hex(pr) + "\n"
2093            out_string += "\t    flags:0x\t" + hex(pr.pr_flags) + "\n"
2094            if (pr.pr_input):
2095                out_string += "\t    input:\t"
2096                out_string += GetSourceInformationForAddress(pr.pr_input) + "\n"
2097            if (pr.pr_output):
2098                out_string += "\t    output:\t"
2099                out_string += GetSourceInformationForAddress(pr.pr_output) + "\n"
2100            if (pr.pr_ctlinput):
2101                out_string += "\t    ctlinput:\t"
2102                out_string += GetSourceInformationForAddress(pr.pr_ctlinput) + "\n"
2103            if (pr.pr_ctloutput):
2104                out_string += "\t    ctloutput:\t"
2105                out_string += GetSourceInformationForAddress(pr.pr_ctloutput) + "\n"
2106            if (pr.pr_init):
2107                out_string += "\t    init:\t"
2108                out_string += GetSourceInformationForAddress(pr.pr_init) + "\n"
2109            if (pr.pr_drain):
2110                out_string += "\t    drain:\t"
2111                out_string += GetSourceInformationForAddress(pr.pr_drain) + "\n"
2112            if (pr.pr_sysctl):
2113                out_string += "\t    sysctl:\t"
2114                out_string += GetSourceInformationForAddress(pr.pr_sysctl) + "\n"
2115            if (pr.pr_lock):
2116                out_string += "\t    lock:\t"
2117                out_string += GetSourceInformationForAddress(pr.pr_lock) + "\n"
2118            if (pr.pr_unlock):
2119                out_string += "\t    unlock:\t"
2120                out_string += GetSourceInformationForAddress(pr.pr_unlock) + "\n"
2121            if (pr.pr_getlock):
2122                out_string += "\t    getlock:\t"
2123                out_string += GetSourceInformationForAddress(pr.pr_getlock) + "\n"
2124            if (pr.pr_old):
2125                out_string += "\t    old:\t"
2126                out_string += GetSourceInformationForAddress(pr.pr_old) + "\n"
2127
2128            out_string += "\t    pru_flags:0x\t" + hex(pru.pru_flags) + "\n"
2129            out_string += "\t    abort:\t"
2130            out_string += GetSourceInformationForAddress(pru.pru_abort) + "\n"
2131            out_string += "\t    accept:\t"
2132            out_string += GetSourceInformationForAddress(pru.pru_accept) + "\n"
2133            out_string += "\t    attach:\t"
2134            out_string += GetSourceInformationForAddress(pru.pru_attach) + "\n"
2135            out_string += "\t    bind:\t"
2136            out_string += GetSourceInformationForAddress(pru.pru_bind) + "\n"
2137            out_string += "\t    connect:\t"
2138            out_string += GetSourceInformationForAddress(pru.pru_connect) + "\n"
2139            out_string += "\t    connect2:\t"
2140            out_string += GetSourceInformationForAddress(pru.pru_connect2) + "\n"
2141            out_string += "\t    connectx:\t"
2142            out_string += GetSourceInformationForAddress(pru.pru_connectx) + "\n"
2143            out_string += "\t    control:\t"
2144            out_string += GetSourceInformationForAddress(pru.pru_control) + "\n"
2145            out_string += "\t    detach:\t"
2146            out_string += GetSourceInformationForAddress(pru.pru_detach) + "\n"
2147            out_string += "\t    disconnect:\t"
2148            out_string += GetSourceInformationForAddress(pru.pru_disconnect) + "\n"
2149            out_string += "\t    listen:\t"
2150            out_string += GetSourceInformationForAddress(pru.pru_listen) + "\n"
2151            out_string += "\t    peeraddr:\t"
2152            out_string += GetSourceInformationForAddress(pru.pru_peeraddr) + "\n"
2153            out_string += "\t    rcvd:\t"
2154            out_string += GetSourceInformationForAddress(pru.pru_rcvd) + "\n"
2155            out_string += "\t    rcvoob:\t"
2156            out_string += GetSourceInformationForAddress(pru.pru_rcvoob) + "\n"
2157            out_string += "\t    send:\t"
2158            out_string += GetSourceInformationForAddress(pru.pru_send) + "\n"
2159            out_string += "\t    sense:\t"
2160            out_string += GetSourceInformationForAddress(pru.pru_sense) + "\n"
2161            out_string += "\t    shutdown:\t"
2162            out_string += GetSourceInformationForAddress(pru.pru_shutdown) + "\n"
2163            out_string += "\t    sockaddr:\t"
2164            out_string += GetSourceInformationForAddress(pru.pru_sockaddr) + "\n"
2165            out_string += "\t    sopoll:\t"
2166            out_string += GetSourceInformationForAddress(pru.pru_sopoll) + "\n"
2167            out_string += "\t    soreceive:\t"
2168            out_string += GetSourceInformationForAddress(pru.pru_soreceive) + "\n"
2169            out_string += "\t    sosend:\t"
2170            out_string += GetSourceInformationForAddress(pru.pru_sosend) + "\n"
2171            pr = pr.pr_entry.tqe_next
2172        dp = dp.dom_entry.tqe_next
2173
2174        print(out_string)
2175# EndMacro: show_domains
2176
2177# Macro: tcp_count_rxt_segments
2178@lldb_command('tcp_count_rxt_segments')
2179def TCPCountRxtSegments(cmd_args=None):
2180    """ Size of the t_rxt_segments chain
2181    """
2182    if not cmd_args:
2183        raise ArgumentError("Missing argument 0 in user function.")
2184
2185    tp = kern.GetValueFromAddress(cmd_args[0], 'tcpcb *')
2186    rxseg = cast(tp.t_rxt_segments.slh_first, 'tcp_rxt_seg *')
2187    cnt = 0
2188    while rxseg != 0:
2189        cnt += 1
2190        rxseg = rxseg.rx_link.sle_next
2191        if (cnt % 1000 == 0):
2192            print(" running count: {:d}".format(cnt))
2193    print(" total count: {:d}".format(cnt))
2194# EndMacro: tcp_count_rxt_segments
2195
2196# Macro: tcp_walk_rxt_segments
2197@lldb_command('tcp_walk_rxt_segments')
2198def TCPWalkRxtSegments(cmd_args=None):
2199    """ Walk the t_rxt_segments chain
2200    """
2201    if not cmd_args:
2202        raise ArgumentError("Missing argument 0 in user function.")
2203
2204    tp = kern.GetValueFromAddress(cmd_args[0], 'tcpcb *')
2205    rxseg = cast(tp.t_rxt_segments.slh_first, 'tcp_rxt_seg *')
2206    cnt = 0
2207    while rxseg != 0:
2208        cnt += 1
2209        rxseg = rxseg.rx_link.sle_next
2210        if (cnt % 1000 == 0):
2211            print(" running count: {:d}".format(cnt))
2212    print(" total count: {:d}".format(cnt))
2213    rxseg = cast(tp.t_rxt_segments.slh_first, 'tcp_rxt_seg *')
2214    cnt = 0
2215    while rxseg != 0:
2216        cnt += 1
2217        out_string = ""
2218        span = rxseg.rx_end - rxseg.rx_start
2219        rxseg_format = "{0:4d} 0x{1:x} rx_start 0x{2:x} rx_end 0x{3:x} rx_count {4:4d} rx_flags 0x{5:x} span {6:d}"
2220        out_string += rxseg_format.format(cnt, rxseg, rxseg.rx_start, rxseg.rx_end, rxseg.rx_count, rxseg.rx_flags, abs(span))
2221        print(out_string)
2222        rxseg = rxseg.rx_link.sle_next
2223# EndMacro: tcp_walk_rxt_segments
2224