Major changes of command processing for sessiond
[lttng-tools.git] / ltt-sessiond / trace.c
index c5d700fd1450b17d842c20a93eef220d75e83d5d..9a3c95956439e08a462b8e1e21f25ed050359a08 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <urcu/list.h>
+#include <ust/ustctl.h>
 
+#include "liblttsessiondcomm.h"
 #include "lttngerr.h"
 #include "trace.h"
 #include "session.h"
+#include "ltt-sessiond.h"
+
+static struct ltt_ust_trace *find_session_ust_trace_by_pid(
+               struct ltt_session *session, pid_t pid);
 
 /*
  *  find_session_ust_trace_by_pid
@@ -34,7 +40,8 @@
  *  Iterate over the session ust_traces and
  *  return a pointer or NULL if not found.
  */
-struct ltt_ust_trace *find_session_ust_trace_by_pid(struct ltt_session *session, pid_t pid)
+static struct ltt_ust_trace *find_session_ust_trace_by_pid(
+               struct ltt_session *session, pid_t pid)
 {
        struct ltt_ust_trace *iter;
 
@@ -97,3 +104,116 @@ void get_traces_per_session(struct ltt_session *session, struct lttng_trace *tra
                i++;
        }
 }
+
+/*
+ *  ust_create_trace
+ *
+ *  Create an userspace trace using pid.
+ *  This trace is then appended to the current session
+ *  ust trace list.
+ */
+int ust_create_trace(struct command_ctx *cmd_ctx)
+{
+       int ret;
+       struct ltt_ust_trace *trace;
+
+       DBG("Creating trace for pid %d", cmd_ctx->lsm->pid);
+
+       trace = malloc(sizeof(struct ltt_ust_trace));
+       if (trace == NULL) {
+               perror("malloc");
+               ret = -1;
+               goto error;
+       }
+
+       /* Init */
+       trace->pid = cmd_ctx->lsm->pid;
+       trace->shmid = 0;
+       /* NOTE: to be removed. Trace name will no longer be
+        * required for LTTng userspace tracer. For now, we set it
+        * to 'auto' for API compliance.
+        */
+       snprintf(trace->name, 5, "auto");
+
+       ret = ustctl_create_trace(cmd_ctx->ust_sock, trace->name);
+       if (ret < 0) {
+               ret = LTTCOMM_CREATE_FAIL;
+               goto error_create;
+       }
+
+       /* Check if current session is valid */
+       if (cmd_ctx->session) {
+               cds_list_add(&trace->list, &cmd_ctx->session->ust_traces);
+               cmd_ctx->session->ust_trace_count++;
+       }
+
+       return LTTCOMM_OK;
+
+error_create:
+       free(trace);
+error:
+       return ret;
+}
+
+/*
+ *  ust_start_trace
+ *
+ *  Start a trace. This trace, identified by the pid, must be
+ *  in the current session ust_traces list.
+ */
+int ust_start_trace(struct command_ctx *cmd_ctx)
+{
+       int ret;
+       struct ltt_ust_trace *trace;
+
+       DBG("Starting trace for pid %d", cmd_ctx->lsm->pid);
+
+       trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
+       if (trace == NULL) {
+               ret = LTTCOMM_NO_TRACE;
+               goto error;
+       }
+
+       ret = ustctl_start_trace(cmd_ctx->ust_sock, "auto");
+       if (ret < 0) {
+               ret = LTTCOMM_START_FAIL;
+               goto error;
+       }
+
+       ret = LTTCOMM_OK;
+
+error:
+       return ret;
+}
+
+/*
+ *  ust_stop_trace
+ *
+ *  Stop a trace. This trace, identified by the pid, must be
+ *  in the current session ust_traces list.
+ */
+int ust_stop_trace(struct command_ctx *cmd_ctx)
+{
+       int ret;
+       struct ltt_ust_trace *trace;
+
+       DBG("Stopping trace for pid %d", cmd_ctx->lsm->pid);
+
+       trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
+       if (trace == NULL) {
+               ret = LTTCOMM_NO_TRACE;
+               goto error;
+       }
+
+       ret = ustctl_stop_trace(cmd_ctx->ust_sock, trace->name);
+       if (ret < 0) {
+               ret = LTTCOMM_STOP_FAIL;
+               goto error;
+       }
+
+       ret = LTTCOMM_OK;
+
+error:
+       return ret;
+}
+
This page took 0.024959 seconds and 4 git commands to generate.