Commit | Line | Data |
---|---|---|
baf20995 | 1 | /* |
e8951e63 | 2 | * lttng-abi.c |
baf20995 | 3 | * |
e8951e63 | 4 | * LTTng ABI |
baf20995 | 5 | * |
886d51a3 MD |
6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | * | |
22 | * | |
baf20995 MD |
23 | * Mimic system calls for: |
24 | * - session creation, returns a file descriptor or failure. | |
ad1c05e1 MD |
25 | * - channel creation, returns a file descriptor or failure. |
26 | * - Operates on a session file descriptor | |
27 | * - Takes all channel options as parameters. | |
28 | * - stream get, returns a file descriptor or failure. | |
29 | * - Operates on a channel file descriptor. | |
30 | * - stream notifier get, returns a file descriptor or failure. | |
31 | * - Operates on a channel file descriptor. | |
32 | * - event creation, returns a file descriptor or failure. | |
33 | * - Operates on a channel file descriptor | |
34 | * - Takes an event name as parameter | |
35 | * - Takes an instrumentation source as parameter | |
36 | * - e.g. tracepoints, dynamic_probes... | |
37 | * - Takes instrumentation source specific arguments. | |
baf20995 MD |
38 | */ |
39 | ||
11b5a3c2 | 40 | #include <linux/module.h> |
e6a17f26 | 41 | #include <linux/proc_fs.h> |
11b5a3c2 MD |
42 | #include <linux/anon_inodes.h> |
43 | #include <linux/file.h> | |
44 | #include <linux/uaccess.h> | |
45 | #include <linux/slab.h> | |
b13f3ebe | 46 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
f3bc08c5 | 47 | #include "wrapper/ringbuffer/vfs.h" |
24cedcfe | 48 | #include "wrapper/poll.h" |
e8951e63 | 49 | #include "lttng-abi.h" |
a90917c3 MD |
50 | #include "lttng-events.h" |
51 | #include "lttng-tracer.h" | |
baf20995 MD |
52 | |
53 | /* | |
54 | * This is LTTng's own personal way to create a system call as an external | |
80996790 | 55 | * module. We use ioctl() on /proc/lttng. |
baf20995 MD |
56 | */ |
57 | ||
e6a17f26 | 58 | static struct proc_dir_entry *lttng_proc_dentry; |
ad1c05e1 MD |
59 | static const struct file_operations lttng_fops; |
60 | static const struct file_operations lttng_session_fops; | |
61 | static const struct file_operations lttng_channel_fops; | |
5dbbdb43 | 62 | static const struct file_operations lttng_metadata_fops; |
305d3e42 | 63 | static const struct file_operations lttng_event_fops; |
baf20995 | 64 | |
a33c9927 MD |
65 | /* |
66 | * Teardown management: opened file descriptors keep a refcount on the module, | |
67 | * so it can only exit when all file descriptors are closed. | |
68 | */ | |
69 | ||
5dbbdb43 MD |
70 | enum channel_type { |
71 | PER_CPU_CHANNEL, | |
5dbbdb43 MD |
72 | METADATA_CHANNEL, |
73 | }; | |
74 | ||
ad1c05e1 | 75 | static |
baf20995 MD |
76 | int lttng_abi_create_session(void) |
77 | { | |
a90917c3 | 78 | struct lttng_session *session; |
c0e31d2e | 79 | struct file *session_file; |
11b5a3c2 | 80 | int session_fd, ret; |
baf20995 | 81 | |
a90917c3 | 82 | session = lttng_session_create(); |
baf20995 MD |
83 | if (!session) |
84 | return -ENOMEM; | |
1c25284c | 85 | session_fd = get_unused_fd(); |
baf20995 MD |
86 | if (session_fd < 0) { |
87 | ret = session_fd; | |
88 | goto fd_error; | |
89 | } | |
c0e31d2e | 90 | session_file = anon_inode_getfile("[lttng_session]", |
ad1c05e1 | 91 | <tng_session_fops, |
baf20995 | 92 | session, O_RDWR); |
c0e31d2e MD |
93 | if (IS_ERR(session_file)) { |
94 | ret = PTR_ERR(session_file); | |
baf20995 MD |
95 | goto file_error; |
96 | } | |
c0e31d2e MD |
97 | session->file = session_file; |
98 | fd_install(session_fd, session_file); | |
baf20995 MD |
99 | return session_fd; |
100 | ||
101 | file_error: | |
102 | put_unused_fd(session_fd); | |
103 | fd_error: | |
a90917c3 | 104 | lttng_session_destroy(session); |
baf20995 MD |
105 | return ret; |
106 | } | |
107 | ||
271b6681 MD |
108 | static |
109 | int lttng_abi_tracepoint_list(void) | |
110 | { | |
111 | struct file *tracepoint_list_file; | |
112 | int file_fd, ret; | |
113 | ||
114 | file_fd = get_unused_fd(); | |
115 | if (file_fd < 0) { | |
116 | ret = file_fd; | |
117 | goto fd_error; | |
118 | } | |
30f18bf0 | 119 | |
271b6681 MD |
120 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
121 | <tng_tracepoint_list_fops, | |
122 | NULL, O_RDWR); | |
123 | if (IS_ERR(tracepoint_list_file)) { | |
124 | ret = PTR_ERR(tracepoint_list_file); | |
125 | goto file_error; | |
126 | } | |
30f18bf0 MD |
127 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
128 | if (ret < 0) | |
129 | goto open_error; | |
271b6681 | 130 | fd_install(file_fd, tracepoint_list_file); |
30f18bf0 MD |
131 | if (file_fd < 0) { |
132 | ret = file_fd; | |
133 | goto fd_error; | |
134 | } | |
271b6681 MD |
135 | return file_fd; |
136 | ||
30f18bf0 MD |
137 | open_error: |
138 | fput(tracepoint_list_file); | |
271b6681 MD |
139 | file_error: |
140 | put_unused_fd(file_fd); | |
141 | fd_error: | |
142 | return ret; | |
143 | } | |
144 | ||
80c16bcf MD |
145 | static |
146 | long lttng_abi_tracer_version(struct file *file, | |
147 | struct lttng_kernel_tracer_version __user *uversion_param) | |
148 | { | |
149 | struct lttng_kernel_tracer_version v; | |
150 | ||
c6c9e10f MD |
151 | v.major = LTTNG_MODULES_MAJOR_VERSION; |
152 | v.minor = LTTNG_MODULES_MINOR_VERSION; | |
153 | v.patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; | |
80c16bcf MD |
154 | |
155 | if (copy_to_user(uversion_param, &v, sizeof(v))) | |
156 | return -EFAULT; | |
157 | return 0; | |
158 | } | |
159 | ||
8070f5c0 MD |
160 | static |
161 | long lttng_abi_add_context(struct file *file, | |
162 | struct lttng_kernel_context __user *ucontext_param, | |
a90917c3 | 163 | struct lttng_ctx **ctx, struct lttng_session *session) |
8070f5c0 MD |
164 | { |
165 | struct lttng_kernel_context context_param; | |
166 | ||
167 | if (session->been_active) | |
168 | return -EPERM; | |
169 | ||
170 | if (copy_from_user(&context_param, ucontext_param, sizeof(context_param))) | |
171 | return -EFAULT; | |
172 | ||
173 | switch (context_param.ctx) { | |
12a313a5 | 174 | case LTTNG_KERNEL_CONTEXT_PID: |
8070f5c0 | 175 | return lttng_add_pid_to_ctx(ctx); |
a8ad3613 MD |
176 | case LTTNG_KERNEL_CONTEXT_PRIO: |
177 | return lttng_add_prio_to_ctx(ctx); | |
53f1f0ca MD |
178 | case LTTNG_KERNEL_CONTEXT_NICE: |
179 | return lttng_add_nice_to_ctx(ctx); | |
b64bc438 MD |
180 | case LTTNG_KERNEL_CONTEXT_VPID: |
181 | return lttng_add_vpid_to_ctx(ctx); | |
182 | case LTTNG_KERNEL_CONTEXT_TID: | |
183 | return lttng_add_tid_to_ctx(ctx); | |
184 | case LTTNG_KERNEL_CONTEXT_VTID: | |
185 | return lttng_add_vtid_to_ctx(ctx); | |
186 | case LTTNG_KERNEL_CONTEXT_PPID: | |
187 | return lttng_add_ppid_to_ctx(ctx); | |
188 | case LTTNG_KERNEL_CONTEXT_VPPID: | |
189 | return lttng_add_vppid_to_ctx(ctx); | |
12a313a5 | 190 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
f8695253 | 191 | context_param.u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
c24a0d71 MD |
192 | return lttng_add_perf_counter_to_ctx(context_param.u.perf_counter.type, |
193 | context_param.u.perf_counter.config, | |
194 | context_param.u.perf_counter.name, | |
195 | ctx); | |
a2563e83 MD |
196 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
197 | return lttng_add_procname_to_ctx(ctx); | |
8070f5c0 MD |
198 | default: |
199 | return -EINVAL; | |
200 | } | |
201 | } | |
202 | ||
ad1c05e1 MD |
203 | /** |
204 | * lttng_ioctl - lttng syscall through ioctl | |
205 | * | |
c0e31d2e | 206 | * @file: the file |
ad1c05e1 MD |
207 | * @cmd: the command |
208 | * @arg: command arg | |
209 | * | |
210 | * This ioctl implements lttng commands: | |
38d024ae | 211 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 212 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
213 | * LTTNG_KERNEL_TRACER_VERSION |
214 | * Returns the LTTng kernel tracer version | |
215 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
216 | * Returns a file descriptor listing available tracepoints | |
360f38ea MD |
217 | * LTTNG_KERNEL_WAIT_QUIESCENT |
218 | * Returns after all previously running probes have completed | |
ad1c05e1 MD |
219 | * |
220 | * The returned session will be deleted when its file descriptor is closed. | |
221 | */ | |
222 | static | |
c0e31d2e | 223 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
224 | { |
225 | switch (cmd) { | |
38d024ae | 226 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 227 | return lttng_abi_create_session(); |
80c16bcf MD |
228 | case LTTNG_KERNEL_TRACER_VERSION: |
229 | return lttng_abi_tracer_version(file, | |
230 | (struct lttng_kernel_tracer_version __user *) arg); | |
271b6681 MD |
231 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
232 | return lttng_abi_tracepoint_list(); | |
5f7f9078 MD |
233 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
234 | synchronize_trace(); | |
235 | return 0; | |
57105fc2 MD |
236 | case LTTNG_KERNEL_CALIBRATE: |
237 | { | |
3db41b2c MD |
238 | struct lttng_kernel_calibrate __user *ucalibrate = |
239 | (struct lttng_kernel_calibrate __user *) arg; | |
240 | struct lttng_kernel_calibrate calibrate; | |
57105fc2 MD |
241 | int ret; |
242 | ||
243 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) | |
244 | return -EFAULT; | |
245 | ret = lttng_calibrate(&calibrate); | |
246 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) | |
247 | return -EFAULT; | |
248 | return ret; | |
249 | } | |
ad1c05e1 MD |
250 | default: |
251 | return -ENOIOCTLCMD; | |
252 | } | |
253 | } | |
254 | ||
ad1c05e1 | 255 | static const struct file_operations lttng_fops = { |
a33c9927 | 256 | .owner = THIS_MODULE, |
ad1c05e1 MD |
257 | .unlocked_ioctl = lttng_ioctl, |
258 | #ifdef CONFIG_COMPAT | |
03037b98 | 259 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 260 | #endif |
11b5a3c2 | 261 | }; |
ad1c05e1 | 262 | |
5dbbdb43 MD |
263 | /* |
264 | * We tolerate no failure in this function (if one happens, we print a dmesg | |
265 | * error, but cannot return any error, because the channel information is | |
266 | * invariant. | |
267 | */ | |
268 | static | |
269 | void lttng_metadata_create_events(struct file *channel_file) | |
270 | { | |
a90917c3 | 271 | struct lttng_channel *channel = channel_file->private_data; |
e70a4758 | 272 | static struct lttng_kernel_event metadata_params = { |
ab2277d6 | 273 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
e70a4758 MD |
274 | .name = "lttng_metadata", |
275 | }; | |
a90917c3 | 276 | struct lttng_event *event; |
5dbbdb43 | 277 | |
5dbbdb43 MD |
278 | /* |
279 | * We tolerate no failure path after event creation. It will stay | |
280 | * invariant for the rest of the session. | |
281 | */ | |
a90917c3 | 282 | event = lttng_event_create(channel, &metadata_params, NULL, NULL); |
5dbbdb43 | 283 | if (!event) { |
271b6681 | 284 | goto create_error; |
5dbbdb43 MD |
285 | } |
286 | return; | |
287 | ||
85a9ca7f | 288 | create_error: |
5dbbdb43 MD |
289 | WARN_ON(1); |
290 | return; /* not allowed to return error */ | |
291 | } | |
292 | ||
293 | static | |
c0e31d2e | 294 | int lttng_abi_create_channel(struct file *session_file, |
38d024ae | 295 | struct lttng_kernel_channel __user *uchan_param, |
5dbbdb43 | 296 | enum channel_type channel_type) |
baf20995 | 297 | { |
a90917c3 | 298 | struct lttng_session *session = session_file->private_data; |
88dfd899 | 299 | const struct file_operations *fops = NULL; |
5dbbdb43 | 300 | const char *transport_name; |
a90917c3 | 301 | struct lttng_channel *chan; |
c0e31d2e | 302 | struct file *chan_file; |
38d024ae | 303 | struct lttng_kernel_channel chan_param; |
baf20995 | 304 | int chan_fd; |
ad1c05e1 | 305 | int ret = 0; |
baf20995 | 306 | |
653fe716 | 307 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
baf20995 | 308 | return -EFAULT; |
1c25284c | 309 | chan_fd = get_unused_fd(); |
baf20995 MD |
310 | if (chan_fd < 0) { |
311 | ret = chan_fd; | |
312 | goto fd_error; | |
313 | } | |
88dfd899 MD |
314 | switch (channel_type) { |
315 | case PER_CPU_CHANNEL: | |
316 | fops = <tng_channel_fops; | |
317 | break; | |
318 | case METADATA_CHANNEL: | |
319 | fops = <tng_metadata_fops; | |
320 | break; | |
321 | } | |
322 | ||
c0e31d2e | 323 | chan_file = anon_inode_getfile("[lttng_channel]", |
88dfd899 | 324 | fops, |
03037b98 | 325 | NULL, O_RDWR); |
c0e31d2e MD |
326 | if (IS_ERR(chan_file)) { |
327 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
328 | goto file_error; |
329 | } | |
5dbbdb43 MD |
330 | switch (channel_type) { |
331 | case PER_CPU_CHANNEL: | |
96ba7208 JD |
332 | if (chan_param.output == LTTNG_KERNEL_SPLICE) { |
333 | transport_name = chan_param.overwrite ? | |
334 | "relay-overwrite" : "relay-discard"; | |
335 | } else if (chan_param.output == LTTNG_KERNEL_MMAP) { | |
336 | transport_name = chan_param.overwrite ? | |
337 | "relay-overwrite-mmap" : "relay-discard-mmap"; | |
338 | } else { | |
339 | return -EINVAL; | |
340 | } | |
5dbbdb43 | 341 | break; |
5dbbdb43 | 342 | case METADATA_CHANNEL: |
96ba7208 JD |
343 | if (chan_param.output == LTTNG_KERNEL_SPLICE) |
344 | transport_name = "relay-metadata"; | |
345 | else if (chan_param.output == LTTNG_KERNEL_MMAP) | |
346 | transport_name = "relay-metadata-mmap"; | |
347 | else | |
348 | return -EINVAL; | |
5dbbdb43 MD |
349 | break; |
350 | default: | |
351 | transport_name = "<unknown>"; | |
352 | break; | |
353 | } | |
03037b98 MD |
354 | /* |
355 | * We tolerate no failure path after channel creation. It will stay | |
356 | * invariant for the rest of the session. | |
357 | */ | |
a90917c3 | 358 | chan = lttng_channel_create(session, transport_name, NULL, |
11b5a3c2 MD |
359 | chan_param.subbuf_size, |
360 | chan_param.num_subbuf, | |
361 | chan_param.switch_timer_interval, | |
362 | chan_param.read_timer_interval); | |
03037b98 | 363 | if (!chan) { |
f3d01b96 | 364 | ret = -EINVAL; |
03037b98 MD |
365 | goto chan_error; |
366 | } | |
11b5a3c2 | 367 | chan->file = chan_file; |
c0e31d2e MD |
368 | chan_file->private_data = chan; |
369 | fd_install(chan_fd, chan_file); | |
cd4bd11f | 370 | if (channel_type == METADATA_CHANNEL) { |
cd4bd11f | 371 | session->metadata = chan; |
4f1c3952 | 372 | lttng_metadata_create_events(chan_file); |
cd4bd11f | 373 | } |
5dbbdb43 | 374 | |
ad1c05e1 | 375 | /* The channel created holds a reference on the session */ |
b0caa15a | 376 | atomic_long_inc(&session_file->f_count); |
ad1c05e1 | 377 | |
baf20995 MD |
378 | return chan_fd; |
379 | ||
03037b98 | 380 | chan_error: |
c0e31d2e | 381 | fput(chan_file); |
baf20995 MD |
382 | file_error: |
383 | put_unused_fd(chan_fd); | |
384 | fd_error: | |
baf20995 MD |
385 | return ret; |
386 | } | |
387 | ||
388 | /** | |
ad1c05e1 | 389 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 390 | * |
c0e31d2e | 391 | * @file: the file |
baf20995 MD |
392 | * @cmd: the command |
393 | * @arg: command arg | |
394 | * | |
395 | * This ioctl implements lttng commands: | |
38d024ae | 396 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 397 | * Returns a LTTng channel file descriptor |
e64957da MD |
398 | * LTTNG_KERNEL_ENABLE |
399 | * Enables tracing for a session (weak enable) | |
400 | * LTTNG_KERNEL_DISABLE | |
401 | * Disables tracing for a session (strong disable) | |
8070f5c0 MD |
402 | * LTTNG_KERNEL_METADATA |
403 | * Returns a LTTng metadata file descriptor | |
ad1c05e1 MD |
404 | * |
405 | * The returned channel will be deleted when its file descriptor is closed. | |
406 | */ | |
407 | static | |
c0e31d2e | 408 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 409 | { |
a90917c3 | 410 | struct lttng_session *session = file->private_data; |
c0e31d2e | 411 | |
ad1c05e1 | 412 | switch (cmd) { |
38d024ae | 413 | case LTTNG_KERNEL_CHANNEL: |
5dbbdb43 | 414 | return lttng_abi_create_channel(file, |
38d024ae | 415 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 416 | PER_CPU_CHANNEL); |
38d024ae | 417 | case LTTNG_KERNEL_SESSION_START: |
e64957da | 418 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 419 | return lttng_session_enable(session); |
38d024ae | 420 | case LTTNG_KERNEL_SESSION_STOP: |
e64957da | 421 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 422 | return lttng_session_disable(session); |
38d024ae | 423 | case LTTNG_KERNEL_METADATA: |
5dbbdb43 | 424 | return lttng_abi_create_channel(file, |
38d024ae | 425 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 426 | METADATA_CHANNEL); |
ad1c05e1 MD |
427 | default: |
428 | return -ENOIOCTLCMD; | |
429 | } | |
430 | } | |
431 | ||
03037b98 MD |
432 | /* |
433 | * Called when the last file reference is dropped. | |
434 | * | |
435 | * Big fat note: channels and events are invariant for the whole session after | |
436 | * their creation. So this session destruction also destroys all channel and | |
437 | * event structures specific to this session (they are not destroyed when their | |
438 | * individual file is released). | |
439 | */ | |
ad1c05e1 | 440 | static |
03037b98 | 441 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 442 | { |
a90917c3 | 443 | struct lttng_session *session = file->private_data; |
c269fff4 MD |
444 | |
445 | if (session) | |
a90917c3 | 446 | lttng_session_destroy(session); |
11b5a3c2 | 447 | return 0; |
ad1c05e1 | 448 | } |
ad1c05e1 MD |
449 | |
450 | static const struct file_operations lttng_session_fops = { | |
a33c9927 | 451 | .owner = THIS_MODULE, |
03037b98 | 452 | .release = lttng_session_release, |
ad1c05e1 MD |
453 | .unlocked_ioctl = lttng_session_ioctl, |
454 | #ifdef CONFIG_COMPAT | |
03037b98 | 455 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 456 | #endif |
11b5a3c2 | 457 | }; |
ad1c05e1 MD |
458 | |
459 | static | |
c0e31d2e | 460 | int lttng_abi_open_stream(struct file *channel_file) |
ad1c05e1 | 461 | { |
a90917c3 | 462 | struct lttng_channel *channel = channel_file->private_data; |
ad1c05e1 MD |
463 | struct lib_ring_buffer *buf; |
464 | int stream_fd, ret; | |
11b5a3c2 | 465 | struct file *stream_file; |
ad1c05e1 | 466 | |
11b5a3c2 | 467 | buf = channel->ops->buffer_read_open(channel->chan); |
ad1c05e1 MD |
468 | if (!buf) |
469 | return -ENOENT; | |
470 | ||
1c25284c | 471 | stream_fd = get_unused_fd(); |
ad1c05e1 MD |
472 | if (stream_fd < 0) { |
473 | ret = stream_fd; | |
474 | goto fd_error; | |
475 | } | |
c0e31d2e | 476 | stream_file = anon_inode_getfile("[lttng_stream]", |
7f57c73c | 477 | &lib_ring_buffer_file_operations, |
ad1c05e1 | 478 | buf, O_RDWR); |
c0e31d2e MD |
479 | if (IS_ERR(stream_file)) { |
480 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
481 | goto file_error; |
482 | } | |
409453cb MD |
483 | /* |
484 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
485 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
486 | * file descriptor, so we set FMODE_PREAD here. | |
487 | */ | |
d7b6f197 | 488 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 489 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
490 | /* |
491 | * The stream holds a reference to the channel within the generic ring | |
492 | * buffer library, so no need to hold a refcount on the channel and | |
493 | * session files here. | |
494 | */ | |
ad1c05e1 MD |
495 | return stream_fd; |
496 | ||
497 | file_error: | |
498 | put_unused_fd(stream_fd); | |
499 | fd_error: | |
11b5a3c2 | 500 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
501 | return ret; |
502 | } | |
503 | ||
653fe716 | 504 | static |
c0e31d2e | 505 | int lttng_abi_create_event(struct file *channel_file, |
38d024ae | 506 | struct lttng_kernel_event __user *uevent_param) |
653fe716 | 507 | { |
a90917c3 MD |
508 | struct lttng_channel *channel = channel_file->private_data; |
509 | struct lttng_event *event; | |
38d024ae | 510 | struct lttng_kernel_event event_param; |
653fe716 | 511 | int event_fd, ret; |
11b5a3c2 | 512 | struct file *event_file; |
653fe716 MD |
513 | |
514 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) | |
515 | return -EFAULT; | |
f8695253 | 516 | event_param.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 517 | switch (event_param.instrumentation) { |
7371f44c | 518 | case LTTNG_KERNEL_KRETPROBE: |
f8695253 | 519 | event_param.u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
7371f44c | 520 | break; |
ab2277d6 | 521 | case LTTNG_KERNEL_KPROBE: |
f8695253 | 522 | event_param.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 523 | break; |
ab2277d6 | 524 | case LTTNG_KERNEL_FUNCTION: |
f8695253 | 525 | event_param.u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 MD |
526 | break; |
527 | default: | |
528 | break; | |
529 | } | |
1ec65de1 MD |
530 | switch (event_param.instrumentation) { |
531 | default: | |
532 | event_fd = get_unused_fd(); | |
533 | if (event_fd < 0) { | |
534 | ret = event_fd; | |
535 | goto fd_error; | |
536 | } | |
537 | event_file = anon_inode_getfile("[lttng_event]", | |
538 | <tng_event_fops, | |
539 | NULL, O_RDWR); | |
540 | if (IS_ERR(event_file)) { | |
541 | ret = PTR_ERR(event_file); | |
542 | goto file_error; | |
543 | } | |
544 | /* | |
545 | * We tolerate no failure path after event creation. It | |
546 | * will stay invariant for the rest of the session. | |
547 | */ | |
a90917c3 | 548 | event = lttng_event_create(channel, &event_param, NULL, NULL); |
1ec65de1 MD |
549 | if (!event) { |
550 | ret = -EINVAL; | |
551 | goto event_error; | |
552 | } | |
553 | event_file->private_data = event; | |
554 | fd_install(event_fd, event_file); | |
555 | /* The event holds a reference on the channel */ | |
556 | atomic_long_inc(&channel_file->f_count); | |
557 | break; | |
43880ee8 MD |
558 | case LTTNG_KERNEL_SYSCALL: |
559 | /* | |
560 | * Only all-syscall tracing supported for now. | |
561 | */ | |
562 | if (event_param.name[0] != '\0') | |
563 | return -EINVAL; | |
1ec65de1 MD |
564 | ret = lttng_syscalls_register(channel, NULL); |
565 | if (ret) | |
566 | goto fd_error; | |
567 | event_fd = 0; | |
568 | break; | |
03037b98 | 569 | } |
653fe716 MD |
570 | return event_fd; |
571 | ||
03037b98 | 572 | event_error: |
c0e31d2e | 573 | fput(event_file); |
653fe716 MD |
574 | file_error: |
575 | put_unused_fd(event_fd); | |
576 | fd_error: | |
653fe716 MD |
577 | return ret; |
578 | } | |
ad1c05e1 MD |
579 | |
580 | /** | |
581 | * lttng_channel_ioctl - lttng syscall through ioctl | |
582 | * | |
c0e31d2e | 583 | * @file: the file |
ad1c05e1 MD |
584 | * @cmd: the command |
585 | * @arg: command arg | |
586 | * | |
587 | * This ioctl implements lttng commands: | |
38d024ae | 588 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
589 | * Returns an event stream file descriptor or failure. |
590 | * (typically, one event stream records events from one CPU) | |
38d024ae | 591 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 592 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
593 | * LTTNG_KERNEL_CONTEXT |
594 | * Prepend a context field to each event in the channel | |
e64957da MD |
595 | * LTTNG_KERNEL_ENABLE |
596 | * Enable recording for events in this channel (weak enable) | |
597 | * LTTNG_KERNEL_DISABLE | |
598 | * Disable recording for events in this channel (strong disable) | |
baf20995 | 599 | * |
baf20995 MD |
600 | * Channel and event file descriptors also hold a reference on the session. |
601 | */ | |
ad1c05e1 | 602 | static |
c0e31d2e | 603 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 604 | { |
a90917c3 | 605 | struct lttng_channel *channel = file->private_data; |
8070f5c0 | 606 | |
baf20995 | 607 | switch (cmd) { |
38d024ae | 608 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 609 | return lttng_abi_open_stream(file); |
38d024ae MD |
610 | case LTTNG_KERNEL_EVENT: |
611 | return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg); | |
8070f5c0 MD |
612 | case LTTNG_KERNEL_CONTEXT: |
613 | return lttng_abi_add_context(file, | |
614 | (struct lttng_kernel_context __user *) arg, | |
615 | &channel->ctx, channel->session); | |
e64957da | 616 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 617 | return lttng_channel_enable(channel); |
e64957da | 618 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 619 | return lttng_channel_disable(channel); |
baf20995 MD |
620 | default: |
621 | return -ENOIOCTLCMD; | |
622 | } | |
623 | } | |
624 | ||
5dbbdb43 MD |
625 | /** |
626 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
627 | * | |
628 | * @file: the file | |
629 | * @cmd: the command | |
630 | * @arg: command arg | |
631 | * | |
632 | * This ioctl implements lttng commands: | |
38d024ae | 633 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
634 | * Returns an event stream file descriptor or failure. |
635 | * | |
636 | * Channel and event file descriptors also hold a reference on the session. | |
637 | */ | |
638 | static | |
639 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
640 | { | |
641 | switch (cmd) { | |
38d024ae | 642 | case LTTNG_KERNEL_STREAM: |
5dbbdb43 MD |
643 | return lttng_abi_open_stream(file); |
644 | default: | |
645 | return -ENOIOCTLCMD; | |
646 | } | |
647 | } | |
648 | ||
653fe716 MD |
649 | /** |
650 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
651 | * | |
c0e31d2e | 652 | * @file: the file |
653fe716 MD |
653 | * @wait: poll table |
654 | */ | |
c0e31d2e | 655 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 656 | { |
a90917c3 | 657 | struct lttng_channel *channel = file->private_data; |
653fe716 MD |
658 | unsigned int mask = 0; |
659 | ||
c0e31d2e | 660 | if (file->f_mode & FMODE_READ) { |
a33e44a6 | 661 | poll_wait_set_exclusive(wait); |
24cedcfe MD |
662 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
663 | wait); | |
653fe716 | 664 | |
254ec7bc MD |
665 | if (channel->ops->is_disabled(channel->chan)) |
666 | return POLLERR; | |
24cedcfe | 667 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 | 668 | return POLLHUP; |
f71ecafa | 669 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
653fe716 | 670 | return POLLIN | POLLRDNORM; |
f71ecafa | 671 | return 0; |
653fe716 MD |
672 | } |
673 | return mask; | |
674 | ||
675 | } | |
676 | ||
0a84a57f MD |
677 | static |
678 | int lttng_channel_release(struct inode *inode, struct file *file) | |
679 | { | |
a90917c3 | 680 | struct lttng_channel *channel = file->private_data; |
c269fff4 MD |
681 | |
682 | if (channel) | |
683 | fput(channel->session->file); | |
0a84a57f MD |
684 | return 0; |
685 | } | |
686 | ||
ad1c05e1 | 687 | static const struct file_operations lttng_channel_fops = { |
a33c9927 | 688 | .owner = THIS_MODULE, |
03037b98 | 689 | .release = lttng_channel_release, |
653fe716 | 690 | .poll = lttng_channel_poll, |
ad1c05e1 | 691 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 692 | #ifdef CONFIG_COMPAT |
03037b98 | 693 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 694 | #endif |
11b5a3c2 | 695 | }; |
baf20995 | 696 | |
5dbbdb43 | 697 | static const struct file_operations lttng_metadata_fops = { |
a33c9927 | 698 | .owner = THIS_MODULE, |
5dbbdb43 MD |
699 | .release = lttng_channel_release, |
700 | .unlocked_ioctl = lttng_metadata_ioctl, | |
701 | #ifdef CONFIG_COMPAT | |
702 | .compat_ioctl = lttng_metadata_ioctl, | |
703 | #endif | |
704 | }; | |
705 | ||
8070f5c0 MD |
706 | /** |
707 | * lttng_event_ioctl - lttng syscall through ioctl | |
708 | * | |
709 | * @file: the file | |
710 | * @cmd: the command | |
711 | * @arg: command arg | |
712 | * | |
713 | * This ioctl implements lttng commands: | |
8070f5c0 MD |
714 | * LTTNG_KERNEL_CONTEXT |
715 | * Prepend a context field to each record of this event | |
e64957da MD |
716 | * LTTNG_KERNEL_ENABLE |
717 | * Enable recording for this event (weak enable) | |
718 | * LTTNG_KERNEL_DISABLE | |
719 | * Disable recording for this event (strong disable) | |
8070f5c0 MD |
720 | */ |
721 | static | |
722 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
723 | { | |
a90917c3 | 724 | struct lttng_event *event = file->private_data; |
8070f5c0 MD |
725 | |
726 | switch (cmd) { | |
727 | case LTTNG_KERNEL_CONTEXT: | |
728 | return lttng_abi_add_context(file, | |
729 | (struct lttng_kernel_context __user *) arg, | |
730 | &event->ctx, event->chan->session); | |
e64957da | 731 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 732 | return lttng_event_enable(event); |
e64957da | 733 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 734 | return lttng_event_disable(event); |
8070f5c0 MD |
735 | default: |
736 | return -ENOIOCTLCMD; | |
737 | } | |
738 | } | |
739 | ||
0a84a57f MD |
740 | static |
741 | int lttng_event_release(struct inode *inode, struct file *file) | |
742 | { | |
a90917c3 | 743 | struct lttng_event *event = file->private_data; |
c269fff4 | 744 | |
aa7c23a9 | 745 | if (event) |
c269fff4 | 746 | fput(event->chan->file); |
0a84a57f MD |
747 | return 0; |
748 | } | |
749 | ||
3b923e5b | 750 | /* TODO: filter control ioctl */ |
0a84a57f | 751 | static const struct file_operations lttng_event_fops = { |
a33c9927 | 752 | .owner = THIS_MODULE, |
0a84a57f | 753 | .release = lttng_event_release, |
8070f5c0 MD |
754 | .unlocked_ioctl = lttng_event_ioctl, |
755 | #ifdef CONFIG_COMPAT | |
756 | .compat_ioctl = lttng_event_ioctl, | |
757 | #endif | |
11b5a3c2 | 758 | }; |
0a84a57f | 759 | |
80996790 | 760 | int __init lttng_abi_init(void) |
baf20995 MD |
761 | { |
762 | int ret = 0; | |
763 | ||
6d2a620c | 764 | wrapper_vmalloc_sync_all(); |
d29348f7 | 765 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
e6a17f26 MD |
766 | <tng_fops, NULL); |
767 | ||
255e52a4 | 768 | if (!lttng_proc_dentry) { |
baf20995 MD |
769 | printk(KERN_ERR "Error creating LTTng control file\n"); |
770 | ret = -ENOMEM; | |
771 | goto error; | |
772 | } | |
773 | error: | |
774 | return ret; | |
775 | } | |
776 | ||
80996790 | 777 | void __exit lttng_abi_exit(void) |
baf20995 | 778 | { |
e6a17f26 MD |
779 | if (lttng_proc_dentry) |
780 | remove_proc_entry("lttng", NULL); | |
baf20995 | 781 | } |