1 =========================================
2 user_events: User-based Event Tracing
3 =========================================
9 User based trace events allow user processes to create events and trace data
10 that can be viewed via existing tools, such as ftrace and perf.
11 To enable this feature, build your kernel with CONFIG_USER_EVENTS=y.
13 Programs can view status of the events via
14 /sys/kernel/tracing/user_events_status and can both register and write
15 data out via /sys/kernel/tracing/user_events_data.
17 Typically programs will register a set of events that they wish to expose to
18 tools that can read trace_events (such as ftrace and perf). The registration
19 process tells the kernel which address and bit to reflect if any tool has
20 enabled the event and data should be written. The registration will give back
21 a write index which describes the data when a write() or writev() is called
22 on the /sys/kernel/tracing/user_events_data file.
24 The structures referenced in this document are contained within the
25 /include/uapi/linux/user_events.h file in the source tree.
27 **NOTE:** *Both user_events_status and user_events_data are under the tracefs
28 filesystem and may be mounted at different paths than above.*
32 Registering within a user process is done via ioctl() out to the
33 /sys/kernel/tracing/user_events_data file. The command to issue is
36 This command takes a packed struct user_reg as an argument::
39 /* Input: Size of the user_reg structure being used */
42 /* Input: Bit in enable address to use */
45 /* Input: Enable size in bytes at address */
48 /* Input: Flags for future use, set to 0 */
51 /* Input: Address to update when enabled */
54 /* Input: Pointer to string with event name, description and flags */
57 /* Output: Index of the event to use when writing data */
59 } __attribute__((__packed__));
61 The struct user_reg requires all the above inputs to be set appropriately.
63 + size: This must be set to sizeof(struct user_reg).
65 + enable_bit: The bit to reflect the event status at the address specified by
68 + enable_size: The size of the value specified by enable_addr.
69 This must be 4 (32-bit) or 8 (64-bit). 64-bit values are only allowed to be
70 used on 64-bit kernels, however, 32-bit can be used on all kernels.
72 + flags: The flags to use, if any. For the initial version this must be 0.
73 Callers should first attempt to use flags and retry without flags to ensure
74 support for lower versions of the kernel. If a flag is not supported -EINVAL
77 + enable_addr: The address of the value to use to reflect event status. This
78 must be naturally aligned and write accessible within the user program.
80 + name_args: The name and arguments to describe the event, see command format
83 Upon successful registration the following is set.
85 + write_index: The index to use for this file descriptor that represents this
86 event when writing out data. The index is unique to this instance of the file
87 descriptor that was used for the registration. See writing data for details.
89 User based events show up under tracefs like any other event under the
90 subsystem named "user_events". This means tools that wish to attach to the
91 events need to use /sys/kernel/tracing/events/user_events/[name]/enable
92 or perf record -e user_events:[name] when attaching/recording.
94 **NOTE:** The event subsystem name by default is "user_events". Callers should
95 not assume it will always be "user_events". Operators reserve the right in the
96 future to change the subsystem name per-process to accomodate event isolation.
100 The command string format is as follows::
102 name[:FLAG1[,FLAG2...]] [Field1[;Field2...]]
114 Basic types are supported (__data_loc, u32, u64, int, char, char[20], etc).
115 User programs are encouraged to use clearly sized types like u32.
117 **NOTE:** *Long is not supported since size can vary between user and kernel.*
119 The size is only valid for types that start with a struct prefix.
120 This allows user programs to describe custom structs out to tools, if required.
122 For example, a struct in C that looks like this::
128 Would be represented by the following field::
130 struct mytype myname 20
134 Deleting an event from within a user process is done via ioctl() out to the
135 /sys/kernel/tracing/user_events_data file. The command to issue is
138 This command only requires a single string specifying the event to delete by
139 its name. Delete will only succeed if there are no references left to the
140 event (in both user and kernel space). User programs should use a separate file
141 to request deletes than the one used for registration due to this.
143 **NOTE:** By default events will auto-delete when there are no references left
144 to the event. Flags in the future may change this logic.
148 If after registering an event it is no longer wanted to be updated then it can
149 be disabled via ioctl() out to the /sys/kernel/tracing/user_events_data file.
150 The command to issue is DIAG_IOCSUNREG. This is different than deleting, where
151 deleting actually removes the event from the system. Unregistering simply tells
152 the kernel your process is no longer interested in updates to the event.
154 This command takes a packed struct user_unreg as an argument::
157 /* Input: Size of the user_unreg structure being used */
160 /* Input: Bit to unregister */
163 /* Input: Reserved, set to 0 */
166 /* Input: Reserved, set to 0 */
169 /* Input: Address to unregister */
171 } __attribute__((__packed__));
173 The struct user_unreg requires all the above inputs to be set appropriately.
175 + size: This must be set to sizeof(struct user_unreg).
177 + disable_bit: This must be set to the bit to disable (same bit that was
178 previously registered via enable_bit).
180 + disable_addr: This must be set to the address to disable (same address that was
181 previously registered via enable_addr).
183 **NOTE:** Events are automatically unregistered when execve() is invoked. During
184 fork() the registered events will be retained and must be unregistered manually
185 in each process if wanted.
189 When tools attach/record user based events the status of the event is updated
190 in realtime. This allows user programs to only incur the cost of the write() or
191 writev() calls when something is actively attached to the event.
193 The kernel will update the specified bit that was registered for the event as
194 tools attach/detach from the event. User programs simply check if the bit is set
195 to see if something is attached or not.
197 Administrators can easily check the status of all registered events by reading
198 the user_events_status file directly via a terminal. The output is as follows::
206 For example, on a system that has a single event the output looks like this::
213 If a user enables the user event via ftrace, the output would change to this::
215 test # Used by ftrace
222 After registering an event the same fd that was used to register can be used
223 to write an entry for that event. The write_index returned must be at the start
224 of the data, then the remaining data is treated as the payload of the event.
226 For example, if write_index returned was 1 and I wanted to write out an int
227 payload of the event. Then the data would have to be 8 bytes (2 ints) in size,
228 with the first 4 bytes being equal to 1 and the last 4 bytes being equal to the
229 value I want as the payload.
231 In memory this would look like this::
236 User programs might have well known structs that they wish to use to emit out
237 as payloads. In those cases writev() can be used, with the first vector being
238 the index and the following vector(s) being the actual event payload.
240 For example, if I have a struct like this::
246 } __attribute__((__packed__));
248 It's advised for user programs to do the following::
253 io[0].iov_base = &write_index;
254 io[0].iov_len = sizeof(write_index);
256 io[1].iov_len = sizeof(e);
258 writev(fd, (const struct iovec*)io, 2);
260 **NOTE:** *The write_index is not emitted out into the trace being recorded.*
264 See sample code in samples/user_events.