^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * intel_pt_log.c: Intel Processor Trace support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2013-2014, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "intel-pt-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "intel-pt-insn-decoder.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "intel-pt-pkt-decoder.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define MAX_LOG_NAME 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static char log_name[MAX_LOG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) bool intel_pt_enable_logging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void *intel_pt_log_fp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) void intel_pt_log_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) intel_pt_enable_logging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void intel_pt_log_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) fflush(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) intel_pt_enable_logging = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void intel_pt_log_set_name(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) strncpy(log_name, name, MAX_LOG_NAME - 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) strcat(log_name, ".log");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int indent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (i = 0; i < indent; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) fprintf(f, " %08" PRIx64 ": ", pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) fprintf(f, " %02x", buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for (; i < 16; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static void intel_pt_print_no_data(uint64_t pos, int indent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (i = 0; i < indent; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fprintf(f, " %08" PRIx64 ": ", pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) for (i = 0; i < 16; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fprintf(f, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int intel_pt_log_open(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!intel_pt_enable_logging)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!log_name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) f = fopen(log_name, "w+");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) intel_pt_enable_logging = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) uint64_t pos, const unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) char desc[INTEL_PT_PKT_DESC_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (intel_pt_log_open())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) intel_pt_print_data(buf, pkt_len, pos, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) fprintf(f, "%s\n", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) char desc[INTEL_PT_INSN_DESC_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) size_t len = intel_pt_insn->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (intel_pt_log_open())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (len > INTEL_PT_INSN_BUF_SZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) len = INTEL_PT_INSN_BUF_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) intel_pt_print_data(intel_pt_insn->buf, len, ip, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fprintf(f, "%s\n", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fprintf(f, "Bad instruction!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) uint64_t ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) char desc[INTEL_PT_INSN_DESC_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (intel_pt_log_open())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) intel_pt_print_no_data(ip, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fprintf(f, "%s\n", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fprintf(f, "Bad instruction!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void __intel_pt_log(const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (intel_pt_log_open())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) vfprintf(f, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }