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>
35 #include "relay.h" /* FIXME: remove */
36 #include "marker-control.h"
40 #define USTSIGNAL SIGIO
42 #define MAX_MSG_SIZE (100)
44 #define MSG_REGISTER_NOTIF 2
46 char consumer_stack
[10000];
48 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
50 static struct ustcomm_app ustcomm_app
;
52 struct tracecmd
{ /* no padding */
57 //struct listener_arg {
62 /* size: the size of all the fields except size itself */
65 /* Only the necessary part of the payload is transferred. It
66 * may even be none of it.
71 struct consumer_channel
{
73 struct ltt_channel_struct
*chan
;
76 struct blocked_consumer
{
81 /* args to ustcomm_send_reply */
82 struct ustcomm_server server
;
83 struct ustcomm_source src
;
85 /* args to ltt_do_get_subbuf */
86 struct rchan_buf
*rbuf
;
87 struct ltt_channel_buf_struct
*lttbuf
;
89 struct list_head list
;
92 static void print_markers(FILE *fp
)
94 struct marker_iter iter
;
97 marker_iter_reset(&iter
);
98 marker_iter_start(&iter
);
101 fprintf(fp
, "marker: %s_%s %d \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
);
102 marker_iter_next(&iter
);
107 void do_command(struct tracecmd
*cmd
)
111 /* This needs to be called whenever a new thread is created. It notifies
112 * liburcu of the new thread.
115 void ust_register_thread(void)
117 rcu_register_thread();
124 struct trctl_msg msg
;
126 /* FIXME: fd_notif should probably be protected by a spinlock */
131 msg
.type
= MSG_NOTIF
;
132 msg
.size
= sizeof(msg
.type
);
134 /* FIXME: don't block here */
135 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
142 static void inform_consumer_daemon(void)
144 ustcomm_request_consumer(getpid(), "metadata");
145 ustcomm_request_consumer(getpid(), "ust");
148 void process_blocked_consumers(void)
152 struct blocked_consumer
*bc
;
157 list_for_each_entry(bc
, &blocked_consumers
, list
) {
161 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
163 ERR("malloc returned NULL");
167 list_for_each_entry(bc
, &blocked_consumers
, list
) {
168 fds
[idx
].fd
= bc
->fd_producer
;
169 fds
[idx
].events
= POLLIN
;
170 bc
->tmp_poll_idx
= idx
;
174 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
181 list_for_each_entry(bc
, &blocked_consumers
, list
) {
182 if(fds
[bc
->tmp_poll_idx
].revents
) {
183 long consumed_old
= 0;
186 result
= read(bc
->fd_producer
, &inbuf
, 1);
194 close(bc
->fd_producer
);
198 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
200 ERR("ustcomm_send_reply failed");
207 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
208 if(result
== -EAGAIN
) {
209 WARN("missed buffer?");
212 else if(result
< 0) {
213 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
215 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
216 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
218 ERR("ustcomm_send_reply failed");
230 void *listener_main(void *p
)
234 ust_register_thread();
239 char trace_name
[] = "auto";
240 char trace_type
[] = "ustrelay";
243 struct ustcomm_source src
;
245 process_blocked_consumers();
247 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
249 WARN("error in ustcomm_app_recv_message");
252 else if(result
== 0) {
257 DBG("received a message! it's: %s\n", recvbuf
);
258 len
= strlen(recvbuf
);
260 if(!strcmp(recvbuf
, "print_markers")) {
261 print_markers(stderr
);
263 else if(!strcmp(recvbuf
, "list_markers")) {
268 fp
= open_memstream(&ptr
, &size
);
272 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
276 else if(!strcmp(recvbuf
, "start")) {
277 /* start is an operation that setups the trace, allocates it and starts it */
278 result
= ltt_trace_setup(trace_name
);
280 ERR("ltt_trace_setup failed");
284 result
= ltt_trace_set_type(trace_name
, trace_type
);
286 ERR("ltt_trace_set_type failed");
290 result
= ltt_trace_alloc(trace_name
);
292 ERR("ltt_trace_alloc failed");
296 inform_consumer_daemon();
298 result
= ltt_trace_start(trace_name
);
300 ERR("ltt_trace_start failed");
304 else if(!strcmp(recvbuf
, "trace_setup")) {
307 result
= ltt_trace_setup(trace_name
);
309 ERR("ltt_trace_setup failed");
313 result
= ltt_trace_set_type(trace_name
, trace_type
);
315 ERR("ltt_trace_set_type failed");
319 else if(!strcmp(recvbuf
, "trace_alloc")) {
322 result
= ltt_trace_alloc(trace_name
);
324 ERR("ltt_trace_alloc failed");
328 else if(!strcmp(recvbuf
, "trace_start")) {
331 result
= ltt_trace_start(trace_name
);
333 ERR("ltt_trace_start failed");
337 else if(!strcmp(recvbuf
, "trace_stop")) {
340 result
= ltt_trace_stop(trace_name
);
342 ERR("ltt_trace_stop failed");
346 else if(!strcmp(recvbuf
, "trace_destroy")) {
348 DBG("trace destroy");
350 result
= ltt_trace_destroy(trace_name
);
352 ERR("ltt_trace_destroy failed");
356 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
357 struct ltt_trace_struct
*trace
;
358 char trace_name
[] = "auto";
364 channel_name
= nth_token(recvbuf
, 1);
365 if(channel_name
== NULL
) {
366 ERR("get_shmid: cannot parse channel");
371 trace
= _ltt_trace_find(trace_name
);
375 ERR("cannot find trace!");
379 for(i
=0; i
<trace
->nr_channels
; i
++) {
380 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
381 struct rchan_buf
*rbuf
= rchan
->buf
;
382 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
384 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
387 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
388 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
389 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
391 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
393 ERR("listener: get_shmid: ustcomm_send_reply failed");
403 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
404 struct ltt_trace_struct
*trace
;
405 char trace_name
[] = "auto";
409 DBG("get_n_subbufs");
411 channel_name
= nth_token(recvbuf
, 1);
412 if(channel_name
== NULL
) {
413 ERR("get_n_subbufs: cannot parse channel");
418 trace
= _ltt_trace_find(trace_name
);
422 ERR("cannot find trace!");
426 for(i
=0; i
<trace
->nr_channels
; i
++) {
427 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
429 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
432 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
433 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
435 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
437 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
447 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
448 struct ltt_trace_struct
*trace
;
449 char trace_name
[] = "auto";
453 DBG("get_subbuf_size");
455 channel_name
= nth_token(recvbuf
, 1);
456 if(channel_name
== NULL
) {
457 ERR("get_subbuf_size: cannot parse channel");
462 trace
= _ltt_trace_find(trace_name
);
466 ERR("cannot find trace!");
470 for(i
=0; i
<trace
->nr_channels
; i
++) {
471 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
473 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
476 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
477 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
479 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
481 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
491 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
494 libfile
= nth_token(recvbuf
, 1);
496 DBG("load_probe_lib loading %s", libfile
);
498 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
499 struct ltt_trace_struct
*trace
;
500 char trace_name
[] = "auto";
506 channel_name
= nth_token(recvbuf
, 1);
507 if(channel_name
== NULL
) {
508 ERR("get_subbuf: cannot parse channel");
513 trace
= _ltt_trace_find(trace_name
);
517 ERR("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
;
527 struct blocked_consumer
*bc
;
529 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
531 ERR("malloc returned NULL");
534 bc
->fd_consumer
= src
.fd
;
535 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
539 bc
->server
= ustcomm_app
.server
;
541 list_add(&bc
->list
, &blocked_consumers
);
547 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
548 struct ltt_trace_struct
*trace
;
549 char trace_name
[] = "auto";
553 char *consumed_old_str
;
558 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
559 if(channel_name
== NULL
) {
560 ERR("put_subbuf_size: cannot parse channel");
564 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
565 if(consumed_old_str
== NULL
) {
566 ERR("put_subbuf: cannot parse consumed_old");
569 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
570 if(*endptr
!= '\0') {
571 ERR("put_subbuf: invalid value for consumed_old");
576 trace
= _ltt_trace_find(trace_name
);
580 ERR("cannot find trace!");
584 for(i
=0; i
<trace
->nr_channels
; i
++) {
585 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
587 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
588 struct rchan_buf
*rbuf
= rchan
->buf
;
589 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
593 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
595 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
596 asprintf(&reply
, "%s", "ERROR");
599 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
600 asprintf(&reply
, "%s", "OK");
603 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
605 ERR("listener: put_subbuf: ustcomm_send_reply failed");
616 free(consumed_old_str
);
618 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
619 char *channel_slash_name
= nth_token(recvbuf
, 1);
620 char channel_name
[256]="";
621 char marker_name
[256]="";
623 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
625 if(channel_name
== NULL
|| marker_name
== NULL
) {
626 WARN("invalid marker name");
629 printf("%s %s\n", channel_name
, marker_name
);
631 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
633 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
636 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
637 char *channel_slash_name
= nth_token(recvbuf
, 1);
641 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
643 if(marker_name
== NULL
) {
645 printf("%s %s\n", channel_name
, marker_name
);
647 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
649 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
652 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
653 // struct ltt_trace_struct *trace;
654 // char trace_name[] = "auto";
656 // char *channel_name;
658 // DBG("get_notifications");
660 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
661 // if(channel_name == NULL) {
662 // ERR("put_subbuf_size: cannot parse channel");
666 // ltt_lock_traces();
667 // trace = _ltt_trace_find(trace_name);
668 // ltt_unlock_traces();
670 // if(trace == NULL) {
671 // ERR("cannot find trace!");
675 // for(i=0; i<trace->nr_channels; i++) {
676 // struct rchan *rchan = trace->channels[i].trans_channel_data;
679 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
680 // struct rchan_buf *rbuf = rchan->buf;
681 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
683 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
684 // if(result == -1) {
685 // ERR("ustcomm_app_detach_client failed");
689 // lttbuf->wake_consumer_arg = (void *) fd;
693 // lttbuf->call_wake_consumer = 1;
699 // free(channel_name);
702 ERR("unable to parse message: %s", recvbuf
);
710 void create_listener(void)
713 static char listener_stack
[16384];
717 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
724 pthread_create(&thread
, NULL
, listener_main
, NULL
);
728 /* The signal handler itself. Signals must be setup so there cannot be
731 void sighandler(int sig
)
733 static char have_listener
= 0;
742 /* Called by the app signal handler to chain it to us. */
744 void chain_signal(void)
746 sighandler(USTSIGNAL
);
749 static int init_socket(void)
751 return ustcomm_init_app(getpid(), &ustcomm_app
);
754 /* FIXME: reenable this to delete socket file. */
757 static void destroy_socket(void)
761 if(mysocketfile
[0] == '\0')
764 result
= unlink(mysocketfile
);
771 static int init_signal_handler(void)
773 /* Attempt to handler SIGIO. If the main program wants to
774 * handle it, fine, it'll override us. They it'll have to
775 * use the chaining function.
779 struct sigaction act
;
781 result
= sigemptyset(&act
.sa_mask
);
783 PERROR("sigemptyset");
787 act
.sa_handler
= sighandler
;
788 act
.sa_flags
= SA_RESTART
;
790 /* Only defer ourselves. Also, try to restart interrupted
791 * syscalls to disturb the traced program as little as possible.
793 result
= sigaction(SIGIO
, &act
, NULL
);
802 static void auto_probe_connect(struct marker
*m
)
806 result
= ltt_marker_connect(m
->channel
, m
->name
, "default");
807 if(result
&& result
!= -EEXIST
)
808 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
810 DBG("just auto connected marker %s %s to probe default", m
->channel
, m
->name
);
813 static void __attribute__((constructor(101))) init0()
815 /* Initialize RCU in case the constructor order is not good. */
818 /* It is important to do this before events start to be generated. */
819 ust_register_thread();
821 DBG("UST_AUTOPROBE constructor");
822 if(getenv("UST_AUTOPROBE")) {
823 marker_set_new_marker_cb(auto_probe_connect
);
827 static void __attribute__((constructor(1000))) init()
831 DBG("UST_TRACE constructor");
833 /* Must create socket before signal handler to prevent races.
835 result
= init_socket();
837 ERR("init_socket error");
840 result
= init_signal_handler();
842 ERR("init_signal_handler error");
846 if(getenv("UST_TRACE")) {
847 char trace_name
[] = "auto";
848 char trace_type
[] = "ustrelay";
850 DBG("starting early tracing");
852 /* Ensure marker control is initialized */
853 init_marker_control();
855 /* Ensure relay is initialized */
856 init_ustrelay_transport();
858 /* Ensure markers are initialized */
862 ltt_channels_register("ust");
864 result
= ltt_trace_setup(trace_name
);
866 ERR("ltt_trace_setup failed");
870 result
= ltt_trace_set_type(trace_name
, trace_type
);
872 ERR("ltt_trace_set_type failed");
876 result
= ltt_trace_alloc(trace_name
);
878 ERR("ltt_trace_alloc failed");
882 result
= ltt_trace_start(trace_name
);
884 ERR("ltt_trace_start failed");
887 inform_consumer_daemon();
893 /* should decrementally destroy stuff if error */
897 /* This is only called if we terminate normally, not with an unhandled signal,
898 * so we cannot rely on it. */
900 /* This destructor probably isn't needed, because ustd can do crash recovery. */
902 static void __attribute__((destructor
)) fini()
906 /* if trace running, finish it */
908 DBG("destructor stopping traces");
910 result
= ltt_trace_stop("auto");
912 ERR("ltt_trace_stop error");
915 result
= ltt_trace_destroy("auto");
917 ERR("ltt_trace_destroy error");