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