intel-pt-log.c 3.2 KB

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