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