Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * intel_pt_log.c: Intel Processor Trace support
0004  * Copyright (c) 2013-2014, Intel Corporation.
0005  */
0006 
0007 #include <stdio.h>
0008 #include <stdint.h>
0009 #include <inttypes.h>
0010 #include <stdarg.h>
0011 #include <stdbool.h>
0012 #include <string.h>
0013 
0014 #include "intel-pt-log.h"
0015 #include "intel-pt-insn-decoder.h"
0016 
0017 #include "intel-pt-pkt-decoder.h"
0018 
0019 #define MAX_LOG_NAME 256
0020 
0021 static FILE *f;
0022 static char log_name[MAX_LOG_NAME];
0023 bool intel_pt_enable_logging;
0024 
0025 void *intel_pt_log_fp(void)
0026 {
0027     return f;
0028 }
0029 
0030 void intel_pt_log_enable(void)
0031 {
0032     intel_pt_enable_logging = true;
0033 }
0034 
0035 void intel_pt_log_disable(void)
0036 {
0037     if (f)
0038         fflush(f);
0039     intel_pt_enable_logging = false;
0040 }
0041 
0042 void intel_pt_log_set_name(const char *name)
0043 {
0044     strncpy(log_name, name, MAX_LOG_NAME - 5);
0045     strcat(log_name, ".log");
0046 }
0047 
0048 static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
0049                 int indent)
0050 {
0051     int i;
0052 
0053     for (i = 0; i < indent; i++)
0054         fprintf(f, " ");
0055 
0056     fprintf(f, "  %08" PRIx64 ": ", pos);
0057     for (i = 0; i < len; i++)
0058         fprintf(f, " %02x", buf[i]);
0059     for (; i < 16; i++)
0060         fprintf(f, "   ");
0061     fprintf(f, " ");
0062 }
0063 
0064 static void intel_pt_print_no_data(uint64_t pos, int indent)
0065 {
0066     int i;
0067 
0068     for (i = 0; i < indent; i++)
0069         fprintf(f, " ");
0070 
0071     fprintf(f, "  %08" PRIx64 ": ", pos);
0072     for (i = 0; i < 16; i++)
0073         fprintf(f, "   ");
0074     fprintf(f, " ");
0075 }
0076 
0077 static int intel_pt_log_open(void)
0078 {
0079     if (!intel_pt_enable_logging)
0080         return -1;
0081 
0082     if (f)
0083         return 0;
0084 
0085     if (log_name[0])
0086         f = fopen(log_name, "w+");
0087     else
0088         f = stdout;
0089     if (!f) {
0090         intel_pt_enable_logging = false;
0091         return -1;
0092     }
0093 
0094     return 0;
0095 }
0096 
0097 void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
0098                uint64_t pos, const unsigned char *buf)
0099 {
0100     char desc[INTEL_PT_PKT_DESC_MAX];
0101 
0102     if (intel_pt_log_open())
0103         return;
0104 
0105     intel_pt_print_data(buf, pkt_len, pos, 0);
0106     intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
0107     fprintf(f, "%s\n", desc);
0108 }
0109 
0110 void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip)
0111 {
0112     char desc[INTEL_PT_INSN_DESC_MAX];
0113     size_t len = intel_pt_insn->length;
0114 
0115     if (intel_pt_log_open())
0116         return;
0117 
0118     if (len > INTEL_PT_INSN_BUF_SZ)
0119         len = INTEL_PT_INSN_BUF_SZ;
0120     intel_pt_print_data(intel_pt_insn->buf, len, ip, 8);
0121     if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
0122         fprintf(f, "%s\n", desc);
0123     else
0124         fprintf(f, "Bad instruction!\n");
0125 }
0126 
0127 void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
0128                  uint64_t ip)
0129 {
0130     char desc[INTEL_PT_INSN_DESC_MAX];
0131 
0132     if (intel_pt_log_open())
0133         return;
0134 
0135     intel_pt_print_no_data(ip, 8);
0136     if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
0137         fprintf(f, "%s\n", desc);
0138     else
0139         fprintf(f, "Bad instruction!\n");
0140 }
0141 
0142 void __intel_pt_log(const char *fmt, ...)
0143 {
0144     va_list args;
0145 
0146     if (intel_pt_log_open())
0147         return;
0148 
0149     va_start(args, fmt);
0150     vfprintf(f, fmt, args);
0151     va_end(args);
0152 }