File: SYSCALL_PROTOTYPES.codegen.py

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (47 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download
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
#!/usr/bin/env python

"""
Generates the SYSCALL_PROTOTYPES dictionary from the Linux kernel source
and prints out Python code representing it.
"""

import urllib2
import re

url = "https://raw.githubusercontent.com/torvalds/linux/master/include/linux/syscalls.h"
source = urllib2.urlopen(url).read()

p1 = re.compile(r"^asmlinkage long sys(?:32)?_(.*?)\((.*?)\)",
                re.MULTILINE | re.DOTALL)
p2 = re.compile(r"^(.*?)([^ *]+)$")

SYSCALL_PROTOTYPES = {}

for m1 in p1.finditer(source):
    call_name = m1.group(1)
    args = m1.group(2)
    args = args.replace("__user", "")
    args = " ".join(args.split())
    args_tuple = ()
    if args != "void":
        for arg in args.split(","):
            if arg.endswith(("*", "long", "int", "size_t")):
                arg_type = arg.strip()
                arg_name = ""
            else:
                m2 = p2.match(arg)
                arg_type = m2.group(1).strip()
                arg_name = m2.group(2).strip()
            # Workaround for pipe system call
            if (call_name == 'pipe' or call_name == 'pipe2') and arg_type == "int *":
                arg_type = "int[2]"
            args_tuple += ((arg_type, arg_name),)
    SYSCALL_PROTOTYPES[call_name] = ("long", args_tuple)

for call_name in sorted(SYSCALL_PROTOTYPES):
    signature = SYSCALL_PROTOTYPES[call_name]
    args_tuple = signature[1]
    print('"%s": ("%s", (' % (call_name, signature[0]))
    for arg in args_tuple:
        print(('    ("%s", "%s"),' % (arg[0], arg[1])))
    print(')),')