Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * event tracer |
| 3 | * |
| 4 | * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 5 | * |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 6 | * - Added format output of fields of the trace point. |
| 7 | * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. |
| 8 | * |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 9 | */ |
| 10 | |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 11 | #include <linux/workqueue.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/kthread.h> |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/ctype.h> |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 18 | #include <linux/delay.h> |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 19 | |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 20 | #include "trace_output.h" |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 21 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 22 | #define TRACE_SYSTEM "TRACE_SYSTEM" |
| 23 | |
Steven Rostedt | 11a241a | 2009-03-02 11:49:04 -0500 | [diff] [blame] | 24 | static DEFINE_MUTEX(event_mutex); |
| 25 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 26 | LIST_HEAD(ftrace_events); |
| 27 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 28 | int trace_define_field(struct ftrace_event_call *call, char *type, |
| 29 | char *name, int offset, int size) |
| 30 | { |
| 31 | struct ftrace_event_field *field; |
| 32 | |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 33 | field = kzalloc(sizeof(*field), GFP_KERNEL); |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 34 | if (!field) |
| 35 | goto err; |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 36 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 37 | field->name = kstrdup(name, GFP_KERNEL); |
| 38 | if (!field->name) |
| 39 | goto err; |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 40 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 41 | field->type = kstrdup(type, GFP_KERNEL); |
| 42 | if (!field->type) |
| 43 | goto err; |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 44 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 45 | field->offset = offset; |
| 46 | field->size = size; |
| 47 | list_add(&field->link, &call->fields); |
| 48 | |
| 49 | return 0; |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 50 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 51 | err: |
| 52 | if (field) { |
| 53 | kfree(field->name); |
| 54 | kfree(field->type); |
| 55 | } |
| 56 | kfree(field); |
Ingo Molnar | fe9f57f2 | 2009-03-22 18:41:59 +0100 | [diff] [blame] | 57 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 58 | return -ENOMEM; |
| 59 | } |
Steven Rostedt | 17c873e | 2009-04-10 18:12:50 -0400 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(trace_define_field); |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 61 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 62 | static void ftrace_clear_events(void) |
| 63 | { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 64 | struct ftrace_event_call *call; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 65 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 66 | list_for_each_entry(call, &ftrace_events, list) { |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 67 | |
| 68 | if (call->enabled) { |
| 69 | call->enabled = 0; |
| 70 | call->unregfunc(); |
| 71 | } |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 75 | static void ftrace_event_enable_disable(struct ftrace_event_call *call, |
| 76 | int enable) |
| 77 | { |
| 78 | |
| 79 | switch (enable) { |
| 80 | case 0: |
| 81 | if (call->enabled) { |
| 82 | call->enabled = 0; |
| 83 | call->unregfunc(); |
| 84 | } |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 85 | break; |
| 86 | case 1: |
Steven Rostedt | da4d030 | 2009-03-09 17:14:30 -0400 | [diff] [blame] | 87 | if (!call->enabled) { |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 88 | call->enabled = 1; |
| 89 | call->regfunc(); |
| 90 | } |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 95 | static int ftrace_set_clr_event(char *buf, int set) |
| 96 | { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 97 | struct ftrace_event_call *call; |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 98 | char *event = NULL, *sub = NULL, *match; |
| 99 | int ret = -EINVAL; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 100 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 101 | /* |
| 102 | * The buf format can be <subsystem>:<event-name> |
| 103 | * *:<event-name> means any event by that name. |
| 104 | * :<event-name> is the same. |
| 105 | * |
| 106 | * <subsystem>:* means all events in that subsystem |
| 107 | * <subsystem>: means the same. |
| 108 | * |
| 109 | * <name> (no ':') means all events in a subsystem with |
| 110 | * the name <name> or any event that matches <name> |
| 111 | */ |
| 112 | |
| 113 | match = strsep(&buf, ":"); |
| 114 | if (buf) { |
| 115 | sub = match; |
| 116 | event = buf; |
| 117 | match = NULL; |
| 118 | |
| 119 | if (!strlen(sub) || strcmp(sub, "*") == 0) |
| 120 | sub = NULL; |
| 121 | if (!strlen(event) || strcmp(event, "*") == 0) |
| 122 | event = NULL; |
| 123 | } |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 124 | |
Steven Rostedt | 11a241a | 2009-03-02 11:49:04 -0500 | [diff] [blame] | 125 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 126 | list_for_each_entry(call, &ftrace_events, list) { |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 127 | |
Steven Rostedt | 40e2681 | 2009-03-10 11:32:40 -0400 | [diff] [blame] | 128 | if (!call->name || !call->regfunc) |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 129 | continue; |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 130 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 131 | if (match && |
| 132 | strcmp(match, call->name) != 0 && |
| 133 | strcmp(match, call->system) != 0) |
| 134 | continue; |
| 135 | |
| 136 | if (sub && strcmp(sub, call->system) != 0) |
| 137 | continue; |
| 138 | |
| 139 | if (event && strcmp(event, call->name) != 0) |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 140 | continue; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 141 | |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 142 | ftrace_event_enable_disable(call, set); |
| 143 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 144 | ret = 0; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 145 | } |
Steven Rostedt | 11a241a | 2009-03-02 11:49:04 -0500 | [diff] [blame] | 146 | mutex_unlock(&event_mutex); |
| 147 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 148 | return ret; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* 128 should be much more than enough */ |
| 152 | #define EVENT_BUF_SIZE 127 |
| 153 | |
| 154 | static ssize_t |
| 155 | ftrace_event_write(struct file *file, const char __user *ubuf, |
| 156 | size_t cnt, loff_t *ppos) |
| 157 | { |
| 158 | size_t read = 0; |
| 159 | int i, set = 1; |
| 160 | ssize_t ret; |
| 161 | char *buf; |
| 162 | char ch; |
| 163 | |
| 164 | if (!cnt || cnt < 0) |
| 165 | return 0; |
| 166 | |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 167 | ret = tracing_update_buffers(); |
| 168 | if (ret < 0) |
| 169 | return ret; |
| 170 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 171 | ret = get_user(ch, ubuf++); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | read++; |
| 175 | cnt--; |
| 176 | |
| 177 | /* skip white space */ |
| 178 | while (cnt && isspace(ch)) { |
| 179 | ret = get_user(ch, ubuf++); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | read++; |
| 183 | cnt--; |
| 184 | } |
| 185 | |
| 186 | /* Only white space found? */ |
| 187 | if (isspace(ch)) { |
| 188 | file->f_pos += read; |
| 189 | ret = read; |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL); |
| 194 | if (!buf) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | if (cnt > EVENT_BUF_SIZE) |
| 198 | cnt = EVENT_BUF_SIZE; |
| 199 | |
| 200 | i = 0; |
| 201 | while (cnt && !isspace(ch)) { |
| 202 | if (!i && ch == '!') |
| 203 | set = 0; |
| 204 | else |
| 205 | buf[i++] = ch; |
| 206 | |
| 207 | ret = get_user(ch, ubuf++); |
| 208 | if (ret) |
| 209 | goto out_free; |
| 210 | read++; |
| 211 | cnt--; |
| 212 | } |
| 213 | buf[i] = 0; |
| 214 | |
| 215 | file->f_pos += read; |
| 216 | |
| 217 | ret = ftrace_set_clr_event(buf, set); |
| 218 | if (ret) |
| 219 | goto out_free; |
| 220 | |
| 221 | ret = read; |
| 222 | |
| 223 | out_free: |
| 224 | kfree(buf); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static void * |
| 230 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 231 | { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 232 | struct list_head *list = m->private; |
| 233 | struct ftrace_event_call *call; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 234 | |
| 235 | (*pos)++; |
| 236 | |
Steven Rostedt | 40e2681 | 2009-03-10 11:32:40 -0400 | [diff] [blame] | 237 | for (;;) { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 238 | if (list == &ftrace_events) |
Steven Rostedt | 40e2681 | 2009-03-10 11:32:40 -0400 | [diff] [blame] | 239 | return NULL; |
| 240 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 241 | call = list_entry(list, struct ftrace_event_call, list); |
| 242 | |
Steven Rostedt | 40e2681 | 2009-03-10 11:32:40 -0400 | [diff] [blame] | 243 | /* |
| 244 | * The ftrace subsystem is for showing formats only. |
| 245 | * They can not be enabled or disabled via the event files. |
| 246 | */ |
| 247 | if (call->regfunc) |
| 248 | break; |
| 249 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 250 | list = list->next; |
Steven Rostedt | 40e2681 | 2009-03-10 11:32:40 -0400 | [diff] [blame] | 251 | } |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 252 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 253 | m->private = list->next; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 254 | |
| 255 | return call; |
| 256 | } |
| 257 | |
| 258 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 259 | { |
| 260 | return t_next(m, NULL, pos); |
| 261 | } |
| 262 | |
| 263 | static void * |
| 264 | s_next(struct seq_file *m, void *v, loff_t *pos) |
| 265 | { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 266 | struct list_head *list = m->private; |
| 267 | struct ftrace_event_call *call; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 268 | |
| 269 | (*pos)++; |
| 270 | |
| 271 | retry: |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 272 | if (list == &ftrace_events) |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 273 | return NULL; |
| 274 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 275 | call = list_entry(list, struct ftrace_event_call, list); |
| 276 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 277 | if (!call->enabled) { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 278 | list = list->next; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 279 | goto retry; |
| 280 | } |
| 281 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 282 | m->private = list->next; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 283 | |
| 284 | return call; |
| 285 | } |
| 286 | |
| 287 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 288 | { |
| 289 | return s_next(m, NULL, pos); |
| 290 | } |
| 291 | |
| 292 | static int t_show(struct seq_file *m, void *v) |
| 293 | { |
| 294 | struct ftrace_event_call *call = v; |
| 295 | |
Steven Rostedt | b628b3e | 2009-02-27 23:32:58 -0500 | [diff] [blame] | 296 | if (strcmp(call->system, TRACE_SYSTEM) != 0) |
| 297 | seq_printf(m, "%s:", call->system); |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 298 | seq_printf(m, "%s\n", call->name); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void t_stop(struct seq_file *m, void *p) |
| 304 | { |
| 305 | } |
| 306 | |
| 307 | static int |
| 308 | ftrace_event_seq_open(struct inode *inode, struct file *file) |
| 309 | { |
| 310 | int ret; |
| 311 | const struct seq_operations *seq_ops; |
| 312 | |
| 313 | if ((file->f_mode & FMODE_WRITE) && |
| 314 | !(file->f_flags & O_APPEND)) |
| 315 | ftrace_clear_events(); |
| 316 | |
| 317 | seq_ops = inode->i_private; |
| 318 | ret = seq_open(file, seq_ops); |
| 319 | if (!ret) { |
| 320 | struct seq_file *m = file->private_data; |
| 321 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 322 | m->private = ftrace_events.next; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 323 | } |
| 324 | return ret; |
| 325 | } |
| 326 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 327 | static ssize_t |
| 328 | event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 329 | loff_t *ppos) |
| 330 | { |
| 331 | struct ftrace_event_call *call = filp->private_data; |
| 332 | char *buf; |
| 333 | |
Steven Rostedt | da4d030 | 2009-03-09 17:14:30 -0400 | [diff] [blame] | 334 | if (call->enabled) |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 335 | buf = "1\n"; |
| 336 | else |
| 337 | buf = "0\n"; |
| 338 | |
| 339 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 340 | } |
| 341 | |
| 342 | static ssize_t |
| 343 | event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 344 | loff_t *ppos) |
| 345 | { |
| 346 | struct ftrace_event_call *call = filp->private_data; |
| 347 | char buf[64]; |
| 348 | unsigned long val; |
| 349 | int ret; |
| 350 | |
| 351 | if (cnt >= sizeof(buf)) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | if (copy_from_user(&buf, ubuf, cnt)) |
| 355 | return -EFAULT; |
| 356 | |
| 357 | buf[cnt] = 0; |
| 358 | |
| 359 | ret = strict_strtoul(buf, 10, &val); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
Steven Rostedt | 1852fcc | 2009-03-11 14:33:00 -0400 | [diff] [blame] | 363 | ret = tracing_update_buffers(); |
| 364 | if (ret < 0) |
| 365 | return ret; |
| 366 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 367 | switch (val) { |
| 368 | case 0: |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 369 | case 1: |
Steven Rostedt | 11a241a | 2009-03-02 11:49:04 -0500 | [diff] [blame] | 370 | mutex_lock(&event_mutex); |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 371 | ftrace_event_enable_disable(call, val); |
Steven Rostedt | 11a241a | 2009-03-02 11:49:04 -0500 | [diff] [blame] | 372 | mutex_unlock(&event_mutex); |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | default: |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | *ppos += cnt; |
| 380 | |
| 381 | return cnt; |
| 382 | } |
| 383 | |
Steven Rostedt | 75db37d | 2009-03-26 11:43:36 -0400 | [diff] [blame^] | 384 | extern char *__bad_type_size(void); |
| 385 | |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 386 | #undef FIELD |
Steven Rostedt | 156b5f1 | 2009-03-06 10:50:53 -0500 | [diff] [blame] | 387 | #define FIELD(type, name) \ |
Steven Rostedt | 75db37d | 2009-03-26 11:43:36 -0400 | [diff] [blame^] | 388 | sizeof(type) != sizeof(field.name) ? __bad_type_size() : \ |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 389 | #type, "common_" #name, offsetof(typeof(field), name), \ |
| 390 | sizeof(field.name) |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 391 | |
| 392 | static int trace_write_header(struct trace_seq *s) |
| 393 | { |
| 394 | struct trace_entry field; |
| 395 | |
| 396 | /* struct trace_entry */ |
| 397 | return trace_seq_printf(s, |
Steven Rostedt | ce8eb2b | 2009-03-10 10:14:35 -0400 | [diff] [blame] | 398 | "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n" |
| 399 | "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n" |
| 400 | "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n" |
| 401 | "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n" |
| 402 | "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n" |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 403 | "\n", |
Steven Rostedt | 89ec0de | 2009-03-26 11:03:29 -0400 | [diff] [blame] | 404 | FIELD(unsigned short, type), |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 405 | FIELD(unsigned char, flags), |
| 406 | FIELD(unsigned char, preempt_count), |
| 407 | FIELD(int, pid), |
| 408 | FIELD(int, tgid)); |
| 409 | } |
Steven Rostedt | da4d030 | 2009-03-09 17:14:30 -0400 | [diff] [blame] | 410 | |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 411 | static ssize_t |
| 412 | event_format_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 413 | loff_t *ppos) |
| 414 | { |
| 415 | struct ftrace_event_call *call = filp->private_data; |
| 416 | struct trace_seq *s; |
| 417 | char *buf; |
| 418 | int r; |
| 419 | |
Tom Zanussi | c269fc8 | 2009-03-17 01:20:59 -0500 | [diff] [blame] | 420 | if (*ppos) |
| 421 | return 0; |
| 422 | |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 423 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 424 | if (!s) |
| 425 | return -ENOMEM; |
| 426 | |
| 427 | trace_seq_init(s); |
| 428 | |
Steven Rostedt | c5e4e19 | 2009-03-02 15:10:02 -0500 | [diff] [blame] | 429 | /* If any of the first writes fail, so will the show_format. */ |
| 430 | |
| 431 | trace_seq_printf(s, "name: %s\n", call->name); |
| 432 | trace_seq_printf(s, "ID: %d\n", call->id); |
| 433 | trace_seq_printf(s, "format:\n"); |
Steven Rostedt | 91729ef9 | 2009-03-02 15:03:01 -0500 | [diff] [blame] | 434 | trace_write_header(s); |
| 435 | |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 436 | r = call->show_format(s); |
| 437 | if (!r) { |
| 438 | /* |
| 439 | * ug! The format output is bigger than a PAGE!! |
| 440 | */ |
| 441 | buf = "FORMAT TOO BIG\n"; |
| 442 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 443 | buf, strlen(buf)); |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 448 | s->buffer, s->len); |
| 449 | out: |
| 450 | kfree(s); |
| 451 | return r; |
| 452 | } |
| 453 | |
Peter Zijlstra | 23725ae | 2009-03-19 20:26:13 +0100 | [diff] [blame] | 454 | static ssize_t |
| 455 | event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 456 | { |
| 457 | struct ftrace_event_call *call = filp->private_data; |
| 458 | struct trace_seq *s; |
| 459 | int r; |
| 460 | |
| 461 | if (*ppos) |
| 462 | return 0; |
| 463 | |
| 464 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 465 | if (!s) |
| 466 | return -ENOMEM; |
| 467 | |
| 468 | trace_seq_init(s); |
| 469 | trace_seq_printf(s, "%d\n", call->id); |
| 470 | |
| 471 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 472 | s->buffer, s->len); |
| 473 | kfree(s); |
| 474 | return r; |
| 475 | } |
| 476 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 477 | static ssize_t |
| 478 | event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 479 | loff_t *ppos) |
| 480 | { |
| 481 | struct ftrace_event_call *call = filp->private_data; |
| 482 | struct trace_seq *s; |
| 483 | int r; |
| 484 | |
| 485 | if (*ppos) |
| 486 | return 0; |
| 487 | |
| 488 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 489 | if (!s) |
| 490 | return -ENOMEM; |
| 491 | |
| 492 | trace_seq_init(s); |
| 493 | |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 494 | filter_print_preds(call, s); |
Tom Zanussi | 4bda2d5 | 2009-03-24 02:14:31 -0500 | [diff] [blame] | 495 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 496 | |
| 497 | kfree(s); |
| 498 | |
| 499 | return r; |
| 500 | } |
| 501 | |
| 502 | static ssize_t |
| 503 | event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 504 | loff_t *ppos) |
| 505 | { |
| 506 | struct ftrace_event_call *call = filp->private_data; |
| 507 | char buf[64], *pbuf = buf; |
| 508 | struct filter_pred *pred; |
| 509 | int err; |
| 510 | |
| 511 | if (cnt >= sizeof(buf)) |
| 512 | return -EINVAL; |
| 513 | |
| 514 | if (copy_from_user(&buf, ubuf, cnt)) |
| 515 | return -EFAULT; |
Li Zefan | 8433a40 | 2009-04-11 15:52:18 +0800 | [diff] [blame] | 516 | buf[cnt] = '\0'; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 517 | |
| 518 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); |
| 519 | if (!pred) |
| 520 | return -ENOMEM; |
| 521 | |
| 522 | err = filter_parse(&pbuf, pred); |
| 523 | if (err < 0) { |
| 524 | filter_free_pred(pred); |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | if (pred->clear) { |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 529 | filter_disable_preds(call); |
Tom Zanussi | 09f1f24 | 2009-03-24 02:14:11 -0500 | [diff] [blame] | 530 | filter_free_pred(pred); |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 531 | return cnt; |
| 532 | } |
| 533 | |
Li Zefan | 44e9c8b | 2009-04-11 15:55:28 +0800 | [diff] [blame] | 534 | err = filter_add_pred(call, pred); |
| 535 | if (err < 0) { |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 536 | filter_free_pred(pred); |
Li Zefan | 44e9c8b | 2009-04-11 15:55:28 +0800 | [diff] [blame] | 537 | return err; |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 540 | filter_free_pred(pred); |
| 541 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 542 | *ppos += cnt; |
| 543 | |
| 544 | return cnt; |
| 545 | } |
| 546 | |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 547 | static ssize_t |
| 548 | subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 549 | loff_t *ppos) |
| 550 | { |
| 551 | struct event_subsystem *system = filp->private_data; |
| 552 | struct trace_seq *s; |
| 553 | int r; |
| 554 | |
| 555 | if (*ppos) |
| 556 | return 0; |
| 557 | |
| 558 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 559 | if (!s) |
| 560 | return -ENOMEM; |
| 561 | |
| 562 | trace_seq_init(s); |
| 563 | |
Tom Zanussi | ac1adc5 | 2009-04-17 00:27:08 -0500 | [diff] [blame] | 564 | filter_print_subsystem_preds(system, s); |
Tom Zanussi | 4bda2d5 | 2009-03-24 02:14:31 -0500 | [diff] [blame] | 565 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 566 | |
| 567 | kfree(s); |
| 568 | |
| 569 | return r; |
| 570 | } |
| 571 | |
| 572 | static ssize_t |
| 573 | subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 574 | loff_t *ppos) |
| 575 | { |
| 576 | struct event_subsystem *system = filp->private_data; |
| 577 | char buf[64], *pbuf = buf; |
| 578 | struct filter_pred *pred; |
| 579 | int err; |
| 580 | |
| 581 | if (cnt >= sizeof(buf)) |
| 582 | return -EINVAL; |
| 583 | |
| 584 | if (copy_from_user(&buf, ubuf, cnt)) |
| 585 | return -EFAULT; |
Li Zefan | 8433a40 | 2009-04-11 15:52:18 +0800 | [diff] [blame] | 586 | buf[cnt] = '\0'; |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 587 | |
| 588 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); |
| 589 | if (!pred) |
| 590 | return -ENOMEM; |
| 591 | |
| 592 | err = filter_parse(&pbuf, pred); |
| 593 | if (err < 0) { |
| 594 | filter_free_pred(pred); |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | if (pred->clear) { |
| 599 | filter_free_subsystem_preds(system); |
Tom Zanussi | 09f1f24 | 2009-03-24 02:14:11 -0500 | [diff] [blame] | 600 | filter_free_pred(pred); |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 601 | return cnt; |
| 602 | } |
| 603 | |
Li Zefan | 44e9c8b | 2009-04-11 15:55:28 +0800 | [diff] [blame] | 604 | err = filter_add_subsystem_pred(system, pred); |
| 605 | if (err < 0) { |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 606 | filter_free_pred(pred); |
Li Zefan | 44e9c8b | 2009-04-11 15:55:28 +0800 | [diff] [blame] | 607 | return err; |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | *ppos += cnt; |
| 611 | |
| 612 | return cnt; |
| 613 | } |
| 614 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 615 | static ssize_t |
| 616 | show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 617 | { |
| 618 | int (*func)(struct trace_seq *s) = filp->private_data; |
| 619 | struct trace_seq *s; |
| 620 | int r; |
| 621 | |
| 622 | if (*ppos) |
| 623 | return 0; |
| 624 | |
| 625 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 626 | if (!s) |
| 627 | return -ENOMEM; |
| 628 | |
| 629 | trace_seq_init(s); |
| 630 | |
| 631 | func(s); |
| 632 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
| 633 | |
| 634 | kfree(s); |
| 635 | |
| 636 | return r; |
| 637 | } |
| 638 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 639 | static const struct seq_operations show_event_seq_ops = { |
| 640 | .start = t_start, |
| 641 | .next = t_next, |
| 642 | .show = t_show, |
| 643 | .stop = t_stop, |
| 644 | }; |
| 645 | |
| 646 | static const struct seq_operations show_set_event_seq_ops = { |
| 647 | .start = s_start, |
| 648 | .next = s_next, |
| 649 | .show = t_show, |
| 650 | .stop = t_stop, |
| 651 | }; |
| 652 | |
Steven Rostedt | 2314c4a | 2009-03-10 12:04:02 -0400 | [diff] [blame] | 653 | static const struct file_operations ftrace_avail_fops = { |
| 654 | .open = ftrace_event_seq_open, |
| 655 | .read = seq_read, |
| 656 | .llseek = seq_lseek, |
| 657 | .release = seq_release, |
| 658 | }; |
| 659 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 660 | static const struct file_operations ftrace_set_event_fops = { |
| 661 | .open = ftrace_event_seq_open, |
| 662 | .read = seq_read, |
| 663 | .write = ftrace_event_write, |
| 664 | .llseek = seq_lseek, |
| 665 | .release = seq_release, |
| 666 | }; |
| 667 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 668 | static const struct file_operations ftrace_enable_fops = { |
| 669 | .open = tracing_open_generic, |
| 670 | .read = event_enable_read, |
| 671 | .write = event_enable_write, |
| 672 | }; |
| 673 | |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 674 | static const struct file_operations ftrace_event_format_fops = { |
| 675 | .open = tracing_open_generic, |
| 676 | .read = event_format_read, |
| 677 | }; |
| 678 | |
Peter Zijlstra | 23725ae | 2009-03-19 20:26:13 +0100 | [diff] [blame] | 679 | static const struct file_operations ftrace_event_id_fops = { |
| 680 | .open = tracing_open_generic, |
| 681 | .read = event_id_read, |
| 682 | }; |
| 683 | |
Tom Zanussi | 7ce7e42 | 2009-03-22 03:31:04 -0500 | [diff] [blame] | 684 | static const struct file_operations ftrace_event_filter_fops = { |
| 685 | .open = tracing_open_generic, |
| 686 | .read = event_filter_read, |
| 687 | .write = event_filter_write, |
| 688 | }; |
| 689 | |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 690 | static const struct file_operations ftrace_subsystem_filter_fops = { |
| 691 | .open = tracing_open_generic, |
| 692 | .read = subsystem_filter_read, |
| 693 | .write = subsystem_filter_write, |
| 694 | }; |
| 695 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 696 | static const struct file_operations ftrace_show_header_fops = { |
| 697 | .open = tracing_open_generic, |
| 698 | .read = show_header, |
| 699 | }; |
| 700 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 701 | static struct dentry *event_trace_events_dir(void) |
| 702 | { |
| 703 | static struct dentry *d_tracer; |
| 704 | static struct dentry *d_events; |
| 705 | |
| 706 | if (d_events) |
| 707 | return d_events; |
| 708 | |
| 709 | d_tracer = tracing_init_dentry(); |
| 710 | if (!d_tracer) |
| 711 | return NULL; |
| 712 | |
| 713 | d_events = debugfs_create_dir("events", d_tracer); |
| 714 | if (!d_events) |
| 715 | pr_warning("Could not create debugfs " |
| 716 | "'events' directory\n"); |
| 717 | |
| 718 | return d_events; |
| 719 | } |
| 720 | |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 721 | static LIST_HEAD(event_subsystems); |
| 722 | |
| 723 | static struct dentry * |
| 724 | event_subsystem_dir(const char *name, struct dentry *d_events) |
| 725 | { |
| 726 | struct event_subsystem *system; |
Tom Zanussi | e1112b4 | 2009-03-31 00:48:49 -0500 | [diff] [blame] | 727 | struct dentry *entry; |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 728 | |
| 729 | /* First see if we did not already create this dir */ |
| 730 | list_for_each_entry(system, &event_subsystems, list) { |
| 731 | if (strcmp(system->name, name) == 0) |
| 732 | return system->entry; |
| 733 | } |
| 734 | |
| 735 | /* need to create new entry */ |
| 736 | system = kmalloc(sizeof(*system), GFP_KERNEL); |
| 737 | if (!system) { |
| 738 | pr_warning("No memory to create event subsystem %s\n", |
| 739 | name); |
| 740 | return d_events; |
| 741 | } |
| 742 | |
| 743 | system->entry = debugfs_create_dir(name, d_events); |
| 744 | if (!system->entry) { |
| 745 | pr_warning("Could not create event subsystem %s\n", |
| 746 | name); |
| 747 | kfree(system); |
| 748 | return d_events; |
| 749 | } |
| 750 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 751 | system->name = kstrdup(name, GFP_KERNEL); |
| 752 | if (!system->name) { |
| 753 | debugfs_remove(system->entry); |
| 754 | kfree(system); |
| 755 | return d_events; |
| 756 | } |
| 757 | |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 758 | list_add(&system->list, &event_subsystems); |
| 759 | |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 760 | system->preds = NULL; |
Tom Zanussi | 0a19e53 | 2009-04-13 03:17:50 -0500 | [diff] [blame] | 761 | system->n_preds = 0; |
Tom Zanussi | cfb180f | 2009-03-22 03:31:17 -0500 | [diff] [blame] | 762 | |
Tom Zanussi | e1112b4 | 2009-03-31 00:48:49 -0500 | [diff] [blame] | 763 | entry = debugfs_create_file("filter", 0644, system->entry, system, |
| 764 | &ftrace_subsystem_filter_fops); |
| 765 | if (!entry) |
| 766 | pr_warning("Could not create debugfs " |
| 767 | "'%s/filter' entry\n", name); |
| 768 | |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 769 | return system->entry; |
| 770 | } |
| 771 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 772 | static int |
| 773 | event_create_dir(struct ftrace_event_call *call, struct dentry *d_events) |
| 774 | { |
| 775 | struct dentry *entry; |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 776 | int ret; |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 777 | |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 778 | /* |
| 779 | * If the trace point header did not define TRACE_SYSTEM |
| 780 | * then the system would be called "TRACE_SYSTEM". |
| 781 | */ |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 782 | if (strcmp(call->system, TRACE_SYSTEM) != 0) |
Steven Rostedt | 6ecc2d1 | 2009-02-27 21:33:02 -0500 | [diff] [blame] | 783 | d_events = event_subsystem_dir(call->system, d_events); |
| 784 | |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 785 | if (call->raw_init) { |
| 786 | ret = call->raw_init(); |
| 787 | if (ret < 0) { |
| 788 | pr_warning("Could not initialize trace point" |
| 789 | " events/%s\n", call->name); |
| 790 | return ret; |
| 791 | } |
| 792 | } |
| 793 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 794 | call->dir = debugfs_create_dir(call->name, d_events); |
| 795 | if (!call->dir) { |
| 796 | pr_warning("Could not create debugfs " |
| 797 | "'%s' directory\n", call->name); |
| 798 | return -1; |
| 799 | } |
| 800 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 801 | if (call->regfunc) |
| 802 | entry = trace_create_file("enable", 0644, call->dir, call, |
| 803 | &ftrace_enable_fops); |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 804 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 805 | if (call->id) |
| 806 | entry = trace_create_file("id", 0444, call->dir, call, |
| 807 | &ftrace_event_id_fops); |
Peter Zijlstra | 23725ae | 2009-03-19 20:26:13 +0100 | [diff] [blame] | 808 | |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 809 | if (call->define_fields) { |
| 810 | ret = call->define_fields(); |
| 811 | if (ret < 0) { |
| 812 | pr_warning("Could not initialize trace point" |
| 813 | " events/%s\n", call->name); |
| 814 | return ret; |
| 815 | } |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 816 | entry = trace_create_file("filter", 0644, call->dir, call, |
| 817 | &ftrace_event_filter_fops); |
Tom Zanussi | cf027f6 | 2009-03-22 03:30:39 -0500 | [diff] [blame] | 818 | } |
| 819 | |
Steven Rostedt | 981d081 | 2009-03-02 13:53:59 -0500 | [diff] [blame] | 820 | /* A trace may not want to export its format */ |
| 821 | if (!call->show_format) |
| 822 | return 0; |
| 823 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 824 | entry = trace_create_file("format", 0444, call->dir, call, |
| 825 | &ftrace_event_format_fops); |
Steven Rostedt | fd99498 | 2009-02-28 02:41:25 -0500 | [diff] [blame] | 826 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 830 | #define for_each_event(event, start, end) \ |
| 831 | for (event = start; \ |
| 832 | (unsigned long)event < (unsigned long)end; \ |
| 833 | event++) |
| 834 | |
Steven Rostedt | 61f919a | 2009-04-14 18:22:32 -0400 | [diff] [blame] | 835 | #ifdef CONFIG_MODULES |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 836 | static void trace_module_add_events(struct module *mod) |
| 837 | { |
| 838 | struct ftrace_event_call *call, *start, *end; |
| 839 | struct dentry *d_events; |
| 840 | |
| 841 | start = mod->trace_events; |
| 842 | end = mod->trace_events + mod->num_trace_events; |
| 843 | |
| 844 | if (start == end) |
| 845 | return; |
| 846 | |
| 847 | d_events = event_trace_events_dir(); |
| 848 | if (!d_events) |
| 849 | return; |
| 850 | |
| 851 | for_each_event(call, start, end) { |
| 852 | /* The linker may leave blanks */ |
| 853 | if (!call->name) |
| 854 | continue; |
| 855 | call->mod = mod; |
| 856 | list_add(&call->list, &ftrace_events); |
| 857 | event_create_dir(call, d_events); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static void trace_module_remove_events(struct module *mod) |
| 862 | { |
| 863 | struct ftrace_event_call *call, *p; |
| 864 | |
| 865 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
| 866 | if (call->mod == mod) { |
| 867 | if (call->enabled) { |
| 868 | call->enabled = 0; |
| 869 | call->unregfunc(); |
| 870 | } |
| 871 | if (call->event) |
| 872 | unregister_ftrace_event(call->event); |
| 873 | debugfs_remove_recursive(call->dir); |
| 874 | list_del(&call->list); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
Steven Rostedt | 61f919a | 2009-04-14 18:22:32 -0400 | [diff] [blame] | 879 | static int trace_module_notify(struct notifier_block *self, |
| 880 | unsigned long val, void *data) |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 881 | { |
| 882 | struct module *mod = data; |
| 883 | |
| 884 | mutex_lock(&event_mutex); |
| 885 | switch (val) { |
| 886 | case MODULE_STATE_COMING: |
| 887 | trace_module_add_events(mod); |
| 888 | break; |
| 889 | case MODULE_STATE_GOING: |
| 890 | trace_module_remove_events(mod); |
| 891 | break; |
| 892 | } |
| 893 | mutex_unlock(&event_mutex); |
| 894 | |
| 895 | return 0; |
| 896 | } |
Steven Rostedt | 61f919a | 2009-04-14 18:22:32 -0400 | [diff] [blame] | 897 | #else |
| 898 | static int trace_module_notify(struct notifier_block *self, |
| 899 | unsigned long val, void *data) |
| 900 | { |
| 901 | return 0; |
| 902 | } |
| 903 | #endif /* CONFIG_MODULES */ |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 904 | |
| 905 | struct notifier_block trace_module_nb = { |
| 906 | .notifier_call = trace_module_notify, |
| 907 | .priority = 0, |
| 908 | }; |
| 909 | |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 910 | extern struct ftrace_event_call __start_ftrace_events[]; |
| 911 | extern struct ftrace_event_call __stop_ftrace_events[]; |
| 912 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 913 | static __init int event_trace_init(void) |
| 914 | { |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 915 | struct ftrace_event_call *call; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 916 | struct dentry *d_tracer; |
| 917 | struct dentry *entry; |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 918 | struct dentry *d_events; |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 919 | int ret; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 920 | |
| 921 | d_tracer = tracing_init_dentry(); |
| 922 | if (!d_tracer) |
| 923 | return 0; |
| 924 | |
Steven Rostedt | 2314c4a | 2009-03-10 12:04:02 -0400 | [diff] [blame] | 925 | entry = debugfs_create_file("available_events", 0444, d_tracer, |
| 926 | (void *)&show_event_seq_ops, |
| 927 | &ftrace_avail_fops); |
| 928 | if (!entry) |
| 929 | pr_warning("Could not create debugfs " |
| 930 | "'available_events' entry\n"); |
| 931 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 932 | entry = debugfs_create_file("set_event", 0644, d_tracer, |
| 933 | (void *)&show_set_event_seq_ops, |
| 934 | &ftrace_set_event_fops); |
| 935 | if (!entry) |
| 936 | pr_warning("Could not create debugfs " |
| 937 | "'set_event' entry\n"); |
| 938 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 939 | d_events = event_trace_events_dir(); |
| 940 | if (!d_events) |
| 941 | return 0; |
| 942 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 943 | /* ring buffer internal formats */ |
| 944 | trace_create_file("header_page", 0444, d_events, |
| 945 | ring_buffer_print_page_header, |
| 946 | &ftrace_show_header_fops); |
| 947 | |
| 948 | trace_create_file("header_event", 0444, d_events, |
| 949 | ring_buffer_print_entry_header, |
| 950 | &ftrace_show_header_fops); |
| 951 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 952 | for_each_event(call, __start_ftrace_events, __stop_ftrace_events) { |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 953 | /* The linker may leave blanks */ |
| 954 | if (!call->name) |
| 955 | continue; |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 956 | list_add(&call->list, &ftrace_events); |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame] | 957 | event_create_dir(call, d_events); |
| 958 | } |
| 959 | |
Steven Rostedt | 6d72373 | 2009-04-10 14:53:50 -0400 | [diff] [blame] | 960 | ret = register_module_notifier(&trace_module_nb); |
| 961 | if (!ret) |
| 962 | pr_warning("Failed to register trace events module notifier\n"); |
| 963 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | fs_initcall(event_trace_init); |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 967 | |
| 968 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 969 | |
| 970 | static DEFINE_SPINLOCK(test_spinlock); |
| 971 | static DEFINE_SPINLOCK(test_spinlock_irq); |
| 972 | static DEFINE_MUTEX(test_mutex); |
| 973 | |
| 974 | static __init void test_work(struct work_struct *dummy) |
| 975 | { |
| 976 | spin_lock(&test_spinlock); |
| 977 | spin_lock_irq(&test_spinlock_irq); |
| 978 | udelay(1); |
| 979 | spin_unlock_irq(&test_spinlock_irq); |
| 980 | spin_unlock(&test_spinlock); |
| 981 | |
| 982 | mutex_lock(&test_mutex); |
| 983 | msleep(1); |
| 984 | mutex_unlock(&test_mutex); |
| 985 | } |
| 986 | |
| 987 | static __init int event_test_thread(void *unused) |
| 988 | { |
| 989 | void *test_malloc; |
| 990 | |
| 991 | test_malloc = kmalloc(1234, GFP_KERNEL); |
| 992 | if (!test_malloc) |
| 993 | pr_info("failed to kmalloc\n"); |
| 994 | |
| 995 | schedule_on_each_cpu(test_work); |
| 996 | |
| 997 | kfree(test_malloc); |
| 998 | |
| 999 | set_current_state(TASK_INTERRUPTIBLE); |
| 1000 | while (!kthread_should_stop()) |
| 1001 | schedule(); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Do various things that may trigger events. |
| 1008 | */ |
| 1009 | static __init void event_test_stuff(void) |
| 1010 | { |
| 1011 | struct task_struct *test_thread; |
| 1012 | |
| 1013 | test_thread = kthread_run(event_test_thread, NULL, "test-events"); |
| 1014 | msleep(1); |
| 1015 | kthread_stop(test_thread); |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * For every trace event defined, we will test each trace point separately, |
| 1020 | * and then by groups, and finally all trace points. |
| 1021 | */ |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1022 | static __init void event_trace_self_tests(void) |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1023 | { |
| 1024 | struct ftrace_event_call *call; |
| 1025 | struct event_subsystem *system; |
| 1026 | char *sysname; |
| 1027 | int ret; |
| 1028 | |
| 1029 | pr_info("Running tests on trace events:\n"); |
| 1030 | |
| 1031 | list_for_each_entry(call, &ftrace_events, list) { |
| 1032 | |
| 1033 | /* Only test those that have a regfunc */ |
| 1034 | if (!call->regfunc) |
| 1035 | continue; |
| 1036 | |
| 1037 | pr_info("Testing event %s: ", call->name); |
| 1038 | |
| 1039 | /* |
| 1040 | * If an event is already enabled, someone is using |
| 1041 | * it and the self test should not be on. |
| 1042 | */ |
| 1043 | if (call->enabled) { |
| 1044 | pr_warning("Enabled event during self test!\n"); |
| 1045 | WARN_ON_ONCE(1); |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | call->enabled = 1; |
| 1050 | call->regfunc(); |
| 1051 | |
| 1052 | event_test_stuff(); |
| 1053 | |
| 1054 | call->unregfunc(); |
| 1055 | call->enabled = 0; |
| 1056 | |
| 1057 | pr_cont("OK\n"); |
| 1058 | } |
| 1059 | |
| 1060 | /* Now test at the sub system level */ |
| 1061 | |
| 1062 | pr_info("Running tests on trace event systems:\n"); |
| 1063 | |
| 1064 | list_for_each_entry(system, &event_subsystems, list) { |
| 1065 | |
| 1066 | /* the ftrace system is special, skip it */ |
| 1067 | if (strcmp(system->name, "ftrace") == 0) |
| 1068 | continue; |
| 1069 | |
| 1070 | pr_info("Testing event system %s: ", system->name); |
| 1071 | |
| 1072 | /* ftrace_set_clr_event can modify the name passed in. */ |
| 1073 | sysname = kstrdup(system->name, GFP_KERNEL); |
| 1074 | if (WARN_ON(!sysname)) { |
| 1075 | pr_warning("Can't allocate memory, giving up!\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1076 | return; |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1077 | } |
| 1078 | ret = ftrace_set_clr_event(sysname, 1); |
| 1079 | kfree(sysname); |
| 1080 | if (WARN_ON_ONCE(ret)) { |
| 1081 | pr_warning("error enabling system %s\n", |
| 1082 | system->name); |
| 1083 | continue; |
| 1084 | } |
| 1085 | |
| 1086 | event_test_stuff(); |
| 1087 | |
| 1088 | sysname = kstrdup(system->name, GFP_KERNEL); |
| 1089 | if (WARN_ON(!sysname)) { |
| 1090 | pr_warning("Can't allocate memory, giving up!\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1091 | return; |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1092 | } |
| 1093 | ret = ftrace_set_clr_event(sysname, 0); |
| 1094 | kfree(sysname); |
| 1095 | |
| 1096 | if (WARN_ON_ONCE(ret)) |
| 1097 | pr_warning("error disabling system %s\n", |
| 1098 | system->name); |
| 1099 | |
| 1100 | pr_cont("OK\n"); |
| 1101 | } |
| 1102 | |
| 1103 | /* Test with all events enabled */ |
| 1104 | |
| 1105 | pr_info("Running tests on all trace events:\n"); |
| 1106 | pr_info("Testing all events: "); |
| 1107 | |
| 1108 | sysname = kmalloc(4, GFP_KERNEL); |
| 1109 | if (WARN_ON(!sysname)) { |
| 1110 | pr_warning("Can't allocate memory, giving up!\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1111 | return; |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1112 | } |
| 1113 | memcpy(sysname, "*:*", 4); |
| 1114 | ret = ftrace_set_clr_event(sysname, 1); |
| 1115 | if (WARN_ON_ONCE(ret)) { |
| 1116 | kfree(sysname); |
| 1117 | pr_warning("error enabling all events\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1118 | return; |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | event_test_stuff(); |
| 1122 | |
| 1123 | /* reset sysname */ |
| 1124 | memcpy(sysname, "*:*", 4); |
| 1125 | ret = ftrace_set_clr_event(sysname, 0); |
| 1126 | kfree(sysname); |
| 1127 | |
| 1128 | if (WARN_ON_ONCE(ret)) { |
| 1129 | pr_warning("error disabling all events\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1130 | return; |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | pr_cont("OK\n"); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | #ifdef CONFIG_FUNCTION_TRACER |
| 1137 | |
| 1138 | static DEFINE_PER_CPU(atomic_t, test_event_disable); |
| 1139 | |
| 1140 | static void |
| 1141 | function_test_events_call(unsigned long ip, unsigned long parent_ip) |
| 1142 | { |
| 1143 | struct ring_buffer_event *event; |
| 1144 | struct ftrace_entry *entry; |
| 1145 | unsigned long flags; |
| 1146 | long disabled; |
| 1147 | int resched; |
| 1148 | int cpu; |
| 1149 | int pc; |
| 1150 | |
| 1151 | pc = preempt_count(); |
| 1152 | resched = ftrace_preempt_disable(); |
| 1153 | cpu = raw_smp_processor_id(); |
| 1154 | disabled = atomic_inc_return(&per_cpu(test_event_disable, cpu)); |
| 1155 | |
| 1156 | if (disabled != 1) |
| 1157 | goto out; |
| 1158 | |
| 1159 | local_save_flags(flags); |
| 1160 | |
| 1161 | event = trace_current_buffer_lock_reserve(TRACE_FN, sizeof(*entry), |
| 1162 | flags, pc); |
| 1163 | if (!event) |
| 1164 | goto out; |
| 1165 | entry = ring_buffer_event_data(event); |
| 1166 | entry->ip = ip; |
| 1167 | entry->parent_ip = parent_ip; |
| 1168 | |
Steven Rostedt | cb4764a | 2009-04-20 18:16:44 -0400 | [diff] [blame] | 1169 | trace_nowake_buffer_unlock_commit(event, flags, pc); |
Steven Rostedt | 9ea21c1 | 2009-04-16 12:15:44 -0400 | [diff] [blame] | 1170 | |
| 1171 | out: |
| 1172 | atomic_dec(&per_cpu(test_event_disable, cpu)); |
| 1173 | ftrace_preempt_enable(resched); |
| 1174 | } |
| 1175 | |
| 1176 | static struct ftrace_ops trace_ops __initdata = |
| 1177 | { |
| 1178 | .func = function_test_events_call, |
| 1179 | }; |
| 1180 | |
| 1181 | static __init void event_trace_self_test_with_function(void) |
| 1182 | { |
| 1183 | register_ftrace_function(&trace_ops); |
| 1184 | pr_info("Running tests again, along with the function tracer\n"); |
| 1185 | event_trace_self_tests(); |
| 1186 | unregister_ftrace_function(&trace_ops); |
| 1187 | } |
| 1188 | #else |
| 1189 | static __init void event_trace_self_test_with_function(void) |
| 1190 | { |
| 1191 | } |
| 1192 | #endif |
| 1193 | |
| 1194 | static __init int event_trace_self_tests_init(void) |
| 1195 | { |
| 1196 | |
| 1197 | event_trace_self_tests(); |
| 1198 | |
| 1199 | event_trace_self_test_with_function(); |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Steven Rostedt | 28d20e2 | 2009-04-20 12:12:44 -0400 | [diff] [blame] | 1204 | late_initcall(event_trace_self_tests_init); |
Steven Rostedt | e618700 | 2009-04-15 13:36:40 -0400 | [diff] [blame] | 1205 | |
| 1206 | #endif |