evsel_fprintf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <traceevent/event-parse.h>
  4. #include "evsel.h"
  5. #include "callchain.h"
  6. #include "map.h"
  7. #include "symbol.h"
  8. static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
  9. {
  10. va_list args;
  11. int ret = 0;
  12. if (!*first) {
  13. ret += fprintf(fp, ",");
  14. } else {
  15. ret += fprintf(fp, ":");
  16. *first = false;
  17. }
  18. va_start(args, fmt);
  19. ret += vfprintf(fp, fmt, args);
  20. va_end(args);
  21. return ret;
  22. }
  23. static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
  24. {
  25. return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
  26. }
  27. int perf_evsel__fprintf(struct perf_evsel *evsel,
  28. struct perf_attr_details *details, FILE *fp)
  29. {
  30. bool first = true;
  31. int printed = 0;
  32. if (details->event_group) {
  33. struct perf_evsel *pos;
  34. if (!perf_evsel__is_group_leader(evsel))
  35. return 0;
  36. if (evsel->nr_members > 1)
  37. printed += fprintf(fp, "%s{", evsel->group_name ?: "");
  38. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  39. for_each_group_member(pos, evsel)
  40. printed += fprintf(fp, ",%s", perf_evsel__name(pos));
  41. if (evsel->nr_members > 1)
  42. printed += fprintf(fp, "}");
  43. goto out;
  44. }
  45. printed += fprintf(fp, "%s", perf_evsel__name(evsel));
  46. if (details->verbose) {
  47. printed += perf_event_attr__fprintf(fp, &evsel->attr,
  48. __print_attr__fprintf, &first);
  49. } else if (details->freq) {
  50. const char *term = "sample_freq";
  51. if (!evsel->attr.freq)
  52. term = "sample_period";
  53. printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
  54. term, (u64)evsel->attr.sample_freq);
  55. }
  56. if (details->trace_fields) {
  57. struct format_field *field;
  58. if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  59. printed += comma_fprintf(fp, &first, " (not a tracepoint)");
  60. goto out;
  61. }
  62. field = evsel->tp_format->format.fields;
  63. if (field == NULL) {
  64. printed += comma_fprintf(fp, &first, " (no trace field)");
  65. goto out;
  66. }
  67. printed += comma_fprintf(fp, &first, " trace_fields: %s", field->name);
  68. field = field->next;
  69. while (field) {
  70. printed += comma_fprintf(fp, &first, "%s", field->name);
  71. field = field->next;
  72. }
  73. }
  74. out:
  75. fputc('\n', fp);
  76. return ++printed;
  77. }
  78. int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
  79. unsigned int print_opts, struct callchain_cursor *cursor,
  80. FILE *fp)
  81. {
  82. int printed = 0;
  83. struct callchain_cursor_node *node;
  84. int print_ip = print_opts & EVSEL__PRINT_IP;
  85. int print_sym = print_opts & EVSEL__PRINT_SYM;
  86. int print_dso = print_opts & EVSEL__PRINT_DSO;
  87. int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
  88. int print_oneline = print_opts & EVSEL__PRINT_ONELINE;
  89. int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
  90. int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
  91. int print_arrow = print_opts & EVSEL__PRINT_CALLCHAIN_ARROW;
  92. int print_skip_ignored = print_opts & EVSEL__PRINT_SKIP_IGNORED;
  93. char s = print_oneline ? ' ' : '\t';
  94. bool first = true;
  95. if (sample->callchain) {
  96. struct addr_location node_al;
  97. callchain_cursor_commit(cursor);
  98. while (1) {
  99. u64 addr = 0;
  100. node = callchain_cursor_current(cursor);
  101. if (!node)
  102. break;
  103. if (node->sym && node->sym->ignore && print_skip_ignored)
  104. goto next;
  105. printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
  106. if (print_arrow && !first)
  107. printed += fprintf(fp, " <-");
  108. if (print_ip)
  109. printed += fprintf(fp, "%c%16" PRIx64, s, node->ip);
  110. if (node->map)
  111. addr = node->map->map_ip(node->map, node->ip);
  112. if (print_sym) {
  113. printed += fprintf(fp, " ");
  114. node_al.addr = addr;
  115. node_al.map = node->map;
  116. if (print_symoffset) {
  117. printed += __symbol__fprintf_symname_offs(node->sym, &node_al,
  118. print_unknown_as_addr,
  119. true, fp);
  120. } else {
  121. printed += __symbol__fprintf_symname(node->sym, &node_al,
  122. print_unknown_as_addr, fp);
  123. }
  124. }
  125. if (print_dso) {
  126. printed += fprintf(fp, " (");
  127. printed += map__fprintf_dsoname(node->map, fp);
  128. printed += fprintf(fp, ")");
  129. }
  130. if (print_srcline)
  131. printed += map__fprintf_srcline(node->map, addr, "\n ", fp);
  132. if (!print_oneline)
  133. printed += fprintf(fp, "\n");
  134. if (symbol_conf.bt_stop_list &&
  135. node->sym &&
  136. node->sym->name &&
  137. strlist__has_entry(symbol_conf.bt_stop_list,
  138. node->sym->name)) {
  139. break;
  140. }
  141. first = false;
  142. next:
  143. callchain_cursor_advance(cursor);
  144. }
  145. }
  146. return printed;
  147. }
  148. int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
  149. int left_alignment, unsigned int print_opts,
  150. struct callchain_cursor *cursor, FILE *fp)
  151. {
  152. int printed = 0;
  153. int print_ip = print_opts & EVSEL__PRINT_IP;
  154. int print_sym = print_opts & EVSEL__PRINT_SYM;
  155. int print_dso = print_opts & EVSEL__PRINT_DSO;
  156. int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
  157. int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
  158. int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
  159. if (cursor != NULL) {
  160. printed += sample__fprintf_callchain(sample, left_alignment,
  161. print_opts, cursor, fp);
  162. } else {
  163. printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
  164. if (print_ip)
  165. printed += fprintf(fp, "%16" PRIx64, sample->ip);
  166. if (print_sym) {
  167. printed += fprintf(fp, " ");
  168. if (print_symoffset) {
  169. printed += __symbol__fprintf_symname_offs(al->sym, al,
  170. print_unknown_as_addr,
  171. true, fp);
  172. } else {
  173. printed += __symbol__fprintf_symname(al->sym, al,
  174. print_unknown_as_addr, fp);
  175. }
  176. }
  177. if (print_dso) {
  178. printed += fprintf(fp, " (");
  179. printed += map__fprintf_dsoname(al->map, fp);
  180. printed += fprintf(fp, ")");
  181. }
  182. if (print_srcline)
  183. printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
  184. }
  185. return printed;
  186. }