cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

intel-pt-events.py (12399B)


      1# SPDX-License-Identifier: GPL-2.0
      2# intel-pt-events.py: Print Intel PT Events including Power Events and PTWRITE
      3# Copyright (c) 2017-2021, Intel Corporation.
      4#
      5# This program is free software; you can redistribute it and/or modify it
      6# under the terms and conditions of the GNU General Public License,
      7# version 2, as published by the Free Software Foundation.
      8#
      9# This program is distributed in the hope it will be useful, but WITHOUT
     10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     11# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
     12# more details.
     13
     14from __future__ import print_function
     15
     16import os
     17import sys
     18import struct
     19import argparse
     20
     21from libxed import LibXED
     22from ctypes import create_string_buffer, addressof
     23
     24sys.path.append(os.environ['PERF_EXEC_PATH'] + \
     25	'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
     26
     27from perf_trace_context import perf_set_itrace_options, \
     28	perf_sample_insn, perf_sample_srccode
     29
     30try:
     31	broken_pipe_exception = BrokenPipeError
     32except:
     33	broken_pipe_exception = IOError
     34
     35glb_switch_str		= {}
     36glb_insn		= False
     37glb_disassembler	= None
     38glb_src			= False
     39glb_source_file_name	= None
     40glb_line_number		= None
     41glb_dso			= None
     42
     43def get_optional_null(perf_dict, field):
     44	if field in perf_dict:
     45		return perf_dict[field]
     46	return ""
     47
     48def get_optional_zero(perf_dict, field):
     49	if field in perf_dict:
     50		return perf_dict[field]
     51	return 0
     52
     53def get_optional_bytes(perf_dict, field):
     54	if field in perf_dict:
     55		return perf_dict[field]
     56	return bytes()
     57
     58def get_optional(perf_dict, field):
     59	if field in perf_dict:
     60		return perf_dict[field]
     61	return "[unknown]"
     62
     63def get_offset(perf_dict, field):
     64	if field in perf_dict:
     65		return "+%#x" % perf_dict[field]
     66	return ""
     67
     68def trace_begin():
     69	ap = argparse.ArgumentParser(usage = "", add_help = False)
     70	ap.add_argument("--insn-trace", action='store_true')
     71	ap.add_argument("--src-trace", action='store_true')
     72	ap.add_argument("--all-switch-events", action='store_true')
     73	global glb_args
     74	global glb_insn
     75	global glb_src
     76	glb_args = ap.parse_args()
     77	if glb_args.insn_trace:
     78		print("Intel PT Instruction Trace")
     79		itrace = "i0nsepwxI"
     80		glb_insn = True
     81	elif glb_args.src_trace:
     82		print("Intel PT Source Trace")
     83		itrace = "i0nsepwxI"
     84		glb_insn = True
     85		glb_src = True
     86	else:
     87		print("Intel PT Branch Trace, Power Events, Event Trace and PTWRITE")
     88		itrace = "bepwxI"
     89	global glb_disassembler
     90	try:
     91		glb_disassembler = LibXED()
     92	except:
     93		glb_disassembler = None
     94	perf_set_itrace_options(perf_script_context, itrace)
     95
     96def trace_end():
     97	print("End")
     98
     99def trace_unhandled(event_name, context, event_fields_dict):
    100		print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
    101
    102def print_ptwrite(raw_buf):
    103	data = struct.unpack_from("<IQ", raw_buf)
    104	flags = data[0]
    105	payload = data[1]
    106	exact_ip = flags & 1
    107	try:
    108		s = payload.to_bytes(8, "little").decode("ascii").rstrip("\x00")
    109		if not s.isprintable():
    110			s = ""
    111	except:
    112		s = ""
    113	print("IP: %u payload: %#x" % (exact_ip, payload), s, end=' ')
    114
    115def print_cbr(raw_buf):
    116	data = struct.unpack_from("<BBBBII", raw_buf)
    117	cbr = data[0]
    118	f = (data[4] + 500) / 1000
    119	p = ((cbr * 1000 / data[2]) + 5) / 10
    120	print("%3u  freq: %4u MHz  (%3u%%)" % (cbr, f, p), end=' ')
    121
    122def print_mwait(raw_buf):
    123	data = struct.unpack_from("<IQ", raw_buf)
    124	payload = data[1]
    125	hints = payload & 0xff
    126	extensions = (payload >> 32) & 0x3
    127	print("hints: %#x extensions: %#x" % (hints, extensions), end=' ')
    128
    129def print_pwre(raw_buf):
    130	data = struct.unpack_from("<IQ", raw_buf)
    131	payload = data[1]
    132	hw = (payload >> 7) & 1
    133	cstate = (payload >> 12) & 0xf
    134	subcstate = (payload >> 8) & 0xf
    135	print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
    136		end=' ')
    137
    138def print_exstop(raw_buf):
    139	data = struct.unpack_from("<I", raw_buf)
    140	flags = data[0]
    141	exact_ip = flags & 1
    142	print("IP: %u" % (exact_ip), end=' ')
    143
    144def print_pwrx(raw_buf):
    145	data = struct.unpack_from("<IQ", raw_buf)
    146	payload = data[1]
    147	deepest_cstate = payload & 0xf
    148	last_cstate = (payload >> 4) & 0xf
    149	wake_reason = (payload >> 8) & 0xf
    150	print("deepest cstate: %u last cstate: %u wake reason: %#x" %
    151		(deepest_cstate, last_cstate, wake_reason), end=' ')
    152
    153def print_psb(raw_buf):
    154	data = struct.unpack_from("<IQ", raw_buf)
    155	offset = data[1]
    156	print("offset: %#x" % (offset), end=' ')
    157
    158glb_cfe = ["", "INTR", "IRET", "SMI", "RSM", "SIPI", "INIT", "VMENTRY", "VMEXIT",
    159		"VMEXIT_INTR", "SHUTDOWN", "", "UINT", "UIRET"] + [""] * 18
    160glb_evd = ["", "PFA", "VMXQ", "VMXR"] + [""] * 60
    161
    162def print_evt(raw_buf):
    163	data = struct.unpack_from("<BBH", raw_buf)
    164	typ = data[0] & 0x1f
    165	ip_flag = (data[0] & 0x80) >> 7
    166	vector = data[1]
    167	evd_cnt = data[2]
    168	s = glb_cfe[typ]
    169	if s:
    170		print(" cfe: %s IP: %u vector: %u" % (s, ip_flag, vector), end=' ')
    171	else:
    172		print(" cfe: %u IP: %u vector: %u" % (typ, ip_flag, vector), end=' ')
    173	pos = 4
    174	for i in range(evd_cnt):
    175		data = struct.unpack_from("<QQ", raw_buf)
    176		et = data[0] & 0x3f
    177		s = glb_evd[et]
    178		if s:
    179			print("%s: %#x" % (s, data[1]), end=' ')
    180		else:
    181			print("EVD_%u: %#x" % (et, data[1]), end=' ')
    182
    183def print_iflag(raw_buf):
    184	data = struct.unpack_from("<IQ", raw_buf)
    185	iflag = data[0] & 1
    186	old_iflag = iflag ^ 1
    187	via_branch = data[0] & 2
    188	branch_ip = data[1]
    189	if via_branch:
    190		s = "via"
    191	else:
    192		s = "non"
    193	print("IFLAG: %u->%u %s branch" % (old_iflag, iflag, s), end=' ')
    194
    195def common_start_str(comm, sample):
    196	ts = sample["time"]
    197	cpu = sample["cpu"]
    198	pid = sample["pid"]
    199	tid = sample["tid"]
    200	return "%16s %5u/%-5u [%03u] %9u.%09u  " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
    201
    202def print_common_start(comm, sample, name):
    203	flags_disp = get_optional_null(sample, "flags_disp")
    204	# Unused fields:
    205	# period      = sample["period"]
    206	# phys_addr   = sample["phys_addr"]
    207	# weight      = sample["weight"]
    208	# transaction = sample["transaction"]
    209	# cpumode     = get_optional_zero(sample, "cpumode")
    210	print(common_start_str(comm, sample) + "%8s  %21s" % (name, flags_disp), end=' ')
    211
    212def print_instructions_start(comm, sample):
    213	if "x" in get_optional_null(sample, "flags"):
    214		print(common_start_str(comm, sample) + "x", end=' ')
    215	else:
    216		print(common_start_str(comm, sample), end='  ')
    217
    218def disassem(insn, ip):
    219	inst = glb_disassembler.Instruction()
    220	glb_disassembler.SetMode(inst, 0) # Assume 64-bit
    221	buf = create_string_buffer(64)
    222	buf.value = insn
    223	return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip)
    224
    225def print_common_ip(param_dict, sample, symbol, dso):
    226	ip   = sample["ip"]
    227	offs = get_offset(param_dict, "symoff")
    228	if "cyc_cnt" in sample:
    229		cyc_cnt = sample["cyc_cnt"]
    230		insn_cnt = get_optional_zero(sample, "insn_cnt")
    231		ipc_str = "  IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt)
    232	else:
    233		ipc_str = ""
    234	if glb_insn and glb_disassembler is not None:
    235		insn = perf_sample_insn(perf_script_context)
    236		if insn and len(insn):
    237			cnt, text = disassem(insn, ip)
    238			byte_str = ("%x" % ip).rjust(16)
    239			if sys.version_info.major >= 3:
    240				for k in range(cnt):
    241					byte_str += " %02x" % insn[k]
    242			else:
    243				for k in xrange(cnt):
    244					byte_str += " %02x" % ord(insn[k])
    245			print("%-40s  %-30s" % (byte_str, text), end=' ')
    246		print("%s%s (%s)" % (symbol, offs, dso), end=' ')
    247	else:
    248		print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
    249	if "addr_correlates_sym" in sample:
    250		addr   = sample["addr"]
    251		dso    = get_optional(sample, "addr_dso")
    252		symbol = get_optional(sample, "addr_symbol")
    253		offs   = get_offset(sample, "addr_symoff")
    254		print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str))
    255	else:
    256		print(ipc_str)
    257
    258def print_srccode(comm, param_dict, sample, symbol, dso, with_insn):
    259	ip = sample["ip"]
    260	if symbol == "[unknown]":
    261		start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
    262	else:
    263		offs = get_offset(param_dict, "symoff")
    264		start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
    265
    266	if with_insn and glb_insn and glb_disassembler is not None:
    267		insn = perf_sample_insn(perf_script_context)
    268		if insn and len(insn):
    269			cnt, text = disassem(insn, ip)
    270		start_str += text.ljust(30)
    271
    272	global glb_source_file_name
    273	global glb_line_number
    274	global glb_dso
    275
    276	source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
    277	if source_file_name:
    278		if glb_line_number == line_number and glb_source_file_name == source_file_name:
    279			src_str = ""
    280		else:
    281			if len(source_file_name) > 40:
    282				src_file = ("..." + source_file_name[-37:]) + " "
    283			else:
    284				src_file = source_file_name.ljust(41)
    285			if source_line is None:
    286				src_str = src_file + str(line_number).rjust(4) + " <source not found>"
    287			else:
    288				src_str = src_file + str(line_number).rjust(4) + " " + source_line
    289		glb_dso = None
    290	elif dso == glb_dso:
    291		src_str = ""
    292	else:
    293		src_str = dso
    294		glb_dso = dso
    295
    296	glb_line_number = line_number
    297	glb_source_file_name = source_file_name
    298
    299	print(start_str, src_str)
    300
    301def do_process_event(param_dict):
    302	event_attr = param_dict["attr"]
    303	sample	   = param_dict["sample"]
    304	raw_buf	   = param_dict["raw_buf"]
    305	comm	   = param_dict["comm"]
    306	name	   = param_dict["ev_name"]
    307	# Unused fields:
    308	# callchain  = param_dict["callchain"]
    309	# brstack    = param_dict["brstack"]
    310	# brstacksym = param_dict["brstacksym"]
    311
    312	# Symbol and dso info are not always resolved
    313	dso    = get_optional(param_dict, "dso")
    314	symbol = get_optional(param_dict, "symbol")
    315
    316	cpu = sample["cpu"]
    317	if cpu in glb_switch_str:
    318		print(glb_switch_str[cpu])
    319		del glb_switch_str[cpu]
    320
    321	if name[0:12] == "instructions":
    322		if glb_src:
    323			print_srccode(comm, param_dict, sample, symbol, dso, True)
    324		else:
    325			print_instructions_start(comm, sample)
    326			print_common_ip(param_dict, sample, symbol, dso)
    327	elif name[0:8] == "branches":
    328		if glb_src:
    329			print_srccode(comm, param_dict, sample, symbol, dso, False)
    330		else:
    331			print_common_start(comm, sample, name)
    332			print_common_ip(param_dict, sample, symbol, dso)
    333	elif name == "ptwrite":
    334		print_common_start(comm, sample, name)
    335		print_ptwrite(raw_buf)
    336		print_common_ip(param_dict, sample, symbol, dso)
    337	elif name == "cbr":
    338		print_common_start(comm, sample, name)
    339		print_cbr(raw_buf)
    340		print_common_ip(param_dict, sample, symbol, dso)
    341	elif name == "mwait":
    342		print_common_start(comm, sample, name)
    343		print_mwait(raw_buf)
    344		print_common_ip(param_dict, sample, symbol, dso)
    345	elif name == "pwre":
    346		print_common_start(comm, sample, name)
    347		print_pwre(raw_buf)
    348		print_common_ip(param_dict, sample, symbol, dso)
    349	elif name == "exstop":
    350		print_common_start(comm, sample, name)
    351		print_exstop(raw_buf)
    352		print_common_ip(param_dict, sample, symbol, dso)
    353	elif name == "pwrx":
    354		print_common_start(comm, sample, name)
    355		print_pwrx(raw_buf)
    356		print_common_ip(param_dict, sample, symbol, dso)
    357	elif name == "psb":
    358		print_common_start(comm, sample, name)
    359		print_psb(raw_buf)
    360		print_common_ip(param_dict, sample, symbol, dso)
    361	elif name == "evt":
    362		print_common_start(comm, sample, name)
    363		print_evt(raw_buf)
    364		print_common_ip(param_dict, sample, symbol, dso)
    365	elif name == "iflag":
    366		print_common_start(comm, sample, name)
    367		print_iflag(raw_buf)
    368		print_common_ip(param_dict, sample, symbol, dso)
    369	else:
    370		print_common_start(comm, sample, name)
    371		print_common_ip(param_dict, sample, symbol, dso)
    372
    373def process_event(param_dict):
    374	try:
    375		do_process_event(param_dict)
    376	except broken_pipe_exception:
    377		# Stop python printing broken pipe errors and traceback
    378		sys.stdout = open(os.devnull, 'w')
    379		sys.exit(1)
    380
    381def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):
    382	try:
    383		print("%16s %5u/%-5u [%03u] %9u.%09u  error type %u code %u: %s ip 0x%16x" %
    384			("Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip))
    385	except broken_pipe_exception:
    386		# Stop python printing broken pipe errors and traceback
    387		sys.stdout = open(os.devnull, 'w')
    388		sys.exit(1)
    389
    390def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):
    391	if out:
    392		out_str = "Switch out "
    393	else:
    394		out_str = "Switch In  "
    395	if out_preempt:
    396		preempt_str = "preempt"
    397	else:
    398		preempt_str = ""
    399	if machine_pid == -1:
    400		machine_str = ""
    401	else:
    402		machine_str = "machine PID %d" % machine_pid
    403	switch_str = "%16s %5d/%-5d [%03u] %9u.%09u %5d/%-5d %s %s" % \
    404		(out_str, pid, tid, cpu, ts / 1000000000, ts %1000000000, np_pid, np_tid, machine_str, preempt_str)
    405	if glb_args.all_switch_events:
    406		print(switch_str);
    407	else:
    408		global glb_switch_str
    409		glb_switch_str[cpu] = switch_str