debug.c 1.6 KB

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