1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt_log.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
14 #include "intel-pt-log.h"
15 #include "intel-pt-insn-decoder.h"
17 #include "intel-pt-pkt-decoder.h"
19 #define MAX_LOG_NAME 256
22 static char log_name[MAX_LOG_NAME];
23 bool intel_pt_enable_logging;
25 void *intel_pt_log_fp(void)
30 void intel_pt_log_enable(void)
32 intel_pt_enable_logging = true;
35 void intel_pt_log_disable(void)
39 intel_pt_enable_logging = false;
42 void intel_pt_log_set_name(const char *name)
44 strncpy(log_name, name, MAX_LOG_NAME - 5);
45 strcat(log_name, ".log");
48 static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
53 for (i = 0; i < indent; i++)
56 fprintf(f, " %08" PRIx64 ": ", pos);
57 for (i = 0; i < len; i++)
58 fprintf(f, " %02x", buf[i]);
64 static void intel_pt_print_no_data(uint64_t pos, int indent)
68 for (i = 0; i < indent; i++)
71 fprintf(f, " %08" PRIx64 ": ", pos);
72 for (i = 0; i < 16; i++)
77 static int intel_pt_log_open(void)
79 if (!intel_pt_enable_logging)
88 f = fopen(log_name, "w+");
90 intel_pt_enable_logging = false;
97 void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
98 uint64_t pos, const unsigned char *buf)
100 char desc[INTEL_PT_PKT_DESC_MAX];
102 if (intel_pt_log_open())
105 intel_pt_print_data(buf, pkt_len, pos, 0);
106 intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
107 fprintf(f, "%s\n", desc);
110 void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip)
112 char desc[INTEL_PT_INSN_DESC_MAX];
113 size_t len = intel_pt_insn->length;
115 if (intel_pt_log_open())
118 if (len > INTEL_PT_INSN_BUF_SZ)
119 len = INTEL_PT_INSN_BUF_SZ;
120 intel_pt_print_data(intel_pt_insn->buf, len, ip, 8);
121 if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
122 fprintf(f, "%s\n", desc);
124 fprintf(f, "Bad instruction!\n");
127 void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
130 char desc[INTEL_PT_INSN_DESC_MAX];
132 if (intel_pt_log_open())
135 intel_pt_print_no_data(ip, 8);
136 if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
137 fprintf(f, "%s\n", desc);
139 fprintf(f, "Bad instruction!\n");
142 void __intel_pt_log(const char *fmt, ...)
146 if (intel_pt_log_open())
150 vfprintf(f, fmt, args);