Fix writable string warning
[dyninst.git] / syscalls / generateSyscallInformation.py
1 #!/usr/bin/python
2
3 """ 
4 Generates common/src/SyscallInformation.C and dynutil/h/dyn_syscalls.h 
5 """
6
7 import os
8 import sys
9 import string
10
11 def generate_dyn_syscalls(allSyscallNames):
12     # Open file for writing
13     outFile = open("dyn_syscalls.h", "w")
14
15     # Write out "header" information
16     outFile.write("/* This file was autogenerated from syscalls/generateSyscallInformation.py */" + "\n")
17     outFile.write("#if !defined(DYN_SYSCALLS_H_)\n")
18     outFile.write("#define DYN_SYSCALLS_H_ \n")
19     outFile.write("\n")
20     outFile.write("namespace Dyninst {\n")
21     outFile.write("namespace Syscall {\n")
22     outFile.write("enum {\n")
23
24     # Write out our system calls!
25     for syscall in allSyscallNames:
26         if (syscall.find("//") == -1):
27             outFile.write("\tdyn_" + syscall + ", \n")
28         else:
29             outFile.write("\t" + syscall + "\n")
30
31     # Write out "footer" information
32     outFile.write("};\n")
33     outFile.write("}}\n")
34     outFile.write("#endif\n")
35
36     # Close file
37     outFile.close()
38
39 def generate_SyscallInformation(allSyscallNames, allSyscalls):
40     # Open file for writing
41     outFile = open("SyscallInformation.C", "w")
42
43     # Write out "header" information
44     outFile.write("/* This file was autogenerated from syscalls/generateSyscallInformation.py */" + "\n")
45     outFile.write("#include \"dyn_regs.h\"\n")
46     outFile.write("#include \"dyn_syscalls.h\"\n")
47     outFile.write("SyscallInformation::SyscallInformation() {\n")
48
49     curOS = allSyscalls[0][0]
50     curArch = allSyscalls[0][1]
51     curPlatName = curOS + "_" + curArch 
52
53     outFile.write("\n/* " + curOS + " " + curArch + " Numbers */\n")
54     outFile.write("Platform " + curPlatName + "(" + curArch + ", " + curOS + ");\n")
55
56     for syscall in allSyscalls:
57         newPlat = False
58         if (curOS != syscall[0]):
59             curOS = syscall[0]
60             newPlat = True
61         if (curArch != syscall[1]):
62             curArch = syscall[1]
63             newPlat = True
64
65         if (newPlat):
66             curPlatName = curOS + "_" + curArch
67             outFile.write("\n/* " + curOS + " " + curArch + " Numbers */\n")
68             outFile.write("Platform " + curPlatName + "_plat(" + curArch + ", " + curOS + ");\n")
69         
70         curName = syscall[2]
71         curPlatformNumber = syscall[3]
72
73         # Write out the two mapping insertions
74         outFile.write(curPlatName + "_syscallNumbers.insert(make_pair<MachSyscall::SyscallIDIndependent, MachSyscall::SyscallIDPlatform>(Syscall::dyn_" + curName + ", " + str(curPlatformNumber) + "));\n")
75         outFile.write(curPlatName + "_syscallNames.insert(make_pair(" + curPlatformNumber + ", \"" + curName + "\"));\n")
76
77     # Write out "footer" information
78     outFile.write("}\n")
79
80     # Close file
81     outFile.close()
82
83 allSyscallNames = []
84 allSyscalls = []
85
86 topDir = "unistd-by-platform"
87 for curOS in os.listdir(topDir):
88     print curOS
89     for curArch in os.listdir(topDir + "/" + curOS):
90         print "\t" + curArch
91         for curFile in os.listdir(topDir + "/" + curOS + "/" + curArch):
92             print "\t\t" + curFile
93
94             # Cheat and add some additional information to the dyn_syscalls.h enum
95             allSyscallNames.append("// " + curOS + "/" + curArch + "/" + curFile)
96
97             # Process the file!
98             curFileHandle = open(topDir + "/" + curOS + "/" + curArch + "/" + curFile, "r")
99
100             # Keep a simple name-number mapping to deal with subsequent syscalls defined with addition
101             sysNumbers = dict()
102
103             for line in curFileHandle:
104                 line = line.rstrip()
105
106                 if (line.find("#define") == 0) and (line.find("NR") != -1):
107                     print line
108
109                     # Extract out system call name and number
110                     define, NR, info = line.partition("NR_")
111                     infoList = info.split()
112                     syscallName = infoList[0]
113                     syscallNumber = infoList[1]
114
115                     # Build a platform-specific descriptor for the current syscall
116                     curSyscall = []
117                     curSyscall.append(curOS)
118                     curSyscall.append(curArch)
119                     curSyscall.append(syscallName)
120                     
121                     # Deal with non-numeric values (e.g., those that do math)
122                     if (syscallNumber.find("NR") != -1):
123                         # Deal with addition shenanigans in unistd.h
124                         left, NR, add = syscallNumber.partition("NR_")
125                         syscallNumber = add.rstrip(")")
126                         baseSyscall, add, val = syscallNumber.partition("+")
127                         baseSyscallNumber = sysNumbers[baseSyscall]
128                         realSyscallNumber = int(float(baseSyscallNumber)) + int(float(val))
129                         syscallNumber = realSyscallNumber
130                        
131                     # Finally, add correct syscall numbers to our map
132                     curSyscall.append(str(syscallNumber))
133                     allSyscalls.append(curSyscall)
134
135                     # Add to our dictionary for later
136                     sysNumbers[syscallName] = syscallNumber
137
138                     # If necessary, add current syscall name to master list
139                     if (allSyscallNames.count(syscallName) == 0):
140                         allSyscallNames.append(syscallName)
141
142 # Generate the two files
143 generate_dyn_syscalls(allSyscallNames)
144 generate_SyscallInformation(allSyscallNames, allSyscalls)
145