Add an instruction trace and a source trace to the intel-pt-events.py script.
Showing the changes to tools/perf/scripts/python (1 of 2 files). Show all
Unified · Split
| @@ -16,21 +16,30 @@ from __future__ import print_function · 15 unmodified lines | ||
| 16 | 16 | import os |
| 17 | 17 | import sys |
| 18 | 18 | import struct |
| 19 | +import argparse | |
| 20 | + | |
| 21 | +from libxed import LibXED | |
| 22 | +from ctypes import create_string_buffer, addressof | |
| 19 | 23 | |
| 20 | 24 | sys.path.append(os.environ['PERF_EXEC_PATH'] + \ |
| 21 | 25 | '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') |
| 22 | 26 | |
| 23 | −# These perf imports are not used at present | |
| 24 | −#from perf_trace_context import * | |
| 25 | −#from Core import * | |
| 27 | +from perf_trace_context import perf_set_itrace_options, \ | |
| 28 | + perf_sample_insn, perf_sample_srccode | |
| 26 | 29 | |
| 27 | 30 | try: |
| 28 | 31 | broken_pipe_exception = BrokenPipeError |
| 29 | 32 | except: |
| 30 | 33 | broken_pipe_exception = IOError |
| 31 | 34 | |
| 32 | −glb_switch_str = None | |
| 33 | −glb_switch_printed = True | |
| 35 | +glb_switch_str = None | |
| 36 | +glb_switch_printed = True | |
| 37 | +glb_insn = False | |
| 38 | +glb_disassembler = None | |
| 39 | +glb_src = False | |
| 40 | +glb_source_file_name = None | |
| 41 | +glb_line_number = None | |
| 42 | +glb_dso = None | |
| 34 | 43 | |
| 35 | 44 | def get_optional_null(perf_dict, field): |
| 36 | 45 | if field in perf_dict: |
| @@ -42,6 +51,11 @@ def get_optional_zero(perf_dict, field): · 5 unmodified lines | ||
| 42 | 51 | return perf_dict[field] |
| 43 | 52 | return 0 |
| 44 | 53 | |
| 54 | +def get_optional_bytes(perf_dict, field): | |
| 55 | + if field in perf_dict: | |
| 56 | + return perf_dict[field] | |
| 57 | + return bytes() | |
| 58 | + | |
| 45 | 59 | def get_optional(perf_dict, field): |
| 46 | 60 | if field in perf_dict: |
| 47 | 61 | return perf_dict[field] |
| @@ -53,7 +67,31 @@ def get_offset(perf_dict, field): · 5 unmodified lines | ||
| 53 | 67 | return "" |
| 54 | 68 | |
| 55 | 69 | def trace_begin(): |
| 56 | − print("Intel PT Branch Trace, Power Events and PTWRITE") | |
| 70 | + ap = argparse.ArgumentParser(usage = "", add_help = False) | |
| 71 | + ap.add_argument("--insn-trace", action='store_true') | |
| 72 | + ap.add_argument("--src-trace", 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 = "i0nsepwx" | |
| 80 | + glb_insn = True | |
| 81 | + elif glb_args.src_trace: | |
| 82 | + print("Intel PT Source Trace") | |
| 83 | + itrace = "i0nsepwx" | |
| 84 | + glb_insn = True | |
| 85 | + glb_src = True | |
| 86 | + else: | |
| 87 | + print("Intel PT Branch Trace, Power Events and PTWRITE") | |
| 88 | + itrace = "bepwx" | |
| 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) | |
| 57 | 95 | |
| 58 | 96 | def trace_end(): |
| 59 | 97 | print("End") |
| @@ -111,11 +149,14 @@ def print_psb(raw_buf): · 51 unmodified lines | ||
| 111 | 149 | offset = data[1] |
| 112 | 150 | print("offset: %#x" % (offset), end=' ') |
| 113 | 151 | |
| 114 | −def print_common_start(comm, sample, name): | |
| 152 | +def common_start_str(comm, sample): | |
| 115 | 153 | ts = sample["time"] |
| 116 | 154 | cpu = sample["cpu"] |
| 117 | 155 | pid = sample["pid"] |
| 118 | 156 | tid = sample["tid"] |
| 157 | + return "%16s %5u/%-5u [%03u] %9u.%09u " % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000) | |
| 158 | + | |
| 159 | +def print_common_start(comm, sample, name): | |
| 119 | 160 | flags_disp = get_optional_null(sample, "flags_disp") |
| 120 | 161 | # Unused fields: |
| 121 | 162 | # period = sample["period"] |
| @@ -123,22 +164,96 @@ def print_common_start(comm, sample, name): · 1 unmodified line | ||
| 123 | 164 | # weight = sample["weight"] |
| 124 | 165 | # transaction = sample["transaction"] |
| 125 | 166 | # cpumode = get_optional_zero(sample, "cpumode") |
| 126 | − print("%16s %5u/%-5u [%03u] %9u.%09u %7s %19s" % | |
| 127 | − (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name, flags_disp), | |
| 128 | − end=' ') | |
| 167 | + print(common_start_str(comm, sample) + "%7s %19s" % (name, flags_disp), end=' ') | |
| 168 | + | |
| 169 | +def print_instructions_start(comm, sample): | |
| 170 | + if "x" in get_optional_null(sample, "flags"): | |
| 171 | + print(common_start_str(comm, sample) + "x", end=' ') | |
| 172 | + else: | |
| 173 | + print(common_start_str(comm, sample), end=' ') | |
| 174 | + | |
| 175 | +def disassem(insn, ip): | |
| 176 | + inst = glb_disassembler.Instruction() | |
| 177 | + glb_disassembler.SetMode(inst, 0) # Assume 64-bit | |
| 178 | + buf = create_string_buffer(64) | |
| 179 | + buf.value = insn | |
| 180 | + return glb_disassembler.DisassembleOne(inst, addressof(buf), len(insn), ip) | |
| 129 | 181 | |
| 130 | 182 | def print_common_ip(param_dict, sample, symbol, dso): |
| 131 | 183 | ip = sample["ip"] |
| 132 | 184 | offs = get_offset(param_dict, "symoff") |
| 133 | − print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ') | |
| 185 | + if "cyc_cnt" in sample: | |
| 186 | + cyc_cnt = sample["cyc_cnt"] | |
| 187 | + insn_cnt = get_optional_zero(sample, "insn_cnt") | |
| 188 | + ipc_str = " IPC: %#.2f (%u/%u)" % (insn_cnt / cyc_cnt, insn_cnt, cyc_cnt) | |
| 189 | + else: | |
| 190 | + ipc_str = "" | |
| 191 | + if glb_insn and glb_disassembler is not None: | |
| 192 | + insn = perf_sample_insn(perf_script_context) | |
| 193 | + if insn and len(insn): | |
| 194 | + cnt, text = disassem(insn, ip) | |
| 195 | + byte_str = ("%x" % ip).rjust(16) | |
| 196 | + if sys.version_info.major >= 3: | |
| 197 | + for k in range(cnt): | |
| 198 | + byte_str += " %02x" % insn[k] | |
| 199 | + else: | |
| 200 | + for k in xrange(cnt): | |
| 201 | + byte_str += " %02x" % ord(insn[k]) | |
| 202 | + print("%-40s %-30s" % (byte_str, text), end=' ') | |
| 203 | + print("%s%s (%s)" % (symbol, offs, dso), end=' ') | |
| 204 | + else: | |
| 205 | + print("%16x %s%s (%s)" % (ip, symbol, offs, dso), end=' ') | |
| 134 | 206 | if "addr_correlates_sym" in sample: |
| 135 | 207 | addr = sample["addr"] |
| 136 | 208 | dso = get_optional(sample, "addr_dso") |
| 137 | 209 | symbol = get_optional(sample, "addr_symbol") |
| 138 | 210 | offs = get_offset(sample, "addr_symoff") |
| 139 | − print("=> %x %s%s (%s)" % (addr, symbol, offs, dso)) | |
| 211 | + print("=> %x %s%s (%s)%s" % (addr, symbol, offs, dso, ipc_str)) | |
| 212 | + else: | |
| 213 | + print(ipc_str) | |
| 214 | + | |
| 215 | +def print_srccode(comm, param_dict, sample, symbol, dso, with_insn): | |
| 216 | + ip = sample["ip"] | |
| 217 | + if symbol == "[unknown]": | |
| 218 | + start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40) | |
| 140 | 219 | else: |
| 141 | − print() | |
| 220 | + offs = get_offset(param_dict, "symoff") | |
| 221 | + start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40) | |
| 222 | + | |
| 223 | + if with_insn and glb_insn and glb_disassembler is not None: | |
| 224 | + insn = perf_sample_insn(perf_script_context) | |
| 225 | + if insn and len(insn): | |
| 226 | + cnt, text = disassem(insn, ip) | |
| 227 | + start_str += text.ljust(30) | |
| 228 | + | |
| 229 | + global glb_source_file_name | |
| 230 | + global glb_line_number | |
| 231 | + global glb_dso | |
| 232 | + | |
| 233 | + source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context) | |
| 234 | + if source_file_name: | |
| 235 | + if glb_line_number == line_number and glb_source_file_name == source_file_name: | |
| 236 | + src_str = "" | |
| 237 | + else: | |
| 238 | + if len(source_file_name) > 40: | |
| 239 | + src_file = ("..." + source_file_name[-37:]) + " " | |
| 240 | + else: | |
| 241 | + src_file = source_file_name.ljust(41) | |
| 242 | + if source_line is None: | |
| 243 | + src_str = src_file + str(line_number).rjust(4) + " <source not found>" | |
| 244 | + else: | |
| 245 | + src_str = src_file + str(line_number).rjust(4) + " " + source_line | |
| 246 | + glb_dso = None | |
| 247 | + elif dso == glb_dso: | |
| 248 | + src_str = "" | |
| 249 | + else: | |
| 250 | + src_str = dso | |
| 251 | + glb_dso = dso | |
| 252 | + | |
| 253 | + glb_line_number = line_number | |
| 254 | + glb_source_file_name = source_file_name | |
| 255 | + | |
| 256 | + print(start_str, src_str) | |
| 142 | 257 | |
| 143 | 258 | def do_process_event(param_dict): |
| 144 | 259 | global glb_switch_printed |
| @@ -159,24 +274,49 @@ def do_process_event(param_dict): · 14 unmodified lines | ||
| 159 | 274 | dso = get_optional(param_dict, "dso") |
| 160 | 275 | symbol = get_optional(param_dict, "symbol") |
| 161 | 276 | |
| 162 | − print_common_start(comm, sample, name) | |
| 163 | − | |
| 164 | − if name == "ptwrite": | |
| 277 | + if name[0:12] == "instructions": | |
| 278 | + if glb_src: | |
| 279 | + print_srccode(comm, param_dict, sample, symbol, dso, True) | |
| 280 | + else: | |
| 281 | + print_instructions_start(comm, sample) | |
| 282 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 283 | + elif name[0:8] == "branches": | |
| 284 | + if glb_src: | |
| 285 | + print_srccode(comm, param_dict, sample, symbol, dso, False) | |
| 286 | + else: | |
| 287 | + print_common_start(comm, sample, name) | |
| 288 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 289 | + elif name == "ptwrite": | |
| 290 | + print_common_start(comm, sample, name) | |
| 165 | 291 | print_ptwrite(raw_buf) |
| 292 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 166 | 293 | elif name == "cbr": |
| 294 | + print_common_start(comm, sample, name) | |
| 167 | 295 | print_cbr(raw_buf) |
| 296 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 168 | 297 | elif name == "mwait": |
| 298 | + print_common_start(comm, sample, name) | |
| 169 | 299 | print_mwait(raw_buf) |
| 300 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 170 | 301 | elif name == "pwre": |
| 302 | + print_common_start(comm, sample, name) | |
| 171 | 303 | print_pwre(raw_buf) |
| 304 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 172 | 305 | elif name == "exstop": |
| 306 | + print_common_start(comm, sample, name) | |
| 173 | 307 | print_exstop(raw_buf) |
| 308 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 174 | 309 | elif name == "pwrx": |
| 310 | + print_common_start(comm, sample, name) | |
| 175 | 311 | print_pwrx(raw_buf) |
| 312 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 176 | 313 | elif name == "psb": |
| 314 | + print_common_start(comm, sample, name) | |
| 177 | 315 | print_psb(raw_buf) |
| 178 | − | |
| 179 | − print_common_ip(param_dict, sample, symbol, dso) | |
| 316 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 317 | + else: | |
| 318 | + print_common_start(comm, sample, name) | |
| 319 | + print_common_ip(param_dict, sample, symbol, dso) | |
| 180 | 320 | |
| 181 | 321 | def process_event(param_dict): |
| 182 | 322 | try: |