-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdb.py
79 lines (70 loc) · 2.51 KB
/
gdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
unit = gdb.lookup_global_symbol("Unit").value()
deadbeef = gdb.lookup_global_symbol("deadbeef").value()
nursery = gdb.lookup_global_symbol("nursery").value()
abc_any = gdb.lookup_type("union any")
def color(s, c):
return "\x1b[" + str(c) + "m" + s + "\x1b[0m"
def gray(s):
return color(s, 90)
def red(s):
return color(s, "1;31")
def p(indent, tag, value):
print(" " * indent + tag + ": " + str(value))
def print_abc(i, v):
v = v.cast(abc_any)
vt = v['as_tagged']
if v == unit:
p(i, "Unit", "Unit")
elif v == deadbeef:
p(i, "Dead", "Beef")
elif vt == 0:
p(i, red("!!!NULL POINTER!!!"), "This should never happen")
elif (vt & 0xfff0000000000000) != 0:
p(i, "Number", (~vt).cast(abc_any)['as_num'])
elif v['as_indirect'] < nursery['base']:
block = gdb.block_for_pc(int(vt))
if block == None:
name = str(v['as_indirect'])
else:
name = str(block.function)
p(i, "Block", name)
else:
tag = vt & 0x7
ptr = vt & ~0x7
hexptr = gray(hex(int(ptr)))
v = ptr.cast(abc_any)
try:
if tag == 0:
pair = v['as_pair'].dereference()
if pair['snd'] == deadbeef:
p(i, "Left", hexptr)
print_abc(i+4, pair['fst'])
else:
p(i, "Pair", hexptr)
print_abc(i+4, pair['fst'])
print_abc(i+4, pair['snd'])
elif tag == 1:
pair = v['as_comp_block'].dereference()
if pair['yz'] == deadbeef:
p(i, "Right", hexptr)
print_abc(i+4, pair['xy'])
else:
p(i, "Composed", hexptr)
print_abc(i+4, pair['xy'])
print_abc(i+4, pair['yz'])
elif tag == 2:
p(i, "Quoted", hexptr)
print_abc(i+4, v['as_indirect'].dereference())
elif (tag & 0x4) != 0:
p(i, "Forwarding pointer", hexptr)
print_abc(i+4, v['as_indirect'].dereference())
else:
p(i, "INVALID TAG", hexptr)
except gdb.MemoryError:
p(i, red("!!!INVALID POINTER!!!"), hexptr)
class PrintABCValue(gdb.Command):
def __init__(self):
super(PrintABCValue, self).__init__('print-abc-value', gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL)
def invoke(self, arg, tty):
print_abc(0, gdb.parse_and_eval(arg))
PrintABCValue()