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