1 # SPDX-License-Identifier: GPL-2.0
2 # arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember
4 # Author: Tor Jeremiassen <tor@ti.com>
5 # Mathieu Poirier <mathieu.poirier@linaro.org>
6 # Leo Yan <leo.yan@linaro.org>
7 # Al Grant <Al.Grant@arm.com>
9 from __future__ import print_function
14 from subprocess import *
15 from optparse import OptionParser, make_option
17 from perf_trace_context import perf_set_itrace_options, \
18 perf_sample_insn, perf_sample_srccode
20 # Below are some example commands for using this script.
22 # Output disassembly with objdump:
23 # perf script -s scripts/python/arm-cs-trace-disasm.py \
24 # -- -d objdump -k path/to/vmlinux
25 # Output disassembly with llvm-objdump:
26 # perf script -s scripts/python/arm-cs-trace-disasm.py \
27 # -- -d llvm-objdump-11 -k path/to/vmlinux
28 # Output only source line and symbols:
29 # perf script -s scripts/python/arm-cs-trace-disasm.py
31 # Command line parsing.
33 # formatting options for the bottom entry of the stack
34 make_option("-k", "--vmlinux", dest="vmlinux_name",
35 help="Set path to vmlinux file"),
36 make_option("-d", "--objdump", dest="objdump_name",
37 help="Set path to objdump executable file"),
38 make_option("-v", "--verbose", dest="verbose",
39 action="store_true", default=False,
40 help="Enable debugging log")
43 parser = OptionParser(option_list=option_list)
44 (options, args) = parser.parse_args()
46 # Initialize global dicts and regular expression
49 disasm_re = re.compile("^\s*([0-9a-fA-F]+):")
50 disasm_func_re = re.compile("^\s*([0-9a-fA-F]+)\s.*:")
53 glb_source_file_name = None
54 glb_line_number = None
57 def get_optional(perf_dict, field):
58 if field in perf_dict:
59 return perf_dict[field]
62 def get_offset(perf_dict, field):
63 if field in perf_dict:
64 return f"+0x{perf_dict[field]:x}"
67 def get_dso_file_path(dso_name, dso_build_id):
68 if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"):
69 if (options.vmlinux_name):
70 return options.vmlinux_name;
74 if (dso_name == "[vdso]") :
79 dso_path = f"{os.environ['PERF_BUILDID_DIR']}/{dso_name}/{dso_build_id}{append}"
80 # Replace duplicate slash chars to single slash char
81 dso_path = dso_path.replace('//', '/', 1)
84 def read_disam(dso_fname, dso_start, start_addr, stop_addr):
85 addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname
87 # Don't let the cache get too big, clear it when it hits max size
88 if (len(disasm_cache) > cache_size):
91 if addr_range in disasm_cache:
92 disasm_output = disasm_cache[addr_range];
94 start_addr = start_addr - dso_start;
95 stop_addr = stop_addr - dso_start;
96 disasm = [ options.objdump_name, "-d", "-z",
97 f"--start-address=0x{start_addr:x}",
98 f"--stop-address=0x{stop_addr:x}" ]
99 disasm += [ dso_fname ]
100 disasm_output = check_output(disasm).decode('utf-8').split('\n')
101 disasm_cache[addr_range] = disasm_output
105 def print_disam(dso_fname, dso_start, start_addr, stop_addr):
106 for line in read_disam(dso_fname, dso_start, start_addr, stop_addr):
107 m = disasm_func_re.search(line)
109 m = disasm_re.search(line)
114 def print_sample(sample):
115 print(f"Sample = {{ cpu: {sample['cpu']:04} addr: 0x{sample['addr']:016x} " \
116 f"phys_addr: 0x{sample['phys_addr']:016x} ip: 0x{sample['ip']:016x} " \
117 f"pid: {sample['pid']} tid: {sample['tid']} period: {sample['period']} time: {sample['time']} }}")
120 print('ARM CoreSight Trace Data Assembler Dump')
125 def trace_unhandled(event_name, context, event_fields_dict):
126 print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
128 def common_start_str(comm, sample):
129 sec = int(sample["time"] / 1000000000)
130 ns = sample["time"] % 1000000000
134 return f"{comm:>16} {pid:>5}/{tid:<5} [{cpu:04}] {sec:9}.{ns:09} "
136 # This code is copied from intel-pt-events.py for printing source code
138 def print_srccode(comm, param_dict, sample, symbol, dso):
140 if symbol == "[unknown]":
141 start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
143 offs = get_offset(param_dict, "symoff")
144 start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
146 global glb_source_file_name
147 global glb_line_number
150 source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
152 if glb_line_number == line_number and glb_source_file_name == source_file_name:
155 if len(source_file_name) > 40:
156 src_file = ("..." + source_file_name[-37:]) + " "
158 src_file = source_file_name.ljust(41)
160 if source_line is None:
161 src_str = src_file + str(line_number).rjust(4) + " <source not found>"
163 src_str = src_file + str(line_number).rjust(4) + " " + source_line
171 glb_line_number = line_number
172 glb_source_file_name = source_file_name
174 print(f"{start_str}{src_str}")
176 def process_event(param_dict):
180 sample = param_dict["sample"]
181 comm = param_dict["comm"]
183 name = param_dict["ev_name"]
184 dso = get_optional(param_dict, "dso")
185 dso_bid = get_optional(param_dict, "dso_bid")
186 dso_start = get_optional(param_dict, "dso_map_start")
187 dso_end = get_optional(param_dict, "dso_map_end")
188 symbol = get_optional(param_dict, "symbol")
190 if (options.verbose == True):
191 print(f"Event type: {name}")
194 # If cannot find dso so cannot dump assembler, bail out
195 if (dso == '[unknown]'):
198 # Validate dso start and end addresses
199 if ((dso_start == '[unknown]') or (dso_end == '[unknown]')):
200 print(f"Failed to find valid dso map for dso {dso}")
203 if (name[0:12] == "instructions"):
204 print_srccode(comm, param_dict, sample, symbol, dso)
207 # Don't proceed if this event is not a branch sample, .
208 if (name[0:8] != "branches"):
213 addr = sample["addr"]
215 # Initialize CPU data if it's empty, and directly return back
216 # if this is the first tracing event for this CPU.
217 if (cpu_data.get(str(cpu) + 'addr') == None):
218 cpu_data[str(cpu) + 'addr'] = addr
221 # The format for packet is:
223 # +------------+------------+------------+
224 # sample_prev: | addr | ip | cpu |
225 # +------------+------------+------------+
226 # sample_next: | addr | ip | cpu |
227 # +------------+------------+------------+
229 # We need to combine the two continuous packets to get the instruction
230 # range for sample_prev::cpu:
232 # [ sample_prev::addr .. sample_next::ip ]
234 # For this purose, sample_prev::addr is stored into cpu_data structure
235 # and read back for 'start_addr' when the new packet comes, and we need
236 # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for
237 # 'stop_addr' is for the sake of objdump so the final assembler dump can
238 # include last instruction for sample_next::ip.
239 start_addr = cpu_data[str(cpu) + 'addr']
242 # Record for previous sample packet
243 cpu_data[str(cpu) + 'addr'] = addr
245 # Handle CS_ETM_TRACE_ON packet if start_addr=0 and stop_addr=4
246 if (start_addr == 0 and stop_addr == 4):
247 print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted")
250 if (start_addr < int(dso_start) or start_addr > int(dso_end)):
251 print(f"Start address 0x{start_addr:x} is out of range [ 0x{dso_start:x} .. 0x{dso_end:x} ] for dso {dso}")
254 if (stop_addr < int(dso_start) or stop_addr > int(dso_end)):
255 print(f"Stop address 0x{stop_addr:x} is out of range [ 0x{dso_start:x} .. 0x{dso_end:x} ] for dso {dso}")
258 if (options.objdump_name != None):
259 # It doesn't need to decrease virtual memory offset for disassembly
260 # for kernel dso, so in this case we set vm_start to zero.
261 if (dso == "[kernel.kallsyms]"):
264 dso_vm_start = int(dso_start)
266 dso_fname = get_dso_file_path(dso, dso_bid)
267 if path.exists(dso_fname):
268 print_disam(dso_fname, dso_vm_start, start_addr, stop_addr)
270 print(f"Failed to find dso {dso} for address range [ 0x{start_addr:x} .. 0x{stop_addr:x} ]")
272 print_srccode(comm, param_dict, sample, symbol, dso)