evsel_fprintf.c 6.6 KB

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