aboutsummaryrefslogtreecommitdiff
path: root/tools/extract_linux_syscall_tables
blob: 0f2d2a66bef30208d57b6068cac563f3463c48d6 (plain)
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
#!/usr/bin/env python3

import re
import sys

from  urllib.request import urlopen

syscalls = {}


def print_table(name):
  tab = syscalls[name]
  print('\n\n----------------- BEGIN OF %s -----------------' % name)
  for i in range(max(tab.keys()) + 1):
    print('"%s",  // %d' % (tab.get(i, ''), i))
  print('----------------- END OF %s -----------------' % name)


# Parses a .tbl file (new format).
def parse_tlb(data):
  table = {}
  for line in data.splitlines():
    line = line.strip()
    if line.startswith('#') or not (line):
      continue
    parts = line.split()
    table[int(parts[0])] = 'sys_' + parts[2]
  return table


# Parses a #define __NR_xx 1234 old-style unistd.h header.
def parse_def(data):
  table = {}
  for line in data.splitlines():
    m = re.match(r'^#define\s+__NR\d*?_(\w+)\s+(\d+)\s*$', line.strip())
    if not m or m.group(1) == 'syscalls':  # __NR_syscalls is just a sentinel.
      continue
    table[int(m.group(2))] = 'sys_' + m.group(1)
  return table


def Main():
  KSRC = 'https://raw.githubusercontent.com/torvalds/linux/v4.20/'

  response = urlopen(KSRC + 'arch/x86/entry/syscalls/syscall_64.tbl')
  syscalls['x86_64'] = parse_tlb(response.read().decode())

  response = urlopen(KSRC + 'arch/x86/entry/syscalls/syscall_32.tbl')
  syscalls['x86'] = parse_tlb(response.read().decode())

  response = urlopen(KSRC + 'arch/arm/tools/syscall.tbl')
  syscalls['armeabi'] = parse_tlb(response.read().decode())

  response = urlopen(KSRC + 'arch/arm64/include/asm/unistd32.h')
  syscalls['aarch32'] = parse_def(response.read().decode())

  # From:
  # arch/arm64/include/asm/unistd.h
  #   -> arch/arm64/include/uapi/asm/unistd.h
  #     -> include/uapi/asm-generic/unistd.h
  response = urlopen(KSRC + 'include/uapi/asm-generic/unistd.h')
  syscalls['aarch64'] = parse_def(response.read().decode())

  print_table('x86_64')
  print_table('x86')
  print_table('aarch64')
  print_table('armeabi')
  print_table('aarch32')


if __name__ == '__main__':
  sys.exit(Main())