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