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