1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <errno.h>
25
26#include "../perf.h"
27#include "debug.h"
28#include "trace-event.h"
29
30#include "sane_ctype.h"
31
32static int get_common_field(struct scripting_context *context,
33 int *offset, int *size, const char *type)
34{
35 struct tep_handle *pevent = context->pevent;
36 struct event_format *event;
37 struct format_field *field;
38
39 if (!*size) {
40 if (!pevent->events)
41 return 0;
42
43 event = pevent->events[0];
44 field = tep_find_common_field(event, type);
45 if (!field)
46 return 0;
47 *offset = field->offset;
48 *size = field->size;
49 }
50
51 return tep_read_number(pevent, context->event_data + *offset, *size);
52}
53
54int common_lock_depth(struct scripting_context *context)
55{
56 static int offset;
57 static int size;
58 int ret;
59
60 ret = get_common_field(context, &size, &offset,
61 "common_lock_depth");
62 if (ret < 0)
63 return -1;
64
65 return ret;
66}
67
68int common_flags(struct scripting_context *context)
69{
70 static int offset;
71 static int size;
72 int ret;
73
74 ret = get_common_field(context, &size, &offset,
75 "common_flags");
76 if (ret < 0)
77 return -1;
78
79 return ret;
80}
81
82int common_pc(struct scripting_context *context)
83{
84 static int offset;
85 static int size;
86 int ret;
87
88 ret = get_common_field(context, &size, &offset,
89 "common_preempt_count");
90 if (ret < 0)
91 return -1;
92
93 return ret;
94}
95
96unsigned long long
97raw_field_value(struct event_format *event, const char *name, void *data)
98{
99 struct format_field *field;
100 unsigned long long val;
101
102 field = tep_find_any_field(event, name);
103 if (!field)
104 return 0ULL;
105
106 tep_read_number_field(field, data, &val);
107
108 return val;
109}
110
111unsigned long long read_size(struct event_format *event, void *ptr, int size)
112{
113 return tep_read_number(event->pevent, ptr, size);
114}
115
116void event_format__fprintf(struct event_format *event,
117 int cpu, void *data, int size, FILE *fp)
118{
119 struct tep_record record;
120 struct trace_seq s;
121
122 memset(&record, 0, sizeof(record));
123 record.cpu = cpu;
124 record.size = size;
125 record.data = data;
126
127 trace_seq_init(&s);
128 tep_event_info(&s, event, &record);
129 trace_seq_do_fprintf(&s, fp);
130 trace_seq_destroy(&s);
131}
132
133void event_format__print(struct event_format *event,
134 int cpu, void *data, int size)
135{
136 return event_format__fprintf(event, cpu, data, size, stdout);
137}
138
139void parse_ftrace_printk(struct tep_handle *pevent,
140 char *file, unsigned int size __maybe_unused)
141{
142 unsigned long long addr;
143 char *printk;
144 char *line;
145 char *next = NULL;
146 char *addr_str;
147 char *fmt = NULL;
148
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;
155 }
156 addr = strtoull(addr_str, NULL, 16);
157
158 printk = strdup(fmt+1);
159 line = strtok_r(NULL, "\n", &next);
160 tep_register_print_string(pevent, printk, addr);
161 }
162}
163
164void parse_saved_cmdline(struct tep_handle *pevent,
165 char *file, unsigned int size __maybe_unused)
166{
167 char comm[17];
168 char *line;
169 char *next = NULL;
170 int pid;
171
172 line = strtok_r(file, "\n", &next);
173 while (line) {
174 if (sscanf(line, "%d %16s", &pid, comm) == 2)
175 tep_register_comm(pevent, comm, pid);
176 line = strtok_r(NULL, "\n", &next);
177 }
178}
179
180int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
181{
182 return tep_parse_event(pevent, buf, size, "ftrace");
183}
184
185int parse_event_file(struct tep_handle *pevent,
186 char *buf, unsigned long size, char *sys)
187{
188 return tep_parse_event(pevent, buf, size, sys);
189}
190
191struct event_format *trace_find_next_event(struct tep_handle *pevent,
192 struct event_format *event)
193{
194 static int idx;
195
196 if (!pevent || !pevent->events)
197 return NULL;
198
199 if (!event) {
200 idx = 0;
201 return pevent->events[0];
202 }
203
204 if (idx < pevent->nr_events && event == pevent->events[idx]) {
205 idx++;
206 if (idx == pevent->nr_events)
207 return NULL;
208 return pevent->events[idx];
209 }
210
211 for (idx = 1; idx < pevent->nr_events; idx++) {
212 if (event == pevent->events[idx - 1])
213 return pevent->events[idx];
214 }
215 return NULL;
216}
217
218struct flag {
219 const char *name;
220 unsigned long long value;
221};
222
223static const struct flag flags[] = {
224 { "HI_SOFTIRQ", 0 },
225 { "TIMER_SOFTIRQ", 1 },
226 { "NET_TX_SOFTIRQ", 2 },
227 { "NET_RX_SOFTIRQ", 3 },
228 { "BLOCK_SOFTIRQ", 4 },
229 { "IRQ_POLL_SOFTIRQ", 5 },
230 { "TASKLET_SOFTIRQ", 6 },
231 { "SCHED_SOFTIRQ", 7 },
232 { "HRTIMER_SOFTIRQ", 8 },
233 { "RCU_SOFTIRQ", 9 },
234
235 { "HRTIMER_NORESTART", 0 },
236 { "HRTIMER_RESTART", 1 },
237};
238
239unsigned long long eval_flag(const char *flag)
240{
241 int i;
242
243
244
245
246
247
248 if (isdigit(flag[0]))
249 return strtoull(flag, NULL, 0);
250
251 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
252 if (strcmp(flags[i].name, flag) == 0)
253 return flags[i].value;
254
255 return 0;
256}
257