code cleanups
[ust.git] / ust / ust.c
index ab5b5360a40ab5f81cc918c7f7d16d1c741cef55..d8916653ad1722b00bfec8df45a40e1bc5c8a7d1 100644 (file)
--- a/ust/ust.c
+++ b/ust/ust.c
@@ -21,6 +21,7 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <fcntl.h>
+#include <regex.h>
 
 #include "ustcomm.h"
 #include "ustcmd.h"
@@ -35,12 +36,14 @@ enum command {
        DISABLE_MARKER,
        GET_ONLINE_PIDS,
        UNKNOWN
-};     
+};
 
 struct ust_opts {
        enum command cmd;
        pid_t *pids;
-       char* m_name;
+       char* regex;
+       regex_t preg;
+       int regex_state;
 };
 
 char *progname = NULL;
@@ -66,7 +69,8 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
        int c;
 
        opts->pids = NULL;
-       opts->m_name = NULL;
+       opts->regex = NULL;
+       opts->regex_state = -1;
 
        while (1) {
                int option_index = 0;
@@ -115,11 +119,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,7 +142,7 @@ 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));
@@ -149,13 +153,57 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
                opts->pids[pididx] = -1;
        }
 
+       if (opts->cmd == ENABLE_MARKER || opts->cmd == DISABLE_MARKER) {
+               if (opts->regex_state = regcomp(&opts->preg, opts->regex, 0)) {
+                       fprintf(stderr, "Invalid regular expression.\n");
+               }
+       }
+
        return 0;
 }
 
+static void regex_change_m_state(struct ust_opts* opts, pid_t pid) {
+       struct USTcmd_cmsf* cmsf = NULL;
+       unsigned int i = 0;
+       int e = (opts->cmd == ENABLE_MARKER);
+
+       if (opts->regex_state != 0) {
+               return;
+       }
+
+       if (ustcmd_get_cmsf(&cmsf, pid)) {
+               fprintf(stderr, "error while trying to get markers for PID "
+                       "%u\n", (unsigned int) pid);
+               return;
+       }
+       while (cmsf[i].channel != NULL) {
+               char* mc;
+               asprintf(&mc, "%s/%s", cmsf[i].channel, cmsf[i].marker);
+               if (regexec(&opts->preg, mc, 0, NULL, 0) == 0) {
+                       /* We got a match! */
+                       if (ustcmd_set_marker_state(mc,
+                       e ? USTCMD_MS_ON : USTCMD_MS_OFF, pid)) {
+                               fprintf(stderr,
+                                       "error while trying to %sable marker"
+                                       "\"%s\" for PID %u\n",
+                                       e ? "en" : "dis", mc,
+                                       (unsigned int) pid);
+                       } else {
+                               printf("sucessfully %sabled marker "
+                                       "\"%s\" for PID %u\n",
+                                       e ? "en" : "dis", mc,
+                                       (unsigned int) pid);
+                       }
+               }
+               free(mc);
+               ++i;
+       }
+       ustcmd_free_cmsf(cmsf);
+}
+
 int main(int argc, char *argv[])
 {
        pid_t *pidit;
-       //char *msg = argv[2];
        struct ustcomm_connection conn;
        int result;
        struct ust_opts opts;
@@ -184,7 +232,6 @@ int main(int argc, char *argv[])
                usage();
                exit(EXIT_FAILURE);
        }
-
        if (opts.cmd == GET_ONLINE_PIDS) {
                pid_t* pp = ustcmd_get_online_pids();
                unsigned int i = 0;
@@ -196,13 +243,13 @@ int main(int argc, char *argv[])
                        }
                        free(pp);
                }
-               
+
                exit(EXIT_SUCCESS);
        }
 
        pidit = opts.pids;
        struct USTcmd_cmsf* cmsf = NULL;
-       
+
        while(*pidit != -1) {
                switch (opts.cmd) {
                        case START_TRACE:
@@ -215,7 +262,7 @@ int main(int argc, char *argv[])
                        printf("sucessfully started trace for PID %u\n",
                                (unsigned int) *pidit);
                        break;
-                       
+
                        case STOP_TRACE:
                        if (ustcmd_stop_trace(*pidit)) {
                                fprintf(stderr,
@@ -226,7 +273,7 @@ int main(int argc, char *argv[])
                        printf("sucessfully stopped trace for PID %u\n",
                                (unsigned int) *pidit);
                        break;
-                       
+
                        case START:
                        if (ustcmd_setup_and_start(*pidit)) {
                                fprintf(stderr,
@@ -238,7 +285,7 @@ int main(int argc, char *argv[])
                        printf("sucessfully setup/started trace for PID %u\n",
                                (unsigned int) *pidit);
                        break;
-                       
+
                        case DESTROY:
                        if (ustcmd_destroy_trace(*pidit)) {
                                fprintf(stderr,
@@ -250,7 +297,7 @@ int main(int argc, char *argv[])
                        printf("sucessfully destroyed trace for PID %u\n",
                                (unsigned int) *pidit);
                        break;
-                       
+
                        case LIST_MARKERS:
                        cmsf = NULL;
                        if (ustcmd_get_cmsf(&cmsf, *pidit)) {
@@ -272,47 +319,26 @@ int main(int argc, char *argv[])
                        }
                        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);
-                               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);
-                               break;
-                       }
-                       printf("sucessfully disabled marker \"%s\" for PID %u\n",
-                                       opts.m_name, (unsigned int) *pidit);
+                               regex_change_m_state(&opts, *pidit);
                        break;
-                       
+
                        default:
                        fprintf(stderr, "error: unknown command...\n");
                        break;
                }
-               
+
                pidit++;
        }
 
-       free(opts.pids);
-       if (opts.m_name != NULL) {
-               free(opts.m_name);
+       exit_free:
+       if (opts.pids != NULL) {
+               free(opts.pids);
+       }
+       if (opts.regex != NULL) {
+               free(opts.regex);
        }
 
        return 0;
This page took 0.025188 seconds and 4 git commands to generate.