1 /*
2  * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3  *
4  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License (not later!)
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
26 
27 #include "../perf.h"
28 #include "util.h"
29 #include "trace-event.h"
30 
31 int header_page_size_size;
32 int header_page_ts_size;
33 int header_page_data_offset;
34 
35 bool latency_format;
36 
37 struct pevent *read_trace_init(int file_bigendian, int host_bigendian)
38 {
39 	struct pevent *pevent = pevent_alloc();
40 
41 	if (pevent != NULL) {
42 		pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
43 		pevent_set_file_bigendian(pevent, file_bigendian);
44 		pevent_set_host_bigendian(pevent, host_bigendian);
45 	}
46 
47 	return pevent;
48 }
49 
50 static int get_common_field(struct scripting_context *context,
51 			    int *offset, int *size, const char *type)
52 {
53 	struct pevent *pevent = context->pevent;
54 	struct event_format *event;
55 	struct format_field *field;
56 
57 	if (!*size) {
58 		if (!pevent->events)
59 			return 0;
60 
61 		event = pevent->events[0];
62 		field = pevent_find_common_field(event, type);
63 		if (!field)
64 			return 0;
65 		*offset = field->offset;
66 		*size = field->size;
67 	}
68 
69 	return pevent_read_number(pevent, context->event_data + *offset, *size);
70 }
71 
72 int common_lock_depth(struct scripting_context *context)
73 {
74 	static int offset;
75 	static int size;
76 	int ret;
77 
78 	ret = get_common_field(context, &size, &offset,
79 			       "common_lock_depth");
80 	if (ret < 0)
81 		return -1;
82 
83 	return ret;
84 }
85 
86 int common_flags(struct scripting_context *context)
87 {
88 	static int offset;
89 	static int size;
90 	int ret;
91 
92 	ret = get_common_field(context, &size, &offset,
93 			       "common_flags");
94 	if (ret < 0)
95 		return -1;
96 
97 	return ret;
98 }
99 
100 int common_pc(struct scripting_context *context)
101 {
102 	static int offset;
103 	static int size;
104 	int ret;
105 
106 	ret = get_common_field(context, &size, &offset,
107 			       "common_preempt_count");
108 	if (ret < 0)
109 		return -1;
110 
111 	return ret;
112 }
113 
114 unsigned long long
115 raw_field_value(struct event_format *event, const char *name, void *data)
116 {
117 	struct format_field *field;
118 	unsigned long long val;
119 
120 	field = pevent_find_any_field(event, name);
121 	if (!field)
122 		return 0ULL;
123 
124 	pevent_read_number_field(field, data, &val);
125 
126 	return val;
127 }
128 
129 void *raw_field_ptr(struct event_format *event, const char *name, void *data)
130 {
131 	struct format_field *field;
132 
133 	field = pevent_find_any_field(event, name);
134 	if (!field)
135 		return NULL;
136 
137 	if (field->flags & FIELD_IS_DYNAMIC) {
138 		int offset;
139 
140 		offset = *(int *)(data + field->offset);
141 		offset &= 0xffff;
142 
143 		return data + offset;
144 	}
145 
146 	return data + field->offset;
147 }
148 
149 int trace_parse_common_type(struct pevent *pevent, void *data)
150 {
151 	struct pevent_record record;
152 
153 	record.data = data;
154 	return pevent_data_type(pevent, &record);
155 }
156 
157 int trace_parse_common_pid(struct pevent *pevent, void *data)
158 {
159 	struct pevent_record record;
160 
161 	record.data = data;
162 	return pevent_data_pid(pevent, &record);
163 }
164 
165 unsigned long long read_size(struct pevent *pevent, void *ptr, int size)
166 {
167 	return pevent_read_number(pevent, ptr, size);
168 }
169 
170 void print_trace_event(struct pevent *pevent, int cpu, void *data, int size)
171 {
172 	struct event_format *event;
173 	struct pevent_record record;
174 	struct trace_seq s;
175 	int type;
176 
177 	type = trace_parse_common_type(pevent, data);
178 
179 	event = pevent_find_event(pevent, type);
180 	if (!event) {
181 		warning("ug! no event found for type %d", type);
182 		return;
183 	}
184 
185 	memset(&record, 0, sizeof(record));
186 	record.cpu = cpu;
187 	record.size = size;
188 	record.data = data;
189 
190 	trace_seq_init(&s);
191 	pevent_print_event(pevent, &s, &record);
192 	trace_seq_do_printf(&s);
193 	printf("\n");
194 }
195 
196 void print_event(struct pevent *pevent, int cpu, void *data, int size,
197 		 unsigned long long nsecs, char *comm)
198 {
199 	struct pevent_record record;
200 	struct trace_seq s;
201 	int pid;
202 
203 	pevent->latency_format = latency_format;
204 
205 	record.ts = nsecs;
206 	record.cpu = cpu;
207 	record.size = size;
208 	record.data = data;
209 	pid = pevent_data_pid(pevent, &record);
210 
211 	if (!pevent_pid_is_registered(pevent, pid))
212 		pevent_register_comm(pevent, comm, pid);
213 
214 	trace_seq_init(&s);
215 	pevent_print_event(pevent, &s, &record);
216 	trace_seq_do_printf(&s);
217 	printf("\n");
218 }
219 
220 void parse_proc_kallsyms(struct pevent *pevent,
221 			 char *file, unsigned int size __unused)
222 {
223 	unsigned long long addr;
224 	char *func;
225 	char *line;
226 	char *next = NULL;
227 	char *addr_str;
228 	char *mod;
229 	char ch;
230 
231 	line = strtok_r(file, "\n", &next);
232 	while (line) {
233 		mod = NULL;
234 		sscanf(line, "%as %c %as\t[%as",
235 		       (float *)(void *)&addr_str, /* workaround gcc warning */
236 		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
237 		addr = strtoull(addr_str, NULL, 16);
238 		free(addr_str);
239 
240 		/* truncate the extra ']' */
241 		if (mod)
242 			mod[strlen(mod) - 1] = 0;
243 
244 		pevent_register_function(pevent, func, addr, mod);
245 		free(func);
246 		free(mod);
247 
248 		line = strtok_r(NULL, "\n", &next);
249 	}
250 }
251 
252 void parse_ftrace_printk(struct pevent *pevent,
253 			 char *file, unsigned int size __unused)
254 {
255 	unsigned long long addr;
256 	char *printk;
257 	char *line;
258 	char *next = NULL;
259 	char *addr_str;
260 	char *fmt;
261 
262 	line = strtok_r(file, "\n", &next);
263 	while (line) {
264 		addr_str = strtok_r(line, ":", &fmt);
265 		if (!addr_str) {
266 			warning("printk format with empty entry");
267 			break;
268 		}
269 		addr = strtoull(addr_str, NULL, 16);
270 		/* fmt still has a space, skip it */
271 		printk = strdup(fmt+1);
272 		line = strtok_r(NULL, "\n", &next);
273 		pevent_register_print_string(pevent, printk, addr);
274 	}
275 }
276 
277 int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
278 {
279 	return pevent_parse_event(pevent, buf, size, "ftrace");
280 }
281 
282 int parse_event_file(struct pevent *pevent,
283 		     char *buf, unsigned long size, char *sys)
284 {
285 	return pevent_parse_event(pevent, buf, size, sys);
286 }
287 
288 struct event_format *trace_find_next_event(struct pevent *pevent,
289 					   struct event_format *event)
290 {
291 	static int idx;
292 
293 	if (!pevent->events)
294 		return NULL;
295 
296 	if (!event) {
297 		idx = 0;
298 		return pevent->events[0];
299 	}
300 
301 	if (idx < pevent->nr_events && event == pevent->events[idx]) {
302 		idx++;
303 		if (idx == pevent->nr_events)
304 			return NULL;
305 		return pevent->events[idx];
306 	}
307 
308 	for (idx = 1; idx < pevent->nr_events; idx++) {
309 		if (event == pevent->events[idx - 1])
310 			return pevent->events[idx];
311 	}
312 	return NULL;
313 }
314 
315 struct flag {
316 	const char *name;
317 	unsigned long long value;
318 };
319 
320 static const struct flag flags[] = {
321 	{ "HI_SOFTIRQ", 0 },
322 	{ "TIMER_SOFTIRQ", 1 },
323 	{ "NET_TX_SOFTIRQ", 2 },
324 	{ "NET_RX_SOFTIRQ", 3 },
325 	{ "BLOCK_SOFTIRQ", 4 },
326 	{ "BLOCK_IOPOLL_SOFTIRQ", 5 },
327 	{ "TASKLET_SOFTIRQ", 6 },
328 	{ "SCHED_SOFTIRQ", 7 },
329 	{ "HRTIMER_SOFTIRQ", 8 },
330 	{ "RCU_SOFTIRQ", 9 },
331 
332 	{ "HRTIMER_NORESTART", 0 },
333 	{ "HRTIMER_RESTART", 1 },
334 };
335 
336 unsigned long long eval_flag(const char *flag)
337 {
338 	int i;
339 
340 	/*
341 	 * Some flags in the format files do not get converted.
342 	 * If the flag is not numeric, see if it is something that
343 	 * we already know about.
344 	 */
345 	if (isdigit(flag[0]))
346 		return strtoull(flag, NULL, 0);
347 
348 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
349 		if (strcmp(flags[i].name, flag) == 0)
350 			return flags[i].value;
351 
352 	return 0;
353 }
354