Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Common header file for generic dynamic events. |
| 4 | */ |
| 5 | |
| 6 | #ifndef _TRACE_DYNEVENT_H |
| 7 | #define _TRACE_DYNEVENT_H |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | |
| 14 | #include "trace.h" |
| 15 | |
| 16 | struct dyn_event; |
| 17 | |
| 18 | /** |
| 19 | * struct dyn_event_operations - Methods for each type of dynamic events |
| 20 | * |
| 21 | * These methods must be set for each type, since there is no default method. |
| 22 | * Before using this for dyn_event_init(), it must be registered by |
| 23 | * dyn_event_register(). |
| 24 | * |
| 25 | * @create: Parse and create event method. This is invoked when user passes |
| 26 | * a event definition to dynamic_events interface. This must not destruct |
| 27 | * the arguments and return -ECANCELED if given arguments doesn't match its |
| 28 | * command prefix. |
| 29 | * @show: Showing method. This is invoked when user reads the event definitions |
| 30 | * via dynamic_events interface. |
| 31 | * @is_busy: Check whether given event is busy so that it can not be deleted. |
Qiujun Huang | 2b5894c | 2020-10-29 23:05:54 +0800 | [diff] [blame] | 32 | * Return true if it is busy, otherwise false. |
| 33 | * @free: Delete the given event. Return 0 if success, otherwise error. |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 34 | * @match: Check whether given event and system name match this event. The argc |
Qiujun Huang | 2b5894c | 2020-10-29 23:05:54 +0800 | [diff] [blame] | 35 | * and argv is used for exact match. Return true if it matches, otherwise |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 36 | * false. |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 37 | * |
| 38 | * Except for @create, these methods are called under holding event_mutex. |
| 39 | */ |
| 40 | struct dyn_event_operations { |
| 41 | struct list_head list; |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 42 | int (*create)(const char *raw_command); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 43 | int (*show)(struct seq_file *m, struct dyn_event *ev); |
| 44 | bool (*is_busy)(struct dyn_event *ev); |
| 45 | int (*free)(struct dyn_event *ev); |
| 46 | bool (*match)(const char *system, const char *event, |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 47 | int argc, const char **argv, struct dyn_event *ev); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /* Register new dyn_event type -- must be called at first */ |
| 51 | int dyn_event_register(struct dyn_event_operations *ops); |
| 52 | |
| 53 | /** |
| 54 | * struct dyn_event - Dynamic event list header |
| 55 | * |
| 56 | * The dyn_event structure encapsulates a list and a pointer to the operators |
| 57 | * for making a global list of dynamic events. |
| 58 | * User must includes this in each event structure, so that those events can |
| 59 | * be added/removed via dynamic_events interface. |
| 60 | */ |
| 61 | struct dyn_event { |
| 62 | struct list_head list; |
| 63 | struct dyn_event_operations *ops; |
| 64 | }; |
| 65 | |
| 66 | extern struct list_head dyn_event_list; |
| 67 | |
| 68 | static inline |
| 69 | int dyn_event_init(struct dyn_event *ev, struct dyn_event_operations *ops) |
| 70 | { |
| 71 | if (!ev || !ops) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | INIT_LIST_HEAD(&ev->list); |
| 75 | ev->ops = ops; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Steven Rostedt (VMware) | 8b0e6c7 | 2021-08-16 23:42:56 -0400 | [diff] [blame] | 79 | static inline int dyn_event_add(struct dyn_event *ev, |
| 80 | struct trace_event_call *call) |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 81 | { |
| 82 | lockdep_assert_held(&event_mutex); |
| 83 | |
| 84 | if (!ev || !ev->ops) |
| 85 | return -EINVAL; |
| 86 | |
Steven Rostedt (VMware) | 8b0e6c7 | 2021-08-16 23:42:56 -0400 | [diff] [blame] | 87 | call->flags |= TRACE_EVENT_FL_DYNAMIC; |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 88 | list_add_tail(&ev->list, &dyn_event_list); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static inline void dyn_event_remove(struct dyn_event *ev) |
| 93 | { |
| 94 | lockdep_assert_held(&event_mutex); |
| 95 | list_del_init(&ev->list); |
| 96 | } |
| 97 | |
| 98 | void *dyn_event_seq_start(struct seq_file *m, loff_t *pos); |
| 99 | void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos); |
| 100 | void dyn_event_seq_stop(struct seq_file *m, void *v); |
| 101 | int dyn_events_release_all(struct dyn_event_operations *type); |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 102 | int dyn_event_release(const char *raw_command, struct dyn_event_operations *type); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * for_each_dyn_event - iterate over the dyn_event list |
| 106 | * @pos: the struct dyn_event * to use as a loop cursor |
| 107 | * |
| 108 | * This is just a basement of for_each macro. Wrap this for |
| 109 | * each actual event structure with ops filtering. |
| 110 | */ |
| 111 | #define for_each_dyn_event(pos) \ |
| 112 | list_for_each_entry(pos, &dyn_event_list, list) |
| 113 | |
| 114 | /* |
| 115 | * for_each_dyn_event - iterate over the dyn_event list safely |
| 116 | * @pos: the struct dyn_event * to use as a loop cursor |
| 117 | * @n: the struct dyn_event * to use as temporary storage |
| 118 | */ |
| 119 | #define for_each_dyn_event_safe(pos, n) \ |
| 120 | list_for_each_entry_safe(pos, n, &dyn_event_list, list) |
| 121 | |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 122 | extern void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen, |
| 123 | enum dynevent_type type, |
| 124 | dynevent_create_fn_t run_command); |
| 125 | |
| 126 | typedef int (*dynevent_check_arg_fn_t)(void *data); |
| 127 | |
| 128 | struct dynevent_arg { |
| 129 | const char *str; |
| 130 | char separator; /* e.g. ';', ',', or nothing */ |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | extern void dynevent_arg_init(struct dynevent_arg *arg, |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 134 | char separator); |
| 135 | extern int dynevent_arg_add(struct dynevent_cmd *cmd, |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 136 | struct dynevent_arg *arg, |
| 137 | dynevent_check_arg_fn_t check_arg); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 138 | |
| 139 | struct dynevent_arg_pair { |
| 140 | const char *lhs; |
| 141 | const char *rhs; |
| 142 | char operator; /* e.g. '=' or nothing */ |
| 143 | char separator; /* e.g. ';', ',', or nothing */ |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | extern void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair, |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 147 | char operator, char separator); |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 148 | |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 149 | extern int dynevent_arg_pair_add(struct dynevent_cmd *cmd, |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 150 | struct dynevent_arg_pair *arg_pair, |
| 151 | dynevent_check_arg_fn_t check_arg); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 152 | extern int dynevent_str_add(struct dynevent_cmd *cmd, const char *str); |
| 153 | |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 154 | #endif |