Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 3 | # Copyright (C) 2013 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 16 | |
| 17 | """Module for looking up symbolic debugging information. |
| 18 | |
| 19 | The information can include symbol names, offsets, and source locations. |
| 20 | """ |
| 21 | |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 22 | import atexit |
Elliott Hughes | 0836593 | 2014-06-13 18:12:25 -0700 | [diff] [blame] | 23 | import glob |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 24 | import os |
Yang Ni | e4b2a1a | 2014-11-06 17:42:33 -0800 | [diff] [blame] | 25 | import platform |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 26 | import re |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 27 | import signal |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 28 | import subprocess |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 29 | import unittest |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | e547eb3 | 2018-10-29 18:31:37 -0700 | [diff] [blame] | 31 | try: |
| 32 | ANDROID_BUILD_TOP = str(os.environ["ANDROID_BUILD_TOP"]) |
| 33 | if not ANDROID_BUILD_TOP: |
| 34 | ANDROID_BUILD_TOP = "." |
| 35 | except: |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 36 | ANDROID_BUILD_TOP = "." |
| 37 | |
| 38 | def FindSymbolsDir(): |
| 39 | saveddir = os.getcwd() |
| 40 | os.chdir(ANDROID_BUILD_TOP) |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 41 | stream = None |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 42 | try: |
Dan Willemsen | d3fc8fa | 2017-10-17 14:04:56 -0700 | [diff] [blame] | 43 | cmd = "build/soong/soong_ui.bash --dumpvar-mode --abs TARGET_OUT_UNSTRIPPED" |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 44 | stream = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 45 | return os.path.join(ANDROID_BUILD_TOP, str(stream.read().strip())) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 46 | finally: |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 47 | if stream is not None: |
| 48 | stream.close() |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 49 | os.chdir(saveddir) |
| 50 | |
| 51 | SYMBOLS_DIR = FindSymbolsDir() |
| 52 | |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 53 | ARCH = None |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 55 | |
| 56 | # These are private. Do not access them from other modules. |
| 57 | _CACHED_TOOLCHAIN = None |
| 58 | _CACHED_TOOLCHAIN_ARCH = None |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 59 | _CACHED_CXX_FILT = None |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 60 | |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 61 | # Caches for symbolized information. |
| 62 | _SYMBOL_INFORMATION_ADDR2LINE_CACHE = {} |
| 63 | _SYMBOL_INFORMATION_OBJDUMP_CACHE = {} |
| 64 | _SYMBOL_DEMANGLING_CACHE = {} |
| 65 | |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 66 | # Caches for pipes to subprocesses. |
| 67 | |
| 68 | class ProcessCache: |
| 69 | _cmd2pipe = {} |
| 70 | _lru = [] |
| 71 | |
| 72 | # Max number of open pipes. |
| 73 | _PIPE_MAX_OPEN = 10 |
| 74 | |
| 75 | def GetProcess(self, cmd): |
| 76 | cmd_tuple = tuple(cmd) # Need to use a tuple as lists can't be dict keys. |
| 77 | # Pipe already available? |
| 78 | if cmd_tuple in self._cmd2pipe: |
| 79 | pipe = self._cmd2pipe[cmd_tuple] |
| 80 | # Update LRU. |
| 81 | self._lru = [(cmd_tuple, pipe)] + [i for i in self._lru if i[0] != cmd_tuple] |
| 82 | return pipe |
| 83 | |
| 84 | # Not cached, yet. Open a new one. |
| 85 | |
| 86 | # Check if too many are open, close the old ones. |
| 87 | while len(self._lru) >= self._PIPE_MAX_OPEN: |
| 88 | open_cmd, open_pipe = self._lru.pop() |
| 89 | del self._cmd2pipe[open_cmd] |
| 90 | self.TerminateProcess(open_pipe) |
| 91 | |
| 92 | # Create and put into cache. |
| 93 | pipe = self.SpawnProcess(cmd) |
| 94 | self._cmd2pipe[cmd_tuple] = pipe |
| 95 | self._lru = [(cmd_tuple, pipe)] + self._lru |
| 96 | return pipe |
| 97 | |
| 98 | def SpawnProcess(self, cmd): |
| 99 | return subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 100 | |
| 101 | def TerminateProcess(self, pipe): |
| 102 | pipe.stdin.close() |
| 103 | pipe.stdout.close() |
| 104 | pipe.terminate() |
| 105 | pipe.wait() |
| 106 | |
| 107 | def KillAllProcesses(self): |
| 108 | for _, open_pipe in self._lru: |
| 109 | self.TerminateProcess(open_pipe) |
| 110 | _cmd2pipe = {} |
| 111 | _lru = [] |
| 112 | |
| 113 | |
| 114 | _PIPE_ADDR2LINE_CACHE = ProcessCache() |
| 115 | _PIPE_CPPFILT_CACHE = ProcessCache() |
| 116 | |
| 117 | |
| 118 | # Process cache cleanup on shutdown. |
| 119 | |
| 120 | def CloseAllPipes(): |
| 121 | _PIPE_ADDR2LINE_CACHE.KillAllProcesses() |
| 122 | _PIPE_CPPFILT_CACHE.KillAllProcesses() |
| 123 | |
| 124 | |
| 125 | atexit.register(CloseAllPipes) |
| 126 | |
| 127 | |
| 128 | def PipeTermHandler(signum, frame): |
| 129 | CloseAllPipes() |
| 130 | os._exit(0) |
| 131 | |
| 132 | |
| 133 | for sig in (signal.SIGABRT, signal.SIGINT, signal.SIGTERM): |
| 134 | signal.signal(sig, PipeTermHandler) |
| 135 | |
| 136 | |
| 137 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 138 | |
Elliott Hughes | 0836593 | 2014-06-13 18:12:25 -0700 | [diff] [blame] | 139 | def ToolPath(tool, toolchain=None): |
| 140 | """Return a fully-qualified path to the specified tool""" |
| 141 | if not toolchain: |
| 142 | toolchain = FindToolchain() |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 143 | return os.path.join(toolchain, tool) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 144 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 145 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 146 | def FindToolchain(): |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 147 | """Returns the toolchain matching ARCH.""" |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 148 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 149 | global _CACHED_TOOLCHAIN, _CACHED_TOOLCHAIN_ARCH |
| 150 | if _CACHED_TOOLCHAIN is not None and _CACHED_TOOLCHAIN_ARCH == ARCH: |
| 151 | return _CACHED_TOOLCHAIN |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 152 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 153 | llvm_binutils_dir = ANDROID_BUILD_TOP + "/prebuilts/clang/host/linux-x86/llvm-binutils-stable/"; |
| 154 | if not os.path.exists(llvm_binutils_dir): |
| 155 | raise Exception("Could not find llvm tool chain directory %s" % (llvm_binutils_dir)) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 156 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 157 | _CACHED_TOOLCHAIN = llvm_binutils_dir |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 158 | _CACHED_TOOLCHAIN_ARCH = ARCH |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 159 | print("Using %s toolchain from: %s" % (_CACHED_TOOLCHAIN_ARCH, _CACHED_TOOLCHAIN)) |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 160 | return _CACHED_TOOLCHAIN |
| 161 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 162 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 163 | def SymbolInformation(lib, addr): |
| 164 | """Look up symbol information about an address. |
| 165 | |
| 166 | Args: |
| 167 | lib: library (or executable) pathname containing symbols |
| 168 | addr: string hexidecimal address |
| 169 | |
| 170 | Returns: |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 171 | A list of the form [(source_symbol, source_location, |
| 172 | object_symbol_with_offset)]. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 173 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 174 | If the function has been inlined then the list may contain |
| 175 | more than one element with the symbols for the most deeply |
| 176 | nested inlined location appearing first. The list is |
| 177 | always non-empty, even if no information is available. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 178 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 179 | Usually you want to display the source_location and |
| 180 | object_symbol_with_offset from the last element in the list. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 181 | """ |
| 182 | info = SymbolInformationForSet(lib, set([addr])) |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 183 | return (info and info.get(addr)) or [(None, None, None)] |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def SymbolInformationForSet(lib, unique_addrs): |
| 187 | """Look up symbol information for a set of addresses from the given library. |
| 188 | |
| 189 | Args: |
| 190 | lib: library (or executable) pathname containing symbols |
| 191 | unique_addrs: set of hexidecimal addresses |
| 192 | |
| 193 | Returns: |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 194 | A dictionary of the form {addr: [(source_symbol, source_location, |
| 195 | object_symbol_with_offset)]} where each address has a list of |
| 196 | associated symbols and locations. The list is always non-empty. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 197 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 198 | If the function has been inlined then the list may contain |
| 199 | more than one element with the symbols for the most deeply |
| 200 | nested inlined location appearing first. The list is |
| 201 | always non-empty, even if no information is available. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 202 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 203 | Usually you want to display the source_location and |
| 204 | object_symbol_with_offset from the last element in the list. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 205 | """ |
| 206 | if not lib: |
| 207 | return None |
| 208 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 209 | addr_to_line = CallLlvmSymbolizerForSet(lib, unique_addrs) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 210 | if not addr_to_line: |
| 211 | return None |
| 212 | |
| 213 | addr_to_objdump = CallObjdumpForSet(lib, unique_addrs) |
| 214 | if not addr_to_objdump: |
| 215 | return None |
| 216 | |
| 217 | result = {} |
| 218 | for addr in unique_addrs: |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 219 | source_info = addr_to_line.get(addr) |
| 220 | if not source_info: |
| 221 | source_info = [(None, None)] |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 222 | if addr in addr_to_objdump: |
| 223 | (object_symbol, object_offset) = addr_to_objdump.get(addr) |
| 224 | object_symbol_with_offset = FormatSymbolWithOffset(object_symbol, |
| 225 | object_offset) |
| 226 | else: |
| 227 | object_symbol_with_offset = None |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 228 | result[addr] = [(source_symbol, source_location, object_symbol_with_offset) |
| 229 | for (source_symbol, source_location) in source_info] |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 230 | |
| 231 | return result |
| 232 | |
| 233 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 234 | def CallLlvmSymbolizerForSet(lib, unique_addrs): |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 235 | """Look up line and symbol information for a set of addresses. |
| 236 | |
| 237 | Args: |
| 238 | lib: library (or executable) pathname containing symbols |
| 239 | unique_addrs: set of string hexidecimal addresses look up. |
| 240 | |
| 241 | Returns: |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 242 | A dictionary of the form {addr: [(symbol, file:line)]} where |
| 243 | each address has a list of associated symbols and locations |
| 244 | or an empty list if no symbol information was found. |
| 245 | |
| 246 | If the function has been inlined then the list may contain |
| 247 | more than one element with the symbols for the most deeply |
| 248 | nested inlined location appearing first. |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 249 | """ |
| 250 | if not lib: |
| 251 | return None |
| 252 | |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 253 | result = {} |
| 254 | addrs = sorted(unique_addrs) |
| 255 | |
| 256 | if lib in _SYMBOL_INFORMATION_ADDR2LINE_CACHE: |
| 257 | addr_cache = _SYMBOL_INFORMATION_ADDR2LINE_CACHE[lib] |
| 258 | |
| 259 | # Go through and handle all known addresses. |
| 260 | for x in range(len(addrs)): |
| 261 | next_addr = addrs.pop(0) |
| 262 | if next_addr in addr_cache: |
| 263 | result[next_addr] = addr_cache[next_addr] |
| 264 | else: |
| 265 | # Re-add, needs to be symbolized. |
| 266 | addrs.append(next_addr) |
| 267 | |
| 268 | if not addrs: |
| 269 | # Everything was cached, we're done. |
| 270 | return result |
| 271 | else: |
| 272 | addr_cache = {} |
| 273 | _SYMBOL_INFORMATION_ADDR2LINE_CACHE[lib] = addr_cache |
| 274 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 275 | symbols = SYMBOLS_DIR + lib |
| 276 | if not os.path.exists(symbols): |
Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 277 | symbols = lib |
| 278 | if not os.path.exists(symbols): |
| 279 | return None |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 280 | |
Christopher Ferris | 5f1b4f0 | 2016-09-19 13:24:37 -0700 | [diff] [blame] | 281 | # Make sure the symbols path is not a directory. |
| 282 | if os.path.isdir(symbols): |
| 283 | return None |
| 284 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 285 | cmd = [ToolPath("llvm-symbolizer"), "--functions", "--inlines", |
| 286 | "--demangle", "--obj=" + symbols, "--output-style=GNU"] |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 287 | child = _PIPE_ADDR2LINE_CACHE.GetProcess(cmd) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 288 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 289 | for addr in addrs: |
Christopher Ferris | 6fc7aef | 2018-08-09 12:40:05 -0700 | [diff] [blame] | 290 | try: |
| 291 | child.stdin.write("0x%s\n" % addr) |
| 292 | child.stdin.flush() |
| 293 | records = [] |
| 294 | first = True |
| 295 | while True: |
| 296 | symbol = child.stdout.readline().strip() |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 297 | if not symbol: |
Christopher Ferris | 6fc7aef | 2018-08-09 12:40:05 -0700 | [diff] [blame] | 298 | break |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 299 | location = child.stdout.readline().strip() |
Christopher Ferris | 6fc7aef | 2018-08-09 12:40:05 -0700 | [diff] [blame] | 300 | records.append((symbol, location)) |
| 301 | if first: |
| 302 | # Write a blank line as a sentinel so we know when to stop |
| 303 | # reading inlines from the output. |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 304 | # The blank line will cause llvm-symbolizer to emit a blank line. |
Christopher Ferris | 6fc7aef | 2018-08-09 12:40:05 -0700 | [diff] [blame] | 305 | child.stdin.write("\n") |
| 306 | first = False |
| 307 | except IOError as e: |
| 308 | # Remove the / in front of the library name to match other output. |
| 309 | records = [(None, lib[1:] + " ***Error: " + str(e))] |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 310 | result[addr] = records |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 311 | addr_cache[addr] = records |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 312 | return result |
| 313 | |
| 314 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 315 | def StripPC(addr): |
| 316 | """Strips the Thumb bit a program counter address when appropriate. |
| 317 | |
| 318 | Args: |
| 319 | addr: the program counter address |
| 320 | |
| 321 | Returns: |
| 322 | The stripped program counter address. |
| 323 | """ |
| 324 | global ARCH |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 325 | if ARCH == "arm": |
| 326 | return addr & ~1 |
| 327 | return addr |
| 328 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 329 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 330 | def CallObjdumpForSet(lib, unique_addrs): |
| 331 | """Use objdump to find out the names of the containing functions. |
| 332 | |
| 333 | Args: |
| 334 | lib: library (or executable) pathname containing symbols |
| 335 | unique_addrs: set of string hexidecimal addresses to find the functions for. |
| 336 | |
| 337 | Returns: |
| 338 | A dictionary of the form {addr: (string symbol, offset)}. |
| 339 | """ |
| 340 | if not lib: |
| 341 | return None |
| 342 | |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 343 | result = {} |
| 344 | addrs = sorted(unique_addrs) |
| 345 | |
| 346 | addr_cache = None |
| 347 | if lib in _SYMBOL_INFORMATION_OBJDUMP_CACHE: |
| 348 | addr_cache = _SYMBOL_INFORMATION_OBJDUMP_CACHE[lib] |
| 349 | |
| 350 | # Go through and handle all known addresses. |
| 351 | for x in range(len(addrs)): |
| 352 | next_addr = addrs.pop(0) |
| 353 | if next_addr in addr_cache: |
| 354 | result[next_addr] = addr_cache[next_addr] |
| 355 | else: |
| 356 | # Re-add, needs to be symbolized. |
| 357 | addrs.append(next_addr) |
| 358 | |
| 359 | if not addrs: |
| 360 | # Everything was cached, we're done. |
| 361 | return result |
| 362 | else: |
| 363 | addr_cache = {} |
| 364 | _SYMBOL_INFORMATION_OBJDUMP_CACHE[lib] = addr_cache |
| 365 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 366 | symbols = SYMBOLS_DIR + lib |
| 367 | if not os.path.exists(symbols): |
Christopher Ferris | ece64c4 | 2015-08-20 20:09:09 -0700 | [diff] [blame] | 368 | symbols = lib |
| 369 | if not os.path.exists(symbols): |
| 370 | return None |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 371 | |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 372 | start_addr_dec = str(StripPC(int(addrs[0], 16))) |
| 373 | stop_addr_dec = str(StripPC(int(addrs[-1], 16)) + 8) |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 374 | cmd = [ToolPath("llvm-objdump"), |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 375 | "--section=.text", |
| 376 | "--demangle", |
| 377 | "--disassemble", |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 378 | "--start-address=" + start_addr_dec, |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 379 | "--stop-address=" + stop_addr_dec, |
| 380 | symbols] |
| 381 | |
| 382 | # Function lines look like: |
| 383 | # 000177b0 <android::IBinder::~IBinder()+0x2c>: |
| 384 | # We pull out the address and function first. Then we check for an optional |
| 385 | # offset. This is tricky due to functions that look like "operator+(..)+0x2c" |
| 386 | func_regexp = re.compile("(^[a-f0-9]*) \<(.*)\>:$") |
| 387 | offset_regexp = re.compile("(.*)\+0x([a-f0-9]*)") |
| 388 | |
| 389 | # A disassembly line looks like: |
| 390 | # 177b2: b510 push {r4, lr} |
| 391 | asm_regexp = re.compile("(^[ a-f0-9]*):[ a-f0-0]*.*$") |
| 392 | |
| 393 | current_symbol = None # The current function symbol in the disassembly. |
| 394 | current_symbol_addr = 0 # The address of the current function. |
| 395 | addr_index = 0 # The address that we are currently looking for. |
| 396 | |
| 397 | stream = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 398 | for line in stream: |
| 399 | # Is it a function line like: |
| 400 | # 000177b0 <android::IBinder::~IBinder()>: |
| 401 | components = func_regexp.match(line) |
| 402 | if components: |
| 403 | # This is a new function, so record the current function and its address. |
| 404 | current_symbol_addr = int(components.group(1), 16) |
| 405 | current_symbol = components.group(2) |
| 406 | |
| 407 | # Does it have an optional offset like: "foo(..)+0x2c"? |
| 408 | components = offset_regexp.match(current_symbol) |
| 409 | if components: |
| 410 | current_symbol = components.group(1) |
| 411 | offset = components.group(2) |
| 412 | if offset: |
| 413 | current_symbol_addr -= int(offset, 16) |
| 414 | |
| 415 | # Is it an disassembly line like: |
| 416 | # 177b2: b510 push {r4, lr} |
| 417 | components = asm_regexp.match(line) |
| 418 | if components: |
| 419 | addr = components.group(1) |
| 420 | target_addr = addrs[addr_index] |
| 421 | i_addr = int(addr, 16) |
Ben Cheng | b42dad0 | 2013-04-25 15:14:04 -0700 | [diff] [blame] | 422 | i_target = StripPC(int(target_addr, 16)) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 423 | if i_addr == i_target: |
| 424 | result[target_addr] = (current_symbol, i_target - current_symbol_addr) |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 425 | addr_cache[target_addr] = result[target_addr] |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 426 | addr_index += 1 |
| 427 | if addr_index >= len(addrs): |
| 428 | break |
| 429 | stream.close() |
| 430 | |
| 431 | return result |
| 432 | |
| 433 | |
| 434 | def CallCppFilt(mangled_symbol): |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 435 | if mangled_symbol in _SYMBOL_DEMANGLING_CACHE: |
| 436 | return _SYMBOL_DEMANGLING_CACHE[mangled_symbol] |
| 437 | |
Christopher Ferris | 49eda0e | 2020-12-09 14:34:01 -0800 | [diff] [blame^] | 438 | # TODO: Replace with llvm-cxxfilt when available. |
| 439 | global _CACHED_CXX_FILT |
| 440 | if not _CACHED_CXX_FILT: |
| 441 | os_name = platform.system().lower() |
| 442 | toolchains = glob.glob("%s/prebuilts/gcc/%s-*/host/*-linux-*/bin/*c++filt" % |
| 443 | (ANDROID_BUILD_TOP, os_name)) |
| 444 | if not toolchains: |
| 445 | raise Exception("Could not find gcc c++filt tool") |
| 446 | _CACHED_CXX_FILT = sorted(toolchains)[-1] |
| 447 | |
| 448 | cmd = [_CACHED_CXX_FILT] |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 449 | process = _PIPE_CPPFILT_CACHE.GetProcess(cmd) |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 450 | process.stdin.write(mangled_symbol) |
| 451 | process.stdin.write("\n") |
Andreas Gampe | 46b00d6 | 2017-05-17 15:12:27 -0700 | [diff] [blame] | 452 | process.stdin.flush() |
| 453 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 454 | demangled_symbol = process.stdout.readline().strip() |
Andreas Gampe | 3d97a46 | 2017-05-17 14:16:45 -0700 | [diff] [blame] | 455 | |
| 456 | _SYMBOL_DEMANGLING_CACHE[mangled_symbol] = demangled_symbol |
| 457 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 458 | return demangled_symbol |
| 459 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 460 | |
Iliyan Malchev | 4929d6a | 2011-08-04 17:44:40 -0700 | [diff] [blame] | 461 | def FormatSymbolWithOffset(symbol, offset): |
| 462 | if offset == 0: |
| 463 | return symbol |
| 464 | return "%s+%d" % (symbol, offset) |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 465 | |
| 466 | |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 467 | def GetAbiFromToolchain(toolchain_var, bits): |
| 468 | toolchain = os.environ.get(toolchain_var) |
| 469 | if not toolchain: |
| 470 | return None |
| 471 | |
| 472 | toolchain_match = re.search("\/(aarch64|arm|mips|x86)\/", toolchain) |
| 473 | if toolchain_match: |
| 474 | abi = toolchain_match.group(1) |
| 475 | if abi == "aarch64": |
| 476 | return "arm64" |
| 477 | elif bits == 64: |
| 478 | if abi == "x86": |
| 479 | return "x86_64" |
| 480 | elif abi == "mips": |
| 481 | return "mips64" |
| 482 | return abi |
| 483 | return None |
| 484 | |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 485 | def Get32BitArch(): |
| 486 | # Check for ANDROID_TOOLCHAIN_2ND_ARCH first, if set, use that. |
| 487 | # If not try ANDROID_TOOLCHAIN to find the arch. |
| 488 | # If this is not set, then default to arm. |
| 489 | arch = GetAbiFromToolchain("ANDROID_TOOLCHAIN_2ND_ARCH", 32) |
| 490 | if not arch: |
| 491 | arch = GetAbiFromToolchain("ANDROID_TOOLCHAIN", 32) |
| 492 | if not arch: |
| 493 | return "arm" |
| 494 | return arch |
| 495 | |
| 496 | def Get64BitArch(): |
| 497 | # Check for ANDROID_TOOLCHAIN, if it is set, we can figure out the |
| 498 | # arch this way. If this is not set, then default to arm64. |
| 499 | arch = GetAbiFromToolchain("ANDROID_TOOLCHAIN", 64) |
| 500 | if not arch: |
| 501 | return "arm64" |
| 502 | return arch |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 503 | |
| 504 | def SetAbi(lines): |
| 505 | global ARCH |
| 506 | |
| 507 | abi_line = re.compile("ABI: \'(.*)\'") |
| 508 | trace_line = re.compile("\#[0-9]+[ \t]+..[ \t]+([0-9a-f]{8}|[0-9a-f]{16})([ \t]+|$)") |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 509 | asan_trace_line = re.compile("\#[0-9]+[ \t]+0x([0-9a-f]+)[ \t]+") |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 510 | |
| 511 | ARCH = None |
| 512 | for line in lines: |
| 513 | abi_match = abi_line.search(line) |
| 514 | if abi_match: |
| 515 | ARCH = abi_match.group(1) |
| 516 | break |
| 517 | trace_match = trace_line.search(line) |
| 518 | if trace_match: |
| 519 | # Try to guess the arch, we know the bitness. |
| 520 | if len(trace_match.group(1)) == 16: |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 521 | ARCH = Get64BitArch() |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 522 | else: |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 523 | ARCH = Get32BitArch() |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 524 | break |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 525 | asan_trace_match = asan_trace_line.search(line) |
| 526 | if asan_trace_match: |
| 527 | # We might be able to guess the bitness by the length of the address. |
| 528 | if len(asan_trace_match.group(1)) > 8: |
| 529 | ARCH = Get64BitArch() |
| 530 | # We know for a fact this is 64 bit, so we are done. |
| 531 | break |
| 532 | else: |
| 533 | ARCH = Get32BitArch() |
| 534 | # This might be 32 bit, or just a small address. Keep going in this |
| 535 | # case, but if we couldn't figure anything else out, go with 32 bit. |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 536 | if not ARCH: |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 537 | raise Exception("Could not determine arch from input, use --arch=XXX to specify it") |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 538 | |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 539 | |
| 540 | class FindToolchainTests(unittest.TestCase): |
| 541 | def assert_toolchain_found(self, abi): |
| 542 | global ARCH |
| 543 | ARCH = abi |
| 544 | FindToolchain() # Will throw on failure. |
| 545 | |
Andreas Gampe | e547eb3 | 2018-10-29 18:31:37 -0700 | [diff] [blame] | 546 | @unittest.skipIf(ANDROID_BUILD_TOP == '.', 'Test only supported in an Android tree.') |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 547 | def test_toolchains_found(self): |
| 548 | self.assert_toolchain_found("arm") |
| 549 | self.assert_toolchain_found("arm64") |
| 550 | self.assert_toolchain_found("mips") |
| 551 | self.assert_toolchain_found("x86") |
| 552 | self.assert_toolchain_found("x86_64") |
| 553 | |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 554 | class SetArchTests(unittest.TestCase): |
| 555 | def test_abi_check(self): |
| 556 | global ARCH |
| 557 | |
| 558 | SetAbi(["ABI: 'arm'"]) |
| 559 | self.assertEqual(ARCH, "arm") |
| 560 | SetAbi(["ABI: 'arm64'"]) |
| 561 | self.assertEqual(ARCH, "arm64") |
| 562 | |
| 563 | SetAbi(["ABI: 'mips'"]) |
| 564 | self.assertEqual(ARCH, "mips") |
| 565 | SetAbi(["ABI: 'mips64'"]) |
| 566 | self.assertEqual(ARCH, "mips64") |
| 567 | |
| 568 | SetAbi(["ABI: 'x86'"]) |
| 569 | self.assertEqual(ARCH, "x86") |
| 570 | SetAbi(["ABI: 'x86_64'"]) |
| 571 | self.assertEqual(ARCH, "x86_64") |
| 572 | |
| 573 | def test_32bit_trace_line_toolchain(self): |
| 574 | global ARCH |
| 575 | |
| 576 | os.environ.clear() |
| 577 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/arm/arm-linux-androideabi-4.9/bin" |
| 578 | SetAbi(["#00 pc 000374e0"]) |
| 579 | self.assertEqual(ARCH, "arm") |
| 580 | |
| 581 | os.environ.clear() |
| 582 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/mips/arm-linux-androideabi-4.9/bin" |
| 583 | SetAbi(["#00 pc 000374e0"]) |
| 584 | self.assertEqual(ARCH, "mips") |
| 585 | |
| 586 | os.environ.clear() |
| 587 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/x86/arm-linux-androideabi-4.9/bin" |
| 588 | SetAbi(["#00 pc 000374e0"]) |
| 589 | self.assertEqual(ARCH, "x86") |
| 590 | |
| 591 | def test_32bit_trace_line_toolchain_2nd(self): |
| 592 | global ARCH |
| 593 | |
| 594 | os.environ.clear() |
| 595 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/arm/arm-linux-androideabi-4.9/bin" |
| 596 | os.environ["ANDROID_TOOLCHAIN_ARCH"] = "linux-x86/aarch64/aarch64-linux-android-4.9/bin" |
| 597 | SetAbi(["#00 pc 000374e0"]) |
| 598 | self.assertEqual(ARCH, "arm") |
| 599 | |
| 600 | os.environ.clear() |
| 601 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/mips/mips-linux-androideabi-4.9/bin" |
| 602 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/unknown/unknown-linux-androideabi-4.9/bin" |
| 603 | SetAbi(["#00 pc 000374e0"]) |
| 604 | self.assertEqual(ARCH, "mips") |
| 605 | |
| 606 | os.environ.clear() |
| 607 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/x86/x86-linux-androideabi-4.9/bin" |
| 608 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/unknown/unknown-linux-androideabi-4.9/bin" |
| 609 | SetAbi(["#00 pc 000374e0"]) |
| 610 | self.assertEqual(ARCH, "x86") |
| 611 | |
| 612 | def test_64bit_trace_line_toolchain(self): |
| 613 | global ARCH |
| 614 | |
| 615 | os.environ.clear() |
| 616 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/aarch/aarch-linux-androideabi-4.9/bin" |
| 617 | SetAbi(["#00 pc 00000000000374e0"]) |
| 618 | self.assertEqual(ARCH, "arm64") |
| 619 | |
| 620 | os.environ.clear() |
| 621 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/mips/arm-linux-androideabi-4.9/bin" |
| 622 | SetAbi(["#00 pc 00000000000374e0"]) |
| 623 | self.assertEqual(ARCH, "mips64") |
| 624 | |
| 625 | os.environ.clear() |
| 626 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/x86/arm-linux-androideabi-4.9/bin" |
| 627 | SetAbi(["#00 pc 00000000000374e0"]) |
| 628 | self.assertEqual(ARCH, "x86_64") |
| 629 | |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 630 | def test_trace_default_abis(self): |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 631 | global ARCH |
| 632 | |
| 633 | os.environ.clear() |
| 634 | SetAbi(["#00 pc 000374e0"]) |
| 635 | self.assertEqual(ARCH, "arm") |
| 636 | SetAbi(["#00 pc 00000000000374e0"]) |
| 637 | self.assertEqual(ARCH, "arm64") |
| 638 | |
Christopher Ferris | 5b820ba | 2016-09-06 14:07:29 -0700 | [diff] [blame] | 639 | def test_32bit_asan_trace_line_toolchain(self): |
| 640 | global ARCH |
| 641 | |
| 642 | os.environ.clear() |
| 643 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/arm/arm-linux-androideabi-4.9/bin" |
| 644 | SetAbi(["#10 0xb5eeba5d (/system/vendor/lib/egl/libGLESv1_CM_adreno.so+0xfa5d)"]) |
| 645 | self.assertEqual(ARCH, "arm") |
| 646 | |
| 647 | os.environ.clear() |
| 648 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/mips/arm-linux-androideabi-4.9/bin" |
| 649 | SetAbi(["#10 0xb5eeba5d (/system/vendor/lib/egl/libGLESv1_CM_adreno.so+0xfa5d)"]) |
| 650 | self.assertEqual(ARCH, "mips") |
| 651 | |
| 652 | os.environ.clear() |
| 653 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/x86/arm-linux-androideabi-4.9/bin" |
| 654 | SetAbi(["#10 0xb5eeba5d (/system/vendor/lib/egl/libGLESv1_CM_adreno.so+0xfa5d)"]) |
| 655 | self.assertEqual(ARCH, "x86") |
| 656 | |
| 657 | def test_32bit_asan_trace_line_toolchain_2nd(self): |
| 658 | global ARCH |
| 659 | |
| 660 | os.environ.clear() |
| 661 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/arm/arm-linux-androideabi-4.9/bin" |
| 662 | os.environ["ANDROID_TOOLCHAIN_ARCH"] = "linux-x86/aarch64/aarch64-linux-android-4.9/bin" |
| 663 | SetAbi(["#3 0xae1725b5 (/system/vendor/lib/libllvm-glnext.so+0x6435b5)"]) |
| 664 | self.assertEqual(ARCH, "arm") |
| 665 | |
| 666 | os.environ.clear() |
| 667 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/mips/mips-linux-androideabi-4.9/bin" |
| 668 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/unknown/unknown-linux-androideabi-4.9/bin" |
| 669 | SetAbi(["#3 0xae1725b5 (/system/vendor/lib/libllvm-glnext.so+0x6435b5)"]) |
| 670 | self.assertEqual(ARCH, "mips") |
| 671 | |
| 672 | os.environ.clear() |
| 673 | os.environ["ANDROID_TOOLCHAIN_2ND_ARCH"] = "linux-x86/x86/x86-linux-androideabi-4.9/bin" |
| 674 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/unknown/unknown-linux-androideabi-4.9/bin" |
| 675 | SetAbi(["#3 0xae1725b5 (/system/vendor/lib/libllvm-glnext.so+0x6435b5)"]) |
| 676 | self.assertEqual(ARCH, "x86") |
| 677 | |
| 678 | def test_64bit_asan_trace_line_toolchain(self): |
| 679 | global ARCH |
| 680 | |
| 681 | os.environ.clear() |
| 682 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/aarch/aarch-linux-androideabi-4.9/bin" |
| 683 | SetAbi(["#0 0x11b35d33bf (/system/lib/libclang_rt.asan-arm-android.so+0x823bf)"]) |
| 684 | self.assertEqual(ARCH, "arm64") |
| 685 | |
| 686 | os.environ.clear() |
| 687 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/mips/arm-linux-androideabi-4.9/bin" |
| 688 | SetAbi(["#1 0x11b35d33bf (/system/lib/libclang_rt.asan-arm-android.so+0x823bf)"]) |
| 689 | self.assertEqual(ARCH, "mips64") |
| 690 | |
| 691 | os.environ.clear() |
| 692 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/x86/arm-linux-androideabi-4.9/bin" |
| 693 | SetAbi(["#12 0x11b35d33bf (/system/lib/libclang_rt.asan-arm-android.so+0x823bf)"]) |
| 694 | self.assertEqual(ARCH, "x86_64") |
| 695 | |
| 696 | # Verify that if an address that might be 32 bit comes first, that |
| 697 | # encountering a 64 bit address returns a 64 bit abi. |
| 698 | ARCH = None |
| 699 | os.environ.clear() |
| 700 | os.environ["ANDROID_TOOLCHAIN"] = "linux-x86/x86/arm-linux-androideabi-4.9/bin" |
| 701 | SetAbi(["#12 0x5d33bf (/system/lib/libclang_rt.asan-arm-android.so+0x823bf)", |
| 702 | "#12 0x11b35d33bf (/system/lib/libclang_rt.asan-arm-android.so+0x823bf)"]) |
| 703 | self.assertEqual(ARCH, "x86_64") |
| 704 | |
| 705 | def test_asan_trace_default_abis(self): |
| 706 | global ARCH |
| 707 | |
| 708 | os.environ.clear() |
| 709 | SetAbi(["#4 0x1234349ab (/system/vendor/lib/libllvm-glnext.so+0x64fc4f)"]) |
| 710 | self.assertEqual(ARCH, "arm64") |
| 711 | SetAbi(["#1 0xae17ec4f (/system/vendor/lib/libllvm-glnext.so+0x64fc4f)"]) |
| 712 | self.assertEqual(ARCH, "arm") |
| 713 | |
Christopher Ferris | bf8a940 | 2016-03-11 15:50:46 -0800 | [diff] [blame] | 714 | def test_no_abi(self): |
| 715 | global ARCH |
| 716 | |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 717 | # Python2 vs Python3 compatibility: Python3 warns on Regexp deprecation, but Regex |
| 718 | # does not provide that name. |
| 719 | if not hasattr(unittest.TestCase, 'assertRaisesRegex'): |
| 720 | unittest.TestCase.assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegexp') |
| 721 | self.assertRaisesRegex(Exception, |
| 722 | "Could not determine arch from input, use --arch=XXX to specify it", |
| 723 | SetAbi, []) |
Elliott Hughes | c3c8619 | 2014-08-29 13:49:57 -0700 | [diff] [blame] | 724 | |
| 725 | if __name__ == '__main__': |
Andreas Gampe | 9240b45 | 2018-10-26 14:17:30 -0700 | [diff] [blame] | 726 | unittest.main(verbosity=2) |