112fdd6c0SLeo Yan# SPDX-License-Identifier: GPL-2.0 212fdd6c0SLeo Yan# arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember 312fdd6c0SLeo Yan# 412fdd6c0SLeo Yan# Author: Tor Jeremiassen <[email protected]> 512fdd6c0SLeo Yan# Mathieu Poirier <[email protected]> 612fdd6c0SLeo Yan# Leo Yan <[email protected]> 712fdd6c0SLeo Yan# Al Grant <[email protected]> 812fdd6c0SLeo Yan 912fdd6c0SLeo Yanfrom __future__ import print_function 1012fdd6c0SLeo Yanimport os 1112fdd6c0SLeo Yanfrom os import path 1212fdd6c0SLeo Yanimport re 1312fdd6c0SLeo Yanfrom subprocess import * 147b371afcSJames Clarkimport argparse 158286cc55SJames Clarkimport platform 1612fdd6c0SLeo Yan 178286cc55SJames Clarkfrom perf_trace_context import perf_sample_srccode, perf_config_get 1812fdd6c0SLeo Yan 1912fdd6c0SLeo Yan# Below are some example commands for using this script. 208286cc55SJames Clark# Note a --kcore recording is required for accurate decode 218286cc55SJames Clark# due to the alternatives patching mechanism. However this 228286cc55SJames Clark# script only supports reading vmlinux for disassembly dump, 238286cc55SJames Clark# meaning that any patched instructions will appear 248286cc55SJames Clark# as unpatched, but the instruction ranges themselves will 258286cc55SJames Clark# be correct. In addition to this, source line info comes 268286cc55SJames Clark# from Perf, and when using kcore there is no debug info. The 278286cc55SJames Clark# following lists the supported features in each mode: 2812fdd6c0SLeo Yan# 298286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 308286cc55SJames Clark# | Recording | Accurate decode | Source line dump | Disassembly dump | 318286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 328286cc55SJames Clark# | --kcore | yes | no | yes | 338286cc55SJames Clark# | normal | no | yes | yes | 348286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 358286cc55SJames Clark# 368286cc55SJames Clark# Output disassembly with objdump and auto detect vmlinux 378286cc55SJames Clark# (when running on same machine.) 388286cc55SJames Clark# perf script -s scripts/python/arm-cs-trace-disasm.py -d 398286cc55SJames Clark# 4012fdd6c0SLeo Yan# Output disassembly with llvm-objdump: 4112fdd6c0SLeo Yan# perf script -s scripts/python/arm-cs-trace-disasm.py \ 4212fdd6c0SLeo Yan# -- -d llvm-objdump-11 -k path/to/vmlinux 438286cc55SJames Clark# 4412fdd6c0SLeo Yan# Output only source line and symbols: 4512fdd6c0SLeo Yan# perf script -s scripts/python/arm-cs-trace-disasm.py 4612fdd6c0SLeo Yan 478286cc55SJames Clarkdef default_objdump(): 488286cc55SJames Clark config = perf_config_get("annotate.objdump") 498286cc55SJames Clark return config if config else "objdump" 508286cc55SJames Clark 5112fdd6c0SLeo Yan# Command line parsing. 5266dd3b53SJames Clarkdef int_arg(v): 5366dd3b53SJames Clark v = int(v) 5466dd3b53SJames Clark if v < 0: 5566dd3b53SJames Clark raise argparse.ArgumentTypeError("Argument must be a positive integer") 5666dd3b53SJames Clark return v 5766dd3b53SJames Clark 587b371afcSJames Clarkargs = argparse.ArgumentParser() 598286cc55SJames Clarkargs.add_argument("-k", "--vmlinux", 608286cc55SJames Clark help="Set path to vmlinux file. Omit to autodetect if running on same machine") 618286cc55SJames Clarkargs.add_argument("-d", "--objdump", nargs="?", const=default_objdump(), 628286cc55SJames Clark help="Show disassembly. Can also be used to change the objdump path"), 637b371afcSJames Clarkargs.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log") 6466dd3b53SJames Clarkargs.add_argument("--start-time", type=int_arg, help="Monotonic clock time of sample to start from. " 6566dd3b53SJames Clark "See 'time' field on samples in -v mode.") 6666dd3b53SJames Clarkargs.add_argument("--stop-time", type=int_arg, help="Monotonic clock time of sample to stop at. " 6766dd3b53SJames Clark "See 'time' field on samples in -v mode.") 6866dd3b53SJames Clarkargs.add_argument("--start-sample", type=int_arg, help="Index of sample to start from. " 6966dd3b53SJames Clark "See 'index' field on samples in -v mode.") 7066dd3b53SJames Clarkargs.add_argument("--stop-sample", type=int_arg, help="Index of sample to stop at. " 7166dd3b53SJames Clark "See 'index' field on samples in -v mode.") 7266dd3b53SJames Clark 737b371afcSJames Clarkoptions = args.parse_args() 7466dd3b53SJames Clarkif (options.start_time and options.stop_time and 7566dd3b53SJames Clark options.start_time >= options.stop_time): 7666dd3b53SJames Clark print("--start-time must less than --stop-time") 7766dd3b53SJames Clark exit(2) 7866dd3b53SJames Clarkif (options.start_sample and options.stop_sample and 7966dd3b53SJames Clark options.start_sample >= options.stop_sample): 8066dd3b53SJames Clark print("--start-sample must less than --stop-sample") 8166dd3b53SJames Clark exit(2) 8212fdd6c0SLeo Yan 8312fdd6c0SLeo Yan# Initialize global dicts and regular expression 8412fdd6c0SLeo Yandisasm_cache = dict() 8512fdd6c0SLeo Yancpu_data = dict() 86280b4e4aSBenjamin Graydisasm_re = re.compile(r"^\s*([0-9a-fA-F]+):") 87280b4e4aSBenjamin Graydisasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:") 8812fdd6c0SLeo Yancache_size = 64*1024 8966dd3b53SJames Clarksample_idx = -1 9012fdd6c0SLeo Yan 9112fdd6c0SLeo Yanglb_source_file_name = None 9212fdd6c0SLeo Yanglb_line_number = None 9312fdd6c0SLeo Yanglb_dso = None 9412fdd6c0SLeo Yan 958286cc55SJames Clarkkver = platform.release() 968286cc55SJames Clarkvmlinux_paths = [ 978286cc55SJames Clark f"/usr/lib/debug/boot/vmlinux-{kver}.debug", 988286cc55SJames Clark f"/usr/lib/debug/lib/modules/{kver}/vmlinux", 998286cc55SJames Clark f"/lib/modules/{kver}/build/vmlinux", 1008286cc55SJames Clark f"/usr/lib/debug/boot/vmlinux-{kver}", 1018286cc55SJames Clark f"/boot/vmlinux-{kver}", 1028286cc55SJames Clark f"/boot/vmlinux", 1038286cc55SJames Clark f"vmlinux" 1048286cc55SJames Clark] 1058286cc55SJames Clark 10612fdd6c0SLeo Yandef get_optional(perf_dict, field): 10712fdd6c0SLeo Yan if field in perf_dict: 10812fdd6c0SLeo Yan return perf_dict[field] 10912fdd6c0SLeo Yan return "[unknown]" 11012fdd6c0SLeo Yan 11112fdd6c0SLeo Yandef get_offset(perf_dict, field): 11212fdd6c0SLeo Yan if field in perf_dict: 113b2265219SLeo Yan return "+%#x" % perf_dict[field] 11412fdd6c0SLeo Yan return "" 11512fdd6c0SLeo Yan 1168286cc55SJames Clarkdef find_vmlinux(): 1178286cc55SJames Clark if hasattr(find_vmlinux, "path"): 1188286cc55SJames Clark return find_vmlinux.path 1198286cc55SJames Clark 1208286cc55SJames Clark for v in vmlinux_paths: 1218286cc55SJames Clark if os.access(v, os.R_OK): 1228286cc55SJames Clark find_vmlinux.path = v 1238286cc55SJames Clark break 1248286cc55SJames Clark else: 1258286cc55SJames Clark find_vmlinux.path = None 1268286cc55SJames Clark 1278286cc55SJames Clark return find_vmlinux.path 1288286cc55SJames Clark 12912fdd6c0SLeo Yandef get_dso_file_path(dso_name, dso_build_id): 13012fdd6c0SLeo Yan if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"): 1317b371afcSJames Clark if (options.vmlinux): 1327b371afcSJames Clark return options.vmlinux; 13312fdd6c0SLeo Yan else: 1348286cc55SJames Clark return find_vmlinux() if find_vmlinux() else dso_name 13512fdd6c0SLeo Yan 13612fdd6c0SLeo Yan if (dso_name == "[vdso]") : 13712fdd6c0SLeo Yan append = "/vdso" 13812fdd6c0SLeo Yan else: 13912fdd6c0SLeo Yan append = "/elf" 14012fdd6c0SLeo Yan 141b2265219SLeo Yan dso_path = os.environ['PERF_BUILDID_DIR'] + "/" + dso_name + "/" + dso_build_id + append; 14212fdd6c0SLeo Yan # Replace duplicate slash chars to single slash char 14312fdd6c0SLeo Yan dso_path = dso_path.replace('//', '/', 1) 14412fdd6c0SLeo Yan return dso_path 14512fdd6c0SLeo Yan 14612fdd6c0SLeo Yandef read_disam(dso_fname, dso_start, start_addr, stop_addr): 14712fdd6c0SLeo Yan addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname 14812fdd6c0SLeo Yan 14912fdd6c0SLeo Yan # Don't let the cache get too big, clear it when it hits max size 15012fdd6c0SLeo Yan if (len(disasm_cache) > cache_size): 15112fdd6c0SLeo Yan disasm_cache.clear(); 15212fdd6c0SLeo Yan 15312fdd6c0SLeo Yan if addr_range in disasm_cache: 15412fdd6c0SLeo Yan disasm_output = disasm_cache[addr_range]; 15512fdd6c0SLeo Yan else: 15612fdd6c0SLeo Yan start_addr = start_addr - dso_start; 15712fdd6c0SLeo Yan stop_addr = stop_addr - dso_start; 1587b371afcSJames Clark disasm = [ options.objdump, "-d", "-z", 159b2265219SLeo Yan "--start-address="+format(start_addr,"#x"), 160b2265219SLeo Yan "--stop-address="+format(stop_addr,"#x") ] 16112fdd6c0SLeo Yan disasm += [ dso_fname ] 16212fdd6c0SLeo Yan disasm_output = check_output(disasm).decode('utf-8').split('\n') 16312fdd6c0SLeo Yan disasm_cache[addr_range] = disasm_output 16412fdd6c0SLeo Yan 16512fdd6c0SLeo Yan return disasm_output 16612fdd6c0SLeo Yan 16712fdd6c0SLeo Yandef print_disam(dso_fname, dso_start, start_addr, stop_addr): 16812fdd6c0SLeo Yan for line in read_disam(dso_fname, dso_start, start_addr, stop_addr): 16912fdd6c0SLeo Yan m = disasm_func_re.search(line) 17012fdd6c0SLeo Yan if m is None: 17112fdd6c0SLeo Yan m = disasm_re.search(line) 17212fdd6c0SLeo Yan if m is None: 17312fdd6c0SLeo Yan continue 174b2265219SLeo Yan print("\t" + line) 17512fdd6c0SLeo Yan 17612fdd6c0SLeo Yandef print_sample(sample): 177b2265219SLeo Yan print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \ 17866dd3b53SJames Clark "pid: %d tid: %d period: %d time: %d index: %d}" % \ 179b2265219SLeo Yan (sample['cpu'], sample['addr'], sample['phys_addr'], \ 180b2265219SLeo Yan sample['ip'], sample['pid'], sample['tid'], \ 18166dd3b53SJames Clark sample['period'], sample['time'], sample_idx)) 18212fdd6c0SLeo Yan 18312fdd6c0SLeo Yandef trace_begin(): 18412fdd6c0SLeo Yan print('ARM CoreSight Trace Data Assembler Dump') 18512fdd6c0SLeo Yan 18612fdd6c0SLeo Yandef trace_end(): 18712fdd6c0SLeo Yan print('End') 18812fdd6c0SLeo Yan 18912fdd6c0SLeo Yandef trace_unhandled(event_name, context, event_fields_dict): 19012fdd6c0SLeo Yan print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])) 19112fdd6c0SLeo Yan 19212fdd6c0SLeo Yandef common_start_str(comm, sample): 19312fdd6c0SLeo Yan sec = int(sample["time"] / 1000000000) 19412fdd6c0SLeo Yan ns = sample["time"] % 1000000000 19512fdd6c0SLeo Yan cpu = sample["cpu"] 19612fdd6c0SLeo Yan pid = sample["pid"] 19712fdd6c0SLeo Yan tid = sample["tid"] 198b2265219SLeo Yan return "%16s %5u/%-5u [%04u] %9u.%09u " % (comm, pid, tid, cpu, sec, ns) 19912fdd6c0SLeo Yan 20012fdd6c0SLeo Yan# This code is copied from intel-pt-events.py for printing source code 20112fdd6c0SLeo Yan# line and symbols. 20212fdd6c0SLeo Yandef print_srccode(comm, param_dict, sample, symbol, dso): 20312fdd6c0SLeo Yan ip = sample["ip"] 20412fdd6c0SLeo Yan if symbol == "[unknown]": 20512fdd6c0SLeo Yan start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40) 20612fdd6c0SLeo Yan else: 20712fdd6c0SLeo Yan offs = get_offset(param_dict, "symoff") 20812fdd6c0SLeo Yan start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40) 20912fdd6c0SLeo Yan 21012fdd6c0SLeo Yan global glb_source_file_name 21112fdd6c0SLeo Yan global glb_line_number 21212fdd6c0SLeo Yan global glb_dso 21312fdd6c0SLeo Yan 21412fdd6c0SLeo Yan source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context) 21512fdd6c0SLeo Yan if source_file_name: 21612fdd6c0SLeo Yan if glb_line_number == line_number and glb_source_file_name == source_file_name: 21712fdd6c0SLeo Yan src_str = "" 21812fdd6c0SLeo Yan else: 21912fdd6c0SLeo Yan if len(source_file_name) > 40: 22012fdd6c0SLeo Yan src_file = ("..." + source_file_name[-37:]) + " " 22112fdd6c0SLeo Yan else: 22212fdd6c0SLeo Yan src_file = source_file_name.ljust(41) 22312fdd6c0SLeo Yan 22412fdd6c0SLeo Yan if source_line is None: 22512fdd6c0SLeo Yan src_str = src_file + str(line_number).rjust(4) + " <source not found>" 22612fdd6c0SLeo Yan else: 22712fdd6c0SLeo Yan src_str = src_file + str(line_number).rjust(4) + " " + source_line 22812fdd6c0SLeo Yan glb_dso = None 22912fdd6c0SLeo Yan elif dso == glb_dso: 23012fdd6c0SLeo Yan src_str = "" 23112fdd6c0SLeo Yan else: 23212fdd6c0SLeo Yan src_str = dso 23312fdd6c0SLeo Yan glb_dso = dso 23412fdd6c0SLeo Yan 23512fdd6c0SLeo Yan glb_line_number = line_number 23612fdd6c0SLeo Yan glb_source_file_name = source_file_name 23712fdd6c0SLeo Yan 238b2265219SLeo Yan print(start_str, src_str) 23912fdd6c0SLeo Yan 24012fdd6c0SLeo Yandef process_event(param_dict): 24112fdd6c0SLeo Yan global cache_size 24212fdd6c0SLeo Yan global options 24366dd3b53SJames Clark global sample_idx 24412fdd6c0SLeo Yan 24512fdd6c0SLeo Yan sample = param_dict["sample"] 24612fdd6c0SLeo Yan comm = param_dict["comm"] 24712fdd6c0SLeo Yan 24812fdd6c0SLeo Yan name = param_dict["ev_name"] 24912fdd6c0SLeo Yan dso = get_optional(param_dict, "dso") 25012fdd6c0SLeo Yan dso_bid = get_optional(param_dict, "dso_bid") 25112fdd6c0SLeo Yan dso_start = get_optional(param_dict, "dso_map_start") 25212fdd6c0SLeo Yan dso_end = get_optional(param_dict, "dso_map_end") 25312fdd6c0SLeo Yan symbol = get_optional(param_dict, "symbol") 254*e8328bf3SSteve Clevenger map_pgoff = get_optional(param_dict, "map_pgoff") 255*e8328bf3SSteve Clevenger # check for valid map offset 256*e8328bf3SSteve Clevenger if (str(map_pgoff) == '[unknown]'): 257*e8328bf3SSteve Clevenger map_pgoff = 0 25812fdd6c0SLeo Yan 2592d98dbb4SRuidong Tian cpu = sample["cpu"] 2602d98dbb4SRuidong Tian ip = sample["ip"] 2612d98dbb4SRuidong Tian addr = sample["addr"] 2622d98dbb4SRuidong Tian 26366dd3b53SJames Clark sample_idx += 1 26466dd3b53SJames Clark 26566dd3b53SJames Clark if (options.start_time and sample["time"] < options.start_time): 26666dd3b53SJames Clark return 26766dd3b53SJames Clark if (options.stop_time and sample["time"] > options.stop_time): 26866dd3b53SJames Clark exit(0) 26966dd3b53SJames Clark if (options.start_sample and sample_idx < options.start_sample): 27066dd3b53SJames Clark return 27166dd3b53SJames Clark if (options.stop_sample and sample_idx > options.stop_sample): 27266dd3b53SJames Clark exit(0) 27366dd3b53SJames Clark 274ae8e4f40SJames Clark if (options.verbose == True): 275ae8e4f40SJames Clark print("Event type: %s" % name) 276ae8e4f40SJames Clark print_sample(sample) 277ae8e4f40SJames Clark 2782d98dbb4SRuidong Tian # Initialize CPU data if it's empty, and directly return back 2792d98dbb4SRuidong Tian # if this is the first tracing event for this CPU. 2802d98dbb4SRuidong Tian if (cpu_data.get(str(cpu) + 'addr') == None): 2812d98dbb4SRuidong Tian cpu_data[str(cpu) + 'addr'] = addr 2822d98dbb4SRuidong Tian return 2832d98dbb4SRuidong Tian 28412fdd6c0SLeo Yan # If cannot find dso so cannot dump assembler, bail out 28512fdd6c0SLeo Yan if (dso == '[unknown]'): 28612fdd6c0SLeo Yan return 28712fdd6c0SLeo Yan 28812fdd6c0SLeo Yan # Validate dso start and end addresses 28912fdd6c0SLeo Yan if ((dso_start == '[unknown]') or (dso_end == '[unknown]')): 290b2265219SLeo Yan print("Failed to find valid dso map for dso %s" % dso) 29112fdd6c0SLeo Yan return 29212fdd6c0SLeo Yan 29312fdd6c0SLeo Yan if (name[0:12] == "instructions"): 29412fdd6c0SLeo Yan print_srccode(comm, param_dict, sample, symbol, dso) 29512fdd6c0SLeo Yan return 29612fdd6c0SLeo Yan 29712fdd6c0SLeo Yan # Don't proceed if this event is not a branch sample, . 29812fdd6c0SLeo Yan if (name[0:8] != "branches"): 29912fdd6c0SLeo Yan return 30012fdd6c0SLeo Yan 30112fdd6c0SLeo Yan # The format for packet is: 30212fdd6c0SLeo Yan # 30312fdd6c0SLeo Yan # +------------+------------+------------+ 30412fdd6c0SLeo Yan # sample_prev: | addr | ip | cpu | 30512fdd6c0SLeo Yan # +------------+------------+------------+ 30612fdd6c0SLeo Yan # sample_next: | addr | ip | cpu | 30712fdd6c0SLeo Yan # +------------+------------+------------+ 30812fdd6c0SLeo Yan # 30912fdd6c0SLeo Yan # We need to combine the two continuous packets to get the instruction 31012fdd6c0SLeo Yan # range for sample_prev::cpu: 31112fdd6c0SLeo Yan # 31212fdd6c0SLeo Yan # [ sample_prev::addr .. sample_next::ip ] 31312fdd6c0SLeo Yan # 31412fdd6c0SLeo Yan # For this purose, sample_prev::addr is stored into cpu_data structure 31512fdd6c0SLeo Yan # and read back for 'start_addr' when the new packet comes, and we need 31612fdd6c0SLeo Yan # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for 31712fdd6c0SLeo Yan # 'stop_addr' is for the sake of objdump so the final assembler dump can 31812fdd6c0SLeo Yan # include last instruction for sample_next::ip. 31912fdd6c0SLeo Yan start_addr = cpu_data[str(cpu) + 'addr'] 32012fdd6c0SLeo Yan stop_addr = ip + 4 32112fdd6c0SLeo Yan 32212fdd6c0SLeo Yan # Record for previous sample packet 32312fdd6c0SLeo Yan cpu_data[str(cpu) + 'addr'] = addr 32412fdd6c0SLeo Yan 325*e8328bf3SSteve Clevenger # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet 326*e8328bf3SSteve Clevenger if (start_addr == 0): 327*e8328bf3SSteve Clevenger if ((stop_addr == 4) and (options.verbose == True)): 328b2265219SLeo Yan print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu) 32912fdd6c0SLeo Yan return 33012fdd6c0SLeo Yan 33112fdd6c0SLeo Yan if (start_addr < int(dso_start) or start_addr > int(dso_end)): 332b2265219SLeo Yan 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)) 33312fdd6c0SLeo Yan return 33412fdd6c0SLeo Yan 33512fdd6c0SLeo Yan if (stop_addr < int(dso_start) or stop_addr > int(dso_end)): 336b2265219SLeo Yan 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)) 33712fdd6c0SLeo Yan return 33812fdd6c0SLeo Yan 3397b371afcSJames Clark if (options.objdump != None): 34012fdd6c0SLeo Yan # It doesn't need to decrease virtual memory offset for disassembly 341c344675aSRuidong Tian # for kernel dso and executable file dso, so in this case we set 342c344675aSRuidong Tian # vm_start to zero. 343c344675aSRuidong Tian if (dso == "[kernel.kallsyms]" or dso_start == 0x400000): 34412fdd6c0SLeo Yan dso_vm_start = 0 345*e8328bf3SSteve Clevenger map_pgoff = 0 34612fdd6c0SLeo Yan else: 34712fdd6c0SLeo Yan dso_vm_start = int(dso_start) 34812fdd6c0SLeo Yan 34912fdd6c0SLeo Yan dso_fname = get_dso_file_path(dso, dso_bid) 35012fdd6c0SLeo Yan if path.exists(dso_fname): 351*e8328bf3SSteve Clevenger print_disam(dso_fname, dso_vm_start, start_addr + map_pgoff, stop_addr + map_pgoff) 35212fdd6c0SLeo Yan else: 353*e8328bf3SSteve Clevenger print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso, start_addr + map_pgoff, stop_addr + map_pgoff)) 35412fdd6c0SLeo Yan 35512fdd6c0SLeo Yan print_srccode(comm, param_dict, sample, symbol, dso) 356