+
+/*
+ * Take a snapshot for a given kernel session.
+ *
+ * Return LTTNG_OK on success or else return a LTTNG_ERR code.
+ */
+enum lttng_error_code kernel_snapshot_record(
+ struct ltt_kernel_session *ksess,
+ const struct consumer_output *output, int wait,
+ uint64_t nb_packets_per_stream)
+{
+ int err, ret, saved_metadata_fd;
+ enum lttng_error_code status = LTTNG_OK;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+ struct ltt_kernel_metadata *saved_metadata;
+ char *trace_path = NULL;
+ size_t consumer_path_offset = 0;
+
+ assert(ksess);
+ assert(ksess->consumer);
+ assert(output);
+
+ DBG("Kernel snapshot record started");
+
+ /* Save current metadata since the following calls will change it. */
+ saved_metadata = ksess->metadata;
+ saved_metadata_fd = ksess->metadata_stream_fd;
+
+ rcu_read_lock();
+
+ ret = kernel_open_metadata(ksess);
+ if (ret < 0) {
+ status = LTTNG_ERR_KERN_META_FAIL;
+ goto error;
+ }
+
+ ret = kernel_open_metadata_stream(ksess);
+ if (ret < 0) {
+ status = LTTNG_ERR_KERN_META_FAIL;
+ goto error_open_stream;
+ }
+
+ trace_path = setup_channel_trace_path(ksess->consumer,
+ DEFAULT_KERNEL_TRACE_DIR, &consumer_path_offset);
+ if (!trace_path) {
+ status = LTTNG_ERR_INVALID;
+ goto error;
+ }
+ /* Send metadata to consumer and snapshot everything. */
+ cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
+ socket, node.node) {
+ struct ltt_kernel_channel *chan;
+
+ pthread_mutex_lock(socket->lock);
+ /* This stream must not be monitored by the consumer. */
+ ret = kernel_consumer_add_metadata(socket, ksess, 0);
+ pthread_mutex_unlock(socket->lock);
+ if (ret < 0) {
+ status = LTTNG_ERR_KERN_META_FAIL;
+ goto error_consumer;
+ }
+
+ /* For each channel, ask the consumer to snapshot it. */
+ cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
+ status = consumer_snapshot_channel(socket, chan->key, output, 0,
+ ksess->uid, ksess->gid,
+ &trace_path[consumer_path_offset], wait,
+ nb_packets_per_stream);
+ if (status != LTTNG_OK) {
+ (void) kernel_consumer_destroy_metadata(socket,
+ ksess->metadata);
+ goto error_consumer;
+ }
+ }
+
+ /* Snapshot metadata, */
+ status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
+ 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset],
+ wait, 0);
+ if (status != LTTNG_OK) {
+ goto error_consumer;
+ }
+
+ /*
+ * The metadata snapshot is done, ask the consumer to destroy it since
+ * it's not monitored on the consumer side.
+ */
+ (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
+ }
+
+error_consumer:
+ /* Close newly opened metadata stream. It's now on the consumer side. */
+ err = close(ksess->metadata_stream_fd);
+ if (err < 0) {
+ PERROR("close snapshot kernel");
+ }
+
+error_open_stream:
+ trace_kernel_destroy_metadata(ksess->metadata);
+error:
+ /* Restore metadata state.*/
+ ksess->metadata = saved_metadata;
+ ksess->metadata_stream_fd = saved_metadata_fd;
+ rcu_read_unlock();
+ free(trace_path);
+ return status;
+}
+
+/*
+ * Get the syscall mask array from the kernel tracer.
+ *
+ * Return 0 on success else a negative value. In both case, syscall_mask should
+ * be freed.
+ */
+int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
+{
+ assert(syscall_mask);
+ assert(nr_bits);
+
+ return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
+}
+
+static
+int kernel_tracer_abi_greater_or_equal(unsigned int major, unsigned int minor)
+{
+ int ret;
+ struct lttng_kernel_tracer_abi_version abi;
+
+ ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
+ if (ret < 0) {
+ ERR("Failed to retrieve lttng-modules ABI version");
+ goto error;
+ }
+
+ ret = abi.major > major || (abi.major == major && abi.minor >= minor);
+error:
+ return ret;
+}
+
+/*
+ * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
+ * version number.
+ *
+ * Return 1 on success, 0 when feature is not supported, negative value in case
+ * of errors.
+ */
+int kernel_supports_ring_buffer_snapshot_sample_positions(void)
+{
+ /*
+ * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
+ */
+ return kernel_tracer_abi_greater_or_equal(2, 3);
+}
+
+/*
+ * Check for the support of the packet sequence number via abi version number.
+ *
+ * Return 1 on success, 0 when feature is not supported, negative value in case
+ * of errors.
+ */
+int kernel_supports_ring_buffer_packet_sequence_number(void)
+{
+ /*
+ * Packet sequence number was introduced in LTTng 2.8,
+ * lttng-modules ABI 2.1.
+ */
+ return kernel_tracer_abi_greater_or_equal(2, 1);
+}
+
+/*
+ * Check for the support of event notifiers via abi version number.
+ *
+ * Return 1 on success, 0 when feature is not supported, negative value in case
+ * of errors.
+ */
+int kernel_supports_event_notifiers(void)
+{
+ /*
+ * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
+ */
+ return kernel_tracer_abi_greater_or_equal(2, 6);
+}
+
+/*
+ * Rotate a kernel session.
+ *
+ * Return LTTNG_OK on success or else an LTTng error code.
+ */
+enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
+{
+ int ret;
+ enum lttng_error_code status = LTTNG_OK;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+
+ assert(ksess);
+ assert(ksess->consumer);
+
+ DBG("Rotate kernel session %s started (session %" PRIu64 ")",
+ session->name, session->id);
+
+ rcu_read_lock();
+
+ /*
+ * Note that this loop will end after one iteration given that there is
+ * only one kernel consumer.
+ */
+ cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
+ socket, node.node) {
+ struct ltt_kernel_channel *chan;
+
+ /* For each channel, ask the consumer to rotate it. */
+ cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
+ DBG("Rotate kernel channel %" PRIu64 ", session %s",
+ chan->key, session->name);
+ ret = consumer_rotate_channel(socket, chan->key,
+ ksess->uid, ksess->gid, ksess->consumer,
+ /* is_metadata_channel */ false);
+ if (ret < 0) {
+ status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
+ goto error;
+ }
+ }
+
+ /*
+ * Rotate the metadata channel.
+ */
+ ret = consumer_rotate_channel(socket, ksess->metadata->key,
+ ksess->uid, ksess->gid, ksess->consumer,
+ /* is_metadata_channel */ true);
+ if (ret < 0) {
+ status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
+ goto error;
+ }
+ }
+
+error:
+ rcu_read_unlock();
+ return status;
+}
+
+enum lttng_error_code kernel_create_channel_subdirectories(
+ const struct ltt_kernel_session *ksess)
+{
+ enum lttng_error_code ret = LTTNG_OK;
+ enum lttng_trace_chunk_status chunk_status;
+
+ rcu_read_lock();
+ assert(ksess->current_trace_chunk);
+
+ /*
+ * Create the index subdirectory which will take care
+ * of implicitly creating the channel's path.
+ */
+ chunk_status = lttng_trace_chunk_create_subdirectory(
+ ksess->current_trace_chunk,
+ DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
+ if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+ ret = LTTNG_ERR_CREATE_DIR_FAIL;
+ goto error;
+ }
+error:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Setup necessary data for kernel tracer action.
+ */
+LTTNG_HIDDEN
+int init_kernel_tracer(void)
+{
+ int ret;
+ bool is_root = !getuid();
+
+ /* Modprobe lttng kernel modules */
+ ret = modprobe_lttng_control();
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Open debugfs lttng */
+ kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
+ if (kernel_tracer_fd < 0) {
+ DBG("Failed to open %s", module_proc_lttng);
+ goto error_open;
+ }
+
+ /* Validate kernel version */
+ ret = kernel_validate_version(&kernel_tracer_version,
+ &kernel_tracer_abi_version);
+ if (ret < 0) {
+ goto error_version;
+ }
+
+ ret = modprobe_lttng_data();
+ if (ret < 0) {
+ goto error_modules;
+ }
+
+ ret = kernel_supports_ring_buffer_snapshot_sample_positions();
+ if (ret < 0) {
+ goto error_modules;
+ }
+ if (ret < 1) {
+ WARN("Kernel tracer does not support buffer monitoring. "
+ "The monitoring timer of channels in the kernel domain "
+ "will be set to 0 (disabled).");
+ }
+
+ ret = kernel_supports_event_notifiers();
+ if (ret < 0) {
+ ERR("Failed to check for kernel tracer event notifier support");
+ goto error_modules;
+ }
+ ret = kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd);
+ if (ret < 0) {
+ /* This is not fatal. */
+ WARN("Failed to create kernel event notifier group");
+ kernel_tracer_event_notifier_group_fd = -1;
+ } else {
+ enum event_notifier_error_accounting_status error_accounting_status;
+ enum lttng_error_code error_code_ret =
+ kernel_create_event_notifier_group_notification_fd(
+ &kernel_tracer_event_notifier_group_notification_fd);
+
+ if (error_code_ret != LTTNG_OK) {
+ goto error_modules;
+ }
+
+ error_accounting_status = event_notifier_error_accounting_register_kernel(
+ kernel_tracer_event_notifier_group_fd);
+ if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ ERR("Error initializing event notifier error accounting for kernel tracer.");
+ error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING;
+ goto error_modules;
+ }
+
+ kernel_token_to_event_notifier_rule_ht = cds_lfht_new(
+ DEFAULT_HT_SIZE, 1, 0,
+ CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
+ NULL);
+ if (!kernel_token_to_event_notifier_rule_ht) {
+ goto error_token_ht;
+ }
+ }
+
+ DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
+ kernel_tracer_fd, kernel_tracer_event_notifier_group_fd,
+ kernel_tracer_event_notifier_group_notification_fd);
+
+ ret = syscall_init_table(kernel_tracer_fd);
+ if (ret < 0) {
+ ERR("Unable to populate syscall table. Syscall tracing won't "
+ "work for this session daemon.");
+ }
+
+ return 0;
+
+error_version:
+ modprobe_remove_lttng_control();
+ ret = close(kernel_tracer_fd);
+ if (ret) {
+ PERROR("Failed to close kernel tracer file descriptor: fd = %d",
+ kernel_tracer_fd);
+ }
+ kernel_tracer_fd = -1;
+ return LTTNG_ERR_KERN_VERSION;
+
+
+error_token_ht:
+ ret = close(kernel_tracer_event_notifier_group_notification_fd);
+ if (ret) {
+ PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
+ kernel_tracer_event_notifier_group_notification_fd);
+ }
+
+error_modules:
+ ret = close(kernel_tracer_event_notifier_group_fd);
+ if (ret) {
+ PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
+ kernel_tracer_event_notifier_group_fd);
+ }
+
+ ret = close(kernel_tracer_fd);
+ if (ret) {
+ PERROR("Failed to close kernel tracer file descriptor: fd = %d",
+ kernel_tracer_fd);
+ }
+
+error_open:
+ modprobe_remove_lttng_control();
+
+error:
+ WARN("No kernel tracer available");
+ kernel_tracer_fd = -1;
+ if (!is_root) {
+ return LTTNG_ERR_NEED_ROOT_SESSIOND;
+ } else {
+ return LTTNG_ERR_KERN_NA;
+ }
+}
+
+LTTNG_HIDDEN
+void cleanup_kernel_tracer(void)
+{
+ DBG2("Closing kernel event notifier group notification file descriptor");
+ if (kernel_tracer_event_notifier_group_notification_fd >= 0) {
+ int ret = notification_thread_command_remove_tracer_event_source(
+ notification_thread_handle,
+ kernel_tracer_event_notifier_group_notification_fd);
+ if (ret != LTTNG_OK) {
+ ERR("Failed to remove kernel event notifier notification from notification thread");
+ }
+
+ ret = close(kernel_tracer_event_notifier_group_notification_fd);
+ if (ret) {
+ PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
+ kernel_tracer_event_notifier_group_notification_fd);
+ }
+
+ kernel_tracer_event_notifier_group_notification_fd = -1;
+ }
+
+ if (kernel_token_to_event_notifier_rule_ht) {
+ const int ret = cds_lfht_destroy(
+ kernel_token_to_event_notifier_rule_ht, NULL);
+ assert(ret == 0);
+ }
+
+ DBG2("Closing kernel event notifier group file descriptor");
+ if (kernel_tracer_event_notifier_group_fd >= 0) {
+ const int ret = close(kernel_tracer_event_notifier_group_fd);
+
+ if (ret) {
+ PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
+ kernel_tracer_event_notifier_group_fd);
+ }
+
+ kernel_tracer_event_notifier_group_fd = -1;
+ }
+
+ DBG2("Closing kernel fd");
+ if (kernel_tracer_fd >= 0) {
+ const int ret = close(kernel_tracer_fd);
+
+ if (ret) {
+ PERROR("Failed to close kernel tracer file descriptor: fd = %d",
+ kernel_tracer_fd);
+ }
+
+ kernel_tracer_fd = -1;
+ }
+
+ free(syscall_table);
+}
+
+LTTNG_HIDDEN
+bool kernel_tracer_is_initialized(void)
+{
+ return kernel_tracer_fd >= 0;
+}
+static
+enum lttng_error_code kernel_clear_session_channels(struct ltt_session *session)
+{
+ int ret;
+ enum lttng_error_code status = LTTNG_OK;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+
+ assert(ksess);
+ assert(ksess->consumer);
+
+ rcu_read_lock();
+
+ if (ksess->active) {
+ ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
+ status = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ /*
+ * Note that this loop will end after one iteration given that there is
+ * only one kernel consumer.
+ */
+ cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
+ socket, node.node) {
+ struct ltt_kernel_channel *chan;
+
+ /* For each channel, ask the consumer to clear it. */
+ cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
+ DBG("Clear kernel channel %" PRIu64 ", session %s",
+ chan->key, session->name);
+ ret = consumer_clear_channel(socket, chan->key);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ if (!ksess->metadata) {
+ /*
+ * Nothing to do for the metadata.
+ * This is a snapshot session.
+ * The metadata is genererated on the fly.
+ */
+ continue;
+ }
+
+ /*
+ * Clear the metadata channel.
+ * Metadata channel is not cleared per se but we still need to
+ * perform a rotation operation on it behind the scene.
+ */
+ ret = consumer_clear_channel(socket, ksess->metadata->key);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+
+ goto end;
+error:
+ switch (-ret) {
+ case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
+ status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
+ break;
+ default:
+ status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
+ break;
+ }
+end:
+ rcu_read_unlock();
+ return status;
+}
+
+static
+enum lttng_error_code kernel_map_clear_all(struct ltt_kernel_map *map)
+{
+ enum lttng_error_code status;
+ uint64_t descr_count, i;
+ int ret;
+
+ assert(map);
+
+ ret = kernctl_counter_map_descriptor_count(map->fd, &descr_count);
+ if (ret) {
+ ERR("Error getting map descriptor count");
+ status = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+
+ for(i = 0; i < descr_count; i++) {
+ struct lttng_kernel_counter_map_descriptor descriptor = {0};
+ struct lttng_kernel_counter_clear counter_clear = {0};
+
+ descriptor.descriptor_index = i;
+
+ ret = kernctl_counter_map_descriptor(map->fd, &descriptor);
+ if (ret) {
+ ERR("Error getting map descriptor %"PRIu64, i);
+ status = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+
+ counter_clear.index.number_dimensions = 1;
+ counter_clear.index.dimension_indexes[0] = descriptor.array_index;
+
+ ret = kernctl_counter_clear(map->fd, &counter_clear);
+ if (ret) {
+ ERR("Error clearing value of map descriptor %"PRIu64, i);
+ status = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+ }
+
+ status = LTTNG_OK;
+end:
+ return status;
+}
+
+static
+enum lttng_error_code kernel_clear_session_maps(struct ltt_session *session)
+{
+ enum lttng_error_code status = LTTNG_OK;
+ struct ltt_kernel_map *map;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+
+ assert(ksess);
+
+ cds_list_for_each_entry(map, &ksess->map_list.head, list) {
+ DBG("Clear kernel map %" PRIu64 ", session %s",
+ map->key, session->name);
+ status = kernel_map_clear_all(map);
+ if (status != LTTNG_OK) {
+ ERR("Clearing all values of map");
+ goto end;
+ }
+ }
+
+end:
+ return status;
+}
+
+/*
+ * Clear a kernel session.
+ *
+ * Return LTTNG_OK on success or else an LTTng error code.
+ */
+enum lttng_error_code kernel_clear_session(struct ltt_session *session)
+{
+ enum lttng_error_code status = LTTNG_OK;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+
+ assert(ksess);
+ assert(ksess->consumer);
+
+ DBG("Clear kernel session %s (session %" PRIu64 ")",
+ session->name, session->id);
+
+ rcu_read_lock();
+
+ if (ksess->active) {
+ ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
+ status = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ status = kernel_clear_session_channels(session);
+ if (status != LTTNG_OK) {
+ goto end;
+ }
+ /*
+ * Iterate and clear all kernel maps.
+ */
+ status = kernel_clear_session_maps(session);
+ if (status != LTTNG_OK) {
+ goto end;
+ }
+
+
+end:
+ rcu_read_unlock();
+ return status;
+}
+
+enum lttng_error_code kernel_create_event_notifier_group_notification_fd(
+ int *event_notifier_group_notification_fd)
+{
+ int local_fd = -1, ret;
+ enum lttng_error_code error_code_ret;
+
+ assert(event_notifier_group_notification_fd);
+
+ ret = kernctl_create_event_notifier_group_notification_fd(
+ kernel_tracer_event_notifier_group_fd);
+ if (ret < 0) {
+ PERROR("Failed to create kernel event notifier group notification file descriptor");
+ error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
+ goto error;
+ }
+
+ local_fd = ret;
+
+ /* Prevent fd duplication after execlp(). */
+ ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
+ local_fd);
+ error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
+ goto error;
+ }
+
+ DBG("Created kernel notifier group notification file descriptor: fd = %d",
+ local_fd);
+ error_code_ret = LTTNG_OK;
+ *event_notifier_group_notification_fd = local_fd;
+ local_fd = -1;
+
+error:
+ if (local_fd >= 0) {
+ ret = close(local_fd);
+ if (ret) {
+ PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
+ local_fd);
+ }
+ }
+
+ return error_code_ret;
+}
+
+enum lttng_error_code kernel_destroy_event_notifier_group_notification_fd(
+ int event_notifier_group_notification_fd)
+{
+ enum lttng_error_code ret_code = LTTNG_OK;
+
+ DBG("Closing event notifier group notification file descriptor: fd = %d",
+ event_notifier_group_notification_fd);
+ if (event_notifier_group_notification_fd >= 0) {
+ const int ret = close(event_notifier_group_notification_fd);
+ if (ret) {
+ PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
+ event_notifier_group_notification_fd);
+ }
+ }
+
+ return ret_code;
+}
+
+static
+unsigned long hash_trigger(const struct lttng_trigger *trigger)
+{
+ const struct lttng_condition *condition =
+ lttng_trigger_get_const_condition(trigger);
+
+ return lttng_condition_hash(condition);
+}
+
+static
+int match_trigger(struct cds_lfht_node *node, const void *key)
+{
+ const struct ltt_kernel_event_notifier_rule *event_notifier_rule;
+ const struct lttng_trigger *trigger = key;
+
+ event_notifier_rule = caa_container_of(node,
+ const struct ltt_kernel_event_notifier_rule, ht_node);
+
+ return lttng_trigger_is_equal(trigger, event_notifier_rule->trigger);
+}
+
+static
+int add_key_token(struct lttng_kernel_key_token *kernel_key_token,
+ const struct lttng_map_key_token *key_token)
+{
+ int ret;
+ switch (key_token->type) {
+ case LTTNG_MAP_KEY_TOKEN_TYPE_STRING:
+ {
+ const struct lttng_map_key_token_string *str_token;
+ str_token = (typeof(str_token)) key_token;
+
+ kernel_key_token->type = LTTNG_KERNEL_KEY_TOKEN_STRING;
+ kernel_key_token->arg.string_ptr = (uint64_t) str_token->string;
+
+ break;
+ }
+ case LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE:
+ {
+ const struct lttng_map_key_token_variable *var_token;
+ var_token = (typeof(var_token)) key_token;
+ switch (var_token->type) {
+ case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME:
+ kernel_key_token->type = LTTNG_KERNEL_KEY_TOKEN_EVENT_NAME;
+ break;
+ case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_PROVIDER_NAME:
+ /* The kernel events don't have providers */
+ ERR("Provider variable token type not supported for kernel tracer");
+ ret = -1;
+ goto end;
+ default:
+ abort();
+ }
+
+ break;
+ }
+ default:
+ abort();
+ }
+ ret = 0;
+end:
+ return ret;
+}
+
+enum lttng_error_code kernel_create_event_counter(
+ struct ltt_kernel_map *kmap,
+ const struct lttng_credentials *creds,
+ uint64_t action_tracer_token,
+ const struct lttng_event_rule *event_rule,
+ struct lttng_map_key *key)
+{
+ int err, fd, ret = 0;
+ unsigned int i, key_token_count;
+ enum lttng_error_code error_code_ret;
+ enum lttng_map_key_status status;
+ struct ltt_kernel_event_counter *event_counter;
+ struct lttng_kernel_counter_event k_counter_event = {};
+
+
+ event_counter = zmalloc(sizeof(*event_counter));
+ if (!event_counter) {
+ error_code_ret = LTTNG_ERR_NOMEM;
+ goto error;
+ }
+
+ trace_kernel_init_event_counter_from_event_rule(event_rule,
+ &k_counter_event);
+ event_counter->fd = -1;
+ event_counter->enabled = 1;
+ event_counter->action_tracer_token = action_tracer_token;
+ event_counter->filter = lttng_event_rule_get_filter_bytecode(event_rule);
+
+ k_counter_event.event.token = action_tracer_token;
+
+ /* Set the key pattern for this event counter. */
+ k_counter_event.key.nr_dimensions = 1;
+
+ status = lttng_map_key_get_token_count(key, &key_token_count);
+ if (status != LTTNG_MAP_KEY_STATUS_OK) {
+ error_code_ret = LTTNG_ERR_UNK;
+ goto error;
+ }
+
+ assert(key_token_count > 0);
+
+ k_counter_event.key.key_dimensions[0].nr_key_tokens = key_token_count;
+
+ for (i = 0; i < key_token_count; i++) {
+ const struct lttng_map_key_token *token =
+ lttng_map_key_get_token_at_index(key, i);
+
+ ret = add_key_token(&k_counter_event.key.key_dimensions[0].key_tokens[i],
+ token);
+ if (ret) {
+ ERR("Error appending map key token");
+ error_code_ret = LTTNG_ERR_INVALID;
+ goto error;
+ }
+ }
+
+ fd = kernctl_create_counter_event(kmap->fd, &k_counter_event);
+ if (fd < 0) {
+ switch (-fd) {
+ case EEXIST:
+ error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
+ break;
+ case ENOSYS:
+ WARN("Event counter type not implemented");
+ error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
+ break;
+ case ENOENT:
+ WARN("Event counter %s not found!", k_counter_event.event.name);
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ break;
+ default:
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ PERROR("create event counter ioctl");
+ }
+ }
+
+ event_counter->fd = fd;
+ event_counter->enabled = true;
+
+ /* Prevent fd duplication after execlp() */
+ err = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ if (err < 0) {
+ PERROR("fcntl session fd");
+ }
+
+ if (event_counter->filter) {
+ err = kernctl_filter(event_counter->fd, event_counter->filter);
+ if (err < 0) {
+ switch (-err) {
+ case ENOMEM:
+ error_code_ret = LTTNG_ERR_FILTER_NOMEM;
+ break;
+ default:
+ error_code_ret = LTTNG_ERR_FILTER_INVAL;
+ break;
+ }
+ goto filter_error;
+ }
+ }
+ if (lttng_event_rule_get_type(event_rule) ==
+ LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE) {
+ ret = userspace_probe_event_rule_add_callsites(
+ event_rule, creds, event_counter->fd);
+ if (ret) {
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ goto add_callsite_error;
+ }
+ }
+
+ err = kernctl_enable(event_counter->fd);
+ if (err < 0) {
+ switch (-err) {
+ case EEXIST:
+ error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
+ break;
+ default:
+ PERROR("enable kernel counter event");
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ break;
+ }
+ goto enable_error;
+ }
+
+ /* Add event to event list */
+ rcu_read_lock();
+ lttng_ht_node_init_u64(&event_counter->ht_node,
+ event_counter->action_tracer_token);
+ lttng_ht_add_unique_u64(kmap->event_counters_ht,
+ &event_counter->ht_node);
+ rcu_read_unlock();
+ kmap->event_count++;
+
+ DBG("Kernel event counter %s created (fd: %d)",
+ event_counter->event->name,
+ event_counter->fd);
+ error_code_ret = LTTNG_OK;
+
+add_callsite_error:
+filter_error:
+enable_error:
+error:
+ return error_code_ret;
+}
+
+static enum lttng_error_code kernel_create_event_notifier_rule(
+ struct lttng_trigger *trigger,
+ const struct lttng_credentials *creds, uint64_t token)
+{
+ int err, fd, ret = 0;
+ enum lttng_error_code error_code_ret;
+ enum lttng_condition_status condition_status;
+ enum lttng_condition_type condition_type;
+ enum lttng_event_rule_type event_rule_type;
+ struct ltt_kernel_event_notifier_rule *event_notifier_rule;
+ struct lttng_kernel_event_notifier kernel_event_notifier = {};
+ unsigned int capture_bytecode_count = 0, i;
+ const struct lttng_condition *condition = NULL;
+ const struct lttng_event_rule *event_rule = NULL;
+ enum lttng_condition_status cond_status;
+
+ assert(trigger);
+
+ condition = lttng_trigger_get_const_condition(trigger);
+ assert(condition);
+
+ condition_type = lttng_condition_get_type(condition);
+ assert(condition_type == LTTNG_CONDITION_TYPE_ON_EVENT);
+
+ /* Does not acquire a reference. */
+ condition_status = lttng_condition_on_event_get_rule(
+ condition, &event_rule);
+ assert(condition_status == LTTNG_CONDITION_STATUS_OK);
+ assert(event_rule);
+
+ event_rule_type = lttng_event_rule_get_type(event_rule);
+ assert(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
+
+ error_code_ret = trace_kernel_create_event_notifier_rule(trigger, token,
+ lttng_condition_on_event_get_error_counter_index(condition),
+ &event_notifier_rule);
+ if (error_code_ret != LTTNG_OK) {
+ goto error;
+ }
+
+ error_code_ret = trace_kernel_init_event_notifier_from_event_rule(
+ event_rule, &kernel_event_notifier);
+ if (error_code_ret != LTTNG_OK) {
+ goto free_event;
+ }
+
+ kernel_event_notifier.event.token = event_notifier_rule->token;
+ kernel_event_notifier.error_counter_idx =
+ lttng_condition_on_event_get_error_counter_index(condition);
+
+ fd = kernctl_create_event_notifier(
+ kernel_tracer_event_notifier_group_fd,
+ &kernel_event_notifier);
+ if (fd < 0) {
+ switch (-fd) {
+ case EEXIST:
+ error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
+ break;
+ case ENOSYS:
+ WARN("Failed to create kernel event notifier: not notifier type not implemented");
+ error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
+ break;
+ case ENOENT:
+ WARN("Failed to create kernel event notifier: not found: name = '%s'",
+ kernel_event_notifier.event.name);
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ break;
+ default:
+ PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
+ fd, kernel_event_notifier.event.name);
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ }
+ goto free_event;
+ }
+
+ event_notifier_rule->fd = fd;
+ /* Prevent fd duplication after execlp(). */
+ err = fcntl(event_notifier_rule->fd, F_SETFD, FD_CLOEXEC);
+ if (err < 0) {
+ PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
+ fd);
+ error_code_ret = LTTNG_ERR_FATAL;
+ goto set_cloexec_error;
+ }
+
+ if (event_notifier_rule->filter) {
+ err = kernctl_filter(event_notifier_rule->fd, event_notifier_rule->filter);
+ if (err < 0) {
+ switch (-err) {
+ case ENOMEM:
+ error_code_ret = LTTNG_ERR_FILTER_NOMEM;
+ break;
+ default:
+ error_code_ret = LTTNG_ERR_FILTER_INVAL;
+ break;
+ }
+ goto filter_error;
+ }
+ }
+
+ if (lttng_event_rule_get_type(event_rule) ==
+ LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE) {
+ ret = userspace_probe_event_rule_add_callsites(
+ event_rule, creds, event_notifier_rule->fd);
+ if (ret) {
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ goto add_callsite_error;
+ }
+ }
+
+ /* Set the capture bytecode if any */
+ cond_status = lttng_condition_on_event_get_capture_descriptor_count(condition, &capture_bytecode_count);
+ assert(cond_status == LTTNG_CONDITION_STATUS_OK);
+ for (i = 0; i < capture_bytecode_count; i++) {
+ const struct lttng_bytecode *capture_bytecode =
+ lttng_condition_on_event_get_capture_bytecode_at_index(
+ condition, i);
+ if (capture_bytecode == NULL) {
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ goto error;
+ }
+
+ ret = kernctl_capture(event_notifier_rule->fd, capture_bytecode);
+ if (ret < 0) {
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ goto error;
+ }
+ }
+
+ err = kernctl_enable(event_notifier_rule->fd);
+ if (err < 0) {
+ switch (-err) {
+ case EEXIST:
+ error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
+ break;
+ default:
+ PERROR("enable kernel event notifier");
+ error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
+ break;
+ }
+ goto enable_error;
+ }
+
+ /* Add trigger to kernel token mapping in the hash table. */
+ rcu_read_lock();
+ cds_lfht_add(kernel_token_to_event_notifier_rule_ht, hash_trigger(trigger),
+ &event_notifier_rule->ht_node);
+ rcu_read_unlock();
+
+ DBG("Created kernel event notifier: name = '%s', fd = %d",
+ kernel_event_notifier.event.name,
+ event_notifier_rule->fd);
+
+ return LTTNG_OK;
+
+add_callsite_error:
+enable_error:
+set_cloexec_error:
+filter_error:
+ {
+ const int close_ret = close(event_notifier_rule->fd);
+
+ if (close_ret) {
+ PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
+ event_notifier_rule->fd);
+ }
+ }
+free_event:
+ free(event_notifier_rule);
+error:
+ return error_code_ret;
+}
+
+enum lttng_error_code kernel_register_event_notifier(
+ struct lttng_trigger *trigger,
+ const struct lttng_credentials *cmd_creds)
+{
+ enum lttng_error_code ret;
+ enum lttng_condition_status status;
+ enum lttng_domain_type domain_type;
+ const struct lttng_event_rule *event_rule;
+ const struct lttng_condition *const condition =
+ lttng_trigger_get_const_condition(trigger);
+ const uint64_t token = lttng_trigger_get_tracer_token(trigger);
+
+ assert(condition);
+
+ /* Does not acquire a reference to the event rule. */
+ status = lttng_condition_on_event_get_rule(
+ condition, &event_rule);
+ assert(status == LTTNG_CONDITION_STATUS_OK);
+
+ domain_type = lttng_event_rule_get_domain_type(event_rule);
+ assert(domain_type == LTTNG_DOMAIN_KERNEL);
+
+ ret = kernel_create_event_notifier_rule(trigger, cmd_creds, token);
+ if (ret != LTTNG_OK) {
+ ERR("Failed to create kernel event notifier rule");
+ }
+
+ return ret;
+}
+
+enum lttng_error_code kernel_unregister_event_notifier(
+ const struct lttng_trigger *trigger)
+{
+ struct ltt_kernel_event_notifier_rule *token_event_rule_element;
+ struct cds_lfht_node *node;
+ struct cds_lfht_iter iter;
+ enum lttng_error_code error_code_ret;
+ int ret;
+
+ rcu_read_lock();
+
+ cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht,
+ hash_trigger(trigger), match_trigger, trigger, &iter);
+
+ node = cds_lfht_iter_get_node(&iter);
+ if (!node) {
+ error_code_ret = LTTNG_ERR_TRIGGER_NOT_FOUND;
+ goto error;
+ }
+
+ token_event_rule_element = caa_container_of(node,
+ struct ltt_kernel_event_notifier_rule, ht_node);
+
+ ret = kernel_disable_event_notifier_rule(token_event_rule_element);
+ if (ret) {
+ error_code_ret = LTTNG_ERR_FATAL;
+ goto error;
+ }
+
+ trace_kernel_destroy_event_notifier_rule(token_event_rule_element);
+ error_code_ret = LTTNG_OK;
+
+error:
+ rcu_read_unlock();
+
+ return error_code_ret;
+}
+
+struct key_ht_entry {
+ char *key;
+ struct lttng_ht_node_str node;
+};
+
+enum lttng_error_code kernel_list_map_values(const struct ltt_kernel_map *map,
+ const struct lttng_map_query *query,
+ struct lttng_map_content **map_content)
+{
+ enum lttng_map_status map_status;
+ enum lttng_error_code ret_code;
+ const char *map_name = NULL;
+ uint64_t descr_count, descr_idx, cpu_idx;
+ struct lttng_map_content *local_map_content;
+ struct lttng_ht *key_ht;
+ struct lttng_ht *values = NULL;
+ struct lttng_ht_node_str *node;
+ struct key_ht_entry *ht_entry;
+ struct lttng_ht_iter iter;
+ enum lttng_map_query_status map_query_status;
+ const char *key_filter;
+ bool sum_cpus = lttng_map_query_get_config_sum_by_cpu(query);
+ enum lttng_map_query_config_cpu config_cpu;
+ int ret;
+ int selected_cpu;
+
+
+ local_map_content = lttng_map_content_create(LTTNG_BUFFER_GLOBAL);
+ if (!local_map_content) {
+ ERR("Error creating map content");
+ ret_code = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ map_query_status = lttng_map_query_get_key_filter(query, &key_filter);
+ if (map_query_status == LTTNG_MAP_QUERY_STATUS_NONE) {
+ key_filter = NULL;
+ } else if (map_query_status != LTTNG_MAP_QUERY_STATUS_OK) {
+ ret_code = LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ config_cpu = lttng_map_query_get_config_cpu(query);
+ if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) {
+ unsigned int count;
+ map_query_status = lttng_map_query_get_cpu_count(query, &count);
+ assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK);
+ assert(count == 1);
+
+ map_query_status = lttng_map_query_get_cpu_at_index(query, 0,
+ &selected_cpu);
+ assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK);
+ }
+
+ map_status = lttng_map_get_name(map->map, &map_name);
+ assert(map_status == LTTNG_MAP_STATUS_OK);
+
+ DBG("Listing kernel map values: map-name = '%s'", map_name);
+
+ ret = kernctl_counter_map_descriptor_count(map->fd, &descr_count);
+ if (ret) {
+ ERR("Error getting map descriptor count");
+ ret_code = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+
+ /*
+ * The kernel tracer sends us descriptors that may be identical aside
+ * from their user token field. This ABI was design this way to cover a
+ * potential use case where the user wants to know what enabler might
+ * have contributed to a specific bucket.
+ *
+ * We use this hashtable to de-duplicate keys.
+ */
+ if (sum_cpus) {
+ values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
+ if (!values) {
+ ret_code = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+ }
+
+ DBG("Querying kernel for all map values: "
+ "map-name = '%s', key-value count = %"PRIu64,
+ map_name, descr_count);
+ for (cpu_idx = 0; cpu_idx < utils_get_number_of_possible_cpus(); cpu_idx++) {
+ struct lttng_kernel_counter_read value = {0};
+
+ if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) {
+ if (selected_cpu != cpu_idx) {
+ continue;
+ }
+ }
+
+ if (!sum_cpus) {
+ values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
+ assert(values);
+ }
+
+ key_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
+ if (!key_ht) {
+ ret_code = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ for(descr_idx = 0; descr_idx < descr_count; descr_idx++) {
+ struct lttng_kernel_counter_map_descriptor descriptor = {0};
+
+ DBG("Querying kernel for map key-value descriptor: "
+ "map-name = '%s', descriptor = %"PRIu64,
+ map_name, descr_idx);
+ descriptor.descriptor_index = descr_idx;
+
+ ret = kernctl_counter_map_descriptor(map->fd, &descriptor);
+ if (ret) {
+ ERR("Error getting map descriptor %"PRIu64, descr_idx);
+ ret_code = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+
+ if (key_filter && strcmp(key_filter, descriptor.key) != 0) {
+ continue;
+ }
+
+ lttng_ht_lookup(key_ht, descriptor.key, &iter);
+ node = lttng_ht_iter_get_node_str(&iter);
+ if (node) {
+ /* This key was already appended to the list. */
+ continue;
+ }
+
+
+ value.index.number_dimensions = 1;
+ value.index.dimension_indexes[0] = descriptor.array_index;
+ value.cpu = cpu_idx;
+
+ DBG("Querying kernel for map descriptor value: "
+ "map-name = '%s', counter-index = %"PRIu64,
+ map_name, descriptor.array_index);
+ ret = kernctl_counter_read_value(map->fd, &value);
+ if (ret) {
+ ERR("Error getting value of map descriptor %"PRIu64, descr_idx);
+ ret_code = LTTNG_ERR_MAP_VALUES_LIST_FAIL;
+ goto end;
+ }
+
+ map_add_or_increment_map_values(values, descriptor.key,
+ value.value.value, value.value.underflow,
+ value.value.overflow);
+
+ ht_entry = zmalloc(sizeof(*ht_entry));
+ assert(ht_entry);
+ ht_entry->key = strdup(descriptor.key);
+ lttng_ht_node_init_str(&ht_entry->node, ht_entry->key);
+ lttng_ht_add_unique_str(key_ht, &ht_entry->node);
+ }
+
+ if (!sum_cpus) {
+ ret = map_new_content_section(local_map_content,
+ LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_KERNEL,
+ sum_cpus, 0,
+ cpu_idx, values);
+ if (ret) {
+ abort();
+ }
+
+ lttng_ht_destroy(values);
+ }
+
+ /*
+ * Remove all the keys before destroying the hashtable.
+ */
+ cds_lfht_for_each_entry(key_ht->ht, &iter.iter, ht_entry, node.node) {
+ struct lttng_ht_iter entry_iter;
+
+ entry_iter.iter.node = &ht_entry->node.node;
+ lttng_ht_del(key_ht, &entry_iter);
+
+ free(ht_entry);
+ }
+
+ lttng_ht_destroy(key_ht);
+ }
+
+ if (sum_cpus) {
+ ret = map_new_content_section(local_map_content,
+ LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_KERNEL,
+ sum_cpus, 0, 0, values);
+ if (ret) {
+ abort();
+ }
+ lttng_ht_destroy(values);
+ }
+
+
+ *map_content = local_map_content;
+ local_map_content = NULL;
+ ret_code = LTTNG_OK;
+
+end:
+ lttng_map_content_destroy(local_map_content);
+ return ret_code;
+}
+
+int kernel_get_notification_fd(void)
+{
+ return kernel_tracer_event_notifier_group_notification_fd;
+}