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