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");
288 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
290 ERR("problem parsing channel name");
291 goto free_short_chan_name
;
295 trace
= _ltt_trace_find(trace_name
);
299 ERR("cannot find trace!");
301 goto free_short_chan_name
;
304 for(i
=0; i
<trace
->nr_channels
; i
++) {
305 struct ust_channel
*channel
= &trace
->channels
[i
];
306 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
308 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
311 // DBG("the shmid for the requested channel is %d", buf->shmid);
312 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
313 if (asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]) < 0) {
314 ERR("do_cmd_get_shmid : asprintf failed (%d %d)",
315 buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
317 goto free_short_chan_name
;
320 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
322 ERR("ustcomm_send_reply failed");
325 goto free_short_chan_name
;
336 ERR("channel not found (%s)", channel_and_cpu
);
339 free_short_chan_name
:
346 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
349 struct ust_trace
*trace
;
350 char trace_name
[] = "auto";
352 char *channel_and_cpu
;
358 DBG("get_n_subbufs");
360 channel_and_cpu
= nth_token(recvbuf
, 1);
361 if(channel_and_cpu
== NULL
) {
362 ERR("cannot parse channel");
366 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
368 ERR("problem parsing channel name");
369 goto free_short_chan_name
;
373 trace
= _ltt_trace_find(trace_name
);
377 ERR("cannot find trace!");
379 goto free_short_chan_name
;
382 for(i
=0; i
<trace
->nr_channels
; i
++) {
383 struct ust_channel
*channel
= &trace
->channels
[i
];
385 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
388 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
389 if (asprintf(&reply
, "%d", channel
->subbuf_cnt
) < 0) {
390 ERR("do_cmd_get_n_subbufs : asprintf failed (%d)",
391 channel
->subbuf_cnt
);
393 goto free_short_chan_name
;
396 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
398 ERR("ustcomm_send_reply failed");
401 goto free_short_chan_name
;
410 ERR("unable to find channel");
413 free_short_chan_name
:
420 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
423 struct ust_trace
*trace
;
424 char trace_name
[] = "auto";
426 char *channel_and_cpu
;
432 DBG("get_subbuf_size");
434 channel_and_cpu
= nth_token(recvbuf
, 1);
435 if(channel_and_cpu
== NULL
) {
436 ERR("cannot parse channel");
440 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
442 ERR("problem parsing channel name");
443 goto free_short_chan_name
;
447 trace
= _ltt_trace_find(trace_name
);
451 ERR("cannot find trace!");
453 goto free_short_chan_name
;
456 for(i
=0; i
<trace
->nr_channels
; i
++) {
457 struct ust_channel
*channel
= &trace
->channels
[i
];
459 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
462 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
463 if (asprintf(&reply
, "%zd", channel
->subbuf_size
) < 0) {
464 ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)",
465 channel
->subbuf_size
);
467 goto free_short_chan_name
;
470 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
472 ERR("ustcomm_send_reply failed");
475 goto free_short_chan_name
;
484 ERR("unable to find channel");
487 free_short_chan_name
:
494 /* Return the power of two which is equal or higher to v */
496 static unsigned int pow2_higher_or_eq(unsigned int v
)
499 int retval
= 1<<(hb
-1);
507 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
509 char *channel_slash_size
;
510 char ch_name
[256]="";
511 unsigned int size
, power
;
513 struct ust_trace
*trace
;
514 char trace_name
[] = "auto";
518 DBG("set_subbuf_size");
520 channel_slash_size
= nth_token(recvbuf
, 1);
521 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
523 if(ch_name
== NULL
) {
524 ERR("cannot parse channel");
528 power
= pow2_higher_or_eq(size
);
529 power
= max_t(unsigned int, 2u, power
);
531 WARN("using the next power of two for buffer size = %u\n", power
);
534 trace
= _ltt_trace_find_setup(trace_name
);
536 ERR("cannot find trace!");
541 for(i
= 0; i
< trace
->nr_channels
; i
++) {
542 struct ust_channel
*channel
= &trace
->channels
[i
];
544 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
546 channel
->subbuf_size
= power
;
547 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
554 ERR("unable to find channel");
562 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
564 char *channel_slash_num
;
565 char ch_name
[256]="";
568 struct ust_trace
*trace
;
569 char trace_name
[] = "auto";
573 DBG("set_subbuf_num");
575 channel_slash_num
= nth_token(recvbuf
, 1);
576 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
578 if(ch_name
== NULL
) {
579 ERR("cannot parse channel");
583 ERR("subbuffer count should be greater than 2");
588 trace
= _ltt_trace_find_setup(trace_name
);
590 ERR("cannot find trace!");
595 for(i
= 0; i
< trace
->nr_channels
; i
++) {
596 struct ust_channel
*channel
= &trace
->channels
[i
];
598 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
600 channel
->subbuf_cnt
= num
;
601 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
608 ERR("unable to find channel");
616 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
619 struct ust_trace
*trace
;
620 char trace_name
[] = "auto";
622 char *channel_and_cpu
;
629 channel_and_cpu
= nth_token(recvbuf
, 1);
630 if(channel_and_cpu
== NULL
) {
631 ERR("cannot parse channel");
635 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
637 ERR("problem parsing channel name");
638 goto free_short_chan_name
;
642 trace
= _ltt_trace_find(trace_name
);
647 DBG("Cannot find trace. It was likely destroyed by the user.");
648 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
650 ERR("ustcomm_send_reply failed");
658 for(i
=0; i
<trace
->nr_channels
; i
++) {
659 struct ust_channel
*channel
= &trace
->channels
[i
];
661 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
662 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
663 struct blocked_consumer
*bc
;
667 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
669 ERR("malloc returned NULL");
672 bc
->fd_consumer
= src
->fd
;
673 bc
->fd_producer
= buf
->data_ready_fd_read
;
676 bc
->server
= ustcomm_app
.server
;
678 list_add(&bc
->list
, &blocked_consumers
);
680 /* Being here is the proof the daemon has mapped the buffer in its
681 * memory. We may now decrement buffers_to_export.
683 if(uatomic_read(&buf
->consumed
) == 0) {
684 DBG("decrementing buffers_to_export");
685 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
692 ERR("unable to find channel");
698 free_short_chan_name
:
705 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
708 struct ust_trace
*trace
;
709 char trace_name
[] = "auto";
711 char *channel_and_cpu
;
717 char *consumed_old_str
;
723 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
724 if(channel_and_cpu
== NULL
) {
725 ERR("cannot parse channel");
730 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
731 if(consumed_old_str
== NULL
) {
732 ERR("cannot parse consumed_old");
734 goto free_channel_and_cpu
;
736 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
737 if(*endptr
!= '\0') {
738 ERR("invalid value for consumed_old");
740 goto free_consumed_old_str
;
743 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
745 ERR("problem parsing channel name");
747 goto free_short_chan_name
;
751 trace
= _ltt_trace_find(trace_name
);
754 DBG("Cannot find trace. It was likely destroyed by the user.");
755 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
757 ERR("ustcomm_send_reply failed");
765 for(i
=0; i
<trace
->nr_channels
; i
++) {
766 struct ust_channel
*channel
= &trace
->channels
[i
];
768 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
769 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
773 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
775 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
776 if (asprintf(&reply
, "%s", "ERROR") < 0) {
777 ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)");
783 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
784 if (asprintf(&reply
, "%s", "OK") < 0) {
785 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
791 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
793 ERR("ustcomm_send_reply failed");
804 ERR("unable to find channel");
809 free_short_chan_name
:
811 free_consumed_old_str
:
812 free(consumed_old_str
);
813 free_channel_and_cpu
:
814 free(channel_and_cpu
);
820 static void listener_cleanup(void *ptr
)
822 ustcomm_fini_app(&ustcomm_app
, 0);
825 static void do_cmd_force_switch()
827 struct blocked_consumer
*bc
;
829 list_for_each_entry(bc
, &blocked_consumers
, list
) {
830 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
834 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
837 char trace_name
[] = "auto";
838 char trace_type
[] = "ustrelay";
841 DBG("received a message! it's: %s", recvbuf
);
842 len
= strlen(recvbuf
);
844 if(!strcmp(recvbuf
, "print_markers")) {
845 print_markers(stderr
);
847 else if(!strcmp(recvbuf
, "list_markers")) {
852 fp
= open_memstream(&ptr
, &size
);
856 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
860 else if(!strcmp(recvbuf
, "start")) {
861 /* start is an operation that setups the trace, allocates it and starts it */
862 result
= ltt_trace_setup(trace_name
);
864 ERR("ltt_trace_setup failed");
868 result
= ltt_trace_set_type(trace_name
, trace_type
);
870 ERR("ltt_trace_set_type failed");
874 result
= ltt_trace_alloc(trace_name
);
876 ERR("ltt_trace_alloc failed");
880 inform_consumer_daemon(trace_name
);
882 result
= ltt_trace_start(trace_name
);
884 ERR("ltt_trace_start failed");
888 else if(!strcmp(recvbuf
, "trace_setup")) {
891 result
= ltt_trace_setup(trace_name
);
893 ERR("ltt_trace_setup failed");
897 result
= ltt_trace_set_type(trace_name
, trace_type
);
899 ERR("ltt_trace_set_type failed");
903 else if(!strcmp(recvbuf
, "trace_alloc")) {
906 result
= ltt_trace_alloc(trace_name
);
908 ERR("ltt_trace_alloc failed");
911 inform_consumer_daemon(trace_name
);
913 else if(!strcmp(recvbuf
, "trace_create")) {
916 result
= ltt_trace_setup(trace_name
);
918 ERR("ltt_trace_setup failed");
922 result
= ltt_trace_set_type(trace_name
, trace_type
);
924 ERR("ltt_trace_set_type failed");
928 else if(!strcmp(recvbuf
, "trace_start")) {
931 result
= ltt_trace_alloc(trace_name
);
933 ERR("ltt_trace_alloc failed");
937 inform_consumer_daemon(trace_name
);
940 result
= ltt_trace_start(trace_name
);
942 ERR("ltt_trace_start failed");
946 else if(!strcmp(recvbuf
, "trace_stop")) {
949 result
= ltt_trace_stop(trace_name
);
951 ERR("ltt_trace_stop failed");
955 else if(!strcmp(recvbuf
, "trace_destroy")) {
957 DBG("trace destroy");
959 result
= ltt_trace_destroy(trace_name
, 0);
961 ERR("ltt_trace_destroy failed");
965 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
966 do_cmd_get_shmid(recvbuf
, src
);
968 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
969 do_cmd_get_n_subbufs(recvbuf
, src
);
971 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
972 do_cmd_get_subbuf_size(recvbuf
, src
);
974 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
977 libfile
= nth_token(recvbuf
, 1);
979 DBG("load_probe_lib loading %s", libfile
);
983 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
984 do_cmd_get_subbuffer(recvbuf
, src
);
986 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
987 do_cmd_put_subbuffer(recvbuf
, src
);
989 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
990 do_cmd_set_subbuf_size(recvbuf
, src
);
992 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
993 do_cmd_set_subbuf_num(recvbuf
, src
);
995 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
996 char *channel_slash_name
= nth_token(recvbuf
, 1);
997 char channel_name
[256]="";
998 char marker_name
[256]="";
1000 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1002 if(channel_name
== NULL
|| marker_name
== NULL
) {
1003 WARN("invalid marker name");
1007 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1009 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1012 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1013 char *channel_slash_name
= nth_token(recvbuf
, 1);
1017 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1019 if(marker_name
== NULL
) {
1022 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1024 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1027 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1030 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1031 ERR("process_client_cmd : asprintf failed (%lld)",
1036 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1038 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1044 else if(nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1045 char *reply
= getenv("UST_DAEMON_SOCKET");
1047 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1048 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1052 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1056 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1059 ERR("ustcomm_send_reply failed");
1061 else if(nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1062 char *sock_path
= nth_token(recvbuf
, 1);
1063 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1065 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1067 else if(nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1068 do_cmd_force_switch();
1071 ERR("unable to parse message: %s", recvbuf
);
1079 void *listener_main(void *p
)
1085 pthread_cleanup_push(listener_cleanup
, NULL
);
1088 struct mpentries mpent
;
1090 multipoll_init(&mpent
);
1092 blocked_consumers_add_to_mp(&mpent
);
1093 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1095 result
= multipoll_poll(&mpent
, -1);
1097 ERR("error in multipoll_poll");
1100 multipoll_destroy(&mpent
);
1103 pthread_cleanup_pop(1);
1106 /* These should only be accessed in the parent thread,
1109 static volatile sig_atomic_t have_listener
= 0;
1110 static pthread_t listener_thread
;
1112 void create_listener(void)
1115 sigset_t sig_all_blocked
;
1116 sigset_t orig_parent_mask
;
1119 WARN("not creating listener because we already had one");
1123 /* A new thread created by pthread_create inherits the signal mask
1124 * from the parent. To avoid any signal being received by the
1125 * listener thread, we block all signals temporarily in the parent,
1126 * while we create the listener thread.
1129 sigfillset(&sig_all_blocked
);
1131 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1133 PERROR("pthread_sigmask: %s", strerror(result
));
1136 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1138 PERROR("pthread_create");
1141 /* Restore original signal mask in parent */
1142 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1144 PERROR("pthread_sigmask: %s", strerror(result
));
1151 static int init_socket(void)
1153 return ustcomm_init_app(getpid(), &ustcomm_app
);
1156 #define AUTOPROBE_DISABLED 0
1157 #define AUTOPROBE_ENABLE_ALL 1
1158 #define AUTOPROBE_ENABLE_REGEX 2
1159 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1160 static regex_t autoprobe_regex
;
1162 static void auto_probe_connect(struct marker
*m
)
1166 char* concat_name
= NULL
;
1167 const char *probe_name
= "default";
1169 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1172 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1173 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1175 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1176 m
->channel
, m
->name
);
1179 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1186 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1187 if(result
&& result
!= -EEXIST
)
1188 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1190 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1194 static void __attribute__((constructor
)) init()
1197 char* autoprobe_val
= NULL
;
1198 char* subbuffer_size_val
= NULL
;
1199 char* subbuffer_count_val
= NULL
;
1200 unsigned int subbuffer_size
;
1201 unsigned int subbuffer_count
;
1204 /* Assign the pidunique, to be able to differentiate the processes with same
1205 * pid, (before and after an exec).
1207 pidunique
= make_pidunique();
1209 DBG("Tracectl constructor");
1211 result
= init_socket();
1213 ERR("init_socket error");
1219 autoprobe_val
= getenv("UST_AUTOPROBE");
1221 struct marker_iter iter
;
1223 DBG("Autoprobe enabled.");
1225 /* Ensure markers are initialized */
1228 /* Ensure marker control is initialized, for the probe */
1229 init_marker_control();
1231 /* first, set the callback that will connect the
1232 * probe on new markers
1234 if(autoprobe_val
[0] == '/') {
1235 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1239 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1240 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1241 /* don't crash the application just for this */
1244 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1248 /* just enable all instrumentation */
1249 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1252 marker_set_new_marker_cb(auto_probe_connect
);
1254 /* Now, connect the probes that were already registered. */
1255 marker_iter_reset(&iter
);
1256 marker_iter_start(&iter
);
1258 DBG("now iterating on markers already registered");
1259 while(iter
.marker
) {
1260 DBG("now iterating on marker %s", iter
.marker
->name
);
1261 auto_probe_connect(iter
.marker
);
1262 marker_iter_next(&iter
);
1266 if(getenv("UST_OVERWRITE")) {
1267 int val
= atoi(getenv("UST_OVERWRITE"));
1268 if(val
== 0 || val
== 1) {
1269 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1272 WARN("invalid value for UST_OVERWRITE");
1276 if(getenv("UST_AUTOCOLLECT")) {
1277 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1278 if(val
== 0 || val
== 1) {
1279 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1282 WARN("invalid value for UST_AUTOCOLLECT");
1286 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1287 if(subbuffer_size_val
) {
1288 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1289 power
= pow2_higher_or_eq(subbuffer_size
);
1290 if(power
!= subbuffer_size
)
1291 WARN("using the next power of two for buffer size = %u\n", power
);
1292 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1295 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1296 if(subbuffer_count_val
) {
1297 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1298 if(subbuffer_count
< 2)
1299 subbuffer_count
= 2;
1300 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1303 if(getenv("UST_TRACE")) {
1304 char trace_name
[] = "auto";
1305 char trace_type
[] = "ustrelay";
1307 DBG("starting early tracing");
1309 /* Ensure marker control is initialized */
1310 init_marker_control();
1312 /* Ensure markers are initialized */
1315 /* Ensure buffers are initialized, for the transport to be available.
1316 * We are about to set a trace type and it will fail without this.
1318 init_ustrelay_transport();
1320 /* FIXME: When starting early tracing (here), depending on the
1321 * order of constructors, it is very well possible some marker
1322 * sections are not yet registered. Because of this, some
1323 * channels may not be registered. Yet, we are about to ask the
1324 * daemon to collect the channels. Channels which are not yet
1325 * registered will not be collected.
1327 * Currently, in LTTng, there is no way to add a channel after
1328 * trace start. The reason for this is that it induces complex
1329 * concurrency issues on the trace structures, which can only
1330 * be resolved using RCU. This has not been done yet. As a
1331 * workaround, we are forcing the registration of the "ust"
1332 * channel here. This is the only channel (apart from metadata)
1333 * that can be reliably used in early tracing.
1335 * Non-early tracing does not have this problem and can use
1336 * arbitrary channel names.
1338 ltt_channels_register("ust");
1340 result
= ltt_trace_setup(trace_name
);
1342 ERR("ltt_trace_setup failed");
1346 result
= ltt_trace_set_type(trace_name
, trace_type
);
1348 ERR("ltt_trace_set_type failed");
1352 result
= ltt_trace_alloc(trace_name
);
1354 ERR("ltt_trace_alloc failed");
1358 result
= ltt_trace_start(trace_name
);
1360 ERR("ltt_trace_start failed");
1364 /* Do this after the trace is started in order to avoid creating confusion
1365 * if the trace fails to start. */
1366 inform_consumer_daemon(trace_name
);
1371 /* should decrementally destroy stuff if error */
1375 /* This is only called if we terminate normally, not with an unhandled signal,
1376 * so we cannot rely on it. However, for now, LTTV requires that the header of
1377 * the last sub-buffer contain a valid end time for the trace. This is done
1378 * automatically only when the trace is properly stopped.
1380 * If the traced program crashed, it is always possible to manually add the
1381 * right value in the header, or to open the trace in text mode.
1383 * FIXME: Fix LTTV so it doesn't need this.
1386 static void destroy_traces(void)
1390 /* if trace running, finish it */
1392 DBG("destructor stopping traces");
1394 result
= ltt_trace_stop("auto");
1396 ERR("ltt_trace_stop error");
1399 result
= ltt_trace_destroy("auto", 0);
1401 ERR("ltt_trace_destroy error");
1405 static int trace_recording(void)
1408 struct ust_trace
*trace
;
1412 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1419 ltt_unlock_traces();
1425 static int have_consumer(void)
1427 return !list_empty(&blocked_consumers
);
1431 int restarting_usleep(useconds_t usecs
)
1437 tv
.tv_nsec
= usecs
* 1000;
1440 result
= nanosleep(&tv
, &tv
);
1441 } while(result
== -1 && errno
== EINTR
);
1446 static void stop_listener(void)
1453 result
= pthread_cancel(listener_thread
);
1455 ERR("pthread_cancel: %s", strerror(result
));
1457 result
= pthread_join(listener_thread
, NULL
);
1459 ERR("pthread_join: %s", strerror(result
));
1463 /* This destructor keeps the process alive for a few seconds in order
1464 * to leave time to ustd to connect to its buffers. This is necessary
1465 * for programs whose execution is very short. It is also useful in all
1466 * programs when tracing is started close to the end of the program
1469 * FIXME: For now, this only works for the first trace created in a
1473 static void __attribute__((destructor
)) keepalive()
1475 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1477 DBG("Keeping process alive for consumer daemon...");
1478 while(LOAD_SHARED(buffers_to_export
)) {
1479 const int interv
= 200000;
1480 restarting_usleep(interv
);
1483 if(total
>= 3000000) {
1484 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1488 DBG("Finally dying...");
1493 /* Ask the listener to stop and clean up. */
1497 void ust_potential_exec(void)
1499 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1506 /* Notify ust that there was a fork. This needs to be called inside
1507 * the new process, anytime a process whose memory is not shared with
1508 * the parent is created. If this function is not called, the events
1509 * of the new process will not be collected.
1511 * Signals should be disabled before the fork and reenabled only after
1512 * this call in order to guarantee tracing is not started before ust_fork()
1513 * sanitizes the new process.
1516 static void ust_fork(void)
1518 struct blocked_consumer
*bc
;
1519 struct blocked_consumer
*deletable_bc
= NULL
;
1522 /* FIXME: technically, the locks could have been taken before the fork */
1523 DBG("ust: forking");
1525 /* break lock if necessary */
1526 ltt_unlock_traces();
1528 ltt_trace_stop("auto");
1529 ltt_trace_destroy("auto", 1);
1530 /* Delete all active connections */
1531 ustcomm_close_all_connections(&ustcomm_app
.server
);
1533 /* Delete all blocked consumers */
1534 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1535 result
= close(bc
->fd_producer
);
1541 list_del(&bc
->list
);
1544 /* free app, keeping socket file */
1545 ustcomm_fini_app(&ustcomm_app
, 1);
1547 STORE_SHARED(buffers_to_export
, 0);
1551 ltt_trace_setup("auto");
1552 result
= ltt_trace_set_type("auto", "ustrelay");
1554 ERR("ltt_trace_set_type failed");
1558 ltt_trace_alloc("auto");
1559 ltt_trace_start("auto");
1560 inform_consumer_daemon("auto");
1563 void ust_before_fork(ust_fork_info_t
*fork_info
)
1565 /* Disable signals. This is to avoid that the child
1566 * intervenes before it is properly setup for tracing. It is
1567 * safer to disable all signals, because then we know we are not
1568 * breaking anything by restoring the original mask.
1574 - only do this if tracing is active
1577 /* Disable signals */
1578 sigfillset(&all_sigs
);
1579 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1581 PERROR("sigprocmask");
1586 /* Don't call this function directly in a traced program */
1587 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1591 /* Restore signals */
1592 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1594 PERROR("sigprocmask");
1599 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1601 /* Reenable signals */
1602 ust_after_fork_common(fork_info
);
1605 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1607 /* First sanitize the child */
1610 /* Then reenable interrupts */
1611 ust_after_fork_common(fork_info
);