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 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
174 list_for_each_entry(bc
, &blocked_consumers
, list
) {
175 if(fds
[bc
->tmp_poll_idx
].revents
) {
176 long consumed_old
= 0;
179 result
= read(bc
->fd_producer
, &inbuf
, 1);
187 close(bc
->fd_producer
);
191 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
193 ERR("ustcomm_send_reply failed");
200 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
201 if(result
== -EAGAIN
) {
202 WARN("missed buffer?");
205 else if(result
< 0) {
206 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
208 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
209 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
211 ERR("ustcomm_send_reply failed");
223 void *listener_main(void *p
)
230 char trace_name
[] = "auto";
231 char trace_type
[] = "ustrelay";
234 struct ustcomm_source src
;
236 process_blocked_consumers();
238 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
240 WARN("error in ustcomm_app_recv_message");
243 else if(result
== 0) {
248 DBG("received a message! it's: %s\n", recvbuf
);
249 len
= strlen(recvbuf
);
251 if(!strcmp(recvbuf
, "print_markers")) {
252 print_markers(stderr
);
254 else if(!strcmp(recvbuf
, "list_markers")) {
259 fp
= open_memstream(&ptr
, &size
);
263 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
267 else if(!strcmp(recvbuf
, "start")) {
268 /* start is an operation that setups the trace, allocates it and starts it */
269 result
= ltt_trace_setup(trace_name
);
271 ERR("ltt_trace_setup failed");
275 result
= ltt_trace_set_type(trace_name
, trace_type
);
277 ERR("ltt_trace_set_type failed");
281 result
= ltt_trace_alloc(trace_name
);
283 ERR("ltt_trace_alloc failed");
287 inform_consumer_daemon();
289 result
= ltt_trace_start(trace_name
);
291 ERR("ltt_trace_start failed");
295 else if(!strcmp(recvbuf
, "trace_setup")) {
298 result
= ltt_trace_setup(trace_name
);
300 ERR("ltt_trace_setup failed");
304 result
= ltt_trace_set_type(trace_name
, trace_type
);
306 ERR("ltt_trace_set_type failed");
310 else if(!strcmp(recvbuf
, "trace_alloc")) {
313 result
= ltt_trace_alloc(trace_name
);
315 ERR("ltt_trace_alloc failed");
319 else if(!strcmp(recvbuf
, "trace_start")) {
322 result
= ltt_trace_start(trace_name
);
324 ERR("ltt_trace_start failed");
328 else if(!strcmp(recvbuf
, "trace_stop")) {
331 result
= ltt_trace_stop(trace_name
);
333 ERR("ltt_trace_stop failed");
337 else if(!strcmp(recvbuf
, "trace_destroy")) {
339 DBG("trace destroy");
341 result
= ltt_trace_destroy(trace_name
);
343 ERR("ltt_trace_destroy failed");
347 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
348 struct ltt_trace_struct
*trace
;
349 char trace_name
[] = "auto";
355 channel_name
= nth_token(recvbuf
, 1);
356 if(channel_name
== NULL
) {
357 ERR("get_shmid: cannot parse channel");
362 trace
= _ltt_trace_find(trace_name
);
366 ERR("cannot find trace!");
370 for(i
=0; i
<trace
->nr_channels
; i
++) {
371 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
372 struct rchan_buf
*rbuf
= rchan
->buf
;
373 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
375 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
378 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
379 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
380 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
382 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
384 ERR("listener: get_shmid: ustcomm_send_reply failed");
394 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
395 struct ltt_trace_struct
*trace
;
396 char trace_name
[] = "auto";
400 DBG("get_n_subbufs");
402 channel_name
= nth_token(recvbuf
, 1);
403 if(channel_name
== NULL
) {
404 ERR("get_n_subbufs: cannot parse channel");
409 trace
= _ltt_trace_find(trace_name
);
413 ERR("cannot find trace!");
417 for(i
=0; i
<trace
->nr_channels
; i
++) {
418 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
420 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
423 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
424 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
426 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
428 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
438 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
439 struct ltt_trace_struct
*trace
;
440 char trace_name
[] = "auto";
444 DBG("get_subbuf_size");
446 channel_name
= nth_token(recvbuf
, 1);
447 if(channel_name
== NULL
) {
448 ERR("get_subbuf_size: cannot parse channel");
453 trace
= _ltt_trace_find(trace_name
);
457 ERR("cannot find trace!");
461 for(i
=0; i
<trace
->nr_channels
; i
++) {
462 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
464 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
467 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
468 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
470 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
472 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
482 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
485 libfile
= nth_token(recvbuf
, 1);
487 DBG("load_probe_lib loading %s", libfile
);
489 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
490 struct ltt_trace_struct
*trace
;
491 char trace_name
[] = "auto";
497 channel_name
= nth_token(recvbuf
, 1);
498 if(channel_name
== NULL
) {
499 ERR("get_subbuf: cannot parse channel");
504 trace
= _ltt_trace_find(trace_name
);
508 ERR("cannot find trace!");
512 for(i
=0; i
<trace
->nr_channels
; i
++) {
513 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
515 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
516 struct rchan_buf
*rbuf
= rchan
->buf
;
517 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
518 struct blocked_consumer
*bc
;
520 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
522 ERR("malloc returned NULL");
525 bc
->fd_consumer
= src
.fd
;
526 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
530 bc
->server
= ustcomm_app
.server
;
532 list_add(&bc
->list
, &blocked_consumers
);
538 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
539 struct ltt_trace_struct
*trace
;
540 char trace_name
[] = "auto";
544 char *consumed_old_str
;
549 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
550 if(channel_name
== NULL
) {
551 ERR("put_subbuf_size: cannot parse channel");
555 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
556 if(consumed_old_str
== NULL
) {
557 ERR("put_subbuf: cannot parse consumed_old");
560 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
561 if(*endptr
!= '\0') {
562 ERR("put_subbuf: invalid value for consumed_old");
567 trace
= _ltt_trace_find(trace_name
);
571 ERR("cannot find trace!");
575 for(i
=0; i
<trace
->nr_channels
; i
++) {
576 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
;
584 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
586 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
587 asprintf(&reply
, "%s", "ERROR");
590 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
591 asprintf(&reply
, "%s", "OK");
594 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
596 ERR("listener: put_subbuf: ustcomm_send_reply failed");
607 free(consumed_old_str
);
609 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
610 char *channel_slash_name
= nth_token(recvbuf
, 1);
611 char channel_name
[256]="";
612 char marker_name
[256]="";
614 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
616 if(channel_name
== NULL
|| marker_name
== NULL
) {
617 WARN("invalid marker name");
620 printf("%s %s\n", channel_name
, marker_name
);
622 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
624 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
627 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
628 char *channel_slash_name
= nth_token(recvbuf
, 1);
632 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
634 if(marker_name
== NULL
) {
636 printf("%s %s\n", channel_name
, marker_name
);
638 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
640 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
643 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
644 // struct ltt_trace_struct *trace;
645 // char trace_name[] = "auto";
647 // char *channel_name;
649 // DBG("get_notifications");
651 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
652 // if(channel_name == NULL) {
653 // ERR("put_subbuf_size: cannot parse channel");
657 // ltt_lock_traces();
658 // trace = _ltt_trace_find(trace_name);
659 // ltt_unlock_traces();
661 // if(trace == NULL) {
662 // ERR("cannot find trace!");
666 // for(i=0; i<trace->nr_channels; i++) {
667 // struct rchan *rchan = trace->channels[i].trans_channel_data;
670 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
671 // struct rchan_buf *rbuf = rchan->buf;
672 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
674 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
675 // if(result == -1) {
676 // ERR("ustcomm_app_detach_client failed");
680 // lttbuf->wake_consumer_arg = (void *) fd;
684 // lttbuf->call_wake_consumer = 1;
690 // free(channel_name);
693 ERR("unable to parse message: %s", recvbuf
);
701 void create_listener(void)
704 static char listener_stack
[16384];
708 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
715 pthread_create(&thread
, NULL
, listener_main
, NULL
);
719 /* The signal handler itself. Signals must be setup so there cannot be
722 void sighandler(int sig
)
724 static char have_listener
= 0;
733 /* Called by the app signal handler to chain it to us. */
735 void chain_signal(void)
737 sighandler(USTSIGNAL
);
740 static int init_socket(void)
742 return ustcomm_init_app(getpid(), &ustcomm_app
);
745 /* FIXME: reenable this to delete socket file. */
748 static void destroy_socket(void)
752 if(mysocketfile
[0] == '\0')
755 result
= unlink(mysocketfile
);
762 static int init_signal_handler(void)
764 /* Attempt to handler SIGIO. If the main program wants to
765 * handle it, fine, it'll override us. They it'll have to
766 * use the chaining function.
770 struct sigaction act
;
772 result
= sigemptyset(&act
.sa_mask
);
774 PERROR("sigemptyset");
778 act
.sa_handler
= sighandler
;
779 act
.sa_flags
= SA_RESTART
;
781 /* Only defer ourselves. Also, try to restart interrupted
782 * syscalls to disturb the traced program as little as possible.
784 result
= sigaction(SIGIO
, &act
, NULL
);
793 static void auto_probe_connect(struct marker
*m
)
797 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
798 if(result
&& result
!= -EEXIST
)
799 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
801 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
804 static void __attribute__((constructor(101))) init0()
806 DBG("UST_AUTOPROBE constructor");
807 if(getenv("UST_AUTOPROBE")) {
808 marker_set_new_marker_cb(auto_probe_connect
);
812 static void __attribute__((constructor(1000))) init()
816 DBG("UST_TRACE constructor");
818 /* Must create socket before signal handler to prevent races.
820 result
= init_socket();
822 ERR("init_socket error");
825 result
= init_signal_handler();
827 ERR("init_signal_handler error");
831 if(getenv("UST_TRACE")) {
832 char trace_name
[] = "auto";
833 char trace_type
[] = "ustrelay";
835 DBG("starting early tracing");
837 /* Ensure marker control is initialized */
838 init_marker_control();
840 /* Ensure relay is initialized */
841 init_ustrelay_transport();
843 /* Ensure markers are initialized */
847 ltt_channels_register("ust");
849 result
= ltt_trace_setup(trace_name
);
851 ERR("ltt_trace_setup failed");
855 result
= ltt_trace_set_type(trace_name
, trace_type
);
857 ERR("ltt_trace_set_type failed");
861 result
= ltt_trace_alloc(trace_name
);
863 ERR("ltt_trace_alloc failed");
867 result
= ltt_trace_start(trace_name
);
869 ERR("ltt_trace_start failed");
872 inform_consumer_daemon();
878 /* should decrementally destroy stuff if error */
882 /* This is only called if we terminate normally, not with an unhandled signal,
883 * so we cannot rely on it. */
885 /* This destructor probably isn't needed, because ustd can do crash recovery. */
887 static void __attribute__((destructor
)) fini()
891 /* if trace running, finish it */
893 DBG("destructor stopping traces");
895 result
= ltt_trace_stop("auto");
897 ERR("ltt_trace_stop error");
900 result
= ltt_trace_destroy("auto");
902 ERR("ltt_trace_destroy error");