intel-pt-log.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_enable(void)
  29. {
  30. intel_pt_enable_logging = true;
  31. }
  32. void intel_pt_log_disable(void)
  33. {
  34. if (f)
  35. fflush(f);
  36. intel_pt_enable_logging = false;
  37. }
  38. void intel_pt_log_set_name(const char *name)
  39. {
  40. strncpy(log_name, name, MAX_LOG_NAME - 5);
  41. strcat(log_name, ".log");
  42. }
  43. static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
  44. int indent)
  45. {
  46. int i;
  47. for (i = 0; i < indent; i++)
  48. fprintf(f, " ");
  49. fprintf(f, " %08" PRIx64 ": ", pos);
  50. for (i = 0; i < len; i++)
  51. fprintf(f, " %02x", buf[i]);
  52. for (; i < 16; i++)
  53. fprintf(f, " ");
  54. fprintf(f, " ");
  55. }
  56. static void intel_pt_print_no_data(uint64_t pos, int indent)
  57. {
  58. int i;
  59. for (i = 0; i < indent; i++)
  60. fprintf(f, " ");
  61. fprintf(f, " %08" PRIx64 ": ", pos);
  62. for (i = 0; i < 16; i++)
  63. fprintf(f, " ");
  64. fprintf(f, " ");
  65. }
  66. static int intel_pt_log_open(void)
  67. {
  68. if (!intel_pt_enable_logging)
  69. return -1;
  70. if (f)
  71. return 0;
  72. if (!log_name[0])
  73. return -1;
  74. f = fopen(log_name, "w+");
  75. if (!f) {
  76. intel_pt_enable_logging = false;
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
  82. uint64_t pos, const unsigned char *buf)
  83. {
  84. char desc[INTEL_PT_PKT_DESC_MAX];
  85. if (intel_pt_log_open())
  86. return;
  87. intel_pt_print_data(buf, pkt_len, pos, 0);
  88. intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
  89. fprintf(f, "%s\n", desc);
  90. }
  91. void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip)
  92. {
  93. char desc[INTEL_PT_INSN_DESC_MAX];
  94. size_t len = intel_pt_insn->length;
  95. if (intel_pt_log_open())
  96. return;
  97. if (len > INTEL_PT_INSN_DBG_BUF_SZ)
  98. len = INTEL_PT_INSN_DBG_BUF_SZ;
  99. intel_pt_print_data(intel_pt_insn->buf, len, ip, 8);
  100. if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
  101. fprintf(f, "%s\n", desc);
  102. else
  103. fprintf(f, "Bad instruction!\n");
  104. }
  105. void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
  106. uint64_t ip)
  107. {
  108. char desc[INTEL_PT_INSN_DESC_MAX];
  109. if (intel_pt_log_open())
  110. return;
  111. intel_pt_print_no_data(ip, 8);
  112. if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
  113. fprintf(f, "%s\n", desc);
  114. else
  115. fprintf(f, "Bad instruction!\n");
  116. }
  117. void __intel_pt_log(const char *fmt, ...)
  118. {
  119. va_list args;
  120. if (intel_pt_log_open())
  121. return;
  122. va_start(args, fmt);
  123. vfprintf(f, fmt, args);
  124. va_end(args);
  125. }