+int lttng_register_trigger(struct lttng_trigger *trigger)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+ struct lttng_dynamic_buffer buffer;
+
+ lttng_dynamic_buffer_init(&buffer);
+ if (!trigger) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (!lttng_trigger_validate(trigger)) {
+ ret = -LTTNG_ERR_INVALID_TRIGGER;
+ goto end;
+ }
+
+ ret = lttng_trigger_serialize(trigger, &buffer);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_REGISTER_TRIGGER;
+ lsm.u.trigger.length = (uint32_t) buffer.size;
+ ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
+ buffer.size, NULL);
+end:
+ lttng_dynamic_buffer_reset(&buffer);
+ return ret;
+}
+
+int lttng_unregister_trigger(struct lttng_trigger *trigger)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+ struct lttng_dynamic_buffer buffer;
+
+ lttng_dynamic_buffer_init(&buffer);
+ if (!trigger) {
+ ret = -LTTNG_ERR_INVALID;
+ goto end;
+ }
+
+ if (!lttng_trigger_validate(trigger)) {
+ ret = -LTTNG_ERR_INVALID_TRIGGER;
+ goto end;
+ }
+
+ ret = lttng_trigger_serialize(trigger, &buffer);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_UNK;
+ goto end;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+ lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
+ lsm.u.trigger.length = (uint32_t) buffer.size;
+ ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buffer.data,
+ buffer.size, NULL);
+end:
+ lttng_dynamic_buffer_reset(&buffer);
+ return ret;
+}
+
+static int lttng_track_untrack_id(struct lttng_handle *handle,
+ enum lttng_tracker_type tracker_type,
+ const struct lttng_tracker_id *id,
+ enum lttcomm_sessiond_command cmd)
+{
+ int ret;
+ struct lttcomm_session_msg lsm;
+ const char *var_data;
+ size_t var_data_len = 0;
+ int value;
+ enum lttng_tracker_id_status status;
+
+ /* NULL arguments are forbidden. No default values. */
+ if (handle == NULL) {
+ goto error;
+ }
+
+ memset(&lsm, 0, sizeof(lsm));
+
+ lsm.cmd_type = cmd;
+ lsm.u.id_tracker.tracker_type = tracker_type;
+ lsm.u.id_tracker.id_type = lttng_tracker_id_get_type(id);
+ switch (lsm.u.id_tracker.id_type) {
+ case LTTNG_ID_ALL:
+ break;
+ case LTTNG_ID_VALUE:
+ status = lttng_tracker_id_get_value(id, &value);
+ if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+ goto error;
+ }
+ lsm.u.id_tracker.u.value = value;
+ break;
+ case LTTNG_ID_STRING:
+ status = lttng_tracker_id_get_string(id, &var_data);
+ if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+ goto error;
+ }
+ var_data_len = strlen(var_data) + 1; /* Includes \0. */
+ lsm.u.id_tracker.u.var_len = var_data_len;
+ break;
+ default:
+ goto error;
+ }
+
+ COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
+
+ lttng_ctl_copy_string(lsm.session.name, handle->session_name,
+ sizeof(lsm.session.name));
+
+ ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
+ &lsm, (char *) var_data, var_data_len, NULL);
+ return ret;
+error:
+ return -LTTNG_ERR_INVALID;
+}
+
+/*
+ * Add ID to session tracker.
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_track_id(struct lttng_handle *handle,
+ enum lttng_tracker_type tracker_type,
+ const struct lttng_tracker_id *id)
+{
+ return lttng_track_untrack_id(handle, tracker_type, id, LTTNG_TRACK_ID);
+}
+
+/*
+ * Remove ID from session tracker.
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_untrack_id(struct lttng_handle *handle,
+ enum lttng_tracker_type tracker_type,
+ const struct lttng_tracker_id *id)
+{
+ return lttng_track_untrack_id(
+ handle, tracker_type, id, LTTNG_UNTRACK_ID);
+}
+
+/*
+ * Add PID to session tracker.
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_track_pid(struct lttng_handle *handle, int pid)
+{
+ int ret;
+ struct lttng_tracker_id *id = NULL;
+ enum lttng_tracker_id_status status;
+
+ id = lttng_tracker_id_create();
+ status = lttng_tracker_id_set_value(id, pid);
+ if (status == LTTNG_TRACKER_ID_STATUS_INVALID) {
+ ret = -LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ ret = lttng_track_id(handle, LTTNG_TRACKER_PID, id);
+error:
+ lttng_tracker_id_destroy(id);
+ return ret;
+}
+
+/*
+ * Remove PID from session tracker.
+ * Return 0 on success else a negative LTTng error code.
+ */
+int lttng_untrack_pid(struct lttng_handle *handle, int pid)
+{
+ int ret;
+ struct lttng_tracker_id *id = NULL;
+ enum lttng_tracker_id_status status;
+
+ id = lttng_tracker_id_create();
+ status = lttng_tracker_id_set_value(id, pid);
+ if (status == LTTNG_TRACKER_ID_STATUS_INVALID) {
+ ret = -LTTNG_ERR_INVALID;
+ goto error;
+ }
+
+ ret = lttng_untrack_id(handle, LTTNG_TRACKER_PID, id);
+error:
+ lttng_tracker_id_destroy(id);
+ return ret;
+}
+