6 #include <sys/socket.h>
16 #include "relay.h" /* FIXME: remove */
20 #define USTSIGNAL SIGIO
22 #define MAX_MSG_SIZE (100)
24 #define MSG_REGISTER_NOTIF 2
26 char consumer_stack
[10000];
28 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
30 static struct ustcomm_app ustcomm_app
;
32 struct tracecmd
{ /* no padding */
37 //struct listener_arg {
42 /* size: the size of all the fields except size itself */
45 /* Only the necessary part of the payload is transferred. It
46 * may even be none of it.
51 struct consumer_channel
{
53 struct ltt_channel_struct
*chan
;
56 struct blocked_consumer
{
61 /* args to ustcomm_send_reply */
62 struct ustcomm_server server
;
63 struct ustcomm_source src
;
65 /* args to ltt_do_get_subbuf */
66 struct rchan_buf
*rbuf
;
67 struct ltt_channel_buf_struct
*lttbuf
;
69 struct list_head list
;
72 static void print_markers(FILE *fp
)
74 struct marker_iter iter
;
77 marker_iter_reset(&iter
);
78 marker_iter_start(&iter
);
81 fprintf(fp
, "marker: %s_%s %d \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
);
82 marker_iter_next(&iter
);
87 void do_command(struct tracecmd
*cmd
)
91 void receive_commands()
101 /* FIXME: fd_notif should probably be protected by a spinlock */
106 msg
.type
= MSG_NOTIF
;
107 msg
.size
= sizeof(msg
.type
);
109 /* FIXME: don't block here */
110 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
117 static void inform_consumer_daemon(void)
119 ustcomm_request_consumer(getpid(), "metadata");
120 ustcomm_request_consumer(getpid(), "ust");
123 void process_blocked_consumers(void)
127 struct blocked_consumer
*bc
;
132 list_for_each_entry(bc
, &blocked_consumers
, list
) {
136 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
138 ERR("malloc returned NULL");
142 list_for_each_entry(bc
, &blocked_consumers
, list
) {
143 fds
[idx
].fd
= bc
->fd_producer
;
144 fds
[idx
].events
= POLLIN
;
145 bc
->tmp_poll_idx
= idx
;
149 result
= poll(fds
, n_fds
, 0);
155 list_for_each_entry(bc
, &blocked_consumers
, list
) {
156 if(fds
[bc
->tmp_poll_idx
].revents
) {
157 long consumed_old
= 0;
160 result
= read(bc
->fd_producer
, &inbuf
, 1);
168 close(bc
->fd_producer
);
172 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
174 ERR("ustcomm_send_reply failed");
181 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
182 if(result
== -EAGAIN
) {
183 WARN("missed buffer?");
186 else if(result
< 0) {
187 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
189 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
190 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
192 ERR("ustcomm_send_reply failed");
204 void *listener_main(void *p
)
212 struct sockaddr_un addr
;
213 socklen_t addrlen
= sizeof(addr
);
214 char trace_name
[] = "auto";
215 char trace_type
[] = "ustrelay";
218 struct ustcomm_source src
;
220 process_blocked_consumers();
222 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
224 WARN("error in ustcomm_app_recv_message");
227 else if(result
== 0) {
232 DBG("received a message! it's: %s\n", recvbuf
);
233 len
= strlen(recvbuf
);
235 if(!strcmp(recvbuf
, "print_markers")) {
236 print_markers(stderr
);
238 else if(!strcmp(recvbuf
, "list_markers")) {
243 fp
= open_memstream(&ptr
, &size
);
247 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
251 else if(!strcmp(recvbuf
, "start")) {
252 /* start is an operation that setups the trace, allocates it and starts it */
253 result
= ltt_trace_setup(trace_name
);
255 ERR("ltt_trace_setup failed");
259 result
= ltt_trace_set_type(trace_name
, trace_type
);
261 ERR("ltt_trace_set_type failed");
265 result
= ltt_trace_alloc(trace_name
);
267 ERR("ltt_trace_alloc failed");
271 inform_consumer_daemon();
273 result
= ltt_trace_start(trace_name
);
275 ERR("ltt_trace_start failed");
279 else if(!strcmp(recvbuf
, "trace_setup")) {
282 result
= ltt_trace_setup(trace_name
);
284 ERR("ltt_trace_setup failed");
288 result
= ltt_trace_set_type(trace_name
, trace_type
);
290 ERR("ltt_trace_set_type failed");
294 else if(!strcmp(recvbuf
, "trace_alloc")) {
297 result
= ltt_trace_alloc(trace_name
);
299 ERR("ltt_trace_alloc failed");
303 else if(!strcmp(recvbuf
, "trace_start")) {
306 result
= ltt_trace_start(trace_name
);
308 ERR("ltt_trace_start failed");
312 else if(!strcmp(recvbuf
, "trace_stop")) {
315 result
= ltt_trace_stop(trace_name
);
317 ERR("ltt_trace_stop failed");
321 else if(!strcmp(recvbuf
, "trace_destroy")) {
323 DBG("trace destroy");
325 result
= ltt_trace_destroy(trace_name
);
327 ERR("ltt_trace_destroy failed");
331 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
332 struct ltt_trace_struct
*trace
;
333 char trace_name
[] = "auto";
339 channel_name
= nth_token(recvbuf
, 1);
340 if(channel_name
== NULL
) {
341 ERR("get_shmid: cannot parse channel");
346 trace
= _ltt_trace_find(trace_name
);
350 CPRINTF("cannot find trace!");
354 for(i
=0; i
<trace
->nr_channels
; i
++) {
355 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
356 struct rchan_buf
*rbuf
= rchan
->buf
;
357 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
358 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
360 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
363 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
364 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
365 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
367 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
369 ERR("listener: get_shmid: ustcomm_send_reply failed");
379 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
380 struct ltt_trace_struct
*trace
;
381 char trace_name
[] = "auto";
385 DBG("get_n_subbufs");
387 channel_name
= nth_token(recvbuf
, 1);
388 if(channel_name
== NULL
) {
389 ERR("get_n_subbufs: cannot parse channel");
394 trace
= _ltt_trace_find(trace_name
);
398 CPRINTF("cannot find trace!");
402 for(i
=0; i
<trace
->nr_channels
; i
++) {
403 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
405 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
408 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
409 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
411 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
413 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
423 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
424 struct ltt_trace_struct
*trace
;
425 char trace_name
[] = "auto";
429 DBG("get_subbuf_size");
431 channel_name
= nth_token(recvbuf
, 1);
432 if(channel_name
== NULL
) {
433 ERR("get_subbuf_size: cannot parse channel");
438 trace
= _ltt_trace_find(trace_name
);
442 CPRINTF("cannot find trace!");
446 for(i
=0; i
<trace
->nr_channels
; i
++) {
447 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
449 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
452 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
453 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
455 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
457 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
467 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
470 libfile
= nth_token(recvbuf
, 1);
472 DBG("load_probe_lib loading %s", libfile
);
474 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
475 struct ltt_trace_struct
*trace
;
476 char trace_name
[] = "auto";
482 channel_name
= nth_token(recvbuf
, 1);
483 if(channel_name
== NULL
) {
484 ERR("get_subbuf: cannot parse channel");
489 trace
= _ltt_trace_find(trace_name
);
493 CPRINTF("cannot find trace!");
497 for(i
=0; i
<trace
->nr_channels
; i
++) {
498 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
500 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
501 struct rchan_buf
*rbuf
= rchan
->buf
;
502 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
506 struct blocked_consumer
*bc
;
508 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
510 ERR("malloc returned NULL");
513 bc
->fd_consumer
= src
.fd
;
514 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
518 bc
->server
= ustcomm_app
.server
;
520 list_add(&bc
->list
, &blocked_consumers
);
526 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
527 struct ltt_trace_struct
*trace
;
528 char trace_name
[] = "auto";
532 char *consumed_old_str
;
537 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
538 if(channel_name
== NULL
) {
539 ERR("put_subbuf_size: cannot parse channel");
543 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
544 if(consumed_old_str
== NULL
) {
545 ERR("put_subbuf: cannot parse consumed_old");
548 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
549 if(*endptr
!= '\0') {
550 ERR("put_subbuf: invalid value for consumed_old");
555 trace
= _ltt_trace_find(trace_name
);
559 CPRINTF("cannot find trace!");
563 for(i
=0; i
<trace
->nr_channels
; i
++) {
564 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
566 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
567 struct rchan_buf
*rbuf
= rchan
->buf
;
568 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
572 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
574 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
575 asprintf(&reply
, "%s", "ERROR");
578 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
579 asprintf(&reply
, "%s", "OK");
582 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
584 ERR("listener: put_subbuf: ustcomm_send_reply failed");
595 free(consumed_old_str
);
597 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
598 char *channel_slash_name
= nth_token(recvbuf
, 1);
599 char channel_name
[256]="";
600 char marker_name
[256]="";
601 struct marker_iter iter
;
603 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
605 if(channel_name
== NULL
|| marker_name
== NULL
) {
606 WARN("invalid marker name");
609 printf("%s %s\n", channel_name
, marker_name
);
611 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
613 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
616 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
617 char *channel_slash_name
= nth_token(recvbuf
, 1);
620 struct marker_iter iter
;
622 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
624 if(marker_name
== NULL
) {
626 printf("%s %s\n", channel_name
, marker_name
);
628 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
630 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
633 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
634 // struct ltt_trace_struct *trace;
635 // char trace_name[] = "auto";
637 // char *channel_name;
639 // DBG("get_notifications");
641 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
642 // if(channel_name == NULL) {
643 // ERR("put_subbuf_size: cannot parse channel");
647 // ltt_lock_traces();
648 // trace = _ltt_trace_find(trace_name);
649 // ltt_unlock_traces();
651 // if(trace == NULL) {
652 // CPRINTF("cannot find trace!");
656 // for(i=0; i<trace->nr_channels; i++) {
657 // struct rchan *rchan = trace->channels[i].trans_channel_data;
660 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
661 // struct rchan_buf *rbuf = rchan->buf;
662 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
664 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
665 // if(result == -1) {
666 // ERR("ustcomm_app_detach_client failed");
670 // lttbuf->wake_consumer_arg = (void *) fd;
674 // lttbuf->call_wake_consumer = 1;
680 // free(channel_name);
683 ERR("unable to parse message: %s", recvbuf
);
691 static char listener_stack
[16384];
693 void create_listener(void)
696 static char listener_stack
[16384];
697 //char *listener_stack = malloc(16384);
700 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
707 pthread_create(&thread
, NULL
, listener_main
, NULL
);
711 /* The signal handler itself. Signals must be setup so there cannot be
714 void sighandler(int sig
)
716 static char have_listener
= 0;
725 /* Called by the app signal handler to chain it to us. */
727 void chain_signal(void)
729 sighandler(USTSIGNAL
);
732 static int init_socket(void)
734 return ustcomm_init_app(getpid(), &ustcomm_app
);
737 static void destroy_socket(void)
741 // if(mysocketfile[0] == '\0')
744 // result = unlink(mysocketfile);
745 // if(result == -1) {
750 static int init_signal_handler(void)
752 /* Attempt to handler SIGIO. If the main program wants to
753 * handle it, fine, it'll override us. They it'll have to
754 * use the chaining function.
758 struct sigaction act
;
760 result
= sigemptyset(&act
.sa_mask
);
762 PERROR("sigemptyset");
766 act
.sa_handler
= sighandler
;
767 act
.sa_flags
= SA_RESTART
;
769 /* Only defer ourselves. Also, try to restart interrupted
770 * syscalls to disturb the traced program as little as possible.
772 result
= sigaction(SIGIO
, &act
, NULL
);
781 static void auto_probe_connect(struct marker
*m
)
785 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
787 ERR("ltt_marker_connect");
789 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
792 static void __attribute__((constructor(101))) init0()
794 DBG("UST_AUTOPROBE constructor");
795 if(getenv("UST_AUTOPROBE")) {
796 marker_set_new_marker_cb(auto_probe_connect
);
800 static void fini(void);
802 static void __attribute__((constructor(1000))) init()
806 DBG("UST_TRACE constructor");
808 /* Must create socket before signal handler to prevent races.
810 result
= init_socket();
812 ERR("init_socket error");
815 result
= init_signal_handler();
817 ERR("init_signal_handler error");
821 if(getenv("UST_TRACE")) {
822 char trace_name
[] = "auto";
823 char trace_type
[] = "ustrelay";
825 DBG("starting early tracing");
827 /* Ensure marker control is initialized */
828 init_marker_control();
830 /* Ensure relay is initialized */
831 init_ustrelay_transport();
833 /* Ensure markers are initialized */
837 ltt_channels_register("ust");
839 result
= ltt_trace_setup(trace_name
);
841 ERR("ltt_trace_setup failed");
845 result
= ltt_trace_set_type(trace_name
, trace_type
);
847 ERR("ltt_trace_set_type failed");
851 result
= ltt_trace_alloc(trace_name
);
853 ERR("ltt_trace_alloc failed");
857 result
= ltt_trace_start(trace_name
);
859 ERR("ltt_trace_start failed");
862 inform_consumer_daemon();
868 /* should decrementally destroy stuff if error */
872 /* This is only called if we terminate normally, not with an unhandled signal,
873 * so we cannot rely on it. */
875 /* This destructor probably isn't needed, because ustd can do crash recovery. */
877 static void __attribute__((destructor
)) fini()
881 /* if trace running, finish it */
883 DBG("destructor stopping traces");
885 result
= ltt_trace_stop("auto");
887 ERR("ltt_trace_stop error");
890 result
= ltt_trace_destroy("auto");
892 ERR("ltt_trace_destroy error");