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