cleanups
[ust.git] / libust / tracectl.c
index 3b9d3d39fccbff0d88b839c58238ee19bc7f5962..7c65e3f82119496c6a608283c78c27ab1cc8eff4 100644 (file)
@@ -1,3 +1,21 @@
+/* Copyright (C) 2009  Pierre-Marc Fournier
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdint.h>
 #include <signal.h>
 #include <sched.h>
 #include <fcntl.h>
 #include <poll.h>
+#include <regex.h>
+
+#include <urcu.h>
 
 #include "marker.h"
 #include "tracer.h"
 #include "localerr.h"
 #include "ustcomm.h"
 #include "relay.h" /* FIXME: remove */
+#include "marker-control.h"
 
 //#define USE_CLONE
 
@@ -83,12 +105,15 @@ static void print_markers(FILE *fp)
        unlock_markers();
 }
 
-void do_command(struct tracecmd *cmd)
-{
-}
+static int init_socket(void);
+
+/* This needs to be called whenever a new thread is created. It notifies
+ * liburcu of the new thread.
+ */
 
-void receive_commands()
+void ust_register_thread(void)
 {
+       rcu_register_thread();
 }
 
 int fd_notif = -1;
@@ -113,7 +138,7 @@ void notif_cb(void)
        }
 }
 
-static int inform_consumer_daemon(void)
+static void inform_consumer_daemon(void)
 {
        ustcomm_request_consumer(getpid(), "metadata");
        ustcomm_request_consumer(getpid(), "ust");
@@ -145,10 +170,11 @@ void process_blocked_consumers(void)
                idx++;
        }
 
-       result = poll(fds, n_fds, 0);
+       while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
+               /* nothing */;
        if(result == -1) {
                PERROR("poll");
-               return -1;
+               return;
        }
 
        list_for_each_entry(bc, &blocked_consumers, list) {
@@ -200,16 +226,15 @@ void process_blocked_consumers(void)
 
 }
 
-int listener_main(void *p)
+void *listener_main(void *p)
 {
        int result;
 
+       ust_register_thread();
+
        DBG("LISTENER");
 
        for(;;) {
-               uint32_t size;
-               struct sockaddr_un addr;
-               socklen_t addrlen = sizeof(addr);
                char trace_name[] = "auto";
                char trace_type[] = "ustrelay";
                char *recvbuf;
@@ -228,7 +253,7 @@ int listener_main(void *p)
                        continue;
                }
 
-               DBG("received a message! it's: %s\n", recvbuf);
+               DBG("received a message! it's: %s", recvbuf);
                len = strlen(recvbuf);
 
                if(!strcmp(recvbuf, "print_markers")) {
@@ -252,19 +277,19 @@ int listener_main(void *p)
                        result = ltt_trace_setup(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_setup failed");
-                               return;
+                               return (void *)1;
                        }
 
                        result = ltt_trace_set_type(trace_name, trace_type);
                        if(result < 0) {
                                ERR("ltt_trace_set_type failed");
-                               return;
+                               return (void *)1;
                        }
 
                        result = ltt_trace_alloc(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_alloc failed");
-                               return;
+                               return (void *)1;
                        }
 
                        inform_consumer_daemon();
@@ -281,13 +306,13 @@ int listener_main(void *p)
                        result = ltt_trace_setup(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_setup failed");
-                               return;
+                               return (void *)1;
                        }
 
                        result = ltt_trace_set_type(trace_name, trace_type);
                        if(result < 0) {
                                ERR("ltt_trace_set_type failed");
-                               return;
+                               return (void *)1;
                        }
                }
                else if(!strcmp(recvbuf, "trace_alloc")) {
@@ -296,7 +321,7 @@ int listener_main(void *p)
                        result = ltt_trace_alloc(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_alloc failed");
-                               return;
+                               return (void *)1;
                        }
                }
                else if(!strcmp(recvbuf, "trace_start")) {
@@ -314,7 +339,7 @@ int listener_main(void *p)
                        result = ltt_trace_stop(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_stop failed");
-                               return;
+                               return (void *)1;
                        }
                }
                else if(!strcmp(recvbuf, "trace_destroy")) {
@@ -324,7 +349,7 @@ int listener_main(void *p)
                        result = ltt_trace_destroy(trace_name);
                        if(result < 0) {
                                ERR("ltt_trace_destroy failed");
-                               return;
+                               return (void *)1;
                        }
                }
                else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
@@ -346,15 +371,14 @@ int listener_main(void *p)
                        ltt_unlock_traces();
 
                        if(trace == NULL) {
-                               CPRINTF("cannot find trace!");
-                               return 1;
+                               ERR("cannot find trace!");
+                               return (void *)1;
                        }
 
                        for(i=0; i<trace->nr_channels; i++) {
                                struct rchan *rchan = trace->channels[i].trans_channel_data;
                                struct rchan_buf *rbuf = rchan->buf;
                                struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)rchan->private_data;
-                               struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
 
                                if(!strcmp(trace->channels[i].channel_name, channel_name)) {
                                        char *reply;
@@ -394,8 +418,8 @@ int listener_main(void *p)
                        ltt_unlock_traces();
 
                        if(trace == NULL) {
-                               CPRINTF("cannot find trace!");
-                               return 1;
+                               ERR("cannot find trace!");
+                               return (void *)1;
                        }
 
                        for(i=0; i<trace->nr_channels; i++) {
@@ -404,8 +428,8 @@ int listener_main(void *p)
                                if(!strcmp(trace->channels[i].channel_name, channel_name)) {
                                        char *reply;
 
-                                       DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs);
-                                       asprintf(&reply, "%d", rchan->n_subbufs);
+                                       DBG("the n_subbufs for the requested channel is %zd", rchan->n_subbufs);
+                                       asprintf(&reply, "%zd", rchan->n_subbufs);
 
                                        result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
                                        if(result) {
@@ -438,8 +462,8 @@ int listener_main(void *p)
                        ltt_unlock_traces();
 
                        if(trace == NULL) {
-                               CPRINTF("cannot find trace!");
-                               return 1;
+                               ERR("cannot find trace!");
+                               return (void *)1;
                        }
 
                        for(i=0; i<trace->nr_channels; i++) {
@@ -448,8 +472,8 @@ int listener_main(void *p)
                                if(!strcmp(trace->channels[i].channel_name, channel_name)) {
                                        char *reply;
 
-                                       DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size);
-                                       asprintf(&reply, "%d", rchan->subbuf_size);
+                                       DBG("the subbuf_size for the requested channel is %zd", rchan->subbuf_size);
+                                       asprintf(&reply, "%zd", rchan->subbuf_size);
 
                                        result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
                                        if(result) {
@@ -489,8 +513,8 @@ int listener_main(void *p)
                        ltt_unlock_traces();
 
                        if(trace == NULL) {
-                               CPRINTF("cannot find trace!");
-                               return 1;
+                               ERR("cannot find trace!");
+                               return (void *)1;
                        }
 
                        for(i=0; i<trace->nr_channels; i++) {
@@ -499,9 +523,6 @@ int listener_main(void *p)
                                if(!strcmp(trace->channels[i].channel_name, channel_name)) {
                                        struct rchan_buf *rbuf = rchan->buf;
                                        struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
-                                       char *reply;
-                                       long consumed_old=0;
-                                       int fd;
                                        struct blocked_consumer *bc;
 
                                        bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
@@ -555,8 +576,8 @@ int listener_main(void *p)
                        ltt_unlock_traces();
 
                        if(trace == NULL) {
-                               CPRINTF("cannot find trace!");
-                               return 1;
+                               ERR("cannot find trace!");
+                               return (void *)1;
                        }
 
                        for(i=0; i<trace->nr_channels; i++) {
@@ -570,12 +591,13 @@ int listener_main(void *p)
 
                                        result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
                                        if(result < 0) {
-                                               WARN("ltt_do_put_subbuf: error");
+                                               WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name);
+                                               asprintf(&reply, "%s", "ERROR");
                                        }
                                        else {
-                                               DBG("ltt_do_put_subbuf: success");
+                                               DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name);
+                                               asprintf(&reply, "%s", "OK");
                                        }
-                                       asprintf(&reply, "%s", "OK", consumed_old);
 
                                        result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
                                        if(result) {
@@ -596,7 +618,6 @@ int listener_main(void *p)
                        char *channel_slash_name = nth_token(recvbuf, 1);
                        char channel_name[256]="";
                        char marker_name[256]="";
-                       struct marker_iter iter;
 
                        result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
 
@@ -615,7 +636,6 @@ int listener_main(void *p)
                        char *channel_slash_name = nth_token(recvbuf, 1);
                        char *marker_name;
                        char *channel_name;
-                       struct marker_iter iter;
 
                        result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
 
@@ -647,8 +667,8 @@ int listener_main(void *p)
 //                     ltt_unlock_traces();
 //
 //                     if(trace == NULL) {
-//                             CPRINTF("cannot find trace!");
-//                             return 1;
+//                             ERR("cannot find trace!");
+//                             return (void *)1;
 //                     }
 //
 //                     for(i=0; i<trace->nr_channels; i++) {
@@ -686,24 +706,31 @@ int listener_main(void *p)
        }
 }
 
-static char listener_stack[16384];
+int have_listener = 0;
 
 void create_listener(void)
 {
-       int result;
+#ifdef USE_CLONE
        static char listener_stack[16384];
-       //char *listener_stack = malloc(16384);
+#else
+       pthread_t thread;
+#endif
+
+       if(have_listener)
+               return;
 
 #ifdef USE_CLONE
        result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
        if(result == -1) {
                perror("clone");
+               return;
        }
 #else
-       pthread_t thread;
 
        pthread_create(&thread, NULL, listener_main, NULL);
 #endif
+
+       have_listener = 1;
 }
 
 /* The signal handler itself. Signals must be setup so there cannot be
@@ -732,18 +759,22 @@ static int init_socket(void)
        return ustcomm_init_app(getpid(), &ustcomm_app);
 }
 
+/* FIXME: reenable this to delete socket file. */
+
+#if 0
 static void destroy_socket(void)
 {
-//     int result;
-//
-//     if(mysocketfile[0] == '\0')
-//             return;
-//
-//     result = unlink(mysocketfile);
-//     if(result == -1) {
-//             PERROR("unlink");
-//     }
+       int result;
+
+       if(mysocketfile[0] == '\0')
+               return;
+
+       result = unlink(mysocketfile);
+       if(result == -1) {
+               PERROR("unlink");
+       }
 }
+#endif
 
 static int init_signal_handler(void)
 {
@@ -776,32 +807,56 @@ static int init_signal_handler(void)
        return 0;
 }
 
+#define AUTOPROBE_DISABLED      0
+#define AUTOPROBE_ENABLE_ALL    1
+#define AUTOPROBE_ENABLE_REGEX  2
+static int autoprobe_method = AUTOPROBE_DISABLED;
+static regex_t autoprobe_regex;
+
 static void auto_probe_connect(struct marker *m)
 {
        int result;
 
-       result = ltt_marker_connect(m->channel, m->name, "default");
-       if(result)
-               ERR("ltt_marker_connect");
-
-       DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
-}
+       char* concat_name = NULL;
+       const char *probe_name = "default";
 
-static void __attribute__((constructor(101))) init0()
-{
-       DBG("UST_AUTOPROBE constructor");
-       if(getenv("UST_AUTOPROBE")) {
-               marker_set_new_marker_cb(auto_probe_connect);
+       if(autoprobe_method == AUTOPROBE_DISABLED) {
+               return;
        }
-}
+       else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
+               result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
+               if(result == -1) {
+                       ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
+                               m->channel, m->name);
+                       return;
+               }
+               if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
+                       free(concat_name);
+                       return;
+               }
+               free(concat_name);
+       }
+
+       result = ltt_marker_connect(m->channel, m->name, probe_name);
+       if(result && result != -EEXIST)
+               ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
 
-static void fini(void);
+       DBG("auto connected marker %s %s to probe default", m->channel, m->name);
+
+}
 
 static void __attribute__((constructor(1000))) init()
 {
        int result;
+       char* autoprobe_val = NULL;
 
-       DBG("UST_TRACE constructor");
+       /* Initialize RCU in case the constructor order is not good. */
+       urcu_init();
+
+       /* It is important to do this before events start to be generated. */
+       ust_register_thread();
+
+       DBG("Tracectl constructor");
 
        /* Must create socket before signal handler to prevent races.
          */
@@ -816,6 +871,53 @@ static void __attribute__((constructor(1000))) init()
                return;
        }
 
+       autoprobe_val = getenv("UST_AUTOPROBE");
+       if(autoprobe_val) {
+               struct marker_iter iter;
+
+               DBG("Autoprobe enabled.");
+
+               /* Ensure markers are initialized */
+               //init_markers();
+
+               /* Ensure marker control is initialized, for the probe */
+               init_marker_control();
+
+               /* first, set the callback that will connect the
+                * probe on new markers
+                */
+               if(autoprobe_val[0] == '/') {
+                       result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
+                       if (result) {
+                               char regexerr[150];
+
+                               regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
+                               ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
+                               /* don't crash the application just for this */
+                       }
+                       else {
+                               autoprobe_method = AUTOPROBE_ENABLE_REGEX;
+                       }
+               }
+               else {
+                       /* just enable all instrumentation */
+                       autoprobe_method = AUTOPROBE_ENABLE_ALL;
+               }
+
+               marker_set_new_marker_cb(auto_probe_connect);
+
+               /* Now, connect the probes that were already registered. */
+               marker_iter_reset(&iter);
+               marker_iter_start(&iter);
+
+               DBG("now iterating on markers already registered");
+               while(iter.marker) {
+                       DBG("now iterating on marker %s", iter.marker->name);
+                       auto_probe_connect(iter.marker);
+                       marker_iter_next(&iter);
+               }
+       }
+
        if(getenv("UST_TRACE")) {
                char trace_name[] = "auto";
                char trace_type[] = "ustrelay";
@@ -870,6 +972,8 @@ static void __attribute__((constructor(1000))) init()
 /* This is only called if we terminate normally, not with an unhandled signal,
  * so we cannot rely on it. */
 
+/* This destructor probably isn't needed, because ustd can do crash recovery. */
+#if 0
 static void __attribute__((destructor)) fini()
 {
        int result;
@@ -888,9 +992,116 @@ static void __attribute__((destructor)) fini()
                ERR("ltt_trace_destroy error");
        }
 
-       /* FIXME: wait for the consumer to be done */
-       //DBG("waiting 5 sec for consume");
-       //sleep(5);
-
        destroy_socket();
 }
+#endif
+
+#if 0
+static int trace_recording(void)
+{
+       int retval = 0;
+       struct ltt_trace_struct *trace;
+
+       ltt_lock_traces();
+
+       list_for_each_entry(trace, &ltt_traces.head, list) {
+               if(trace->active) {
+                       retval = 1;
+                       break;
+               }
+       }
+
+       ltt_unlock_traces();
+
+       return retval;
+}
+
+static int have_consumer(void)
+{
+       return !list_empty(&blocked_consumers);
+}
+
+/* This destructor keeps the process alive for a few seconds in order
+ * to leave time to ustd to consume its buffers.
+ */
+
+int restarting_sleep(int secs)
+{
+        struct timespec tv; 
+        int result; 
+        tv.tv_sec = secs; 
+        tv.tv_nsec = 0; 
+        do { 
+                result = nanosleep(&tv, &tv); 
+        } while(result == -1 && errno == EINTR); 
+
+       return result;
+}
+
+static void __attribute__((destructor)) keepalive()
+{
+//     struct ustcomm_ustd ustd;
+//     int result;
+//     sigset_t sigset;
+//
+//     result = sigemptyset(&sigset);
+//     if(result == -1) {
+//             perror("sigemptyset");
+//             return;
+//     }
+//     result = sigaddset(&sigset, SIGIO);
+//     if(result == -1) {
+//             perror("sigaddset");
+//             return;
+//     }
+//     result = sigprocmask(SIG_BLOCK, &sigset, NULL);
+//     if(result == -1) {
+//             perror("sigprocmask");
+//             return;
+//     }
+//
+//     if(trace_recording()) {
+//             if(!have_consumer()) {
+//                     /* Request listener creation. We've blocked SIGIO's in
+//                      * order to not interrupt sleep(), so we will miss the
+//                      * one sent by the daemon and therefore won't create
+//                      * the listener automatically.
+//                      */
+//                     create_listener();
+//
+                       printf("Keeping process alive for consumer daemon...\n");
+                       restarting_sleep(3);
+                       printf("Finally dying...\n");
+//             }
+//     }
+//
+//     result = sigprocmask(SIG_UNBLOCK, &sigset, NULL);
+//     if(result == -1) {
+//             perror("sigprocmask");
+//             return;
+//     }
+}
+#endif
+
+/* Notify ust that there was a fork. This needs to be called inside
+ * the new process, anytime a process whose memory is not shared with
+ * the parent is created. If this function is not called, the events
+ * of the new process will not be collected.
+ */
+
+void ust_fork(void)
+{
+       DBG("ust: forking");
+       ltt_trace_stop("auto");
+       ltt_trace_destroy("auto");
+       ltt_trace_alloc("auto");
+       ltt_trace_start("auto");
+       init_socket();
+       have_listener = 0;
+       create_listener();
+       ustcomm_request_consumer(getpid(), "metadata");
+       ustcomm_request_consumer(getpid(), "ust");
+}
+
This page took 0.031181 seconds and 4 git commands to generate.