X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=src%2Flttng-abi.c;h=344f18c25acf3d0fa18fd318c907d7bc4a942524;hb=99d223adfae9b68800e91bda470c92acb14efae6;hp=07c520f00a0c8d2604e0d170186621b35f46e2ae;hpb=dffef45d53ab388fd08c19e8865b245b40ca9dff;p=lttng-modules.git diff --git a/src/lttng-abi.c b/src/lttng-abi.c index 07c520f0..344f18c2 100644 --- a/src/lttng-abi.c +++ b/src/lttng-abi.c @@ -44,6 +44,7 @@ #include #include #include +#include /* * This is LTTng's own personal way to create a system call as an external @@ -106,6 +107,14 @@ fd_error: return ret; } +void event_notifier_send_notification_work_wakeup(struct irq_work *entry) +{ + struct lttng_event_notifier_group *event_notifier_group = + container_of(entry, struct lttng_event_notifier_group, + wakeup_pending); + wake_up_interruptible(&event_notifier_group->read_wait); +} + static int lttng_abi_create_event_notifier_group(void) { @@ -131,6 +140,9 @@ int lttng_abi_create_event_notifier_group(void) } event_notifier_group->file = event_notifier_group_file; + init_waitqueue_head(&event_notifier_group->read_wait); + init_irq_work(&event_notifier_group->wakeup_pending, + event_notifier_send_notification_work_wakeup); fd_install(event_notifier_group_fd, event_notifier_group_file); return event_notifier_group_fd; @@ -804,6 +816,229 @@ static const struct file_operations lttng_session_fops = { #endif }; +/* + * When encountering empty buffer, flush current sub-buffer if non-empty + * and retry (if new data available to read after flush). + */ +static +ssize_t lttng_event_notifier_group_notif_read(struct file *filp, char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct lttng_event_notifier_group *event_notifier_group = filp->private_data; + struct channel *chan = event_notifier_group->chan; + struct lib_ring_buffer *buf = event_notifier_group->buf; + ssize_t read_count = 0, len; + size_t read_offset; + + might_sleep(); + if (!lttng_access_ok(VERIFY_WRITE, user_buf, count)) + return -EFAULT; + + /* Finish copy of previous record */ + if (*ppos != 0) { + if (read_count < count) { + len = chan->iter.len_left; + read_offset = *ppos; + goto skip_get_next; + } + } + + while (read_count < count) { + size_t copy_len, space_left; + + len = lib_ring_buffer_get_next_record(chan, buf); +len_test: + if (len < 0) { + /* + * Check if buffer is finalized (end of file). + */ + if (len == -ENODATA) { + /* A 0 read_count will tell about end of file */ + goto nodata; + } + if (filp->f_flags & O_NONBLOCK) { + if (!read_count) + read_count = -EAGAIN; + goto nodata; + } else { + int error; + + /* + * No data available at the moment, return what + * we got. + */ + if (read_count) + goto nodata; + + /* + * Wait for returned len to be >= 0 or -ENODATA. + */ + error = wait_event_interruptible( + event_notifier_group->read_wait, + ((len = lib_ring_buffer_get_next_record( + chan, buf)), len != -EAGAIN)); + CHAN_WARN_ON(chan, len == -EBUSY); + if (error) { + read_count = error; + goto nodata; + } + CHAN_WARN_ON(chan, len < 0 && len != -ENODATA); + goto len_test; + } + } + read_offset = buf->iter.read_offset; +skip_get_next: + space_left = count - read_count; + if (len <= space_left) { + copy_len = len; + chan->iter.len_left = 0; + *ppos = 0; + } else { + copy_len = space_left; + chan->iter.len_left = len - copy_len; + *ppos = read_offset + copy_len; + } + if (__lib_ring_buffer_copy_to_user(&buf->backend, read_offset, + &user_buf[read_count], + copy_len)) { + /* + * Leave the len_left and ppos values at their current + * state, as we currently have a valid event to read. + */ + return -EFAULT; + } + read_count += copy_len; + } + goto put_record; + +nodata: + *ppos = 0; + chan->iter.len_left = 0; + +put_record: + lib_ring_buffer_put_current_record(buf); + return read_count; +} + +/* + * If the ring buffer is non empty (even just a partial subbuffer), return that + * there is data available. Perform a ring buffer flush if we encounter a + * non-empty ring buffer which does not have any consumeable subbuffer available. + */ +static +unsigned int lttng_event_notifier_group_notif_poll(struct file *filp, + poll_table *wait) +{ + unsigned int mask = 0; + struct lttng_event_notifier_group *event_notifier_group = filp->private_data; + struct channel *chan = event_notifier_group->chan; + struct lib_ring_buffer *buf = event_notifier_group->buf; + const struct lib_ring_buffer_config *config = &chan->backend.config; + int finalized, disabled; + unsigned long consumed, offset; + size_t subbuffer_header_size = config->cb.subbuffer_header_size(); + + if (filp->f_mode & FMODE_READ) { + poll_wait_set_exclusive(wait); + poll_wait(filp, &event_notifier_group->read_wait, wait); + + finalized = lib_ring_buffer_is_finalized(config, buf); + disabled = lib_ring_buffer_channel_is_disabled(chan); + + /* + * lib_ring_buffer_is_finalized() contains a smp_rmb() ordering + * finalized load before offsets loads. + */ + WARN_ON(atomic_long_read(&buf->active_readers) != 1); +retry: + if (disabled) + return POLLERR; + + offset = lib_ring_buffer_get_offset(config, buf); + consumed = lib_ring_buffer_get_consumed(config, buf); + + /* + * If there is no buffer available to consume. + */ + if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) == 0) { + /* + * If there is a non-empty subbuffer, flush and try again. + */ + if (subbuf_offset(offset, chan) > subbuffer_header_size) { + lib_ring_buffer_switch_remote(buf); + goto retry; + } + + if (finalized) + return POLLHUP; + else { + /* + * The memory barriers + * __wait_event()/wake_up_interruptible() take + * care of "raw_spin_is_locked" memory ordering. + */ + if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) + goto retry; + else + return 0; + } + } else { + if (subbuf_trunc(offset, chan) - subbuf_trunc(consumed, chan) + >= chan->backend.buf_size) + return POLLPRI | POLLRDBAND; + else + return POLLIN | POLLRDNORM; + } + } + + return mask; +} + +/** + * lttng_event_notifier_group_notif_open - event_notifier ring buffer open file operation + * @inode: opened inode + * @file: opened file + * + * Open implementation. Makes sure only one open instance of a buffer is + * done at a given moment. + */ +static int lttng_event_notifier_group_notif_open(struct inode *inode, struct file *file) +{ + struct lttng_event_notifier_group *event_notifier_group = inode->i_private; + struct lib_ring_buffer *buf = event_notifier_group->buf; + + file->private_data = event_notifier_group; + return lib_ring_buffer_open(inode, file, buf); +} + +/** + * lttng_event_notifier_group_notif_release - event_notifier ring buffer release file operation + * @inode: opened inode + * @file: opened file + * + * Release implementation. + */ +static int lttng_event_notifier_group_notif_release(struct inode *inode, struct file *file) +{ + struct lttng_event_notifier_group *event_notifier_group = file->private_data; + struct lib_ring_buffer *buf = event_notifier_group->buf; + int ret; + + ret = lib_ring_buffer_release(inode, file, buf); + if (ret) + return ret; + fput(event_notifier_group->file); + return 0; +} + +static const struct file_operations lttng_event_notifier_group_notif_fops = { + .owner = THIS_MODULE, + .open = lttng_event_notifier_group_notif_open, + .release = lttng_event_notifier_group_notif_release, + .read = lttng_event_notifier_group_notif_read, + .poll = lttng_event_notifier_group_notif_poll, +}; + /** * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation * @filp: the file @@ -1307,6 +1542,41 @@ nomem: return ret; } +static +int lttng_abi_open_event_notifier_group_stream(struct file *notif_file) +{ + struct lttng_event_notifier_group *event_notifier_group = notif_file->private_data; + struct channel *chan = event_notifier_group->chan; + struct lib_ring_buffer *buf; + int ret; + void *stream_priv; + + buf = event_notifier_group->ops->buffer_read_open(chan); + if (!buf) + return -ENOENT; + + /* The event_notifier notification fd holds a reference on the event_notifier group */ + if (!atomic_long_add_unless(¬if_file->f_count, 1, LONG_MAX)) { + ret = -EOVERFLOW; + goto refcount_error; + } + event_notifier_group->buf = buf; + stream_priv = event_notifier_group; + ret = lttng_abi_create_stream_fd(notif_file, stream_priv, + <tng_event_notifier_group_notif_fops, + "[lttng_event_notifier_stream]"); + if (ret < 0) + goto fd_error; + + return ret; + +fd_error: + atomic_long_dec(¬if_file->f_count); +refcount_error: + event_notifier_group->ops->buffer_read_close(buf); + return ret; +} + static int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param) { @@ -1314,6 +1584,8 @@ int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param) switch (event_param->instrumentation) { case LTTNG_KERNEL_SYSCALL: switch (event_param->u.syscall.entryexit) { + case LTTNG_KERNEL_SYSCALL_ENTRY: + case LTTNG_KERNEL_SYSCALL_EXIT: case LTTNG_KERNEL_SYSCALL_ENTRYEXIT: break; default: @@ -1326,7 +1598,7 @@ int lttng_abi_validate_event_param(struct lttng_kernel_event *event_param) return -EINVAL; } switch (event_param->u.syscall.match) { - case LTTNG_SYSCALL_MATCH_NAME: + case LTTNG_KERNEL_SYSCALL_MATCH_NAME: break; default: return -EINVAL; @@ -1441,6 +1713,7 @@ fd_error: static long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { + struct lttng_event_notifier *event_notifier; struct lttng_event_notifier_enabler *event_notifier_enabler; enum lttng_event_type *evtype = file->private_data; @@ -1448,7 +1721,8 @@ long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned lo case LTTNG_KERNEL_ENABLE: switch (*evtype) { case LTTNG_TYPE_EVENT: - return -EINVAL; + event_notifier = file->private_data; + return lttng_event_notifier_enable(event_notifier); case LTTNG_TYPE_ENABLER: event_notifier_enabler = file->private_data; return lttng_event_notifier_enabler_enable(event_notifier_enabler); @@ -1459,7 +1733,8 @@ long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned lo case LTTNG_KERNEL_DISABLE: switch (*evtype) { case LTTNG_TYPE_EVENT: - return -EINVAL; + event_notifier = file->private_data; + return lttng_event_notifier_disable(event_notifier); case LTTNG_TYPE_ENABLER: event_notifier_enabler = file->private_data; return lttng_event_notifier_enabler_disable(event_notifier_enabler); @@ -1473,12 +1748,39 @@ long lttng_event_notifier_ioctl(struct file *file, unsigned int cmd, unsigned lo return -EINVAL; case LTTNG_TYPE_ENABLER: event_notifier_enabler = file->private_data; - return lttng_event_notifier_enabler_attach_bytecode(event_notifier_enabler, + return lttng_event_notifier_enabler_attach_filter_bytecode( + event_notifier_enabler, (struct lttng_kernel_filter_bytecode __user *) arg); default: WARN_ON_ONCE(1); return -ENOSYS; } + + case LTTNG_KERNEL_CAPTURE: + switch (*evtype) { + case LTTNG_TYPE_EVENT: + return -EINVAL; + case LTTNG_TYPE_ENABLER: + event_notifier_enabler = file->private_data; + return lttng_event_notifier_enabler_attach_capture_bytecode( + event_notifier_enabler, + (struct lttng_kernel_capture_bytecode __user *) arg); + default: + WARN_ON_ONCE(1); + return -ENOSYS; + } + case LTTNG_KERNEL_ADD_CALLSITE: + switch (*evtype) { + case LTTNG_TYPE_EVENT: + event_notifier = file->private_data; + return lttng_event_notifier_add_callsite(event_notifier, + (struct lttng_kernel_event_callsite __user *) arg); + case LTTNG_TYPE_ENABLER: + return -EINVAL; + default: + WARN_ON_ONCE(1); + return -ENOSYS; + } default: return -ENOIOCTLCMD; } @@ -1534,12 +1836,17 @@ int lttng_abi_create_event_notifier(struct file *event_notifier_group_file, switch (event_notifier_param->event.instrumentation) { case LTTNG_KERNEL_TRACEPOINT: - case LTTNG_KERNEL_KPROBE: case LTTNG_KERNEL_UPROBE: + break; + case LTTNG_KERNEL_KPROBE: + event_notifier_param->event.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; + break; + case LTTNG_KERNEL_SYSCALL: + break; case LTTNG_KERNEL_KRETPROBE: + /* Placing an event notifier on kretprobe is not supported. */ case LTTNG_KERNEL_FUNCTION: case LTTNG_KERNEL_NOOP: - case LTTNG_KERNEL_SYSCALL: default: ret = -EINVAL; goto inval_instr; @@ -1625,6 +1932,10 @@ long lttng_event_notifier_group_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { + case LTTNG_KERNEL_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD: + { + return lttng_abi_open_event_notifier_group_stream(file); + } case LTTNG_KERNEL_EVENT_NOTIFIER_CREATE: { struct lttng_kernel_event_notifier uevent_notifier_param; @@ -2005,7 +2316,8 @@ long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) case LTTNG_TYPE_ENABLER: { event_enabler = file->private_data; - return lttng_event_enabler_attach_bytecode(event_enabler, + return lttng_event_enabler_attach_filter_bytecode( + event_enabler, (struct lttng_kernel_filter_bytecode __user *) arg); } default: