usttrace: improve
[ust.git] / ust / ust.c
index ab5b5360a40ab5f81cc918c7f7d16d1c741cef55..789dfe0feeb7c8d44da14ebc97ae23cef75969c3 100644 (file)
--- a/ust/ust.c
+++ b/ust/ust.c
@@ -24,6 +24,7 @@
 
 #include "ustcomm.h"
 #include "ustcmd.h"
+#include "usterr.h"
 
 enum command {
        START_TRACE,
@@ -35,12 +36,12 @@ enum command {
        DISABLE_MARKER,
        GET_ONLINE_PIDS,
        UNKNOWN
-};     
+};
 
 struct ust_opts {
        enum command cmd;
        pid_t *pids;
-       char* m_name;
+       char *regex;
 };
 
 char *progname = NULL;
@@ -66,7 +67,7 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
        int c;
 
        opts->pids = NULL;
-       opts->m_name = NULL;
+       opts->regex = NULL;
 
        while (1) {
                int option_index = 0;
@@ -115,11 +116,11 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
                        break;
                case 1007:
                        opts->cmd = ENABLE_MARKER;
-                       opts->m_name = strdup(optarg);
+                       opts->regex = strdup(optarg);
                        break;
                case 1008:
                        opts->cmd = DISABLE_MARKER;
-                       opts->m_name = strdup(optarg);
+                       opts->regex = strdup(optarg);
                        break;
                case 1011:
                        opts->cmd = GET_ONLINE_PIDS;
@@ -138,13 +139,21 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
                }
        }
 
-       if(argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
+       if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) {
                int i;
                int pididx=0;
                opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
 
                for(i=optind; i<argc; i++) {
-                       opts->pids[pididx++] = atoi(argv[i]);
+                       /* don't take any chances, use a long long */
+                       long long tmp;
+                       char *endptr;
+                       tmp = strtoull(argv[i], &endptr, 10);
+                       if(*endptr != '\0') {
+                               ERR("The pid \"%s\" is invalid.", argv[i]);
+                               return 1;
+                       }
+                       opts->pids[pididx++] = (pid_t) tmp;
                }
                opts->pids[pididx] = -1;
        }
@@ -155,8 +164,6 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
 int main(int argc, char *argv[])
 {
        pid_t *pidit;
-       //char *msg = argv[2];
-       struct ustcomm_connection conn;
        int result;
        struct ust_opts opts;
 
@@ -170,6 +177,7 @@ int main(int argc, char *argv[])
 
        result = parse_opts_long(argc, argv, &opts);
        if(result) {
+               fprintf(stderr, "\n");
                usage();
                exit(EXIT_FAILURE);
        }
@@ -184,9 +192,8 @@ int main(int argc, char *argv[])
                usage();
                exit(EXIT_FAILURE);
        }
-
        if (opts.cmd == GET_ONLINE_PIDS) {
-               pid_tpp = ustcmd_get_online_pids();
+               pid_t *pp = ustcmd_get_online_pids();
                unsigned int i = 0;
 
                if (pp) {
@@ -196,123 +203,95 @@ int main(int argc, char *argv[])
                        }
                        free(pp);
                }
-               
+
                exit(EXIT_SUCCESS);
        }
 
        pidit = opts.pids;
-       struct USTcmd_cmsf* cmsf = NULL;
-       
+       struct marker_status *cmsf = NULL;
+
        while(*pidit != -1) {
                switch (opts.cmd) {
                        case START_TRACE:
-                       if (ustcmd_start_trace(*pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to for trace "
-                                       "with PID %u\n", (unsigned int) *pidit);
+                               result = ustcmd_start_trace(*pidit);
+                               if (result) {
+                                       ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit);
+                                       break;
+                               }
+                               //printf("sucessfully started trace for PID %u\n", (unsigned int) *pidit);
                                break;
-                       }
-                       printf("sucessfully started trace for PID %u\n",
-                               (unsigned int) *pidit);
-                       break;
-                       
+
                        case STOP_TRACE:
-                       if (ustcmd_stop_trace(*pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to stop trace "
-                                       "for PID %u\n", (unsigned int) *pidit);
+                               result = ustcmd_stop_trace(*pidit);
+                               if (result) {
+                                       ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit);
+                                       break;
+                               }
+                               //printf("sucessfully stopped trace for PID %u\n", (unsigned int) *pidit);
                                break;
-                       }
-                       printf("sucessfully stopped trace for PID %u\n",
-                               (unsigned int) *pidit);
-                       break;
-                       
+
                        case START:
-                       if (ustcmd_setup_and_start(*pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to setup/start "
-                                       "trace for PID %u\n",
-                                       (unsigned int) *pidit);
+                               result = ustcmd_setup_and_start(*pidit);
+                               if (result) {
+                                       ERR("error while trying to setup/start trace for PID %u\n", (unsigned int) *pidit);
+                                       break;
+                               }
+                               //printf("sucessfully setup/started trace for PID %u\n", (unsigned int) *pidit);
                                break;
-                       }
-                       printf("sucessfully setup/started trace for PID %u\n",
-                               (unsigned int) *pidit);
-                       break;
-                       
+
                        case DESTROY:
-                       if (ustcmd_destroy_trace(*pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to destroy "
-                                       "trace with PID %u\n",
-                                       (unsigned int) *pidit);
+                               result = ustcmd_destroy_trace(*pidit);
+                               if (result) {
+                                       ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit);
+                                       break;
+                               }
+                               //printf("sucessfully destroyed trace for PID %u\n", (unsigned int) *pidit);
                                break;
-                       }
-                       printf("sucessfully destroyed trace for PID %u\n",
-                               (unsigned int) *pidit);
-                       break;
-                       
+
                        case LIST_MARKERS:
-                       cmsf = NULL;
-                       if (ustcmd_get_cmsf(&cmsf, *pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to list markers for"
-                                       " PID %u\n", (unsigned int) *pidit);
+                               cmsf = NULL;
+                               if (ustcmd_get_cmsf(&cmsf, *pidit)) {
+                                       fprintf(stderr,
+                                               "error while trying to list markers for"
+                                               " PID %u\n", (unsigned int) *pidit);
+                                       break;
+                               }
+                               unsigned int i = 0;
+                               while (cmsf[i].channel != NULL) {
+                                       printf("{PID: %u, channel/marker: %s/%s, "
+                                               "state: %u, fmt: %s}\n",
+                                               (unsigned int) *pidit,
+                                               cmsf[i].channel,
+                                               cmsf[i].marker,
+                                               cmsf[i].state,
+                                               cmsf[i].fs);
+                                       ++i;
+                               }
+                               ustcmd_free_cmsf(cmsf);
                                break;
-                       }
-                       unsigned int i = 0;
-                       while (cmsf[i].channel != NULL) {
-                               printf("{PID: %u, channel/marker: %s/%s, "
-                                       "state: %u, fs: %s}\n",
-                                       (unsigned int) *pidit,
-                                       cmsf[i].channel,
-                                       cmsf[i].marker,
-                                       cmsf[i].state,
-                                       cmsf[i].fs);
-                               ++i;
-                       }
-                       ustcmd_free_cmsf(cmsf);
-                       break;
-                       
+
                        case ENABLE_MARKER:
-                       if (ustcmd_set_marker_state(opts.m_name, USTCMD_MS_ON,
-                               *pidit)) {
-                               
-                               fprintf(stderr,
-                                       "error while trying to enable marker"
-                                       "\"%s\" for PID %u\n",
-                                       opts.m_name,
-                                       (unsigned int) *pidit);
+                               if(opts.regex)
+                                       ustcmd_set_marker_state(opts.regex, 1, *pidit);
                                break;
-                       }
-                       printf("sucessfully enabled marker \"%s\" for PID %u\n",
-                                       opts.m_name, (unsigned int) *pidit);
-                       break;
-                       
                        case DISABLE_MARKER:
-                       if (ustcmd_set_marker_state(opts.m_name, USTCMD_MS_OFF,
-                               *pidit)) {
-                               fprintf(stderr,
-                                       "error while trying to disable marker"
-                                       "\"%s\" for PID %u\n",
-                                       opts.m_name,
-                                       (unsigned int) *pidit);
+                               if(opts.regex)
+                                       ustcmd_set_marker_state(opts.regex, 0, *pidit);
                                break;
-                       }
-                       printf("sucessfully disabled marker \"%s\" for PID %u\n",
-                                       opts.m_name, (unsigned int) *pidit);
-                       break;
-                       
+
                        default:
-                       fprintf(stderr, "error: unknown command...\n");
+                               ERR("unknown command\n");
                        break;
                }
-               
+
                pidit++;
        }
 
-       free(opts.pids);
-       if (opts.m_name != NULL) {
-               free(opts.m_name);
+       if (opts.pids != NULL) {
+               free(opts.pids);
+       }
+       if (opts.regex != NULL) {
+               free(opts.regex);
        }
 
        return 0;
This page took 0.034898 seconds and 4 git commands to generate.