| 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 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/anon_inodes.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 32 | #include "wrapper/ringbuffer/vfs.h" |
| 33 | #include "ltt-debugfs-abi.h" |
| 34 | #include "ltt-events.h" |
| 35 | #include "ltt-tracer.h" |
| 36 | |
| 37 | /* |
| 38 | * This is LTTng's own personal way to create a system call as an external |
| 39 | * module. We use ioctl() on /sys/kernel/debug/lttng. |
| 40 | */ |
| 41 | |
| 42 | static struct dentry *lttng_dentry; |
| 43 | static const struct file_operations lttng_fops; |
| 44 | static const struct file_operations lttng_session_fops; |
| 45 | static const struct file_operations lttng_channel_fops; |
| 46 | static const struct file_operations lttng_metadata_fops; |
| 47 | static const struct file_operations lttng_event_fops; |
| 48 | |
| 49 | enum channel_type { |
| 50 | PER_CPU_CHANNEL, |
| 51 | METADATA_CHANNEL, |
| 52 | }; |
| 53 | |
| 54 | static |
| 55 | int lttng_abi_create_session(void) |
| 56 | { |
| 57 | struct ltt_session *session; |
| 58 | struct file *session_file; |
| 59 | int session_fd, ret; |
| 60 | |
| 61 | session = ltt_session_create(); |
| 62 | if (!session) |
| 63 | return -ENOMEM; |
| 64 | session_fd = get_unused_fd(); |
| 65 | if (session_fd < 0) { |
| 66 | ret = session_fd; |
| 67 | goto fd_error; |
| 68 | } |
| 69 | session_file = anon_inode_getfile("[lttng_session]", |
| 70 | <tng_session_fops, |
| 71 | session, O_RDWR); |
| 72 | if (IS_ERR(session_file)) { |
| 73 | ret = PTR_ERR(session_file); |
| 74 | goto file_error; |
| 75 | } |
| 76 | session->file = session_file; |
| 77 | fd_install(session_fd, session_file); |
| 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 | |
| 87 | static |
| 88 | int lttng_abi_tracepoint_list(void) |
| 89 | { |
| 90 | struct file *tracepoint_list_file; |
| 91 | int file_fd, ret; |
| 92 | |
| 93 | file_fd = get_unused_fd(); |
| 94 | if (file_fd < 0) { |
| 95 | ret = file_fd; |
| 96 | goto fd_error; |
| 97 | } |
| 98 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
| 99 | <tng_tracepoint_list_fops, |
| 100 | NULL, O_RDWR); |
| 101 | if (IS_ERR(tracepoint_list_file)) { |
| 102 | ret = PTR_ERR(tracepoint_list_file); |
| 103 | goto file_error; |
| 104 | } |
| 105 | fd_install(file_fd, tracepoint_list_file); |
| 106 | return file_fd; |
| 107 | |
| 108 | file_error: |
| 109 | put_unused_fd(file_fd); |
| 110 | fd_error: |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static |
| 115 | long lttng_abi_tracer_version(struct file *file, |
| 116 | struct lttng_kernel_tracer_version __user *uversion_param) |
| 117 | { |
| 118 | struct lttng_kernel_tracer_version v; |
| 119 | |
| 120 | v.version = LTTNG_VERSION; |
| 121 | v.patchlevel = LTTNG_PATCHLEVEL; |
| 122 | v.sublevel = LTTNG_SUBLEVEL; |
| 123 | |
| 124 | if (copy_to_user(uversion_param, &v, sizeof(v))) |
| 125 | return -EFAULT; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * lttng_ioctl - lttng syscall through ioctl |
| 131 | * |
| 132 | * @file: the file |
| 133 | * @cmd: the command |
| 134 | * @arg: command arg |
| 135 | * |
| 136 | * This ioctl implements lttng commands: |
| 137 | * LTTNG_KERNEL_SESSION |
| 138 | * Returns a LTTng trace session file descriptor |
| 139 | * LTTNG_KERNEL_TRACER_VERSION |
| 140 | * Returns the LTTng kernel tracer version |
| 141 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 142 | * Returns a file descriptor listing available tracepoints |
| 143 | * |
| 144 | * The returned session will be deleted when its file descriptor is closed. |
| 145 | */ |
| 146 | static |
| 147 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 148 | { |
| 149 | switch (cmd) { |
| 150 | case LTTNG_KERNEL_SESSION: |
| 151 | return lttng_abi_create_session(); |
| 152 | case LTTNG_KERNEL_TRACER_VERSION: |
| 153 | return lttng_abi_tracer_version(file, |
| 154 | (struct lttng_kernel_tracer_version __user *) arg); |
| 155 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 156 | return lttng_abi_tracepoint_list(); |
| 157 | default: |
| 158 | return -ENOIOCTLCMD; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static const struct file_operations lttng_fops = { |
| 163 | .unlocked_ioctl = lttng_ioctl, |
| 164 | #ifdef CONFIG_COMPAT |
| 165 | .compat_ioctl = lttng_ioctl, |
| 166 | #endif |
| 167 | }; |
| 168 | |
| 169 | /* |
| 170 | * We tolerate no failure in this function (if one happens, we print a dmesg |
| 171 | * error, but cannot return any error, because the channel information is |
| 172 | * invariant. |
| 173 | */ |
| 174 | static |
| 175 | void lttng_metadata_create_events(struct file *channel_file) |
| 176 | { |
| 177 | struct ltt_channel *channel = channel_file->private_data; |
| 178 | static struct lttng_kernel_event metadata_params = { |
| 179 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
| 180 | .name = "lttng_metadata", |
| 181 | }; |
| 182 | struct ltt_event *event; |
| 183 | int ret; |
| 184 | |
| 185 | /* |
| 186 | * We tolerate no failure path after event creation. It will stay |
| 187 | * invariant for the rest of the session. |
| 188 | */ |
| 189 | event = ltt_event_create(channel, &metadata_params, NULL); |
| 190 | if (!event) { |
| 191 | ret = -EINVAL; |
| 192 | goto create_error; |
| 193 | } |
| 194 | return; |
| 195 | |
| 196 | create_error: |
| 197 | WARN_ON(1); |
| 198 | return; /* not allowed to return error */ |
| 199 | } |
| 200 | |
| 201 | static |
| 202 | int lttng_abi_create_channel(struct file *session_file, |
| 203 | struct lttng_kernel_channel __user *uchan_param, |
| 204 | enum channel_type channel_type) |
| 205 | { |
| 206 | struct ltt_session *session = session_file->private_data; |
| 207 | const struct file_operations *fops; |
| 208 | const char *transport_name; |
| 209 | struct ltt_channel *chan; |
| 210 | struct file *chan_file; |
| 211 | struct lttng_kernel_channel chan_param; |
| 212 | int chan_fd; |
| 213 | int ret = 0; |
| 214 | |
| 215 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
| 216 | return -EFAULT; |
| 217 | chan_fd = get_unused_fd(); |
| 218 | if (chan_fd < 0) { |
| 219 | ret = chan_fd; |
| 220 | goto fd_error; |
| 221 | } |
| 222 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 223 | <tng_channel_fops, |
| 224 | NULL, O_RDWR); |
| 225 | if (IS_ERR(chan_file)) { |
| 226 | ret = PTR_ERR(chan_file); |
| 227 | goto file_error; |
| 228 | } |
| 229 | switch (channel_type) { |
| 230 | case PER_CPU_CHANNEL: |
| 231 | transport_name = chan_param.overwrite ? |
| 232 | "relay-overwrite" : "relay-discard"; |
| 233 | fops = <tng_channel_fops; |
| 234 | break; |
| 235 | case METADATA_CHANNEL: |
| 236 | transport_name = "relay-metadata"; |
| 237 | fops = <tng_metadata_fops; |
| 238 | break; |
| 239 | default: |
| 240 | transport_name = "<unknown>"; |
| 241 | break; |
| 242 | } |
| 243 | /* |
| 244 | * We tolerate no failure path after channel creation. It will stay |
| 245 | * invariant for the rest of the session. |
| 246 | */ |
| 247 | chan = ltt_channel_create(session, transport_name, NULL, |
| 248 | chan_param.subbuf_size, |
| 249 | chan_param.num_subbuf, |
| 250 | chan_param.switch_timer_interval, |
| 251 | chan_param.read_timer_interval); |
| 252 | if (!chan) { |
| 253 | ret = -EINVAL; |
| 254 | goto chan_error; |
| 255 | } |
| 256 | chan->file = chan_file; |
| 257 | chan_file->private_data = chan; |
| 258 | fd_install(chan_fd, chan_file); |
| 259 | if (channel_type == METADATA_CHANNEL) { |
| 260 | session->metadata = chan; |
| 261 | lttng_metadata_create_events(chan_file); |
| 262 | } |
| 263 | |
| 264 | /* The channel created holds a reference on the session */ |
| 265 | atomic_long_inc(&session_file->f_count); |
| 266 | |
| 267 | return chan_fd; |
| 268 | |
| 269 | chan_error: |
| 270 | fput(chan_file); |
| 271 | file_error: |
| 272 | put_unused_fd(chan_fd); |
| 273 | fd_error: |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * lttng_session_ioctl - lttng session fd ioctl |
| 279 | * |
| 280 | * @file: the file |
| 281 | * @cmd: the command |
| 282 | * @arg: command arg |
| 283 | * |
| 284 | * This ioctl implements lttng commands: |
| 285 | * LTTNG_KERNEL_CHANNEL |
| 286 | * Returns a LTTng channel file descriptor |
| 287 | * |
| 288 | * The returned channel will be deleted when its file descriptor is closed. |
| 289 | */ |
| 290 | static |
| 291 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 292 | { |
| 293 | struct ltt_session *session = file->private_data; |
| 294 | |
| 295 | switch (cmd) { |
| 296 | case LTTNG_KERNEL_CHANNEL: |
| 297 | return lttng_abi_create_channel(file, |
| 298 | (struct lttng_kernel_channel __user *) arg, |
| 299 | PER_CPU_CHANNEL); |
| 300 | case LTTNG_KERNEL_SESSION_START: |
| 301 | return ltt_session_start(session); |
| 302 | case LTTNG_KERNEL_SESSION_STOP: |
| 303 | return ltt_session_stop(session); |
| 304 | case LTTNG_KERNEL_METADATA: |
| 305 | return lttng_abi_create_channel(file, |
| 306 | (struct lttng_kernel_channel __user *) arg, |
| 307 | METADATA_CHANNEL); |
| 308 | default: |
| 309 | return -ENOIOCTLCMD; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Called when the last file reference is dropped. |
| 315 | * |
| 316 | * Big fat note: channels and events are invariant for the whole session after |
| 317 | * their creation. So this session destruction also destroys all channel and |
| 318 | * event structures specific to this session (they are not destroyed when their |
| 319 | * individual file is released). |
| 320 | */ |
| 321 | static |
| 322 | int lttng_session_release(struct inode *inode, struct file *file) |
| 323 | { |
| 324 | struct ltt_session *session = file->private_data; |
| 325 | |
| 326 | if (session) |
| 327 | ltt_session_destroy(session); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static const struct file_operations lttng_session_fops = { |
| 332 | .release = lttng_session_release, |
| 333 | .unlocked_ioctl = lttng_session_ioctl, |
| 334 | #ifdef CONFIG_COMPAT |
| 335 | .compat_ioctl = lttng_session_ioctl, |
| 336 | #endif |
| 337 | }; |
| 338 | |
| 339 | static |
| 340 | int lttng_abi_open_stream(struct file *channel_file) |
| 341 | { |
| 342 | struct ltt_channel *channel = channel_file->private_data; |
| 343 | struct lib_ring_buffer *buf; |
| 344 | int stream_fd, ret; |
| 345 | struct file *stream_file; |
| 346 | |
| 347 | buf = channel->ops->buffer_read_open(channel->chan); |
| 348 | if (!buf) |
| 349 | return -ENOENT; |
| 350 | |
| 351 | stream_fd = get_unused_fd(); |
| 352 | if (stream_fd < 0) { |
| 353 | ret = stream_fd; |
| 354 | goto fd_error; |
| 355 | } |
| 356 | stream_file = anon_inode_getfile("[lttng_stream]", |
| 357 | &lib_ring_buffer_file_operations, |
| 358 | buf, O_RDWR); |
| 359 | if (IS_ERR(stream_file)) { |
| 360 | ret = PTR_ERR(stream_file); |
| 361 | goto file_error; |
| 362 | } |
| 363 | /* |
| 364 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 365 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 366 | * file descriptor, so we set FMODE_PREAD here. |
| 367 | */ |
| 368 | stream_file->f_mode |= FMODE_PREAD; |
| 369 | fd_install(stream_fd, stream_file); |
| 370 | /* |
| 371 | * The stream holds a reference to the channel within the generic ring |
| 372 | * buffer library, so no need to hold a refcount on the channel and |
| 373 | * session files here. |
| 374 | */ |
| 375 | return stream_fd; |
| 376 | |
| 377 | file_error: |
| 378 | put_unused_fd(stream_fd); |
| 379 | fd_error: |
| 380 | channel->ops->buffer_read_close(buf); |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static |
| 385 | int lttng_abi_create_event(struct file *channel_file, |
| 386 | struct lttng_kernel_event __user *uevent_param) |
| 387 | { |
| 388 | struct ltt_channel *channel = channel_file->private_data; |
| 389 | struct ltt_event *event; |
| 390 | struct lttng_kernel_event event_param; |
| 391 | int event_fd, ret; |
| 392 | struct file *event_file; |
| 393 | |
| 394 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) |
| 395 | return -EFAULT; |
| 396 | event_param.name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
| 397 | switch (event_param.instrumentation) { |
| 398 | case LTTNG_KERNEL_KPROBE: |
| 399 | event_param.u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
| 400 | break; |
| 401 | case LTTNG_KERNEL_FUNCTION: |
| 402 | event_param.u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0'; |
| 403 | break; |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | event_fd = get_unused_fd(); |
| 408 | if (event_fd < 0) { |
| 409 | ret = event_fd; |
| 410 | goto fd_error; |
| 411 | } |
| 412 | event_file = anon_inode_getfile("[lttng_event]", |
| 413 | <tng_event_fops, |
| 414 | NULL, O_RDWR); |
| 415 | if (IS_ERR(event_file)) { |
| 416 | ret = PTR_ERR(event_file); |
| 417 | goto file_error; |
| 418 | } |
| 419 | /* |
| 420 | * We tolerate no failure path after event creation. It will stay |
| 421 | * invariant for the rest of the session. |
| 422 | */ |
| 423 | event = ltt_event_create(channel, &event_param, NULL); |
| 424 | if (!event) { |
| 425 | ret = -EINVAL; |
| 426 | goto event_error; |
| 427 | } |
| 428 | event_file->private_data = event; |
| 429 | fd_install(event_fd, event_file); |
| 430 | /* The event holds a reference on the channel */ |
| 431 | atomic_long_inc(&channel_file->f_count); |
| 432 | return event_fd; |
| 433 | |
| 434 | event_error: |
| 435 | fput(event_file); |
| 436 | file_error: |
| 437 | put_unused_fd(event_fd); |
| 438 | fd_error: |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 444 | * |
| 445 | * @file: the file |
| 446 | * @cmd: the command |
| 447 | * @arg: command arg |
| 448 | * |
| 449 | * This ioctl implements lttng commands: |
| 450 | * LTTNG_KERNEL_STREAM |
| 451 | * Returns an event stream file descriptor or failure. |
| 452 | * (typically, one event stream records events from one CPU) |
| 453 | * LTTNG_KERNEL_EVENT |
| 454 | * Returns an event file descriptor or failure. |
| 455 | * |
| 456 | * Channel and event file descriptors also hold a reference on the session. |
| 457 | */ |
| 458 | static |
| 459 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 460 | { |
| 461 | switch (cmd) { |
| 462 | case LTTNG_KERNEL_STREAM: |
| 463 | return lttng_abi_open_stream(file); |
| 464 | case LTTNG_KERNEL_EVENT: |
| 465 | return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg); |
| 466 | default: |
| 467 | return -ENOIOCTLCMD; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 473 | * |
| 474 | * @file: the file |
| 475 | * @cmd: the command |
| 476 | * @arg: command arg |
| 477 | * |
| 478 | * This ioctl implements lttng commands: |
| 479 | * LTTNG_KERNEL_STREAM |
| 480 | * Returns an event stream file descriptor or failure. |
| 481 | * |
| 482 | * Channel and event file descriptors also hold a reference on the session. |
| 483 | */ |
| 484 | static |
| 485 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 486 | { |
| 487 | switch (cmd) { |
| 488 | case LTTNG_KERNEL_STREAM: |
| 489 | return lttng_abi_open_stream(file); |
| 490 | default: |
| 491 | return -ENOIOCTLCMD; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* TODO: poll */ |
| 496 | #if 0 |
| 497 | /** |
| 498 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 499 | * |
| 500 | * @file: the file |
| 501 | * @wait: poll table |
| 502 | */ |
| 503 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 504 | { |
| 505 | struct ltt_channel *channel = file->private_data; |
| 506 | unsigned int mask = 0; |
| 507 | |
| 508 | if (file->f_mode & FMODE_READ) { |
| 509 | poll_wait_set_exclusive(wait); |
| 510 | poll_wait(file, &channel->notify_wait, wait); |
| 511 | |
| 512 | /* TODO: identify when the channel is being finalized. */ |
| 513 | if (finalized) |
| 514 | return POLLHUP; |
| 515 | else |
| 516 | return POLLIN | POLLRDNORM; |
| 517 | } |
| 518 | return mask; |
| 519 | |
| 520 | } |
| 521 | #endif //0 |
| 522 | |
| 523 | static |
| 524 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 525 | { |
| 526 | struct ltt_channel *channel = file->private_data; |
| 527 | |
| 528 | if (channel) |
| 529 | fput(channel->session->file); |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static const struct file_operations lttng_channel_fops = { |
| 534 | .release = lttng_channel_release, |
| 535 | /* TODO */ |
| 536 | #if 0 |
| 537 | .poll = lttng_channel_poll, |
| 538 | #endif //0 |
| 539 | .unlocked_ioctl = lttng_channel_ioctl, |
| 540 | #ifdef CONFIG_COMPAT |
| 541 | .compat_ioctl = lttng_channel_ioctl, |
| 542 | #endif |
| 543 | }; |
| 544 | |
| 545 | static const struct file_operations lttng_metadata_fops = { |
| 546 | .release = lttng_channel_release, |
| 547 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 548 | #ifdef CONFIG_COMPAT |
| 549 | .compat_ioctl = lttng_metadata_ioctl, |
| 550 | #endif |
| 551 | }; |
| 552 | |
| 553 | static |
| 554 | int lttng_event_release(struct inode *inode, struct file *file) |
| 555 | { |
| 556 | struct ltt_event *event = file->private_data; |
| 557 | |
| 558 | if (event) |
| 559 | fput(event->chan->file); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | /* TODO: filter control ioctl */ |
| 564 | static const struct file_operations lttng_event_fops = { |
| 565 | .release = lttng_event_release, |
| 566 | }; |
| 567 | |
| 568 | int __init ltt_debugfs_abi_init(void) |
| 569 | { |
| 570 | int ret = 0; |
| 571 | |
| 572 | wrapper_vmalloc_sync_all(); |
| 573 | lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL, |
| 574 | <tng_fops); |
| 575 | if (IS_ERR(lttng_dentry) || !lttng_dentry) { |
| 576 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 577 | ret = -ENOMEM; |
| 578 | goto error; |
| 579 | } |
| 580 | error: |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | void __exit ltt_debugfs_abi_exit(void) |
| 585 | { |
| 586 | debugfs_remove(lttng_dentry); |
| 587 | } |