1# SPDX-License-Identifier: GPL-2.0
2# arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember
3#
4# Author: Tor Jeremiassen <[email protected]>
5#         Mathieu Poirier <[email protected]>
6#         Leo Yan <[email protected]>
7#         Al Grant <[email protected]>
8
9from __future__ import print_function
10import os
11from os import path
12import re
13from subprocess import *
14import argparse
15import platform
16
17from perf_trace_context import perf_sample_srccode, perf_config_get
18
19# Below are some example commands for using this script.
20# Note a --kcore recording is required for accurate decode
21# due to the alternatives patching mechanism. However this
22# script only supports reading vmlinux for disassembly dump,
23# meaning that any patched instructions will appear
24# as unpatched, but the instruction ranges themselves will
25# be correct. In addition to this, source line info comes
26# from Perf, and when using kcore there is no debug info. The
27# following lists the supported features in each mode:
28#
29# +-----------+-----------------+------------------+------------------+
30# | Recording | Accurate decode | Source line dump | Disassembly dump |
31# +-----------+-----------------+------------------+------------------+
32# | --kcore   | yes             | no               | yes              |
33# | normal    | no              | yes              | yes              |
34# +-----------+-----------------+------------------+------------------+
35#
36# Output disassembly with objdump and auto detect vmlinux
37# (when running on same machine.)
38#  perf script -s scripts/python/arm-cs-trace-disasm.py -d
39#
40# Output disassembly with llvm-objdump:
41#  perf script -s scripts/python/arm-cs-trace-disasm.py \
42#		-- -d llvm-objdump-11 -k path/to/vmlinux
43#
44# Output only source line and symbols:
45#  perf script -s scripts/python/arm-cs-trace-disasm.py
46
47def default_objdump():
48	config = perf_config_get("annotate.objdump")
49	return config if config else "objdump"
50
51# Command line parsing.
52args = argparse.ArgumentParser()
53args.add_argument("-k", "--vmlinux",
54		  help="Set path to vmlinux file. Omit to autodetect if running on same machine")
55args.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
56		  help="Show disassembly. Can also be used to change the objdump path"),
57args.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
58options = args.parse_args()
59
60# Initialize global dicts and regular expression
61disasm_cache = dict()
62cpu_data = dict()
63disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")
64disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
65cache_size = 64*1024
66
67glb_source_file_name	= None
68glb_line_number		= None
69glb_dso			= None
70
71kver = platform.release()
72vmlinux_paths = [
73	f"/usr/lib/debug/boot/vmlinux-{kver}.debug",
74	f"/usr/lib/debug/lib/modules/{kver}/vmlinux",
75	f"/lib/modules/{kver}/build/vmlinux",
76	f"/usr/lib/debug/boot/vmlinux-{kver}",
77	f"/boot/vmlinux-{kver}",
78	f"/boot/vmlinux",
79	f"vmlinux"
80]
81
82def get_optional(perf_dict, field):
83       if field in perf_dict:
84               return perf_dict[field]
85       return "[unknown]"
86
87def get_offset(perf_dict, field):
88	if field in perf_dict:
89		return "+%#x" % perf_dict[field]
90	return ""
91
92def find_vmlinux():
93	if hasattr(find_vmlinux, "path"):
94		return find_vmlinux.path
95
96	for v in vmlinux_paths:
97		if os.access(v, os.R_OK):
98			find_vmlinux.path = v
99			break
100	else:
101		find_vmlinux.path = None
102
103	return find_vmlinux.path
104
105def get_dso_file_path(dso_name, dso_build_id):
106	if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"):
107		if (options.vmlinux):
108			return options.vmlinux;
109		else:
110			return find_vmlinux() if find_vmlinux() else dso_name
111
112	if (dso_name == "[vdso]") :
113		append = "/vdso"
114	else:
115		append = "/elf"
116
117	dso_path = os.environ['PERF_BUILDID_DIR'] + "/" + dso_name + "/" + dso_build_id + append;
118	# Replace duplicate slash chars to single slash char
119	dso_path = dso_path.replace('//', '/', 1)
120	return dso_path
121
122def read_disam(dso_fname, dso_start, start_addr, stop_addr):
123	addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname
124
125	# Don't let the cache get too big, clear it when it hits max size
126	if (len(disasm_cache) > cache_size):
127		disasm_cache.clear();
128
129	if addr_range in disasm_cache:
130		disasm_output = disasm_cache[addr_range];
131	else:
132		start_addr = start_addr - dso_start;
133		stop_addr = stop_addr - dso_start;
134		disasm = [ options.objdump, "-d", "-z",
135			   "--start-address="+format(start_addr,"#x"),
136			   "--stop-address="+format(stop_addr,"#x") ]
137		disasm += [ dso_fname ]
138		disasm_output = check_output(disasm).decode('utf-8').split('\n')
139		disasm_cache[addr_range] = disasm_output
140
141	return disasm_output
142
143def print_disam(dso_fname, dso_start, start_addr, stop_addr):
144	for line in read_disam(dso_fname, dso_start, start_addr, stop_addr):
145		m = disasm_func_re.search(line)
146		if m is None:
147			m = disasm_re.search(line)
148			if m is None:
149				continue
150		print("\t" + line)
151
152def print_sample(sample):
153	print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \
154	      "pid: %d tid: %d period: %d time: %d }" % \
155	      (sample['cpu'], sample['addr'], sample['phys_addr'], \
156	       sample['ip'], sample['pid'], sample['tid'], \
157	       sample['period'], sample['time']))
158
159def trace_begin():
160	print('ARM CoreSight Trace Data Assembler Dump')
161
162def trace_end():
163	print('End')
164
165def trace_unhandled(event_name, context, event_fields_dict):
166	print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
167
168def common_start_str(comm, sample):
169	sec = int(sample["time"] / 1000000000)
170	ns = sample["time"] % 1000000000
171	cpu = sample["cpu"]
172	pid = sample["pid"]
173	tid = sample["tid"]
174	return "%16s %5u/%-5u [%04u] %9u.%09u  " % (comm, pid, tid, cpu, sec, ns)
175
176# This code is copied from intel-pt-events.py for printing source code
177# line and symbols.
178def print_srccode(comm, param_dict, sample, symbol, dso):
179	ip = sample["ip"]
180	if symbol == "[unknown]":
181		start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
182	else:
183		offs = get_offset(param_dict, "symoff")
184		start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
185
186	global glb_source_file_name
187	global glb_line_number
188	global glb_dso
189
190	source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
191	if source_file_name:
192		if glb_line_number == line_number and glb_source_file_name == source_file_name:
193			src_str = ""
194		else:
195			if len(source_file_name) > 40:
196				src_file = ("..." + source_file_name[-37:]) + " "
197			else:
198				src_file = source_file_name.ljust(41)
199
200			if source_line is None:
201				src_str = src_file + str(line_number).rjust(4) + " <source not found>"
202			else:
203				src_str = src_file + str(line_number).rjust(4) + " " + source_line
204		glb_dso = None
205	elif dso == glb_dso:
206		src_str = ""
207	else:
208		src_str = dso
209		glb_dso = dso
210
211	glb_line_number = line_number
212	glb_source_file_name = source_file_name
213
214	print(start_str, src_str)
215
216def process_event(param_dict):
217	global cache_size
218	global options
219
220	sample = param_dict["sample"]
221	comm = param_dict["comm"]
222
223	name = param_dict["ev_name"]
224	dso = get_optional(param_dict, "dso")
225	dso_bid = get_optional(param_dict, "dso_bid")
226	dso_start = get_optional(param_dict, "dso_map_start")
227	dso_end = get_optional(param_dict, "dso_map_end")
228	symbol = get_optional(param_dict, "symbol")
229
230	cpu = sample["cpu"]
231	ip = sample["ip"]
232	addr = sample["addr"]
233
234	if (options.verbose == True):
235		print("Event type: %s" % name)
236		print_sample(sample)
237
238	# Initialize CPU data if it's empty, and directly return back
239	# if this is the first tracing event for this CPU.
240	if (cpu_data.get(str(cpu) + 'addr') == None):
241		cpu_data[str(cpu) + 'addr'] = addr
242		return
243
244	# If cannot find dso so cannot dump assembler, bail out
245	if (dso == '[unknown]'):
246		return
247
248	# Validate dso start and end addresses
249	if ((dso_start == '[unknown]') or (dso_end == '[unknown]')):
250		print("Failed to find valid dso map for dso %s" % dso)
251		return
252
253	if (name[0:12] == "instructions"):
254		print_srccode(comm, param_dict, sample, symbol, dso)
255		return
256
257	# Don't proceed if this event is not a branch sample, .
258	if (name[0:8] != "branches"):
259		return
260
261	# The format for packet is:
262	#
263	#		  +------------+------------+------------+
264	#  sample_prev:   |    addr    |    ip	    |	 cpu	 |
265	#		  +------------+------------+------------+
266	#  sample_next:   |    addr    |    ip	    |	 cpu	 |
267	#		  +------------+------------+------------+
268	#
269	# We need to combine the two continuous packets to get the instruction
270	# range for sample_prev::cpu:
271	#
272	#     [ sample_prev::addr .. sample_next::ip ]
273	#
274	# For this purose, sample_prev::addr is stored into cpu_data structure
275	# and read back for 'start_addr' when the new packet comes, and we need
276	# to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for
277	# 'stop_addr' is for the sake of objdump so the final assembler dump can
278	# include last instruction for sample_next::ip.
279	start_addr = cpu_data[str(cpu) + 'addr']
280	stop_addr  = ip + 4
281
282	# Record for previous sample packet
283	cpu_data[str(cpu) + 'addr'] = addr
284
285	# Handle CS_ETM_TRACE_ON packet if start_addr=0 and stop_addr=4
286	if (start_addr == 0 and stop_addr == 4):
287		print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu)
288		return
289
290	if (start_addr < int(dso_start) or start_addr > int(dso_end)):
291		print("Start address 0x%x is out of range [ 0x%x .. 0x%x ] for dso %s" % (start_addr, int(dso_start), int(dso_end), dso))
292		return
293
294	if (stop_addr < int(dso_start) or stop_addr > int(dso_end)):
295		print("Stop address 0x%x is out of range [ 0x%x .. 0x%x ] for dso %s" % (stop_addr, int(dso_start), int(dso_end), dso))
296		return
297
298	if (options.objdump != None):
299		# It doesn't need to decrease virtual memory offset for disassembly
300		# for kernel dso and executable file dso, so in this case we set
301		# vm_start to zero.
302		if (dso == "[kernel.kallsyms]" or dso_start == 0x400000):
303			dso_vm_start = 0
304		else:
305			dso_vm_start = int(dso_start)
306
307		dso_fname = get_dso_file_path(dso, dso_bid)
308		if path.exists(dso_fname):
309			print_disam(dso_fname, dso_vm_start, start_addr, stop_addr)
310		else:
311			print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso, start_addr, stop_addr))
312
313	print_srccode(comm, param_dict, sample, symbol, dso)
314