1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
12 #include "trace-event.h"
14 #include <linux/ctype.h>
16 static int get_common_field(struct scripting_context *context,
17 int *offset, int *size, const char *type)
19 struct tep_handle *pevent = context->pevent;
20 struct tep_event *event;
21 struct tep_format_field *field;
25 event = tep_get_first_event(pevent);
29 field = tep_find_common_field(event, type);
32 *offset = field->offset;
36 return tep_read_number(pevent, context->event_data + *offset, *size);
39 int common_lock_depth(struct scripting_context *context)
45 ret = get_common_field(context, &size, &offset,
53 int common_flags(struct scripting_context *context)
59 ret = get_common_field(context, &size, &offset,
67 int common_pc(struct scripting_context *context)
73 ret = get_common_field(context, &size, &offset,
74 "common_preempt_count");
82 raw_field_value(struct tep_event *event, const char *name, void *data)
84 struct tep_format_field *field;
85 unsigned long long val;
87 field = tep_find_any_field(event, name);
91 tep_read_number_field(field, data, &val);
96 unsigned long long read_size(struct tep_event *event, void *ptr, int size)
98 return tep_read_number(event->tep, ptr, size);
101 void event_format__fprintf(struct tep_event *event,
102 int cpu, void *data, int size, FILE *fp)
104 struct tep_record record;
107 memset(&record, 0, sizeof(record));
113 tep_event_info(&s, event, &record);
114 trace_seq_do_fprintf(&s, fp);
115 trace_seq_destroy(&s);
118 void event_format__print(struct tep_event *event,
119 int cpu, void *data, int size)
121 return event_format__fprintf(event, cpu, data, size, stdout);
124 void parse_ftrace_printk(struct tep_handle *pevent,
125 char *file, unsigned int size __maybe_unused)
127 unsigned long long addr;
134 line = strtok_r(file, "\n", &next);
136 addr_str = strtok_r(line, ":", &fmt);
138 pr_warning("printk format with empty entry");
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);
150 void parse_saved_cmdline(struct tep_handle *pevent,
151 char *file, unsigned int size __maybe_unused)
153 char comm[17]; /* Max comm length in the kernel is 16. */
158 line = strtok_r(file, "\n", &next);
160 if (sscanf(line, "%d %16s", &pid, comm) == 2)
161 tep_register_comm(pevent, comm, pid);
162 line = strtok_r(NULL, "\n", &next);
166 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
168 return tep_parse_event(pevent, buf, size, "ftrace");
171 int parse_event_file(struct tep_handle *pevent,
172 char *buf, unsigned long size, char *sys)
174 return tep_parse_event(pevent, buf, size, sys);
177 struct tep_event *trace_find_next_event(struct tep_handle *pevent,
178 struct tep_event *event)
182 struct tep_event *all_events;
184 all_events = tep_get_first_event(pevent);
185 events_count = tep_get_events_count(pevent);
186 if (!pevent || !all_events || events_count < 1)
194 if (idx < events_count && event == (all_events + idx)) {
196 if (idx == events_count)
198 return (all_events + idx);
201 for (idx = 1; idx < events_count; idx++) {
202 if (event == (all_events + (idx - 1)))
203 return (all_events + idx);
210 unsigned long long value;
213 static const struct flag flags[] = {
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 },
225 { "HRTIMER_NORESTART", 0 },
226 { "HRTIMER_RESTART", 1 },
229 unsigned long long eval_flag(const char *flag)
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.
238 if (isdigit(flag[0]))
239 return strtoull(flag, NULL, 0);
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;