""" Please make sure you read the README COMPLETELY BEFORE reading anything below.
- It is very critical that you read coding guidelines in Section E in README file.
+ It is very critical that you read coding guidelines in Section E in README file.
"""
from xnu import *
# Macro: mbuf_stat
@lldb_command('mbuf_stat')
def MBufStat(cmd_args=None):
- """ Print extended mbuf allocator statistics.
+ """ Print extended mbuf allocator statistics.
"""
hdr_format = "{0: <16s} {1: >8s} {2: >8s} {3: ^16s} {4: >8s} {5: >12s} {6: >8s} {7: >8s} {8: >8s}"
print hdr_format.format('class', 'total', 'cached', 'uncached', 'inuse', 'failed', 'waiter', 'notified', 'purge')
num_items = sizeof(kern.globals.mbuf_table) / sizeof(kern.globals.mbuf_table[0])
ncpus = int(kern.globals.ncpu)
for i in range(num_items):
- mbuf = kern.globals.mbuf_table[i]
+ mbuf = kern.globals.mbuf_table[i]
mcs = Cast(mbuf.mtbl_stats, 'mb_class_stat_t *')
mc = mbuf.mtbl_cache
total = 0
mcs.mbcl_notified, mcs.mbcl_purge_cnt
)
# EndMacro: mbuf_stat
-
+
# Macro: mbuf_walkpkt
@lldb_command('mbuf_walkpkt')
def MbufWalkPacket(cmd_args=None):
""" Walk the mbuf packet chain (m_nextpkt)
"""
- if (cmd_args == None or len(cmd_args) == 0):
- print "Missing argument 0 in user function."
- return
+ if not cmd_args:
+ raise ArgumentError("Missing argument 0 in user function.")
+
mp = kern.GetValueFromAddress(cmd_args[0], 'mbuf *')
cnt = 1
tot = 0
def MbufBuf2Slab(cmd_args=None):
""" Given an mbuf object, find its corresponding slab address
"""
- if (cmd_args == None or len(cmd_args) == 0):
- print "Missing argument 0 in user function."
- return
+ if not cmd_args:
+ raise ArgumentError("Missing argument 0 in user function.")
+
m = kern.GetValueFromAddress(cmd_args[0], 'mbuf *')
- gix = (m - Cast(kern.globals.mbutl, 'char *')) >> MBSHIFT
- slabstbl = kern.globals.slabstbl
- ix = (m - Cast(slabstbl[int(gix)].slg_slab[0].sl_base, 'char *')) >> 12
- slab = addressof(slabstbl[int(gix)].slg_slab[int(ix)])
+ slab = GetMbufSlab(m)
if (kern.ptrsize == 8):
mbuf_slab_format = "0x{0:<16x}"
print mbuf_slab_format.format(slab)
def MbufSlabs(cmd_args=None):
""" Print all slabs in the group
"""
+
out_string = ""
+ if not cmd_args:
+ raise ArgumentError("Invalid arguments passed.")
+
slg = kern.GetValueFromAddress(cmd_args[0], 'mcl_slabg_t *')
x = 0
out_string += "slot slab next obj mca tstamp C R N size flags\n"
out_string += "---- ---------- ---------- ---------- ---------- ---------- -- -- -- ------ -----\n"
- mbutl = cast(kern.globals.mbutl, 'union mbigcluster *')
- while x < NSLABSPMB:
+ mbutl = cast(kern.globals.mbutl, 'unsigned char *')
+ nslabspmb = int((1 << MBSHIFT) >> unsigned(kern.globals.page_shift))
+ while x < nslabspmb:
sl = addressof(slg.slg_slab[x])
mca = 0
obj = sl.sl_base
ts = 0
if (kern.globals.mclaudit != 0):
- ix = (obj - Cast(kern.globals.mbutl, 'char *')) >> 12
- clbase = mbutl + (sizeof(dereference(mbutl)) * ix)
- mclidx = (obj - clbase) >> 8
- mca = kern.globals.mclaudit[int(ix)].cl_audit[int(mclidx)]
- ts = mca.mca_tstamp
+ mca = GetMbufMcaPtr(obj, sl.sl_class)
+ trn = (mca.mca_next_trn + unsigned(kern.globals.mca_trn_max) - 1) % unsigned(kern.globals.mca_trn_max)
+ ts = mca.mca_trns[trn].mca_tstamp
out_string += slabs_string_format.format((x + 1), sl, sl.sl_next, obj, hex(mca), int(ts), int(sl.sl_class), int(sl.sl_refcnt), int(sl.sl_chunks), int(sl.sl_len), hex(sl.sl_flags))
ts = 0
if (kern.globals.mclaudit != 0):
- ix = (obj - Cast(kern.globals.mbutl, 'char *')) >> 12
- clbase = mbutl + (sizeof(dereference(mbutl)) * ix)
- mclidx = (obj - clbase) >> 8
- mca = kern.globals.mclaudit[int(ix)].cl_audit[int(mclidx)]
- ts = mca.mca_tstamp
+ mca = GetMbufMcaPtr(obj, sl.sl_class)
+ trn = (mca.mca_next_trn + unsigned(kern.globals.mca_trn_max) - 1) % unsigned(kern.globals.mca_trn_max)
+ ts = mca.mca_trns[trn].mca_tstamp
if (kern.ptrsize == 8):
out_string += " " + hex(obj) + " " + hex(mca) + " " + str(unsigned(ts)) + "\n"
slabstbl = kern.globals.slabstbl
slabs_table_blank_string_format = "{0:>3d}: - \n"
+ nslabspmb = int(((1 << MBSHIFT) >> unsigned(kern.globals.page_shift)))
while (x < unsigned(kern.globals.maxslabgrp)):
slg = slabstbl[x]
if (slg == 0):
else:
if (kern.ptrsize == 8):
slabs_table_string_format = "{0:>3d}: 0x{1:16x} [ 0x{2:16x} - 0x{3:16x} ]\n"
- out_string += slabs_table_string_format.format(x+1, slg, addressof(slg.slg_slab[0]), addressof(slg.slg_slab[NSLABSPMB-1]))
+ out_string += slabs_table_string_format.format(x+1, slg, addressof(slg.slg_slab[0]), addressof(slg.slg_slab[nslabspmb-1]))
else:
slabs_table_string_format = "{0:>3d}: 0x{1:8x} [ 0x{2:8x} - 0x{3:8x} ]\n"
- out_string += slabs_table_string_format.format(x+1, slg, addressof(slg.slg_slab[0]), addressof(slg.slg_slab[NSLABSPMB-1]))
+ out_string += slabs_table_string_format.format(x+1, slg, addressof(slg.slg_slab[0]), addressof(slg.slg_slab[nslabspmb-1]))
x += 1
print out_string
# EndMacro: mbuf_slabstbl
+def GetMbufMcaPtr(m, cl):
+ pgshift = int(kern.globals.page_shift)
+ ix = int((m - Cast(kern.globals.mbutl, 'char *')) >> pgshift)
+ page_addr = (Cast(kern.globals.mbutl, 'char *') + (ix << pgshift))
+
+ if (int(cl) == 0):
+ midx = int((m - page_addr) >> 8)
+ mca = kern.globals.mclaudit[ix].cl_audit[midx]
+ elif (int(cl) == 1):
+ midx = int((m - page_addr) >> 11)
+ mca = kern.globals.mclaudit[ix].cl_audit[midx]
+ elif (int(cl) == 2):
+ midx = int((m - page_addr) >> 12)
+ mca = kern.globals.mclaudit[ix].cl_audit[midx]
+ else:
+ mca = kern.globals.mclaudit[ix].cl_audit[0]
+ return Cast(mca, 'mcache_audit_t *')
+
+def GetMbufSlab(m):
+ pgshift = int(kern.globals.page_shift)
+ gix = int((Cast(m, 'char *') - Cast(kern.globals.mbutl, 'char *')) >> MBSHIFT)
+ slabstbl = kern.globals.slabstbl
+ ix = int((Cast(m, 'char *') - Cast(slabstbl[gix].slg_slab[0].sl_base, 'char *')) >> pgshift)
+ return addressof(slabstbl[gix].slg_slab[ix])
def GetMbufBuf2Mca(m):
- ix = (m - Cast(kern.globals.mbutl, 'char *')) >> 12
- #mbutl = Cast(kern.globals.mbutl, 'union mbigcluster *')
- mbutl = cast(kern.globals.mbutl, 'union mbigcluster *')
- clbase = mbutl + (sizeof(dereference(mbutl)) * ix)
- mclidx = (m - clbase) >> 8
- mca = kern.globals.mclaudit[int(ix)].cl_audit[int(mclidx)]
+ sl = GetMbufSlab(m)
+ mca = GetMbufMcaPtr(m, sl.sl_class)
return str(mca)
def GetMbufWalkAllSlabs(show_a, show_f, show_tr):
out_string = ""
kern.globals.slabstbl[0]
-
+
x = 0
total = 0
total_a = 0
show_mca_string_format = "{0:4s} {1:4s} {2:8s} {3:8s} {4:8} {5:12s} {6:12s}"
out_string += show_mca_string_format.format("slot", "idx", "slab address", "mca address", "obj address", "type", "allocation state\n")
+ nslabspmb = unsigned((1 << MBSHIFT) >> unsigned(kern.globals.page_shift))
while (x < unsigned(kern.globals.slabgrp)):
slg = kern.globals.slabstbl[x]
y = 0
stop = 0
- while ((y < NSLABSPMB) and (stop == 0)):
+ while ((y < nslabspmb) and (stop == 0)):
sl = addressof(slg.slg_slab[y])
base = sl.sl_base
- mbutl = cast(kern.globals.mbutl, 'union mbigcluster *')
- ix = (base - mbutl) >> 12
- clbase = mbutl + (sizeof(dereference(mbutl)) * ix)
- mclidx = (base - clbase) >> 8
- mca = kern.globals.mclaudit[int(ix)].cl_audit[int(mclidx)]
+ mca = GetMbufMcaPtr(base, sl.sl_class)
first = 1
while ((Cast(mca, 'int') != 0) and (unsigned(mca.mca_addr) != 0)):
printmca = 0
- if (mca.mca_uflags & (MB_INUSE|MB_COMP_INUSE)):
+ if (mca.mca_uflags & (MB_INUSE | MB_COMP_INUSE)):
total_a = total_a + 1
printmca = show_a
else:
out_string += GetMbufMcaCtype(mca, 0)
- if (mca.mca_uflags & (MB_INUSE|MB_COMP_INUSE)):
+ if (mca.mca_uflags & (MB_INUSE | MB_COMP_INUSE)):
out_string += "active "
else:
out_string += " freed "
total = total + 1
if (show_tr != 0):
- out_string += "Recent transaction for this buffer (thread: 0x" + hex(mca.mca_thread) + "):\n"
+ trn = (mca.mca_next_trn + idx - 1) % unsigned(kern.globals.mca_trn_max)
+ out_string += "Transaction " + str(int(trn)) + " at " + str(int(mca.mca_trns[int(trn)].mca_tstamp)) + " by thread: 0x" + str(hex(mca.mca_trns[int(trn)].mca_thread)) + ":\n"
cnt = 0
- while (cnt < mca.mca_depth):
- kgm_pc = mca.mca_stack[int(cnt)]
+ while (cnt < mca.mca_trns[int(trn)].mca_depth):
+ kgm_pc = mca.mca_trns[int(trn)].mca_stack[int(cnt)]
out_string += str(int(cnt) + 1) + " "
- out_string += GetPc(kgm_pc)
+ out_string += GetPc(kgm_pc)
cnt += 1
+ print out_string
+ out_string = ""
mca = mca.mca_next
y += 1
def GetMbufMcaCtype(mca, vopt):
cp = mca.mca_cache
mca_class = unsigned(cp.mc_private)
- csize = kern.globals.mbuf_table[mca_class].mtbl_stats.mbcl_size
+ csize = unsigned(kern.globals.mbuf_table[mca_class].mtbl_stats.mbcl_size)
done = 0
out_string = " "
if (csize == MSIZE):
else:
out_string += "CL "
return out_string
- if (csize == NBPG):
+ if (csize == MBIGCLBYTES):
if (vopt):
out_string += "BCL (4K cluster) "
else:
out_string += "(unpaired 2K cluster, mbuf) "
return out_string
- if (csize == (MSIZE + NBPG)):
+ if (csize == (MSIZE + MBIGCLBYTES)):
if (mca.mca_uflags & MB_SCVALID):
if (mca.mca_uptr):
out_string += "M+BCL "
out_string += "(paired mbuf, 4K cluster) "
else:
out_string += "M-BCL "
- if vopt:
+ if vopt:
out_string += "(unpaired mbuf, 4K cluster) "
else:
if (mca.mca_uptr):
out_string += "unknown: " + cp.mc_name
return out_string
-
+
kgm_pkmod = 0
kgm_pkmodst = 0
kgm_pkmoden = 0
def GetPc(kgm_pc):
out_string = ""
mh_execute_addr = int(lldb_run_command('p/x (uintptr_t *)&_mh_execute_header').split('=')[-1].strip(), 16)
- if (unsigned(kgm_pc) < unsigned(mh_execute_addr) or
- unsigned(kgm_pc) >= unsigned(kern.globals.vm_kernel_top)):
+ if (unsigned(kgm_pc) < unsigned(mh_execute_addr) or unsigned(kgm_pc) >= unsigned(kern.globals.vm_kernel_top)):
out_string += GetKmodAddrIntAsString(kgm_pc)
else:
out_string += GetSourceInformationForAddress(int(kgm_pc))
def MbufShowActive(cmd_args=None):
""" Print all active/in-use mbuf objects
"""
- if cmd_args != None and len(cmd_args) > 0 :
+ if cmd_args:
print GetMbufWalkAllSlabs(1, 0, cmd_args[0])
else:
print GetMbufWalkAllSlabs(1, 0, 0)
""" Print the contents of an mbuf mcache audit structure
"""
out_string = ""
- if cmd_args != None and len(cmd_args) > 0 :
+ pgshift = unsigned(kern.globals.page_shift)
+ if cmd_args:
mca = kern.GetValueFromAddress(cmd_args[0], 'mcache_audit_t *')
cp = mca.mca_cache
out_string += "object type:\t"
out_string += GetMbufMcaCtype(mca, 1)
out_string += "\nControlling mcache :\t" + hex(mca.mca_cache) + " (" + str(cp.mc_name) + ")\n"
if (mca.mca_uflags & MB_SCVALID):
- mbutl = cast(kern.globals.mbutl, 'union mbigcluster *')
- ix = (mca.mca_addr - mbutl) >> 12
- clbase = mbutl + (sizeof(dereference(mbutl)) * ix)
+ mbutl = Cast(kern.globals.mbutl, 'unsigned char *')
+ ix = (mca.mca_addr - mbutl) >> pgshift
+ clbase = mbutl + (ix << pgshift)
mclidx = (mca.mca_addr - clbase) >> 8
out_string += "mbuf obj :\t\t" + hex(mca.mca_addr) + "\n"
out_string += "mbuf index :\t\t" + str(mclidx + 1) + " (out of 16) in cluster base " + hex(clbase) + "\n"
if (mca.mca_uptr != 0):
peer_mca = cast(mca.mca_uptr, 'mcache_audit_t *')
out_string += "paired mbuf obj :\t" + hex(peer_mca.mca_addr) + " (mca " + hex(peer_mca) + ")\n"
-
- out_string += "Recent transaction (tstamp " + str(unsigned(mca.mca_tstamp)) + ", thread " + hex(mca.mca_thread) + ") :\n"
- cnt = 0
- while (cnt < mca.mca_depth):
- kgm_pc = mca.mca_stack[cnt]
- out_string += " " + str(cnt + 1) + ". "
- out_string += GetPc(kgm_pc)
- cnt += 1
-
- if (mca.mca_pdepth > 0):
- out_string += "previous transaction (tstamp " + str(unsigned(mca.mca_ptstamp)) + ", thread " + hex(mca.mca_pthread) + "):\n"
- cnt = 0
-
- while (cnt < mca.mca_pdepth):
- kgm_pc = mca.mca_pstack[cnt]
- out_string += " " + str(cnt + 1) + ". "
- out_string += GetPc(kgm_pc)
- cnt += 1
+ for idx in range(unsigned(kern.globals.mca_trn_max), 0, -1):
+ trn = (mca.mca_next_trn + idx - 1) % unsigned(kern.globals.mca_trn_max)
+ out_string += "transaction {:d} (tstamp {:d}, thread 0x{:x}):\n".format(trn, mca.mca_trns[trn].mca_tstamp, mca.mca_trns[trn].mca_thread)
+ cnt = 0
+ while (cnt < mca.mca_trns[trn].mca_depth):
+ kgm_pc = mca.mca_trns[trn].mca_stack[cnt]
+ out_string += " " + str(cnt + 1) + ". "
+ out_string += GetPc(kgm_pc)
+ cnt += 1
+
+ msc = cast(mca.mca_contents, 'mcl_saved_contents_t *')
+ msa = addressof(msc.sc_scratch)
if (mca.mca_uflags & MB_SCVALID):
- msc = cast(mca.mca_contents, 'mcl_saved_contents_t *')
- msa = addressof(msc.sc_scratch)
if (msa.msa_depth > 0):
- out_string += "Recent scratch transaction (tstamp " + str(unsigned(msa.msa_tstamp)) + ", thread " + hex(msa.msa_thread) + ") :\n"
+ out_string += "Recent scratch transaction (tstamp {:d}, thread 0x{:x}):\n".format(msa.msa_tstamp, msa.msa_thread)
cnt = 0
while (cnt < msa.msa_depth):
kgm_pc = msa.msa_stack[cnt]
cnt += 1
if (msa.msa_pdepth > 0):
- out_string += "previous scratch transaction (tstamp " + msa.msa_ptstamp + ", thread " + msa.msa_pthread + "):\n"
- cnt = 0
- while (cnt < msa.msa_pdepth):
- kgm_pc = msa.msa_pstack[cnt]
- out_string += " " + str(cnt + 1) + ". "
- out_string += GetPc(kgm_pc)
- cnt += 1
- else :
+ out_string += "previous scratch transaction (tstamp {:d}, thread 0x{:x}):\n".format(msa.msa_ptstamp, msa.msa_pthread)
+ if (msa):
+ cnt = 0
+ while (cnt < msa.msa_pdepth):
+ kgm_pc = msa.msa_pstack[cnt]
+ out_string += " " + str(cnt + 1) + ". "
+ out_string += GetPc(kgm_pc)
+ cnt += 1
+ else:
out_string += "Missing argument 0 in user function."
print out_string
def MbufShowAll(cmd_args=None):
""" Print all mbuf objects
"""
- print GetMbufWalkAllSlabs(1, 1, 0)
+ print GetMbufWalkAllSlabs(1, 1, 0)
# EndMacro: mbuf_showall
# Macro: mbuf_countchain
def MbufCountChain(cmd_args=None):
""" Count the length of an mbuf chain
"""
- if (cmd_args == None or len(cmd_args) == 0):
- print "Missing argument 0 in user function."
- return
+ if not cmd_args:
+ raise ArgumentError("Missing argument 0 in user function.")
+
mp = kern.GetValueFromAddress(cmd_args[0], 'mbuf *')
pkt = 0
nxt = 0
-
+
while (mp):
pkt = pkt + 1
mn = mp.m_hdr.mh_next
stored information with that trace
syntax: (lldb) mbuf_traceleak <addr>
"""
- if (cmd_args == None or len(cmd_args) == 0):
- print "Missing argument 0 in user function."
- return
+ if not cmd_args:
+ raise ArgumentError("Missing argument 0 in user function.")
+
trace = kern.GetValueFromAddress(cmd_args[0], 'mtrace *')
print GetMbufTraceLeak(trace)
# EndMacro: mbuf_traceleak
def McacheWalkObject(cmd_args=None):
""" Given a mcache object address, walk its obj_next pointer
"""
- if (cmd_args == None or len(cmd_args) == 0):
- print "Missing argument 0 in user function."
- return
+ if not cmd_args:
+ raise ArgumentError("Missing argument 0 in user function.")
+
out_string = ""
p = kern.GetValueFromAddress(cmd_args[0], 'mcache_obj_t *')
cnt = 1
mcache_stat_format_string = "{0:<24s} {1:>8s} {2:>20s} {3:>5s} {4:>5s} {5:>20s} {6:>30s} {7:>18s}"
else:
mcache_stat_format_string = "{0:<24s} {1:>8s} {2:>12s} {3:>5s} {4:>5s} {5:>12s} {6:>30s} {7:>18s}"
-
+
if (kern.ptrsize == 8):
mcache_stat_data_format_string = "{0:<24s} {1:>12s} {2:>20s} {3:>5s} {4:>5s} {5:>22s} {6:>12d} {7:>8d} {8:>8d} {9:>18d}"
else:
mcache_stat_data_format_string = "{0:<24s} {1:>12s} {2:>12s} {3:>5s} {4:>5s} {5:>14s} {6:>12d} {7:>8d} {8:>8d} {9:>18d}"
-
- out_string += mcache_stat_format_string.format("cache name", "cache state" , "cache addr", "buf size", "buf align", "backing zone", "wait nowait failed", "bufs incache")
+
+ out_string += mcache_stat_format_string.format("cache name", "cache state", "cache addr", "buf size", "buf align", "backing zone", "wait nowait failed", "bufs incache")
out_string += "\n"
-
+
ncpu = int(kern.globals.ncpu)
while mc != 0:
bktsize = mc.mc_cpu[0].cc_bktsize
backing_zone = " custom"
else:
backing_zone = " custom"
-
+
total = 0
total += mc.mc_full.bl_total * bktsize
n = 0
out_string += " " + str(total) + "\n\n"
total += cp.mc_full.bl_total * bktsize
- out_string += "Total # of full buckets (" + str(int(bktsize)) + " objs/bkt):\t" + str(int(cp.mc_full.bl_total)) +"\n"
+ out_string += "Total # of full buckets (" + str(int(bktsize)) + " objs/bkt):\t" + str(int(cp.mc_full.bl_total)) + "\n"
out_string += "Total # of objects cached:\t\t" + str(total) + "\n"
print out_string
# EndMacro: mcache_showcache