Merge branch 'x86-apic-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / tools / perf / util / trace-event-parse.c
blob6681f71f2f95e329ddc5eba636d05ce67d3591ee
1 /*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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!)
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.
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
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
27 #include "../perf.h"
28 #include "util.h"
29 #include "trace-event.h"
31 struct pevent *read_trace_init(int file_bigendian, int host_bigendian)
33 struct pevent *pevent = pevent_alloc();
35 if (pevent != NULL) {
36 pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
37 pevent_set_file_bigendian(pevent, file_bigendian);
38 pevent_set_host_bigendian(pevent, host_bigendian);
41 return pevent;
44 static int get_common_field(struct scripting_context *context,
45 int *offset, int *size, const char *type)
47 struct pevent *pevent = context->pevent;
48 struct event_format *event;
49 struct format_field *field;
51 if (!*size) {
52 if (!pevent->events)
53 return 0;
55 event = pevent->events[0];
56 field = pevent_find_common_field(event, type);
57 if (!field)
58 return 0;
59 *offset = field->offset;
60 *size = field->size;
63 return pevent_read_number(pevent, context->event_data + *offset, *size);
66 int common_lock_depth(struct scripting_context *context)
68 static int offset;
69 static int size;
70 int ret;
72 ret = get_common_field(context, &size, &offset,
73 "common_lock_depth");
74 if (ret < 0)
75 return -1;
77 return ret;
80 int common_flags(struct scripting_context *context)
82 static int offset;
83 static int size;
84 int ret;
86 ret = get_common_field(context, &size, &offset,
87 "common_flags");
88 if (ret < 0)
89 return -1;
91 return ret;
94 int common_pc(struct scripting_context *context)
96 static int offset;
97 static int size;
98 int ret;
100 ret = get_common_field(context, &size, &offset,
101 "common_preempt_count");
102 if (ret < 0)
103 return -1;
105 return ret;
108 unsigned long long
109 raw_field_value(struct event_format *event, const char *name, void *data)
111 struct format_field *field;
112 unsigned long long val;
114 field = pevent_find_any_field(event, name);
115 if (!field)
116 return 0ULL;
118 pevent_read_number_field(field, data, &val);
120 return val;
123 unsigned long long read_size(struct event_format *event, void *ptr, int size)
125 return pevent_read_number(event->pevent, ptr, size);
128 void event_format__print(struct event_format *event,
129 int cpu, void *data, int size)
131 struct pevent_record record;
132 struct trace_seq s;
134 memset(&record, 0, sizeof(record));
135 record.cpu = cpu;
136 record.size = size;
137 record.data = data;
139 trace_seq_init(&s);
140 pevent_event_info(&s, event, &record);
141 trace_seq_do_printf(&s);
144 void parse_proc_kallsyms(struct pevent *pevent,
145 char *file, unsigned int size __maybe_unused)
147 unsigned long long addr;
148 char *func;
149 char *line;
150 char *next = NULL;
151 char *addr_str;
152 char *mod;
153 char *fmt = NULL;
155 line = strtok_r(file, "\n", &next);
156 while (line) {
157 mod = NULL;
158 addr_str = strtok_r(line, " ", &fmt);
159 addr = strtoull(addr_str, NULL, 16);
160 /* skip character */
161 strtok_r(NULL, " ", &fmt);
162 func = strtok_r(NULL, "\t", &fmt);
163 mod = strtok_r(NULL, "]", &fmt);
164 /* truncate the extra '[' */
165 if (mod)
166 mod = mod + 1;
168 pevent_register_function(pevent, func, addr, mod);
170 line = strtok_r(NULL, "\n", &next);
174 void parse_ftrace_printk(struct pevent *pevent,
175 char *file, unsigned int size __maybe_unused)
177 unsigned long long addr;
178 char *printk;
179 char *line;
180 char *next = NULL;
181 char *addr_str;
182 char *fmt;
184 line = strtok_r(file, "\n", &next);
185 while (line) {
186 addr_str = strtok_r(line, ":", &fmt);
187 if (!addr_str) {
188 warning("printk format with empty entry");
189 break;
191 addr = strtoull(addr_str, NULL, 16);
192 /* fmt still has a space, skip it */
193 printk = strdup(fmt+1);
194 line = strtok_r(NULL, "\n", &next);
195 pevent_register_print_string(pevent, printk, addr);
199 int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
201 return pevent_parse_event(pevent, buf, size, "ftrace");
204 int parse_event_file(struct pevent *pevent,
205 char *buf, unsigned long size, char *sys)
207 return pevent_parse_event(pevent, buf, size, sys);
210 struct event_format *trace_find_next_event(struct pevent *pevent,
211 struct event_format *event)
213 static int idx;
215 if (!pevent || !pevent->events)
216 return NULL;
218 if (!event) {
219 idx = 0;
220 return pevent->events[0];
223 if (idx < pevent->nr_events && event == pevent->events[idx]) {
224 idx++;
225 if (idx == pevent->nr_events)
226 return NULL;
227 return pevent->events[idx];
230 for (idx = 1; idx < pevent->nr_events; idx++) {
231 if (event == pevent->events[idx - 1])
232 return pevent->events[idx];
234 return NULL;
237 struct flag {
238 const char *name;
239 unsigned long long value;
242 static const struct flag flags[] = {
243 { "HI_SOFTIRQ", 0 },
244 { "TIMER_SOFTIRQ", 1 },
245 { "NET_TX_SOFTIRQ", 2 },
246 { "NET_RX_SOFTIRQ", 3 },
247 { "BLOCK_SOFTIRQ", 4 },
248 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
249 { "TASKLET_SOFTIRQ", 6 },
250 { "SCHED_SOFTIRQ", 7 },
251 { "HRTIMER_SOFTIRQ", 8 },
252 { "RCU_SOFTIRQ", 9 },
254 { "HRTIMER_NORESTART", 0 },
255 { "HRTIMER_RESTART", 1 },
258 unsigned long long eval_flag(const char *flag)
260 int i;
263 * Some flags in the format files do not get converted.
264 * If the flag is not numeric, see if it is something that
265 * we already know about.
267 if (isdigit(flag[0]))
268 return strtoull(flag, NULL, 0);
270 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
271 if (strcmp(flags[i].name, flag) == 0)
272 return flags[i].value;
274 return 0;