debug.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* For general debugging purposes */
  2. #include "../perf.h"
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <api/debug.h>
  7. #include <linux/time64.h>
  8. #include "cache.h"
  9. #include "color.h"
  10. #include "event.h"
  11. #include "debug.h"
  12. #include "util.h"
  13. #include "target.h"
  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. int veprintf(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 eprintf(int level, int var, const char *fmt, ...)
  31. {
  32. va_list args;
  33. int ret;
  34. va_start(args, fmt);
  35. ret = veprintf(level, var, fmt, args);
  36. va_end(args);
  37. return ret;
  38. }
  39. static int veprintf_time(u64 t, const char *fmt, va_list args)
  40. {
  41. int ret = 0;
  42. u64 secs, usecs, nsecs = t;
  43. secs = nsecs / NSEC_PER_SEC;
  44. nsecs -= secs * NSEC_PER_SEC;
  45. usecs = nsecs / NSEC_PER_USEC;
  46. ret = fprintf(stderr, "[%13" PRIu64 ".%06" PRIu64 "] ",
  47. secs, usecs);
  48. ret += vfprintf(stderr, fmt, args);
  49. return ret;
  50. }
  51. int eprintf_time(int level, int var, u64 t, const char *fmt, ...)
  52. {
  53. int ret = 0;
  54. va_list args;
  55. if (var >= level) {
  56. va_start(args, fmt);
  57. ret = veprintf_time(t, fmt, args);
  58. va_end(args);
  59. }
  60. return ret;
  61. }
  62. /*
  63. * Overloading libtraceevent standard info print
  64. * function, display with -v in perf.
  65. */
  66. void pr_stat(const char *fmt, ...)
  67. {
  68. va_list args;
  69. va_start(args, fmt);
  70. veprintf(1, verbose, fmt, args);
  71. va_end(args);
  72. eprintf(1, verbose, "\n");
  73. }
  74. int dump_printf(const char *fmt, ...)
  75. {
  76. va_list args;
  77. int ret = 0;
  78. if (dump_trace) {
  79. va_start(args, fmt);
  80. ret = vprintf(fmt, args);
  81. va_end(args);
  82. }
  83. return ret;
  84. }
  85. static void trace_event_printer(enum binary_printer_ops op,
  86. unsigned int val, void *extra)
  87. {
  88. const char *color = PERF_COLOR_BLUE;
  89. union perf_event *event = (union perf_event *)extra;
  90. unsigned char ch = (unsigned char)val;
  91. switch (op) {
  92. case BINARY_PRINT_DATA_BEGIN:
  93. printf(".");
  94. color_fprintf(stdout, color, "\n. ... raw event: size %d bytes\n",
  95. event->header.size);
  96. break;
  97. case BINARY_PRINT_LINE_BEGIN:
  98. printf(".");
  99. break;
  100. case BINARY_PRINT_ADDR:
  101. color_fprintf(stdout, color, " %04x: ", val);
  102. break;
  103. case BINARY_PRINT_NUM_DATA:
  104. color_fprintf(stdout, color, " %02x", val);
  105. break;
  106. case BINARY_PRINT_NUM_PAD:
  107. color_fprintf(stdout, color, " ");
  108. break;
  109. case BINARY_PRINT_SEP:
  110. color_fprintf(stdout, color, " ");
  111. break;
  112. case BINARY_PRINT_CHAR_DATA:
  113. color_fprintf(stdout, color, "%c",
  114. isprint(ch) ? ch : '.');
  115. break;
  116. case BINARY_PRINT_CHAR_PAD:
  117. color_fprintf(stdout, color, " ");
  118. break;
  119. case BINARY_PRINT_LINE_END:
  120. color_fprintf(stdout, color, "\n");
  121. break;
  122. case BINARY_PRINT_DATA_END:
  123. printf("\n");
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. void trace_event(union perf_event *event)
  130. {
  131. unsigned char *raw_event = (void *)event;
  132. if (!dump_trace)
  133. return;
  134. print_binary(raw_event, event->header.size, 16,
  135. trace_event_printer, event);
  136. }
  137. static struct debug_variable {
  138. const char *name;
  139. int *ptr;
  140. } debug_variables[] = {
  141. { .name = "verbose", .ptr = &verbose },
  142. { .name = "ordered-events", .ptr = &debug_ordered_events},
  143. { .name = "stderr", .ptr = &redirect_to_stderr},
  144. { .name = "data-convert", .ptr = &debug_data_convert },
  145. { .name = NULL, }
  146. };
  147. int perf_debug_option(const char *str)
  148. {
  149. struct debug_variable *var = &debug_variables[0];
  150. char *vstr, *s = strdup(str);
  151. int v = 1;
  152. vstr = strchr(s, '=');
  153. if (vstr)
  154. *vstr++ = 0;
  155. while (var->name) {
  156. if (!strcmp(s, var->name))
  157. break;
  158. var++;
  159. }
  160. if (!var->name) {
  161. pr_err("Unknown debug variable name '%s'\n", s);
  162. free(s);
  163. return -1;
  164. }
  165. if (vstr) {
  166. v = atoi(vstr);
  167. /*
  168. * Allow only values in range (0, 10),
  169. * otherwise set 0.
  170. */
  171. v = (v < 0) || (v > 10) ? 0 : v;
  172. }
  173. *var->ptr = v;
  174. free(s);
  175. return 0;
  176. }
  177. #define DEBUG_WRAPPER(__n, __l) \
  178. static int pr_ ## __n ## _wrapper(const char *fmt, ...) \
  179. { \
  180. va_list args; \
  181. int ret; \
  182. \
  183. va_start(args, fmt); \
  184. ret = veprintf(__l, verbose, fmt, args); \
  185. va_end(args); \
  186. return ret; \
  187. }
  188. DEBUG_WRAPPER(warning, 0);
  189. DEBUG_WRAPPER(debug, 1);
  190. void perf_debug_setup(void)
  191. {
  192. libapi_set_print(pr_warning_wrapper, pr_warning_wrapper, pr_debug_wrapper);
  193. }