| 1 | /* |
| 2 | * lttng-abi.c |
| 3 | * |
| 4 | * LTTng ABI |
| 5 | * |
| 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 | * |
| 23 | * Mimic system calls for: |
| 24 | * - session creation, returns a file descriptor or failure. |
| 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. |
| 38 | */ |
| 39 | |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/proc_fs.h> |
| 42 | #include <linux/anon_inodes.h> |
| 43 | #include <linux/file.h> |
| 44 | #include <linux/uaccess.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 47 | #include "wrapper/ringbuffer/vfs.h" |
| 48 | #include "wrapper/poll.h" |
| 49 | #include "lttng-abi.h" |
| 50 | #include "lttng-abi-old.h" |
| 51 | #include "lttng-events.h" |
| 52 | #include "lttng-tracer.h" |
| 53 | |
| 54 | /* |
| 55 | * This is LTTng's own personal way to create a system call as an external |
| 56 | * module. We use ioctl() on /proc/lttng. |
| 57 | */ |
| 58 | |
| 59 | static struct proc_dir_entry *lttng_proc_dentry; |
| 60 | static const struct file_operations lttng_fops; |
| 61 | static const struct file_operations lttng_session_fops; |
| 62 | static const struct file_operations lttng_channel_fops; |
| 63 | static const struct file_operations lttng_metadata_fops; |
| 64 | static const struct file_operations lttng_event_fops; |
| 65 | |
| 66 | /* |
| 67 | * Teardown management: opened file descriptors keep a refcount on the module, |
| 68 | * so it can only exit when all file descriptors are closed. |
| 69 | */ |
| 70 | |
| 71 | enum channel_type { |
| 72 | PER_CPU_CHANNEL, |
| 73 | METADATA_CHANNEL, |
| 74 | }; |
| 75 | |
| 76 | static |
| 77 | int lttng_abi_create_session(void) |
| 78 | { |
| 79 | struct lttng_session *session; |
| 80 | struct file *session_file; |
| 81 | int session_fd, ret; |
| 82 | |
| 83 | session = lttng_session_create(); |
| 84 | if (!session) |
| 85 | return -ENOMEM; |
| 86 | session_fd = get_unused_fd(); |
| 87 | if (session_fd < 0) { |
| 88 | ret = session_fd; |
| 89 | goto fd_error; |
| 90 | } |
| 91 | session_file = anon_inode_getfile("[lttng_session]", |
| 92 | <tng_session_fops, |
| 93 | session, O_RDWR); |
| 94 | if (IS_ERR(session_file)) { |
| 95 | ret = PTR_ERR(session_file); |
| 96 | goto file_error; |
| 97 | } |
| 98 | session->file = session_file; |
| 99 | fd_install(session_fd, session_file); |
| 100 | return session_fd; |
| 101 | |
| 102 | file_error: |
| 103 | put_unused_fd(session_fd); |
| 104 | fd_error: |
| 105 | lttng_session_destroy(session); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | int lttng_abi_tracepoint_list(void) |
| 111 | { |
| 112 | struct file *tracepoint_list_file; |
| 113 | int file_fd, ret; |
| 114 | |
| 115 | file_fd = get_unused_fd(); |
| 116 | if (file_fd < 0) { |
| 117 | ret = file_fd; |
| 118 | goto fd_error; |
| 119 | } |
| 120 | |
| 121 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
| 122 | <tng_tracepoint_list_fops, |
| 123 | NULL, O_RDWR); |
| 124 | if (IS_ERR(tracepoint_list_file)) { |
| 125 | ret = PTR_ERR(tracepoint_list_file); |
| 126 | goto file_error; |
| 127 | } |
| 128 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
| 129 | if (ret < 0) |
| 130 | goto open_error; |
| 131 | fd_install(file_fd, tracepoint_list_file); |
| 132 | if (file_fd < 0) { |
| 133 | ret = file_fd; |
| 134 | goto fd_error; |
| 135 | } |
| 136 | return file_fd; |
| 137 | |
| 138 | open_error: |
| 139 | fput(tracepoint_list_file); |
| 140 | file_error: |
| 141 | put_unused_fd(file_fd); |
| 142 | fd_error: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static |
| 147 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
| 148 | { |
| 149 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
| 150 | v->minor = LTTNG_MODULES_MINOR_VERSION; |
| 151 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; |
| 152 | } |
| 153 | |
| 154 | static |
| 155 | long lttng_abi_add_context(struct file *file, |
| 156 | struct lttng_kernel_context *context_param, |
| 157 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 158 | { |
| 159 | |
| 160 | if (session->been_active) |
| 161 | return -EPERM; |
| 162 | |
| 163 | switch (context_param->ctx) { |
| 164 | case LTTNG_KERNEL_CONTEXT_PID: |
| 165 | return lttng_add_pid_to_ctx(ctx); |
| 166 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 167 | return lttng_add_prio_to_ctx(ctx); |
| 168 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 169 | return lttng_add_nice_to_ctx(ctx); |
| 170 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 171 | return lttng_add_vpid_to_ctx(ctx); |
| 172 | case LTTNG_KERNEL_CONTEXT_TID: |
| 173 | return lttng_add_tid_to_ctx(ctx); |
| 174 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 175 | return lttng_add_vtid_to_ctx(ctx); |
| 176 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 177 | return lttng_add_ppid_to_ctx(ctx); |
| 178 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 179 | return lttng_add_vppid_to_ctx(ctx); |
| 180 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
| 181 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 182 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, |
| 183 | context_param->u.perf_counter.config, |
| 184 | context_param->u.perf_counter.name, |
| 185 | ctx); |
| 186 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 187 | return lttng_add_procname_to_ctx(ctx); |
| 188 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
| 189 | return lttng_add_hostname_to_ctx(ctx); |
| 190 | default: |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * lttng_ioctl - lttng syscall through ioctl |
| 197 | * |
| 198 | * @file: the file |
| 199 | * @cmd: the command |
| 200 | * @arg: command arg |
| 201 | * |
| 202 | * This ioctl implements lttng commands: |
| 203 | * LTTNG_KERNEL_SESSION |
| 204 | * Returns a LTTng trace session file descriptor |
| 205 | * LTTNG_KERNEL_TRACER_VERSION |
| 206 | * Returns the LTTng kernel tracer version |
| 207 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 208 | * Returns a file descriptor listing available tracepoints |
| 209 | * LTTNG_KERNEL_WAIT_QUIESCENT |
| 210 | * Returns after all previously running probes have completed |
| 211 | * |
| 212 | * The returned session will be deleted when its file descriptor is closed. |
| 213 | */ |
| 214 | static |
| 215 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 216 | { |
| 217 | switch (cmd) { |
| 218 | case LTTNG_KERNEL_OLD_SESSION: |
| 219 | case LTTNG_KERNEL_SESSION: |
| 220 | return lttng_abi_create_session(); |
| 221 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
| 222 | { |
| 223 | struct lttng_kernel_tracer_version v; |
| 224 | struct lttng_kernel_old_tracer_version oldv; |
| 225 | struct lttng_kernel_old_tracer_version *uversion = |
| 226 | (struct lttng_kernel_old_tracer_version __user *) arg; |
| 227 | |
| 228 | lttng_abi_tracer_version(&v); |
| 229 | oldv.major = v.major; |
| 230 | oldv.minor = v.minor; |
| 231 | oldv.patchlevel = v.patchlevel; |
| 232 | |
| 233 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) |
| 234 | return -EFAULT; |
| 235 | return 0; |
| 236 | } |
| 237 | case LTTNG_KERNEL_TRACER_VERSION: |
| 238 | { |
| 239 | struct lttng_kernel_tracer_version version; |
| 240 | struct lttng_kernel_tracer_version *uversion = |
| 241 | (struct lttng_kernel_tracer_version __user *) arg; |
| 242 | |
| 243 | lttng_abi_tracer_version(&version); |
| 244 | |
| 245 | if (copy_to_user(uversion, &version, sizeof(version))) |
| 246 | return -EFAULT; |
| 247 | return 0; |
| 248 | } |
| 249 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: |
| 250 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 251 | return lttng_abi_tracepoint_list(); |
| 252 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
| 253 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
| 254 | synchronize_trace(); |
| 255 | return 0; |
| 256 | case LTTNG_KERNEL_OLD_CALIBRATE: |
| 257 | { |
| 258 | struct lttng_kernel_old_calibrate __user *ucalibrate = |
| 259 | (struct lttng_kernel_old_calibrate __user *) arg; |
| 260 | struct lttng_kernel_old_calibrate old_calibrate; |
| 261 | struct lttng_kernel_calibrate calibrate; |
| 262 | int ret; |
| 263 | |
| 264 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) |
| 265 | return -EFAULT; |
| 266 | calibrate.type = old_calibrate.type; |
| 267 | ret = lttng_calibrate(&calibrate); |
| 268 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) |
| 269 | return -EFAULT; |
| 270 | return ret; |
| 271 | } |
| 272 | case LTTNG_KERNEL_CALIBRATE: |
| 273 | { |
| 274 | struct lttng_kernel_calibrate __user *ucalibrate = |
| 275 | (struct lttng_kernel_calibrate __user *) arg; |
| 276 | struct lttng_kernel_calibrate calibrate; |
| 277 | int ret; |
| 278 | |
| 279 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) |
| 280 | return -EFAULT; |
| 281 | ret = lttng_calibrate(&calibrate); |
| 282 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) |
| 283 | return -EFAULT; |
| 284 | return ret; |
| 285 | } |
| 286 | default: |
| 287 | return -ENOIOCTLCMD; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static const struct file_operations lttng_fops = { |
| 292 | .owner = THIS_MODULE, |
| 293 | .unlocked_ioctl = lttng_ioctl, |
| 294 | #ifdef CONFIG_COMPAT |
| 295 | .compat_ioctl = lttng_ioctl, |
| 296 | #endif |
| 297 | }; |
| 298 | |
| 299 | /* |
| 300 | * We tolerate no failure in this function (if one happens, we print a dmesg |
| 301 | * error, but cannot return any error, because the channel information is |
| 302 | * invariant. |
| 303 | */ |
| 304 | static |
| 305 | void lttng_metadata_create_events(struct file *channel_file) |
| 306 | { |
| 307 | struct lttng_channel *channel = channel_file->private_data; |
| 308 | static struct lttng_kernel_event metadata_params = { |
| 309 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
| 310 | .name = "lttng_metadata", |
| 311 | }; |
| 312 | struct lttng_event *event; |
| 313 | |
| 314 | /* |
| 315 | * We tolerate no failure path after event creation. It will stay |
| 316 | * invariant for the rest of the session. |
| 317 | */ |
| 318 | event = lttng_event_create(channel, &metadata_params, NULL, NULL); |
| 319 | if (!event) { |
| 320 | goto create_error; |
| 321 | } |
| 322 | return; |
| 323 | |
| 324 | create_error: |
| 325 | WARN_ON(1); |
| 326 | return; /* not allowed to return error */ |
| 327 | } |
| 328 | |
| 329 | static |
| 330 | int lttng_abi_create_channel(struct file *session_file, |
| 331 | struct lttng_kernel_channel *chan_param, |
| 332 | enum channel_type channel_type) |
| 333 | { |
| 334 | struct lttng_session *session = session_file->private_data; |
| 335 | const struct file_operations *fops = NULL; |
| 336 | const char *transport_name; |
| 337 | struct lttng_channel *chan; |
| 338 | struct file *chan_file; |
| 339 | int chan_fd; |
| 340 | int ret = 0; |
| 341 | |
| 342 | chan_fd = get_unused_fd(); |
| 343 | if (chan_fd < 0) { |
| 344 | ret = chan_fd; |
| 345 | goto fd_error; |
| 346 | } |
| 347 | switch (channel_type) { |
| 348 | case PER_CPU_CHANNEL: |
| 349 | fops = <tng_channel_fops; |
| 350 | break; |
| 351 | case METADATA_CHANNEL: |
| 352 | fops = <tng_metadata_fops; |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 357 | fops, |
| 358 | NULL, O_RDWR); |
| 359 | if (IS_ERR(chan_file)) { |
| 360 | ret = PTR_ERR(chan_file); |
| 361 | goto file_error; |
| 362 | } |
| 363 | switch (channel_type) { |
| 364 | case PER_CPU_CHANNEL: |
| 365 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
| 366 | transport_name = chan_param->overwrite ? |
| 367 | "relay-overwrite" : "relay-discard"; |
| 368 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
| 369 | transport_name = chan_param->overwrite ? |
| 370 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 371 | } else { |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | break; |
| 375 | case METADATA_CHANNEL: |
| 376 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
| 377 | transport_name = "relay-metadata"; |
| 378 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
| 379 | transport_name = "relay-metadata-mmap"; |
| 380 | else |
| 381 | return -EINVAL; |
| 382 | break; |
| 383 | default: |
| 384 | transport_name = "<unknown>"; |
| 385 | break; |
| 386 | } |
| 387 | /* |
| 388 | * We tolerate no failure path after channel creation. It will stay |
| 389 | * invariant for the rest of the session. |
| 390 | */ |
| 391 | chan = lttng_channel_create(session, transport_name, NULL, |
| 392 | chan_param->subbuf_size, |
| 393 | chan_param->num_subbuf, |
| 394 | chan_param->switch_timer_interval, |
| 395 | chan_param->read_timer_interval); |
| 396 | if (!chan) { |
| 397 | ret = -EINVAL; |
| 398 | goto chan_error; |
| 399 | } |
| 400 | chan->file = chan_file; |
| 401 | chan_file->private_data = chan; |
| 402 | fd_install(chan_fd, chan_file); |
| 403 | if (channel_type == METADATA_CHANNEL) { |
| 404 | session->metadata = chan; |
| 405 | lttng_metadata_create_events(chan_file); |
| 406 | } |
| 407 | |
| 408 | /* The channel created holds a reference on the session */ |
| 409 | atomic_long_inc(&session_file->f_count); |
| 410 | |
| 411 | return chan_fd; |
| 412 | |
| 413 | chan_error: |
| 414 | fput(chan_file); |
| 415 | file_error: |
| 416 | put_unused_fd(chan_fd); |
| 417 | fd_error: |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * lttng_session_ioctl - lttng session fd ioctl |
| 423 | * |
| 424 | * @file: the file |
| 425 | * @cmd: the command |
| 426 | * @arg: command arg |
| 427 | * |
| 428 | * This ioctl implements lttng commands: |
| 429 | * LTTNG_KERNEL_CHANNEL |
| 430 | * Returns a LTTng channel file descriptor |
| 431 | * LTTNG_KERNEL_ENABLE |
| 432 | * Enables tracing for a session (weak enable) |
| 433 | * LTTNG_KERNEL_DISABLE |
| 434 | * Disables tracing for a session (strong disable) |
| 435 | * LTTNG_KERNEL_METADATA |
| 436 | * Returns a LTTng metadata file descriptor |
| 437 | * |
| 438 | * The returned channel will be deleted when its file descriptor is closed. |
| 439 | */ |
| 440 | static |
| 441 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 442 | { |
| 443 | struct lttng_session *session = file->private_data; |
| 444 | |
| 445 | switch (cmd) { |
| 446 | case LTTNG_KERNEL_OLD_CHANNEL: |
| 447 | { |
| 448 | struct lttng_kernel_channel chan_param; |
| 449 | struct lttng_kernel_old_channel old_chan_param; |
| 450 | |
| 451 | if (copy_from_user(&old_chan_param, |
| 452 | (struct lttng_kernel_old_channel __user *) arg, |
| 453 | sizeof(struct lttng_kernel_old_channel))) |
| 454 | return -EFAULT; |
| 455 | chan_param.overwrite = old_chan_param.overwrite; |
| 456 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 457 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 458 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 459 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 460 | chan_param.output = old_chan_param.output; |
| 461 | |
| 462 | return lttng_abi_create_channel(file, &chan_param, |
| 463 | PER_CPU_CHANNEL); |
| 464 | } |
| 465 | case LTTNG_KERNEL_CHANNEL: |
| 466 | { |
| 467 | struct lttng_kernel_channel chan_param; |
| 468 | |
| 469 | if (copy_from_user(&chan_param, |
| 470 | (struct lttng_kernel_channel __user *) arg, |
| 471 | sizeof(struct lttng_kernel_channel))) |
| 472 | return -EFAULT; |
| 473 | return lttng_abi_create_channel(file, &chan_param, |
| 474 | PER_CPU_CHANNEL); |
| 475 | } |
| 476 | case LTTNG_KERNEL_OLD_SESSION_START: |
| 477 | case LTTNG_KERNEL_OLD_ENABLE: |
| 478 | case LTTNG_KERNEL_SESSION_START: |
| 479 | case LTTNG_KERNEL_ENABLE: |
| 480 | return lttng_session_enable(session); |
| 481 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
| 482 | case LTTNG_KERNEL_OLD_DISABLE: |
| 483 | case LTTNG_KERNEL_SESSION_STOP: |
| 484 | case LTTNG_KERNEL_DISABLE: |
| 485 | return lttng_session_disable(session); |
| 486 | case LTTNG_KERNEL_OLD_METADATA: |
| 487 | { |
| 488 | struct lttng_kernel_channel chan_param; |
| 489 | struct lttng_kernel_old_channel old_chan_param; |
| 490 | |
| 491 | if (copy_from_user(&old_chan_param, |
| 492 | (struct lttng_kernel_old_channel __user *) arg, |
| 493 | sizeof(struct lttng_kernel_old_channel))) |
| 494 | return -EFAULT; |
| 495 | chan_param.overwrite = old_chan_param.overwrite; |
| 496 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 497 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 498 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 499 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 500 | chan_param.output = old_chan_param.output; |
| 501 | |
| 502 | return lttng_abi_create_channel(file, &chan_param, |
| 503 | METADATA_CHANNEL); |
| 504 | } |
| 505 | case LTTNG_KERNEL_METADATA: |
| 506 | { |
| 507 | struct lttng_kernel_channel chan_param; |
| 508 | |
| 509 | if (copy_from_user(&chan_param, |
| 510 | (struct lttng_kernel_channel __user *) arg, |
| 511 | sizeof(struct lttng_kernel_channel))) |
| 512 | return -EFAULT; |
| 513 | return lttng_abi_create_channel(file, &chan_param, |
| 514 | METADATA_CHANNEL); |
| 515 | } |
| 516 | default: |
| 517 | return -ENOIOCTLCMD; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Called when the last file reference is dropped. |
| 523 | * |
| 524 | * Big fat note: channels and events are invariant for the whole session after |
| 525 | * their creation. So this session destruction also destroys all channel and |
| 526 | * event structures specific to this session (they are not destroyed when their |
| 527 | * individual file is released). |
| 528 | */ |
| 529 | static |
| 530 | int lttng_session_release(struct inode *inode, struct file *file) |
| 531 | { |
| 532 | struct lttng_session *session = file->private_data; |
| 533 | |
| 534 | if (session) |
| 535 | lttng_session_destroy(session); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static const struct file_operations lttng_session_fops = { |
| 540 | .owner = THIS_MODULE, |
| 541 | .release = lttng_session_release, |
| 542 | .unlocked_ioctl = lttng_session_ioctl, |
| 543 | #ifdef CONFIG_COMPAT |
| 544 | .compat_ioctl = lttng_session_ioctl, |
| 545 | #endif |
| 546 | }; |
| 547 | |
| 548 | static |
| 549 | int lttng_abi_open_stream(struct file *channel_file) |
| 550 | { |
| 551 | struct lttng_channel *channel = channel_file->private_data; |
| 552 | struct lib_ring_buffer *buf; |
| 553 | int stream_fd, ret; |
| 554 | struct file *stream_file; |
| 555 | |
| 556 | buf = channel->ops->buffer_read_open(channel->chan); |
| 557 | if (!buf) |
| 558 | return -ENOENT; |
| 559 | |
| 560 | stream_fd = get_unused_fd(); |
| 561 | if (stream_fd < 0) { |
| 562 | ret = stream_fd; |
| 563 | goto fd_error; |
| 564 | } |
| 565 | stream_file = anon_inode_getfile("[lttng_stream]", |
| 566 | &lib_ring_buffer_file_operations, |
| 567 | buf, O_RDWR); |
| 568 | if (IS_ERR(stream_file)) { |
| 569 | ret = PTR_ERR(stream_file); |
| 570 | goto file_error; |
| 571 | } |
| 572 | /* |
| 573 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 574 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 575 | * file descriptor, so we set FMODE_PREAD here. |
| 576 | */ |
| 577 | stream_file->f_mode |= FMODE_PREAD; |
| 578 | fd_install(stream_fd, stream_file); |
| 579 | /* |
| 580 | * The stream holds a reference to the channel within the generic ring |
| 581 | * buffer library, so no need to hold a refcount on the channel and |
| 582 | * session files here. |
| 583 | */ |
| 584 | return stream_fd; |
| 585 | |
| 586 | file_error: |
| 587 | put_unused_fd(stream_fd); |
| 588 | fd_error: |
| 589 | channel->ops->buffer_read_close(buf); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static |
| 594 | int lttng_abi_create_event(struct file *channel_file, |
| 595 | struct lttng_kernel_event *event_param) |
| 596 | { |
| 597 | struct lttng_channel *channel = channel_file->private_data; |
| 598 | struct lttng_event *event; |
| 599 | int event_fd, ret; |
| 600 | struct file *event_file; |
| 601 | |
| 602 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 603 | switch (event_param->instrumentation) { |
| 604 | case LTTNG_KERNEL_KRETPROBE: |
| 605 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 606 | break; |
| 607 | case LTTNG_KERNEL_KPROBE: |
| 608 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 609 | break; |
| 610 | case LTTNG_KERNEL_FUNCTION: |
| 611 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 612 | break; |
| 613 | default: |
| 614 | break; |
| 615 | } |
| 616 | switch (event_param->instrumentation) { |
| 617 | default: |
| 618 | event_fd = get_unused_fd(); |
| 619 | if (event_fd < 0) { |
| 620 | ret = event_fd; |
| 621 | goto fd_error; |
| 622 | } |
| 623 | event_file = anon_inode_getfile("[lttng_event]", |
| 624 | <tng_event_fops, |
| 625 | NULL, O_RDWR); |
| 626 | if (IS_ERR(event_file)) { |
| 627 | ret = PTR_ERR(event_file); |
| 628 | goto file_error; |
| 629 | } |
| 630 | /* |
| 631 | * We tolerate no failure path after event creation. It |
| 632 | * will stay invariant for the rest of the session. |
| 633 | */ |
| 634 | event = lttng_event_create(channel, event_param, NULL, NULL); |
| 635 | if (!event) { |
| 636 | ret = -EINVAL; |
| 637 | goto event_error; |
| 638 | } |
| 639 | event_file->private_data = event; |
| 640 | fd_install(event_fd, event_file); |
| 641 | /* The event holds a reference on the channel */ |
| 642 | atomic_long_inc(&channel_file->f_count); |
| 643 | break; |
| 644 | case LTTNG_KERNEL_SYSCALL: |
| 645 | /* |
| 646 | * Only all-syscall tracing supported for now. |
| 647 | */ |
| 648 | if (event_param->name[0] != '\0') |
| 649 | return -EINVAL; |
| 650 | ret = lttng_syscalls_register(channel, NULL); |
| 651 | if (ret) |
| 652 | goto fd_error; |
| 653 | event_fd = 0; |
| 654 | break; |
| 655 | } |
| 656 | return event_fd; |
| 657 | |
| 658 | event_error: |
| 659 | fput(event_file); |
| 660 | file_error: |
| 661 | put_unused_fd(event_fd); |
| 662 | fd_error: |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 668 | * |
| 669 | * @file: the file |
| 670 | * @cmd: the command |
| 671 | * @arg: command arg |
| 672 | * |
| 673 | * This ioctl implements lttng commands: |
| 674 | * LTTNG_KERNEL_STREAM |
| 675 | * Returns an event stream file descriptor or failure. |
| 676 | * (typically, one event stream records events from one CPU) |
| 677 | * LTTNG_KERNEL_EVENT |
| 678 | * Returns an event file descriptor or failure. |
| 679 | * LTTNG_KERNEL_CONTEXT |
| 680 | * Prepend a context field to each event in the channel |
| 681 | * LTTNG_KERNEL_ENABLE |
| 682 | * Enable recording for events in this channel (weak enable) |
| 683 | * LTTNG_KERNEL_DISABLE |
| 684 | * Disable recording for events in this channel (strong disable) |
| 685 | * |
| 686 | * Channel and event file descriptors also hold a reference on the session. |
| 687 | */ |
| 688 | static |
| 689 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 690 | { |
| 691 | struct lttng_channel *channel = file->private_data; |
| 692 | |
| 693 | switch (cmd) { |
| 694 | case LTTNG_KERNEL_OLD_STREAM: |
| 695 | case LTTNG_KERNEL_STREAM: |
| 696 | return lttng_abi_open_stream(file); |
| 697 | case LTTNG_KERNEL_OLD_EVENT: |
| 698 | { |
| 699 | struct lttng_kernel_event *uevent_param; |
| 700 | struct lttng_kernel_old_event *old_uevent_param; |
| 701 | int ret; |
| 702 | |
| 703 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), |
| 704 | GFP_KERNEL); |
| 705 | if (!uevent_param) { |
| 706 | ret = -ENOMEM; |
| 707 | goto old_event_end; |
| 708 | } |
| 709 | old_uevent_param = kmalloc( |
| 710 | sizeof(struct lttng_kernel_old_event), |
| 711 | GFP_KERNEL); |
| 712 | if (!old_uevent_param) { |
| 713 | ret = -ENOMEM; |
| 714 | goto old_event_error_free_param; |
| 715 | } |
| 716 | if (copy_from_user(old_uevent_param, |
| 717 | (struct lttng_kernel_old_event __user *) arg, |
| 718 | sizeof(struct lttng_kernel_old_event))) { |
| 719 | ret = -EFAULT; |
| 720 | goto old_event_error_free_old_param; |
| 721 | } |
| 722 | |
| 723 | memcpy(uevent_param->name, old_uevent_param->name, |
| 724 | sizeof(uevent_param->name)); |
| 725 | uevent_param->instrumentation = |
| 726 | old_uevent_param->instrumentation; |
| 727 | |
| 728 | switch (old_uevent_param->instrumentation) { |
| 729 | case LTTNG_KERNEL_KPROBE: |
| 730 | uevent_param->u.kprobe.addr = |
| 731 | old_uevent_param->u.kprobe.addr; |
| 732 | uevent_param->u.kprobe.offset = |
| 733 | old_uevent_param->u.kprobe.offset; |
| 734 | memcpy(uevent_param->u.kprobe.symbol_name, |
| 735 | old_uevent_param->u.kprobe.symbol_name, |
| 736 | sizeof(uevent_param->u.kprobe.symbol_name)); |
| 737 | break; |
| 738 | case LTTNG_KERNEL_KRETPROBE: |
| 739 | uevent_param->u.kretprobe.addr = |
| 740 | old_uevent_param->u.kretprobe.addr; |
| 741 | uevent_param->u.kretprobe.offset = |
| 742 | old_uevent_param->u.kretprobe.offset; |
| 743 | memcpy(uevent_param->u.kretprobe.symbol_name, |
| 744 | old_uevent_param->u.kretprobe.symbol_name, |
| 745 | sizeof(uevent_param->u.kretprobe.symbol_name)); |
| 746 | break; |
| 747 | case LTTNG_KERNEL_FUNCTION: |
| 748 | memcpy(uevent_param->u.ftrace.symbol_name, |
| 749 | old_uevent_param->u.ftrace.symbol_name, |
| 750 | sizeof(uevent_param->u.ftrace.symbol_name)); |
| 751 | break; |
| 752 | default: |
| 753 | break; |
| 754 | } |
| 755 | ret = lttng_abi_create_event(file, uevent_param); |
| 756 | |
| 757 | old_event_error_free_old_param: |
| 758 | kfree(old_uevent_param); |
| 759 | old_event_error_free_param: |
| 760 | kfree(uevent_param); |
| 761 | old_event_end: |
| 762 | return ret; |
| 763 | } |
| 764 | case LTTNG_KERNEL_EVENT: |
| 765 | { |
| 766 | struct lttng_kernel_event uevent_param; |
| 767 | |
| 768 | if (copy_from_user(&uevent_param, |
| 769 | (struct lttng_kernel_event __user *) arg, |
| 770 | sizeof(uevent_param))) |
| 771 | return -EFAULT; |
| 772 | return lttng_abi_create_event(file, &uevent_param); |
| 773 | } |
| 774 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 775 | { |
| 776 | struct lttng_kernel_context *ucontext_param; |
| 777 | struct lttng_kernel_old_context *old_ucontext_param; |
| 778 | int ret; |
| 779 | |
| 780 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 781 | GFP_KERNEL); |
| 782 | if (!ucontext_param) { |
| 783 | ret = -ENOMEM; |
| 784 | goto old_ctx_end; |
| 785 | } |
| 786 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 787 | GFP_KERNEL); |
| 788 | if (!old_ucontext_param) { |
| 789 | ret = -ENOMEM; |
| 790 | goto old_ctx_error_free_param; |
| 791 | } |
| 792 | |
| 793 | if (copy_from_user(old_ucontext_param, |
| 794 | (struct lttng_kernel_old_context __user *) arg, |
| 795 | sizeof(struct lttng_kernel_old_context))) { |
| 796 | ret = -EFAULT; |
| 797 | goto old_ctx_error_free_old_param; |
| 798 | } |
| 799 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 800 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 801 | sizeof(ucontext_param->padding)); |
| 802 | /* only type that uses the union */ |
| 803 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 804 | ucontext_param->u.perf_counter.type = |
| 805 | old_ucontext_param->u.perf_counter.type; |
| 806 | ucontext_param->u.perf_counter.config = |
| 807 | old_ucontext_param->u.perf_counter.config; |
| 808 | memcpy(ucontext_param->u.perf_counter.name, |
| 809 | old_ucontext_param->u.perf_counter.name, |
| 810 | sizeof(ucontext_param->u.perf_counter.name)); |
| 811 | } |
| 812 | |
| 813 | ret = lttng_abi_add_context(file, |
| 814 | ucontext_param, |
| 815 | &channel->ctx, channel->session); |
| 816 | |
| 817 | old_ctx_error_free_old_param: |
| 818 | kfree(old_ucontext_param); |
| 819 | old_ctx_error_free_param: |
| 820 | kfree(ucontext_param); |
| 821 | old_ctx_end: |
| 822 | return ret; |
| 823 | } |
| 824 | case LTTNG_KERNEL_CONTEXT: |
| 825 | { |
| 826 | struct lttng_kernel_context ucontext_param; |
| 827 | |
| 828 | if (copy_from_user(&ucontext_param, |
| 829 | (struct lttng_kernel_context __user *) arg, |
| 830 | sizeof(ucontext_param))) |
| 831 | return -EFAULT; |
| 832 | return lttng_abi_add_context(file, |
| 833 | &ucontext_param, |
| 834 | &channel->ctx, channel->session); |
| 835 | } |
| 836 | case LTTNG_KERNEL_OLD_ENABLE: |
| 837 | case LTTNG_KERNEL_ENABLE: |
| 838 | return lttng_channel_enable(channel); |
| 839 | case LTTNG_KERNEL_OLD_DISABLE: |
| 840 | case LTTNG_KERNEL_DISABLE: |
| 841 | return lttng_channel_disable(channel); |
| 842 | default: |
| 843 | return -ENOIOCTLCMD; |
| 844 | } |
| 845 | |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 850 | * |
| 851 | * @file: the file |
| 852 | * @cmd: the command |
| 853 | * @arg: command arg |
| 854 | * |
| 855 | * This ioctl implements lttng commands: |
| 856 | * LTTNG_KERNEL_STREAM |
| 857 | * Returns an event stream file descriptor or failure. |
| 858 | * |
| 859 | * Channel and event file descriptors also hold a reference on the session. |
| 860 | */ |
| 861 | static |
| 862 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 863 | { |
| 864 | switch (cmd) { |
| 865 | case LTTNG_KERNEL_OLD_STREAM: |
| 866 | case LTTNG_KERNEL_STREAM: |
| 867 | return lttng_abi_open_stream(file); |
| 868 | default: |
| 869 | return -ENOIOCTLCMD; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 875 | * |
| 876 | * @file: the file |
| 877 | * @wait: poll table |
| 878 | */ |
| 879 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 880 | { |
| 881 | struct lttng_channel *channel = file->private_data; |
| 882 | unsigned int mask = 0; |
| 883 | |
| 884 | if (file->f_mode & FMODE_READ) { |
| 885 | poll_wait_set_exclusive(wait); |
| 886 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 887 | wait); |
| 888 | |
| 889 | if (channel->ops->is_disabled(channel->chan)) |
| 890 | return POLLERR; |
| 891 | if (channel->ops->is_finalized(channel->chan)) |
| 892 | return POLLHUP; |
| 893 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 894 | return POLLIN | POLLRDNORM; |
| 895 | return 0; |
| 896 | } |
| 897 | return mask; |
| 898 | |
| 899 | } |
| 900 | |
| 901 | static |
| 902 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 903 | { |
| 904 | struct lttng_channel *channel = file->private_data; |
| 905 | |
| 906 | if (channel) |
| 907 | fput(channel->session->file); |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | static const struct file_operations lttng_channel_fops = { |
| 912 | .owner = THIS_MODULE, |
| 913 | .release = lttng_channel_release, |
| 914 | .poll = lttng_channel_poll, |
| 915 | .unlocked_ioctl = lttng_channel_ioctl, |
| 916 | #ifdef CONFIG_COMPAT |
| 917 | .compat_ioctl = lttng_channel_ioctl, |
| 918 | #endif |
| 919 | }; |
| 920 | |
| 921 | static const struct file_operations lttng_metadata_fops = { |
| 922 | .owner = THIS_MODULE, |
| 923 | .release = lttng_channel_release, |
| 924 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 925 | #ifdef CONFIG_COMPAT |
| 926 | .compat_ioctl = lttng_metadata_ioctl, |
| 927 | #endif |
| 928 | }; |
| 929 | |
| 930 | /** |
| 931 | * lttng_event_ioctl - lttng syscall through ioctl |
| 932 | * |
| 933 | * @file: the file |
| 934 | * @cmd: the command |
| 935 | * @arg: command arg |
| 936 | * |
| 937 | * This ioctl implements lttng commands: |
| 938 | * LTTNG_KERNEL_CONTEXT |
| 939 | * Prepend a context field to each record of this event |
| 940 | * LTTNG_KERNEL_ENABLE |
| 941 | * Enable recording for this event (weak enable) |
| 942 | * LTTNG_KERNEL_DISABLE |
| 943 | * Disable recording for this event (strong disable) |
| 944 | */ |
| 945 | static |
| 946 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 947 | { |
| 948 | struct lttng_event *event = file->private_data; |
| 949 | |
| 950 | switch (cmd) { |
| 951 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 952 | { |
| 953 | struct lttng_kernel_context *ucontext_param; |
| 954 | struct lttng_kernel_old_context *old_ucontext_param; |
| 955 | int ret; |
| 956 | |
| 957 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 958 | GFP_KERNEL); |
| 959 | if (!ucontext_param) { |
| 960 | ret = -ENOMEM; |
| 961 | goto old_ctx_end; |
| 962 | } |
| 963 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 964 | GFP_KERNEL); |
| 965 | if (!old_ucontext_param) { |
| 966 | ret = -ENOMEM; |
| 967 | goto old_ctx_error_free_param; |
| 968 | } |
| 969 | |
| 970 | if (copy_from_user(old_ucontext_param, |
| 971 | (struct lttng_kernel_old_context __user *) arg, |
| 972 | sizeof(struct lttng_kernel_old_context))) { |
| 973 | ret = -EFAULT; |
| 974 | goto old_ctx_error_free_old_param; |
| 975 | } |
| 976 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 977 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 978 | sizeof(ucontext_param->padding)); |
| 979 | /* only type that uses the union */ |
| 980 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 981 | ucontext_param->u.perf_counter.type = |
| 982 | old_ucontext_param->u.perf_counter.type; |
| 983 | ucontext_param->u.perf_counter.config = |
| 984 | old_ucontext_param->u.perf_counter.config; |
| 985 | memcpy(ucontext_param->u.perf_counter.name, |
| 986 | old_ucontext_param->u.perf_counter.name, |
| 987 | sizeof(ucontext_param->u.perf_counter.name)); |
| 988 | } |
| 989 | |
| 990 | ret = lttng_abi_add_context(file, |
| 991 | ucontext_param, |
| 992 | &event->ctx, event->chan->session); |
| 993 | |
| 994 | old_ctx_error_free_old_param: |
| 995 | kfree(old_ucontext_param); |
| 996 | old_ctx_error_free_param: |
| 997 | kfree(ucontext_param); |
| 998 | old_ctx_end: |
| 999 | return ret; |
| 1000 | } |
| 1001 | case LTTNG_KERNEL_CONTEXT: |
| 1002 | { |
| 1003 | struct lttng_kernel_context ucontext_param; |
| 1004 | |
| 1005 | if (copy_from_user(&ucontext_param, |
| 1006 | (struct lttng_kernel_context __user *) arg, |
| 1007 | sizeof(ucontext_param))) |
| 1008 | return -EFAULT; |
| 1009 | return lttng_abi_add_context(file, |
| 1010 | &ucontext_param, |
| 1011 | &event->ctx, event->chan->session); |
| 1012 | } |
| 1013 | case LTTNG_KERNEL_OLD_ENABLE: |
| 1014 | case LTTNG_KERNEL_ENABLE: |
| 1015 | return lttng_event_enable(event); |
| 1016 | case LTTNG_KERNEL_OLD_DISABLE: |
| 1017 | case LTTNG_KERNEL_DISABLE: |
| 1018 | return lttng_event_disable(event); |
| 1019 | default: |
| 1020 | return -ENOIOCTLCMD; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | static |
| 1025 | int lttng_event_release(struct inode *inode, struct file *file) |
| 1026 | { |
| 1027 | struct lttng_event *event = file->private_data; |
| 1028 | |
| 1029 | if (event) |
| 1030 | fput(event->chan->file); |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | /* TODO: filter control ioctl */ |
| 1035 | static const struct file_operations lttng_event_fops = { |
| 1036 | .owner = THIS_MODULE, |
| 1037 | .release = lttng_event_release, |
| 1038 | .unlocked_ioctl = lttng_event_ioctl, |
| 1039 | #ifdef CONFIG_COMPAT |
| 1040 | .compat_ioctl = lttng_event_ioctl, |
| 1041 | #endif |
| 1042 | }; |
| 1043 | |
| 1044 | int __init lttng_abi_init(void) |
| 1045 | { |
| 1046 | int ret = 0; |
| 1047 | |
| 1048 | wrapper_vmalloc_sync_all(); |
| 1049 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
| 1050 | <tng_fops, NULL); |
| 1051 | |
| 1052 | if (!lttng_proc_dentry) { |
| 1053 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 1054 | ret = -ENOMEM; |
| 1055 | goto error; |
| 1056 | } |
| 1057 | error: |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | void __exit lttng_abi_exit(void) |
| 1062 | { |
| 1063 | if (lttng_proc_dentry) |
| 1064 | remove_proc_entry("lttng", NULL); |
| 1065 | } |