Rizin
unix-like reverse engineering framework and cli tools
build_mig_index.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #
3 # SPDX-FileCopyrightText: 2019 Francesco Tamagni <mrmacete@protonmail.ch>
4 # SPDX-License-Identifier: LGPL-3.0-only
5 #
6 # -*- coding: utf-8 -*-
7 
8 import json
9 import re
10 import sys
11 
12 header = """// SPDX-FileCopyrightText: 2019-2021 RizinOrg <info@rizin.re>
13 // SPDX-FileCopyrightText: 2019 Francesco Tamagni <mrmacete@protonmail.ch>
14 // SPDX-License-Identifier: LGPL-3.0-only
15 
16 // clang-format off
17 
18 /*
19  * This file is generated in this way:
20  *
21  * python3 build_mig_index.py ~/xnu-4570.51.1/bsd/kern/trace_codes traps.json > mig_index.h
22  *
23  *
24  * The traps.json file is generated from any dyld cache using the machtraps.py rzpipe script.
25  *
26  */
27 """
28 
29 
30 def convert(trace_codes, trap_json):
31  data = {}
32  with open(trace_codes, "r") as f:
33  for line in f:
34  splitted = re.compile("\s+").split(line.rstrip("\n"))
35  name = splitted[1]
36  code = int(splitted[0], 0)
37  klass = code & 0xFF000000
38  if klass == 0xFF000000: # MIG
39  name = name.replace("MSG_", "")
40  num = (code & 0x00FFFFFF) >> 2
41  data[num] = name
42 
43  with open(trap_json, "r") as f:
44  traps = json.loads(f.read())
45  for routine in traps:
46  num = routine["num"]
47  if num in data:
48  continue
49  data[num] = routine["name"]
50 
51  result = []
52  for num in data:
53  result.append((num, data[num]))
54 
55  result.sort(key=lambda x: x[0])
56 
57  print(header)
58  print("#ifndef RZ_MIG_INDEX_H")
59  print("#define RZ_MIG_INDEX_H\n")
60 
61  print("#define RZ_MIG_INDEX_LEN %d\n" % (len(data) * 2))
62 
63  print("static const char *mig_index[RZ_MIG_INDEX_LEN] = {")
64  for pair in result:
65  print('\t"%d", "%s",' % pair)
66  print("};\n")
67 
68  print("#endif")
69 
70 
71 if __name__ == "__main__":
72  if len(sys.argv) < 3:
73  print("usage %s bsd/kern/trace_codes traps.json" % sys.argv[0])
74  exit(1)
75  else:
76  convert(sys.argv[1], sys.argv[2])
size_t len
Definition: 6502dis.c:15
def convert(trace_codes, trap_json)
static int
Definition: sfsocketcall.h:114