debug.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* For general debugging purposes */
  2. #include "../perf.h"
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include "color.h"
  7. #include "event.h"
  8. #include "debug.h"
  9. #include "util.h"
  10. int verbose = 0;
  11. int dump_trace = 0;
  12. int eprintf(int level, const char *fmt, ...)
  13. {
  14. va_list args;
  15. int ret = 0;
  16. if (verbose >= level) {
  17. va_start(args, fmt);
  18. ret = vfprintf(stderr, fmt, args);
  19. va_end(args);
  20. }
  21. return ret;
  22. }
  23. int dump_printf(const char *fmt, ...)
  24. {
  25. va_list args;
  26. int ret = 0;
  27. if (dump_trace) {
  28. va_start(args, fmt);
  29. ret = vprintf(fmt, args);
  30. va_end(args);
  31. }
  32. return ret;
  33. }
  34. static int dump_printf_color(const char *fmt, const char *color, ...)
  35. {
  36. va_list args;
  37. int ret = 0;
  38. if (dump_trace) {
  39. va_start(args, color);
  40. ret = color_vfprintf(stdout, color, fmt, args);
  41. va_end(args);
  42. }
  43. return ret;
  44. }
  45. void trace_event(event_t *event)
  46. {
  47. unsigned char *raw_event = (void *)event;
  48. const char *color = PERF_COLOR_BLUE;
  49. int i, j;
  50. if (!dump_trace)
  51. return;
  52. dump_printf(".");
  53. dump_printf_color("\n. ... raw event: size %d bytes\n", color,
  54. event->header.size);
  55. for (i = 0; i < event->header.size; i++) {
  56. if ((i & 15) == 0) {
  57. dump_printf(".");
  58. dump_printf_color(" %04x: ", color, i);
  59. }
  60. dump_printf_color(" %02x", color, raw_event[i]);
  61. if (((i & 15) == 15) || i == event->header.size-1) {
  62. dump_printf_color(" ", color);
  63. for (j = 0; j < 15-(i & 15); j++)
  64. dump_printf_color(" ", color);
  65. for (j = 0; j < (i & 15); j++) {
  66. if (isprint(raw_event[i-15+j]))
  67. dump_printf_color("%c", color,
  68. raw_event[i-15+j]);
  69. else
  70. dump_printf_color(".", color);
  71. }
  72. dump_printf_color("\n", color);
  73. }
  74. }
  75. dump_printf(".\n");
  76. }