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
18 /* This file contains the implementation of the UST listener thread, which
19 * receives trace control commands. It also coordinates the initialization of
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #include <urcu/uatomic_arch.h>
37 #include <ust/marker.h>
38 #include <ust/tracectl.h>
43 #include "marker-control.h"
44 #include "multipoll.h"
46 #define USTSIGNAL SIGIO
48 #define MAX_MSG_SIZE (100)
50 #define MSG_REGISTER_NOTIF 2
52 /* This should only be accessed by the constructor, before the creation
53 * of the listener, and then only by the listener.
57 extern struct chan_info_struct chan_infos
[];
59 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
61 static struct ustcomm_app ustcomm_app
;
63 struct tracecmd
{ /* no padding */
68 /* volatile because shared between the listener and the main thread */
69 int buffers_to_export
= 0;
72 /* size: the size of all the fields except size itself */
75 /* Only the necessary part of the payload is transferred. It
76 * may even be none of it.
81 struct consumer_channel
{
83 struct ltt_channel_struct
*chan
;
86 struct blocked_consumer
{
91 /* args to ustcomm_send_reply */
92 struct ustcomm_server server
;
93 struct ustcomm_source src
;
95 /* args to ust_buffers_get_subbuf */
96 struct ust_buffer
*buf
;
98 struct list_head list
;
101 static long long make_pidunique(void)
106 gettimeofday(&tv
, NULL
);
110 retval
|= tv
.tv_usec
;
115 static void print_markers(FILE *fp
)
117 struct marker_iter iter
;
120 marker_iter_reset(&iter
);
121 marker_iter_start(&iter
);
124 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
, iter
.marker
->location
);
125 marker_iter_next(&iter
);
130 static int init_socket(void);
132 /* Ask the daemon to collect a trace called trace_name and being
133 * produced by this pid.
135 * The trace must be at least allocated. (It can also be started.)
136 * This is because _ltt_trace_find is used.
139 static void inform_consumer_daemon(const char *trace_name
)
142 struct ust_trace
*trace
;
143 pid_t pid
= getpid();
148 trace
= _ltt_trace_find(trace_name
);
150 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
154 for(i
=0; i
< trace
->nr_channels
; i
++) {
155 if(trace
->channels
[i
].request_collection
) {
156 /* iterate on all cpus */
157 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
159 if (asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
) < 0) {
160 ERR("inform_consumer_daemon : asprintf failed (%s_%d)",
161 trace
->channels
[i
].channel_name
, j
);
164 result
= ustcomm_request_consumer(pid
, buf
);
166 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
167 /* continue even if fail */
170 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
179 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
182 long consumed_old
= 0;
184 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
187 result
= read(bc
->fd_producer
, &inbuf
, 1);
194 DBG("listener: got messsage that a buffer ended");
196 res
= close(bc
->fd_producer
);
203 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
205 ERR("ustcomm_send_reply failed");
212 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
213 if(result
== -EAGAIN
) {
214 WARN("missed buffer?");
217 else if(result
< 0) {
218 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
220 if (asprintf(&reply
, "%s %ld", "OK", consumed_old
) < 0) {
221 ERR("process_blkd_consumer_act : asprintf failed (OK %ld)",
225 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
227 ERR("ustcomm_send_reply failed");
238 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
240 struct blocked_consumer
*bc
;
242 list_for_each_entry(bc
, &blocked_consumers
, list
) {
243 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
248 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
252 sep
= rindex(channel_and_cpu
, '_');
255 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
261 if (asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
) < 0) {
262 ERR("seperate_channel_cpu : asprintf failed (%.*s)",
263 (int)(sep
-channel_and_cpu
), channel_and_cpu
);
268 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
271 struct ust_trace
*trace
;
272 char trace_name
[] = "auto";
274 char *channel_and_cpu
;
282 channel_and_cpu
= nth_token(recvbuf
, 1);
283 if(channel_and_cpu
== NULL
) {
284 ERR("cannot parse channel");
289 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
291 ERR("problem parsing channel name");
293 goto free_short_chan_name
;
297 trace
= _ltt_trace_find(trace_name
);
301 ERR("cannot find trace!");
303 goto free_short_chan_name
;
306 for(i
=0; i
<trace
->nr_channels
; i
++) {
307 struct ust_channel
*channel
= &trace
->channels
[i
];
308 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
310 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
313 // DBG("the shmid for the requested channel is %d", buf->shmid);
314 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
315 if (asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]) < 0) {
316 ERR("do_cmd_get_shmid : asprintf failed (%d %d)",
317 buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
319 goto free_short_chan_name
;
322 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
324 ERR("ustcomm_send_reply failed");
327 goto free_short_chan_name
;
338 ERR("channel not found (%s)", channel_and_cpu
);
341 free_short_chan_name
:
348 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
351 struct ust_trace
*trace
;
352 char trace_name
[] = "auto";
354 char *channel_and_cpu
;
360 DBG("get_n_subbufs");
362 channel_and_cpu
= nth_token(recvbuf
, 1);
363 if(channel_and_cpu
== NULL
) {
364 ERR("cannot parse channel");
369 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
371 ERR("problem parsing channel name");
373 goto free_short_chan_name
;
377 trace
= _ltt_trace_find(trace_name
);
381 ERR("cannot find trace!");
383 goto free_short_chan_name
;
386 for(i
=0; i
<trace
->nr_channels
; i
++) {
387 struct ust_channel
*channel
= &trace
->channels
[i
];
389 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
392 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
393 if (asprintf(&reply
, "%d", channel
->subbuf_cnt
) < 0) {
394 ERR("do_cmd_get_n_subbufs : asprintf failed (%d)",
395 channel
->subbuf_cnt
);
397 goto free_short_chan_name
;
400 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
402 ERR("ustcomm_send_reply failed");
405 goto free_short_chan_name
;
414 ERR("unable to find channel");
417 free_short_chan_name
:
424 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
427 struct ust_trace
*trace
;
428 char trace_name
[] = "auto";
430 char *channel_and_cpu
;
436 DBG("get_subbuf_size");
438 channel_and_cpu
= nth_token(recvbuf
, 1);
439 if(channel_and_cpu
== NULL
) {
440 ERR("cannot parse channel");
445 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
447 ERR("problem parsing channel name");
449 goto free_short_chan_name
;
453 trace
= _ltt_trace_find(trace_name
);
457 ERR("cannot find trace!");
459 goto free_short_chan_name
;
462 for(i
=0; i
<trace
->nr_channels
; i
++) {
463 struct ust_channel
*channel
= &trace
->channels
[i
];
465 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
468 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
469 if (asprintf(&reply
, "%zd", channel
->subbuf_size
) < 0) {
470 ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)",
471 channel
->subbuf_size
);
473 goto free_short_chan_name
;
476 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
478 ERR("ustcomm_send_reply failed");
481 goto free_short_chan_name
;
490 ERR("unable to find channel");
493 free_short_chan_name
:
500 /* Return the power of two which is equal or higher to v */
502 static unsigned int pow2_higher_or_eq(unsigned int v
)
505 int retval
= 1<<(hb
-1);
513 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
515 char *channel_slash_size
;
516 char ch_name
[256]="";
517 unsigned int size
, power
;
519 struct ust_trace
*trace
;
520 char trace_name
[] = "auto";
524 DBG("set_subbuf_size");
526 channel_slash_size
= nth_token(recvbuf
, 1);
527 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
529 if(ch_name
== NULL
) {
530 ERR("cannot parse channel");
535 power
= pow2_higher_or_eq(size
);
536 power
= max_t(unsigned int, 2u, power
);
538 WARN("using the next power of two for buffer size = %u\n", power
);
541 trace
= _ltt_trace_find_setup(trace_name
);
543 ERR("cannot find trace!");
548 for(i
= 0; i
< trace
->nr_channels
; i
++) {
549 struct ust_channel
*channel
= &trace
->channels
[i
];
551 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
553 channel
->subbuf_size
= power
;
554 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
561 ERR("unable to find channel");
569 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
571 char *channel_slash_num
;
572 char ch_name
[256]="";
575 struct ust_trace
*trace
;
576 char trace_name
[] = "auto";
580 DBG("set_subbuf_num");
582 channel_slash_num
= nth_token(recvbuf
, 1);
583 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
585 if(ch_name
== NULL
) {
586 ERR("cannot parse channel");
591 ERR("subbuffer count should be greater than 2");
597 trace
= _ltt_trace_find_setup(trace_name
);
599 ERR("cannot find trace!");
604 for(i
= 0; i
< trace
->nr_channels
; i
++) {
605 struct ust_channel
*channel
= &trace
->channels
[i
];
607 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
609 channel
->subbuf_cnt
= num
;
610 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
617 ERR("unable to find channel");
625 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
628 struct ust_trace
*trace
;
629 char trace_name
[] = "auto";
631 char *channel_and_cpu
;
638 channel_and_cpu
= nth_token(recvbuf
, 1);
639 if(channel_and_cpu
== NULL
) {
640 ERR("cannot parse channel");
645 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
647 ERR("problem parsing channel name");
649 goto free_short_chan_name
;
653 trace
= _ltt_trace_find(trace_name
);
658 DBG("Cannot find trace. It was likely destroyed by the user.");
659 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
661 ERR("ustcomm_send_reply failed");
669 for(i
=0; i
<trace
->nr_channels
; i
++) {
670 struct ust_channel
*channel
= &trace
->channels
[i
];
672 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
673 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
674 struct blocked_consumer
*bc
;
678 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
680 ERR("malloc returned NULL");
683 bc
->fd_consumer
= src
->fd
;
684 bc
->fd_producer
= buf
->data_ready_fd_read
;
687 bc
->server
= ustcomm_app
.server
;
689 list_add(&bc
->list
, &blocked_consumers
);
691 /* Being here is the proof the daemon has mapped the buffer in its
692 * memory. We may now decrement buffers_to_export.
694 if(uatomic_read(&buf
->consumed
) == 0) {
695 DBG("decrementing buffers_to_export");
696 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
703 ERR("unable to find channel");
709 free_short_chan_name
:
716 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
719 struct ust_trace
*trace
;
720 char trace_name
[] = "auto";
722 char *channel_and_cpu
;
728 char *consumed_old_str
;
734 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
735 if(channel_and_cpu
== NULL
) {
736 ERR("cannot parse channel");
741 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
742 if(consumed_old_str
== NULL
) {
743 ERR("cannot parse consumed_old");
745 goto free_channel_and_cpu
;
747 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
748 if(*endptr
!= '\0') {
749 ERR("invalid value for consumed_old");
751 goto free_consumed_old_str
;
754 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
756 ERR("problem parsing channel name");
758 goto free_short_chan_name
;
762 trace
= _ltt_trace_find(trace_name
);
765 DBG("Cannot find trace. It was likely destroyed by the user.");
766 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
768 ERR("ustcomm_send_reply failed");
776 for(i
=0; i
<trace
->nr_channels
; i
++) {
777 struct ust_channel
*channel
= &trace
->channels
[i
];
779 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
780 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
784 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
786 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
787 if (asprintf(&reply
, "%s", "ERROR") < 0) {
788 ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)");
794 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
795 if (asprintf(&reply
, "%s", "OK") < 0) {
796 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
802 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
804 ERR("ustcomm_send_reply failed");
815 ERR("unable to find channel");
820 free_short_chan_name
:
822 free_consumed_old_str
:
823 free(consumed_old_str
);
824 free_channel_and_cpu
:
825 free(channel_and_cpu
);
831 static void listener_cleanup(void *ptr
)
833 ustcomm_fini_app(&ustcomm_app
, 0);
836 static void do_cmd_force_switch()
838 struct blocked_consumer
*bc
;
840 list_for_each_entry(bc
, &blocked_consumers
, list
) {
841 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
845 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
848 char trace_name
[] = "auto";
849 char trace_type
[] = "ustrelay";
852 DBG("received a message! it's: %s", recvbuf
);
853 len
= strlen(recvbuf
);
855 if(!strcmp(recvbuf
, "print_markers")) {
856 print_markers(stderr
);
858 else if(!strcmp(recvbuf
, "list_markers")) {
863 fp
= open_memstream(&ptr
, &size
);
867 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
871 else if(!strcmp(recvbuf
, "start")) {
872 /* start is an operation that setups the trace, allocates it and starts it */
873 result
= ltt_trace_setup(trace_name
);
875 ERR("ltt_trace_setup failed");
879 result
= ltt_trace_set_type(trace_name
, trace_type
);
881 ERR("ltt_trace_set_type failed");
885 result
= ltt_trace_alloc(trace_name
);
887 ERR("ltt_trace_alloc failed");
891 inform_consumer_daemon(trace_name
);
893 result
= ltt_trace_start(trace_name
);
895 ERR("ltt_trace_start failed");
899 else if(!strcmp(recvbuf
, "trace_setup")) {
902 result
= ltt_trace_setup(trace_name
);
904 ERR("ltt_trace_setup failed");
908 result
= ltt_trace_set_type(trace_name
, trace_type
);
910 ERR("ltt_trace_set_type failed");
914 else if(!strcmp(recvbuf
, "trace_alloc")) {
917 result
= ltt_trace_alloc(trace_name
);
919 ERR("ltt_trace_alloc failed");
922 inform_consumer_daemon(trace_name
);
924 else if(!strcmp(recvbuf
, "trace_create")) {
927 result
= ltt_trace_setup(trace_name
);
929 ERR("ltt_trace_setup failed");
933 result
= ltt_trace_set_type(trace_name
, trace_type
);
935 ERR("ltt_trace_set_type failed");
939 else if(!strcmp(recvbuf
, "trace_start")) {
942 result
= ltt_trace_alloc(trace_name
);
944 ERR("ltt_trace_alloc failed");
948 inform_consumer_daemon(trace_name
);
951 result
= ltt_trace_start(trace_name
);
953 ERR("ltt_trace_start failed");
957 else if(!strcmp(recvbuf
, "trace_stop")) {
960 result
= ltt_trace_stop(trace_name
);
962 ERR("ltt_trace_stop failed");
966 else if(!strcmp(recvbuf
, "trace_destroy")) {
968 DBG("trace destroy");
970 result
= ltt_trace_destroy(trace_name
, 0);
972 ERR("ltt_trace_destroy failed");
976 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
977 do_cmd_get_shmid(recvbuf
, src
);
979 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
980 do_cmd_get_n_subbufs(recvbuf
, src
);
982 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
983 do_cmd_get_subbuf_size(recvbuf
, src
);
985 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
988 libfile
= nth_token(recvbuf
, 1);
990 DBG("load_probe_lib loading %s", libfile
);
994 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
995 do_cmd_get_subbuffer(recvbuf
, src
);
997 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
998 do_cmd_put_subbuffer(recvbuf
, src
);
1000 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1001 do_cmd_set_subbuf_size(recvbuf
, src
);
1003 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1004 do_cmd_set_subbuf_num(recvbuf
, src
);
1006 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1007 char *channel_slash_name
= nth_token(recvbuf
, 1);
1008 char channel_name
[256]="";
1009 char marker_name
[256]="";
1011 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1013 if(channel_name
== NULL
|| marker_name
== NULL
) {
1014 WARN("invalid marker name");
1018 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1020 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1023 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1024 char *channel_slash_name
= nth_token(recvbuf
, 1);
1028 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1030 if(marker_name
== NULL
) {
1033 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1035 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1038 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1041 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1042 ERR("process_client_cmd : asprintf failed (%lld)",
1047 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1049 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1055 else if(nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1056 char *reply
= getenv("UST_DAEMON_SOCKET");
1058 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1059 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1063 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1067 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1070 ERR("ustcomm_send_reply failed");
1072 else if(nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1073 char *sock_path
= nth_token(recvbuf
, 1);
1074 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1076 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1078 else if(nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1079 do_cmd_force_switch();
1082 ERR("unable to parse message: %s", recvbuf
);
1090 void *listener_main(void *p
)
1096 pthread_cleanup_push(listener_cleanup
, NULL
);
1099 struct mpentries mpent
;
1101 multipoll_init(&mpent
);
1103 blocked_consumers_add_to_mp(&mpent
);
1104 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1106 result
= multipoll_poll(&mpent
, -1);
1108 ERR("error in multipoll_poll");
1111 multipoll_destroy(&mpent
);
1114 pthread_cleanup_pop(1);
1117 /* These should only be accessed in the parent thread,
1120 static volatile sig_atomic_t have_listener
= 0;
1121 static pthread_t listener_thread
;
1123 void create_listener(void)
1126 sigset_t sig_all_blocked
;
1127 sigset_t orig_parent_mask
;
1130 WARN("not creating listener because we already had one");
1134 /* A new thread created by pthread_create inherits the signal mask
1135 * from the parent. To avoid any signal being received by the
1136 * listener thread, we block all signals temporarily in the parent,
1137 * while we create the listener thread.
1140 sigfillset(&sig_all_blocked
);
1142 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1144 PERROR("pthread_sigmask: %s", strerror(result
));
1147 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1149 PERROR("pthread_create");
1152 /* Restore original signal mask in parent */
1153 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1155 PERROR("pthread_sigmask: %s", strerror(result
));
1162 static int init_socket(void)
1164 return ustcomm_init_app(getpid(), &ustcomm_app
);
1167 #define AUTOPROBE_DISABLED 0
1168 #define AUTOPROBE_ENABLE_ALL 1
1169 #define AUTOPROBE_ENABLE_REGEX 2
1170 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1171 static regex_t autoprobe_regex
;
1173 static void auto_probe_connect(struct marker
*m
)
1177 char* concat_name
= NULL
;
1178 const char *probe_name
= "default";
1180 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1183 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1184 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1186 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1187 m
->channel
, m
->name
);
1190 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1197 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1198 if(result
&& result
!= -EEXIST
)
1199 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1201 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1205 static void __attribute__((constructor
)) init()
1208 char* autoprobe_val
= NULL
;
1209 char* subbuffer_size_val
= NULL
;
1210 char* subbuffer_count_val
= NULL
;
1211 unsigned int subbuffer_size
;
1212 unsigned int subbuffer_count
;
1215 /* Assign the pidunique, to be able to differentiate the processes with same
1216 * pid, (before and after an exec).
1218 pidunique
= make_pidunique();
1220 DBG("Tracectl constructor");
1222 result
= init_socket();
1224 ERR("init_socket error");
1230 autoprobe_val
= getenv("UST_AUTOPROBE");
1232 struct marker_iter iter
;
1234 DBG("Autoprobe enabled.");
1236 /* Ensure markers are initialized */
1239 /* Ensure marker control is initialized, for the probe */
1240 init_marker_control();
1242 /* first, set the callback that will connect the
1243 * probe on new markers
1245 if(autoprobe_val
[0] == '/') {
1246 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1250 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1251 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1252 /* don't crash the application just for this */
1255 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1259 /* just enable all instrumentation */
1260 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1263 marker_set_new_marker_cb(auto_probe_connect
);
1265 /* Now, connect the probes that were already registered. */
1266 marker_iter_reset(&iter
);
1267 marker_iter_start(&iter
);
1269 DBG("now iterating on markers already registered");
1270 while(iter
.marker
) {
1271 DBG("now iterating on marker %s", iter
.marker
->name
);
1272 auto_probe_connect(iter
.marker
);
1273 marker_iter_next(&iter
);
1277 if(getenv("UST_OVERWRITE")) {
1278 int val
= atoi(getenv("UST_OVERWRITE"));
1279 if(val
== 0 || val
== 1) {
1280 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1283 WARN("invalid value for UST_OVERWRITE");
1287 if(getenv("UST_AUTOCOLLECT")) {
1288 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1289 if(val
== 0 || val
== 1) {
1290 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1293 WARN("invalid value for UST_AUTOCOLLECT");
1297 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1298 if(subbuffer_size_val
) {
1299 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1300 power
= pow2_higher_or_eq(subbuffer_size
);
1301 if(power
!= subbuffer_size
)
1302 WARN("using the next power of two for buffer size = %u\n", power
);
1303 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1306 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1307 if(subbuffer_count_val
) {
1308 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1309 if(subbuffer_count
< 2)
1310 subbuffer_count
= 2;
1311 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1314 if(getenv("UST_TRACE")) {
1315 char trace_name
[] = "auto";
1316 char trace_type
[] = "ustrelay";
1318 DBG("starting early tracing");
1320 /* Ensure marker control is initialized */
1321 init_marker_control();
1323 /* Ensure markers are initialized */
1326 /* Ensure buffers are initialized, for the transport to be available.
1327 * We are about to set a trace type and it will fail without this.
1329 init_ustrelay_transport();
1331 /* FIXME: When starting early tracing (here), depending on the
1332 * order of constructors, it is very well possible some marker
1333 * sections are not yet registered. Because of this, some
1334 * channels may not be registered. Yet, we are about to ask the
1335 * daemon to collect the channels. Channels which are not yet
1336 * registered will not be collected.
1338 * Currently, in LTTng, there is no way to add a channel after
1339 * trace start. The reason for this is that it induces complex
1340 * concurrency issues on the trace structures, which can only
1341 * be resolved using RCU. This has not been done yet. As a
1342 * workaround, we are forcing the registration of the "ust"
1343 * channel here. This is the only channel (apart from metadata)
1344 * that can be reliably used in early tracing.
1346 * Non-early tracing does not have this problem and can use
1347 * arbitrary channel names.
1349 ltt_channels_register("ust");
1351 result
= ltt_trace_setup(trace_name
);
1353 ERR("ltt_trace_setup failed");
1357 result
= ltt_trace_set_type(trace_name
, trace_type
);
1359 ERR("ltt_trace_set_type failed");
1363 result
= ltt_trace_alloc(trace_name
);
1365 ERR("ltt_trace_alloc failed");
1369 result
= ltt_trace_start(trace_name
);
1371 ERR("ltt_trace_start failed");
1375 /* Do this after the trace is started in order to avoid creating confusion
1376 * if the trace fails to start. */
1377 inform_consumer_daemon(trace_name
);
1382 /* should decrementally destroy stuff if error */
1386 /* This is only called if we terminate normally, not with an unhandled signal,
1387 * so we cannot rely on it. However, for now, LTTV requires that the header of
1388 * the last sub-buffer contain a valid end time for the trace. This is done
1389 * automatically only when the trace is properly stopped.
1391 * If the traced program crashed, it is always possible to manually add the
1392 * right value in the header, or to open the trace in text mode.
1394 * FIXME: Fix LTTV so it doesn't need this.
1397 static void destroy_traces(void)
1401 /* if trace running, finish it */
1403 DBG("destructor stopping traces");
1405 result
= ltt_trace_stop("auto");
1407 ERR("ltt_trace_stop error");
1410 result
= ltt_trace_destroy("auto", 0);
1412 ERR("ltt_trace_destroy error");
1416 static int trace_recording(void)
1419 struct ust_trace
*trace
;
1423 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1430 ltt_unlock_traces();
1436 static int have_consumer(void)
1438 return !list_empty(&blocked_consumers
);
1442 int restarting_usleep(useconds_t usecs
)
1448 tv
.tv_nsec
= usecs
* 1000;
1451 result
= nanosleep(&tv
, &tv
);
1452 } while(result
== -1 && errno
== EINTR
);
1457 static void stop_listener(void)
1464 result
= pthread_cancel(listener_thread
);
1466 ERR("pthread_cancel: %s", strerror(result
));
1468 result
= pthread_join(listener_thread
, NULL
);
1470 ERR("pthread_join: %s", strerror(result
));
1474 /* This destructor keeps the process alive for a few seconds in order
1475 * to leave time to ustd to connect to its buffers. This is necessary
1476 * for programs whose execution is very short. It is also useful in all
1477 * programs when tracing is started close to the end of the program
1480 * FIXME: For now, this only works for the first trace created in a
1484 static void __attribute__((destructor
)) keepalive()
1486 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1488 DBG("Keeping process alive for consumer daemon...");
1489 while(LOAD_SHARED(buffers_to_export
)) {
1490 const int interv
= 200000;
1491 restarting_usleep(interv
);
1494 if(total
>= 3000000) {
1495 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1499 DBG("Finally dying...");
1504 /* Ask the listener to stop and clean up. */
1508 void ust_potential_exec(void)
1510 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1517 /* Notify ust that there was a fork. This needs to be called inside
1518 * the new process, anytime a process whose memory is not shared with
1519 * the parent is created. If this function is not called, the events
1520 * of the new process will not be collected.
1522 * Signals should be disabled before the fork and reenabled only after
1523 * this call in order to guarantee tracing is not started before ust_fork()
1524 * sanitizes the new process.
1527 static void ust_fork(void)
1529 struct blocked_consumer
*bc
;
1530 struct blocked_consumer
*deletable_bc
= NULL
;
1533 /* FIXME: technically, the locks could have been taken before the fork */
1534 DBG("ust: forking");
1536 /* break lock if necessary */
1537 ltt_unlock_traces();
1539 ltt_trace_stop("auto");
1540 ltt_trace_destroy("auto", 1);
1541 /* Delete all active connections */
1542 ustcomm_close_all_connections(&ustcomm_app
.server
);
1544 /* Delete all blocked consumers */
1545 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1546 result
= close(bc
->fd_producer
);
1552 list_del(&bc
->list
);
1555 /* free app, keeping socket file */
1556 ustcomm_fini_app(&ustcomm_app
, 1);
1558 STORE_SHARED(buffers_to_export
, 0);
1562 ltt_trace_setup("auto");
1563 result
= ltt_trace_set_type("auto", "ustrelay");
1565 ERR("ltt_trace_set_type failed");
1569 ltt_trace_alloc("auto");
1570 ltt_trace_start("auto");
1571 inform_consumer_daemon("auto");
1574 void ust_before_fork(ust_fork_info_t
*fork_info
)
1576 /* Disable signals. This is to avoid that the child
1577 * intervenes before it is properly setup for tracing. It is
1578 * safer to disable all signals, because then we know we are not
1579 * breaking anything by restoring the original mask.
1585 - only do this if tracing is active
1588 /* Disable signals */
1589 sigfillset(&all_sigs
);
1590 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1592 PERROR("sigprocmask");
1597 /* Don't call this function directly in a traced program */
1598 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1602 /* Restore signals */
1603 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1605 PERROR("sigprocmask");
1610 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1612 /* Reenable signals */
1613 ust_after_fork_common(fork_info
);
1616 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1618 /* First sanitize the child */
1621 /* Then reenable interrupts */
1622 ust_after_fork_common(fork_info
);