2 * intel_pt_log.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, 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
23 #include "intel-pt-log.h"
24 #include "intel-pt-insn-decoder.h"
26 #include "intel-pt-pkt-decoder.h"
28 #define MAX_LOG_NAME 256
31 static char log_name
[MAX_LOG_NAME
];
32 bool intel_pt_enable_logging
;
34 void intel_pt_log_enable(void)
36 intel_pt_enable_logging
= true;
39 void intel_pt_log_disable(void)
43 intel_pt_enable_logging
= false;
46 void intel_pt_log_set_name(const char *name
)
48 strncpy(log_name
, name
, MAX_LOG_NAME
- 5);
49 strcat(log_name
, ".log");
52 static void intel_pt_print_data(const unsigned char *buf
, int len
, uint64_t pos
,
57 for (i
= 0; i
< indent
; i
++)
60 fprintf(f
, " %08" PRIx64
": ", pos
);
61 for (i
= 0; i
< len
; i
++)
62 fprintf(f
, " %02x", buf
[i
]);
68 static void intel_pt_print_no_data(uint64_t pos
, int indent
)
72 for (i
= 0; i
< indent
; i
++)
75 fprintf(f
, " %08" PRIx64
": ", pos
);
76 for (i
= 0; i
< 16; i
++)
81 static int intel_pt_log_open(void)
83 if (!intel_pt_enable_logging
)
92 f
= fopen(log_name
, "w+");
94 intel_pt_enable_logging
= false;
101 void __intel_pt_log_packet(const struct intel_pt_pkt
*packet
, int pkt_len
,
102 uint64_t pos
, const unsigned char *buf
)
104 char desc
[INTEL_PT_PKT_DESC_MAX
];
106 if (intel_pt_log_open())
109 intel_pt_print_data(buf
, pkt_len
, pos
, 0);
110 intel_pt_pkt_desc(packet
, desc
, INTEL_PT_PKT_DESC_MAX
);
111 fprintf(f
, "%s\n", desc
);
114 void __intel_pt_log_insn(struct intel_pt_insn
*intel_pt_insn
, uint64_t ip
)
116 char desc
[INTEL_PT_INSN_DESC_MAX
];
117 size_t len
= intel_pt_insn
->length
;
119 if (intel_pt_log_open())
122 if (len
> INTEL_PT_INSN_BUF_SZ
)
123 len
= INTEL_PT_INSN_BUF_SZ
;
124 intel_pt_print_data(intel_pt_insn
->buf
, len
, ip
, 8);
125 if (intel_pt_insn_desc(intel_pt_insn
, desc
, INTEL_PT_INSN_DESC_MAX
) > 0)
126 fprintf(f
, "%s\n", desc
);
128 fprintf(f
, "Bad instruction!\n");
131 void __intel_pt_log_insn_no_data(struct intel_pt_insn
*intel_pt_insn
,
134 char desc
[INTEL_PT_INSN_DESC_MAX
];
136 if (intel_pt_log_open())
139 intel_pt_print_no_data(ip
, 8);
140 if (intel_pt_insn_desc(intel_pt_insn
, desc
, INTEL_PT_INSN_DESC_MAX
) > 0)
141 fprintf(f
, "%s\n", desc
);
143 fprintf(f
, "Bad instruction!\n");
146 void __intel_pt_log(const char *fmt
, ...)
150 if (intel_pt_log_open())
154 vfprintf(f
, fmt
, args
);