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
;
316 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
319 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
320 asprintf(&reply
, "%d", rbuf
->shmid
);
322 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
324 ERR("listener: get_shmid: ustcomm_send_reply failed");
334 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
335 struct ltt_trace_struct
*trace
;
336 char trace_name
[] = "auto";
340 DBG("get_n_subbufs");
342 channel_name
= nth_token(recvbuf
, 1);
343 if(channel_name
== NULL
) {
344 ERR("get_n_subbufs: cannot parse channel");
349 trace
= _ltt_trace_find(trace_name
);
353 CPRINTF("cannot find trace!");
357 for(i
=0; i
<trace
->nr_channels
; i
++) {
358 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
360 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
363 DBG("the n_subbufs for the requested channel is %d", rchan
->n_subbufs
);
364 asprintf(&reply
, "%d", rchan
->n_subbufs
);
366 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
368 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
378 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
379 struct ltt_trace_struct
*trace
;
380 char trace_name
[] = "auto";
384 DBG("get_subbuf_size");
386 channel_name
= nth_token(recvbuf
, 1);
387 if(channel_name
== NULL
) {
388 ERR("get_subbuf_size: cannot parse channel");
393 trace
= _ltt_trace_find(trace_name
);
397 CPRINTF("cannot find trace!");
401 for(i
=0; i
<trace
->nr_channels
; i
++) {
402 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
404 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
407 DBG("the subbuf_size for the requested channel is %d", rchan
->subbuf_size
);
408 asprintf(&reply
, "%d", rchan
->subbuf_size
);
410 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
412 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
422 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
425 libfile
= nth_token(recvbuf
, 1);
427 DBG("load_probe_lib loading %s", libfile
);
429 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
430 struct ltt_trace_struct
*trace
;
431 char trace_name
[] = "auto";
437 channel_name
= nth_token(recvbuf
, 1);
438 if(channel_name
== NULL
) {
439 ERR("get_subbuf: cannot parse channel");
444 trace
= _ltt_trace_find(trace_name
);
448 CPRINTF("cannot find trace!");
452 for(i
=0; i
<trace
->nr_channels
; i
++) {
453 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
455 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
456 struct rchan_buf
*rbuf
= rchan
->buf
;
457 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
461 struct blocked_consumer
*bc
;
463 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
465 ERR("malloc returned NULL");
468 bc
->fd_consumer
= src
.fd
;
469 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
473 bc
->server
= ustcomm_app
.server
;
475 list_add(&bc
->list
, &blocked_consumers
);
481 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
482 struct ltt_trace_struct
*trace
;
483 char trace_name
[] = "auto";
487 char *consumed_old_str
;
492 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
493 if(channel_name
== NULL
) {
494 ERR("put_subbuf_size: cannot parse channel");
498 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
499 if(consumed_old_str
== NULL
) {
500 ERR("put_subbuf: cannot parse consumed_old");
503 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
504 if(*endptr
!= '\0') {
505 ERR("put_subbuf: invalid value for consumed_old");
510 trace
= _ltt_trace_find(trace_name
);
514 CPRINTF("cannot find trace!");
518 for(i
=0; i
<trace
->nr_channels
; i
++) {
519 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
521 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
522 struct rchan_buf
*rbuf
= rchan
->buf
;
523 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
527 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
529 WARN("ltt_do_put_subbuf: error");
532 DBG("ltt_do_put_subbuf: success");
534 asprintf(&reply
, "%s", "OK", consumed_old
);
536 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
538 ERR("listener: put_subbuf: ustcomm_send_reply failed");
549 free(consumed_old_str
);
551 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
552 // struct ltt_trace_struct *trace;
553 // char trace_name[] = "auto";
555 // char *channel_name;
557 // DBG("get_notifications");
559 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
560 // if(channel_name == NULL) {
561 // ERR("put_subbuf_size: cannot parse channel");
565 // ltt_lock_traces();
566 // trace = _ltt_trace_find(trace_name);
567 // ltt_unlock_traces();
569 // if(trace == NULL) {
570 // CPRINTF("cannot find trace!");
574 // for(i=0; i<trace->nr_channels; i++) {
575 // struct rchan *rchan = trace->channels[i].trans_channel_data;
578 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
579 // struct rchan_buf *rbuf = rchan->buf;
580 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
582 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
583 // if(result == -1) {
584 // ERR("ustcomm_app_detach_client failed");
588 // lttbuf->wake_consumer_arg = (void *) fd;
592 // lttbuf->call_wake_consumer = 1;
598 // free(channel_name);
601 ERR("unable to parse message: %s", recvbuf
);
609 static char listener_stack
[16384];
611 void create_listener(void)
614 static char listener_stack
[16384];
615 //char *listener_stack = malloc(16384);
618 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
625 pthread_create(&thread
, NULL
, listener_main
, NULL
);
629 /* The signal handler itself. Signals must be setup so there cannot be
632 void sighandler(int sig
)
634 static char have_listener
= 0;
643 /* Called by the app signal handler to chain it to us. */
645 void chain_signal(void)
647 sighandler(USTSIGNAL
);
650 static int init_socket(void)
652 return ustcomm_init_app(getpid(), &ustcomm_app
);
655 static void destroy_socket(void)
659 // if(mysocketfile[0] == '\0')
662 // result = unlink(mysocketfile);
663 // if(result == -1) {
668 static int init_signal_handler(void)
670 /* Attempt to handler SIGIO. If the main program wants to
671 * handle it, fine, it'll override us. They it'll have to
672 * use the chaining function.
676 struct sigaction act
;
678 result
= sigemptyset(&act
.sa_mask
);
680 PERROR("sigemptyset");
684 act
.sa_handler
= sighandler
;
685 act
.sa_flags
= SA_RESTART
;
687 /* Only defer ourselves. Also, try to restart interrupted
688 * syscalls to disturb the traced program as little as possible.
690 result
= sigaction(SIGIO
, &act
, NULL
);
699 static void auto_probe_connect(struct marker
*m
)
703 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
705 ERR("ltt_marker_connect");
707 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
710 static void __attribute__((constructor(101))) init0()
712 DBG("UST_AUTOPROBE constructor");
713 if(getenv("UST_AUTOPROBE")) {
714 marker_set_new_marker_cb(auto_probe_connect
);
718 static void fini(void);
720 static void __attribute__((constructor(1000))) init()
724 DBG("UST_TRACE constructor");
726 /* Must create socket before signal handler to prevent races.
728 result
= init_socket();
730 ERR("init_socket error");
733 result
= init_signal_handler();
735 ERR("init_signal_handler error");
739 if(getenv("UST_TRACE")) {
740 char trace_name
[] = "auto";
741 char trace_type
[] = "ustrelay";
743 DBG("starting early tracing");
745 /* Ensure marker control is initialized */
746 init_marker_control();
748 /* Ensure relay is initialized */
749 init_ustrelay_transport();
751 /* Ensure markers are initialized */
755 ltt_channels_register("ust");
757 result
= ltt_trace_setup(trace_name
);
759 ERR("ltt_trace_setup failed");
763 result
= ltt_trace_set_type(trace_name
, trace_type
);
765 ERR("ltt_trace_set_type failed");
769 result
= ltt_trace_alloc(trace_name
);
771 ERR("ltt_trace_alloc failed");
775 result
= ltt_trace_start(trace_name
);
777 ERR("ltt_trace_start failed");
781 inform_consumer_daemon();
787 /* should decrementally destroy stuff if error */
791 /* This is only called if we terminate normally, not with an unhandled signal,
792 * so we cannot rely on it. */
794 static void __attribute__((destructor
)) fini()
798 /* if trace running, finish it */
800 DBG("destructor stopping traces");
802 result
= ltt_trace_stop("auto");
804 ERR("ltt_trace_stop error");
807 result
= ltt_trace_destroy("auto");
809 ERR("ltt_trace_destroy error");
812 /* FIXME: wait for the consumer to be done */
813 //DBG("waiting 5 sec for consume");