| 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/ringbuffer/backend.h" |
| 49 | #include "wrapper/ringbuffer/frontend.h" |
| 50 | #include "wrapper/poll.h" |
| 51 | #include "lttng-abi.h" |
| 52 | #include "lttng-abi-old.h" |
| 53 | #include "lttng-events.h" |
| 54 | #include "lttng-tracer.h" |
| 55 | #include "lib/ringbuffer/frontend_types.h" |
| 56 | |
| 57 | /* |
| 58 | * This is LTTng's own personal way to create a system call as an external |
| 59 | * module. We use ioctl() on /proc/lttng. |
| 60 | */ |
| 61 | |
| 62 | static struct proc_dir_entry *lttng_proc_dentry; |
| 63 | static const struct file_operations lttng_fops; |
| 64 | static const struct file_operations lttng_session_fops; |
| 65 | static const struct file_operations lttng_channel_fops; |
| 66 | static const struct file_operations lttng_metadata_fops; |
| 67 | static const struct file_operations lttng_event_fops; |
| 68 | static struct file_operations lttng_stream_ring_buffer_file_operations; |
| 69 | |
| 70 | /* |
| 71 | * Teardown management: opened file descriptors keep a refcount on the module, |
| 72 | * so it can only exit when all file descriptors are closed. |
| 73 | */ |
| 74 | |
| 75 | static |
| 76 | int lttng_abi_create_session(void) |
| 77 | { |
| 78 | struct lttng_session *session; |
| 79 | struct file *session_file; |
| 80 | int session_fd, ret; |
| 81 | |
| 82 | session = lttng_session_create(); |
| 83 | if (!session) |
| 84 | return -ENOMEM; |
| 85 | session_fd = get_unused_fd(); |
| 86 | if (session_fd < 0) { |
| 87 | ret = session_fd; |
| 88 | goto fd_error; |
| 89 | } |
| 90 | session_file = anon_inode_getfile("[lttng_session]", |
| 91 | <tng_session_fops, |
| 92 | session, O_RDWR); |
| 93 | if (IS_ERR(session_file)) { |
| 94 | ret = PTR_ERR(session_file); |
| 95 | goto file_error; |
| 96 | } |
| 97 | session->file = session_file; |
| 98 | fd_install(session_fd, session_file); |
| 99 | return session_fd; |
| 100 | |
| 101 | file_error: |
| 102 | put_unused_fd(session_fd); |
| 103 | fd_error: |
| 104 | lttng_session_destroy(session); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | int lttng_abi_tracepoint_list(void) |
| 110 | { |
| 111 | struct file *tracepoint_list_file; |
| 112 | int file_fd, ret; |
| 113 | |
| 114 | file_fd = get_unused_fd(); |
| 115 | if (file_fd < 0) { |
| 116 | ret = file_fd; |
| 117 | goto fd_error; |
| 118 | } |
| 119 | |
| 120 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
| 121 | <tng_tracepoint_list_fops, |
| 122 | NULL, O_RDWR); |
| 123 | if (IS_ERR(tracepoint_list_file)) { |
| 124 | ret = PTR_ERR(tracepoint_list_file); |
| 125 | goto file_error; |
| 126 | } |
| 127 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
| 128 | if (ret < 0) |
| 129 | goto open_error; |
| 130 | fd_install(file_fd, tracepoint_list_file); |
| 131 | if (file_fd < 0) { |
| 132 | ret = file_fd; |
| 133 | goto fd_error; |
| 134 | } |
| 135 | return file_fd; |
| 136 | |
| 137 | open_error: |
| 138 | fput(tracepoint_list_file); |
| 139 | file_error: |
| 140 | put_unused_fd(file_fd); |
| 141 | fd_error: |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static |
| 146 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
| 147 | { |
| 148 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
| 149 | v->minor = LTTNG_MODULES_MINOR_VERSION; |
| 150 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | long lttng_abi_add_context(struct file *file, |
| 155 | struct lttng_kernel_context *context_param, |
| 156 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 157 | { |
| 158 | |
| 159 | if (session->been_active) |
| 160 | return -EPERM; |
| 161 | |
| 162 | switch (context_param->ctx) { |
| 163 | case LTTNG_KERNEL_CONTEXT_PID: |
| 164 | return lttng_add_pid_to_ctx(ctx); |
| 165 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 166 | return lttng_add_prio_to_ctx(ctx); |
| 167 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 168 | return lttng_add_nice_to_ctx(ctx); |
| 169 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 170 | return lttng_add_vpid_to_ctx(ctx); |
| 171 | case LTTNG_KERNEL_CONTEXT_TID: |
| 172 | return lttng_add_tid_to_ctx(ctx); |
| 173 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 174 | return lttng_add_vtid_to_ctx(ctx); |
| 175 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 176 | return lttng_add_ppid_to_ctx(ctx); |
| 177 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 178 | return lttng_add_vppid_to_ctx(ctx); |
| 179 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
| 180 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 181 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, |
| 182 | context_param->u.perf_counter.config, |
| 183 | context_param->u.perf_counter.name, |
| 184 | ctx); |
| 185 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 186 | return lttng_add_procname_to_ctx(ctx); |
| 187 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
| 188 | return lttng_add_hostname_to_ctx(ctx); |
| 189 | default: |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * lttng_ioctl - lttng syscall through ioctl |
| 196 | * |
| 197 | * @file: the file |
| 198 | * @cmd: the command |
| 199 | * @arg: command arg |
| 200 | * |
| 201 | * This ioctl implements lttng commands: |
| 202 | * LTTNG_KERNEL_SESSION |
| 203 | * Returns a LTTng trace session file descriptor |
| 204 | * LTTNG_KERNEL_TRACER_VERSION |
| 205 | * Returns the LTTng kernel tracer version |
| 206 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 207 | * Returns a file descriptor listing available tracepoints |
| 208 | * LTTNG_KERNEL_WAIT_QUIESCENT |
| 209 | * Returns after all previously running probes have completed |
| 210 | * |
| 211 | * The returned session will be deleted when its file descriptor is closed. |
| 212 | */ |
| 213 | static |
| 214 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 215 | { |
| 216 | switch (cmd) { |
| 217 | case LTTNG_KERNEL_OLD_SESSION: |
| 218 | case LTTNG_KERNEL_SESSION: |
| 219 | return lttng_abi_create_session(); |
| 220 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
| 221 | { |
| 222 | struct lttng_kernel_tracer_version v; |
| 223 | struct lttng_kernel_old_tracer_version oldv; |
| 224 | struct lttng_kernel_old_tracer_version *uversion = |
| 225 | (struct lttng_kernel_old_tracer_version __user *) arg; |
| 226 | |
| 227 | lttng_abi_tracer_version(&v); |
| 228 | oldv.major = v.major; |
| 229 | oldv.minor = v.minor; |
| 230 | oldv.patchlevel = v.patchlevel; |
| 231 | |
| 232 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) |
| 233 | return -EFAULT; |
| 234 | return 0; |
| 235 | } |
| 236 | case LTTNG_KERNEL_TRACER_VERSION: |
| 237 | { |
| 238 | struct lttng_kernel_tracer_version version; |
| 239 | struct lttng_kernel_tracer_version *uversion = |
| 240 | (struct lttng_kernel_tracer_version __user *) arg; |
| 241 | |
| 242 | lttng_abi_tracer_version(&version); |
| 243 | |
| 244 | if (copy_to_user(uversion, &version, sizeof(version))) |
| 245 | return -EFAULT; |
| 246 | return 0; |
| 247 | } |
| 248 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: |
| 249 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 250 | return lttng_abi_tracepoint_list(); |
| 251 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
| 252 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
| 253 | synchronize_trace(); |
| 254 | return 0; |
| 255 | case LTTNG_KERNEL_OLD_CALIBRATE: |
| 256 | { |
| 257 | struct lttng_kernel_old_calibrate __user *ucalibrate = |
| 258 | (struct lttng_kernel_old_calibrate __user *) arg; |
| 259 | struct lttng_kernel_old_calibrate old_calibrate; |
| 260 | struct lttng_kernel_calibrate calibrate; |
| 261 | int ret; |
| 262 | |
| 263 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) |
| 264 | return -EFAULT; |
| 265 | calibrate.type = old_calibrate.type; |
| 266 | ret = lttng_calibrate(&calibrate); |
| 267 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) |
| 268 | return -EFAULT; |
| 269 | return ret; |
| 270 | } |
| 271 | case LTTNG_KERNEL_CALIBRATE: |
| 272 | { |
| 273 | struct lttng_kernel_calibrate __user *ucalibrate = |
| 274 | (struct lttng_kernel_calibrate __user *) arg; |
| 275 | struct lttng_kernel_calibrate calibrate; |
| 276 | int ret; |
| 277 | |
| 278 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) |
| 279 | return -EFAULT; |
| 280 | ret = lttng_calibrate(&calibrate); |
| 281 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) |
| 282 | return -EFAULT; |
| 283 | return ret; |
| 284 | } |
| 285 | default: |
| 286 | return -ENOIOCTLCMD; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static const struct file_operations lttng_fops = { |
| 291 | .owner = THIS_MODULE, |
| 292 | .unlocked_ioctl = lttng_ioctl, |
| 293 | #ifdef CONFIG_COMPAT |
| 294 | .compat_ioctl = lttng_ioctl, |
| 295 | #endif |
| 296 | }; |
| 297 | |
| 298 | static |
| 299 | int lttng_abi_create_channel(struct file *session_file, |
| 300 | struct lttng_kernel_channel *chan_param, |
| 301 | enum channel_type channel_type) |
| 302 | { |
| 303 | struct lttng_session *session = session_file->private_data; |
| 304 | const struct file_operations *fops = NULL; |
| 305 | const char *transport_name; |
| 306 | struct lttng_channel *chan; |
| 307 | struct file *chan_file; |
| 308 | int chan_fd; |
| 309 | int ret = 0; |
| 310 | |
| 311 | chan_fd = get_unused_fd(); |
| 312 | if (chan_fd < 0) { |
| 313 | ret = chan_fd; |
| 314 | goto fd_error; |
| 315 | } |
| 316 | switch (channel_type) { |
| 317 | case PER_CPU_CHANNEL: |
| 318 | fops = <tng_channel_fops; |
| 319 | break; |
| 320 | case METADATA_CHANNEL: |
| 321 | fops = <tng_metadata_fops; |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 326 | fops, |
| 327 | NULL, O_RDWR); |
| 328 | if (IS_ERR(chan_file)) { |
| 329 | ret = PTR_ERR(chan_file); |
| 330 | goto file_error; |
| 331 | } |
| 332 | switch (channel_type) { |
| 333 | case PER_CPU_CHANNEL: |
| 334 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
| 335 | transport_name = chan_param->overwrite ? |
| 336 | "relay-overwrite" : "relay-discard"; |
| 337 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
| 338 | transport_name = chan_param->overwrite ? |
| 339 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 340 | } else { |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | break; |
| 344 | case METADATA_CHANNEL: |
| 345 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
| 346 | transport_name = "relay-metadata"; |
| 347 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
| 348 | transport_name = "relay-metadata-mmap"; |
| 349 | else |
| 350 | return -EINVAL; |
| 351 | break; |
| 352 | default: |
| 353 | transport_name = "<unknown>"; |
| 354 | break; |
| 355 | } |
| 356 | /* |
| 357 | * We tolerate no failure path after channel creation. It will stay |
| 358 | * invariant for the rest of the session. |
| 359 | */ |
| 360 | chan = lttng_channel_create(session, transport_name, NULL, |
| 361 | chan_param->subbuf_size, |
| 362 | chan_param->num_subbuf, |
| 363 | chan_param->switch_timer_interval, |
| 364 | chan_param->read_timer_interval, |
| 365 | channel_type); |
| 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 | atomic_long_inc(&session_file->f_count); |
| 374 | |
| 375 | return chan_fd; |
| 376 | |
| 377 | chan_error: |
| 378 | fput(chan_file); |
| 379 | file_error: |
| 380 | put_unused_fd(chan_fd); |
| 381 | fd_error: |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * lttng_session_ioctl - lttng session fd ioctl |
| 387 | * |
| 388 | * @file: the file |
| 389 | * @cmd: the command |
| 390 | * @arg: command arg |
| 391 | * |
| 392 | * This ioctl implements lttng commands: |
| 393 | * LTTNG_KERNEL_CHANNEL |
| 394 | * Returns a LTTng channel file descriptor |
| 395 | * LTTNG_KERNEL_ENABLE |
| 396 | * Enables tracing for a session (weak enable) |
| 397 | * LTTNG_KERNEL_DISABLE |
| 398 | * Disables tracing for a session (strong disable) |
| 399 | * LTTNG_KERNEL_METADATA |
| 400 | * Returns a LTTng metadata file descriptor |
| 401 | * |
| 402 | * The returned channel will be deleted when its file descriptor is closed. |
| 403 | */ |
| 404 | static |
| 405 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 406 | { |
| 407 | struct lttng_session *session = file->private_data; |
| 408 | |
| 409 | switch (cmd) { |
| 410 | case LTTNG_KERNEL_OLD_CHANNEL: |
| 411 | { |
| 412 | struct lttng_kernel_channel chan_param; |
| 413 | struct lttng_kernel_old_channel old_chan_param; |
| 414 | |
| 415 | if (copy_from_user(&old_chan_param, |
| 416 | (struct lttng_kernel_old_channel __user *) arg, |
| 417 | sizeof(struct lttng_kernel_old_channel))) |
| 418 | return -EFAULT; |
| 419 | chan_param.overwrite = old_chan_param.overwrite; |
| 420 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 421 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 422 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 423 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 424 | chan_param.output = old_chan_param.output; |
| 425 | |
| 426 | return lttng_abi_create_channel(file, &chan_param, |
| 427 | PER_CPU_CHANNEL); |
| 428 | } |
| 429 | case LTTNG_KERNEL_CHANNEL: |
| 430 | { |
| 431 | struct lttng_kernel_channel chan_param; |
| 432 | |
| 433 | if (copy_from_user(&chan_param, |
| 434 | (struct lttng_kernel_channel __user *) arg, |
| 435 | sizeof(struct lttng_kernel_channel))) |
| 436 | return -EFAULT; |
| 437 | return lttng_abi_create_channel(file, &chan_param, |
| 438 | PER_CPU_CHANNEL); |
| 439 | } |
| 440 | case LTTNG_KERNEL_OLD_SESSION_START: |
| 441 | case LTTNG_KERNEL_OLD_ENABLE: |
| 442 | case LTTNG_KERNEL_SESSION_START: |
| 443 | case LTTNG_KERNEL_ENABLE: |
| 444 | return lttng_session_enable(session); |
| 445 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
| 446 | case LTTNG_KERNEL_OLD_DISABLE: |
| 447 | case LTTNG_KERNEL_SESSION_STOP: |
| 448 | case LTTNG_KERNEL_DISABLE: |
| 449 | return lttng_session_disable(session); |
| 450 | case LTTNG_KERNEL_OLD_METADATA: |
| 451 | { |
| 452 | struct lttng_kernel_channel chan_param; |
| 453 | struct lttng_kernel_old_channel old_chan_param; |
| 454 | |
| 455 | if (copy_from_user(&old_chan_param, |
| 456 | (struct lttng_kernel_old_channel __user *) arg, |
| 457 | sizeof(struct lttng_kernel_old_channel))) |
| 458 | return -EFAULT; |
| 459 | chan_param.overwrite = old_chan_param.overwrite; |
| 460 | chan_param.subbuf_size = old_chan_param.subbuf_size; |
| 461 | chan_param.num_subbuf = old_chan_param.num_subbuf; |
| 462 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; |
| 463 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; |
| 464 | chan_param.output = old_chan_param.output; |
| 465 | |
| 466 | return lttng_abi_create_channel(file, &chan_param, |
| 467 | METADATA_CHANNEL); |
| 468 | } |
| 469 | case LTTNG_KERNEL_METADATA: |
| 470 | { |
| 471 | struct lttng_kernel_channel chan_param; |
| 472 | |
| 473 | if (copy_from_user(&chan_param, |
| 474 | (struct lttng_kernel_channel __user *) arg, |
| 475 | sizeof(struct lttng_kernel_channel))) |
| 476 | return -EFAULT; |
| 477 | return lttng_abi_create_channel(file, &chan_param, |
| 478 | METADATA_CHANNEL); |
| 479 | } |
| 480 | default: |
| 481 | return -ENOIOCTLCMD; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Called when the last file reference is dropped. |
| 487 | * |
| 488 | * Big fat note: channels and events are invariant for the whole session after |
| 489 | * their creation. So this session destruction also destroys all channel and |
| 490 | * event structures specific to this session (they are not destroyed when their |
| 491 | * individual file is released). |
| 492 | */ |
| 493 | static |
| 494 | int lttng_session_release(struct inode *inode, struct file *file) |
| 495 | { |
| 496 | struct lttng_session *session = file->private_data; |
| 497 | |
| 498 | if (session) |
| 499 | lttng_session_destroy(session); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static const struct file_operations lttng_session_fops = { |
| 504 | .owner = THIS_MODULE, |
| 505 | .release = lttng_session_release, |
| 506 | .unlocked_ioctl = lttng_session_ioctl, |
| 507 | #ifdef CONFIG_COMPAT |
| 508 | .compat_ioctl = lttng_session_ioctl, |
| 509 | #endif |
| 510 | }; |
| 511 | |
| 512 | /** |
| 513 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation |
| 514 | * @filp: the file |
| 515 | * @wait: poll table |
| 516 | * |
| 517 | * Handles the poll operations for the metadata channels. |
| 518 | */ |
| 519 | static |
| 520 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
| 521 | poll_table *wait) |
| 522 | { |
| 523 | struct lttng_metadata_stream *stream = filp->private_data; |
| 524 | struct lib_ring_buffer *buf = stream->priv; |
| 525 | int finalized; |
| 526 | unsigned int mask = 0; |
| 527 | |
| 528 | if (filp->f_mode & FMODE_READ) { |
| 529 | poll_wait_set_exclusive(wait); |
| 530 | poll_wait(filp, &stream->read_wait, wait); |
| 531 | |
| 532 | finalized = stream->finalized; |
| 533 | |
| 534 | /* |
| 535 | * lib_ring_buffer_is_finalized() contains a smp_rmb() |
| 536 | * ordering finalized load before offsets loads. |
| 537 | */ |
| 538 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); |
| 539 | |
| 540 | if (finalized) |
| 541 | mask |= POLLHUP; |
| 542 | |
| 543 | if (stream->metadata_cache->metadata_written > |
| 544 | stream->metadata_out) |
| 545 | mask |= POLLIN; |
| 546 | } |
| 547 | |
| 548 | return mask; |
| 549 | } |
| 550 | |
| 551 | static |
| 552 | void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp, |
| 553 | unsigned int cmd, unsigned long arg) |
| 554 | { |
| 555 | struct lttng_metadata_stream *stream = filp->private_data; |
| 556 | |
| 557 | stream->metadata_out = stream->metadata_in; |
| 558 | } |
| 559 | |
| 560 | static |
| 561 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, |
| 562 | unsigned int cmd, unsigned long arg) |
| 563 | { |
| 564 | int ret; |
| 565 | struct lttng_metadata_stream *stream = filp->private_data; |
| 566 | struct lib_ring_buffer *buf = stream->priv; |
| 567 | |
| 568 | switch (cmd) { |
| 569 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 570 | { |
| 571 | struct lttng_metadata_stream *stream = filp->private_data; |
| 572 | struct lib_ring_buffer *buf = stream->priv; |
| 573 | struct channel *chan = buf->backend.chan; |
| 574 | |
| 575 | ret = lttng_metadata_output_channel(stream, chan); |
| 576 | if (ret > 0) { |
| 577 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 578 | ret = 0; |
| 579 | } else if (ret < 0) |
| 580 | goto err; |
| 581 | break; |
| 582 | } |
| 583 | case RING_BUFFER_GET_SUBBUF: |
| 584 | { |
| 585 | /* |
| 586 | * Random access is not allowed for metadata channel. |
| 587 | */ |
| 588 | return -ENOSYS; |
| 589 | } |
| 590 | case RING_BUFFER_FLUSH: |
| 591 | { |
| 592 | struct lttng_metadata_stream *stream = filp->private_data; |
| 593 | struct lib_ring_buffer *buf = stream->priv; |
| 594 | struct channel *chan = buf->backend.chan; |
| 595 | |
| 596 | /* |
| 597 | * Before doing the actual ring buffer flush, write up to one |
| 598 | * packet of metadata in the ring buffer. |
| 599 | */ |
| 600 | ret = lttng_metadata_output_channel(stream, chan); |
| 601 | if (ret < 0) |
| 602 | goto err; |
| 603 | break; |
| 604 | } |
| 605 | default: |
| 606 | break; |
| 607 | } |
| 608 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 609 | |
| 610 | /* Performing lib ring buffer ioctl after our own. */ |
| 611 | ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf); |
| 612 | if (ret < 0) |
| 613 | goto err; |
| 614 | |
| 615 | switch (cmd) { |
| 616 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 617 | { |
| 618 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 619 | cmd, arg); |
| 620 | break; |
| 621 | } |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | err: |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | #ifdef CONFIG_COMPAT |
| 630 | static |
| 631 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, |
| 632 | unsigned int cmd, unsigned long arg) |
| 633 | { |
| 634 | int ret; |
| 635 | struct lttng_metadata_stream *stream = filp->private_data; |
| 636 | struct lib_ring_buffer *buf = stream->priv; |
| 637 | |
| 638 | switch (cmd) { |
| 639 | case RING_BUFFER_GET_NEXT_SUBBUF: |
| 640 | { |
| 641 | struct lttng_metadata_stream *stream = filp->private_data; |
| 642 | struct lib_ring_buffer *buf = stream->priv; |
| 643 | struct channel *chan = buf->backend.chan; |
| 644 | |
| 645 | ret = lttng_metadata_output_channel(stream, chan); |
| 646 | if (ret > 0) { |
| 647 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 648 | ret = 0; |
| 649 | } else if (ret < 0) |
| 650 | goto err; |
| 651 | break; |
| 652 | } |
| 653 | case RING_BUFFER_GET_SUBBUF: |
| 654 | { |
| 655 | /* |
| 656 | * Random access is not allowed for metadata channel. |
| 657 | */ |
| 658 | return -ENOSYS; |
| 659 | } |
| 660 | default: |
| 661 | break; |
| 662 | } |
| 663 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
| 664 | |
| 665 | /* Performing lib ring buffer ioctl after our own. */ |
| 666 | ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); |
| 667 | if (ret < 0) |
| 668 | goto err; |
| 669 | |
| 670 | switch (cmd) { |
| 671 | case RING_BUFFER_PUT_NEXT_SUBBUF: |
| 672 | { |
| 673 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, |
| 674 | cmd, arg); |
| 675 | break; |
| 676 | } |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | err: |
| 681 | return ret; |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | /* |
| 686 | * This is not used by anonymous file descriptors. This code is left |
| 687 | * there if we ever want to implement an inode with open() operation. |
| 688 | */ |
| 689 | static |
| 690 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) |
| 691 | { |
| 692 | struct lttng_metadata_stream *stream = inode->i_private; |
| 693 | struct lib_ring_buffer *buf = stream->priv; |
| 694 | |
| 695 | file->private_data = buf; |
| 696 | /* |
| 697 | * Since life-time of metadata cache differs from that of |
| 698 | * session, we need to keep our own reference on the transport. |
| 699 | */ |
| 700 | if (!try_module_get(stream->transport->owner)) { |
| 701 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 702 | return -EBUSY; |
| 703 | } |
| 704 | return lib_ring_buffer_open(inode, file, buf); |
| 705 | } |
| 706 | |
| 707 | static |
| 708 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) |
| 709 | { |
| 710 | struct lttng_metadata_stream *stream = file->private_data; |
| 711 | struct lib_ring_buffer *buf = stream->priv; |
| 712 | |
| 713 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); |
| 714 | module_put(stream->transport->owner); |
| 715 | return lib_ring_buffer_release(inode, file, buf); |
| 716 | } |
| 717 | |
| 718 | static |
| 719 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, |
| 720 | struct pipe_inode_info *pipe, size_t len, |
| 721 | unsigned int flags) |
| 722 | { |
| 723 | struct lttng_metadata_stream *stream = in->private_data; |
| 724 | struct lib_ring_buffer *buf = stream->priv; |
| 725 | |
| 726 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, |
| 727 | flags, buf); |
| 728 | } |
| 729 | |
| 730 | static |
| 731 | int lttng_metadata_ring_buffer_mmap(struct file *filp, |
| 732 | struct vm_area_struct *vma) |
| 733 | { |
| 734 | struct lttng_metadata_stream *stream = filp->private_data; |
| 735 | struct lib_ring_buffer *buf = stream->priv; |
| 736 | |
| 737 | return lib_ring_buffer_mmap(filp, vma, buf); |
| 738 | } |
| 739 | |
| 740 | static |
| 741 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { |
| 742 | .owner = THIS_MODULE, |
| 743 | .open = lttng_metadata_ring_buffer_open, |
| 744 | .release = lttng_metadata_ring_buffer_release, |
| 745 | .poll = lttng_metadata_ring_buffer_poll, |
| 746 | .splice_read = lttng_metadata_ring_buffer_splice_read, |
| 747 | .mmap = lttng_metadata_ring_buffer_mmap, |
| 748 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, |
| 749 | .llseek = vfs_lib_ring_buffer_no_llseek, |
| 750 | #ifdef CONFIG_COMPAT |
| 751 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, |
| 752 | #endif |
| 753 | }; |
| 754 | |
| 755 | static |
| 756 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, |
| 757 | const struct file_operations *fops) |
| 758 | { |
| 759 | int stream_fd, ret; |
| 760 | struct file *stream_file; |
| 761 | |
| 762 | stream_fd = get_unused_fd(); |
| 763 | if (stream_fd < 0) { |
| 764 | ret = stream_fd; |
| 765 | goto fd_error; |
| 766 | } |
| 767 | stream_file = anon_inode_getfile("[lttng_stream]", fops, |
| 768 | stream_priv, O_RDWR); |
| 769 | if (IS_ERR(stream_file)) { |
| 770 | ret = PTR_ERR(stream_file); |
| 771 | goto file_error; |
| 772 | } |
| 773 | /* |
| 774 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 775 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 776 | * file descriptor, so we set FMODE_PREAD here. |
| 777 | */ |
| 778 | stream_file->f_mode |= FMODE_PREAD; |
| 779 | fd_install(stream_fd, stream_file); |
| 780 | /* |
| 781 | * The stream holds a reference to the channel within the generic ring |
| 782 | * buffer library, so no need to hold a refcount on the channel and |
| 783 | * session files here. |
| 784 | */ |
| 785 | return stream_fd; |
| 786 | |
| 787 | file_error: |
| 788 | put_unused_fd(stream_fd); |
| 789 | fd_error: |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | static |
| 794 | int lttng_abi_open_stream(struct file *channel_file) |
| 795 | { |
| 796 | struct lttng_channel *channel = channel_file->private_data; |
| 797 | struct lib_ring_buffer *buf; |
| 798 | int ret; |
| 799 | void *stream_priv; |
| 800 | |
| 801 | buf = channel->ops->buffer_read_open(channel->chan); |
| 802 | if (!buf) |
| 803 | return -ENOENT; |
| 804 | |
| 805 | stream_priv = buf; |
| 806 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 807 | <tng_stream_ring_buffer_file_operations); |
| 808 | if (ret < 0) |
| 809 | goto fd_error; |
| 810 | |
| 811 | return ret; |
| 812 | |
| 813 | fd_error: |
| 814 | channel->ops->buffer_read_close(buf); |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | static |
| 819 | int lttng_abi_open_metadata_stream(struct file *channel_file) |
| 820 | { |
| 821 | struct lttng_channel *channel = channel_file->private_data; |
| 822 | struct lttng_session *session = channel->session; |
| 823 | struct lib_ring_buffer *buf; |
| 824 | int ret; |
| 825 | struct lttng_metadata_stream *metadata_stream; |
| 826 | void *stream_priv; |
| 827 | |
| 828 | buf = channel->ops->buffer_read_open(channel->chan); |
| 829 | if (!buf) |
| 830 | return -ENOENT; |
| 831 | |
| 832 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), |
| 833 | GFP_KERNEL); |
| 834 | if (!metadata_stream) { |
| 835 | ret = -ENOMEM; |
| 836 | goto nomem; |
| 837 | } |
| 838 | metadata_stream->metadata_cache = session->metadata_cache; |
| 839 | init_waitqueue_head(&metadata_stream->read_wait); |
| 840 | metadata_stream->priv = buf; |
| 841 | stream_priv = metadata_stream; |
| 842 | metadata_stream->transport = channel->transport; |
| 843 | |
| 844 | /* |
| 845 | * Since life-time of metadata cache differs from that of |
| 846 | * session, we need to keep our own reference on the transport. |
| 847 | */ |
| 848 | if (!try_module_get(metadata_stream->transport->owner)) { |
| 849 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 850 | ret = -EINVAL; |
| 851 | goto notransport; |
| 852 | } |
| 853 | |
| 854 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
| 855 | <tng_metadata_ring_buffer_file_operations); |
| 856 | if (ret < 0) |
| 857 | goto fd_error; |
| 858 | |
| 859 | kref_get(&session->metadata_cache->refcount); |
| 860 | list_add(&metadata_stream->list, |
| 861 | &session->metadata_cache->metadata_stream); |
| 862 | return ret; |
| 863 | |
| 864 | fd_error: |
| 865 | module_put(metadata_stream->transport->owner); |
| 866 | notransport: |
| 867 | kfree(metadata_stream); |
| 868 | nomem: |
| 869 | channel->ops->buffer_read_close(buf); |
| 870 | return ret; |
| 871 | } |
| 872 | |
| 873 | static |
| 874 | int lttng_abi_create_event(struct file *channel_file, |
| 875 | struct lttng_kernel_event *event_param) |
| 876 | { |
| 877 | struct lttng_channel *channel = channel_file->private_data; |
| 878 | struct lttng_event *event; |
| 879 | int event_fd, ret; |
| 880 | struct file *event_file; |
| 881 | |
| 882 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 883 | switch (event_param->instrumentation) { |
| 884 | case LTTNG_KERNEL_KRETPROBE: |
| 885 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 886 | break; |
| 887 | case LTTNG_KERNEL_KPROBE: |
| 888 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 889 | break; |
| 890 | case LTTNG_KERNEL_FUNCTION: |
| 891 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 892 | break; |
| 893 | default: |
| 894 | break; |
| 895 | } |
| 896 | switch (event_param->instrumentation) { |
| 897 | default: |
| 898 | event_fd = get_unused_fd(); |
| 899 | if (event_fd < 0) { |
| 900 | ret = event_fd; |
| 901 | goto fd_error; |
| 902 | } |
| 903 | event_file = anon_inode_getfile("[lttng_event]", |
| 904 | <tng_event_fops, |
| 905 | NULL, O_RDWR); |
| 906 | if (IS_ERR(event_file)) { |
| 907 | ret = PTR_ERR(event_file); |
| 908 | goto file_error; |
| 909 | } |
| 910 | /* |
| 911 | * We tolerate no failure path after event creation. It |
| 912 | * will stay invariant for the rest of the session. |
| 913 | */ |
| 914 | event = lttng_event_create(channel, event_param, NULL, NULL); |
| 915 | if (!event) { |
| 916 | ret = -EINVAL; |
| 917 | goto event_error; |
| 918 | } |
| 919 | event_file->private_data = event; |
| 920 | fd_install(event_fd, event_file); |
| 921 | /* The event holds a reference on the channel */ |
| 922 | atomic_long_inc(&channel_file->f_count); |
| 923 | break; |
| 924 | case LTTNG_KERNEL_SYSCALL: |
| 925 | /* |
| 926 | * Only all-syscall tracing supported for now. |
| 927 | */ |
| 928 | if (event_param->name[0] != '\0') |
| 929 | return -EINVAL; |
| 930 | ret = lttng_syscalls_register(channel, NULL); |
| 931 | if (ret) |
| 932 | goto fd_error; |
| 933 | event_fd = 0; |
| 934 | break; |
| 935 | } |
| 936 | return event_fd; |
| 937 | |
| 938 | event_error: |
| 939 | fput(event_file); |
| 940 | file_error: |
| 941 | put_unused_fd(event_fd); |
| 942 | fd_error: |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 948 | * |
| 949 | * @file: the file |
| 950 | * @cmd: the command |
| 951 | * @arg: command arg |
| 952 | * |
| 953 | * This ioctl implements lttng commands: |
| 954 | * LTTNG_KERNEL_STREAM |
| 955 | * Returns an event stream file descriptor or failure. |
| 956 | * (typically, one event stream records events from one CPU) |
| 957 | * LTTNG_KERNEL_EVENT |
| 958 | * Returns an event file descriptor or failure. |
| 959 | * LTTNG_KERNEL_CONTEXT |
| 960 | * Prepend a context field to each event in the channel |
| 961 | * LTTNG_KERNEL_ENABLE |
| 962 | * Enable recording for events in this channel (weak enable) |
| 963 | * LTTNG_KERNEL_DISABLE |
| 964 | * Disable recording for events in this channel (strong disable) |
| 965 | * |
| 966 | * Channel and event file descriptors also hold a reference on the session. |
| 967 | */ |
| 968 | static |
| 969 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 970 | { |
| 971 | struct lttng_channel *channel = file->private_data; |
| 972 | |
| 973 | switch (cmd) { |
| 974 | case LTTNG_KERNEL_OLD_STREAM: |
| 975 | case LTTNG_KERNEL_STREAM: |
| 976 | return lttng_abi_open_stream(file); |
| 977 | case LTTNG_KERNEL_OLD_EVENT: |
| 978 | { |
| 979 | struct lttng_kernel_event *uevent_param; |
| 980 | struct lttng_kernel_old_event *old_uevent_param; |
| 981 | int ret; |
| 982 | |
| 983 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), |
| 984 | GFP_KERNEL); |
| 985 | if (!uevent_param) { |
| 986 | ret = -ENOMEM; |
| 987 | goto old_event_end; |
| 988 | } |
| 989 | old_uevent_param = kmalloc( |
| 990 | sizeof(struct lttng_kernel_old_event), |
| 991 | GFP_KERNEL); |
| 992 | if (!old_uevent_param) { |
| 993 | ret = -ENOMEM; |
| 994 | goto old_event_error_free_param; |
| 995 | } |
| 996 | if (copy_from_user(old_uevent_param, |
| 997 | (struct lttng_kernel_old_event __user *) arg, |
| 998 | sizeof(struct lttng_kernel_old_event))) { |
| 999 | ret = -EFAULT; |
| 1000 | goto old_event_error_free_old_param; |
| 1001 | } |
| 1002 | |
| 1003 | memcpy(uevent_param->name, old_uevent_param->name, |
| 1004 | sizeof(uevent_param->name)); |
| 1005 | uevent_param->instrumentation = |
| 1006 | old_uevent_param->instrumentation; |
| 1007 | |
| 1008 | switch (old_uevent_param->instrumentation) { |
| 1009 | case LTTNG_KERNEL_KPROBE: |
| 1010 | uevent_param->u.kprobe.addr = |
| 1011 | old_uevent_param->u.kprobe.addr; |
| 1012 | uevent_param->u.kprobe.offset = |
| 1013 | old_uevent_param->u.kprobe.offset; |
| 1014 | memcpy(uevent_param->u.kprobe.symbol_name, |
| 1015 | old_uevent_param->u.kprobe.symbol_name, |
| 1016 | sizeof(uevent_param->u.kprobe.symbol_name)); |
| 1017 | break; |
| 1018 | case LTTNG_KERNEL_KRETPROBE: |
| 1019 | uevent_param->u.kretprobe.addr = |
| 1020 | old_uevent_param->u.kretprobe.addr; |
| 1021 | uevent_param->u.kretprobe.offset = |
| 1022 | old_uevent_param->u.kretprobe.offset; |
| 1023 | memcpy(uevent_param->u.kretprobe.symbol_name, |
| 1024 | old_uevent_param->u.kretprobe.symbol_name, |
| 1025 | sizeof(uevent_param->u.kretprobe.symbol_name)); |
| 1026 | break; |
| 1027 | case LTTNG_KERNEL_FUNCTION: |
| 1028 | memcpy(uevent_param->u.ftrace.symbol_name, |
| 1029 | old_uevent_param->u.ftrace.symbol_name, |
| 1030 | sizeof(uevent_param->u.ftrace.symbol_name)); |
| 1031 | break; |
| 1032 | default: |
| 1033 | break; |
| 1034 | } |
| 1035 | ret = lttng_abi_create_event(file, uevent_param); |
| 1036 | |
| 1037 | old_event_error_free_old_param: |
| 1038 | kfree(old_uevent_param); |
| 1039 | old_event_error_free_param: |
| 1040 | kfree(uevent_param); |
| 1041 | old_event_end: |
| 1042 | return ret; |
| 1043 | } |
| 1044 | case LTTNG_KERNEL_EVENT: |
| 1045 | { |
| 1046 | struct lttng_kernel_event uevent_param; |
| 1047 | |
| 1048 | if (copy_from_user(&uevent_param, |
| 1049 | (struct lttng_kernel_event __user *) arg, |
| 1050 | sizeof(uevent_param))) |
| 1051 | return -EFAULT; |
| 1052 | return lttng_abi_create_event(file, &uevent_param); |
| 1053 | } |
| 1054 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 1055 | { |
| 1056 | struct lttng_kernel_context *ucontext_param; |
| 1057 | struct lttng_kernel_old_context *old_ucontext_param; |
| 1058 | int ret; |
| 1059 | |
| 1060 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 1061 | GFP_KERNEL); |
| 1062 | if (!ucontext_param) { |
| 1063 | ret = -ENOMEM; |
| 1064 | goto old_ctx_end; |
| 1065 | } |
| 1066 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 1067 | GFP_KERNEL); |
| 1068 | if (!old_ucontext_param) { |
| 1069 | ret = -ENOMEM; |
| 1070 | goto old_ctx_error_free_param; |
| 1071 | } |
| 1072 | |
| 1073 | if (copy_from_user(old_ucontext_param, |
| 1074 | (struct lttng_kernel_old_context __user *) arg, |
| 1075 | sizeof(struct lttng_kernel_old_context))) { |
| 1076 | ret = -EFAULT; |
| 1077 | goto old_ctx_error_free_old_param; |
| 1078 | } |
| 1079 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 1080 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 1081 | sizeof(ucontext_param->padding)); |
| 1082 | /* only type that uses the union */ |
| 1083 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 1084 | ucontext_param->u.perf_counter.type = |
| 1085 | old_ucontext_param->u.perf_counter.type; |
| 1086 | ucontext_param->u.perf_counter.config = |
| 1087 | old_ucontext_param->u.perf_counter.config; |
| 1088 | memcpy(ucontext_param->u.perf_counter.name, |
| 1089 | old_ucontext_param->u.perf_counter.name, |
| 1090 | sizeof(ucontext_param->u.perf_counter.name)); |
| 1091 | } |
| 1092 | |
| 1093 | ret = lttng_abi_add_context(file, |
| 1094 | ucontext_param, |
| 1095 | &channel->ctx, channel->session); |
| 1096 | |
| 1097 | old_ctx_error_free_old_param: |
| 1098 | kfree(old_ucontext_param); |
| 1099 | old_ctx_error_free_param: |
| 1100 | kfree(ucontext_param); |
| 1101 | old_ctx_end: |
| 1102 | return ret; |
| 1103 | } |
| 1104 | case LTTNG_KERNEL_CONTEXT: |
| 1105 | { |
| 1106 | struct lttng_kernel_context ucontext_param; |
| 1107 | |
| 1108 | if (copy_from_user(&ucontext_param, |
| 1109 | (struct lttng_kernel_context __user *) arg, |
| 1110 | sizeof(ucontext_param))) |
| 1111 | return -EFAULT; |
| 1112 | return lttng_abi_add_context(file, |
| 1113 | &ucontext_param, |
| 1114 | &channel->ctx, channel->session); |
| 1115 | } |
| 1116 | case LTTNG_KERNEL_OLD_ENABLE: |
| 1117 | case LTTNG_KERNEL_ENABLE: |
| 1118 | return lttng_channel_enable(channel); |
| 1119 | case LTTNG_KERNEL_OLD_DISABLE: |
| 1120 | case LTTNG_KERNEL_DISABLE: |
| 1121 | return lttng_channel_disable(channel); |
| 1122 | default: |
| 1123 | return -ENOIOCTLCMD; |
| 1124 | } |
| 1125 | |
| 1126 | } |
| 1127 | |
| 1128 | /** |
| 1129 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 1130 | * |
| 1131 | * @file: the file |
| 1132 | * @cmd: the command |
| 1133 | * @arg: command arg |
| 1134 | * |
| 1135 | * This ioctl implements lttng commands: |
| 1136 | * LTTNG_KERNEL_STREAM |
| 1137 | * Returns an event stream file descriptor or failure. |
| 1138 | * |
| 1139 | * Channel and event file descriptors also hold a reference on the session. |
| 1140 | */ |
| 1141 | static |
| 1142 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1143 | { |
| 1144 | switch (cmd) { |
| 1145 | case LTTNG_KERNEL_OLD_STREAM: |
| 1146 | case LTTNG_KERNEL_STREAM: |
| 1147 | return lttng_abi_open_metadata_stream(file); |
| 1148 | default: |
| 1149 | return -ENOIOCTLCMD; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 1155 | * |
| 1156 | * @file: the file |
| 1157 | * @wait: poll table |
| 1158 | */ |
| 1159 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 1160 | { |
| 1161 | struct lttng_channel *channel = file->private_data; |
| 1162 | unsigned int mask = 0; |
| 1163 | |
| 1164 | if (file->f_mode & FMODE_READ) { |
| 1165 | poll_wait_set_exclusive(wait); |
| 1166 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 1167 | wait); |
| 1168 | |
| 1169 | if (channel->ops->is_disabled(channel->chan)) |
| 1170 | return POLLERR; |
| 1171 | if (channel->ops->is_finalized(channel->chan)) |
| 1172 | return POLLHUP; |
| 1173 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 1174 | return POLLIN | POLLRDNORM; |
| 1175 | return 0; |
| 1176 | } |
| 1177 | return mask; |
| 1178 | |
| 1179 | } |
| 1180 | |
| 1181 | static |
| 1182 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 1183 | { |
| 1184 | struct lttng_channel *channel = file->private_data; |
| 1185 | |
| 1186 | if (channel) |
| 1187 | fput(channel->session->file); |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | static |
| 1192 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) |
| 1193 | { |
| 1194 | struct lttng_channel *channel = file->private_data; |
| 1195 | |
| 1196 | if (channel) { |
| 1197 | lttng_metadata_channel_destroy(channel); |
| 1198 | fput(channel->session->file); |
| 1199 | } |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static const struct file_operations lttng_channel_fops = { |
| 1205 | .owner = THIS_MODULE, |
| 1206 | .release = lttng_channel_release, |
| 1207 | .poll = lttng_channel_poll, |
| 1208 | .unlocked_ioctl = lttng_channel_ioctl, |
| 1209 | #ifdef CONFIG_COMPAT |
| 1210 | .compat_ioctl = lttng_channel_ioctl, |
| 1211 | #endif |
| 1212 | }; |
| 1213 | |
| 1214 | static const struct file_operations lttng_metadata_fops = { |
| 1215 | .owner = THIS_MODULE, |
| 1216 | .release = lttng_metadata_channel_release, |
| 1217 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 1218 | #ifdef CONFIG_COMPAT |
| 1219 | .compat_ioctl = lttng_metadata_ioctl, |
| 1220 | #endif |
| 1221 | }; |
| 1222 | |
| 1223 | /** |
| 1224 | * lttng_event_ioctl - lttng syscall through ioctl |
| 1225 | * |
| 1226 | * @file: the file |
| 1227 | * @cmd: the command |
| 1228 | * @arg: command arg |
| 1229 | * |
| 1230 | * This ioctl implements lttng commands: |
| 1231 | * LTTNG_KERNEL_CONTEXT |
| 1232 | * Prepend a context field to each record of this event |
| 1233 | * LTTNG_KERNEL_ENABLE |
| 1234 | * Enable recording for this event (weak enable) |
| 1235 | * LTTNG_KERNEL_DISABLE |
| 1236 | * Disable recording for this event (strong disable) |
| 1237 | */ |
| 1238 | static |
| 1239 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1240 | { |
| 1241 | struct lttng_event *event = file->private_data; |
| 1242 | |
| 1243 | switch (cmd) { |
| 1244 | case LTTNG_KERNEL_OLD_CONTEXT: |
| 1245 | { |
| 1246 | struct lttng_kernel_context *ucontext_param; |
| 1247 | struct lttng_kernel_old_context *old_ucontext_param; |
| 1248 | int ret; |
| 1249 | |
| 1250 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), |
| 1251 | GFP_KERNEL); |
| 1252 | if (!ucontext_param) { |
| 1253 | ret = -ENOMEM; |
| 1254 | goto old_ctx_end; |
| 1255 | } |
| 1256 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), |
| 1257 | GFP_KERNEL); |
| 1258 | if (!old_ucontext_param) { |
| 1259 | ret = -ENOMEM; |
| 1260 | goto old_ctx_error_free_param; |
| 1261 | } |
| 1262 | |
| 1263 | if (copy_from_user(old_ucontext_param, |
| 1264 | (struct lttng_kernel_old_context __user *) arg, |
| 1265 | sizeof(struct lttng_kernel_old_context))) { |
| 1266 | ret = -EFAULT; |
| 1267 | goto old_ctx_error_free_old_param; |
| 1268 | } |
| 1269 | ucontext_param->ctx = old_ucontext_param->ctx; |
| 1270 | memcpy(ucontext_param->padding, old_ucontext_param->padding, |
| 1271 | sizeof(ucontext_param->padding)); |
| 1272 | /* only type that uses the union */ |
| 1273 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { |
| 1274 | ucontext_param->u.perf_counter.type = |
| 1275 | old_ucontext_param->u.perf_counter.type; |
| 1276 | ucontext_param->u.perf_counter.config = |
| 1277 | old_ucontext_param->u.perf_counter.config; |
| 1278 | memcpy(ucontext_param->u.perf_counter.name, |
| 1279 | old_ucontext_param->u.perf_counter.name, |
| 1280 | sizeof(ucontext_param->u.perf_counter.name)); |
| 1281 | } |
| 1282 | |
| 1283 | ret = lttng_abi_add_context(file, |
| 1284 | ucontext_param, |
| 1285 | &event->ctx, event->chan->session); |
| 1286 | |
| 1287 | old_ctx_error_free_old_param: |
| 1288 | kfree(old_ucontext_param); |
| 1289 | old_ctx_error_free_param: |
| 1290 | kfree(ucontext_param); |
| 1291 | old_ctx_end: |
| 1292 | return ret; |
| 1293 | } |
| 1294 | case LTTNG_KERNEL_CONTEXT: |
| 1295 | { |
| 1296 | struct lttng_kernel_context ucontext_param; |
| 1297 | |
| 1298 | if (copy_from_user(&ucontext_param, |
| 1299 | (struct lttng_kernel_context __user *) arg, |
| 1300 | sizeof(ucontext_param))) |
| 1301 | return -EFAULT; |
| 1302 | return lttng_abi_add_context(file, |
| 1303 | &ucontext_param, |
| 1304 | &event->ctx, event->chan->session); |
| 1305 | } |
| 1306 | case LTTNG_KERNEL_OLD_ENABLE: |
| 1307 | case LTTNG_KERNEL_ENABLE: |
| 1308 | return lttng_event_enable(event); |
| 1309 | case LTTNG_KERNEL_OLD_DISABLE: |
| 1310 | case LTTNG_KERNEL_DISABLE: |
| 1311 | return lttng_event_disable(event); |
| 1312 | default: |
| 1313 | return -ENOIOCTLCMD; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | static |
| 1318 | int lttng_event_release(struct inode *inode, struct file *file) |
| 1319 | { |
| 1320 | struct lttng_event *event = file->private_data; |
| 1321 | |
| 1322 | if (event) |
| 1323 | fput(event->chan->file); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* TODO: filter control ioctl */ |
| 1328 | static const struct file_operations lttng_event_fops = { |
| 1329 | .owner = THIS_MODULE, |
| 1330 | .release = lttng_event_release, |
| 1331 | .unlocked_ioctl = lttng_event_ioctl, |
| 1332 | #ifdef CONFIG_COMPAT |
| 1333 | .compat_ioctl = lttng_event_ioctl, |
| 1334 | #endif |
| 1335 | }; |
| 1336 | |
| 1337 | static int put_u64(uint64_t val, unsigned long arg) |
| 1338 | { |
| 1339 | return put_user(val, (uint64_t __user *) arg); |
| 1340 | } |
| 1341 | |
| 1342 | static long lttng_stream_ring_buffer_ioctl(struct file *filp, |
| 1343 | unsigned int cmd, unsigned long arg) |
| 1344 | { |
| 1345 | struct lib_ring_buffer *buf = filp->private_data; |
| 1346 | struct channel *chan = buf->backend.chan; |
| 1347 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1348 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 1349 | int ret; |
| 1350 | |
| 1351 | if (atomic_read(&chan->record_disabled)) |
| 1352 | return -EIO; |
| 1353 | |
| 1354 | switch (cmd) { |
| 1355 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN: |
| 1356 | { |
| 1357 | uint64_t ts; |
| 1358 | |
| 1359 | if (!lttng_chan->ops) |
| 1360 | goto error; |
| 1361 | ret = lttng_chan->ops->timestamp_begin(config, buf, &ts); |
| 1362 | if (ret < 0) |
| 1363 | goto error; |
| 1364 | return put_u64(ts, arg); |
| 1365 | } |
| 1366 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_END: |
| 1367 | { |
| 1368 | uint64_t ts; |
| 1369 | |
| 1370 | if (!lttng_chan->ops) |
| 1371 | goto error; |
| 1372 | ret = lttng_chan->ops->timestamp_end(config, buf, &ts); |
| 1373 | if (ret < 0) |
| 1374 | goto error; |
| 1375 | return put_u64(ts, arg); |
| 1376 | } |
| 1377 | case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED: |
| 1378 | { |
| 1379 | uint64_t ed; |
| 1380 | |
| 1381 | if (!lttng_chan->ops) |
| 1382 | goto error; |
| 1383 | ret = lttng_chan->ops->events_discarded(config, buf, &ed); |
| 1384 | if (ret < 0) |
| 1385 | goto error; |
| 1386 | return put_u64(ed, arg); |
| 1387 | } |
| 1388 | case LTTNG_RING_BUFFER_GET_CONTENT_SIZE: |
| 1389 | { |
| 1390 | uint64_t cs; |
| 1391 | |
| 1392 | if (!lttng_chan->ops) |
| 1393 | goto error; |
| 1394 | ret = lttng_chan->ops->content_size(config, buf, &cs); |
| 1395 | if (ret < 0) |
| 1396 | goto error; |
| 1397 | return put_u64(cs, arg); |
| 1398 | } |
| 1399 | case LTTNG_RING_BUFFER_GET_PACKET_SIZE: |
| 1400 | { |
| 1401 | uint64_t ps; |
| 1402 | |
| 1403 | if (!lttng_chan->ops) |
| 1404 | goto error; |
| 1405 | ret = lttng_chan->ops->packet_size(config, buf, &ps); |
| 1406 | if (ret < 0) |
| 1407 | goto error; |
| 1408 | return put_u64(ps, arg); |
| 1409 | } |
| 1410 | case LTTNG_RING_BUFFER_GET_STREAM_ID: |
| 1411 | { |
| 1412 | uint64_t si; |
| 1413 | |
| 1414 | if (!lttng_chan->ops) |
| 1415 | goto error; |
| 1416 | ret = lttng_chan->ops->stream_id(config, buf, &si); |
| 1417 | if (ret < 0) |
| 1418 | goto error; |
| 1419 | return put_u64(si, arg); |
| 1420 | } |
| 1421 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 1422 | { |
| 1423 | uint64_t ts; |
| 1424 | |
| 1425 | if (!lttng_chan->ops) |
| 1426 | goto error; |
| 1427 | ret = lttng_chan->ops->current_timestamp(config, buf, &ts); |
| 1428 | if (ret < 0) |
| 1429 | goto error; |
| 1430 | return put_u64(ts, arg); |
| 1431 | } |
| 1432 | default: |
| 1433 | return lib_ring_buffer_file_operations.unlocked_ioctl(filp, |
| 1434 | cmd, arg); |
| 1435 | } |
| 1436 | |
| 1437 | error: |
| 1438 | return -ENOSYS; |
| 1439 | } |
| 1440 | |
| 1441 | #ifdef CONFIG_COMPAT |
| 1442 | static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp, |
| 1443 | unsigned int cmd, unsigned long arg) |
| 1444 | { |
| 1445 | struct lib_ring_buffer *buf = filp->private_data; |
| 1446 | struct channel *chan = buf->backend.chan; |
| 1447 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1448 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 1449 | int ret; |
| 1450 | |
| 1451 | if (atomic_read(&chan->record_disabled)) |
| 1452 | return -EIO; |
| 1453 | |
| 1454 | switch (cmd) { |
| 1455 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN: |
| 1456 | { |
| 1457 | uint64_t ts; |
| 1458 | |
| 1459 | if (!lttng_chan->ops) |
| 1460 | goto error; |
| 1461 | ret = lttng_chan->ops->timestamp_begin(config, buf, &ts); |
| 1462 | if (ret < 0) |
| 1463 | goto error; |
| 1464 | return put_u64(ts, arg); |
| 1465 | } |
| 1466 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END: |
| 1467 | { |
| 1468 | uint64_t ts; |
| 1469 | |
| 1470 | if (!lttng_chan->ops) |
| 1471 | goto error; |
| 1472 | ret = lttng_chan->ops->timestamp_end(config, buf, &ts); |
| 1473 | if (ret < 0) |
| 1474 | goto error; |
| 1475 | return put_u64(ts, arg); |
| 1476 | } |
| 1477 | case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED: |
| 1478 | { |
| 1479 | uint64_t ed; |
| 1480 | |
| 1481 | if (!lttng_chan->ops) |
| 1482 | goto error; |
| 1483 | ret = lttng_chan->ops->events_discarded(config, buf, &ed); |
| 1484 | if (ret < 0) |
| 1485 | goto error; |
| 1486 | return put_u64(ed, arg); |
| 1487 | } |
| 1488 | case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE: |
| 1489 | { |
| 1490 | uint64_t cs; |
| 1491 | |
| 1492 | if (!lttng_chan->ops) |
| 1493 | goto error; |
| 1494 | ret = lttng_chan->ops->content_size(config, buf, &cs); |
| 1495 | if (ret < 0) |
| 1496 | goto error; |
| 1497 | return put_u64(cs, arg); |
| 1498 | } |
| 1499 | case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE: |
| 1500 | { |
| 1501 | uint64_t ps; |
| 1502 | |
| 1503 | if (!lttng_chan->ops) |
| 1504 | goto error; |
| 1505 | ret = lttng_chan->ops->packet_size(config, buf, &ps); |
| 1506 | if (ret < 0) |
| 1507 | goto error; |
| 1508 | return put_u64(ps, arg); |
| 1509 | } |
| 1510 | case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID: |
| 1511 | { |
| 1512 | uint64_t si; |
| 1513 | |
| 1514 | if (!lttng_chan->ops) |
| 1515 | goto error; |
| 1516 | ret = lttng_chan->ops->stream_id(config, buf, &si); |
| 1517 | if (ret < 0) |
| 1518 | goto error; |
| 1519 | return put_u64(si, arg); |
| 1520 | } |
| 1521 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
| 1522 | { |
| 1523 | uint64_t ts; |
| 1524 | |
| 1525 | if (!lttng_chan->ops) |
| 1526 | goto error; |
| 1527 | ret = lttng_chan->ops->current_timestamp(config, buf, &ts); |
| 1528 | if (ret < 0) |
| 1529 | goto error; |
| 1530 | return put_u64(ts, arg); |
| 1531 | } |
| 1532 | default: |
| 1533 | return lib_ring_buffer_file_operations.compat_ioctl(filp, |
| 1534 | cmd, arg); |
| 1535 | } |
| 1536 | |
| 1537 | error: |
| 1538 | return -ENOSYS; |
| 1539 | } |
| 1540 | #endif /* CONFIG_COMPAT */ |
| 1541 | |
| 1542 | static void lttng_stream_override_ring_buffer_fops(void) |
| 1543 | { |
| 1544 | lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE; |
| 1545 | lttng_stream_ring_buffer_file_operations.open = |
| 1546 | lib_ring_buffer_file_operations.open; |
| 1547 | lttng_stream_ring_buffer_file_operations.release = |
| 1548 | lib_ring_buffer_file_operations.release; |
| 1549 | lttng_stream_ring_buffer_file_operations.poll = |
| 1550 | lib_ring_buffer_file_operations.poll; |
| 1551 | lttng_stream_ring_buffer_file_operations.splice_read = |
| 1552 | lib_ring_buffer_file_operations.splice_read; |
| 1553 | lttng_stream_ring_buffer_file_operations.mmap = |
| 1554 | lib_ring_buffer_file_operations.mmap; |
| 1555 | lttng_stream_ring_buffer_file_operations.unlocked_ioctl = |
| 1556 | lttng_stream_ring_buffer_ioctl; |
| 1557 | lttng_stream_ring_buffer_file_operations.llseek = |
| 1558 | lib_ring_buffer_file_operations.llseek; |
| 1559 | #ifdef CONFIG_COMPAT |
| 1560 | lttng_stream_ring_buffer_file_operations.compat_ioctl = |
| 1561 | lttng_stream_ring_buffer_compat_ioctl; |
| 1562 | #endif |
| 1563 | } |
| 1564 | |
| 1565 | int __init lttng_abi_init(void) |
| 1566 | { |
| 1567 | int ret = 0; |
| 1568 | |
| 1569 | wrapper_vmalloc_sync_all(); |
| 1570 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
| 1571 | <tng_fops, NULL); |
| 1572 | |
| 1573 | if (!lttng_proc_dentry) { |
| 1574 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 1575 | ret = -ENOMEM; |
| 1576 | goto error; |
| 1577 | } |
| 1578 | lttng_stream_override_ring_buffer_fops(); |
| 1579 | |
| 1580 | error: |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | void __exit lttng_abi_exit(void) |
| 1585 | { |
| 1586 | if (lttng_proc_dentry) |
| 1587 | remove_proc_entry("lttng", NULL); |
| 1588 | } |