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>
36 #include "relay.h" /* FIXME: remove */
37 #include "marker-control.h"
41 #define USTSIGNAL SIGIO
43 #define MAX_MSG_SIZE (100)
45 #define MSG_REGISTER_NOTIF 2
47 char consumer_stack
[10000];
49 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
51 static struct ustcomm_app ustcomm_app
;
53 struct tracecmd
{ /* no padding */
58 /* volatile because shared between the listener and the main thread */
59 volatile sig_atomic_t buffers_to_export
= 0;
61 //struct listener_arg {
66 /* size: the size of all the fields except size itself */
69 /* Only the necessary part of the payload is transferred. It
70 * may even be none of it.
75 struct consumer_channel
{
77 struct ltt_channel_struct
*chan
;
80 struct blocked_consumer
{
85 /* args to ustcomm_send_reply */
86 struct ustcomm_server server
;
87 struct ustcomm_source src
;
89 /* args to ltt_do_get_subbuf */
90 struct rchan_buf
*rbuf
;
91 struct ltt_channel_buf_struct
*lttbuf
;
93 struct list_head list
;
96 static void print_markers(FILE *fp
)
98 struct marker_iter iter
;
101 marker_iter_reset(&iter
);
102 marker_iter_start(&iter
);
105 fprintf(fp
, "marker: %s_%s %d \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
);
106 marker_iter_next(&iter
);
111 static int init_socket(void);
113 /* This needs to be called whenever a new thread is created. It notifies
114 * liburcu of the new thread.
117 void ust_register_thread(void)
119 rcu_register_thread();
126 struct trctl_msg msg
;
128 /* FIXME: fd_notif should probably be protected by a spinlock */
133 msg
.type
= MSG_NOTIF
;
134 msg
.size
= sizeof(msg
.type
);
136 /* FIXME: don't block here */
137 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
144 /* Ask the daemon to collect a trace called trace_name and being
145 * produced by this pid.
147 * The trace must be at least allocated. (It can also be started.)
148 * This is because _ltt_trace_find is used.
151 static void inform_consumer_daemon(const char *trace_name
)
154 struct ltt_trace_struct
*trace
;
155 pid_t pid
= getpid();
160 trace
= _ltt_trace_find(trace_name
);
162 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
166 for(i
=0; i
< trace
->nr_channels
; i
++) {
167 result
= ustcomm_request_consumer(pid
, trace
->channels
[i
].channel_name
);
169 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
170 /* continue even if fail */
179 void process_blocked_consumers(void)
183 struct blocked_consumer
*bc
;
188 list_for_each_entry(bc
, &blocked_consumers
, list
) {
192 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
194 ERR("malloc returned NULL");
198 list_for_each_entry(bc
, &blocked_consumers
, list
) {
199 fds
[idx
].fd
= bc
->fd_producer
;
200 fds
[idx
].events
= POLLIN
;
201 bc
->tmp_poll_idx
= idx
;
205 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
212 list_for_each_entry(bc
, &blocked_consumers
, list
) {
213 if(fds
[bc
->tmp_poll_idx
].revents
) {
214 long consumed_old
= 0;
217 result
= read(bc
->fd_producer
, &inbuf
, 1);
225 close(bc
->fd_producer
);
229 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
231 ERR("ustcomm_send_reply failed");
238 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
239 if(result
== -EAGAIN
) {
240 WARN("missed buffer?");
243 else if(result
< 0) {
244 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
246 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
247 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
249 ERR("ustcomm_send_reply failed");
261 void *listener_main(void *p
)
265 ust_register_thread();
270 char trace_name
[] = "auto";
271 char trace_type
[] = "ustrelay";
274 struct ustcomm_source src
;
276 process_blocked_consumers();
278 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
280 WARN("error in ustcomm_app_recv_message");
283 else if(result
== 0) {
288 DBG("received a message! it's: %s", recvbuf
);
289 len
= strlen(recvbuf
);
291 if(!strcmp(recvbuf
, "print_markers")) {
292 print_markers(stderr
);
294 else if(!strcmp(recvbuf
, "list_markers")) {
299 fp
= open_memstream(&ptr
, &size
);
303 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
307 else if(!strcmp(recvbuf
, "start")) {
308 /* start is an operation that setups the trace, allocates it and starts it */
309 result
= ltt_trace_setup(trace_name
);
311 ERR("ltt_trace_setup failed");
315 result
= ltt_trace_set_type(trace_name
, trace_type
);
317 ERR("ltt_trace_set_type failed");
321 result
= ltt_trace_alloc(trace_name
);
323 ERR("ltt_trace_alloc failed");
327 inform_consumer_daemon(trace_name
);
329 result
= ltt_trace_start(trace_name
);
331 ERR("ltt_trace_start failed");
335 else if(!strcmp(recvbuf
, "trace_setup")) {
338 result
= ltt_trace_setup(trace_name
);
340 ERR("ltt_trace_setup failed");
344 result
= ltt_trace_set_type(trace_name
, trace_type
);
346 ERR("ltt_trace_set_type failed");
350 else if(!strcmp(recvbuf
, "trace_alloc")) {
353 result
= ltt_trace_alloc(trace_name
);
355 ERR("ltt_trace_alloc failed");
359 else if(!strcmp(recvbuf
, "trace_start")) {
362 result
= ltt_trace_start(trace_name
);
364 ERR("ltt_trace_start failed");
368 else if(!strcmp(recvbuf
, "trace_stop")) {
371 result
= ltt_trace_stop(trace_name
);
373 ERR("ltt_trace_stop failed");
377 else if(!strcmp(recvbuf
, "trace_destroy")) {
379 DBG("trace destroy");
381 result
= ltt_trace_destroy(trace_name
);
383 ERR("ltt_trace_destroy failed");
387 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
388 struct ltt_trace_struct
*trace
;
389 char trace_name
[] = "auto";
395 channel_name
= nth_token(recvbuf
, 1);
396 if(channel_name
== NULL
) {
397 ERR("get_shmid: cannot parse channel");
402 trace
= _ltt_trace_find(trace_name
);
406 ERR("cannot find trace!");
410 for(i
=0; i
<trace
->nr_channels
; i
++) {
411 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
412 struct rchan_buf
*rbuf
= rchan
->buf
;
413 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
415 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
418 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
419 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
420 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
422 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
424 ERR("listener: get_shmid: ustcomm_send_reply failed");
436 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
437 struct ltt_trace_struct
*trace
;
438 char trace_name
[] = "auto";
442 DBG("get_n_subbufs");
444 channel_name
= nth_token(recvbuf
, 1);
445 if(channel_name
== NULL
) {
446 ERR("get_n_subbufs: cannot parse channel");
451 trace
= _ltt_trace_find(trace_name
);
455 ERR("cannot find trace!");
459 for(i
=0; i
<trace
->nr_channels
; i
++) {
460 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
462 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
465 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
466 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
468 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
470 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
480 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
481 struct ltt_trace_struct
*trace
;
482 char trace_name
[] = "auto";
486 DBG("get_subbuf_size");
488 channel_name
= nth_token(recvbuf
, 1);
489 if(channel_name
== NULL
) {
490 ERR("get_subbuf_size: cannot parse channel");
495 trace
= _ltt_trace_find(trace_name
);
499 ERR("cannot find trace!");
503 for(i
=0; i
<trace
->nr_channels
; i
++) {
504 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
506 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
509 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
510 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
512 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
514 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
524 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
527 libfile
= nth_token(recvbuf
, 1);
529 DBG("load_probe_lib loading %s", libfile
);
531 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
532 struct ltt_trace_struct
*trace
;
533 char trace_name
[] = "auto";
539 channel_name
= nth_token(recvbuf
, 1);
540 if(channel_name
== NULL
) {
541 ERR("get_subbuf: cannot parse channel");
546 trace
= _ltt_trace_find(trace_name
);
550 ERR("cannot find trace!");
554 for(i
=0; i
<trace
->nr_channels
; i
++) {
555 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
557 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
558 struct rchan_buf
*rbuf
= rchan
->buf
;
559 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
560 struct blocked_consumer
*bc
;
562 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
564 ERR("malloc returned NULL");
567 bc
->fd_consumer
= src
.fd
;
568 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
572 bc
->server
= ustcomm_app
.server
;
574 list_add(&bc
->list
, &blocked_consumers
);
580 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
581 struct ltt_trace_struct
*trace
;
582 char trace_name
[] = "auto";
586 char *consumed_old_str
;
591 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
592 if(channel_name
== NULL
) {
593 ERR("put_subbuf_size: cannot parse channel");
597 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
598 if(consumed_old_str
== NULL
) {
599 ERR("put_subbuf: cannot parse consumed_old");
602 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
603 if(*endptr
!= '\0') {
604 ERR("put_subbuf: invalid value for consumed_old");
609 trace
= _ltt_trace_find(trace_name
);
613 ERR("cannot find trace!");
617 for(i
=0; i
<trace
->nr_channels
; i
++) {
618 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
620 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
621 struct rchan_buf
*rbuf
= rchan
->buf
;
622 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
626 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
628 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
629 asprintf(&reply
, "%s", "ERROR");
632 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
633 asprintf(&reply
, "%s", "OK");
636 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
638 ERR("listener: put_subbuf: ustcomm_send_reply failed");
649 free(consumed_old_str
);
651 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
652 char *channel_slash_name
= nth_token(recvbuf
, 1);
653 char channel_name
[256]="";
654 char marker_name
[256]="";
656 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
658 if(channel_name
== NULL
|| marker_name
== NULL
) {
659 WARN("invalid marker name");
662 printf("%s %s\n", channel_name
, marker_name
);
664 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
666 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
669 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
670 char *channel_slash_name
= nth_token(recvbuf
, 1);
674 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
676 if(marker_name
== NULL
) {
678 printf("%s %s\n", channel_name
, marker_name
);
680 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
682 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
685 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
686 // struct ltt_trace_struct *trace;
687 // char trace_name[] = "auto";
689 // char *channel_name;
691 // DBG("get_notifications");
693 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
694 // if(channel_name == NULL) {
695 // ERR("put_subbuf_size: cannot parse channel");
699 // ltt_lock_traces();
700 // trace = _ltt_trace_find(trace_name);
701 // ltt_unlock_traces();
703 // if(trace == NULL) {
704 // ERR("cannot find trace!");
708 // for(i=0; i<trace->nr_channels; i++) {
709 // struct rchan *rchan = trace->channels[i].trans_channel_data;
712 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
713 // struct rchan_buf *rbuf = rchan->buf;
714 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
716 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
717 // if(result == -1) {
718 // ERR("ustcomm_app_detach_client failed");
722 // lttbuf->wake_consumer_arg = (void *) fd;
726 // lttbuf->call_wake_consumer = 1;
732 // free(channel_name);
735 ERR("unable to parse message: %s", recvbuf
);
743 int have_listener
= 0;
745 void create_listener(void)
748 static char listener_stack
[16384];
757 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
764 pthread_create(&thread
, NULL
, listener_main
, NULL
);
770 /* The signal handler itself. Signals must be setup so there cannot be
773 void sighandler(int sig
)
775 static char have_listener
= 0;
784 /* Called by the app signal handler to chain it to us. */
786 void chain_signal(void)
788 sighandler(USTSIGNAL
);
791 static int init_socket(void)
793 return ustcomm_init_app(getpid(), &ustcomm_app
);
796 /* FIXME: reenable this to delete socket file. */
799 static void destroy_socket(void)
803 if(mysocketfile
[0] == '\0')
806 result
= unlink(mysocketfile
);
813 static int init_signal_handler(void)
815 /* Attempt to handler SIGIO. If the main program wants to
816 * handle it, fine, it'll override us. They it'll have to
817 * use the chaining function.
821 struct sigaction act
;
823 result
= sigemptyset(&act
.sa_mask
);
825 PERROR("sigemptyset");
829 act
.sa_handler
= sighandler
;
830 act
.sa_flags
= SA_RESTART
;
832 /* Only defer ourselves. Also, try to restart interrupted
833 * syscalls to disturb the traced program as little as possible.
835 result
= sigaction(SIGIO
, &act
, NULL
);
844 #define AUTOPROBE_DISABLED 0
845 #define AUTOPROBE_ENABLE_ALL 1
846 #define AUTOPROBE_ENABLE_REGEX 2
847 static int autoprobe_method
= AUTOPROBE_DISABLED
;
848 static regex_t autoprobe_regex
;
850 static void auto_probe_connect(struct marker
*m
)
854 char* concat_name
= NULL
;
855 const char *probe_name
= "default";
857 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
860 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
861 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
863 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
864 m
->channel
, m
->name
);
867 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
874 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
875 if(result
&& result
!= -EEXIST
)
876 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
878 DBG("auto connected marker %s %s to probe default", m
->channel
, m
->name
);
882 static void __attribute__((constructor(1000))) init()
885 char* autoprobe_val
= NULL
;
887 /* Initialize RCU in case the constructor order is not good. */
890 /* It is important to do this before events start to be generated. */
891 ust_register_thread();
893 DBG("Tracectl constructor");
895 /* Must create socket before signal handler to prevent races.
897 result
= init_socket();
899 ERR("init_socket error");
902 result
= init_signal_handler();
904 ERR("init_signal_handler error");
908 autoprobe_val
= getenv("UST_AUTOPROBE");
910 struct marker_iter iter
;
912 DBG("Autoprobe enabled.");
914 /* Ensure markers are initialized */
917 /* Ensure marker control is initialized, for the probe */
918 init_marker_control();
920 /* first, set the callback that will connect the
921 * probe on new markers
923 if(autoprobe_val
[0] == '/') {
924 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
928 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
929 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
930 /* don't crash the application just for this */
933 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
937 /* just enable all instrumentation */
938 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
941 marker_set_new_marker_cb(auto_probe_connect
);
943 /* Now, connect the probes that were already registered. */
944 marker_iter_reset(&iter
);
945 marker_iter_start(&iter
);
947 DBG("now iterating on markers already registered");
949 DBG("now iterating on marker %s", iter
.marker
->name
);
950 auto_probe_connect(iter
.marker
);
951 marker_iter_next(&iter
);
955 if(getenv("UST_TRACE")) {
956 char trace_name
[] = "auto";
957 char trace_type
[] = "ustrelay";
959 DBG("starting early tracing");
961 /* Ensure marker control is initialized */
962 init_marker_control();
964 /* Ensure relay is initialized */
965 init_ustrelay_transport();
967 /* Ensure markers are initialized */
971 ltt_channels_register("ust");
973 result
= ltt_trace_setup(trace_name
);
975 ERR("ltt_trace_setup failed");
979 result
= ltt_trace_set_type(trace_name
, trace_type
);
981 ERR("ltt_trace_set_type failed");
985 result
= ltt_trace_alloc(trace_name
);
987 ERR("ltt_trace_alloc failed");
991 inform_consumer_daemon(trace_name
);
993 result
= ltt_trace_start(trace_name
);
995 ERR("ltt_trace_start failed");
1003 /* should decrementally destroy stuff if error */
1007 /* This is only called if we terminate normally, not with an unhandled signal,
1008 * so we cannot rely on it. */
1010 /* This destructor probably isn't needed, because ustd can do crash recovery. */
1012 static void __attribute__((destructor
)) fini()
1016 /* if trace running, finish it */
1018 // DBG("destructor stopping traces");
1020 // result = ltt_trace_stop("auto");
1021 // if(result == -1) {
1022 // ERR("ltt_trace_stop error");
1025 // result = ltt_trace_destroy("auto");
1026 // if(result == -1) {
1027 // ERR("ltt_trace_destroy error");
1030 // destroy_socket();
1034 static int trace_recording(void)
1037 struct ltt_trace_struct
*trace
;
1041 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1048 ltt_unlock_traces();
1054 static int have_consumer(void)
1056 return !list_empty(&blocked_consumers
);
1060 int restarting_usleep(useconds_t usecs
)
1066 tv
.tv_nsec
= usecs
* 1000;
1069 result
= nanosleep(&tv
, &tv
);
1070 } while(result
== -1 && errno
== EINTR
);
1075 /* This destructor keeps the process alive for a few seconds in order
1076 * to leave time to ustd to connect to its buffers. This is necessary
1077 * for programs whose execution is very short. It is also useful in all
1078 * programs when tracing is started close to the end of the program
1081 * FIXME: For now, this only works for the first trace created in a
1085 static void __attribute__((destructor
)) keepalive()
1087 if(trace_recording() && buffers_to_export
) {
1089 DBG("Keeping process alive for consumer daemon...");
1090 while(buffers_to_export
) {
1091 const int interv
= 200000;
1092 restarting_usleep(interv
);
1095 if(total
>= 3000000) {
1096 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1100 DBG("Finally dying...");
1104 /* Notify ust that there was a fork. This needs to be called inside
1105 * the new process, anytime a process whose memory is not shared with
1106 * the parent is created. If this function is not called, the events
1107 * of the new process will not be collected.
1112 DBG("ust: forking");
1113 ltt_trace_stop("auto");
1114 ltt_trace_destroy("auto");
1115 ltt_trace_alloc("auto");
1116 ltt_trace_start("auto");
1120 inform_consumer_daemon("auto");