llvm_objdump.py 4.4 KB

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