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