1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ring buffer based function tracer 4 * 5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 7 * 8 * Based on code from the latency_tracer, that is: 9 * 10 * Copyright (C) 2004-2006 Ingo Molnar 11 * Copyright (C) 2004 Nadia Yvette Chambers 12 */ 13 #include <linux/ring_buffer.h> 14 #include <linux/debugfs.h> 15 #include <linux/uaccess.h> 16 #include <linux/ftrace.h> 17 #include <linux/slab.h> 18 #include <linux/fs.h> 19 20 #include "trace.h" 21 22 static void tracing_start_function_trace(struct trace_array *tr); 23 static void tracing_stop_function_trace(struct trace_array *tr); 24 static void 25 function_trace_call(unsigned long ip, unsigned long parent_ip, 26 struct ftrace_ops *op, struct ftrace_regs *fregs); 27 static void 28 function_args_trace_call(unsigned long ip, unsigned long parent_ip, 29 struct ftrace_ops *op, struct ftrace_regs *fregs); 30 static void 31 function_stack_trace_call(unsigned long ip, unsigned long parent_ip, 32 struct ftrace_ops *op, struct ftrace_regs *fregs); 33 static void 34 function_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip, 35 struct ftrace_ops *op, struct ftrace_regs *fregs); 36 static void 37 function_stack_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip, 38 struct ftrace_ops *op, 39 struct ftrace_regs *fregs); 40 static struct tracer_flags func_flags; 41 42 /* Our option */ 43 enum { 44 45 TRACE_FUNC_NO_OPTS = 0x0, /* No flags set. */ 46 TRACE_FUNC_OPT_STACK = 0x1, 47 TRACE_FUNC_OPT_NO_REPEATS = 0x2, 48 TRACE_FUNC_OPT_ARGS = 0x4, 49 50 /* Update this to next highest bit. */ 51 TRACE_FUNC_OPT_HIGHEST_BIT = 0x8 52 }; 53 54 #define TRACE_FUNC_OPT_MASK (TRACE_FUNC_OPT_HIGHEST_BIT - 1) 55 56 int ftrace_allocate_ftrace_ops(struct trace_array *tr) 57 { 58 struct ftrace_ops *ops; 59 60 /* The top level array uses the "global_ops" */ 61 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 62 return 0; 63 64 ops = kzalloc_obj(*ops); 65 if (!ops) 66 return -ENOMEM; 67 68 /* Currently only the non stack version is supported */ 69 ops->func = function_trace_call; 70 ops->flags = FTRACE_OPS_FL_PID; 71 72 tr->ops = ops; 73 ops->private = tr; 74 75 return 0; 76 } 77 78 void ftrace_free_ftrace_ops(struct trace_array *tr) 79 { 80 kfree(tr->ops); 81 tr->ops = NULL; 82 } 83 84 int ftrace_create_function_files(struct trace_array *tr, 85 struct dentry *parent) 86 { 87 int ret; 88 /* 89 * The top level array uses the "global_ops", and the files are 90 * created on boot up. 91 */ 92 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 93 return 0; 94 95 if (!tr->ops) 96 return -EINVAL; 97 98 ret = allocate_fgraph_ops(tr, tr->ops); 99 if (ret) { 100 kfree(tr->ops); 101 return ret; 102 } 103 104 ftrace_create_filter_files(tr, parent); 105 106 return 0; 107 } 108 109 void ftrace_destroy_function_files(struct trace_array *tr) 110 { 111 ftrace_destroy_filter_files(tr->ops); 112 ftrace_free_ftrace_ops(tr); 113 free_fgraph_ops(tr); 114 } 115 116 static ftrace_func_t select_trace_function(u32 flags_val) 117 { 118 switch (flags_val & TRACE_FUNC_OPT_MASK) { 119 case TRACE_FUNC_NO_OPTS: 120 return function_trace_call; 121 case TRACE_FUNC_OPT_ARGS: 122 return function_args_trace_call; 123 case TRACE_FUNC_OPT_STACK: 124 case TRACE_FUNC_OPT_STACK | TRACE_FUNC_OPT_ARGS: 125 return function_stack_trace_call; 126 case TRACE_FUNC_OPT_NO_REPEATS: 127 return function_no_repeats_trace_call; 128 case TRACE_FUNC_OPT_STACK | TRACE_FUNC_OPT_NO_REPEATS: 129 return function_stack_no_repeats_trace_call; 130 default: 131 return NULL; 132 } 133 } 134 135 static bool handle_func_repeats(struct trace_array *tr, u32 flags_val) 136 { 137 if (!tr->last_func_repeats && 138 (flags_val & TRACE_FUNC_OPT_NO_REPEATS)) { 139 tr->last_func_repeats = alloc_percpu(struct trace_func_repeats); 140 if (!tr->last_func_repeats) 141 return false; 142 } 143 144 return true; 145 } 146 147 static int function_trace_init(struct trace_array *tr) 148 { 149 ftrace_func_t func; 150 /* 151 * Instance trace_arrays get their ops allocated 152 * at instance creation. Unless it failed 153 * the allocation. 154 */ 155 if (!tr->ops) 156 return -ENOMEM; 157 158 func = select_trace_function(tr->current_trace_flags->val); 159 if (!func) 160 return -EINVAL; 161 162 if (!handle_func_repeats(tr, tr->current_trace_flags->val)) 163 return -ENOMEM; 164 165 ftrace_init_array_ops(tr, func); 166 167 tr->array_buffer.cpu = raw_smp_processor_id(); 168 169 tracing_start_cmdline_record(); 170 tracing_start_function_trace(tr); 171 return 0; 172 } 173 174 static void function_trace_reset(struct trace_array *tr) 175 { 176 tracing_stop_function_trace(tr); 177 tracing_stop_cmdline_record(); 178 ftrace_reset_array_ops(tr); 179 } 180 181 static void function_trace_start(struct trace_array *tr) 182 { 183 tracing_reset_online_cpus(&tr->array_buffer); 184 } 185 186 /* fregs are guaranteed not to be NULL if HAVE_DYNAMIC_FTRACE_WITH_ARGS is set */ 187 #if defined(CONFIG_FUNCTION_GRAPH_TRACER) && defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) 188 static __always_inline unsigned long 189 function_get_true_parent_ip(unsigned long parent_ip, struct ftrace_regs *fregs) 190 { 191 unsigned long true_parent_ip; 192 int idx = 0; 193 194 true_parent_ip = parent_ip; 195 if (unlikely(parent_ip == (unsigned long)&return_to_handler) && fregs) 196 true_parent_ip = ftrace_graph_ret_addr(current, &idx, parent_ip, 197 (unsigned long *)ftrace_regs_get_stack_pointer(fregs)); 198 return true_parent_ip; 199 } 200 #else 201 static __always_inline unsigned long 202 function_get_true_parent_ip(unsigned long parent_ip, struct ftrace_regs *fregs) 203 { 204 return parent_ip; 205 } 206 #endif 207 208 static void 209 function_trace_call(unsigned long ip, unsigned long parent_ip, 210 struct ftrace_ops *op, struct ftrace_regs *fregs) 211 { 212 struct trace_array *tr = op->private; 213 unsigned int trace_ctx; 214 int bit; 215 216 if (unlikely(!tr->function_enabled)) 217 return; 218 219 bit = ftrace_test_recursion_trylock(ip, parent_ip); 220 if (bit < 0) 221 return; 222 223 parent_ip = function_get_true_parent_ip(parent_ip, fregs); 224 225 trace_ctx = tracing_gen_ctx_dec(); 226 227 trace_function(tr, ip, parent_ip, trace_ctx, NULL); 228 229 ftrace_test_recursion_unlock(bit); 230 } 231 232 static void 233 function_args_trace_call(unsigned long ip, unsigned long parent_ip, 234 struct ftrace_ops *op, struct ftrace_regs *fregs) 235 { 236 struct trace_array *tr = op->private; 237 unsigned int trace_ctx; 238 int bit; 239 240 if (unlikely(!tr->function_enabled)) 241 return; 242 243 bit = ftrace_test_recursion_trylock(ip, parent_ip); 244 if (bit < 0) 245 return; 246 247 trace_ctx = tracing_gen_ctx(); 248 249 trace_function(tr, ip, parent_ip, trace_ctx, fregs); 250 251 ftrace_test_recursion_unlock(bit); 252 } 253 254 #ifdef CONFIG_UNWINDER_ORC 255 /* 256 * Skip 2: 257 * 258 * function_stack_trace_call() 259 * ftrace_call() 260 */ 261 #define STACK_SKIP 2 262 #else 263 /* 264 * Skip 3: 265 * __trace_stack() 266 * function_stack_trace_call() 267 * ftrace_call() 268 */ 269 #define STACK_SKIP 3 270 #endif 271 272 static void 273 function_stack_trace_call(unsigned long ip, unsigned long parent_ip, 274 struct ftrace_ops *op, struct ftrace_regs *fregs) 275 { 276 struct trace_array *tr = op->private; 277 struct trace_array_cpu *data; 278 unsigned long flags; 279 long disabled; 280 int cpu; 281 unsigned int trace_ctx; 282 int skip = STACK_SKIP; 283 284 if (unlikely(!tr->function_enabled)) 285 return; 286 287 /* 288 * Need to use raw, since this must be called before the 289 * recursive protection is performed. 290 */ 291 local_irq_save(flags); 292 parent_ip = function_get_true_parent_ip(parent_ip, fregs); 293 cpu = raw_smp_processor_id(); 294 data = per_cpu_ptr(tr->array_buffer.data, cpu); 295 disabled = local_inc_return(&data->disabled); 296 297 if (likely(disabled == 1)) { 298 trace_ctx = tracing_gen_ctx_flags(flags); 299 if (!(tr->current_trace_flags->val & TRACE_FUNC_OPT_ARGS)) 300 fregs = NULL; 301 trace_function(tr, ip, parent_ip, trace_ctx, fregs); 302 #ifdef CONFIG_UNWINDER_FRAME_POINTER 303 if (ftrace_pids_enabled(op)) 304 skip++; 305 #endif 306 __trace_stack(tr, trace_ctx, skip); 307 } 308 309 local_dec(&data->disabled); 310 local_irq_restore(flags); 311 } 312 313 static inline bool is_repeat_check(struct trace_array *tr, 314 struct trace_func_repeats *last_info, 315 unsigned long ip, unsigned long parent_ip) 316 { 317 if (last_info->ip == ip && 318 last_info->parent_ip == parent_ip && 319 last_info->count < U16_MAX) { 320 last_info->ts_last_call = 321 ring_buffer_time_stamp(tr->array_buffer.buffer); 322 last_info->count++; 323 return true; 324 } 325 326 return false; 327 } 328 329 static inline void process_repeats(struct trace_array *tr, 330 unsigned long ip, unsigned long parent_ip, 331 struct trace_func_repeats *last_info, 332 unsigned int trace_ctx) 333 { 334 if (last_info->count) { 335 trace_last_func_repeats(tr, last_info, trace_ctx); 336 last_info->count = 0; 337 } 338 339 last_info->ip = ip; 340 last_info->parent_ip = parent_ip; 341 } 342 343 static void 344 function_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip, 345 struct ftrace_ops *op, 346 struct ftrace_regs *fregs) 347 { 348 struct trace_func_repeats *last_info; 349 struct trace_array *tr = op->private; 350 unsigned int trace_ctx; 351 int bit; 352 353 if (unlikely(!tr->function_enabled)) 354 return; 355 356 bit = ftrace_test_recursion_trylock(ip, parent_ip); 357 if (bit < 0) 358 return; 359 360 parent_ip = function_get_true_parent_ip(parent_ip, fregs); 361 if (!tracer_tracing_is_on(tr)) 362 goto out; 363 364 /* 365 * An interrupt may happen at any place here. But as far as I can see, 366 * the only damage that this can cause is to mess up the repetition 367 * counter without valuable data being lost. 368 * TODO: think about a solution that is better than just hoping to be 369 * lucky. 370 */ 371 last_info = this_cpu_ptr(tr->last_func_repeats); 372 if (is_repeat_check(tr, last_info, ip, parent_ip)) 373 goto out; 374 375 trace_ctx = tracing_gen_ctx_dec(); 376 process_repeats(tr, ip, parent_ip, last_info, trace_ctx); 377 378 trace_function(tr, ip, parent_ip, trace_ctx, NULL); 379 380 out: 381 ftrace_test_recursion_unlock(bit); 382 } 383 384 static void 385 function_stack_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip, 386 struct ftrace_ops *op, 387 struct ftrace_regs *fregs) 388 { 389 struct trace_func_repeats *last_info; 390 struct trace_array *tr = op->private; 391 struct trace_array_cpu *data; 392 unsigned long flags; 393 long disabled; 394 int cpu; 395 unsigned int trace_ctx; 396 397 if (unlikely(!tr->function_enabled)) 398 return; 399 400 /* 401 * Need to use raw, since this must be called before the 402 * recursive protection is performed. 403 */ 404 local_irq_save(flags); 405 parent_ip = function_get_true_parent_ip(parent_ip, fregs); 406 cpu = raw_smp_processor_id(); 407 data = per_cpu_ptr(tr->array_buffer.data, cpu); 408 disabled = local_inc_return(&data->disabled); 409 410 if (likely(disabled == 1)) { 411 last_info = per_cpu_ptr(tr->last_func_repeats, cpu); 412 if (is_repeat_check(tr, last_info, ip, parent_ip)) 413 goto out; 414 415 trace_ctx = tracing_gen_ctx_flags(flags); 416 process_repeats(tr, ip, parent_ip, last_info, trace_ctx); 417 418 trace_function(tr, ip, parent_ip, trace_ctx, NULL); 419 __trace_stack(tr, trace_ctx, STACK_SKIP); 420 } 421 422 out: 423 local_dec(&data->disabled); 424 local_irq_restore(flags); 425 } 426 427 static struct tracer_opt func_opts[] = { 428 #ifdef CONFIG_STACKTRACE 429 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) }, 430 #endif 431 { TRACER_OPT(func-no-repeats, TRACE_FUNC_OPT_NO_REPEATS) }, 432 #ifdef CONFIG_FUNCTION_TRACE_ARGS 433 { TRACER_OPT(func-args, TRACE_FUNC_OPT_ARGS) }, 434 #endif 435 { } /* Always set a last empty entry */ 436 }; 437 438 static struct tracer_flags func_flags = { 439 .val = TRACE_FUNC_NO_OPTS, /* By default: all flags disabled */ 440 .opts = func_opts 441 }; 442 443 static void tracing_start_function_trace(struct trace_array *tr) 444 { 445 tr->function_enabled = 0; 446 register_ftrace_function(tr->ops); 447 tr->function_enabled = 1; 448 } 449 450 static void tracing_stop_function_trace(struct trace_array *tr) 451 { 452 tr->function_enabled = 0; 453 unregister_ftrace_function(tr->ops); 454 } 455 456 static struct tracer function_trace; 457 458 static int 459 func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) 460 { 461 ftrace_func_t func; 462 u32 new_flags; 463 464 /* We can change this flag only when current tracer is function. */ 465 if (tr->current_trace != &function_trace) 466 return 0; 467 468 /* Do nothing if already set. */ 469 if (!!set == !!(tr->current_trace_flags->val & bit)) 470 return 0; 471 472 new_flags = (tr->current_trace_flags->val & ~bit) | (set ? bit : 0); 473 func = select_trace_function(new_flags); 474 if (!func) 475 return -EINVAL; 476 477 /* Check if there's anything to change. */ 478 if (tr->ops->func == func) 479 return 0; 480 481 if (!handle_func_repeats(tr, new_flags)) 482 return -ENOMEM; 483 484 unregister_ftrace_function(tr->ops); 485 tr->ops->func = func; 486 register_ftrace_function(tr->ops); 487 488 return 0; 489 } 490 491 static struct tracer function_trace __tracer_data = 492 { 493 .name = "function", 494 .init = function_trace_init, 495 .reset = function_trace_reset, 496 .start = function_trace_start, 497 .default_flags = &func_flags, 498 .set_flag = func_set_flag, 499 .allow_instances = true, 500 #ifdef CONFIG_FTRACE_SELFTEST 501 .selftest = trace_selftest_startup_function, 502 #endif 503 }; 504 505 #ifdef CONFIG_DYNAMIC_FTRACE 506 static void update_traceon_count(struct ftrace_probe_ops *ops, 507 unsigned long ip, 508 struct trace_array *tr, bool on, 509 void *data) 510 { 511 struct ftrace_func_mapper *mapper = data; 512 long *count; 513 long old_count; 514 515 /* 516 * Tracing gets disabled (or enabled) once per count. 517 * This function can be called at the same time on multiple CPUs. 518 * It is fine if both disable (or enable) tracing, as disabling 519 * (or enabling) the second time doesn't do anything as the 520 * state of the tracer is already disabled (or enabled). 521 * What needs to be synchronized in this case is that the count 522 * only gets decremented once, even if the tracer is disabled 523 * (or enabled) twice, as the second one is really a nop. 524 * 525 * The memory barriers guarantee that we only decrement the 526 * counter once. First the count is read to a local variable 527 * and a read barrier is used to make sure that it is loaded 528 * before checking if the tracer is in the state we want. 529 * If the tracer is not in the state we want, then the count 530 * is guaranteed to be the old count. 531 * 532 * Next the tracer is set to the state we want (disabled or enabled) 533 * then a write memory barrier is used to make sure that 534 * the new state is visible before changing the counter by 535 * one minus the old counter. This guarantees that another CPU 536 * executing this code will see the new state before seeing 537 * the new counter value, and would not do anything if the new 538 * counter is seen. 539 * 540 * Note, there is no synchronization between this and a user 541 * setting the tracing_on file. But we currently don't care 542 * about that. 543 */ 544 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 545 old_count = *count; 546 547 if (old_count <= 0) 548 return; 549 550 /* Make sure we see count before checking tracing state */ 551 smp_rmb(); 552 553 if (on == !!tracer_tracing_is_on(tr)) 554 return; 555 556 if (on) 557 tracer_tracing_on(tr); 558 else 559 tracer_tracing_off(tr); 560 561 /* Make sure tracing state is visible before updating count */ 562 smp_wmb(); 563 564 *count = old_count - 1; 565 } 566 567 static void 568 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip, 569 struct trace_array *tr, struct ftrace_probe_ops *ops, 570 void *data) 571 { 572 update_traceon_count(ops, ip, tr, 1, data); 573 } 574 575 static void 576 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip, 577 struct trace_array *tr, struct ftrace_probe_ops *ops, 578 void *data) 579 { 580 update_traceon_count(ops, ip, tr, 0, data); 581 } 582 583 static void 584 ftrace_traceon(unsigned long ip, unsigned long parent_ip, 585 struct trace_array *tr, struct ftrace_probe_ops *ops, 586 void *data) 587 { 588 if (tracer_tracing_is_on(tr)) 589 return; 590 591 tracer_tracing_on(tr); 592 } 593 594 static void 595 ftrace_traceoff(unsigned long ip, unsigned long parent_ip, 596 struct trace_array *tr, struct ftrace_probe_ops *ops, 597 void *data) 598 { 599 if (!tracer_tracing_is_on(tr)) 600 return; 601 602 tracer_tracing_off(tr); 603 } 604 605 #ifdef CONFIG_UNWINDER_ORC 606 /* 607 * Skip 3: 608 * 609 * function_trace_probe_call() 610 * ftrace_ops_assist_func() 611 * ftrace_call() 612 */ 613 #define FTRACE_STACK_SKIP 3 614 #else 615 /* 616 * Skip 5: 617 * 618 * __trace_stack() 619 * ftrace_stacktrace() 620 * function_trace_probe_call() 621 * ftrace_ops_assist_func() 622 * ftrace_call() 623 */ 624 #define FTRACE_STACK_SKIP 5 625 #endif 626 627 static __always_inline void trace_stack(struct trace_array *tr) 628 { 629 __trace_stack(tr, tracing_gen_ctx_dec(), FTRACE_STACK_SKIP); 630 } 631 632 static void 633 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip, 634 struct trace_array *tr, struct ftrace_probe_ops *ops, 635 void *data) 636 { 637 trace_stack(tr); 638 } 639 640 static void 641 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip, 642 struct trace_array *tr, struct ftrace_probe_ops *ops, 643 void *data) 644 { 645 struct ftrace_func_mapper *mapper = data; 646 long *count; 647 long old_count; 648 long new_count; 649 650 if (!tracing_is_on()) 651 return; 652 653 /* unlimited? */ 654 if (!mapper) { 655 trace_stack(tr); 656 return; 657 } 658 659 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 660 661 /* 662 * Stack traces should only execute the number of times the 663 * user specified in the counter. 664 */ 665 do { 666 old_count = *count; 667 668 if (!old_count) 669 return; 670 671 new_count = old_count - 1; 672 new_count = cmpxchg(count, old_count, new_count); 673 if (new_count == old_count) 674 trace_stack(tr); 675 676 if (!tracing_is_on()) 677 return; 678 679 } while (new_count != old_count); 680 } 681 682 static int update_count(struct ftrace_probe_ops *ops, unsigned long ip, 683 void *data) 684 { 685 struct ftrace_func_mapper *mapper = data; 686 long *count = NULL; 687 688 if (mapper) 689 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 690 691 if (count) { 692 if (*count <= 0) 693 return 0; 694 (*count)--; 695 } 696 697 return 1; 698 } 699 700 static void 701 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip, 702 struct trace_array *tr, struct ftrace_probe_ops *ops, 703 void *data) 704 { 705 if (update_count(ops, ip, data)) 706 ftrace_dump(DUMP_ALL); 707 } 708 709 /* Only dump the current CPU buffer. */ 710 static void 711 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip, 712 struct trace_array *tr, struct ftrace_probe_ops *ops, 713 void *data) 714 { 715 if (update_count(ops, ip, data)) 716 ftrace_dump(DUMP_ORIG); 717 } 718 719 static int 720 ftrace_probe_print(const char *name, struct seq_file *m, 721 unsigned long ip, struct ftrace_probe_ops *ops, 722 void *data) 723 { 724 struct ftrace_func_mapper *mapper = data; 725 long *count = NULL; 726 727 seq_printf(m, "%ps:%s", (void *)ip, name); 728 729 if (mapper) 730 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 731 732 if (count) 733 seq_printf(m, ":count=%ld\n", *count); 734 else 735 seq_puts(m, ":unlimited\n"); 736 737 return 0; 738 } 739 740 static int 741 ftrace_traceon_print(struct seq_file *m, unsigned long ip, 742 struct ftrace_probe_ops *ops, 743 void *data) 744 { 745 return ftrace_probe_print("traceon", m, ip, ops, data); 746 } 747 748 static int 749 ftrace_traceoff_print(struct seq_file *m, unsigned long ip, 750 struct ftrace_probe_ops *ops, void *data) 751 { 752 return ftrace_probe_print("traceoff", m, ip, ops, data); 753 } 754 755 static int 756 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip, 757 struct ftrace_probe_ops *ops, void *data) 758 { 759 return ftrace_probe_print("stacktrace", m, ip, ops, data); 760 } 761 762 static int 763 ftrace_dump_print(struct seq_file *m, unsigned long ip, 764 struct ftrace_probe_ops *ops, void *data) 765 { 766 return ftrace_probe_print("dump", m, ip, ops, data); 767 } 768 769 static int 770 ftrace_cpudump_print(struct seq_file *m, unsigned long ip, 771 struct ftrace_probe_ops *ops, void *data) 772 { 773 return ftrace_probe_print("cpudump", m, ip, ops, data); 774 } 775 776 777 static int 778 ftrace_count_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 779 unsigned long ip, void *init_data, void **data) 780 { 781 struct ftrace_func_mapper *mapper = *data; 782 783 if (!mapper) { 784 mapper = allocate_ftrace_func_mapper(); 785 if (!mapper) 786 return -ENOMEM; 787 *data = mapper; 788 } 789 790 return ftrace_func_mapper_add_ip(mapper, ip, init_data); 791 } 792 793 static void 794 ftrace_count_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 795 unsigned long ip, void *data) 796 { 797 struct ftrace_func_mapper *mapper = data; 798 799 if (!ip) { 800 free_ftrace_func_mapper(mapper, NULL); 801 return; 802 } 803 804 ftrace_func_mapper_remove_ip(mapper, ip); 805 } 806 807 static struct ftrace_probe_ops traceon_count_probe_ops = { 808 .func = ftrace_traceon_count, 809 .print = ftrace_traceon_print, 810 .init = ftrace_count_init, 811 .free = ftrace_count_free, 812 }; 813 814 static struct ftrace_probe_ops traceoff_count_probe_ops = { 815 .func = ftrace_traceoff_count, 816 .print = ftrace_traceoff_print, 817 .init = ftrace_count_init, 818 .free = ftrace_count_free, 819 }; 820 821 static struct ftrace_probe_ops stacktrace_count_probe_ops = { 822 .func = ftrace_stacktrace_count, 823 .print = ftrace_stacktrace_print, 824 .init = ftrace_count_init, 825 .free = ftrace_count_free, 826 }; 827 828 static struct ftrace_probe_ops dump_probe_ops = { 829 .func = ftrace_dump_probe, 830 .print = ftrace_dump_print, 831 .init = ftrace_count_init, 832 .free = ftrace_count_free, 833 }; 834 835 static struct ftrace_probe_ops cpudump_probe_ops = { 836 .func = ftrace_cpudump_probe, 837 .print = ftrace_cpudump_print, 838 }; 839 840 static struct ftrace_probe_ops traceon_probe_ops = { 841 .func = ftrace_traceon, 842 .print = ftrace_traceon_print, 843 }; 844 845 static struct ftrace_probe_ops traceoff_probe_ops = { 846 .func = ftrace_traceoff, 847 .print = ftrace_traceoff_print, 848 }; 849 850 static struct ftrace_probe_ops stacktrace_probe_ops = { 851 .func = ftrace_stacktrace, 852 .print = ftrace_stacktrace_print, 853 }; 854 855 static int 856 ftrace_trace_probe_callback(struct trace_array *tr, 857 struct ftrace_probe_ops *ops, 858 struct ftrace_hash *hash, char *glob, 859 char *cmd, char *param, int enable) 860 { 861 void *count = (void *)-1; 862 char *number; 863 int ret; 864 865 /* hash funcs only work with set_ftrace_filter */ 866 if (!enable) 867 return -EINVAL; 868 869 if (glob[0] == '!') 870 return unregister_ftrace_function_probe_func(glob+1, tr, ops); 871 872 if (!param) 873 goto out_reg; 874 875 number = strsep(¶m, ":"); 876 877 if (!strlen(number)) 878 goto out_reg; 879 880 /* 881 * We use the callback data field (which is a pointer) 882 * as our counter. 883 */ 884 ret = kstrtoul(number, 0, (unsigned long *)&count); 885 if (ret) 886 return ret; 887 888 out_reg: 889 ret = register_ftrace_function_probe(glob, tr, ops, count); 890 891 return ret < 0 ? ret : 0; 892 } 893 894 static int 895 ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash, 896 char *glob, char *cmd, char *param, int enable) 897 { 898 struct ftrace_probe_ops *ops; 899 900 if (!tr) 901 return -ENODEV; 902 903 /* we register both traceon and traceoff to this callback */ 904 if (strcmp(cmd, "traceon") == 0) 905 ops = param ? &traceon_count_probe_ops : &traceon_probe_ops; 906 else 907 ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops; 908 909 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, 910 param, enable); 911 } 912 913 static int 914 ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash, 915 char *glob, char *cmd, char *param, int enable) 916 { 917 struct ftrace_probe_ops *ops; 918 919 if (!tr) 920 return -ENODEV; 921 922 ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops; 923 924 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, 925 param, enable); 926 } 927 928 static int 929 ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash, 930 char *glob, char *cmd, char *param, int enable) 931 { 932 struct ftrace_probe_ops *ops; 933 934 if (!tr) 935 return -ENODEV; 936 937 ops = &dump_probe_ops; 938 939 /* Only dump once. */ 940 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, 941 "1", enable); 942 } 943 944 static int 945 ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash, 946 char *glob, char *cmd, char *param, int enable) 947 { 948 struct ftrace_probe_ops *ops; 949 950 if (!tr) 951 return -ENODEV; 952 953 ops = &cpudump_probe_ops; 954 955 /* Only dump once. */ 956 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd, 957 "1", enable); 958 } 959 960 static struct ftrace_func_command ftrace_traceon_cmd = { 961 .name = "traceon", 962 .func = ftrace_trace_onoff_callback, 963 }; 964 965 static struct ftrace_func_command ftrace_traceoff_cmd = { 966 .name = "traceoff", 967 .func = ftrace_trace_onoff_callback, 968 }; 969 970 static struct ftrace_func_command ftrace_stacktrace_cmd = { 971 .name = "stacktrace", 972 .func = ftrace_stacktrace_callback, 973 }; 974 975 static struct ftrace_func_command ftrace_dump_cmd = { 976 .name = "dump", 977 .func = ftrace_dump_callback, 978 }; 979 980 static struct ftrace_func_command ftrace_cpudump_cmd = { 981 .name = "cpudump", 982 .func = ftrace_cpudump_callback, 983 }; 984 985 static int __init init_func_cmd_traceon(void) 986 { 987 int ret; 988 989 ret = register_ftrace_command(&ftrace_traceoff_cmd); 990 if (ret) 991 return ret; 992 993 ret = register_ftrace_command(&ftrace_traceon_cmd); 994 if (ret) 995 goto out_free_traceoff; 996 997 ret = register_ftrace_command(&ftrace_stacktrace_cmd); 998 if (ret) 999 goto out_free_traceon; 1000 1001 ret = register_ftrace_command(&ftrace_dump_cmd); 1002 if (ret) 1003 goto out_free_stacktrace; 1004 1005 ret = register_ftrace_command(&ftrace_cpudump_cmd); 1006 if (ret) 1007 goto out_free_dump; 1008 1009 return 0; 1010 1011 out_free_dump: 1012 unregister_ftrace_command(&ftrace_dump_cmd); 1013 out_free_stacktrace: 1014 unregister_ftrace_command(&ftrace_stacktrace_cmd); 1015 out_free_traceon: 1016 unregister_ftrace_command(&ftrace_traceon_cmd); 1017 out_free_traceoff: 1018 unregister_ftrace_command(&ftrace_traceoff_cmd); 1019 1020 return ret; 1021 } 1022 #else 1023 static inline int init_func_cmd_traceon(void) 1024 { 1025 return 0; 1026 } 1027 #endif /* CONFIG_DYNAMIC_FTRACE */ 1028 1029 __init int init_function_trace(void) 1030 { 1031 init_func_cmd_traceon(); 1032 return register_tracer(&function_trace); 1033 } 1034