1 // SPDX-License-Identifier: GPL-2.0-only
11 #include "trace-event.h"
13 #include <linux/ctype.h>
15 static int get_common_field(struct scripting_context *context,
16 int *offset, int *size, const char *type)
18 struct tep_handle *pevent = context->pevent;
19 struct tep_event *event;
20 struct tep_format_field *field;
24 event = tep_get_first_event(pevent);
28 field = tep_find_common_field(event, type);
31 *offset = field->offset;
35 return tep_read_number(pevent, context->event_data + *offset, *size);
38 int common_lock_depth(struct scripting_context *context)
44 ret = get_common_field(context, &size, &offset,
52 int common_flags(struct scripting_context *context)
58 ret = get_common_field(context, &size, &offset,
66 int common_pc(struct scripting_context *context)
72 ret = get_common_field(context, &size, &offset,
73 "common_preempt_count");
81 raw_field_value(struct tep_event *event, const char *name, void *data)
83 struct tep_format_field *field;
84 unsigned long long val;
86 field = tep_find_any_field(event, name);
90 tep_read_number_field(field, data, &val);
95 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
97 return tep_read_number(event->tep, ptr, size);
100 void event_format__fprintf(struct tep_event *event,
101 int cpu, void *data, int size, FILE *fp)
103 struct tep_record record;
106 memset(&record, 0, sizeof(record));
112 tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO);
113 trace_seq_do_fprintf(&s, fp);
114 trace_seq_destroy(&s);
117 void event_format__print(struct tep_event *event,
118 int cpu, void *data, int size)
120 return event_format__fprintf(event, cpu, data, size, stdout);
123 void parse_ftrace_printk(struct tep_handle *pevent,
124 char *file, unsigned int size __maybe_unused)
126 unsigned long long addr;
133 line = strtok_r(file, "\n", &next);
135 addr_str = strtok_r(line, ":", &fmt);
137 pr_warning("printk format with empty entry");
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);
149 void parse_saved_cmdline(struct tep_handle *pevent,
150 char *file, unsigned int size __maybe_unused)
152 char comm[17]; /* Max comm length in the kernel is 16. */
157 line = strtok_r(file, "\n", &next);
159 if (sscanf(line, "%d %16s", &pid, comm) == 2)
160 tep_register_comm(pevent, comm, pid);
161 line = strtok_r(NULL, "\n", &next);
165 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
167 return tep_parse_event(pevent, buf, size, "ftrace");
170 int parse_event_file(struct tep_handle *pevent,
171 char *buf, unsigned long size, char *sys)
173 return tep_parse_event(pevent, buf, size, sys);
178 unsigned long long value;
181 static const struct flag flags[] = {
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 },
193 { "HRTIMER_NORESTART", 0 },
194 { "HRTIMER_RESTART", 1 },
197 unsigned long long eval_flag(const char *flag)
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.
206 if (isdigit(flag[0]))
207 return strtoull(flag, NULL, 0);
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;