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
13 from subprocess
import *
17 from perf_trace_context
import perf_sample_srccode
, perf_config_get
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:
29 # +-----------+-----------------+------------------+------------------+
30 # | Recording | Accurate decode | Source line dump | Disassembly dump |
31 # +-----------+-----------------+------------------+------------------+
32 # | --kcore | yes | no | yes |
33 # | normal | no | yes | yes |
34 # +-----------+-----------------+------------------+------------------+
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
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
44 # Output only source line and symbols:
45 # perf script -s scripts/python/arm-cs-trace-disasm.py
47 def default_objdump():
48 config
= perf_config_get("annotate.objdump")
49 return config
if config
else "objdump"
51 # Command line parsing.
55 raise argparse
.ArgumentTypeError("Argument must be a positive integer")
58 args
= argparse
.ArgumentParser()
59 args
.add_argument("-k", "--vmlinux",
60 help="Set path to vmlinux file. Omit to autodetect if running on same machine")
61 args
.add_argument("-d", "--objdump", nargs
="?", const
=default_objdump(),
62 help="Show disassembly. Can also be used to change the objdump path"),
63 args
.add_argument("-v", "--verbose", action
="store_true", help="Enable debugging log")
64 args
.add_argument("--start-time", type=int_arg
, help="Monotonic clock time of sample to start from. "
65 "See 'time' field on samples in -v mode.")
66 args
.add_argument("--stop-time", type=int_arg
, help="Monotonic clock time of sample to stop at. "
67 "See 'time' field on samples in -v mode.")
68 args
.add_argument("--start-sample", type=int_arg
, help="Index of sample to start from. "
69 "See 'index' field on samples in -v mode.")
70 args
.add_argument("--stop-sample", type=int_arg
, help="Index of sample to stop at. "
71 "See 'index' field on samples in -v mode.")
73 options
= args
.parse_args()
74 if (options
.start_time
and options
.stop_time
and
75 options
.start_time
>= options
.stop_time
):
76 print("--start-time must less than --stop-time")
78 if (options
.start_sample
and options
.stop_sample
and
79 options
.start_sample
>= options
.stop_sample
):
80 print("--start-sample must less than --stop-sample")
83 # Initialize global dicts and regular expression
86 disasm_re
= re
.compile(r
"^\s*([0-9a-fA-F]+):")
87 disasm_func_re
= re
.compile(r
"^\s*([0-9a-fA-F]+)\s.*:")
91 glb_source_file_name
= None
92 glb_line_number
= None
95 kver
= platform
.release()
97 f
"/usr/lib/debug/boot/vmlinux-{kver}.debug",
98 f
"/usr/lib/debug/lib/modules/{kver}/vmlinux",
99 f
"/lib/modules/{kver}/build/vmlinux",
100 f
"/usr/lib/debug/boot/vmlinux-{kver}",
101 f
"/boot/vmlinux-{kver}",
106 def get_optional(perf_dict
, field
):
107 if field
in perf_dict
:
108 return perf_dict
[field
]
111 def get_offset(perf_dict
, field
):
112 if field
in perf_dict
:
113 return "+%#x" % perf_dict
[field
]
117 if hasattr(find_vmlinux
, "path"):
118 return find_vmlinux
.path
120 for v
in vmlinux_paths
:
121 if os
.access(v
, os
.R_OK
):
122 find_vmlinux
.path
= v
125 find_vmlinux
.path
= None
127 return find_vmlinux
.path
129 def get_dso_file_path(dso_name
, dso_build_id
):
130 if (dso_name
== "[kernel.kallsyms]" or dso_name
== "vmlinux"):
131 if (options
.vmlinux
):
132 return options
.vmlinux
;
134 return find_vmlinux() if find_vmlinux() else dso_name
136 if (dso_name
== "[vdso]") :
141 dso_path
= os
.environ
['PERF_BUILDID_DIR'] + "/" + dso_name
+ "/" + dso_build_id
+ append
;
142 # Replace duplicate slash chars to single slash char
143 dso_path
= dso_path
.replace('//', '/', 1)
146 def read_disam(dso_fname
, dso_start
, start_addr
, stop_addr
):
147 addr_range
= str(start_addr
) + ":" + str(stop_addr
) + ":" + dso_fname
149 # Don't let the cache get too big, clear it when it hits max size
150 if (len(disasm_cache
) > cache_size
):
151 disasm_cache
.clear();
153 if addr_range
in disasm_cache
:
154 disasm_output
= disasm_cache
[addr_range
];
156 start_addr
= start_addr
- dso_start
;
157 stop_addr
= stop_addr
- dso_start
;
158 disasm
= [ options
.objdump
, "-d", "-z",
159 "--start-address="+format(start_addr
,"#x"),
160 "--stop-address="+format(stop_addr
,"#x") ]
161 disasm
+= [ dso_fname
]
162 disasm_output
= check_output(disasm
).decode('utf-8').split('\n')
163 disasm_cache
[addr_range
] = disasm_output
167 def print_disam(dso_fname
, dso_start
, start_addr
, stop_addr
):
168 for line
in read_disam(dso_fname
, dso_start
, start_addr
, stop_addr
):
169 m
= disasm_func_re
.search(line
)
171 m
= disasm_re
.search(line
)
176 def print_sample(sample
):
177 print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \
178 "pid: %d tid: %d period: %d time: %d index: %d}" % \
179 (sample
['cpu'], sample
['addr'], sample
['phys_addr'], \
180 sample
['ip'], sample
['pid'], sample
['tid'], \
181 sample
['period'], sample
['time'], sample_idx
))
184 print('ARM CoreSight Trace Data Assembler Dump')
189 def trace_unhandled(event_name
, context
, event_fields_dict
):
190 print(' '.join(['%s=%s'%(k
,str(v
))for k
,v
in sorted(event_fields_dict
.items())]))
192 def common_start_str(comm
, sample
):
193 sec
= int(sample
["time"] / 1000000000)
194 ns
= sample
["time"] % 1000000000
198 return "%16s %5u/%-5u [%04u] %9u.%09u " % (comm
, pid
, tid
, cpu
, sec
, ns
)
200 # This code is copied from intel-pt-events.py for printing source code
202 def print_srccode(comm
, param_dict
, sample
, symbol
, dso
):
204 if symbol
== "[unknown]":
205 start_str
= common_start_str(comm
, sample
) + ("%x" % ip
).rjust(16).ljust(40)
207 offs
= get_offset(param_dict
, "symoff")
208 start_str
= common_start_str(comm
, sample
) + (symbol
+ offs
).ljust(40)
210 global glb_source_file_name
211 global glb_line_number
214 source_file_name
, line_number
, source_line
= perf_sample_srccode(perf_script_context
)
216 if glb_line_number
== line_number
and glb_source_file_name
== source_file_name
:
219 if len(source_file_name
) > 40:
220 src_file
= ("..." + source_file_name
[-37:]) + " "
222 src_file
= source_file_name
.ljust(41)
224 if source_line
is None:
225 src_str
= src_file
+ str(line_number
).rjust(4) + " <source not found>"
227 src_str
= src_file
+ str(line_number
).rjust(4) + " " + source_line
235 glb_line_number
= line_number
236 glb_source_file_name
= source_file_name
238 print(start_str
, src_str
)
240 def process_event(param_dict
):
245 sample
= param_dict
["sample"]
246 comm
= param_dict
["comm"]
248 name
= param_dict
["ev_name"]
249 dso
= get_optional(param_dict
, "dso")
250 dso_bid
= get_optional(param_dict
, "dso_bid")
251 dso_start
= get_optional(param_dict
, "dso_map_start")
252 dso_end
= get_optional(param_dict
, "dso_map_end")
253 symbol
= get_optional(param_dict
, "symbol")
254 map_pgoff
= get_optional(param_dict
, "map_pgoff")
255 # check for valid map offset
256 if (str(map_pgoff
) == '[unknown]'):
261 addr
= sample
["addr"]
265 if (options
.start_time
and sample
["time"] < options
.start_time
):
267 if (options
.stop_time
and sample
["time"] > options
.stop_time
):
269 if (options
.start_sample
and sample_idx
< options
.start_sample
):
271 if (options
.stop_sample
and sample_idx
> options
.stop_sample
):
274 if (options
.verbose
== True):
275 print("Event type: %s" % name
)
278 # Initialize CPU data if it's empty, and directly return back
279 # if this is the first tracing event for this CPU.
280 if (cpu_data
.get(str(cpu
) + 'addr') == None):
281 cpu_data
[str(cpu
) + 'addr'] = addr
284 # If cannot find dso so cannot dump assembler, bail out
285 if (dso
== '[unknown]'):
288 # Validate dso start and end addresses
289 if ((dso_start
== '[unknown]') or (dso_end
== '[unknown]')):
290 print("Failed to find valid dso map for dso %s" % dso
)
293 if (name
[0:12] == "instructions"):
294 print_srccode(comm
, param_dict
, sample
, symbol
, dso
)
297 # Don't proceed if this event is not a branch sample, .
298 if (name
[0:8] != "branches"):
301 # The format for packet is:
303 # +------------+------------+------------+
304 # sample_prev: | addr | ip | cpu |
305 # +------------+------------+------------+
306 # sample_next: | addr | ip | cpu |
307 # +------------+------------+------------+
309 # We need to combine the two continuous packets to get the instruction
310 # range for sample_prev::cpu:
312 # [ sample_prev::addr .. sample_next::ip ]
314 # For this purose, sample_prev::addr is stored into cpu_data structure
315 # and read back for 'start_addr' when the new packet comes, and we need
316 # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for
317 # 'stop_addr' is for the sake of objdump so the final assembler dump can
318 # include last instruction for sample_next::ip.
319 start_addr
= cpu_data
[str(cpu
) + 'addr']
322 # Record for previous sample packet
323 cpu_data
[str(cpu
) + 'addr'] = addr
325 # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet
326 if (start_addr
== 0):
327 if ((stop_addr
== 4) and (options
.verbose
== True)):
328 print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu
)
331 if (start_addr
< int(dso_start
) or start_addr
> int(dso_end
)):
332 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
))
335 if (stop_addr
< int(dso_start
) or stop_addr
> int(dso_end
)):
336 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
))
339 if (options
.objdump
!= None):
340 # It doesn't need to decrease virtual memory offset for disassembly
341 # for kernel dso and executable file dso, so in this case we set
343 if (dso
== "[kernel.kallsyms]" or dso_start
== 0x400000):
347 dso_vm_start
= int(dso_start
)
349 dso_fname
= get_dso_file_path(dso
, dso_bid
)
350 if path
.exists(dso_fname
):
351 print_disam(dso_fname
, dso_vm_start
, start_addr
+ map_pgoff
, stop_addr
+ map_pgoff
)
353 print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso
, start_addr
+ map_pgoff
, stop_addr
+ map_pgoff
))
355 print_srccode(comm
, param_dict
, sample
, symbol
, dso
)