blob: 4454835a9ebccd5d826d3f054d8d3f1a25ffc109 [file] [log] [blame]
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001/*
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
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020027#include "../perf.h"
Steven Rostedtea4010d2009-08-17 16:18:07 +020028#include "util.h"
29#include "trace-event.h"
30
Steven Rostedtaaf045f2012-04-06 00:47:56 +020031int header_page_size_size;
32int header_page_ts_size;
33int header_page_data_offset;
34
Steven Rostedtaaf045f2012-04-06 00:47:56 +020035bool latency_format;
36
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030037struct pevent *read_trace_init(int file_bigendian, int host_bigendian)
Tom Zanussi7397d802010-01-27 02:27:54 -060038{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030039 struct pevent *pevent = pevent_alloc();
Steven Rostedtaaf045f2012-04-06 00:47:56 +020040
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030041 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 }
Steven Rostedtaaf045f2012-04-06 00:47:56 +020046
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030047 return pevent;
Tom Zanussi7397d802010-01-27 02:27:54 -060048}
49
Steven Rostedtaaf045f2012-04-06 00:47:56 +020050static int get_common_field(struct scripting_context *context,
51 int *offset, int *size, const char *type)
Tom Zanussi7397d802010-01-27 02:27:54 -060052{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030053 struct pevent *pevent = context->pevent;
Steven Rostedtaaf045f2012-04-06 00:47:56 +020054 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);
Tom Zanussi7397d802010-01-27 02:27:54 -060070}
71
72int common_lock_depth(struct scripting_context *context)
73{
Steven Rostedtaaf045f2012-04-06 00:47:56 +020074 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
86int 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
100int 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
114unsigned long long
115raw_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
129void *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
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300149int trace_parse_common_type(struct pevent *pevent, void *data)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200150{
Steven Rostedt1c698182012-04-06 00:48:06 +0200151 struct pevent_record record;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200152
153 record.data = data;
154 return pevent_data_type(pevent, &record);
155}
156
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300157int trace_parse_common_pid(struct pevent *pevent, void *data)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200158{
Steven Rostedt1c698182012-04-06 00:48:06 +0200159 struct pevent_record record;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200160
161 record.data = data;
162 return pevent_data_pid(pevent, &record);
163}
164
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300165unsigned long long read_size(struct event_format *event, void *ptr, int size)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200166{
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300167 return pevent_read_number(event->pevent, ptr, size);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200168}
169
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300170void event_format__print(struct event_format *event,
171 int cpu, void *data, int size)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200172{
Steven Rostedt1c698182012-04-06 00:48:06 +0200173 struct pevent_record record;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200174 struct trace_seq s;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200175
176 memset(&record, 0, sizeof(record));
177 record.cpu = cpu;
178 record.size = size;
179 record.data = data;
180
181 trace_seq_init(&s);
David Ahern76a83492012-06-14 12:36:17 -0600182 pevent_event_info(&s, event, &record);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200183 trace_seq_do_printf(&s);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200184}
185
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300186void parse_proc_kallsyms(struct pevent *pevent,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300187 char *file, unsigned int size __maybe_unused)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200188{
189 unsigned long long addr;
190 char *func;
191 char *line;
192 char *next = NULL;
193 char *addr_str;
194 char *mod;
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300195 char *fmt;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200196
197 line = strtok_r(file, "\n", &next);
198 while (line) {
199 mod = NULL;
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300200 addr_str = strtok_r(line, " ", &fmt);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200201 addr = strtoull(addr_str, NULL, 16);
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300202 /* skip character */
203 strtok_r(NULL, " ", &fmt);
204 func = strtok_r(NULL, "\t", &fmt);
205 mod = strtok_r(NULL, "]", &fmt);
206 /* truncate the extra '[' */
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200207 if (mod)
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300208 mod = mod + 1;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200209
210 pevent_register_function(pevent, func, addr, mod);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200211
212 line = strtok_r(NULL, "\n", &next);
213 }
214}
215
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300216void parse_ftrace_printk(struct pevent *pevent,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300217 char *file, unsigned int size __maybe_unused)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200218{
219 unsigned long long addr;
220 char *printk;
221 char *line;
222 char *next = NULL;
223 char *addr_str;
224 char *fmt;
225
226 line = strtok_r(file, "\n", &next);
227 while (line) {
228 addr_str = strtok_r(line, ":", &fmt);
229 if (!addr_str) {
230 warning("printk format with empty entry");
231 break;
232 }
233 addr = strtoull(addr_str, NULL, 16);
234 /* fmt still has a space, skip it */
235 printk = strdup(fmt+1);
236 line = strtok_r(NULL, "\n", &next);
237 pevent_register_print_string(pevent, printk, addr);
238 }
239}
240
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300241int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200242{
243 return pevent_parse_event(pevent, buf, size, "ftrace");
244}
245
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300246int parse_event_file(struct pevent *pevent,
247 char *buf, unsigned long size, char *sys)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200248{
249 return pevent_parse_event(pevent, buf, size, sys);
250}
251
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300252struct event_format *trace_find_next_event(struct pevent *pevent,
253 struct event_format *event)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200254{
255 static int idx;
256
Namhyung Kimd0d39132012-08-14 10:57:03 +0900257 if (!pevent || !pevent->events)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200258 return NULL;
259
260 if (!event) {
261 idx = 0;
262 return pevent->events[0];
263 }
264
265 if (idx < pevent->nr_events && event == pevent->events[idx]) {
266 idx++;
267 if (idx == pevent->nr_events)
268 return NULL;
269 return pevent->events[idx];
270 }
271
272 for (idx = 1; idx < pevent->nr_events; idx++) {
273 if (event == pevent->events[idx - 1])
274 return pevent->events[idx];
275 }
276 return NULL;
277}
278
279struct flag {
280 const char *name;
281 unsigned long long value;
282};
283
284static const struct flag flags[] = {
285 { "HI_SOFTIRQ", 0 },
286 { "TIMER_SOFTIRQ", 1 },
287 { "NET_TX_SOFTIRQ", 2 },
288 { "NET_RX_SOFTIRQ", 3 },
289 { "BLOCK_SOFTIRQ", 4 },
290 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
291 { "TASKLET_SOFTIRQ", 6 },
292 { "SCHED_SOFTIRQ", 7 },
293 { "HRTIMER_SOFTIRQ", 8 },
294 { "RCU_SOFTIRQ", 9 },
295
296 { "HRTIMER_NORESTART", 0 },
297 { "HRTIMER_RESTART", 1 },
298};
299
300unsigned long long eval_flag(const char *flag)
301{
302 int i;
303
304 /*
305 * Some flags in the format files do not get converted.
306 * If the flag is not numeric, see if it is something that
307 * we already know about.
308 */
309 if (isdigit(flag[0]))
310 return strtoull(flag, NULL, 0);
311
312 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
313 if (strcmp(flags[i].name, flag) == 0)
314 return flags[i].value;
315
316 return 0;
Tom Zanussi7397d802010-01-27 02:27:54 -0600317}