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); | |
12a313a5 | 162 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
c24a0d71 MD |
163 | context_param.u.perf_counter.name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
164 | return lttng_add_perf_counter_to_ctx(context_param.u.perf_counter.type, | |
165 | context_param.u.perf_counter.config, | |
166 | context_param.u.perf_counter.name, | |
167 | ctx); | |
25b2f99a MD |
168 | case LTTNG_KERNEL_CONTEXT_COMM: |
169 | return lttng_add_comm_to_ctx(ctx); | |
8070f5c0 MD |
170 | default: |
171 | return -EINVAL; | |
172 | } | |
173 | } | |
174 | ||
ad1c05e1 MD |
175 | /** |
176 | * lttng_ioctl - lttng syscall through ioctl | |
177 | * | |
c0e31d2e | 178 | * @file: the file |
ad1c05e1 MD |
179 | * @cmd: the command |
180 | * @arg: command arg | |
181 | * | |
182 | * This ioctl implements lttng commands: | |
38d024ae | 183 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 184 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
185 | * LTTNG_KERNEL_TRACER_VERSION |
186 | * Returns the LTTng kernel tracer version | |
187 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
188 | * Returns a file descriptor listing available tracepoints | |
ad1c05e1 MD |
189 | * |
190 | * The returned session will be deleted when its file descriptor is closed. | |
191 | */ | |
192 | static | |
c0e31d2e | 193 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
194 | { |
195 | switch (cmd) { | |
38d024ae | 196 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 197 | return lttng_abi_create_session(); |
80c16bcf MD |
198 | case LTTNG_KERNEL_TRACER_VERSION: |
199 | return lttng_abi_tracer_version(file, | |
200 | (struct lttng_kernel_tracer_version __user *) arg); | |
271b6681 MD |
201 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
202 | return lttng_abi_tracepoint_list(); | |
ad1c05e1 MD |
203 | default: |
204 | return -ENOIOCTLCMD; | |
205 | } | |
206 | } | |
207 | ||
ad1c05e1 MD |
208 | static const struct file_operations lttng_fops = { |
209 | .unlocked_ioctl = lttng_ioctl, | |
210 | #ifdef CONFIG_COMPAT | |
03037b98 | 211 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 212 | #endif |
11b5a3c2 | 213 | }; |
ad1c05e1 | 214 | |
5dbbdb43 MD |
215 | /* |
216 | * We tolerate no failure in this function (if one happens, we print a dmesg | |
217 | * error, but cannot return any error, because the channel information is | |
218 | * invariant. | |
219 | */ | |
220 | static | |
221 | void lttng_metadata_create_events(struct file *channel_file) | |
222 | { | |
223 | struct ltt_channel *channel = channel_file->private_data; | |
e70a4758 | 224 | static struct lttng_kernel_event metadata_params = { |
ab2277d6 | 225 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
e70a4758 MD |
226 | .name = "lttng_metadata", |
227 | }; | |
5dbbdb43 MD |
228 | struct ltt_event *event; |
229 | int ret; | |
5dbbdb43 | 230 | |
5dbbdb43 MD |
231 | /* |
232 | * We tolerate no failure path after event creation. It will stay | |
233 | * invariant for the rest of the session. | |
234 | */ | |
30bdb6e4 | 235 | event = ltt_event_create(channel, &metadata_params, NULL); |
5dbbdb43 | 236 | if (!event) { |
588e6a7f | 237 | ret = -EINVAL; |
271b6681 | 238 | goto create_error; |
5dbbdb43 MD |
239 | } |
240 | return; | |
241 | ||
85a9ca7f | 242 | create_error: |
5dbbdb43 MD |
243 | WARN_ON(1); |
244 | return; /* not allowed to return error */ | |
245 | } | |
246 | ||
247 | static | |
c0e31d2e | 248 | int lttng_abi_create_channel(struct file *session_file, |
38d024ae | 249 | struct lttng_kernel_channel __user *uchan_param, |
5dbbdb43 | 250 | enum channel_type channel_type) |
baf20995 | 251 | { |
c0e31d2e | 252 | struct ltt_session *session = session_file->private_data; |
5dbbdb43 MD |
253 | const struct file_operations *fops; |
254 | const char *transport_name; | |
baf20995 | 255 | struct ltt_channel *chan; |
c0e31d2e | 256 | struct file *chan_file; |
38d024ae | 257 | struct lttng_kernel_channel chan_param; |
baf20995 | 258 | int chan_fd; |
ad1c05e1 | 259 | int ret = 0; |
baf20995 | 260 | |
653fe716 | 261 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
baf20995 | 262 | return -EFAULT; |
1c25284c | 263 | chan_fd = get_unused_fd(); |
baf20995 MD |
264 | if (chan_fd < 0) { |
265 | ret = chan_fd; | |
266 | goto fd_error; | |
267 | } | |
c0e31d2e | 268 | chan_file = anon_inode_getfile("[lttng_channel]", |
ad1c05e1 | 269 | <tng_channel_fops, |
03037b98 | 270 | NULL, O_RDWR); |
c0e31d2e MD |
271 | if (IS_ERR(chan_file)) { |
272 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
273 | goto file_error; |
274 | } | |
5dbbdb43 MD |
275 | switch (channel_type) { |
276 | case PER_CPU_CHANNEL: | |
277 | transport_name = chan_param.overwrite ? | |
278 | "relay-overwrite" : "relay-discard"; | |
279 | fops = <tng_channel_fops; | |
280 | break; | |
5dbbdb43 | 281 | case METADATA_CHANNEL: |
881833e3 | 282 | transport_name = "relay-metadata"; |
5dbbdb43 MD |
283 | fops = <tng_metadata_fops; |
284 | break; | |
285 | default: | |
286 | transport_name = "<unknown>"; | |
287 | break; | |
288 | } | |
03037b98 MD |
289 | /* |
290 | * We tolerate no failure path after channel creation. It will stay | |
291 | * invariant for the rest of the session. | |
292 | */ | |
5dbbdb43 | 293 | chan = ltt_channel_create(session, transport_name, NULL, |
11b5a3c2 MD |
294 | chan_param.subbuf_size, |
295 | chan_param.num_subbuf, | |
296 | chan_param.switch_timer_interval, | |
297 | chan_param.read_timer_interval); | |
03037b98 | 298 | if (!chan) { |
f3d01b96 | 299 | ret = -EINVAL; |
03037b98 MD |
300 | goto chan_error; |
301 | } | |
11b5a3c2 | 302 | chan->file = chan_file; |
c0e31d2e MD |
303 | chan_file->private_data = chan; |
304 | fd_install(chan_fd, chan_file); | |
cd4bd11f | 305 | if (channel_type == METADATA_CHANNEL) { |
cd4bd11f | 306 | session->metadata = chan; |
4f1c3952 | 307 | lttng_metadata_create_events(chan_file); |
cd4bd11f | 308 | } |
5dbbdb43 | 309 | |
ad1c05e1 | 310 | /* The channel created holds a reference on the session */ |
b0caa15a | 311 | atomic_long_inc(&session_file->f_count); |
ad1c05e1 | 312 | |
baf20995 MD |
313 | return chan_fd; |
314 | ||
03037b98 | 315 | chan_error: |
c0e31d2e | 316 | fput(chan_file); |
baf20995 MD |
317 | file_error: |
318 | put_unused_fd(chan_fd); | |
319 | fd_error: | |
baf20995 MD |
320 | return ret; |
321 | } | |
322 | ||
323 | /** | |
ad1c05e1 | 324 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 325 | * |
c0e31d2e | 326 | * @file: the file |
baf20995 MD |
327 | * @cmd: the command |
328 | * @arg: command arg | |
329 | * | |
330 | * This ioctl implements lttng commands: | |
38d024ae | 331 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 332 | * Returns a LTTng channel file descriptor |
8070f5c0 MD |
333 | * LTTNG_KERNEL_SESSION_START |
334 | * Starts tracing session | |
335 | * LTTNG_KERNEL_SESSION_STOP | |
336 | * Stops tracing session | |
337 | * LTTNG_KERNEL_METADATA | |
338 | * Returns a LTTng metadata file descriptor | |
ad1c05e1 MD |
339 | * |
340 | * The returned channel will be deleted when its file descriptor is closed. | |
341 | */ | |
342 | static | |
c0e31d2e | 343 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 344 | { |
c0e31d2e MD |
345 | struct ltt_session *session = file->private_data; |
346 | ||
ad1c05e1 | 347 | switch (cmd) { |
38d024ae | 348 | case LTTNG_KERNEL_CHANNEL: |
5dbbdb43 | 349 | return lttng_abi_create_channel(file, |
38d024ae | 350 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 351 | PER_CPU_CHANNEL); |
38d024ae | 352 | case LTTNG_KERNEL_SESSION_START: |
c0e31d2e | 353 | return ltt_session_start(session); |
38d024ae | 354 | case LTTNG_KERNEL_SESSION_STOP: |
c0e31d2e | 355 | return ltt_session_stop(session); |
38d024ae | 356 | case LTTNG_KERNEL_METADATA: |
5dbbdb43 | 357 | return lttng_abi_create_channel(file, |
38d024ae | 358 | (struct lttng_kernel_channel __user *) arg, |
5dbbdb43 | 359 | METADATA_CHANNEL); |
ad1c05e1 MD |
360 | default: |
361 | return -ENOIOCTLCMD; | |
362 | } | |
363 | } | |
364 | ||
03037b98 MD |
365 | /* |
366 | * Called when the last file reference is dropped. | |
367 | * | |
368 | * Big fat note: channels and events are invariant for the whole session after | |
369 | * their creation. So this session destruction also destroys all channel and | |
370 | * event structures specific to this session (they are not destroyed when their | |
371 | * individual file is released). | |
372 | */ | |
ad1c05e1 | 373 | static |
03037b98 | 374 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 375 | { |
03037b98 | 376 | struct ltt_session *session = file->private_data; |
c269fff4 MD |
377 | |
378 | if (session) | |
379 | ltt_session_destroy(session); | |
11b5a3c2 | 380 | return 0; |
ad1c05e1 | 381 | } |
ad1c05e1 MD |
382 | |
383 | static const struct file_operations lttng_session_fops = { | |
03037b98 | 384 | .release = lttng_session_release, |
ad1c05e1 MD |
385 | .unlocked_ioctl = lttng_session_ioctl, |
386 | #ifdef CONFIG_COMPAT | |
03037b98 | 387 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 388 | #endif |
11b5a3c2 | 389 | }; |
ad1c05e1 MD |
390 | |
391 | static | |
c0e31d2e | 392 | int lttng_abi_open_stream(struct file *channel_file) |
ad1c05e1 | 393 | { |
c0e31d2e | 394 | struct ltt_channel *channel = channel_file->private_data; |
ad1c05e1 MD |
395 | struct lib_ring_buffer *buf; |
396 | int stream_fd, ret; | |
11b5a3c2 | 397 | struct file *stream_file; |
ad1c05e1 | 398 | |
11b5a3c2 | 399 | buf = channel->ops->buffer_read_open(channel->chan); |
ad1c05e1 MD |
400 | if (!buf) |
401 | return -ENOENT; | |
402 | ||
1c25284c | 403 | stream_fd = get_unused_fd(); |
ad1c05e1 MD |
404 | if (stream_fd < 0) { |
405 | ret = stream_fd; | |
406 | goto fd_error; | |
407 | } | |
c0e31d2e | 408 | stream_file = anon_inode_getfile("[lttng_stream]", |
7f57c73c | 409 | &lib_ring_buffer_file_operations, |
ad1c05e1 | 410 | buf, O_RDWR); |
c0e31d2e MD |
411 | if (IS_ERR(stream_file)) { |
412 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
413 | goto file_error; |
414 | } | |
409453cb MD |
415 | /* |
416 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
417 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
418 | * file descriptor, so we set FMODE_PREAD here. | |
419 | */ | |
d7b6f197 | 420 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 421 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
422 | /* |
423 | * The stream holds a reference to the channel within the generic ring | |
424 | * buffer library, so no need to hold a refcount on the channel and | |
425 | * session files here. | |
426 | */ | |
ad1c05e1 MD |
427 | return stream_fd; |
428 | ||
429 | file_error: | |
430 | put_unused_fd(stream_fd); | |
431 | fd_error: | |
11b5a3c2 | 432 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
433 | return ret; |
434 | } | |
435 | ||
653fe716 | 436 | static |
c0e31d2e | 437 | int lttng_abi_create_event(struct file *channel_file, |
38d024ae | 438 | struct lttng_kernel_event __user *uevent_param) |
653fe716 | 439 | { |
c0e31d2e | 440 | struct ltt_channel *channel = channel_file->private_data; |
653fe716 | 441 | struct ltt_event *event; |
38d024ae | 442 | struct lttng_kernel_event event_param; |
653fe716 | 443 | int event_fd, ret; |
11b5a3c2 | 444 | struct file *event_file; |
653fe716 MD |
445 | |
446 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) | |
447 | return -EFAULT; | |
30bdb6e4 | 448 | event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 449 | switch (event_param.instrumentation) { |
ab2277d6 | 450 | case LTTNG_KERNEL_KPROBE: |
e0a7a7c4 MD |
451 | event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
452 | break; | |
ab2277d6 | 453 | case LTTNG_KERNEL_FUNCTION: |
e0a7a7c4 MD |
454 | event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
455 | break; | |
456 | default: | |
457 | break; | |
458 | } | |
1c25284c | 459 | event_fd = get_unused_fd(); |
653fe716 MD |
460 | if (event_fd < 0) { |
461 | ret = event_fd; | |
462 | goto fd_error; | |
463 | } | |
c0e31d2e | 464 | event_file = anon_inode_getfile("[lttng_event]", |
3b923e5b | 465 | <tng_event_fops, |
03037b98 | 466 | NULL, O_RDWR); |
c0e31d2e MD |
467 | if (IS_ERR(event_file)) { |
468 | ret = PTR_ERR(event_file); | |
653fe716 MD |
469 | goto file_error; |
470 | } | |
03037b98 MD |
471 | /* |
472 | * We tolerate no failure path after event creation. It will stay | |
473 | * invariant for the rest of the session. | |
474 | */ | |
30bdb6e4 | 475 | event = ltt_event_create(channel, &event_param, NULL); |
03037b98 | 476 | if (!event) { |
271b6681 | 477 | ret = -EINVAL; |
d6d808f3 | 478 | goto event_error; |
03037b98 | 479 | } |
c0e31d2e MD |
480 | event_file->private_data = event; |
481 | fd_install(event_fd, event_file); | |
653fe716 | 482 | /* The event holds a reference on the channel */ |
b0caa15a | 483 | atomic_long_inc(&channel_file->f_count); |
653fe716 MD |
484 | return event_fd; |
485 | ||
03037b98 | 486 | event_error: |
c0e31d2e | 487 | fput(event_file); |
653fe716 MD |
488 | file_error: |
489 | put_unused_fd(event_fd); | |
490 | fd_error: | |
653fe716 MD |
491 | return ret; |
492 | } | |
ad1c05e1 MD |
493 | |
494 | /** | |
495 | * lttng_channel_ioctl - lttng syscall through ioctl | |
496 | * | |
c0e31d2e | 497 | * @file: the file |
ad1c05e1 MD |
498 | * @cmd: the command |
499 | * @arg: command arg | |
500 | * | |
501 | * This ioctl implements lttng commands: | |
38d024ae | 502 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
503 | * Returns an event stream file descriptor or failure. |
504 | * (typically, one event stream records events from one CPU) | |
38d024ae | 505 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 506 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
507 | * LTTNG_KERNEL_CONTEXT |
508 | * Prepend a context field to each event in the channel | |
baf20995 | 509 | * |
baf20995 MD |
510 | * Channel and event file descriptors also hold a reference on the session. |
511 | */ | |
ad1c05e1 | 512 | static |
c0e31d2e | 513 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 514 | { |
8070f5c0 MD |
515 | struct ltt_channel *channel = file->private_data; |
516 | ||
baf20995 | 517 | switch (cmd) { |
38d024ae | 518 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 519 | return lttng_abi_open_stream(file); |
38d024ae MD |
520 | case LTTNG_KERNEL_EVENT: |
521 | return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg); | |
8070f5c0 MD |
522 | case LTTNG_KERNEL_CONTEXT: |
523 | return lttng_abi_add_context(file, | |
524 | (struct lttng_kernel_context __user *) arg, | |
525 | &channel->ctx, channel->session); | |
baf20995 MD |
526 | default: |
527 | return -ENOIOCTLCMD; | |
528 | } | |
529 | } | |
530 | ||
5dbbdb43 MD |
531 | /** |
532 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
533 | * | |
534 | * @file: the file | |
535 | * @cmd: the command | |
536 | * @arg: command arg | |
537 | * | |
538 | * This ioctl implements lttng commands: | |
38d024ae | 539 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
540 | * Returns an event stream file descriptor or failure. |
541 | * | |
542 | * Channel and event file descriptors also hold a reference on the session. | |
543 | */ | |
544 | static | |
545 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
546 | { | |
547 | switch (cmd) { | |
38d024ae | 548 | case LTTNG_KERNEL_STREAM: |
5dbbdb43 MD |
549 | return lttng_abi_open_stream(file); |
550 | default: | |
551 | return -ENOIOCTLCMD; | |
552 | } | |
553 | } | |
554 | ||
653fe716 MD |
555 | /** |
556 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
557 | * | |
c0e31d2e | 558 | * @file: the file |
653fe716 MD |
559 | * @wait: poll table |
560 | */ | |
c0e31d2e | 561 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 562 | { |
c0e31d2e | 563 | struct ltt_channel *channel = file->private_data; |
653fe716 MD |
564 | unsigned int mask = 0; |
565 | ||
c0e31d2e | 566 | if (file->f_mode & FMODE_READ) { |
24cedcfe MD |
567 | init_poll_funcptr(wait, wrapper_pollwait_exclusive); |
568 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), | |
569 | wait); | |
653fe716 | 570 | |
254ec7bc MD |
571 | if (channel->ops->is_disabled(channel->chan)) |
572 | return POLLERR; | |
24cedcfe | 573 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 MD |
574 | return POLLHUP; |
575 | else | |
576 | return POLLIN | POLLRDNORM; | |
577 | } | |
578 | return mask; | |
579 | ||
580 | } | |
581 | ||
0a84a57f MD |
582 | static |
583 | int lttng_channel_release(struct inode *inode, struct file *file) | |
584 | { | |
585 | struct ltt_channel *channel = file->private_data; | |
c269fff4 MD |
586 | |
587 | if (channel) | |
588 | fput(channel->session->file); | |
0a84a57f MD |
589 | return 0; |
590 | } | |
591 | ||
ad1c05e1 | 592 | static const struct file_operations lttng_channel_fops = { |
03037b98 | 593 | .release = lttng_channel_release, |
653fe716 | 594 | .poll = lttng_channel_poll, |
ad1c05e1 | 595 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 596 | #ifdef CONFIG_COMPAT |
03037b98 | 597 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 598 | #endif |
11b5a3c2 | 599 | }; |
baf20995 | 600 | |
5dbbdb43 MD |
601 | static const struct file_operations lttng_metadata_fops = { |
602 | .release = lttng_channel_release, | |
603 | .unlocked_ioctl = lttng_metadata_ioctl, | |
604 | #ifdef CONFIG_COMPAT | |
605 | .compat_ioctl = lttng_metadata_ioctl, | |
606 | #endif | |
607 | }; | |
608 | ||
8070f5c0 MD |
609 | /** |
610 | * lttng_event_ioctl - lttng syscall through ioctl | |
611 | * | |
612 | * @file: the file | |
613 | * @cmd: the command | |
614 | * @arg: command arg | |
615 | * | |
616 | * This ioctl implements lttng commands: | |
617 | * LTTNG_KERNEL_STREAM | |
618 | * Returns an event stream file descriptor or failure. | |
619 | * (typically, one event stream records events from one CPU) | |
620 | * LTTNG_KERNEL_EVENT | |
621 | * Returns an event file descriptor or failure. | |
622 | * LTTNG_KERNEL_CONTEXT | |
623 | * Prepend a context field to each record of this event | |
624 | */ | |
625 | static | |
626 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
627 | { | |
628 | struct ltt_event *event = file->private_data; | |
629 | ||
630 | switch (cmd) { | |
631 | case LTTNG_KERNEL_CONTEXT: | |
632 | return lttng_abi_add_context(file, | |
633 | (struct lttng_kernel_context __user *) arg, | |
634 | &event->ctx, event->chan->session); | |
635 | default: | |
636 | return -ENOIOCTLCMD; | |
637 | } | |
638 | } | |
639 | ||
0a84a57f MD |
640 | static |
641 | int lttng_event_release(struct inode *inode, struct file *file) | |
642 | { | |
643 | struct ltt_event *event = file->private_data; | |
c269fff4 | 644 | |
aa7c23a9 | 645 | if (event) |
c269fff4 | 646 | fput(event->chan->file); |
0a84a57f MD |
647 | return 0; |
648 | } | |
649 | ||
3b923e5b | 650 | /* TODO: filter control ioctl */ |
0a84a57f MD |
651 | static const struct file_operations lttng_event_fops = { |
652 | .release = lttng_event_release, | |
8070f5c0 MD |
653 | .unlocked_ioctl = lttng_event_ioctl, |
654 | #ifdef CONFIG_COMPAT | |
655 | .compat_ioctl = lttng_event_ioctl, | |
656 | #endif | |
11b5a3c2 | 657 | }; |
0a84a57f | 658 | |
1c25284c | 659 | int __init ltt_debugfs_abi_init(void) |
baf20995 MD |
660 | { |
661 | int ret = 0; | |
662 | ||
6d2a620c | 663 | wrapper_vmalloc_sync_all(); |
11b5a3c2 | 664 | lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL, |
f3d01b96 | 665 | <tng_fops); |
11b5a3c2 | 666 | if (IS_ERR(lttng_dentry) || !lttng_dentry) { |
baf20995 MD |
667 | printk(KERN_ERR "Error creating LTTng control file\n"); |
668 | ret = -ENOMEM; | |
669 | goto error; | |
670 | } | |
671 | error: | |
672 | return ret; | |
673 | } | |
674 | ||
1c25284c | 675 | void __exit ltt_debugfs_abi_exit(void) |
baf20995 | 676 | { |
11b5a3c2 | 677 | debugfs_remove(lttng_dentry); |
baf20995 | 678 | } |