Skip to content

Instantly share code, notes, and snippets.

@DipSwitch
Created October 24, 2012 21:44
Show Gist options
  • Save DipSwitch/3949109 to your computer and use it in GitHub Desktop.
Save DipSwitch/3949109 to your computer and use it in GitHub Desktop.
Resolve the syscall number from your header files
#!/usr/bin/env python
#
# Filename: syscall.py
# Description: Resolve the syscall number from your header files
# Author: DipSwitch
# Maintainer: DipSwitch
# Created: Wed Oct 24 23:40:13 2012 (+0200)
# Version: v1.0
# Last-Updated: Thu Oct 25 07:34:19 2012 (+0200)
# By: DipSwitch
# Update #: 19
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change Log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
import fnmatch
import os
import re
import sys
class tc:
gray = '\033[1;30m'
red = '\033[1;31m'
green = '\033[1;32m'
yellow = '\033[1;33m'
blue = '\033[1;34m'
magenta = '\033[1;35m'
cyan = '\033[1;36m'
white = '\033[1;37m'
crimson = '\033[1;38m'
normal = '\033[0m'
bold = '\033[1m'
def disable():
gray = ''
red = ''
green = ''
yellow = ''
blue = ''
magenta = ''
cyan = ''
white = ''
crimson = ''
normal = ''
bold = ''
files = {
"x86" : "asm/unistd_32.h",
"x86_64": "asm/unistd_64.h",
"arm" : "valgrind/vki/vki-scnums-arm-linux.h",
"ppc32" : "valgrind/vki/vki-scnums-ppc32-linux.h",
"ppc64" : "valgrind/vki/vki-scnums-ppc64-linux.h",
"s390x" : "valgrind/vki/vki-scnums-s390x-linux.h",
"test" : "valgrind/vki/vki-scnums-test-linux.h"
}
def usage():
print "%s syscall" % sys.argv[0]
sys.exit(1)
def print_prototype(func):
regxp = (r"extern (?P<return>([a-z0-9_ ]+ |[a-z0-9_ ]+ \*))%s \((?P<ebx>([\[\]a-z0-9_* ]+|\.\.\.))(,(\n| )?(?P<ecx>([\[\]a-z0-9_* ]+|\.\.\.)))?(,(\n| )?(?P<edx>([\[\]a-z0-9_* ]+|\.\.\.)))?(,(\n| )?(?P<esi>([\[\]a-z0-9_* ]+|\.\.\.)))?(,(\n| )?(?P<edi>([\[\]a-z0-9_* ]+|\.\.\.)))?(,(\n| )?(?P<ebp>([\[\]a-z0-9_* ]+|\.\.\.)))?\).*?;" % func)
for root, dirnames, filenames in os.walk('/usr/include'):
for filename in fnmatch.filter(filenames, '*.h'):
path = os.path.join(root, filename)
ret = re.search(regxp, open(os.path.join(root, filename)).read(), re.M | re.S | re.I)
if ret:
print(" found prototype in: %s%s%s" % (tc.green, path, tc.normal))
print(" protype: %s%s%s" % (tc.yellow, ret.group(0), tc.normal))
print
if ret.group("return"):
print (" return : %s" % (ret.group("return")))
if ret.group("ebx") and ret.group("ebx") != "void":
print (" ebx : %s" % (ret.group("ebx")))
if ret.group("ecx"):
print (" ecx : %s" % (ret.group("ecx")))
if ret.group("edx"):
print (" edx : %s" % (ret.group("edx")))
if ret.group("esi"):
print (" esi : %s" % (ret.group("esi")))
if ret.group("edi"):
print (" edi : %s" % (ret.group("edi")))
if ret.group("ebp"):
print (" ebp : %s" % (ret.group("ebp")))
print
return
print("%sCan't find prototype for '%s'!%s" % (tc.red, func, tc.normal))
def main():
# parse arguments
if len(sys.argv) < 2 or len(sys.argv) > 3:
usage()
if len(sys.argv) == 3 and sys.argv[1] != "-r":
usage()
view = dict()
# fetch numbers
for key in files.iterkeys():
path = "/usr/include/%s" % files[key]
try:
if os.stat(path):
f = open(path)
res = re.search(("#define __NR_%s[\t ]+(?P<NR>[0-9]+)" % sys.argv[1]), f.read())
if res:
view[key] = int(res.group("NR"));
except OSError:
continue # don't care about file no found
# print numbers
if len(view) == 0:
print("No syscall named '%s%s%s' found" % (tc.red, sys.argv[1], tc.normal))
sys.exit(1)
else:
print("Syscall numbers for '%s%s%s':" % (tc.green, sys.argv[1], tc.normal))
# print the prototype
print_prototype(sys.argv[1])
# print syscall number
for key in view.iterkeys():
print ("%8s : %s%d%s" % (key, tc.red, view[key], tc.normal))
sys.exit(0)
if __name__ == "__main__":
main()
#
# syscall.py ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment