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 | |
| 5 | # support Bionic architectures, add new ones as appropriate |
| 6 | # |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 7 | bionic_archs = [ "arm", "x86", "mips" ] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 8 | |
| 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 Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 13 | verbose = 0 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 14 | |
| 15 | def D(msg): |
| 16 | global verbose |
| 17 | if verbose > 0: |
| 18 | print msg |
| 19 | |
| 20 | def D2(msg): |
| 21 | global verbose |
| 22 | if verbose >= 2: |
| 23 | print msg |
| 24 | |
| 25 | def D3(msg): |
| 26 | global verbose |
| 27 | if verbose >= 3: |
| 28 | print msg |
| 29 | |
| 30 | def D4(msg): |
| 31 | global verbose |
| 32 | if verbose >= 4: |
| 33 | print msg |
| 34 | |
| 35 | def D_setlevel(level): |
| 36 | global verbose |
| 37 | verbose = level |
| 38 | |
| 39 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | # parser for the SYSCALLS.TXT file |
| 41 | # |
| 42 | class SysCallsTxtParser: |
| 43 | def __init__(self): |
| 44 | self.syscalls = [] |
| 45 | self.lineno = 0 |
| 46 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 47 | def E(self, msg): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | print "%d: %s" % (self.lineno, msg) |
| 49 | |
| 50 | def parse_line(self, line): |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 51 | """ parse a syscall spec line. |
| 52 | |
| 53 | line processing, format is |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 54 | return type func_name[:syscall_name[:call_id]] ( [paramlist] ) architecture_list |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 55 | """ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | 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 Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 74 | call_id = -1 |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | |
| 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 Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 83 | |
| 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
| 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 Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 105 | # 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | else: |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 116 | 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 Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 123 | else: |
Elliott Hughes | 5e52279 | 2013-09-24 00:30:25 -0700 | [diff] [blame] | 124 | E("invalid syscall architecture list in '%s'" % line) |
| 125 | return |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 127 | global verbose |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 128 | if verbose >= 2: |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 129 | 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' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 134 | else: |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 135 | 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 KAWASAKI | ce0595d | 2009-09-01 19:03:06 +0900 | [diff] [blame] | 139 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 140 | t = { "armid" : syscall_arm, |
| 141 | "x86id" : syscall_x86, |
| 142 | "mipsid" : syscall_mips, |
| 143 | "common" : syscall_common, |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 144 | "cid" : call_id, |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 145 | "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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | class 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | def get(self): |
| 173 | return self.line |