1 /* Copyright (C) 2009 Pierre-Marc Fournier
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
33 #include "relay.h" /* FIXME: remove */
34 #include "marker-control.h"
38 #define USTSIGNAL SIGIO
40 #define MAX_MSG_SIZE (100)
42 #define MSG_REGISTER_NOTIF 2
44 char consumer_stack
[10000];
46 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
48 static struct ustcomm_app ustcomm_app
;
50 struct tracecmd
{ /* no padding */
55 //struct listener_arg {
60 /* size: the size of all the fields except size itself */
63 /* Only the necessary part of the payload is transferred. It
64 * may even be none of it.
69 struct consumer_channel
{
71 struct ltt_channel_struct
*chan
;
74 struct blocked_consumer
{
79 /* args to ustcomm_send_reply */
80 struct ustcomm_server server
;
81 struct ustcomm_source src
;
83 /* args to ltt_do_get_subbuf */
84 struct rchan_buf
*rbuf
;
85 struct ltt_channel_buf_struct
*lttbuf
;
87 struct list_head list
;
90 static void print_markers(FILE *fp
)
92 struct marker_iter iter
;
95 marker_iter_reset(&iter
);
96 marker_iter_start(&iter
);
99 fprintf(fp
, "marker: %s_%s %d \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
);
100 marker_iter_next(&iter
);
105 void do_command(struct tracecmd
*cmd
)
109 void receive_commands()
117 struct trctl_msg msg
;
119 /* FIXME: fd_notif should probably be protected by a spinlock */
124 msg
.type
= MSG_NOTIF
;
125 msg
.size
= sizeof(msg
.type
);
127 /* FIXME: don't block here */
128 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
135 static void inform_consumer_daemon(void)
137 ustcomm_request_consumer(getpid(), "metadata");
138 ustcomm_request_consumer(getpid(), "ust");
141 void process_blocked_consumers(void)
145 struct blocked_consumer
*bc
;
150 list_for_each_entry(bc
, &blocked_consumers
, list
) {
154 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
156 ERR("malloc returned NULL");
160 list_for_each_entry(bc
, &blocked_consumers
, list
) {
161 fds
[idx
].fd
= bc
->fd_producer
;
162 fds
[idx
].events
= POLLIN
;
163 bc
->tmp_poll_idx
= idx
;
167 result
= poll(fds
, n_fds
, 0);
173 list_for_each_entry(bc
, &blocked_consumers
, list
) {
174 if(fds
[bc
->tmp_poll_idx
].revents
) {
175 long consumed_old
= 0;
178 result
= read(bc
->fd_producer
, &inbuf
, 1);
186 close(bc
->fd_producer
);
190 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
192 ERR("ustcomm_send_reply failed");
199 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
200 if(result
== -EAGAIN
) {
201 WARN("missed buffer?");
204 else if(result
< 0) {
205 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
207 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
208 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
210 ERR("ustcomm_send_reply failed");
222 void *listener_main(void *p
)
229 char trace_name
[] = "auto";
230 char trace_type
[] = "ustrelay";
233 struct ustcomm_source src
;
235 process_blocked_consumers();
237 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
239 WARN("error in ustcomm_app_recv_message");
242 else if(result
== 0) {
247 DBG("received a message! it's: %s\n", recvbuf
);
248 len
= strlen(recvbuf
);
250 if(!strcmp(recvbuf
, "print_markers")) {
251 print_markers(stderr
);
253 else if(!strcmp(recvbuf
, "list_markers")) {
258 fp
= open_memstream(&ptr
, &size
);
262 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
266 else if(!strcmp(recvbuf
, "start")) {
267 /* start is an operation that setups the trace, allocates it and starts it */
268 result
= ltt_trace_setup(trace_name
);
270 ERR("ltt_trace_setup failed");
274 result
= ltt_trace_set_type(trace_name
, trace_type
);
276 ERR("ltt_trace_set_type failed");
280 result
= ltt_trace_alloc(trace_name
);
282 ERR("ltt_trace_alloc failed");
286 inform_consumer_daemon();
288 result
= ltt_trace_start(trace_name
);
290 ERR("ltt_trace_start failed");
294 else if(!strcmp(recvbuf
, "trace_setup")) {
297 result
= ltt_trace_setup(trace_name
);
299 ERR("ltt_trace_setup failed");
303 result
= ltt_trace_set_type(trace_name
, trace_type
);
305 ERR("ltt_trace_set_type failed");
309 else if(!strcmp(recvbuf
, "trace_alloc")) {
312 result
= ltt_trace_alloc(trace_name
);
314 ERR("ltt_trace_alloc failed");
318 else if(!strcmp(recvbuf
, "trace_start")) {
321 result
= ltt_trace_start(trace_name
);
323 ERR("ltt_trace_start failed");
327 else if(!strcmp(recvbuf
, "trace_stop")) {
330 result
= ltt_trace_stop(trace_name
);
332 ERR("ltt_trace_stop failed");
336 else if(!strcmp(recvbuf
, "trace_destroy")) {
338 DBG("trace destroy");
340 result
= ltt_trace_destroy(trace_name
);
342 ERR("ltt_trace_destroy failed");
346 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
347 struct ltt_trace_struct
*trace
;
348 char trace_name
[] = "auto";
354 channel_name
= nth_token(recvbuf
, 1);
355 if(channel_name
== NULL
) {
356 ERR("get_shmid: cannot parse channel");
361 trace
= _ltt_trace_find(trace_name
);
365 ERR("cannot find trace!");
369 for(i
=0; i
<trace
->nr_channels
; i
++) {
370 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
371 struct rchan_buf
*rbuf
= rchan
->buf
;
372 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
374 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
377 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
378 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
379 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
381 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
383 ERR("listener: get_shmid: ustcomm_send_reply failed");
393 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
394 struct ltt_trace_struct
*trace
;
395 char trace_name
[] = "auto";
399 DBG("get_n_subbufs");
401 channel_name
= nth_token(recvbuf
, 1);
402 if(channel_name
== NULL
) {
403 ERR("get_n_subbufs: cannot parse channel");
408 trace
= _ltt_trace_find(trace_name
);
412 ERR("cannot find trace!");
416 for(i
=0; i
<trace
->nr_channels
; i
++) {
417 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
419 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
422 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
423 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
425 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
427 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
437 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
438 struct ltt_trace_struct
*trace
;
439 char trace_name
[] = "auto";
443 DBG("get_subbuf_size");
445 channel_name
= nth_token(recvbuf
, 1);
446 if(channel_name
== NULL
) {
447 ERR("get_subbuf_size: cannot parse channel");
452 trace
= _ltt_trace_find(trace_name
);
456 ERR("cannot find trace!");
460 for(i
=0; i
<trace
->nr_channels
; i
++) {
461 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
463 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
466 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
467 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
469 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
471 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
481 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
484 libfile
= nth_token(recvbuf
, 1);
486 DBG("load_probe_lib loading %s", libfile
);
488 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
489 struct ltt_trace_struct
*trace
;
490 char trace_name
[] = "auto";
496 channel_name
= nth_token(recvbuf
, 1);
497 if(channel_name
== NULL
) {
498 ERR("get_subbuf: cannot parse channel");
503 trace
= _ltt_trace_find(trace_name
);
507 ERR("cannot find trace!");
511 for(i
=0; i
<trace
->nr_channels
; i
++) {
512 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
514 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
515 struct rchan_buf
*rbuf
= rchan
->buf
;
516 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
517 struct blocked_consumer
*bc
;
519 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
521 ERR("malloc returned NULL");
524 bc
->fd_consumer
= src
.fd
;
525 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
529 bc
->server
= ustcomm_app
.server
;
531 list_add(&bc
->list
, &blocked_consumers
);
537 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
538 struct ltt_trace_struct
*trace
;
539 char trace_name
[] = "auto";
543 char *consumed_old_str
;
548 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
549 if(channel_name
== NULL
) {
550 ERR("put_subbuf_size: cannot parse channel");
554 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
555 if(consumed_old_str
== NULL
) {
556 ERR("put_subbuf: cannot parse consumed_old");
559 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
560 if(*endptr
!= '\0') {
561 ERR("put_subbuf: invalid value for consumed_old");
566 trace
= _ltt_trace_find(trace_name
);
570 ERR("cannot find trace!");
574 for(i
=0; i
<trace
->nr_channels
; i
++) {
575 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
577 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
578 struct rchan_buf
*rbuf
= rchan
->buf
;
579 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
583 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
585 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
586 asprintf(&reply
, "%s", "ERROR");
589 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
590 asprintf(&reply
, "%s", "OK");
593 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
595 ERR("listener: put_subbuf: ustcomm_send_reply failed");
606 free(consumed_old_str
);
608 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
609 char *channel_slash_name
= nth_token(recvbuf
, 1);
610 char channel_name
[256]="";
611 char marker_name
[256]="";
613 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
615 if(channel_name
== NULL
|| marker_name
== NULL
) {
616 WARN("invalid marker name");
619 printf("%s %s\n", channel_name
, marker_name
);
621 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
623 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
626 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
627 char *channel_slash_name
= nth_token(recvbuf
, 1);
631 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
633 if(marker_name
== NULL
) {
635 printf("%s %s\n", channel_name
, marker_name
);
637 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
639 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
642 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
643 // struct ltt_trace_struct *trace;
644 // char trace_name[] = "auto";
646 // char *channel_name;
648 // DBG("get_notifications");
650 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
651 // if(channel_name == NULL) {
652 // ERR("put_subbuf_size: cannot parse channel");
656 // ltt_lock_traces();
657 // trace = _ltt_trace_find(trace_name);
658 // ltt_unlock_traces();
660 // if(trace == NULL) {
661 // ERR("cannot find trace!");
665 // for(i=0; i<trace->nr_channels; i++) {
666 // struct rchan *rchan = trace->channels[i].trans_channel_data;
669 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
670 // struct rchan_buf *rbuf = rchan->buf;
671 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
673 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
674 // if(result == -1) {
675 // ERR("ustcomm_app_detach_client failed");
679 // lttbuf->wake_consumer_arg = (void *) fd;
683 // lttbuf->call_wake_consumer = 1;
689 // free(channel_name);
692 ERR("unable to parse message: %s", recvbuf
);
700 void create_listener(void)
703 static char listener_stack
[16384];
707 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
714 pthread_create(&thread
, NULL
, listener_main
, NULL
);
718 /* The signal handler itself. Signals must be setup so there cannot be
721 void sighandler(int sig
)
723 static char have_listener
= 0;
732 /* Called by the app signal handler to chain it to us. */
734 void chain_signal(void)
736 sighandler(USTSIGNAL
);
739 static int init_socket(void)
741 return ustcomm_init_app(getpid(), &ustcomm_app
);
744 /* FIXME: reenable this to delete socket file. */
747 static void destroy_socket(void)
751 if(mysocketfile
[0] == '\0')
754 result
= unlink(mysocketfile
);
761 static int init_signal_handler(void)
763 /* Attempt to handler SIGIO. If the main program wants to
764 * handle it, fine, it'll override us. They it'll have to
765 * use the chaining function.
769 struct sigaction act
;
771 result
= sigemptyset(&act
.sa_mask
);
773 PERROR("sigemptyset");
777 act
.sa_handler
= sighandler
;
778 act
.sa_flags
= SA_RESTART
;
780 /* Only defer ourselves. Also, try to restart interrupted
781 * syscalls to disturb the traced program as little as possible.
783 result
= sigaction(SIGIO
, &act
, NULL
);
792 static void auto_probe_connect(struct marker
*m
)
796 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
797 if(result
&& result
!= -EEXIST
)
798 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
800 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
803 static void __attribute__((constructor(101))) init0()
805 DBG("UST_AUTOPROBE constructor");
806 if(getenv("UST_AUTOPROBE")) {
807 marker_set_new_marker_cb(auto_probe_connect
);
811 static void __attribute__((constructor(1000))) init()
815 DBG("UST_TRACE constructor");
817 /* Must create socket before signal handler to prevent races.
819 result
= init_socket();
821 ERR("init_socket error");
824 result
= init_signal_handler();
826 ERR("init_signal_handler error");
830 if(getenv("UST_TRACE")) {
831 char trace_name
[] = "auto";
832 char trace_type
[] = "ustrelay";
834 DBG("starting early tracing");
836 /* Ensure marker control is initialized */
837 init_marker_control();
839 /* Ensure relay is initialized */
840 init_ustrelay_transport();
842 /* Ensure markers are initialized */
846 ltt_channels_register("ust");
848 result
= ltt_trace_setup(trace_name
);
850 ERR("ltt_trace_setup failed");
854 result
= ltt_trace_set_type(trace_name
, trace_type
);
856 ERR("ltt_trace_set_type failed");
860 result
= ltt_trace_alloc(trace_name
);
862 ERR("ltt_trace_alloc failed");
866 result
= ltt_trace_start(trace_name
);
868 ERR("ltt_trace_start failed");
871 inform_consumer_daemon();
877 /* should decrementally destroy stuff if error */
881 /* This is only called if we terminate normally, not with an unhandled signal,
882 * so we cannot rely on it. */
884 /* This destructor probably isn't needed, because ustd can do crash recovery. */
886 static void __attribute__((destructor
)) fini()
890 /* if trace running, finish it */
892 DBG("destructor stopping traces");
894 result
= ltt_trace_stop("auto");
896 ERR("ltt_trace_stop error");
899 result
= ltt_trace_destroy("auto");
901 ERR("ltt_trace_destroy error");