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