debug.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* For general debugging purposes */
  2. #include "../perf.h"
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include "cache.h"
  7. #include "color.h"
  8. #include "event.h"
  9. #include "debug.h"
  10. #include "util.h"
  11. #include "target.h"
  12. int verbose;
  13. bool dump_trace = false, quiet = false;
  14. static int _eprintf(int level, const char *fmt, va_list args)
  15. {
  16. int ret = 0;
  17. if (verbose >= level) {
  18. if (use_browser >= 1)
  19. ui_helpline__vshow(fmt, args);
  20. else
  21. ret = vfprintf(stderr, fmt, args);
  22. }
  23. return ret;
  24. }
  25. int eprintf(int level, const char *fmt, ...)
  26. {
  27. va_list args;
  28. int ret;
  29. va_start(args, fmt);
  30. ret = _eprintf(level, fmt, args);
  31. va_end(args);
  32. return ret;
  33. }
  34. /*
  35. * Overloading libtraceevent standard info print
  36. * function, display with -v in perf.
  37. */
  38. void pr_stat(const char *fmt, ...)
  39. {
  40. va_list args;
  41. va_start(args, fmt);
  42. _eprintf(1, fmt, args);
  43. va_end(args);
  44. eprintf(1, "\n");
  45. }
  46. int dump_printf(const char *fmt, ...)
  47. {
  48. va_list args;
  49. int ret = 0;
  50. if (dump_trace) {
  51. va_start(args, fmt);
  52. ret = vprintf(fmt, args);
  53. va_end(args);
  54. }
  55. return ret;
  56. }
  57. void trace_event(union perf_event *event)
  58. {
  59. unsigned char *raw_event = (void *)event;
  60. const char *color = PERF_COLOR_BLUE;
  61. int i, j;
  62. if (!dump_trace)
  63. return;
  64. printf(".");
  65. color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
  66. event->header.size);
  67. for (i = 0; i < event->header.size; i++) {
  68. if ((i & 15) == 0) {
  69. printf(".");
  70. color_fprintf(stdout, color, " %04x: ", i);
  71. }
  72. color_fprintf(stdout, color, " %02x", raw_event[i]);
  73. if (((i & 15) == 15) || i == event->header.size-1) {
  74. color_fprintf(stdout, color, " ");
  75. for (j = 0; j < 15-(i & 15); j++)
  76. color_fprintf(stdout, color, " ");
  77. for (j = i & ~15; j <= i; j++) {
  78. color_fprintf(stdout, color, "%c",
  79. isprint(raw_event[j]) ?
  80. raw_event[j] : '.');
  81. }
  82. color_fprintf(stdout, color, "\n");
  83. }
  84. }
  85. printf(".\n");
  86. }