yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * event tracer |
| 3 | * |
| 4 | * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 5 | * |
| 6 | * - Added format output of fields of the trace point. |
| 7 | * This was based off of work by Tom Zanussi <tzanussi@gmail.com>. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/workqueue.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/kthread.h> |
| 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/delay.h> |
| 20 | |
| 21 | #include <asm/setup.h> |
| 22 | |
| 23 | #include "trace_output.h" |
| 24 | |
| 25 | #undef TRACE_SYSTEM |
| 26 | #define TRACE_SYSTEM "TRACE_SYSTEM" |
| 27 | |
| 28 | DEFINE_MUTEX(event_mutex); |
| 29 | |
| 30 | DEFINE_MUTEX(event_storage_mutex); |
| 31 | EXPORT_SYMBOL_GPL(event_storage_mutex); |
| 32 | |
| 33 | char event_storage[EVENT_STORAGE_SIZE]; |
| 34 | EXPORT_SYMBOL_GPL(event_storage); |
| 35 | |
| 36 | LIST_HEAD(ftrace_events); |
| 37 | LIST_HEAD(ftrace_common_fields); |
| 38 | |
| 39 | struct list_head * |
| 40 | trace_get_fields(struct ftrace_event_call *event_call) |
| 41 | { |
| 42 | if (!event_call->class->get_fields) |
| 43 | return &event_call->class->fields; |
| 44 | return event_call->class->get_fields(event_call); |
| 45 | } |
| 46 | |
| 47 | static int __trace_define_field(struct list_head *head, const char *type, |
| 48 | const char *name, int offset, int size, |
| 49 | int is_signed, int filter_type) |
| 50 | { |
| 51 | struct ftrace_event_field *field; |
| 52 | |
| 53 | field = kzalloc(sizeof(*field), GFP_KERNEL); |
| 54 | if (!field) |
| 55 | goto err; |
| 56 | |
| 57 | field->name = kstrdup(name, GFP_KERNEL); |
| 58 | if (!field->name) |
| 59 | goto err; |
| 60 | |
| 61 | field->type = kstrdup(type, GFP_KERNEL); |
| 62 | if (!field->type) |
| 63 | goto err; |
| 64 | |
| 65 | if (filter_type == FILTER_OTHER) |
| 66 | field->filter_type = filter_assign_type(type); |
| 67 | else |
| 68 | field->filter_type = filter_type; |
| 69 | |
| 70 | field->offset = offset; |
| 71 | field->size = size; |
| 72 | field->is_signed = is_signed; |
| 73 | |
| 74 | list_add(&field->link, head); |
| 75 | |
| 76 | return 0; |
| 77 | |
| 78 | err: |
| 79 | if (field) |
| 80 | kfree(field->name); |
| 81 | kfree(field); |
| 82 | |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
| 86 | int trace_define_field(struct ftrace_event_call *call, const char *type, |
| 87 | const char *name, int offset, int size, int is_signed, |
| 88 | int filter_type) |
| 89 | { |
| 90 | struct list_head *head; |
| 91 | |
| 92 | if (WARN_ON(!call->class)) |
| 93 | return 0; |
| 94 | |
| 95 | head = trace_get_fields(call); |
| 96 | return __trace_define_field(head, type, name, offset, size, |
| 97 | is_signed, filter_type); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(trace_define_field); |
| 100 | |
| 101 | #define __common_field(type, item) \ |
| 102 | ret = __trace_define_field(&ftrace_common_fields, #type, \ |
| 103 | "common_" #item, \ |
| 104 | offsetof(typeof(ent), item), \ |
| 105 | sizeof(ent.item), \ |
| 106 | is_signed_type(type), FILTER_OTHER); \ |
| 107 | if (ret) \ |
| 108 | return ret; |
| 109 | |
| 110 | static int trace_define_common_fields(void) |
| 111 | { |
| 112 | int ret; |
| 113 | struct trace_entry ent; |
| 114 | |
| 115 | __common_field(unsigned short, type); |
| 116 | __common_field(unsigned char, flags); |
| 117 | __common_field(unsigned char, preempt_count); |
| 118 | __common_field(int, pid); |
| 119 | __common_field(unsigned short, migrate_disable); |
| 120 | __common_field(unsigned short, padding); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | void trace_destroy_fields(struct ftrace_event_call *call) |
| 126 | { |
| 127 | struct ftrace_event_field *field, *next; |
| 128 | struct list_head *head; |
| 129 | |
| 130 | head = trace_get_fields(call); |
| 131 | list_for_each_entry_safe(field, next, head, link) { |
| 132 | list_del(&field->link); |
| 133 | kfree(field->type); |
| 134 | kfree(field->name); |
| 135 | kfree(field); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int trace_event_raw_init(struct ftrace_event_call *call) |
| 140 | { |
| 141 | int id; |
| 142 | |
| 143 | id = register_ftrace_event(&call->event); |
| 144 | if (!id) |
| 145 | return -ENODEV; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(trace_event_raw_init); |
| 150 | |
| 151 | int ftrace_event_reg(struct ftrace_event_call *call, |
| 152 | enum trace_reg type, void *data) |
| 153 | { |
| 154 | switch (type) { |
| 155 | case TRACE_REG_REGISTER: |
| 156 | return tracepoint_probe_register(call->name, |
| 157 | call->class->probe, |
| 158 | call); |
| 159 | case TRACE_REG_UNREGISTER: |
| 160 | tracepoint_probe_unregister(call->name, |
| 161 | call->class->probe, |
| 162 | call); |
| 163 | return 0; |
| 164 | |
| 165 | #ifdef CONFIG_PERF_EVENTS |
| 166 | case TRACE_REG_PERF_REGISTER: |
| 167 | return tracepoint_probe_register(call->name, |
| 168 | call->class->perf_probe, |
| 169 | call); |
| 170 | case TRACE_REG_PERF_UNREGISTER: |
| 171 | tracepoint_probe_unregister(call->name, |
| 172 | call->class->perf_probe, |
| 173 | call); |
| 174 | return 0; |
| 175 | case TRACE_REG_PERF_OPEN: |
| 176 | case TRACE_REG_PERF_CLOSE: |
| 177 | case TRACE_REG_PERF_ADD: |
| 178 | case TRACE_REG_PERF_DEL: |
| 179 | return 0; |
| 180 | #endif |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(ftrace_event_reg); |
| 185 | |
| 186 | void trace_event_enable_cmd_record(bool enable) |
| 187 | { |
| 188 | struct ftrace_event_call *call; |
| 189 | |
| 190 | mutex_lock(&event_mutex); |
| 191 | list_for_each_entry(call, &ftrace_events, list) { |
| 192 | if (!(call->flags & TRACE_EVENT_FL_ENABLED)) |
| 193 | continue; |
| 194 | |
| 195 | if (enable) { |
| 196 | tracing_start_cmdline_record(); |
| 197 | call->flags |= TRACE_EVENT_FL_RECORDED_CMD; |
| 198 | } else { |
| 199 | tracing_stop_cmdline_record(); |
| 200 | call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD; |
| 201 | } |
| 202 | } |
| 203 | mutex_unlock(&event_mutex); |
| 204 | } |
| 205 | |
| 206 | static int ftrace_event_enable_disable(struct ftrace_event_call *call, |
| 207 | int enable) |
| 208 | { |
| 209 | int ret = 0; |
| 210 | |
| 211 | switch (enable) { |
| 212 | case 0: |
| 213 | if (call->flags & TRACE_EVENT_FL_ENABLED) { |
| 214 | call->flags &= ~TRACE_EVENT_FL_ENABLED; |
| 215 | if (call->flags & TRACE_EVENT_FL_RECORDED_CMD) { |
| 216 | tracing_stop_cmdline_record(); |
| 217 | call->flags &= ~TRACE_EVENT_FL_RECORDED_CMD; |
| 218 | } |
| 219 | call->class->reg(call, TRACE_REG_UNREGISTER, NULL); |
| 220 | } |
| 221 | break; |
| 222 | case 1: |
| 223 | if (!(call->flags & TRACE_EVENT_FL_ENABLED)) { |
| 224 | if (trace_flags & TRACE_ITER_RECORD_CMD) { |
| 225 | tracing_start_cmdline_record(); |
| 226 | call->flags |= TRACE_EVENT_FL_RECORDED_CMD; |
| 227 | } |
| 228 | ret = call->class->reg(call, TRACE_REG_REGISTER, NULL); |
| 229 | if (ret) { |
| 230 | tracing_stop_cmdline_record(); |
| 231 | pr_info("event trace: Could not enable event " |
| 232 | "%s\n", call->name); |
| 233 | break; |
| 234 | } |
| 235 | call->flags |= TRACE_EVENT_FL_ENABLED; |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static void ftrace_clear_events(void) |
| 244 | { |
| 245 | struct ftrace_event_call *call; |
| 246 | |
| 247 | mutex_lock(&event_mutex); |
| 248 | list_for_each_entry(call, &ftrace_events, list) { |
| 249 | ftrace_event_enable_disable(call, 0); |
| 250 | } |
| 251 | mutex_unlock(&event_mutex); |
| 252 | } |
| 253 | |
| 254 | static void __put_system(struct event_subsystem *system) |
| 255 | { |
| 256 | struct event_filter *filter = system->filter; |
| 257 | |
| 258 | WARN_ON_ONCE(system->ref_count == 0); |
| 259 | if (--system->ref_count) |
| 260 | return; |
| 261 | |
| 262 | if (filter) { |
| 263 | kfree(filter->filter_string); |
| 264 | kfree(filter); |
| 265 | } |
| 266 | kfree(system->name); |
| 267 | kfree(system); |
| 268 | } |
| 269 | |
| 270 | static void __get_system(struct event_subsystem *system) |
| 271 | { |
| 272 | WARN_ON_ONCE(system->ref_count == 0); |
| 273 | system->ref_count++; |
| 274 | } |
| 275 | |
| 276 | static void put_system(struct event_subsystem *system) |
| 277 | { |
| 278 | mutex_lock(&event_mutex); |
| 279 | __put_system(system); |
| 280 | mutex_unlock(&event_mutex); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events. |
| 285 | */ |
| 286 | static int __ftrace_set_clr_event(const char *match, const char *sub, |
| 287 | const char *event, int set) |
| 288 | { |
| 289 | struct ftrace_event_call *call; |
| 290 | int ret = -EINVAL; |
| 291 | |
| 292 | mutex_lock(&event_mutex); |
| 293 | list_for_each_entry(call, &ftrace_events, list) { |
| 294 | |
| 295 | if (!call->name || !call->class || !call->class->reg) |
| 296 | continue; |
| 297 | |
| 298 | if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) |
| 299 | continue; |
| 300 | |
| 301 | if (match && |
| 302 | strcmp(match, call->name) != 0 && |
| 303 | strcmp(match, call->class->system) != 0) |
| 304 | continue; |
| 305 | |
| 306 | if (sub && strcmp(sub, call->class->system) != 0) |
| 307 | continue; |
| 308 | |
| 309 | if (event && strcmp(event, call->name) != 0) |
| 310 | continue; |
| 311 | |
| 312 | ftrace_event_enable_disable(call, set); |
| 313 | |
| 314 | ret = 0; |
| 315 | } |
| 316 | mutex_unlock(&event_mutex); |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int ftrace_set_clr_event(char *buf, int set) |
| 322 | { |
| 323 | char *event = NULL, *sub = NULL, *match; |
| 324 | |
| 325 | /* |
| 326 | * The buf format can be <subsystem>:<event-name> |
| 327 | * *:<event-name> means any event by that name. |
| 328 | * :<event-name> is the same. |
| 329 | * |
| 330 | * <subsystem>:* means all events in that subsystem |
| 331 | * <subsystem>: means the same. |
| 332 | * |
| 333 | * <name> (no ':') means all events in a subsystem with |
| 334 | * the name <name> or any event that matches <name> |
| 335 | */ |
| 336 | |
| 337 | match = strsep(&buf, ":"); |
| 338 | if (buf) { |
| 339 | sub = match; |
| 340 | event = buf; |
| 341 | match = NULL; |
| 342 | |
| 343 | if (!strlen(sub) || strcmp(sub, "*") == 0) |
| 344 | sub = NULL; |
| 345 | if (!strlen(event) || strcmp(event, "*") == 0) |
| 346 | event = NULL; |
| 347 | } |
| 348 | |
| 349 | return __ftrace_set_clr_event(match, sub, event, set); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * trace_set_clr_event - enable or disable an event |
| 354 | * @system: system name to match (NULL for any system) |
| 355 | * @event: event name to match (NULL for all events, within system) |
| 356 | * @set: 1 to enable, 0 to disable |
| 357 | * |
| 358 | * This is a way for other parts of the kernel to enable or disable |
| 359 | * event recording. |
| 360 | * |
| 361 | * Returns 0 on success, -EINVAL if the parameters do not match any |
| 362 | * registered events. |
| 363 | */ |
| 364 | int trace_set_clr_event(const char *system, const char *event, int set) |
| 365 | { |
| 366 | return __ftrace_set_clr_event(NULL, system, event, set); |
| 367 | } |
| 368 | EXPORT_SYMBOL_GPL(trace_set_clr_event); |
| 369 | |
| 370 | /* 128 should be much more than enough */ |
| 371 | #define EVENT_BUF_SIZE 127 |
| 372 | |
| 373 | static ssize_t |
| 374 | ftrace_event_write(struct file *file, const char __user *ubuf, |
| 375 | size_t cnt, loff_t *ppos) |
| 376 | { |
| 377 | struct trace_parser parser; |
| 378 | ssize_t read, ret; |
| 379 | |
| 380 | if (!cnt) |
| 381 | return 0; |
| 382 | |
| 383 | ret = tracing_update_buffers(); |
| 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
| 387 | if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1)) |
| 388 | return -ENOMEM; |
| 389 | |
| 390 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
| 391 | |
| 392 | if (read >= 0 && trace_parser_loaded((&parser))) { |
| 393 | int set = 1; |
| 394 | |
| 395 | if (*parser.buffer == '!') |
| 396 | set = 0; |
| 397 | |
| 398 | parser.buffer[parser.idx] = 0; |
| 399 | |
| 400 | ret = ftrace_set_clr_event(parser.buffer + !set, set); |
| 401 | if (ret) |
| 402 | goto out_put; |
| 403 | } |
| 404 | |
| 405 | ret = read; |
| 406 | |
| 407 | out_put: |
| 408 | trace_parser_put(&parser); |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static void * |
| 414 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 415 | { |
| 416 | struct ftrace_event_call *call = v; |
| 417 | |
| 418 | (*pos)++; |
| 419 | |
| 420 | list_for_each_entry_continue(call, &ftrace_events, list) { |
| 421 | /* |
| 422 | * The ftrace subsystem is for showing formats only. |
| 423 | * They can not be enabled or disabled via the event files. |
| 424 | */ |
| 425 | if (call->class && call->class->reg) |
| 426 | return call; |
| 427 | } |
| 428 | |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 433 | { |
| 434 | struct ftrace_event_call *call; |
| 435 | loff_t l; |
| 436 | |
| 437 | mutex_lock(&event_mutex); |
| 438 | |
| 439 | call = list_entry(&ftrace_events, struct ftrace_event_call, list); |
| 440 | for (l = 0; l <= *pos; ) { |
| 441 | call = t_next(m, call, &l); |
| 442 | if (!call) |
| 443 | break; |
| 444 | } |
| 445 | return call; |
| 446 | } |
| 447 | |
| 448 | static void * |
| 449 | s_next(struct seq_file *m, void *v, loff_t *pos) |
| 450 | { |
| 451 | struct ftrace_event_call *call = v; |
| 452 | |
| 453 | (*pos)++; |
| 454 | |
| 455 | list_for_each_entry_continue(call, &ftrace_events, list) { |
| 456 | if (call->flags & TRACE_EVENT_FL_ENABLED) |
| 457 | return call; |
| 458 | } |
| 459 | |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 464 | { |
| 465 | struct ftrace_event_call *call; |
| 466 | loff_t l; |
| 467 | |
| 468 | mutex_lock(&event_mutex); |
| 469 | |
| 470 | call = list_entry(&ftrace_events, struct ftrace_event_call, list); |
| 471 | for (l = 0; l <= *pos; ) { |
| 472 | call = s_next(m, call, &l); |
| 473 | if (!call) |
| 474 | break; |
| 475 | } |
| 476 | return call; |
| 477 | } |
| 478 | |
| 479 | static int t_show(struct seq_file *m, void *v) |
| 480 | { |
| 481 | struct ftrace_event_call *call = v; |
| 482 | |
| 483 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
| 484 | seq_printf(m, "%s:", call->class->system); |
| 485 | seq_printf(m, "%s\n", call->name); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static void t_stop(struct seq_file *m, void *p) |
| 491 | { |
| 492 | mutex_unlock(&event_mutex); |
| 493 | } |
| 494 | |
| 495 | static int |
| 496 | ftrace_event_seq_open(struct inode *inode, struct file *file) |
| 497 | { |
| 498 | const struct seq_operations *seq_ops; |
| 499 | |
| 500 | if ((file->f_mode & FMODE_WRITE) && |
| 501 | (file->f_flags & O_TRUNC)) |
| 502 | ftrace_clear_events(); |
| 503 | |
| 504 | seq_ops = inode->i_private; |
| 505 | return seq_open(file, seq_ops); |
| 506 | } |
| 507 | |
| 508 | static ssize_t |
| 509 | event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 510 | loff_t *ppos) |
| 511 | { |
| 512 | struct ftrace_event_call *call = filp->private_data; |
| 513 | char *buf; |
| 514 | |
| 515 | if (call->flags & TRACE_EVENT_FL_ENABLED) |
| 516 | buf = "1\n"; |
| 517 | else |
| 518 | buf = "0\n"; |
| 519 | |
| 520 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 521 | } |
| 522 | |
| 523 | static ssize_t |
| 524 | event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 525 | loff_t *ppos) |
| 526 | { |
| 527 | struct ftrace_event_call *call = filp->private_data; |
| 528 | unsigned long val; |
| 529 | int ret; |
| 530 | |
| 531 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | |
| 535 | ret = tracing_update_buffers(); |
| 536 | if (ret < 0) |
| 537 | return ret; |
| 538 | |
| 539 | switch (val) { |
| 540 | case 0: |
| 541 | case 1: |
| 542 | mutex_lock(&event_mutex); |
| 543 | ret = ftrace_event_enable_disable(call, val); |
| 544 | mutex_unlock(&event_mutex); |
| 545 | break; |
| 546 | |
| 547 | default: |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | *ppos += cnt; |
| 552 | |
| 553 | return ret ? ret : cnt; |
| 554 | } |
| 555 | |
| 556 | static ssize_t |
| 557 | system_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 558 | loff_t *ppos) |
| 559 | { |
| 560 | const char set_to_char[4] = { '?', '0', '1', 'X' }; |
| 561 | struct event_subsystem *system = filp->private_data; |
| 562 | struct ftrace_event_call *call; |
| 563 | char buf[2]; |
| 564 | int set = 0; |
| 565 | int ret; |
| 566 | |
| 567 | mutex_lock(&event_mutex); |
| 568 | list_for_each_entry(call, &ftrace_events, list) { |
| 569 | if (!call->name || !call->class || !call->class->reg) |
| 570 | continue; |
| 571 | |
| 572 | if (system && strcmp(call->class->system, system->name) != 0) |
| 573 | continue; |
| 574 | |
| 575 | /* |
| 576 | * We need to find out if all the events are set |
| 577 | * or if all events or cleared, or if we have |
| 578 | * a mixture. |
| 579 | */ |
| 580 | set |= (1 << !!(call->flags & TRACE_EVENT_FL_ENABLED)); |
| 581 | |
| 582 | /* |
| 583 | * If we have a mixture, no need to look further. |
| 584 | */ |
| 585 | if (set == 3) |
| 586 | break; |
| 587 | } |
| 588 | mutex_unlock(&event_mutex); |
| 589 | |
| 590 | buf[0] = set_to_char[set]; |
| 591 | buf[1] = '\n'; |
| 592 | |
| 593 | ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 594 | |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static ssize_t |
| 599 | system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 600 | loff_t *ppos) |
| 601 | { |
| 602 | struct event_subsystem *system = filp->private_data; |
| 603 | const char *name = NULL; |
| 604 | unsigned long val; |
| 605 | ssize_t ret; |
| 606 | |
| 607 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | |
| 611 | ret = tracing_update_buffers(); |
| 612 | if (ret < 0) |
| 613 | return ret; |
| 614 | |
| 615 | if (val != 0 && val != 1) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | /* |
| 619 | * Opening of "enable" adds a ref count to system, |
| 620 | * so the name is safe to use. |
| 621 | */ |
| 622 | if (system) |
| 623 | name = system->name; |
| 624 | |
| 625 | ret = __ftrace_set_clr_event(NULL, name, NULL, val); |
| 626 | if (ret) |
| 627 | goto out; |
| 628 | |
| 629 | ret = cnt; |
| 630 | |
| 631 | out: |
| 632 | *ppos += cnt; |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | enum { |
| 638 | FORMAT_HEADER = 1, |
| 639 | FORMAT_FIELD_SEPERATOR = 2, |
| 640 | FORMAT_PRINTFMT = 3, |
| 641 | }; |
| 642 | |
| 643 | static void *f_next(struct seq_file *m, void *v, loff_t *pos) |
| 644 | { |
| 645 | struct ftrace_event_call *call = m->private; |
| 646 | struct ftrace_event_field *field; |
| 647 | struct list_head *common_head = &ftrace_common_fields; |
| 648 | struct list_head *head = trace_get_fields(call); |
| 649 | |
| 650 | (*pos)++; |
| 651 | |
| 652 | switch ((unsigned long)v) { |
| 653 | case FORMAT_HEADER: |
| 654 | if (unlikely(list_empty(common_head))) |
| 655 | return NULL; |
| 656 | |
| 657 | field = list_entry(common_head->prev, |
| 658 | struct ftrace_event_field, link); |
| 659 | return field; |
| 660 | |
| 661 | case FORMAT_FIELD_SEPERATOR: |
| 662 | if (unlikely(list_empty(head))) |
| 663 | return NULL; |
| 664 | |
| 665 | field = list_entry(head->prev, struct ftrace_event_field, link); |
| 666 | return field; |
| 667 | |
| 668 | case FORMAT_PRINTFMT: |
| 669 | /* all done */ |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | field = v; |
| 674 | if (field->link.prev == common_head) |
| 675 | return (void *)FORMAT_FIELD_SEPERATOR; |
| 676 | else if (field->link.prev == head) |
| 677 | return (void *)FORMAT_PRINTFMT; |
| 678 | |
| 679 | field = list_entry(field->link.prev, struct ftrace_event_field, link); |
| 680 | |
| 681 | return field; |
| 682 | } |
| 683 | |
| 684 | static void *f_start(struct seq_file *m, loff_t *pos) |
| 685 | { |
| 686 | loff_t l = 0; |
| 687 | void *p; |
| 688 | |
| 689 | /* Start by showing the header */ |
| 690 | if (!*pos) |
| 691 | return (void *)FORMAT_HEADER; |
| 692 | |
| 693 | p = (void *)FORMAT_HEADER; |
| 694 | do { |
| 695 | p = f_next(m, p, &l); |
| 696 | } while (p && l < *pos); |
| 697 | |
| 698 | return p; |
| 699 | } |
| 700 | |
| 701 | static int f_show(struct seq_file *m, void *v) |
| 702 | { |
| 703 | struct ftrace_event_call *call = m->private; |
| 704 | struct ftrace_event_field *field; |
| 705 | const char *array_descriptor; |
| 706 | |
| 707 | switch ((unsigned long)v) { |
| 708 | case FORMAT_HEADER: |
| 709 | seq_printf(m, "name: %s\n", call->name); |
| 710 | seq_printf(m, "ID: %d\n", call->event.type); |
| 711 | seq_printf(m, "format:\n"); |
| 712 | return 0; |
| 713 | |
| 714 | case FORMAT_FIELD_SEPERATOR: |
| 715 | seq_putc(m, '\n'); |
| 716 | return 0; |
| 717 | |
| 718 | case FORMAT_PRINTFMT: |
| 719 | seq_printf(m, "\nprint fmt: %s\n", |
| 720 | call->print_fmt); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | field = v; |
| 725 | |
| 726 | /* |
| 727 | * Smartly shows the array type(except dynamic array). |
| 728 | * Normal: |
| 729 | * field:TYPE VAR |
| 730 | * If TYPE := TYPE[LEN], it is shown: |
| 731 | * field:TYPE VAR[LEN] |
| 732 | */ |
| 733 | array_descriptor = strchr(field->type, '['); |
| 734 | |
| 735 | if (!strncmp(field->type, "__data_loc", 10)) |
| 736 | array_descriptor = NULL; |
| 737 | |
| 738 | if (!array_descriptor) |
| 739 | seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", |
| 740 | field->type, field->name, field->offset, |
| 741 | field->size, !!field->is_signed); |
| 742 | else |
| 743 | seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n", |
| 744 | (int)(array_descriptor - field->type), |
| 745 | field->type, field->name, |
| 746 | array_descriptor, field->offset, |
| 747 | field->size, !!field->is_signed); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static void f_stop(struct seq_file *m, void *p) |
| 753 | { |
| 754 | } |
| 755 | |
| 756 | static const struct seq_operations trace_format_seq_ops = { |
| 757 | .start = f_start, |
| 758 | .next = f_next, |
| 759 | .stop = f_stop, |
| 760 | .show = f_show, |
| 761 | }; |
| 762 | |
| 763 | static int trace_format_open(struct inode *inode, struct file *file) |
| 764 | { |
| 765 | struct ftrace_event_call *call = inode->i_private; |
| 766 | struct seq_file *m; |
| 767 | int ret; |
| 768 | |
| 769 | ret = seq_open(file, &trace_format_seq_ops); |
| 770 | if (ret < 0) |
| 771 | return ret; |
| 772 | |
| 773 | m = file->private_data; |
| 774 | m->private = call; |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static ssize_t |
| 780 | event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 781 | { |
| 782 | struct ftrace_event_call *call = filp->private_data; |
| 783 | struct trace_seq *s; |
| 784 | int r; |
| 785 | |
| 786 | if (*ppos) |
| 787 | return 0; |
| 788 | |
| 789 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 790 | if (!s) |
| 791 | return -ENOMEM; |
| 792 | |
| 793 | trace_seq_init(s); |
| 794 | trace_seq_printf(s, "%d\n", call->event.type); |
| 795 | |
| 796 | r = simple_read_from_buffer(ubuf, cnt, ppos, |
| 797 | s->buffer, s->len); |
| 798 | kfree(s); |
| 799 | return r; |
| 800 | } |
| 801 | |
| 802 | static ssize_t |
| 803 | event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 804 | loff_t *ppos) |
| 805 | { |
| 806 | struct ftrace_event_call *call = filp->private_data; |
| 807 | struct trace_seq *s; |
| 808 | int r; |
| 809 | |
| 810 | if (*ppos) |
| 811 | return 0; |
| 812 | |
| 813 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 814 | if (!s) |
| 815 | return -ENOMEM; |
| 816 | |
| 817 | trace_seq_init(s); |
| 818 | |
| 819 | print_event_filter(call, s); |
| 820 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
| 821 | |
| 822 | kfree(s); |
| 823 | |
| 824 | return r; |
| 825 | } |
| 826 | |
| 827 | static ssize_t |
| 828 | event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 829 | loff_t *ppos) |
| 830 | { |
| 831 | struct ftrace_event_call *call = filp->private_data; |
| 832 | char *buf; |
| 833 | int err; |
| 834 | |
| 835 | if (cnt >= PAGE_SIZE) |
| 836 | return -EINVAL; |
| 837 | |
| 838 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
| 839 | if (!buf) |
| 840 | return -ENOMEM; |
| 841 | |
| 842 | if (copy_from_user(buf, ubuf, cnt)) { |
| 843 | free_page((unsigned long) buf); |
| 844 | return -EFAULT; |
| 845 | } |
| 846 | buf[cnt] = '\0'; |
| 847 | |
| 848 | err = apply_event_filter(call, buf); |
| 849 | free_page((unsigned long) buf); |
| 850 | if (err < 0) |
| 851 | return err; |
| 852 | |
| 853 | *ppos += cnt; |
| 854 | |
| 855 | return cnt; |
| 856 | } |
| 857 | |
| 858 | static LIST_HEAD(event_subsystems); |
| 859 | |
| 860 | static int subsystem_open(struct inode *inode, struct file *filp) |
| 861 | { |
| 862 | struct event_subsystem *system = NULL; |
| 863 | int ret; |
| 864 | |
| 865 | if (!inode->i_private) |
| 866 | goto skip_search; |
| 867 | |
| 868 | /* Make sure the system still exists */ |
| 869 | mutex_lock(&event_mutex); |
| 870 | list_for_each_entry(system, &event_subsystems, list) { |
| 871 | if (system == inode->i_private) { |
| 872 | /* Don't open systems with no events */ |
| 873 | if (!system->nr_events) { |
| 874 | system = NULL; |
| 875 | break; |
| 876 | } |
| 877 | __get_system(system); |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | mutex_unlock(&event_mutex); |
| 882 | |
| 883 | if (system != inode->i_private) |
| 884 | return -ENODEV; |
| 885 | |
| 886 | skip_search: |
| 887 | ret = tracing_open_generic(inode, filp); |
| 888 | if (ret < 0 && system) |
| 889 | put_system(system); |
| 890 | |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | static int subsystem_release(struct inode *inode, struct file *file) |
| 895 | { |
| 896 | struct event_subsystem *system = inode->i_private; |
| 897 | |
| 898 | if (system) |
| 899 | put_system(system); |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static ssize_t |
| 905 | subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 906 | loff_t *ppos) |
| 907 | { |
| 908 | struct event_subsystem *system = filp->private_data; |
| 909 | struct trace_seq *s; |
| 910 | int r; |
| 911 | |
| 912 | if (*ppos) |
| 913 | return 0; |
| 914 | |
| 915 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 916 | if (!s) |
| 917 | return -ENOMEM; |
| 918 | |
| 919 | trace_seq_init(s); |
| 920 | |
| 921 | print_subsystem_event_filter(system, s); |
| 922 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
| 923 | |
| 924 | kfree(s); |
| 925 | |
| 926 | return r; |
| 927 | } |
| 928 | |
| 929 | static ssize_t |
| 930 | subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 931 | loff_t *ppos) |
| 932 | { |
| 933 | struct event_subsystem *system = filp->private_data; |
| 934 | char *buf; |
| 935 | int err; |
| 936 | |
| 937 | if (cnt >= PAGE_SIZE) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | buf = (char *)__get_free_page(GFP_TEMPORARY); |
| 941 | if (!buf) |
| 942 | return -ENOMEM; |
| 943 | |
| 944 | if (copy_from_user(buf, ubuf, cnt)) { |
| 945 | free_page((unsigned long) buf); |
| 946 | return -EFAULT; |
| 947 | } |
| 948 | buf[cnt] = '\0'; |
| 949 | |
| 950 | err = apply_subsystem_event_filter(system, buf); |
| 951 | free_page((unsigned long) buf); |
| 952 | if (err < 0) |
| 953 | return err; |
| 954 | |
| 955 | *ppos += cnt; |
| 956 | |
| 957 | return cnt; |
| 958 | } |
| 959 | |
| 960 | static ssize_t |
| 961 | show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) |
| 962 | { |
| 963 | int (*func)(struct trace_seq *s) = filp->private_data; |
| 964 | struct trace_seq *s; |
| 965 | int r; |
| 966 | |
| 967 | if (*ppos) |
| 968 | return 0; |
| 969 | |
| 970 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 971 | if (!s) |
| 972 | return -ENOMEM; |
| 973 | |
| 974 | trace_seq_init(s); |
| 975 | |
| 976 | func(s); |
| 977 | r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len); |
| 978 | |
| 979 | kfree(s); |
| 980 | |
| 981 | return r; |
| 982 | } |
| 983 | |
| 984 | static const struct seq_operations show_event_seq_ops = { |
| 985 | .start = t_start, |
| 986 | .next = t_next, |
| 987 | .show = t_show, |
| 988 | .stop = t_stop, |
| 989 | }; |
| 990 | |
| 991 | static const struct seq_operations show_set_event_seq_ops = { |
| 992 | .start = s_start, |
| 993 | .next = s_next, |
| 994 | .show = t_show, |
| 995 | .stop = t_stop, |
| 996 | }; |
| 997 | |
| 998 | static const struct file_operations ftrace_avail_fops = { |
| 999 | .open = ftrace_event_seq_open, |
| 1000 | .read = seq_read, |
| 1001 | .llseek = seq_lseek, |
| 1002 | .release = seq_release, |
| 1003 | }; |
| 1004 | |
| 1005 | static const struct file_operations ftrace_set_event_fops = { |
| 1006 | .open = ftrace_event_seq_open, |
| 1007 | .read = seq_read, |
| 1008 | .write = ftrace_event_write, |
| 1009 | .llseek = seq_lseek, |
| 1010 | .release = seq_release, |
| 1011 | }; |
| 1012 | |
| 1013 | static const struct file_operations ftrace_enable_fops = { |
| 1014 | .open = tracing_open_generic, |
| 1015 | .read = event_enable_read, |
| 1016 | .write = event_enable_write, |
| 1017 | .llseek = default_llseek, |
| 1018 | }; |
| 1019 | |
| 1020 | static const struct file_operations ftrace_event_format_fops = { |
| 1021 | .open = trace_format_open, |
| 1022 | .read = seq_read, |
| 1023 | .llseek = seq_lseek, |
| 1024 | .release = seq_release, |
| 1025 | }; |
| 1026 | |
| 1027 | static const struct file_operations ftrace_event_id_fops = { |
| 1028 | .open = tracing_open_generic, |
| 1029 | .read = event_id_read, |
| 1030 | .llseek = default_llseek, |
| 1031 | }; |
| 1032 | |
| 1033 | static const struct file_operations ftrace_event_filter_fops = { |
| 1034 | .open = tracing_open_generic, |
| 1035 | .read = event_filter_read, |
| 1036 | .write = event_filter_write, |
| 1037 | .llseek = default_llseek, |
| 1038 | }; |
| 1039 | |
| 1040 | static const struct file_operations ftrace_subsystem_filter_fops = { |
| 1041 | .open = subsystem_open, |
| 1042 | .read = subsystem_filter_read, |
| 1043 | .write = subsystem_filter_write, |
| 1044 | .llseek = default_llseek, |
| 1045 | .release = subsystem_release, |
| 1046 | }; |
| 1047 | |
| 1048 | static const struct file_operations ftrace_system_enable_fops = { |
| 1049 | .open = subsystem_open, |
| 1050 | .read = system_enable_read, |
| 1051 | .write = system_enable_write, |
| 1052 | .llseek = default_llseek, |
| 1053 | .release = subsystem_release, |
| 1054 | }; |
| 1055 | |
| 1056 | static const struct file_operations ftrace_show_header_fops = { |
| 1057 | .open = tracing_open_generic, |
| 1058 | .read = show_header, |
| 1059 | .llseek = default_llseek, |
| 1060 | }; |
| 1061 | |
| 1062 | static struct dentry *event_trace_events_dir(void) |
| 1063 | { |
| 1064 | static struct dentry *d_tracer; |
| 1065 | static struct dentry *d_events; |
| 1066 | |
| 1067 | if (d_events) |
| 1068 | return d_events; |
| 1069 | |
| 1070 | d_tracer = tracing_init_dentry(); |
| 1071 | if (!d_tracer) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | d_events = debugfs_create_dir("events", d_tracer); |
| 1075 | if (!d_events) |
| 1076 | pr_warning("Could not create debugfs " |
| 1077 | "'events' directory\n"); |
| 1078 | |
| 1079 | return d_events; |
| 1080 | } |
| 1081 | |
| 1082 | static struct dentry * |
| 1083 | event_subsystem_dir(const char *name, struct dentry *d_events) |
| 1084 | { |
| 1085 | struct event_subsystem *system; |
| 1086 | struct dentry *entry; |
| 1087 | |
| 1088 | /* First see if we did not already create this dir */ |
| 1089 | list_for_each_entry(system, &event_subsystems, list) { |
| 1090 | if (strcmp(system->name, name) == 0) { |
| 1091 | system->nr_events++; |
| 1092 | return system->entry; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* need to create new entry */ |
| 1097 | system = kmalloc(sizeof(*system), GFP_KERNEL); |
| 1098 | if (!system) { |
| 1099 | pr_warning("No memory to create event subsystem %s\n", |
| 1100 | name); |
| 1101 | return d_events; |
| 1102 | } |
| 1103 | |
| 1104 | system->entry = debugfs_create_dir(name, d_events); |
| 1105 | if (!system->entry) { |
| 1106 | pr_warning("Could not create event subsystem %s\n", |
| 1107 | name); |
| 1108 | kfree(system); |
| 1109 | return d_events; |
| 1110 | } |
| 1111 | |
| 1112 | system->nr_events = 1; |
| 1113 | system->ref_count = 1; |
| 1114 | system->name = kstrdup(name, GFP_KERNEL); |
| 1115 | if (!system->name) { |
| 1116 | debugfs_remove(system->entry); |
| 1117 | kfree(system); |
| 1118 | return d_events; |
| 1119 | } |
| 1120 | |
| 1121 | list_add(&system->list, &event_subsystems); |
| 1122 | |
| 1123 | system->filter = NULL; |
| 1124 | |
| 1125 | system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL); |
| 1126 | if (!system->filter) { |
| 1127 | pr_warning("Could not allocate filter for subsystem " |
| 1128 | "'%s'\n", name); |
| 1129 | return system->entry; |
| 1130 | } |
| 1131 | |
| 1132 | entry = debugfs_create_file("filter", 0644, system->entry, system, |
| 1133 | &ftrace_subsystem_filter_fops); |
| 1134 | if (!entry) { |
| 1135 | kfree(system->filter); |
| 1136 | system->filter = NULL; |
| 1137 | pr_warning("Could not create debugfs " |
| 1138 | "'%s/filter' entry\n", name); |
| 1139 | } |
| 1140 | |
| 1141 | trace_create_file("enable", 0644, system->entry, system, |
| 1142 | &ftrace_system_enable_fops); |
| 1143 | |
| 1144 | return system->entry; |
| 1145 | } |
| 1146 | |
| 1147 | static int |
| 1148 | event_create_dir(struct ftrace_event_call *call, struct dentry *d_events, |
| 1149 | const struct file_operations *id, |
| 1150 | const struct file_operations *enable, |
| 1151 | const struct file_operations *filter, |
| 1152 | const struct file_operations *format) |
| 1153 | { |
| 1154 | struct list_head *head; |
| 1155 | int ret; |
| 1156 | |
| 1157 | /* |
| 1158 | * If the trace point header did not define TRACE_SYSTEM |
| 1159 | * then the system would be called "TRACE_SYSTEM". |
| 1160 | */ |
| 1161 | if (strcmp(call->class->system, TRACE_SYSTEM) != 0) |
| 1162 | d_events = event_subsystem_dir(call->class->system, d_events); |
| 1163 | |
| 1164 | call->dir = debugfs_create_dir(call->name, d_events); |
| 1165 | if (!call->dir) { |
| 1166 | pr_warning("Could not create debugfs " |
| 1167 | "'%s' directory\n", call->name); |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
| 1171 | if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) |
| 1172 | trace_create_file("enable", 0644, call->dir, call, |
| 1173 | enable); |
| 1174 | |
| 1175 | #ifdef CONFIG_PERF_EVENTS |
| 1176 | if (call->event.type && call->class->reg) |
| 1177 | trace_create_file("id", 0444, call->dir, call, |
| 1178 | id); |
| 1179 | #endif |
| 1180 | |
| 1181 | /* |
| 1182 | * Other events may have the same class. Only update |
| 1183 | * the fields if they are not already defined. |
| 1184 | */ |
| 1185 | head = trace_get_fields(call); |
| 1186 | if (list_empty(head)) { |
| 1187 | ret = call->class->define_fields(call); |
| 1188 | if (ret < 0) { |
| 1189 | pr_warning("Could not initialize trace point" |
| 1190 | " events/%s\n", call->name); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | } |
| 1194 | trace_create_file("filter", 0644, call->dir, call, |
| 1195 | filter); |
| 1196 | |
| 1197 | trace_create_file("format", 0444, call->dir, call, |
| 1198 | format); |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int |
| 1204 | __trace_add_event_call(struct ftrace_event_call *call, struct module *mod, |
| 1205 | const struct file_operations *id, |
| 1206 | const struct file_operations *enable, |
| 1207 | const struct file_operations *filter, |
| 1208 | const struct file_operations *format) |
| 1209 | { |
| 1210 | struct dentry *d_events; |
| 1211 | int ret; |
| 1212 | |
| 1213 | /* The linker may leave blanks */ |
| 1214 | if (!call->name) |
| 1215 | return -EINVAL; |
| 1216 | |
| 1217 | if (call->class->raw_init) { |
| 1218 | ret = call->class->raw_init(call); |
| 1219 | if (ret < 0) { |
| 1220 | if (ret != -ENOSYS) |
| 1221 | pr_warning("Could not initialize trace events/%s\n", |
| 1222 | call->name); |
| 1223 | return ret; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | d_events = event_trace_events_dir(); |
| 1228 | if (!d_events) |
| 1229 | return -ENOENT; |
| 1230 | |
| 1231 | ret = event_create_dir(call, d_events, id, enable, filter, format); |
| 1232 | if (!ret) |
| 1233 | list_add(&call->list, &ftrace_events); |
| 1234 | call->mod = mod; |
| 1235 | |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | /* Add an additional event_call dynamically */ |
| 1240 | int trace_add_event_call(struct ftrace_event_call *call) |
| 1241 | { |
| 1242 | int ret; |
| 1243 | mutex_lock(&event_mutex); |
| 1244 | ret = __trace_add_event_call(call, NULL, &ftrace_event_id_fops, |
| 1245 | &ftrace_enable_fops, |
| 1246 | &ftrace_event_filter_fops, |
| 1247 | &ftrace_event_format_fops); |
| 1248 | mutex_unlock(&event_mutex); |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | static void remove_subsystem_dir(const char *name) |
| 1253 | { |
| 1254 | struct event_subsystem *system; |
| 1255 | |
| 1256 | if (strcmp(name, TRACE_SYSTEM) == 0) |
| 1257 | return; |
| 1258 | |
| 1259 | list_for_each_entry(system, &event_subsystems, list) { |
| 1260 | if (strcmp(system->name, name) == 0) { |
| 1261 | if (!--system->nr_events) { |
| 1262 | debugfs_remove_recursive(system->entry); |
| 1263 | list_del(&system->list); |
| 1264 | __put_system(system); |
| 1265 | } |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * Must be called under locking both of event_mutex and trace_event_mutex. |
| 1273 | */ |
| 1274 | static void __trace_remove_event_call(struct ftrace_event_call *call) |
| 1275 | { |
| 1276 | ftrace_event_enable_disable(call, 0); |
| 1277 | if (call->event.funcs) |
| 1278 | __unregister_ftrace_event(&call->event); |
| 1279 | debugfs_remove_recursive(call->dir); |
| 1280 | list_del(&call->list); |
| 1281 | trace_destroy_fields(call); |
| 1282 | destroy_preds(call); |
| 1283 | remove_subsystem_dir(call->class->system); |
| 1284 | } |
| 1285 | |
| 1286 | /* Remove an event_call */ |
| 1287 | void trace_remove_event_call(struct ftrace_event_call *call) |
| 1288 | { |
| 1289 | mutex_lock(&event_mutex); |
| 1290 | down_write(&trace_event_mutex); |
| 1291 | __trace_remove_event_call(call); |
| 1292 | up_write(&trace_event_mutex); |
| 1293 | mutex_unlock(&event_mutex); |
| 1294 | } |
| 1295 | |
| 1296 | #define for_each_event(event, start, end) \ |
| 1297 | for (event = start; \ |
| 1298 | (unsigned long)event < (unsigned long)end; \ |
| 1299 | event++) |
| 1300 | |
| 1301 | #ifdef CONFIG_MODULES |
| 1302 | |
| 1303 | static LIST_HEAD(ftrace_module_file_list); |
| 1304 | |
| 1305 | /* |
| 1306 | * Modules must own their file_operations to keep up with |
| 1307 | * reference counting. |
| 1308 | */ |
| 1309 | struct ftrace_module_file_ops { |
| 1310 | struct list_head list; |
| 1311 | struct module *mod; |
| 1312 | struct file_operations id; |
| 1313 | struct file_operations enable; |
| 1314 | struct file_operations format; |
| 1315 | struct file_operations filter; |
| 1316 | }; |
| 1317 | |
| 1318 | static struct ftrace_module_file_ops * |
| 1319 | trace_create_file_ops(struct module *mod) |
| 1320 | { |
| 1321 | struct ftrace_module_file_ops *file_ops; |
| 1322 | |
| 1323 | /* |
| 1324 | * This is a bit of a PITA. To allow for correct reference |
| 1325 | * counting, modules must "own" their file_operations. |
| 1326 | * To do this, we allocate the file operations that will be |
| 1327 | * used in the event directory. |
| 1328 | */ |
| 1329 | |
| 1330 | file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL); |
| 1331 | if (!file_ops) |
| 1332 | return NULL; |
| 1333 | |
| 1334 | file_ops->mod = mod; |
| 1335 | |
| 1336 | file_ops->id = ftrace_event_id_fops; |
| 1337 | file_ops->id.owner = mod; |
| 1338 | |
| 1339 | file_ops->enable = ftrace_enable_fops; |
| 1340 | file_ops->enable.owner = mod; |
| 1341 | |
| 1342 | file_ops->filter = ftrace_event_filter_fops; |
| 1343 | file_ops->filter.owner = mod; |
| 1344 | |
| 1345 | file_ops->format = ftrace_event_format_fops; |
| 1346 | file_ops->format.owner = mod; |
| 1347 | |
| 1348 | list_add(&file_ops->list, &ftrace_module_file_list); |
| 1349 | |
| 1350 | return file_ops; |
| 1351 | } |
| 1352 | |
| 1353 | static void trace_module_add_events(struct module *mod) |
| 1354 | { |
| 1355 | struct ftrace_module_file_ops *file_ops = NULL; |
| 1356 | struct ftrace_event_call **call, **start, **end; |
| 1357 | |
| 1358 | if (!mod->num_trace_events) |
| 1359 | return; |
| 1360 | |
| 1361 | /* Don't add infrastructure for mods without tracepoints */ |
| 1362 | if (trace_module_has_bad_taint(mod)) { |
| 1363 | pr_err("%s: module has bad taint, not creating trace events\n", |
| 1364 | mod->name); |
| 1365 | return; |
| 1366 | } |
| 1367 | |
| 1368 | start = mod->trace_events; |
| 1369 | end = mod->trace_events + mod->num_trace_events; |
| 1370 | |
| 1371 | if (start == end) |
| 1372 | return; |
| 1373 | |
| 1374 | file_ops = trace_create_file_ops(mod); |
| 1375 | if (!file_ops) |
| 1376 | return; |
| 1377 | |
| 1378 | for_each_event(call, start, end) { |
| 1379 | __trace_add_event_call(*call, mod, |
| 1380 | &file_ops->id, &file_ops->enable, |
| 1381 | &file_ops->filter, &file_ops->format); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | static void trace_module_remove_events(struct module *mod) |
| 1386 | { |
| 1387 | struct ftrace_module_file_ops *file_ops; |
| 1388 | struct ftrace_event_call *call, *p; |
| 1389 | bool found = false; |
| 1390 | |
| 1391 | down_write(&trace_event_mutex); |
| 1392 | list_for_each_entry_safe(call, p, &ftrace_events, list) { |
| 1393 | if (call->mod == mod) { |
| 1394 | found = true; |
| 1395 | __trace_remove_event_call(call); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | /* Now free the file_operations */ |
| 1400 | list_for_each_entry(file_ops, &ftrace_module_file_list, list) { |
| 1401 | if (file_ops->mod == mod) |
| 1402 | break; |
| 1403 | } |
| 1404 | if (&file_ops->list != &ftrace_module_file_list) { |
| 1405 | list_del(&file_ops->list); |
| 1406 | kfree(file_ops); |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * It is safest to reset the ring buffer if the module being unloaded |
| 1411 | * registered any events. |
| 1412 | */ |
| 1413 | if (found) |
| 1414 | tracing_reset_current_online_cpus(); |
| 1415 | up_write(&trace_event_mutex); |
| 1416 | } |
| 1417 | |
| 1418 | static int trace_module_notify(struct notifier_block *self, |
| 1419 | unsigned long val, void *data) |
| 1420 | { |
| 1421 | struct module *mod = data; |
| 1422 | |
| 1423 | mutex_lock(&event_mutex); |
| 1424 | switch (val) { |
| 1425 | case MODULE_STATE_COMING: |
| 1426 | trace_module_add_events(mod); |
| 1427 | break; |
| 1428 | case MODULE_STATE_GOING: |
| 1429 | trace_module_remove_events(mod); |
| 1430 | break; |
| 1431 | } |
| 1432 | mutex_unlock(&event_mutex); |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | #else |
| 1437 | static int trace_module_notify(struct notifier_block *self, |
| 1438 | unsigned long val, void *data) |
| 1439 | { |
| 1440 | return 0; |
| 1441 | } |
| 1442 | #endif /* CONFIG_MODULES */ |
| 1443 | |
| 1444 | static struct notifier_block trace_module_nb = { |
| 1445 | .notifier_call = trace_module_notify, |
| 1446 | .priority = 0, |
| 1447 | }; |
| 1448 | |
| 1449 | extern struct ftrace_event_call *__start_ftrace_events[]; |
| 1450 | extern struct ftrace_event_call *__stop_ftrace_events[]; |
| 1451 | |
| 1452 | static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata; |
| 1453 | |
| 1454 | static __init int setup_trace_event(char *str) |
| 1455 | { |
| 1456 | strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE); |
| 1457 | ring_buffer_expanded = 1; |
| 1458 | tracing_selftest_disabled = 1; |
| 1459 | |
| 1460 | return 1; |
| 1461 | } |
| 1462 | __setup("trace_event=", setup_trace_event); |
| 1463 | |
| 1464 | static __init int event_trace_init(void) |
| 1465 | { |
| 1466 | struct ftrace_event_call **call; |
| 1467 | struct dentry *d_tracer; |
| 1468 | struct dentry *entry; |
| 1469 | struct dentry *d_events; |
| 1470 | int ret; |
| 1471 | char *buf = bootup_event_buf; |
| 1472 | char *token; |
| 1473 | |
| 1474 | d_tracer = tracing_init_dentry(); |
| 1475 | if (!d_tracer) |
| 1476 | return 0; |
| 1477 | |
| 1478 | entry = debugfs_create_file("available_events", 0444, d_tracer, |
| 1479 | (void *)&show_event_seq_ops, |
| 1480 | &ftrace_avail_fops); |
| 1481 | if (!entry) |
| 1482 | pr_warning("Could not create debugfs " |
| 1483 | "'available_events' entry\n"); |
| 1484 | |
| 1485 | entry = debugfs_create_file("set_event", 0644, d_tracer, |
| 1486 | (void *)&show_set_event_seq_ops, |
| 1487 | &ftrace_set_event_fops); |
| 1488 | if (!entry) |
| 1489 | pr_warning("Could not create debugfs " |
| 1490 | "'set_event' entry\n"); |
| 1491 | |
| 1492 | d_events = event_trace_events_dir(); |
| 1493 | if (!d_events) |
| 1494 | return 0; |
| 1495 | |
| 1496 | /* ring buffer internal formats */ |
| 1497 | trace_create_file("header_page", 0444, d_events, |
| 1498 | ring_buffer_print_page_header, |
| 1499 | &ftrace_show_header_fops); |
| 1500 | |
| 1501 | trace_create_file("header_event", 0444, d_events, |
| 1502 | ring_buffer_print_entry_header, |
| 1503 | &ftrace_show_header_fops); |
| 1504 | |
| 1505 | trace_create_file("enable", 0644, d_events, |
| 1506 | NULL, &ftrace_system_enable_fops); |
| 1507 | |
| 1508 | if (trace_define_common_fields()) |
| 1509 | pr_warning("tracing: Failed to allocate common fields"); |
| 1510 | |
| 1511 | for_each_event(call, __start_ftrace_events, __stop_ftrace_events) { |
| 1512 | __trace_add_event_call(*call, NULL, &ftrace_event_id_fops, |
| 1513 | &ftrace_enable_fops, |
| 1514 | &ftrace_event_filter_fops, |
| 1515 | &ftrace_event_format_fops); |
| 1516 | } |
| 1517 | |
| 1518 | while (true) { |
| 1519 | token = strsep(&buf, ","); |
| 1520 | |
| 1521 | if (!token) |
| 1522 | break; |
| 1523 | if (!*token) |
| 1524 | continue; |
| 1525 | |
| 1526 | ret = ftrace_set_clr_event(token, 1); |
| 1527 | if (ret) |
| 1528 | pr_warning("Failed to enable trace event: %s\n", token); |
| 1529 | } |
| 1530 | |
| 1531 | ret = register_module_notifier(&trace_module_nb); |
| 1532 | if (ret) |
| 1533 | pr_warning("Failed to register trace events module notifier\n"); |
| 1534 | |
| 1535 | return 0; |
| 1536 | } |
| 1537 | fs_initcall(event_trace_init); |
| 1538 | |
| 1539 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1540 | |
| 1541 | static DEFINE_SPINLOCK(test_spinlock); |
| 1542 | static DEFINE_SPINLOCK(test_spinlock_irq); |
| 1543 | static DEFINE_MUTEX(test_mutex); |
| 1544 | |
| 1545 | static __init void test_work(struct work_struct *dummy) |
| 1546 | { |
| 1547 | spin_lock(&test_spinlock); |
| 1548 | spin_lock_irq(&test_spinlock_irq); |
| 1549 | udelay(1); |
| 1550 | spin_unlock_irq(&test_spinlock_irq); |
| 1551 | spin_unlock(&test_spinlock); |
| 1552 | |
| 1553 | mutex_lock(&test_mutex); |
| 1554 | msleep(1); |
| 1555 | mutex_unlock(&test_mutex); |
| 1556 | } |
| 1557 | |
| 1558 | static __init int event_test_thread(void *unused) |
| 1559 | { |
| 1560 | void *test_malloc; |
| 1561 | |
| 1562 | test_malloc = kmalloc(1234, GFP_KERNEL); |
| 1563 | if (!test_malloc) |
| 1564 | pr_info("failed to kmalloc\n"); |
| 1565 | |
| 1566 | schedule_on_each_cpu(test_work); |
| 1567 | |
| 1568 | kfree(test_malloc); |
| 1569 | |
| 1570 | set_current_state(TASK_INTERRUPTIBLE); |
| 1571 | while (!kthread_should_stop()) |
| 1572 | schedule(); |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Do various things that may trigger events. |
| 1579 | */ |
| 1580 | static __init void event_test_stuff(void) |
| 1581 | { |
| 1582 | struct task_struct *test_thread; |
| 1583 | |
| 1584 | test_thread = kthread_run(event_test_thread, NULL, "test-events"); |
| 1585 | msleep(1); |
| 1586 | kthread_stop(test_thread); |
| 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | * For every trace event defined, we will test each trace point separately, |
| 1591 | * and then by groups, and finally all trace points. |
| 1592 | */ |
| 1593 | static __init void event_trace_self_tests(void) |
| 1594 | { |
| 1595 | struct ftrace_event_call *call; |
| 1596 | struct event_subsystem *system; |
| 1597 | int ret; |
| 1598 | |
| 1599 | pr_info("Running tests on trace events:\n"); |
| 1600 | |
| 1601 | list_for_each_entry(call, &ftrace_events, list) { |
| 1602 | |
| 1603 | /* Only test those that have a probe */ |
| 1604 | if (!call->class || !call->class->probe) |
| 1605 | continue; |
| 1606 | |
| 1607 | /* |
| 1608 | * Testing syscall events here is pretty useless, but |
| 1609 | * we still do it if configured. But this is time consuming. |
| 1610 | * What we really need is a user thread to perform the |
| 1611 | * syscalls as we test. |
| 1612 | */ |
| 1613 | #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS |
| 1614 | if (call->class->system && |
| 1615 | strcmp(call->class->system, "syscalls") == 0) |
| 1616 | continue; |
| 1617 | #endif |
| 1618 | |
| 1619 | pr_info("Testing event %s: ", call->name); |
| 1620 | |
| 1621 | /* |
| 1622 | * If an event is already enabled, someone is using |
| 1623 | * it and the self test should not be on. |
| 1624 | */ |
| 1625 | if (call->flags & TRACE_EVENT_FL_ENABLED) { |
| 1626 | pr_warning("Enabled event during self test!\n"); |
| 1627 | WARN_ON_ONCE(1); |
| 1628 | continue; |
| 1629 | } |
| 1630 | |
| 1631 | ftrace_event_enable_disable(call, 1); |
| 1632 | event_test_stuff(); |
| 1633 | ftrace_event_enable_disable(call, 0); |
| 1634 | |
| 1635 | pr_cont("OK\n"); |
| 1636 | } |
| 1637 | |
| 1638 | /* Now test at the sub system level */ |
| 1639 | |
| 1640 | pr_info("Running tests on trace event systems:\n"); |
| 1641 | |
| 1642 | list_for_each_entry(system, &event_subsystems, list) { |
| 1643 | |
| 1644 | /* the ftrace system is special, skip it */ |
| 1645 | if (strcmp(system->name, "ftrace") == 0) |
| 1646 | continue; |
| 1647 | |
| 1648 | pr_info("Testing event system %s: ", system->name); |
| 1649 | |
| 1650 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1); |
| 1651 | if (WARN_ON_ONCE(ret)) { |
| 1652 | pr_warning("error enabling system %s\n", |
| 1653 | system->name); |
| 1654 | continue; |
| 1655 | } |
| 1656 | |
| 1657 | event_test_stuff(); |
| 1658 | |
| 1659 | ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0); |
| 1660 | if (WARN_ON_ONCE(ret)) |
| 1661 | pr_warning("error disabling system %s\n", |
| 1662 | system->name); |
| 1663 | |
| 1664 | pr_cont("OK\n"); |
| 1665 | } |
| 1666 | |
| 1667 | /* Test with all events enabled */ |
| 1668 | |
| 1669 | pr_info("Running tests on all trace events:\n"); |
| 1670 | pr_info("Testing all events: "); |
| 1671 | |
| 1672 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1); |
| 1673 | if (WARN_ON_ONCE(ret)) { |
| 1674 | pr_warning("error enabling all events\n"); |
| 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | event_test_stuff(); |
| 1679 | |
| 1680 | /* reset sysname */ |
| 1681 | ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0); |
| 1682 | if (WARN_ON_ONCE(ret)) { |
| 1683 | pr_warning("error disabling all events\n"); |
| 1684 | return; |
| 1685 | } |
| 1686 | |
| 1687 | pr_cont("OK\n"); |
| 1688 | } |
| 1689 | |
| 1690 | #ifdef CONFIG_FUNCTION_TRACER |
| 1691 | |
| 1692 | static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable); |
| 1693 | |
| 1694 | static void |
| 1695 | function_test_events_call(unsigned long ip, unsigned long parent_ip) |
| 1696 | { |
| 1697 | struct ring_buffer_event *event; |
| 1698 | struct ring_buffer *buffer; |
| 1699 | struct ftrace_entry *entry; |
| 1700 | unsigned long flags; |
| 1701 | long disabled; |
| 1702 | int cpu; |
| 1703 | int pc; |
| 1704 | |
| 1705 | pc = preempt_count(); |
| 1706 | preempt_disable_notrace(); |
| 1707 | cpu = raw_smp_processor_id(); |
| 1708 | disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu)); |
| 1709 | |
| 1710 | if (disabled != 1) |
| 1711 | goto out; |
| 1712 | |
| 1713 | local_save_flags(flags); |
| 1714 | |
| 1715 | event = trace_current_buffer_lock_reserve(&buffer, |
| 1716 | TRACE_FN, sizeof(*entry), |
| 1717 | flags, pc); |
| 1718 | if (!event) |
| 1719 | goto out; |
| 1720 | entry = ring_buffer_event_data(event); |
| 1721 | entry->ip = ip; |
| 1722 | entry->parent_ip = parent_ip; |
| 1723 | |
| 1724 | trace_nowake_buffer_unlock_commit(buffer, event, flags, pc); |
| 1725 | |
| 1726 | out: |
| 1727 | atomic_dec(&per_cpu(ftrace_test_event_disable, cpu)); |
| 1728 | preempt_enable_notrace(); |
| 1729 | } |
| 1730 | |
| 1731 | static struct ftrace_ops trace_ops __initdata = |
| 1732 | { |
| 1733 | .func = function_test_events_call, |
| 1734 | }; |
| 1735 | |
| 1736 | static __init void event_trace_self_test_with_function(void) |
| 1737 | { |
| 1738 | int ret; |
| 1739 | ret = register_ftrace_function(&trace_ops); |
| 1740 | if (WARN_ON(ret < 0)) { |
| 1741 | pr_info("Failed to enable function tracer for event tests\n"); |
| 1742 | return; |
| 1743 | } |
| 1744 | pr_info("Running tests again, along with the function tracer\n"); |
| 1745 | event_trace_self_tests(); |
| 1746 | unregister_ftrace_function(&trace_ops); |
| 1747 | } |
| 1748 | #else |
| 1749 | static __init void event_trace_self_test_with_function(void) |
| 1750 | { |
| 1751 | } |
| 1752 | #endif |
| 1753 | |
| 1754 | static __init int event_trace_self_tests_init(void) |
| 1755 | { |
| 1756 | if (!tracing_selftest_disabled) { |
| 1757 | event_trace_self_tests(); |
| 1758 | event_trace_self_test_with_function(); |
| 1759 | } |
| 1760 | |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | late_initcall(event_trace_self_tests_init); |
| 1765 | |
| 1766 | #endif |