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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include "trace-event.h"
30 #include "sane_ctype.h"
32 static int get_common_field(struct scripting_context *context,
33 int *offset, int *size, const char *type)
35 struct tep_handle *pevent = context->pevent;
36 struct tep_event_format *event;
37 struct tep_format_field *field;
41 event = tep_get_first_event(pevent);
45 field = tep_find_common_field(event, type);
48 *offset = field->offset;
52 return tep_read_number(pevent, context->event_data + *offset, *size);
55 int common_lock_depth(struct scripting_context *context)
61 ret = get_common_field(context, &size, &offset,
69 int common_flags(struct scripting_context *context)
75 ret = get_common_field(context, &size, &offset,
83 int common_pc(struct scripting_context *context)
89 ret = get_common_field(context, &size, &offset,
90 "common_preempt_count");
98 raw_field_value(struct tep_event_format *event, const char *name, void *data)
100 struct tep_format_field *field;
101 unsigned long long val;
103 field = tep_find_any_field(event, name);
107 tep_read_number_field(field, data, &val);
112 unsigned long long read_size(struct tep_event_format *event, void *ptr, int size)
114 return tep_read_number(event->pevent, ptr, size);
117 void event_format__fprintf(struct tep_event_format *event,
118 int cpu, void *data, int size, FILE *fp)
120 struct tep_record record;
123 memset(&record, 0, sizeof(record));
129 tep_event_info(&s, event, &record);
130 trace_seq_do_fprintf(&s, fp);
131 trace_seq_destroy(&s);
134 void event_format__print(struct tep_event_format *event,
135 int cpu, void *data, int size)
137 return event_format__fprintf(event, cpu, data, size, stdout);
140 void parse_ftrace_printk(struct tep_handle *pevent,
141 char *file, unsigned int size __maybe_unused)
143 unsigned long long addr;
150 line = strtok_r(file, "\n", &next);
152 addr_str = strtok_r(line, ":", &fmt);
154 pr_warning("printk format with empty entry");
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);
166 void parse_saved_cmdline(struct tep_handle *pevent,
167 char *file, unsigned int size __maybe_unused)
169 char comm[17]; /* Max comm length in the kernel is 16. */
174 line = strtok_r(file, "\n", &next);
176 if (sscanf(line, "%d %16s", &pid, comm) == 2)
177 tep_register_comm(pevent, comm, pid);
178 line = strtok_r(NULL, "\n", &next);
182 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
184 return tep_parse_event(pevent, buf, size, "ftrace");
187 int parse_event_file(struct tep_handle *pevent,
188 char *buf, unsigned long size, char *sys)
190 return tep_parse_event(pevent, buf, size, sys);
193 struct tep_event_format *trace_find_next_event(struct tep_handle *pevent,
194 struct tep_event_format *event)
198 struct tep_event_format *all_events;
200 all_events = tep_get_first_event(pevent);
201 events_count = tep_get_events_count(pevent);
202 if (!pevent || !all_events || events_count < 1)
210 if (idx < events_count && event == (all_events + idx)) {
212 if (idx == events_count)
214 return (all_events + idx);
217 for (idx = 1; idx < events_count; idx++) {
218 if (event == (all_events + (idx - 1)))
219 return (all_events + idx);
226 unsigned long long value;
229 static const struct flag flags[] = {
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 },
241 { "HRTIMER_NORESTART", 0 },
242 { "HRTIMER_RESTART", 1 },
245 unsigned long long eval_flag(const char *flag)
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;