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> | |
31 | #include <linux/ringbuffer/vfs.h> | |
32 | #include "ltt-debugfs-abi.h" | |
ad1c05e1 | 33 | #include "ltt-events.h" |
baf20995 MD |
34 | |
35 | /* | |
36 | * This is LTTng's own personal way to create a system call as an external | |
37 | * module. We use ioctl() on /sys/kernel/debug/lttng. | |
38 | */ | |
39 | ||
40 | static struct dentry *lttng_dentry; | |
ad1c05e1 MD |
41 | static const struct file_operations lttng_fops; |
42 | static const struct file_operations lttng_session_fops; | |
43 | static const struct file_operations lttng_channel_fops; | |
305d3e42 | 44 | static const struct file_operations lttng_event_fops; |
baf20995 | 45 | |
ad1c05e1 | 46 | static |
baf20995 MD |
47 | int lttng_abi_create_session(void) |
48 | { | |
49 | struct ltt_session *session; | |
c0e31d2e | 50 | struct file *session_file; |
11b5a3c2 | 51 | int session_fd, ret; |
baf20995 | 52 | |
653fe716 | 53 | session = ltt_session_create(); |
baf20995 MD |
54 | if (!session) |
55 | return -ENOMEM; | |
1c25284c | 56 | session_fd = get_unused_fd(); |
baf20995 MD |
57 | if (session_fd < 0) { |
58 | ret = session_fd; | |
59 | goto fd_error; | |
60 | } | |
c0e31d2e | 61 | session_file = anon_inode_getfile("[lttng_session]", |
ad1c05e1 | 62 | <tng_session_fops, |
baf20995 | 63 | session, O_RDWR); |
c0e31d2e MD |
64 | if (IS_ERR(session_file)) { |
65 | ret = PTR_ERR(session_file); | |
baf20995 MD |
66 | goto file_error; |
67 | } | |
c0e31d2e MD |
68 | session->file = session_file; |
69 | fd_install(session_fd, session_file); | |
baf20995 MD |
70 | return session_fd; |
71 | ||
72 | file_error: | |
73 | put_unused_fd(session_fd); | |
74 | fd_error: | |
75 | ltt_session_destroy(session); | |
76 | return ret; | |
77 | } | |
78 | ||
ad1c05e1 MD |
79 | /** |
80 | * lttng_ioctl - lttng syscall through ioctl | |
81 | * | |
c0e31d2e | 82 | * @file: the file |
ad1c05e1 MD |
83 | * @cmd: the command |
84 | * @arg: command arg | |
85 | * | |
86 | * This ioctl implements lttng commands: | |
87 | * LTTNG_SESSION | |
88 | * Returns a LTTng trace session file descriptor | |
89 | * | |
90 | * The returned session will be deleted when its file descriptor is closed. | |
91 | */ | |
92 | static | |
c0e31d2e | 93 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
94 | { |
95 | switch (cmd) { | |
96 | case LTTNG_SESSION: | |
97 | return lttng_abi_create_session(); | |
98 | default: | |
99 | return -ENOIOCTLCMD; | |
100 | } | |
101 | } | |
102 | ||
ad1c05e1 MD |
103 | static const struct file_operations lttng_fops = { |
104 | .unlocked_ioctl = lttng_ioctl, | |
105 | #ifdef CONFIG_COMPAT | |
03037b98 | 106 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 107 | #endif |
11b5a3c2 | 108 | }; |
ad1c05e1 | 109 | |
c0e31d2e | 110 | int lttng_abi_create_channel(struct file *session_file, |
ad1c05e1 | 111 | struct lttng_channel __user *uchan_param) |
baf20995 | 112 | { |
c0e31d2e | 113 | struct ltt_session *session = session_file->private_data; |
baf20995 | 114 | struct ltt_channel *chan; |
c0e31d2e | 115 | struct file *chan_file; |
baf20995 MD |
116 | struct lttng_channel chan_param; |
117 | int chan_fd; | |
ad1c05e1 | 118 | int ret = 0; |
baf20995 | 119 | |
653fe716 | 120 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
baf20995 | 121 | return -EFAULT; |
1c25284c | 122 | chan_fd = get_unused_fd(); |
baf20995 MD |
123 | if (chan_fd < 0) { |
124 | ret = chan_fd; | |
125 | goto fd_error; | |
126 | } | |
c0e31d2e | 127 | chan_file = anon_inode_getfile("[lttng_channel]", |
ad1c05e1 | 128 | <tng_channel_fops, |
03037b98 | 129 | NULL, O_RDWR); |
c0e31d2e MD |
130 | if (IS_ERR(chan_file)) { |
131 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
132 | goto file_error; |
133 | } | |
03037b98 MD |
134 | /* |
135 | * We tolerate no failure path after channel creation. It will stay | |
136 | * invariant for the rest of the session. | |
137 | */ | |
11b5a3c2 MD |
138 | chan = ltt_channel_create(session, chan_param.overwrite, NULL, |
139 | chan_param.subbuf_size, | |
140 | chan_param.num_subbuf, | |
141 | chan_param.switch_timer_interval, | |
142 | chan_param.read_timer_interval); | |
03037b98 | 143 | if (!chan) { |
f3d01b96 | 144 | ret = -EINVAL; |
03037b98 MD |
145 | goto chan_error; |
146 | } | |
11b5a3c2 | 147 | chan->file = chan_file; |
c0e31d2e MD |
148 | chan_file->private_data = chan; |
149 | fd_install(chan_fd, chan_file); | |
ad1c05e1 | 150 | /* The channel created holds a reference on the session */ |
b0caa15a | 151 | atomic_long_inc(&session_file->f_count); |
ad1c05e1 | 152 | |
baf20995 MD |
153 | return chan_fd; |
154 | ||
03037b98 | 155 | chan_error: |
c0e31d2e | 156 | fput(chan_file); |
baf20995 MD |
157 | file_error: |
158 | put_unused_fd(chan_fd); | |
159 | fd_error: | |
baf20995 MD |
160 | return ret; |
161 | } | |
162 | ||
163 | /** | |
ad1c05e1 | 164 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 165 | * |
c0e31d2e | 166 | * @file: the file |
baf20995 MD |
167 | * @cmd: the command |
168 | * @arg: command arg | |
169 | * | |
170 | * This ioctl implements lttng commands: | |
baf20995 MD |
171 | * LTTNG_CHANNEL |
172 | * Returns a LTTng channel file descriptor | |
ad1c05e1 MD |
173 | * |
174 | * The returned channel will be deleted when its file descriptor is closed. | |
175 | */ | |
176 | static | |
c0e31d2e | 177 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 178 | { |
c0e31d2e MD |
179 | struct ltt_session *session = file->private_data; |
180 | ||
ad1c05e1 MD |
181 | switch (cmd) { |
182 | case LTTNG_CHANNEL: | |
c0e31d2e MD |
183 | return lttng_abi_create_channel(file, (struct lttng_channel __user *)arg); |
184 | case LTTNG_SESSION_START: | |
185 | return ltt_session_start(session); | |
186 | case LTTNG_SESSION_STOP: | |
187 | return ltt_session_stop(session); | |
ad1c05e1 MD |
188 | default: |
189 | return -ENOIOCTLCMD; | |
190 | } | |
191 | } | |
192 | ||
03037b98 MD |
193 | /* |
194 | * Called when the last file reference is dropped. | |
195 | * | |
196 | * Big fat note: channels and events are invariant for the whole session after | |
197 | * their creation. So this session destruction also destroys all channel and | |
198 | * event structures specific to this session (they are not destroyed when their | |
199 | * individual file is released). | |
200 | */ | |
ad1c05e1 | 201 | static |
03037b98 | 202 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 203 | { |
03037b98 | 204 | struct ltt_session *session = file->private_data; |
c269fff4 MD |
205 | |
206 | if (session) | |
207 | ltt_session_destroy(session); | |
11b5a3c2 | 208 | return 0; |
ad1c05e1 | 209 | } |
ad1c05e1 MD |
210 | |
211 | static const struct file_operations lttng_session_fops = { | |
03037b98 | 212 | .release = lttng_session_release, |
ad1c05e1 MD |
213 | .unlocked_ioctl = lttng_session_ioctl, |
214 | #ifdef CONFIG_COMPAT | |
03037b98 | 215 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 216 | #endif |
11b5a3c2 | 217 | }; |
ad1c05e1 MD |
218 | |
219 | static | |
c0e31d2e | 220 | int lttng_abi_open_stream(struct file *channel_file) |
ad1c05e1 | 221 | { |
c0e31d2e | 222 | struct ltt_channel *channel = channel_file->private_data; |
ad1c05e1 MD |
223 | struct lib_ring_buffer *buf; |
224 | int stream_fd, ret; | |
11b5a3c2 | 225 | struct file *stream_file; |
ad1c05e1 | 226 | |
11b5a3c2 | 227 | buf = channel->ops->buffer_read_open(channel->chan); |
ad1c05e1 MD |
228 | if (!buf) |
229 | return -ENOENT; | |
230 | ||
1c25284c | 231 | stream_fd = get_unused_fd(); |
ad1c05e1 MD |
232 | if (stream_fd < 0) { |
233 | ret = stream_fd; | |
234 | goto fd_error; | |
235 | } | |
c0e31d2e | 236 | stream_file = anon_inode_getfile("[lttng_stream]", |
7f57c73c | 237 | &lib_ring_buffer_file_operations, |
ad1c05e1 | 238 | buf, O_RDWR); |
c0e31d2e MD |
239 | if (IS_ERR(stream_file)) { |
240 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
241 | goto file_error; |
242 | } | |
c0e31d2e | 243 | fd_install(stream_fd, stream_file); |
ad1c05e1 | 244 | /* The stream holds a reference on the channel */ |
b0caa15a | 245 | atomic_long_inc(&channel_file->f_count); |
ad1c05e1 MD |
246 | return stream_fd; |
247 | ||
248 | file_error: | |
249 | put_unused_fd(stream_fd); | |
250 | fd_error: | |
11b5a3c2 | 251 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
252 | return ret; |
253 | } | |
254 | ||
653fe716 | 255 | static |
c0e31d2e | 256 | int lttng_abi_create_event(struct file *channel_file, |
653fe716 MD |
257 | struct lttng_event __user *uevent_param) |
258 | { | |
c0e31d2e | 259 | struct ltt_channel *channel = channel_file->private_data; |
653fe716 MD |
260 | struct ltt_event *event; |
261 | char *event_name; | |
262 | struct lttng_event event_param; | |
263 | int event_fd, ret; | |
11b5a3c2 | 264 | struct file *event_file; |
653fe716 MD |
265 | |
266 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) | |
267 | return -EFAULT; | |
268 | event_name = kmalloc(PATH_MAX, GFP_KERNEL); | |
269 | if (!event_name) | |
270 | return -ENOMEM; | |
981616e8 | 271 | if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) { |
653fe716 MD |
272 | ret = -EFAULT; |
273 | goto name_error; | |
274 | } | |
275 | event_name[PATH_MAX - 1] = '\0'; | |
1c25284c | 276 | event_fd = get_unused_fd(); |
653fe716 MD |
277 | if (event_fd < 0) { |
278 | ret = event_fd; | |
279 | goto fd_error; | |
280 | } | |
c0e31d2e | 281 | event_file = anon_inode_getfile("[lttng_event]", |
3b923e5b | 282 | <tng_event_fops, |
03037b98 | 283 | NULL, O_RDWR); |
c0e31d2e MD |
284 | if (IS_ERR(event_file)) { |
285 | ret = PTR_ERR(event_file); | |
653fe716 MD |
286 | goto file_error; |
287 | } | |
03037b98 MD |
288 | /* |
289 | * We tolerate no failure path after event creation. It will stay | |
290 | * invariant for the rest of the session. | |
291 | */ | |
11b5a3c2 MD |
292 | event = ltt_event_create(channel, event_name, event_param.itype, |
293 | NULL, NULL); /* TODO non-null probe */ | |
03037b98 MD |
294 | if (!event) { |
295 | goto event_error; | |
296 | ret = -EEXIST; | |
297 | } | |
c0e31d2e MD |
298 | event_file->private_data = event; |
299 | fd_install(event_fd, event_file); | |
653fe716 | 300 | /* The event holds a reference on the channel */ |
b0caa15a | 301 | atomic_long_inc(&channel_file->f_count); |
03037b98 | 302 | kfree(event_name); |
653fe716 MD |
303 | return event_fd; |
304 | ||
03037b98 | 305 | event_error: |
c0e31d2e | 306 | fput(event_file); |
653fe716 MD |
307 | file_error: |
308 | put_unused_fd(event_fd); | |
309 | fd_error: | |
653fe716 MD |
310 | name_error: |
311 | kfree(event_name); | |
312 | return ret; | |
313 | } | |
ad1c05e1 MD |
314 | |
315 | /** | |
316 | * lttng_channel_ioctl - lttng syscall through ioctl | |
317 | * | |
c0e31d2e | 318 | * @file: the file |
ad1c05e1 MD |
319 | * @cmd: the command |
320 | * @arg: command arg | |
321 | * | |
322 | * This ioctl implements lttng commands: | |
323 | * LTTNG_STREAM | |
324 | * Returns an event stream file descriptor or failure. | |
325 | * (typically, one event stream records events from one CPU) | |
baf20995 | 326 | * LTTNG_EVENT |
ad1c05e1 | 327 | * Returns an event file descriptor or failure. |
baf20995 MD |
328 | * |
329 | * The returned session will be deleted when its file descriptor is closed. | |
330 | * Channel and event file descriptors also hold a reference on the session. | |
331 | */ | |
ad1c05e1 | 332 | static |
c0e31d2e | 333 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 334 | { |
baf20995 | 335 | switch (cmd) { |
ad1c05e1 | 336 | case LTTNG_STREAM: |
c0e31d2e | 337 | return lttng_abi_open_stream(file); |
baf20995 | 338 | case LTTNG_EVENT: |
c0e31d2e | 339 | return lttng_abi_create_event(file, (struct lttng_event __user *)arg); |
baf20995 MD |
340 | default: |
341 | return -ENOIOCTLCMD; | |
342 | } | |
343 | } | |
344 | ||
3b923e5b MD |
345 | /* TODO: poll */ |
346 | #if 0 | |
653fe716 MD |
347 | /** |
348 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
349 | * | |
c0e31d2e | 350 | * @file: the file |
653fe716 MD |
351 | * @wait: poll table |
352 | */ | |
c0e31d2e | 353 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 354 | { |
c0e31d2e | 355 | struct ltt_channel *channel = file->private_data; |
653fe716 MD |
356 | unsigned int mask = 0; |
357 | ||
c0e31d2e | 358 | if (file->f_mode & FMODE_READ) { |
653fe716 | 359 | poll_wait_set_exclusive(wait); |
c0e31d2e | 360 | poll_wait(file, &channel->notify_wait, wait); |
653fe716 MD |
361 | |
362 | /* TODO: identify when the channel is being finalized. */ | |
363 | if (finalized) | |
364 | return POLLHUP; | |
365 | else | |
366 | return POLLIN | POLLRDNORM; | |
367 | } | |
368 | return mask; | |
369 | ||
370 | } | |
3b923e5b | 371 | #endif //0 |
653fe716 | 372 | |
0a84a57f MD |
373 | static |
374 | int lttng_channel_release(struct inode *inode, struct file *file) | |
375 | { | |
376 | struct ltt_channel *channel = file->private_data; | |
c269fff4 MD |
377 | |
378 | if (channel) | |
379 | fput(channel->session->file); | |
0a84a57f MD |
380 | return 0; |
381 | } | |
382 | ||
ad1c05e1 | 383 | static const struct file_operations lttng_channel_fops = { |
03037b98 | 384 | .release = lttng_channel_release, |
3b923e5b MD |
385 | /* TODO */ |
386 | #if 0 | |
653fe716 | 387 | .poll = lttng_channel_poll, |
3b923e5b | 388 | #endif //0 |
ad1c05e1 | 389 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 390 | #ifdef CONFIG_COMPAT |
03037b98 | 391 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 392 | #endif |
11b5a3c2 | 393 | }; |
baf20995 | 394 | |
0a84a57f MD |
395 | static |
396 | int lttng_event_release(struct inode *inode, struct file *file) | |
397 | { | |
398 | struct ltt_event *event = file->private_data; | |
c269fff4 MD |
399 | |
400 | if (event) | |
401 | fput(event->chan->file); | |
0a84a57f MD |
402 | return 0; |
403 | } | |
404 | ||
3b923e5b | 405 | /* TODO: filter control ioctl */ |
0a84a57f MD |
406 | static const struct file_operations lttng_event_fops = { |
407 | .release = lttng_event_release, | |
11b5a3c2 | 408 | }; |
0a84a57f | 409 | |
1c25284c | 410 | int __init ltt_debugfs_abi_init(void) |
baf20995 MD |
411 | { |
412 | int ret = 0; | |
413 | ||
11b5a3c2 | 414 | lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL, |
f3d01b96 | 415 | <tng_fops); |
11b5a3c2 | 416 | if (IS_ERR(lttng_dentry) || !lttng_dentry) { |
baf20995 MD |
417 | printk(KERN_ERR "Error creating LTTng control file\n"); |
418 | ret = -ENOMEM; | |
419 | goto error; | |
420 | } | |
421 | error: | |
422 | return ret; | |
423 | } | |
424 | ||
1c25284c | 425 | void __exit ltt_debugfs_abi_exit(void) |
baf20995 | 426 | { |
11b5a3c2 | 427 | debugfs_remove(lttng_dentry); |
baf20995 | 428 | } |