Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / trace-event-parse.c
blobad74be1f0e4208ab04e9425464f1a977b82db02f
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 tep_handle *pevent = context->pevent;
36 struct tep_event *event;
37 struct tep_format_field *field;
39 if (!*size) {
41 event = tep_get_first_event(pevent);
42 if (!event)
43 return 0;
45 field = tep_find_common_field(event, type);
46 if (!field)
47 return 0;
48 *offset = field->offset;
49 *size = field->size;
52 return tep_read_number(pevent, context->event_data + *offset, *size);
55 int common_lock_depth(struct scripting_context *context)
57 static int offset;
58 static int size;
59 int ret;
61 ret = get_common_field(context, &size, &offset,
62 "common_lock_depth");
63 if (ret < 0)
64 return -1;
66 return ret;
69 int common_flags(struct scripting_context *context)
71 static int offset;
72 static int size;
73 int ret;
75 ret = get_common_field(context, &size, &offset,
76 "common_flags");
77 if (ret < 0)
78 return -1;
80 return ret;
83 int common_pc(struct scripting_context *context)
85 static int offset;
86 static int size;
87 int ret;
89 ret = get_common_field(context, &size, &offset,
90 "common_preempt_count");
91 if (ret < 0)
92 return -1;
94 return ret;
97 unsigned long long
98 raw_field_value(struct tep_event *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);
104 if (!field)
105 return 0ULL;
107 tep_read_number_field(field, data, &val);
109 return val;
112 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
114 return tep_read_number(event->pevent, ptr, size);
117 void event_format__fprintf(struct tep_event *event,
118 int cpu, void *data, int size, FILE *fp)
120 struct tep_record record;
121 struct trace_seq s;
123 memset(&record, 0, sizeof(record));
124 record.cpu = cpu;
125 record.size = size;
126 record.data = data;
128 trace_seq_init(&s);
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 *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;
144 char *printk;
145 char *line;
146 char *next = NULL;
147 char *addr_str;
148 char *fmt = NULL;
150 line = strtok_r(file, "\n", &next);
151 while (line) {
152 addr_str = strtok_r(line, ":", &fmt);
153 if (!addr_str) {
154 pr_warning("printk format with empty entry");
155 break;
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);
162 free(printk);
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. */
170 char *line;
171 char *next = NULL;
172 int pid;
174 line = strtok_r(file, "\n", &next);
175 while (line) {
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 *trace_find_next_event(struct tep_handle *pevent,
194 struct tep_event *event)
196 static int idx;
197 int events_count;
198 struct tep_event *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)
203 return NULL;
205 if (!event) {
206 idx = 0;
207 return all_events;
210 if (idx < events_count && event == (all_events + idx)) {
211 idx++;
212 if (idx == events_count)
213 return NULL;
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);
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 { "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)
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;