Loading...
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 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 }
99out:
100 fputc('\n', fp);
101 return ++printed;
102}
103
104int 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 u64 addr = 0;
129
130 node = callchain_cursor_current(cursor);
131 if (!node)
132 break;
133
134 if (node->sym && node->sym->ignore && print_skip_ignored)
135 goto next;
136
137 printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
138
139 if (print_arrow && !first)
140 printed += fprintf(fp, " <-");
141
142 if (print_ip)
143 printed += fprintf(fp, "%c%16" PRIx64, s, node->ip);
144
145 if (node->map)
146 addr = node->map->map_ip(node->map, node->ip);
147
148 if (print_sym) {
149 printed += fprintf(fp, " ");
150 node_al.addr = addr;
151 node_al.map = node->map;
152
153 if (print_symoffset) {
154 printed += __symbol__fprintf_symname_offs(node->sym, &node_al,
155 print_unknown_as_addr,
156 true, fp);
157 } else {
158 printed += __symbol__fprintf_symname(node->sym, &node_al,
159 print_unknown_as_addr, fp);
160 }
161 }
162
163 if (print_dso && (!node->sym || !node->sym->inlined)) {
164 printed += fprintf(fp, " (");
165 printed += map__fprintf_dsoname(node->map, fp);
166 printed += fprintf(fp, ")");
167 }
168
169 if (print_srcline)
170 printed += map__fprintf_srcline(node->map, addr, "\n ", fp);
171
172 if (node->sym && node->sym->inlined)
173 printed += fprintf(fp, " (inlined)");
174
175 if (!print_oneline)
176 printed += fprintf(fp, "\n");
177
178 /* Add srccode here too? */
179 if (bt_stop_list && node->sym &&
180 strlist__has_entry(bt_stop_list, node->sym->name)) {
181 break;
182 }
183
184 first = false;
185next:
186 callchain_cursor_advance(cursor);
187 }
188 }
189
190 return printed;
191}
192
193int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
194 int left_alignment, unsigned int print_opts,
195 struct callchain_cursor *cursor, struct strlist *bt_stop_list, FILE *fp)
196{
197 int printed = 0;
198 int print_ip = print_opts & EVSEL__PRINT_IP;
199 int print_sym = print_opts & EVSEL__PRINT_SYM;
200 int print_dso = print_opts & EVSEL__PRINT_DSO;
201 int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
202 int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
203 int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
204
205 if (cursor != NULL) {
206 printed += sample__fprintf_callchain(sample, left_alignment, print_opts,
207 cursor, bt_stop_list, fp);
208 } else {
209 printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
210
211 if (print_ip)
212 printed += fprintf(fp, "%16" PRIx64, sample->ip);
213
214 if (print_sym) {
215 printed += fprintf(fp, " ");
216 if (print_symoffset) {
217 printed += __symbol__fprintf_symname_offs(al->sym, al,
218 print_unknown_as_addr,
219 true, fp);
220 } else {
221 printed += __symbol__fprintf_symname(al->sym, al,
222 print_unknown_as_addr, fp);
223 }
224 }
225
226 if (print_dso) {
227 printed += fprintf(fp, " (");
228 printed += map__fprintf_dsoname(al->map, fp);
229 printed += fprintf(fp, ")");
230 }
231
232 if (print_srcline)
233 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
234 }
235
236 return printed;
237}
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 <traceevent/event-parse.h>
18#endif
19
20static 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
38static 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
43int 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
85 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
86 printed += comma_fprintf(fp, &first, " (not a tracepoint)");
87 goto out;
88 }
89
90 field = evsel->tp_format->format.fields;
91 if (field == NULL) {
92 printed += comma_fprintf(fp, &first, " (no trace field)");
93 goto out;
94 }
95
96 printed += comma_fprintf(fp, &first, " trace_fields: %s", field->name);
97
98 field = field->next;
99 while (field) {
100 printed += comma_fprintf(fp, &first, "%s", field->name);
101 field = field->next;
102 }
103 }
104#endif
105out:
106 fputc('\n', fp);
107 return ++printed;
108}
109
110#ifndef PYTHON_PERF
111int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
112 unsigned int print_opts, struct callchain_cursor *cursor,
113 struct strlist *bt_stop_list, FILE *fp)
114{
115 int printed = 0;
116 struct callchain_cursor_node *node;
117 int print_ip = print_opts & EVSEL__PRINT_IP;
118 int print_sym = print_opts & EVSEL__PRINT_SYM;
119 int print_dso = print_opts & EVSEL__PRINT_DSO;
120 int print_dsoff = print_opts & EVSEL__PRINT_DSOFF;
121 int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
122 int print_oneline = print_opts & EVSEL__PRINT_ONELINE;
123 int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
124 int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
125 int print_arrow = print_opts & EVSEL__PRINT_CALLCHAIN_ARROW;
126 int print_skip_ignored = print_opts & EVSEL__PRINT_SKIP_IGNORED;
127 char s = print_oneline ? ' ' : '\t';
128 bool first = true;
129
130 if (cursor == NULL)
131 return fprintf(fp, "<not enough memory for the callchain cursor>%s", print_oneline ? "" : "\n");
132
133 if (sample->callchain) {
134 callchain_cursor_commit(cursor);
135
136 while (1) {
137 struct symbol *sym;
138 struct map *map;
139 u64 addr = 0;
140
141 node = callchain_cursor_current(cursor);
142 if (!node)
143 break;
144
145 sym = node->ms.sym;
146 map = node->ms.map;
147
148 if (sym && sym->ignore && print_skip_ignored)
149 goto next;
150
151 printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
152
153 if (print_arrow && !first)
154 printed += fprintf(fp, " <-");
155
156 if (map)
157 addr = map__map_ip(map, node->ip);
158
159 if (print_ip)
160 printed += fprintf(fp, "%c%16" PRIx64, s, node->ip);
161
162 if (print_sym) {
163 struct addr_location node_al;
164
165 addr_location__init(&node_al);
166 printed += fprintf(fp, " ");
167 node_al.addr = addr;
168 node_al.map = map__get(map);
169
170 if (print_symoffset) {
171 printed += __symbol__fprintf_symname_offs(sym, &node_al,
172 print_unknown_as_addr,
173 true, fp);
174 } else {
175 printed += __symbol__fprintf_symname(sym, &node_al,
176 print_unknown_as_addr, fp);
177 }
178 addr_location__exit(&node_al);
179 }
180
181 if (print_dso && (!sym || !sym->inlined))
182 printed += map__fprintf_dsoname_dsoff(map, print_dsoff, addr, fp);
183
184 if (print_srcline)
185 printed += map__fprintf_srcline(map, addr, "\n ", fp);
186
187 if (sym && sym->inlined)
188 printed += fprintf(fp, " (inlined)");
189
190 if (!print_oneline)
191 printed += fprintf(fp, "\n");
192
193 /* Add srccode here too? */
194 if (bt_stop_list && sym &&
195 strlist__has_entry(bt_stop_list, sym->name)) {
196 break;
197 }
198
199 first = false;
200next:
201 callchain_cursor_advance(cursor);
202 }
203 }
204
205 return printed;
206}
207
208int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
209 int left_alignment, unsigned int print_opts,
210 struct callchain_cursor *cursor, struct strlist *bt_stop_list, FILE *fp)
211{
212 int printed = 0;
213 int print_ip = print_opts & EVSEL__PRINT_IP;
214 int print_sym = print_opts & EVSEL__PRINT_SYM;
215 int print_dso = print_opts & EVSEL__PRINT_DSO;
216 int print_dsoff = print_opts & EVSEL__PRINT_DSOFF;
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 += map__fprintf_dsoname_dsoff(al->map, print_dsoff, al->addr, fp);
244
245 if (print_srcline)
246 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
247 }
248
249 return printed;
250}
251#endif /* PYTHON_PERF */