evsel_fprintf.c 5.9 KB

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