Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 27 | #include "../perf.h" |
Steven Rostedt | ea4010d | 2009-08-17 16:18:07 +0200 | [diff] [blame] | 28 | #include "util.h" |
| 29 | #include "trace-event.h" |
| 30 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 31 | struct pevent *read_trace_init(int file_bigendian, int host_bigendian) |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 32 | { |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 33 | struct pevent *pevent = pevent_alloc(); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 34 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 35 | if (pevent != NULL) { |
| 36 | pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT); |
| 37 | pevent_set_file_bigendian(pevent, file_bigendian); |
| 38 | pevent_set_host_bigendian(pevent, host_bigendian); |
| 39 | } |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 40 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 41 | return pevent; |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 44 | static int get_common_field(struct scripting_context *context, |
| 45 | int *offset, int *size, const char *type) |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 46 | { |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 47 | struct pevent *pevent = context->pevent; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 48 | struct event_format *event; |
| 49 | struct format_field *field; |
| 50 | |
| 51 | if (!*size) { |
| 52 | if (!pevent->events) |
| 53 | return 0; |
| 54 | |
| 55 | event = pevent->events[0]; |
| 56 | field = pevent_find_common_field(event, type); |
| 57 | if (!field) |
| 58 | return 0; |
| 59 | *offset = field->offset; |
| 60 | *size = field->size; |
| 61 | } |
| 62 | |
| 63 | return pevent_read_number(pevent, context->event_data + *offset, *size); |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int common_lock_depth(struct scripting_context *context) |
| 67 | { |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 68 | static int offset; |
| 69 | static int size; |
| 70 | int ret; |
| 71 | |
| 72 | ret = get_common_field(context, &size, &offset, |
| 73 | "common_lock_depth"); |
| 74 | if (ret < 0) |
| 75 | return -1; |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | int common_flags(struct scripting_context *context) |
| 81 | { |
| 82 | static int offset; |
| 83 | static int size; |
| 84 | int ret; |
| 85 | |
| 86 | ret = get_common_field(context, &size, &offset, |
| 87 | "common_flags"); |
| 88 | if (ret < 0) |
| 89 | return -1; |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int common_pc(struct scripting_context *context) |
| 95 | { |
| 96 | static int offset; |
| 97 | static int size; |
| 98 | int ret; |
| 99 | |
| 100 | ret = get_common_field(context, &size, &offset, |
| 101 | "common_preempt_count"); |
| 102 | if (ret < 0) |
| 103 | return -1; |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | unsigned long long |
| 109 | raw_field_value(struct event_format *event, const char *name, void *data) |
| 110 | { |
| 111 | struct format_field *field; |
| 112 | unsigned long long val; |
| 113 | |
| 114 | field = pevent_find_any_field(event, name); |
| 115 | if (!field) |
| 116 | return 0ULL; |
| 117 | |
| 118 | pevent_read_number_field(field, data, &val); |
| 119 | |
| 120 | return val; |
| 121 | } |
| 122 | |
| 123 | void *raw_field_ptr(struct event_format *event, const char *name, void *data) |
| 124 | { |
| 125 | struct format_field *field; |
| 126 | |
| 127 | field = pevent_find_any_field(event, name); |
| 128 | if (!field) |
| 129 | return NULL; |
| 130 | |
| 131 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 132 | int offset; |
| 133 | |
| 134 | offset = *(int *)(data + field->offset); |
| 135 | offset &= 0xffff; |
| 136 | |
| 137 | return data + offset; |
| 138 | } |
| 139 | |
| 140 | return data + field->offset; |
| 141 | } |
| 142 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 143 | int trace_parse_common_type(struct pevent *pevent, void *data) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 144 | { |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 145 | struct pevent_record record; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 146 | |
| 147 | record.data = data; |
| 148 | return pevent_data_type(pevent, &record); |
| 149 | } |
| 150 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 151 | int trace_parse_common_pid(struct pevent *pevent, void *data) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 152 | { |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 153 | struct pevent_record record; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 154 | |
| 155 | record.data = data; |
| 156 | return pevent_data_pid(pevent, &record); |
| 157 | } |
| 158 | |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 159 | unsigned long long read_size(struct event_format *event, void *ptr, int size) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 160 | { |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 161 | return pevent_read_number(event->pevent, ptr, size); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 164 | void event_format__print(struct event_format *event, |
| 165 | int cpu, void *data, int size) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 166 | { |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 167 | struct pevent_record record; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 168 | struct trace_seq s; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 169 | |
| 170 | memset(&record, 0, sizeof(record)); |
| 171 | record.cpu = cpu; |
| 172 | record.size = size; |
| 173 | record.data = data; |
| 174 | |
| 175 | trace_seq_init(&s); |
David Ahern | 76a8349 | 2012-06-14 12:36:17 -0600 | [diff] [blame] | 176 | pevent_event_info(&s, event, &record); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 177 | trace_seq_do_printf(&s); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 180 | void parse_proc_kallsyms(struct pevent *pevent, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 181 | char *file, unsigned int size __maybe_unused) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 182 | { |
| 183 | unsigned long long addr; |
| 184 | char *func; |
| 185 | char *line; |
| 186 | char *next = NULL; |
| 187 | char *addr_str; |
| 188 | char *mod; |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 189 | char *fmt; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 190 | |
| 191 | line = strtok_r(file, "\n", &next); |
| 192 | while (line) { |
| 193 | mod = NULL; |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 194 | addr_str = strtok_r(line, " ", &fmt); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 195 | addr = strtoull(addr_str, NULL, 16); |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 196 | /* skip character */ |
| 197 | strtok_r(NULL, " ", &fmt); |
| 198 | func = strtok_r(NULL, "\t", &fmt); |
| 199 | mod = strtok_r(NULL, "]", &fmt); |
| 200 | /* truncate the extra '[' */ |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 201 | if (mod) |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 202 | mod = mod + 1; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 203 | |
| 204 | pevent_register_function(pevent, func, addr, mod); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 205 | |
| 206 | line = strtok_r(NULL, "\n", &next); |
| 207 | } |
| 208 | } |
| 209 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 210 | void parse_ftrace_printk(struct pevent *pevent, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 211 | char *file, unsigned int size __maybe_unused) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 212 | { |
| 213 | unsigned long long addr; |
| 214 | char *printk; |
| 215 | char *line; |
| 216 | char *next = NULL; |
| 217 | char *addr_str; |
| 218 | char *fmt; |
| 219 | |
| 220 | line = strtok_r(file, "\n", &next); |
| 221 | while (line) { |
| 222 | addr_str = strtok_r(line, ":", &fmt); |
| 223 | if (!addr_str) { |
| 224 | warning("printk format with empty entry"); |
| 225 | break; |
| 226 | } |
| 227 | addr = strtoull(addr_str, NULL, 16); |
| 228 | /* fmt still has a space, skip it */ |
| 229 | printk = strdup(fmt+1); |
| 230 | line = strtok_r(NULL, "\n", &next); |
| 231 | pevent_register_print_string(pevent, printk, addr); |
| 232 | } |
| 233 | } |
| 234 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 235 | int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 236 | { |
| 237 | return pevent_parse_event(pevent, buf, size, "ftrace"); |
| 238 | } |
| 239 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 240 | int parse_event_file(struct pevent *pevent, |
| 241 | char *buf, unsigned long size, char *sys) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 242 | { |
| 243 | return pevent_parse_event(pevent, buf, size, sys); |
| 244 | } |
| 245 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 246 | struct event_format *trace_find_next_event(struct pevent *pevent, |
| 247 | struct event_format *event) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 248 | { |
| 249 | static int idx; |
| 250 | |
Namhyung Kim | d0d3913 | 2012-08-14 10:57:03 +0900 | [diff] [blame] | 251 | if (!pevent || !pevent->events) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 252 | return NULL; |
| 253 | |
| 254 | if (!event) { |
| 255 | idx = 0; |
| 256 | return pevent->events[0]; |
| 257 | } |
| 258 | |
| 259 | if (idx < pevent->nr_events && event == pevent->events[idx]) { |
| 260 | idx++; |
| 261 | if (idx == pevent->nr_events) |
| 262 | return NULL; |
| 263 | return pevent->events[idx]; |
| 264 | } |
| 265 | |
| 266 | for (idx = 1; idx < pevent->nr_events; idx++) { |
| 267 | if (event == pevent->events[idx - 1]) |
| 268 | return pevent->events[idx]; |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | struct flag { |
| 274 | const char *name; |
| 275 | unsigned long long value; |
| 276 | }; |
| 277 | |
| 278 | static const struct flag flags[] = { |
| 279 | { "HI_SOFTIRQ", 0 }, |
| 280 | { "TIMER_SOFTIRQ", 1 }, |
| 281 | { "NET_TX_SOFTIRQ", 2 }, |
| 282 | { "NET_RX_SOFTIRQ", 3 }, |
| 283 | { "BLOCK_SOFTIRQ", 4 }, |
| 284 | { "BLOCK_IOPOLL_SOFTIRQ", 5 }, |
| 285 | { "TASKLET_SOFTIRQ", 6 }, |
| 286 | { "SCHED_SOFTIRQ", 7 }, |
| 287 | { "HRTIMER_SOFTIRQ", 8 }, |
| 288 | { "RCU_SOFTIRQ", 9 }, |
| 289 | |
| 290 | { "HRTIMER_NORESTART", 0 }, |
| 291 | { "HRTIMER_RESTART", 1 }, |
| 292 | }; |
| 293 | |
| 294 | unsigned long long eval_flag(const char *flag) |
| 295 | { |
| 296 | int i; |
| 297 | |
| 298 | /* |
| 299 | * Some flags in the format files do not get converted. |
| 300 | * If the flag is not numeric, see if it is something that |
| 301 | * we already know about. |
| 302 | */ |
| 303 | if (isdigit(flag[0])) |
| 304 | return strtoull(flag, NULL, 0); |
| 305 | |
| 306 | for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) |
| 307 | if (strcmp(flags[i].name, flag) == 0) |
| 308 | return flags[i].value; |
| 309 | |
| 310 | return 0; |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 311 | } |