Commit | Line | Data |
---|---|---|
baf20995 | 1 | /* |
e8951e63 | 2 | * lttng-abi.c |
baf20995 | 3 | * |
e8951e63 | 4 | * LTTng ABI |
baf20995 | 5 | * |
886d51a3 MD |
6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | * | |
22 | * | |
baf20995 MD |
23 | * Mimic system calls for: |
24 | * - session creation, returns a file descriptor or failure. | |
ad1c05e1 MD |
25 | * - channel creation, returns a file descriptor or failure. |
26 | * - Operates on a session file descriptor | |
27 | * - Takes all channel options as parameters. | |
28 | * - stream get, returns a file descriptor or failure. | |
29 | * - Operates on a channel file descriptor. | |
30 | * - stream notifier get, returns a file descriptor or failure. | |
31 | * - Operates on a channel file descriptor. | |
32 | * - event creation, returns a file descriptor or failure. | |
33 | * - Operates on a channel file descriptor | |
34 | * - Takes an event name as parameter | |
35 | * - Takes an instrumentation source as parameter | |
36 | * - e.g. tracepoints, dynamic_probes... | |
37 | * - Takes instrumentation source specific arguments. | |
baf20995 MD |
38 | */ |
39 | ||
11b5a3c2 | 40 | #include <linux/module.h> |
e6a17f26 | 41 | #include <linux/proc_fs.h> |
11b5a3c2 MD |
42 | #include <linux/anon_inodes.h> |
43 | #include <linux/file.h> | |
44 | #include <linux/uaccess.h> | |
45 | #include <linux/slab.h> | |
b13f3ebe | 46 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
f3bc08c5 | 47 | #include "wrapper/ringbuffer/vfs.h" |
d83004aa JD |
48 | #include "wrapper/ringbuffer/backend.h" |
49 | #include "wrapper/ringbuffer/frontend.h" | |
24cedcfe | 50 | #include "wrapper/poll.h" |
e8951e63 | 51 | #include "lttng-abi.h" |
6dccd6c1 | 52 | #include "lttng-abi-old.h" |
a90917c3 MD |
53 | #include "lttng-events.h" |
54 | #include "lttng-tracer.h" | |
baf20995 MD |
55 | |
56 | /* | |
57 | * This is LTTng's own personal way to create a system call as an external | |
80996790 | 58 | * module. We use ioctl() on /proc/lttng. |
baf20995 MD |
59 | */ |
60 | ||
e6a17f26 | 61 | static struct proc_dir_entry *lttng_proc_dentry; |
ad1c05e1 MD |
62 | static const struct file_operations lttng_fops; |
63 | static const struct file_operations lttng_session_fops; | |
64 | static const struct file_operations lttng_channel_fops; | |
5dbbdb43 | 65 | static const struct file_operations lttng_metadata_fops; |
305d3e42 | 66 | static const struct file_operations lttng_event_fops; |
baf20995 | 67 | |
a33c9927 MD |
68 | /* |
69 | * Teardown management: opened file descriptors keep a refcount on the module, | |
70 | * so it can only exit when all file descriptors are closed. | |
71 | */ | |
72 | ||
ad1c05e1 | 73 | static |
baf20995 MD |
74 | int lttng_abi_create_session(void) |
75 | { | |
a90917c3 | 76 | struct lttng_session *session; |
c0e31d2e | 77 | struct file *session_file; |
11b5a3c2 | 78 | int session_fd, ret; |
baf20995 | 79 | |
a90917c3 | 80 | session = lttng_session_create(); |
baf20995 MD |
81 | if (!session) |
82 | return -ENOMEM; | |
1c25284c | 83 | session_fd = get_unused_fd(); |
baf20995 MD |
84 | if (session_fd < 0) { |
85 | ret = session_fd; | |
86 | goto fd_error; | |
87 | } | |
c0e31d2e | 88 | session_file = anon_inode_getfile("[lttng_session]", |
ad1c05e1 | 89 | <tng_session_fops, |
baf20995 | 90 | session, O_RDWR); |
c0e31d2e MD |
91 | if (IS_ERR(session_file)) { |
92 | ret = PTR_ERR(session_file); | |
baf20995 MD |
93 | goto file_error; |
94 | } | |
c0e31d2e MD |
95 | session->file = session_file; |
96 | fd_install(session_fd, session_file); | |
baf20995 MD |
97 | return session_fd; |
98 | ||
99 | file_error: | |
100 | put_unused_fd(session_fd); | |
101 | fd_error: | |
a90917c3 | 102 | lttng_session_destroy(session); |
baf20995 MD |
103 | return ret; |
104 | } | |
105 | ||
271b6681 MD |
106 | static |
107 | int lttng_abi_tracepoint_list(void) | |
108 | { | |
109 | struct file *tracepoint_list_file; | |
110 | int file_fd, ret; | |
111 | ||
112 | file_fd = get_unused_fd(); | |
113 | if (file_fd < 0) { | |
114 | ret = file_fd; | |
115 | goto fd_error; | |
116 | } | |
30f18bf0 | 117 | |
271b6681 MD |
118 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
119 | <tng_tracepoint_list_fops, | |
120 | NULL, O_RDWR); | |
121 | if (IS_ERR(tracepoint_list_file)) { | |
122 | ret = PTR_ERR(tracepoint_list_file); | |
123 | goto file_error; | |
124 | } | |
30f18bf0 MD |
125 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
126 | if (ret < 0) | |
127 | goto open_error; | |
271b6681 | 128 | fd_install(file_fd, tracepoint_list_file); |
30f18bf0 MD |
129 | if (file_fd < 0) { |
130 | ret = file_fd; | |
131 | goto fd_error; | |
132 | } | |
271b6681 MD |
133 | return file_fd; |
134 | ||
30f18bf0 MD |
135 | open_error: |
136 | fput(tracepoint_list_file); | |
271b6681 MD |
137 | file_error: |
138 | put_unused_fd(file_fd); | |
139 | fd_error: | |
140 | return ret; | |
141 | } | |
142 | ||
80c16bcf | 143 | static |
6dccd6c1 | 144 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
80c16bcf | 145 | { |
6dccd6c1 JD |
146 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
147 | v->minor = LTTNG_MODULES_MINOR_VERSION; | |
148 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; | |
80c16bcf MD |
149 | } |
150 | ||
8070f5c0 MD |
151 | static |
152 | long lttng_abi_add_context(struct file *file, | |
6dccd6c1 | 153 | struct lttng_kernel_context *context_param, |
a90917c3 | 154 | struct lttng_ctx **ctx, struct lttng_session *session) |
8070f5c0 | 155 | { |
8070f5c0 MD |
156 | |
157 | if (session->been_active) | |
158 | return -EPERM; | |
159 | ||
6dccd6c1 | 160 | switch (context_param->ctx) { |
12a313a5 | 161 | case LTTNG_KERNEL_CONTEXT_PID: |
8070f5c0 | 162 | return lttng_add_pid_to_ctx(ctx); |
a8ad3613 MD |
163 | case LTTNG_KERNEL_CONTEXT_PRIO: |
164 | return lttng_add_prio_to_ctx(ctx); | |
53f1f0ca MD |
165 | case LTTNG_KERNEL_CONTEXT_NICE: |
166 | return lttng_add_nice_to_ctx(ctx); | |
b64bc438 MD |
167 | case LTTNG_KERNEL_CONTEXT_VPID: |
168 | return lttng_add_vpid_to_ctx(ctx); | |
169 | case LTTNG_KERNEL_CONTEXT_TID: | |
170 | return lttng_add_tid_to_ctx(ctx); | |
171 | case LTTNG_KERNEL_CONTEXT_VTID: | |
172 | return lttng_add_vtid_to_ctx(ctx); | |
173 | case LTTNG_KERNEL_CONTEXT_PPID: | |
174 | return lttng_add_ppid_to_ctx(ctx); | |
175 | case LTTNG_KERNEL_CONTEXT_VPPID: | |
176 | return lttng_add_vppid_to_ctx(ctx); | |
12a313a5 | 177 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
6dccd6c1 JD |
178 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
179 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, | |
180 | context_param->u.perf_counter.config, | |
181 | context_param->u.perf_counter.name, | |
c24a0d71 | 182 | ctx); |
a2563e83 MD |
183 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
184 | return lttng_add_procname_to_ctx(ctx); | |
975da2c0 JD |
185 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
186 | return lttng_add_hostname_to_ctx(ctx); | |
8070f5c0 MD |
187 | default: |
188 | return -EINVAL; | |
189 | } | |
190 | } | |
191 | ||
ad1c05e1 MD |
192 | /** |
193 | * lttng_ioctl - lttng syscall through ioctl | |
194 | * | |
c0e31d2e | 195 | * @file: the file |
ad1c05e1 MD |
196 | * @cmd: the command |
197 | * @arg: command arg | |
198 | * | |
199 | * This ioctl implements lttng commands: | |
38d024ae | 200 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 201 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
202 | * LTTNG_KERNEL_TRACER_VERSION |
203 | * Returns the LTTng kernel tracer version | |
204 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
205 | * Returns a file descriptor listing available tracepoints | |
360f38ea MD |
206 | * LTTNG_KERNEL_WAIT_QUIESCENT |
207 | * Returns after all previously running probes have completed | |
ad1c05e1 MD |
208 | * |
209 | * The returned session will be deleted when its file descriptor is closed. | |
210 | */ | |
211 | static | |
c0e31d2e | 212 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
213 | { |
214 | switch (cmd) { | |
6dccd6c1 | 215 | case LTTNG_KERNEL_OLD_SESSION: |
38d024ae | 216 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 217 | return lttng_abi_create_session(); |
6dccd6c1 JD |
218 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
219 | { | |
220 | struct lttng_kernel_tracer_version v; | |
221 | struct lttng_kernel_old_tracer_version oldv; | |
222 | struct lttng_kernel_old_tracer_version *uversion = | |
223 | (struct lttng_kernel_old_tracer_version __user *) arg; | |
224 | ||
225 | lttng_abi_tracer_version(&v); | |
226 | oldv.major = v.major; | |
227 | oldv.minor = v.minor; | |
228 | oldv.patchlevel = v.patchlevel; | |
229 | ||
230 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) | |
231 | return -EFAULT; | |
232 | return 0; | |
233 | } | |
80c16bcf | 234 | case LTTNG_KERNEL_TRACER_VERSION: |
6dccd6c1 JD |
235 | { |
236 | struct lttng_kernel_tracer_version version; | |
237 | struct lttng_kernel_tracer_version *uversion = | |
238 | (struct lttng_kernel_tracer_version __user *) arg; | |
239 | ||
240 | lttng_abi_tracer_version(&version); | |
241 | ||
242 | if (copy_to_user(uversion, &version, sizeof(version))) | |
243 | return -EFAULT; | |
244 | return 0; | |
245 | } | |
246 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: | |
271b6681 MD |
247 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
248 | return lttng_abi_tracepoint_list(); | |
6dccd6c1 | 249 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
5f7f9078 MD |
250 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
251 | synchronize_trace(); | |
252 | return 0; | |
6dccd6c1 JD |
253 | case LTTNG_KERNEL_OLD_CALIBRATE: |
254 | { | |
255 | struct lttng_kernel_old_calibrate __user *ucalibrate = | |
256 | (struct lttng_kernel_old_calibrate __user *) arg; | |
257 | struct lttng_kernel_old_calibrate old_calibrate; | |
258 | struct lttng_kernel_calibrate calibrate; | |
259 | int ret; | |
260 | ||
261 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) | |
262 | return -EFAULT; | |
263 | calibrate.type = old_calibrate.type; | |
264 | ret = lttng_calibrate(&calibrate); | |
265 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) | |
266 | return -EFAULT; | |
267 | return ret; | |
268 | } | |
57105fc2 MD |
269 | case LTTNG_KERNEL_CALIBRATE: |
270 | { | |
3db41b2c MD |
271 | struct lttng_kernel_calibrate __user *ucalibrate = |
272 | (struct lttng_kernel_calibrate __user *) arg; | |
273 | struct lttng_kernel_calibrate calibrate; | |
57105fc2 MD |
274 | int ret; |
275 | ||
276 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) | |
277 | return -EFAULT; | |
278 | ret = lttng_calibrate(&calibrate); | |
279 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) | |
280 | return -EFAULT; | |
281 | return ret; | |
282 | } | |
ad1c05e1 MD |
283 | default: |
284 | return -ENOIOCTLCMD; | |
285 | } | |
286 | } | |
287 | ||
ad1c05e1 | 288 | static const struct file_operations lttng_fops = { |
a33c9927 | 289 | .owner = THIS_MODULE, |
ad1c05e1 MD |
290 | .unlocked_ioctl = lttng_ioctl, |
291 | #ifdef CONFIG_COMPAT | |
03037b98 | 292 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 293 | #endif |
11b5a3c2 | 294 | }; |
ad1c05e1 | 295 | |
5dbbdb43 | 296 | static |
c0e31d2e | 297 | int lttng_abi_create_channel(struct file *session_file, |
6dccd6c1 | 298 | struct lttng_kernel_channel *chan_param, |
5dbbdb43 | 299 | enum channel_type channel_type) |
baf20995 | 300 | { |
a90917c3 | 301 | struct lttng_session *session = session_file->private_data; |
88dfd899 | 302 | const struct file_operations *fops = NULL; |
5dbbdb43 | 303 | const char *transport_name; |
a90917c3 | 304 | struct lttng_channel *chan; |
c0e31d2e | 305 | struct file *chan_file; |
baf20995 | 306 | int chan_fd; |
ad1c05e1 | 307 | int ret = 0; |
baf20995 | 308 | |
1c25284c | 309 | chan_fd = get_unused_fd(); |
baf20995 MD |
310 | if (chan_fd < 0) { |
311 | ret = chan_fd; | |
312 | goto fd_error; | |
313 | } | |
88dfd899 MD |
314 | switch (channel_type) { |
315 | case PER_CPU_CHANNEL: | |
316 | fops = <tng_channel_fops; | |
317 | break; | |
318 | case METADATA_CHANNEL: | |
319 | fops = <tng_metadata_fops; | |
320 | break; | |
321 | } | |
322 | ||
c0e31d2e | 323 | chan_file = anon_inode_getfile("[lttng_channel]", |
88dfd899 | 324 | fops, |
03037b98 | 325 | NULL, O_RDWR); |
c0e31d2e MD |
326 | if (IS_ERR(chan_file)) { |
327 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
328 | goto file_error; |
329 | } | |
5dbbdb43 MD |
330 | switch (channel_type) { |
331 | case PER_CPU_CHANNEL: | |
6dccd6c1 JD |
332 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
333 | transport_name = chan_param->overwrite ? | |
96ba7208 | 334 | "relay-overwrite" : "relay-discard"; |
6dccd6c1 JD |
335 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
336 | transport_name = chan_param->overwrite ? | |
96ba7208 JD |
337 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
338 | } else { | |
339 | return -EINVAL; | |
340 | } | |
5dbbdb43 | 341 | break; |
5dbbdb43 | 342 | case METADATA_CHANNEL: |
6dccd6c1 | 343 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
96ba7208 | 344 | transport_name = "relay-metadata"; |
6dccd6c1 | 345 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
96ba7208 JD |
346 | transport_name = "relay-metadata-mmap"; |
347 | else | |
348 | return -EINVAL; | |
5dbbdb43 MD |
349 | break; |
350 | default: | |
351 | transport_name = "<unknown>"; | |
352 | break; | |
353 | } | |
03037b98 MD |
354 | /* |
355 | * We tolerate no failure path after channel creation. It will stay | |
356 | * invariant for the rest of the session. | |
357 | */ | |
a90917c3 | 358 | chan = lttng_channel_create(session, transport_name, NULL, |
6dccd6c1 JD |
359 | chan_param->subbuf_size, |
360 | chan_param->num_subbuf, | |
361 | chan_param->switch_timer_interval, | |
d83004aa JD |
362 | chan_param->read_timer_interval, |
363 | channel_type); | |
03037b98 | 364 | if (!chan) { |
f3d01b96 | 365 | ret = -EINVAL; |
03037b98 MD |
366 | goto chan_error; |
367 | } | |
11b5a3c2 | 368 | chan->file = chan_file; |
c0e31d2e MD |
369 | chan_file->private_data = chan; |
370 | fd_install(chan_fd, chan_file); | |
b0caa15a | 371 | atomic_long_inc(&session_file->f_count); |
ad1c05e1 | 372 | |
baf20995 MD |
373 | return chan_fd; |
374 | ||
03037b98 | 375 | chan_error: |
c0e31d2e | 376 | fput(chan_file); |
baf20995 MD |
377 | file_error: |
378 | put_unused_fd(chan_fd); | |
379 | fd_error: | |
baf20995 MD |
380 | return ret; |
381 | } | |
382 | ||
383 | /** | |
ad1c05e1 | 384 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 385 | * |
c0e31d2e | 386 | * @file: the file |
baf20995 MD |
387 | * @cmd: the command |
388 | * @arg: command arg | |
389 | * | |
390 | * This ioctl implements lttng commands: | |
38d024ae | 391 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 392 | * Returns a LTTng channel file descriptor |
e64957da MD |
393 | * LTTNG_KERNEL_ENABLE |
394 | * Enables tracing for a session (weak enable) | |
395 | * LTTNG_KERNEL_DISABLE | |
396 | * Disables tracing for a session (strong disable) | |
8070f5c0 MD |
397 | * LTTNG_KERNEL_METADATA |
398 | * Returns a LTTng metadata file descriptor | |
ad1c05e1 MD |
399 | * |
400 | * The returned channel will be deleted when its file descriptor is closed. | |
401 | */ | |
402 | static | |
c0e31d2e | 403 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 404 | { |
a90917c3 | 405 | struct lttng_session *session = file->private_data; |
c0e31d2e | 406 | |
ad1c05e1 | 407 | switch (cmd) { |
6dccd6c1 JD |
408 | case LTTNG_KERNEL_OLD_CHANNEL: |
409 | { | |
410 | struct lttng_kernel_channel chan_param; | |
411 | struct lttng_kernel_old_channel old_chan_param; | |
412 | ||
413 | if (copy_from_user(&old_chan_param, | |
414 | (struct lttng_kernel_old_channel __user *) arg, | |
415 | sizeof(struct lttng_kernel_old_channel))) | |
416 | return -EFAULT; | |
417 | chan_param.overwrite = old_chan_param.overwrite; | |
418 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
419 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
420 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
421 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
422 | chan_param.output = old_chan_param.output; | |
423 | ||
424 | return lttng_abi_create_channel(file, &chan_param, | |
425 | PER_CPU_CHANNEL); | |
426 | } | |
38d024ae | 427 | case LTTNG_KERNEL_CHANNEL: |
6dccd6c1 JD |
428 | { |
429 | struct lttng_kernel_channel chan_param; | |
430 | ||
431 | if (copy_from_user(&chan_param, | |
38d024ae | 432 | (struct lttng_kernel_channel __user *) arg, |
6dccd6c1 JD |
433 | sizeof(struct lttng_kernel_channel))) |
434 | return -EFAULT; | |
435 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 436 | PER_CPU_CHANNEL); |
6dccd6c1 JD |
437 | } |
438 | case LTTNG_KERNEL_OLD_SESSION_START: | |
439 | case LTTNG_KERNEL_OLD_ENABLE: | |
38d024ae | 440 | case LTTNG_KERNEL_SESSION_START: |
e64957da | 441 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 442 | return lttng_session_enable(session); |
6dccd6c1 JD |
443 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
444 | case LTTNG_KERNEL_OLD_DISABLE: | |
38d024ae | 445 | case LTTNG_KERNEL_SESSION_STOP: |
e64957da | 446 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 447 | return lttng_session_disable(session); |
6dccd6c1 JD |
448 | case LTTNG_KERNEL_OLD_METADATA: |
449 | { | |
450 | struct lttng_kernel_channel chan_param; | |
451 | struct lttng_kernel_old_channel old_chan_param; | |
452 | ||
453 | if (copy_from_user(&old_chan_param, | |
454 | (struct lttng_kernel_old_channel __user *) arg, | |
455 | sizeof(struct lttng_kernel_old_channel))) | |
456 | return -EFAULT; | |
457 | chan_param.overwrite = old_chan_param.overwrite; | |
458 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
459 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
460 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
461 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
462 | chan_param.output = old_chan_param.output; | |
463 | ||
464 | return lttng_abi_create_channel(file, &chan_param, | |
465 | METADATA_CHANNEL); | |
466 | } | |
38d024ae | 467 | case LTTNG_KERNEL_METADATA: |
6dccd6c1 JD |
468 | { |
469 | struct lttng_kernel_channel chan_param; | |
470 | ||
471 | if (copy_from_user(&chan_param, | |
472 | (struct lttng_kernel_channel __user *) arg, | |
473 | sizeof(struct lttng_kernel_channel))) | |
474 | return -EFAULT; | |
475 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 476 | METADATA_CHANNEL); |
6dccd6c1 | 477 | } |
ad1c05e1 MD |
478 | default: |
479 | return -ENOIOCTLCMD; | |
480 | } | |
481 | } | |
482 | ||
03037b98 MD |
483 | /* |
484 | * Called when the last file reference is dropped. | |
485 | * | |
486 | * Big fat note: channels and events are invariant for the whole session after | |
487 | * their creation. So this session destruction also destroys all channel and | |
488 | * event structures specific to this session (they are not destroyed when their | |
489 | * individual file is released). | |
490 | */ | |
ad1c05e1 | 491 | static |
03037b98 | 492 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 493 | { |
a90917c3 | 494 | struct lttng_session *session = file->private_data; |
c269fff4 MD |
495 | |
496 | if (session) | |
a90917c3 | 497 | lttng_session_destroy(session); |
11b5a3c2 | 498 | return 0; |
ad1c05e1 | 499 | } |
ad1c05e1 MD |
500 | |
501 | static const struct file_operations lttng_session_fops = { | |
a33c9927 | 502 | .owner = THIS_MODULE, |
03037b98 | 503 | .release = lttng_session_release, |
ad1c05e1 MD |
504 | .unlocked_ioctl = lttng_session_ioctl, |
505 | #ifdef CONFIG_COMPAT | |
03037b98 | 506 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 507 | #endif |
11b5a3c2 | 508 | }; |
ad1c05e1 | 509 | |
d83004aa JD |
510 | /** |
511 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation | |
512 | * @filp: the file | |
513 | * @wait: poll table | |
514 | * | |
515 | * Handles the poll operations for the metadata channels. | |
516 | */ | |
ad1c05e1 | 517 | static |
d83004aa JD |
518 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
519 | poll_table *wait) | |
520 | { | |
521 | struct lttng_metadata_stream *stream = filp->private_data; | |
522 | struct lib_ring_buffer *buf = stream->priv; | |
523 | int finalized; | |
524 | unsigned int mask = 0; | |
525 | ||
526 | if (filp->f_mode & FMODE_READ) { | |
527 | poll_wait_set_exclusive(wait); | |
528 | poll_wait(filp, &stream->read_wait, wait); | |
529 | ||
530 | finalized = stream->finalized; | |
531 | ||
532 | /* | |
533 | * lib_ring_buffer_is_finalized() contains a smp_rmb() | |
534 | * ordering finalized load before offsets loads. | |
535 | */ | |
536 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
537 | ||
538 | if (finalized) | |
539 | mask |= POLLHUP; | |
540 | ||
541 | if (stream->metadata_cache->metadata_written > | |
542 | stream->metadata_cache_read) | |
543 | mask |= POLLIN; | |
544 | } | |
545 | ||
546 | return mask; | |
547 | } | |
548 | ||
549 | static | |
550 | int lttng_metadata_ring_buffer_ioctl_get_subbuf(struct file *filp, | |
551 | unsigned int cmd, unsigned long arg) | |
552 | { | |
553 | struct lttng_metadata_stream *stream = filp->private_data; | |
554 | struct lib_ring_buffer *buf = stream->priv; | |
555 | struct channel *chan = buf->backend.chan; | |
556 | struct lttng_channel *lttng_chan = channel_get_private(chan); | |
557 | int ret; | |
558 | ||
559 | ret = lttng_metadata_output_channel(lttng_chan, stream); | |
560 | if (ret > 0) { | |
561 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
562 | ret = 0; | |
563 | } | |
564 | return ret; | |
565 | } | |
566 | ||
567 | static | |
568 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, | |
569 | unsigned int cmd, unsigned long arg) | |
570 | { | |
571 | int ret; | |
572 | struct lttng_metadata_stream *stream = filp->private_data; | |
573 | struct lib_ring_buffer *buf = stream->priv; | |
574 | ||
575 | switch (cmd) { | |
576 | case RING_BUFFER_GET_SUBBUF: | |
577 | case RING_BUFFER_GET_NEXT_SUBBUF: | |
578 | { | |
579 | ret = lttng_metadata_ring_buffer_ioctl_get_subbuf(filp, | |
580 | cmd, arg); | |
581 | if (ret < 0) | |
582 | goto err; | |
583 | break; | |
584 | } | |
585 | default: | |
586 | break; | |
587 | } | |
588 | /* Performing lib ring buffer ioctl after our own. */ | |
589 | return lib_ring_buffer_ioctl(filp, cmd, arg, buf); | |
590 | ||
591 | err: | |
592 | return ret; | |
593 | } | |
594 | ||
aeb9064d | 595 | #ifdef CONFIG_COMPAT |
d83004aa JD |
596 | static |
597 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, | |
598 | unsigned int cmd, unsigned long arg) | |
599 | { | |
600 | int ret; | |
601 | struct lttng_metadata_stream *stream = filp->private_data; | |
602 | struct lib_ring_buffer *buf = stream->priv; | |
603 | ||
604 | switch (cmd) { | |
605 | case RING_BUFFER_GET_SUBBUF: | |
606 | case RING_BUFFER_GET_NEXT_SUBBUF: | |
607 | { | |
608 | ret = lttng_metadata_ring_buffer_ioctl_get_subbuf(filp, | |
609 | cmd, arg); | |
610 | if (ret < 0) | |
611 | goto err; | |
612 | break; | |
613 | } | |
614 | default: | |
615 | break; | |
616 | } | |
617 | /* Performing lib ring buffer ioctl after our own. */ | |
618 | return lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); | |
619 | ||
620 | err: | |
621 | return ret; | |
622 | } | |
aeb9064d | 623 | #endif |
d83004aa JD |
624 | |
625 | static | |
626 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) | |
627 | { | |
628 | struct lttng_metadata_stream *stream = inode->i_private; | |
629 | struct lib_ring_buffer *buf = stream->priv; | |
630 | ||
631 | file->private_data = buf; | |
632 | return lib_ring_buffer_open(inode, file, buf); | |
633 | } | |
634 | ||
635 | static | |
636 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) | |
637 | { | |
638 | struct lttng_metadata_stream *stream = file->private_data; | |
639 | struct lib_ring_buffer *buf = stream->priv; | |
640 | ||
641 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); | |
642 | ||
643 | return lib_ring_buffer_release(inode, file, buf); | |
644 | } | |
645 | ||
646 | static | |
647 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
648 | struct pipe_inode_info *pipe, size_t len, | |
649 | unsigned int flags) | |
650 | { | |
651 | struct lttng_metadata_stream *stream = in->private_data; | |
652 | struct lib_ring_buffer *buf = stream->priv; | |
653 | ||
654 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, | |
655 | flags, buf); | |
656 | } | |
657 | ||
658 | static | |
659 | int lttng_metadata_ring_buffer_mmap(struct file *filp, | |
660 | struct vm_area_struct *vma) | |
661 | { | |
662 | struct lttng_metadata_stream *stream = filp->private_data; | |
663 | struct lib_ring_buffer *buf = stream->priv; | |
664 | ||
665 | return lib_ring_buffer_mmap(filp, vma, buf); | |
666 | } | |
667 | ||
668 | static | |
669 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { | |
670 | .owner = THIS_MODULE, | |
671 | .open = lttng_metadata_ring_buffer_open, | |
672 | .release = lttng_metadata_ring_buffer_release, | |
673 | .poll = lttng_metadata_ring_buffer_poll, | |
674 | .splice_read = lttng_metadata_ring_buffer_splice_read, | |
675 | .mmap = lttng_metadata_ring_buffer_mmap, | |
676 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, | |
677 | .llseek = vfs_lib_ring_buffer_no_llseek, | |
678 | #ifdef CONFIG_COMPAT | |
679 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, | |
680 | #endif | |
681 | }; | |
682 | ||
683 | static | |
684 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, | |
685 | const struct file_operations *fops) | |
ad1c05e1 | 686 | { |
ad1c05e1 | 687 | int stream_fd, ret; |
11b5a3c2 | 688 | struct file *stream_file; |
ad1c05e1 | 689 | |
1c25284c | 690 | stream_fd = get_unused_fd(); |
ad1c05e1 MD |
691 | if (stream_fd < 0) { |
692 | ret = stream_fd; | |
693 | goto fd_error; | |
694 | } | |
d83004aa JD |
695 | stream_file = anon_inode_getfile("[lttng_stream]", fops, |
696 | stream_priv, O_RDWR); | |
c0e31d2e MD |
697 | if (IS_ERR(stream_file)) { |
698 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
699 | goto file_error; |
700 | } | |
409453cb MD |
701 | /* |
702 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
703 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
704 | * file descriptor, so we set FMODE_PREAD here. | |
705 | */ | |
d7b6f197 | 706 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 707 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
708 | /* |
709 | * The stream holds a reference to the channel within the generic ring | |
710 | * buffer library, so no need to hold a refcount on the channel and | |
711 | * session files here. | |
712 | */ | |
ad1c05e1 MD |
713 | return stream_fd; |
714 | ||
715 | file_error: | |
716 | put_unused_fd(stream_fd); | |
d83004aa JD |
717 | fd_error: |
718 | return ret; | |
719 | } | |
720 | ||
721 | static | |
722 | int lttng_abi_open_stream(struct file *channel_file) | |
723 | { | |
724 | struct lttng_channel *channel = channel_file->private_data; | |
725 | struct lib_ring_buffer *buf; | |
726 | int ret; | |
727 | void *stream_priv; | |
728 | ||
729 | buf = channel->ops->buffer_read_open(channel->chan); | |
730 | if (!buf) | |
731 | return -ENOENT; | |
732 | ||
733 | stream_priv = buf; | |
734 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, | |
735 | &lib_ring_buffer_file_operations); | |
736 | if (ret < 0) | |
737 | goto fd_error; | |
738 | ||
739 | return ret; | |
740 | ||
741 | fd_error: | |
742 | channel->ops->buffer_read_close(buf); | |
743 | return ret; | |
744 | } | |
745 | ||
746 | static | |
747 | int lttng_abi_open_metadata_stream(struct file *channel_file) | |
748 | { | |
749 | struct lttng_channel *channel = channel_file->private_data; | |
750 | struct lttng_session *session = channel->session; | |
751 | struct lib_ring_buffer *buf; | |
752 | int ret; | |
753 | struct lttng_metadata_stream *metadata_stream; | |
754 | void *stream_priv; | |
755 | ||
756 | buf = channel->ops->buffer_read_open(channel->chan); | |
757 | if (!buf) | |
758 | return -ENOENT; | |
759 | ||
760 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), | |
761 | GFP_KERNEL); | |
762 | if (!metadata_stream) | |
763 | return -ENOMEM; | |
764 | metadata_stream->metadata_cache = session->metadata_cache; | |
765 | init_waitqueue_head(&metadata_stream->read_wait); | |
766 | metadata_stream->priv = buf; | |
767 | stream_priv = metadata_stream; | |
768 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, | |
769 | <tng_metadata_ring_buffer_file_operations); | |
770 | if (ret < 0) | |
771 | goto fd_error; | |
772 | ||
773 | kref_get(&session->metadata_cache->refcount); | |
774 | list_add(&metadata_stream->list, | |
775 | &session->metadata_cache->metadata_stream); | |
776 | return ret; | |
777 | ||
ad1c05e1 | 778 | fd_error: |
11b5a3c2 | 779 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
780 | return ret; |
781 | } | |
782 | ||
653fe716 | 783 | static |
c0e31d2e | 784 | int lttng_abi_create_event(struct file *channel_file, |
6dccd6c1 | 785 | struct lttng_kernel_event *event_param) |
653fe716 | 786 | { |
a90917c3 MD |
787 | struct lttng_channel *channel = channel_file->private_data; |
788 | struct lttng_event *event; | |
653fe716 | 789 | int event_fd, ret; |
11b5a3c2 | 790 | struct file *event_file; |
653fe716 | 791 | |
6dccd6c1 JD |
792 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
793 | switch (event_param->instrumentation) { | |
7371f44c | 794 | case LTTNG_KERNEL_KRETPROBE: |
6dccd6c1 | 795 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
7371f44c | 796 | break; |
ab2277d6 | 797 | case LTTNG_KERNEL_KPROBE: |
6dccd6c1 | 798 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 799 | break; |
ab2277d6 | 800 | case LTTNG_KERNEL_FUNCTION: |
6dccd6c1 | 801 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 MD |
802 | break; |
803 | default: | |
804 | break; | |
805 | } | |
6dccd6c1 | 806 | switch (event_param->instrumentation) { |
1ec65de1 MD |
807 | default: |
808 | event_fd = get_unused_fd(); | |
809 | if (event_fd < 0) { | |
810 | ret = event_fd; | |
811 | goto fd_error; | |
812 | } | |
813 | event_file = anon_inode_getfile("[lttng_event]", | |
814 | <tng_event_fops, | |
815 | NULL, O_RDWR); | |
816 | if (IS_ERR(event_file)) { | |
817 | ret = PTR_ERR(event_file); | |
818 | goto file_error; | |
819 | } | |
820 | /* | |
821 | * We tolerate no failure path after event creation. It | |
822 | * will stay invariant for the rest of the session. | |
823 | */ | |
6dccd6c1 | 824 | event = lttng_event_create(channel, event_param, NULL, NULL); |
1ec65de1 MD |
825 | if (!event) { |
826 | ret = -EINVAL; | |
827 | goto event_error; | |
828 | } | |
829 | event_file->private_data = event; | |
830 | fd_install(event_fd, event_file); | |
831 | /* The event holds a reference on the channel */ | |
832 | atomic_long_inc(&channel_file->f_count); | |
833 | break; | |
43880ee8 MD |
834 | case LTTNG_KERNEL_SYSCALL: |
835 | /* | |
836 | * Only all-syscall tracing supported for now. | |
837 | */ | |
6dccd6c1 | 838 | if (event_param->name[0] != '\0') |
43880ee8 | 839 | return -EINVAL; |
1ec65de1 MD |
840 | ret = lttng_syscalls_register(channel, NULL); |
841 | if (ret) | |
842 | goto fd_error; | |
843 | event_fd = 0; | |
844 | break; | |
03037b98 | 845 | } |
653fe716 MD |
846 | return event_fd; |
847 | ||
03037b98 | 848 | event_error: |
c0e31d2e | 849 | fput(event_file); |
653fe716 MD |
850 | file_error: |
851 | put_unused_fd(event_fd); | |
852 | fd_error: | |
653fe716 MD |
853 | return ret; |
854 | } | |
ad1c05e1 MD |
855 | |
856 | /** | |
857 | * lttng_channel_ioctl - lttng syscall through ioctl | |
858 | * | |
c0e31d2e | 859 | * @file: the file |
ad1c05e1 MD |
860 | * @cmd: the command |
861 | * @arg: command arg | |
862 | * | |
863 | * This ioctl implements lttng commands: | |
38d024ae | 864 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
865 | * Returns an event stream file descriptor or failure. |
866 | * (typically, one event stream records events from one CPU) | |
38d024ae | 867 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 868 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
869 | * LTTNG_KERNEL_CONTEXT |
870 | * Prepend a context field to each event in the channel | |
e64957da MD |
871 | * LTTNG_KERNEL_ENABLE |
872 | * Enable recording for events in this channel (weak enable) | |
873 | * LTTNG_KERNEL_DISABLE | |
874 | * Disable recording for events in this channel (strong disable) | |
baf20995 | 875 | * |
baf20995 MD |
876 | * Channel and event file descriptors also hold a reference on the session. |
877 | */ | |
ad1c05e1 | 878 | static |
c0e31d2e | 879 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 880 | { |
a90917c3 | 881 | struct lttng_channel *channel = file->private_data; |
8070f5c0 | 882 | |
baf20995 | 883 | switch (cmd) { |
6dccd6c1 | 884 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 885 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 886 | return lttng_abi_open_stream(file); |
6dccd6c1 JD |
887 | case LTTNG_KERNEL_OLD_EVENT: |
888 | { | |
889 | struct lttng_kernel_event *uevent_param; | |
890 | struct lttng_kernel_old_event *old_uevent_param; | |
891 | int ret; | |
892 | ||
893 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), | |
894 | GFP_KERNEL); | |
895 | if (!uevent_param) { | |
896 | ret = -ENOMEM; | |
897 | goto old_event_end; | |
898 | } | |
899 | old_uevent_param = kmalloc( | |
900 | sizeof(struct lttng_kernel_old_event), | |
901 | GFP_KERNEL); | |
902 | if (!old_uevent_param) { | |
903 | ret = -ENOMEM; | |
904 | goto old_event_error_free_param; | |
905 | } | |
906 | if (copy_from_user(old_uevent_param, | |
907 | (struct lttng_kernel_old_event __user *) arg, | |
908 | sizeof(struct lttng_kernel_old_event))) { | |
909 | ret = -EFAULT; | |
910 | goto old_event_error_free_old_param; | |
911 | } | |
912 | ||
913 | memcpy(uevent_param->name, old_uevent_param->name, | |
914 | sizeof(uevent_param->name)); | |
915 | uevent_param->instrumentation = | |
916 | old_uevent_param->instrumentation; | |
917 | ||
918 | switch (old_uevent_param->instrumentation) { | |
919 | case LTTNG_KERNEL_KPROBE: | |
920 | uevent_param->u.kprobe.addr = | |
921 | old_uevent_param->u.kprobe.addr; | |
922 | uevent_param->u.kprobe.offset = | |
923 | old_uevent_param->u.kprobe.offset; | |
924 | memcpy(uevent_param->u.kprobe.symbol_name, | |
925 | old_uevent_param->u.kprobe.symbol_name, | |
926 | sizeof(uevent_param->u.kprobe.symbol_name)); | |
927 | break; | |
928 | case LTTNG_KERNEL_KRETPROBE: | |
929 | uevent_param->u.kretprobe.addr = | |
930 | old_uevent_param->u.kretprobe.addr; | |
931 | uevent_param->u.kretprobe.offset = | |
932 | old_uevent_param->u.kretprobe.offset; | |
933 | memcpy(uevent_param->u.kretprobe.symbol_name, | |
934 | old_uevent_param->u.kretprobe.symbol_name, | |
935 | sizeof(uevent_param->u.kretprobe.symbol_name)); | |
936 | break; | |
937 | case LTTNG_KERNEL_FUNCTION: | |
938 | memcpy(uevent_param->u.ftrace.symbol_name, | |
939 | old_uevent_param->u.ftrace.symbol_name, | |
940 | sizeof(uevent_param->u.ftrace.symbol_name)); | |
941 | break; | |
942 | default: | |
943 | break; | |
944 | } | |
945 | ret = lttng_abi_create_event(file, uevent_param); | |
946 | ||
947 | old_event_error_free_old_param: | |
948 | kfree(old_uevent_param); | |
949 | old_event_error_free_param: | |
950 | kfree(uevent_param); | |
951 | old_event_end: | |
952 | return ret; | |
953 | } | |
38d024ae | 954 | case LTTNG_KERNEL_EVENT: |
6dccd6c1 JD |
955 | { |
956 | struct lttng_kernel_event uevent_param; | |
957 | ||
958 | if (copy_from_user(&uevent_param, | |
959 | (struct lttng_kernel_event __user *) arg, | |
960 | sizeof(uevent_param))) | |
961 | return -EFAULT; | |
962 | return lttng_abi_create_event(file, &uevent_param); | |
963 | } | |
964 | case LTTNG_KERNEL_OLD_CONTEXT: | |
965 | { | |
966 | struct lttng_kernel_context *ucontext_param; | |
967 | struct lttng_kernel_old_context *old_ucontext_param; | |
968 | int ret; | |
969 | ||
970 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), | |
971 | GFP_KERNEL); | |
972 | if (!ucontext_param) { | |
973 | ret = -ENOMEM; | |
974 | goto old_ctx_end; | |
975 | } | |
976 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), | |
977 | GFP_KERNEL); | |
978 | if (!old_ucontext_param) { | |
979 | ret = -ENOMEM; | |
980 | goto old_ctx_error_free_param; | |
981 | } | |
982 | ||
983 | if (copy_from_user(old_ucontext_param, | |
984 | (struct lttng_kernel_old_context __user *) arg, | |
985 | sizeof(struct lttng_kernel_old_context))) { | |
986 | ret = -EFAULT; | |
987 | goto old_ctx_error_free_old_param; | |
988 | } | |
989 | ucontext_param->ctx = old_ucontext_param->ctx; | |
990 | memcpy(ucontext_param->padding, old_ucontext_param->padding, | |
991 | sizeof(ucontext_param->padding)); | |
992 | /* only type that uses the union */ | |
993 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { | |
994 | ucontext_param->u.perf_counter.type = | |
995 | old_ucontext_param->u.perf_counter.type; | |
996 | ucontext_param->u.perf_counter.config = | |
997 | old_ucontext_param->u.perf_counter.config; | |
998 | memcpy(ucontext_param->u.perf_counter.name, | |
999 | old_ucontext_param->u.perf_counter.name, | |
1000 | sizeof(ucontext_param->u.perf_counter.name)); | |
1001 | } | |
1002 | ||
1003 | ret = lttng_abi_add_context(file, | |
1004 | ucontext_param, | |
1005 | &channel->ctx, channel->session); | |
1006 | ||
1007 | old_ctx_error_free_old_param: | |
1008 | kfree(old_ucontext_param); | |
1009 | old_ctx_error_free_param: | |
1010 | kfree(ucontext_param); | |
1011 | old_ctx_end: | |
1012 | return ret; | |
1013 | } | |
8070f5c0 | 1014 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 JD |
1015 | { |
1016 | struct lttng_kernel_context ucontext_param; | |
1017 | ||
1018 | if (copy_from_user(&ucontext_param, | |
8070f5c0 | 1019 | (struct lttng_kernel_context __user *) arg, |
6dccd6c1 JD |
1020 | sizeof(ucontext_param))) |
1021 | return -EFAULT; | |
1022 | return lttng_abi_add_context(file, | |
1023 | &ucontext_param, | |
8070f5c0 | 1024 | &channel->ctx, channel->session); |
6dccd6c1 JD |
1025 | } |
1026 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1027 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 1028 | return lttng_channel_enable(channel); |
6dccd6c1 | 1029 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1030 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 1031 | return lttng_channel_disable(channel); |
baf20995 MD |
1032 | default: |
1033 | return -ENOIOCTLCMD; | |
1034 | } | |
6dccd6c1 | 1035 | |
baf20995 MD |
1036 | } |
1037 | ||
5dbbdb43 MD |
1038 | /** |
1039 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
1040 | * | |
1041 | * @file: the file | |
1042 | * @cmd: the command | |
1043 | * @arg: command arg | |
1044 | * | |
1045 | * This ioctl implements lttng commands: | |
38d024ae | 1046 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
1047 | * Returns an event stream file descriptor or failure. |
1048 | * | |
1049 | * Channel and event file descriptors also hold a reference on the session. | |
1050 | */ | |
1051 | static | |
1052 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1053 | { | |
1054 | switch (cmd) { | |
6dccd6c1 | 1055 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 1056 | case LTTNG_KERNEL_STREAM: |
d83004aa | 1057 | return lttng_abi_open_metadata_stream(file); |
5dbbdb43 MD |
1058 | default: |
1059 | return -ENOIOCTLCMD; | |
1060 | } | |
1061 | } | |
1062 | ||
653fe716 MD |
1063 | /** |
1064 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
1065 | * | |
c0e31d2e | 1066 | * @file: the file |
653fe716 MD |
1067 | * @wait: poll table |
1068 | */ | |
c0e31d2e | 1069 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 1070 | { |
a90917c3 | 1071 | struct lttng_channel *channel = file->private_data; |
653fe716 MD |
1072 | unsigned int mask = 0; |
1073 | ||
c0e31d2e | 1074 | if (file->f_mode & FMODE_READ) { |
a33e44a6 | 1075 | poll_wait_set_exclusive(wait); |
24cedcfe MD |
1076 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
1077 | wait); | |
653fe716 | 1078 | |
254ec7bc MD |
1079 | if (channel->ops->is_disabled(channel->chan)) |
1080 | return POLLERR; | |
24cedcfe | 1081 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 | 1082 | return POLLHUP; |
f71ecafa | 1083 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
653fe716 | 1084 | return POLLIN | POLLRDNORM; |
f71ecafa | 1085 | return 0; |
653fe716 MD |
1086 | } |
1087 | return mask; | |
1088 | ||
1089 | } | |
1090 | ||
0a84a57f MD |
1091 | static |
1092 | int lttng_channel_release(struct inode *inode, struct file *file) | |
1093 | { | |
a90917c3 | 1094 | struct lttng_channel *channel = file->private_data; |
c269fff4 MD |
1095 | |
1096 | if (channel) | |
1097 | fput(channel->session->file); | |
0a84a57f MD |
1098 | return 0; |
1099 | } | |
1100 | ||
d83004aa JD |
1101 | static |
1102 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) | |
1103 | { | |
1104 | struct lttng_channel *channel = file->private_data; | |
1105 | ||
1106 | if (channel) { | |
1107 | lttng_metadata_channel_destroy(channel); | |
1108 | fput(channel->session->file); | |
1109 | } | |
1110 | ||
1111 | return 0; | |
1112 | } | |
1113 | ||
ad1c05e1 | 1114 | static const struct file_operations lttng_channel_fops = { |
a33c9927 | 1115 | .owner = THIS_MODULE, |
03037b98 | 1116 | .release = lttng_channel_release, |
653fe716 | 1117 | .poll = lttng_channel_poll, |
ad1c05e1 | 1118 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 1119 | #ifdef CONFIG_COMPAT |
03037b98 | 1120 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 1121 | #endif |
11b5a3c2 | 1122 | }; |
baf20995 | 1123 | |
5dbbdb43 | 1124 | static const struct file_operations lttng_metadata_fops = { |
a33c9927 | 1125 | .owner = THIS_MODULE, |
d83004aa | 1126 | .release = lttng_metadata_channel_release, |
5dbbdb43 MD |
1127 | .unlocked_ioctl = lttng_metadata_ioctl, |
1128 | #ifdef CONFIG_COMPAT | |
1129 | .compat_ioctl = lttng_metadata_ioctl, | |
1130 | #endif | |
1131 | }; | |
1132 | ||
8070f5c0 MD |
1133 | /** |
1134 | * lttng_event_ioctl - lttng syscall through ioctl | |
1135 | * | |
1136 | * @file: the file | |
1137 | * @cmd: the command | |
1138 | * @arg: command arg | |
1139 | * | |
1140 | * This ioctl implements lttng commands: | |
8070f5c0 MD |
1141 | * LTTNG_KERNEL_CONTEXT |
1142 | * Prepend a context field to each record of this event | |
e64957da MD |
1143 | * LTTNG_KERNEL_ENABLE |
1144 | * Enable recording for this event (weak enable) | |
1145 | * LTTNG_KERNEL_DISABLE | |
1146 | * Disable recording for this event (strong disable) | |
8070f5c0 MD |
1147 | */ |
1148 | static | |
1149 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1150 | { | |
a90917c3 | 1151 | struct lttng_event *event = file->private_data; |
8070f5c0 MD |
1152 | |
1153 | switch (cmd) { | |
6dccd6c1 JD |
1154 | case LTTNG_KERNEL_OLD_CONTEXT: |
1155 | { | |
1156 | struct lttng_kernel_context *ucontext_param; | |
1157 | struct lttng_kernel_old_context *old_ucontext_param; | |
1158 | int ret; | |
1159 | ||
1160 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), | |
1161 | GFP_KERNEL); | |
1162 | if (!ucontext_param) { | |
1163 | ret = -ENOMEM; | |
1164 | goto old_ctx_end; | |
1165 | } | |
1166 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), | |
1167 | GFP_KERNEL); | |
1168 | if (!old_ucontext_param) { | |
1169 | ret = -ENOMEM; | |
1170 | goto old_ctx_error_free_param; | |
1171 | } | |
1172 | ||
1173 | if (copy_from_user(old_ucontext_param, | |
1174 | (struct lttng_kernel_old_context __user *) arg, | |
1175 | sizeof(struct lttng_kernel_old_context))) { | |
1176 | ret = -EFAULT; | |
1177 | goto old_ctx_error_free_old_param; | |
1178 | } | |
1179 | ucontext_param->ctx = old_ucontext_param->ctx; | |
1180 | memcpy(ucontext_param->padding, old_ucontext_param->padding, | |
1181 | sizeof(ucontext_param->padding)); | |
1182 | /* only type that uses the union */ | |
1183 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { | |
1184 | ucontext_param->u.perf_counter.type = | |
1185 | old_ucontext_param->u.perf_counter.type; | |
1186 | ucontext_param->u.perf_counter.config = | |
1187 | old_ucontext_param->u.perf_counter.config; | |
1188 | memcpy(ucontext_param->u.perf_counter.name, | |
1189 | old_ucontext_param->u.perf_counter.name, | |
1190 | sizeof(ucontext_param->u.perf_counter.name)); | |
1191 | } | |
1192 | ||
1193 | ret = lttng_abi_add_context(file, | |
1194 | ucontext_param, | |
1195 | &event->ctx, event->chan->session); | |
1196 | ||
1197 | old_ctx_error_free_old_param: | |
1198 | kfree(old_ucontext_param); | |
1199 | old_ctx_error_free_param: | |
1200 | kfree(ucontext_param); | |
1201 | old_ctx_end: | |
1202 | return ret; | |
1203 | } | |
8070f5c0 | 1204 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 JD |
1205 | { |
1206 | struct lttng_kernel_context ucontext_param; | |
1207 | ||
1208 | if (copy_from_user(&ucontext_param, | |
1209 | (struct lttng_kernel_context __user *) arg, | |
1210 | sizeof(ucontext_param))) | |
1211 | return -EFAULT; | |
8070f5c0 | 1212 | return lttng_abi_add_context(file, |
6dccd6c1 | 1213 | &ucontext_param, |
8070f5c0 | 1214 | &event->ctx, event->chan->session); |
6dccd6c1 JD |
1215 | } |
1216 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1217 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 1218 | return lttng_event_enable(event); |
6dccd6c1 | 1219 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1220 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 1221 | return lttng_event_disable(event); |
8070f5c0 MD |
1222 | default: |
1223 | return -ENOIOCTLCMD; | |
1224 | } | |
1225 | } | |
1226 | ||
0a84a57f MD |
1227 | static |
1228 | int lttng_event_release(struct inode *inode, struct file *file) | |
1229 | { | |
a90917c3 | 1230 | struct lttng_event *event = file->private_data; |
c269fff4 | 1231 | |
aa7c23a9 | 1232 | if (event) |
c269fff4 | 1233 | fput(event->chan->file); |
0a84a57f MD |
1234 | return 0; |
1235 | } | |
1236 | ||
3b923e5b | 1237 | /* TODO: filter control ioctl */ |
0a84a57f | 1238 | static const struct file_operations lttng_event_fops = { |
a33c9927 | 1239 | .owner = THIS_MODULE, |
0a84a57f | 1240 | .release = lttng_event_release, |
8070f5c0 MD |
1241 | .unlocked_ioctl = lttng_event_ioctl, |
1242 | #ifdef CONFIG_COMPAT | |
1243 | .compat_ioctl = lttng_event_ioctl, | |
1244 | #endif | |
11b5a3c2 | 1245 | }; |
0a84a57f | 1246 | |
80996790 | 1247 | int __init lttng_abi_init(void) |
baf20995 MD |
1248 | { |
1249 | int ret = 0; | |
1250 | ||
6d2a620c | 1251 | wrapper_vmalloc_sync_all(); |
d29348f7 | 1252 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
e6a17f26 MD |
1253 | <tng_fops, NULL); |
1254 | ||
255e52a4 | 1255 | if (!lttng_proc_dentry) { |
baf20995 MD |
1256 | printk(KERN_ERR "Error creating LTTng control file\n"); |
1257 | ret = -ENOMEM; | |
1258 | goto error; | |
1259 | } | |
1260 | error: | |
1261 | return ret; | |
1262 | } | |
1263 | ||
80996790 | 1264 | void __exit lttng_abi_exit(void) |
baf20995 | 1265 | { |
e6a17f26 MD |
1266 | if (lttng_proc_dentry) |
1267 | remove_proc_entry("lttng", NULL); | |
baf20995 | 1268 | } |