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.
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.
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
14 from __future__ import print_function
21 from libxed import LibXED
22 from ctypes import create_string_buffer, addressof
24 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
25 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
27 from perf_trace_context import perf_set_itrace_options, \
28 perf_sample_insn, perf_sample_srccode
31 broken_pipe_exception = BrokenPipeError
33 broken_pipe_exception = IOError
37 glb_disassembler = None
39 glb_source_file_name = None
40 glb_line_number = None
43 def get_optional_null(perf_dict, field):
44 if field in perf_dict:
45 return perf_dict[field]
48 def get_optional_zero(perf_dict, field):
49 if field in perf_dict:
50 return perf_dict[field]
53 def get_optional_bytes(perf_dict, field):
54 if field in perf_dict:
55 return perf_dict[field]
58 def get_optional(perf_dict, field):
59 if field in perf_dict:
60 return perf_dict[field]
63 def get_offset(perf_dict, field):
64 if field in perf_dict:
65 return "+%#x" % perf_dict[field]
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')
76 glb_args = ap.parse_args()
77 if glb_args.insn_trace:
78 print("Intel PT Instruction Trace")
81 elif glb_args.src_trace:
82 print("Intel PT Source Trace")
87 print("Intel PT Branch Trace, Power Events, Event Trace and PTWRITE")
89 global glb_disassembler
91 glb_disassembler = LibXED()
93 glb_disassembler = None
94 perf_set_itrace_options(perf_script_context, itrace)
99 def 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())]))
102 def print_ptwrite(raw_buf):
103 data = struct.unpack_from("<IQ", raw_buf)
108 s = payload.to_bytes(8, "little").decode("ascii").rstrip("\x00")
109 if not s.isprintable():
113 print("IP: %u payload: %#x" % (exact_ip, payload), s, end=' ')
115 def print_cbr(raw_buf):
116 data = struct.unpack_from("<BBBBII", raw_buf)
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=' ')
122 def print_mwait(raw_buf):
123 data = struct.unpack_from("<IQ", raw_buf)
125 hints = payload & 0xff
126 extensions = (payload >> 32) & 0x3
127 print("hints: %#x extensions: %#x" % (hints, extensions), end=' ')
129 def print_pwre(raw_buf):
130 data = struct.unpack_from("<IQ", raw_buf)
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),
138 def print_exstop(raw_buf):
139 data = struct.unpack_from("<I", raw_buf)
142 print("IP: %u" % (exact_ip), end=' ')
144 def print_pwrx(raw_buf):
145 data = struct.unpack_from("<IQ", raw_buf)
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=' ')
153 def print_psb(raw_buf):
154 data = struct.unpack_from("<IQ", raw_buf)
156 print("offset: %#x" % (offset), end=' ')
158 glb_cfe = ["", "INTR", "IRET", "SMI", "RSM", "SIPI", "INIT", "VMENTRY", "VMEXIT",
159 "VMEXIT_INTR", "SHUTDOWN", "", "UINT", "UIRET"] + [""] * 18
160 glb_evd = ["", "PFA", "VMXQ", "VMXR"] + [""] * 60
162 def print_evt(raw_buf):
163 data = struct.unpack_from("<BBH", raw_buf)
165 ip_flag = (data[0] & 0x80) >> 7
170 print(" cfe: %s IP: %u vector: %u" % (s, ip_flag, vector), end=' ')
172 print(" cfe: %u IP: %u vector: %u" % (typ, ip_flag, vector), end=' ')
174 for i in range(evd_cnt):
175 data = struct.unpack_from("<QQ", raw_buf)
179 print("%s: %#x" % (s, data[1]), end=' ')
181 print("EVD_%u: %#x" % (et, data[1]), end=' ')
183 def print_iflag(raw_buf):
184 data = struct.unpack_from("<IQ", raw_buf)
186 old_iflag = iflag ^ 1
187 via_branch = data[0] & 2
193 print("IFLAG: %u->%u %s branch" % (old_iflag, iflag, s), end=' ')
195 def common_start_str(comm, sample):
200 if "machine_pid" in sample:
201 machine_pid = sample["machine_pid"]
202 vcpu = sample["vcpu"]
203 return "VM:%5d VCPU:%03d %16s %5u/%-5u [%03u] %9u.%09u " % (machine_pid, vcpu, comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
205 return "%16s %5u/%-5u [%03u] %9u.%09u " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
207 def print_common_start(comm, sample, name):
208 flags_disp = get_optional_null(sample, "flags_disp")
210 # period = sample["period"]
211 # phys_addr = sample["phys_addr"]
212 # weight = sample["weight"]
213 # transaction = sample["transaction"]
214 # cpumode = get_optional_zero(sample, "cpumode")
215 print(common_start_str(comm, sample) + "%8s %21s" % (name, flags_disp), end=' ')
217 def print_instructions_start(comm, sample):
218 if "x" in get_optional_null(sample, "flags"):
219 print(common_start_str(comm, sample) + "x", end=' ')
221 print(common_start_str(comm, sample), end=' ')
223 def disassem(insn, ip):
224 inst = glb_disassembler.Instruction()
225 glb_disassembler.SetMode(inst, 0) # Assume 64-bit
226 buf = create_string_buffer(64)
228 return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip)
230 def print_common_ip(param_dict, sample, symbol, dso):
232 offs = get_offset(param_dict, "symoff")
233 if "cyc_cnt" in sample:
234 cyc_cnt = sample["cyc_cnt"]
235 insn_cnt = get_optional_zero(sample, "insn_cnt")
236 ipc_str = " IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt)
239 if glb_insn and glb_disassembler is not None:
240 insn = perf_sample_insn(perf_script_context)
241 if insn and len(insn):
242 cnt, text = disassem(insn, ip)
243 byte_str = ("%x" % ip).rjust(16)
244 if sys.version_info.major >= 3:
246 byte_str += " %02x" % insn[k]
248 for k in xrange(cnt):
249 byte_str += " %02x" % ord(insn[k])
250 print("%-40s %-30s" % (byte_str, text), end=' ')
251 print("%s%s (%s)" % (symbol, offs, dso), end=' ')
253 print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
254 if "addr_correlates_sym" in sample:
255 addr = sample["addr"]
256 dso = get_optional(sample, "addr_dso")
257 symbol = get_optional(sample, "addr_symbol")
258 offs = get_offset(sample, "addr_symoff")
259 print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str))
263 def print_srccode(comm, param_dict, sample, symbol, dso, with_insn):
265 if symbol == "[unknown]":
266 start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
268 offs = get_offset(param_dict, "symoff")
269 start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
271 if with_insn and glb_insn and glb_disassembler is not None:
272 insn = perf_sample_insn(perf_script_context)
273 if insn and len(insn):
274 cnt, text = disassem(insn, ip)
275 start_str += text.ljust(30)
277 global glb_source_file_name
278 global glb_line_number
281 source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
283 if glb_line_number == line_number and glb_source_file_name == source_file_name:
286 if len(source_file_name) > 40:
287 src_file = ("..." + source_file_name[-37:]) + " "
289 src_file = source_file_name.ljust(41)
290 if source_line is None:
291 src_str = src_file + str(line_number).rjust(4) + " <source not found>"
293 src_str = src_file + str(line_number).rjust(4) + " " + source_line
301 glb_line_number = line_number
302 glb_source_file_name = source_file_name
304 print(start_str, src_str)
306 def do_process_event(param_dict):
307 event_attr = param_dict["attr"]
308 sample = param_dict["sample"]
309 raw_buf = param_dict["raw_buf"]
310 comm = param_dict["comm"]
311 name = param_dict["ev_name"]
313 # callchain = param_dict["callchain"]
314 # brstack = param_dict["brstack"]
315 # brstacksym = param_dict["brstacksym"]
317 # Symbol and dso info are not always resolved
318 dso = get_optional(param_dict, "dso")
319 symbol = get_optional(param_dict, "symbol")
322 if cpu in glb_switch_str:
323 print(glb_switch_str[cpu])
324 del glb_switch_str[cpu]
326 if name[0:12] == "instructions":
328 print_srccode(comm, param_dict, sample, symbol, dso, True)
330 print_instructions_start(comm, sample)
331 print_common_ip(param_dict, sample, symbol, dso)
332 elif name[0:8] == "branches":
334 print_srccode(comm, param_dict, sample, symbol, dso, False)
336 print_common_start(comm, sample, name)
337 print_common_ip(param_dict, sample, symbol, dso)
338 elif name == "ptwrite":
339 print_common_start(comm, sample, name)
340 print_ptwrite(raw_buf)
341 print_common_ip(param_dict, sample, symbol, dso)
343 print_common_start(comm, sample, name)
345 print_common_ip(param_dict, sample, symbol, dso)
346 elif name == "mwait":
347 print_common_start(comm, sample, name)
349 print_common_ip(param_dict, sample, symbol, dso)
351 print_common_start(comm, sample, name)
353 print_common_ip(param_dict, sample, symbol, dso)
354 elif name == "exstop":
355 print_common_start(comm, sample, name)
356 print_exstop(raw_buf)
357 print_common_ip(param_dict, sample, symbol, dso)
359 print_common_start(comm, sample, name)
361 print_common_ip(param_dict, sample, symbol, dso)
363 print_common_start(comm, sample, name)
365 print_common_ip(param_dict, sample, symbol, dso)
367 print_common_start(comm, sample, name)
369 print_common_ip(param_dict, sample, symbol, dso)
370 elif name == "iflag":
371 print_common_start(comm, sample, name)
373 print_common_ip(param_dict, sample, symbol, dso)
375 print_common_start(comm, sample, name)
376 print_common_ip(param_dict, sample, symbol, dso)
378 def process_event(param_dict):
380 do_process_event(param_dict)
381 except broken_pipe_exception:
382 # Stop python printing broken pipe errors and traceback
383 sys.stdout = open(os.devnull, 'w')
386 def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):
387 if len(x) >= 2 and x[0]:
395 print("VM:%5d VCPU:%03d %16s %5u/%-5u [%03u] %9u.%09u error type %u code %u: %s ip 0x%16x" %
396 (machine_pid, vcpu, "Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip))
398 print("%16s %5u/%-5u [%03u] %9u.%09u error type %u code %u: %s ip 0x%16x" %
399 ("Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip))
400 except broken_pipe_exception:
401 # Stop python printing broken pipe errors and traceback
402 sys.stdout = open(os.devnull, 'w')
405 def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):
407 out_str = "Switch out "
409 out_str = "Switch In "
411 preempt_str = "preempt"
414 if len(x) >= 2 and x[0]:
419 if machine_pid == -1:
422 machine_str = "machine PID %d" % machine_pid
424 machine_str = "machine PID %d VCPU %d" % (machine_pid, vcpu)
425 switch_str = "%16s %5d/%-5d [%03u] %9u.%09u %5d/%-5d %s %s" % \
426 (out_str, pid, tid, cpu, ts / 1000000000, ts %1000000000, np_pid, np_tid, machine_str, preempt_str)
427 if glb_args.all_switch_events:
430 global glb_switch_str
431 glb_switch_str[cpu] = switch_str