debug.c 1.9 KB

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