blob: c38efb503c3d48fd3bcd988af7c00ff460bdcdd1 [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
Chris Dearman50432122014-02-05 16:59:23 -08005all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07006
7# basic debugging trace support
8# call D_setlevel to set the verbosity level
9# and D(), D2(), D3(), D4() to add traces
10#
Raghu Gandham1fa0d842012-01-27 17:51:42 -080011verbose = 0
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070012
13def D(msg):
14 global verbose
15 if verbose > 0:
16 print msg
17
18def D2(msg):
19 global verbose
20 if verbose >= 2:
21 print msg
22
23def D3(msg):
24 global verbose
25 if verbose >= 3:
26 print msg
27
28def D4(msg):
29 global verbose
30 if verbose >= 4:
31 print msg
32
33def D_setlevel(level):
34 global verbose
35 verbose = level
36
37
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070038# parser for the SYSCALLS.TXT file
39#
40class SysCallsTxtParser:
41 def __init__(self):
42 self.syscalls = []
43 self.lineno = 0
44
Raghu Gandham1fa0d842012-01-27 17:51:42 -080045 def E(self, msg):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070046 print "%d: %s" % (self.lineno, msg)
47
48 def parse_line(self, line):
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080049 """ parse a syscall spec line.
50
51 line processing, format is
H.J. Lu6fe4e872013-10-04 10:03:17 -070052 return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080053 """
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070054 pos_lparen = line.find('(')
55 E = self.E
56 if pos_lparen < 0:
57 E("missing left parenthesis in '%s'" % line)
58 return
59
60 pos_rparen = line.rfind(')')
61 if pos_rparen < 0 or pos_rparen <= pos_lparen:
62 E("missing or misplaced right parenthesis in '%s'" % line)
63 return
64
65 return_type = line[:pos_lparen].strip().split()
66 if len(return_type) < 2:
67 E("missing return type in '%s'" % line)
68 return
69
70 syscall_func = return_type[-1]
71 return_type = string.join(return_type[:-1],' ')
Elliott Hughesd6121652013-09-25 22:43:36 -070072 socketcall_id = -1
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070073
74 pos_colon = syscall_func.find(':')
75 if pos_colon < 0:
76 syscall_name = syscall_func
77 else:
78 if pos_colon == 0 or pos_colon+1 >= len(syscall_func):
79 E("misplaced colon in '%s'" % line)
80 return
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080081
Elliott Hughesd6121652013-09-25 22:43:36 -070082 # now find if there is a socketcall_id for a dispatch-type syscall
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080083 # after the optional 2nd colon
84 pos_colon2 = syscall_func.find(':', pos_colon + 1)
85 if pos_colon2 < 0:
86 syscall_name = syscall_func[pos_colon+1:]
87 syscall_func = syscall_func[:pos_colon]
88 else:
89 if pos_colon2+1 >= len(syscall_func):
90 E("misplaced colon2 in '%s'" % line)
91 return
92 syscall_name = syscall_func[(pos_colon+1):pos_colon2]
Elliott Hughesd6121652013-09-25 22:43:36 -070093 socketcall_id = int(syscall_func[pos_colon2+1:])
The Android Open Source Project4e468ed2008-12-17 18:03:48 -080094 syscall_func = syscall_func[:pos_colon]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070095
H.J. Lu6fe4e872013-10-04 10:03:17 -070096 alias_delim = syscall_func.find('|')
97 if alias_delim > 0:
98 alias_list = syscall_func[alias_delim+1:].strip()
99 syscall_func = syscall_func[:alias_delim]
100 alias_delim = syscall_name.find('|')
101 if alias_delim > 0:
102 syscall_name = syscall_name[:alias_delim]
103 syscall_aliases = string.split(alias_list, ',')
104 else:
105 syscall_aliases = []
106
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700107 if pos_rparen > pos_lparen+1:
108 syscall_params = line[pos_lparen+1:pos_rparen].split(',')
109 params = string.join(syscall_params,',')
110 else:
111 syscall_params = []
112 params = "void"
113
Elliott Hughesd6121652013-09-25 22:43:36 -0700114 t = {
115 "name" : syscall_name,
116 "func" : syscall_func,
H.J. Lu6fe4e872013-10-04 10:03:17 -0700117 "aliases" : syscall_aliases,
Elliott Hughesd6121652013-09-25 22:43:36 -0700118 "params" : syscall_params,
119 "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params),
120 "socketcall_id" : socketcall_id
121 }
122
Elliott Hughes5e522792013-09-24 00:30:25 -0700123 # Parse the architecture list.
Elliott Hughes5e522792013-09-24 00:30:25 -0700124 arch_list = line[pos_rparen+1:].strip()
Elliott Hughesc9da3322013-10-15 18:18:58 -0700125 if arch_list == "all":
Elliott Hughesd6121652013-09-25 22:43:36 -0700126 for arch in all_arches:
127 t[arch] = True
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700128 else:
Elliott Hughes5e522792013-09-24 00:30:25 -0700129 for arch in string.split(arch_list, ','):
Elliott Hughesd6121652013-09-25 22:43:36 -0700130 if arch in all_arches:
131 t[arch] = True
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800132 else:
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800133 E("invalid syscall architecture '%s' in '%s'" % (arch, line))
Elliott Hughes5e522792013-09-24 00:30:25 -0700134 return
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700135
Elliott Hughesd6121652013-09-25 22:43:36 -0700136 self.syscalls.append(t)
137
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800138 global verbose
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200139 if verbose >= 2:
Elliott Hughesd6121652013-09-25 22:43:36 -0700140 print t
Shin-ichiro KAWASAKIce0595d2009-09-01 19:03:06 +0900141
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700142
143 def parse_file(self, file_path):
144 D2("parse_file: %s" % file_path)
145 fp = open(file_path)
146 for line in fp.xreadlines():
147 self.lineno += 1
148 line = line.strip()
149 if not line: continue
150 if line[0] == '#': continue
151 self.parse_line(line)
152
153 fp.close()
154
155
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700156class StringOutput:
157 def __init__(self):
158 self.line = ""
159
160 def write(self,msg):
161 self.line += msg
162 D2("write '%s'" % msg)
163
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700164 def get(self):
165 return self.line