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