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