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