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); | |
975da2c0 JD |
198 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
199 | return lttng_add_hostname_to_ctx(ctx); | |
8070f5c0 MD |
200 | default: |
201 | return -EINVAL; | |
202 | } | |
203 | } | |
204 | ||
ad1c05e1 MD |
205 | /** |
206 | * lttng_ioctl - lttng syscall through ioctl | |
207 | * | |
c0e31d2e | 208 | * @file: the file |
ad1c05e1 MD |
209 | * @cmd: the command |
210 | * @arg: command arg | |
211 | * | |
212 | * This ioctl implements lttng commands: | |
38d024ae | 213 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 214 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
215 | * LTTNG_KERNEL_TRACER_VERSION |
216 | * Returns the LTTng kernel tracer version | |
217 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
218 | * Returns a file descriptor listing available tracepoints | |
360f38ea MD |
219 | * LTTNG_KERNEL_WAIT_QUIESCENT |
220 | * Returns after all previously running probes have completed | |
ad1c05e1 MD |
221 | * |
222 | * The returned session will be deleted when its file descriptor is closed. | |
223 | */ | |
224 | static | |
c0e31d2e | 225 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
226 | { |
227 | switch (cmd) { | |
38d024ae | 228 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 229 | return lttng_abi_create_session(); |
80c16bcf MD |
230 | case LTTNG_KERNEL_TRACER_VERSION: |
231 | return lttng_abi_tracer_version(file, | |
232 | (struct lttng_kernel_tracer_version __user *) arg); | |
271b6681 MD |
233 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
234 | return lttng_abi_tracepoint_list(); | |
5f7f9078 MD |
235 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
236 | synchronize_trace(); | |
237 | return 0; | |
57105fc2 MD |
238 | case LTTNG_KERNEL_CALIBRATE: |
239 | { | |
3db41b2c MD |
240 | struct lttng_kernel_calibrate __user *ucalibrate = |
241 | (struct lttng_kernel_calibrate __user *) arg; | |
242 | struct lttng_kernel_calibrate calibrate; | |
57105fc2 MD |
243 | int ret; |
244 | ||
245 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) | |
246 | return -EFAULT; | |
247 | ret = lttng_calibrate(&calibrate); | |
248 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) | |
249 | return -EFAULT; | |
250 | return ret; | |
251 | } | |
ad1c05e1 MD |
252 | default: |
253 | return -ENOIOCTLCMD; | |
254 | } | |
255 | } | |
256 | ||
ad1c05e1 | 257 | static const struct file_operations lttng_fops = { |
a33c9927 | 258 | .owner = THIS_MODULE, |
ad1c05e1 MD |
259 | .unlocked_ioctl = lttng_ioctl, |
260 | #ifdef CONFIG_COMPAT | |
03037b98 | 261 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 262 | #endif |
11b5a3c2 | 263 | }; |
ad1c05e1 | 264 | |
5dbbdb43 MD |
265 | /* |
266 | * We tolerate no failure in this function (if one happens, we print a dmesg | |
267 | * error, but cannot return any error, because the channel information is | |
268 | * invariant. | |
269 | */ | |
270 | static | |
271 | void lttng_metadata_create_events(struct file *channel_file) | |
272 | { | |
a90917c3 | 273 | struct lttng_channel *channel = channel_file->private_data; |
e70a4758 | 274 | static struct lttng_kernel_event metadata_params = { |
ab2277d6 | 275 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
e70a4758 MD |
276 | .name = "lttng_metadata", |
277 | }; | |
a90917c3 | 278 | struct lttng_event *event; |
5dbbdb43 | 279 | |
5dbbdb43 MD |
280 | /* |
281 | * We tolerate no failure path after event creation. It will stay | |
282 | * invariant for the rest of the session. | |
283 | */ | |
a90917c3 | 284 | event = lttng_event_create(channel, &metadata_params, NULL, NULL); |
5dbbdb43 | 285 | if (!event) { |
271b6681 | 286 | goto create_error; |
5dbbdb43 MD |
287 | } |
288 | return; | |
289 | ||
85a9ca7f | 290 | create_error: |
5dbbdb43 MD |
291 | WARN_ON(1); |
292 | return; /* not allowed to return error */ | |
293 | } | |
294 | ||
295 | static | |
c0e31d2e | 296 | int lttng_abi_create_channel(struct file *session_file, |
38d024ae | 297 | struct lttng_kernel_channel __user *uchan_param, |
5dbbdb43 | 298 | enum channel_type channel_type) |
baf20995 | 299 | { |
a90917c3 | 300 | struct lttng_session *session = session_file->private_data; |
88dfd899 | 301 | const struct file_operations *fops = NULL; |
5dbbdb43 | 302 | const char *transport_name; |
a90917c3 | 303 | struct lttng_channel *chan; |
c0e31d2e | 304 | struct file *chan_file; |
38d024ae | 305 | struct lttng_kernel_channel chan_param; |
baf20995 | 306 | int chan_fd; |
ad1c05e1 | 307 | int ret = 0; |
baf20995 | 308 | |
653fe716 | 309 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
baf20995 | 310 | return -EFAULT; |
1c25284c | 311 | chan_fd = get_unused_fd(); |
baf20995 MD |
312 | if (chan_fd < 0) { |
313 | ret = chan_fd; | |
314 | goto fd_error; | |
315 | } | |
88dfd899 MD |
316 | switch (channel_type) { |
317 | case PER_CPU_CHANNEL: | |
318 | fops = <tng_channel_fops; | |
319 | break; | |
320 | case METADATA_CHANNEL: | |
321 | fops = <tng_metadata_fops; | |
322 | break; | |
323 | } | |
324 | ||
c0e31d2e | 325 | chan_file = anon_inode_getfile("[lttng_channel]", |
88dfd899 | 326 | fops, |
03037b98 | 327 | NULL, O_RDWR); |
c0e31d2e MD |
328 | if (IS_ERR(chan_file)) { |
329 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
330 | goto file_error; |
331 | } | |
5dbbdb43 MD |
332 | switch (channel_type) { |
333 | case PER_CPU_CHANNEL: | |
96ba7208 JD |
334 | if (chan_param.output == LTTNG_KERNEL_SPLICE) { |
335 | transport_name = chan_param.overwrite ? | |
336 | "relay-overwrite" : "relay-discard"; | |
337 | } else if (chan_param.output == LTTNG_KERNEL_MMAP) { | |
338 | transport_name = chan_param.overwrite ? | |
339 | "relay-overwrite-mmap" : "relay-discard-mmap"; | |
340 | } else { | |
341 | return -EINVAL; | |
342 | } | |
5dbbdb43 | 343 | break; |
5dbbdb43 | 344 | case METADATA_CHANNEL: |
96ba7208 JD |
345 | if (chan_param.output == LTTNG_KERNEL_SPLICE) |
346 | transport_name = "relay-metadata"; | |
347 | else if (chan_param.output == LTTNG_KERNEL_MMAP) | |
348 | transport_name = "relay-metadata-mmap"; | |
349 | else | |
350 | return -EINVAL; | |
5dbbdb43 MD |
351 | break; |
352 | default: | |
353 | transport_name = "<unknown>"; | |
354 | break; | |
355 | } | |
03037b98 MD |
356 | /* |
357 | * We tolerate no failure path after channel creation. It will stay | |
358 | * invariant for the rest of the session. | |
359 | */ | |
a90917c3 | 360 | chan = lttng_channel_create(session, transport_name, NULL, |
11b5a3c2 MD |
361 | chan_param.subbuf_size, |
362 | chan_param.num_subbuf, | |
363 | chan_param.switch_timer_interval, | |
364 | chan_param.read_timer_interval); | |
03037b98 | 365 | if (!chan) { |
f3d01b96 | 366 | ret = -EINVAL; |
03037b98 MD |
367 | goto chan_error; |
368 | } | |
11b5a3c2 | 369 | chan->file = chan_file; |
c0e31d2e MD |
370 | chan_file->private_data = chan; |
371 | fd_install(chan_fd, chan_file); | |
cd4bd11f | 372 | if (channel_type == METADATA_CHANNEL) { |
cd4bd11f | 373 | session->metadata = chan; |
4f1c3952 | 374 | lttng_metadata_create_events(chan_file); |
cd4bd11f | 375 | } |
5dbbdb43 | 376 | |
ad1c05e1 | 377 | /* The channel created holds a reference on the session */ |
b0caa15a | 378 | atomic_long_inc(&session_file->f_count); |
ad1c05e1 | 379 | |
baf20995 MD |
380 | return chan_fd; |
381 | ||
03037b98 | 382 | chan_error: |
c0e31d2e | 383 | fput(chan_file); |
baf20995 MD |
384 | file_error: |
385 | put_unused_fd(chan_fd); | |
386 | fd_error: | |
baf20995 MD |
387 | return ret; |
388 | } | |
389 | ||
390 | /** | |
ad1c05e1 | 391 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 392 | * |
c0e31d2e | 393 | * @file: the file |
baf20995 MD |
394 | * @cmd: the command |
395 | * @arg: command arg | |
396 | * | |
397 | * This ioctl implements lttng commands: | |
38d024ae | 398 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 399 | * Returns a LTTng channel file descriptor |
e64957da MD |
400 | * LTTNG_KERNEL_ENABLE |
401 | * Enables tracing for a session (weak enable) | |
402 | * LTTNG_KERNEL_DISABLE | |
403 | * Disables tracing for a session (strong disable) | |
8070f5c0 MD |
404 | * LTTNG_KERNEL_METADATA |
405 | * Returns a LTTng metadata file descriptor | |
ad1c05e1 MD |
406 | * |
407 | * The returned channel will be deleted when its file descriptor is closed. | |
408 | */ | |
409 | static | |
c0e31d2e | 410 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 411 | { |
a90917c3 | 412 | struct lttng_session *session = file->private_data; |
c0e31d2e | 413 | |
ad1c05e1 | 414 | switch (cmd) { |
38d024ae | 415 | case LTTNG_KERNEL_CHANNEL: |
5dbbdb43 | 416 | return lttng_abi_create_channel(file, |
38d024ae | 417 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 418 | PER_CPU_CHANNEL); |
38d024ae | 419 | case LTTNG_KERNEL_SESSION_START: |
e64957da | 420 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 421 | return lttng_session_enable(session); |
38d024ae | 422 | case LTTNG_KERNEL_SESSION_STOP: |
e64957da | 423 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 424 | return lttng_session_disable(session); |
38d024ae | 425 | case LTTNG_KERNEL_METADATA: |
5dbbdb43 | 426 | return lttng_abi_create_channel(file, |
38d024ae | 427 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 428 | METADATA_CHANNEL); |
ad1c05e1 MD |
429 | default: |
430 | return -ENOIOCTLCMD; | |
431 | } | |
432 | } | |
433 | ||
03037b98 MD |
434 | /* |
435 | * Called when the last file reference is dropped. | |
436 | * | |
437 | * Big fat note: channels and events are invariant for the whole session after | |
438 | * their creation. So this session destruction also destroys all channel and | |
439 | * event structures specific to this session (they are not destroyed when their | |
440 | * individual file is released). | |
441 | */ | |
ad1c05e1 | 442 | static |
03037b98 | 443 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 444 | { |
a90917c3 | 445 | struct lttng_session *session = file->private_data; |
c269fff4 MD |
446 | |
447 | if (session) | |
a90917c3 | 448 | lttng_session_destroy(session); |
11b5a3c2 | 449 | return 0; |
ad1c05e1 | 450 | } |
ad1c05e1 MD |
451 | |
452 | static const struct file_operations lttng_session_fops = { | |
a33c9927 | 453 | .owner = THIS_MODULE, |
03037b98 | 454 | .release = lttng_session_release, |
ad1c05e1 MD |
455 | .unlocked_ioctl = lttng_session_ioctl, |
456 | #ifdef CONFIG_COMPAT | |
03037b98 | 457 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 458 | #endif |
11b5a3c2 | 459 | }; |
ad1c05e1 MD |
460 | |
461 | static | |
c0e31d2e | 462 | int lttng_abi_open_stream(struct file *channel_file) |
ad1c05e1 | 463 | { |
a90917c3 | 464 | struct lttng_channel *channel = channel_file->private_data; |
ad1c05e1 MD |
465 | struct lib_ring_buffer *buf; |
466 | int stream_fd, ret; | |
11b5a3c2 | 467 | struct file *stream_file; |
ad1c05e1 | 468 | |
11b5a3c2 | 469 | buf = channel->ops->buffer_read_open(channel->chan); |
ad1c05e1 MD |
470 | if (!buf) |
471 | return -ENOENT; | |
472 | ||
1c25284c | 473 | stream_fd = get_unused_fd(); |
ad1c05e1 MD |
474 | if (stream_fd < 0) { |
475 | ret = stream_fd; | |
476 | goto fd_error; | |
477 | } | |
c0e31d2e | 478 | stream_file = anon_inode_getfile("[lttng_stream]", |
7f57c73c | 479 | &lib_ring_buffer_file_operations, |
ad1c05e1 | 480 | buf, O_RDWR); |
c0e31d2e MD |
481 | if (IS_ERR(stream_file)) { |
482 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
483 | goto file_error; |
484 | } | |
409453cb MD |
485 | /* |
486 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
487 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
488 | * file descriptor, so we set FMODE_PREAD here. | |
489 | */ | |
d7b6f197 | 490 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 491 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
492 | /* |
493 | * The stream holds a reference to the channel within the generic ring | |
494 | * buffer library, so no need to hold a refcount on the channel and | |
495 | * session files here. | |
496 | */ | |
ad1c05e1 MD |
497 | return stream_fd; |
498 | ||
499 | file_error: | |
500 | put_unused_fd(stream_fd); | |
501 | fd_error: | |
11b5a3c2 | 502 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
503 | return ret; |
504 | } | |
505 | ||
653fe716 | 506 | static |
c0e31d2e | 507 | int lttng_abi_create_event(struct file *channel_file, |
38d024ae | 508 | struct lttng_kernel_event __user *uevent_param) |
653fe716 | 509 | { |
a90917c3 MD |
510 | struct lttng_channel *channel = channel_file->private_data; |
511 | struct lttng_event *event; | |
38d024ae | 512 | struct lttng_kernel_event event_param; |
653fe716 | 513 | int event_fd, ret; |
11b5a3c2 | 514 | struct file *event_file; |
653fe716 MD |
515 | |
516 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) | |
517 | return -EFAULT; | |
f8695253 | 518 | event_param.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 519 | switch (event_param.instrumentation) { |
7371f44c | 520 | case LTTNG_KERNEL_KRETPROBE: |
f8695253 | 521 | event_param.u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
7371f44c | 522 | break; |
ab2277d6 | 523 | case LTTNG_KERNEL_KPROBE: |
f8695253 | 524 | event_param.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 525 | break; |
ab2277d6 | 526 | case LTTNG_KERNEL_FUNCTION: |
f8695253 | 527 | event_param.u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 MD |
528 | break; |
529 | default: | |
530 | break; | |
531 | } | |
1ec65de1 MD |
532 | switch (event_param.instrumentation) { |
533 | default: | |
534 | event_fd = get_unused_fd(); | |
535 | if (event_fd < 0) { | |
536 | ret = event_fd; | |
537 | goto fd_error; | |
538 | } | |
539 | event_file = anon_inode_getfile("[lttng_event]", | |
540 | <tng_event_fops, | |
541 | NULL, O_RDWR); | |
542 | if (IS_ERR(event_file)) { | |
543 | ret = PTR_ERR(event_file); | |
544 | goto file_error; | |
545 | } | |
546 | /* | |
547 | * We tolerate no failure path after event creation. It | |
548 | * will stay invariant for the rest of the session. | |
549 | */ | |
a90917c3 | 550 | event = lttng_event_create(channel, &event_param, NULL, NULL); |
1ec65de1 MD |
551 | if (!event) { |
552 | ret = -EINVAL; | |
553 | goto event_error; | |
554 | } | |
555 | event_file->private_data = event; | |
556 | fd_install(event_fd, event_file); | |
557 | /* The event holds a reference on the channel */ | |
558 | atomic_long_inc(&channel_file->f_count); | |
559 | break; | |
43880ee8 MD |
560 | case LTTNG_KERNEL_SYSCALL: |
561 | /* | |
562 | * Only all-syscall tracing supported for now. | |
563 | */ | |
564 | if (event_param.name[0] != '\0') | |
565 | return -EINVAL; | |
1ec65de1 MD |
566 | ret = lttng_syscalls_register(channel, NULL); |
567 | if (ret) | |
568 | goto fd_error; | |
569 | event_fd = 0; | |
570 | break; | |
03037b98 | 571 | } |
653fe716 MD |
572 | return event_fd; |
573 | ||
03037b98 | 574 | event_error: |
c0e31d2e | 575 | fput(event_file); |
653fe716 MD |
576 | file_error: |
577 | put_unused_fd(event_fd); | |
578 | fd_error: | |
653fe716 MD |
579 | return ret; |
580 | } | |
ad1c05e1 MD |
581 | |
582 | /** | |
583 | * lttng_channel_ioctl - lttng syscall through ioctl | |
584 | * | |
c0e31d2e | 585 | * @file: the file |
ad1c05e1 MD |
586 | * @cmd: the command |
587 | * @arg: command arg | |
588 | * | |
589 | * This ioctl implements lttng commands: | |
38d024ae | 590 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
591 | * Returns an event stream file descriptor or failure. |
592 | * (typically, one event stream records events from one CPU) | |
38d024ae | 593 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 594 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
595 | * LTTNG_KERNEL_CONTEXT |
596 | * Prepend a context field to each event in the channel | |
e64957da MD |
597 | * LTTNG_KERNEL_ENABLE |
598 | * Enable recording for events in this channel (weak enable) | |
599 | * LTTNG_KERNEL_DISABLE | |
600 | * Disable recording for events in this channel (strong disable) | |
baf20995 | 601 | * |
baf20995 MD |
602 | * Channel and event file descriptors also hold a reference on the session. |
603 | */ | |
ad1c05e1 | 604 | static |
c0e31d2e | 605 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 606 | { |
a90917c3 | 607 | struct lttng_channel *channel = file->private_data; |
8070f5c0 | 608 | |
baf20995 | 609 | switch (cmd) { |
38d024ae | 610 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 611 | return lttng_abi_open_stream(file); |
38d024ae MD |
612 | case LTTNG_KERNEL_EVENT: |
613 | return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg); | |
8070f5c0 MD |
614 | case LTTNG_KERNEL_CONTEXT: |
615 | return lttng_abi_add_context(file, | |
616 | (struct lttng_kernel_context __user *) arg, | |
617 | &channel->ctx, channel->session); | |
e64957da | 618 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 619 | return lttng_channel_enable(channel); |
e64957da | 620 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 621 | return lttng_channel_disable(channel); |
baf20995 MD |
622 | default: |
623 | return -ENOIOCTLCMD; | |
624 | } | |
625 | } | |
626 | ||
5dbbdb43 MD |
627 | /** |
628 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
629 | * | |
630 | * @file: the file | |
631 | * @cmd: the command | |
632 | * @arg: command arg | |
633 | * | |
634 | * This ioctl implements lttng commands: | |
38d024ae | 635 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
636 | * Returns an event stream file descriptor or failure. |
637 | * | |
638 | * Channel and event file descriptors also hold a reference on the session. | |
639 | */ | |
640 | static | |
641 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
642 | { | |
643 | switch (cmd) { | |
38d024ae | 644 | case LTTNG_KERNEL_STREAM: |
5dbbdb43 MD |
645 | return lttng_abi_open_stream(file); |
646 | default: | |
647 | return -ENOIOCTLCMD; | |
648 | } | |
649 | } | |
650 | ||
653fe716 MD |
651 | /** |
652 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
653 | * | |
c0e31d2e | 654 | * @file: the file |
653fe716 MD |
655 | * @wait: poll table |
656 | */ | |
c0e31d2e | 657 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 658 | { |
a90917c3 | 659 | struct lttng_channel *channel = file->private_data; |
653fe716 MD |
660 | unsigned int mask = 0; |
661 | ||
c0e31d2e | 662 | if (file->f_mode & FMODE_READ) { |
a33e44a6 | 663 | poll_wait_set_exclusive(wait); |
24cedcfe MD |
664 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
665 | wait); | |
653fe716 | 666 | |
254ec7bc MD |
667 | if (channel->ops->is_disabled(channel->chan)) |
668 | return POLLERR; | |
24cedcfe | 669 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 | 670 | return POLLHUP; |
f71ecafa | 671 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
653fe716 | 672 | return POLLIN | POLLRDNORM; |
f71ecafa | 673 | return 0; |
653fe716 MD |
674 | } |
675 | return mask; | |
676 | ||
677 | } | |
678 | ||
0a84a57f MD |
679 | static |
680 | int lttng_channel_release(struct inode *inode, struct file *file) | |
681 | { | |
a90917c3 | 682 | struct lttng_channel *channel = file->private_data; |
c269fff4 MD |
683 | |
684 | if (channel) | |
685 | fput(channel->session->file); | |
0a84a57f MD |
686 | return 0; |
687 | } | |
688 | ||
ad1c05e1 | 689 | static const struct file_operations lttng_channel_fops = { |
a33c9927 | 690 | .owner = THIS_MODULE, |
03037b98 | 691 | .release = lttng_channel_release, |
653fe716 | 692 | .poll = lttng_channel_poll, |
ad1c05e1 | 693 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 694 | #ifdef CONFIG_COMPAT |
03037b98 | 695 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 696 | #endif |
11b5a3c2 | 697 | }; |
baf20995 | 698 | |
5dbbdb43 | 699 | static const struct file_operations lttng_metadata_fops = { |
a33c9927 | 700 | .owner = THIS_MODULE, |
5dbbdb43 MD |
701 | .release = lttng_channel_release, |
702 | .unlocked_ioctl = lttng_metadata_ioctl, | |
703 | #ifdef CONFIG_COMPAT | |
704 | .compat_ioctl = lttng_metadata_ioctl, | |
705 | #endif | |
706 | }; | |
707 | ||
8070f5c0 MD |
708 | /** |
709 | * lttng_event_ioctl - lttng syscall through ioctl | |
710 | * | |
711 | * @file: the file | |
712 | * @cmd: the command | |
713 | * @arg: command arg | |
714 | * | |
715 | * This ioctl implements lttng commands: | |
8070f5c0 MD |
716 | * LTTNG_KERNEL_CONTEXT |
717 | * Prepend a context field to each record of this event | |
e64957da MD |
718 | * LTTNG_KERNEL_ENABLE |
719 | * Enable recording for this event (weak enable) | |
720 | * LTTNG_KERNEL_DISABLE | |
721 | * Disable recording for this event (strong disable) | |
8070f5c0 MD |
722 | */ |
723 | static | |
724 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
725 | { | |
a90917c3 | 726 | struct lttng_event *event = file->private_data; |
8070f5c0 MD |
727 | |
728 | switch (cmd) { | |
729 | case LTTNG_KERNEL_CONTEXT: | |
730 | return lttng_abi_add_context(file, | |
731 | (struct lttng_kernel_context __user *) arg, | |
732 | &event->ctx, event->chan->session); | |
e64957da | 733 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 734 | return lttng_event_enable(event); |
e64957da | 735 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 736 | return lttng_event_disable(event); |
8070f5c0 MD |
737 | default: |
738 | return -ENOIOCTLCMD; | |
739 | } | |
740 | } | |
741 | ||
0a84a57f MD |
742 | static |
743 | int lttng_event_release(struct inode *inode, struct file *file) | |
744 | { | |
a90917c3 | 745 | struct lttng_event *event = file->private_data; |
c269fff4 | 746 | |
aa7c23a9 | 747 | if (event) |
c269fff4 | 748 | fput(event->chan->file); |
0a84a57f MD |
749 | return 0; |
750 | } | |
751 | ||
3b923e5b | 752 | /* TODO: filter control ioctl */ |
0a84a57f | 753 | static const struct file_operations lttng_event_fops = { |
a33c9927 | 754 | .owner = THIS_MODULE, |
0a84a57f | 755 | .release = lttng_event_release, |
8070f5c0 MD |
756 | .unlocked_ioctl = lttng_event_ioctl, |
757 | #ifdef CONFIG_COMPAT | |
758 | .compat_ioctl = lttng_event_ioctl, | |
759 | #endif | |
11b5a3c2 | 760 | }; |
0a84a57f | 761 | |
80996790 | 762 | int __init lttng_abi_init(void) |
baf20995 MD |
763 | { |
764 | int ret = 0; | |
765 | ||
6d2a620c | 766 | wrapper_vmalloc_sync_all(); |
d29348f7 | 767 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
e6a17f26 MD |
768 | <tng_fops, NULL); |
769 | ||
255e52a4 | 770 | if (!lttng_proc_dentry) { |
baf20995 MD |
771 | printk(KERN_ERR "Error creating LTTng control file\n"); |
772 | ret = -ENOMEM; | |
773 | goto error; | |
774 | } | |
775 | error: | |
776 | return ret; | |
777 | } | |
778 | ||
80996790 | 779 | void __exit lttng_abi_exit(void) |
baf20995 | 780 | { |
e6a17f26 MD |
781 | if (lttng_proc_dentry) |
782 | remove_proc_entry("lttng", NULL); | |
baf20995 | 783 | } |