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