blob: 25d6c737be3e673db1904104a83286c5fbda0b40 [file] [log] [blame]
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
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!)
9 *
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.
14 *
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
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25#include <errno.h>
26
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +020027#include "../perf.h"
Steven Rostedtea4010d2009-08-17 16:18:07 +020028#include "util.h"
29#include "trace-event.h"
30
Steven Rostedtaaf045f2012-04-06 00:47:56 +020031static int get_common_field(struct scripting_context *context,
32 int *offset, int *size, const char *type)
Tom Zanussi7397d802010-01-27 02:27:54 -060033{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030034 struct pevent *pevent = context->pevent;
Steven Rostedtaaf045f2012-04-06 00:47:56 +020035 struct event_format *event;
36 struct format_field *field;
37
38 if (!*size) {
39 if (!pevent->events)
40 return 0;
41
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;
48 }
49
50 return pevent_read_number(pevent, context->event_data + *offset, *size);
Tom Zanussi7397d802010-01-27 02:27:54 -060051}
52
53int common_lock_depth(struct scripting_context *context)
54{
Steven Rostedtaaf045f2012-04-06 00:47:56 +020055 static int offset;
56 static int size;
57 int ret;
58
59 ret = get_common_field(context, &size, &offset,
60 "common_lock_depth");
61 if (ret < 0)
62 return -1;
63
64 return ret;
65}
66
67int common_flags(struct scripting_context *context)
68{
69 static int offset;
70 static int size;
71 int ret;
72
73 ret = get_common_field(context, &size, &offset,
74 "common_flags");
75 if (ret < 0)
76 return -1;
77
78 return ret;
79}
80
81int common_pc(struct scripting_context *context)
82{
83 static int offset;
84 static int size;
85 int ret;
86
87 ret = get_common_field(context, &size, &offset,
88 "common_preempt_count");
89 if (ret < 0)
90 return -1;
91
92 return ret;
93}
94
95unsigned long long
96raw_field_value(struct event_format *event, const char *name, void *data)
97{
98 struct format_field *field;
99 unsigned long long val;
100
101 field = pevent_find_any_field(event, name);
102 if (!field)
103 return 0ULL;
104
105 pevent_read_number_field(field, data, &val);
106
107 return val;
108}
109
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300110unsigned long long read_size(struct event_format *event, void *ptr, int size)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200111{
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300112 return pevent_read_number(event->pevent, ptr, size);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200113}
114
Arnaldo Carvalho de Meloaa1aac12015-02-03 12:46:58 -0300115void event_format__fprintf(struct event_format *event,
116 int cpu, void *data, int size, FILE *fp)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200117{
Steven Rostedt1c698182012-04-06 00:48:06 +0200118 struct pevent_record record;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200119 struct trace_seq s;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200120
121 memset(&record, 0, sizeof(record));
122 record.cpu = cpu;
123 record.size = size;
124 record.data = data;
125
126 trace_seq_init(&s);
David Ahern76a83492012-06-14 12:36:17 -0600127 pevent_event_info(&s, event, &record);
Arnaldo Carvalho de Meloaa1aac12015-02-03 12:46:58 -0300128 trace_seq_do_fprintf(&s, fp);
Jiri Olsab58f6082014-02-02 22:38:49 +0100129 trace_seq_destroy(&s);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200130}
131
Arnaldo Carvalho de Meloaa1aac12015-02-03 12:46:58 -0300132void event_format__print(struct event_format *event,
133 int cpu, void *data, int size)
134{
135 return event_format__fprintf(event, cpu, data, size, stdout);
136}
137
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300138void parse_proc_kallsyms(struct pevent *pevent,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300139 char *file, unsigned int size __maybe_unused)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200140{
141 unsigned long long addr;
142 char *func;
143 char *line;
144 char *next = NULL;
145 char *addr_str;
146 char *mod;
Ingo Molnar0f965422013-09-12 15:16:49 +0200147 char *fmt = NULL;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200148
149 line = strtok_r(file, "\n", &next);
150 while (line) {
151 mod = NULL;
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300152 addr_str = strtok_r(line, " ", &fmt);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200153 addr = strtoull(addr_str, NULL, 16);
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300154 /* skip character */
155 strtok_r(NULL, " ", &fmt);
156 func = strtok_r(NULL, "\t", &fmt);
157 mod = strtok_r(NULL, "]", &fmt);
158 /* truncate the extra '[' */
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200159 if (mod)
Irina Tirdeabcbd0042012-09-20 23:37:50 +0300160 mod = mod + 1;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200161
162 pevent_register_function(pevent, func, addr, mod);
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200163
164 line = strtok_r(NULL, "\n", &next);
165 }
166}
167
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300168void parse_ftrace_printk(struct pevent *pevent,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300169 char *file, unsigned int size __maybe_unused)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200170{
171 unsigned long long addr;
172 char *printk;
173 char *line;
174 char *next = NULL;
175 char *addr_str;
176 char *fmt;
177
178 line = strtok_r(file, "\n", &next);
179 while (line) {
180 addr_str = strtok_r(line, ":", &fmt);
181 if (!addr_str) {
182 warning("printk format with empty entry");
183 break;
184 }
185 addr = strtoull(addr_str, NULL, 16);
186 /* fmt still has a space, skip it */
187 printk = strdup(fmt+1);
188 line = strtok_r(NULL, "\n", &next);
189 pevent_register_print_string(pevent, printk, addr);
190 }
191}
192
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300193int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200194{
195 return pevent_parse_event(pevent, buf, size, "ftrace");
196}
197
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300198int parse_event_file(struct pevent *pevent,
199 char *buf, unsigned long size, char *sys)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200200{
201 return pevent_parse_event(pevent, buf, size, sys);
202}
203
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300204struct event_format *trace_find_next_event(struct pevent *pevent,
205 struct event_format *event)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200206{
207 static int idx;
208
Namhyung Kimd0d39132012-08-14 10:57:03 +0900209 if (!pevent || !pevent->events)
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200210 return NULL;
211
212 if (!event) {
213 idx = 0;
214 return pevent->events[0];
215 }
216
217 if (idx < pevent->nr_events && event == pevent->events[idx]) {
218 idx++;
219 if (idx == pevent->nr_events)
220 return NULL;
221 return pevent->events[idx];
222 }
223
224 for (idx = 1; idx < pevent->nr_events; idx++) {
225 if (event == pevent->events[idx - 1])
226 return pevent->events[idx];
227 }
228 return NULL;
229}
230
231struct flag {
232 const char *name;
233 unsigned long long value;
234};
235
236static const struct flag flags[] = {
237 { "HI_SOFTIRQ", 0 },
238 { "TIMER_SOFTIRQ", 1 },
239 { "NET_TX_SOFTIRQ", 2 },
240 { "NET_RX_SOFTIRQ", 3 },
241 { "BLOCK_SOFTIRQ", 4 },
242 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
243 { "TASKLET_SOFTIRQ", 6 },
244 { "SCHED_SOFTIRQ", 7 },
245 { "HRTIMER_SOFTIRQ", 8 },
246 { "RCU_SOFTIRQ", 9 },
247
248 { "HRTIMER_NORESTART", 0 },
249 { "HRTIMER_RESTART", 1 },
250};
251
252unsigned long long eval_flag(const char *flag)
253{
254 int i;
255
256 /*
257 * Some flags in the format files do not get converted.
258 * If the flag is not numeric, see if it is something that
259 * we already know about.
260 */
261 if (isdigit(flag[0]))
262 return strtoull(flag, NULL, 0);
263
264 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
265 if (strcmp(flags[i].name, flag) == 0)
266 return flags[i].value;
267
268 return 0;
Tom Zanussi7397d802010-01-27 02:27:54 -0600269}