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 | |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 31 | static int get_common_field(struct scripting_context *context, |
| 32 | int *offset, int *size, const char *type) |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 33 | { |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 34 | struct pevent *pevent = context->pevent; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 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); |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int common_lock_depth(struct scripting_context *context) |
| 54 | { |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 110 | 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] | 111 | { |
Arnaldo Carvalho de Melo | 9782243 | 2012-08-07 23:50:21 -0300 | [diff] [blame] | 112 | return pevent_read_number(event->pevent, ptr, size); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 115 | void event_format__print(struct event_format *event, |
| 116 | int cpu, void *data, int size) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 117 | { |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 118 | struct pevent_record record; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 119 | struct trace_seq s; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 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); |
David Ahern | 76a8349 | 2012-06-14 12:36:17 -0600 | [diff] [blame] | 127 | pevent_event_info(&s, event, &record); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 128 | trace_seq_do_printf(&s); |
Jiri Olsa | b58f608 | 2014-02-02 22:38:49 +0100 | [diff] [blame] | 129 | trace_seq_destroy(&s); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 132 | void parse_proc_kallsyms(struct pevent *pevent, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 133 | char *file, unsigned int size __maybe_unused) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 134 | { |
| 135 | unsigned long long addr; |
| 136 | char *func; |
| 137 | char *line; |
| 138 | char *next = NULL; |
| 139 | char *addr_str; |
| 140 | char *mod; |
Ingo Molnar | 0f96542 | 2013-09-12 15:16:49 +0200 | [diff] [blame] | 141 | char *fmt = NULL; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 142 | |
| 143 | line = strtok_r(file, "\n", &next); |
| 144 | while (line) { |
| 145 | mod = NULL; |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 146 | addr_str = strtok_r(line, " ", &fmt); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 147 | addr = strtoull(addr_str, NULL, 16); |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 148 | /* skip character */ |
| 149 | strtok_r(NULL, " ", &fmt); |
| 150 | func = strtok_r(NULL, "\t", &fmt); |
| 151 | mod = strtok_r(NULL, "]", &fmt); |
| 152 | /* truncate the extra '[' */ |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 153 | if (mod) |
Irina Tirdea | bcbd004 | 2012-09-20 23:37:50 +0300 | [diff] [blame] | 154 | mod = mod + 1; |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 155 | |
| 156 | pevent_register_function(pevent, func, addr, mod); |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 157 | |
| 158 | line = strtok_r(NULL, "\n", &next); |
| 159 | } |
| 160 | } |
| 161 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 162 | void parse_ftrace_printk(struct pevent *pevent, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 163 | char *file, unsigned int size __maybe_unused) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 164 | { |
| 165 | unsigned long long addr; |
| 166 | char *printk; |
| 167 | char *line; |
| 168 | char *next = NULL; |
| 169 | char *addr_str; |
| 170 | char *fmt; |
| 171 | |
| 172 | line = strtok_r(file, "\n", &next); |
| 173 | while (line) { |
| 174 | addr_str = strtok_r(line, ":", &fmt); |
| 175 | if (!addr_str) { |
| 176 | warning("printk format with empty entry"); |
| 177 | break; |
| 178 | } |
| 179 | addr = strtoull(addr_str, NULL, 16); |
| 180 | /* fmt still has a space, skip it */ |
| 181 | printk = strdup(fmt+1); |
| 182 | line = strtok_r(NULL, "\n", &next); |
| 183 | pevent_register_print_string(pevent, printk, addr); |
| 184 | } |
| 185 | } |
| 186 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 187 | int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 188 | { |
| 189 | return pevent_parse_event(pevent, buf, size, "ftrace"); |
| 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 192 | int parse_event_file(struct pevent *pevent, |
| 193 | char *buf, unsigned long size, char *sys) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 194 | { |
| 195 | return pevent_parse_event(pevent, buf, size, sys); |
| 196 | } |
| 197 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 198 | struct event_format *trace_find_next_event(struct pevent *pevent, |
| 199 | struct event_format *event) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 200 | { |
| 201 | static int idx; |
| 202 | |
Namhyung Kim | d0d3913 | 2012-08-14 10:57:03 +0900 | [diff] [blame] | 203 | if (!pevent || !pevent->events) |
Steven Rostedt | aaf045f | 2012-04-06 00:47:56 +0200 | [diff] [blame] | 204 | return NULL; |
| 205 | |
| 206 | if (!event) { |
| 207 | idx = 0; |
| 208 | return pevent->events[0]; |
| 209 | } |
| 210 | |
| 211 | if (idx < pevent->nr_events && event == pevent->events[idx]) { |
| 212 | idx++; |
| 213 | if (idx == pevent->nr_events) |
| 214 | return NULL; |
| 215 | return pevent->events[idx]; |
| 216 | } |
| 217 | |
| 218 | for (idx = 1; idx < pevent->nr_events; idx++) { |
| 219 | if (event == pevent->events[idx - 1]) |
| 220 | return pevent->events[idx]; |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | struct flag { |
| 226 | const char *name; |
| 227 | unsigned long long value; |
| 228 | }; |
| 229 | |
| 230 | static const struct flag flags[] = { |
| 231 | { "HI_SOFTIRQ", 0 }, |
| 232 | { "TIMER_SOFTIRQ", 1 }, |
| 233 | { "NET_TX_SOFTIRQ", 2 }, |
| 234 | { "NET_RX_SOFTIRQ", 3 }, |
| 235 | { "BLOCK_SOFTIRQ", 4 }, |
| 236 | { "BLOCK_IOPOLL_SOFTIRQ", 5 }, |
| 237 | { "TASKLET_SOFTIRQ", 6 }, |
| 238 | { "SCHED_SOFTIRQ", 7 }, |
| 239 | { "HRTIMER_SOFTIRQ", 8 }, |
| 240 | { "RCU_SOFTIRQ", 9 }, |
| 241 | |
| 242 | { "HRTIMER_NORESTART", 0 }, |
| 243 | { "HRTIMER_RESTART", 1 }, |
| 244 | }; |
| 245 | |
| 246 | unsigned long long eval_flag(const char *flag) |
| 247 | { |
| 248 | int i; |
| 249 | |
| 250 | /* |
| 251 | * Some flags in the format files do not get converted. |
| 252 | * If the flag is not numeric, see if it is something that |
| 253 | * we already know about. |
| 254 | */ |
| 255 | if (isdigit(flag[0])) |
| 256 | return strtoull(flag, NULL, 0); |
| 257 | |
| 258 | for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) |
| 259 | if (strcmp(flags[i].name, flag) == 0) |
| 260 | return flags[i].value; |
| 261 | |
| 262 | return 0; |
Tom Zanussi | 7397d80 | 2010-01-27 02:27:54 -0600 | [diff] [blame] | 263 | } |