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 15 static int get_common_field(struct scripting_context *context, 16 int *offset, int *size, const char *type) 17 { 18 struct tep_handle *pevent = context->pevent; 19 struct tep_event *event; 20 struct tep_format_field *field; 21 22 if (!*size) { 23 24 event = tep_get_first_event(pevent); 25 if (!event) 26 return 0; 27 28 field = tep_find_common_field(event, type); 29 if (!field) 30 return 0; 31 *offset = field->offset; 32 *size = field->size; 33 } 34 35 return tep_read_number(pevent, context->event_data + *offset, *size); 36 } 37 38 int common_lock_depth(struct scripting_context *context) 39 { 40 static int offset; 41 static int size; 42 int ret; 43 44 ret = get_common_field(context, &size, &offset, 45 "common_lock_depth"); 46 if (ret < 0) 47 return -1; 48 49 return ret; 50 } 51 52 int common_flags(struct scripting_context *context) 53 { 54 static int offset; 55 static int size; 56 int ret; 57 58 ret = get_common_field(context, &size, &offset, 59 "common_flags"); 60 if (ret < 0) 61 return -1; 62 63 return ret; 64 } 65 66 int common_pc(struct scripting_context *context) 67 { 68 static int offset; 69 static int size; 70 int ret; 71 72 ret = get_common_field(context, &size, &offset, 73 "common_preempt_count"); 74 if (ret < 0) 75 return -1; 76 77 return ret; 78 } 79 80 unsigned long long 81 raw_field_value(struct tep_event *event, const char *name, void *data) 82 { 83 struct tep_format_field *field; 84 unsigned long long val; 85 86 field = tep_find_any_field(event, name); 87 if (!field) 88 return 0ULL; 89 90 tep_read_number_field(field, data, &val); 91 92 return val; 93 } 94 95 unsigned long long read_size(struct tep_event *event, void *ptr, int size) 96 { 97 return tep_read_number(event->tep, ptr, size); 98 } 99 100 void event_format__fprintf(struct tep_event *event, 101 int cpu, void *data, int size, FILE *fp) 102 { 103 struct tep_record record; 104 struct trace_seq s; 105 106 memset(&record, 0, sizeof(record)); 107 record.cpu = cpu; 108 record.size = size; 109 record.data = data; 110 111 trace_seq_init(&s); 112 tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO); 113 trace_seq_do_fprintf(&s, fp); 114 trace_seq_destroy(&s); 115 } 116 117 void event_format__print(struct tep_event *event, 118 int cpu, void *data, int size) 119 { 120 return event_format__fprintf(event, cpu, data, size, stdout); 121 } 122 123 void parse_ftrace_printk(struct tep_handle *pevent, 124 char *file, unsigned int size __maybe_unused) 125 { 126 unsigned long long addr; 127 char *printk; 128 char *line; 129 char *next = NULL; 130 char *addr_str; 131 char *fmt = NULL; 132 133 line = strtok_r(file, "\n", &next); 134 while (line) { 135 addr_str = strtok_r(line, ":", &fmt); 136 if (!addr_str) { 137 pr_warning("printk format with empty entry"); 138 break; 139 } 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); 145 free(printk); 146 } 147 } 148 149 void parse_saved_cmdline(struct tep_handle *pevent, 150 char *file, unsigned int size __maybe_unused) 151 { 152 char comm[17]; /* Max comm length in the kernel is 16. */ 153 char *line; 154 char *next = NULL; 155 int pid; 156 157 line = strtok_r(file, "\n", &next); 158 while (line) { 159 if (sscanf(line, "%d %16s", &pid, comm) == 2) 160 tep_register_comm(pevent, comm, pid); 161 line = strtok_r(NULL, "\n", &next); 162 } 163 } 164 165 int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size) 166 { 167 return tep_parse_event(pevent, buf, size, "ftrace"); 168 } 169 170 int parse_event_file(struct tep_handle *pevent, 171 char *buf, unsigned long size, char *sys) 172 { 173 return tep_parse_event(pevent, buf, size, sys); 174 } 175 176 struct tep_event *trace_find_next_event(struct tep_handle *pevent, 177 struct tep_event *event) 178 { 179 static int idx; 180 int events_count; 181 struct tep_event *all_events; 182 183 all_events = tep_get_first_event(pevent); 184 events_count = tep_get_events_count(pevent); 185 if (!pevent || !all_events || events_count < 1) 186 return NULL; 187 188 if (!event) { 189 idx = 0; 190 return all_events; 191 } 192 193 if (idx < events_count && event == (all_events + idx)) { 194 idx++; 195 if (idx == events_count) 196 return NULL; 197 return (all_events + idx); 198 } 199 200 for (idx = 1; idx < events_count; idx++) { 201 if (event == (all_events + (idx - 1))) 202 return (all_events + idx); 203 } 204 return NULL; 205 } 206 207 struct flag { 208 const char *name; 209 unsigned long long value; 210 }; 211 212 static const struct flag flags[] = { 213 { "HI_SOFTIRQ", 0 }, 214 { "TIMER_SOFTIRQ", 1 }, 215 { "NET_TX_SOFTIRQ", 2 }, 216 { "NET_RX_SOFTIRQ", 3 }, 217 { "BLOCK_SOFTIRQ", 4 }, 218 { "IRQ_POLL_SOFTIRQ", 5 }, 219 { "TASKLET_SOFTIRQ", 6 }, 220 { "SCHED_SOFTIRQ", 7 }, 221 { "HRTIMER_SOFTIRQ", 8 }, 222 { "RCU_SOFTIRQ", 9 }, 223 224 { "HRTIMER_NORESTART", 0 }, 225 { "HRTIMER_RESTART", 1 }, 226 }; 227 228 unsigned long long eval_flag(const char *flag) 229 { 230 int i; 231 232 /* 233 * Some flags in the format files do not get converted. 234 * If the flag is not numeric, see if it is something that 235 * we already know about. 236 */ 237 if (isdigit(flag[0])) 238 return strtoull(flag, NULL, 0); 239 240 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 241 if (strcmp(flags[i].name, flag) == 0) 242 return flags[i].value; 243 244 return 0; 245 } 246