5 #include <sys/socket.h>
15 #include "relay.h" /* FIXME: remove */
19 #define USTSIGNAL SIGIO
21 #define MAX_MSG_SIZE (100)
23 #define MSG_REGISTER_NOTIF 2
25 char consumer_stack
[10000];
27 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
29 static struct ustcomm_app ustcomm_app
;
31 struct tracecmd
{ /* no padding */
36 //struct listener_arg {
41 /* size: the size of all the fields except size itself */
44 /* Only the necessary part of the payload is transferred. It
45 * may even be none of it.
50 struct consumer_channel
{
52 struct ltt_channel_struct
*chan
;
55 struct blocked_consumer
{
60 /* args to ustcomm_send_reply */
61 struct ustcomm_server server
;
62 struct ustcomm_source src
;
64 /* args to ltt_do_get_subbuf */
65 struct rchan_buf
*rbuf
;
66 struct ltt_channel_buf_struct
*lttbuf
;
68 struct list_head list
;
71 static void print_markers(void)
73 struct marker_iter iter
;
76 marker_iter_reset(&iter
);
77 marker_iter_start(&iter
);
80 fprintf(stderr
, "marker: %s_%s \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, iter
.marker
->format
);
81 marker_iter_next(&iter
);
86 void do_command(struct tracecmd
*cmd
)
90 void receive_commands()
100 /* FIXME: fd_notif should probably be protected by a spinlock */
105 msg
.type
= MSG_NOTIF
;
106 msg
.size
= sizeof(msg
.type
);
108 /* FIXME: don't block here */
109 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
116 static int inform_consumer_daemon(void)
118 ustcomm_request_consumer(getpid(), "metadata");
119 ustcomm_request_consumer(getpid(), "ust");
122 void process_blocked_consumers(void)
126 struct blocked_consumer
*bc
;
131 list_for_each_entry(bc
, &blocked_consumers
, list
) {
135 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
137 ERR("malloc returned NULL");
141 list_for_each_entry(bc
, &blocked_consumers
, list
) {
142 fds
[idx
].fd
= bc
->fd_producer
;
143 fds
[idx
].events
= POLLIN
;
144 bc
->tmp_poll_idx
= idx
;
148 result
= poll(fds
, n_fds
, 0);
154 list_for_each_entry(bc
, &blocked_consumers
, list
) {
155 if(fds
[bc
->tmp_poll_idx
].revents
) {
156 long consumed_old
= 0;
159 result
= read(bc
->fd_producer
, &inbuf
, 1);
167 close(bc
->fd_producer
);
169 __list_del(bc
->list
.prev
, bc
->list
.next
);
171 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
173 ERR("ustcomm_send_reply failed");
180 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
181 if(result
== -EAGAIN
) {
182 WARN("missed buffer?");
185 else if(result
< 0) {
186 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
188 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
189 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
191 ERR("ustcomm_send_reply failed");
197 __list_del(bc
->list
.prev
, bc
->list
.next
);
203 int listener_main(void *p
)
211 struct sockaddr_un addr
;
212 socklen_t addrlen
= sizeof(addr
);
213 char trace_name
[] = "auto";
214 char trace_type
[] = "ustrelay";
217 struct ustcomm_source src
;
219 process_blocked_consumers();
221 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
223 WARN("error in ustcomm_app_recv_message");
226 else if(result
== 0) {
231 DBG("received a message! it's: %s\n", recvbuf
);
232 len
= strlen(recvbuf
);
234 if(!strcmp(recvbuf
, "print_markers")) {
237 else if(!strcmp(recvbuf
, "trace_setup")) {
240 result
= ltt_trace_setup(trace_name
);
242 ERR("ltt_trace_setup failed");
246 result
= ltt_trace_set_type(trace_name
, trace_type
);
248 ERR("ltt_trace_set_type failed");
252 else if(!strcmp(recvbuf
, "trace_alloc")) {
255 result
= ltt_trace_alloc(trace_name
);
257 ERR("ltt_trace_alloc failed");
261 else if(!strcmp(recvbuf
, "trace_start")) {
264 result
= ltt_trace_start(trace_name
);
266 ERR("ltt_trace_start failed");
270 else if(!strcmp(recvbuf
, "trace_stop")) {
273 result
= ltt_trace_stop(trace_name
);
275 ERR("ltt_trace_stop failed");
279 else if(!strcmp(recvbuf
, "trace_destroy")) {
281 DBG("trace destroy");
283 result
= ltt_trace_destroy(trace_name
);
285 ERR("ltt_trace_destroy failed");
289 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
290 struct ltt_trace_struct
*trace
;
291 char trace_name
[] = "auto";
297 channel_name
= nth_token(recvbuf
, 1);
298 if(channel_name
== NULL
) {
299 ERR("get_shmid: cannot parse channel");
304 trace
= _ltt_trace_find(trace_name
);
308 CPRINTF("cannot find trace!");
312 for(i
=0; i
<trace
->nr_channels
; i
++) {
313 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
314 struct rchan_buf
*rbuf
= rchan
->buf
;
315 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
316 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
318 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
321 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
322 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
323 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
325 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
327 ERR("listener: get_shmid: ustcomm_send_reply failed");
337 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
338 struct ltt_trace_struct
*trace
;
339 char trace_name
[] = "auto";
343 DBG("get_n_subbufs");
345 channel_name
= nth_token(recvbuf
, 1);
346 if(channel_name
== NULL
) {
347 ERR("get_n_subbufs: cannot parse channel");
352 trace
= _ltt_trace_find(trace_name
);
356 CPRINTF("cannot find trace!");
360 for(i
=0; i
<trace
->nr_channels
; i
++) {
361 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
363 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
366 DBG("the n_subbufs for the requested channel is %d", rchan
->n_subbufs
);
367 asprintf(&reply
, "%d", rchan
->n_subbufs
);
369 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
371 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
381 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
382 struct ltt_trace_struct
*trace
;
383 char trace_name
[] = "auto";
387 DBG("get_subbuf_size");
389 channel_name
= nth_token(recvbuf
, 1);
390 if(channel_name
== NULL
) {
391 ERR("get_subbuf_size: cannot parse channel");
396 trace
= _ltt_trace_find(trace_name
);
400 CPRINTF("cannot find trace!");
404 for(i
=0; i
<trace
->nr_channels
; i
++) {
405 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
407 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
410 DBG("the subbuf_size for the requested channel is %d", rchan
->subbuf_size
);
411 asprintf(&reply
, "%d", rchan
->subbuf_size
);
413 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
415 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
425 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
428 libfile
= nth_token(recvbuf
, 1);
430 DBG("load_probe_lib loading %s", libfile
);
432 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
433 struct ltt_trace_struct
*trace
;
434 char trace_name
[] = "auto";
440 channel_name
= nth_token(recvbuf
, 1);
441 if(channel_name
== NULL
) {
442 ERR("get_subbuf: cannot parse channel");
447 trace
= _ltt_trace_find(trace_name
);
451 CPRINTF("cannot find trace!");
455 for(i
=0; i
<trace
->nr_channels
; i
++) {
456 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
458 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
459 struct rchan_buf
*rbuf
= rchan
->buf
;
460 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
464 struct blocked_consumer
*bc
;
466 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
468 ERR("malloc returned NULL");
471 bc
->fd_consumer
= src
.fd
;
472 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
476 bc
->server
= ustcomm_app
.server
;
478 list_add(&bc
->list
, &blocked_consumers
);
484 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
485 struct ltt_trace_struct
*trace
;
486 char trace_name
[] = "auto";
490 char *consumed_old_str
;
495 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
496 if(channel_name
== NULL
) {
497 ERR("put_subbuf_size: cannot parse channel");
501 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
502 if(consumed_old_str
== NULL
) {
503 ERR("put_subbuf: cannot parse consumed_old");
506 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
507 if(*endptr
!= '\0') {
508 ERR("put_subbuf: invalid value for consumed_old");
513 trace
= _ltt_trace_find(trace_name
);
517 CPRINTF("cannot find trace!");
521 for(i
=0; i
<trace
->nr_channels
; i
++) {
522 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
524 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
525 struct rchan_buf
*rbuf
= rchan
->buf
;
526 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
530 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
532 WARN("ltt_do_put_subbuf: error");
535 DBG("ltt_do_put_subbuf: success");
537 asprintf(&reply
, "%s", "OK", consumed_old
);
539 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
541 ERR("listener: put_subbuf: ustcomm_send_reply failed");
552 free(consumed_old_str
);
554 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
555 // struct ltt_trace_struct *trace;
556 // char trace_name[] = "auto";
558 // char *channel_name;
560 // DBG("get_notifications");
562 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
563 // if(channel_name == NULL) {
564 // ERR("put_subbuf_size: cannot parse channel");
568 // ltt_lock_traces();
569 // trace = _ltt_trace_find(trace_name);
570 // ltt_unlock_traces();
572 // if(trace == NULL) {
573 // CPRINTF("cannot find trace!");
577 // for(i=0; i<trace->nr_channels; i++) {
578 // struct rchan *rchan = trace->channels[i].trans_channel_data;
581 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
582 // struct rchan_buf *rbuf = rchan->buf;
583 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
585 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
586 // if(result == -1) {
587 // ERR("ustcomm_app_detach_client failed");
591 // lttbuf->wake_consumer_arg = (void *) fd;
595 // lttbuf->call_wake_consumer = 1;
601 // free(channel_name);
604 ERR("unable to parse message: %s", recvbuf
);
612 static char listener_stack
[16384];
614 void create_listener(void)
617 static char listener_stack
[16384];
618 //char *listener_stack = malloc(16384);
621 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
628 pthread_create(&thread
, NULL
, listener_main
, NULL
);
632 /* The signal handler itself. Signals must be setup so there cannot be
635 void sighandler(int sig
)
637 static char have_listener
= 0;
646 /* Called by the app signal handler to chain it to us. */
648 void chain_signal(void)
650 sighandler(USTSIGNAL
);
653 static int init_socket(void)
655 return ustcomm_init_app(getpid(), &ustcomm_app
);
658 static void destroy_socket(void)
662 // if(mysocketfile[0] == '\0')
665 // result = unlink(mysocketfile);
666 // if(result == -1) {
671 static int init_signal_handler(void)
673 /* Attempt to handler SIGIO. If the main program wants to
674 * handle it, fine, it'll override us. They it'll have to
675 * use the chaining function.
679 struct sigaction act
;
681 result
= sigemptyset(&act
.sa_mask
);
683 PERROR("sigemptyset");
687 act
.sa_handler
= sighandler
;
688 act
.sa_flags
= SA_RESTART
;
690 /* Only defer ourselves. Also, try to restart interrupted
691 * syscalls to disturb the traced program as little as possible.
693 result
= sigaction(SIGIO
, &act
, NULL
);
702 static void auto_probe_connect(struct marker
*m
)
706 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
708 ERR("ltt_marker_connect");
710 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
713 static void __attribute__((constructor(101))) init0()
715 DBG("UST_AUTOPROBE constructor");
716 if(getenv("UST_AUTOPROBE")) {
717 marker_set_new_marker_cb(auto_probe_connect
);
721 static void fini(void);
723 static void __attribute__((constructor(1000))) init()
727 DBG("UST_TRACE constructor");
729 /* Must create socket before signal handler to prevent races.
731 result
= init_socket();
733 ERR("init_socket error");
736 result
= init_signal_handler();
738 ERR("init_signal_handler error");
742 if(getenv("UST_TRACE")) {
743 char trace_name
[] = "auto";
744 char trace_type
[] = "ustrelay";
746 DBG("starting early tracing");
748 /* Ensure marker control is initialized */
749 init_marker_control();
751 /* Ensure relay is initialized */
752 init_ustrelay_transport();
754 /* Ensure markers are initialized */
758 ltt_channels_register("ust");
760 result
= ltt_trace_setup(trace_name
);
762 ERR("ltt_trace_setup failed");
766 result
= ltt_trace_set_type(trace_name
, trace_type
);
768 ERR("ltt_trace_set_type failed");
772 result
= ltt_trace_alloc(trace_name
);
774 ERR("ltt_trace_alloc failed");
778 result
= ltt_trace_start(trace_name
);
780 ERR("ltt_trace_start failed");
784 inform_consumer_daemon();
790 /* should decrementally destroy stuff if error */
794 /* This is only called if we terminate normally, not with an unhandled signal,
795 * so we cannot rely on it. */
797 static void __attribute__((destructor
)) fini()
801 /* if trace running, finish it */
803 DBG("destructor stopping traces");
805 result
= ltt_trace_stop("auto");
807 ERR("ltt_trace_stop error");
810 result
= ltt_trace_destroy("auto");
812 ERR("ltt_trace_destroy error");
815 /* FIXME: wait for the consumer to be done */
816 //DBG("waiting 5 sec for consume");