| 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 | static |
| 300 | int lttng_abi_create_channel(struct file *session_file, |
| 301 | struct lttng_kernel_channel *chan_param, |
| 302 | enum channel_type channel_type) |
| 303 | { |
| 304 | struct lttng_session *session = session_file->private_data; |
| 305 | const struct file_operations *fops = NULL; |
| 306 | const char *transport_name; |
| 307 | struct lttng_channel *chan; |
| 308 | struct file *chan_file; |
| 309 | int chan_fd; |
| 310 | int ret = 0; |
| 311 | |
| 312 | chan_fd = get_unused_fd(); |
| 313 | if (chan_fd < 0) { |
| 314 | ret = chan_fd; |
| 315 | goto fd_error; |
| 316 | } |
| 317 | switch (channel_type) { |
| 318 | case PER_CPU_CHANNEL: |
| 319 | fops = <tng_channel_fops; |
| 320 | break; |
| 321 | case METADATA_CHANNEL: |
| 322 | fops = <tng_metadata_fops; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 327 | fops, |
| 328 | NULL, O_RDWR); |
| 329 | if (IS_ERR(chan_file)) { |
| 330 | ret = PTR_ERR(chan_file); |
| 331 | goto file_error; |
| 332 | } |
| 333 | switch (channel_type) { |
| 334 | case PER_CPU_CHANNEL: |
| 335 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
| 336 | transport_name = chan_param->overwrite ? |
| 337 | "relay-overwrite" : "relay-discard"; |
| 338 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
| 339 | transport_name = chan_param->overwrite ? |
| 340 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 341 | } else { |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | break; |
| 345 | case METADATA_CHANNEL: |
| 346 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
| 347 | transport_name = "relay-metadata"; |
| 348 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
| 349 | transport_name = "relay-metadata-mmap"; |
| 350 | else |
| 351 | return -EINVAL; |
| 352 | break; |
| 353 | default: |
| 354 | transport_name = "<unknown>"; |
| 355 | break; |
| 356 | } |
| 357 | /* |
| 358 | * We tolerate no failure path after channel creation. It will stay |
| 359 | * invariant for the rest of the session. |
| 360 | */ |
| 361 | chan = lttng_channel_create(session, transport_name, NULL, |
| 362 | chan_param->subbuf_size, |
| 363 | chan_param->num_subbuf, |
| 364 | chan_param->switch_timer_interval, |
| 365 | chan_param->read_timer_interval); |
| 366 | if (!chan) { |
| 367 | ret = -EINVAL; |
| 368 | goto chan_error; |
| 369 | } |
| 370 | chan->file = chan_file; |
| 371 | chan_file->private_data = chan; |
| 372 | fd_install(chan_fd, chan_file); |
| 373 | if (channel_type == METADATA_CHANNEL) { |
| 374 | session->metadata = chan; |
| 375 | } |
| 376 | |
| 377 | /* The channel created holds a reference on the session */ |
| 378 | atomic_long_inc(&session_file->f_count); |
| 379 | |
| 380 | return chan_fd; |
| 381 | |
| 382 | chan_error: |
| 383 | fput(chan_file); |
| 384 | file_error: |
| 385 | put_unused_fd(chan_fd); |
| 386 | fd_error: |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * lttng_session_ioctl - lttng session fd ioctl |
| 392 | * |
| 393 | * @file: the file |
| 394 | * @cmd: the command |
| 395 | * @arg: command arg |
| 396 | * |
| 397 | * This ioctl implements lttng commands: |
| 398 | * LTTNG_KERNEL_CHANNEL |
| 399 | * Returns a LTTng channel file descriptor |
| 400 | * LTTNG_KERNEL_ENABLE |
| 401 | * Enables tracing for a session (weak enable) |
| 402 | * LTTNG_KERNEL_DISABLE |
| 403 | * Disables tracing for a session (strong disable) |
| 404 | * LTTNG_KERNEL_METADATA |
| 405 | * Returns a LTTng metadata file descriptor |
| 406 | * |
| 407 | * The returned channel will be deleted when its file descriptor is closed. |
| 408 | */ |
| 409 | static |
| 410 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 411 | { |
| 412 | struct lttng_session *session = file->private_data; |
| 413 | |
| 414 | switch (cmd) { |
| 415 | case LTTNG_KERNEL_OLD_CHANNEL: |
| 416 | { |
| 417 | struct lttng_kernel_channel chan_param; |
| 418 | struct lttng_kernel_old_channel old_chan_param; |
| 419 | |
| 420 | if (copy_from_user(&old_chan_param, |
| 421 | (struct lttng_kernel_old_channel __user *) arg, |
| 422 | sizeof(struct lttng_kernel_old_channel))) |
| 423 | return -EFAULT; |
| 424 | chan_param.overwrite = old_chan_param.overwrite; |
| 425 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 426 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 427 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 428 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 429 | chan_param.output = old_chan_param.output; |
| 430 | |
| 431 | return lttng_abi_create_channel(file, &chan_param, |
| 432 | PER_CPU_CHANNEL); |
| 433 | } |
| 434 | case LTTNG_KERNEL_CHANNEL: |
| 435 | { |
| 436 | struct lttng_kernel_channel chan_param; |
| 437 | |
| 438 | if (copy_from_user(&chan_param, |
| 439 | (struct lttng_kernel_channel __user *) arg, |
| 440 | sizeof(struct lttng_kernel_channel))) |
| 441 | return -EFAULT; |
| 442 | return lttng_abi_create_channel(file, &chan_param, |
| 443 | PER_CPU_CHANNEL); |
| 444 | } |
| 445 | case LTTNG_KERNEL_OLD_SESSION_START: |
| 446 | case LTTNG_KERNEL_OLD_ENABLE: |
| 447 | case LTTNG_KERNEL_SESSION_START: |
| 448 | case LTTNG_KERNEL_ENABLE: |
| 449 | return lttng_session_enable(session); |
| 450 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
| 451 | case LTTNG_KERNEL_OLD_DISABLE: |
| 452 | case LTTNG_KERNEL_SESSION_STOP: |
| 453 | case LTTNG_KERNEL_DISABLE: |
| 454 | return lttng_session_disable(session); |
| 455 | case LTTNG_KERNEL_OLD_METADATA: |
| 456 | { |
| 457 | struct lttng_kernel_channel chan_param; |
| 458 | struct lttng_kernel_old_channel old_chan_param; |
| 459 | |
| 460 | if (copy_from_user(&old_chan_param, |
| 461 | (struct lttng_kernel_old_channel __user *) arg, |
| 462 | sizeof(struct lttng_kernel_old_channel))) |
| 463 | return -EFAULT; |
| 464 | chan_param.overwrite = old_chan_param.overwrite; |
| 465 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 466 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 467 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 468 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 469 | chan_param.output = old_chan_param.output; |
| 470 | |
| 471 | return lttng_abi_create_channel(file, &chan_param, |
| 472 | METADATA_CHANNEL); |
| 473 | } |
| 474 | case LTTNG_KERNEL_METADATA: |
| 475 | { |
| 476 | struct lttng_kernel_channel chan_param; |
| 477 | |
| 478 | if (copy_from_user(&chan_param, |
| 479 | (struct lttng_kernel_channel __user *) arg, |
| 480 | sizeof(struct lttng_kernel_channel))) |
| 481 | return -EFAULT; |
| 482 | return lttng_abi_create_channel(file, &chan_param, |
| 483 | METADATA_CHANNEL); |
| 484 | } |
| 485 | default: |
| 486 | return -ENOIOCTLCMD; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Called when the last file reference is dropped. |
| 492 | * |
| 493 | * Big fat note: channels and events are invariant for the whole session after |
| 494 | * their creation. So this session destruction also destroys all channel and |
| 495 | * event structures specific to this session (they are not destroyed when their |
| 496 | * individual file is released). |
| 497 | */ |
| 498 | static |
| 499 | int lttng_session_release(struct inode *inode, struct file *file) |
| 500 | { |
| 501 | struct lttng_session *session = file->private_data; |
| 502 | |
| 503 | if (session) |
| 504 | lttng_session_destroy(session); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static const struct file_operations lttng_session_fops = { |
| 509 | .owner = THIS_MODULE, |
| 510 | .release = lttng_session_release, |
| 511 | .unlocked_ioctl = lttng_session_ioctl, |
| 512 | #ifdef CONFIG_COMPAT |
| 513 | .compat_ioctl = lttng_session_ioctl, |
| 514 | #endif |
| 515 | }; |
| 516 | |
| 517 | static |
| 518 | int lttng_abi_open_stream(struct file *channel_file) |
| 519 | { |
| 520 | struct lttng_channel *channel = channel_file->private_data; |
| 521 | struct lib_ring_buffer *buf; |
| 522 | int stream_fd, ret; |
| 523 | struct file *stream_file; |
| 524 | |
| 525 | buf = channel->ops->buffer_read_open(channel->chan); |
| 526 | if (!buf) |
| 527 | return -ENOENT; |
| 528 | |
| 529 | stream_fd = get_unused_fd(); |
| 530 | if (stream_fd < 0) { |
| 531 | ret = stream_fd; |
| 532 | goto fd_error; |
| 533 | } |
| 534 | stream_file = anon_inode_getfile("[lttng_stream]", |
| 535 | &lib_ring_buffer_file_operations, |
| 536 | buf, O_RDWR); |
| 537 | if (IS_ERR(stream_file)) { |
| 538 | ret = PTR_ERR(stream_file); |
| 539 | goto file_error; |
| 540 | } |
| 541 | /* |
| 542 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 543 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 544 | * file descriptor, so we set FMODE_PREAD here. |
| 545 | */ |
| 546 | stream_file->f_mode |= FMODE_PREAD; |
| 547 | fd_install(stream_fd, stream_file); |
| 548 | /* |
| 549 | * The stream holds a reference to the channel within the generic ring |
| 550 | * buffer library, so no need to hold a refcount on the channel and |
| 551 | * session files here. |
| 552 | */ |
| 553 | return stream_fd; |
| 554 | |
| 555 | file_error: |
| 556 | put_unused_fd(stream_fd); |
| 557 | fd_error: |
| 558 | channel->ops->buffer_read_close(buf); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static |
| 563 | int lttng_abi_create_event(struct file *channel_file, |
| 564 | struct lttng_kernel_event *event_param) |
| 565 | { |
| 566 | struct lttng_channel *channel = channel_file->private_data; |
| 567 | struct lttng_event *event; |
| 568 | int event_fd, ret; |
| 569 | struct file *event_file; |
| 570 | |
| 571 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 572 | switch (event_param->instrumentation) { |
| 573 | case LTTNG_KERNEL_KRETPROBE: |
| 574 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 575 | break; |
| 576 | case LTTNG_KERNEL_KPROBE: |
| 577 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 578 | break; |
| 579 | case LTTNG_KERNEL_FUNCTION: |
| 580 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 581 | break; |
| 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | switch (event_param->instrumentation) { |
| 586 | default: |
| 587 | event_fd = get_unused_fd(); |
| 588 | if (event_fd < 0) { |
| 589 | ret = event_fd; |
| 590 | goto fd_error; |
| 591 | } |
| 592 | event_file = anon_inode_getfile("[lttng_event]", |
| 593 | <tng_event_fops, |
| 594 | NULL, O_RDWR); |
| 595 | if (IS_ERR(event_file)) { |
| 596 | ret = PTR_ERR(event_file); |
| 597 | goto file_error; |
| 598 | } |
| 599 | /* |
| 600 | * We tolerate no failure path after event creation. It |
| 601 | * will stay invariant for the rest of the session. |
| 602 | */ |
| 603 | event = lttng_event_create(channel, event_param, NULL, NULL); |
| 604 | if (!event) { |
| 605 | ret = -EINVAL; |
| 606 | goto event_error; |
| 607 | } |
| 608 | event_file->private_data = event; |
| 609 | fd_install(event_fd, event_file); |
| 610 | /* The event holds a reference on the channel */ |
| 611 | atomic_long_inc(&channel_file->f_count); |
| 612 | break; |
| 613 | case LTTNG_KERNEL_SYSCALL: |
| 614 | /* |
| 615 | * Only all-syscall tracing supported for now. |
| 616 | */ |
| 617 | if (event_param->name[0] != '\0') |
| 618 | return -EINVAL; |
| 619 | ret = lttng_syscalls_register(channel, NULL); |
| 620 | if (ret) |
| 621 | goto fd_error; |
| 622 | event_fd = 0; |
| 623 | break; |
| 624 | } |
| 625 | return event_fd; |
| 626 | |
| 627 | event_error: |
| 628 | fput(event_file); |
| 629 | file_error: |
| 630 | put_unused_fd(event_fd); |
| 631 | fd_error: |
| 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 637 | * |
| 638 | * @file: the file |
| 639 | * @cmd: the command |
| 640 | * @arg: command arg |
| 641 | * |
| 642 | * This ioctl implements lttng commands: |
| 643 | * LTTNG_KERNEL_STREAM |
| 644 | * Returns an event stream file descriptor or failure. |
| 645 | * (typically, one event stream records events from one CPU) |
| 646 | * LTTNG_KERNEL_EVENT |
| 647 | * Returns an event file descriptor or failure. |
| 648 | * LTTNG_KERNEL_CONTEXT |
| 649 | * Prepend a context field to each event in the channel |
| 650 | * LTTNG_KERNEL_ENABLE |
| 651 | * Enable recording for events in this channel (weak enable) |
| 652 | * LTTNG_KERNEL_DISABLE |
| 653 | * Disable recording for events in this channel (strong disable) |
| 654 | * |
| 655 | * Channel and event file descriptors also hold a reference on the session. |
| 656 | */ |
| 657 | static |
| 658 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 659 | { |
| 660 | struct lttng_channel *channel = file->private_data; |
| 661 | |
| 662 | switch (cmd) { |
| 663 | case LTTNG_KERNEL_OLD_STREAM: |
| 664 | case LTTNG_KERNEL_STREAM: |
| 665 | return lttng_abi_open_stream(file); |
| 666 | case LTTNG_KERNEL_OLD_EVENT: |
| 667 | { |
| 668 | struct lttng_kernel_event *uevent_param; |
| 669 | struct lttng_kernel_old_event *old_uevent_param; |
| 670 | int ret; |
| 671 | |
| 672 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), |
| 673 | GFP_KERNEL); |
| 674 | if (!uevent_param) { |
| 675 | ret = -ENOMEM; |
| 676 | goto old_event_end; |
| 677 | } |
| 678 | old_uevent_param = kmalloc( |
| 679 | sizeof(struct lttng_kernel_old_event), |
| 680 | GFP_KERNEL); |
| 681 | if (!old_uevent_param) { |
| 682 | ret = -ENOMEM; |
| 683 | goto old_event_error_free_param; |
| 684 | } |
| 685 | if (copy_from_user(old_uevent_param, |
| 686 | (struct lttng_kernel_old_event __user *) arg, |
| 687 | sizeof(struct lttng_kernel_old_event))) { |
| 688 | ret = -EFAULT; |
| 689 | goto old_event_error_free_old_param; |
| 690 | } |
| 691 | |
| 692 | memcpy(uevent_param->name, old_uevent_param->name, |
| 693 | sizeof(uevent_param->name)); |
| 694 | uevent_param->instrumentation = |
| 695 | old_uevent_param->instrumentation; |
| 696 | |
| 697 | switch (old_uevent_param->instrumentation) { |
| 698 | case LTTNG_KERNEL_KPROBE: |
| 699 | uevent_param->u.kprobe.addr = |
| 700 | old_uevent_param->u.kprobe.addr; |
| 701 | uevent_param->u.kprobe.offset = |
| 702 | old_uevent_param->u.kprobe.offset; |
| 703 | memcpy(uevent_param->u.kprobe.symbol_name, |
| 704 | old_uevent_param->u.kprobe.symbol_name, |
| 705 | sizeof(uevent_param->u.kprobe.symbol_name)); |
| 706 | break; |
| 707 | case LTTNG_KERNEL_KRETPROBE: |
| 708 | uevent_param->u.kretprobe.addr = |
| 709 | old_uevent_param->u.kretprobe.addr; |
| 710 | uevent_param->u.kretprobe.offset = |
| 711 | old_uevent_param->u.kretprobe.offset; |
| 712 | memcpy(uevent_param->u.kretprobe.symbol_name, |
| 713 | old_uevent_param->u.kretprobe.symbol_name, |
| 714 | sizeof(uevent_param->u.kretprobe.symbol_name)); |
| 715 | break; |
| 716 | case LTTNG_KERNEL_FUNCTION: |
| 717 | memcpy(uevent_param->u.ftrace.symbol_name, |
| 718 | old_uevent_param->u.ftrace.symbol_name, |
| 719 | sizeof(uevent_param->u.ftrace.symbol_name)); |
| 720 | break; |
| 721 | default: |
| 722 | break; |
| 723 | } |
| 724 | ret = lttng_abi_create_event(file, uevent_param); |
| 725 | |
| 726 | old_event_error_free_old_param: |
| 727 | kfree(old_uevent_param); |
| 728 | old_event_error_free_param: |
| 729 | kfree(uevent_param); |
| 730 | old_event_end: |
| 731 | return ret; |
| 732 | } |
| 733 | case LTTNG_KERNEL_EVENT: |
| 734 | { |
| 735 | struct lttng_kernel_event uevent_param; |
| 736 | |
| 737 | if (copy_from_user(&uevent_param, |
| 738 | (struct lttng_kernel_event __user *) arg, |
| 739 | sizeof(uevent_param))) |
| 740 | return -EFAULT; |
| 741 | return lttng_abi_create_event(file, &uevent_param); |
| 742 | } |
| 743 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 744 | { |
| 745 | struct lttng_kernel_context *ucontext_param; |
| 746 | struct lttng_kernel_old_context *old_ucontext_param; |
| 747 | int ret; |
| 748 | |
| 749 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 750 | GFP_KERNEL); |
| 751 | if (!ucontext_param) { |
| 752 | ret = -ENOMEM; |
| 753 | goto old_ctx_end; |
| 754 | } |
| 755 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 756 | GFP_KERNEL); |
| 757 | if (!old_ucontext_param) { |
| 758 | ret = -ENOMEM; |
| 759 | goto old_ctx_error_free_param; |
| 760 | } |
| 761 | |
| 762 | if (copy_from_user(old_ucontext_param, |
| 763 | (struct lttng_kernel_old_context __user *) arg, |
| 764 | sizeof(struct lttng_kernel_old_context))) { |
| 765 | ret = -EFAULT; |
| 766 | goto old_ctx_error_free_old_param; |
| 767 | } |
| 768 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 769 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 770 | sizeof(ucontext_param->padding)); |
| 771 | /* only type that uses the union */ |
| 772 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 773 | ucontext_param->u.perf_counter.type = |
| 774 | old_ucontext_param->u.perf_counter.type; |
| 775 | ucontext_param->u.perf_counter.config = |
| 776 | old_ucontext_param->u.perf_counter.config; |
| 777 | memcpy(ucontext_param->u.perf_counter.name, |
| 778 | old_ucontext_param->u.perf_counter.name, |
| 779 | sizeof(ucontext_param->u.perf_counter.name)); |
| 780 | } |
| 781 | |
| 782 | ret = lttng_abi_add_context(file, |
| 783 | ucontext_param, |
| 784 | &channel->ctx, channel->session); |
| 785 | |
| 786 | old_ctx_error_free_old_param: |
| 787 | kfree(old_ucontext_param); |
| 788 | old_ctx_error_free_param: |
| 789 | kfree(ucontext_param); |
| 790 | old_ctx_end: |
| 791 | return ret; |
| 792 | } |
| 793 | case LTTNG_KERNEL_CONTEXT: |
| 794 | { |
| 795 | struct lttng_kernel_context ucontext_param; |
| 796 | |
| 797 | if (copy_from_user(&ucontext_param, |
| 798 | (struct lttng_kernel_context __user *) arg, |
| 799 | sizeof(ucontext_param))) |
| 800 | return -EFAULT; |
| 801 | return lttng_abi_add_context(file, |
| 802 | &ucontext_param, |
| 803 | &channel->ctx, channel->session); |
| 804 | } |
| 805 | case LTTNG_KERNEL_OLD_ENABLE: |
| 806 | case LTTNG_KERNEL_ENABLE: |
| 807 | return lttng_channel_enable(channel); |
| 808 | case LTTNG_KERNEL_OLD_DISABLE: |
| 809 | case LTTNG_KERNEL_DISABLE: |
| 810 | return lttng_channel_disable(channel); |
| 811 | default: |
| 812 | return -ENOIOCTLCMD; |
| 813 | } |
| 814 | |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 819 | * |
| 820 | * @file: the file |
| 821 | * @cmd: the command |
| 822 | * @arg: command arg |
| 823 | * |
| 824 | * This ioctl implements lttng commands: |
| 825 | * LTTNG_KERNEL_STREAM |
| 826 | * Returns an event stream file descriptor or failure. |
| 827 | * |
| 828 | * Channel and event file descriptors also hold a reference on the session. |
| 829 | */ |
| 830 | static |
| 831 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 832 | { |
| 833 | switch (cmd) { |
| 834 | case LTTNG_KERNEL_OLD_STREAM: |
| 835 | case LTTNG_KERNEL_STREAM: |
| 836 | return lttng_abi_open_stream(file); |
| 837 | default: |
| 838 | return -ENOIOCTLCMD; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 844 | * |
| 845 | * @file: the file |
| 846 | * @wait: poll table |
| 847 | */ |
| 848 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 849 | { |
| 850 | struct lttng_channel *channel = file->private_data; |
| 851 | unsigned int mask = 0; |
| 852 | |
| 853 | if (file->f_mode & FMODE_READ) { |
| 854 | poll_wait_set_exclusive(wait); |
| 855 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 856 | wait); |
| 857 | |
| 858 | if (channel->ops->is_disabled(channel->chan)) |
| 859 | return POLLERR; |
| 860 | if (channel->ops->is_finalized(channel->chan)) |
| 861 | return POLLHUP; |
| 862 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 863 | return POLLIN | POLLRDNORM; |
| 864 | return 0; |
| 865 | } |
| 866 | return mask; |
| 867 | |
| 868 | } |
| 869 | |
| 870 | static |
| 871 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 872 | { |
| 873 | struct lttng_channel *channel = file->private_data; |
| 874 | |
| 875 | if (channel) |
| 876 | fput(channel->session->file); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static const struct file_operations lttng_channel_fops = { |
| 881 | .owner = THIS_MODULE, |
| 882 | .release = lttng_channel_release, |
| 883 | .poll = lttng_channel_poll, |
| 884 | .unlocked_ioctl = lttng_channel_ioctl, |
| 885 | #ifdef CONFIG_COMPAT |
| 886 | .compat_ioctl = lttng_channel_ioctl, |
| 887 | #endif |
| 888 | }; |
| 889 | |
| 890 | static const struct file_operations lttng_metadata_fops = { |
| 891 | .owner = THIS_MODULE, |
| 892 | .release = lttng_channel_release, |
| 893 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 894 | #ifdef CONFIG_COMPAT |
| 895 | .compat_ioctl = lttng_metadata_ioctl, |
| 896 | #endif |
| 897 | }; |
| 898 | |
| 899 | /** |
| 900 | * lttng_event_ioctl - lttng syscall through ioctl |
| 901 | * |
| 902 | * @file: the file |
| 903 | * @cmd: the command |
| 904 | * @arg: command arg |
| 905 | * |
| 906 | * This ioctl implements lttng commands: |
| 907 | * LTTNG_KERNEL_CONTEXT |
| 908 | * Prepend a context field to each record of this event |
| 909 | * LTTNG_KERNEL_ENABLE |
| 910 | * Enable recording for this event (weak enable) |
| 911 | * LTTNG_KERNEL_DISABLE |
| 912 | * Disable recording for this event (strong disable) |
| 913 | */ |
| 914 | static |
| 915 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 916 | { |
| 917 | struct lttng_event *event = file->private_data; |
| 918 | |
| 919 | switch (cmd) { |
| 920 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 921 | { |
| 922 | struct lttng_kernel_context *ucontext_param; |
| 923 | struct lttng_kernel_old_context *old_ucontext_param; |
| 924 | int ret; |
| 925 | |
| 926 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 927 | GFP_KERNEL); |
| 928 | if (!ucontext_param) { |
| 929 | ret = -ENOMEM; |
| 930 | goto old_ctx_end; |
| 931 | } |
| 932 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 933 | GFP_KERNEL); |
| 934 | if (!old_ucontext_param) { |
| 935 | ret = -ENOMEM; |
| 936 | goto old_ctx_error_free_param; |
| 937 | } |
| 938 | |
| 939 | if (copy_from_user(old_ucontext_param, |
| 940 | (struct lttng_kernel_old_context __user *) arg, |
| 941 | sizeof(struct lttng_kernel_old_context))) { |
| 942 | ret = -EFAULT; |
| 943 | goto old_ctx_error_free_old_param; |
| 944 | } |
| 945 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 946 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 947 | sizeof(ucontext_param->padding)); |
| 948 | /* only type that uses the union */ |
| 949 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 950 | ucontext_param->u.perf_counter.type = |
| 951 | old_ucontext_param->u.perf_counter.type; |
| 952 | ucontext_param->u.perf_counter.config = |
| 953 | old_ucontext_param->u.perf_counter.config; |
| 954 | memcpy(ucontext_param->u.perf_counter.name, |
| 955 | old_ucontext_param->u.perf_counter.name, |
| 956 | sizeof(ucontext_param->u.perf_counter.name)); |
| 957 | } |
| 958 | |
| 959 | ret = lttng_abi_add_context(file, |
| 960 | ucontext_param, |
| 961 | &event->ctx, event->chan->session); |
| 962 | |
| 963 | old_ctx_error_free_old_param: |
| 964 | kfree(old_ucontext_param); |
| 965 | old_ctx_error_free_param: |
| 966 | kfree(ucontext_param); |
| 967 | old_ctx_end: |
| 968 | return ret; |
| 969 | } |
| 970 | case LTTNG_KERNEL_CONTEXT: |
| 971 | { |
| 972 | struct lttng_kernel_context ucontext_param; |
| 973 | |
| 974 | if (copy_from_user(&ucontext_param, |
| 975 | (struct lttng_kernel_context __user *) arg, |
| 976 | sizeof(ucontext_param))) |
| 977 | return -EFAULT; |
| 978 | return lttng_abi_add_context(file, |
| 979 | &ucontext_param, |
| 980 | &event->ctx, event->chan->session); |
| 981 | } |
| 982 | case LTTNG_KERNEL_OLD_ENABLE: |
| 983 | case LTTNG_KERNEL_ENABLE: |
| 984 | return lttng_event_enable(event); |
| 985 | case LTTNG_KERNEL_OLD_DISABLE: |
| 986 | case LTTNG_KERNEL_DISABLE: |
| 987 | return lttng_event_disable(event); |
| 988 | default: |
| 989 | return -ENOIOCTLCMD; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | static |
| 994 | int lttng_event_release(struct inode *inode, struct file *file) |
| 995 | { |
| 996 | struct lttng_event *event = file->private_data; |
| 997 | |
| 998 | if (event) |
| 999 | fput(event->chan->file); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /* TODO: filter control ioctl */ |
| 1004 | static const struct file_operations lttng_event_fops = { |
| 1005 | .owner = THIS_MODULE, |
| 1006 | .release = lttng_event_release, |
| 1007 | .unlocked_ioctl = lttng_event_ioctl, |
| 1008 | #ifdef CONFIG_COMPAT |
| 1009 | .compat_ioctl = lttng_event_ioctl, |
| 1010 | #endif |
| 1011 | }; |
| 1012 | |
| 1013 | int __init lttng_abi_init(void) |
| 1014 | { |
| 1015 | int ret = 0; |
| 1016 | |
| 1017 | wrapper_vmalloc_sync_all(); |
| 1018 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
| 1019 | <tng_fops, NULL); |
| 1020 | |
| 1021 | if (!lttng_proc_dentry) { |
| 1022 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 1023 | ret = -ENOMEM; |
| 1024 | goto error; |
| 1025 | } |
| 1026 | error: |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
| 1030 | void __exit lttng_abi_exit(void) |
| 1031 | { |
| 1032 | if (lttng_proc_dentry) |
| 1033 | remove_proc_entry("lttng", NULL); |
| 1034 | } |