Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
2 | * | |
e8951e63 | 3 | * lttng-abi.c |
baf20995 | 4 | * |
e8951e63 | 5 | * LTTng ABI |
baf20995 | 6 | * |
886d51a3 MD |
7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
8 | * | |
baf20995 MD |
9 | * Mimic system calls for: |
10 | * - session creation, returns a file descriptor or failure. | |
ad1c05e1 MD |
11 | * - channel creation, returns a file descriptor or failure. |
12 | * - Operates on a session file descriptor | |
13 | * - Takes all channel options as parameters. | |
14 | * - stream get, returns a file descriptor or failure. | |
15 | * - Operates on a channel file descriptor. | |
16 | * - stream notifier get, returns a file descriptor or failure. | |
17 | * - Operates on a channel file descriptor. | |
18 | * - event creation, returns a file descriptor or failure. | |
19 | * - Operates on a channel file descriptor | |
20 | * - Takes an event name as parameter | |
21 | * - Takes an instrumentation source as parameter | |
22 | * - e.g. tracepoints, dynamic_probes... | |
23 | * - Takes instrumentation source specific arguments. | |
baf20995 MD |
24 | */ |
25 | ||
11b5a3c2 | 26 | #include <linux/module.h> |
e6a17f26 | 27 | #include <linux/proc_fs.h> |
11b5a3c2 MD |
28 | #include <linux/anon_inodes.h> |
29 | #include <linux/file.h> | |
30 | #include <linux/uaccess.h> | |
31 | #include <linux/slab.h> | |
abc0446a | 32 | #include <linux/err.h> |
241ae9a8 MD |
33 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */ |
34 | #include <wrapper/ringbuffer/vfs.h> | |
35 | #include <wrapper/ringbuffer/backend.h> | |
36 | #include <wrapper/ringbuffer/frontend.h> | |
37 | #include <wrapper/poll.h> | |
38 | #include <wrapper/file.h> | |
39 | #include <wrapper/kref.h> | |
4993071a | 40 | #include <lttng-string-utils.h> |
241ae9a8 MD |
41 | #include <lttng-abi.h> |
42 | #include <lttng-abi-old.h> | |
43 | #include <lttng-events.h> | |
44 | #include <lttng-tracer.h> | |
f771eda6 | 45 | #include <lttng-tp-mempool.h> |
241ae9a8 | 46 | #include <lib/ringbuffer/frontend_types.h> |
baf20995 MD |
47 | |
48 | /* | |
49 | * This is LTTng's own personal way to create a system call as an external | |
80996790 | 50 | * module. We use ioctl() on /proc/lttng. |
baf20995 MD |
51 | */ |
52 | ||
e6a17f26 | 53 | static struct proc_dir_entry *lttng_proc_dentry; |
ad1c05e1 MD |
54 | static const struct file_operations lttng_fops; |
55 | static const struct file_operations lttng_session_fops; | |
56 | static const struct file_operations lttng_channel_fops; | |
5dbbdb43 | 57 | static const struct file_operations lttng_metadata_fops; |
305d3e42 | 58 | static const struct file_operations lttng_event_fops; |
ed8d02d6 | 59 | static struct file_operations lttng_stream_ring_buffer_file_operations; |
baf20995 | 60 | |
9616f0bf JD |
61 | static int put_u64(uint64_t val, unsigned long arg); |
62 | ||
a33c9927 MD |
63 | /* |
64 | * Teardown management: opened file descriptors keep a refcount on the module, | |
65 | * so it can only exit when all file descriptors are closed. | |
66 | */ | |
67 | ||
ad1c05e1 | 68 | static |
baf20995 MD |
69 | int lttng_abi_create_session(void) |
70 | { | |
a90917c3 | 71 | struct lttng_session *session; |
c0e31d2e | 72 | struct file *session_file; |
11b5a3c2 | 73 | int session_fd, ret; |
baf20995 | 74 | |
a90917c3 | 75 | session = lttng_session_create(); |
baf20995 MD |
76 | if (!session) |
77 | return -ENOMEM; | |
4ac10b76 | 78 | session_fd = lttng_get_unused_fd(); |
baf20995 MD |
79 | if (session_fd < 0) { |
80 | ret = session_fd; | |
81 | goto fd_error; | |
82 | } | |
c0e31d2e | 83 | session_file = anon_inode_getfile("[lttng_session]", |
ad1c05e1 | 84 | <tng_session_fops, |
baf20995 | 85 | session, O_RDWR); |
c0e31d2e MD |
86 | if (IS_ERR(session_file)) { |
87 | ret = PTR_ERR(session_file); | |
baf20995 MD |
88 | goto file_error; |
89 | } | |
c0e31d2e MD |
90 | session->file = session_file; |
91 | fd_install(session_fd, session_file); | |
baf20995 MD |
92 | return session_fd; |
93 | ||
94 | file_error: | |
95 | put_unused_fd(session_fd); | |
96 | fd_error: | |
a90917c3 | 97 | lttng_session_destroy(session); |
baf20995 MD |
98 | return ret; |
99 | } | |
100 | ||
271b6681 MD |
101 | static |
102 | int lttng_abi_tracepoint_list(void) | |
103 | { | |
104 | struct file *tracepoint_list_file; | |
105 | int file_fd, ret; | |
106 | ||
4ac10b76 | 107 | file_fd = lttng_get_unused_fd(); |
271b6681 MD |
108 | if (file_fd < 0) { |
109 | ret = file_fd; | |
110 | goto fd_error; | |
111 | } | |
30f18bf0 | 112 | |
8ee099b6 | 113 | tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]", |
271b6681 MD |
114 | <tng_tracepoint_list_fops, |
115 | NULL, O_RDWR); | |
116 | if (IS_ERR(tracepoint_list_file)) { | |
117 | ret = PTR_ERR(tracepoint_list_file); | |
118 | goto file_error; | |
119 | } | |
30f18bf0 MD |
120 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
121 | if (ret < 0) | |
122 | goto open_error; | |
271b6681 MD |
123 | fd_install(file_fd, tracepoint_list_file); |
124 | return file_fd; | |
125 | ||
30f18bf0 MD |
126 | open_error: |
127 | fput(tracepoint_list_file); | |
271b6681 MD |
128 | file_error: |
129 | put_unused_fd(file_fd); | |
130 | fd_error: | |
131 | return ret; | |
132 | } | |
133 | ||
f127e61e MD |
134 | #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
135 | static inline | |
136 | int lttng_abi_syscall_list(void) | |
137 | { | |
138 | return -ENOSYS; | |
139 | } | |
140 | #else | |
141 | static | |
142 | int lttng_abi_syscall_list(void) | |
143 | { | |
144 | struct file *syscall_list_file; | |
145 | int file_fd, ret; | |
146 | ||
147 | file_fd = lttng_get_unused_fd(); | |
148 | if (file_fd < 0) { | |
149 | ret = file_fd; | |
150 | goto fd_error; | |
151 | } | |
152 | ||
153 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", | |
154 | <tng_syscall_list_fops, | |
155 | NULL, O_RDWR); | |
156 | if (IS_ERR(syscall_list_file)) { | |
157 | ret = PTR_ERR(syscall_list_file); | |
158 | goto file_error; | |
159 | } | |
160 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); | |
161 | if (ret < 0) | |
162 | goto open_error; | |
163 | fd_install(file_fd, syscall_list_file); | |
f127e61e MD |
164 | return file_fd; |
165 | ||
166 | open_error: | |
167 | fput(syscall_list_file); | |
168 | file_error: | |
169 | put_unused_fd(file_fd); | |
170 | fd_error: | |
171 | return ret; | |
172 | } | |
173 | #endif | |
174 | ||
80c16bcf | 175 | static |
6dccd6c1 | 176 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
80c16bcf | 177 | { |
6dccd6c1 JD |
178 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
179 | v->minor = LTTNG_MODULES_MINOR_VERSION; | |
180 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; | |
80c16bcf MD |
181 | } |
182 | ||
42cabb80 MD |
183 | static |
184 | void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v) | |
185 | { | |
186 | v->major = LTTNG_MODULES_ABI_MAJOR_VERSION; | |
187 | v->minor = LTTNG_MODULES_ABI_MINOR_VERSION; | |
188 | } | |
189 | ||
8070f5c0 MD |
190 | static |
191 | long lttng_abi_add_context(struct file *file, | |
6dccd6c1 | 192 | struct lttng_kernel_context *context_param, |
a90917c3 | 193 | struct lttng_ctx **ctx, struct lttng_session *session) |
8070f5c0 | 194 | { |
8070f5c0 MD |
195 | |
196 | if (session->been_active) | |
197 | return -EPERM; | |
198 | ||
6dccd6c1 | 199 | switch (context_param->ctx) { |
12a313a5 | 200 | case LTTNG_KERNEL_CONTEXT_PID: |
8070f5c0 | 201 | return lttng_add_pid_to_ctx(ctx); |
a8ad3613 MD |
202 | case LTTNG_KERNEL_CONTEXT_PRIO: |
203 | return lttng_add_prio_to_ctx(ctx); | |
53f1f0ca MD |
204 | case LTTNG_KERNEL_CONTEXT_NICE: |
205 | return lttng_add_nice_to_ctx(ctx); | |
b64bc438 MD |
206 | case LTTNG_KERNEL_CONTEXT_VPID: |
207 | return lttng_add_vpid_to_ctx(ctx); | |
208 | case LTTNG_KERNEL_CONTEXT_TID: | |
209 | return lttng_add_tid_to_ctx(ctx); | |
210 | case LTTNG_KERNEL_CONTEXT_VTID: | |
211 | return lttng_add_vtid_to_ctx(ctx); | |
212 | case LTTNG_KERNEL_CONTEXT_PPID: | |
213 | return lttng_add_ppid_to_ctx(ctx); | |
214 | case LTTNG_KERNEL_CONTEXT_VPPID: | |
215 | return lttng_add_vppid_to_ctx(ctx); | |
12a313a5 | 216 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
6dccd6c1 JD |
217 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
218 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, | |
219 | context_param->u.perf_counter.config, | |
220 | context_param->u.perf_counter.name, | |
c24a0d71 | 221 | ctx); |
a2563e83 MD |
222 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
223 | return lttng_add_procname_to_ctx(ctx); | |
975da2c0 JD |
224 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
225 | return lttng_add_hostname_to_ctx(ctx); | |
b3699d90 MD |
226 | case LTTNG_KERNEL_CONTEXT_CPU_ID: |
227 | return lttng_add_cpu_id_to_ctx(ctx); | |
79150a49 JD |
228 | case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE: |
229 | return lttng_add_interruptible_to_ctx(ctx); | |
230 | case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE: | |
231 | return lttng_add_need_reschedule_to_ctx(ctx); | |
232 | case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE: | |
233 | return lttng_add_preemptible_to_ctx(ctx); | |
234 | case LTTNG_KERNEL_CONTEXT_MIGRATABLE: | |
235 | return lttng_add_migratable_to_ctx(ctx); | |
2fa2d39a FG |
236 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: |
237 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: | |
238 | return lttng_add_callstack_to_ctx(ctx, context_param->ctx); | |
45b27d78 MJ |
239 | case LTTNG_KERNEL_CONTEXT_CGROUP_NS: |
240 | return lttng_add_cgroup_ns_to_ctx(ctx); | |
241 | case LTTNG_KERNEL_CONTEXT_IPC_NS: | |
242 | return lttng_add_ipc_ns_to_ctx(ctx); | |
243 | case LTTNG_KERNEL_CONTEXT_MNT_NS: | |
244 | return lttng_add_mnt_ns_to_ctx(ctx); | |
245 | case LTTNG_KERNEL_CONTEXT_NET_NS: | |
246 | return lttng_add_net_ns_to_ctx(ctx); | |
247 | case LTTNG_KERNEL_CONTEXT_PID_NS: | |
248 | return lttng_add_pid_ns_to_ctx(ctx); | |
249 | case LTTNG_KERNEL_CONTEXT_USER_NS: | |
250 | return lttng_add_user_ns_to_ctx(ctx); | |
251 | case LTTNG_KERNEL_CONTEXT_UTS_NS: | |
252 | return lttng_add_uts_ns_to_ctx(ctx); | |
3673e803 MJ |
253 | case LTTNG_KERNEL_CONTEXT_UID: |
254 | return lttng_add_uid_to_ctx(ctx); | |
255 | case LTTNG_KERNEL_CONTEXT_EUID: | |
256 | return lttng_add_euid_to_ctx(ctx); | |
257 | case LTTNG_KERNEL_CONTEXT_SUID: | |
258 | return lttng_add_suid_to_ctx(ctx); | |
259 | case LTTNG_KERNEL_CONTEXT_GID: | |
260 | return lttng_add_gid_to_ctx(ctx); | |
261 | case LTTNG_KERNEL_CONTEXT_EGID: | |
262 | return lttng_add_egid_to_ctx(ctx); | |
263 | case LTTNG_KERNEL_CONTEXT_SGID: | |
264 | return lttng_add_sgid_to_ctx(ctx); | |
265 | case LTTNG_KERNEL_CONTEXT_VUID: | |
266 | return lttng_add_vuid_to_ctx(ctx); | |
267 | case LTTNG_KERNEL_CONTEXT_VEUID: | |
268 | return lttng_add_veuid_to_ctx(ctx); | |
269 | case LTTNG_KERNEL_CONTEXT_VSUID: | |
270 | return lttng_add_vsuid_to_ctx(ctx); | |
271 | case LTTNG_KERNEL_CONTEXT_VGID: | |
272 | return lttng_add_vgid_to_ctx(ctx); | |
273 | case LTTNG_KERNEL_CONTEXT_VEGID: | |
274 | return lttng_add_vegid_to_ctx(ctx); | |
275 | case LTTNG_KERNEL_CONTEXT_VSGID: | |
276 | return lttng_add_vsgid_to_ctx(ctx); | |
8070f5c0 MD |
277 | default: |
278 | return -EINVAL; | |
279 | } | |
280 | } | |
281 | ||
ad1c05e1 MD |
282 | /** |
283 | * lttng_ioctl - lttng syscall through ioctl | |
284 | * | |
c0e31d2e | 285 | * @file: the file |
ad1c05e1 MD |
286 | * @cmd: the command |
287 | * @arg: command arg | |
288 | * | |
289 | * This ioctl implements lttng commands: | |
38d024ae | 290 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 291 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
292 | * LTTNG_KERNEL_TRACER_VERSION |
293 | * Returns the LTTng kernel tracer version | |
294 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
295 | * Returns a file descriptor listing available tracepoints | |
360f38ea MD |
296 | * LTTNG_KERNEL_WAIT_QUIESCENT |
297 | * Returns after all previously running probes have completed | |
42cabb80 MD |
298 | * LTTNG_KERNEL_TRACER_ABI_VERSION |
299 | * Returns the LTTng kernel tracer ABI version | |
ad1c05e1 MD |
300 | * |
301 | * The returned session will be deleted when its file descriptor is closed. | |
302 | */ | |
303 | static | |
c0e31d2e | 304 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
305 | { |
306 | switch (cmd) { | |
6dccd6c1 | 307 | case LTTNG_KERNEL_OLD_SESSION: |
38d024ae | 308 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 309 | return lttng_abi_create_session(); |
6dccd6c1 JD |
310 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
311 | { | |
312 | struct lttng_kernel_tracer_version v; | |
313 | struct lttng_kernel_old_tracer_version oldv; | |
314 | struct lttng_kernel_old_tracer_version *uversion = | |
315 | (struct lttng_kernel_old_tracer_version __user *) arg; | |
316 | ||
317 | lttng_abi_tracer_version(&v); | |
318 | oldv.major = v.major; | |
319 | oldv.minor = v.minor; | |
320 | oldv.patchlevel = v.patchlevel; | |
321 | ||
322 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) | |
323 | return -EFAULT; | |
324 | return 0; | |
325 | } | |
80c16bcf | 326 | case LTTNG_KERNEL_TRACER_VERSION: |
6dccd6c1 JD |
327 | { |
328 | struct lttng_kernel_tracer_version version; | |
329 | struct lttng_kernel_tracer_version *uversion = | |
330 | (struct lttng_kernel_tracer_version __user *) arg; | |
331 | ||
332 | lttng_abi_tracer_version(&version); | |
42cabb80 MD |
333 | |
334 | if (copy_to_user(uversion, &version, sizeof(version))) | |
335 | return -EFAULT; | |
336 | return 0; | |
337 | } | |
338 | case LTTNG_KERNEL_TRACER_ABI_VERSION: | |
339 | { | |
340 | struct lttng_kernel_tracer_abi_version version; | |
341 | struct lttng_kernel_tracer_abi_version *uversion = | |
342 | (struct lttng_kernel_tracer_abi_version __user *) arg; | |
343 | ||
344 | lttng_abi_tracer_abi_version(&version); | |
345 | ||
6dccd6c1 JD |
346 | if (copy_to_user(uversion, &version, sizeof(version))) |
347 | return -EFAULT; | |
348 | return 0; | |
349 | } | |
350 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: | |
271b6681 MD |
351 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
352 | return lttng_abi_tracepoint_list(); | |
2d2464bd MD |
353 | case LTTNG_KERNEL_SYSCALL_LIST: |
354 | return lttng_abi_syscall_list(); | |
6dccd6c1 | 355 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
5f7f9078 MD |
356 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
357 | synchronize_trace(); | |
358 | return 0; | |
6dccd6c1 JD |
359 | case LTTNG_KERNEL_OLD_CALIBRATE: |
360 | { | |
361 | struct lttng_kernel_old_calibrate __user *ucalibrate = | |
362 | (struct lttng_kernel_old_calibrate __user *) arg; | |
363 | struct lttng_kernel_old_calibrate old_calibrate; | |
364 | struct lttng_kernel_calibrate calibrate; | |
365 | int ret; | |
366 | ||
367 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) | |
368 | return -EFAULT; | |
369 | calibrate.type = old_calibrate.type; | |
370 | ret = lttng_calibrate(&calibrate); | |
371 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) | |
372 | return -EFAULT; | |
373 | return ret; | |
374 | } | |
57105fc2 MD |
375 | case LTTNG_KERNEL_CALIBRATE: |
376 | { | |
3db41b2c MD |
377 | struct lttng_kernel_calibrate __user *ucalibrate = |
378 | (struct lttng_kernel_calibrate __user *) arg; | |
379 | struct lttng_kernel_calibrate calibrate; | |
57105fc2 MD |
380 | int ret; |
381 | ||
382 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) | |
383 | return -EFAULT; | |
384 | ret = lttng_calibrate(&calibrate); | |
385 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) | |
386 | return -EFAULT; | |
387 | return ret; | |
388 | } | |
ad1c05e1 MD |
389 | default: |
390 | return -ENOIOCTLCMD; | |
391 | } | |
392 | } | |
393 | ||
ad1c05e1 | 394 | static const struct file_operations lttng_fops = { |
a33c9927 | 395 | .owner = THIS_MODULE, |
ad1c05e1 MD |
396 | .unlocked_ioctl = lttng_ioctl, |
397 | #ifdef CONFIG_COMPAT | |
03037b98 | 398 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 399 | #endif |
11b5a3c2 | 400 | }; |
ad1c05e1 | 401 | |
5dbbdb43 | 402 | static |
c0e31d2e | 403 | int lttng_abi_create_channel(struct file *session_file, |
6dccd6c1 | 404 | struct lttng_kernel_channel *chan_param, |
5dbbdb43 | 405 | enum channel_type channel_type) |
baf20995 | 406 | { |
a90917c3 | 407 | struct lttng_session *session = session_file->private_data; |
88dfd899 | 408 | const struct file_operations *fops = NULL; |
5dbbdb43 | 409 | const char *transport_name; |
a90917c3 | 410 | struct lttng_channel *chan; |
c0e31d2e | 411 | struct file *chan_file; |
baf20995 | 412 | int chan_fd; |
ad1c05e1 | 413 | int ret = 0; |
baf20995 | 414 | |
4ac10b76 | 415 | chan_fd = lttng_get_unused_fd(); |
baf20995 MD |
416 | if (chan_fd < 0) { |
417 | ret = chan_fd; | |
418 | goto fd_error; | |
419 | } | |
88dfd899 MD |
420 | switch (channel_type) { |
421 | case PER_CPU_CHANNEL: | |
422 | fops = <tng_channel_fops; | |
423 | break; | |
424 | case METADATA_CHANNEL: | |
425 | fops = <tng_metadata_fops; | |
426 | break; | |
427 | } | |
2470a237 | 428 | |
c0e31d2e | 429 | chan_file = anon_inode_getfile("[lttng_channel]", |
88dfd899 | 430 | fops, |
03037b98 | 431 | NULL, O_RDWR); |
c0e31d2e MD |
432 | if (IS_ERR(chan_file)) { |
433 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
434 | goto file_error; |
435 | } | |
5dbbdb43 MD |
436 | switch (channel_type) { |
437 | case PER_CPU_CHANNEL: | |
6dccd6c1 JD |
438 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
439 | transport_name = chan_param->overwrite ? | |
96ba7208 | 440 | "relay-overwrite" : "relay-discard"; |
6dccd6c1 JD |
441 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
442 | transport_name = chan_param->overwrite ? | |
96ba7208 JD |
443 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
444 | } else { | |
445 | return -EINVAL; | |
446 | } | |
5dbbdb43 | 447 | break; |
5dbbdb43 | 448 | case METADATA_CHANNEL: |
6dccd6c1 | 449 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
96ba7208 | 450 | transport_name = "relay-metadata"; |
6dccd6c1 | 451 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
96ba7208 JD |
452 | transport_name = "relay-metadata-mmap"; |
453 | else | |
454 | return -EINVAL; | |
5dbbdb43 MD |
455 | break; |
456 | default: | |
457 | transport_name = "<unknown>"; | |
458 | break; | |
459 | } | |
98d7281c MJ |
460 | if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) { |
461 | ret = -EOVERFLOW; | |
9c1f4643 MD |
462 | goto refcount_error; |
463 | } | |
03037b98 MD |
464 | /* |
465 | * We tolerate no failure path after channel creation. It will stay | |
466 | * invariant for the rest of the session. | |
467 | */ | |
a90917c3 | 468 | chan = lttng_channel_create(session, transport_name, NULL, |
6dccd6c1 JD |
469 | chan_param->subbuf_size, |
470 | chan_param->num_subbuf, | |
471 | chan_param->switch_timer_interval, | |
d83004aa JD |
472 | chan_param->read_timer_interval, |
473 | channel_type); | |
03037b98 | 474 | if (!chan) { |
f3d01b96 | 475 | ret = -EINVAL; |
03037b98 MD |
476 | goto chan_error; |
477 | } | |
11b5a3c2 | 478 | chan->file = chan_file; |
c0e31d2e MD |
479 | chan_file->private_data = chan; |
480 | fd_install(chan_fd, chan_file); | |
ad1c05e1 | 481 | |
baf20995 MD |
482 | return chan_fd; |
483 | ||
03037b98 | 484 | chan_error: |
9c1f4643 MD |
485 | atomic_long_dec(&session_file->f_count); |
486 | refcount_error: | |
c0e31d2e | 487 | fput(chan_file); |
baf20995 MD |
488 | file_error: |
489 | put_unused_fd(chan_fd); | |
490 | fd_error: | |
baf20995 MD |
491 | return ret; |
492 | } | |
493 | ||
494 | /** | |
ad1c05e1 | 495 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 496 | * |
c0e31d2e | 497 | * @file: the file |
baf20995 MD |
498 | * @cmd: the command |
499 | * @arg: command arg | |
500 | * | |
501 | * This ioctl implements lttng commands: | |
38d024ae | 502 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 503 | * Returns a LTTng channel file descriptor |
e64957da MD |
504 | * LTTNG_KERNEL_ENABLE |
505 | * Enables tracing for a session (weak enable) | |
506 | * LTTNG_KERNEL_DISABLE | |
507 | * Disables tracing for a session (strong disable) | |
8070f5c0 MD |
508 | * LTTNG_KERNEL_METADATA |
509 | * Returns a LTTng metadata file descriptor | |
e0130fab MD |
510 | * LTTNG_KERNEL_SESSION_TRACK_PID |
511 | * Add PID to session tracker | |
512 | * LTTNG_KERNEL_SESSION_UNTRACK_PID | |
513 | * Remove PID from session tracker | |
ad1c05e1 MD |
514 | * |
515 | * The returned channel will be deleted when its file descriptor is closed. | |
516 | */ | |
517 | static | |
c0e31d2e | 518 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 519 | { |
a90917c3 | 520 | struct lttng_session *session = file->private_data; |
c0e31d2e | 521 | |
ad1c05e1 | 522 | switch (cmd) { |
6dccd6c1 JD |
523 | case LTTNG_KERNEL_OLD_CHANNEL: |
524 | { | |
525 | struct lttng_kernel_channel chan_param; | |
526 | struct lttng_kernel_old_channel old_chan_param; | |
527 | ||
528 | if (copy_from_user(&old_chan_param, | |
529 | (struct lttng_kernel_old_channel __user *) arg, | |
530 | sizeof(struct lttng_kernel_old_channel))) | |
531 | return -EFAULT; | |
532 | chan_param.overwrite = old_chan_param.overwrite; | |
533 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
534 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
535 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
536 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
537 | chan_param.output = old_chan_param.output; | |
538 | ||
539 | return lttng_abi_create_channel(file, &chan_param, | |
540 | PER_CPU_CHANNEL); | |
541 | } | |
38d024ae | 542 | case LTTNG_KERNEL_CHANNEL: |
6dccd6c1 JD |
543 | { |
544 | struct lttng_kernel_channel chan_param; | |
545 | ||
546 | if (copy_from_user(&chan_param, | |
38d024ae | 547 | (struct lttng_kernel_channel __user *) arg, |
6dccd6c1 JD |
548 | sizeof(struct lttng_kernel_channel))) |
549 | return -EFAULT; | |
550 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 551 | PER_CPU_CHANNEL); |
6dccd6c1 JD |
552 | } |
553 | case LTTNG_KERNEL_OLD_SESSION_START: | |
554 | case LTTNG_KERNEL_OLD_ENABLE: | |
38d024ae | 555 | case LTTNG_KERNEL_SESSION_START: |
e64957da | 556 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 557 | return lttng_session_enable(session); |
6dccd6c1 JD |
558 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
559 | case LTTNG_KERNEL_OLD_DISABLE: | |
38d024ae | 560 | case LTTNG_KERNEL_SESSION_STOP: |
e64957da | 561 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 562 | return lttng_session_disable(session); |
6dccd6c1 JD |
563 | case LTTNG_KERNEL_OLD_METADATA: |
564 | { | |
565 | struct lttng_kernel_channel chan_param; | |
566 | struct lttng_kernel_old_channel old_chan_param; | |
567 | ||
568 | if (copy_from_user(&old_chan_param, | |
569 | (struct lttng_kernel_old_channel __user *) arg, | |
570 | sizeof(struct lttng_kernel_old_channel))) | |
571 | return -EFAULT; | |
572 | chan_param.overwrite = old_chan_param.overwrite; | |
573 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
574 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
575 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
576 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
577 | chan_param.output = old_chan_param.output; | |
578 | ||
579 | return lttng_abi_create_channel(file, &chan_param, | |
580 | METADATA_CHANNEL); | |
581 | } | |
38d024ae | 582 | case LTTNG_KERNEL_METADATA: |
6dccd6c1 JD |
583 | { |
584 | struct lttng_kernel_channel chan_param; | |
585 | ||
586 | if (copy_from_user(&chan_param, | |
587 | (struct lttng_kernel_channel __user *) arg, | |
588 | sizeof(struct lttng_kernel_channel))) | |
589 | return -EFAULT; | |
590 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 591 | METADATA_CHANNEL); |
6dccd6c1 | 592 | } |
e0130fab MD |
593 | case LTTNG_KERNEL_SESSION_TRACK_PID: |
594 | return lttng_session_track_pid(session, (int) arg); | |
595 | case LTTNG_KERNEL_SESSION_UNTRACK_PID: | |
596 | return lttng_session_untrack_pid(session, (int) arg); | |
7e6f9ef6 MD |
597 | case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS: |
598 | return lttng_session_list_tracker_pids(session); | |
9616f0bf JD |
599 | case LTTNG_KERNEL_SESSION_METADATA_REGEN: |
600 | return lttng_session_metadata_regenerate(session); | |
601252cf MD |
601 | case LTTNG_KERNEL_SESSION_STATEDUMP: |
602 | return lttng_session_statedump(session); | |
ad1c05e1 MD |
603 | default: |
604 | return -ENOIOCTLCMD; | |
605 | } | |
606 | } | |
607 | ||
03037b98 MD |
608 | /* |
609 | * Called when the last file reference is dropped. | |
610 | * | |
611 | * Big fat note: channels and events are invariant for the whole session after | |
612 | * their creation. So this session destruction also destroys all channel and | |
613 | * event structures specific to this session (they are not destroyed when their | |
614 | * individual file is released). | |
615 | */ | |
ad1c05e1 | 616 | static |
03037b98 | 617 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 618 | { |
a90917c3 | 619 | struct lttng_session *session = file->private_data; |
c269fff4 MD |
620 | |
621 | if (session) | |
a90917c3 | 622 | lttng_session_destroy(session); |
11b5a3c2 | 623 | return 0; |
ad1c05e1 | 624 | } |
ad1c05e1 MD |
625 | |
626 | static const struct file_operations lttng_session_fops = { | |
a33c9927 | 627 | .owner = THIS_MODULE, |
03037b98 | 628 | .release = lttng_session_release, |
ad1c05e1 MD |
629 | .unlocked_ioctl = lttng_session_ioctl, |
630 | #ifdef CONFIG_COMPAT | |
03037b98 | 631 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 632 | #endif |
11b5a3c2 | 633 | }; |
ad1c05e1 | 634 | |
d83004aa JD |
635 | /** |
636 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation | |
637 | * @filp: the file | |
638 | * @wait: poll table | |
639 | * | |
640 | * Handles the poll operations for the metadata channels. | |
641 | */ | |
ad1c05e1 | 642 | static |
d83004aa JD |
643 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
644 | poll_table *wait) | |
645 | { | |
646 | struct lttng_metadata_stream *stream = filp->private_data; | |
647 | struct lib_ring_buffer *buf = stream->priv; | |
648 | int finalized; | |
649 | unsigned int mask = 0; | |
650 | ||
651 | if (filp->f_mode & FMODE_READ) { | |
652 | poll_wait_set_exclusive(wait); | |
653 | poll_wait(filp, &stream->read_wait, wait); | |
654 | ||
655 | finalized = stream->finalized; | |
656 | ||
657 | /* | |
658 | * lib_ring_buffer_is_finalized() contains a smp_rmb() | |
659 | * ordering finalized load before offsets loads. | |
660 | */ | |
661 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
662 | ||
663 | if (finalized) | |
664 | mask |= POLLHUP; | |
665 | ||
92d9f5e6 | 666 | mutex_lock(&stream->metadata_cache->lock); |
d83004aa | 667 | if (stream->metadata_cache->metadata_written > |
f613e3e6 | 668 | stream->metadata_out) |
d83004aa | 669 | mask |= POLLIN; |
92d9f5e6 | 670 | mutex_unlock(&stream->metadata_cache->lock); |
d83004aa JD |
671 | } |
672 | ||
673 | return mask; | |
674 | } | |
675 | ||
f613e3e6 MD |
676 | static |
677 | void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp, | |
678 | unsigned int cmd, unsigned long arg) | |
679 | { | |
680 | struct lttng_metadata_stream *stream = filp->private_data; | |
681 | ||
682 | stream->metadata_out = stream->metadata_in; | |
683 | } | |
684 | ||
d1344afa JD |
685 | /* |
686 | * Reset the counter of how much metadata has been consumed to 0. That way, | |
687 | * the consumer receives the content of the metadata cache unchanged. This is | |
688 | * different from the metadata_regenerate where the offset from epoch is | |
689 | * resampled, here we want the exact same content as the last time the metadata | |
690 | * was generated. This command is only possible if all the metadata written | |
691 | * in the cache has been output to the metadata stream to avoid corrupting the | |
692 | * metadata file. | |
693 | * | |
694 | * Return 0 on success, a negative value on error. | |
695 | */ | |
696 | static | |
697 | int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream) | |
698 | { | |
699 | int ret; | |
700 | struct lttng_metadata_cache *cache = stream->metadata_cache; | |
701 | ||
702 | mutex_lock(&cache->lock); | |
703 | if (stream->metadata_out != cache->metadata_written) { | |
704 | ret = -EBUSY; | |
705 | goto end; | |
706 | } | |
707 | stream->metadata_out = 0; | |
708 | stream->metadata_in = 0; | |
709 | wake_up_interruptible(&stream->read_wait); | |
710 | ret = 0; | |
711 | ||
712 | end: | |
713 | mutex_unlock(&cache->lock); | |
714 | return ret; | |
715 | } | |
716 | ||
d83004aa JD |
717 | static |
718 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, | |
719 | unsigned int cmd, unsigned long arg) | |
720 | { | |
721 | int ret; | |
722 | struct lttng_metadata_stream *stream = filp->private_data; | |
723 | struct lib_ring_buffer *buf = stream->priv; | |
724 | ||
725 | switch (cmd) { | |
d83004aa JD |
726 | case RING_BUFFER_GET_NEXT_SUBBUF: |
727 | { | |
35097f36 JD |
728 | struct lttng_metadata_stream *stream = filp->private_data; |
729 | struct lib_ring_buffer *buf = stream->priv; | |
730 | struct channel *chan = buf->backend.chan; | |
731 | ||
732 | ret = lttng_metadata_output_channel(stream, chan); | |
733 | if (ret > 0) { | |
734 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
735 | ret = 0; | |
736 | } else if (ret < 0) | |
d83004aa JD |
737 | goto err; |
738 | break; | |
739 | } | |
f613e3e6 MD |
740 | case RING_BUFFER_GET_SUBBUF: |
741 | { | |
742 | /* | |
743 | * Random access is not allowed for metadata channel. | |
744 | */ | |
745 | return -ENOSYS; | |
746 | } | |
c6f05468 | 747 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
35097f36 JD |
748 | case RING_BUFFER_FLUSH: |
749 | { | |
750 | struct lttng_metadata_stream *stream = filp->private_data; | |
751 | struct lib_ring_buffer *buf = stream->priv; | |
752 | struct channel *chan = buf->backend.chan; | |
753 | ||
754 | /* | |
755 | * Before doing the actual ring buffer flush, write up to one | |
756 | * packet of metadata in the ring buffer. | |
757 | */ | |
758 | ret = lttng_metadata_output_channel(stream, chan); | |
759 | if (ret < 0) | |
760 | goto err; | |
761 | break; | |
762 | } | |
9616f0bf JD |
763 | case RING_BUFFER_GET_METADATA_VERSION: |
764 | { | |
765 | struct lttng_metadata_stream *stream = filp->private_data; | |
766 | ||
767 | return put_u64(stream->version, arg); | |
768 | } | |
d1344afa JD |
769 | case RING_BUFFER_METADATA_CACHE_DUMP: |
770 | { | |
771 | struct lttng_metadata_stream *stream = filp->private_data; | |
772 | ||
773 | return lttng_metadata_cache_dump(stream); | |
774 | } | |
d83004aa JD |
775 | default: |
776 | break; | |
777 | } | |
f613e3e6 MD |
778 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
779 | ||
d83004aa | 780 | /* Performing lib ring buffer ioctl after our own. */ |
f613e3e6 MD |
781 | ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf); |
782 | if (ret < 0) | |
783 | goto err; | |
d83004aa | 784 | |
f613e3e6 MD |
785 | switch (cmd) { |
786 | case RING_BUFFER_PUT_NEXT_SUBBUF: | |
787 | { | |
788 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, | |
789 | cmd, arg); | |
790 | break; | |
791 | } | |
792 | default: | |
793 | break; | |
794 | } | |
d83004aa JD |
795 | err: |
796 | return ret; | |
797 | } | |
798 | ||
aeb9064d | 799 | #ifdef CONFIG_COMPAT |
d83004aa JD |
800 | static |
801 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, | |
802 | unsigned int cmd, unsigned long arg) | |
803 | { | |
804 | int ret; | |
805 | struct lttng_metadata_stream *stream = filp->private_data; | |
806 | struct lib_ring_buffer *buf = stream->priv; | |
807 | ||
808 | switch (cmd) { | |
d83004aa JD |
809 | case RING_BUFFER_GET_NEXT_SUBBUF: |
810 | { | |
35097f36 JD |
811 | struct lttng_metadata_stream *stream = filp->private_data; |
812 | struct lib_ring_buffer *buf = stream->priv; | |
813 | struct channel *chan = buf->backend.chan; | |
814 | ||
815 | ret = lttng_metadata_output_channel(stream, chan); | |
816 | if (ret > 0) { | |
817 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
818 | ret = 0; | |
819 | } else if (ret < 0) | |
d83004aa JD |
820 | goto err; |
821 | break; | |
822 | } | |
f613e3e6 MD |
823 | case RING_BUFFER_GET_SUBBUF: |
824 | { | |
825 | /* | |
826 | * Random access is not allowed for metadata channel. | |
827 | */ | |
828 | return -ENOSYS; | |
829 | } | |
c6f05468 | 830 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
96c55c2f MD |
831 | case RING_BUFFER_FLUSH: |
832 | { | |
833 | struct lttng_metadata_stream *stream = filp->private_data; | |
834 | struct lib_ring_buffer *buf = stream->priv; | |
835 | struct channel *chan = buf->backend.chan; | |
836 | ||
837 | /* | |
838 | * Before doing the actual ring buffer flush, write up to one | |
839 | * packet of metadata in the ring buffer. | |
840 | */ | |
841 | ret = lttng_metadata_output_channel(stream, chan); | |
842 | if (ret < 0) | |
843 | goto err; | |
844 | break; | |
845 | } | |
846 | case RING_BUFFER_GET_METADATA_VERSION: | |
847 | { | |
848 | struct lttng_metadata_stream *stream = filp->private_data; | |
849 | ||
850 | return put_u64(stream->version, arg); | |
851 | } | |
d1344afa JD |
852 | case RING_BUFFER_METADATA_CACHE_DUMP: |
853 | { | |
854 | struct lttng_metadata_stream *stream = filp->private_data; | |
855 | ||
856 | return lttng_metadata_cache_dump(stream); | |
857 | } | |
d83004aa JD |
858 | default: |
859 | break; | |
860 | } | |
f613e3e6 MD |
861 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
862 | ||
d83004aa | 863 | /* Performing lib ring buffer ioctl after our own. */ |
f613e3e6 MD |
864 | ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); |
865 | if (ret < 0) | |
866 | goto err; | |
d83004aa | 867 | |
f613e3e6 MD |
868 | switch (cmd) { |
869 | case RING_BUFFER_PUT_NEXT_SUBBUF: | |
870 | { | |
871 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, | |
872 | cmd, arg); | |
873 | break; | |
874 | } | |
875 | default: | |
876 | break; | |
877 | } | |
d83004aa JD |
878 | err: |
879 | return ret; | |
880 | } | |
aeb9064d | 881 | #endif |
d83004aa | 882 | |
b3b8072b MD |
883 | /* |
884 | * This is not used by anonymous file descriptors. This code is left | |
885 | * there if we ever want to implement an inode with open() operation. | |
886 | */ | |
d83004aa JD |
887 | static |
888 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) | |
889 | { | |
890 | struct lttng_metadata_stream *stream = inode->i_private; | |
891 | struct lib_ring_buffer *buf = stream->priv; | |
892 | ||
893 | file->private_data = buf; | |
b3b8072b MD |
894 | /* |
895 | * Since life-time of metadata cache differs from that of | |
896 | * session, we need to keep our own reference on the transport. | |
897 | */ | |
898 | if (!try_module_get(stream->transport->owner)) { | |
899 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
900 | return -EBUSY; | |
901 | } | |
d83004aa JD |
902 | return lib_ring_buffer_open(inode, file, buf); |
903 | } | |
904 | ||
905 | static | |
906 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) | |
907 | { | |
908 | struct lttng_metadata_stream *stream = file->private_data; | |
909 | struct lib_ring_buffer *buf = stream->priv; | |
910 | ||
911 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); | |
b3b8072b | 912 | module_put(stream->transport->owner); |
d83004aa JD |
913 | return lib_ring_buffer_release(inode, file, buf); |
914 | } | |
915 | ||
916 | static | |
917 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
918 | struct pipe_inode_info *pipe, size_t len, | |
919 | unsigned int flags) | |
920 | { | |
921 | struct lttng_metadata_stream *stream = in->private_data; | |
922 | struct lib_ring_buffer *buf = stream->priv; | |
923 | ||
924 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, | |
925 | flags, buf); | |
926 | } | |
927 | ||
928 | static | |
929 | int lttng_metadata_ring_buffer_mmap(struct file *filp, | |
930 | struct vm_area_struct *vma) | |
931 | { | |
932 | struct lttng_metadata_stream *stream = filp->private_data; | |
933 | struct lib_ring_buffer *buf = stream->priv; | |
934 | ||
935 | return lib_ring_buffer_mmap(filp, vma, buf); | |
936 | } | |
937 | ||
938 | static | |
939 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { | |
940 | .owner = THIS_MODULE, | |
941 | .open = lttng_metadata_ring_buffer_open, | |
942 | .release = lttng_metadata_ring_buffer_release, | |
943 | .poll = lttng_metadata_ring_buffer_poll, | |
944 | .splice_read = lttng_metadata_ring_buffer_splice_read, | |
945 | .mmap = lttng_metadata_ring_buffer_mmap, | |
946 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, | |
947 | .llseek = vfs_lib_ring_buffer_no_llseek, | |
948 | #ifdef CONFIG_COMPAT | |
949 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, | |
950 | #endif | |
951 | }; | |
952 | ||
953 | static | |
954 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, | |
955 | const struct file_operations *fops) | |
ad1c05e1 | 956 | { |
ad1c05e1 | 957 | int stream_fd, ret; |
11b5a3c2 | 958 | struct file *stream_file; |
ad1c05e1 | 959 | |
4ac10b76 | 960 | stream_fd = lttng_get_unused_fd(); |
ad1c05e1 MD |
961 | if (stream_fd < 0) { |
962 | ret = stream_fd; | |
963 | goto fd_error; | |
964 | } | |
d83004aa JD |
965 | stream_file = anon_inode_getfile("[lttng_stream]", fops, |
966 | stream_priv, O_RDWR); | |
c0e31d2e MD |
967 | if (IS_ERR(stream_file)) { |
968 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
969 | goto file_error; |
970 | } | |
409453cb MD |
971 | /* |
972 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
973 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
974 | * file descriptor, so we set FMODE_PREAD here. | |
975 | */ | |
d7b6f197 | 976 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 977 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
978 | /* |
979 | * The stream holds a reference to the channel within the generic ring | |
980 | * buffer library, so no need to hold a refcount on the channel and | |
981 | * session files here. | |
982 | */ | |
ad1c05e1 MD |
983 | return stream_fd; |
984 | ||
985 | file_error: | |
986 | put_unused_fd(stream_fd); | |
d83004aa JD |
987 | fd_error: |
988 | return ret; | |
989 | } | |
990 | ||
991 | static | |
992 | int lttng_abi_open_stream(struct file *channel_file) | |
993 | { | |
994 | struct lttng_channel *channel = channel_file->private_data; | |
995 | struct lib_ring_buffer *buf; | |
996 | int ret; | |
997 | void *stream_priv; | |
998 | ||
999 | buf = channel->ops->buffer_read_open(channel->chan); | |
1000 | if (!buf) | |
1001 | return -ENOENT; | |
1002 | ||
1003 | stream_priv = buf; | |
1004 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, | |
ed8d02d6 | 1005 | <tng_stream_ring_buffer_file_operations); |
d83004aa JD |
1006 | if (ret < 0) |
1007 | goto fd_error; | |
1008 | ||
1009 | return ret; | |
1010 | ||
1011 | fd_error: | |
1012 | channel->ops->buffer_read_close(buf); | |
1013 | return ret; | |
1014 | } | |
1015 | ||
1016 | static | |
1017 | int lttng_abi_open_metadata_stream(struct file *channel_file) | |
1018 | { | |
1019 | struct lttng_channel *channel = channel_file->private_data; | |
1020 | struct lttng_session *session = channel->session; | |
1021 | struct lib_ring_buffer *buf; | |
1022 | int ret; | |
1023 | struct lttng_metadata_stream *metadata_stream; | |
1024 | void *stream_priv; | |
1025 | ||
1026 | buf = channel->ops->buffer_read_open(channel->chan); | |
1027 | if (!buf) | |
1028 | return -ENOENT; | |
1029 | ||
1030 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), | |
1031 | GFP_KERNEL); | |
b3b8072b MD |
1032 | if (!metadata_stream) { |
1033 | ret = -ENOMEM; | |
1034 | goto nomem; | |
1035 | } | |
d83004aa JD |
1036 | metadata_stream->metadata_cache = session->metadata_cache; |
1037 | init_waitqueue_head(&metadata_stream->read_wait); | |
1038 | metadata_stream->priv = buf; | |
1039 | stream_priv = metadata_stream; | |
b3b8072b MD |
1040 | metadata_stream->transport = channel->transport; |
1041 | ||
1042 | /* | |
1043 | * Since life-time of metadata cache differs from that of | |
1044 | * session, we need to keep our own reference on the transport. | |
1045 | */ | |
1046 | if (!try_module_get(metadata_stream->transport->owner)) { | |
1047 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
1048 | ret = -EINVAL; | |
1049 | goto notransport; | |
1050 | } | |
1051 | ||
901aaa5f FD |
1052 | if (!lttng_kref_get(&session->metadata_cache->refcount)) { |
1053 | ret = -EOVERFLOW; | |
9c1f4643 | 1054 | goto kref_error; |
901aaa5f FD |
1055 | } |
1056 | ||
d83004aa JD |
1057 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
1058 | <tng_metadata_ring_buffer_file_operations); | |
1059 | if (ret < 0) | |
1060 | goto fd_error; | |
1061 | ||
d83004aa JD |
1062 | list_add(&metadata_stream->list, |
1063 | &session->metadata_cache->metadata_stream); | |
1064 | return ret; | |
1065 | ||
ad1c05e1 | 1066 | fd_error: |
9c1f4643 MD |
1067 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
1068 | kref_error: | |
b3b8072b MD |
1069 | module_put(metadata_stream->transport->owner); |
1070 | notransport: | |
1071 | kfree(metadata_stream); | |
1072 | nomem: | |
11b5a3c2 | 1073 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
1074 | return ret; |
1075 | } | |
1076 | ||
653fe716 | 1077 | static |
c0e31d2e | 1078 | int lttng_abi_create_event(struct file *channel_file, |
6dccd6c1 | 1079 | struct lttng_kernel_event *event_param) |
653fe716 | 1080 | { |
a90917c3 | 1081 | struct lttng_channel *channel = channel_file->private_data; |
653fe716 | 1082 | int event_fd, ret; |
11b5a3c2 | 1083 | struct file *event_file; |
3c997079 | 1084 | void *priv; |
653fe716 | 1085 | |
6dccd6c1 JD |
1086 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
1087 | switch (event_param->instrumentation) { | |
7371f44c | 1088 | case LTTNG_KERNEL_KRETPROBE: |
6dccd6c1 | 1089 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
7371f44c | 1090 | break; |
ab2277d6 | 1091 | case LTTNG_KERNEL_KPROBE: |
6dccd6c1 | 1092 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 1093 | break; |
ab2277d6 | 1094 | case LTTNG_KERNEL_FUNCTION: |
6dccd6c1 | 1095 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 MD |
1096 | break; |
1097 | default: | |
1098 | break; | |
1099 | } | |
33a39a3c MD |
1100 | event_fd = lttng_get_unused_fd(); |
1101 | if (event_fd < 0) { | |
1102 | ret = event_fd; | |
1103 | goto fd_error; | |
1104 | } | |
1105 | event_file = anon_inode_getfile("[lttng_event]", | |
1106 | <tng_event_fops, | |
1107 | NULL, O_RDWR); | |
1108 | if (IS_ERR(event_file)) { | |
1109 | ret = PTR_ERR(event_file); | |
1110 | goto file_error; | |
1111 | } | |
9c1f4643 | 1112 | /* The event holds a reference on the channel */ |
98d7281c | 1113 | if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) { |
0d2c717f | 1114 | ret = -EOVERFLOW; |
9c1f4643 MD |
1115 | goto refcount_error; |
1116 | } | |
33a39a3c MD |
1117 | if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT |
1118 | || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) { | |
1119 | struct lttng_enabler *enabler; | |
1120 | ||
4993071a PP |
1121 | if (strutils_is_star_glob_pattern(event_param->name)) { |
1122 | /* | |
1123 | * If the event name is a star globbing pattern, | |
1124 | * we create the special star globbing enabler. | |
1125 | */ | |
1126 | enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB, | |
33a39a3c | 1127 | event_param, channel); |
3c997079 | 1128 | } else { |
33a39a3c MD |
1129 | enabler = lttng_enabler_create(LTTNG_ENABLER_NAME, |
1130 | event_param, channel); | |
1ec65de1 | 1131 | } |
33a39a3c MD |
1132 | priv = enabler; |
1133 | } else { | |
1134 | struct lttng_event *event; | |
4ee2453d | 1135 | |
33a39a3c MD |
1136 | /* |
1137 | * We tolerate no failure path after event creation. It | |
1138 | * will stay invariant for the rest of the session. | |
1139 | */ | |
1140 | event = lttng_event_create(channel, event_param, | |
1141 | NULL, NULL, | |
1142 | event_param->instrumentation); | |
1143 | WARN_ON_ONCE(!event); | |
1144 | if (IS_ERR(event)) { | |
1145 | ret = PTR_ERR(event); | |
1146 | goto event_error; | |
80f87dd2 | 1147 | } |
33a39a3c | 1148 | priv = event; |
03037b98 | 1149 | } |
33a39a3c MD |
1150 | event_file->private_data = priv; |
1151 | fd_install(event_fd, event_file); | |
653fe716 MD |
1152 | return event_fd; |
1153 | ||
03037b98 | 1154 | event_error: |
9c1f4643 MD |
1155 | atomic_long_dec(&channel_file->f_count); |
1156 | refcount_error: | |
c0e31d2e | 1157 | fput(event_file); |
653fe716 MD |
1158 | file_error: |
1159 | put_unused_fd(event_fd); | |
1160 | fd_error: | |
653fe716 MD |
1161 | return ret; |
1162 | } | |
ad1c05e1 MD |
1163 | |
1164 | /** | |
1165 | * lttng_channel_ioctl - lttng syscall through ioctl | |
1166 | * | |
c0e31d2e | 1167 | * @file: the file |
ad1c05e1 MD |
1168 | * @cmd: the command |
1169 | * @arg: command arg | |
1170 | * | |
1171 | * This ioctl implements lttng commands: | |
38d024ae | 1172 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
1173 | * Returns an event stream file descriptor or failure. |
1174 | * (typically, one event stream records events from one CPU) | |
38d024ae | 1175 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 1176 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
1177 | * LTTNG_KERNEL_CONTEXT |
1178 | * Prepend a context field to each event in the channel | |
e64957da MD |
1179 | * LTTNG_KERNEL_ENABLE |
1180 | * Enable recording for events in this channel (weak enable) | |
1181 | * LTTNG_KERNEL_DISABLE | |
1182 | * Disable recording for events in this channel (strong disable) | |
baf20995 | 1183 | * |
baf20995 MD |
1184 | * Channel and event file descriptors also hold a reference on the session. |
1185 | */ | |
ad1c05e1 | 1186 | static |
c0e31d2e | 1187 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 1188 | { |
a90917c3 | 1189 | struct lttng_channel *channel = file->private_data; |
8070f5c0 | 1190 | |
baf20995 | 1191 | switch (cmd) { |
6dccd6c1 | 1192 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 1193 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 1194 | return lttng_abi_open_stream(file); |
6dccd6c1 JD |
1195 | case LTTNG_KERNEL_OLD_EVENT: |
1196 | { | |
1197 | struct lttng_kernel_event *uevent_param; | |
1198 | struct lttng_kernel_old_event *old_uevent_param; | |
1199 | int ret; | |
1200 | ||
1201 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), | |
1202 | GFP_KERNEL); | |
1203 | if (!uevent_param) { | |
1204 | ret = -ENOMEM; | |
1205 | goto old_event_end; | |
1206 | } | |
1207 | old_uevent_param = kmalloc( | |
1208 | sizeof(struct lttng_kernel_old_event), | |
1209 | GFP_KERNEL); | |
1210 | if (!old_uevent_param) { | |
1211 | ret = -ENOMEM; | |
1212 | goto old_event_error_free_param; | |
1213 | } | |
1214 | if (copy_from_user(old_uevent_param, | |
1215 | (struct lttng_kernel_old_event __user *) arg, | |
1216 | sizeof(struct lttng_kernel_old_event))) { | |
1217 | ret = -EFAULT; | |
1218 | goto old_event_error_free_old_param; | |
1219 | } | |
1220 | ||
1221 | memcpy(uevent_param->name, old_uevent_param->name, | |
1222 | sizeof(uevent_param->name)); | |
1223 | uevent_param->instrumentation = | |
1224 | old_uevent_param->instrumentation; | |
1225 | ||
1226 | switch (old_uevent_param->instrumentation) { | |
1227 | case LTTNG_KERNEL_KPROBE: | |
1228 | uevent_param->u.kprobe.addr = | |
1229 | old_uevent_param->u.kprobe.addr; | |
1230 | uevent_param->u.kprobe.offset = | |
1231 | old_uevent_param->u.kprobe.offset; | |
1232 | memcpy(uevent_param->u.kprobe.symbol_name, | |
1233 | old_uevent_param->u.kprobe.symbol_name, | |
1234 | sizeof(uevent_param->u.kprobe.symbol_name)); | |
1235 | break; | |
1236 | case LTTNG_KERNEL_KRETPROBE: | |
1237 | uevent_param->u.kretprobe.addr = | |
1238 | old_uevent_param->u.kretprobe.addr; | |
1239 | uevent_param->u.kretprobe.offset = | |
1240 | old_uevent_param->u.kretprobe.offset; | |
1241 | memcpy(uevent_param->u.kretprobe.symbol_name, | |
1242 | old_uevent_param->u.kretprobe.symbol_name, | |
1243 | sizeof(uevent_param->u.kretprobe.symbol_name)); | |
1244 | break; | |
1245 | case LTTNG_KERNEL_FUNCTION: | |
1246 | memcpy(uevent_param->u.ftrace.symbol_name, | |
1247 | old_uevent_param->u.ftrace.symbol_name, | |
1248 | sizeof(uevent_param->u.ftrace.symbol_name)); | |
1249 | break; | |
1250 | default: | |
1251 | break; | |
1252 | } | |
1253 | ret = lttng_abi_create_event(file, uevent_param); | |
1254 | ||
1255 | old_event_error_free_old_param: | |
1256 | kfree(old_uevent_param); | |
1257 | old_event_error_free_param: | |
1258 | kfree(uevent_param); | |
1259 | old_event_end: | |
1260 | return ret; | |
1261 | } | |
38d024ae | 1262 | case LTTNG_KERNEL_EVENT: |
6dccd6c1 JD |
1263 | { |
1264 | struct lttng_kernel_event uevent_param; | |
1265 | ||
1266 | if (copy_from_user(&uevent_param, | |
1267 | (struct lttng_kernel_event __user *) arg, | |
1268 | sizeof(uevent_param))) | |
1269 | return -EFAULT; | |
1270 | return lttng_abi_create_event(file, &uevent_param); | |
1271 | } | |
1272 | case LTTNG_KERNEL_OLD_CONTEXT: | |
1273 | { | |
1274 | struct lttng_kernel_context *ucontext_param; | |
1275 | struct lttng_kernel_old_context *old_ucontext_param; | |
1276 | int ret; | |
1277 | ||
1278 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), | |
1279 | GFP_KERNEL); | |
1280 | if (!ucontext_param) { | |
1281 | ret = -ENOMEM; | |
1282 | goto old_ctx_end; | |
1283 | } | |
1284 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), | |
1285 | GFP_KERNEL); | |
1286 | if (!old_ucontext_param) { | |
1287 | ret = -ENOMEM; | |
1288 | goto old_ctx_error_free_param; | |
1289 | } | |
1290 | ||
1291 | if (copy_from_user(old_ucontext_param, | |
1292 | (struct lttng_kernel_old_context __user *) arg, | |
1293 | sizeof(struct lttng_kernel_old_context))) { | |
1294 | ret = -EFAULT; | |
1295 | goto old_ctx_error_free_old_param; | |
1296 | } | |
1297 | ucontext_param->ctx = old_ucontext_param->ctx; | |
1298 | memcpy(ucontext_param->padding, old_ucontext_param->padding, | |
1299 | sizeof(ucontext_param->padding)); | |
1300 | /* only type that uses the union */ | |
1301 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { | |
1302 | ucontext_param->u.perf_counter.type = | |
1303 | old_ucontext_param->u.perf_counter.type; | |
1304 | ucontext_param->u.perf_counter.config = | |
1305 | old_ucontext_param->u.perf_counter.config; | |
1306 | memcpy(ucontext_param->u.perf_counter.name, | |
1307 | old_ucontext_param->u.perf_counter.name, | |
1308 | sizeof(ucontext_param->u.perf_counter.name)); | |
1309 | } | |
1310 | ||
1311 | ret = lttng_abi_add_context(file, | |
1312 | ucontext_param, | |
1313 | &channel->ctx, channel->session); | |
1314 | ||
1315 | old_ctx_error_free_old_param: | |
1316 | kfree(old_ucontext_param); | |
1317 | old_ctx_error_free_param: | |
1318 | kfree(ucontext_param); | |
1319 | old_ctx_end: | |
1320 | return ret; | |
1321 | } | |
8070f5c0 | 1322 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 JD |
1323 | { |
1324 | struct lttng_kernel_context ucontext_param; | |
1325 | ||
1326 | if (copy_from_user(&ucontext_param, | |
8070f5c0 | 1327 | (struct lttng_kernel_context __user *) arg, |
6dccd6c1 JD |
1328 | sizeof(ucontext_param))) |
1329 | return -EFAULT; | |
1330 | return lttng_abi_add_context(file, | |
1331 | &ucontext_param, | |
8070f5c0 | 1332 | &channel->ctx, channel->session); |
6dccd6c1 JD |
1333 | } |
1334 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1335 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 1336 | return lttng_channel_enable(channel); |
6dccd6c1 | 1337 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1338 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 1339 | return lttng_channel_disable(channel); |
12e579db MD |
1340 | case LTTNG_KERNEL_SYSCALL_MASK: |
1341 | return lttng_channel_syscall_mask(channel, | |
1342 | (struct lttng_kernel_syscall_mask __user *) arg); | |
baf20995 MD |
1343 | default: |
1344 | return -ENOIOCTLCMD; | |
1345 | } | |
1346 | } | |
1347 | ||
5dbbdb43 MD |
1348 | /** |
1349 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
1350 | * | |
1351 | * @file: the file | |
1352 | * @cmd: the command | |
1353 | * @arg: command arg | |
1354 | * | |
1355 | * This ioctl implements lttng commands: | |
38d024ae | 1356 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
1357 | * Returns an event stream file descriptor or failure. |
1358 | * | |
1359 | * Channel and event file descriptors also hold a reference on the session. | |
1360 | */ | |
1361 | static | |
1362 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1363 | { | |
1364 | switch (cmd) { | |
6dccd6c1 | 1365 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 1366 | case LTTNG_KERNEL_STREAM: |
d83004aa | 1367 | return lttng_abi_open_metadata_stream(file); |
5dbbdb43 MD |
1368 | default: |
1369 | return -ENOIOCTLCMD; | |
1370 | } | |
1371 | } | |
1372 | ||
653fe716 MD |
1373 | /** |
1374 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
1375 | * | |
c0e31d2e | 1376 | * @file: the file |
653fe716 MD |
1377 | * @wait: poll table |
1378 | */ | |
c0e31d2e | 1379 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 1380 | { |
a90917c3 | 1381 | struct lttng_channel *channel = file->private_data; |
653fe716 MD |
1382 | unsigned int mask = 0; |
1383 | ||
c0e31d2e | 1384 | if (file->f_mode & FMODE_READ) { |
a33e44a6 | 1385 | poll_wait_set_exclusive(wait); |
24cedcfe MD |
1386 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
1387 | wait); | |
653fe716 | 1388 | |
254ec7bc MD |
1389 | if (channel->ops->is_disabled(channel->chan)) |
1390 | return POLLERR; | |
24cedcfe | 1391 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 | 1392 | return POLLHUP; |
f71ecafa | 1393 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
653fe716 | 1394 | return POLLIN | POLLRDNORM; |
f71ecafa | 1395 | return 0; |
653fe716 MD |
1396 | } |
1397 | return mask; | |
1398 | ||
1399 | } | |
1400 | ||
0a84a57f MD |
1401 | static |
1402 | int lttng_channel_release(struct inode *inode, struct file *file) | |
1403 | { | |
a90917c3 | 1404 | struct lttng_channel *channel = file->private_data; |
c269fff4 MD |
1405 | |
1406 | if (channel) | |
1407 | fput(channel->session->file); | |
0a84a57f MD |
1408 | return 0; |
1409 | } | |
1410 | ||
d83004aa JD |
1411 | static |
1412 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) | |
1413 | { | |
1414 | struct lttng_channel *channel = file->private_data; | |
1415 | ||
1416 | if (channel) { | |
d83004aa | 1417 | fput(channel->session->file); |
a3381417 | 1418 | lttng_metadata_channel_destroy(channel); |
d83004aa JD |
1419 | } |
1420 | ||
1421 | return 0; | |
1422 | } | |
1423 | ||
ad1c05e1 | 1424 | static const struct file_operations lttng_channel_fops = { |
a33c9927 | 1425 | .owner = THIS_MODULE, |
03037b98 | 1426 | .release = lttng_channel_release, |
653fe716 | 1427 | .poll = lttng_channel_poll, |
ad1c05e1 | 1428 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 1429 | #ifdef CONFIG_COMPAT |
03037b98 | 1430 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 1431 | #endif |
11b5a3c2 | 1432 | }; |
baf20995 | 1433 | |
5dbbdb43 | 1434 | static const struct file_operations lttng_metadata_fops = { |
a33c9927 | 1435 | .owner = THIS_MODULE, |
d83004aa | 1436 | .release = lttng_metadata_channel_release, |
5dbbdb43 MD |
1437 | .unlocked_ioctl = lttng_metadata_ioctl, |
1438 | #ifdef CONFIG_COMPAT | |
1439 | .compat_ioctl = lttng_metadata_ioctl, | |
1440 | #endif | |
1441 | }; | |
1442 | ||
8070f5c0 MD |
1443 | /** |
1444 | * lttng_event_ioctl - lttng syscall through ioctl | |
1445 | * | |
1446 | * @file: the file | |
1447 | * @cmd: the command | |
1448 | * @arg: command arg | |
1449 | * | |
1450 | * This ioctl implements lttng commands: | |
8070f5c0 MD |
1451 | * LTTNG_KERNEL_CONTEXT |
1452 | * Prepend a context field to each record of this event | |
e64957da MD |
1453 | * LTTNG_KERNEL_ENABLE |
1454 | * Enable recording for this event (weak enable) | |
1455 | * LTTNG_KERNEL_DISABLE | |
1456 | * Disable recording for this event (strong disable) | |
8070f5c0 MD |
1457 | */ |
1458 | static | |
1459 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1460 | { | |
3c997079 MD |
1461 | struct lttng_event *event; |
1462 | struct lttng_enabler *enabler; | |
1463 | enum lttng_event_type *evtype = file->private_data; | |
8070f5c0 MD |
1464 | |
1465 | switch (cmd) { | |
6dccd6c1 JD |
1466 | case LTTNG_KERNEL_OLD_CONTEXT: |
1467 | { | |
3c997079 MD |
1468 | /* Not implemented */ |
1469 | return -ENOSYS; | |
6dccd6c1 | 1470 | } |
8070f5c0 | 1471 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 | 1472 | { |
3c997079 MD |
1473 | /* Not implemented */ |
1474 | return -ENOSYS; | |
6dccd6c1 JD |
1475 | } |
1476 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1477 | case LTTNG_KERNEL_ENABLE: |
3c997079 MD |
1478 | switch (*evtype) { |
1479 | case LTTNG_TYPE_EVENT: | |
1480 | event = file->private_data; | |
1481 | return lttng_event_enable(event); | |
1482 | case LTTNG_TYPE_ENABLER: | |
1483 | enabler = file->private_data; | |
1484 | return lttng_enabler_enable(enabler); | |
1485 | default: | |
1486 | WARN_ON_ONCE(1); | |
1487 | return -ENOSYS; | |
1488 | } | |
6dccd6c1 | 1489 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1490 | case LTTNG_KERNEL_DISABLE: |
3c997079 MD |
1491 | switch (*evtype) { |
1492 | case LTTNG_TYPE_EVENT: | |
1493 | event = file->private_data; | |
1494 | return lttng_event_disable(event); | |
1495 | case LTTNG_TYPE_ENABLER: | |
1496 | enabler = file->private_data; | |
1497 | return lttng_enabler_disable(enabler); | |
1498 | default: | |
1499 | WARN_ON_ONCE(1); | |
1500 | return -ENOSYS; | |
1501 | } | |
07dfc1d0 MD |
1502 | case LTTNG_KERNEL_FILTER: |
1503 | switch (*evtype) { | |
1504 | case LTTNG_TYPE_EVENT: | |
1505 | return -EINVAL; | |
1506 | case LTTNG_TYPE_ENABLER: | |
1507 | { | |
1508 | enabler = file->private_data; | |
1509 | return lttng_enabler_attach_bytecode(enabler, | |
1510 | (struct lttng_kernel_filter_bytecode __user *) arg); | |
1511 | } | |
0586316f MD |
1512 | default: |
1513 | WARN_ON_ONCE(1); | |
1514 | return -ENOSYS; | |
07dfc1d0 | 1515 | } |
3aed4dca FD |
1516 | case LTTNG_KERNEL_ADD_CALLSITE: |
1517 | switch (*evtype) { | |
1518 | case LTTNG_TYPE_EVENT: | |
1519 | event = file->private_data; | |
1520 | return lttng_event_add_callsite(event, | |
1521 | (struct lttng_kernel_event_callsite __user *) arg); | |
1522 | case LTTNG_TYPE_ENABLER: | |
1523 | return -EINVAL; | |
1524 | } | |
8070f5c0 MD |
1525 | default: |
1526 | return -ENOIOCTLCMD; | |
1527 | } | |
1528 | } | |
1529 | ||
0a84a57f MD |
1530 | static |
1531 | int lttng_event_release(struct inode *inode, struct file *file) | |
1532 | { | |
3c997079 MD |
1533 | struct lttng_event *event; |
1534 | struct lttng_enabler *enabler; | |
1535 | enum lttng_event_type *evtype = file->private_data; | |
1536 | ||
1537 | if (!evtype) | |
1538 | return 0; | |
1539 | ||
1540 | switch (*evtype) { | |
1541 | case LTTNG_TYPE_EVENT: | |
1542 | event = file->private_data; | |
1543 | if (event) | |
1544 | fput(event->chan->file); | |
1545 | break; | |
1546 | case LTTNG_TYPE_ENABLER: | |
1547 | enabler = file->private_data; | |
1548 | if (enabler) | |
1549 | fput(enabler->chan->file); | |
1550 | break; | |
1551 | default: | |
1552 | WARN_ON_ONCE(1); | |
1553 | break; | |
1554 | } | |
c269fff4 | 1555 | |
0a84a57f MD |
1556 | return 0; |
1557 | } | |
1558 | ||
3b923e5b | 1559 | /* TODO: filter control ioctl */ |
0a84a57f | 1560 | static const struct file_operations lttng_event_fops = { |
a33c9927 | 1561 | .owner = THIS_MODULE, |
0a84a57f | 1562 | .release = lttng_event_release, |
8070f5c0 MD |
1563 | .unlocked_ioctl = lttng_event_ioctl, |
1564 | #ifdef CONFIG_COMPAT | |
1565 | .compat_ioctl = lttng_event_ioctl, | |
1566 | #endif | |
11b5a3c2 | 1567 | }; |
0a84a57f | 1568 | |
3b731ab1 JD |
1569 | static int put_u64(uint64_t val, unsigned long arg) |
1570 | { | |
1571 | return put_user(val, (uint64_t __user *) arg); | |
1572 | } | |
1573 | ||
ed8d02d6 JD |
1574 | static long lttng_stream_ring_buffer_ioctl(struct file *filp, |
1575 | unsigned int cmd, unsigned long arg) | |
1576 | { | |
3b731ab1 JD |
1577 | struct lib_ring_buffer *buf = filp->private_data; |
1578 | struct channel *chan = buf->backend.chan; | |
1579 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
dd5a0db3 | 1580 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
3b731ab1 JD |
1581 | int ret; |
1582 | ||
1583 | if (atomic_read(&chan->record_disabled)) | |
1584 | return -EIO; | |
1585 | ||
ed8d02d6 | 1586 | switch (cmd) { |
3b731ab1 JD |
1587 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN: |
1588 | { | |
1589 | uint64_t ts; | |
1590 | ||
dd5a0db3 | 1591 | ret = ops->timestamp_begin(config, buf, &ts); |
3b731ab1 JD |
1592 | if (ret < 0) |
1593 | goto error; | |
1594 | return put_u64(ts, arg); | |
1595 | } | |
1596 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_END: | |
1597 | { | |
1598 | uint64_t ts; | |
1599 | ||
dd5a0db3 | 1600 | ret = ops->timestamp_end(config, buf, &ts); |
3b731ab1 JD |
1601 | if (ret < 0) |
1602 | goto error; | |
1603 | return put_u64(ts, arg); | |
1604 | } | |
1605 | case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED: | |
1606 | { | |
1607 | uint64_t ed; | |
1608 | ||
dd5a0db3 | 1609 | ret = ops->events_discarded(config, buf, &ed); |
3b731ab1 JD |
1610 | if (ret < 0) |
1611 | goto error; | |
1612 | return put_u64(ed, arg); | |
1613 | } | |
1614 | case LTTNG_RING_BUFFER_GET_CONTENT_SIZE: | |
1615 | { | |
1616 | uint64_t cs; | |
1617 | ||
dd5a0db3 | 1618 | ret = ops->content_size(config, buf, &cs); |
3b731ab1 JD |
1619 | if (ret < 0) |
1620 | goto error; | |
1621 | return put_u64(cs, arg); | |
1622 | } | |
1623 | case LTTNG_RING_BUFFER_GET_PACKET_SIZE: | |
1624 | { | |
1625 | uint64_t ps; | |
1626 | ||
dd5a0db3 | 1627 | ret = ops->packet_size(config, buf, &ps); |
3b731ab1 JD |
1628 | if (ret < 0) |
1629 | goto error; | |
1630 | return put_u64(ps, arg); | |
1631 | } | |
1632 | case LTTNG_RING_BUFFER_GET_STREAM_ID: | |
1633 | { | |
1634 | uint64_t si; | |
1635 | ||
dd5a0db3 | 1636 | ret = ops->stream_id(config, buf, &si); |
3b731ab1 JD |
1637 | if (ret < 0) |
1638 | goto error; | |
1639 | return put_u64(si, arg); | |
1640 | } | |
2348ca17 JD |
1641 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
1642 | { | |
1643 | uint64_t ts; | |
1644 | ||
dd5a0db3 | 1645 | ret = ops->current_timestamp(config, buf, &ts); |
2348ca17 JD |
1646 | if (ret < 0) |
1647 | goto error; | |
1648 | return put_u64(ts, arg); | |
1649 | } | |
5b3cf4f9 JD |
1650 | case LTTNG_RING_BUFFER_GET_SEQ_NUM: |
1651 | { | |
1652 | uint64_t seq; | |
1653 | ||
1654 | ret = ops->sequence_number(config, buf, &seq); | |
1655 | if (ret < 0) | |
1656 | goto error; | |
1657 | return put_u64(seq, arg); | |
1658 | } | |
5594698f JD |
1659 | case LTTNG_RING_BUFFER_INSTANCE_ID: |
1660 | { | |
1661 | uint64_t id; | |
1662 | ||
1663 | ret = ops->instance_id(config, buf, &id); | |
1664 | if (ret < 0) | |
1665 | goto error; | |
1666 | return put_u64(id, arg); | |
1667 | } | |
3b731ab1 JD |
1668 | default: |
1669 | return lib_ring_buffer_file_operations.unlocked_ioctl(filp, | |
1670 | cmd, arg); | |
ed8d02d6 | 1671 | } |
3b731ab1 JD |
1672 | |
1673 | error: | |
1674 | return -ENOSYS; | |
ed8d02d6 JD |
1675 | } |
1676 | ||
1677 | #ifdef CONFIG_COMPAT | |
1678 | static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp, | |
1679 | unsigned int cmd, unsigned long arg) | |
1680 | { | |
3b731ab1 JD |
1681 | struct lib_ring_buffer *buf = filp->private_data; |
1682 | struct channel *chan = buf->backend.chan; | |
1683 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
dd5a0db3 | 1684 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
3b731ab1 JD |
1685 | int ret; |
1686 | ||
1687 | if (atomic_read(&chan->record_disabled)) | |
1688 | return -EIO; | |
1689 | ||
ed8d02d6 | 1690 | switch (cmd) { |
3b731ab1 JD |
1691 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN: |
1692 | { | |
1693 | uint64_t ts; | |
1694 | ||
dd5a0db3 | 1695 | ret = ops->timestamp_begin(config, buf, &ts); |
3b731ab1 JD |
1696 | if (ret < 0) |
1697 | goto error; | |
1698 | return put_u64(ts, arg); | |
1699 | } | |
1700 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END: | |
1701 | { | |
1702 | uint64_t ts; | |
1703 | ||
dd5a0db3 | 1704 | ret = ops->timestamp_end(config, buf, &ts); |
3b731ab1 JD |
1705 | if (ret < 0) |
1706 | goto error; | |
1707 | return put_u64(ts, arg); | |
1708 | } | |
1709 | case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED: | |
1710 | { | |
1711 | uint64_t ed; | |
1712 | ||
dd5a0db3 | 1713 | ret = ops->events_discarded(config, buf, &ed); |
3b731ab1 JD |
1714 | if (ret < 0) |
1715 | goto error; | |
1716 | return put_u64(ed, arg); | |
ed8d02d6 | 1717 | } |
3b731ab1 JD |
1718 | case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE: |
1719 | { | |
1720 | uint64_t cs; | |
1721 | ||
dd5a0db3 | 1722 | ret = ops->content_size(config, buf, &cs); |
3b731ab1 JD |
1723 | if (ret < 0) |
1724 | goto error; | |
1725 | return put_u64(cs, arg); | |
1726 | } | |
1727 | case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE: | |
1728 | { | |
1729 | uint64_t ps; | |
1730 | ||
dd5a0db3 | 1731 | ret = ops->packet_size(config, buf, &ps); |
3b731ab1 JD |
1732 | if (ret < 0) |
1733 | goto error; | |
1734 | return put_u64(ps, arg); | |
1735 | } | |
1736 | case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID: | |
1737 | { | |
1738 | uint64_t si; | |
1739 | ||
dd5a0db3 | 1740 | ret = ops->stream_id(config, buf, &si); |
3b731ab1 JD |
1741 | if (ret < 0) |
1742 | goto error; | |
1743 | return put_u64(si, arg); | |
1744 | } | |
2348ca17 JD |
1745 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
1746 | { | |
1747 | uint64_t ts; | |
1748 | ||
dd5a0db3 | 1749 | ret = ops->current_timestamp(config, buf, &ts); |
2348ca17 JD |
1750 | if (ret < 0) |
1751 | goto error; | |
1752 | return put_u64(ts, arg); | |
1753 | } | |
5b3cf4f9 JD |
1754 | case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM: |
1755 | { | |
1756 | uint64_t seq; | |
1757 | ||
1758 | ret = ops->sequence_number(config, buf, &seq); | |
1759 | if (ret < 0) | |
1760 | goto error; | |
1761 | return put_u64(seq, arg); | |
1762 | } | |
5594698f JD |
1763 | case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID: |
1764 | { | |
1765 | uint64_t id; | |
1766 | ||
1767 | ret = ops->instance_id(config, buf, &id); | |
1768 | if (ret < 0) | |
1769 | goto error; | |
1770 | return put_u64(id, arg); | |
1771 | } | |
3b731ab1 JD |
1772 | default: |
1773 | return lib_ring_buffer_file_operations.compat_ioctl(filp, | |
1774 | cmd, arg); | |
1775 | } | |
1776 | ||
1777 | error: | |
1778 | return -ENOSYS; | |
ed8d02d6 JD |
1779 | } |
1780 | #endif /* CONFIG_COMPAT */ | |
1781 | ||
1782 | static void lttng_stream_override_ring_buffer_fops(void) | |
1783 | { | |
1784 | lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE; | |
1785 | lttng_stream_ring_buffer_file_operations.open = | |
1786 | lib_ring_buffer_file_operations.open; | |
1787 | lttng_stream_ring_buffer_file_operations.release = | |
1788 | lib_ring_buffer_file_operations.release; | |
1789 | lttng_stream_ring_buffer_file_operations.poll = | |
1790 | lib_ring_buffer_file_operations.poll; | |
1791 | lttng_stream_ring_buffer_file_operations.splice_read = | |
1792 | lib_ring_buffer_file_operations.splice_read; | |
1793 | lttng_stream_ring_buffer_file_operations.mmap = | |
1794 | lib_ring_buffer_file_operations.mmap; | |
1795 | lttng_stream_ring_buffer_file_operations.unlocked_ioctl = | |
1796 | lttng_stream_ring_buffer_ioctl; | |
1797 | lttng_stream_ring_buffer_file_operations.llseek = | |
1798 | lib_ring_buffer_file_operations.llseek; | |
1799 | #ifdef CONFIG_COMPAT | |
1800 | lttng_stream_ring_buffer_file_operations.compat_ioctl = | |
1801 | lttng_stream_ring_buffer_compat_ioctl; | |
1802 | #endif | |
1803 | } | |
1804 | ||
80996790 | 1805 | int __init lttng_abi_init(void) |
baf20995 MD |
1806 | { |
1807 | int ret = 0; | |
1808 | ||
6d2a620c | 1809 | wrapper_vmalloc_sync_all(); |
2754583e | 1810 | lttng_clock_ref(); |
f771eda6 JD |
1811 | |
1812 | ret = lttng_tp_mempool_init(); | |
1813 | if (ret) { | |
1814 | goto error; | |
1815 | } | |
1816 | ||
d29348f7 | 1817 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
e6a17f26 | 1818 | <tng_fops, NULL); |
2470a237 | 1819 | |
255e52a4 | 1820 | if (!lttng_proc_dentry) { |
baf20995 MD |
1821 | printk(KERN_ERR "Error creating LTTng control file\n"); |
1822 | ret = -ENOMEM; | |
1823 | goto error; | |
1824 | } | |
ed8d02d6 | 1825 | lttng_stream_override_ring_buffer_fops(); |
2754583e | 1826 | return 0; |
ed8d02d6 | 1827 | |
baf20995 | 1828 | error: |
f771eda6 | 1829 | lttng_tp_mempool_destroy(); |
2754583e | 1830 | lttng_clock_unref(); |
baf20995 MD |
1831 | return ret; |
1832 | } | |
1833 | ||
e6e65fcd MD |
1834 | /* No __exit annotation because used by init error path too. */ |
1835 | void lttng_abi_exit(void) | |
baf20995 | 1836 | { |
f771eda6 | 1837 | lttng_tp_mempool_destroy(); |
2754583e | 1838 | lttng_clock_unref(); |
e6a17f26 MD |
1839 | if (lttng_proc_dentry) |
1840 | remove_proc_entry("lttng", NULL); | |
baf20995 | 1841 | } |