1 /* 2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com> 3 * 4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 of the License (not later!) 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * 19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 */ 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <ctype.h> 25 #include <errno.h> 26 27 #include "../perf.h" 28 #include "util.h" 29 #include "trace-event.h" 30 31 static int get_common_field(struct scripting_context *context, 32 int *offset, int *size, const char *type) 33 { 34 struct pevent *pevent = context->pevent; 35 struct event_format *event; 36 struct format_field *field; 37 38 if (!*size) { 39 if (!pevent->events) 40 return 0; 41 42 event = pevent->events[0]; 43 field = pevent_find_common_field(event, type); 44 if (!field) 45 return 0; 46 *offset = field->offset; 47 *size = field->size; 48 } 49 50 return pevent_read_number(pevent, context->event_data + *offset, *size); 51 } 52 53 int common_lock_depth(struct scripting_context *context) 54 { 55 static int offset; 56 static int size; 57 int ret; 58 59 ret = get_common_field(context, &size, &offset, 60 "common_lock_depth"); 61 if (ret < 0) 62 return -1; 63 64 return ret; 65 } 66 67 int common_flags(struct scripting_context *context) 68 { 69 static int offset; 70 static int size; 71 int ret; 72 73 ret = get_common_field(context, &size, &offset, 74 "common_flags"); 75 if (ret < 0) 76 return -1; 77 78 return ret; 79 } 80 81 int common_pc(struct scripting_context *context) 82 { 83 static int offset; 84 static int size; 85 int ret; 86 87 ret = get_common_field(context, &size, &offset, 88 "common_preempt_count"); 89 if (ret < 0) 90 return -1; 91 92 return ret; 93 } 94 95 unsigned long long 96 raw_field_value(struct event_format *event, const char *name, void *data) 97 { 98 struct format_field *field; 99 unsigned long long val; 100 101 field = pevent_find_any_field(event, name); 102 if (!field) 103 return 0ULL; 104 105 pevent_read_number_field(field, data, &val); 106 107 return val; 108 } 109 110 unsigned long long read_size(struct event_format *event, void *ptr, int size) 111 { 112 return pevent_read_number(event->pevent, ptr, size); 113 } 114 115 void event_format__fprintf(struct event_format *event, 116 int cpu, void *data, int size, FILE *fp) 117 { 118 struct pevent_record record; 119 struct trace_seq s; 120 121 memset(&record, 0, sizeof(record)); 122 record.cpu = cpu; 123 record.size = size; 124 record.data = data; 125 126 trace_seq_init(&s); 127 pevent_event_info(&s, event, &record); 128 trace_seq_do_fprintf(&s, fp); 129 trace_seq_destroy(&s); 130 } 131 132 void event_format__print(struct event_format *event, 133 int cpu, void *data, int size) 134 { 135 return event_format__fprintf(event, cpu, data, size, stdout); 136 } 137 138 void parse_ftrace_printk(struct pevent *pevent, 139 char *file, unsigned int size __maybe_unused) 140 { 141 unsigned long long addr; 142 char *printk; 143 char *line; 144 char *next = NULL; 145 char *addr_str; 146 char *fmt = NULL; 147 148 line = strtok_r(file, "\n", &next); 149 while (line) { 150 addr_str = strtok_r(line, ":", &fmt); 151 if (!addr_str) { 152 warning("printk format with empty entry"); 153 break; 154 } 155 addr = strtoull(addr_str, NULL, 16); 156 /* fmt still has a space, skip it */ 157 printk = strdup(fmt+1); 158 line = strtok_r(NULL, "\n", &next); 159 pevent_register_print_string(pevent, printk, addr); 160 } 161 } 162 163 void parse_saved_cmdline(struct pevent *pevent, 164 char *file, unsigned int size __maybe_unused) 165 { 166 char *comm; 167 char *line; 168 char *next = NULL; 169 int pid; 170 171 line = strtok_r(file, "\n", &next); 172 while (line) { 173 sscanf(line, "%d %ms", &pid, &comm); 174 pevent_register_comm(pevent, comm, pid); 175 free(comm); 176 line = strtok_r(NULL, "\n", &next); 177 } 178 } 179 180 int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size) 181 { 182 return pevent_parse_event(pevent, buf, size, "ftrace"); 183 } 184 185 int parse_event_file(struct pevent *pevent, 186 char *buf, unsigned long size, char *sys) 187 { 188 return pevent_parse_event(pevent, buf, size, sys); 189 } 190 191 struct event_format *trace_find_next_event(struct pevent *pevent, 192 struct event_format *event) 193 { 194 static int idx; 195 196 if (!pevent || !pevent->events) 197 return NULL; 198 199 if (!event) { 200 idx = 0; 201 return pevent->events[0]; 202 } 203 204 if (idx < pevent->nr_events && event == pevent->events[idx]) { 205 idx++; 206 if (idx == pevent->nr_events) 207 return NULL; 208 return pevent->events[idx]; 209 } 210 211 for (idx = 1; idx < pevent->nr_events; idx++) { 212 if (event == pevent->events[idx - 1]) 213 return pevent->events[idx]; 214 } 215 return NULL; 216 } 217 218 struct flag { 219 const char *name; 220 unsigned long long value; 221 }; 222 223 static const struct flag flags[] = { 224 { "HI_SOFTIRQ", 0 }, 225 { "TIMER_SOFTIRQ", 1 }, 226 { "NET_TX_SOFTIRQ", 2 }, 227 { "NET_RX_SOFTIRQ", 3 }, 228 { "BLOCK_SOFTIRQ", 4 }, 229 { "IRQ_POLL_SOFTIRQ", 5 }, 230 { "TASKLET_SOFTIRQ", 6 }, 231 { "SCHED_SOFTIRQ", 7 }, 232 { "HRTIMER_SOFTIRQ", 8 }, 233 { "RCU_SOFTIRQ", 9 }, 234 235 { "HRTIMER_NORESTART", 0 }, 236 { "HRTIMER_RESTART", 1 }, 237 }; 238 239 unsigned long long eval_flag(const char *flag) 240 { 241 int i; 242 243 /* 244 * Some flags in the format files do not get converted. 245 * If the flag is not numeric, see if it is something that 246 * we already know about. 247 */ 248 if (isdigit(flag[0])) 249 return strtoull(flag, NULL, 0); 250 251 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 252 if (strcmp(flags[i].name, flag) == 0) 253 return flags[i].value; 254 255 return 0; 256 } 257