perf annotate: Split allocation of annotated_source struct
[linux/fpc-iii.git] / tools / perf / util / trace-event-parse.c
blobe0a6e9a6a05355bc43e99585ec3a3f134e65b1e8
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 <errno.h>
26 #include "../perf.h"
27 #include "debug.h"
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 pevent *pevent = context->pevent;
36 struct event_format *event;
37 struct format_field *field;
39 if (!*size) {
40 if (!pevent->events)
41 return 0;
43 event = pevent->events[0];
44 field = pevent_find_common_field(event, type);
45 if (!field)
46 return 0;
47 *offset = field->offset;
48 *size = field->size;
51 return pevent_read_number(pevent, context->event_data + *offset, *size);
54 int common_lock_depth(struct scripting_context *context)
56 static int offset;
57 static int size;
58 int ret;
60 ret = get_common_field(context, &size, &offset,
61 "common_lock_depth");
62 if (ret < 0)
63 return -1;
65 return ret;
68 int common_flags(struct scripting_context *context)
70 static int offset;
71 static int size;
72 int ret;
74 ret = get_common_field(context, &size, &offset,
75 "common_flags");
76 if (ret < 0)
77 return -1;
79 return ret;
82 int common_pc(struct scripting_context *context)
84 static int offset;
85 static int size;
86 int ret;
88 ret = get_common_field(context, &size, &offset,
89 "common_preempt_count");
90 if (ret < 0)
91 return -1;
93 return ret;
96 unsigned long long
97 raw_field_value(struct event_format *event, const char *name, void *data)
99 struct format_field *field;
100 unsigned long long val;
102 field = pevent_find_any_field(event, name);
103 if (!field)
104 return 0ULL;
106 pevent_read_number_field(field, data, &val);
108 return val;
111 unsigned long long read_size(struct event_format *event, void *ptr, int size)
113 return pevent_read_number(event->pevent, ptr, size);
116 void event_format__fprintf(struct event_format *event,
117 int cpu, void *data, int size, FILE *fp)
119 struct pevent_record record;
120 struct trace_seq s;
122 memset(&record, 0, sizeof(record));
123 record.cpu = cpu;
124 record.size = size;
125 record.data = data;
127 trace_seq_init(&s);
128 pevent_event_info(&s, event, &record);
129 trace_seq_do_fprintf(&s, fp);
130 trace_seq_destroy(&s);
133 void event_format__print(struct event_format *event,
134 int cpu, void *data, int size)
136 return event_format__fprintf(event, cpu, data, size, stdout);
139 void parse_ftrace_printk(struct pevent *pevent,
140 char *file, unsigned int size __maybe_unused)
142 unsigned long long addr;
143 char *printk;
144 char *line;
145 char *next = NULL;
146 char *addr_str;
147 char *fmt = NULL;
149 line = strtok_r(file, "\n", &next);
150 while (line) {
151 addr_str = strtok_r(line, ":", &fmt);
152 if (!addr_str) {
153 pr_warning("printk format with empty entry");
154 break;
156 addr = strtoull(addr_str, NULL, 16);
157 /* fmt still has a space, skip it */
158 printk = strdup(fmt+1);
159 line = strtok_r(NULL, "\n", &next);
160 pevent_register_print_string(pevent, printk, addr);
164 void parse_saved_cmdline(struct pevent *pevent,
165 char *file, unsigned int size __maybe_unused)
167 char *comm;
168 char *line;
169 char *next = NULL;
170 int pid;
172 line = strtok_r(file, "\n", &next);
173 while (line) {
174 sscanf(line, "%d %ms", &pid, &comm);
175 pevent_register_comm(pevent, comm, pid);
176 free(comm);
177 line = strtok_r(NULL, "\n", &next);
181 int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
183 return pevent_parse_event(pevent, buf, size, "ftrace");
186 int parse_event_file(struct pevent *pevent,
187 char *buf, unsigned long size, char *sys)
189 return pevent_parse_event(pevent, buf, size, sys);
192 struct event_format *trace_find_next_event(struct pevent *pevent,
193 struct event_format *event)
195 static int idx;
197 if (!pevent || !pevent->events)
198 return NULL;
200 if (!event) {
201 idx = 0;
202 return pevent->events[0];
205 if (idx < pevent->nr_events && event == pevent->events[idx]) {
206 idx++;
207 if (idx == pevent->nr_events)
208 return NULL;
209 return pevent->events[idx];
212 for (idx = 1; idx < pevent->nr_events; idx++) {
213 if (event == pevent->events[idx - 1])
214 return pevent->events[idx];
216 return NULL;
219 struct flag {
220 const char *name;
221 unsigned long long value;
224 static const struct flag flags[] = {
225 { "HI_SOFTIRQ", 0 },
226 { "TIMER_SOFTIRQ", 1 },
227 { "NET_TX_SOFTIRQ", 2 },
228 { "NET_RX_SOFTIRQ", 3 },
229 { "BLOCK_SOFTIRQ", 4 },
230 { "IRQ_POLL_SOFTIRQ", 5 },
231 { "TASKLET_SOFTIRQ", 6 },
232 { "SCHED_SOFTIRQ", 7 },
233 { "HRTIMER_SOFTIRQ", 8 },
234 { "RCU_SOFTIRQ", 9 },
236 { "HRTIMER_NORESTART", 0 },
237 { "HRTIMER_RESTART", 1 },
240 unsigned long long eval_flag(const char *flag)
242 int i;
245 * Some flags in the format files do not get converted.
246 * If the flag is not numeric, see if it is something that
247 * we already know about.
249 if (isdigit(flag[0]))
250 return strtoull(flag, NULL, 0);
252 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
253 if (strcmp(flags[i].name, flag) == 0)
254 return flags[i].value;
256 return 0;