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