fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / trace-event-parse.c
blobe0d6d07f68485167f2b05260fd932a288b42eec9
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 static int get_common_field(struct scripting_context *context,
32 int *offset, int *size, const char *type)
34 struct pevent *pevent = context->pevent;
35 struct event_format *event;
36 struct format_field *field;
38 if (!*size) {
39 if (!pevent->events)
40 return 0;
42 event = pevent->events[0];
43 field = pevent_find_common_field(event, type);
44 if (!field)
45 return 0;
46 *offset = field->offset;
47 *size = field->size;
50 return pevent_read_number(pevent, context->event_data + *offset, *size);
53 int common_lock_depth(struct scripting_context *context)
55 static int offset;
56 static int size;
57 int ret;
59 ret = get_common_field(context, &size, &offset,
60 "common_lock_depth");
61 if (ret < 0)
62 return -1;
64 return ret;
67 int common_flags(struct scripting_context *context)
69 static int offset;
70 static int size;
71 int ret;
73 ret = get_common_field(context, &size, &offset,
74 "common_flags");
75 if (ret < 0)
76 return -1;
78 return ret;
81 int common_pc(struct scripting_context *context)
83 static int offset;
84 static int size;
85 int ret;
87 ret = get_common_field(context, &size, &offset,
88 "common_preempt_count");
89 if (ret < 0)
90 return -1;
92 return ret;
95 unsigned long long
96 raw_field_value(struct event_format *event, const char *name, void *data)
98 struct format_field *field;
99 unsigned long long val;
101 field = pevent_find_any_field(event, name);
102 if (!field)
103 return 0ULL;
105 pevent_read_number_field(field, data, &val);
107 return val;
110 unsigned long long read_size(struct event_format *event, void *ptr, int size)
112 return pevent_read_number(event->pevent, ptr, size);
115 void event_format__print(struct event_format *event,
116 int cpu, void *data, int size)
118 struct pevent_record record;
119 struct trace_seq s;
121 memset(&record, 0, sizeof(record));
122 record.cpu = cpu;
123 record.size = size;
124 record.data = data;
126 trace_seq_init(&s);
127 pevent_event_info(&s, event, &record);
128 trace_seq_do_printf(&s);
131 void parse_proc_kallsyms(struct pevent *pevent,
132 char *file, unsigned int size __maybe_unused)
134 unsigned long long addr;
135 char *func;
136 char *line;
137 char *next = NULL;
138 char *addr_str;
139 char *mod;
140 char *fmt = NULL;
142 line = strtok_r(file, "\n", &next);
143 while (line) {
144 mod = NULL;
145 addr_str = strtok_r(line, " ", &fmt);
146 addr = strtoull(addr_str, NULL, 16);
147 /* skip character */
148 strtok_r(NULL, " ", &fmt);
149 func = strtok_r(NULL, "\t", &fmt);
150 mod = strtok_r(NULL, "]", &fmt);
151 /* truncate the extra '[' */
152 if (mod)
153 mod = mod + 1;
155 pevent_register_function(pevent, func, addr, mod);
157 line = strtok_r(NULL, "\n", &next);
161 void parse_ftrace_printk(struct pevent *pevent,
162 char *file, unsigned int size __maybe_unused)
164 unsigned long long addr;
165 char *printk;
166 char *line;
167 char *next = NULL;
168 char *addr_str;
169 char *fmt;
171 line = strtok_r(file, "\n", &next);
172 while (line) {
173 addr_str = strtok_r(line, ":", &fmt);
174 if (!addr_str) {
175 warning("printk format with empty entry");
176 break;
178 addr = strtoull(addr_str, NULL, 16);
179 /* fmt still has a space, skip it */
180 printk = strdup(fmt+1);
181 line = strtok_r(NULL, "\n", &next);
182 pevent_register_print_string(pevent, printk, addr);
186 int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
188 return pevent_parse_event(pevent, buf, size, "ftrace");
191 int parse_event_file(struct pevent *pevent,
192 char *buf, unsigned long size, char *sys)
194 return pevent_parse_event(pevent, buf, size, sys);
197 struct event_format *trace_find_next_event(struct pevent *pevent,
198 struct event_format *event)
200 static int idx;
202 if (!pevent || !pevent->events)
203 return NULL;
205 if (!event) {
206 idx = 0;
207 return pevent->events[0];
210 if (idx < pevent->nr_events && event == pevent->events[idx]) {
211 idx++;
212 if (idx == pevent->nr_events)
213 return NULL;
214 return pevent->events[idx];
217 for (idx = 1; idx < pevent->nr_events; idx++) {
218 if (event == pevent->events[idx - 1])
219 return pevent->events[idx];
221 return NULL;
224 struct flag {
225 const char *name;
226 unsigned long long value;
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 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
236 { "TASKLET_SOFTIRQ", 6 },
237 { "SCHED_SOFTIRQ", 7 },
238 { "HRTIMER_SOFTIRQ", 8 },
239 { "RCU_SOFTIRQ", 9 },
241 { "HRTIMER_NORESTART", 0 },
242 { "HRTIMER_RESTART", 1 },
245 unsigned long long eval_flag(const char *flag)
247 int i;
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.
254 if (isdigit(flag[0]))
255 return strtoull(flag, NULL, 0);
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;
261 return 0;