debug.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #define NSECS_PER_SEC 1000000000ULL
  13. #define NSECS_PER_USEC 1000ULL
  14. int verbose;
  15. bool dump_trace = false, quiet = false;
  16. int debug_ordered_events;
  17. static int redirect_to_stderr;
  18. int debug_data_convert;
  19. static int _eprintf(int level, int var, const char *fmt, va_list args)
  20. {
  21. int ret = 0;
  22. if (var >= level) {
  23. if (use_browser >= 1 && !redirect_to_stderr)
  24. ui_helpline__vshow(fmt, args);
  25. else
  26. ret = vfprintf(stderr, fmt, args);
  27. }
  28. return ret;
  29. }
  30. int veprintf(int level, int var, const char *fmt, va_list args)
  31. {
  32. return _eprintf(level, var, fmt, args);
  33. }
  34. int eprintf(int level, int var, const char *fmt, ...)
  35. {
  36. va_list args;
  37. int ret;
  38. va_start(args, fmt);
  39. ret = _eprintf(level, var, fmt, args);
  40. va_end(args);
  41. return ret;
  42. }
  43. static int __eprintf_time(u64 t, const char *fmt, va_list args)
  44. {
  45. int ret = 0;
  46. u64 secs, usecs, nsecs = t;
  47. secs = nsecs / NSECS_PER_SEC;
  48. nsecs -= secs * NSECS_PER_SEC;
  49. usecs = nsecs / NSECS_PER_USEC;
  50. ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
  51. secs, usecs);
  52. ret += vfprintf(stderr, fmt, args);
  53. return ret;
  54. }
  55. int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
  56. {
  57. int ret = 0;
  58. va_list args;
  59. if (var >= level) {
  60. va_start(args, fmt);
  61. ret = __eprintf_time(t, fmt, args);
  62. va_end(args);
  63. }
  64. return ret;
  65. }
  66. /*
  67. * Overloading libtraceevent standard info print
  68. * function, display with -v in perf.
  69. */
  70. void pr_stat(const char *fmt, ...)
  71. {
  72. va_list args;
  73. va_start(args, fmt);
  74. _eprintf(1, verbose, fmt, args);
  75. va_end(args);
  76. eprintf(1, verbose, "\n");
  77. }
  78. int dump_printf(const char *fmt, ...)
  79. {
  80. va_list args;
  81. int ret = 0;
  82. if (dump_trace) {
  83. va_start(args, fmt);
  84. ret = vprintf(fmt, args);
  85. va_end(args);
  86. }
  87. return ret;
  88. }
  89. void trace_event(union perf_event *event)
  90. {
  91. unsigned char *raw_event = (void *)event;
  92. const char *color = PERF_COLOR_BLUE;
  93. int i, j;
  94. if (!dump_trace)
  95. return;
  96. printf(".");
  97. color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
  98. event->header.size);
  99. for (i = 0; i < event->header.size; i++) {
  100. if ((i & 15) == 0) {
  101. printf(".");
  102. color_fprintf(stdout, color, " %04x: ", i);
  103. }
  104. color_fprintf(stdout, color, " %02x", raw_event[i]);
  105. if (((i & 15) == 15) || i == event->header.size-1) {
  106. color_fprintf(stdout, color, " ");
  107. for (j = 0; j < 15-(i & 15); j++)
  108. color_fprintf(stdout, color, " ");
  109. for (j = i & ~15; j <= i; j++) {
  110. color_fprintf(stdout, color, "%c",
  111. isprint(raw_event[j]) ?
  112. raw_event[j] : '.');
  113. }
  114. color_fprintf(stdout, color, "\n");
  115. }
  116. }
  117. printf(".\n");
  118. }
  119. static struct debug_variable {
  120. const char *name;
  121. int *ptr;
  122. } debug_variables[] = {
  123. { .name = "verbose", .ptr = &verbose },
  124. { .name = "ordered-events", .ptr = &debug_ordered_events},
  125. { .name = "stderr", .ptr = &redirect_to_stderr},
  126. { .name = "data-convert", .ptr = &debug_data_convert },
  127. { .name = NULL, }
  128. };
  129. int perf_debug_option(const char *str)
  130. {
  131. struct debug_variable *var = &debug_variables[0];
  132. char *vstr, *s = strdup(str);
  133. int v = 1;
  134. vstr = strchr(s, '=');
  135. if (vstr)
  136. *vstr++ = 0;
  137. while (var->name) {
  138. if (!strcmp(s, var->name))
  139. break;
  140. var++;
  141. }
  142. if (!var->name) {
  143. pr_err("Unknown debug variable name '%s'\n", s);
  144. free(s);
  145. return -1;
  146. }
  147. if (vstr) {
  148. v = atoi(vstr);
  149. /*
  150. * Allow only values in range (0, 10),
  151. * otherwise set 0.
  152. */
  153. v = (v < 0) || (v > 10) ? 0 : v;
  154. }
  155. *var->ptr = v;
  156. free(s);
  157. return 0;
  158. }