/
debuginfo.py
executable file
·219 lines (171 loc) · 7.21 KB
/
debuginfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import argparse
import sys
import math
from babeltrace import TraceCollection
from collections import namedtuple
from elftools.elf.elffile import ELFFile
from elftools.common.py3compat import bytes2str
_DESC = 'Map events from userspace trace to symbols'
_VERSION = '0.1.0'
SourceLocation = namedtuple('SourceLocation', 'filename, line')
class DebugInfoAnalysis():
def __init__(self, args):
self._open_trace(args.trace_path)
# SO info indexed by base address
self._shared_objects = {}
# Memoized function names and SourceLocations, indexed by
# address
self._function_names = {}
self._source_locations = {}
def _open_trace(self, path):
traces = TraceCollection()
handles = traces.add_traces_recursive(path, 'ctf')
if not handles:
print('Failed to open ' + path, file=sys.stderr)
sys.exit(-1)
self._handles = handles
self._traces = traces
def _close_trace(self):
for handle in self._handles.values():
self._traces.remove_trace(handle)
def _lookup_function_name(self, address):
so = self.get_so_by_address(address)
# Addresses in DWARF are relative to base address for PIC, so
# make the address argument relative too if needed
if so.is_pic:
address -= so.low_addr
for compile_unit in so.dwarf_info.iter_CUs():
for die in compile_unit.iter_DIEs():
try:
if die.tag == 'DW_TAG_subprogram':
func_name = bytes2str(
die.attributes['DW_AT_name'].value)
low_pc = die.attributes['DW_AT_low_pc'].value
high_pc_attr = die.attributes['DW_AT_high_pc']
if high_pc_attr.form == 'DW_FORM_addr':
high_pc = high_pc_attr.value
else:
# high_pc relative to low_pc
high_pc = low_pc + high_pc_attr.value
if low_pc <= address <= high_pc:
self._function_names[address] = func_name
return self._function_names[address]
except KeyError:
continue
return None
def _lookup_source_location(self, address):
so = self.get_so_by_address(address)
# Addresses in DWARF are relative to base address for PIC, so
# make the address argument relative too if needed
if so.is_pic:
address -= so.low_addr
for compile_unit in so.dwarf_info.iter_CUs():
line_program = so.dwarf_info.line_program_for_CU(compile_unit)
prev_state = None
for entry in line_program.get_entries():
cur_state = entry.state
if cur_state is None or cur_state.end_sequence:
continue
if prev_state:
file_entry = line_program['file_entry']
filename = file_entry[prev_state.file - 1].name
filename = bytes2str(filename)
line = prev_state.line
low_pc = prev_state.address
high_pc = cur_state.address
if low_pc > high_pc:
low_pc, high_pc = high_pc, low_pc
if low_pc <= address <= high_pc:
self._source_locations[address] = SourceLocation(
filename, line)
return self._source_locations[address]
prev_state = cur_state
return SourceLocation(None, None)
def get_so_by_address(self, address):
for so in self._shared_objects.values():
if so.low_addr <= address <= so.high_addr:
return so
return None
def get_function_name(self, address):
if address in self._function_names:
return self._function_names[address]
else:
return self._lookup_function_name(address)
def get_source_location(self, address):
if address in self._source_locations:
return self._source_locations[address]
else:
return self._lookup_source_location(address)
def _handle_baddr_event(self, event):
# Size will be 0 for VDSO
if event['size'] == 0:
return
path = event['sopath']
baddr = event['baddr']
self._shared_objects[baddr] = SharedObjectInfo(path, baddr)
def _print_debug_info(self, event):
address = event['ip']
func_name = self.get_function_name(address)
source_location = self.get_source_location(address)
if func_name is not None:
print(func_name)
if source_location.filename is not None:
print(source_location)
def run(self):
for event in self._traces.events:
if event.name in ['lttng_ust_statedump:soinfo', 'lttng_ust_dl:dlopen']:
self._handle_baddr_event(event)
elif event.name.startswith('lttng_ust'):
# TODO handle dlclose and state dump start/end
continue
else:
self._print_debug_info(event)
self._close_trace()
class SharedObjectInfo():
def __init__(self, path, baddr):
self.path = path
self._set_elf_file()
self.low_addr = baddr
self.high_addr = baddr + self._get_mem_size()
# Check whether the ELF file is position independent code
self.is_pic = self.elf_file.header['e_type'] == 'ET_DYN'
# Don't set the so info's dwarf_info initially, only when
# symbol lookup is first required
self._dwarf_info = None
@property
def dwarf_info(self):
if self._dwarf_info is None:
self._set_dwarf_info()
return self._dwarf_info
def _set_elf_file(self):
try:
binary_file = open(self.path, 'rb')
self.elf_file = ELFFile(binary_file)
except IOError:
print('Failed to open ' + self.path, file=sys.stderr)
sys.exit(-1)
def _set_dwarf_info(self):
if not self.elf_file.has_dwarf_info():
print('Binary ' + self.path + ' has no DWARF info',
file=sys.stderr)
sys.exit(-1)
self._dwarf_info = self.elf_file.get_dwarf_info()
def _get_mem_size(self):
mem_size = 0
for segment in self.elf_file.iter_segments():
if segment['p_type'] == 'PT_LOAD':
alignment = segment['p_align']
segment_size = segment['p_memsz']
aligned_size = math.ceil(segment_size / alignment) * alignment
mem_size += aligned_size
return mem_size
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description=_DESC)
arg_parser.add_argument('trace_path', metavar='<path/to/trace>',
help='trace path')
arg_parser.add_argument('-V', '--version', action='version',
version='pydebuginfo v' + _VERSION)
args = arg_parser.parse_args()
debug_info_analysis = DebugInfoAnalysis(args)
debug_info_analysis.run()