evsel_fprintf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. char s = print_oneline ? ' ' : '\t';
  92. if (sample->callchain) {
  93. struct addr_location node_al;
  94. callchain_cursor_commit(cursor);
  95. while (1) {
  96. u64 addr = 0;
  97. node = callchain_cursor_current(cursor);
  98. if (!node)
  99. break;
  100. if (node->sym && node->sym->ignore)
  101. goto next;
  102. printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
  103. if (print_ip)
  104. printed += fprintf(fp, "%c%16" PRIx64, s, node->ip);
  105. if (node->map)
  106. addr = node->map->map_ip(node->map, node->ip);
  107. if (print_sym) {
  108. printed += fprintf(fp, " ");
  109. node_al.addr = addr;
  110. node_al.map = node->map;
  111. if (print_symoffset) {
  112. printed += __symbol__fprintf_symname_offs(node->sym, &node_al,
  113. print_unknown_as_addr, fp);
  114. } else {
  115. printed += __symbol__fprintf_symname(node->sym, &node_al,
  116. print_unknown_as_addr, fp);
  117. }
  118. }
  119. if (print_dso) {
  120. printed += fprintf(fp, " (");
  121. printed += map__fprintf_dsoname(node->map, fp);
  122. printed += fprintf(fp, ")");
  123. }
  124. if (print_srcline)
  125. printed += map__fprintf_srcline(node->map, addr, "\n ", fp);
  126. if (!print_oneline)
  127. printed += fprintf(fp, "\n");
  128. next:
  129. callchain_cursor_advance(cursor);
  130. }
  131. }
  132. return printed;
  133. }
  134. int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
  135. int left_alignment, unsigned int print_opts,
  136. struct callchain_cursor *cursor, FILE *fp)
  137. {
  138. int printed = 0;
  139. int print_ip = print_opts & EVSEL__PRINT_IP;
  140. int print_sym = print_opts & EVSEL__PRINT_SYM;
  141. int print_dso = print_opts & EVSEL__PRINT_DSO;
  142. int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
  143. int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
  144. int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
  145. if (cursor != NULL) {
  146. printed += sample__fprintf_callchain(sample, left_alignment,
  147. print_opts, cursor, fp);
  148. } else if (!(al->sym && al->sym->ignore)) {
  149. printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
  150. if (print_ip)
  151. printed += fprintf(fp, "%16" PRIx64, sample->ip);
  152. if (print_sym) {
  153. printed += fprintf(fp, " ");
  154. if (print_symoffset) {
  155. printed += __symbol__fprintf_symname_offs(al->sym, al,
  156. print_unknown_as_addr, fp);
  157. } else {
  158. printed += __symbol__fprintf_symname(al->sym, al,
  159. print_unknown_as_addr, fp);
  160. }
  161. }
  162. if (print_dso) {
  163. printed += fprintf(fp, " (");
  164. printed += map__fprintf_dsoname(al->map, fp);
  165. printed += fprintf(fp, ")");
  166. }
  167. if (print_srcline)
  168. printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
  169. }
  170. return printed;
  171. }