xref: /linux/tools/perf/util/evsel_fprintf.c (revision 983034cd0d212b23a63efb48ecc47d55d70ee301)
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 
19 static 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 
37 static 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 
42 int 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
104 out:
105 	fputc('\n', fp);
106 	return ++printed;
107 }
108 
109 #ifndef PYTHON_PERF
110 int 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_dsoff = print_opts & EVSEL__PRINT_DSOFF;
120 	int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
121 	int print_oneline = print_opts & EVSEL__PRINT_ONELINE;
122 	int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
123 	int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
124 	int print_arrow = print_opts & EVSEL__PRINT_CALLCHAIN_ARROW;
125 	int print_skip_ignored = print_opts & EVSEL__PRINT_SKIP_IGNORED;
126 	char s = print_oneline ? ' ' : '\t';
127 	bool first = true;
128 
129 	if (sample->callchain) {
130 		struct addr_location node_al;
131 
132 		callchain_cursor_commit(cursor);
133 
134 		while (1) {
135 			struct symbol *sym;
136 			struct map *map;
137 			u64 addr = 0;
138 
139 			node = callchain_cursor_current(cursor);
140 			if (!node)
141 				break;
142 
143 			sym = node->ms.sym;
144 			map = node->ms.map;
145 
146 			if (sym && sym->ignore && print_skip_ignored)
147 				goto next;
148 
149 			printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
150 
151 			if (print_arrow && !first)
152 				printed += fprintf(fp, " <-");
153 
154 			if (map)
155 				addr = map__map_ip(map, node->ip);
156 
157 			if (print_ip)
158 				printed += fprintf(fp, "%c%16" PRIx64, s, node->ip);
159 
160 			if (print_sym) {
161 				printed += fprintf(fp, " ");
162 				node_al.addr = addr;
163 				node_al.map  = map;
164 
165 				if (print_symoffset) {
166 					printed += __symbol__fprintf_symname_offs(sym, &node_al,
167 										  print_unknown_as_addr,
168 										  true, fp);
169 				} else {
170 					printed += __symbol__fprintf_symname(sym, &node_al,
171 									     print_unknown_as_addr, fp);
172 				}
173 			}
174 
175 			if (print_dso && (!sym || !sym->inlined))
176 				printed += map__fprintf_dsoname_dsoff(map, print_dsoff, addr, fp);
177 
178 			if (print_srcline)
179 				printed += map__fprintf_srcline(map, addr, "\n  ", fp);
180 
181 			if (sym && sym->inlined)
182 				printed += fprintf(fp, " (inlined)");
183 
184 			if (!print_oneline)
185 				printed += fprintf(fp, "\n");
186 
187 			/* Add srccode here too? */
188 			if (bt_stop_list && sym &&
189 			    strlist__has_entry(bt_stop_list, sym->name)) {
190 				break;
191 			}
192 
193 			first = false;
194 next:
195 			callchain_cursor_advance(cursor);
196 		}
197 	}
198 
199 	return printed;
200 }
201 
202 int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
203 			int left_alignment, unsigned int print_opts,
204 			struct callchain_cursor *cursor, struct strlist *bt_stop_list, FILE *fp)
205 {
206 	int printed = 0;
207 	int print_ip = print_opts & EVSEL__PRINT_IP;
208 	int print_sym = print_opts & EVSEL__PRINT_SYM;
209 	int print_dso = print_opts & EVSEL__PRINT_DSO;
210 	int print_dsoff = print_opts & EVSEL__PRINT_DSOFF;
211 	int print_symoffset = print_opts & EVSEL__PRINT_SYMOFFSET;
212 	int print_srcline = print_opts & EVSEL__PRINT_SRCLINE;
213 	int print_unknown_as_addr = print_opts & EVSEL__PRINT_UNKNOWN_AS_ADDR;
214 
215 	if (cursor != NULL) {
216 		printed += sample__fprintf_callchain(sample, left_alignment, print_opts,
217 						     cursor, bt_stop_list, fp);
218 	} else {
219 		printed += fprintf(fp, "%-*.*s", left_alignment, left_alignment, " ");
220 
221 		if (print_ip)
222 			printed += fprintf(fp, "%16" PRIx64, sample->ip);
223 
224 		if (print_sym) {
225 			printed += fprintf(fp, " ");
226 			if (print_symoffset) {
227 				printed += __symbol__fprintf_symname_offs(al->sym, al,
228 									  print_unknown_as_addr,
229 									  true, fp);
230 			} else {
231 				printed += __symbol__fprintf_symname(al->sym, al,
232 								     print_unknown_as_addr, fp);
233 			}
234 		}
235 
236 		if (print_dso)
237 			printed += map__fprintf_dsoname_dsoff(al->map, print_dsoff, al->addr, fp);
238 
239 		if (print_srcline)
240 			printed += map__fprintf_srcline(al->map, al->addr, "\n  ", fp);
241 	}
242 
243 	return printed;
244 }
245 #endif /* PYTHON_PERF */
246