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 15 16from perf_trace_context import perf_set_itrace_options, \ 17 perf_sample_insn, perf_sample_srccode 18 19# Below are some example commands for using this script. 20# 21# Output disassembly with objdump: 22# perf script -s scripts/python/arm-cs-trace-disasm.py \ 23# -- -d objdump -k path/to/vmlinux 24# Output disassembly with llvm-objdump: 25# perf script -s scripts/python/arm-cs-trace-disasm.py \ 26# -- -d llvm-objdump-11 -k path/to/vmlinux 27# Output only source line and symbols: 28# perf script -s scripts/python/arm-cs-trace-disasm.py 29 30# Command line parsing. 31args = argparse.ArgumentParser() 32args.add_argument("-k", "--vmlinux", help="Set path to vmlinux file") 33args.add_argument("-d", "--objdump", help="Set path to objdump executable file"), 34args.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log") 35options = args.parse_args() 36 37# Initialize global dicts and regular expression 38disasm_cache = dict() 39cpu_data = dict() 40disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):") 41disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:") 42cache_size = 64*1024 43 44glb_source_file_name = None 45glb_line_number = None 46glb_dso = None 47 48def get_optional(perf_dict, field): 49 if field in perf_dict: 50 return perf_dict[field] 51 return "[unknown]" 52 53def get_offset(perf_dict, field): 54 if field in perf_dict: 55 return "+%#x" % perf_dict[field] 56 return "" 57 58def get_dso_file_path(dso_name, dso_build_id): 59 if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"): 60 if (options.vmlinux): 61 return options.vmlinux; 62 else: 63 return dso_name 64 65 if (dso_name == "[vdso]") : 66 append = "/vdso" 67 else: 68 append = "/elf" 69 70 dso_path = os.environ['PERF_BUILDID_DIR'] + "/" + dso_name + "/" + dso_build_id + append; 71 # Replace duplicate slash chars to single slash char 72 dso_path = dso_path.replace('//', '/', 1) 73 return dso_path 74 75def read_disam(dso_fname, dso_start, start_addr, stop_addr): 76 addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname 77 78 # Don't let the cache get too big, clear it when it hits max size 79 if (len(disasm_cache) > cache_size): 80 disasm_cache.clear(); 81 82 if addr_range in disasm_cache: 83 disasm_output = disasm_cache[addr_range]; 84 else: 85 start_addr = start_addr - dso_start; 86 stop_addr = stop_addr - dso_start; 87 disasm = [ options.objdump, "-d", "-z", 88 "--start-address="+format(start_addr,"#x"), 89 "--stop-address="+format(stop_addr,"#x") ] 90 disasm += [ dso_fname ] 91 disasm_output = check_output(disasm).decode('utf-8').split('\n') 92 disasm_cache[addr_range] = disasm_output 93 94 return disasm_output 95 96def print_disam(dso_fname, dso_start, start_addr, stop_addr): 97 for line in read_disam(dso_fname, dso_start, start_addr, stop_addr): 98 m = disasm_func_re.search(line) 99 if m is None: 100 m = disasm_re.search(line) 101 if m is None: 102 continue 103 print("\t" + line) 104 105def print_sample(sample): 106 print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \ 107 "pid: %d tid: %d period: %d time: %d }" % \ 108 (sample['cpu'], sample['addr'], sample['phys_addr'], \ 109 sample['ip'], sample['pid'], sample['tid'], \ 110 sample['period'], sample['time'])) 111 112def trace_begin(): 113 print('ARM CoreSight Trace Data Assembler Dump') 114 115def trace_end(): 116 print('End') 117 118def trace_unhandled(event_name, context, event_fields_dict): 119 print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])) 120 121def common_start_str(comm, sample): 122 sec = int(sample["time"] / 1000000000) 123 ns = sample["time"] % 1000000000 124 cpu = sample["cpu"] 125 pid = sample["pid"] 126 tid = sample["tid"] 127 return "%16s %5u/%-5u [%04u] %9u.%09u " % (comm, pid, tid, cpu, sec, ns) 128 129# This code is copied from intel-pt-events.py for printing source code 130# line and symbols. 131def print_srccode(comm, param_dict, sample, symbol, dso): 132 ip = sample["ip"] 133 if symbol == "[unknown]": 134 start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40) 135 else: 136 offs = get_offset(param_dict, "symoff") 137 start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40) 138 139 global glb_source_file_name 140 global glb_line_number 141 global glb_dso 142 143 source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context) 144 if source_file_name: 145 if glb_line_number == line_number and glb_source_file_name == source_file_name: 146 src_str = "" 147 else: 148 if len(source_file_name) > 40: 149 src_file = ("..." + source_file_name[-37:]) + " " 150 else: 151 src_file = source_file_name.ljust(41) 152 153 if source_line is None: 154 src_str = src_file + str(line_number).rjust(4) + " <source not found>" 155 else: 156 src_str = src_file + str(line_number).rjust(4) + " " + source_line 157 glb_dso = None 158 elif dso == glb_dso: 159 src_str = "" 160 else: 161 src_str = dso 162 glb_dso = dso 163 164 glb_line_number = line_number 165 glb_source_file_name = source_file_name 166 167 print(start_str, src_str) 168 169def process_event(param_dict): 170 global cache_size 171 global options 172 173 sample = param_dict["sample"] 174 comm = param_dict["comm"] 175 176 name = param_dict["ev_name"] 177 dso = get_optional(param_dict, "dso") 178 dso_bid = get_optional(param_dict, "dso_bid") 179 dso_start = get_optional(param_dict, "dso_map_start") 180 dso_end = get_optional(param_dict, "dso_map_end") 181 symbol = get_optional(param_dict, "symbol") 182 183 cpu = sample["cpu"] 184 ip = sample["ip"] 185 addr = sample["addr"] 186 187 if (options.verbose == True): 188 print("Event type: %s" % name) 189 print_sample(sample) 190 191 # Initialize CPU data if it's empty, and directly return back 192 # if this is the first tracing event for this CPU. 193 if (cpu_data.get(str(cpu) + 'addr') == None): 194 cpu_data[str(cpu) + 'addr'] = addr 195 return 196 197 # If cannot find dso so cannot dump assembler, bail out 198 if (dso == '[unknown]'): 199 return 200 201 # Validate dso start and end addresses 202 if ((dso_start == '[unknown]') or (dso_end == '[unknown]')): 203 print("Failed to find valid dso map for dso %s" % dso) 204 return 205 206 if (name[0:12] == "instructions"): 207 print_srccode(comm, param_dict, sample, symbol, dso) 208 return 209 210 # Don't proceed if this event is not a branch sample, . 211 if (name[0:8] != "branches"): 212 return 213 214 # The format for packet is: 215 # 216 # +------------+------------+------------+ 217 # sample_prev: | addr | ip | cpu | 218 # +------------+------------+------------+ 219 # sample_next: | addr | ip | cpu | 220 # +------------+------------+------------+ 221 # 222 # We need to combine the two continuous packets to get the instruction 223 # range for sample_prev::cpu: 224 # 225 # [ sample_prev::addr .. sample_next::ip ] 226 # 227 # For this purose, sample_prev::addr is stored into cpu_data structure 228 # and read back for 'start_addr' when the new packet comes, and we need 229 # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for 230 # 'stop_addr' is for the sake of objdump so the final assembler dump can 231 # include last instruction for sample_next::ip. 232 start_addr = cpu_data[str(cpu) + 'addr'] 233 stop_addr = ip + 4 234 235 # Record for previous sample packet 236 cpu_data[str(cpu) + 'addr'] = addr 237 238 # Handle CS_ETM_TRACE_ON packet if start_addr=0 and stop_addr=4 239 if (start_addr == 0 and stop_addr == 4): 240 print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu) 241 return 242 243 if (start_addr < int(dso_start) or start_addr > int(dso_end)): 244 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)) 245 return 246 247 if (stop_addr < int(dso_start) or stop_addr > int(dso_end)): 248 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)) 249 return 250 251 if (options.objdump != None): 252 # It doesn't need to decrease virtual memory offset for disassembly 253 # for kernel dso and executable file dso, so in this case we set 254 # vm_start to zero. 255 if (dso == "[kernel.kallsyms]" or dso_start == 0x400000): 256 dso_vm_start = 0 257 else: 258 dso_vm_start = int(dso_start) 259 260 dso_fname = get_dso_file_path(dso, dso_bid) 261 if path.exists(dso_fname): 262 print_disam(dso_fname, dso_vm_start, start_addr, stop_addr) 263 else: 264 print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso, start_addr, stop_addr)) 265 266 print_srccode(comm, param_dict, sample, symbol, dso) 267