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