The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # common python utility routines for the Bionic tool scripts |
| 2 | |
| 3 | import sys, os, commands, string |
| 4 | |
Serban Constantinescu | feaa89a | 2013-10-07 16:49:09 +0100 | [diff] [blame] | 5 | all_arches = [ "aarch64", "arm", "mips", "x86", "x86_64" ] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | |
| 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 Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 11 | verbose = 0 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 12 | |
| 13 | def D(msg): |
| 14 | global verbose |
| 15 | if verbose > 0: |
| 16 | print msg |
| 17 | |
| 18 | def D2(msg): |
| 19 | global verbose |
| 20 | if verbose >= 2: |
| 21 | print msg |
| 22 | |
| 23 | def D3(msg): |
| 24 | global verbose |
| 25 | if verbose >= 3: |
| 26 | print msg |
| 27 | |
| 28 | def D4(msg): |
| 29 | global verbose |
| 30 | if verbose >= 4: |
| 31 | print msg |
| 32 | |
| 33 | def D_setlevel(level): |
| 34 | global verbose |
| 35 | verbose = level |
| 36 | |
| 37 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | # parser for the SYSCALLS.TXT file |
| 39 | # |
| 40 | class SysCallsTxtParser: |
| 41 | def __init__(self): |
| 42 | self.syscalls = [] |
| 43 | self.lineno = 0 |
| 44 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 45 | def E(self, msg): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | print "%d: %s" % (self.lineno, msg) |
| 47 | |
| 48 | def parse_line(self, line): |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 49 | """ parse a syscall spec line. |
| 50 | |
| 51 | line processing, format is |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 52 | return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 53 | """ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | 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 Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 72 | socketcall_id = -1 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | |
| 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 Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 81 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 82 | # now find if there is a socketcall_id for a dispatch-type syscall |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 83 | # 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 Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 93 | socketcall_id = int(syscall_func[pos_colon2+1:]) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 94 | syscall_func = syscall_func[:pos_colon] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 96 | 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | 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 Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 114 | t = { |
| 115 | "name" : syscall_name, |
| 116 | "func" : syscall_func, |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 117 | "aliases" : syscall_aliases, |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 118 | "params" : syscall_params, |
| 119 | "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params), |
| 120 | "socketcall_id" : socketcall_id |
| 121 | } |
| 122 | |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 123 | # Parse the architecture list. |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 124 | arch_list = line[pos_rparen+1:].strip() |
Elliott Hughes | c9da332 | 2013-10-15 18:18:58 -0700 | [diff] [blame] | 125 | if arch_list == "all": |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 126 | for arch in all_arches: |
| 127 | t[arch] = True |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | else: |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 129 | for arch in string.split(arch_list, ','): |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 130 | if arch in all_arches: |
| 131 | t[arch] = True |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 132 | else: |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 133 | E("invalid syscall architecture list in '%s'" % line) |
| 134 | return |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 136 | self.syscalls.append(t) |
| 137 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 138 | global verbose |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 139 | if verbose >= 2: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 140 | print t |
Shin-ichiro KAWASAKI | ce0595d | 2009-09-01 19:03:06 +0900 | [diff] [blame] | 141 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | |
| 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | class 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | def get(self): |
| 165 | return self.line |