evsel_fprintf.c 5.2 KB

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