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