+
+/*
+ * Can be safely performed concurrently with tracing using the struct
+ * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
+ * contexts.
+ *
+ * This does not allow adding, removing, or changing typing of the
+ * contexts, since this needs to stay invariant for metadata. However,
+ * it allows updating the handlers associated with all contexts matching
+ * a provider (by name) while tracing is using it, in a way that ensures
+ * a single RCU read-side critical section see either all old, or all
+ * new handlers.
+ */
+int lttng_ust_context_set_provider_rcu(struct lttng_ctx **_ctx,
+ const char *name,
+ size_t (*get_size)(struct lttng_ctx_field *field, size_t offset),
+ void (*record)(struct lttng_ctx_field *field,
+ struct lttng_ust_lib_ring_buffer_ctx *ctx,
+ struct lttng_channel *chan),
+ void (*get_value)(struct lttng_ctx_field *field,
+ struct lttng_ctx_value *value))
+{
+ int i, ret;
+ struct lttng_ctx *ctx = *_ctx, *new_ctx;
+ struct lttng_ctx_field *new_fields;
+
+ if (!ctx || !lttng_find_context_provider(ctx, name))
+ return 0;
+ /*
+ * We have at least one instance of context for the provider.
+ */
+ new_ctx = zmalloc(sizeof(*new_ctx));
+ if (!new_ctx)
+ return -ENOMEM;
+ *new_ctx = *ctx;
+ new_fields = zmalloc(sizeof(*new_fields) * ctx->allocated_fields);
+ if (!new_fields) {
+ ret = -ENOMEM;
+ goto field_error;
+ }
+ memcpy(new_fields, ctx->fields,
+ sizeof(*new_fields) * ctx->allocated_fields);
+ for (i = 0; i < ctx->nr_fields; i++) {
+ if (strncmp(new_fields[i].event_field.name,
+ name, strlen(name)) != 0)
+ continue;
+ new_fields[i].get_size = get_size;
+ new_fields[i].record = record;
+ new_fields[i].get_value = get_value;
+ }
+ new_ctx->fields = new_fields;
+ rcu_assign_pointer(*_ctx, new_ctx);
+ synchronize_trace();
+ free(ctx->fields);
+ free(ctx);
+ return 0;
+
+field_error:
+ free(new_ctx);
+ return ret;
+}
+
+int lttng_session_context_init(struct lttng_ctx **ctx)
+{
+ int ret;
+
+ ret = lttng_add_pthread_id_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_pthread_id_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vtid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vtid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vpid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vpid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_procname_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_procname_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_cpu_id_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_cpu_id_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_cgroup_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_ipc_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_mnt_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_net_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_net_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_pid_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_pid_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_user_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_user_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_uts_ns_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_uts_ns_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vuid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vuid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_veuid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_veuid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vsuid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vsuid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vgid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vgid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vegid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vegid_to_ctx");
+ goto error;
+ }
+ ret = lttng_add_vsgid_to_ctx(ctx);
+ if (ret) {
+ WARN("Cannot add context lttng_add_vsgid_to_ctx");
+ goto error;
+ }
+ lttng_context_update(*ctx);
+ return 0;
+
+error:
+ lttng_destroy_context(*ctx);
+ return ret;
+}
+
+/* For backward compatibility. Leave those exported symbols in place. */
+struct lttng_ctx *lttng_static_ctx;
+
+void lttng_context_init(void)
+{
+}
+
+void lttng_context_exit(void)
+{
+}