blob: eed9001fc4da4bb770711d8bbe34d14493319ec8 [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
Elliott Hughesd6121652013-09-25 22:43:36 -07005all_arches = [ "arm", "mips", "x86" ]
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
Elliott Hughesd6121652013-09-25 22:43:36 -070052 return type func_name[: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
96 if pos_rparen > pos_lparen+1:
97 syscall_params = line[pos_lparen+1:pos_rparen].split(',')
98 params = string.join(syscall_params,',')
99 else:
100 syscall_params = []
101 params = "void"
102
Elliott Hughesd6121652013-09-25 22:43:36 -0700103 t = {
104 "name" : syscall_name,
105 "func" : syscall_func,
106 "params" : syscall_params,
107 "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params),
108 "socketcall_id" : socketcall_id
109 }
110
Elliott Hughes5e522792013-09-24 00:30:25 -0700111 # Parse the architecture list.
Elliott Hughes5e522792013-09-24 00:30:25 -0700112 arch_list = line[pos_rparen+1:].strip()
113 if arch_list == "custom":
114 pass
115 elif arch_list == "all":
Elliott Hughesd6121652013-09-25 22:43:36 -0700116 for arch in all_arches:
117 t[arch] = True
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700118 else:
Elliott Hughes5e522792013-09-24 00:30:25 -0700119 for arch in string.split(arch_list, ','):
Elliott Hughesd6121652013-09-25 22:43:36 -0700120 if arch in all_arches:
121 t[arch] = True
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800122 else:
Elliott Hughes5e522792013-09-24 00:30:25 -0700123 E("invalid syscall architecture list in '%s'" % line)
124 return
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700125
Elliott Hughesd6121652013-09-25 22:43:36 -0700126 self.syscalls.append(t)
127
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800128 global verbose
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200129 if verbose >= 2:
Elliott Hughesd6121652013-09-25 22:43:36 -0700130 print t
Shin-ichiro KAWASAKIce0595d2009-09-01 19:03:06 +0900131
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700132
133 def parse_file(self, file_path):
134 D2("parse_file: %s" % file_path)
135 fp = open(file_path)
136 for line in fp.xreadlines():
137 self.lineno += 1
138 line = line.strip()
139 if not line: continue
140 if line[0] == '#': continue
141 self.parse_line(line)
142
143 fp.close()
144
145
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700146class StringOutput:
147 def __init__(self):
148 self.line = ""
149
150 def write(self,msg):
151 self.line += msg
152 D2("write '%s'" % msg)
153
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700154 def get(self):
155 return self.line