-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcpu-jtoy.py
82 lines (65 loc) · 2.42 KB
/
cpu-jtoy.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
80
81
82
#!/usr/bin/env python
import json
import sys
import yaml
"""
This is an annoying hack -- Right now the windbg js dumps integer values as
hex strings, e.g.:
{ "rax": "0x4141" }
This is because js ints are 53 bit, so we can't actually store full 64b
registers. Writing a decoder for this in rust kind sucks for two reasons:
- We'd need to create a separate decode function for each bitsize we want
to decode, e.g. one for u16, u32, and u64
- We'd need to create a separate decode function for each array size we
want to decide, e.g. [u64; 4 vs [u64; 6]
While this is definitely doable, I'd rather just bang out a json to yaml
converter and have the mofo load yaml instead.
"""
def usage():
print('Usage: %s <cpu-state.json>' % sys.argv[0])
sys.exit(-1)
# json hex string decoder class
# https://stackoverflow.com/questions/45068797/how-to-convert-string-int-json-into-real-int-with-json-loads
class HexStr(json.JSONDecoder):
def decode(self, s):
result = super(HexStr, self).decode(s)
return self._decode(result)
def _decode(self, o):
if isinstance(o, str) or isinstance(o, unicode):
try:
return int(o, 0)
except ValueError:
try:
return float(o)
except ValueError:
return o
elif isinstance(o, dict):
return {k: self._decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [self._decode(v) for v in o]
else:
return o
# yaml hex encoder functions
# https://stackoverflow.com/questions/9100662/how-to-print-integers-as-hex-strings-using-json-dumps-in-python/9101562#9101562
def hexint_presenter(dumper, data):
return dumper.represent_int('%#x' % data)
def unicode_representer(dumper, uni):
node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni)
return node
yaml.add_representer(int, hexint_presenter)
yaml.add_representer(long, hexint_presenter)
yaml.add_representer(unicode, unicode_representer)
def main():
if len(sys.argv) != 2: usage()
json_cpu_file = sys.argv[1]
with open(json_cpu_file, 'rb') as h:
j = json.load(h, cls=HexStr)
# HACK HACK HACK
# remove when I acutally lift zmm state from windbg
j['zmm'] = []
for ii in range(32):
j['zmm'].append({ 'q': [0 for _ in range(8)] })
y = yaml.dump(j)
print(y)
if __name__ == '__main__':
main()