1aaf045f7SSteven Rostedt /*
2aaf045f7SSteven Rostedt  * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3aaf045f7SSteven Rostedt  *
4aaf045f7SSteven Rostedt  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5aaf045f7SSteven Rostedt  *
6aaf045f7SSteven Rostedt  * This program is free software; you can redistribute it and/or modify
7aaf045f7SSteven Rostedt  * it under the terms of the GNU General Public License as published by
8aaf045f7SSteven Rostedt  * the Free Software Foundation; version 2 of the License (not later!)
9aaf045f7SSteven Rostedt  *
10aaf045f7SSteven Rostedt  * This program is distributed in the hope that it will be useful,
11aaf045f7SSteven Rostedt  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12aaf045f7SSteven Rostedt  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13aaf045f7SSteven Rostedt  * GNU General Public License for more details.
14aaf045f7SSteven Rostedt  *
15aaf045f7SSteven Rostedt  * You should have received a copy of the GNU General Public License
16aaf045f7SSteven Rostedt  * along with this program; if not, write to the Free Software
17aaf045f7SSteven Rostedt  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18aaf045f7SSteven Rostedt  *
19aaf045f7SSteven Rostedt  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20aaf045f7SSteven Rostedt  */
21aaf045f7SSteven Rostedt #include <stdio.h>
22aaf045f7SSteven Rostedt #include <stdlib.h>
23aaf045f7SSteven Rostedt #include <string.h>
24aaf045f7SSteven Rostedt #include <ctype.h>
25aaf045f7SSteven Rostedt #include <errno.h>
26aaf045f7SSteven Rostedt 
271ef2ed10SFrederic Weisbecker #include "../perf.h"
28ea4010d1SSteven Rostedt #include "util.h"
29ea4010d1SSteven Rostedt #include "trace-event.h"
30ea4010d1SSteven Rostedt 
31aaf045f7SSteven Rostedt static int get_common_field(struct scripting_context *context,
32aaf045f7SSteven Rostedt 			    int *offset, int *size, const char *type)
337397d80dSTom Zanussi {
34da378962SArnaldo Carvalho de Melo 	struct pevent *pevent = context->pevent;
35aaf045f7SSteven Rostedt 	struct event_format *event;
36aaf045f7SSteven Rostedt 	struct format_field *field;
37aaf045f7SSteven Rostedt 
38aaf045f7SSteven Rostedt 	if (!*size) {
39aaf045f7SSteven Rostedt 		if (!pevent->events)
40aaf045f7SSteven Rostedt 			return 0;
41aaf045f7SSteven Rostedt 
42aaf045f7SSteven Rostedt 		event = pevent->events[0];
43aaf045f7SSteven Rostedt 		field = pevent_find_common_field(event, type);
44aaf045f7SSteven Rostedt 		if (!field)
45aaf045f7SSteven Rostedt 			return 0;
46aaf045f7SSteven Rostedt 		*offset = field->offset;
47aaf045f7SSteven Rostedt 		*size = field->size;
48aaf045f7SSteven Rostedt 	}
49aaf045f7SSteven Rostedt 
50aaf045f7SSteven Rostedt 	return pevent_read_number(pevent, context->event_data + *offset, *size);
517397d80dSTom Zanussi }
527397d80dSTom Zanussi 
537397d80dSTom Zanussi int common_lock_depth(struct scripting_context *context)
547397d80dSTom Zanussi {
55aaf045f7SSteven Rostedt 	static int offset;
56aaf045f7SSteven Rostedt 	static int size;
57aaf045f7SSteven Rostedt 	int ret;
58aaf045f7SSteven Rostedt 
59aaf045f7SSteven Rostedt 	ret = get_common_field(context, &size, &offset,
60aaf045f7SSteven Rostedt 			       "common_lock_depth");
61aaf045f7SSteven Rostedt 	if (ret < 0)
62aaf045f7SSteven Rostedt 		return -1;
63aaf045f7SSteven Rostedt 
64aaf045f7SSteven Rostedt 	return ret;
65aaf045f7SSteven Rostedt }
66aaf045f7SSteven Rostedt 
67aaf045f7SSteven Rostedt int common_flags(struct scripting_context *context)
68aaf045f7SSteven Rostedt {
69aaf045f7SSteven Rostedt 	static int offset;
70aaf045f7SSteven Rostedt 	static int size;
71aaf045f7SSteven Rostedt 	int ret;
72aaf045f7SSteven Rostedt 
73aaf045f7SSteven Rostedt 	ret = get_common_field(context, &size, &offset,
74aaf045f7SSteven Rostedt 			       "common_flags");
75aaf045f7SSteven Rostedt 	if (ret < 0)
76aaf045f7SSteven Rostedt 		return -1;
77aaf045f7SSteven Rostedt 
78aaf045f7SSteven Rostedt 	return ret;
79aaf045f7SSteven Rostedt }
80aaf045f7SSteven Rostedt 
81aaf045f7SSteven Rostedt int common_pc(struct scripting_context *context)
82aaf045f7SSteven Rostedt {
83aaf045f7SSteven Rostedt 	static int offset;
84aaf045f7SSteven Rostedt 	static int size;
85aaf045f7SSteven Rostedt 	int ret;
86aaf045f7SSteven Rostedt 
87aaf045f7SSteven Rostedt 	ret = get_common_field(context, &size, &offset,
88aaf045f7SSteven Rostedt 			       "common_preempt_count");
89aaf045f7SSteven Rostedt 	if (ret < 0)
90aaf045f7SSteven Rostedt 		return -1;
91aaf045f7SSteven Rostedt 
92aaf045f7SSteven Rostedt 	return ret;
93aaf045f7SSteven Rostedt }
94aaf045f7SSteven Rostedt 
95aaf045f7SSteven Rostedt unsigned long long
96aaf045f7SSteven Rostedt raw_field_value(struct event_format *event, const char *name, void *data)
97aaf045f7SSteven Rostedt {
98aaf045f7SSteven Rostedt 	struct format_field *field;
99aaf045f7SSteven Rostedt 	unsigned long long val;
100aaf045f7SSteven Rostedt 
101aaf045f7SSteven Rostedt 	field = pevent_find_any_field(event, name);
102aaf045f7SSteven Rostedt 	if (!field)
103aaf045f7SSteven Rostedt 		return 0ULL;
104aaf045f7SSteven Rostedt 
105aaf045f7SSteven Rostedt 	pevent_read_number_field(field, data, &val);
106aaf045f7SSteven Rostedt 
107aaf045f7SSteven Rostedt 	return val;
108aaf045f7SSteven Rostedt }
109aaf045f7SSteven Rostedt 
11097822433SArnaldo Carvalho de Melo unsigned long long read_size(struct event_format *event, void *ptr, int size)
111aaf045f7SSteven Rostedt {
11297822433SArnaldo Carvalho de Melo 	return pevent_read_number(event->pevent, ptr, size);
113aaf045f7SSteven Rostedt }
114aaf045f7SSteven Rostedt 
115fcf65bf1SArnaldo Carvalho de Melo void event_format__print(struct event_format *event,
116fcf65bf1SArnaldo Carvalho de Melo 			 int cpu, void *data, int size)
117aaf045f7SSteven Rostedt {
1181c698186SSteven Rostedt 	struct pevent_record record;
119aaf045f7SSteven Rostedt 	struct trace_seq s;
120aaf045f7SSteven Rostedt 
121aaf045f7SSteven Rostedt 	memset(&record, 0, sizeof(record));
122aaf045f7SSteven Rostedt 	record.cpu = cpu;
123aaf045f7SSteven Rostedt 	record.size = size;
124aaf045f7SSteven Rostedt 	record.data = data;
125aaf045f7SSteven Rostedt 
126aaf045f7SSteven Rostedt 	trace_seq_init(&s);
12776a8349dSDavid Ahern 	pevent_event_info(&s, event, &record);
128aaf045f7SSteven Rostedt 	trace_seq_do_printf(&s);
129b58f608eSJiri Olsa 	trace_seq_destroy(&s);
130aaf045f7SSteven Rostedt }
131aaf045f7SSteven Rostedt 
132da378962SArnaldo Carvalho de Melo void parse_proc_kallsyms(struct pevent *pevent,
1331d037ca1SIrina Tirdea 			 char *file, unsigned int size __maybe_unused)
134aaf045f7SSteven Rostedt {
135aaf045f7SSteven Rostedt 	unsigned long long addr;
136aaf045f7SSteven Rostedt 	char *func;
137aaf045f7SSteven Rostedt 	char *line;
138aaf045f7SSteven Rostedt 	char *next = NULL;
139aaf045f7SSteven Rostedt 	char *addr_str;
140aaf045f7SSteven Rostedt 	char *mod;
1410f965429SIngo Molnar 	char *fmt = NULL;
142aaf045f7SSteven Rostedt 
143aaf045f7SSteven Rostedt 	line = strtok_r(file, "\n", &next);
144aaf045f7SSteven Rostedt 	while (line) {
145aaf045f7SSteven Rostedt 		mod = NULL;
146bcbd0040SIrina Tirdea 		addr_str = strtok_r(line, " ", &fmt);
147aaf045f7SSteven Rostedt 		addr = strtoull(addr_str, NULL, 16);
148bcbd0040SIrina Tirdea 		/* skip character */
149bcbd0040SIrina Tirdea 		strtok_r(NULL, " ", &fmt);
150bcbd0040SIrina Tirdea 		func = strtok_r(NULL, "\t", &fmt);
151bcbd0040SIrina Tirdea 		mod = strtok_r(NULL, "]", &fmt);
152bcbd0040SIrina Tirdea 		/* truncate the extra '[' */
153aaf045f7SSteven Rostedt 		if (mod)
154bcbd0040SIrina Tirdea 			mod = mod + 1;
155aaf045f7SSteven Rostedt 
156aaf045f7SSteven Rostedt 		pevent_register_function(pevent, func, addr, mod);
157aaf045f7SSteven Rostedt 
158aaf045f7SSteven Rostedt 		line = strtok_r(NULL, "\n", &next);
159aaf045f7SSteven Rostedt 	}
160aaf045f7SSteven Rostedt }
161aaf045f7SSteven Rostedt 
162da378962SArnaldo Carvalho de Melo void parse_ftrace_printk(struct pevent *pevent,
1631d037ca1SIrina Tirdea 			 char *file, unsigned int size __maybe_unused)
164aaf045f7SSteven Rostedt {
165aaf045f7SSteven Rostedt 	unsigned long long addr;
166aaf045f7SSteven Rostedt 	char *printk;
167aaf045f7SSteven Rostedt 	char *line;
168aaf045f7SSteven Rostedt 	char *next = NULL;
169aaf045f7SSteven Rostedt 	char *addr_str;
170aaf045f7SSteven Rostedt 	char *fmt;
171aaf045f7SSteven Rostedt 
172aaf045f7SSteven Rostedt 	line = strtok_r(file, "\n", &next);
173aaf045f7SSteven Rostedt 	while (line) {
174aaf045f7SSteven Rostedt 		addr_str = strtok_r(line, ":", &fmt);
175aaf045f7SSteven Rostedt 		if (!addr_str) {
176aaf045f7SSteven Rostedt 			warning("printk format with empty entry");
177aaf045f7SSteven Rostedt 			break;
178aaf045f7SSteven Rostedt 		}
179aaf045f7SSteven Rostedt 		addr = strtoull(addr_str, NULL, 16);
180aaf045f7SSteven Rostedt 		/* fmt still has a space, skip it */
181aaf045f7SSteven Rostedt 		printk = strdup(fmt+1);
182aaf045f7SSteven Rostedt 		line = strtok_r(NULL, "\n", &next);
183aaf045f7SSteven Rostedt 		pevent_register_print_string(pevent, printk, addr);
184aaf045f7SSteven Rostedt 	}
185aaf045f7SSteven Rostedt }
186aaf045f7SSteven Rostedt 
187da378962SArnaldo Carvalho de Melo int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
188aaf045f7SSteven Rostedt {
189aaf045f7SSteven Rostedt 	return pevent_parse_event(pevent, buf, size, "ftrace");
190aaf045f7SSteven Rostedt }
191aaf045f7SSteven Rostedt 
192da378962SArnaldo Carvalho de Melo int parse_event_file(struct pevent *pevent,
193da378962SArnaldo Carvalho de Melo 		     char *buf, unsigned long size, char *sys)
194aaf045f7SSteven Rostedt {
195aaf045f7SSteven Rostedt 	return pevent_parse_event(pevent, buf, size, sys);
196aaf045f7SSteven Rostedt }
197aaf045f7SSteven Rostedt 
198da378962SArnaldo Carvalho de Melo struct event_format *trace_find_next_event(struct pevent *pevent,
199da378962SArnaldo Carvalho de Melo 					   struct event_format *event)
200aaf045f7SSteven Rostedt {
201aaf045f7SSteven Rostedt 	static int idx;
202aaf045f7SSteven Rostedt 
203d0d39138SNamhyung Kim 	if (!pevent || !pevent->events)
204aaf045f7SSteven Rostedt 		return NULL;
205aaf045f7SSteven Rostedt 
206aaf045f7SSteven Rostedt 	if (!event) {
207aaf045f7SSteven Rostedt 		idx = 0;
208aaf045f7SSteven Rostedt 		return pevent->events[0];
209aaf045f7SSteven Rostedt 	}
210aaf045f7SSteven Rostedt 
211aaf045f7SSteven Rostedt 	if (idx < pevent->nr_events && event == pevent->events[idx]) {
212aaf045f7SSteven Rostedt 		idx++;
213aaf045f7SSteven Rostedt 		if (idx == pevent->nr_events)
214aaf045f7SSteven Rostedt 			return NULL;
215aaf045f7SSteven Rostedt 		return pevent->events[idx];
216aaf045f7SSteven Rostedt 	}
217aaf045f7SSteven Rostedt 
218aaf045f7SSteven Rostedt 	for (idx = 1; idx < pevent->nr_events; idx++) {
219aaf045f7SSteven Rostedt 		if (event == pevent->events[idx - 1])
220aaf045f7SSteven Rostedt 			return pevent->events[idx];
221aaf045f7SSteven Rostedt 	}
222aaf045f7SSteven Rostedt 	return NULL;
223aaf045f7SSteven Rostedt }
224aaf045f7SSteven Rostedt 
225aaf045f7SSteven Rostedt struct flag {
226aaf045f7SSteven Rostedt 	const char *name;
227aaf045f7SSteven Rostedt 	unsigned long long value;
228aaf045f7SSteven Rostedt };
229aaf045f7SSteven Rostedt 
230aaf045f7SSteven Rostedt static const struct flag flags[] = {
231aaf045f7SSteven Rostedt 	{ "HI_SOFTIRQ", 0 },
232aaf045f7SSteven Rostedt 	{ "TIMER_SOFTIRQ", 1 },
233aaf045f7SSteven Rostedt 	{ "NET_TX_SOFTIRQ", 2 },
234aaf045f7SSteven Rostedt 	{ "NET_RX_SOFTIRQ", 3 },
235aaf045f7SSteven Rostedt 	{ "BLOCK_SOFTIRQ", 4 },
236aaf045f7SSteven Rostedt 	{ "BLOCK_IOPOLL_SOFTIRQ", 5 },
237aaf045f7SSteven Rostedt 	{ "TASKLET_SOFTIRQ", 6 },
238aaf045f7SSteven Rostedt 	{ "SCHED_SOFTIRQ", 7 },
239aaf045f7SSteven Rostedt 	{ "HRTIMER_SOFTIRQ", 8 },
240aaf045f7SSteven Rostedt 	{ "RCU_SOFTIRQ", 9 },
241aaf045f7SSteven Rostedt 
242aaf045f7SSteven Rostedt 	{ "HRTIMER_NORESTART", 0 },
243aaf045f7SSteven Rostedt 	{ "HRTIMER_RESTART", 1 },
244aaf045f7SSteven Rostedt };
245aaf045f7SSteven Rostedt 
246aaf045f7SSteven Rostedt unsigned long long eval_flag(const char *flag)
247aaf045f7SSteven Rostedt {
248aaf045f7SSteven Rostedt 	int i;
249aaf045f7SSteven Rostedt 
250aaf045f7SSteven Rostedt 	/*
251aaf045f7SSteven Rostedt 	 * Some flags in the format files do not get converted.
252aaf045f7SSteven Rostedt 	 * If the flag is not numeric, see if it is something that
253aaf045f7SSteven Rostedt 	 * we already know about.
254aaf045f7SSteven Rostedt 	 */
255aaf045f7SSteven Rostedt 	if (isdigit(flag[0]))
256aaf045f7SSteven Rostedt 		return strtoull(flag, NULL, 0);
257aaf045f7SSteven Rostedt 
258aaf045f7SSteven Rostedt 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
259aaf045f7SSteven Rostedt 		if (strcmp(flags[i].name, flag) == 0)
260aaf045f7SSteven Rostedt 			return flags[i].value;
261aaf045f7SSteven Rostedt 
262aaf045f7SSteven Rostedt 	return 0;
2637397d80dSTom Zanussi }
264