xref: /linux/tools/perf/util/trace-event-parse.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
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 <errno.h>
25 
26 #include "../perf.h"
27 #include "debug.h"
28 #include "trace-event.h"
29 
30 #include "sane_ctype.h"
31 
32 static int get_common_field(struct scripting_context *context,
33 			    int *offset, int *size, const char *type)
34 {
35 	struct tep_handle *pevent = context->pevent;
36 	struct tep_event *event;
37 	struct tep_format_field *field;
38 
39 	if (!*size) {
40 
41 		event = tep_get_first_event(pevent);
42 		if (!event)
43 			return 0;
44 
45 		field = tep_find_common_field(event, type);
46 		if (!field)
47 			return 0;
48 		*offset = field->offset;
49 		*size = field->size;
50 	}
51 
52 	return tep_read_number(pevent, context->event_data + *offset, *size);
53 }
54 
55 int common_lock_depth(struct scripting_context *context)
56 {
57 	static int offset;
58 	static int size;
59 	int ret;
60 
61 	ret = get_common_field(context, &size, &offset,
62 			       "common_lock_depth");
63 	if (ret < 0)
64 		return -1;
65 
66 	return ret;
67 }
68 
69 int common_flags(struct scripting_context *context)
70 {
71 	static int offset;
72 	static int size;
73 	int ret;
74 
75 	ret = get_common_field(context, &size, &offset,
76 			       "common_flags");
77 	if (ret < 0)
78 		return -1;
79 
80 	return ret;
81 }
82 
83 int common_pc(struct scripting_context *context)
84 {
85 	static int offset;
86 	static int size;
87 	int ret;
88 
89 	ret = get_common_field(context, &size, &offset,
90 			       "common_preempt_count");
91 	if (ret < 0)
92 		return -1;
93 
94 	return ret;
95 }
96 
97 unsigned long long
98 raw_field_value(struct tep_event *event, const char *name, void *data)
99 {
100 	struct tep_format_field *field;
101 	unsigned long long val;
102 
103 	field = tep_find_any_field(event, name);
104 	if (!field)
105 		return 0ULL;
106 
107 	tep_read_number_field(field, data, &val);
108 
109 	return val;
110 }
111 
112 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
113 {
114 	return tep_read_number(event->pevent, ptr, size);
115 }
116 
117 void event_format__fprintf(struct tep_event *event,
118 			   int cpu, void *data, int size, FILE *fp)
119 {
120 	struct tep_record record;
121 	struct trace_seq s;
122 
123 	memset(&record, 0, sizeof(record));
124 	record.cpu = cpu;
125 	record.size = size;
126 	record.data = data;
127 
128 	trace_seq_init(&s);
129 	tep_event_info(&s, event, &record);
130 	trace_seq_do_fprintf(&s, fp);
131 	trace_seq_destroy(&s);
132 }
133 
134 void event_format__print(struct tep_event *event,
135 			 int cpu, void *data, int size)
136 {
137 	return event_format__fprintf(event, cpu, data, size, stdout);
138 }
139 
140 void parse_ftrace_printk(struct tep_handle *pevent,
141 			 char *file, unsigned int size __maybe_unused)
142 {
143 	unsigned long long addr;
144 	char *printk;
145 	char *line;
146 	char *next = NULL;
147 	char *addr_str;
148 	char *fmt = NULL;
149 
150 	line = strtok_r(file, "\n", &next);
151 	while (line) {
152 		addr_str = strtok_r(line, ":", &fmt);
153 		if (!addr_str) {
154 			pr_warning("printk format with empty entry");
155 			break;
156 		}
157 		addr = strtoull(addr_str, NULL, 16);
158 		/* fmt still has a space, skip it */
159 		printk = strdup(fmt+1);
160 		line = strtok_r(NULL, "\n", &next);
161 		tep_register_print_string(pevent, printk, addr);
162 		free(printk);
163 	}
164 }
165 
166 void parse_saved_cmdline(struct tep_handle *pevent,
167 			 char *file, unsigned int size __maybe_unused)
168 {
169 	char comm[17]; /* Max comm length in the kernel is 16. */
170 	char *line;
171 	char *next = NULL;
172 	int pid;
173 
174 	line = strtok_r(file, "\n", &next);
175 	while (line) {
176 		if (sscanf(line, "%d %16s", &pid, comm) == 2)
177 			tep_register_comm(pevent, comm, pid);
178 		line = strtok_r(NULL, "\n", &next);
179 	}
180 }
181 
182 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
183 {
184 	return tep_parse_event(pevent, buf, size, "ftrace");
185 }
186 
187 int parse_event_file(struct tep_handle *pevent,
188 		     char *buf, unsigned long size, char *sys)
189 {
190 	return tep_parse_event(pevent, buf, size, sys);
191 }
192 
193 struct tep_event *trace_find_next_event(struct tep_handle *pevent,
194 					struct tep_event *event)
195 {
196 	static int idx;
197 	int events_count;
198 	struct tep_event *all_events;
199 
200 	all_events = tep_get_first_event(pevent);
201 	events_count = tep_get_events_count(pevent);
202 	if (!pevent || !all_events || events_count < 1)
203 		return NULL;
204 
205 	if (!event) {
206 		idx = 0;
207 		return all_events;
208 	}
209 
210 	if (idx < events_count && event == (all_events + idx)) {
211 		idx++;
212 		if (idx == events_count)
213 			return NULL;
214 		return (all_events + idx);
215 	}
216 
217 	for (idx = 1; idx < events_count; idx++) {
218 		if (event == (all_events + (idx - 1)))
219 			return (all_events + idx);
220 	}
221 	return NULL;
222 }
223 
224 struct flag {
225 	const char *name;
226 	unsigned long long value;
227 };
228 
229 static const struct flag flags[] = {
230 	{ "HI_SOFTIRQ", 0 },
231 	{ "TIMER_SOFTIRQ", 1 },
232 	{ "NET_TX_SOFTIRQ", 2 },
233 	{ "NET_RX_SOFTIRQ", 3 },
234 	{ "BLOCK_SOFTIRQ", 4 },
235 	{ "IRQ_POLL_SOFTIRQ", 5 },
236 	{ "TASKLET_SOFTIRQ", 6 },
237 	{ "SCHED_SOFTIRQ", 7 },
238 	{ "HRTIMER_SOFTIRQ", 8 },
239 	{ "RCU_SOFTIRQ", 9 },
240 
241 	{ "HRTIMER_NORESTART", 0 },
242 	{ "HRTIMER_RESTART", 1 },
243 };
244 
245 unsigned long long eval_flag(const char *flag)
246 {
247 	int i;
248 
249 	/*
250 	 * Some flags in the format files do not get converted.
251 	 * If the flag is not numeric, see if it is something that
252 	 * we already know about.
253 	 */
254 	if (isdigit(flag[0]))
255 		return strtoull(flag, NULL, 0);
256 
257 	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
258 		if (strcmp(flags[i].name, flag) == 0)
259 			return flags[i].value;
260 
261 	return 0;
262 }
263