add regex support for marker names
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Wed, 2 Sep 2009 14:42:25 +0000 (10:42 -0400)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Wed, 2 Sep 2009 14:42:25 +0000 (10:42 -0400)
by Philippe Proulx

libust/tracectl.c
libustcmd/ustcmd.c
ust/ust.c

index 1b0af3a0fe99e43b913a4ed2b37834f2b80c4938..2d6ced05785f50ebbc61e05d8dc5842fe1dbbafb 100644 (file)
@@ -25,6 +25,7 @@
 #include <sched.h>
 #include <fcntl.h>
 #include <poll.h>
+#include <regex.h>
 
 #include <urcu.h>
 
@@ -806,15 +807,41 @@ static int init_signal_handler(void)
        return 0;
 }
 
+static regex_t preg;
+static int regex_is_ok = -1;
+
 static void auto_probe_connect(struct marker *m)
 {
        int result;
 
+       char* comp_name = NULL;
+       char* regex;
+
+       if (regex_is_ok != 0) {
+               goto end;
+       }
+
+       if (asprintf(&comp_name, "%s/%s", m->channel, m->name) == -1) {
+               ERR("auto_probe_connect: `asprintf' failed (marker %s/%s)",
+                       m->channel, m->name);
+               return;
+       }
+       if (regexec(&preg, comp_name, 0, NULL, 0) != 0) {
+               goto end; /* Not matching */
+       }
+
+//     connect:
+
        result = ltt_marker_connect(m->channel, m->name, "default");
        if(result && result != -EEXIST)
                ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
 
        DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
+
+       end:
+       if (comp_name != NULL) {
+               free(comp_name);
+       }
 }
 
 static void __attribute__((constructor(101))) init0()
@@ -825,6 +852,7 @@ static void __attribute__((constructor(101))) init0()
 static void __attribute__((constructor(1000))) init()
 {
        int result;
+       char* regex = NULL;
 
        /* Initialize RCU in case the constructor order is not good. */
        urcu_init();
@@ -847,7 +875,9 @@ static void __attribute__((constructor(1000))) init()
                return;
        }
 
-       if(getenv("UST_AUTOPROBE")) {
+       regex = getenv("UST_AUTOPROBE");
+       if(regex) {
+               char* regex = NULL;
                struct marker_iter iter;
 
                DBG("IN AUTOPROBE\n");
@@ -862,7 +892,10 @@ static void __attribute__((constructor(1000))) init()
                 * probe on new markers
                 */
                marker_set_new_marker_cb(auto_probe_connect);
-
+               regex_is_ok = regcomp(&preg, regex, 0);
+               if (regex_is_ok) {
+                       ERR("cannot parse regex %s", regex);
+               }
                /* Now, connect the probes that were already registered. */
                marker_iter_reset(&iter);
                marker_iter_start(&iter);
index 55ac02a6193996afd109269bc077d6792feffe28..9d0d116042a1a6a4d4f783834b24d2d827a3df67 100644 (file)
@@ -15,6 +15,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <unistd.h>
 #include <getopt.h>
 pid_t* ustcmd_get_online_pids(void) {
        struct dirent* dirent;
        DIR* dir;
-       char* ping_res;
+       unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
 
        dir = opendir(SOCK_DIR);
        if (!dir) {
                return NULL;
        }
 
-       unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
        pid_t* ret = (pid_t*) malloc(ret_size);
 
        while (dirent = readdir(dir)) {
@@ -80,17 +80,20 @@ pid_t* ustcmd_get_online_pids(void) {
  * @param pid  Traced process ID
  * @return     0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
  */
-int ustcmd_set_marker_state(const char* mn, int state, pid_t pid) {
+int ustcmd_set_marker_state(const char* mn, int state, pid_t pid)
+{
+       char* cmd_str [] = {"disable_marker", "enable_marker"};
+       char* cmd;
+       int result;
+
        if (mn == NULL) {
                return USTCMD_ERR_ARG;
        }
 
-       char* cmd_str [] = {"disable_marker", "enable_marker"};
-       char* cmd;
        asprintf(&cmd, "%s %s", cmd_str[state], mn);
 
-       int tres;
-       if (tres = ustcmd_shoot(cmd, pid, NULL)) {
+       result = ustcmd_shoot(cmd, pid, NULL);
+       if (result) {
                free(cmd);
                return USTCMD_ERR_GEN;
        }
@@ -214,13 +217,16 @@ int ustcmd_free_cmsf(struct USTcmd_cmsf* cmsf) {
  * @return     0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN}
  */
 int ustcmd_get_cmsf(struct USTcmd_cmsf** cmsf, const pid_t pid) {
+       char* big_str = NULL;
+       int result;
+       struct USTcmd_cmsf* tmp_cmsf = NULL;
+       unsigned int i = 0, cmsf_ind = 0;
+
        if (cmsf == NULL) {
                return USTCMD_ERR_ARG;
        }
-       char* big_str = NULL;
-       int tres;
-
-       if (tres = ustcmd_shoot("list_markers", pid, &big_str)) {
+       result = ustcmd_shoot("list_markers", pid, &big_str);
+       if (result) {
                return USTCMD_ERR_GEN;
        }
 
@@ -229,7 +235,6 @@ int ustcmd_get_cmsf(struct USTcmd_cmsf** cmsf, const pid_t pid) {
                return USTCMD_ERR_GEN;
        }
 
-       struct USTcmd_cmsf* tmp_cmsf = NULL;
        tmp_cmsf = (struct USTcmd_cmsf*) malloc(sizeof(struct USTcmd_cmsf) *
                (ustcmd_count_nl(big_str) + 1));
        if (tmp_cmsf == NULL) {
@@ -237,9 +242,9 @@ int ustcmd_get_cmsf(struct USTcmd_cmsf** cmsf, const pid_t pid) {
        }
 
        /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
-       unsigned int i = 0, cur_st, cmsf_ind = 0;
        while (big_str[i] != '\0') {
                char state;
+
                sscanf(big_str + i, "%a[^/]/%a[^ ] %c %a[^\n]",
                        &tmp_cmsf[cmsf_ind].channel,
                        &tmp_cmsf[cmsf_ind].marker,
@@ -278,7 +283,6 @@ int ustcmd_shoot(const char* cmd, const pid_t pid, char** reply) {
                return USTCMD_ERR_ARG;
        }
 
-       struct ustcomm_connection conn;
        if (ustcomm_connect_app(pid, &conn)) {
                fprintf(stderr, "ustcmd_shoot: could not connect to PID %u\n",
                        (unsigned int) pid);
index ab5b5360a40ab5f81cc918c7f7d16d1c741cef55..d572973acd540dbb1c1b00db3bdb7addfe25f3be 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;
@@ -184,7 +188,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 +199,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 +218,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 +229,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 +241,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 +253,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 +275,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.031113 seconds and 4 git commands to generate.