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