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)
107 print("IP: %u payload: %#x" % (exact_ip, payload), end=' ')
109 def print_cbr(raw_buf):
110 data = struct.unpack_from("<BBBBII", raw_buf)
112 f = (data[4] + 500) / 1000
113 p = ((cbr * 1000 / data[2]) + 5) / 10
114 print("%3u freq: %4u MHz (%3u%%)" % (cbr, f, p), end=' ')
116 def print_mwait(raw_buf):
117 data = struct.unpack_from("<IQ", raw_buf)
119 hints = payload & 0xff
120 extensions = (payload >> 32) & 0x3
121 print("hints: %#x extensions: %#x" % (hints, extensions), end=' ')
123 def print_pwre(raw_buf):
124 data = struct.unpack_from("<IQ", raw_buf)
126 hw = (payload >> 7) & 1
127 cstate = (payload >> 12) & 0xf
128 subcstate = (payload >> 8) & 0xf
129 print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
132 def print_exstop(raw_buf):
133 data = struct.unpack_from("<I", raw_buf)
136 print("IP: %u" % (exact_ip), end=' ')
138 def print_pwrx(raw_buf):
139 data = struct.unpack_from("<IQ", raw_buf)
141 deepest_cstate = payload & 0xf
142 last_cstate = (payload >> 4) & 0xf
143 wake_reason = (payload >> 8) & 0xf
144 print("deepest cstate: %u last cstate: %u wake reason: %#x" %
145 (deepest_cstate, last_cstate, wake_reason), end=' ')
147 def print_psb(raw_buf):
148 data = struct.unpack_from("<IQ", raw_buf)
150 print("offset: %#x" % (offset), end=' ')
152 glb_cfe = ["", "INTR", "IRET", "SMI", "RSM", "SIPI", "INIT", "VMENTRY", "VMEXIT",
153 "VMEXIT_INTR", "SHUTDOWN", "", "UINT", "UIRET"] + [""] * 18
154 glb_evd = ["", "PFA", "VMXQ", "VMXR"] + [""] * 60
156 def print_evt(raw_buf):
157 data = struct.unpack_from("<BBH", raw_buf)
159 ip_flag = (data[0] & 0x80) >> 7
164 print(" cfe: %s IP: %u vector: %u" % (s, ip_flag, vector), end=' ')
166 print(" cfe: %u IP: %u vector: %u" % (typ, ip_flag, vector), end=' ')
168 for i in range(evd_cnt):
169 data = struct.unpack_from("<QQ", raw_buf)
173 print("%s: %#x" % (s, data[1]), end=' ')
175 print("EVD_%u: %#x" % (et, data[1]), end=' ')
177 def print_iflag(raw_buf):
178 data = struct.unpack_from("<IQ", raw_buf)
180 old_iflag = iflag ^ 1
181 via_branch = data[0] & 2
187 print("IFLAG: %u->%u %s branch" % (old_iflag, iflag, s), end=' ')
189 def common_start_str(comm, sample):
194 return "%16s %5u/%-5u [%03u] %9u.%09u " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000)
196 def print_common_start(comm, sample, name):
197 flags_disp = get_optional_null(sample, "flags_disp")
199 # period = sample["period"]
200 # phys_addr = sample["phys_addr"]
201 # weight = sample["weight"]
202 # transaction = sample["transaction"]
203 # cpumode = get_optional_zero(sample, "cpumode")
204 print(common_start_str(comm, sample) + "%8s %21s" % (name, flags_disp), end=' ')
206 def print_instructions_start(comm, sample):
207 if "x" in get_optional_null(sample, "flags"):
208 print(common_start_str(comm, sample) + "x", end=' ')
210 print(common_start_str(comm, sample), end=' ')
212 def disassem(insn, ip):
213 inst = glb_disassembler.Instruction()
214 glb_disassembler.SetMode(inst, 0) # Assume 64-bit
215 buf = create_string_buffer(64)
217 return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip)
219 def print_common_ip(param_dict, sample, symbol, dso):
221 offs = get_offset(param_dict, "symoff")
222 if "cyc_cnt" in sample:
223 cyc_cnt = sample["cyc_cnt"]
224 insn_cnt = get_optional_zero(sample, "insn_cnt")
225 ipc_str = " IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt)
228 if glb_insn and glb_disassembler is not None:
229 insn = perf_sample_insn(perf_script_context)
230 if insn and len(insn):
231 cnt, text = disassem(insn, ip)
232 byte_str = ("%x" % ip).rjust(16)
233 if sys.version_info.major >= 3:
235 byte_str += " %02x" % insn[k]
237 for k in xrange(cnt):
238 byte_str += " %02x" % ord(insn[k])
239 print("%-40s %-30s" % (byte_str, text), end=' ')
240 print("%s%s (%s)" % (symbol, offs, dso), end=' ')
242 print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ')
243 if "addr_correlates_sym" in sample:
244 addr = sample["addr"]
245 dso = get_optional(sample, "addr_dso")
246 symbol = get_optional(sample, "addr_symbol")
247 offs = get_offset(sample, "addr_symoff")
248 print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str))
252 def print_srccode(comm, param_dict, sample, symbol, dso, with_insn):
254 if symbol == "[unknown]":
255 start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
257 offs = get_offset(param_dict, "symoff")
258 start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
260 if with_insn and glb_insn and glb_disassembler is not None:
261 insn = perf_sample_insn(perf_script_context)
262 if insn and len(insn):
263 cnt, text = disassem(insn, ip)
264 start_str += text.ljust(30)
266 global glb_source_file_name
267 global glb_line_number
270 source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
272 if glb_line_number == line_number and glb_source_file_name == source_file_name:
275 if len(source_file_name) > 40:
276 src_file = ("..." + source_file_name[-37:]) + " "
278 src_file = source_file_name.ljust(41)
279 if source_line is None:
280 src_str = src_file + str(line_number).rjust(4) + " <source not found>"
282 src_str = src_file + str(line_number).rjust(4) + " " + source_line
290 glb_line_number = line_number
291 glb_source_file_name = source_file_name
293 print(start_str, src_str)
295 def do_process_event(param_dict):
296 event_attr = param_dict["attr"]
297 sample = param_dict["sample"]
298 raw_buf = param_dict["raw_buf"]
299 comm = param_dict["comm"]
300 name = param_dict["ev_name"]
302 # callchain = param_dict["callchain"]
303 # brstack = param_dict["brstack"]
304 # brstacksym = param_dict["brstacksym"]
306 # Symbol and dso info are not always resolved
307 dso = get_optional(param_dict, "dso")
308 symbol = get_optional(param_dict, "symbol")
311 if cpu in glb_switch_str:
312 print(glb_switch_str[cpu])
313 del glb_switch_str[cpu]
315 if name[0:12] == "instructions":
317 print_srccode(comm, param_dict, sample, symbol, dso, True)
319 print_instructions_start(comm, sample)
320 print_common_ip(param_dict, sample, symbol, dso)
321 elif name[0:8] == "branches":
323 print_srccode(comm, param_dict, sample, symbol, dso, False)
325 print_common_start(comm, sample, name)
326 print_common_ip(param_dict, sample, symbol, dso)
327 elif name == "ptwrite":
328 print_common_start(comm, sample, name)
329 print_ptwrite(raw_buf)
330 print_common_ip(param_dict, sample, symbol, dso)
332 print_common_start(comm, sample, name)
334 print_common_ip(param_dict, sample, symbol, dso)
335 elif name == "mwait":
336 print_common_start(comm, sample, name)
338 print_common_ip(param_dict, sample, symbol, dso)
340 print_common_start(comm, sample, name)
342 print_common_ip(param_dict, sample, symbol, dso)
343 elif name == "exstop":
344 print_common_start(comm, sample, name)
345 print_exstop(raw_buf)
346 print_common_ip(param_dict, sample, symbol, dso)
348 print_common_start(comm, sample, name)
350 print_common_ip(param_dict, sample, symbol, dso)
352 print_common_start(comm, sample, name)
354 print_common_ip(param_dict, sample, symbol, dso)
356 print_common_start(comm, sample, name)
358 print_common_ip(param_dict, sample, symbol, dso)
359 elif name == "iflag":
360 print_common_start(comm, sample, name)
362 print_common_ip(param_dict, sample, symbol, dso)
364 print_common_start(comm, sample, name)
365 print_common_ip(param_dict, sample, symbol, dso)
367 def process_event(param_dict):
369 do_process_event(param_dict)
370 except broken_pipe_exception:
371 # Stop python printing broken pipe errors and traceback
372 sys.stdout = open(os.devnull, 'w')
375 def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):
377 print("%16s %5u/%-5u [%03u] %9u.%09u error type %u code %u: %s ip 0x%16x" %
378 ("Trace error", pid, tid, cpu, ts / 1000000000, ts %1000000000, typ, code, msg, ip))
379 except broken_pipe_exception:
380 # Stop python printing broken pipe errors and traceback
381 sys.stdout = open(os.devnull, 'w')
384 def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):
386 out_str = "Switch out "
388 out_str = "Switch In "
390 preempt_str = "preempt"
393 if machine_pid == -1:
396 machine_str = "machine PID %d" % machine_pid
397 switch_str = "%16s %5d/%-5d [%03u] %9u.%09u %5d/%-5d %s %s" % \
398 (out_str, pid, tid, cpu, ts / 1000000000, ts %1000000000, np_pid, np_tid, machine_str, preempt_str)
399 if glb_args.all_switch_events:
402 global glb_switch_str
403 glb_switch_str[cpu] = switch_str