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 "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 15 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...) 16 { 17 va_list args; 18 int ret = 0; 19 20 if (!*first) { 21 ret += fprintf(fp, ","); 22 } else { 23 ret += fprintf(fp, ":"); 24 *first = false; 25 } 26 27 va_start(args, fmt); 28 ret += vfprintf(fp, fmt, args); 29 va_end(args); 30 return ret; 31 } 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 38 int perf_evsel__fprintf(struct evsel *evsel, 39 struct perf_attr_details *details, FILE *fp) 40 { 41 bool first = true; 42 int printed = 0; 43 44 if (details->event_group) { 45 struct evsel *pos; 46 47 if (!perf_evsel__is_group_leader(evsel)) 48 return 0; 49 50 if (evsel->core.nr_members > 1) 51 printed += fprintf(fp, "%s{", evsel->group_name ?: ""); 52 53 printed += fprintf(fp, "%s", perf_evsel__name(evsel)); 54 for_each_group_member(pos, evsel) 55 printed += fprintf(fp, ",%s", perf_evsel__name(pos)); 56 57 if (evsel->core.nr_members > 1) 58 printed += fprintf(fp, "}"); 59 goto out; 60 } 61 62 printed += fprintf(fp, "%s", perf_evsel__name(evsel)); 63 64 if (details->verbose) { 65 printed += perf_event_attr__fprintf(fp, &evsel->core.attr, 66 __print_attr__fprintf, &first); 67 } else if (details->freq) { 68 const char *term = "sample_freq"; 69 70 if (!evsel->core.attr.freq) 71 term = "sample_period"; 72 73 printed += comma_fprintf(fp, &first, " %s=%" PRIu64, 74 term, (u64)evsel->core.attr.sample_freq); 75 } 76 77 if (details->trace_fields) { 78 struct tep_format_field *field; 79 80 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 81 printed += comma_fprintf(fp, &first, " (not a tracepoint)"); 82 goto out; 83 } 84 85 field = evsel->tp_format->format.fields; 86 if (field == NULL) { 87 printed += comma_fprintf(fp, &first, " (no trace field)"); 88 goto out; 89 } 90 91 printed += comma_fprintf(fp, &first, " trace_fields: %s", field->name); 92 93 field = field->next; 94 while (field) { 95 printed += comma_fprintf(fp, &first, "%s", field->name); 96 field = field->next; 97 } 98 } 99 out: 100 fputc('\n', fp); 101 return ++printed; 102 } 103 104 int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment, 105 unsigned int print_opts, struct callchain_cursor *cursor, 106 struct strlist *bt_stop_list, FILE *fp) 107 { 108 int printed = 0; 109 struct callchain_cursor_node *node; 110 int print_ip = print_opts & EVSEL__PRINT_IP; 111 int print_sym = print_opts & EVSEL__PRINT_SYM; 112 int print_dso = print_opts & EVSEL__PRINT_DSO; 113 int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET; 114 int print_oneline = print_opts & EVSEL__PRINT_ONELINE; 115 int print_srcline = print_opts & EVSEL__PRINT_SRCLINE; 116 int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR; 117 int print_arrow = print_opts & EVSEL__PRINT_CALLCHAIN_ARROW; 118 int print_skip_ignored = print_opts & EVSEL__PRINT_SKIP_IGNORED; 119 char s = print_oneline ? ' ' : '\t'; 120 bool first = true; 121 122 if (sample->callchain) { 123 struct addr_location node_al; 124 125 callchain_cursor_commit(cursor); 126 127 while (1) { 128 struct symbol *sym; 129 struct map *map; 130 u64 addr = 0; 131 132 node = callchain_cursor_current(cursor); 133 if (!node) 134 break; 135 136 sym = node->ms.sym; 137 map = node->ms.map; 138 139 if (sym && sym->ignore && print_skip_ignored) 140 goto next; 141 142 printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " "); 143 144 if (print_arrow && !first) 145 printed += fprintf(fp, " <-"); 146 147 if (print_ip) 148 printed += fprintf(fp, "%c%16" PRIx64, s, node->ip); 149 150 if (map) 151 addr = map->map_ip(map, node->ip); 152 153 if (print_sym) { 154 printed += fprintf(fp, " "); 155 node_al.addr = addr; 156 node_al.map = map; 157 158 if (print_symoffset) { 159 printed += __symbol__fprintf_symname_offs(sym, &node_al, 160 print_unknown_as_addr, 161 true, fp); 162 } else { 163 printed += __symbol__fprintf_symname(sym, &node_al, 164 print_unknown_as_addr, fp); 165 } 166 } 167 168 if (print_dso && (!sym || !sym->inlined)) { 169 printed += fprintf(fp, " ("); 170 printed += map__fprintf_dsoname(map, fp); 171 printed += fprintf(fp, ")"); 172 } 173 174 if (print_srcline) 175 printed += map__fprintf_srcline(map, addr, "\n ", fp); 176 177 if (sym && sym->inlined) 178 printed += fprintf(fp, " (inlined)"); 179 180 if (!print_oneline) 181 printed += fprintf(fp, "\n"); 182 183 /* Add srccode here too? */ 184 if (bt_stop_list && sym && 185 strlist__has_entry(bt_stop_list, sym->name)) { 186 break; 187 } 188 189 first = false; 190 next: 191 callchain_cursor_advance(cursor); 192 } 193 } 194 195 return printed; 196 } 197 198 int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al, 199 int left_alignment, unsigned int print_opts, 200 struct callchain_cursor *cursor, struct strlist *bt_stop_list, FILE *fp) 201 { 202 int printed = 0; 203 int print_ip = print_opts & EVSEL__PRINT_IP; 204 int print_sym = print_opts & EVSEL__PRINT_SYM; 205 int print_dso = print_opts & EVSEL__PRINT_DSO; 206 int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET; 207 int print_srcline = print_opts & EVSEL__PRINT_SRCLINE; 208 int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR; 209 210 if (cursor != NULL) { 211 printed += sample__fprintf_callchain(sample, left_alignment, print_opts, 212 cursor, bt_stop_list, fp); 213 } else { 214 printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " "); 215 216 if (print_ip) 217 printed += fprintf(fp, "%16" PRIx64, sample->ip); 218 219 if (print_sym) { 220 printed += fprintf(fp, " "); 221 if (print_symoffset) { 222 printed += __symbol__fprintf_symname_offs(al->sym, al, 223 print_unknown_as_addr, 224 true, fp); 225 } else { 226 printed += __symbol__fprintf_symname(al->sym, al, 227 print_unknown_as_addr, fp); 228 } 229 } 230 231 if (print_dso) { 232 printed += fprintf(fp, " ("); 233 printed += map__fprintf_dsoname(al->map, fp); 234 printed += fprintf(fp, ")"); 235 } 236 237 if (print_srcline) 238 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp); 239 } 240 241 return printed; 242 } 243