Linux Audio

Check our new training course

Loading...
v6.13.7
  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#include <linux/kernel.h>
 15#include <event-parse.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 16
 17static int get_common_field(struct scripting_context *context,
 18			    int *offset, int *size, const char *type)
 19{
 20	struct tep_handle *pevent = context->pevent;
 21	struct tep_event *event;
 22	struct tep_format_field *field;
 23
 24	if (!*size) {
 25
 26		event = tep_get_first_event(pevent);
 27		if (!event)
 28			return 0;
 29
 30		field = tep_find_common_field(event, type);
 
 31		if (!field)
 32			return 0;
 33		*offset = field->offset;
 34		*size = field->size;
 35	}
 36
 37	return tep_read_number(pevent, context->event_data + *offset, *size);
 38}
 39
 40int common_lock_depth(struct scripting_context *context)
 41{
 42	static int offset;
 43	static int size;
 44	int ret;
 45
 46	ret = get_common_field(context, &size, &offset,
 47			       "common_lock_depth");
 48	if (ret < 0)
 49		return -1;
 50
 51	return ret;
 52}
 53
 54int common_flags(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_flags");
 62	if (ret < 0)
 63		return -1;
 64
 65	return ret;
 66}
 67
 68int common_pc(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_preempt_count");
 76	if (ret < 0)
 77		return -1;
 78
 79	return ret;
 80}
 81
 82unsigned long long
 83raw_field_value(struct tep_event *event, const char *name, void *data)
 84{
 85	struct tep_format_field *field;
 86	unsigned long long val;
 87
 88	field = tep_find_any_field(event, name);
 89	if (!field)
 90		return 0ULL;
 91
 92	tep_read_number_field(field, data, &val);
 93
 94	return val;
 95}
 96
 97unsigned long long read_size(struct tep_event *event, void *ptr, int size)
 98{
 99	return tep_read_number(event->tep, ptr, size);
100}
101
102void event_format__fprintf(struct tep_event *event,
103			   int cpu, void *data, int size, FILE *fp)
104{
105	struct tep_record record;
106	struct trace_seq s;
107
108	memset(&record, 0, sizeof(record));
109	record.cpu = cpu;
110	record.size = size;
111	record.data = data;
112
113	trace_seq_init(&s);
114	tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO);
115	trace_seq_do_fprintf(&s, fp);
116	trace_seq_destroy(&s);
117}
118
119/*
120 * prev_state is of size long, which is 32 bits on 32 bit architectures.
121 * As it needs to have the same bits for both 32 bit and 64 bit architectures
122 * we can just assume that the flags we care about will all be within
123 * the 32 bits.
124 */
125#define MAX_STATE_BITS 32
126
127static const char *convert_sym(struct tep_print_flag_sym *sym)
128{
129	static char save_states[MAX_STATE_BITS + 1];
130
131	memset(save_states, 0, sizeof(save_states));
 
132
133	/* This is the flags for the prev_state_field, now make them into a string */
134	for (; sym; sym = sym->next) {
135		long bitmask = strtoul(sym->value, NULL, 0);
136		int i;
137
138		for (i = 0; !(bitmask & 1); i++)
139			bitmask >>= 1;
 
140
141		if (i >= MAX_STATE_BITS)
142			continue;
 
143
144		save_states[i] = sym->str[0];
145	}
 
146
147	return save_states;
 
 
148}
149
150static struct tep_print_arg_field *
151find_arg_field(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
152{
153	struct tep_print_arg_field *field;
 
154
155	if (!arg)
156		return NULL;
157
158	if (arg->type == TEP_PRINT_FIELD)
159		return &arg->field;
 
 
 
 
160
161	if (arg->type == TEP_PRINT_OP) {
162		field = find_arg_field(prev_state_field, arg->op.left);
163		if (field && field->field == prev_state_field)
164			return field;
165		field = find_arg_field(prev_state_field, arg->op.right);
166		if (field && field->field == prev_state_field)
167			return field;
168	}
169	return NULL;
170}
171
172static struct tep_print_flag_sym *
173test_flags(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
174{
175	struct tep_print_arg_field *field;
 
176
177	field = find_arg_field(prev_state_field, arg->flags.field);
178	if (!field)
179		return NULL;
 
180
181	return arg->flags.flags;
 
 
182}
183
184static struct tep_print_flag_sym *
185search_op(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
186{
187	struct tep_print_flag_sym *sym = NULL;
 
 
188
189	if (!arg)
190		return NULL;
191
192	if (arg->type == TEP_PRINT_OP) {
193		sym = search_op(prev_state_field, arg->op.left);
194		if (sym)
195			return sym;
196
197		sym = search_op(prev_state_field, arg->op.right);
198		if (sym)
199			return sym;
200	} else if (arg->type == TEP_PRINT_FLAGS) {
201		sym = test_flags(prev_state_field, arg);
202	}
203
204	return sym;
 
 
 
205}
206
207const char *parse_task_states(struct tep_format_field *state_field)
208{
209	struct tep_print_flag_sym *sym;
210	struct tep_print_arg *arg;
211	struct tep_event *event;
 
 
 
 
 
 
 
 
 
 
 
 
 
212
213	event = state_field->event;
 
 
 
 
 
 
214
215	/*
216	 * Look at the event format fields, and search for where
217	 * the prev_state is parsed via the format flags.
218	 */
219	for (arg = event->print_fmt.args; arg; arg = arg->next) {
220		/*
221		 * Currently, the __print_flags() for the prev_state
222		 * is embedded in operations, so they too must be
223		 * searched.
224		 */
225		sym = search_op(state_field, arg);
226		if (sym)
227			return convert_sym(sym);
228	}
229	return NULL;
230}
231
232void parse_ftrace_printk(struct tep_handle *pevent,
233			 char *file, unsigned int size __maybe_unused)
234{
235	unsigned long long addr;
236	char *printk;
237	char *line;
238	char *next = NULL;
239	char *addr_str;
240	char *fmt = NULL;
241
242	line = strtok_r(file, "\n", &next);
243	while (line) {
244		addr_str = strtok_r(line, ":", &fmt);
245		if (!addr_str) {
246			pr_warning("printk format with empty entry");
247			break;
248		}
249		addr = strtoull(addr_str, NULL, 16);
250		/* fmt still has a space, skip it */
251		printk = strdup(fmt+1);
252		line = strtok_r(NULL, "\n", &next);
253		tep_register_print_string(pevent, printk, addr);
254		free(printk);
255	}
256}
257
258void parse_saved_cmdline(struct tep_handle *pevent,
259			 char *file, unsigned int size __maybe_unused)
260{
261	char comm[17]; /* Max comm length in the kernel is 16. */
262	char *line;
263	char *next = NULL;
264	int pid;
265
266	line = strtok_r(file, "\n", &next);
267	while (line) {
268		if (sscanf(line, "%d %16s", &pid, comm) == 2)
269			tep_register_comm(pevent, comm, pid);
270		line = strtok_r(NULL, "\n", &next);
271	}
272}
273
274int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
275{
276	return tep_parse_event(pevent, buf, size, "ftrace");
277}
278
279int parse_event_file(struct tep_handle *pevent,
280		     char *buf, unsigned long size, char *sys)
281{
282	return tep_parse_event(pevent, buf, size, sys);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283}
284
285struct flag {
286	const char *name;
287	unsigned long long value;
288};
289
290static const struct flag flags[] = {
291	{ "HI_SOFTIRQ", 0 },
292	{ "TIMER_SOFTIRQ", 1 },
293	{ "NET_TX_SOFTIRQ", 2 },
294	{ "NET_RX_SOFTIRQ", 3 },
295	{ "BLOCK_SOFTIRQ", 4 },
296	{ "IRQ_POLL_SOFTIRQ", 5 },
297	{ "TASKLET_SOFTIRQ", 6 },
298	{ "SCHED_SOFTIRQ", 7 },
299	{ "HRTIMER_SOFTIRQ", 8 },
300	{ "RCU_SOFTIRQ", 9 },
301
302	{ "HRTIMER_NORESTART", 0 },
303	{ "HRTIMER_RESTART", 1 },
304};
305
306unsigned long long eval_flag(const char *flag)
307{
308	int i;
309
310	/*
311	 * Some flags in the format files do not get converted.
312	 * If the flag is not numeric, see if it is something that
313	 * we already know about.
314	 */
315	if (isdigit(flag[0]))
316		return strtoull(flag, NULL, 0);
317
318	for (i = 0; i < (int)(ARRAY_SIZE(flags)); i++)
319		if (strcmp(flags[i].name, flag) == 0)
320			return flags[i].value;
321
322	return 0;
323}
v3.5.6
 
  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 <ctype.h>
 25#include <errno.h>
 26
 27#include "../perf.h"
 28#include "util.h"
 29#include "trace-event.h"
 30
 31int header_page_size_size;
 32int header_page_ts_size;
 33int header_page_data_offset;
 34
 35struct pevent *perf_pevent;
 36static struct pevent *pevent;
 37
 38bool latency_format;
 39
 40int read_trace_init(int file_bigendian, int host_bigendian)
 41{
 42	if (pevent)
 43		return 0;
 44
 45	perf_pevent = pevent_alloc();
 46	pevent = perf_pevent;
 47
 48	pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
 49	pevent_set_file_bigendian(pevent, file_bigendian);
 50	pevent_set_host_bigendian(pevent, host_bigendian);
 51
 52	return 0;
 53}
 54
 55static int get_common_field(struct scripting_context *context,
 56			    int *offset, int *size, const char *type)
 57{
 58	struct event_format *event;
 59	struct format_field *field;
 
 60
 61	if (!*size) {
 62		if (!pevent->events)
 
 
 63			return 0;
 64
 65		event = pevent->events[0];
 66		field = pevent_find_common_field(event, type);
 67		if (!field)
 68			return 0;
 69		*offset = field->offset;
 70		*size = field->size;
 71	}
 72
 73	return pevent_read_number(pevent, context->event_data + *offset, *size);
 74}
 75
 76int common_lock_depth(struct scripting_context *context)
 77{
 78	static int offset;
 79	static int size;
 80	int ret;
 81
 82	ret = get_common_field(context, &size, &offset,
 83			       "common_lock_depth");
 84	if (ret < 0)
 85		return -1;
 86
 87	return ret;
 88}
 89
 90int common_flags(struct scripting_context *context)
 91{
 92	static int offset;
 93	static int size;
 94	int ret;
 95
 96	ret = get_common_field(context, &size, &offset,
 97			       "common_flags");
 98	if (ret < 0)
 99		return -1;
100
101	return ret;
102}
103
104int common_pc(struct scripting_context *context)
105{
106	static int offset;
107	static int size;
108	int ret;
109
110	ret = get_common_field(context, &size, &offset,
111			       "common_preempt_count");
112	if (ret < 0)
113		return -1;
114
115	return ret;
116}
117
118unsigned long long
119raw_field_value(struct event_format *event, const char *name, void *data)
120{
121	struct format_field *field;
122	unsigned long long val;
123
124	field = pevent_find_any_field(event, name);
125	if (!field)
126		return 0ULL;
127
128	pevent_read_number_field(field, data, &val);
129
130	return val;
131}
132
133void *raw_field_ptr(struct event_format *event, const char *name, void *data)
 
 
 
 
 
 
134{
135	struct format_field *field;
 
136
137	field = pevent_find_any_field(event, name);
138	if (!field)
139		return NULL;
 
140
141	if (field->flags & FIELD_IS_DYNAMIC) {
142		int offset;
 
 
 
143
144		offset = *(int *)(data + field->offset);
145		offset &= 0xffff;
 
 
 
 
 
146
147		return data + offset;
148	}
 
149
150	return data + field->offset;
151}
152
153int trace_parse_common_type(void *data)
154{
155	struct pevent_record record;
 
156
157	record.data = data;
158	return pevent_data_type(pevent, &record);
159}
160
161int trace_parse_common_pid(void *data)
162{
163	struct pevent_record record;
164
165	record.data = data;
166	return pevent_data_pid(pevent, &record);
167}
168
169unsigned long long read_size(void *ptr, int size)
170{
171	return pevent_read_number(pevent, ptr, size);
172}
173
174struct event_format *trace_find_event(int type)
 
175{
176	return pevent_find_event(pevent, type);
177}
178
 
 
179
180void print_trace_event(int cpu, void *data, int size)
181{
182	struct event_format *event;
183	struct pevent_record record;
184	struct trace_seq s;
185	int type;
186
187	type = trace_parse_common_type(data);
 
 
 
 
 
 
 
 
 
188
189	event = trace_find_event(type);
190	if (!event) {
191		warning("ug! no event found for type %d", type);
192		return;
193	}
194
195	memset(&record, 0, sizeof(record));
196	record.cpu = cpu;
197	record.size = size;
198	record.data = data;
199
200	trace_seq_init(&s);
201	pevent_event_info(&s, event, &record);
202	trace_seq_do_printf(&s);
203}
204
205void print_event(int cpu, void *data, int size, unsigned long long nsecs,
206		  char *comm)
207{
208	struct pevent_record record;
209	struct trace_seq s;
210	int pid;
211
212	pevent->latency_format = latency_format;
 
213
214	record.ts = nsecs;
215	record.cpu = cpu;
216	record.size = size;
217	record.data = data;
218	pid = pevent_data_pid(pevent, &record);
219
220	if (!pevent_pid_is_registered(pevent, pid))
221		pevent_register_comm(pevent, comm, pid);
 
 
 
222
223	trace_seq_init(&s);
224	pevent_print_event(pevent, &s, &record);
225	trace_seq_do_printf(&s);
226	printf("\n");
227}
228
229void parse_proc_kallsyms(char *file, unsigned int size __unused)
230{
231	unsigned long long addr;
232	char *func;
233	char *line;
234	char *next = NULL;
235	char *addr_str;
236	char *mod;
237	char ch;
238
239	line = strtok_r(file, "\n", &next);
240	while (line) {
241		mod = NULL;
242		sscanf(line, "%as %c %as\t[%as",
243		       (float *)(void *)&addr_str, /* workaround gcc warning */
244		       &ch, (float *)(void *)&func, (float *)(void *)&mod);
245		addr = strtoull(addr_str, NULL, 16);
246		free(addr_str);
247
248		/* truncate the extra ']' */
249		if (mod)
250			mod[strlen(mod) - 1] = 0;
251
252		pevent_register_function(pevent, func, addr, mod);
253		free(func);
254		free(mod);
255
256		line = strtok_r(NULL, "\n", &next);
 
 
 
 
 
 
 
 
 
 
 
 
257	}
 
258}
259
260void parse_ftrace_printk(char *file, unsigned int size __unused)
 
261{
262	unsigned long long addr;
263	char *printk;
264	char *line;
265	char *next = NULL;
266	char *addr_str;
267	char *fmt;
268
269	line = strtok_r(file, "\n", &next);
270	while (line) {
271		addr_str = strtok_r(line, ":", &fmt);
272		if (!addr_str) {
273			warning("printk format with empty entry");
274			break;
275		}
276		addr = strtoull(addr_str, NULL, 16);
277		/* fmt still has a space, skip it */
278		printk = strdup(fmt+1);
279		line = strtok_r(NULL, "\n", &next);
280		pevent_register_print_string(pevent, printk, addr);
 
281	}
282}
283
284int parse_ftrace_file(char *buf, unsigned long size)
 
285{
286	return pevent_parse_event(pevent, buf, size, "ftrace");
 
 
 
 
 
 
 
 
 
 
287}
288
289int parse_event_file(char *buf, unsigned long size, char *sys)
290{
291	return pevent_parse_event(pevent, buf, size, sys);
292}
293
294struct event_format *trace_find_next_event(struct event_format *event)
 
295{
296	static int idx;
297
298	if (!pevent->events)
299		return NULL;
300
301	if (!event) {
302		idx = 0;
303		return pevent->events[0];
304	}
305
306	if (idx < pevent->nr_events && event == pevent->events[idx]) {
307		idx++;
308		if (idx == pevent->nr_events)
309			return NULL;
310		return pevent->events[idx];
311	}
312
313	for (idx = 1; idx < pevent->nr_events; idx++) {
314		if (event == pevent->events[idx - 1])
315			return pevent->events[idx];
316	}
317	return NULL;
318}
319
320struct flag {
321	const char *name;
322	unsigned long long value;
323};
324
325static const struct flag flags[] = {
326	{ "HI_SOFTIRQ", 0 },
327	{ "TIMER_SOFTIRQ", 1 },
328	{ "NET_TX_SOFTIRQ", 2 },
329	{ "NET_RX_SOFTIRQ", 3 },
330	{ "BLOCK_SOFTIRQ", 4 },
331	{ "BLOCK_IOPOLL_SOFTIRQ", 5 },
332	{ "TASKLET_SOFTIRQ", 6 },
333	{ "SCHED_SOFTIRQ", 7 },
334	{ "HRTIMER_SOFTIRQ", 8 },
335	{ "RCU_SOFTIRQ", 9 },
336
337	{ "HRTIMER_NORESTART", 0 },
338	{ "HRTIMER_RESTART", 1 },
339};
340
341unsigned long long eval_flag(const char *flag)
342{
343	int i;
344
345	/*
346	 * Some flags in the format files do not get converted.
347	 * If the flag is not numeric, see if it is something that
348	 * we already know about.
349	 */
350	if (isdigit(flag[0]))
351		return strtoull(flag, NULL, 0);
352
353	for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
354		if (strcmp(flags[i].name, flag) == 0)
355			return flags[i].value;
356
357	return 0;
358}