Cleaned up dbgrbyd.py, implemented tree rendering for the new 3-leb encoding

This commit is contained in:
Christopher Haster
2023-01-30 02:56:11 -06:00
parent bab79bb1e7
commit 11e91e6612
+75 -107
View File
@@ -219,10 +219,6 @@ def show_log(block_size, data, rev, off, *,
lifetime, weight = lifetimes.get(j, ('', 0)) lifetime, weight = lifetimes.get(j, ('', 0))
return '%s%*s' % (lifetime, 2*(max_weight-weight), '') return '%s%*s' % (lifetime, 2*(max_weight-weight), '')
# prepare other things
if args.get('rbyd'):
alts = []
# print header # print header
print('%-8s %s%-22s %s' % ( print('%-8s %s%-22s %s' % (
'off', 'off',
@@ -258,14 +254,6 @@ def show_log(block_size, data, rev, off, *,
notes.append('crc!=%08x' % crc) notes.append('crc!=%08x' % crc)
j_ += size j_ += size
# # adjust weight?
# if args.get('lifetimes'):
# if (tag & ~0x3f0) == 0x0400:
# weight += 1
# elif ((tag & ~0x3f0) == 0x0402
# and ((tag >> 15) & 0xffff)-1 < len(ids)):
# weight -= 1
# show human-readable tag representation # show human-readable tag representation
print('%s%08x:%s %s%s%-57s%s%s' % ( print('%s%08x:%s %s%s%-57s%s%s' % (
'\x1b[90m' if color and j >= off else '', '\x1b[90m' if color and j >= off else '',
@@ -281,16 +269,6 @@ def show_log(block_size, data, rev, off, *,
and not tag & 0x8 else ''), and not tag & 0x8 else ''),
'\x1b[m' if color and j >= off else '', '\x1b[m' if color and j >= off else '',
' (%s)' % ', '.join(notes) if notes ' (%s)' % ', '.join(notes) if notes
else ' %s' % ''.join(
('\x1b[33my\x1b[m' if color else 'y')
if alts[i] & 0x2
and i+1 < len(alts)
and alts[i+1] & 0x2
else ('\x1b[31mr\x1b[m' if color else 'r')
if alts[i] & 0x2
else ('\x1b[90mb\x1b[m' if color else 'b')
for i in range(len(alts)-1, -1, -1))
if args.get('rbyd') and (tag & 0x7) == 0
else ' %s' % jumprepr(j) else ' %s' % jumprepr(j)
if args.get('jumps') if args.get('jumps')
else '')) else ''))
@@ -333,12 +311,6 @@ def show_log(block_size, data, rev, off, *,
line, line,
'\x1b[m' if color and j >= off else '')) '\x1b[m' if color and j >= off else ''))
if args.get('rbyd'):
if tag & 0x8:
alts.append(tag)
else:
alts = []
def show_tree(block_size, data, rev, trunk, weight, *, def show_tree(block_size, data, rev, trunk, weight, *,
color=False, color=False,
@@ -348,31 +320,32 @@ def show_tree(block_size, data, rev, trunk, weight, *,
# lookup a tag, returning also the search path for decoration # lookup a tag, returning also the search path for decoration
# purposes # purposes
def lookup(tag): def lookup(tag, id):
lower = 0 lower = -1
upper = (weight+1) << 15 upper = weight
path = [] path = []
# descend down tree # descend down tree
j = trunk j = trunk
while True: while True:
_, alt, jump, delta = fromtag(data[j:]) _, alt, weight_, jump, delta = fromtag(data[j:])
# founat alt? # founat alt?
if alt & 0x4: if alt & 0x8:
weight_ += 1
# follow? # follow?
if (tag >= upper - (alt & ~0x7) if ((id, tag & ~0xf) > (upper-weight_-1, alt & ~0xf)
if alt & 0x2 if alt & 0x4
else tag < lower + (alt & ~0x7)): else ((id, tag & ~0xf) <= (lower+weight_, alt & ~0xf))):
lower += (upper-lower)-(alt & ~0x7) if alt & 0x2 else 0 lower += upper-lower-1-weight_ if alt & 0x4 else 0
upper -= (upper-lower)-(alt & ~0x7) if not alt & 0x2 else 0 upper -= upper-lower-1-weight_ if not alt & 0x4 else 0
j = j - jump j = j - jump
if args.get('rbyd') or args.get('tree'): if args.get('tree'):
# figure out which color # figure out which color
if alt & 0x1: if alt & 0x2:
_, nalt, _, _ = fromtag(data[j+jump+delta:]) _, nalt, _, _, _ = fromtag(data[j+jump+delta:])
if nalt & 0x1: if nalt & 0x2:
path.append((j+jump, j, 'y')) path.append((j+jump, j, 'y'))
else: else:
path.append((j+jump, j, 'r')) path.append((j+jump, j, 'r'))
@@ -380,15 +353,15 @@ def show_tree(block_size, data, rev, trunk, weight, *,
path.append((j+jump, j, 'b')) path.append((j+jump, j, 'b'))
# stay on path # stay on path
else: else:
lower += (alt & ~0x7) if not alt & 0x2 else 0 lower += weight_ if not alt & 0x4 else 0
upper -= (alt & ~0x7) if alt & 0x2 else 0 upper -= weight_ if alt & 0x4 else 0
j = j + delta j = j + delta
if args.get('rbyd') or args.get('tree'): if args.get('tree'):
# figure out which color # figure out which color
if alt & 0x1: if alt & 0x2:
_, nalt, _, _ = fromtag(data[j:]) _, nalt, _, _, _ = fromtag(data[j:])
if nalt & 0x1: if nalt & 0x2:
path.append((j-delta, j, 'y')) path.append((j-delta, j, 'y'))
else: else:
path.append((j-delta, j, 'r')) path.append((j-delta, j, 'r'))
@@ -396,9 +369,9 @@ def show_tree(block_size, data, rev, trunk, weight, *,
path.append((j-delta, j, 'b')) path.append((j-delta, j, 'b'))
# found tag # found tag
else: else:
tag_ = (((upper-0x8) & ~0x7fff) tag_ = alt
| (alt & 0x7fff)) id_ = upper-1
return tag_, j, delta, jump, path return tag_, id_, j, delta, jump, path
# precompute tree # precompute tree
if args.get('tree'): if args.get('tree'):
@@ -406,22 +379,24 @@ def show_tree(block_size, data, rev, trunk, weight, *,
paths = {} paths = {}
tag_ = 0 tag_ = 0
id_ = -1
while True: while True:
tag, j, delta, size, path = lookup(tag_) tag, id, j, delta, size, path = lookup(tag_, id_)
# found end of tree? # found end of tree?
if tag < tag_ or tag & 1: if (id, tag) < (id_, tag_) or tag & 2:
break break
tag_ = tag + 0x8 tag_ = tag + 0x10
id_ = id
tags.append((j, tag)) tags.append((j, tag, id))
for x, (a, b, c) in enumerate(path): for x, (a, b, c) in enumerate(path):
paths[a, b, x] = c paths[a, b, x] = c
# align paths to nearest tag # align paths to nearest tag
tags.sort() tags.sort()
paths = {( paths = {(
tags[bisect.bisect_left(tags, (a, 0), hi=len(tags)-1)], tags[bisect.bisect_left(tags, (a, 0, -1), hi=len(tags)-1)],
tags[bisect.bisect_left(tags, (b, 0), hi=len(tags)-1)], tags[bisect.bisect_left(tags, (b, 0, -1), hi=len(tags)-1)],
x): c for (a, b, x), c in paths.items()} x): c for (a, b, x), c in paths.items()}
# also find the maximum depth # also find the maximum depth
@@ -431,7 +406,8 @@ def show_tree(block_size, data, rev, trunk, weight, *,
if depth == 0: if depth == 0:
return '' return ''
_, tag = tags[bisect.bisect_left(tags, (j, 0), hi=len(tags)-1)] _, tag, id = tags[bisect.bisect_left(
tags, (j, 0, -1), hi=len(tags)-1)]
def c_start(c): def c_start(c):
return ('\x1b[33m' if color and c == 'y' return ('\x1b[33m' if color and c == 'y'
@@ -445,41 +421,44 @@ def show_tree(block_size, data, rev, trunk, weight, *,
path = [] path = []
seen = None seen = None
for x in range(depth): for x in range(depth):
if any(x == x_ and tag == a_tag if any(x == x_ and tag == a_tag and id == a_id
for (_, a_tag), _, x_ in paths.keys()): for (_, a_tag, a_id), _, x_ in paths.keys()):
c = next(c c = next(c
for ((_, a_tag), _, x_), c in paths.items() for ((_, a_tag, a_id), _, x_), c in paths.items()
if x == x_ and tag == a_tag) if x == x_ and tag == a_tag and id == a_id)
path.append('%s+%s' % (c_start(c), c_stop(c))) path.append('%s+%s' % (c_start(c), c_stop(c)))
elif any(x == x_ and tag == b_tag elif any(x == x_ and tag == b_tag and id == b_id
for _, (_, b_tag), x_ in paths.keys()): for _, (_, b_tag, b_id), x_ in paths.keys()):
a_tag, c = next((a_tag, c) a_tag, a_id, c = next((a_tag, a_id, c)
for ((_, a_tag), (_, b_tag), x_), c in paths.items() for ((_, a_tag, a_id), (_, b_tag, b_id), x_), c
if x == x_ and tag == b_tag) in paths.items()
if a_tag < tag: if x == x_ and tag == b_tag and id == b_id)
if (a_id, a_tag) < (id, tag):
path.append('%s\'%s' % (c_start(c), c_stop(c))) path.append('%s\'%s' % (c_start(c), c_stop(c)))
else: else:
path.append('%s.%s' % (c_start(c), c_stop(c))) path.append('%s.%s' % (c_start(c), c_stop(c)))
elif any(x == x_ elif any(x == x_
and tag >= min(a_tag, b_tag) and (id, tag) >= min((a_id, a_tag), (b_id, b_tag))
and tag <= max(a_tag, b_tag) and (id, tag) <= max((a_id, a_tag), (b_id, b_tag))
for (_, a_tag), (_, b_tag), x_ in paths.keys()): for (_, a_tag, a_id), (_, b_tag, b_id), x_
in paths.keys()):
c = next(c c = next(c
for ((_, a_tag), (_, b_tag), x_), c in paths.items() for ((_, a_tag, a_id), (_, b_tag, b_id), x_), c
in paths.items()
if x == x_ if x == x_
and tag >= min(a_tag, b_tag) and (id, tag) >= min((a_id, a_tag), (b_id, b_tag))
and tag <= max(a_tag, b_tag)) and (id, tag) <= max((a_id, a_tag), (b_id, b_tag)))
path.append('%s|%s' % (c_start(c), c_stop(c))) path.append('%s|%s' % (c_start(c), c_stop(c)))
elif seen: elif seen:
path.append('%s-%s' % (c_start(seen), c_stop(seen))) path.append('%s-%s' % (c_start(seen), c_stop(seen)))
else: else:
path.append(' ') path.append(' ')
if any(x == x_ and tag == b_tag if any(x == x_ and tag == b_tag and id == b_id
for _, (_, b_tag), x_ in paths.keys()): for _, (_, b_tag, b_id), x_ in paths.keys()):
c = next(c c = next(c
for (_, (_, b_tag), x_), c in paths.items() for (_, (_, b_tag, b_id), x_), c in paths.items()
if x == x_ and tag == b_tag) if x == x_ and tag == b_tag and id == b_id)
seen = c seen = c
if seen and x == depth-1: if seen and x == depth-1:
@@ -500,31 +479,25 @@ def show_tree(block_size, data, rev, trunk, weight, *,
if not args.get('no_truncate') else '')) if not args.get('no_truncate') else ''))
tag_ = 0 tag_ = 0
id_ = -1
while True: while True:
tag, j, delta, size, path = lookup(tag_) tag, id, j, delta, size, path = lookup(tag_, id_)
# found end of tree? # found end of tree?
if tag < tag_ or tag & 1: if (id, tag) < (id_, tag_) or tag & 2:
break break
tag_ = tag + 0x8 tag_ = tag + 0x10
id_ = id
# show human-readable tag representation # show human-readable tag representation
print('%08x:%s %-57s%s' % ( print('%08x:%s %-57s' % (
j, j,
treerepr(j) if args.get('tree') else '', treerepr(j) if args.get('tree') else '',
'%-22s%s' % ( '%-22s%s' % (
tagrepr(tag, size, j), tagrepr(tag, id, size, j),
' %s' % next(xxd( ' %s' % next(xxd(
data[j+delta:j+delta+min(size, 8)], 8), '') data[j+delta:j+delta+min(size, 8)], 8), '')
if not args.get('no_truncate') if not args.get('no_truncate')
and not tag & 0x4 else ''), and not tag & 0x8 else '')))
' %s' % ''.join(
('\x1b[33my\x1b[m' if color else 'y')
if path[i][2] == 'y'
else ('\x1b[31mr\x1b[m' if color else 'r')
if path[i][2] == 'r'
else ('\x1b[90mb\x1b[m' if color else 'b')
for i in range(len(path)-1, -1, -1))
if args.get('rbyd') else ''))
if args.get('raw'): if args.get('raw'):
# show on-disk encoding of tags # show on-disk encoding of tags
@@ -533,19 +506,22 @@ def show_tree(block_size, data, rev, trunk, weight, *,
'%04x' % (j + o*16), '%04x' % (j + o*16),
line)) line))
# show in-device representation # show in-device representation, including some extra
# crc/parity info
if args.get('device'): if args.get('device'):
print('%8s %*s%-47s' % ( print('%8s %s%-47s' % (
'', '',
2*depth+1 if args.get('tree') and depth > 0 else 0, '', lifetimerepr(0) if args.get('lifetimes') else '',
'%-22s%s' % ( '%-22s%s' % (
'%08x %08x' % (tag, size), '%04x %08x %07x' % (tag, 0xffffffff & id, size),
' %s' % ' '.join( ' %s' % ' '.join(
'%08x' % struct.unpack('<I', '%08x' % struct.unpack('<I',
data[j+delta+i*4:j+delta+i*4+4]) data[j+delta+i*4:j+delta+min(i*4+4,size)]
for i in range(min(size//4, 3)))[:23] .ljust(4, b'\0'))
if not tag & 0x4 else ''))) for i in range(min(m.ceil(size/4), 3)))[:23]
if not tag & 0x8 else '')))
if not tag & 0x8:
# show on-disk encoding of data # show on-disk encoding of data
if args.get('raw') or args.get('no_truncate'): if args.get('raw') or args.get('no_truncate'):
for o, line in enumerate(xxd(data[j+delta:j+delta+size])): for o, line in enumerate(xxd(data[j+delta:j+delta+size])):
@@ -695,10 +671,6 @@ if __name__ == "__main__":
'-l', '--log', '-l', '--log',
action='store_true', action='store_true',
help="Show the raw tags as they appear in the log.") help="Show the raw tags as they appear in the log.")
# parser.add_argument(
# '-i', '--in-tree',
# action='store_true',
# help="Only show tags in the tree.")
parser.add_argument( parser.add_argument(
'-r', '--raw', '-r', '--raw',
action='store_true', action='store_true',
@@ -711,10 +683,6 @@ if __name__ == "__main__":
'-T', '--no-truncate', '-T', '--no-truncate',
action='store_true', action='store_true',
help="Don't truncate, show the full contents.") help="Don't truncate, show the full contents.")
parser.add_argument(
'-y', '--rbyd',
action='store_true',
help="Show the rbyd tree in the margin.")
parser.add_argument( parser.add_argument(
'-t', '--tree', '-t', '--tree',
action='store_true', action='store_true',