Correct error handling and add error message in ustcmd.c
[ust.git] / libustcmd / ustcmd.c
index c2a98459ce6fb069730966a441da0cafee6d87f7..c51232000310340d81144ed8e6607c412a6b405a 100644 (file)
@@ -25,7 +25,7 @@
 #include <dirent.h>
 
 #include "ustcomm.h"
-#include "ustcmd.h"
+#include "ust/ustcmd.h"
 #include "usterr.h"
 
 pid_t *ustcmd_get_online_pids(void)
@@ -90,10 +90,14 @@ int ustcmd_set_marker_state(const char *mn, int state, pid_t pid)
                return USTCMD_ERR_ARG;
        }
 
-       asprintf(&cmd, "%s %s", cmd_str[state], mn);
+       if (asprintf(&cmd, "%s %s", cmd_str[state], mn) < 0) {
+               ERR("ustcmd_set_marker_state : asprintf failed (%s %s)",
+                   cmd_str[state], mn);
+               return USTCMD_ERR_GEN;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
-       if (result) {
+       if (result != 1) {
                free(cmd);
                return USTCMD_ERR_GEN;
        }
@@ -114,7 +118,11 @@ int ustcmd_set_subbuf_size(const char *channel_size, pid_t pid)
        char *cmd;
        int result;
 
-       asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size);
+       if (asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size) < 0) {
+               ERR("ustcmd_set_subbuf_size : asprintf failed (set_subbuf_size %s)",
+                   channel_size);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
        if (result != 1) {
@@ -138,7 +146,11 @@ int ustcmd_set_subbuf_num(const char *channel_size, pid_t pid)
        char *cmd;
        int result;
 
-       asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size);
+       if (asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size) < 0) {
+               ERR("ustcmd_set_subbuf_num : asprintf failed (set_subbuf_num %s",
+                   channel_size);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
        if (result != 1) {
@@ -163,12 +175,15 @@ int ustcmd_get_subbuf_size(const char *channel, pid_t pid)
        int result;
 
        /* format: channel_cpu */
-       asprintf(&cmd, "%s %s_0", "get_subbuf_size", channel);
+       if (asprintf(&cmd, "%s %s_0", "get_subbuf_size", channel) < 0) {
+               ERR("ustcmd_get_subbuf_size : asprintf failed (get_subbuf_size, %s_0",
+                   channel);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, &reply);
-       if (result) {
+       if (result != 1) {
                free(cmd);
-               free(reply);
                return -1;
        }
 
@@ -191,12 +206,15 @@ int ustcmd_get_subbuf_num(const char *channel, pid_t pid)
        int result;
 
        /* format: channel_cpu */
-       asprintf(&cmd, "%s %s_0", "get_n_subbufs", channel);
+       if (asprintf(&cmd, "%s %s_0", "get_n_subbufs", channel) < 0) {
+               ERR("ustcmd_get_subbuf_num : asprintf failed (get_n_subbufs, %s_0",
+                   channel);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, &reply);
-       if (result) {
+       if (result != 1) {
                free(cmd);
-               free(reply);
                return -1;
        }
 
@@ -377,10 +395,6 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
                return -1;
        }
        result = ustcmd_send_cmd("list_markers", pid, &big_str);
-       if (result != 1) {
-               return -1;
-       }
-
        if (result != 1) {
                ERR("error while getting markers list");
                return -1;
@@ -389,6 +403,7 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
        tmp_cmsf = (struct marker_status *) malloc(sizeof(struct marker_status) *
                (ustcmd_count_nl(big_str) + 1));
        if (tmp_cmsf == NULL) {
+               ERR("Failed to allocate CMSF array");
                return -1;
        }
 
@@ -420,6 +435,151 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
        return 0;
 }
 
+
+/**
+ * Frees a TES array.
+ *
+ * @param tes  TES array to free
+ * @return     0 if successful, or error USTCMD_ERR_ARG
+ */
+int ustcmd_free_tes(struct trace_event_status *tes)
+{
+       if (tes == NULL) {
+               return USTCMD_ERR_ARG;
+       }
+
+       unsigned int i = 0;
+       while (tes[i].name != NULL) {
+               free(tes[i].name);
+               ++i;
+       }
+       free(tes);
+
+       return 0;
+}
+
+/**
+ * Gets trace_events string for a given PID.
+ *
+ * @param tes  Pointer to TES array to be filled (callee allocates, caller
+ *             frees with `ustcmd_free_tes')
+ * @param pid  Targeted PID
+ * @return     0 if successful, or -1 on error
+ */
+int ustcmd_get_tes(struct trace_event_status **tes,
+                           const pid_t pid)
+{
+       char *big_str = NULL;
+       int result;
+       struct trace_event_status *tmp_tes = NULL;
+       unsigned int i = 0, tes_ind = 0;
+
+       if (tes == NULL) {
+               return -1;
+       }
+
+       result = ustcmd_send_cmd("list_trace_events", pid, &big_str);
+       if (result != 1) {
+               ERR("error while getting trace_event list");
+               return -1;
+       }
+
+       tmp_tes = (struct trace_event_status *)
+               zmalloc(sizeof(struct trace_event_status) *
+                       (ustcmd_count_nl(big_str) + 1));
+       if (tmp_tes == NULL) {
+               ERR("Failed to allocate TES array");
+               return -1;
+       }
+
+       /* Parse received reply string (format: "[name]"): */
+       while (big_str[i] != '\0') {
+               char state;
+
+               sscanf(big_str + i, "trace_event: %a[^\n]",
+                       &tmp_tes[tes_ind].name);
+               while (big_str[i] != '\n') {
+                       ++i; /* Go to next '\n' */
+               }
+               ++i; /* Skip current pointed '\n' */
+               ++tes_ind;
+       }
+       tmp_tes[tes_ind].name = NULL;
+
+       *tes = tmp_tes;
+
+       free(big_str);
+       return 0;
+}
+
+/**
+ * Set socket path
+ *
+ * @param sock_path    Socket path
+ * @param pid          Traced process ID
+ * @return             0 if successful, or error
+ */
+int ustcmd_set_sock_path(const char *sock_path, pid_t pid)
+{
+       char *cmd;
+       int result;
+
+       if (asprintf(&cmd, "%s %s", "set_sock_path", sock_path) < 0) {
+               ERR("ustcmd_set_sock_path : asprintf failed (set_sock_path, %s",
+                   sock_path);
+               return -1;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, NULL);
+       if (result != 1) {
+               free(cmd);
+               return USTCMD_ERR_GEN;
+       }
+
+       free(cmd);
+       return 0;
+}
+
+/**
+ * Get socket path
+ *
+ * @param sock_path    Pointer to where the socket path will be returned
+ * @param pid          Traced process ID
+ * @return             0 if successful, or error
+ */
+int ustcmd_get_sock_path(char **sock_path, pid_t pid)
+{
+       char *cmd, *reply;
+       int result;
+
+       if (asprintf(&cmd, "%s", "get_sock_path") < 0) {
+               ERR("ustcmd_get_sock_path : asprintf failed");
+               return USTCMD_ERR_GEN;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, &reply);
+       if (result != 1) {
+               free(cmd);
+               return USTCMD_ERR_GEN;
+       }
+
+       free(cmd);
+       *sock_path = reply;
+       return 0;
+}
+
+int ustcmd_force_switch(pid_t pid)
+{
+       int result;
+
+       result = ustcmd_send_cmd("force_switch", pid, NULL);
+       if (result != 1) {
+               return USTCMD_ERR_GEN;
+       }
+
+       return 0;
+}
+
 /**
  * Sends a given command to a traceable process
  *
@@ -427,7 +587,7 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
  * @param pid  Targeted PID
  * @param reply        Pointer to string to be filled with a reply string (must
  *             be NULL if no reply is needed for the given command).
- * @return     -1 if successful, 0 on EOT, 1 on success
+ * @return     -1 if not successful, 0 on EOT, 1 on success
  */
 
 int ustcmd_send_cmd(const char *cmd, const pid_t pid, char **reply)
This page took 0.028178 seconds and 4 git commands to generate.