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 15*8286cc55SJames Clarkimport platform 1612fdd6c0SLeo Yan 17*8286cc55SJames Clarkfrom perf_trace_context import perf_sample_srccode, perf_config_get 1812fdd6c0SLeo Yan 1912fdd6c0SLeo Yan# Below are some example commands for using this script. 20*8286cc55SJames Clark# Note a --kcore recording is required for accurate decode 21*8286cc55SJames Clark# due to the alternatives patching mechanism. However this 22*8286cc55SJames Clark# script only supports reading vmlinux for disassembly dump, 23*8286cc55SJames Clark# meaning that any patched instructions will appear 24*8286cc55SJames Clark# as unpatched, but the instruction ranges themselves will 25*8286cc55SJames Clark# be correct. In addition to this, source line info comes 26*8286cc55SJames Clark# from Perf, and when using kcore there is no debug info. The 27*8286cc55SJames Clark# following lists the supported features in each mode: 2812fdd6c0SLeo Yan# 29*8286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 30*8286cc55SJames Clark# | Recording | Accurate decode | Source line dump | Disassembly dump | 31*8286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 32*8286cc55SJames Clark# | --kcore | yes | no | yes | 33*8286cc55SJames Clark# | normal | no | yes | yes | 34*8286cc55SJames Clark# +-----------+-----------------+------------------+------------------+ 35*8286cc55SJames Clark# 36*8286cc55SJames Clark# Output disassembly with objdump and auto detect vmlinux 37*8286cc55SJames Clark# (when running on same machine.) 38*8286cc55SJames Clark# perf script -s scripts/python/arm-cs-trace-disasm.py -d 39*8286cc55SJames 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 43*8286cc55SJames Clark# 4412fdd6c0SLeo Yan# Output only source line and symbols: 4512fdd6c0SLeo Yan# perf script -s scripts/python/arm-cs-trace-disasm.py 4612fdd6c0SLeo Yan 47*8286cc55SJames Clarkdef default_objdump(): 48*8286cc55SJames Clark config = perf_config_get("annotate.objdump") 49*8286cc55SJames Clark return config if config else "objdump" 50*8286cc55SJames Clark 5112fdd6c0SLeo Yan# Command line parsing. 527b371afcSJames Clarkargs = argparse.ArgumentParser() 53*8286cc55SJames Clarkargs.add_argument("-k", "--vmlinux", 54*8286cc55SJames Clark help="Set path to vmlinux file. Omit to autodetect if running on same machine") 55*8286cc55SJames Clarkargs.add_argument("-d", "--objdump", nargs="?", const=default_objdump(), 56*8286cc55SJames Clark help="Show disassembly. Can also be used to change the objdump path"), 577b371afcSJames Clarkargs.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log") 587b371afcSJames Clarkoptions = args.parse_args() 5912fdd6c0SLeo Yan 6012fdd6c0SLeo Yan# Initialize global dicts and regular expression 6112fdd6c0SLeo Yandisasm_cache = dict() 6212fdd6c0SLeo Yancpu_data = dict() 63280b4e4aSBenjamin Graydisasm_re = re.compile(r"^\s*([0-9a-fA-F]+):") 64280b4e4aSBenjamin Graydisasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:") 6512fdd6c0SLeo Yancache_size = 64*1024 6612fdd6c0SLeo Yan 6712fdd6c0SLeo Yanglb_source_file_name = None 6812fdd6c0SLeo Yanglb_line_number = None 6912fdd6c0SLeo Yanglb_dso = None 7012fdd6c0SLeo Yan 71*8286cc55SJames Clarkkver = platform.release() 72*8286cc55SJames Clarkvmlinux_paths = [ 73*8286cc55SJames Clark f"/usr/lib/debug/boot/vmlinux-{kver}.debug", 74*8286cc55SJames Clark f"/usr/lib/debug/lib/modules/{kver}/vmlinux", 75*8286cc55SJames Clark f"/lib/modules/{kver}/build/vmlinux", 76*8286cc55SJames Clark f"/usr/lib/debug/boot/vmlinux-{kver}", 77*8286cc55SJames Clark f"/boot/vmlinux-{kver}", 78*8286cc55SJames Clark f"/boot/vmlinux", 79*8286cc55SJames Clark f"vmlinux" 80*8286cc55SJames Clark] 81*8286cc55SJames Clark 8212fdd6c0SLeo Yandef get_optional(perf_dict, field): 8312fdd6c0SLeo Yan if field in perf_dict: 8412fdd6c0SLeo Yan return perf_dict[field] 8512fdd6c0SLeo Yan return "[unknown]" 8612fdd6c0SLeo Yan 8712fdd6c0SLeo Yandef get_offset(perf_dict, field): 8812fdd6c0SLeo Yan if field in perf_dict: 89b2265219SLeo Yan return "+%#x" % perf_dict[field] 9012fdd6c0SLeo Yan return "" 9112fdd6c0SLeo Yan 92*8286cc55SJames Clarkdef find_vmlinux(): 93*8286cc55SJames Clark if hasattr(find_vmlinux, "path"): 94*8286cc55SJames Clark return find_vmlinux.path 95*8286cc55SJames Clark 96*8286cc55SJames Clark for v in vmlinux_paths: 97*8286cc55SJames Clark if os.access(v, os.R_OK): 98*8286cc55SJames Clark find_vmlinux.path = v 99*8286cc55SJames Clark break 100*8286cc55SJames Clark else: 101*8286cc55SJames Clark find_vmlinux.path = None 102*8286cc55SJames Clark 103*8286cc55SJames Clark return find_vmlinux.path 104*8286cc55SJames Clark 10512fdd6c0SLeo Yandef get_dso_file_path(dso_name, dso_build_id): 10612fdd6c0SLeo Yan if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"): 1077b371afcSJames Clark if (options.vmlinux): 1087b371afcSJames Clark return options.vmlinux; 10912fdd6c0SLeo Yan else: 110*8286cc55SJames Clark return find_vmlinux() if find_vmlinux() else dso_name 11112fdd6c0SLeo Yan 11212fdd6c0SLeo Yan if (dso_name == "[vdso]") : 11312fdd6c0SLeo Yan append = "/vdso" 11412fdd6c0SLeo Yan else: 11512fdd6c0SLeo Yan append = "/elf" 11612fdd6c0SLeo Yan 117b2265219SLeo Yan dso_path = os.environ['PERF_BUILDID_DIR'] + "/" + dso_name + "/" + dso_build_id + append; 11812fdd6c0SLeo Yan # Replace duplicate slash chars to single slash char 11912fdd6c0SLeo Yan dso_path = dso_path.replace('//', '/', 1) 12012fdd6c0SLeo Yan return dso_path 12112fdd6c0SLeo Yan 12212fdd6c0SLeo Yandef read_disam(dso_fname, dso_start, start_addr, stop_addr): 12312fdd6c0SLeo Yan addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname 12412fdd6c0SLeo Yan 12512fdd6c0SLeo Yan # Don't let the cache get too big, clear it when it hits max size 12612fdd6c0SLeo Yan if (len(disasm_cache) > cache_size): 12712fdd6c0SLeo Yan disasm_cache.clear(); 12812fdd6c0SLeo Yan 12912fdd6c0SLeo Yan if addr_range in disasm_cache: 13012fdd6c0SLeo Yan disasm_output = disasm_cache[addr_range]; 13112fdd6c0SLeo Yan else: 13212fdd6c0SLeo Yan start_addr = start_addr - dso_start; 13312fdd6c0SLeo Yan stop_addr = stop_addr - dso_start; 1347b371afcSJames Clark disasm = [ options.objdump, "-d", "-z", 135b2265219SLeo Yan "--start-address="+format(start_addr,"#x"), 136b2265219SLeo Yan "--stop-address="+format(stop_addr,"#x") ] 13712fdd6c0SLeo Yan disasm += [ dso_fname ] 13812fdd6c0SLeo Yan disasm_output = check_output(disasm).decode('utf-8').split('\n') 13912fdd6c0SLeo Yan disasm_cache[addr_range] = disasm_output 14012fdd6c0SLeo Yan 14112fdd6c0SLeo Yan return disasm_output 14212fdd6c0SLeo Yan 14312fdd6c0SLeo Yandef print_disam(dso_fname, dso_start, start_addr, stop_addr): 14412fdd6c0SLeo Yan for line in read_disam(dso_fname, dso_start, start_addr, stop_addr): 14512fdd6c0SLeo Yan m = disasm_func_re.search(line) 14612fdd6c0SLeo Yan if m is None: 14712fdd6c0SLeo Yan m = disasm_re.search(line) 14812fdd6c0SLeo Yan if m is None: 14912fdd6c0SLeo Yan continue 150b2265219SLeo Yan print("\t" + line) 15112fdd6c0SLeo Yan 15212fdd6c0SLeo Yandef print_sample(sample): 153b2265219SLeo Yan print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \ 154b2265219SLeo Yan "pid: %d tid: %d period: %d time: %d }" % \ 155b2265219SLeo Yan (sample['cpu'], sample['addr'], sample['phys_addr'], \ 156b2265219SLeo Yan sample['ip'], sample['pid'], sample['tid'], \ 157b2265219SLeo Yan sample['period'], sample['time'])) 15812fdd6c0SLeo Yan 15912fdd6c0SLeo Yandef trace_begin(): 16012fdd6c0SLeo Yan print('ARM CoreSight Trace Data Assembler Dump') 16112fdd6c0SLeo Yan 16212fdd6c0SLeo Yandef trace_end(): 16312fdd6c0SLeo Yan print('End') 16412fdd6c0SLeo Yan 16512fdd6c0SLeo Yandef trace_unhandled(event_name, context, event_fields_dict): 16612fdd6c0SLeo Yan print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])) 16712fdd6c0SLeo Yan 16812fdd6c0SLeo Yandef common_start_str(comm, sample): 16912fdd6c0SLeo Yan sec = int(sample["time"] / 1000000000) 17012fdd6c0SLeo Yan ns = sample["time"] % 1000000000 17112fdd6c0SLeo Yan cpu = sample["cpu"] 17212fdd6c0SLeo Yan pid = sample["pid"] 17312fdd6c0SLeo Yan tid = sample["tid"] 174b2265219SLeo Yan return "%16s %5u/%-5u [%04u] %9u.%09u " % (comm, pid, tid, cpu, sec, ns) 17512fdd6c0SLeo Yan 17612fdd6c0SLeo Yan# This code is copied from intel-pt-events.py for printing source code 17712fdd6c0SLeo Yan# line and symbols. 17812fdd6c0SLeo Yandef print_srccode(comm, param_dict, sample, symbol, dso): 17912fdd6c0SLeo Yan ip = sample["ip"] 18012fdd6c0SLeo Yan if symbol == "[unknown]": 18112fdd6c0SLeo Yan start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40) 18212fdd6c0SLeo Yan else: 18312fdd6c0SLeo Yan offs = get_offset(param_dict, "symoff") 18412fdd6c0SLeo Yan start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40) 18512fdd6c0SLeo Yan 18612fdd6c0SLeo Yan global glb_source_file_name 18712fdd6c0SLeo Yan global glb_line_number 18812fdd6c0SLeo Yan global glb_dso 18912fdd6c0SLeo Yan 19012fdd6c0SLeo Yan source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context) 19112fdd6c0SLeo Yan if source_file_name: 19212fdd6c0SLeo Yan if glb_line_number == line_number and glb_source_file_name == source_file_name: 19312fdd6c0SLeo Yan src_str = "" 19412fdd6c0SLeo Yan else: 19512fdd6c0SLeo Yan if len(source_file_name) > 40: 19612fdd6c0SLeo Yan src_file = ("..." + source_file_name[-37:]) + " " 19712fdd6c0SLeo Yan else: 19812fdd6c0SLeo Yan src_file = source_file_name.ljust(41) 19912fdd6c0SLeo Yan 20012fdd6c0SLeo Yan if source_line is None: 20112fdd6c0SLeo Yan src_str = src_file + str(line_number).rjust(4) + " <source not found>" 20212fdd6c0SLeo Yan else: 20312fdd6c0SLeo Yan src_str = src_file + str(line_number).rjust(4) + " " + source_line 20412fdd6c0SLeo Yan glb_dso = None 20512fdd6c0SLeo Yan elif dso == glb_dso: 20612fdd6c0SLeo Yan src_str = "" 20712fdd6c0SLeo Yan else: 20812fdd6c0SLeo Yan src_str = dso 20912fdd6c0SLeo Yan glb_dso = dso 21012fdd6c0SLeo Yan 21112fdd6c0SLeo Yan glb_line_number = line_number 21212fdd6c0SLeo Yan glb_source_file_name = source_file_name 21312fdd6c0SLeo Yan 214b2265219SLeo Yan print(start_str, src_str) 21512fdd6c0SLeo Yan 21612fdd6c0SLeo Yandef process_event(param_dict): 21712fdd6c0SLeo Yan global cache_size 21812fdd6c0SLeo Yan global options 21912fdd6c0SLeo Yan 22012fdd6c0SLeo Yan sample = param_dict["sample"] 22112fdd6c0SLeo Yan comm = param_dict["comm"] 22212fdd6c0SLeo Yan 22312fdd6c0SLeo Yan name = param_dict["ev_name"] 22412fdd6c0SLeo Yan dso = get_optional(param_dict, "dso") 22512fdd6c0SLeo Yan dso_bid = get_optional(param_dict, "dso_bid") 22612fdd6c0SLeo Yan dso_start = get_optional(param_dict, "dso_map_start") 22712fdd6c0SLeo Yan dso_end = get_optional(param_dict, "dso_map_end") 22812fdd6c0SLeo Yan symbol = get_optional(param_dict, "symbol") 22912fdd6c0SLeo Yan 2302d98dbb4SRuidong Tian cpu = sample["cpu"] 2312d98dbb4SRuidong Tian ip = sample["ip"] 2322d98dbb4SRuidong Tian addr = sample["addr"] 2332d98dbb4SRuidong Tian 234ae8e4f40SJames Clark if (options.verbose == True): 235ae8e4f40SJames Clark print("Event type: %s" % name) 236ae8e4f40SJames Clark print_sample(sample) 237ae8e4f40SJames Clark 2382d98dbb4SRuidong Tian # Initialize CPU data if it's empty, and directly return back 2392d98dbb4SRuidong Tian # if this is the first tracing event for this CPU. 2402d98dbb4SRuidong Tian if (cpu_data.get(str(cpu) + 'addr') == None): 2412d98dbb4SRuidong Tian cpu_data[str(cpu) + 'addr'] = addr 2422d98dbb4SRuidong Tian return 2432d98dbb4SRuidong Tian 24412fdd6c0SLeo Yan # If cannot find dso so cannot dump assembler, bail out 24512fdd6c0SLeo Yan if (dso == '[unknown]'): 24612fdd6c0SLeo Yan return 24712fdd6c0SLeo Yan 24812fdd6c0SLeo Yan # Validate dso start and end addresses 24912fdd6c0SLeo Yan if ((dso_start == '[unknown]') or (dso_end == '[unknown]')): 250b2265219SLeo Yan print("Failed to find valid dso map for dso %s" % dso) 25112fdd6c0SLeo Yan return 25212fdd6c0SLeo Yan 25312fdd6c0SLeo Yan if (name[0:12] == "instructions"): 25412fdd6c0SLeo Yan print_srccode(comm, param_dict, sample, symbol, dso) 25512fdd6c0SLeo Yan return 25612fdd6c0SLeo Yan 25712fdd6c0SLeo Yan # Don't proceed if this event is not a branch sample, . 25812fdd6c0SLeo Yan if (name[0:8] != "branches"): 25912fdd6c0SLeo Yan return 26012fdd6c0SLeo Yan 26112fdd6c0SLeo Yan # The format for packet is: 26212fdd6c0SLeo Yan # 26312fdd6c0SLeo Yan # +------------+------------+------------+ 26412fdd6c0SLeo Yan # sample_prev: | addr | ip | cpu | 26512fdd6c0SLeo Yan # +------------+------------+------------+ 26612fdd6c0SLeo Yan # sample_next: | addr | ip | cpu | 26712fdd6c0SLeo Yan # +------------+------------+------------+ 26812fdd6c0SLeo Yan # 26912fdd6c0SLeo Yan # We need to combine the two continuous packets to get the instruction 27012fdd6c0SLeo Yan # range for sample_prev::cpu: 27112fdd6c0SLeo Yan # 27212fdd6c0SLeo Yan # [ sample_prev::addr .. sample_next::ip ] 27312fdd6c0SLeo Yan # 27412fdd6c0SLeo Yan # For this purose, sample_prev::addr is stored into cpu_data structure 27512fdd6c0SLeo Yan # and read back for 'start_addr' when the new packet comes, and we need 27612fdd6c0SLeo Yan # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for 27712fdd6c0SLeo Yan # 'stop_addr' is for the sake of objdump so the final assembler dump can 27812fdd6c0SLeo Yan # include last instruction for sample_next::ip. 27912fdd6c0SLeo Yan start_addr = cpu_data[str(cpu) + 'addr'] 28012fdd6c0SLeo Yan stop_addr = ip + 4 28112fdd6c0SLeo Yan 28212fdd6c0SLeo Yan # Record for previous sample packet 28312fdd6c0SLeo Yan cpu_data[str(cpu) + 'addr'] = addr 28412fdd6c0SLeo Yan 28512fdd6c0SLeo Yan # Handle CS_ETM_TRACE_ON packet if start_addr=0 and stop_addr=4 28612fdd6c0SLeo Yan if (start_addr == 0 and stop_addr == 4): 287b2265219SLeo Yan print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu) 28812fdd6c0SLeo Yan return 28912fdd6c0SLeo Yan 29012fdd6c0SLeo Yan if (start_addr < int(dso_start) or start_addr > int(dso_end)): 291b2265219SLeo 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)) 29212fdd6c0SLeo Yan return 29312fdd6c0SLeo Yan 29412fdd6c0SLeo Yan if (stop_addr < int(dso_start) or stop_addr > int(dso_end)): 295b2265219SLeo 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)) 29612fdd6c0SLeo Yan return 29712fdd6c0SLeo Yan 2987b371afcSJames Clark if (options.objdump != None): 29912fdd6c0SLeo Yan # It doesn't need to decrease virtual memory offset for disassembly 300c344675aSRuidong Tian # for kernel dso and executable file dso, so in this case we set 301c344675aSRuidong Tian # vm_start to zero. 302c344675aSRuidong Tian if (dso == "[kernel.kallsyms]" or dso_start == 0x400000): 30312fdd6c0SLeo Yan dso_vm_start = 0 30412fdd6c0SLeo Yan else: 30512fdd6c0SLeo Yan dso_vm_start = int(dso_start) 30612fdd6c0SLeo Yan 30712fdd6c0SLeo Yan dso_fname = get_dso_file_path(dso, dso_bid) 30812fdd6c0SLeo Yan if path.exists(dso_fname): 30912fdd6c0SLeo Yan print_disam(dso_fname, dso_vm_start, start_addr, stop_addr) 31012fdd6c0SLeo Yan else: 311b2265219SLeo Yan print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso, start_addr, stop_addr)) 31212fdd6c0SLeo Yan 31312fdd6c0SLeo Yan print_srccode(comm, param_dict, sample, symbol, dso) 314