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