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