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