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 //struct listener_arg {
63 /* size: the size of all the fields except size itself */
66 /* Only the necessary part of the payload is transferred. It
67 * may even be none of it.
72 struct consumer_channel
{
74 struct ltt_channel_struct
*chan
;
77 struct blocked_consumer
{
82 /* args to ustcomm_send_reply */
83 struct ustcomm_server server
;
84 struct ustcomm_source src
;
86 /* args to ltt_do_get_subbuf */
87 struct rchan_buf
*rbuf
;
88 struct ltt_channel_buf_struct
*lttbuf
;
90 struct list_head list
;
93 static void print_markers(FILE *fp
)
95 struct marker_iter iter
;
98 marker_iter_reset(&iter
);
99 marker_iter_start(&iter
);
102 fprintf(fp
, "marker: %s_%s %d \"%s\"\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
);
103 marker_iter_next(&iter
);
108 static int init_socket(void);
110 /* This needs to be called whenever a new thread is created. It notifies
111 * liburcu of the new thread.
114 void ust_register_thread(void)
116 rcu_register_thread();
123 struct trctl_msg msg
;
125 /* FIXME: fd_notif should probably be protected by a spinlock */
130 msg
.type
= MSG_NOTIF
;
131 msg
.size
= sizeof(msg
.type
);
133 /* FIXME: don't block here */
134 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
141 /* Ask the daemon to collect a trace called trace_name and being
142 * produced by this pid.
144 * The trace must be at least allocated. (It can also be started.)
145 * This is because _ltt_trace_find is used.
148 static void inform_consumer_daemon(const char *trace_name
)
151 struct ltt_trace_struct
*trace
;
152 pid_t pid
= getpid();
157 trace
= _ltt_trace_find(trace_name
);
159 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
163 for(i
=0; i
< trace
->nr_channels
; i
++) {
164 result
= ustcomm_request_consumer(pid
, trace
->channels
[i
].channel_name
);
166 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
167 /* continue even if fail */
175 void process_blocked_consumers(void)
179 struct blocked_consumer
*bc
;
184 list_for_each_entry(bc
, &blocked_consumers
, list
) {
188 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
190 ERR("malloc returned NULL");
194 list_for_each_entry(bc
, &blocked_consumers
, list
) {
195 fds
[idx
].fd
= bc
->fd_producer
;
196 fds
[idx
].events
= POLLIN
;
197 bc
->tmp_poll_idx
= idx
;
201 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
208 list_for_each_entry(bc
, &blocked_consumers
, list
) {
209 if(fds
[bc
->tmp_poll_idx
].revents
) {
210 long consumed_old
= 0;
213 result
= read(bc
->fd_producer
, &inbuf
, 1);
221 close(bc
->fd_producer
);
225 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
227 ERR("ustcomm_send_reply failed");
234 result
= ltt_do_get_subbuf(bc
->rbuf
, bc
->lttbuf
, &consumed_old
);
235 if(result
== -EAGAIN
) {
236 WARN("missed buffer?");
239 else if(result
< 0) {
240 DBG("ltt_do_get_subbuf: error: %s", strerror(-result
));
242 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
243 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
245 ERR("ustcomm_send_reply failed");
257 void *listener_main(void *p
)
261 ust_register_thread();
266 char trace_name
[] = "auto";
267 char trace_type
[] = "ustrelay";
270 struct ustcomm_source src
;
272 process_blocked_consumers();
274 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
276 WARN("error in ustcomm_app_recv_message");
279 else if(result
== 0) {
284 DBG("received a message! it's: %s", recvbuf
);
285 len
= strlen(recvbuf
);
287 if(!strcmp(recvbuf
, "print_markers")) {
288 print_markers(stderr
);
290 else if(!strcmp(recvbuf
, "list_markers")) {
295 fp
= open_memstream(&ptr
, &size
);
299 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
303 else if(!strcmp(recvbuf
, "start")) {
304 /* start is an operation that setups the trace, allocates it and starts it */
305 result
= ltt_trace_setup(trace_name
);
307 ERR("ltt_trace_setup failed");
311 result
= ltt_trace_set_type(trace_name
, trace_type
);
313 ERR("ltt_trace_set_type failed");
317 result
= ltt_trace_alloc(trace_name
);
319 ERR("ltt_trace_alloc failed");
323 inform_consumer_daemon(trace_name
);
325 result
= ltt_trace_start(trace_name
);
327 ERR("ltt_trace_start failed");
331 else if(!strcmp(recvbuf
, "trace_setup")) {
334 result
= ltt_trace_setup(trace_name
);
336 ERR("ltt_trace_setup failed");
340 result
= ltt_trace_set_type(trace_name
, trace_type
);
342 ERR("ltt_trace_set_type failed");
346 else if(!strcmp(recvbuf
, "trace_alloc")) {
349 result
= ltt_trace_alloc(trace_name
);
351 ERR("ltt_trace_alloc failed");
355 else if(!strcmp(recvbuf
, "trace_start")) {
358 result
= ltt_trace_start(trace_name
);
360 ERR("ltt_trace_start failed");
364 else if(!strcmp(recvbuf
, "trace_stop")) {
367 result
= ltt_trace_stop(trace_name
);
369 ERR("ltt_trace_stop failed");
373 else if(!strcmp(recvbuf
, "trace_destroy")) {
375 DBG("trace destroy");
377 result
= ltt_trace_destroy(trace_name
);
379 ERR("ltt_trace_destroy failed");
383 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
384 struct ltt_trace_struct
*trace
;
385 char trace_name
[] = "auto";
391 channel_name
= nth_token(recvbuf
, 1);
392 if(channel_name
== NULL
) {
393 ERR("get_shmid: cannot parse channel");
398 trace
= _ltt_trace_find(trace_name
);
402 ERR("cannot find trace!");
406 for(i
=0; i
<trace
->nr_channels
; i
++) {
407 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
408 struct rchan_buf
*rbuf
= rchan
->buf
;
409 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)rchan
->private_data
;
411 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
414 DBG("the shmid for the requested channel is %d", rbuf
->shmid
);
415 DBG("the shmid for its buffer structure is %d", ltt_channel
->buf_shmid
);
416 asprintf(&reply
, "%d %d", rbuf
->shmid
, ltt_channel
->buf_shmid
);
418 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
420 ERR("listener: get_shmid: ustcomm_send_reply failed");
430 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
431 struct ltt_trace_struct
*trace
;
432 char trace_name
[] = "auto";
436 DBG("get_n_subbufs");
438 channel_name
= nth_token(recvbuf
, 1);
439 if(channel_name
== NULL
) {
440 ERR("get_n_subbufs: cannot parse channel");
445 trace
= _ltt_trace_find(trace_name
);
449 ERR("cannot find trace!");
453 for(i
=0; i
<trace
->nr_channels
; i
++) {
454 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
456 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
459 DBG("the n_subbufs for the requested channel is %zd", rchan
->n_subbufs
);
460 asprintf(&reply
, "%zd", rchan
->n_subbufs
);
462 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
464 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
474 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
475 struct ltt_trace_struct
*trace
;
476 char trace_name
[] = "auto";
480 DBG("get_subbuf_size");
482 channel_name
= nth_token(recvbuf
, 1);
483 if(channel_name
== NULL
) {
484 ERR("get_subbuf_size: cannot parse channel");
489 trace
= _ltt_trace_find(trace_name
);
493 ERR("cannot find trace!");
497 for(i
=0; i
<trace
->nr_channels
; i
++) {
498 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
500 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
503 DBG("the subbuf_size for the requested channel is %zd", rchan
->subbuf_size
);
504 asprintf(&reply
, "%zd", rchan
->subbuf_size
);
506 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
508 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
518 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
521 libfile
= nth_token(recvbuf
, 1);
523 DBG("load_probe_lib loading %s", libfile
);
525 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
526 struct ltt_trace_struct
*trace
;
527 char trace_name
[] = "auto";
533 channel_name
= nth_token(recvbuf
, 1);
534 if(channel_name
== NULL
) {
535 ERR("get_subbuf: cannot parse channel");
540 trace
= _ltt_trace_find(trace_name
);
544 ERR("cannot find trace!");
548 for(i
=0; i
<trace
->nr_channels
; i
++) {
549 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
551 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
552 struct rchan_buf
*rbuf
= rchan
->buf
;
553 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
554 struct blocked_consumer
*bc
;
556 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
558 ERR("malloc returned NULL");
561 bc
->fd_consumer
= src
.fd
;
562 bc
->fd_producer
= lttbuf
->data_ready_fd_read
;
566 bc
->server
= ustcomm_app
.server
;
568 list_add(&bc
->list
, &blocked_consumers
);
574 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
575 struct ltt_trace_struct
*trace
;
576 char trace_name
[] = "auto";
580 char *consumed_old_str
;
585 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
586 if(channel_name
== NULL
) {
587 ERR("put_subbuf_size: cannot parse channel");
591 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
592 if(consumed_old_str
== NULL
) {
593 ERR("put_subbuf: cannot parse consumed_old");
596 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
597 if(*endptr
!= '\0') {
598 ERR("put_subbuf: invalid value for consumed_old");
603 trace
= _ltt_trace_find(trace_name
);
607 ERR("cannot find trace!");
611 for(i
=0; i
<trace
->nr_channels
; i
++) {
612 struct rchan
*rchan
= trace
->channels
[i
].trans_channel_data
;
614 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
615 struct rchan_buf
*rbuf
= rchan
->buf
;
616 struct ltt_channel_buf_struct
*lttbuf
= trace
->channels
[i
].buf
;
620 result
= ltt_do_put_subbuf(rbuf
, lttbuf
, consumed_old
);
622 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name
);
623 asprintf(&reply
, "%s", "ERROR");
626 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name
);
627 asprintf(&reply
, "%s", "OK");
630 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
632 ERR("listener: put_subbuf: ustcomm_send_reply failed");
643 free(consumed_old_str
);
645 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
646 char *channel_slash_name
= nth_token(recvbuf
, 1);
647 char channel_name
[256]="";
648 char marker_name
[256]="";
650 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
652 if(channel_name
== NULL
|| marker_name
== NULL
) {
653 WARN("invalid marker name");
656 printf("%s %s\n", channel_name
, marker_name
);
658 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
660 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
663 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
664 char *channel_slash_name
= nth_token(recvbuf
, 1);
668 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
670 if(marker_name
== NULL
) {
672 printf("%s %s\n", channel_name
, marker_name
);
674 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
676 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
679 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
680 // struct ltt_trace_struct *trace;
681 // char trace_name[] = "auto";
683 // char *channel_name;
685 // DBG("get_notifications");
687 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
688 // if(channel_name == NULL) {
689 // ERR("put_subbuf_size: cannot parse channel");
693 // ltt_lock_traces();
694 // trace = _ltt_trace_find(trace_name);
695 // ltt_unlock_traces();
697 // if(trace == NULL) {
698 // ERR("cannot find trace!");
702 // for(i=0; i<trace->nr_channels; i++) {
703 // struct rchan *rchan = trace->channels[i].trans_channel_data;
706 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
707 // struct rchan_buf *rbuf = rchan->buf;
708 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
710 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
711 // if(result == -1) {
712 // ERR("ustcomm_app_detach_client failed");
716 // lttbuf->wake_consumer_arg = (void *) fd;
720 // lttbuf->call_wake_consumer = 1;
726 // free(channel_name);
729 ERR("unable to parse message: %s", recvbuf
);
737 int have_listener
= 0;
739 void create_listener(void)
742 static char listener_stack
[16384];
751 result
= clone(listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
758 pthread_create(&thread
, NULL
, listener_main
, NULL
);
764 /* The signal handler itself. Signals must be setup so there cannot be
767 void sighandler(int sig
)
769 static char have_listener
= 0;
778 /* Called by the app signal handler to chain it to us. */
780 void chain_signal(void)
782 sighandler(USTSIGNAL
);
785 static int init_socket(void)
787 return ustcomm_init_app(getpid(), &ustcomm_app
);
790 /* FIXME: reenable this to delete socket file. */
793 static void destroy_socket(void)
797 if(mysocketfile
[0] == '\0')
800 result
= unlink(mysocketfile
);
807 static int init_signal_handler(void)
809 /* Attempt to handler SIGIO. If the main program wants to
810 * handle it, fine, it'll override us. They it'll have to
811 * use the chaining function.
815 struct sigaction act
;
817 result
= sigemptyset(&act
.sa_mask
);
819 PERROR("sigemptyset");
823 act
.sa_handler
= sighandler
;
824 act
.sa_flags
= SA_RESTART
;
826 /* Only defer ourselves. Also, try to restart interrupted
827 * syscalls to disturb the traced program as little as possible.
829 result
= sigaction(SIGIO
, &act
, NULL
);
838 #define AUTOPROBE_DISABLED 0
839 #define AUTOPROBE_ENABLE_ALL 1
840 #define AUTOPROBE_ENABLE_REGEX 2
841 static int autoprobe_method
= AUTOPROBE_DISABLED
;
842 static regex_t autoprobe_regex
;
844 static void auto_probe_connect(struct marker
*m
)
848 char* concat_name
= NULL
;
849 const char *probe_name
= "default";
851 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
854 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
855 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
857 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
858 m
->channel
, m
->name
);
861 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
868 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
869 if(result
&& result
!= -EEXIST
)
870 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
872 DBG("auto connected marker %s %s to probe default", m
->channel
, m
->name
);
876 static void __attribute__((constructor(1000))) init()
879 char* autoprobe_val
= NULL
;
881 /* Initialize RCU in case the constructor order is not good. */
884 /* It is important to do this before events start to be generated. */
885 ust_register_thread();
887 DBG("Tracectl constructor");
889 /* Must create socket before signal handler to prevent races.
891 result
= init_socket();
893 ERR("init_socket error");
896 result
= init_signal_handler();
898 ERR("init_signal_handler error");
902 autoprobe_val
= getenv("UST_AUTOPROBE");
904 struct marker_iter iter
;
906 DBG("Autoprobe enabled.");
908 /* Ensure markers are initialized */
911 /* Ensure marker control is initialized, for the probe */
912 init_marker_control();
914 /* first, set the callback that will connect the
915 * probe on new markers
917 if(autoprobe_val
[0] == '/') {
918 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
922 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
923 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
924 /* don't crash the application just for this */
927 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
931 /* just enable all instrumentation */
932 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
935 marker_set_new_marker_cb(auto_probe_connect
);
937 /* Now, connect the probes that were already registered. */
938 marker_iter_reset(&iter
);
939 marker_iter_start(&iter
);
941 DBG("now iterating on markers already registered");
943 DBG("now iterating on marker %s", iter
.marker
->name
);
944 auto_probe_connect(iter
.marker
);
945 marker_iter_next(&iter
);
949 if(getenv("UST_TRACE")) {
950 char trace_name
[] = "auto";
951 char trace_type
[] = "ustrelay";
953 DBG("starting early tracing");
955 /* Ensure marker control is initialized */
956 init_marker_control();
958 /* Ensure relay is initialized */
959 init_ustrelay_transport();
961 /* Ensure markers are initialized */
965 ltt_channels_register("ust");
967 result
= ltt_trace_setup(trace_name
);
969 ERR("ltt_trace_setup failed");
973 result
= ltt_trace_set_type(trace_name
, trace_type
);
975 ERR("ltt_trace_set_type failed");
979 result
= ltt_trace_alloc(trace_name
);
981 ERR("ltt_trace_alloc failed");
985 inform_consumer_daemon(trace_name
);
987 result
= ltt_trace_start(trace_name
);
989 ERR("ltt_trace_start failed");
997 /* should decrementally destroy stuff if error */
1001 /* This is only called if we terminate normally, not with an unhandled signal,
1002 * so we cannot rely on it. */
1004 /* This destructor probably isn't needed, because ustd can do crash recovery. */
1006 static void __attribute__((destructor
)) fini()
1010 /* if trace running, finish it */
1012 DBG("destructor stopping traces");
1014 result
= ltt_trace_stop("auto");
1016 ERR("ltt_trace_stop error");
1019 result
= ltt_trace_destroy("auto");
1021 ERR("ltt_trace_destroy error");
1029 static int trace_recording(void)
1032 struct ltt_trace_struct
*trace
;
1036 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1043 ltt_unlock_traces();
1048 static int have_consumer(void)
1050 return !list_empty(&blocked_consumers
);
1053 /* This destructor keeps the process alive for a few seconds in order
1054 * to leave time to ustd to consume its buffers.
1057 int restarting_sleep(int secs
)
1066 result
= nanosleep(&tv
, &tv
);
1067 } while(result
== -1 && errno
== EINTR
);
1072 static void __attribute__((destructor
)) keepalive()
1074 // struct ustcomm_ustd ustd;
1078 // result = sigemptyset(&sigset);
1079 // if(result == -1) {
1080 // perror("sigemptyset");
1083 // result = sigaddset(&sigset, SIGIO);
1084 // if(result == -1) {
1085 // perror("sigaddset");
1088 // result = sigprocmask(SIG_BLOCK, &sigset, NULL);
1089 // if(result == -1) {
1090 // perror("sigprocmask");
1094 // if(trace_recording()) {
1095 // if(!have_consumer()) {
1096 // /* Request listener creation. We've blocked SIGIO's in
1097 // * order to not interrupt sleep(), so we will miss the
1098 // * one sent by the daemon and therefore won't create
1099 // * the listener automatically.
1101 // create_listener();
1103 printf("Keeping process alive for consumer daemon...\n");
1104 restarting_sleep(3);
1105 printf("Finally dying...\n");
1109 // result = sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1110 // if(result == -1) {
1111 // perror("sigprocmask");
1117 /* Notify ust that there was a fork. This needs to be called inside
1118 * the new process, anytime a process whose memory is not shared with
1119 * the parent is created. If this function is not called, the events
1120 * of the new process will not be collected.
1125 DBG("ust: forking");
1126 ltt_trace_stop("auto");
1127 ltt_trace_destroy("auto");
1128 ltt_trace_alloc("auto");
1129 ltt_trace_start("auto");
1133 inform_consumer_daemon("auto");