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 int header_page_size_size; 32 int header_page_ts_size; 33 int header_page_data_offset; 34 35 struct pevent *perf_pevent; 36 static struct pevent *pevent; 37 38 bool latency_format; 39 40 int read_trace_init(int file_bigendian, int host_bigendian) 41 { 42 if (pevent) 43 return 0; 44 45 perf_pevent = pevent_alloc(); 46 pevent = perf_pevent; 47 48 pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT); 49 pevent_set_file_bigendian(pevent, file_bigendian); 50 pevent_set_host_bigendian(pevent, host_bigendian); 51 52 return 0; 53 } 54 55 static int get_common_field(struct scripting_context *context, 56 int *offset, int *size, const char *type) 57 { 58 struct event_format *event; 59 struct format_field *field; 60 61 if (!*size) { 62 if (!pevent->events) 63 return 0; 64 65 event = pevent->events[0]; 66 field = pevent_find_common_field(event, type); 67 if (!field) 68 return 0; 69 *offset = field->offset; 70 *size = field->size; 71 } 72 73 return pevent_read_number(pevent, context->event_data + *offset, *size); 74 } 75 76 int common_lock_depth(struct scripting_context *context) 77 { 78 static int offset; 79 static int size; 80 int ret; 81 82 ret = get_common_field(context, &size, &offset, 83 "common_lock_depth"); 84 if (ret < 0) 85 return -1; 86 87 return ret; 88 } 89 90 int common_flags(struct scripting_context *context) 91 { 92 static int offset; 93 static int size; 94 int ret; 95 96 ret = get_common_field(context, &size, &offset, 97 "common_flags"); 98 if (ret < 0) 99 return -1; 100 101 return ret; 102 } 103 104 int common_pc(struct scripting_context *context) 105 { 106 static int offset; 107 static int size; 108 int ret; 109 110 ret = get_common_field(context, &size, &offset, 111 "common_preempt_count"); 112 if (ret < 0) 113 return -1; 114 115 return ret; 116 } 117 118 unsigned long long 119 raw_field_value(struct event_format *event, const char *name, void *data) 120 { 121 struct format_field *field; 122 unsigned long long val; 123 124 field = pevent_find_any_field(event, name); 125 if (!field) 126 return 0ULL; 127 128 pevent_read_number_field(field, data, &val); 129 130 return val; 131 } 132 133 void *raw_field_ptr(struct event_format *event, const char *name, void *data) 134 { 135 struct format_field *field; 136 137 field = pevent_find_any_field(event, name); 138 if (!field) 139 return NULL; 140 141 if (field->flags & FIELD_IS_DYNAMIC) { 142 int offset; 143 144 offset = *(int *)(data + field->offset); 145 offset &= 0xffff; 146 147 return data + offset; 148 } 149 150 return data + field->offset; 151 } 152 153 int trace_parse_common_type(void *data) 154 { 155 struct pevent_record record; 156 157 record.data = data; 158 return pevent_data_type(pevent, &record); 159 } 160 161 int trace_parse_common_pid(void *data) 162 { 163 struct pevent_record record; 164 165 record.data = data; 166 return pevent_data_pid(pevent, &record); 167 } 168 169 unsigned long long read_size(void *ptr, int size) 170 { 171 return pevent_read_number(pevent, ptr, size); 172 } 173 174 struct event_format *trace_find_event(int type) 175 { 176 return pevent_find_event(pevent, type); 177 } 178 179 180 void print_trace_event(int cpu, void *data, int size) 181 { 182 struct event_format *event; 183 struct pevent_record record; 184 struct trace_seq s; 185 int type; 186 187 type = trace_parse_common_type(data); 188 189 event = trace_find_event(type); 190 if (!event) { 191 warning("ug! no event found for type %d", type); 192 return; 193 } 194 195 memset(&record, 0, sizeof(record)); 196 record.cpu = cpu; 197 record.size = size; 198 record.data = data; 199 200 trace_seq_init(&s); 201 pevent_print_event(pevent, &s, &record); 202 trace_seq_do_printf(&s); 203 printf("\n"); 204 } 205 206 void print_event(int cpu, void *data, int size, unsigned long long nsecs, 207 char *comm) 208 { 209 struct pevent_record record; 210 struct trace_seq s; 211 int pid; 212 213 pevent->latency_format = latency_format; 214 215 record.ts = nsecs; 216 record.cpu = cpu; 217 record.size = size; 218 record.data = data; 219 pid = pevent_data_pid(pevent, &record); 220 221 if (!pevent_pid_is_registered(pevent, pid)) 222 pevent_register_comm(pevent, comm, pid); 223 224 trace_seq_init(&s); 225 pevent_print_event(pevent, &s, &record); 226 trace_seq_do_printf(&s); 227 printf("\n"); 228 } 229 230 void parse_proc_kallsyms(char *file, unsigned int size __unused) 231 { 232 unsigned long long addr; 233 char *func; 234 char *line; 235 char *next = NULL; 236 char *addr_str; 237 char *mod; 238 char ch; 239 240 line = strtok_r(file, "\n", &next); 241 while (line) { 242 mod = NULL; 243 sscanf(line, "%as %c %as\t[%as", 244 (float *)(void *)&addr_str, /* workaround gcc warning */ 245 &ch, (float *)(void *)&func, (float *)(void *)&mod); 246 addr = strtoull(addr_str, NULL, 16); 247 free(addr_str); 248 249 /* truncate the extra ']' */ 250 if (mod) 251 mod[strlen(mod) - 1] = 0; 252 253 pevent_register_function(pevent, func, addr, mod); 254 free(func); 255 free(mod); 256 257 line = strtok_r(NULL, "\n", &next); 258 } 259 } 260 261 void parse_ftrace_printk(char *file, unsigned int size __unused) 262 { 263 unsigned long long addr; 264 char *printk; 265 char *line; 266 char *next = NULL; 267 char *addr_str; 268 char *fmt; 269 270 line = strtok_r(file, "\n", &next); 271 while (line) { 272 addr_str = strtok_r(line, ":", &fmt); 273 if (!addr_str) { 274 warning("printk format with empty entry"); 275 break; 276 } 277 addr = strtoull(addr_str, NULL, 16); 278 /* fmt still has a space, skip it */ 279 printk = strdup(fmt+1); 280 line = strtok_r(NULL, "\n", &next); 281 pevent_register_print_string(pevent, printk, addr); 282 } 283 } 284 285 int parse_ftrace_file(char *buf, unsigned long size) 286 { 287 return pevent_parse_event(pevent, buf, size, "ftrace"); 288 } 289 290 int parse_event_file(char *buf, unsigned long size, char *sys) 291 { 292 return pevent_parse_event(pevent, buf, size, sys); 293 } 294 295 struct event_format *trace_find_next_event(struct event_format *event) 296 { 297 static int idx; 298 299 if (!pevent->events) 300 return NULL; 301 302 if (!event) { 303 idx = 0; 304 return pevent->events[0]; 305 } 306 307 if (idx < pevent->nr_events && event == pevent->events[idx]) { 308 idx++; 309 if (idx == pevent->nr_events) 310 return NULL; 311 return pevent->events[idx]; 312 } 313 314 for (idx = 1; idx < pevent->nr_events; idx++) { 315 if (event == pevent->events[idx - 1]) 316 return pevent->events[idx]; 317 } 318 return NULL; 319 } 320 321 struct flag { 322 const char *name; 323 unsigned long long value; 324 }; 325 326 static const struct flag flags[] = { 327 { "HI_SOFTIRQ", 0 }, 328 { "TIMER_SOFTIRQ", 1 }, 329 { "NET_TX_SOFTIRQ", 2 }, 330 { "NET_RX_SOFTIRQ", 3 }, 331 { "BLOCK_SOFTIRQ", 4 }, 332 { "BLOCK_IOPOLL_SOFTIRQ", 5 }, 333 { "TASKLET_SOFTIRQ", 6 }, 334 { "SCHED_SOFTIRQ", 7 }, 335 { "HRTIMER_SOFTIRQ", 8 }, 336 { "RCU_SOFTIRQ", 9 }, 337 338 { "HRTIMER_NORESTART", 0 }, 339 { "HRTIMER_RESTART", 1 }, 340 }; 341 342 unsigned long long eval_flag(const char *flag) 343 { 344 int i; 345 346 /* 347 * Some flags in the format files do not get converted. 348 * If the flag is not numeric, see if it is something that 349 * we already know about. 350 */ 351 if (isdigit(flag[0])) 352 return strtoull(flag, NULL, 0); 353 354 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 355 if (strcmp(flags[i].name, flag) == 0) 356 return flags[i].value; 357 358 return 0; 359 } 360