gen_syscall_strings.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. import re
  3. rx = re.compile('#define __NR_(.*?)\s+(.*)')
  4. ry = re.compile('\(__NR_(.*?)\+(\d+)\)')
  5. out_cc = file('common/misc/syscall_strings.cc', 'w')
  6. out_py = file('scripts/syscall_strings.py', 'w')
  7. print >> out_cc, '''\
  8. // Automatically generated by %s
  9. #include "syscall_strings.h"
  10. const char * syscall_string(int syscall_number)
  11. {
  12. switch(syscall_number)
  13. {
  14. ''' % __file__
  15. print >> out_py, '''\
  16. # Automatically generated by %s
  17. ''' % __file__
  18. def make(bits):
  19. seen = set()
  20. names = {}
  21. for line in open('/usr/include/asm/unistd_%d.h' % bits, 'r').readlines():
  22. m = rx.match(line)
  23. if m:
  24. name, number = m.group(1), m.group(2)
  25. mm = ry.match(number)
  26. if mm:
  27. number = names[mm.group(1)] + int(mm.group(2))
  28. else:
  29. try:
  30. number = int(number.split()[0])
  31. except ValueError:
  32. continue
  33. if number not in seen:
  34. seen.add(number)
  35. names[name] = number
  36. print >> out_cc, ' case %s: return "%s";' % (number, name)
  37. print >> out_py, ' %s: "%s",' % (number, name)
  38. print >> out_cc, '#ifdef TARGET_IA32'
  39. print >> out_py, 'syscall_strings_32 = {'
  40. make(32)
  41. print >> out_cc, '#else'
  42. print >> out_py, '}'
  43. print >> out_py, 'syscall_strings_64 = {'
  44. make(64)
  45. print >> out_cc, '#endif'
  46. print >> out_py, '''
  47. }
  48. # Determine whether we're in 32-bit or 64-bit mode
  49. import sys
  50. if sys.maxsize == 2**31-1:
  51. syscall_strings = syscall_strings_32
  52. else:
  53. syscall_strings = syscall_strings_64
  54. '''
  55. print >> out_cc, '''\
  56. default: return "(unknown)";
  57. }
  58. }
  59. '''