Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /tools /python /llvm_objdump.py
blob: a4b668a8bd114c0b39e0f0f564d39bbeed2f7d96 [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:09[diff] [blame]1# Copyright 2022 The Chromium Authors
André Kempeffa736c2022-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
5import logging
6import os
7import re
8import 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
26def_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 """
36if cpu_arch=="arm":
37return addr&~1
38return addr
39
40
41classObjdumpInformation(object):
42def __init__(self, address, library, symbol, offset):
43 self.address= address
44 self.library= library
45 self.symbol= symbol
46 self.offset= offset
47
48
49classLLVMObjdumper(object):
50def __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
59def __enter__(self):
60return self
61
62def __exit__(self, exc_type, exc_val, exc_tb):
63pass
64
65@staticmethod
66defGetSymbolDataFromObjdumpOutput(objdump_out, address, cpu_arch):
67 stripped_target_address=_StripPC(address, cpu_arch)
68for linein objdump_out.split(os.linesep):
69 components= _FUNC.match(line)
70if 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)
78if components:
79 current_symbol= components.group(1)
80 offset= components.group(2)
81if 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)
86if components:
87 addr= components.group(1)
88 i_addr= int(addr,16)
89if i_addr== stripped_target_address:
90return(current_symbol, stripped_target_address- current_symbol_addr)
91
92return(None,None)
93
94defGetSymbolInformation(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 """
105ifnot os.path.isfile(_LLVM_OBJDUMP_PATH):
106 logging.error('Cannot find llvm-objdump. path=%s', _LLVM_OBJDUMP_PATH)
107returnNone
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
124if 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))
129returnNone
130
131 symbol, offset=LLVMObjdumper.GetSymbolDataFromObjdumpOutput(
132 stdout, address, cpu_arch)
133
134returnObjdumpInformation(address=address,
135 library=lib,
136 symbol=symbol,
137 offset=offset)

[8]ページ先頭

©2009-2025 Movatter.jp