+static
+enum lttng_error_code snapshot_record(struct ltt_session *session,
+ const struct snapshot_output *snapshot_output, int wait)
+{
+ int fmt_ret;
+ int64_t nb_packets_per_stream;
+ char snapshot_chunk_name[LTTNG_NAME_MAX];
+ enum lttng_error_code ret = LTTNG_OK;
+ struct lttng_trace_chunk *snapshot_trace_chunk;
+
+ fmt_ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
+ "%s-%s-%" PRIu64,
+ snapshot_output->name,
+ snapshot_output->datetime,
+ snapshot_output->nb_snapshot);
+ if (fmt_ret < 0 || fmt_ret >= sizeof(snapshot_chunk_name)) {
+ ERR("Failed to format snapshot name");
+ ret = LTTNG_ERR_INVALID;
+ goto end;
+ }
+ DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
+ snapshot_output->name, session->name,
+ snapshot_chunk_name);
+ snapshot_trace_chunk = session_create_new_trace_chunk(session,
+ snapshot_output_get_base_path(snapshot_output),
+ snapshot_chunk_name);
+ if (!snapshot_trace_chunk) {
+ ret = LTTNG_ERR_CREATE_DIR_FAIL;
+ goto end;
+ }
+ assert(!session->current_trace_chunk);
+ ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
+ lttng_trace_chunk_put(snapshot_trace_chunk);
+ snapshot_trace_chunk = NULL;
+ if (ret) {
+ ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
+ goto end;
+ }
+
+ nb_packets_per_stream = get_session_nb_packets_per_stream(session,
+ snapshot_output->max_size);
+ if (nb_packets_per_stream < 0) {
+ ret = LTTNG_ERR_MAX_SIZE_INVALID;
+ goto end;
+ }
+
+ if (session->kernel_session) {
+ ret = record_kernel_snapshot(session->kernel_session,
+ snapshot_output, session,
+ wait, nb_packets_per_stream);
+ if (ret != LTTNG_OK) {
+ goto end;
+ }
+ }
+
+ if (session->ust_session) {
+ ret = record_ust_snapshot(session->ust_session,
+ snapshot_output, session,
+ wait, nb_packets_per_stream);
+ if (ret != LTTNG_OK) {
+ goto end;
+ }
+ }
+
+ if (session_close_trace_chunk(
+ session, session->current_trace_chunk, NULL)) {
+ /*
+ * Don't goto end; make sure the chunk is closed for the session
+ * to allow future snapshots.
+ */
+ ERR("Failed to close snapshot trace chunk of session \"%s\"",
+ session->name);
+ ret = -1;
+ }
+ if (session_set_trace_chunk(session, NULL, NULL)) {
+ ERR("Failed to release the current trace chunk of session \"%s\"",
+ session->name);
+ ret = -1;
+ }
+end:
+ return ret;
+}
+