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