1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
11 #include "trace-event.h"
13 #include <linux/ctype.h>
14 #include <linux/kernel.h>
15 #include <traceevent/event-parse.h>
17 static int get_common_field(struct scripting_context *context,
18 int *offset, int *size, const char *type)
20 struct tep_handle *pevent = context->pevent;
21 struct tep_event *event;
22 struct tep_format_field *field;
26 event = tep_get_first_event(pevent);
30 field = tep_find_common_field(event, type);
33 *offset = field->offset;
37 return tep_read_number(pevent, context->event_data + *offset, *size);
40 int common_lock_depth(struct scripting_context *context)
46 ret = get_common_field(context, &size, &offset,
54 int common_flags(struct scripting_context *context)
60 ret = get_common_field(context, &size, &offset,
68 int common_pc(struct scripting_context *context)
74 ret = get_common_field(context, &size, &offset,
75 "common_preempt_count");
83 raw_field_value(struct tep_event *event, const char *name, void *data)
85 struct tep_format_field *field;
86 unsigned long long val;
88 field = tep_find_any_field(event, name);
92 tep_read_number_field(field, data, &val);
97 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
99 return tep_read_number(event->tep, ptr, size);
102 void event_format__fprintf(struct tep_event *event,
103 int cpu, void *data, int size, FILE *fp)
105 struct tep_record record;
108 memset(&record, 0, sizeof(record));
114 tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO);
115 trace_seq_do_fprintf(&s, fp);
116 trace_seq_destroy(&s);
119 void event_format__print(struct tep_event *event,
120 int cpu, void *data, int size)
122 return event_format__fprintf(event, cpu, data, size, stdout);
125 void parse_ftrace_printk(struct tep_handle *pevent,
126 char *file, unsigned int size __maybe_unused)
128 unsigned long long addr;
135 line = strtok_r(file, "\n", &next);
137 addr_str = strtok_r(line, ":", &fmt);
139 pr_warning("printk format with empty entry");
142 addr = strtoull(addr_str, NULL, 16);
143 /* fmt still has a space, skip it */
144 printk = strdup(fmt+1);
145 line = strtok_r(NULL, "\n", &next);
146 tep_register_print_string(pevent, printk, addr);
151 void parse_saved_cmdline(struct tep_handle *pevent,
152 char *file, unsigned int size __maybe_unused)
154 char comm[17]; /* Max comm length in the kernel is 16. */
159 line = strtok_r(file, "\n", &next);
161 if (sscanf(line, "%d %16s", &pid, comm) == 2)
162 tep_register_comm(pevent, comm, pid);
163 line = strtok_r(NULL, "\n", &next);
167 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
169 return tep_parse_event(pevent, buf, size, "ftrace");
172 int parse_event_file(struct tep_handle *pevent,
173 char *buf, unsigned long size, char *sys)
175 return tep_parse_event(pevent, buf, size, sys);
180 unsigned long long value;
183 static const struct flag flags[] = {
185 { "TIMER_SOFTIRQ", 1 },
186 { "NET_TX_SOFTIRQ", 2 },
187 { "NET_RX_SOFTIRQ", 3 },
188 { "BLOCK_SOFTIRQ", 4 },
189 { "IRQ_POLL_SOFTIRQ", 5 },
190 { "TASKLET_SOFTIRQ", 6 },
191 { "SCHED_SOFTIRQ", 7 },
192 { "HRTIMER_SOFTIRQ", 8 },
193 { "RCU_SOFTIRQ", 9 },
195 { "HRTIMER_NORESTART", 0 },
196 { "HRTIMER_RESTART", 1 },
199 unsigned long long eval_flag(const char *flag)
204 * Some flags in the format files do not get converted.
205 * If the flag is not numeric, see if it is something that
206 * we already know about.
208 if (isdigit(flag[0]))
209 return strtoull(flag, NULL, 0);
211 for (i = 0; i < (int)(ARRAY_SIZE(flags)); i++)
212 if (strcmp(flags[i].name, flag) == 0)
213 return flags[i].value;