Fix create trace using the current session
authorDavid Goulet <david.goulet@polymtl.ca>
Wed, 27 Apr 2011 18:53:49 +0000 (14:53 -0400)
committerDavid Goulet <david.goulet@polymtl.ca>
Wed, 27 Apr 2011 18:56:32 +0000 (14:56 -0400)
Better create trace internal function for the session daemon.

Add a check for command that needs a session.
Add lttcomm error message for missing session.

Signed-off-by: David Goulet <david.goulet@polymtl.ca>
liblttsessiondcomm/liblttsessiondcomm.c
liblttsessiondcomm/liblttsessiondcomm.h
ltt-sessiond/ltt-sessiond.c
ltt-sessiond/ltt-sessiond.h

index bc74cc9e837ac867fab89c38836929f6096f236d..2ab185ce950c03e3f0f870838db069edf4e3b68f 100644 (file)
@@ -42,6 +42,9 @@ static const char *lttcomm_readable_code[] = {
        [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESS) ] = "No session found",
        [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
        [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
 };
 
 /*
index 83dcdc377ddbeaeebcc7eb35d8a54389185efc99..540241a62df1078c4109d15c83d58d86757b552f 100644 (file)
@@ -76,6 +76,8 @@ enum lttcomm_return_code {
        LTTCOMM_NO_APPS,                /* No traceable application */
        LTTCOMM_NO_SESS,                /* No sessions available */
        LTTCOMM_FATAL,                  /* Session daemon had a fatal error */
+       LTTCOMM_NO_TRACEABLE,   /* Error for non traceable app */
+       LTTCOMM_SELECT_SESS,    /* Must select a session */
        LTTCOMM_NR,                             /* Last element */
 };
 
index 6df78d8e3c40cb88d11301d66be4878aabd77fcf..a4baafc0761147687a9b03410df5a986dff609a0 100644 (file)
@@ -56,6 +56,7 @@ static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_sessi
 static int check_existing_daemon(void);
 static int notify_apps(const char* name);
 static int connect_app(pid_t pid);
+static int find_app_by_pid(pid_t pid);
 static int init_daemon_socket(void);
 static int process_client_msg(int sock, struct lttcomm_session_msg*);
 static int send_unix_sock(int sock, void *buf, size_t len);
@@ -157,10 +158,10 @@ static void *thread_manage_apps(void *data)
                        traceable_app_count++;
                } else {
                        /* Unregistering */
-                       lta = NULL;
                        cds_list_for_each_entry(lta, &ltt_traceable_app_list.head, list) {
                                if (lta->pid == reg_msg.pid && lta->uid == reg_msg.uid) {
                                        cds_list_del(&lta->list);
+                                       free(lta);
                                        /* Check to not overflow here */
                                        if (traceable_app_count != 0) {
                                                traceable_app_count--;
@@ -168,11 +169,6 @@ static void *thread_manage_apps(void *data)
                                        break;
                                }
                        }
-
-                       /* If an item was found, free it from memory */
-                       if (lta) {
-                               free(lta);
-                       }
                }
        }
 
@@ -259,10 +255,18 @@ static int send_unix_sock(int sock, void *buf, size_t len)
  *
  *     Return a socket connected to the libust communication socket
  *     of the application identified by the pid.
+ *
+ *     If the pid is not found in the traceable list,
+ *     return -1 to indicate error.
  */
 static int connect_app(pid_t pid)
 {
-       int sock;
+       int sock, ret;
+
+       ret = find_app_by_pid(pid);
+       if (ret == 0) {
+               return -1;
+       }
 
        sock = ustctl_connect_pid(pid);
        if (sock < 0) {
@@ -302,6 +306,26 @@ error:
        return ret;
 }
 
+/*
+ *  find_app_by_pid
+ *
+ *  Iterate over the traceable apps list.
+ *  On success, return 1, else return 0
+ */
+static int find_app_by_pid(pid_t pid)
+{
+       struct ltt_traceable_app *iter;
+
+       cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
+               if (iter->pid == pid) {
+                       /* Found */
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 /*
  *     find_session_by_uuid
  *
@@ -436,6 +460,51 @@ error:
        return -1;
 }
 
+/*
+ *  ust_create_trace
+ *
+ *  Create an userspace trace using pid.
+ *  This trace is then appended to the current session
+ *  ust trace list.
+ */
+static int ust_create_trace(pid_t pid)
+{
+       int sock, ret;
+       struct ltt_ust_trace *trace;
+
+       trace = malloc(sizeof(struct ltt_ust_trace));
+       if (trace == NULL) {
+               perror("malloc");
+               ret = -1;
+               goto error;
+       }
+
+       /* Init */
+       trace->pid = pid;
+       trace->shmid = 0;
+
+       /* Connect to app using ustctl API */
+       sock = connect_app(pid);
+       if (sock < 0) {
+               ret = LTTCOMM_NO_TRACEABLE;
+               goto error;
+       }
+
+       ret = ustctl_create_trace(sock, "auto");
+       if (ret < 0) {
+               ret = LTTCOMM_CREATE_FAIL;
+               goto error;
+       }
+
+       /* Check if current session is valid */
+       if (current_session) {
+               cds_list_add(&trace->list, &current_session->ust_traces);
+       }
+
+error:
+       return ret;
+}
+
 /*
  *     get_list_apps
  *
@@ -553,6 +622,19 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
         */
        copy_common_data(&llm, lsm);
 
+       /* Check command that needs a session */
+       if (lsm->cmd_type != LTTNG_CREATE_SESSION &&
+               lsm->cmd_type != LTTNG_LIST_SESSIONS &&
+               lsm->cmd_type != UST_LIST_APPS)
+       {
+               current_session = find_session_by_uuid(lsm->session_id);
+               if (current_session == NULL) {
+                       ret = LTTCOMM_SELECT_SESS;
+                       goto end;
+               }
+       }
+
+
        /* Default return code.
         * In our world, everything is OK... right? ;)
         */
@@ -589,16 +671,13 @@ static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
                }
                case UST_CREATE_TRACE:
                {
-                       int sock;
-                       sock = connect_app(lsm->pid);
-
-                       ret = ustctl_create_trace(sock, "auto");
+                       ret = ust_create_trace(lsm->pid);
                        if (ret < 0) {
                                ret = LTTCOMM_CREATE_FAIL;
-                       } else {
-                               ret = LTTCOMM_OK;
+                               goto end;
                        }
 
+                       /* No auxiliary data so only send the llm struct. */
                        goto end;
                }
                case UST_LIST_APPS:
index 97f8658e1ab764a24774e04fe2e527423cf02169..6c0824b80a6bb7d4f4436dba9304fed3594378f3 100644 (file)
@@ -39,7 +39,7 @@ struct ltt_lttng_trace {
 struct ltt_ust_trace {
        struct cds_list_head list;
        int shmid;
-       char trace_name[NAME_MAX];
+       pid_t pid;
        struct cds_list_head markers;
 };
 
This page took 0.028324 seconds and 4 git commands to generate.