Skip to content

Commit 85f2b08

Browse files
tzanussirostedt
authored andcommitted
tracing: Add basic event trigger framework
Add a 'trigger' file for each trace event, enabling 'trace event triggers' to be set for trace events. 'trace event triggers' are patterned after the existing 'ftrace function triggers' implementation except that triggers are written to per-event 'trigger' files instead of to a single file such as the 'set_ftrace_filter' used for ftrace function triggers. The implementation is meant to be entirely separate from ftrace function triggers, in order to keep the respective implementations relatively simple and to allow them to diverge. The event trigger functionality is built on top of SOFT_DISABLE functionality. It adds a TRIGGER_MODE bit to the ftrace_event_file flags which is checked when any trace event fires. Triggers set for a particular event need to be checked regardless of whether that event is actually enabled or not - getting an event to fire even if it's not enabled is what's already implemented by SOFT_DISABLE mode, so trigger mode directly reuses that. Event trigger essentially inherit the soft disable logic in __ftrace_event_enable_disable() while adding a bit of logic and trigger reference counting via tm_ref on top of that in a new trace_event_trigger_enable_disable() function. Because the base __ftrace_event_enable_disable() code now needs to be invoked from outside trace_events.c, a wrapper is also added for those usages. The triggers for an event are actually invoked via a new function, event_triggers_call(), and code is also added to invoke them for ftrace_raw_event calls as well as syscall events. The main part of the patch creates a new trace_events_trigger.c file to contain the trace event triggers implementation. The standard open, read, and release file operations are implemented here. The open() implementation sets up for the various open modes of the 'trigger' file. It creates and attaches the trigger iterator and sets up the command parser. If opened for reading set up the trigger seq_ops. The read() implementation parses the event trigger written to the 'trigger' file, looks up the trigger command, and passes it along to that event_command's func() implementation for command-specific processing. The release() implementation does whatever cleanup is needed to release the 'trigger' file, like releasing the parser and trigger iterator, etc. A couple of functions for event command registration and unregistration are added, along with a list to add them to and a mutex to protect them, as well as an (initially empty) registration function to add the set of commands that will be added by future commits, and call to it from the trace event initialization code. also added are a couple trigger-specific data structures needed for these implementations such as a trigger iterator and a struct for trigger-specific data. A couple structs consisting mostly of function meant to be implemented in command-specific ways, event_command and event_trigger_ops, are used by the generic event trigger command implementations. They're being put into trace.h alongside the other trace_event data structures and functions, in the expectation that they'll be needed in several trace_event-related files such as trace_events_trigger.c and trace_events.c. The event_command.func() function is meant to be called by the trigger parsing code in order to add a trigger instance to the corresponding event. It essentially coordinates adding a live trigger instance to the event, and arming the triggering the event. Every event_command func() implementation essentially does the same thing for any command: - choose ops - use the value of param to choose either a number or count version of event_trigger_ops specific to the command - do the register or unregister of those ops - associate a filter, if specified, with the triggering event The reg() and unreg() ops allow command-specific implementations for event_trigger_op registration and unregistration, and the get_trigger_ops() op allows command-specific event_trigger_ops selection to be parameterized. When a trigger instance is added, the reg() op essentially adds that trigger to the triggering event and arms it, while unreg() does the opposite. The set_filter() function is used to associate a filter with the trigger - if the command doesn't specify a set_filter() implementation, the command will ignore filters. Each command has an associated trigger_type, which serves double duty, both as a unique identifier for the command as well as a value that can be used for setting a trigger mode bit during trigger invocation. The signature of func() adds a pointer to the event_command struct, used to invoke those functions, along with a command_data param that can be passed to the reg/unreg functions. This allows func() implementations to use command-specific blobs and supports code re-use. The event_trigger_ops.func() command corrsponds to the trigger 'probe' function that gets called when the triggering event is actually invoked. The other functions are used to list the trigger when needed, along with a couple mundane book-keeping functions. This also moves event_file_data() into trace.h so it can be used outside of trace_events.c. Link: http://lkml.kernel.org/r/316d95061accdee070aac8e5750afba0192fa5b9.1382622043.git.tom.zanussi@linux.intel.com Signed-off-by: Tom Zanussi <[email protected]> Idea-by: Steve Rostedt <[email protected]> Signed-off-by: Steven Rostedt <[email protected]>
1 parent 319e2e3 commit 85f2b08

File tree

7 files changed

+495
-5
lines changed

7 files changed

+495
-5
lines changed

include/linux/ftrace_event.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ enum {
264264
FTRACE_EVENT_FL_NO_SET_FILTER_BIT,
265265
FTRACE_EVENT_FL_SOFT_MODE_BIT,
266266
FTRACE_EVENT_FL_SOFT_DISABLED_BIT,
267+
FTRACE_EVENT_FL_TRIGGER_MODE_BIT,
267268
};
268269

269270
/*
@@ -275,6 +276,7 @@ enum {
275276
* SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
276277
* SOFT_DISABLED - When set, do not trace the event (even though its
277278
* tracepoint may be enabled)
279+
* TRIGGER_MODE - When set, invoke the triggers associated with the event
278280
*/
279281
enum {
280282
FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT),
@@ -283,6 +285,7 @@ enum {
283285
FTRACE_EVENT_FL_NO_SET_FILTER = (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT),
284286
FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
285287
FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
288+
FTRACE_EVENT_FL_TRIGGER_MODE = (1 << FTRACE_EVENT_FL_TRIGGER_MODE_BIT),
286289
};
287290

288291
struct ftrace_event_file {
@@ -292,13 +295,15 @@ struct ftrace_event_file {
292295
struct dentry *dir;
293296
struct trace_array *tr;
294297
struct ftrace_subsystem_dir *system;
298+
struct list_head triggers;
295299

296300
/*
297301
* 32 bit flags:
298302
* bit 0: enabled
299303
* bit 1: enabled cmd record
300304
* bit 2: enable/disable with the soft disable bit
301305
* bit 3: soft disabled
306+
* bit 4: trigger enabled
302307
*
303308
* Note: The bits must be set atomically to prevent races
304309
* from other writers. Reads of flags do not need to be in
@@ -310,6 +315,7 @@ struct ftrace_event_file {
310315
*/
311316
unsigned long flags;
312317
atomic_t sm_ref; /* soft-mode reference counter */
318+
atomic_t tm_ref; /* trigger-mode reference counter */
313319
};
314320

315321
#define __TRACE_EVENT_FLAGS(name, value) \
@@ -337,6 +343,10 @@ struct ftrace_event_file {
337343

338344
#define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
339345

346+
enum event_trigger_type {
347+
ETT_NONE = (0),
348+
};
349+
340350
extern void destroy_preds(struct ftrace_event_file *file);
341351
extern void destroy_call_preds(struct ftrace_event_call *call);
342352
extern int filter_match_preds(struct event_filter *filter, void *rec);
@@ -347,6 +357,7 @@ extern int filter_check_discard(struct ftrace_event_file *file, void *rec,
347357
extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
348358
struct ring_buffer *buffer,
349359
struct ring_buffer_event *event);
360+
extern void event_triggers_call(struct ftrace_event_file *file);
350361

351362
enum {
352363
FILTER_OTHER = 0,

include/trace/ftrace.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ ftrace_raw_event_##call(void *__data, proto) \
539539
int __data_size; \
540540
int pc; \
541541
\
542+
if (test_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, \
543+
&ftrace_file->flags)) \
544+
event_triggers_call(ftrace_file); \
545+
\
542546
if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, \
543547
&ftrace_file->flags)) \
544548
return; \

kernel/trace/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ifeq ($(CONFIG_PERF_EVENTS),y)
5050
obj-$(CONFIG_EVENT_TRACING) += trace_event_perf.o
5151
endif
5252
obj-$(CONFIG_EVENT_TRACING) += trace_events_filter.o
53+
obj-$(CONFIG_EVENT_TRACING) += trace_events_trigger.o
5354
obj-$(CONFIG_KPROBE_EVENT) += trace_kprobe.o
5455
obj-$(CONFIG_TRACEPOINTS) += power-traces.o
5556
ifeq ($(CONFIG_PM_RUNTIME),y)

kernel/trace/trace.h

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,9 +1028,190 @@ extern void trace_event_enable_cmd_record(bool enable);
10281028
extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
10291029
extern int event_trace_del_tracer(struct trace_array *tr);
10301030

1031+
static inline void *event_file_data(struct file *filp)
1032+
{
1033+
return ACCESS_ONCE(file_inode(filp)->i_private);
1034+
}
1035+
10311036
extern struct mutex event_mutex;
10321037
extern struct list_head ftrace_events;
10331038

1039+
extern const struct file_operations event_trigger_fops;
1040+
1041+
extern int register_trigger_cmds(void);
1042+
extern void clear_event_triggers(struct trace_array *tr);
1043+
1044+
struct event_trigger_data {
1045+
unsigned long count;
1046+
int ref;
1047+
struct event_trigger_ops *ops;
1048+
struct event_command *cmd_ops;
1049+
struct event_filter *filter;
1050+
char *filter_str;
1051+
void *private_data;
1052+
struct list_head list;
1053+
};
1054+
1055+
/**
1056+
* struct event_trigger_ops - callbacks for trace event triggers
1057+
*
1058+
* The methods in this structure provide per-event trigger hooks for
1059+
* various trigger operations.
1060+
*
1061+
* All the methods below, except for @init() and @free(), must be
1062+
* implemented.
1063+
*
1064+
* @func: The trigger 'probe' function called when the triggering
1065+
* event occurs. The data passed into this callback is the data
1066+
* that was supplied to the event_command @reg() function that
1067+
* registered the trigger (see struct event_command).
1068+
*
1069+
* @init: An optional initialization function called for the trigger
1070+
* when the trigger is registered (via the event_command reg()
1071+
* function). This can be used to perform per-trigger
1072+
* initialization such as incrementing a per-trigger reference
1073+
* count, for instance. This is usually implemented by the
1074+
* generic utility function @event_trigger_init() (see
1075+
* trace_event_triggers.c).
1076+
*
1077+
* @free: An optional de-initialization function called for the
1078+
* trigger when the trigger is unregistered (via the
1079+
* event_command @reg() function). This can be used to perform
1080+
* per-trigger de-initialization such as decrementing a
1081+
* per-trigger reference count and freeing corresponding trigger
1082+
* data, for instance. This is usually implemented by the
1083+
* generic utility function @event_trigger_free() (see
1084+
* trace_event_triggers.c).
1085+
*
1086+
* @print: The callback function invoked to have the trigger print
1087+
* itself. This is usually implemented by a wrapper function
1088+
* that calls the generic utility function @event_trigger_print()
1089+
* (see trace_event_triggers.c).
1090+
*/
1091+
struct event_trigger_ops {
1092+
void (*func)(struct event_trigger_data *data);
1093+
int (*init)(struct event_trigger_ops *ops,
1094+
struct event_trigger_data *data);
1095+
void (*free)(struct event_trigger_ops *ops,
1096+
struct event_trigger_data *data);
1097+
int (*print)(struct seq_file *m,
1098+
struct event_trigger_ops *ops,
1099+
struct event_trigger_data *data);
1100+
};
1101+
1102+
/**
1103+
* struct event_command - callbacks and data members for event commands
1104+
*
1105+
* Event commands are invoked by users by writing the command name
1106+
* into the 'trigger' file associated with a trace event. The
1107+
* parameters associated with a specific invocation of an event
1108+
* command are used to create an event trigger instance, which is
1109+
* added to the list of trigger instances associated with that trace
1110+
* event. When the event is hit, the set of triggers associated with
1111+
* that event is invoked.
1112+
*
1113+
* The data members in this structure provide per-event command data
1114+
* for various event commands.
1115+
*
1116+
* All the data members below, except for @post_trigger, must be set
1117+
* for each event command.
1118+
*
1119+
* @name: The unique name that identifies the event command. This is
1120+
* the name used when setting triggers via trigger files.
1121+
*
1122+
* @trigger_type: A unique id that identifies the event command
1123+
* 'type'. This value has two purposes, the first to ensure that
1124+
* only one trigger of the same type can be set at a given time
1125+
* for a particular event e.g. it doesn't make sense to have both
1126+
* a traceon and traceoff trigger attached to a single event at
1127+
* the same time, so traceon and traceoff have the same type
1128+
* though they have different names. The @trigger_type value is
1129+
* also used as a bit value for deferring the actual trigger
1130+
* action until after the current event is finished. Some
1131+
* commands need to do this if they themselves log to the trace
1132+
* buffer (see the @post_trigger() member below). @trigger_type
1133+
* values are defined by adding new values to the trigger_type
1134+
* enum in include/linux/ftrace_event.h.
1135+
*
1136+
* @post_trigger: A flag that says whether or not this command needs
1137+
* to have its action delayed until after the current event has
1138+
* been closed. Some triggers need to avoid being invoked while
1139+
* an event is currently in the process of being logged, since
1140+
* the trigger may itself log data into the trace buffer. Thus
1141+
* we make sure the current event is committed before invoking
1142+
* those triggers. To do that, the trigger invocation is split
1143+
* in two - the first part checks the filter using the current
1144+
* trace record; if a command has the @post_trigger flag set, it
1145+
* sets a bit for itself in the return value, otherwise it
1146+
* directly invokes the trigger. Once all commands have been
1147+
* either invoked or set their return flag, the current record is
1148+
* either committed or discarded. At that point, if any commands
1149+
* have deferred their triggers, those commands are finally
1150+
* invoked following the close of the current event. In other
1151+
* words, if the event_trigger_ops @func() probe implementation
1152+
* itself logs to the trace buffer, this flag should be set,
1153+
* otherwise it can be left unspecified.
1154+
*
1155+
* All the methods below, except for @set_filter(), must be
1156+
* implemented.
1157+
*
1158+
* @func: The callback function responsible for parsing and
1159+
* registering the trigger written to the 'trigger' file by the
1160+
* user. It allocates the trigger instance and registers it with
1161+
* the appropriate trace event. It makes use of the other
1162+
* event_command callback functions to orchestrate this, and is
1163+
* usually implemented by the generic utility function
1164+
* @event_trigger_callback() (see trace_event_triggers.c).
1165+
*
1166+
* @reg: Adds the trigger to the list of triggers associated with the
1167+
* event, and enables the event trigger itself, after
1168+
* initializing it (via the event_trigger_ops @init() function).
1169+
* This is also where commands can use the @trigger_type value to
1170+
* make the decision as to whether or not multiple instances of
1171+
* the trigger should be allowed. This is usually implemented by
1172+
* the generic utility function @register_trigger() (see
1173+
* trace_event_triggers.c).
1174+
*
1175+
* @unreg: Removes the trigger from the list of triggers associated
1176+
* with the event, and disables the event trigger itself, after
1177+
* initializing it (via the event_trigger_ops @free() function).
1178+
* This is usually implemented by the generic utility function
1179+
* @unregister_trigger() (see trace_event_triggers.c).
1180+
*
1181+
* @set_filter: An optional function called to parse and set a filter
1182+
* for the trigger. If no @set_filter() method is set for the
1183+
* event command, filters set by the user for the command will be
1184+
* ignored. This is usually implemented by the generic utility
1185+
* function @set_trigger_filter() (see trace_event_triggers.c).
1186+
*
1187+
* @get_trigger_ops: The callback function invoked to retrieve the
1188+
* event_trigger_ops implementation associated with the command.
1189+
*/
1190+
struct event_command {
1191+
struct list_head list;
1192+
char *name;
1193+
enum event_trigger_type trigger_type;
1194+
bool post_trigger;
1195+
int (*func)(struct event_command *cmd_ops,
1196+
struct ftrace_event_file *file,
1197+
char *glob, char *cmd, char *params);
1198+
int (*reg)(char *glob,
1199+
struct event_trigger_ops *ops,
1200+
struct event_trigger_data *data,
1201+
struct ftrace_event_file *file);
1202+
void (*unreg)(char *glob,
1203+
struct event_trigger_ops *ops,
1204+
struct event_trigger_data *data,
1205+
struct ftrace_event_file *file);
1206+
int (*set_filter)(char *filter_str,
1207+
struct event_trigger_data *data,
1208+
struct ftrace_event_file *file);
1209+
struct event_trigger_ops *(*get_trigger_ops)(char *cmd, char *param);
1210+
};
1211+
1212+
extern int trace_event_enable_disable(struct ftrace_event_file *file,
1213+
int enable, int soft_disable);
1214+
10341215
extern const char *__start___trace_bprintk_fmt[];
10351216
extern const char *__stop___trace_bprintk_fmt[];
10361217

kernel/trace/trace_events.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
342342
return ret;
343343
}
344344

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+
345351
static int ftrace_event_enable_disable(struct ftrace_event_file *file,
346352
int enable)
347353
{
@@ -421,11 +427,6 @@ static void remove_subsystem(struct ftrace_subsystem_dir *dir)
421427
}
422428
}
423429

424-
static void *event_file_data(struct file *filp)
425-
{
426-
return ACCESS_ONCE(file_inode(filp)->i_private);
427-
}
428-
429430
static void remove_event_file_dir(struct ftrace_event_file *file)
430431
{
431432
struct dentry *dir = file->dir;
@@ -1549,6 +1550,9 @@ event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
15491550
trace_create_file("filter", 0644, file->dir, file,
15501551
&ftrace_event_filter_fops);
15511552

1553+
trace_create_file("trigger", 0644, file->dir, file,
1554+
&event_trigger_fops);
1555+
15521556
trace_create_file("format", 0444, file->dir, call,
15531557
&ftrace_event_format_fops);
15541558

@@ -1645,6 +1649,8 @@ trace_create_new_event(struct ftrace_event_call *call,
16451649
file->event_call = call;
16461650
file->tr = tr;
16471651
atomic_set(&file->sm_ref, 0);
1652+
atomic_set(&file->tm_ref, 0);
1653+
INIT_LIST_HEAD(&file->triggers);
16481654
list_add(&file->list, &tr->events);
16491655

16501656
return file;
@@ -2311,6 +2317,9 @@ int event_trace_del_tracer(struct trace_array *tr)
23112317
{
23122318
mutex_lock(&event_mutex);
23132319

2320+
/* Disable any event triggers and associated soft-disabled events */
2321+
clear_event_triggers(tr);
2322+
23142323
/* Disable any running events */
23152324
__ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
23162325

@@ -2377,6 +2386,8 @@ static __init int event_trace_enable(void)
23772386

23782387
register_event_cmds();
23792388

2389+
register_trigger_cmds();
2390+
23802391
return 0;
23812392
}
23822393

0 commit comments

Comments
 (0)