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