Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'trace-3.14' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace

Pull tracing updates from Steven Rostedt:
"This pull request has a new feature to ftrace, namely the trace event
triggers by Tom Zanussi. A trigger is a way to enable an action when
an event is hit. The actions are:

o trace on/off - enable or disable tracing
o snapshot - save the current trace buffer in the snapshot
o stacktrace - dump the current stack trace to the ringbuffer
o enable/disable events - enable or disable another event

Namhyung Kim added updates to the tracing uprobes code. Having the
uprobes add support for fetch methods.

The rest are various bug fixes with the new code, and minor ones for
the old code"

* tag 'trace-3.14' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace: (38 commits)
tracing: Fix buggered tee(2) on tracing_pipe
tracing: Have trace buffer point back to trace_array
ftrace: Fix synchronization location disabling and freeing ftrace_ops
ftrace: Have function graph only trace based on global_ops filters
ftrace: Synchronize setting function_trace_op with ftrace_trace_function
tracing: Show available event triggers when no trigger is set
tracing: Consolidate event trigger code
tracing: Fix counter for traceon/off event triggers
tracing: Remove double-underscore naming in syscall trigger invocations
tracing/kprobes: Add trace event trigger invocations
tracing/probes: Fix build break on !CONFIG_KPROBE_EVENT
tracing/uprobes: Add @+file_offset fetch method
uprobes: Allocate ->utask before handler_chain() for tracing handlers
tracing/uprobes: Add support for full argument access methods
tracing/uprobes: Fetch args before reserving a ring buffer
tracing/uprobes: Pass 'is_return' to traceprobe_parse_probe_arg()
tracing/probes: Implement 'memory' fetch method for uprobes
tracing/probes: Add fetch{,_size} member into deref fetch method
tracing/probes: Move 'symbol' fetch method to kprobes
tracing/probes: Implement 'stack' fetch method for uprobes
...

+3464 -924
+207
Documentation/trace/events.txt
··· 287 287 prev_pid == 0 288 288 # cat sched_wakeup/filter 289 289 common_pid == 0 290 + 291 + 6. Event triggers 292 + ================= 293 + 294 + Trace events can be made to conditionally invoke trigger 'commands' 295 + which can take various forms and are described in detail below; 296 + examples would be enabling or disabling other trace events or invoking 297 + a stack trace whenever the trace event is hit. Whenever a trace event 298 + with attached triggers is invoked, the set of trigger commands 299 + associated with that event is invoked. Any given trigger can 300 + additionally have an event filter of the same form as described in 301 + section 5 (Event filtering) associated with it - the command will only 302 + be invoked if the event being invoked passes the associated filter. 303 + If no filter is associated with the trigger, it always passes. 304 + 305 + Triggers are added to and removed from a particular event by writing 306 + trigger expressions to the 'trigger' file for the given event. 307 + 308 + A given event can have any number of triggers associated with it, 309 + subject to any restrictions that individual commands may have in that 310 + regard. 311 + 312 + Event triggers are implemented on top of "soft" mode, which means that 313 + whenever a trace event has one or more triggers associated with it, 314 + the event is activated even if it isn't actually enabled, but is 315 + disabled in a "soft" mode. That is, the tracepoint will be called, 316 + but just will not be traced, unless of course it's actually enabled. 317 + This scheme allows triggers to be invoked even for events that aren't 318 + enabled, and also allows the current event filter implementation to be 319 + used for conditionally invoking triggers. 320 + 321 + The syntax for event triggers is roughly based on the syntax for 322 + set_ftrace_filter 'ftrace filter commands' (see the 'Filter commands' 323 + section of Documentation/trace/ftrace.txt), but there are major 324 + differences and the implementation isn't currently tied to it in any 325 + way, so beware about making generalizations between the two. 326 + 327 + 6.1 Expression syntax 328 + --------------------- 329 + 330 + Triggers are added by echoing the command to the 'trigger' file: 331 + 332 + # echo 'command[:count] [if filter]' > trigger 333 + 334 + Triggers are removed by echoing the same command but starting with '!' 335 + to the 'trigger' file: 336 + 337 + # echo '!command[:count] [if filter]' > trigger 338 + 339 + The [if filter] part isn't used in matching commands when removing, so 340 + leaving that off in a '!' command will accomplish the same thing as 341 + having it in. 342 + 343 + The filter syntax is the same as that described in the 'Event 344 + filtering' section above. 345 + 346 + For ease of use, writing to the trigger file using '>' currently just 347 + adds or removes a single trigger and there's no explicit '>>' support 348 + ('>' actually behaves like '>>') or truncation support to remove all 349 + triggers (you have to use '!' for each one added.) 350 + 351 + 6.2 Supported trigger commands 352 + ------------------------------ 353 + 354 + The following commands are supported: 355 + 356 + - enable_event/disable_event 357 + 358 + These commands can enable or disable another trace event whenever 359 + the triggering event is hit. When these commands are registered, 360 + the other trace event is activated, but disabled in a "soft" mode. 361 + That is, the tracepoint will be called, but just will not be traced. 362 + The event tracepoint stays in this mode as long as there's a trigger 363 + in effect that can trigger it. 364 + 365 + For example, the following trigger causes kmalloc events to be 366 + traced when a read system call is entered, and the :1 at the end 367 + specifies that this enablement happens only once: 368 + 369 + # echo 'enable_event:kmem:kmalloc:1' > \ 370 + /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger 371 + 372 + The following trigger causes kmalloc events to stop being traced 373 + when a read system call exits. This disablement happens on every 374 + read system call exit: 375 + 376 + # echo 'disable_event:kmem:kmalloc' > \ 377 + /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger 378 + 379 + The format is: 380 + 381 + enable_event:<system>:<event>[:count] 382 + disable_event:<system>:<event>[:count] 383 + 384 + To remove the above commands: 385 + 386 + # echo '!enable_event:kmem:kmalloc:1' > \ 387 + /sys/kernel/debug/tracing/events/syscalls/sys_enter_read/trigger 388 + 389 + # echo '!disable_event:kmem:kmalloc' > \ 390 + /sys/kernel/debug/tracing/events/syscalls/sys_exit_read/trigger 391 + 392 + Note that there can be any number of enable/disable_event triggers 393 + per triggering event, but there can only be one trigger per 394 + triggered event. e.g. sys_enter_read can have triggers enabling both 395 + kmem:kmalloc and sched:sched_switch, but can't have two kmem:kmalloc 396 + versions such as kmem:kmalloc and kmem:kmalloc:1 or 'kmem:kmalloc if 397 + bytes_req == 256' and 'kmem:kmalloc if bytes_alloc == 256' (they 398 + could be combined into a single filter on kmem:kmalloc though). 399 + 400 + - stacktrace 401 + 402 + This command dumps a stacktrace in the trace buffer whenever the 403 + triggering event occurs. 404 + 405 + For example, the following trigger dumps a stacktrace every time the 406 + kmalloc tracepoint is hit: 407 + 408 + # echo 'stacktrace' > \ 409 + /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger 410 + 411 + The following trigger dumps a stacktrace the first 5 times a kmalloc 412 + request happens with a size >= 64K 413 + 414 + # echo 'stacktrace:5 if bytes_req >= 65536' > \ 415 + /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger 416 + 417 + The format is: 418 + 419 + stacktrace[:count] 420 + 421 + To remove the above commands: 422 + 423 + # echo '!stacktrace' > \ 424 + /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger 425 + 426 + # echo '!stacktrace:5 if bytes_req >= 65536' > \ 427 + /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger 428 + 429 + The latter can also be removed more simply by the following (without 430 + the filter): 431 + 432 + # echo '!stacktrace:5' > \ 433 + /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger 434 + 435 + Note that there can be only one stacktrace trigger per triggering 436 + event. 437 + 438 + - snapshot 439 + 440 + This command causes a snapshot to be triggered whenever the 441 + triggering event occurs. 442 + 443 + The following command creates a snapshot every time a block request 444 + queue is unplugged with a depth > 1. If you were tracing a set of 445 + events or functions at the time, the snapshot trace buffer would 446 + capture those events when the trigger event occured: 447 + 448 + # echo 'snapshot if nr_rq > 1' > \ 449 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 450 + 451 + To only snapshot once: 452 + 453 + # echo 'snapshot:1 if nr_rq > 1' > \ 454 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 455 + 456 + To remove the above commands: 457 + 458 + # echo '!snapshot if nr_rq > 1' > \ 459 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 460 + 461 + # echo '!snapshot:1 if nr_rq > 1' > \ 462 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 463 + 464 + Note that there can be only one snapshot trigger per triggering 465 + event. 466 + 467 + - traceon/traceoff 468 + 469 + These commands turn tracing on and off when the specified events are 470 + hit. The parameter determines how many times the tracing system is 471 + turned on and off. If unspecified, there is no limit. 472 + 473 + The following command turns tracing off the first time a block 474 + request queue is unplugged with a depth > 1. If you were tracing a 475 + set of events or functions at the time, you could then examine the 476 + trace buffer to see the sequence of events that led up to the 477 + trigger event: 478 + 479 + # echo 'traceoff:1 if nr_rq > 1' > \ 480 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 481 + 482 + To always disable tracing when nr_rq > 1 : 483 + 484 + # echo 'traceoff if nr_rq > 1' > \ 485 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 486 + 487 + To remove the above commands: 488 + 489 + # echo '!traceoff:1 if nr_rq > 1' > \ 490 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 491 + 492 + # echo '!traceoff if nr_rq > 1' > \ 493 + /sys/kernel/debug/tracing/events/block/block_unplug/trigger 494 + 495 + Note that there can be only one traceon or traceoff trigger per 496 + triggering event.
+31 -5
Documentation/trace/uprobetracer.txt
··· 19 19 20 20 Synopsis of uprobe_tracer 21 21 ------------------------- 22 - p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS] : Set a uprobe 23 - r[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS] : Set a return uprobe (uretprobe) 24 - -:[GRP/]EVENT : Clear uprobe or uretprobe event 22 + p[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] : Set a uprobe 23 + r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] : Set a return uprobe (uretprobe) 24 + -:[GRP/]EVENT : Clear uprobe or uretprobe event 25 25 26 26 GRP : Group name. If omitted, "uprobes" is the default value. 27 27 EVENT : Event name. If omitted, the event name is generated based 28 - on SYMBOL+offs. 28 + on PATH+OFFSET. 29 29 PATH : Path to an executable or a library. 30 - SYMBOL[+offs] : Symbol+offset where the probe is inserted. 30 + OFFSET : Offset where the probe is inserted. 31 31 32 32 FETCHARGS : Arguments. Each probe can have up to 128 args. 33 33 %REG : Fetch register REG 34 + @ADDR : Fetch memory at ADDR (ADDR should be in userspace) 35 + @+OFFSET : Fetch memory at OFFSET (OFFSET from same file as PATH) 36 + $stackN : Fetch Nth entry of stack (N >= 0) 37 + $stack : Fetch stack address. 38 + $retval : Fetch return value.(*) 39 + +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**) 40 + NAME=FETCHARG : Set NAME as the argument name of FETCHARG. 41 + FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types 42 + (u8/u16/u32/u64/s8/s16/s32/s64), "string" and bitfield 43 + are supported. 44 + 45 + (*) only for return probe. 46 + (**) this is useful for fetching a field of data structures. 47 + 48 + Types 49 + ----- 50 + Several types are supported for fetch-args. Uprobe tracer will access memory 51 + by given type. Prefix 's' and 'u' means those types are signed and unsigned 52 + respectively. Traced arguments are shown in decimal (signed) or hex (unsigned). 53 + String type is a special type, which fetches a "null-terminated" string from 54 + user space. 55 + Bitfield is another special type, which takes 3 parameters, bit-width, bit- 56 + offset, and container-size (usually 32). The syntax is; 57 + 58 + b<bit-width>@<bit-offset>/<container-size> 59 + 34 60 35 61 Event Profiling 36 62 ---------------
-2
include/linux/ftrace.h
··· 570 570 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; } 571 571 #endif /* CONFIG_DYNAMIC_FTRACE */ 572 572 573 - loff_t ftrace_filter_lseek(struct file *file, loff_t offset, int whence); 574 - 575 573 /* totally disable ftrace - can not re-enable after this */ 576 574 void ftrace_kill(void); 577 575
+139
include/linux/ftrace_event.h
··· 1 + 1 2 #ifndef _LINUX_FTRACE_EVENT_H 2 3 #define _LINUX_FTRACE_EVENT_H 3 4 ··· 265 264 FTRACE_EVENT_FL_NO_SET_FILTER_BIT, 266 265 FTRACE_EVENT_FL_SOFT_MODE_BIT, 267 266 FTRACE_EVENT_FL_SOFT_DISABLED_BIT, 267 + FTRACE_EVENT_FL_TRIGGER_MODE_BIT, 268 + FTRACE_EVENT_FL_TRIGGER_COND_BIT, 268 269 }; 269 270 270 271 /* ··· 278 275 * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED 279 276 * SOFT_DISABLED - When set, do not trace the event (even though its 280 277 * tracepoint may be enabled) 278 + * TRIGGER_MODE - When set, invoke the triggers associated with the event 279 + * TRIGGER_COND - When set, one or more triggers has an associated filter 281 280 */ 282 281 enum { 283 282 FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT), ··· 288 283 FTRACE_EVENT_FL_NO_SET_FILTER = (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT), 289 284 FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT), 290 285 FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT), 286 + FTRACE_EVENT_FL_TRIGGER_MODE = (1 << FTRACE_EVENT_FL_TRIGGER_MODE_BIT), 287 + FTRACE_EVENT_FL_TRIGGER_COND = (1 << FTRACE_EVENT_FL_TRIGGER_COND_BIT), 291 288 }; 292 289 293 290 struct ftrace_event_file { ··· 299 292 struct dentry *dir; 300 293 struct trace_array *tr; 301 294 struct ftrace_subsystem_dir *system; 295 + struct list_head triggers; 302 296 303 297 /* 304 298 * 32 bit flags: ··· 307 299 * bit 1: enabled cmd record 308 300 * bit 2: enable/disable with the soft disable bit 309 301 * bit 3: soft disabled 302 + * bit 4: trigger enabled 310 303 * 311 304 * Note: The bits must be set atomically to prevent races 312 305 * from other writers. Reads of flags do not need to be in ··· 319 310 */ 320 311 unsigned long flags; 321 312 atomic_t sm_ref; /* soft-mode reference counter */ 313 + atomic_t tm_ref; /* trigger-mode reference counter */ 322 314 }; 323 315 324 316 #define __TRACE_EVENT_FLAGS(name, value) \ ··· 347 337 348 338 #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */ 349 339 340 + enum event_trigger_type { 341 + ETT_NONE = (0), 342 + ETT_TRACE_ONOFF = (1 << 0), 343 + ETT_SNAPSHOT = (1 << 1), 344 + ETT_STACKTRACE = (1 << 2), 345 + ETT_EVENT_ENABLE = (1 << 3), 346 + }; 347 + 350 348 extern void destroy_preds(struct ftrace_event_file *file); 351 349 extern void destroy_call_preds(struct ftrace_event_call *call); 352 350 extern int filter_match_preds(struct event_filter *filter, void *rec); ··· 365 347 extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec, 366 348 struct ring_buffer *buffer, 367 349 struct ring_buffer_event *event); 350 + extern enum event_trigger_type event_triggers_call(struct ftrace_event_file *file, 351 + void *rec); 352 + extern void event_triggers_post_call(struct ftrace_event_file *file, 353 + enum event_trigger_type tt); 354 + 355 + /** 356 + * ftrace_trigger_soft_disabled - do triggers and test if soft disabled 357 + * @file: The file pointer of the event to test 358 + * 359 + * If any triggers without filters are attached to this event, they 360 + * will be called here. If the event is soft disabled and has no 361 + * triggers that require testing the fields, it will return true, 362 + * otherwise false. 363 + */ 364 + static inline bool 365 + ftrace_trigger_soft_disabled(struct ftrace_event_file *file) 366 + { 367 + unsigned long eflags = file->flags; 368 + 369 + if (!(eflags & FTRACE_EVENT_FL_TRIGGER_COND)) { 370 + if (eflags & FTRACE_EVENT_FL_TRIGGER_MODE) 371 + event_triggers_call(file, NULL); 372 + if (eflags & FTRACE_EVENT_FL_SOFT_DISABLED) 373 + return true; 374 + } 375 + return false; 376 + } 377 + 378 + /* 379 + * Helper function for event_trigger_unlock_commit{_regs}(). 380 + * If there are event triggers attached to this event that requires 381 + * filtering against its fields, then they wil be called as the 382 + * entry already holds the field information of the current event. 383 + * 384 + * It also checks if the event should be discarded or not. 385 + * It is to be discarded if the event is soft disabled and the 386 + * event was only recorded to process triggers, or if the event 387 + * filter is active and this event did not match the filters. 388 + * 389 + * Returns true if the event is discarded, false otherwise. 390 + */ 391 + static inline bool 392 + __event_trigger_test_discard(struct ftrace_event_file *file, 393 + struct ring_buffer *buffer, 394 + struct ring_buffer_event *event, 395 + void *entry, 396 + enum event_trigger_type *tt) 397 + { 398 + unsigned long eflags = file->flags; 399 + 400 + if (eflags & FTRACE_EVENT_FL_TRIGGER_COND) 401 + *tt = event_triggers_call(file, entry); 402 + 403 + if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags)) 404 + ring_buffer_discard_commit(buffer, event); 405 + else if (!filter_check_discard(file, entry, buffer, event)) 406 + return false; 407 + 408 + return true; 409 + } 410 + 411 + /** 412 + * event_trigger_unlock_commit - handle triggers and finish event commit 413 + * @file: The file pointer assoctiated to the event 414 + * @buffer: The ring buffer that the event is being written to 415 + * @event: The event meta data in the ring buffer 416 + * @entry: The event itself 417 + * @irq_flags: The state of the interrupts at the start of the event 418 + * @pc: The state of the preempt count at the start of the event. 419 + * 420 + * This is a helper function to handle triggers that require data 421 + * from the event itself. It also tests the event against filters and 422 + * if the event is soft disabled and should be discarded. 423 + */ 424 + static inline void 425 + event_trigger_unlock_commit(struct ftrace_event_file *file, 426 + struct ring_buffer *buffer, 427 + struct ring_buffer_event *event, 428 + void *entry, unsigned long irq_flags, int pc) 429 + { 430 + enum event_trigger_type tt = ETT_NONE; 431 + 432 + if (!__event_trigger_test_discard(file, buffer, event, entry, &tt)) 433 + trace_buffer_unlock_commit(buffer, event, irq_flags, pc); 434 + 435 + if (tt) 436 + event_triggers_post_call(file, tt); 437 + } 438 + 439 + /** 440 + * event_trigger_unlock_commit_regs - handle triggers and finish event commit 441 + * @file: The file pointer assoctiated to the event 442 + * @buffer: The ring buffer that the event is being written to 443 + * @event: The event meta data in the ring buffer 444 + * @entry: The event itself 445 + * @irq_flags: The state of the interrupts at the start of the event 446 + * @pc: The state of the preempt count at the start of the event. 447 + * 448 + * This is a helper function to handle triggers that require data 449 + * from the event itself. It also tests the event against filters and 450 + * if the event is soft disabled and should be discarded. 451 + * 452 + * Same as event_trigger_unlock_commit() but calls 453 + * trace_buffer_unlock_commit_regs() instead of trace_buffer_unlock_commit(). 454 + */ 455 + static inline void 456 + event_trigger_unlock_commit_regs(struct ftrace_event_file *file, 457 + struct ring_buffer *buffer, 458 + struct ring_buffer_event *event, 459 + void *entry, unsigned long irq_flags, int pc, 460 + struct pt_regs *regs) 461 + { 462 + enum event_trigger_type tt = ETT_NONE; 463 + 464 + if (!__event_trigger_test_discard(file, buffer, event, entry, &tt)) 465 + trace_buffer_unlock_commit_regs(buffer, event, 466 + irq_flags, pc, regs); 467 + 468 + if (tt) 469 + event_triggers_post_call(file, tt); 470 + } 368 471 369 472 enum { 370 473 FILTER_OTHER = 0,
+21 -8
include/trace/ftrace.h
··· 418 418 * struct ftrace_event_file *ftrace_file = __data; 419 419 * struct ftrace_event_call *event_call = ftrace_file->event_call; 420 420 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets; 421 + * unsigned long eflags = ftrace_file->flags; 422 + * enum event_trigger_type __tt = ETT_NONE; 421 423 * struct ring_buffer_event *event; 422 424 * struct ftrace_raw_<call> *entry; <-- defined in stage 1 423 425 * struct ring_buffer *buffer; ··· 427 425 * int __data_size; 428 426 * int pc; 429 427 * 430 - * if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, 431 - * &ftrace_file->flags)) 432 - * return; 428 + * if (!(eflags & FTRACE_EVENT_FL_TRIGGER_COND)) { 429 + * if (eflags & FTRACE_EVENT_FL_TRIGGER_MODE) 430 + * event_triggers_call(ftrace_file, NULL); 431 + * if (eflags & FTRACE_EVENT_FL_SOFT_DISABLED) 432 + * return; 433 + * } 433 434 * 434 435 * local_save_flags(irq_flags); 435 436 * pc = preempt_count(); ··· 450 445 * { <assign>; } <-- Here we assign the entries by the __field and 451 446 * __array macros. 452 447 * 453 - * if (!filter_check_discard(ftrace_file, entry, buffer, event)) 448 + * if (eflags & FTRACE_EVENT_FL_TRIGGER_COND) 449 + * __tt = event_triggers_call(ftrace_file, entry); 450 + * 451 + * if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, 452 + * &ftrace_file->flags)) 453 + * ring_buffer_discard_commit(buffer, event); 454 + * else if (!filter_check_discard(ftrace_file, entry, buffer, event)) 454 455 * trace_buffer_unlock_commit(buffer, event, irq_flags, pc); 456 + * 457 + * if (__tt) 458 + * event_triggers_post_call(ftrace_file, __tt); 455 459 * } 456 460 * 457 461 * static struct trace_event ftrace_event_type_<call> = { ··· 553 539 int __data_size; \ 554 540 int pc; \ 555 541 \ 556 - if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, \ 557 - &ftrace_file->flags)) \ 542 + if (ftrace_trigger_soft_disabled(ftrace_file)) \ 558 543 return; \ 559 544 \ 560 545 local_save_flags(irq_flags); \ ··· 573 560 \ 574 561 { assign; } \ 575 562 \ 576 - if (!filter_check_discard(ftrace_file, entry, buffer, event)) \ 577 - trace_buffer_unlock_commit(buffer, event, irq_flags, pc); \ 563 + event_trigger_unlock_commit(ftrace_file, buffer, event, entry, \ 564 + irq_flags, pc); \ 578 565 } 579 566 /* 580 567 * The ftrace_test_probe is compiled out, it is only here as a build time check
+4
kernel/events/uprobes.c
··· 1854 1854 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags))) 1855 1855 goto out; 1856 1856 1857 + /* Tracing handlers use ->utask to communicate with fetch methods */ 1858 + if (!get_utask()) 1859 + goto out; 1860 + 1857 1861 handler_chain(uprobe, regs); 1858 1862 if (can_skip_sstep(uprobe, regs)) 1859 1863 goto out;
+1
kernel/trace/Makefile
··· 50 50 obj-$(CONFIG_EVENT_TRACING) += trace_event_perf.o 51 51 endif 52 52 obj-$(CONFIG_EVENT_TRACING) += trace_events_filter.o 53 + obj-$(CONFIG_EVENT_TRACING) += trace_events_trigger.o 53 54 obj-$(CONFIG_KPROBE_EVENT) += trace_kprobe.o 54 55 obj-$(CONFIG_TRACEPOINTS) += power-traces.o 55 56 ifeq ($(CONFIG_PM_RUNTIME),y)
+154 -61
kernel/trace/ftrace.c
··· 85 85 86 86 /* Current function tracing op */ 87 87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; 88 + /* What to set function_trace_op to */ 89 + static struct ftrace_ops *set_function_trace_op; 88 90 89 91 /* List for set_ftrace_pid's pids. */ 90 92 LIST_HEAD(ftrace_pids); ··· 280 278 global_ops.func = func; 281 279 } 282 280 281 + static void ftrace_sync(struct work_struct *work) 282 + { 283 + /* 284 + * This function is just a stub to implement a hard force 285 + * of synchronize_sched(). This requires synchronizing 286 + * tasks even in userspace and idle. 287 + * 288 + * Yes, function tracing is rude. 289 + */ 290 + } 291 + 292 + static void ftrace_sync_ipi(void *data) 293 + { 294 + /* Probably not needed, but do it anyway */ 295 + smp_rmb(); 296 + } 297 + 298 + #ifdef CONFIG_FUNCTION_GRAPH_TRACER 299 + static void update_function_graph_func(void); 300 + #else 301 + static inline void update_function_graph_func(void) { } 302 + #endif 303 + 283 304 static void update_ftrace_function(void) 284 305 { 285 306 ftrace_func_t func; ··· 321 296 !FTRACE_FORCE_LIST_FUNC)) { 322 297 /* Set the ftrace_ops that the arch callback uses */ 323 298 if (ftrace_ops_list == &global_ops) 324 - function_trace_op = ftrace_global_list; 299 + set_function_trace_op = ftrace_global_list; 325 300 else 326 - function_trace_op = ftrace_ops_list; 301 + set_function_trace_op = ftrace_ops_list; 327 302 func = ftrace_ops_list->func; 328 303 } else { 329 304 /* Just use the default ftrace_ops */ 330 - function_trace_op = &ftrace_list_end; 305 + set_function_trace_op = &ftrace_list_end; 331 306 func = ftrace_ops_list_func; 332 307 } 308 + 309 + /* If there's no change, then do nothing more here */ 310 + if (ftrace_trace_function == func) 311 + return; 312 + 313 + update_function_graph_func(); 314 + 315 + /* 316 + * If we are using the list function, it doesn't care 317 + * about the function_trace_ops. 318 + */ 319 + if (func == ftrace_ops_list_func) { 320 + ftrace_trace_function = func; 321 + /* 322 + * Don't even bother setting function_trace_ops, 323 + * it would be racy to do so anyway. 324 + */ 325 + return; 326 + } 327 + 328 + #ifndef CONFIG_DYNAMIC_FTRACE 329 + /* 330 + * For static tracing, we need to be a bit more careful. 331 + * The function change takes affect immediately. Thus, 332 + * we need to coorditate the setting of the function_trace_ops 333 + * with the setting of the ftrace_trace_function. 334 + * 335 + * Set the function to the list ops, which will call the 336 + * function we want, albeit indirectly, but it handles the 337 + * ftrace_ops and doesn't depend on function_trace_op. 338 + */ 339 + ftrace_trace_function = ftrace_ops_list_func; 340 + /* 341 + * Make sure all CPUs see this. Yes this is slow, but static 342 + * tracing is slow and nasty to have enabled. 343 + */ 344 + schedule_on_each_cpu(ftrace_sync); 345 + /* Now all cpus are using the list ops. */ 346 + function_trace_op = set_function_trace_op; 347 + /* Make sure the function_trace_op is visible on all CPUs */ 348 + smp_wmb(); 349 + /* Nasty way to force a rmb on all cpus */ 350 + smp_call_function(ftrace_sync_ipi, NULL, 1); 351 + /* OK, we are all set to update the ftrace_trace_function now! */ 352 + #endif /* !CONFIG_DYNAMIC_FTRACE */ 333 353 334 354 ftrace_trace_function = func; 335 355 } ··· 480 410 return 0; 481 411 } 482 412 483 - static void ftrace_sync(struct work_struct *work) 484 - { 485 - /* 486 - * This function is just a stub to implement a hard force 487 - * of synchronize_sched(). This requires synchronizing 488 - * tasks even in userspace and idle. 489 - * 490 - * Yes, function tracing is rude. 491 - */ 492 - } 493 - 494 413 static int __unregister_ftrace_function(struct ftrace_ops *ops) 495 414 { 496 415 int ret; ··· 498 439 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) { 499 440 ret = remove_ftrace_list_ops(&ftrace_control_list, 500 441 &control_ops, ops); 501 - if (!ret) { 502 - /* 503 - * The ftrace_ops is now removed from the list, 504 - * so there'll be no new users. We must ensure 505 - * all current users are done before we free 506 - * the control data. 507 - * Note synchronize_sched() is not enough, as we 508 - * use preempt_disable() to do RCU, but the function 509 - * tracer can be called where RCU is not active 510 - * (before user_exit()). 511 - */ 512 - schedule_on_each_cpu(ftrace_sync); 513 - control_ops_free(ops); 514 - } 515 442 } else 516 443 ret = remove_ftrace_ops(&ftrace_ops_list, ops); 517 444 ··· 506 461 507 462 if (ftrace_enabled) 508 463 update_ftrace_function(); 509 - 510 - /* 511 - * Dynamic ops may be freed, we must make sure that all 512 - * callers are done before leaving this function. 513 - * 514 - * Again, normal synchronize_sched() is not good enough. 515 - * We need to do a hard force of sched synchronization. 516 - */ 517 - if (ops->flags & FTRACE_OPS_FL_DYNAMIC) 518 - schedule_on_each_cpu(ftrace_sync); 519 - 520 464 521 465 return 0; 522 466 } ··· 1115 1081 #endif /* CONFIG_FUNCTION_PROFILER */ 1116 1082 1117 1083 static struct pid * const ftrace_swapper_pid = &init_struct_pid; 1118 - 1119 - loff_t 1120 - ftrace_filter_lseek(struct file *file, loff_t offset, int whence) 1121 - { 1122 - loff_t ret; 1123 - 1124 - if (file->f_mode & FMODE_READ) 1125 - ret = seq_lseek(file, offset, whence); 1126 - else 1127 - file->f_pos = ret = 1; 1128 - 1129 - return ret; 1130 - } 1131 1084 1132 1085 #ifdef CONFIG_DYNAMIC_FTRACE 1133 1086 ··· 2013 1992 else if (command & FTRACE_DISABLE_CALLS) 2014 1993 ftrace_replace_code(0); 2015 1994 2016 - if (update && ftrace_trace_function != ftrace_ops_list_func) 1995 + if (update && ftrace_trace_function != ftrace_ops_list_func) { 1996 + function_trace_op = set_function_trace_op; 1997 + smp_wmb(); 1998 + /* If irqs are disabled, we are in stop machine */ 1999 + if (!irqs_disabled()) 2000 + smp_call_function(ftrace_sync_ipi, NULL, 1); 2017 2001 ftrace_update_ftrace_func(ftrace_trace_function); 2002 + } 2018 2003 2019 2004 if (command & FTRACE_START_FUNC_RET) 2020 2005 ftrace_enable_ftrace_graph_caller(); ··· 2183 2156 command |= FTRACE_UPDATE_TRACE_FUNC; 2184 2157 } 2185 2158 2186 - if (!command || !ftrace_enabled) 2159 + if (!command || !ftrace_enabled) { 2160 + /* 2161 + * If these are control ops, they still need their 2162 + * per_cpu field freed. Since, function tracing is 2163 + * not currently active, we can just free them 2164 + * without synchronizing all CPUs. 2165 + */ 2166 + if (ops->flags & FTRACE_OPS_FL_CONTROL) 2167 + control_ops_free(ops); 2187 2168 return 0; 2169 + } 2188 2170 2189 2171 ftrace_run_update_code(command); 2172 + 2173 + /* 2174 + * Dynamic ops may be freed, we must make sure that all 2175 + * callers are done before leaving this function. 2176 + * The same goes for freeing the per_cpu data of the control 2177 + * ops. 2178 + * 2179 + * Again, normal synchronize_sched() is not good enough. 2180 + * We need to do a hard force of sched synchronization. 2181 + * This is because we use preempt_disable() to do RCU, but 2182 + * the function tracers can be called where RCU is not watching 2183 + * (like before user_exit()). We can not rely on the RCU 2184 + * infrastructure to do the synchronization, thus we must do it 2185 + * ourselves. 2186 + */ 2187 + if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) { 2188 + schedule_on_each_cpu(ftrace_sync); 2189 + 2190 + if (ops->flags & FTRACE_OPS_FL_CONTROL) 2191 + control_ops_free(ops); 2192 + } 2193 + 2190 2194 return 0; 2191 2195 } 2192 2196 ··· 2797 2739 * routine, you can use ftrace_filter_write() for the write 2798 2740 * routine if @flag has FTRACE_ITER_FILTER set, or 2799 2741 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. 2800 - * ftrace_filter_lseek() should be used as the lseek routine, and 2742 + * tracing_lseek() should be used as the lseek routine, and 2801 2743 * release must call ftrace_regex_release(). 2802 2744 */ 2803 2745 int ··· 3825 3767 .open = ftrace_filter_open, 3826 3768 .read = seq_read, 3827 3769 .write = ftrace_filter_write, 3828 - .llseek = ftrace_filter_lseek, 3770 + .llseek = tracing_lseek, 3829 3771 .release = ftrace_regex_release, 3830 3772 }; 3831 3773 ··· 3833 3775 .open = ftrace_notrace_open, 3834 3776 .read = seq_read, 3835 3777 .write = ftrace_notrace_write, 3836 - .llseek = ftrace_filter_lseek, 3778 + .llseek = tracing_lseek, 3837 3779 .release = ftrace_regex_release, 3838 3780 }; 3839 3781 ··· 4096 4038 .open = ftrace_graph_open, 4097 4039 .read = seq_read, 4098 4040 .write = ftrace_graph_write, 4099 - .llseek = ftrace_filter_lseek, 4041 + .llseek = tracing_lseek, 4100 4042 .release = ftrace_graph_release, 4101 4043 }; 4102 4044 ··· 4104 4046 .open = ftrace_graph_notrace_open, 4105 4047 .read = seq_read, 4106 4048 .write = ftrace_graph_write, 4107 - .llseek = ftrace_filter_lseek, 4049 + .llseek = tracing_lseek, 4108 4050 .release = ftrace_graph_release, 4109 4051 }; 4110 4052 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ ··· 4777 4719 .open = ftrace_pid_open, 4778 4720 .write = ftrace_pid_write, 4779 4721 .read = seq_read, 4780 - .llseek = ftrace_filter_lseek, 4722 + .llseek = tracing_lseek, 4781 4723 .release = ftrace_pid_release, 4782 4724 }; 4783 4725 ··· 4920 4862 trace_func_graph_ret_t ftrace_graph_return = 4921 4863 (trace_func_graph_ret_t)ftrace_stub; 4922 4864 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; 4865 + static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; 4923 4866 4924 4867 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ 4925 4868 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) ··· 5062 5003 FTRACE_OPS_FL_RECURSION_SAFE, 5063 5004 }; 5064 5005 5006 + static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) 5007 + { 5008 + if (!ftrace_ops_test(&global_ops, trace->func, NULL)) 5009 + return 0; 5010 + return __ftrace_graph_entry(trace); 5011 + } 5012 + 5013 + /* 5014 + * The function graph tracer should only trace the functions defined 5015 + * by set_ftrace_filter and set_ftrace_notrace. If another function 5016 + * tracer ops is registered, the graph tracer requires testing the 5017 + * function against the global ops, and not just trace any function 5018 + * that any ftrace_ops registered. 5019 + */ 5020 + static void update_function_graph_func(void) 5021 + { 5022 + if (ftrace_ops_list == &ftrace_list_end || 5023 + (ftrace_ops_list == &global_ops && 5024 + global_ops.next == &ftrace_list_end)) 5025 + ftrace_graph_entry = __ftrace_graph_entry; 5026 + else 5027 + ftrace_graph_entry = ftrace_graph_entry_test; 5028 + } 5029 + 5065 5030 int register_ftrace_graph(trace_func_graph_ret_t retfunc, 5066 5031 trace_func_graph_ent_t entryfunc) 5067 5032 { ··· 5110 5027 } 5111 5028 5112 5029 ftrace_graph_return = retfunc; 5113 - ftrace_graph_entry = entryfunc; 5030 + 5031 + /* 5032 + * Update the indirect function to the entryfunc, and the 5033 + * function that gets called to the entry_test first. Then 5034 + * call the update fgraph entry function to determine if 5035 + * the entryfunc should be called directly or not. 5036 + */ 5037 + __ftrace_graph_entry = entryfunc; 5038 + ftrace_graph_entry = ftrace_graph_entry_test; 5039 + update_function_graph_func(); 5114 5040 5115 5041 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET); 5116 5042 ··· 5138 5046 ftrace_graph_active--; 5139 5047 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; 5140 5048 ftrace_graph_entry = ftrace_graph_entry_stub; 5049 + __ftrace_graph_entry = ftrace_graph_entry_stub; 5141 5050 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET); 5142 5051 unregister_pm_notifier(&ftrace_suspend_notifier); 5143 5052 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
+42 -15
kernel/trace/trace.c
··· 595 595 } 596 596 597 597 /** 598 + * tracing_alloc_snapshot - allocate snapshot buffer. 599 + * 600 + * This only allocates the snapshot buffer if it isn't already 601 + * allocated - it doesn't also take a snapshot. 602 + * 603 + * This is meant to be used in cases where the snapshot buffer needs 604 + * to be set up for events that can't sleep but need to be able to 605 + * trigger a snapshot. 606 + */ 607 + int tracing_alloc_snapshot(void) 608 + { 609 + struct trace_array *tr = &global_trace; 610 + int ret; 611 + 612 + ret = alloc_snapshot(tr); 613 + WARN_ON(ret < 0); 614 + 615 + return ret; 616 + } 617 + EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); 618 + 619 + /** 598 620 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer. 599 621 * 600 622 * This is similar to trace_snapshot(), but it will allocate the ··· 629 607 */ 630 608 void tracing_snapshot_alloc(void) 631 609 { 632 - struct trace_array *tr = &global_trace; 633 610 int ret; 634 611 635 - ret = alloc_snapshot(tr); 636 - if (WARN_ON(ret < 0)) 612 + ret = tracing_alloc_snapshot(); 613 + if (ret < 0) 637 614 return; 638 615 639 616 tracing_snapshot(); ··· 644 623 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); 645 624 } 646 625 EXPORT_SYMBOL_GPL(tracing_snapshot); 626 + int tracing_alloc_snapshot(void) 627 + { 628 + WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"); 629 + return -ENODEV; 630 + } 631 + EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); 647 632 void tracing_snapshot_alloc(void) 648 633 { 649 634 /* Give warning */ ··· 3183 3156 return count; 3184 3157 } 3185 3158 3186 - static loff_t tracing_seek(struct file *file, loff_t offset, int origin) 3159 + loff_t tracing_lseek(struct file *file, loff_t offset, int whence) 3187 3160 { 3161 + int ret; 3162 + 3188 3163 if (file->f_mode & FMODE_READ) 3189 - return seq_lseek(file, offset, origin); 3164 + ret = seq_lseek(file, offset, whence); 3190 3165 else 3191 - return 0; 3166 + file->f_pos = ret = 0; 3167 + 3168 + return ret; 3192 3169 } 3193 3170 3194 3171 static const struct file_operations tracing_fops = { 3195 3172 .open = tracing_open, 3196 3173 .read = seq_read, 3197 3174 .write = tracing_write_stub, 3198 - .llseek = tracing_seek, 3175 + .llseek = tracing_lseek, 3199 3176 .release = tracing_release, 3200 3177 }; 3201 3178 ··· 4243 4212 return sret; 4244 4213 } 4245 4214 4246 - static void tracing_pipe_buf_release(struct pipe_inode_info *pipe, 4247 - struct pipe_buffer *buf) 4248 - { 4249 - __free_page(buf->page); 4250 - } 4251 - 4252 4215 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, 4253 4216 unsigned int idx) 4254 4217 { ··· 4254 4229 .map = generic_pipe_buf_map, 4255 4230 .unmap = generic_pipe_buf_unmap, 4256 4231 .confirm = generic_pipe_buf_confirm, 4257 - .release = tracing_pipe_buf_release, 4232 + .release = generic_pipe_buf_release, 4258 4233 .steal = generic_pipe_buf_steal, 4259 4234 .get = generic_pipe_buf_get, 4260 4235 }; ··· 4938 4913 .open = tracing_snapshot_open, 4939 4914 .read = seq_read, 4940 4915 .write = tracing_snapshot_write, 4941 - .llseek = tracing_seek, 4916 + .llseek = tracing_lseek, 4942 4917 .release = tracing_snapshot_release, 4943 4918 }; 4944 4919 ··· 5907 5882 enum ring_buffer_flags rb_flags; 5908 5883 5909 5884 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; 5885 + 5886 + buf->tr = tr; 5910 5887 5911 5888 buf->buffer = ring_buffer_alloc(size, rb_flags); 5912 5889 if (!buf->buffer)
+193
kernel/trace/trace.h
··· 1 + 1 2 #ifndef _LINUX_KERNEL_TRACE_H 2 3 #define _LINUX_KERNEL_TRACE_H 3 4 ··· 588 587 int register_tracer(struct tracer *type); 589 588 int is_tracing_stopped(void); 590 589 590 + loff_t tracing_lseek(struct file *file, loff_t offset, int whence); 591 + 591 592 extern cpumask_var_t __read_mostly tracing_buffer_mask; 592 593 593 594 #define for_each_tracing_cpu(cpu) \ ··· 1023 1020 extern void print_subsystem_event_filter(struct event_subsystem *system, 1024 1021 struct trace_seq *s); 1025 1022 extern int filter_assign_type(const char *type); 1023 + extern int create_event_filter(struct ftrace_event_call *call, 1024 + char *filter_str, bool set_str, 1025 + struct event_filter **filterp); 1026 + extern void free_event_filter(struct event_filter *filter); 1026 1027 1027 1028 struct ftrace_event_field * 1028 1029 trace_find_event_field(struct ftrace_event_call *call, char *name); ··· 1035 1028 extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr); 1036 1029 extern int event_trace_del_tracer(struct trace_array *tr); 1037 1030 1031 + extern struct ftrace_event_file *find_event_file(struct trace_array *tr, 1032 + const char *system, 1033 + const char *event); 1034 + 1035 + static inline void *event_file_data(struct file *filp) 1036 + { 1037 + return ACCESS_ONCE(file_inode(filp)->i_private); 1038 + } 1039 + 1038 1040 extern struct mutex event_mutex; 1039 1041 extern struct list_head ftrace_events; 1042 + 1043 + extern const struct file_operations event_trigger_fops; 1044 + 1045 + extern int register_trigger_cmds(void); 1046 + extern void clear_event_triggers(struct trace_array *tr); 1047 + 1048 + struct event_trigger_data { 1049 + unsigned long count; 1050 + int ref; 1051 + struct event_trigger_ops *ops; 1052 + struct event_command *cmd_ops; 1053 + struct event_filter __rcu *filter; 1054 + char *filter_str; 1055 + void *private_data; 1056 + struct list_head list; 1057 + }; 1058 + 1059 + /** 1060 + * struct event_trigger_ops - callbacks for trace event triggers 1061 + * 1062 + * The methods in this structure provide per-event trigger hooks for 1063 + * various trigger operations. 1064 + * 1065 + * All the methods below, except for @init() and @free(), must be 1066 + * implemented. 1067 + * 1068 + * @func: The trigger 'probe' function called when the triggering 1069 + * event occurs. The data passed into this callback is the data 1070 + * that was supplied to the event_command @reg() function that 1071 + * registered the trigger (see struct event_command). 1072 + * 1073 + * @init: An optional initialization function called for the trigger 1074 + * when the trigger is registered (via the event_command reg() 1075 + * function). This can be used to perform per-trigger 1076 + * initialization such as incrementing a per-trigger reference 1077 + * count, for instance. This is usually implemented by the 1078 + * generic utility function @event_trigger_init() (see 1079 + * trace_event_triggers.c). 1080 + * 1081 + * @free: An optional de-initialization function called for the 1082 + * trigger when the trigger is unregistered (via the 1083 + * event_command @reg() function). This can be used to perform 1084 + * per-trigger de-initialization such as decrementing a 1085 + * per-trigger reference count and freeing corresponding trigger 1086 + * data, for instance. This is usually implemented by the 1087 + * generic utility function @event_trigger_free() (see 1088 + * trace_event_triggers.c). 1089 + * 1090 + * @print: The callback function invoked to have the trigger print 1091 + * itself. This is usually implemented by a wrapper function 1092 + * that calls the generic utility function @event_trigger_print() 1093 + * (see trace_event_triggers.c). 1094 + */ 1095 + struct event_trigger_ops { 1096 + void (*func)(struct event_trigger_data *data); 1097 + int (*init)(struct event_trigger_ops *ops, 1098 + struct event_trigger_data *data); 1099 + void (*free)(struct event_trigger_ops *ops, 1100 + struct event_trigger_data *data); 1101 + int (*print)(struct seq_file *m, 1102 + struct event_trigger_ops *ops, 1103 + struct event_trigger_data *data); 1104 + }; 1105 + 1106 + /** 1107 + * struct event_command - callbacks and data members for event commands 1108 + * 1109 + * Event commands are invoked by users by writing the command name 1110 + * into the 'trigger' file associated with a trace event. The 1111 + * parameters associated with a specific invocation of an event 1112 + * command are used to create an event trigger instance, which is 1113 + * added to the list of trigger instances associated with that trace 1114 + * event. When the event is hit, the set of triggers associated with 1115 + * that event is invoked. 1116 + * 1117 + * The data members in this structure provide per-event command data 1118 + * for various event commands. 1119 + * 1120 + * All the data members below, except for @post_trigger, must be set 1121 + * for each event command. 1122 + * 1123 + * @name: The unique name that identifies the event command. This is 1124 + * the name used when setting triggers via trigger files. 1125 + * 1126 + * @trigger_type: A unique id that identifies the event command 1127 + * 'type'. This value has two purposes, the first to ensure that 1128 + * only one trigger of the same type can be set at a given time 1129 + * for a particular event e.g. it doesn't make sense to have both 1130 + * a traceon and traceoff trigger attached to a single event at 1131 + * the same time, so traceon and traceoff have the same type 1132 + * though they have different names. The @trigger_type value is 1133 + * also used as a bit value for deferring the actual trigger 1134 + * action until after the current event is finished. Some 1135 + * commands need to do this if they themselves log to the trace 1136 + * buffer (see the @post_trigger() member below). @trigger_type 1137 + * values are defined by adding new values to the trigger_type 1138 + * enum in include/linux/ftrace_event.h. 1139 + * 1140 + * @post_trigger: A flag that says whether or not this command needs 1141 + * to have its action delayed until after the current event has 1142 + * been closed. Some triggers need to avoid being invoked while 1143 + * an event is currently in the process of being logged, since 1144 + * the trigger may itself log data into the trace buffer. Thus 1145 + * we make sure the current event is committed before invoking 1146 + * those triggers. To do that, the trigger invocation is split 1147 + * in two - the first part checks the filter using the current 1148 + * trace record; if a command has the @post_trigger flag set, it 1149 + * sets a bit for itself in the return value, otherwise it 1150 + * directly invokes the trigger. Once all commands have been 1151 + * either invoked or set their return flag, the current record is 1152 + * either committed or discarded. At that point, if any commands 1153 + * have deferred their triggers, those commands are finally 1154 + * invoked following the close of the current event. In other 1155 + * words, if the event_trigger_ops @func() probe implementation 1156 + * itself logs to the trace buffer, this flag should be set, 1157 + * otherwise it can be left unspecified. 1158 + * 1159 + * All the methods below, except for @set_filter(), must be 1160 + * implemented. 1161 + * 1162 + * @func: The callback function responsible for parsing and 1163 + * registering the trigger written to the 'trigger' file by the 1164 + * user. It allocates the trigger instance and registers it with 1165 + * the appropriate trace event. It makes use of the other 1166 + * event_command callback functions to orchestrate this, and is 1167 + * usually implemented by the generic utility function 1168 + * @event_trigger_callback() (see trace_event_triggers.c). 1169 + * 1170 + * @reg: Adds the trigger to the list of triggers associated with the 1171 + * event, and enables the event trigger itself, after 1172 + * initializing it (via the event_trigger_ops @init() function). 1173 + * This is also where commands can use the @trigger_type value to 1174 + * make the decision as to whether or not multiple instances of 1175 + * the trigger should be allowed. This is usually implemented by 1176 + * the generic utility function @register_trigger() (see 1177 + * trace_event_triggers.c). 1178 + * 1179 + * @unreg: Removes the trigger from the list of triggers associated 1180 + * with the event, and disables the event trigger itself, after 1181 + * initializing it (via the event_trigger_ops @free() function). 1182 + * This is usually implemented by the generic utility function 1183 + * @unregister_trigger() (see trace_event_triggers.c). 1184 + * 1185 + * @set_filter: An optional function called to parse and set a filter 1186 + * for the trigger. If no @set_filter() method is set for the 1187 + * event command, filters set by the user for the command will be 1188 + * ignored. This is usually implemented by the generic utility 1189 + * function @set_trigger_filter() (see trace_event_triggers.c). 1190 + * 1191 + * @get_trigger_ops: The callback function invoked to retrieve the 1192 + * event_trigger_ops implementation associated with the command. 1193 + */ 1194 + struct event_command { 1195 + struct list_head list; 1196 + char *name; 1197 + enum event_trigger_type trigger_type; 1198 + bool post_trigger; 1199 + int (*func)(struct event_command *cmd_ops, 1200 + struct ftrace_event_file *file, 1201 + char *glob, char *cmd, char *params); 1202 + int (*reg)(char *glob, 1203 + struct event_trigger_ops *ops, 1204 + struct event_trigger_data *data, 1205 + struct ftrace_event_file *file); 1206 + void (*unreg)(char *glob, 1207 + struct event_trigger_ops *ops, 1208 + struct event_trigger_data *data, 1209 + struct ftrace_event_file *file); 1210 + int (*set_filter)(char *filter_str, 1211 + struct event_trigger_data *data, 1212 + struct ftrace_event_file *file); 1213 + struct event_trigger_ops *(*get_trigger_ops)(char *cmd, char *param); 1214 + }; 1215 + 1216 + extern int trace_event_enable_disable(struct ftrace_event_file *file, 1217 + int enable, int soft_disable); 1218 + extern int tracing_alloc_snapshot(void); 1040 1219 1041 1220 extern const char *__start___trace_bprintk_fmt[]; 1042 1221 extern const char *__stop___trace_bprintk_fmt[];
+30 -19
kernel/trace/trace_events.c
··· 342 342 return ret; 343 343 } 344 344 345 + int trace_event_enable_disable(struct ftrace_event_file *file, 346 + int enable, int soft_disable) 347 + { 348 + return __ftrace_event_enable_disable(file, enable, soft_disable); 349 + } 350 + 345 351 static int ftrace_event_enable_disable(struct ftrace_event_file *file, 346 352 int enable) 347 353 { ··· 425 419 list_del(&dir->list); 426 420 __put_system_dir(dir); 427 421 } 428 - } 429 - 430 - static void *event_file_data(struct file *filp) 431 - { 432 - return ACCESS_ONCE(file_inode(filp)->i_private); 433 422 } 434 423 435 424 static void remove_event_file_dir(struct ftrace_event_file *file) ··· 1550 1549 trace_create_file("filter", 0644, file->dir, file, 1551 1550 &ftrace_event_filter_fops); 1552 1551 1552 + trace_create_file("trigger", 0644, file->dir, file, 1553 + &event_trigger_fops); 1554 + 1553 1555 trace_create_file("format", 0444, file->dir, call, 1554 1556 &ftrace_event_format_fops); 1555 1557 ··· 1649 1645 file->event_call = call; 1650 1646 file->tr = tr; 1651 1647 atomic_set(&file->sm_ref, 0); 1648 + atomic_set(&file->tm_ref, 0); 1649 + INIT_LIST_HEAD(&file->triggers); 1652 1650 list_add(&file->list, &tr->events); 1653 1651 1654 1652 return file; ··· 1855 1849 } 1856 1850 } 1857 1851 1858 - #ifdef CONFIG_DYNAMIC_FTRACE 1859 - 1860 - /* Avoid typos */ 1861 - #define ENABLE_EVENT_STR "enable_event" 1862 - #define DISABLE_EVENT_STR "disable_event" 1863 - 1864 - struct event_probe_data { 1865 - struct ftrace_event_file *file; 1866 - unsigned long count; 1867 - int ref; 1868 - bool enable; 1869 - }; 1870 - 1871 - static struct ftrace_event_file * 1852 + struct ftrace_event_file * 1872 1853 find_event_file(struct trace_array *tr, const char *system, const char *event) 1873 1854 { 1874 1855 struct ftrace_event_file *file; ··· 1877 1884 } 1878 1885 return NULL; 1879 1886 } 1887 + 1888 + #ifdef CONFIG_DYNAMIC_FTRACE 1889 + 1890 + /* Avoid typos */ 1891 + #define ENABLE_EVENT_STR "enable_event" 1892 + #define DISABLE_EVENT_STR "disable_event" 1893 + 1894 + struct event_probe_data { 1895 + struct ftrace_event_file *file; 1896 + unsigned long count; 1897 + int ref; 1898 + bool enable; 1899 + }; 1880 1900 1881 1901 static void 1882 1902 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data) ··· 2317 2311 { 2318 2312 mutex_lock(&event_mutex); 2319 2313 2314 + /* Disable any event triggers and associated soft-disabled events */ 2315 + clear_event_triggers(tr); 2316 + 2320 2317 /* Disable any running events */ 2321 2318 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0); 2322 2319 ··· 2385 2376 trace_printk_start_comm(); 2386 2377 2387 2378 register_event_cmds(); 2379 + 2380 + register_trigger_cmds(); 2388 2381 2389 2382 return 0; 2390 2383 }
+12
kernel/trace/trace_events_filter.c
··· 799 799 kfree(filter); 800 800 } 801 801 802 + void free_event_filter(struct event_filter *filter) 803 + { 804 + __free_filter(filter); 805 + } 806 + 802 807 void destroy_call_preds(struct ftrace_event_call *call) 803 808 { 804 809 __free_filter(call->filter); ··· 1941 1936 1942 1937 *filterp = filter; 1943 1938 return err; 1939 + } 1940 + 1941 + int create_event_filter(struct ftrace_event_call *call, 1942 + char *filter_str, bool set_str, 1943 + struct event_filter **filterp) 1944 + { 1945 + return create_filter(call, filter_str, set_str, filterp); 1944 1946 } 1945 1947 1946 1948 /**
+1437
kernel/trace/trace_events_trigger.c
··· 1 + /* 2 + * trace_events_trigger - trace event triggers 3 + * 4 + * This program is free software; you can redistribute it and/or modify 5 + * it under the terms of the GNU General Public License as published by 6 + * the Free Software Foundation; either version 2 of the License, or 7 + * (at your option) any later version. 8 + * 9 + * This program is distributed in the hope that it will be useful, 10 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 + * GNU General Public License for more details. 13 + * 14 + * You should have received a copy of the GNU General Public License 15 + * along with this program; if not, write to the Free Software 16 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 + * 18 + * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com> 19 + */ 20 + 21 + #include <linux/module.h> 22 + #include <linux/ctype.h> 23 + #include <linux/mutex.h> 24 + #include <linux/slab.h> 25 + 26 + #include "trace.h" 27 + 28 + static LIST_HEAD(trigger_commands); 29 + static DEFINE_MUTEX(trigger_cmd_mutex); 30 + 31 + static void 32 + trigger_data_free(struct event_trigger_data *data) 33 + { 34 + if (data->cmd_ops->set_filter) 35 + data->cmd_ops->set_filter(NULL, data, NULL); 36 + 37 + synchronize_sched(); /* make sure current triggers exit before free */ 38 + kfree(data); 39 + } 40 + 41 + /** 42 + * event_triggers_call - Call triggers associated with a trace event 43 + * @file: The ftrace_event_file associated with the event 44 + * @rec: The trace entry for the event, NULL for unconditional invocation 45 + * 46 + * For each trigger associated with an event, invoke the trigger 47 + * function registered with the associated trigger command. If rec is 48 + * non-NULL, it means that the trigger requires further processing and 49 + * shouldn't be unconditionally invoked. If rec is non-NULL and the 50 + * trigger has a filter associated with it, rec will checked against 51 + * the filter and if the record matches the trigger will be invoked. 52 + * If the trigger is a 'post_trigger', meaning it shouldn't be invoked 53 + * in any case until the current event is written, the trigger 54 + * function isn't invoked but the bit associated with the deferred 55 + * trigger is set in the return value. 56 + * 57 + * Returns an enum event_trigger_type value containing a set bit for 58 + * any trigger that should be deferred, ETT_NONE if nothing to defer. 59 + * 60 + * Called from tracepoint handlers (with rcu_read_lock_sched() held). 61 + * 62 + * Return: an enum event_trigger_type value containing a set bit for 63 + * any trigger that should be deferred, ETT_NONE if nothing to defer. 64 + */ 65 + enum event_trigger_type 66 + event_triggers_call(struct ftrace_event_file *file, void *rec) 67 + { 68 + struct event_trigger_data *data; 69 + enum event_trigger_type tt = ETT_NONE; 70 + struct event_filter *filter; 71 + 72 + if (list_empty(&file->triggers)) 73 + return tt; 74 + 75 + list_for_each_entry_rcu(data, &file->triggers, list) { 76 + if (!rec) { 77 + data->ops->func(data); 78 + continue; 79 + } 80 + filter = rcu_dereference(data->filter); 81 + if (filter && !filter_match_preds(filter, rec)) 82 + continue; 83 + if (data->cmd_ops->post_trigger) { 84 + tt |= data->cmd_ops->trigger_type; 85 + continue; 86 + } 87 + data->ops->func(data); 88 + } 89 + return tt; 90 + } 91 + EXPORT_SYMBOL_GPL(event_triggers_call); 92 + 93 + /** 94 + * event_triggers_post_call - Call 'post_triggers' for a trace event 95 + * @file: The ftrace_event_file associated with the event 96 + * @tt: enum event_trigger_type containing a set bit for each trigger to invoke 97 + * 98 + * For each trigger associated with an event, invoke the trigger 99 + * function registered with the associated trigger command, if the 100 + * corresponding bit is set in the tt enum passed into this function. 101 + * See @event_triggers_call for details on how those bits are set. 102 + * 103 + * Called from tracepoint handlers (with rcu_read_lock_sched() held). 104 + */ 105 + void 106 + event_triggers_post_call(struct ftrace_event_file *file, 107 + enum event_trigger_type tt) 108 + { 109 + struct event_trigger_data *data; 110 + 111 + list_for_each_entry_rcu(data, &file->triggers, list) { 112 + if (data->cmd_ops->trigger_type & tt) 113 + data->ops->func(data); 114 + } 115 + } 116 + EXPORT_SYMBOL_GPL(event_triggers_post_call); 117 + 118 + #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL) 119 + 120 + static void *trigger_next(struct seq_file *m, void *t, loff_t *pos) 121 + { 122 + struct ftrace_event_file *event_file = event_file_data(m->private); 123 + 124 + if (t == SHOW_AVAILABLE_TRIGGERS) 125 + return NULL; 126 + 127 + return seq_list_next(t, &event_file->triggers, pos); 128 + } 129 + 130 + static void *trigger_start(struct seq_file *m, loff_t *pos) 131 + { 132 + struct ftrace_event_file *event_file; 133 + 134 + /* ->stop() is called even if ->start() fails */ 135 + mutex_lock(&event_mutex); 136 + event_file = event_file_data(m->private); 137 + if (unlikely(!event_file)) 138 + return ERR_PTR(-ENODEV); 139 + 140 + if (list_empty(&event_file->triggers)) 141 + return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL; 142 + 143 + return seq_list_start(&event_file->triggers, *pos); 144 + } 145 + 146 + static void trigger_stop(struct seq_file *m, void *t) 147 + { 148 + mutex_unlock(&event_mutex); 149 + } 150 + 151 + static int trigger_show(struct seq_file *m, void *v) 152 + { 153 + struct event_trigger_data *data; 154 + struct event_command *p; 155 + 156 + if (v == SHOW_AVAILABLE_TRIGGERS) { 157 + seq_puts(m, "# Available triggers:\n"); 158 + seq_putc(m, '#'); 159 + mutex_lock(&trigger_cmd_mutex); 160 + list_for_each_entry_reverse(p, &trigger_commands, list) 161 + seq_printf(m, " %s", p->name); 162 + seq_putc(m, '\n'); 163 + mutex_unlock(&trigger_cmd_mutex); 164 + return 0; 165 + } 166 + 167 + data = list_entry(v, struct event_trigger_data, list); 168 + data->ops->print(m, data->ops, data); 169 + 170 + return 0; 171 + } 172 + 173 + static const struct seq_operations event_triggers_seq_ops = { 174 + .start = trigger_start, 175 + .next = trigger_next, 176 + .stop = trigger_stop, 177 + .show = trigger_show, 178 + }; 179 + 180 + static int event_trigger_regex_open(struct inode *inode, struct file *file) 181 + { 182 + int ret = 0; 183 + 184 + mutex_lock(&event_mutex); 185 + 186 + if (unlikely(!event_file_data(file))) { 187 + mutex_unlock(&event_mutex); 188 + return -ENODEV; 189 + } 190 + 191 + if (file->f_mode & FMODE_READ) { 192 + ret = seq_open(file, &event_triggers_seq_ops); 193 + if (!ret) { 194 + struct seq_file *m = file->private_data; 195 + m->private = file; 196 + } 197 + } 198 + 199 + mutex_unlock(&event_mutex); 200 + 201 + return ret; 202 + } 203 + 204 + static int trigger_process_regex(struct ftrace_event_file *file, char *buff) 205 + { 206 + char *command, *next = buff; 207 + struct event_command *p; 208 + int ret = -EINVAL; 209 + 210 + command = strsep(&next, ": \t"); 211 + command = (command[0] != '!') ? command : command + 1; 212 + 213 + mutex_lock(&trigger_cmd_mutex); 214 + list_for_each_entry(p, &trigger_commands, list) { 215 + if (strcmp(p->name, command) == 0) { 216 + ret = p->func(p, file, buff, command, next); 217 + goto out_unlock; 218 + } 219 + } 220 + out_unlock: 221 + mutex_unlock(&trigger_cmd_mutex); 222 + 223 + return ret; 224 + } 225 + 226 + static ssize_t event_trigger_regex_write(struct file *file, 227 + const char __user *ubuf, 228 + size_t cnt, loff_t *ppos) 229 + { 230 + struct ftrace_event_file *event_file; 231 + ssize_t ret; 232 + char *buf; 233 + 234 + if (!cnt) 235 + return 0; 236 + 237 + if (cnt >= PAGE_SIZE) 238 + return -EINVAL; 239 + 240 + buf = (char *)__get_free_page(GFP_TEMPORARY); 241 + if (!buf) 242 + return -ENOMEM; 243 + 244 + if (copy_from_user(buf, ubuf, cnt)) { 245 + free_page((unsigned long)buf); 246 + return -EFAULT; 247 + } 248 + buf[cnt] = '\0'; 249 + strim(buf); 250 + 251 + mutex_lock(&event_mutex); 252 + event_file = event_file_data(file); 253 + if (unlikely(!event_file)) { 254 + mutex_unlock(&event_mutex); 255 + free_page((unsigned long)buf); 256 + return -ENODEV; 257 + } 258 + ret = trigger_process_regex(event_file, buf); 259 + mutex_unlock(&event_mutex); 260 + 261 + free_page((unsigned long)buf); 262 + if (ret < 0) 263 + goto out; 264 + 265 + *ppos += cnt; 266 + ret = cnt; 267 + out: 268 + return ret; 269 + } 270 + 271 + static int event_trigger_regex_release(struct inode *inode, struct file *file) 272 + { 273 + mutex_lock(&event_mutex); 274 + 275 + if (file->f_mode & FMODE_READ) 276 + seq_release(inode, file); 277 + 278 + mutex_unlock(&event_mutex); 279 + 280 + return 0; 281 + } 282 + 283 + static ssize_t 284 + event_trigger_write(struct file *filp, const char __user *ubuf, 285 + size_t cnt, loff_t *ppos) 286 + { 287 + return event_trigger_regex_write(filp, ubuf, cnt, ppos); 288 + } 289 + 290 + static int 291 + event_trigger_open(struct inode *inode, struct file *filp) 292 + { 293 + return event_trigger_regex_open(inode, filp); 294 + } 295 + 296 + static int 297 + event_trigger_release(struct inode *inode, struct file *file) 298 + { 299 + return event_trigger_regex_release(inode, file); 300 + } 301 + 302 + const struct file_operations event_trigger_fops = { 303 + .open = event_trigger_open, 304 + .read = seq_read, 305 + .write = event_trigger_write, 306 + .llseek = tracing_lseek, 307 + .release = event_trigger_release, 308 + }; 309 + 310 + /* 311 + * Currently we only register event commands from __init, so mark this 312 + * __init too. 313 + */ 314 + static __init int register_event_command(struct event_command *cmd) 315 + { 316 + struct event_command *p; 317 + int ret = 0; 318 + 319 + mutex_lock(&trigger_cmd_mutex); 320 + list_for_each_entry(p, &trigger_commands, list) { 321 + if (strcmp(cmd->name, p->name) == 0) { 322 + ret = -EBUSY; 323 + goto out_unlock; 324 + } 325 + } 326 + list_add(&cmd->list, &trigger_commands); 327 + out_unlock: 328 + mutex_unlock(&trigger_cmd_mutex); 329 + 330 + return ret; 331 + } 332 + 333 + /* 334 + * Currently we only unregister event commands from __init, so mark 335 + * this __init too. 336 + */ 337 + static __init int unregister_event_command(struct event_command *cmd) 338 + { 339 + struct event_command *p, *n; 340 + int ret = -ENODEV; 341 + 342 + mutex_lock(&trigger_cmd_mutex); 343 + list_for_each_entry_safe(p, n, &trigger_commands, list) { 344 + if (strcmp(cmd->name, p->name) == 0) { 345 + ret = 0; 346 + list_del_init(&p->list); 347 + goto out_unlock; 348 + } 349 + } 350 + out_unlock: 351 + mutex_unlock(&trigger_cmd_mutex); 352 + 353 + return ret; 354 + } 355 + 356 + /** 357 + * event_trigger_print - Generic event_trigger_ops @print implementation 358 + * @name: The name of the event trigger 359 + * @m: The seq_file being printed to 360 + * @data: Trigger-specific data 361 + * @filter_str: filter_str to print, if present 362 + * 363 + * Common implementation for event triggers to print themselves. 364 + * 365 + * Usually wrapped by a function that simply sets the @name of the 366 + * trigger command and then invokes this. 367 + * 368 + * Return: 0 on success, errno otherwise 369 + */ 370 + static int 371 + event_trigger_print(const char *name, struct seq_file *m, 372 + void *data, char *filter_str) 373 + { 374 + long count = (long)data; 375 + 376 + seq_printf(m, "%s", name); 377 + 378 + if (count == -1) 379 + seq_puts(m, ":unlimited"); 380 + else 381 + seq_printf(m, ":count=%ld", count); 382 + 383 + if (filter_str) 384 + seq_printf(m, " if %s\n", filter_str); 385 + else 386 + seq_puts(m, "\n"); 387 + 388 + return 0; 389 + } 390 + 391 + /** 392 + * event_trigger_init - Generic event_trigger_ops @init implementation 393 + * @ops: The trigger ops associated with the trigger 394 + * @data: Trigger-specific data 395 + * 396 + * Common implementation of event trigger initialization. 397 + * 398 + * Usually used directly as the @init method in event trigger 399 + * implementations. 400 + * 401 + * Return: 0 on success, errno otherwise 402 + */ 403 + static int 404 + event_trigger_init(struct event_trigger_ops *ops, 405 + struct event_trigger_data *data) 406 + { 407 + data->ref++; 408 + return 0; 409 + } 410 + 411 + /** 412 + * event_trigger_free - Generic event_trigger_ops @free implementation 413 + * @ops: The trigger ops associated with the trigger 414 + * @data: Trigger-specific data 415 + * 416 + * Common implementation of event trigger de-initialization. 417 + * 418 + * Usually used directly as the @free method in event trigger 419 + * implementations. 420 + */ 421 + static void 422 + event_trigger_free(struct event_trigger_ops *ops, 423 + struct event_trigger_data *data) 424 + { 425 + if (WARN_ON_ONCE(data->ref <= 0)) 426 + return; 427 + 428 + data->ref--; 429 + if (!data->ref) 430 + trigger_data_free(data); 431 + } 432 + 433 + static int trace_event_trigger_enable_disable(struct ftrace_event_file *file, 434 + int trigger_enable) 435 + { 436 + int ret = 0; 437 + 438 + if (trigger_enable) { 439 + if (atomic_inc_return(&file->tm_ref) > 1) 440 + return ret; 441 + set_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags); 442 + ret = trace_event_enable_disable(file, 1, 1); 443 + } else { 444 + if (atomic_dec_return(&file->tm_ref) > 0) 445 + return ret; 446 + clear_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags); 447 + ret = trace_event_enable_disable(file, 0, 1); 448 + } 449 + 450 + return ret; 451 + } 452 + 453 + /** 454 + * clear_event_triggers - Clear all triggers associated with a trace array 455 + * @tr: The trace array to clear 456 + * 457 + * For each trigger, the triggering event has its tm_ref decremented 458 + * via trace_event_trigger_enable_disable(), and any associated event 459 + * (in the case of enable/disable_event triggers) will have its sm_ref 460 + * decremented via free()->trace_event_enable_disable(). That 461 + * combination effectively reverses the soft-mode/trigger state added 462 + * by trigger registration. 463 + * 464 + * Must be called with event_mutex held. 465 + */ 466 + void 467 + clear_event_triggers(struct trace_array *tr) 468 + { 469 + struct ftrace_event_file *file; 470 + 471 + list_for_each_entry(file, &tr->events, list) { 472 + struct event_trigger_data *data; 473 + list_for_each_entry_rcu(data, &file->triggers, list) { 474 + trace_event_trigger_enable_disable(file, 0); 475 + if (data->ops->free) 476 + data->ops->free(data->ops, data); 477 + } 478 + } 479 + } 480 + 481 + /** 482 + * update_cond_flag - Set or reset the TRIGGER_COND bit 483 + * @file: The ftrace_event_file associated with the event 484 + * 485 + * If an event has triggers and any of those triggers has a filter or 486 + * a post_trigger, trigger invocation needs to be deferred until after 487 + * the current event has logged its data, and the event should have 488 + * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be 489 + * cleared. 490 + */ 491 + static void update_cond_flag(struct ftrace_event_file *file) 492 + { 493 + struct event_trigger_data *data; 494 + bool set_cond = false; 495 + 496 + list_for_each_entry_rcu(data, &file->triggers, list) { 497 + if (data->filter || data->cmd_ops->post_trigger) { 498 + set_cond = true; 499 + break; 500 + } 501 + } 502 + 503 + if (set_cond) 504 + set_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT, &file->flags); 505 + else 506 + clear_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT, &file->flags); 507 + } 508 + 509 + /** 510 + * register_trigger - Generic event_command @reg implementation 511 + * @glob: The raw string used to register the trigger 512 + * @ops: The trigger ops associated with the trigger 513 + * @data: Trigger-specific data to associate with the trigger 514 + * @file: The ftrace_event_file associated with the event 515 + * 516 + * Common implementation for event trigger registration. 517 + * 518 + * Usually used directly as the @reg method in event command 519 + * implementations. 520 + * 521 + * Return: 0 on success, errno otherwise 522 + */ 523 + static int register_trigger(char *glob, struct event_trigger_ops *ops, 524 + struct event_trigger_data *data, 525 + struct ftrace_event_file *file) 526 + { 527 + struct event_trigger_data *test; 528 + int ret = 0; 529 + 530 + list_for_each_entry_rcu(test, &file->triggers, list) { 531 + if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) { 532 + ret = -EEXIST; 533 + goto out; 534 + } 535 + } 536 + 537 + if (data->ops->init) { 538 + ret = data->ops->init(data->ops, data); 539 + if (ret < 0) 540 + goto out; 541 + } 542 + 543 + list_add_rcu(&data->list, &file->triggers); 544 + ret++; 545 + 546 + if (trace_event_trigger_enable_disable(file, 1) < 0) { 547 + list_del_rcu(&data->list); 548 + ret--; 549 + } 550 + update_cond_flag(file); 551 + out: 552 + return ret; 553 + } 554 + 555 + /** 556 + * unregister_trigger - Generic event_command @unreg implementation 557 + * @glob: The raw string used to register the trigger 558 + * @ops: The trigger ops associated with the trigger 559 + * @test: Trigger-specific data used to find the trigger to remove 560 + * @file: The ftrace_event_file associated with the event 561 + * 562 + * Common implementation for event trigger unregistration. 563 + * 564 + * Usually used directly as the @unreg method in event command 565 + * implementations. 566 + */ 567 + static void unregister_trigger(char *glob, struct event_trigger_ops *ops, 568 + struct event_trigger_data *test, 569 + struct ftrace_event_file *file) 570 + { 571 + struct event_trigger_data *data; 572 + bool unregistered = false; 573 + 574 + list_for_each_entry_rcu(data, &file->triggers, list) { 575 + if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) { 576 + unregistered = true; 577 + list_del_rcu(&data->list); 578 + update_cond_flag(file); 579 + trace_event_trigger_enable_disable(file, 0); 580 + break; 581 + } 582 + } 583 + 584 + if (unregistered && data->ops->free) 585 + data->ops->free(data->ops, data); 586 + } 587 + 588 + /** 589 + * event_trigger_callback - Generic event_command @func implementation 590 + * @cmd_ops: The command ops, used for trigger registration 591 + * @file: The ftrace_event_file associated with the event 592 + * @glob: The raw string used to register the trigger 593 + * @cmd: The cmd portion of the string used to register the trigger 594 + * @param: The params portion of the string used to register the trigger 595 + * 596 + * Common implementation for event command parsing and trigger 597 + * instantiation. 598 + * 599 + * Usually used directly as the @func method in event command 600 + * implementations. 601 + * 602 + * Return: 0 on success, errno otherwise 603 + */ 604 + static int 605 + event_trigger_callback(struct event_command *cmd_ops, 606 + struct ftrace_event_file *file, 607 + char *glob, char *cmd, char *param) 608 + { 609 + struct event_trigger_data *trigger_data; 610 + struct event_trigger_ops *trigger_ops; 611 + char *trigger = NULL; 612 + char *number; 613 + int ret; 614 + 615 + /* separate the trigger from the filter (t:n [if filter]) */ 616 + if (param && isdigit(param[0])) 617 + trigger = strsep(&param, " \t"); 618 + 619 + trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); 620 + 621 + ret = -ENOMEM; 622 + trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); 623 + if (!trigger_data) 624 + goto out; 625 + 626 + trigger_data->count = -1; 627 + trigger_data->ops = trigger_ops; 628 + trigger_data->cmd_ops = cmd_ops; 629 + INIT_LIST_HEAD(&trigger_data->list); 630 + 631 + if (glob[0] == '!') { 632 + cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 633 + kfree(trigger_data); 634 + ret = 0; 635 + goto out; 636 + } 637 + 638 + if (trigger) { 639 + number = strsep(&trigger, ":"); 640 + 641 + ret = -EINVAL; 642 + if (!strlen(number)) 643 + goto out_free; 644 + 645 + /* 646 + * We use the callback data field (which is a pointer) 647 + * as our counter. 648 + */ 649 + ret = kstrtoul(number, 0, &trigger_data->count); 650 + if (ret) 651 + goto out_free; 652 + } 653 + 654 + if (!param) /* if param is non-empty, it's supposed to be a filter */ 655 + goto out_reg; 656 + 657 + if (!cmd_ops->set_filter) 658 + goto out_reg; 659 + 660 + ret = cmd_ops->set_filter(param, trigger_data, file); 661 + if (ret < 0) 662 + goto out_free; 663 + 664 + out_reg: 665 + ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); 666 + /* 667 + * The above returns on success the # of functions enabled, 668 + * but if it didn't find any functions it returns zero. 669 + * Consider no functions a failure too. 670 + */ 671 + if (!ret) { 672 + ret = -ENOENT; 673 + goto out_free; 674 + } else if (ret < 0) 675 + goto out_free; 676 + ret = 0; 677 + out: 678 + return ret; 679 + 680 + out_free: 681 + if (cmd_ops->set_filter) 682 + cmd_ops->set_filter(NULL, trigger_data, NULL); 683 + kfree(trigger_data); 684 + goto out; 685 + } 686 + 687 + /** 688 + * set_trigger_filter - Generic event_command @set_filter implementation 689 + * @filter_str: The filter string for the trigger, NULL to remove filter 690 + * @trigger_data: Trigger-specific data 691 + * @file: The ftrace_event_file associated with the event 692 + * 693 + * Common implementation for event command filter parsing and filter 694 + * instantiation. 695 + * 696 + * Usually used directly as the @set_filter method in event command 697 + * implementations. 698 + * 699 + * Also used to remove a filter (if filter_str = NULL). 700 + * 701 + * Return: 0 on success, errno otherwise 702 + */ 703 + static int set_trigger_filter(char *filter_str, 704 + struct event_trigger_data *trigger_data, 705 + struct ftrace_event_file *file) 706 + { 707 + struct event_trigger_data *data = trigger_data; 708 + struct event_filter *filter = NULL, *tmp; 709 + int ret = -EINVAL; 710 + char *s; 711 + 712 + if (!filter_str) /* clear the current filter */ 713 + goto assign; 714 + 715 + s = strsep(&filter_str, " \t"); 716 + 717 + if (!strlen(s) || strcmp(s, "if") != 0) 718 + goto out; 719 + 720 + if (!filter_str) 721 + goto out; 722 + 723 + /* The filter is for the 'trigger' event, not the triggered event */ 724 + ret = create_event_filter(file->event_call, filter_str, false, &filter); 725 + if (ret) 726 + goto out; 727 + assign: 728 + tmp = rcu_access_pointer(data->filter); 729 + 730 + rcu_assign_pointer(data->filter, filter); 731 + 732 + if (tmp) { 733 + /* Make sure the call is done with the filter */ 734 + synchronize_sched(); 735 + free_event_filter(tmp); 736 + } 737 + 738 + kfree(data->filter_str); 739 + data->filter_str = NULL; 740 + 741 + if (filter_str) { 742 + data->filter_str = kstrdup(filter_str, GFP_KERNEL); 743 + if (!data->filter_str) { 744 + free_event_filter(rcu_access_pointer(data->filter)); 745 + data->filter = NULL; 746 + ret = -ENOMEM; 747 + } 748 + } 749 + out: 750 + return ret; 751 + } 752 + 753 + static void 754 + traceon_trigger(struct event_trigger_data *data) 755 + { 756 + if (tracing_is_on()) 757 + return; 758 + 759 + tracing_on(); 760 + } 761 + 762 + static void 763 + traceon_count_trigger(struct event_trigger_data *data) 764 + { 765 + if (tracing_is_on()) 766 + return; 767 + 768 + if (!data->count) 769 + return; 770 + 771 + if (data->count != -1) 772 + (data->count)--; 773 + 774 + tracing_on(); 775 + } 776 + 777 + static void 778 + traceoff_trigger(struct event_trigger_data *data) 779 + { 780 + if (!tracing_is_on()) 781 + return; 782 + 783 + tracing_off(); 784 + } 785 + 786 + static void 787 + traceoff_count_trigger(struct event_trigger_data *data) 788 + { 789 + if (!tracing_is_on()) 790 + return; 791 + 792 + if (!data->count) 793 + return; 794 + 795 + if (data->count != -1) 796 + (data->count)--; 797 + 798 + tracing_off(); 799 + } 800 + 801 + static int 802 + traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, 803 + struct event_trigger_data *data) 804 + { 805 + return event_trigger_print("traceon", m, (void *)data->count, 806 + data->filter_str); 807 + } 808 + 809 + static int 810 + traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, 811 + struct event_trigger_data *data) 812 + { 813 + return event_trigger_print("traceoff", m, (void *)data->count, 814 + data->filter_str); 815 + } 816 + 817 + static struct event_trigger_ops traceon_trigger_ops = { 818 + .func = traceon_trigger, 819 + .print = traceon_trigger_print, 820 + .init = event_trigger_init, 821 + .free = event_trigger_free, 822 + }; 823 + 824 + static struct event_trigger_ops traceon_count_trigger_ops = { 825 + .func = traceon_count_trigger, 826 + .print = traceon_trigger_print, 827 + .init = event_trigger_init, 828 + .free = event_trigger_free, 829 + }; 830 + 831 + static struct event_trigger_ops traceoff_trigger_ops = { 832 + .func = traceoff_trigger, 833 + .print = traceoff_trigger_print, 834 + .init = event_trigger_init, 835 + .free = event_trigger_free, 836 + }; 837 + 838 + static struct event_trigger_ops traceoff_count_trigger_ops = { 839 + .func = traceoff_count_trigger, 840 + .print = traceoff_trigger_print, 841 + .init = event_trigger_init, 842 + .free = event_trigger_free, 843 + }; 844 + 845 + static struct event_trigger_ops * 846 + onoff_get_trigger_ops(char *cmd, char *param) 847 + { 848 + struct event_trigger_ops *ops; 849 + 850 + /* we register both traceon and traceoff to this callback */ 851 + if (strcmp(cmd, "traceon") == 0) 852 + ops = param ? &traceon_count_trigger_ops : 853 + &traceon_trigger_ops; 854 + else 855 + ops = param ? &traceoff_count_trigger_ops : 856 + &traceoff_trigger_ops; 857 + 858 + return ops; 859 + } 860 + 861 + static struct event_command trigger_traceon_cmd = { 862 + .name = "traceon", 863 + .trigger_type = ETT_TRACE_ONOFF, 864 + .func = event_trigger_callback, 865 + .reg = register_trigger, 866 + .unreg = unregister_trigger, 867 + .get_trigger_ops = onoff_get_trigger_ops, 868 + .set_filter = set_trigger_filter, 869 + }; 870 + 871 + static struct event_command trigger_traceoff_cmd = { 872 + .name = "traceoff", 873 + .trigger_type = ETT_TRACE_ONOFF, 874 + .func = event_trigger_callback, 875 + .reg = register_trigger, 876 + .unreg = unregister_trigger, 877 + .get_trigger_ops = onoff_get_trigger_ops, 878 + .set_filter = set_trigger_filter, 879 + }; 880 + 881 + #ifdef CONFIG_TRACER_SNAPSHOT 882 + static void 883 + snapshot_trigger(struct event_trigger_data *data) 884 + { 885 + tracing_snapshot(); 886 + } 887 + 888 + static void 889 + snapshot_count_trigger(struct event_trigger_data *data) 890 + { 891 + if (!data->count) 892 + return; 893 + 894 + if (data->count != -1) 895 + (data->count)--; 896 + 897 + snapshot_trigger(data); 898 + } 899 + 900 + static int 901 + register_snapshot_trigger(char *glob, struct event_trigger_ops *ops, 902 + struct event_trigger_data *data, 903 + struct ftrace_event_file *file) 904 + { 905 + int ret = register_trigger(glob, ops, data, file); 906 + 907 + if (ret > 0 && tracing_alloc_snapshot() != 0) { 908 + unregister_trigger(glob, ops, data, file); 909 + ret = 0; 910 + } 911 + 912 + return ret; 913 + } 914 + 915 + static int 916 + snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, 917 + struct event_trigger_data *data) 918 + { 919 + return event_trigger_print("snapshot", m, (void *)data->count, 920 + data->filter_str); 921 + } 922 + 923 + static struct event_trigger_ops snapshot_trigger_ops = { 924 + .func = snapshot_trigger, 925 + .print = snapshot_trigger_print, 926 + .init = event_trigger_init, 927 + .free = event_trigger_free, 928 + }; 929 + 930 + static struct event_trigger_ops snapshot_count_trigger_ops = { 931 + .func = snapshot_count_trigger, 932 + .print = snapshot_trigger_print, 933 + .init = event_trigger_init, 934 + .free = event_trigger_free, 935 + }; 936 + 937 + static struct event_trigger_ops * 938 + snapshot_get_trigger_ops(char *cmd, char *param) 939 + { 940 + return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops; 941 + } 942 + 943 + static struct event_command trigger_snapshot_cmd = { 944 + .name = "snapshot", 945 + .trigger_type = ETT_SNAPSHOT, 946 + .func = event_trigger_callback, 947 + .reg = register_snapshot_trigger, 948 + .unreg = unregister_trigger, 949 + .get_trigger_ops = snapshot_get_trigger_ops, 950 + .set_filter = set_trigger_filter, 951 + }; 952 + 953 + static __init int register_trigger_snapshot_cmd(void) 954 + { 955 + int ret; 956 + 957 + ret = register_event_command(&trigger_snapshot_cmd); 958 + WARN_ON(ret < 0); 959 + 960 + return ret; 961 + } 962 + #else 963 + static __init int register_trigger_snapshot_cmd(void) { return 0; } 964 + #endif /* CONFIG_TRACER_SNAPSHOT */ 965 + 966 + #ifdef CONFIG_STACKTRACE 967 + /* 968 + * Skip 3: 969 + * stacktrace_trigger() 970 + * event_triggers_post_call() 971 + * ftrace_raw_event_xxx() 972 + */ 973 + #define STACK_SKIP 3 974 + 975 + static void 976 + stacktrace_trigger(struct event_trigger_data *data) 977 + { 978 + trace_dump_stack(STACK_SKIP); 979 + } 980 + 981 + static void 982 + stacktrace_count_trigger(struct event_trigger_data *data) 983 + { 984 + if (!data->count) 985 + return; 986 + 987 + if (data->count != -1) 988 + (data->count)--; 989 + 990 + stacktrace_trigger(data); 991 + } 992 + 993 + static int 994 + stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, 995 + struct event_trigger_data *data) 996 + { 997 + return event_trigger_print("stacktrace", m, (void *)data->count, 998 + data->filter_str); 999 + } 1000 + 1001 + static struct event_trigger_ops stacktrace_trigger_ops = { 1002 + .func = stacktrace_trigger, 1003 + .print = stacktrace_trigger_print, 1004 + .init = event_trigger_init, 1005 + .free = event_trigger_free, 1006 + }; 1007 + 1008 + static struct event_trigger_ops stacktrace_count_trigger_ops = { 1009 + .func = stacktrace_count_trigger, 1010 + .print = stacktrace_trigger_print, 1011 + .init = event_trigger_init, 1012 + .free = event_trigger_free, 1013 + }; 1014 + 1015 + static struct event_trigger_ops * 1016 + stacktrace_get_trigger_ops(char *cmd, char *param) 1017 + { 1018 + return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops; 1019 + } 1020 + 1021 + static struct event_command trigger_stacktrace_cmd = { 1022 + .name = "stacktrace", 1023 + .trigger_type = ETT_STACKTRACE, 1024 + .post_trigger = true, 1025 + .func = event_trigger_callback, 1026 + .reg = register_trigger, 1027 + .unreg = unregister_trigger, 1028 + .get_trigger_ops = stacktrace_get_trigger_ops, 1029 + .set_filter = set_trigger_filter, 1030 + }; 1031 + 1032 + static __init int register_trigger_stacktrace_cmd(void) 1033 + { 1034 + int ret; 1035 + 1036 + ret = register_event_command(&trigger_stacktrace_cmd); 1037 + WARN_ON(ret < 0); 1038 + 1039 + return ret; 1040 + } 1041 + #else 1042 + static __init int register_trigger_stacktrace_cmd(void) { return 0; } 1043 + #endif /* CONFIG_STACKTRACE */ 1044 + 1045 + static __init void unregister_trigger_traceon_traceoff_cmds(void) 1046 + { 1047 + unregister_event_command(&trigger_traceon_cmd); 1048 + unregister_event_command(&trigger_traceoff_cmd); 1049 + } 1050 + 1051 + /* Avoid typos */ 1052 + #define ENABLE_EVENT_STR "enable_event" 1053 + #define DISABLE_EVENT_STR "disable_event" 1054 + 1055 + struct enable_trigger_data { 1056 + struct ftrace_event_file *file; 1057 + bool enable; 1058 + }; 1059 + 1060 + static void 1061 + event_enable_trigger(struct event_trigger_data *data) 1062 + { 1063 + struct enable_trigger_data *enable_data = data->private_data; 1064 + 1065 + if (enable_data->enable) 1066 + clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1067 + else 1068 + set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &enable_data->file->flags); 1069 + } 1070 + 1071 + static void 1072 + event_enable_count_trigger(struct event_trigger_data *data) 1073 + { 1074 + struct enable_trigger_data *enable_data = data->private_data; 1075 + 1076 + if (!data->count) 1077 + return; 1078 + 1079 + /* Skip if the event is in a state we want to switch to */ 1080 + if (enable_data->enable == !(enable_data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED)) 1081 + return; 1082 + 1083 + if (data->count != -1) 1084 + (data->count)--; 1085 + 1086 + event_enable_trigger(data); 1087 + } 1088 + 1089 + static int 1090 + event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops, 1091 + struct event_trigger_data *data) 1092 + { 1093 + struct enable_trigger_data *enable_data = data->private_data; 1094 + 1095 + seq_printf(m, "%s:%s:%s", 1096 + enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR, 1097 + enable_data->file->event_call->class->system, 1098 + enable_data->file->event_call->name); 1099 + 1100 + if (data->count == -1) 1101 + seq_puts(m, ":unlimited"); 1102 + else 1103 + seq_printf(m, ":count=%ld", data->count); 1104 + 1105 + if (data->filter_str) 1106 + seq_printf(m, " if %s\n", data->filter_str); 1107 + else 1108 + seq_puts(m, "\n"); 1109 + 1110 + return 0; 1111 + } 1112 + 1113 + static void 1114 + event_enable_trigger_free(struct event_trigger_ops *ops, 1115 + struct event_trigger_data *data) 1116 + { 1117 + struct enable_trigger_data *enable_data = data->private_data; 1118 + 1119 + if (WARN_ON_ONCE(data->ref <= 0)) 1120 + return; 1121 + 1122 + data->ref--; 1123 + if (!data->ref) { 1124 + /* Remove the SOFT_MODE flag */ 1125 + trace_event_enable_disable(enable_data->file, 0, 1); 1126 + module_put(enable_data->file->event_call->mod); 1127 + trigger_data_free(data); 1128 + kfree(enable_data); 1129 + } 1130 + } 1131 + 1132 + static struct event_trigger_ops event_enable_trigger_ops = { 1133 + .func = event_enable_trigger, 1134 + .print = event_enable_trigger_print, 1135 + .init = event_trigger_init, 1136 + .free = event_enable_trigger_free, 1137 + }; 1138 + 1139 + static struct event_trigger_ops event_enable_count_trigger_ops = { 1140 + .func = event_enable_count_trigger, 1141 + .print = event_enable_trigger_print, 1142 + .init = event_trigger_init, 1143 + .free = event_enable_trigger_free, 1144 + }; 1145 + 1146 + static struct event_trigger_ops event_disable_trigger_ops = { 1147 + .func = event_enable_trigger, 1148 + .print = event_enable_trigger_print, 1149 + .init = event_trigger_init, 1150 + .free = event_enable_trigger_free, 1151 + }; 1152 + 1153 + static struct event_trigger_ops event_disable_count_trigger_ops = { 1154 + .func = event_enable_count_trigger, 1155 + .print = event_enable_trigger_print, 1156 + .init = event_trigger_init, 1157 + .free = event_enable_trigger_free, 1158 + }; 1159 + 1160 + static int 1161 + event_enable_trigger_func(struct event_command *cmd_ops, 1162 + struct ftrace_event_file *file, 1163 + char *glob, char *cmd, char *param) 1164 + { 1165 + struct ftrace_event_file *event_enable_file; 1166 + struct enable_trigger_data *enable_data; 1167 + struct event_trigger_data *trigger_data; 1168 + struct event_trigger_ops *trigger_ops; 1169 + struct trace_array *tr = file->tr; 1170 + const char *system; 1171 + const char *event; 1172 + char *trigger; 1173 + char *number; 1174 + bool enable; 1175 + int ret; 1176 + 1177 + if (!param) 1178 + return -EINVAL; 1179 + 1180 + /* separate the trigger from the filter (s:e:n [if filter]) */ 1181 + trigger = strsep(&param, " \t"); 1182 + if (!trigger) 1183 + return -EINVAL; 1184 + 1185 + system = strsep(&trigger, ":"); 1186 + if (!trigger) 1187 + return -EINVAL; 1188 + 1189 + event = strsep(&trigger, ":"); 1190 + 1191 + ret = -EINVAL; 1192 + event_enable_file = find_event_file(tr, system, event); 1193 + if (!event_enable_file) 1194 + goto out; 1195 + 1196 + enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 1197 + 1198 + trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); 1199 + 1200 + ret = -ENOMEM; 1201 + trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); 1202 + if (!trigger_data) 1203 + goto out; 1204 + 1205 + enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL); 1206 + if (!enable_data) { 1207 + kfree(trigger_data); 1208 + goto out; 1209 + } 1210 + 1211 + trigger_data->count = -1; 1212 + trigger_data->ops = trigger_ops; 1213 + trigger_data->cmd_ops = cmd_ops; 1214 + INIT_LIST_HEAD(&trigger_data->list); 1215 + RCU_INIT_POINTER(trigger_data->filter, NULL); 1216 + 1217 + enable_data->enable = enable; 1218 + enable_data->file = event_enable_file; 1219 + trigger_data->private_data = enable_data; 1220 + 1221 + if (glob[0] == '!') { 1222 + cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); 1223 + kfree(trigger_data); 1224 + kfree(enable_data); 1225 + ret = 0; 1226 + goto out; 1227 + } 1228 + 1229 + if (trigger) { 1230 + number = strsep(&trigger, ":"); 1231 + 1232 + ret = -EINVAL; 1233 + if (!strlen(number)) 1234 + goto out_free; 1235 + 1236 + /* 1237 + * We use the callback data field (which is a pointer) 1238 + * as our counter. 1239 + */ 1240 + ret = kstrtoul(number, 0, &trigger_data->count); 1241 + if (ret) 1242 + goto out_free; 1243 + } 1244 + 1245 + if (!param) /* if param is non-empty, it's supposed to be a filter */ 1246 + goto out_reg; 1247 + 1248 + if (!cmd_ops->set_filter) 1249 + goto out_reg; 1250 + 1251 + ret = cmd_ops->set_filter(param, trigger_data, file); 1252 + if (ret < 0) 1253 + goto out_free; 1254 + 1255 + out_reg: 1256 + /* Don't let event modules unload while probe registered */ 1257 + ret = try_module_get(event_enable_file->event_call->mod); 1258 + if (!ret) { 1259 + ret = -EBUSY; 1260 + goto out_free; 1261 + } 1262 + 1263 + ret = trace_event_enable_disable(event_enable_file, 1, 1); 1264 + if (ret < 0) 1265 + goto out_put; 1266 + ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); 1267 + /* 1268 + * The above returns on success the # of functions enabled, 1269 + * but if it didn't find any functions it returns zero. 1270 + * Consider no functions a failure too. 1271 + */ 1272 + if (!ret) { 1273 + ret = -ENOENT; 1274 + goto out_disable; 1275 + } else if (ret < 0) 1276 + goto out_disable; 1277 + /* Just return zero, not the number of enabled functions */ 1278 + ret = 0; 1279 + out: 1280 + return ret; 1281 + 1282 + out_disable: 1283 + trace_event_enable_disable(event_enable_file, 0, 1); 1284 + out_put: 1285 + module_put(event_enable_file->event_call->mod); 1286 + out_free: 1287 + if (cmd_ops->set_filter) 1288 + cmd_ops->set_filter(NULL, trigger_data, NULL); 1289 + kfree(trigger_data); 1290 + kfree(enable_data); 1291 + goto out; 1292 + } 1293 + 1294 + static int event_enable_register_trigger(char *glob, 1295 + struct event_trigger_ops *ops, 1296 + struct event_trigger_data *data, 1297 + struct ftrace_event_file *file) 1298 + { 1299 + struct enable_trigger_data *enable_data = data->private_data; 1300 + struct enable_trigger_data *test_enable_data; 1301 + struct event_trigger_data *test; 1302 + int ret = 0; 1303 + 1304 + list_for_each_entry_rcu(test, &file->triggers, list) { 1305 + test_enable_data = test->private_data; 1306 + if (test_enable_data && 1307 + (test_enable_data->file == enable_data->file)) { 1308 + ret = -EEXIST; 1309 + goto out; 1310 + } 1311 + } 1312 + 1313 + if (data->ops->init) { 1314 + ret = data->ops->init(data->ops, data); 1315 + if (ret < 0) 1316 + goto out; 1317 + } 1318 + 1319 + list_add_rcu(&data->list, &file->triggers); 1320 + ret++; 1321 + 1322 + if (trace_event_trigger_enable_disable(file, 1) < 0) { 1323 + list_del_rcu(&data->list); 1324 + ret--; 1325 + } 1326 + update_cond_flag(file); 1327 + out: 1328 + return ret; 1329 + } 1330 + 1331 + static void event_enable_unregister_trigger(char *glob, 1332 + struct event_trigger_ops *ops, 1333 + struct event_trigger_data *test, 1334 + struct ftrace_event_file *file) 1335 + { 1336 + struct enable_trigger_data *test_enable_data = test->private_data; 1337 + struct enable_trigger_data *enable_data; 1338 + struct event_trigger_data *data; 1339 + bool unregistered = false; 1340 + 1341 + list_for_each_entry_rcu(data, &file->triggers, list) { 1342 + enable_data = data->private_data; 1343 + if (enable_data && 1344 + (enable_data->file == test_enable_data->file)) { 1345 + unregistered = true; 1346 + list_del_rcu(&data->list); 1347 + update_cond_flag(file); 1348 + trace_event_trigger_enable_disable(file, 0); 1349 + break; 1350 + } 1351 + } 1352 + 1353 + if (unregistered && data->ops->free) 1354 + data->ops->free(data->ops, data); 1355 + } 1356 + 1357 + static struct event_trigger_ops * 1358 + event_enable_get_trigger_ops(char *cmd, char *param) 1359 + { 1360 + struct event_trigger_ops *ops; 1361 + bool enable; 1362 + 1363 + enable = strcmp(cmd, ENABLE_EVENT_STR) == 0; 1364 + 1365 + if (enable) 1366 + ops = param ? &event_enable_count_trigger_ops : 1367 + &event_enable_trigger_ops; 1368 + else 1369 + ops = param ? &event_disable_count_trigger_ops : 1370 + &event_disable_trigger_ops; 1371 + 1372 + return ops; 1373 + } 1374 + 1375 + static struct event_command trigger_enable_cmd = { 1376 + .name = ENABLE_EVENT_STR, 1377 + .trigger_type = ETT_EVENT_ENABLE, 1378 + .func = event_enable_trigger_func, 1379 + .reg = event_enable_register_trigger, 1380 + .unreg = event_enable_unregister_trigger, 1381 + .get_trigger_ops = event_enable_get_trigger_ops, 1382 + .set_filter = set_trigger_filter, 1383 + }; 1384 + 1385 + static struct event_command trigger_disable_cmd = { 1386 + .name = DISABLE_EVENT_STR, 1387 + .trigger_type = ETT_EVENT_ENABLE, 1388 + .func = event_enable_trigger_func, 1389 + .reg = event_enable_register_trigger, 1390 + .unreg = event_enable_unregister_trigger, 1391 + .get_trigger_ops = event_enable_get_trigger_ops, 1392 + .set_filter = set_trigger_filter, 1393 + }; 1394 + 1395 + static __init void unregister_trigger_enable_disable_cmds(void) 1396 + { 1397 + unregister_event_command(&trigger_enable_cmd); 1398 + unregister_event_command(&trigger_disable_cmd); 1399 + } 1400 + 1401 + static __init int register_trigger_enable_disable_cmds(void) 1402 + { 1403 + int ret; 1404 + 1405 + ret = register_event_command(&trigger_enable_cmd); 1406 + if (WARN_ON(ret < 0)) 1407 + return ret; 1408 + ret = register_event_command(&trigger_disable_cmd); 1409 + if (WARN_ON(ret < 0)) 1410 + unregister_trigger_enable_disable_cmds(); 1411 + 1412 + return ret; 1413 + } 1414 + 1415 + static __init int register_trigger_traceon_traceoff_cmds(void) 1416 + { 1417 + int ret; 1418 + 1419 + ret = register_event_command(&trigger_traceon_cmd); 1420 + if (WARN_ON(ret < 0)) 1421 + return ret; 1422 + ret = register_event_command(&trigger_traceoff_cmd); 1423 + if (WARN_ON(ret < 0)) 1424 + unregister_trigger_traceon_traceoff_cmds(); 1425 + 1426 + return ret; 1427 + } 1428 + 1429 + __init int register_trigger_cmds(void) 1430 + { 1431 + register_trigger_traceon_traceoff_cmds(); 1432 + register_trigger_snapshot_cmd(); 1433 + register_trigger_stacktrace_cmd(); 1434 + register_trigger_enable_disable_cmds(); 1435 + 1436 + return 0; 1437 + }
+448 -390
kernel/trace/trace_kprobe.c
··· 27 27 /** 28 28 * Kprobe event core functions 29 29 */ 30 - struct trace_probe { 30 + struct trace_kprobe { 31 31 struct list_head list; 32 32 struct kretprobe rp; /* Use rp.kp for kprobe use */ 33 33 unsigned long nhit; 34 - unsigned int flags; /* For TP_FLAG_* */ 35 34 const char *symbol; /* symbol name */ 36 - struct ftrace_event_class class; 37 - struct ftrace_event_call call; 38 - struct list_head files; 39 - ssize_t size; /* trace entry size */ 40 - unsigned int nr_args; 41 - struct probe_arg args[]; 35 + struct trace_probe tp; 42 36 }; 43 37 44 38 struct event_file_link { ··· 40 46 struct list_head list; 41 47 }; 42 48 43 - #define SIZEOF_TRACE_PROBE(n) \ 44 - (offsetof(struct trace_probe, args) + \ 49 + #define SIZEOF_TRACE_KPROBE(n) \ 50 + (offsetof(struct trace_kprobe, tp.args) + \ 45 51 (sizeof(struct probe_arg) * (n))) 46 52 47 53 48 - static __kprobes bool trace_probe_is_return(struct trace_probe *tp) 54 + static __kprobes bool trace_kprobe_is_return(struct trace_kprobe *tk) 49 55 { 50 - return tp->rp.handler != NULL; 56 + return tk->rp.handler != NULL; 51 57 } 52 58 53 - static __kprobes const char *trace_probe_symbol(struct trace_probe *tp) 59 + static __kprobes const char *trace_kprobe_symbol(struct trace_kprobe *tk) 54 60 { 55 - return tp->symbol ? tp->symbol : "unknown"; 61 + return tk->symbol ? tk->symbol : "unknown"; 56 62 } 57 63 58 - static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp) 64 + static __kprobes unsigned long trace_kprobe_offset(struct trace_kprobe *tk) 59 65 { 60 - return tp->rp.kp.offset; 66 + return tk->rp.kp.offset; 61 67 } 62 68 63 - static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp) 69 + static __kprobes bool trace_kprobe_has_gone(struct trace_kprobe *tk) 64 70 { 65 - return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE)); 71 + return !!(kprobe_gone(&tk->rp.kp)); 66 72 } 67 73 68 - static __kprobes bool trace_probe_is_registered(struct trace_probe *tp) 69 - { 70 - return !!(tp->flags & TP_FLAG_REGISTERED); 71 - } 72 - 73 - static __kprobes bool trace_probe_has_gone(struct trace_probe *tp) 74 - { 75 - return !!(kprobe_gone(&tp->rp.kp)); 76 - } 77 - 78 - static __kprobes bool trace_probe_within_module(struct trace_probe *tp, 79 - struct module *mod) 74 + static __kprobes bool trace_kprobe_within_module(struct trace_kprobe *tk, 75 + struct module *mod) 80 76 { 81 77 int len = strlen(mod->name); 82 - const char *name = trace_probe_symbol(tp); 78 + const char *name = trace_kprobe_symbol(tk); 83 79 return strncmp(mod->name, name, len) == 0 && name[len] == ':'; 84 80 } 85 81 86 - static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp) 82 + static __kprobes bool trace_kprobe_is_on_module(struct trace_kprobe *tk) 87 83 { 88 - return !!strchr(trace_probe_symbol(tp), ':'); 84 + return !!strchr(trace_kprobe_symbol(tk), ':'); 89 85 } 90 86 91 - static int register_probe_event(struct trace_probe *tp); 92 - static int unregister_probe_event(struct trace_probe *tp); 87 + static int register_kprobe_event(struct trace_kprobe *tk); 88 + static int unregister_kprobe_event(struct trace_kprobe *tk); 93 89 94 90 static DEFINE_MUTEX(probe_lock); 95 91 static LIST_HEAD(probe_list); ··· 88 104 static int kretprobe_dispatcher(struct kretprobe_instance *ri, 89 105 struct pt_regs *regs); 90 106 107 + /* Memory fetching by symbol */ 108 + struct symbol_cache { 109 + char *symbol; 110 + long offset; 111 + unsigned long addr; 112 + }; 113 + 114 + unsigned long update_symbol_cache(struct symbol_cache *sc) 115 + { 116 + sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); 117 + 118 + if (sc->addr) 119 + sc->addr += sc->offset; 120 + 121 + return sc->addr; 122 + } 123 + 124 + void free_symbol_cache(struct symbol_cache *sc) 125 + { 126 + kfree(sc->symbol); 127 + kfree(sc); 128 + } 129 + 130 + struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) 131 + { 132 + struct symbol_cache *sc; 133 + 134 + if (!sym || strlen(sym) == 0) 135 + return NULL; 136 + 137 + sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); 138 + if (!sc) 139 + return NULL; 140 + 141 + sc->symbol = kstrdup(sym, GFP_KERNEL); 142 + if (!sc->symbol) { 143 + kfree(sc); 144 + return NULL; 145 + } 146 + sc->offset = offset; 147 + update_symbol_cache(sc); 148 + 149 + return sc; 150 + } 151 + 152 + /* 153 + * Kprobes-specific fetch functions 154 + */ 155 + #define DEFINE_FETCH_stack(type) \ 156 + static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ 157 + void *offset, void *dest) \ 158 + { \ 159 + *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \ 160 + (unsigned int)((unsigned long)offset)); \ 161 + } 162 + DEFINE_BASIC_FETCH_FUNCS(stack) 163 + /* No string on the stack entry */ 164 + #define fetch_stack_string NULL 165 + #define fetch_stack_string_size NULL 166 + 167 + #define DEFINE_FETCH_memory(type) \ 168 + static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ 169 + void *addr, void *dest) \ 170 + { \ 171 + type retval; \ 172 + if (probe_kernel_address(addr, retval)) \ 173 + *(type *)dest = 0; \ 174 + else \ 175 + *(type *)dest = retval; \ 176 + } 177 + DEFINE_BASIC_FETCH_FUNCS(memory) 178 + /* 179 + * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max 180 + * length and relative data location. 181 + */ 182 + static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, 183 + void *addr, void *dest) 184 + { 185 + long ret; 186 + int maxlen = get_rloc_len(*(u32 *)dest); 187 + u8 *dst = get_rloc_data(dest); 188 + u8 *src = addr; 189 + mm_segment_t old_fs = get_fs(); 190 + 191 + if (!maxlen) 192 + return; 193 + 194 + /* 195 + * Try to get string again, since the string can be changed while 196 + * probing. 197 + */ 198 + set_fs(KERNEL_DS); 199 + pagefault_disable(); 200 + 201 + do 202 + ret = __copy_from_user_inatomic(dst++, src++, 1); 203 + while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen); 204 + 205 + dst[-1] = '\0'; 206 + pagefault_enable(); 207 + set_fs(old_fs); 208 + 209 + if (ret < 0) { /* Failed to fetch string */ 210 + ((u8 *)get_rloc_data(dest))[0] = '\0'; 211 + *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest)); 212 + } else { 213 + *(u32 *)dest = make_data_rloc(src - (u8 *)addr, 214 + get_rloc_offs(*(u32 *)dest)); 215 + } 216 + } 217 + 218 + /* Return the length of string -- including null terminal byte */ 219 + static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs, 220 + void *addr, void *dest) 221 + { 222 + mm_segment_t old_fs; 223 + int ret, len = 0; 224 + u8 c; 225 + 226 + old_fs = get_fs(); 227 + set_fs(KERNEL_DS); 228 + pagefault_disable(); 229 + 230 + do { 231 + ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1); 232 + len++; 233 + } while (c && ret == 0 && len < MAX_STRING_SIZE); 234 + 235 + pagefault_enable(); 236 + set_fs(old_fs); 237 + 238 + if (ret < 0) /* Failed to check the length */ 239 + *(u32 *)dest = 0; 240 + else 241 + *(u32 *)dest = len; 242 + } 243 + 244 + #define DEFINE_FETCH_symbol(type) \ 245 + __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, \ 246 + void *data, void *dest) \ 247 + { \ 248 + struct symbol_cache *sc = data; \ 249 + if (sc->addr) \ 250 + fetch_memory_##type(regs, (void *)sc->addr, dest); \ 251 + else \ 252 + *(type *)dest = 0; \ 253 + } 254 + DEFINE_BASIC_FETCH_FUNCS(symbol) 255 + DEFINE_FETCH_symbol(string) 256 + DEFINE_FETCH_symbol(string_size) 257 + 258 + /* kprobes don't support file_offset fetch methods */ 259 + #define fetch_file_offset_u8 NULL 260 + #define fetch_file_offset_u16 NULL 261 + #define fetch_file_offset_u32 NULL 262 + #define fetch_file_offset_u64 NULL 263 + #define fetch_file_offset_string NULL 264 + #define fetch_file_offset_string_size NULL 265 + 266 + /* Fetch type information table */ 267 + const struct fetch_type kprobes_fetch_type_table[] = { 268 + /* Special types */ 269 + [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string, 270 + sizeof(u32), 1, "__data_loc char[]"), 271 + [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32, 272 + string_size, sizeof(u32), 0, "u32"), 273 + /* Basic types */ 274 + ASSIGN_FETCH_TYPE(u8, u8, 0), 275 + ASSIGN_FETCH_TYPE(u16, u16, 0), 276 + ASSIGN_FETCH_TYPE(u32, u32, 0), 277 + ASSIGN_FETCH_TYPE(u64, u64, 0), 278 + ASSIGN_FETCH_TYPE(s8, u8, 1), 279 + ASSIGN_FETCH_TYPE(s16, u16, 1), 280 + ASSIGN_FETCH_TYPE(s32, u32, 1), 281 + ASSIGN_FETCH_TYPE(s64, u64, 1), 282 + 283 + ASSIGN_FETCH_TYPE_END 284 + }; 285 + 91 286 /* 92 287 * Allocate new trace_probe and initialize it (including kprobes). 93 288 */ 94 - static struct trace_probe *alloc_trace_probe(const char *group, 289 + static struct trace_kprobe *alloc_trace_kprobe(const char *group, 95 290 const char *event, 96 291 void *addr, 97 292 const char *symbol, 98 293 unsigned long offs, 99 294 int nargs, bool is_return) 100 295 { 101 - struct trace_probe *tp; 296 + struct trace_kprobe *tk; 102 297 int ret = -ENOMEM; 103 298 104 - tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); 105 - if (!tp) 299 + tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL); 300 + if (!tk) 106 301 return ERR_PTR(ret); 107 302 108 303 if (symbol) { 109 - tp->symbol = kstrdup(symbol, GFP_KERNEL); 110 - if (!tp->symbol) 304 + tk->symbol = kstrdup(symbol, GFP_KERNEL); 305 + if (!tk->symbol) 111 306 goto error; 112 - tp->rp.kp.symbol_name = tp->symbol; 113 - tp->rp.kp.offset = offs; 307 + tk->rp.kp.symbol_name = tk->symbol; 308 + tk->rp.kp.offset = offs; 114 309 } else 115 - tp->rp.kp.addr = addr; 310 + tk->rp.kp.addr = addr; 116 311 117 312 if (is_return) 118 - tp->rp.handler = kretprobe_dispatcher; 313 + tk->rp.handler = kretprobe_dispatcher; 119 314 else 120 - tp->rp.kp.pre_handler = kprobe_dispatcher; 315 + tk->rp.kp.pre_handler = kprobe_dispatcher; 121 316 122 317 if (!event || !is_good_name(event)) { 123 318 ret = -EINVAL; 124 319 goto error; 125 320 } 126 321 127 - tp->call.class = &tp->class; 128 - tp->call.name = kstrdup(event, GFP_KERNEL); 129 - if (!tp->call.name) 322 + tk->tp.call.class = &tk->tp.class; 323 + tk->tp.call.name = kstrdup(event, GFP_KERNEL); 324 + if (!tk->tp.call.name) 130 325 goto error; 131 326 132 327 if (!group || !is_good_name(group)) { ··· 313 150 goto error; 314 151 } 315 152 316 - tp->class.system = kstrdup(group, GFP_KERNEL); 317 - if (!tp->class.system) 153 + tk->tp.class.system = kstrdup(group, GFP_KERNEL); 154 + if (!tk->tp.class.system) 318 155 goto error; 319 156 320 - INIT_LIST_HEAD(&tp->list); 321 - INIT_LIST_HEAD(&tp->files); 322 - return tp; 157 + INIT_LIST_HEAD(&tk->list); 158 + INIT_LIST_HEAD(&tk->tp.files); 159 + return tk; 323 160 error: 324 - kfree(tp->call.name); 325 - kfree(tp->symbol); 326 - kfree(tp); 161 + kfree(tk->tp.call.name); 162 + kfree(tk->symbol); 163 + kfree(tk); 327 164 return ERR_PTR(ret); 328 165 } 329 166 330 - static void free_trace_probe(struct trace_probe *tp) 167 + static void free_trace_kprobe(struct trace_kprobe *tk) 331 168 { 332 169 int i; 333 170 334 - for (i = 0; i < tp->nr_args; i++) 335 - traceprobe_free_probe_arg(&tp->args[i]); 171 + for (i = 0; i < tk->tp.nr_args; i++) 172 + traceprobe_free_probe_arg(&tk->tp.args[i]); 336 173 337 - kfree(tp->call.class->system); 338 - kfree(tp->call.name); 339 - kfree(tp->symbol); 340 - kfree(tp); 174 + kfree(tk->tp.call.class->system); 175 + kfree(tk->tp.call.name); 176 + kfree(tk->symbol); 177 + kfree(tk); 341 178 } 342 179 343 - static struct trace_probe *find_trace_probe(const char *event, 344 - const char *group) 180 + static struct trace_kprobe *find_trace_kprobe(const char *event, 181 + const char *group) 345 182 { 346 - struct trace_probe *tp; 183 + struct trace_kprobe *tk; 347 184 348 - list_for_each_entry(tp, &probe_list, list) 349 - if (strcmp(tp->call.name, event) == 0 && 350 - strcmp(tp->call.class->system, group) == 0) 351 - return tp; 185 + list_for_each_entry(tk, &probe_list, list) 186 + if (strcmp(tk->tp.call.name, event) == 0 && 187 + strcmp(tk->tp.call.class->system, group) == 0) 188 + return tk; 352 189 return NULL; 353 190 } 354 191 ··· 357 194 * if the file is NULL, enable "perf" handler, or enable "trace" handler. 358 195 */ 359 196 static int 360 - enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file) 197 + enable_trace_kprobe(struct trace_kprobe *tk, struct ftrace_event_file *file) 361 198 { 362 199 int ret = 0; 363 200 ··· 371 208 } 372 209 373 210 link->file = file; 374 - list_add_tail_rcu(&link->list, &tp->files); 211 + list_add_tail_rcu(&link->list, &tk->tp.files); 375 212 376 - tp->flags |= TP_FLAG_TRACE; 213 + tk->tp.flags |= TP_FLAG_TRACE; 377 214 } else 378 - tp->flags |= TP_FLAG_PROFILE; 215 + tk->tp.flags |= TP_FLAG_PROFILE; 379 216 380 - if (trace_probe_is_registered(tp) && !trace_probe_has_gone(tp)) { 381 - if (trace_probe_is_return(tp)) 382 - ret = enable_kretprobe(&tp->rp); 217 + if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) { 218 + if (trace_kprobe_is_return(tk)) 219 + ret = enable_kretprobe(&tk->rp); 383 220 else 384 - ret = enable_kprobe(&tp->rp.kp); 221 + ret = enable_kprobe(&tk->rp.kp); 385 222 } 386 223 out: 387 224 return ret; ··· 404 241 * if the file is NULL, disable "perf" handler, or disable "trace" handler. 405 242 */ 406 243 static int 407 - disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file) 244 + disable_trace_kprobe(struct trace_kprobe *tk, struct ftrace_event_file *file) 408 245 { 409 246 struct event_file_link *link = NULL; 410 247 int wait = 0; 411 248 int ret = 0; 412 249 413 250 if (file) { 414 - link = find_event_file_link(tp, file); 251 + link = find_event_file_link(&tk->tp, file); 415 252 if (!link) { 416 253 ret = -EINVAL; 417 254 goto out; ··· 419 256 420 257 list_del_rcu(&link->list); 421 258 wait = 1; 422 - if (!list_empty(&tp->files)) 259 + if (!list_empty(&tk->tp.files)) 423 260 goto out; 424 261 425 - tp->flags &= ~TP_FLAG_TRACE; 262 + tk->tp.flags &= ~TP_FLAG_TRACE; 426 263 } else 427 - tp->flags &= ~TP_FLAG_PROFILE; 264 + tk->tp.flags &= ~TP_FLAG_PROFILE; 428 265 429 - if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) { 430 - if (trace_probe_is_return(tp)) 431 - disable_kretprobe(&tp->rp); 266 + if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) { 267 + if (trace_kprobe_is_return(tk)) 268 + disable_kretprobe(&tk->rp); 432 269 else 433 - disable_kprobe(&tp->rp.kp); 270 + disable_kprobe(&tk->rp.kp); 434 271 wait = 1; 435 272 } 436 273 out: ··· 451 288 } 452 289 453 290 /* Internal register function - just handle k*probes and flags */ 454 - static int __register_trace_probe(struct trace_probe *tp) 291 + static int __register_trace_kprobe(struct trace_kprobe *tk) 455 292 { 456 293 int i, ret; 457 294 458 - if (trace_probe_is_registered(tp)) 295 + if (trace_probe_is_registered(&tk->tp)) 459 296 return -EINVAL; 460 297 461 - for (i = 0; i < tp->nr_args; i++) 462 - traceprobe_update_arg(&tp->args[i]); 298 + for (i = 0; i < tk->tp.nr_args; i++) 299 + traceprobe_update_arg(&tk->tp.args[i]); 463 300 464 301 /* Set/clear disabled flag according to tp->flag */ 465 - if (trace_probe_is_enabled(tp)) 466 - tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; 302 + if (trace_probe_is_enabled(&tk->tp)) 303 + tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; 467 304 else 468 - tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; 305 + tk->rp.kp.flags |= KPROBE_FLAG_DISABLED; 469 306 470 - if (trace_probe_is_return(tp)) 471 - ret = register_kretprobe(&tp->rp); 307 + if (trace_kprobe_is_return(tk)) 308 + ret = register_kretprobe(&tk->rp); 472 309 else 473 - ret = register_kprobe(&tp->rp.kp); 310 + ret = register_kprobe(&tk->rp.kp); 474 311 475 312 if (ret == 0) 476 - tp->flags |= TP_FLAG_REGISTERED; 313 + tk->tp.flags |= TP_FLAG_REGISTERED; 477 314 else { 478 315 pr_warning("Could not insert probe at %s+%lu: %d\n", 479 - trace_probe_symbol(tp), trace_probe_offset(tp), ret); 480 - if (ret == -ENOENT && trace_probe_is_on_module(tp)) { 316 + trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret); 317 + if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) { 481 318 pr_warning("This probe might be able to register after" 482 319 "target module is loaded. Continue.\n"); 483 320 ret = 0; 484 321 } else if (ret == -EILSEQ) { 485 322 pr_warning("Probing address(0x%p) is not an " 486 323 "instruction boundary.\n", 487 - tp->rp.kp.addr); 324 + tk->rp.kp.addr); 488 325 ret = -EINVAL; 489 326 } 490 327 } ··· 493 330 } 494 331 495 332 /* Internal unregister function - just handle k*probes and flags */ 496 - static void __unregister_trace_probe(struct trace_probe *tp) 333 + static void __unregister_trace_kprobe(struct trace_kprobe *tk) 497 334 { 498 - if (trace_probe_is_registered(tp)) { 499 - if (trace_probe_is_return(tp)) 500 - unregister_kretprobe(&tp->rp); 335 + if (trace_probe_is_registered(&tk->tp)) { 336 + if (trace_kprobe_is_return(tk)) 337 + unregister_kretprobe(&tk->rp); 501 338 else 502 - unregister_kprobe(&tp->rp.kp); 503 - tp->flags &= ~TP_FLAG_REGISTERED; 339 + unregister_kprobe(&tk->rp.kp); 340 + tk->tp.flags &= ~TP_FLAG_REGISTERED; 504 341 /* Cleanup kprobe for reuse */ 505 - if (tp->rp.kp.symbol_name) 506 - tp->rp.kp.addr = NULL; 342 + if (tk->rp.kp.symbol_name) 343 + tk->rp.kp.addr = NULL; 507 344 } 508 345 } 509 346 510 347 /* Unregister a trace_probe and probe_event: call with locking probe_lock */ 511 - static int unregister_trace_probe(struct trace_probe *tp) 348 + static int unregister_trace_kprobe(struct trace_kprobe *tk) 512 349 { 513 350 /* Enabled event can not be unregistered */ 514 - if (trace_probe_is_enabled(tp)) 351 + if (trace_probe_is_enabled(&tk->tp)) 515 352 return -EBUSY; 516 353 517 354 /* Will fail if probe is being used by ftrace or perf */ 518 - if (unregister_probe_event(tp)) 355 + if (unregister_kprobe_event(tk)) 519 356 return -EBUSY; 520 357 521 - __unregister_trace_probe(tp); 522 - list_del(&tp->list); 358 + __unregister_trace_kprobe(tk); 359 + list_del(&tk->list); 523 360 524 361 return 0; 525 362 } 526 363 527 364 /* Register a trace_probe and probe_event */ 528 - static int register_trace_probe(struct trace_probe *tp) 365 + static int register_trace_kprobe(struct trace_kprobe *tk) 529 366 { 530 - struct trace_probe *old_tp; 367 + struct trace_kprobe *old_tk; 531 368 int ret; 532 369 533 370 mutex_lock(&probe_lock); 534 371 535 372 /* Delete old (same name) event if exist */ 536 - old_tp = find_trace_probe(tp->call.name, tp->call.class->system); 537 - if (old_tp) { 538 - ret = unregister_trace_probe(old_tp); 373 + old_tk = find_trace_kprobe(tk->tp.call.name, tk->tp.call.class->system); 374 + if (old_tk) { 375 + ret = unregister_trace_kprobe(old_tk); 539 376 if (ret < 0) 540 377 goto end; 541 - free_trace_probe(old_tp); 378 + free_trace_kprobe(old_tk); 542 379 } 543 380 544 381 /* Register new event */ 545 - ret = register_probe_event(tp); 382 + ret = register_kprobe_event(tk); 546 383 if (ret) { 547 384 pr_warning("Failed to register probe event(%d)\n", ret); 548 385 goto end; 549 386 } 550 387 551 388 /* Register k*probe */ 552 - ret = __register_trace_probe(tp); 389 + ret = __register_trace_kprobe(tk); 553 390 if (ret < 0) 554 - unregister_probe_event(tp); 391 + unregister_kprobe_event(tk); 555 392 else 556 - list_add_tail(&tp->list, &probe_list); 393 + list_add_tail(&tk->list, &probe_list); 557 394 558 395 end: 559 396 mutex_unlock(&probe_lock); ··· 561 398 } 562 399 563 400 /* Module notifier call back, checking event on the module */ 564 - static int trace_probe_module_callback(struct notifier_block *nb, 401 + static int trace_kprobe_module_callback(struct notifier_block *nb, 565 402 unsigned long val, void *data) 566 403 { 567 404 struct module *mod = data; 568 - struct trace_probe *tp; 405 + struct trace_kprobe *tk; 569 406 int ret; 570 407 571 408 if (val != MODULE_STATE_COMING) ··· 573 410 574 411 /* Update probes on coming module */ 575 412 mutex_lock(&probe_lock); 576 - list_for_each_entry(tp, &probe_list, list) { 577 - if (trace_probe_within_module(tp, mod)) { 413 + list_for_each_entry(tk, &probe_list, list) { 414 + if (trace_kprobe_within_module(tk, mod)) { 578 415 /* Don't need to check busy - this should have gone. */ 579 - __unregister_trace_probe(tp); 580 - ret = __register_trace_probe(tp); 416 + __unregister_trace_kprobe(tk); 417 + ret = __register_trace_kprobe(tk); 581 418 if (ret) 582 419 pr_warning("Failed to re-register probe %s on" 583 420 "%s: %d\n", 584 - tp->call.name, mod->name, ret); 421 + tk->tp.call.name, mod->name, ret); 585 422 } 586 423 } 587 424 mutex_unlock(&probe_lock); ··· 589 426 return NOTIFY_DONE; 590 427 } 591 428 592 - static struct notifier_block trace_probe_module_nb = { 593 - .notifier_call = trace_probe_module_callback, 429 + static struct notifier_block trace_kprobe_module_nb = { 430 + .notifier_call = trace_kprobe_module_callback, 594 431 .priority = 1 /* Invoked after kprobe module callback */ 595 432 }; 596 433 597 - static int create_trace_probe(int argc, char **argv) 434 + static int create_trace_kprobe(int argc, char **argv) 598 435 { 599 436 /* 600 437 * Argument syntax: ··· 614 451 * Type of args: 615 452 * FETCHARG:TYPE : use TYPE instead of unsigned long. 616 453 */ 617 - struct trace_probe *tp; 454 + struct trace_kprobe *tk; 618 455 int i, ret = 0; 619 456 bool is_return = false, is_delete = false; 620 457 char *symbol = NULL, *event = NULL, *group = NULL; ··· 661 498 return -EINVAL; 662 499 } 663 500 mutex_lock(&probe_lock); 664 - tp = find_trace_probe(event, group); 665 - if (!tp) { 501 + tk = find_trace_kprobe(event, group); 502 + if (!tk) { 666 503 mutex_unlock(&probe_lock); 667 504 pr_info("Event %s/%s doesn't exist.\n", group, event); 668 505 return -ENOENT; 669 506 } 670 507 /* delete an event */ 671 - ret = unregister_trace_probe(tp); 508 + ret = unregister_trace_kprobe(tk); 672 509 if (ret == 0) 673 - free_trace_probe(tp); 510 + free_trace_kprobe(tk); 674 511 mutex_unlock(&probe_lock); 675 512 return ret; 676 513 } ··· 717 554 is_return ? 'r' : 'p', addr); 718 555 event = buf; 719 556 } 720 - tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, 557 + tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc, 721 558 is_return); 722 - if (IS_ERR(tp)) { 559 + if (IS_ERR(tk)) { 723 560 pr_info("Failed to allocate trace_probe.(%d)\n", 724 - (int)PTR_ERR(tp)); 725 - return PTR_ERR(tp); 561 + (int)PTR_ERR(tk)); 562 + return PTR_ERR(tk); 726 563 } 727 564 728 565 /* parse arguments */ 729 566 ret = 0; 730 567 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { 568 + struct probe_arg *parg = &tk->tp.args[i]; 569 + 731 570 /* Increment count for freeing args in error case */ 732 - tp->nr_args++; 571 + tk->tp.nr_args++; 733 572 734 573 /* Parse argument name */ 735 574 arg = strchr(argv[i], '='); 736 575 if (arg) { 737 576 *arg++ = '\0'; 738 - tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); 577 + parg->name = kstrdup(argv[i], GFP_KERNEL); 739 578 } else { 740 579 arg = argv[i]; 741 580 /* If argument name is omitted, set "argN" */ 742 581 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); 743 - tp->args[i].name = kstrdup(buf, GFP_KERNEL); 582 + parg->name = kstrdup(buf, GFP_KERNEL); 744 583 } 745 584 746 - if (!tp->args[i].name) { 585 + if (!parg->name) { 747 586 pr_info("Failed to allocate argument[%d] name.\n", i); 748 587 ret = -ENOMEM; 749 588 goto error; 750 589 } 751 590 752 - if (!is_good_name(tp->args[i].name)) { 591 + if (!is_good_name(parg->name)) { 753 592 pr_info("Invalid argument[%d] name: %s\n", 754 - i, tp->args[i].name); 593 + i, parg->name); 755 594 ret = -EINVAL; 756 595 goto error; 757 596 } 758 597 759 - if (traceprobe_conflict_field_name(tp->args[i].name, 760 - tp->args, i)) { 598 + if (traceprobe_conflict_field_name(parg->name, 599 + tk->tp.args, i)) { 761 600 pr_info("Argument[%d] name '%s' conflicts with " 762 601 "another field.\n", i, argv[i]); 763 602 ret = -EINVAL; ··· 767 602 } 768 603 769 604 /* Parse fetch argument */ 770 - ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i], 605 + ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg, 771 606 is_return, true); 772 607 if (ret) { 773 608 pr_info("Parse error at argument[%d]. (%d)\n", i, ret); ··· 775 610 } 776 611 } 777 612 778 - ret = register_trace_probe(tp); 613 + ret = register_trace_kprobe(tk); 779 614 if (ret) 780 615 goto error; 781 616 return 0; 782 617 783 618 error: 784 - free_trace_probe(tp); 619 + free_trace_kprobe(tk); 785 620 return ret; 786 621 } 787 622 788 - static int release_all_trace_probes(void) 623 + static int release_all_trace_kprobes(void) 789 624 { 790 - struct trace_probe *tp; 625 + struct trace_kprobe *tk; 791 626 int ret = 0; 792 627 793 628 mutex_lock(&probe_lock); 794 629 /* Ensure no probe is in use. */ 795 - list_for_each_entry(tp, &probe_list, list) 796 - if (trace_probe_is_enabled(tp)) { 630 + list_for_each_entry(tk, &probe_list, list) 631 + if (trace_probe_is_enabled(&tk->tp)) { 797 632 ret = -EBUSY; 798 633 goto end; 799 634 } 800 635 /* TODO: Use batch unregistration */ 801 636 while (!list_empty(&probe_list)) { 802 - tp = list_entry(probe_list.next, struct trace_probe, list); 803 - ret = unregister_trace_probe(tp); 637 + tk = list_entry(probe_list.next, struct trace_kprobe, list); 638 + ret = unregister_trace_kprobe(tk); 804 639 if (ret) 805 640 goto end; 806 - free_trace_probe(tp); 641 + free_trace_kprobe(tk); 807 642 } 808 643 809 644 end: ··· 831 666 832 667 static int probes_seq_show(struct seq_file *m, void *v) 833 668 { 834 - struct trace_probe *tp = v; 669 + struct trace_kprobe *tk = v; 835 670 int i; 836 671 837 - seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p'); 838 - seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name); 672 + seq_printf(m, "%c", trace_kprobe_is_return(tk) ? 'r' : 'p'); 673 + seq_printf(m, ":%s/%s", tk->tp.call.class->system, tk->tp.call.name); 839 674 840 - if (!tp->symbol) 841 - seq_printf(m, " 0x%p", tp->rp.kp.addr); 842 - else if (tp->rp.kp.offset) 843 - seq_printf(m, " %s+%u", trace_probe_symbol(tp), 844 - tp->rp.kp.offset); 675 + if (!tk->symbol) 676 + seq_printf(m, " 0x%p", tk->rp.kp.addr); 677 + else if (tk->rp.kp.offset) 678 + seq_printf(m, " %s+%u", trace_kprobe_symbol(tk), 679 + tk->rp.kp.offset); 845 680 else 846 - seq_printf(m, " %s", trace_probe_symbol(tp)); 681 + seq_printf(m, " %s", trace_kprobe_symbol(tk)); 847 682 848 - for (i = 0; i < tp->nr_args; i++) 849 - seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm); 683 + for (i = 0; i < tk->tp.nr_args; i++) 684 + seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm); 850 685 seq_printf(m, "\n"); 851 686 852 687 return 0; ··· 864 699 int ret; 865 700 866 701 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 867 - ret = release_all_trace_probes(); 702 + ret = release_all_trace_kprobes(); 868 703 if (ret < 0) 869 704 return ret; 870 705 } ··· 876 711 size_t count, loff_t *ppos) 877 712 { 878 713 return traceprobe_probes_write(file, buffer, count, ppos, 879 - create_trace_probe); 714 + create_trace_kprobe); 880 715 } 881 716 882 717 static const struct file_operations kprobe_events_ops = { ··· 891 726 /* Probes profiling interfaces */ 892 727 static int probes_profile_seq_show(struct seq_file *m, void *v) 893 728 { 894 - struct trace_probe *tp = v; 729 + struct trace_kprobe *tk = v; 895 730 896 - seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, 897 - tp->rp.kp.nmissed); 731 + seq_printf(m, " %-44s %15lu %15lu\n", tk->tp.call.name, tk->nhit, 732 + tk->rp.kp.nmissed); 898 733 899 734 return 0; 900 735 } ··· 919 754 .release = seq_release, 920 755 }; 921 756 922 - /* Sum up total data length for dynamic arraies (strings) */ 923 - static __kprobes int __get_data_size(struct trace_probe *tp, 924 - struct pt_regs *regs) 925 - { 926 - int i, ret = 0; 927 - u32 len; 928 - 929 - for (i = 0; i < tp->nr_args; i++) 930 - if (unlikely(tp->args[i].fetch_size.fn)) { 931 - call_fetch(&tp->args[i].fetch_size, regs, &len); 932 - ret += len; 933 - } 934 - 935 - return ret; 936 - } 937 - 938 - /* Store the value of each argument */ 939 - static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp, 940 - struct pt_regs *regs, 941 - u8 *data, int maxlen) 942 - { 943 - int i; 944 - u32 end = tp->size; 945 - u32 *dl; /* Data (relative) location */ 946 - 947 - for (i = 0; i < tp->nr_args; i++) { 948 - if (unlikely(tp->args[i].fetch_size.fn)) { 949 - /* 950 - * First, we set the relative location and 951 - * maximum data length to *dl 952 - */ 953 - dl = (u32 *)(data + tp->args[i].offset); 954 - *dl = make_data_rloc(maxlen, end - tp->args[i].offset); 955 - /* Then try to fetch string or dynamic array data */ 956 - call_fetch(&tp->args[i].fetch, regs, dl); 957 - /* Reduce maximum length */ 958 - end += get_rloc_len(*dl); 959 - maxlen -= get_rloc_len(*dl); 960 - /* Trick here, convert data_rloc to data_loc */ 961 - *dl = convert_rloc_to_loc(*dl, 962 - ent_size + tp->args[i].offset); 963 - } else 964 - /* Just fetching data normally */ 965 - call_fetch(&tp->args[i].fetch, regs, 966 - data + tp->args[i].offset); 967 - } 968 - } 969 - 970 757 /* Kprobe handler */ 971 758 static __kprobes void 972 - __kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs, 759 + __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs, 973 760 struct ftrace_event_file *ftrace_file) 974 761 { 975 762 struct kprobe_trace_entry_head *entry; ··· 929 812 struct ring_buffer *buffer; 930 813 int size, dsize, pc; 931 814 unsigned long irq_flags; 932 - struct ftrace_event_call *call = &tp->call; 815 + struct ftrace_event_call *call = &tk->tp.call; 933 816 934 817 WARN_ON(call != ftrace_file->event_call); 935 818 936 - if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags)) 819 + if (ftrace_trigger_soft_disabled(ftrace_file)) 937 820 return; 938 821 939 822 local_save_flags(irq_flags); 940 823 pc = preempt_count(); 941 824 942 - dsize = __get_data_size(tp, regs); 943 - size = sizeof(*entry) + tp->size + dsize; 825 + dsize = __get_data_size(&tk->tp, regs); 826 + size = sizeof(*entry) + tk->tp.size + dsize; 944 827 945 828 event = trace_event_buffer_lock_reserve(&buffer, ftrace_file, 946 829 call->event.type, ··· 949 832 return; 950 833 951 834 entry = ring_buffer_event_data(event); 952 - entry->ip = (unsigned long)tp->rp.kp.addr; 953 - store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize); 835 + entry->ip = (unsigned long)tk->rp.kp.addr; 836 + store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); 954 837 955 - if (!filter_check_discard(ftrace_file, entry, buffer, event)) 956 - trace_buffer_unlock_commit_regs(buffer, event, 957 - irq_flags, pc, regs); 838 + event_trigger_unlock_commit_regs(ftrace_file, buffer, event, 839 + entry, irq_flags, pc, regs); 958 840 } 959 841 960 842 static __kprobes void 961 - kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs) 843 + kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs) 962 844 { 963 845 struct event_file_link *link; 964 846 965 - list_for_each_entry_rcu(link, &tp->files, list) 966 - __kprobe_trace_func(tp, regs, link->file); 847 + list_for_each_entry_rcu(link, &tk->tp.files, list) 848 + __kprobe_trace_func(tk, regs, link->file); 967 849 } 968 850 969 851 /* Kretprobe handler */ 970 852 static __kprobes void 971 - __kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri, 853 + __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 972 854 struct pt_regs *regs, 973 855 struct ftrace_event_file *ftrace_file) 974 856 { ··· 976 860 struct ring_buffer *buffer; 977 861 int size, pc, dsize; 978 862 unsigned long irq_flags; 979 - struct ftrace_event_call *call = &tp->call; 863 + struct ftrace_event_call *call = &tk->tp.call; 980 864 981 865 WARN_ON(call != ftrace_file->event_call); 982 866 983 - if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags)) 867 + if (ftrace_trigger_soft_disabled(ftrace_file)) 984 868 return; 985 869 986 870 local_save_flags(irq_flags); 987 871 pc = preempt_count(); 988 872 989 - dsize = __get_data_size(tp, regs); 990 - size = sizeof(*entry) + tp->size + dsize; 873 + dsize = __get_data_size(&tk->tp, regs); 874 + size = sizeof(*entry) + tk->tp.size + dsize; 991 875 992 876 event = trace_event_buffer_lock_reserve(&buffer, ftrace_file, 993 877 call->event.type, ··· 996 880 return; 997 881 998 882 entry = ring_buffer_event_data(event); 999 - entry->func = (unsigned long)tp->rp.kp.addr; 883 + entry->func = (unsigned long)tk->rp.kp.addr; 1000 884 entry->ret_ip = (unsigned long)ri->ret_addr; 1001 - store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize); 885 + store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); 1002 886 1003 - if (!filter_check_discard(ftrace_file, entry, buffer, event)) 1004 - trace_buffer_unlock_commit_regs(buffer, event, 1005 - irq_flags, pc, regs); 887 + event_trigger_unlock_commit_regs(ftrace_file, buffer, event, 888 + entry, irq_flags, pc, regs); 1006 889 } 1007 890 1008 891 static __kprobes void 1009 - kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri, 892 + kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 1010 893 struct pt_regs *regs) 1011 894 { 1012 895 struct event_file_link *link; 1013 896 1014 - list_for_each_entry_rcu(link, &tp->files, list) 1015 - __kretprobe_trace_func(tp, ri, regs, link->file); 897 + list_for_each_entry_rcu(link, &tk->tp.files, list) 898 + __kretprobe_trace_func(tk, ri, regs, link->file); 1016 899 } 1017 900 1018 901 /* Event entry printers */ ··· 1098 983 { 1099 984 int ret, i; 1100 985 struct kprobe_trace_entry_head field; 1101 - struct trace_probe *tp = (struct trace_probe *)event_call->data; 986 + struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; 1102 987 1103 988 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); 1104 989 /* Set argument names as fields */ 1105 - for (i = 0; i < tp->nr_args; i++) { 1106 - ret = trace_define_field(event_call, tp->args[i].type->fmttype, 1107 - tp->args[i].name, 1108 - sizeof(field) + tp->args[i].offset, 1109 - tp->args[i].type->size, 1110 - tp->args[i].type->is_signed, 990 + for (i = 0; i < tk->tp.nr_args; i++) { 991 + struct probe_arg *parg = &tk->tp.args[i]; 992 + 993 + ret = trace_define_field(event_call, parg->type->fmttype, 994 + parg->name, 995 + sizeof(field) + parg->offset, 996 + parg->type->size, 997 + parg->type->is_signed, 1111 998 FILTER_OTHER); 1112 999 if (ret) 1113 1000 return ret; ··· 1121 1004 { 1122 1005 int ret, i; 1123 1006 struct kretprobe_trace_entry_head field; 1124 - struct trace_probe *tp = (struct trace_probe *)event_call->data; 1007 + struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; 1125 1008 1126 1009 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); 1127 1010 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); 1128 1011 /* Set argument names as fields */ 1129 - for (i = 0; i < tp->nr_args; i++) { 1130 - ret = trace_define_field(event_call, tp->args[i].type->fmttype, 1131 - tp->args[i].name, 1132 - sizeof(field) + tp->args[i].offset, 1133 - tp->args[i].type->size, 1134 - tp->args[i].type->is_signed, 1012 + for (i = 0; i < tk->tp.nr_args; i++) { 1013 + struct probe_arg *parg = &tk->tp.args[i]; 1014 + 1015 + ret = trace_define_field(event_call, parg->type->fmttype, 1016 + parg->name, 1017 + sizeof(field) + parg->offset, 1018 + parg->type->size, 1019 + parg->type->is_signed, 1135 1020 FILTER_OTHER); 1136 1021 if (ret) 1137 1022 return ret; ··· 1141 1022 return 0; 1142 1023 } 1143 1024 1144 - static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) 1145 - { 1146 - int i; 1147 - int pos = 0; 1148 - 1149 - const char *fmt, *arg; 1150 - 1151 - if (!trace_probe_is_return(tp)) { 1152 - fmt = "(%lx)"; 1153 - arg = "REC->" FIELD_STRING_IP; 1154 - } else { 1155 - fmt = "(%lx <- %lx)"; 1156 - arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 1157 - } 1158 - 1159 - /* When len=0, we just calculate the needed length */ 1160 - #define LEN_OR_ZERO (len ? len - pos : 0) 1161 - 1162 - pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 1163 - 1164 - for (i = 0; i < tp->nr_args; i++) { 1165 - pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", 1166 - tp->args[i].name, tp->args[i].type->fmt); 1167 - } 1168 - 1169 - pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); 1170 - 1171 - for (i = 0; i < tp->nr_args; i++) { 1172 - if (strcmp(tp->args[i].type->name, "string") == 0) 1173 - pos += snprintf(buf + pos, LEN_OR_ZERO, 1174 - ", __get_str(%s)", 1175 - tp->args[i].name); 1176 - else 1177 - pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", 1178 - tp->args[i].name); 1179 - } 1180 - 1181 - #undef LEN_OR_ZERO 1182 - 1183 - /* return the length of print_fmt */ 1184 - return pos; 1185 - } 1186 - 1187 - static int set_print_fmt(struct trace_probe *tp) 1188 - { 1189 - int len; 1190 - char *print_fmt; 1191 - 1192 - /* First: called with 0 length to calculate the needed length */ 1193 - len = __set_print_fmt(tp, NULL, 0); 1194 - print_fmt = kmalloc(len + 1, GFP_KERNEL); 1195 - if (!print_fmt) 1196 - return -ENOMEM; 1197 - 1198 - /* Second: actually write the @print_fmt */ 1199 - __set_print_fmt(tp, print_fmt, len + 1); 1200 - tp->call.print_fmt = print_fmt; 1201 - 1202 - return 0; 1203 - } 1204 - 1205 1025 #ifdef CONFIG_PERF_EVENTS 1206 1026 1207 1027 /* Kprobe profile handler */ 1208 1028 static __kprobes void 1209 - kprobe_perf_func(struct trace_probe *tp, struct pt_regs *regs) 1029 + kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) 1210 1030 { 1211 - struct ftrace_event_call *call = &tp->call; 1031 + struct ftrace_event_call *call = &tk->tp.call; 1212 1032 struct kprobe_trace_entry_head *entry; 1213 1033 struct hlist_head *head; 1214 1034 int size, __size, dsize; ··· 1157 1099 if (hlist_empty(head)) 1158 1100 return; 1159 1101 1160 - dsize = __get_data_size(tp, regs); 1161 - __size = sizeof(*entry) + tp->size + dsize; 1102 + dsize = __get_data_size(&tk->tp, regs); 1103 + __size = sizeof(*entry) + tk->tp.size + dsize; 1162 1104 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 1163 1105 size -= sizeof(u32); 1164 1106 ··· 1166 1108 if (!entry) 1167 1109 return; 1168 1110 1169 - entry->ip = (unsigned long)tp->rp.kp.addr; 1111 + entry->ip = (unsigned long)tk->rp.kp.addr; 1170 1112 memset(&entry[1], 0, dsize); 1171 - store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize); 1113 + store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); 1172 1114 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); 1173 1115 } 1174 1116 1175 1117 /* Kretprobe profile handler */ 1176 1118 static __kprobes void 1177 - kretprobe_perf_func(struct trace_probe *tp, struct kretprobe_instance *ri, 1119 + kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 1178 1120 struct pt_regs *regs) 1179 1121 { 1180 - struct ftrace_event_call *call = &tp->call; 1122 + struct ftrace_event_call *call = &tk->tp.call; 1181 1123 struct kretprobe_trace_entry_head *entry; 1182 1124 struct hlist_head *head; 1183 1125 int size, __size, dsize; ··· 1187 1129 if (hlist_empty(head)) 1188 1130 return; 1189 1131 1190 - dsize = __get_data_size(tp, regs); 1191 - __size = sizeof(*entry) + tp->size + dsize; 1132 + dsize = __get_data_size(&tk->tp, regs); 1133 + __size = sizeof(*entry) + tk->tp.size + dsize; 1192 1134 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 1193 1135 size -= sizeof(u32); 1194 1136 ··· 1196 1138 if (!entry) 1197 1139 return; 1198 1140 1199 - entry->func = (unsigned long)tp->rp.kp.addr; 1141 + entry->func = (unsigned long)tk->rp.kp.addr; 1200 1142 entry->ret_ip = (unsigned long)ri->ret_addr; 1201 - store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize); 1143 + store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize); 1202 1144 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); 1203 1145 } 1204 1146 #endif /* CONFIG_PERF_EVENTS */ ··· 1213 1155 int kprobe_register(struct ftrace_event_call *event, 1214 1156 enum trace_reg type, void *data) 1215 1157 { 1216 - struct trace_probe *tp = (struct trace_probe *)event->data; 1158 + struct trace_kprobe *tk = (struct trace_kprobe *)event->data; 1217 1159 struct ftrace_event_file *file = data; 1218 1160 1219 1161 switch (type) { 1220 1162 case TRACE_REG_REGISTER: 1221 - return enable_trace_probe(tp, file); 1163 + return enable_trace_kprobe(tk, file); 1222 1164 case TRACE_REG_UNREGISTER: 1223 - return disable_trace_probe(tp, file); 1165 + return disable_trace_kprobe(tk, file); 1224 1166 1225 1167 #ifdef CONFIG_PERF_EVENTS 1226 1168 case TRACE_REG_PERF_REGISTER: 1227 - return enable_trace_probe(tp, NULL); 1169 + return enable_trace_kprobe(tk, NULL); 1228 1170 case TRACE_REG_PERF_UNREGISTER: 1229 - return disable_trace_probe(tp, NULL); 1171 + return disable_trace_kprobe(tk, NULL); 1230 1172 case TRACE_REG_PERF_OPEN: 1231 1173 case TRACE_REG_PERF_CLOSE: 1232 1174 case TRACE_REG_PERF_ADD: ··· 1240 1182 static __kprobes 1241 1183 int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) 1242 1184 { 1243 - struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); 1185 + struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp); 1244 1186 1245 - tp->nhit++; 1187 + tk->nhit++; 1246 1188 1247 - if (tp->flags & TP_FLAG_TRACE) 1248 - kprobe_trace_func(tp, regs); 1189 + if (tk->tp.flags & TP_FLAG_TRACE) 1190 + kprobe_trace_func(tk, regs); 1249 1191 #ifdef CONFIG_PERF_EVENTS 1250 - if (tp->flags & TP_FLAG_PROFILE) 1251 - kprobe_perf_func(tp, regs); 1192 + if (tk->tp.flags & TP_FLAG_PROFILE) 1193 + kprobe_perf_func(tk, regs); 1252 1194 #endif 1253 1195 return 0; /* We don't tweek kernel, so just return 0 */ 1254 1196 } ··· 1256 1198 static __kprobes 1257 1199 int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) 1258 1200 { 1259 - struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); 1201 + struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp); 1260 1202 1261 - tp->nhit++; 1203 + tk->nhit++; 1262 1204 1263 - if (tp->flags & TP_FLAG_TRACE) 1264 - kretprobe_trace_func(tp, ri, regs); 1205 + if (tk->tp.flags & TP_FLAG_TRACE) 1206 + kretprobe_trace_func(tk, ri, regs); 1265 1207 #ifdef CONFIG_PERF_EVENTS 1266 - if (tp->flags & TP_FLAG_PROFILE) 1267 - kretprobe_perf_func(tp, ri, regs); 1208 + if (tk->tp.flags & TP_FLAG_PROFILE) 1209 + kretprobe_perf_func(tk, ri, regs); 1268 1210 #endif 1269 1211 return 0; /* We don't tweek kernel, so just return 0 */ 1270 1212 } ··· 1277 1219 .trace = print_kprobe_event 1278 1220 }; 1279 1221 1280 - static int register_probe_event(struct trace_probe *tp) 1222 + static int register_kprobe_event(struct trace_kprobe *tk) 1281 1223 { 1282 - struct ftrace_event_call *call = &tp->call; 1224 + struct ftrace_event_call *call = &tk->tp.call; 1283 1225 int ret; 1284 1226 1285 1227 /* Initialize ftrace_event_call */ 1286 1228 INIT_LIST_HEAD(&call->class->fields); 1287 - if (trace_probe_is_return(tp)) { 1229 + if (trace_kprobe_is_return(tk)) { 1288 1230 call->event.funcs = &kretprobe_funcs; 1289 1231 call->class->define_fields = kretprobe_event_define_fields; 1290 1232 } else { 1291 1233 call->event.funcs = &kprobe_funcs; 1292 1234 call->class->define_fields = kprobe_event_define_fields; 1293 1235 } 1294 - if (set_print_fmt(tp) < 0) 1236 + if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) 1295 1237 return -ENOMEM; 1296 1238 ret = register_ftrace_event(&call->event); 1297 1239 if (!ret) { ··· 1300 1242 } 1301 1243 call->flags = 0; 1302 1244 call->class->reg = kprobe_register; 1303 - call->data = tp; 1245 + call->data = tk; 1304 1246 ret = trace_add_event_call(call); 1305 1247 if (ret) { 1306 1248 pr_info("Failed to register kprobe event: %s\n", call->name); ··· 1310 1252 return ret; 1311 1253 } 1312 1254 1313 - static int unregister_probe_event(struct trace_probe *tp) 1255 + static int unregister_kprobe_event(struct trace_kprobe *tk) 1314 1256 { 1315 1257 int ret; 1316 1258 1317 1259 /* tp->event is unregistered in trace_remove_event_call() */ 1318 - ret = trace_remove_event_call(&tp->call); 1260 + ret = trace_remove_event_call(&tk->tp.call); 1319 1261 if (!ret) 1320 - kfree(tp->call.print_fmt); 1262 + kfree(tk->tp.call.print_fmt); 1321 1263 return ret; 1322 1264 } 1323 1265 ··· 1327 1269 struct dentry *d_tracer; 1328 1270 struct dentry *entry; 1329 1271 1330 - if (register_module_notifier(&trace_probe_module_nb)) 1272 + if (register_module_notifier(&trace_kprobe_module_nb)) 1331 1273 return -EINVAL; 1332 1274 1333 1275 d_tracer = tracing_init_dentry(); ··· 1367 1309 } 1368 1310 1369 1311 static struct ftrace_event_file * 1370 - find_trace_probe_file(struct trace_probe *tp, struct trace_array *tr) 1312 + find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr) 1371 1313 { 1372 1314 struct ftrace_event_file *file; 1373 1315 1374 1316 list_for_each_entry(file, &tr->events, list) 1375 - if (file->event_call == &tp->call) 1317 + if (file->event_call == &tk->tp.call) 1376 1318 return file; 1377 1319 1378 1320 return NULL; 1379 1321 } 1380 1322 1381 1323 /* 1382 - * Nobody but us can call enable_trace_probe/disable_trace_probe at this 1324 + * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this 1383 1325 * stage, we can do this lockless. 1384 1326 */ 1385 1327 static __init int kprobe_trace_self_tests_init(void) 1386 1328 { 1387 1329 int ret, warn = 0; 1388 1330 int (*target)(int, int, int, int, int, int); 1389 - struct trace_probe *tp; 1331 + struct trace_kprobe *tk; 1390 1332 struct ftrace_event_file *file; 1391 1333 1392 1334 target = kprobe_trace_selftest_target; ··· 1395 1337 1396 1338 ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target " 1397 1339 "$stack $stack0 +0($stack)", 1398 - create_trace_probe); 1340 + create_trace_kprobe); 1399 1341 if (WARN_ON_ONCE(ret)) { 1400 1342 pr_warn("error on probing function entry.\n"); 1401 1343 warn++; 1402 1344 } else { 1403 1345 /* Enable trace point */ 1404 - tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM); 1405 - if (WARN_ON_ONCE(tp == NULL)) { 1346 + tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); 1347 + if (WARN_ON_ONCE(tk == NULL)) { 1406 1348 pr_warn("error on getting new probe.\n"); 1407 1349 warn++; 1408 1350 } else { 1409 - file = find_trace_probe_file(tp, top_trace_array()); 1351 + file = find_trace_probe_file(tk, top_trace_array()); 1410 1352 if (WARN_ON_ONCE(file == NULL)) { 1411 1353 pr_warn("error on getting probe file.\n"); 1412 1354 warn++; 1413 1355 } else 1414 - enable_trace_probe(tp, file); 1356 + enable_trace_kprobe(tk, file); 1415 1357 } 1416 1358 } 1417 1359 1418 1360 ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target " 1419 - "$retval", create_trace_probe); 1361 + "$retval", create_trace_kprobe); 1420 1362 if (WARN_ON_ONCE(ret)) { 1421 1363 pr_warn("error on probing function return.\n"); 1422 1364 warn++; 1423 1365 } else { 1424 1366 /* Enable trace point */ 1425 - tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM); 1426 - if (WARN_ON_ONCE(tp == NULL)) { 1367 + tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); 1368 + if (WARN_ON_ONCE(tk == NULL)) { 1427 1369 pr_warn("error on getting 2nd new probe.\n"); 1428 1370 warn++; 1429 1371 } else { 1430 - file = find_trace_probe_file(tp, top_trace_array()); 1372 + file = find_trace_probe_file(tk, top_trace_array()); 1431 1373 if (WARN_ON_ONCE(file == NULL)) { 1432 1374 pr_warn("error on getting probe file.\n"); 1433 1375 warn++; 1434 1376 } else 1435 - enable_trace_probe(tp, file); 1377 + enable_trace_kprobe(tk, file); 1436 1378 } 1437 1379 } 1438 1380 ··· 1442 1384 ret = target(1, 2, 3, 4, 5, 6); 1443 1385 1444 1386 /* Disable trace points before removing it */ 1445 - tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM); 1446 - if (WARN_ON_ONCE(tp == NULL)) { 1387 + tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); 1388 + if (WARN_ON_ONCE(tk == NULL)) { 1447 1389 pr_warn("error on getting test probe.\n"); 1448 1390 warn++; 1449 1391 } else { 1450 - file = find_trace_probe_file(tp, top_trace_array()); 1392 + file = find_trace_probe_file(tk, top_trace_array()); 1451 1393 if (WARN_ON_ONCE(file == NULL)) { 1452 1394 pr_warn("error on getting probe file.\n"); 1453 1395 warn++; 1454 1396 } else 1455 - disable_trace_probe(tp, file); 1397 + disable_trace_kprobe(tk, file); 1456 1398 } 1457 1399 1458 - tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM); 1459 - if (WARN_ON_ONCE(tp == NULL)) { 1400 + tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); 1401 + if (WARN_ON_ONCE(tk == NULL)) { 1460 1402 pr_warn("error on getting 2nd test probe.\n"); 1461 1403 warn++; 1462 1404 } else { 1463 - file = find_trace_probe_file(tp, top_trace_array()); 1405 + file = find_trace_probe_file(tk, top_trace_array()); 1464 1406 if (WARN_ON_ONCE(file == NULL)) { 1465 1407 pr_warn("error on getting probe file.\n"); 1466 1408 warn++; 1467 1409 } else 1468 - disable_trace_probe(tp, file); 1410 + disable_trace_kprobe(tk, file); 1469 1411 } 1470 1412 1471 - ret = traceprobe_command("-:testprobe", create_trace_probe); 1413 + ret = traceprobe_command("-:testprobe", create_trace_kprobe); 1472 1414 if (WARN_ON_ONCE(ret)) { 1473 1415 pr_warn("error on deleting a probe.\n"); 1474 1416 warn++; 1475 1417 } 1476 1418 1477 - ret = traceprobe_command("-:testprobe2", create_trace_probe); 1419 + ret = traceprobe_command("-:testprobe2", create_trace_kprobe); 1478 1420 if (WARN_ON_ONCE(ret)) { 1479 1421 pr_warn("error on deleting a probe.\n"); 1480 1422 warn++; 1481 1423 } 1482 1424 1483 1425 end: 1484 - release_all_trace_probes(); 1426 + release_all_trace_kprobes(); 1485 1427 if (warn) 1486 1428 pr_cont("NG: Some tests are failed. Please check them.\n"); 1487 1429 else
+161 -279
kernel/trace/trace_probe.c
··· 35 35 FIELD_STRING_FUNC, 36 36 }; 37 37 38 - /* Printing function type */ 39 - #define PRINT_TYPE_FUNC_NAME(type) print_type_##type 40 - #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type 41 - 42 38 /* Printing in basic type function template */ 43 - #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \ 44 - static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ 39 + #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \ 40 + __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ 45 41 const char *name, \ 46 - void *data, void *ent)\ 42 + void *data, void *ent) \ 47 43 { \ 48 - return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\ 44 + return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \ 49 45 } \ 50 - static const char PRINT_TYPE_FMT_NAME(type)[] = fmt; 46 + const char PRINT_TYPE_FMT_NAME(type)[] = fmt; 51 47 52 - DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int) 53 - DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int) 54 - DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long) 55 - DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long) 56 - DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int) 57 - DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int) 58 - DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long) 59 - DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long) 60 - 61 - static inline void *get_rloc_data(u32 *dl) 62 - { 63 - return (u8 *)dl + get_rloc_offs(*dl); 64 - } 65 - 66 - /* For data_loc conversion */ 67 - static inline void *get_loc_data(u32 *dl, void *ent) 68 - { 69 - return (u8 *)ent + get_rloc_offs(*dl); 70 - } 71 - 72 - /* For defining macros, define string/string_size types */ 73 - typedef u32 string; 74 - typedef u32 string_size; 48 + DEFINE_BASIC_PRINT_TYPE_FUNC(u8 , "0x%x") 49 + DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "0x%x") 50 + DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "0x%x") 51 + DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "0x%Lx") 52 + DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d") 53 + DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d") 54 + DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%d") 55 + DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%Ld") 75 56 76 57 /* Print type function for string type */ 77 - static __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, 58 + __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, 78 59 const char *name, 79 60 void *data, void *ent) 80 61 { ··· 68 87 (const char *)get_loc_data(data, ent)); 69 88 } 70 89 71 - static const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; 72 - 73 - #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type 74 - /* 75 - * Define macro for basic types - we don't need to define s* types, because 76 - * we have to care only about bitwidth at recording time. 77 - */ 78 - #define DEFINE_BASIC_FETCH_FUNCS(method) \ 79 - DEFINE_FETCH_##method(u8) \ 80 - DEFINE_FETCH_##method(u16) \ 81 - DEFINE_FETCH_##method(u32) \ 82 - DEFINE_FETCH_##method(u64) 90 + const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; 83 91 84 92 #define CHECK_FETCH_FUNCS(method, fn) \ 85 93 (((FETCH_FUNC_NAME(method, u8) == fn) || \ ··· 81 111 82 112 /* Data fetch function templates */ 83 113 #define DEFINE_FETCH_reg(type) \ 84 - static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ 114 + __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ 85 115 void *offset, void *dest) \ 86 116 { \ 87 117 *(type *)dest = (type)regs_get_register(regs, \ ··· 92 122 #define fetch_reg_string NULL 93 123 #define fetch_reg_string_size NULL 94 124 95 - #define DEFINE_FETCH_stack(type) \ 96 - static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ 97 - void *offset, void *dest) \ 98 - { \ 99 - *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \ 100 - (unsigned int)((unsigned long)offset)); \ 101 - } 102 - DEFINE_BASIC_FETCH_FUNCS(stack) 103 - /* No string on the stack entry */ 104 - #define fetch_stack_string NULL 105 - #define fetch_stack_string_size NULL 106 - 107 125 #define DEFINE_FETCH_retval(type) \ 108 - static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\ 126 + __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \ 109 127 void *dummy, void *dest) \ 110 128 { \ 111 129 *(type *)dest = (type)regs_return_value(regs); \ ··· 103 145 #define fetch_retval_string NULL 104 146 #define fetch_retval_string_size NULL 105 147 106 - #define DEFINE_FETCH_memory(type) \ 107 - static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ 108 - void *addr, void *dest) \ 109 - { \ 110 - type retval; \ 111 - if (probe_kernel_address(addr, retval)) \ 112 - *(type *)dest = 0; \ 113 - else \ 114 - *(type *)dest = retval; \ 115 - } 116 - DEFINE_BASIC_FETCH_FUNCS(memory) 117 - /* 118 - * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max 119 - * length and relative data location. 120 - */ 121 - static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, 122 - void *addr, void *dest) 123 - { 124 - long ret; 125 - int maxlen = get_rloc_len(*(u32 *)dest); 126 - u8 *dst = get_rloc_data(dest); 127 - u8 *src = addr; 128 - mm_segment_t old_fs = get_fs(); 129 - 130 - if (!maxlen) 131 - return; 132 - 133 - /* 134 - * Try to get string again, since the string can be changed while 135 - * probing. 136 - */ 137 - set_fs(KERNEL_DS); 138 - pagefault_disable(); 139 - 140 - do 141 - ret = __copy_from_user_inatomic(dst++, src++, 1); 142 - while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen); 143 - 144 - dst[-1] = '\0'; 145 - pagefault_enable(); 146 - set_fs(old_fs); 147 - 148 - if (ret < 0) { /* Failed to fetch string */ 149 - ((u8 *)get_rloc_data(dest))[0] = '\0'; 150 - *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest)); 151 - } else { 152 - *(u32 *)dest = make_data_rloc(src - (u8 *)addr, 153 - get_rloc_offs(*(u32 *)dest)); 154 - } 155 - } 156 - 157 - /* Return the length of string -- including null terminal byte */ 158 - static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs, 159 - void *addr, void *dest) 160 - { 161 - mm_segment_t old_fs; 162 - int ret, len = 0; 163 - u8 c; 164 - 165 - old_fs = get_fs(); 166 - set_fs(KERNEL_DS); 167 - pagefault_disable(); 168 - 169 - do { 170 - ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1); 171 - len++; 172 - } while (c && ret == 0 && len < MAX_STRING_SIZE); 173 - 174 - pagefault_enable(); 175 - set_fs(old_fs); 176 - 177 - if (ret < 0) /* Failed to check the length */ 178 - *(u32 *)dest = 0; 179 - else 180 - *(u32 *)dest = len; 181 - } 182 - 183 - /* Memory fetching by symbol */ 184 - struct symbol_cache { 185 - char *symbol; 186 - long offset; 187 - unsigned long addr; 188 - }; 189 - 190 - static unsigned long update_symbol_cache(struct symbol_cache *sc) 191 - { 192 - sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); 193 - 194 - if (sc->addr) 195 - sc->addr += sc->offset; 196 - 197 - return sc->addr; 198 - } 199 - 200 - static void free_symbol_cache(struct symbol_cache *sc) 201 - { 202 - kfree(sc->symbol); 203 - kfree(sc); 204 - } 205 - 206 - static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) 207 - { 208 - struct symbol_cache *sc; 209 - 210 - if (!sym || strlen(sym) == 0) 211 - return NULL; 212 - 213 - sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); 214 - if (!sc) 215 - return NULL; 216 - 217 - sc->symbol = kstrdup(sym, GFP_KERNEL); 218 - if (!sc->symbol) { 219 - kfree(sc); 220 - return NULL; 221 - } 222 - sc->offset = offset; 223 - update_symbol_cache(sc); 224 - 225 - return sc; 226 - } 227 - 228 - #define DEFINE_FETCH_symbol(type) \ 229 - static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\ 230 - void *data, void *dest) \ 231 - { \ 232 - struct symbol_cache *sc = data; \ 233 - if (sc->addr) \ 234 - fetch_memory_##type(regs, (void *)sc->addr, dest); \ 235 - else \ 236 - *(type *)dest = 0; \ 237 - } 238 - DEFINE_BASIC_FETCH_FUNCS(symbol) 239 - DEFINE_FETCH_symbol(string) 240 - DEFINE_FETCH_symbol(string_size) 241 - 242 148 /* Dereference memory access function */ 243 149 struct deref_fetch_param { 244 150 struct fetch_param orig; 245 151 long offset; 152 + fetch_func_t fetch; 153 + fetch_func_t fetch_size; 246 154 }; 247 155 248 156 #define DEFINE_FETCH_deref(type) \ 249 - static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\ 157 + __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \ 250 158 void *data, void *dest) \ 251 159 { \ 252 160 struct deref_fetch_param *dprm = data; \ ··· 120 296 call_fetch(&dprm->orig, regs, &addr); \ 121 297 if (addr) { \ 122 298 addr += dprm->offset; \ 123 - fetch_memory_##type(regs, (void *)addr, dest); \ 299 + dprm->fetch(regs, (void *)addr, dest); \ 124 300 } else \ 125 301 *(type *)dest = 0; \ 126 302 } 127 303 DEFINE_BASIC_FETCH_FUNCS(deref) 128 304 DEFINE_FETCH_deref(string) 129 - DEFINE_FETCH_deref(string_size) 305 + 306 + __kprobes void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs, 307 + void *data, void *dest) 308 + { 309 + struct deref_fetch_param *dprm = data; 310 + unsigned long addr; 311 + 312 + call_fetch(&dprm->orig, regs, &addr); 313 + if (addr && dprm->fetch_size) { 314 + addr += dprm->offset; 315 + dprm->fetch_size(regs, (void *)addr, dest); 316 + } else 317 + *(string_size *)dest = 0; 318 + } 130 319 131 320 static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data) 132 321 { ··· 166 329 }; 167 330 168 331 #define DEFINE_FETCH_bitfield(type) \ 169 - static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\ 332 + __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \ 170 333 void *data, void *dest) \ 171 334 { \ 172 335 struct bitfield_fetch_param *bprm = data; \ ··· 211 374 kfree(data); 212 375 } 213 376 214 - /* Default (unsigned long) fetch type */ 215 - #define __DEFAULT_FETCH_TYPE(t) u##t 216 - #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t) 217 - #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG) 218 - #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE) 219 - 220 - #define ASSIGN_FETCH_FUNC(method, type) \ 221 - [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type) 222 - 223 - #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \ 224 - {.name = _name, \ 225 - .size = _size, \ 226 - .is_signed = sign, \ 227 - .print = PRINT_TYPE_FUNC_NAME(ptype), \ 228 - .fmt = PRINT_TYPE_FMT_NAME(ptype), \ 229 - .fmttype = _fmttype, \ 230 - .fetch = { \ 231 - ASSIGN_FETCH_FUNC(reg, ftype), \ 232 - ASSIGN_FETCH_FUNC(stack, ftype), \ 233 - ASSIGN_FETCH_FUNC(retval, ftype), \ 234 - ASSIGN_FETCH_FUNC(memory, ftype), \ 235 - ASSIGN_FETCH_FUNC(symbol, ftype), \ 236 - ASSIGN_FETCH_FUNC(deref, ftype), \ 237 - ASSIGN_FETCH_FUNC(bitfield, ftype), \ 238 - } \ 239 - } 240 - 241 - #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \ 242 - __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype) 243 - 244 - #define FETCH_TYPE_STRING 0 245 - #define FETCH_TYPE_STRSIZE 1 246 - 247 - /* Fetch type information table */ 248 - static const struct fetch_type fetch_type_table[] = { 249 - /* Special types */ 250 - [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string, 251 - sizeof(u32), 1, "__data_loc char[]"), 252 - [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32, 253 - string_size, sizeof(u32), 0, "u32"), 254 - /* Basic types */ 255 - ASSIGN_FETCH_TYPE(u8, u8, 0), 256 - ASSIGN_FETCH_TYPE(u16, u16, 0), 257 - ASSIGN_FETCH_TYPE(u32, u32, 0), 258 - ASSIGN_FETCH_TYPE(u64, u64, 0), 259 - ASSIGN_FETCH_TYPE(s8, u8, 1), 260 - ASSIGN_FETCH_TYPE(s16, u16, 1), 261 - ASSIGN_FETCH_TYPE(s32, u32, 1), 262 - ASSIGN_FETCH_TYPE(s64, u64, 1), 263 - }; 264 - 265 - static const struct fetch_type *find_fetch_type(const char *type) 377 + static const struct fetch_type *find_fetch_type(const char *type, 378 + const struct fetch_type *ftbl) 266 379 { 267 380 int i; 268 381 ··· 233 446 234 447 switch (bs) { 235 448 case 8: 236 - return find_fetch_type("u8"); 449 + return find_fetch_type("u8", ftbl); 237 450 case 16: 238 - return find_fetch_type("u16"); 451 + return find_fetch_type("u16", ftbl); 239 452 case 32: 240 - return find_fetch_type("u32"); 453 + return find_fetch_type("u32", ftbl); 241 454 case 64: 242 - return find_fetch_type("u64"); 455 + return find_fetch_type("u64", ftbl); 243 456 default: 244 457 goto fail; 245 458 } 246 459 } 247 460 248 - for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++) 249 - if (strcmp(type, fetch_type_table[i].name) == 0) 250 - return &fetch_type_table[i]; 461 + for (i = 0; ftbl[i].name; i++) { 462 + if (strcmp(type, ftbl[i].name) == 0) 463 + return &ftbl[i]; 464 + } 251 465 252 466 fail: 253 467 return NULL; 254 468 } 255 469 256 470 /* Special function : only accept unsigned long */ 257 - static __kprobes void fetch_stack_address(struct pt_regs *regs, 258 - void *dummy, void *dest) 471 + static __kprobes void fetch_kernel_stack_address(struct pt_regs *regs, 472 + void *dummy, void *dest) 259 473 { 260 474 *(unsigned long *)dest = kernel_stack_pointer(regs); 261 475 } 262 476 477 + static __kprobes void fetch_user_stack_address(struct pt_regs *regs, 478 + void *dummy, void *dest) 479 + { 480 + *(unsigned long *)dest = user_stack_pointer(regs); 481 + } 482 + 263 483 static fetch_func_t get_fetch_size_function(const struct fetch_type *type, 264 - fetch_func_t orig_fn) 484 + fetch_func_t orig_fn, 485 + const struct fetch_type *ftbl) 265 486 { 266 487 int i; 267 488 268 - if (type != &fetch_type_table[FETCH_TYPE_STRING]) 489 + if (type != &ftbl[FETCH_TYPE_STRING]) 269 490 return NULL; /* Only string type needs size function */ 270 491 271 492 for (i = 0; i < FETCH_MTD_END; i++) 272 493 if (type->fetch[i] == orig_fn) 273 - return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i]; 494 + return ftbl[FETCH_TYPE_STRSIZE].fetch[i]; 274 495 275 496 WARN_ON(1); /* This should not happen */ 276 497 ··· 311 516 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) 312 517 313 518 static int parse_probe_vars(char *arg, const struct fetch_type *t, 314 - struct fetch_param *f, bool is_return) 519 + struct fetch_param *f, bool is_return, 520 + bool is_kprobe) 315 521 { 316 522 int ret = 0; 317 523 unsigned long param; ··· 324 528 ret = -EINVAL; 325 529 } else if (strncmp(arg, "stack", 5) == 0) { 326 530 if (arg[5] == '\0') { 327 - if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0) 328 - f->fn = fetch_stack_address; 531 + if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR)) 532 + return -EINVAL; 533 + 534 + if (is_kprobe) 535 + f->fn = fetch_kernel_stack_address; 329 536 else 330 - ret = -EINVAL; 537 + f->fn = fetch_user_stack_address; 331 538 } else if (isdigit(arg[5])) { 332 539 ret = kstrtoul(arg + 5, 10, &param); 333 - if (ret || param > PARAM_MAX_STACK) 540 + if (ret || (is_kprobe && param > PARAM_MAX_STACK)) 334 541 ret = -EINVAL; 335 542 else { 336 543 f->fn = t->fetch[FETCH_MTD_stack]; ··· 351 552 static int parse_probe_arg(char *arg, const struct fetch_type *t, 352 553 struct fetch_param *f, bool is_return, bool is_kprobe) 353 554 { 555 + const struct fetch_type *ftbl; 354 556 unsigned long param; 355 557 long offset; 356 558 char *tmp; 357 - int ret; 559 + int ret = 0; 358 560 359 - ret = 0; 360 - 361 - /* Until uprobe_events supports only reg arguments */ 362 - if (!is_kprobe && arg[0] != '%') 363 - return -EINVAL; 561 + ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; 562 + BUG_ON(ftbl == NULL); 364 563 365 564 switch (arg[0]) { 366 565 case '$': 367 - ret = parse_probe_vars(arg + 1, t, f, is_return); 566 + ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe); 368 567 break; 369 568 370 569 case '%': /* named register */ ··· 374 577 } 375 578 break; 376 579 377 - case '@': /* memory or symbol */ 580 + case '@': /* memory, file-offset or symbol */ 378 581 if (isdigit(arg[1])) { 379 582 ret = kstrtoul(arg + 1, 0, &param); 380 583 if (ret) ··· 382 585 383 586 f->fn = t->fetch[FETCH_MTD_memory]; 384 587 f->data = (void *)param; 588 + } else if (arg[1] == '+') { 589 + /* kprobes don't support file offsets */ 590 + if (is_kprobe) 591 + return -EINVAL; 592 + 593 + ret = kstrtol(arg + 2, 0, &offset); 594 + if (ret) 595 + break; 596 + 597 + f->fn = t->fetch[FETCH_MTD_file_offset]; 598 + f->data = (void *)offset; 385 599 } else { 600 + /* uprobes don't support symbols */ 601 + if (!is_kprobe) 602 + return -EINVAL; 603 + 386 604 ret = traceprobe_split_symbol_offset(arg + 1, &offset); 387 605 if (ret) 388 606 break; ··· 428 616 struct deref_fetch_param *dprm; 429 617 const struct fetch_type *t2; 430 618 431 - t2 = find_fetch_type(NULL); 619 + t2 = find_fetch_type(NULL, ftbl); 432 620 *tmp = '\0'; 433 621 dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL); 434 622 ··· 436 624 return -ENOMEM; 437 625 438 626 dprm->offset = offset; 627 + dprm->fetch = t->fetch[FETCH_MTD_memory]; 628 + dprm->fetch_size = get_fetch_size_function(t, 629 + dprm->fetch, ftbl); 439 630 ret = parse_probe_arg(arg, t2, &dprm->orig, is_return, 440 631 is_kprobe); 441 632 if (ret) ··· 500 685 int traceprobe_parse_probe_arg(char *arg, ssize_t *size, 501 686 struct probe_arg *parg, bool is_return, bool is_kprobe) 502 687 { 688 + const struct fetch_type *ftbl; 503 689 const char *t; 504 690 int ret; 691 + 692 + ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; 693 + BUG_ON(ftbl == NULL); 505 694 506 695 if (strlen(arg) > MAX_ARGSTR_LEN) { 507 696 pr_info("Argument is too long.: %s\n", arg); ··· 521 702 arg[t - parg->comm] = '\0'; 522 703 t++; 523 704 } 524 - parg->type = find_fetch_type(t); 705 + parg->type = find_fetch_type(t, ftbl); 525 706 if (!parg->type) { 526 707 pr_info("Unsupported type: %s\n", t); 527 708 return -EINVAL; ··· 535 716 536 717 if (ret >= 0) { 537 718 parg->fetch_size.fn = get_fetch_size_function(parg->type, 538 - parg->fetch.fn); 719 + parg->fetch.fn, 720 + ftbl); 539 721 parg->fetch_size.data = parg->fetch.data; 540 722 } 541 723 ··· 656 836 kfree(kbuf); 657 837 658 838 return ret; 839 + } 840 + 841 + static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, 842 + bool is_return) 843 + { 844 + int i; 845 + int pos = 0; 846 + 847 + const char *fmt, *arg; 848 + 849 + if (!is_return) { 850 + fmt = "(%lx)"; 851 + arg = "REC->" FIELD_STRING_IP; 852 + } else { 853 + fmt = "(%lx <- %lx)"; 854 + arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 855 + } 856 + 857 + /* When len=0, we just calculate the needed length */ 858 + #define LEN_OR_ZERO (len ? len - pos : 0) 859 + 860 + pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 861 + 862 + for (i = 0; i < tp->nr_args; i++) { 863 + pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", 864 + tp->args[i].name, tp->args[i].type->fmt); 865 + } 866 + 867 + pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); 868 + 869 + for (i = 0; i < tp->nr_args; i++) { 870 + if (strcmp(tp->args[i].type->name, "string") == 0) 871 + pos += snprintf(buf + pos, LEN_OR_ZERO, 872 + ", __get_str(%s)", 873 + tp->args[i].name); 874 + else 875 + pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", 876 + tp->args[i].name); 877 + } 878 + 879 + #undef LEN_OR_ZERO 880 + 881 + /* return the length of print_fmt */ 882 + return pos; 883 + } 884 + 885 + int set_print_fmt(struct trace_probe *tp, bool is_return) 886 + { 887 + int len; 888 + char *print_fmt; 889 + 890 + /* First: called with 0 length to calculate the needed length */ 891 + len = __set_print_fmt(tp, NULL, 0, is_return); 892 + print_fmt = kmalloc(len + 1, GFP_KERNEL); 893 + if (!print_fmt) 894 + return -ENOMEM; 895 + 896 + /* Second: actually write the @print_fmt */ 897 + __set_print_fmt(tp, print_fmt, len + 1, is_return); 898 + tp->call.print_fmt = print_fmt; 899 + 900 + return 0; 659 901 }
+224
kernel/trace/trace_probe.h
··· 81 81 */ 82 82 #define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs)) 83 83 84 + static inline void *get_rloc_data(u32 *dl) 85 + { 86 + return (u8 *)dl + get_rloc_offs(*dl); 87 + } 88 + 89 + /* For data_loc conversion */ 90 + static inline void *get_loc_data(u32 *dl, void *ent) 91 + { 92 + return (u8 *)ent + get_rloc_offs(*dl); 93 + } 94 + 84 95 /* Data fetch function type */ 85 96 typedef void (*fetch_func_t)(struct pt_regs *, void *, void *); 86 97 /* Printing function type */ ··· 106 95 FETCH_MTD_symbol, 107 96 FETCH_MTD_deref, 108 97 FETCH_MTD_bitfield, 98 + FETCH_MTD_file_offset, 109 99 FETCH_MTD_END, 110 100 }; 111 101 ··· 127 115 void *data; 128 116 }; 129 117 118 + /* For defining macros, define string/string_size types */ 119 + typedef u32 string; 120 + typedef u32 string_size; 121 + 122 + #define PRINT_TYPE_FUNC_NAME(type) print_type_##type 123 + #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type 124 + 125 + /* Printing in basic type function template */ 126 + #define DECLARE_BASIC_PRINT_TYPE_FUNC(type) \ 127 + __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ 128 + const char *name, \ 129 + void *data, void *ent); \ 130 + extern const char PRINT_TYPE_FMT_NAME(type)[] 131 + 132 + DECLARE_BASIC_PRINT_TYPE_FUNC(u8); 133 + DECLARE_BASIC_PRINT_TYPE_FUNC(u16); 134 + DECLARE_BASIC_PRINT_TYPE_FUNC(u32); 135 + DECLARE_BASIC_PRINT_TYPE_FUNC(u64); 136 + DECLARE_BASIC_PRINT_TYPE_FUNC(s8); 137 + DECLARE_BASIC_PRINT_TYPE_FUNC(s16); 138 + DECLARE_BASIC_PRINT_TYPE_FUNC(s32); 139 + DECLARE_BASIC_PRINT_TYPE_FUNC(s64); 140 + DECLARE_BASIC_PRINT_TYPE_FUNC(string); 141 + 142 + #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type 143 + 144 + /* Declare macro for basic types */ 145 + #define DECLARE_FETCH_FUNC(method, type) \ 146 + extern void FETCH_FUNC_NAME(method, type)(struct pt_regs *regs, \ 147 + void *data, void *dest) 148 + 149 + #define DECLARE_BASIC_FETCH_FUNCS(method) \ 150 + DECLARE_FETCH_FUNC(method, u8); \ 151 + DECLARE_FETCH_FUNC(method, u16); \ 152 + DECLARE_FETCH_FUNC(method, u32); \ 153 + DECLARE_FETCH_FUNC(method, u64) 154 + 155 + DECLARE_BASIC_FETCH_FUNCS(reg); 156 + #define fetch_reg_string NULL 157 + #define fetch_reg_string_size NULL 158 + 159 + DECLARE_BASIC_FETCH_FUNCS(retval); 160 + #define fetch_retval_string NULL 161 + #define fetch_retval_string_size NULL 162 + 163 + DECLARE_BASIC_FETCH_FUNCS(symbol); 164 + DECLARE_FETCH_FUNC(symbol, string); 165 + DECLARE_FETCH_FUNC(symbol, string_size); 166 + 167 + DECLARE_BASIC_FETCH_FUNCS(deref); 168 + DECLARE_FETCH_FUNC(deref, string); 169 + DECLARE_FETCH_FUNC(deref, string_size); 170 + 171 + DECLARE_BASIC_FETCH_FUNCS(bitfield); 172 + #define fetch_bitfield_string NULL 173 + #define fetch_bitfield_string_size NULL 174 + 175 + /* 176 + * Define macro for basic types - we don't need to define s* types, because 177 + * we have to care only about bitwidth at recording time. 178 + */ 179 + #define DEFINE_BASIC_FETCH_FUNCS(method) \ 180 + DEFINE_FETCH_##method(u8) \ 181 + DEFINE_FETCH_##method(u16) \ 182 + DEFINE_FETCH_##method(u32) \ 183 + DEFINE_FETCH_##method(u64) 184 + 185 + /* Default (unsigned long) fetch type */ 186 + #define __DEFAULT_FETCH_TYPE(t) u##t 187 + #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t) 188 + #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG) 189 + #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE) 190 + 191 + #define ASSIGN_FETCH_FUNC(method, type) \ 192 + [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type) 193 + 194 + #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \ 195 + {.name = _name, \ 196 + .size = _size, \ 197 + .is_signed = sign, \ 198 + .print = PRINT_TYPE_FUNC_NAME(ptype), \ 199 + .fmt = PRINT_TYPE_FMT_NAME(ptype), \ 200 + .fmttype = _fmttype, \ 201 + .fetch = { \ 202 + ASSIGN_FETCH_FUNC(reg, ftype), \ 203 + ASSIGN_FETCH_FUNC(stack, ftype), \ 204 + ASSIGN_FETCH_FUNC(retval, ftype), \ 205 + ASSIGN_FETCH_FUNC(memory, ftype), \ 206 + ASSIGN_FETCH_FUNC(symbol, ftype), \ 207 + ASSIGN_FETCH_FUNC(deref, ftype), \ 208 + ASSIGN_FETCH_FUNC(bitfield, ftype), \ 209 + ASSIGN_FETCH_FUNC(file_offset, ftype), \ 210 + } \ 211 + } 212 + 213 + #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \ 214 + __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype) 215 + 216 + #define ASSIGN_FETCH_TYPE_END {} 217 + 218 + #define FETCH_TYPE_STRING 0 219 + #define FETCH_TYPE_STRSIZE 1 220 + 221 + /* 222 + * Fetch type information table. 223 + * It's declared as a weak symbol due to conditional compilation. 224 + */ 225 + extern __weak const struct fetch_type kprobes_fetch_type_table[]; 226 + extern __weak const struct fetch_type uprobes_fetch_type_table[]; 227 + 228 + #ifdef CONFIG_KPROBE_EVENT 229 + struct symbol_cache; 230 + unsigned long update_symbol_cache(struct symbol_cache *sc); 231 + void free_symbol_cache(struct symbol_cache *sc); 232 + struct symbol_cache *alloc_symbol_cache(const char *sym, long offset); 233 + #else 234 + /* uprobes do not support symbol fetch methods */ 235 + #define fetch_symbol_u8 NULL 236 + #define fetch_symbol_u16 NULL 237 + #define fetch_symbol_u32 NULL 238 + #define fetch_symbol_u64 NULL 239 + #define fetch_symbol_string NULL 240 + #define fetch_symbol_string_size NULL 241 + 242 + struct symbol_cache { 243 + }; 244 + static inline unsigned long __used update_symbol_cache(struct symbol_cache *sc) 245 + { 246 + return 0; 247 + } 248 + 249 + static inline void __used free_symbol_cache(struct symbol_cache *sc) 250 + { 251 + } 252 + 253 + static inline struct symbol_cache * __used 254 + alloc_symbol_cache(const char *sym, long offset) 255 + { 256 + return NULL; 257 + } 258 + #endif /* CONFIG_KPROBE_EVENT */ 259 + 130 260 struct probe_arg { 131 261 struct fetch_param fetch; 132 262 struct fetch_param fetch_size; ··· 277 123 const char *comm; /* Command of this argument */ 278 124 const struct fetch_type *type; /* Type of this argument */ 279 125 }; 126 + 127 + struct trace_probe { 128 + unsigned int flags; /* For TP_FLAG_* */ 129 + struct ftrace_event_class class; 130 + struct ftrace_event_call call; 131 + struct list_head files; 132 + ssize_t size; /* trace entry size */ 133 + unsigned int nr_args; 134 + struct probe_arg args[]; 135 + }; 136 + 137 + static inline bool trace_probe_is_enabled(struct trace_probe *tp) 138 + { 139 + return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE)); 140 + } 141 + 142 + static inline bool trace_probe_is_registered(struct trace_probe *tp) 143 + { 144 + return !!(tp->flags & TP_FLAG_REGISTERED); 145 + } 280 146 281 147 static inline __kprobes void call_fetch(struct fetch_param *fprm, 282 148 struct pt_regs *regs, void *dest) ··· 332 158 int (*createfn)(int, char**)); 333 159 334 160 extern int traceprobe_command(const char *buf, int (*createfn)(int, char**)); 161 + 162 + /* Sum up total data length for dynamic arraies (strings) */ 163 + static inline __kprobes int 164 + __get_data_size(struct trace_probe *tp, struct pt_regs *regs) 165 + { 166 + int i, ret = 0; 167 + u32 len; 168 + 169 + for (i = 0; i < tp->nr_args; i++) 170 + if (unlikely(tp->args[i].fetch_size.fn)) { 171 + call_fetch(&tp->args[i].fetch_size, regs, &len); 172 + ret += len; 173 + } 174 + 175 + return ret; 176 + } 177 + 178 + /* Store the value of each argument */ 179 + static inline __kprobes void 180 + store_trace_args(int ent_size, struct trace_probe *tp, struct pt_regs *regs, 181 + u8 *data, int maxlen) 182 + { 183 + int i; 184 + u32 end = tp->size; 185 + u32 *dl; /* Data (relative) location */ 186 + 187 + for (i = 0; i < tp->nr_args; i++) { 188 + if (unlikely(tp->args[i].fetch_size.fn)) { 189 + /* 190 + * First, we set the relative location and 191 + * maximum data length to *dl 192 + */ 193 + dl = (u32 *)(data + tp->args[i].offset); 194 + *dl = make_data_rloc(maxlen, end - tp->args[i].offset); 195 + /* Then try to fetch string or dynamic array data */ 196 + call_fetch(&tp->args[i].fetch, regs, dl); 197 + /* Reduce maximum length */ 198 + end += get_rloc_len(*dl); 199 + maxlen -= get_rloc_len(*dl); 200 + /* Trick here, convert data_rloc to data_loc */ 201 + *dl = convert_rloc_to_loc(*dl, 202 + ent_size + tp->args[i].offset); 203 + } else 204 + /* Just fetching data normally */ 205 + call_fetch(&tp->args[i].fetch, regs, 206 + data + tp->args[i].offset); 207 + } 208 + } 209 + 210 + extern int set_print_fmt(struct trace_probe *tp, bool is_return);
+1 -1
kernel/trace/trace_stack.c
··· 382 382 .open = stack_trace_filter_open, 383 383 .read = seq_read, 384 384 .write = ftrace_filter_write, 385 - .llseek = ftrace_filter_lseek, 385 + .llseek = tracing_lseek, 386 386 .release = ftrace_regex_release, 387 387 }; 388 388
+6 -8
kernel/trace/trace_syscalls.c
··· 321 321 if (!ftrace_file) 322 322 return; 323 323 324 - if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags)) 324 + if (ftrace_trigger_soft_disabled(ftrace_file)) 325 325 return; 326 326 327 327 sys_data = syscall_nr_to_meta(syscall_nr); ··· 343 343 entry->nr = syscall_nr; 344 344 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args); 345 345 346 - if (!filter_check_discard(ftrace_file, entry, buffer, event)) 347 - trace_current_buffer_unlock_commit(buffer, event, 348 - irq_flags, pc); 346 + event_trigger_unlock_commit(ftrace_file, buffer, event, entry, 347 + irq_flags, pc); 349 348 } 350 349 351 350 static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret) ··· 368 369 if (!ftrace_file) 369 370 return; 370 371 371 - if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags)) 372 + if (ftrace_trigger_soft_disabled(ftrace_file)) 372 373 return; 373 374 374 375 sys_data = syscall_nr_to_meta(syscall_nr); ··· 389 390 entry->nr = syscall_nr; 390 391 entry->ret = syscall_get_return_value(current, regs); 391 392 392 - if (!filter_check_discard(ftrace_file, entry, buffer, event)) 393 - trace_current_buffer_unlock_commit(buffer, event, 394 - irq_flags, pc); 393 + event_trigger_unlock_commit(ftrace_file, buffer, event, entry, 394 + irq_flags, pc); 395 395 } 396 396 397 397 static int reg_event_syscall_enter(struct ftrace_event_file *file,
+353 -136
kernel/trace/trace_uprobe.c
··· 51 51 */ 52 52 struct trace_uprobe { 53 53 struct list_head list; 54 - struct ftrace_event_class class; 55 - struct ftrace_event_call call; 56 54 struct trace_uprobe_filter filter; 57 55 struct uprobe_consumer consumer; 58 56 struct inode *inode; 59 57 char *filename; 60 58 unsigned long offset; 61 59 unsigned long nhit; 62 - unsigned int flags; /* For TP_FLAG_* */ 63 - ssize_t size; /* trace entry size */ 64 - unsigned int nr_args; 65 - struct probe_arg args[]; 60 + struct trace_probe tp; 66 61 }; 67 62 68 - #define SIZEOF_TRACE_UPROBE(n) \ 69 - (offsetof(struct trace_uprobe, args) + \ 63 + #define SIZEOF_TRACE_UPROBE(n) \ 64 + (offsetof(struct trace_uprobe, tp.args) + \ 70 65 (sizeof(struct probe_arg) * (n))) 71 66 72 67 static int register_uprobe_event(struct trace_uprobe *tu); ··· 70 75 static DEFINE_MUTEX(uprobe_lock); 71 76 static LIST_HEAD(uprobe_list); 72 77 78 + struct uprobe_dispatch_data { 79 + struct trace_uprobe *tu; 80 + unsigned long bp_addr; 81 + }; 82 + 73 83 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); 74 84 static int uretprobe_dispatcher(struct uprobe_consumer *con, 75 85 unsigned long func, struct pt_regs *regs); 86 + 87 + #ifdef CONFIG_STACK_GROWSUP 88 + static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) 89 + { 90 + return addr - (n * sizeof(long)); 91 + } 92 + #else 93 + static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) 94 + { 95 + return addr + (n * sizeof(long)); 96 + } 97 + #endif 98 + 99 + static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) 100 + { 101 + unsigned long ret; 102 + unsigned long addr = user_stack_pointer(regs); 103 + 104 + addr = adjust_stack_addr(addr, n); 105 + 106 + if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) 107 + return 0; 108 + 109 + return ret; 110 + } 111 + 112 + /* 113 + * Uprobes-specific fetch functions 114 + */ 115 + #define DEFINE_FETCH_stack(type) \ 116 + static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ 117 + void *offset, void *dest) \ 118 + { \ 119 + *(type *)dest = (type)get_user_stack_nth(regs, \ 120 + ((unsigned long)offset)); \ 121 + } 122 + DEFINE_BASIC_FETCH_FUNCS(stack) 123 + /* No string on the stack entry */ 124 + #define fetch_stack_string NULL 125 + #define fetch_stack_string_size NULL 126 + 127 + #define DEFINE_FETCH_memory(type) \ 128 + static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ 129 + void *addr, void *dest) \ 130 + { \ 131 + type retval; \ 132 + void __user *vaddr = (void __force __user *) addr; \ 133 + \ 134 + if (copy_from_user(&retval, vaddr, sizeof(type))) \ 135 + *(type *)dest = 0; \ 136 + else \ 137 + *(type *) dest = retval; \ 138 + } 139 + DEFINE_BASIC_FETCH_FUNCS(memory) 140 + /* 141 + * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max 142 + * length and relative data location. 143 + */ 144 + static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, 145 + void *addr, void *dest) 146 + { 147 + long ret; 148 + u32 rloc = *(u32 *)dest; 149 + int maxlen = get_rloc_len(rloc); 150 + u8 *dst = get_rloc_data(dest); 151 + void __user *src = (void __force __user *) addr; 152 + 153 + if (!maxlen) 154 + return; 155 + 156 + ret = strncpy_from_user(dst, src, maxlen); 157 + 158 + if (ret < 0) { /* Failed to fetch string */ 159 + ((u8 *)get_rloc_data(dest))[0] = '\0'; 160 + *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc)); 161 + } else { 162 + *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc)); 163 + } 164 + } 165 + 166 + static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs, 167 + void *addr, void *dest) 168 + { 169 + int len; 170 + void __user *vaddr = (void __force __user *) addr; 171 + 172 + len = strnlen_user(vaddr, MAX_STRING_SIZE); 173 + 174 + if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */ 175 + *(u32 *)dest = 0; 176 + else 177 + *(u32 *)dest = len; 178 + } 179 + 180 + static unsigned long translate_user_vaddr(void *file_offset) 181 + { 182 + unsigned long base_addr; 183 + struct uprobe_dispatch_data *udd; 184 + 185 + udd = (void *) current->utask->vaddr; 186 + 187 + base_addr = udd->bp_addr - udd->tu->offset; 188 + return base_addr + (unsigned long)file_offset; 189 + } 190 + 191 + #define DEFINE_FETCH_file_offset(type) \ 192 + static __kprobes void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs,\ 193 + void *offset, void *dest) \ 194 + { \ 195 + void *vaddr = (void *)translate_user_vaddr(offset); \ 196 + \ 197 + FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \ 198 + } 199 + DEFINE_BASIC_FETCH_FUNCS(file_offset) 200 + DEFINE_FETCH_file_offset(string) 201 + DEFINE_FETCH_file_offset(string_size) 202 + 203 + /* Fetch type information table */ 204 + const struct fetch_type uprobes_fetch_type_table[] = { 205 + /* Special types */ 206 + [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string, 207 + sizeof(u32), 1, "__data_loc char[]"), 208 + [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32, 209 + string_size, sizeof(u32), 0, "u32"), 210 + /* Basic types */ 211 + ASSIGN_FETCH_TYPE(u8, u8, 0), 212 + ASSIGN_FETCH_TYPE(u16, u16, 0), 213 + ASSIGN_FETCH_TYPE(u32, u32, 0), 214 + ASSIGN_FETCH_TYPE(u64, u64, 0), 215 + ASSIGN_FETCH_TYPE(s8, u8, 1), 216 + ASSIGN_FETCH_TYPE(s16, u16, 1), 217 + ASSIGN_FETCH_TYPE(s32, u32, 1), 218 + ASSIGN_FETCH_TYPE(s64, u64, 1), 219 + 220 + ASSIGN_FETCH_TYPE_END 221 + }; 76 222 77 223 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) 78 224 { ··· 250 114 if (!tu) 251 115 return ERR_PTR(-ENOMEM); 252 116 253 - tu->call.class = &tu->class; 254 - tu->call.name = kstrdup(event, GFP_KERNEL); 255 - if (!tu->call.name) 117 + tu->tp.call.class = &tu->tp.class; 118 + tu->tp.call.name = kstrdup(event, GFP_KERNEL); 119 + if (!tu->tp.call.name) 256 120 goto error; 257 121 258 - tu->class.system = kstrdup(group, GFP_KERNEL); 259 - if (!tu->class.system) 122 + tu->tp.class.system = kstrdup(group, GFP_KERNEL); 123 + if (!tu->tp.class.system) 260 124 goto error; 261 125 262 126 INIT_LIST_HEAD(&tu->list); ··· 264 128 if (is_ret) 265 129 tu->consumer.ret_handler = uretprobe_dispatcher; 266 130 init_trace_uprobe_filter(&tu->filter); 267 - tu->call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER; 131 + tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER; 268 132 return tu; 269 133 270 134 error: 271 - kfree(tu->call.name); 135 + kfree(tu->tp.call.name); 272 136 kfree(tu); 273 137 274 138 return ERR_PTR(-ENOMEM); ··· 278 142 { 279 143 int i; 280 144 281 - for (i = 0; i < tu->nr_args; i++) 282 - traceprobe_free_probe_arg(&tu->args[i]); 145 + for (i = 0; i < tu->tp.nr_args; i++) 146 + traceprobe_free_probe_arg(&tu->tp.args[i]); 283 147 284 148 iput(tu->inode); 285 - kfree(tu->call.class->system); 286 - kfree(tu->call.name); 149 + kfree(tu->tp.call.class->system); 150 + kfree(tu->tp.call.name); 287 151 kfree(tu->filename); 288 152 kfree(tu); 289 153 } ··· 293 157 struct trace_uprobe *tu; 294 158 295 159 list_for_each_entry(tu, &uprobe_list, list) 296 - if (strcmp(tu->call.name, event) == 0 && 297 - strcmp(tu->call.class->system, group) == 0) 160 + if (strcmp(tu->tp.call.name, event) == 0 && 161 + strcmp(tu->tp.call.class->system, group) == 0) 298 162 return tu; 299 163 300 164 return NULL; ··· 317 181 /* Register a trace_uprobe and probe_event */ 318 182 static int register_trace_uprobe(struct trace_uprobe *tu) 319 183 { 320 - struct trace_uprobe *old_tp; 184 + struct trace_uprobe *old_tu; 321 185 int ret; 322 186 323 187 mutex_lock(&uprobe_lock); 324 188 325 189 /* register as an event */ 326 - old_tp = find_probe_event(tu->call.name, tu->call.class->system); 327 - if (old_tp) { 190 + old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system); 191 + if (old_tu) { 328 192 /* delete old event */ 329 - ret = unregister_trace_uprobe(old_tp); 193 + ret = unregister_trace_uprobe(old_tu); 330 194 if (ret) 331 195 goto end; 332 196 } ··· 347 211 348 212 /* 349 213 * Argument syntax: 350 - * - Add uprobe: p|r[:[GRP/]EVENT] PATH:SYMBOL [FETCHARGS] 214 + * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] 351 215 * 352 216 * - Remove uprobe: -:[GRP/]EVENT 353 217 */ ··· 496 360 /* parse arguments */ 497 361 ret = 0; 498 362 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { 363 + struct probe_arg *parg = &tu->tp.args[i]; 364 + 499 365 /* Increment count for freeing args in error case */ 500 - tu->nr_args++; 366 + tu->tp.nr_args++; 501 367 502 368 /* Parse argument name */ 503 369 arg = strchr(argv[i], '='); 504 370 if (arg) { 505 371 *arg++ = '\0'; 506 - tu->args[i].name = kstrdup(argv[i], GFP_KERNEL); 372 + parg->name = kstrdup(argv[i], GFP_KERNEL); 507 373 } else { 508 374 arg = argv[i]; 509 375 /* If argument name is omitted, set "argN" */ 510 376 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); 511 - tu->args[i].name = kstrdup(buf, GFP_KERNEL); 377 + parg->name = kstrdup(buf, GFP_KERNEL); 512 378 } 513 379 514 - if (!tu->args[i].name) { 380 + if (!parg->name) { 515 381 pr_info("Failed to allocate argument[%d] name.\n", i); 516 382 ret = -ENOMEM; 517 383 goto error; 518 384 } 519 385 520 - if (!is_good_name(tu->args[i].name)) { 521 - pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name); 386 + if (!is_good_name(parg->name)) { 387 + pr_info("Invalid argument[%d] name: %s\n", i, parg->name); 522 388 ret = -EINVAL; 523 389 goto error; 524 390 } 525 391 526 - if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) { 392 + if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) { 527 393 pr_info("Argument[%d] name '%s' conflicts with " 528 394 "another field.\n", i, argv[i]); 529 395 ret = -EINVAL; ··· 533 395 } 534 396 535 397 /* Parse fetch argument */ 536 - ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false); 398 + ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg, 399 + is_return, false); 537 400 if (ret) { 538 401 pr_info("Parse error at argument[%d]. (%d)\n", i, ret); 539 402 goto error; ··· 598 459 char c = is_ret_probe(tu) ? 'r' : 'p'; 599 460 int i; 600 461 601 - seq_printf(m, "%c:%s/%s", c, tu->call.class->system, tu->call.name); 462 + seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name); 602 463 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset); 603 464 604 - for (i = 0; i < tu->nr_args; i++) 605 - seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm); 465 + for (i = 0; i < tu->tp.nr_args; i++) 466 + seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); 606 467 607 468 seq_printf(m, "\n"); 608 469 return 0; ··· 648 509 { 649 510 struct trace_uprobe *tu = v; 650 511 651 - seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit); 512 + seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit); 652 513 return 0; 653 514 } 654 515 ··· 672 533 .release = seq_release, 673 534 }; 674 535 536 + struct uprobe_cpu_buffer { 537 + struct mutex mutex; 538 + void *buf; 539 + }; 540 + static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; 541 + static int uprobe_buffer_refcnt; 542 + 543 + static int uprobe_buffer_init(void) 544 + { 545 + int cpu, err_cpu; 546 + 547 + uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); 548 + if (uprobe_cpu_buffer == NULL) 549 + return -ENOMEM; 550 + 551 + for_each_possible_cpu(cpu) { 552 + struct page *p = alloc_pages_node(cpu_to_node(cpu), 553 + GFP_KERNEL, 0); 554 + if (p == NULL) { 555 + err_cpu = cpu; 556 + goto err; 557 + } 558 + per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); 559 + mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); 560 + } 561 + 562 + return 0; 563 + 564 + err: 565 + for_each_possible_cpu(cpu) { 566 + if (cpu == err_cpu) 567 + break; 568 + free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); 569 + } 570 + 571 + free_percpu(uprobe_cpu_buffer); 572 + return -ENOMEM; 573 + } 574 + 575 + static int uprobe_buffer_enable(void) 576 + { 577 + int ret = 0; 578 + 579 + BUG_ON(!mutex_is_locked(&event_mutex)); 580 + 581 + if (uprobe_buffer_refcnt++ == 0) { 582 + ret = uprobe_buffer_init(); 583 + if (ret < 0) 584 + uprobe_buffer_refcnt--; 585 + } 586 + 587 + return ret; 588 + } 589 + 590 + static void uprobe_buffer_disable(void) 591 + { 592 + BUG_ON(!mutex_is_locked(&event_mutex)); 593 + 594 + if (--uprobe_buffer_refcnt == 0) { 595 + free_percpu(uprobe_cpu_buffer); 596 + uprobe_cpu_buffer = NULL; 597 + } 598 + } 599 + 600 + static struct uprobe_cpu_buffer *uprobe_buffer_get(void) 601 + { 602 + struct uprobe_cpu_buffer *ucb; 603 + int cpu; 604 + 605 + cpu = raw_smp_processor_id(); 606 + ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); 607 + 608 + /* 609 + * Use per-cpu buffers for fastest access, but we might migrate 610 + * so the mutex makes sure we have sole access to it. 611 + */ 612 + mutex_lock(&ucb->mutex); 613 + 614 + return ucb; 615 + } 616 + 617 + static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) 618 + { 619 + mutex_unlock(&ucb->mutex); 620 + } 621 + 675 622 static void uprobe_trace_print(struct trace_uprobe *tu, 676 623 unsigned long func, struct pt_regs *regs) 677 624 { 678 625 struct uprobe_trace_entry_head *entry; 679 626 struct ring_buffer_event *event; 680 627 struct ring_buffer *buffer; 628 + struct uprobe_cpu_buffer *ucb; 681 629 void *data; 682 - int size, i; 683 - struct ftrace_event_call *call = &tu->call; 630 + int size, dsize, esize; 631 + struct ftrace_event_call *call = &tu->tp.call; 684 632 685 - size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 686 - event = trace_current_buffer_lock_reserve(&buffer, call->event.type, 687 - size + tu->size, 0, 0); 688 - if (!event) 633 + dsize = __get_data_size(&tu->tp, regs); 634 + esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 635 + 636 + if (WARN_ON_ONCE(!uprobe_cpu_buffer || tu->tp.size + dsize > PAGE_SIZE)) 689 637 return; 638 + 639 + ucb = uprobe_buffer_get(); 640 + store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize); 641 + 642 + size = esize + tu->tp.size + dsize; 643 + event = trace_current_buffer_lock_reserve(&buffer, call->event.type, 644 + size, 0, 0); 645 + if (!event) 646 + goto out; 690 647 691 648 entry = ring_buffer_event_data(event); 692 649 if (is_ret_probe(tu)) { ··· 794 559 data = DATAOF_TRACE_ENTRY(entry, false); 795 560 } 796 561 797 - for (i = 0; i < tu->nr_args; i++) 798 - call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset); 562 + memcpy(data, ucb->buf, tu->tp.size + dsize); 799 563 800 564 if (!call_filter_check_discard(call, entry, buffer, event)) 801 565 trace_buffer_unlock_commit(buffer, event, 0, 0); 566 + 567 + out: 568 + uprobe_buffer_put(ucb); 802 569 } 803 570 804 571 /* uprobe handler */ ··· 828 591 int i; 829 592 830 593 entry = (struct uprobe_trace_entry_head *)iter->ent; 831 - tu = container_of(event, struct trace_uprobe, call.event); 594 + tu = container_of(event, struct trace_uprobe, tp.call.event); 832 595 833 596 if (is_ret_probe(tu)) { 834 - if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->call.name, 597 + if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name, 835 598 entry->vaddr[1], entry->vaddr[0])) 836 599 goto partial; 837 600 data = DATAOF_TRACE_ENTRY(entry, true); 838 601 } else { 839 - if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name, 602 + if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name, 840 603 entry->vaddr[0])) 841 604 goto partial; 842 605 data = DATAOF_TRACE_ENTRY(entry, false); 843 606 } 844 607 845 - for (i = 0; i < tu->nr_args; i++) { 846 - if (!tu->args[i].type->print(s, tu->args[i].name, 847 - data + tu->args[i].offset, entry)) 608 + for (i = 0; i < tu->tp.nr_args; i++) { 609 + struct probe_arg *parg = &tu->tp.args[i]; 610 + 611 + if (!parg->type->print(s, parg->name, data + parg->offset, entry)) 848 612 goto partial; 849 613 } 850 614 ··· 854 616 855 617 partial: 856 618 return TRACE_TYPE_PARTIAL_LINE; 857 - } 858 - 859 - static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu) 860 - { 861 - return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE); 862 619 } 863 620 864 621 typedef bool (*filter_func_t)(struct uprobe_consumer *self, ··· 865 632 { 866 633 int ret = 0; 867 634 868 - if (is_trace_uprobe_enabled(tu)) 635 + if (trace_probe_is_enabled(&tu->tp)) 869 636 return -EINTR; 637 + 638 + ret = uprobe_buffer_enable(); 639 + if (ret < 0) 640 + return ret; 870 641 871 642 WARN_ON(!uprobe_filter_is_empty(&tu->filter)); 872 643 873 - tu->flags |= flag; 644 + tu->tp.flags |= flag; 874 645 tu->consumer.filter = filter; 875 646 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); 876 647 if (ret) 877 - tu->flags &= ~flag; 648 + tu->tp.flags &= ~flag; 878 649 879 650 return ret; 880 651 } 881 652 882 653 static void probe_event_disable(struct trace_uprobe *tu, int flag) 883 654 { 884 - if (!is_trace_uprobe_enabled(tu)) 655 + if (!trace_probe_is_enabled(&tu->tp)) 885 656 return; 886 657 887 658 WARN_ON(!uprobe_filter_is_empty(&tu->filter)); 888 659 889 660 uprobe_unregister(tu->inode, tu->offset, &tu->consumer); 890 - tu->flags &= ~flag; 661 + tu->tp.flags &= ~flag; 662 + 663 + uprobe_buffer_disable(); 891 664 } 892 665 893 666 static int uprobe_event_define_fields(struct ftrace_event_call *event_call) ··· 911 672 size = SIZEOF_TRACE_ENTRY(false); 912 673 } 913 674 /* Set argument names as fields */ 914 - for (i = 0; i < tu->nr_args; i++) { 915 - ret = trace_define_field(event_call, tu->args[i].type->fmttype, 916 - tu->args[i].name, 917 - size + tu->args[i].offset, 918 - tu->args[i].type->size, 919 - tu->args[i].type->is_signed, 675 + for (i = 0; i < tu->tp.nr_args; i++) { 676 + struct probe_arg *parg = &tu->tp.args[i]; 677 + 678 + ret = trace_define_field(event_call, parg->type->fmttype, 679 + parg->name, size + parg->offset, 680 + parg->type->size, parg->type->is_signed, 920 681 FILTER_OTHER); 921 682 922 683 if (ret) 923 684 return ret; 924 685 } 925 - return 0; 926 - } 927 - 928 - #define LEN_OR_ZERO (len ? len - pos : 0) 929 - static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len) 930 - { 931 - const char *fmt, *arg; 932 - int i; 933 - int pos = 0; 934 - 935 - if (is_ret_probe(tu)) { 936 - fmt = "(%lx <- %lx)"; 937 - arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 938 - } else { 939 - fmt = "(%lx)"; 940 - arg = "REC->" FIELD_STRING_IP; 941 - } 942 - 943 - /* When len=0, we just calculate the needed length */ 944 - 945 - pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 946 - 947 - for (i = 0; i < tu->nr_args; i++) { 948 - pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", 949 - tu->args[i].name, tu->args[i].type->fmt); 950 - } 951 - 952 - pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); 953 - 954 - for (i = 0; i < tu->nr_args; i++) { 955 - pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", 956 - tu->args[i].name); 957 - } 958 - 959 - return pos; /* return the length of print_fmt */ 960 - } 961 - #undef LEN_OR_ZERO 962 - 963 - static int set_print_fmt(struct trace_uprobe *tu) 964 - { 965 - char *print_fmt; 966 - int len; 967 - 968 - /* First: called with 0 length to calculate the needed length */ 969 - len = __set_print_fmt(tu, NULL, 0); 970 - print_fmt = kmalloc(len + 1, GFP_KERNEL); 971 - if (!print_fmt) 972 - return -ENOMEM; 973 - 974 - /* Second: actually write the @print_fmt */ 975 - __set_print_fmt(tu, print_fmt, len + 1); 976 - tu->call.print_fmt = print_fmt; 977 - 978 686 return 0; 979 687 } 980 688 ··· 1017 831 static void uprobe_perf_print(struct trace_uprobe *tu, 1018 832 unsigned long func, struct pt_regs *regs) 1019 833 { 1020 - struct ftrace_event_call *call = &tu->call; 834 + struct ftrace_event_call *call = &tu->tp.call; 1021 835 struct uprobe_trace_entry_head *entry; 1022 836 struct hlist_head *head; 837 + struct uprobe_cpu_buffer *ucb; 1023 838 void *data; 1024 - int size, rctx, i; 839 + int size, dsize, esize; 840 + int rctx; 1025 841 1026 - size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 1027 - size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32); 842 + dsize = __get_data_size(&tu->tp, regs); 843 + esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 844 + 845 + if (WARN_ON_ONCE(!uprobe_cpu_buffer)) 846 + return; 847 + 848 + size = esize + tu->tp.size + dsize; 849 + size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); 850 + if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) 851 + return; 852 + 853 + ucb = uprobe_buffer_get(); 854 + store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize); 1028 855 1029 856 preempt_disable(); 1030 857 head = this_cpu_ptr(call->perf_events); ··· 1057 858 data = DATAOF_TRACE_ENTRY(entry, false); 1058 859 } 1059 860 1060 - for (i = 0; i < tu->nr_args; i++) 1061 - call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset); 861 + memcpy(data, ucb->buf, tu->tp.size + dsize); 862 + 863 + if (size - esize > tu->tp.size + dsize) { 864 + int len = tu->tp.size + dsize; 865 + 866 + memset(data + len, 0, size - esize - len); 867 + } 1062 868 1063 869 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); 1064 870 out: 1065 871 preempt_enable(); 872 + uprobe_buffer_put(ucb); 1066 873 } 1067 874 1068 875 /* uprobe profile handler */ ··· 1126 921 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) 1127 922 { 1128 923 struct trace_uprobe *tu; 924 + struct uprobe_dispatch_data udd; 1129 925 int ret = 0; 1130 926 1131 927 tu = container_of(con, struct trace_uprobe, consumer); 1132 928 tu->nhit++; 1133 929 1134 - if (tu->flags & TP_FLAG_TRACE) 930 + udd.tu = tu; 931 + udd.bp_addr = instruction_pointer(regs); 932 + 933 + current->utask->vaddr = (unsigned long) &udd; 934 + 935 + if (tu->tp.flags & TP_FLAG_TRACE) 1135 936 ret |= uprobe_trace_func(tu, regs); 1136 937 1137 938 #ifdef CONFIG_PERF_EVENTS 1138 - if (tu->flags & TP_FLAG_PROFILE) 939 + if (tu->tp.flags & TP_FLAG_PROFILE) 1139 940 ret |= uprobe_perf_func(tu, regs); 1140 941 #endif 1141 942 return ret; ··· 1151 940 unsigned long func, struct pt_regs *regs) 1152 941 { 1153 942 struct trace_uprobe *tu; 943 + struct uprobe_dispatch_data udd; 1154 944 1155 945 tu = container_of(con, struct trace_uprobe, consumer); 1156 946 1157 - if (tu->flags & TP_FLAG_TRACE) 947 + udd.tu = tu; 948 + udd.bp_addr = func; 949 + 950 + current->utask->vaddr = (unsigned long) &udd; 951 + 952 + if (tu->tp.flags & TP_FLAG_TRACE) 1158 953 uretprobe_trace_func(tu, func, regs); 1159 954 1160 955 #ifdef CONFIG_PERF_EVENTS 1161 - if (tu->flags & TP_FLAG_PROFILE) 956 + if (tu->tp.flags & TP_FLAG_PROFILE) 1162 957 uretprobe_perf_func(tu, func, regs); 1163 958 #endif 1164 959 return 0; ··· 1176 959 1177 960 static int register_uprobe_event(struct trace_uprobe *tu) 1178 961 { 1179 - struct ftrace_event_call *call = &tu->call; 962 + struct ftrace_event_call *call = &tu->tp.call; 1180 963 int ret; 1181 964 1182 965 /* Initialize ftrace_event_call */ ··· 1184 967 call->event.funcs = &uprobe_funcs; 1185 968 call->class->define_fields = uprobe_event_define_fields; 1186 969 1187 - if (set_print_fmt(tu) < 0) 970 + if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) 1188 971 return -ENOMEM; 1189 972 1190 973 ret = register_ftrace_event(&call->event); ··· 1211 994 int ret; 1212 995 1213 996 /* tu->event is unregistered in trace_remove_event_call() */ 1214 - ret = trace_remove_event_call(&tu->call); 997 + ret = trace_remove_event_call(&tu->tp.call); 1215 998 if (ret) 1216 999 return ret; 1217 - kfree(tu->call.print_fmt); 1218 - tu->call.print_fmt = NULL; 1000 + kfree(tu->tp.call.print_fmt); 1001 + tu->tp.call.print_fmt = NULL; 1219 1002 return 0; 1220 1003 } 1221 1004