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