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