Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 1 | # Copyright 2022 The Chromium Authors |
André Kempe | ffa736c | 2022-01-20 16:55:05 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import logging |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | |
| 10 | _CHROME_SRC= os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) |
| 11 | _LLVM_OBJDUMP_PATH= os.path.join(_CHROME_SRC,'third_party','llvm-build', |
| 12 | 'Release+Asserts','bin','llvm-objdump') |
| 13 | |
| 14 | # Function lines look like: |
| 15 | # 000177b0 <android::IBinder::~IBinder()+0x2c>: |
| 16 | # We pull out the address and function first. Then we check for an optional |
| 17 | # offset. This is tricky due to functions that look like "operator+(..)+0x2c" |
| 18 | _FUNC= re.compile(r"(^[a-f0-9]*) <(.*)>:$") |
| 19 | _OFFSET= re.compile(r"(.*)\+0x([a-f0-9]*)") |
| 20 | |
| 21 | # A disassembly line looks like: |
| 22 | # 177b2: b510 push {r4, lr} |
| 23 | _ASM= re.compile(r"(^[ a-f0-9]*):[ a-f0-0]*.*$") |
| 24 | |
| 25 | |
| 26 | def_StripPC(addr, cpu_arch): |
| 27 | """Strips the Thumb bit from a program counter address when appropriate. |
| 28 | |
| 29 | Args: |
| 30 | addr: the program counter address |
| 31 | cpu_arch: Target CPU architecture. |
| 32 | |
| 33 | Returns: |
| 34 | The stripped program counter address. |
| 35 | """ |
| 36 | if cpu_arch=="arm": |
| 37 | return addr&~1 |
| 38 | return addr |
| 39 | |
| 40 | |
| 41 | classObjdumpInformation(object): |
| 42 | def __init__(self, address, library, symbol, offset): |
| 43 | self.address= address |
| 44 | self.library= library |
| 45 | self.symbol= symbol |
| 46 | self.offset= offset |
| 47 | |
| 48 | |
| 49 | classLLVMObjdumper(object): |
| 50 | def __init__(self): |
| 51 | """Creates an instance of LLVMObjdumper that interacts with llvm-objdump. |
| 52 | """ |
| 53 | self._llvm_objdump_parameters=[ |
| 54 | '--disassemble', |
| 55 | '--demangle', |
| 56 | '--section=.text', |
| 57 | ] |
| 58 | |
| 59 | def __enter__(self): |
| 60 | return self |
| 61 | |
| 62 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 63 | pass |
| 64 | |
| 65 | @staticmethod |
| 66 | defGetSymbolDataFromObjdumpOutput(objdump_out, address, cpu_arch): |
| 67 | stripped_target_address=_StripPC(address, cpu_arch) |
| 68 | for linein objdump_out.split(os.linesep): |
| 69 | components= _FUNC.match(line) |
| 70 | if components: |
| 71 | # This is a new function, so record the current function and its |
| 72 | # address. |
| 73 | current_symbol_addr= int(components.group(1),16) |
| 74 | current_symbol= components.group(2) |
| 75 | |
| 76 | # Does it have an optional offset like: "foo(..)+0x2c"? |
| 77 | components= _OFFSET.match(current_symbol) |
| 78 | if components: |
| 79 | current_symbol= components.group(1) |
| 80 | offset= components.group(2) |
| 81 | if offset: |
| 82 | current_symbol_addr-= int(offset,16) |
| 83 | |
| 84 | # Is it a disassembly line like: "177b2: b510 push {r4, lr}"? |
| 85 | components= _ASM.match(line) |
| 86 | if components: |
| 87 | addr= components.group(1) |
| 88 | i_addr= int(addr,16) |
| 89 | if i_addr== stripped_target_address: |
| 90 | return(current_symbol, stripped_target_address- current_symbol_addr) |
| 91 | |
| 92 | return(None,None) |
| 93 | |
| 94 | defGetSymbolInformation(self, lib, address, cpu_arch): |
| 95 | """Returns the corresponding function names and line numbers. |
| 96 | |
| 97 | Args: |
| 98 | lib: library to search for info. |
| 99 | address: address to look for info. |
| 100 | cpu_arch: architecture where the dump was taken |
| 101 | |
| 102 | Returns: |
| 103 | An ObjdumpInformation object |
| 104 | """ |
| 105 | ifnot os.path.isfile(_LLVM_OBJDUMP_PATH): |
| 106 | logging.error('Cannot find llvm-objdump. path=%s', _LLVM_OBJDUMP_PATH) |
| 107 | returnNone |
| 108 | |
| 109 | stripped_address=_StripPC(address, cpu_arch) |
| 110 | |
| 111 | full_arguments=[_LLVM_OBJDUMP_PATH]+ self._llvm_objdump_parameters |
| 112 | full_arguments.append('--start-address='+ str(stripped_address)) |
| 113 | full_arguments.append('--stop-address='+ str(stripped_address+8)) |
| 114 | full_arguments.append(lib) |
| 115 | |
| 116 | objdump_process= subprocess.Popen(full_arguments, |
| 117 | stdout=subprocess.PIPE, |
| 118 | stdin=subprocess.PIPE, |
| 119 | universal_newlines=True) |
| 120 | |
| 121 | stdout, stderr= objdump_process.communicate() |
| 122 | objdump_process_return_code= objdump_process.poll() |
| 123 | |
| 124 | if objdump_process_return_code!=0: |
| 125 | logging.error( |
| 126 | 'Invocation of llvm-objdump failed!'+ |
| 127 | ' tool-command-line=\'{}\', return-code={}, std-error=\'{}\''.format( |
| 128 | ' '.join(full_arguments), objdump_process_return_code, stderr)) |
| 129 | returnNone |
| 130 | |
| 131 | symbol, offset=LLVMObjdumper.GetSymbolDataFromObjdumpOutput( |
| 132 | stdout, address, cpu_arch) |
| 133 | |
| 134 | returnObjdumpInformation(address=address, |
| 135 | library=lib, |
| 136 | symbol=symbol, |
| 137 | offset=offset) |