blob: baa41bebf3de202baf1cb6019567925515d1ddb7 [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001# common python utility routines for the Bionic tool scripts
2
3import sys, os, commands, string
4
5# support Bionic architectures, add new ones as appropriate
6#
Raghu Gandham1fa0d842012-01-27 17:51:42 -08007bionic_archs = [ "arm", "x86", "mips" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07008
9# basic debugging trace support
10# call D_setlevel to set the verbosity level
11# and D(), D2(), D3(), D4() to add traces
12#
Raghu Gandham1fa0d842012-01-27 17:51:42 -080013verbose = 0
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070014
15def D(msg):
16 global verbose
17 if verbose > 0:
18 print msg
19
20def D2(msg):
21 global verbose
22 if verbose >= 2:
23 print msg
24
25def D3(msg):
26 global verbose
27 if verbose >= 3:
28 print msg
29
30def D4(msg):
31 global verbose
32 if verbose >= 4:
33 print msg
34
35def D_setlevel(level):
36 global verbose
37 verbose = level
38
39
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070040# parser for the SYSCALLS.TXT file
41#
42class SysCallsTxtParser:
43 def __init__(self):
44 self.syscalls = []
45 self.lineno = 0
46
Raghu Gandham1fa0d842012-01-27 17:51:42 -080047 def E(self, msg):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070048 print "%d: %s" % (self.lineno, msg)
49
50 def parse_line(self, line):
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080051 """ parse a syscall spec line.
52
53 line processing, format is
Elliott Hughes5e522792013-09-24 00:30:25 -070054 return type func_name[:syscall_name[:call_id]] ( [paramlist] ) architecture_list
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080055 """
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070056 pos_lparen = line.find('(')
57 E = self.E
58 if pos_lparen < 0:
59 E("missing left parenthesis in '%s'" % line)
60 return
61
62 pos_rparen = line.rfind(')')
63 if pos_rparen < 0 or pos_rparen <= pos_lparen:
64 E("missing or misplaced right parenthesis in '%s'" % line)
65 return
66
67 return_type = line[:pos_lparen].strip().split()
68 if len(return_type) < 2:
69 E("missing return type in '%s'" % line)
70 return
71
72 syscall_func = return_type[-1]
73 return_type = string.join(return_type[:-1],' ')
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080074 call_id = -1
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070075
76 pos_colon = syscall_func.find(':')
77 if pos_colon < 0:
78 syscall_name = syscall_func
79 else:
80 if pos_colon == 0 or pos_colon+1 >= len(syscall_func):
81 E("misplaced colon in '%s'" % line)
82 return
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080083
84 # now find if there is a call_id for a dispatch-type syscall
85 # after the optional 2nd colon
86 pos_colon2 = syscall_func.find(':', pos_colon + 1)
87 if pos_colon2 < 0:
88 syscall_name = syscall_func[pos_colon+1:]
89 syscall_func = syscall_func[:pos_colon]
90 else:
91 if pos_colon2+1 >= len(syscall_func):
92 E("misplaced colon2 in '%s'" % line)
93 return
94 syscall_name = syscall_func[(pos_colon+1):pos_colon2]
95 call_id = int(syscall_func[pos_colon2+1:])
96 syscall_func = syscall_func[:pos_colon]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070097
98 if pos_rparen > pos_lparen+1:
99 syscall_params = line[pos_lparen+1:pos_rparen].split(',')
100 params = string.join(syscall_params,',')
101 else:
102 syscall_params = []
103 params = "void"
104
Elliott Hughes5e522792013-09-24 00:30:25 -0700105 # Parse the architecture list.
106 syscall_common = -1
107 syscall_arm = -1
108 syscall_x86 = -1
109 syscall_mips = -1
110 arch_list = line[pos_rparen+1:].strip()
111 if arch_list == "custom":
112 pass
113 elif arch_list == "all":
114 syscall_common = 1
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700115 else:
Elliott Hughes5e522792013-09-24 00:30:25 -0700116 for arch in string.split(arch_list, ','):
117 if arch == "arm":
118 syscall_arm = 1
119 elif arch == "x86":
120 syscall_x86 = 1
121 elif arch == "mips":
122 syscall_mips = 1
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800123 else:
Elliott Hughes5e522792013-09-24 00:30:25 -0700124 E("invalid syscall architecture list in '%s'" % line)
125 return
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700126
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800127 global verbose
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200128 if verbose >= 2:
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800129 if call_id == -1:
130 if syscall_common == -1:
131 print "%s: %d,%d,%d" % (syscall_name, syscall_arm, syscall_x86, syscall_mips)
132 else:
133 print "%s: %d" % (syscall_name, syscall_common)
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200134 else:
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800135 if syscall_common == -1:
136 print "%s(%d): %d,%d,%d" % (syscall_name, call_id, syscall_arm, syscall_x86, syscall_mips)
137 else:
138 print "%s(%d): %d" % (syscall_name, call_id, syscall_common)
Shin-ichiro KAWASAKIce0595d2009-09-01 19:03:06 +0900139
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800140 t = { "armid" : syscall_arm,
141 "x86id" : syscall_x86,
142 "mipsid" : syscall_mips,
143 "common" : syscall_common,
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800144 "cid" : call_id,
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700145 "name" : syscall_name,
146 "func" : syscall_func,
147 "params" : syscall_params,
148 "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params) }
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700149 self.syscalls.append(t)
150
151 def parse_file(self, file_path):
152 D2("parse_file: %s" % file_path)
153 fp = open(file_path)
154 for line in fp.xreadlines():
155 self.lineno += 1
156 line = line.strip()
157 if not line: continue
158 if line[0] == '#': continue
159 self.parse_line(line)
160
161 fp.close()
162
163
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700164class StringOutput:
165 def __init__(self):
166 self.line = ""
167
168 def write(self,msg):
169 self.line += msg
170 D2("write '%s'" % msg)
171
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700172 def get(self):
173 return self.line