X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=libtracectl%2Ftracectl.c;h=e926c9efdfd7eb369a8ea88bc2e349fe6b7d68dd;hb=b02e31e53039229b50d2f54ee31d68709aba1412;hp=a2521585b336f204d56cf1ef7b8489d18d242578;hpb=1c184644865c13e167b174cd593e80a2c62b2482;p=ust.git diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index a252158..e926c9e 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -9,11 +9,13 @@ #include "marker.h" #include "tracer.h" -#include "usterr.h" +#include "localerr.h" +#include "ustcomm.h" + +//#define USE_CLONE #define UNIX_PATH_MAX 108 -//#define SOCKETDIR "/var/run/ust/socks" #define SOCKETDIR "/tmp/socks" #define SOCKETDIRLEN sizeof(SOCKETDIR) #define USTSIGNAL SIGIO @@ -24,6 +26,8 @@ char consumer_stack[10000]; +static struct ustcomm_app ustcomm_app; + struct tracecmd { /* no padding */ uint32_t size; uint16_t command; @@ -43,9 +47,8 @@ struct trctl_msg { char payload[94]; }; -pid_t mypid; char mysocketfile[UNIX_PATH_MAX] = ""; -int pfd = -1; +//int pfd = -1; struct consumer_channel { int fd; @@ -160,18 +163,25 @@ int consumer(void *arg) void start_consumer(void) { +#ifdef USE_CLONE int result; result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); if(result == -1) { perror("clone"); } +#else + pthread_t thread; + + pthread_create(&thread, NULL, consumer, NULL); +#endif } static void print_markers(void) { struct marker_iter iter; + lock_markers(); marker_iter_reset(&iter); marker_iter_start(&iter); @@ -179,6 +189,7 @@ static void print_markers(void) fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format); marker_iter_next(&iter); } + unlock_markers(); } void do_command(struct tracecmd *cmd) @@ -211,112 +222,166 @@ void notif_cb(void) } } -char recvbuf[10000]; +#define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd" + +static int inform_consumer_daemon(void) +{ + ustcomm_request_consumer(getpid(), "metadata"); + ustcomm_request_consumer(getpid(), "ust"); +} int listener_main(void *p) { int result; + 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; + int len; + struct ustcomm_source src; + + result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src); + DBG("HERE"); + if(result) { + WARN("error in ustcomm_app_recv_message"); + continue; + } - for(;;) { - struct trctl_msg msg; - int len; - - result = len = recvfrom(pfd, recvbuf, sizeof(recvbuf), 0, &addr, &addrlen); - if(result == -1) { - PERROR("recvfrom"); - continue; - } + DBG("received a message! it's: %s\n", recvbuf); + len = strlen(recvbuf); + //if(len && recvbuf[len-1] == '\n') { + // recvbuf[len-1] = '\0'; + //} - if(recvbuf[len-1] == '\n') - recvbuf[len-1] = '\0'; + if(!strcmp(recvbuf, "print_markers")) { + print_markers(); + } + else if(!strcmp(recvbuf, "trace_setup")) { + DBG("trace setup"); - fprintf(stderr, "received a message! it's: %s\n", recvbuf); + result = ltt_trace_setup(trace_name); + if(result < 0) { + ERR("ltt_trace_setup failed"); + return; + } + result = ltt_trace_set_type(trace_name, trace_type); + if(result < 0) { + ERR("ltt_trace_set_type failed"); + return; + } + } + else if(!strcmp(recvbuf, "trace_alloc")) { + DBG("trace alloc"); - if(!strcmp(recvbuf, "print_markers")) { - print_markers(); + result = ltt_trace_alloc(trace_name); + if(result < 0) { + ERR("ltt_trace_alloc failed"); + return; } - else if(!strcmp(recvbuf, "trace_setup")) { - DBG("trace setup"); - - result = ltt_trace_setup(trace_name); - if(result < 0) { - ERR("ltt_trace_setup failed"); - return; - } - - result = ltt_trace_set_type(trace_name, trace_type); - if(result < 0) { - ERR("ltt_trace_set_type failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_start")) { + DBG("trace start"); + + result = ltt_trace_start(trace_name); + if(result < 0) { + ERR("ltt_trace_start failed"); + continue; } - else if(!strcmp(recvbuf, "trace_alloc")) { - DBG("trace alloc"); - - result = ltt_trace_alloc(trace_name); - if(result < 0) { - ERR("ltt_trace_alloc failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_stop")) { + DBG("trace stop"); + + result = ltt_trace_stop(trace_name); + if(result < 0) { + ERR("ltt_trace_stop failed"); + return; } - else if(!strcmp(recvbuf, "trace_start")) { - DBG("trace start"); - - result = ltt_trace_start(trace_name); - if(result < 0) { - ERR("ltt_trace_start failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_destroy")) { + + DBG("trace destroy"); + + result = ltt_trace_destroy(trace_name); + if(result < 0) { + ERR("ltt_trace_destroy failed"); + return; } - else if(!strcmp(recvbuf, "trace_stop")) { - DBG("trace stop"); - - result = ltt_trace_stop(trace_name); - if(result < 0) { - ERR("ltt_trace_stop failed"); - return; - } + } + else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) { + struct ltt_trace_struct *trace; + char trace_name[] = "auto"; + int i; + + DBG("get_shmid"); + + ltt_lock_traces(); + trace = _ltt_trace_find(trace_name); + ltt_unlock_traces(); + + if(trace == NULL) { + CPRINTF("cannot find trace!"); + return 1; } - else if(!strcmp(recvbuf, "trace_destroy")) { - DBG("trace destroy"); + for(i=0; inr_channels; i++) { + struct rchan *rchan = trace->channels[i].trans_channel_data; + struct rchan_buf *rbuf = rchan->buf; + + DBG("the shmid is %d", rbuf->shmid); - result = ltt_trace_destroy(trace_name); - if(result < 0) { - ERR("ltt_trace_destroy failed"); - return; - } } } - next_conn:; + else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { + char *libfile; + + libfile = nth_token(recvbuf, 1); + + DBG("load_probe_lib loading %s", libfile); + } + + free(recvbuf); } } +static char listener_stack[16384]; + void create_listener(void) { int result; static char listener_stack[16384]; + //char *listener_stack = malloc(16384); +#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"); } +#else + pthread_t thread; + + pthread_create(&thread, NULL, listener_main, NULL); +#endif } -/* The signal handler itself. */ +/* The signal handler itself. Signals must be setup so there cannot be + nested signals. */ void sighandler(int sig) { + static char have_listener = 0; DBG("sighandler"); - create_listener(); + + if(!have_listener) { + create_listener(); + have_listener = 1; + } } /* Called by the app signal handler to chain it to us. */ @@ -328,43 +393,7 @@ void chain_signal(void) static int init_socket(void) { - int result; - int fd; - char pidstr[6]; - int pidlen; - - struct sockaddr_un addr; - - result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); - if(result == -1) { - PERROR("socket"); - return -1; - } - - addr.sun_family = AF_UNIX; - - result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCKETDIR, mypid); - if(result >= UNIX_PATH_MAX) { - ERR("string overflow allocating socket name"); - goto close_sock; - } - //DBG("opening socket at %s", addr.sun_path); - - result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); - if(result == -1) { - PERROR("bind"); - goto close_sock; - } - - strcpy(mysocketfile, addr.sun_path); - - pfd = fd; - return 0; - - close_sock: - close(fd); - - return -1; + return ustcomm_init_app(getpid(), &ustcomm_app); } static void destroy_socket(void) @@ -438,7 +467,18 @@ static void __attribute__((constructor(1000))) init() DBG("UST_TRACE constructor"); - mypid = getpid(); + /* Must create socket before signal handler to prevent races. + */ + result = init_socket(); + if(result == -1) { + ERR("init_socket error"); + return; + } + result = init_signal_handler(); + if(result == -1) { + ERR("init_signal_handler error"); + return; + } if(getenv("UST_TRACE")) { char trace_name[] = "auto"; @@ -481,22 +521,10 @@ static void __attribute__((constructor(1000))) init() ERR("ltt_trace_start failed"); return; } - start_consumer(); + //start_consumer(); + inform_consumer_daemon(); } - /* Must create socket before signal handler to prevent races - * on pfd variable. - */ - result = init_socket(); - if(result == -1) { - ERR("init_socket error"); - return; - } - result = init_signal_handler(); - if(result == -1) { - ERR("init_signal_handler error"); - return; - } return; @@ -526,7 +554,7 @@ static void __attribute__((destructor)) fini() } /* FIXME: wait for the consumer to be done */ - sleep(3); + sleep(1); destroy_socket(); }