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/epoll.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <urcu/uatomic_arch.h>
37 #include <urcu/list.h>
39 #include <ust/marker.h>
40 #include <ust/tracepoint.h>
41 #include <ust/tracectl.h>
46 #include "marker-control.h"
48 #define USTSIGNAL SIGIO
50 #define MAX_MSG_SIZE (100)
52 #define MSG_REGISTER_NOTIF 2
54 /* This should only be accessed by the constructor, before the creation
55 * of the listener, and then only by the listener.
60 static struct ustcomm_sock
*listen_sock
;
62 extern struct chan_info_struct chan_infos
[];
64 static struct list_head open_buffers_list
= LIST_HEAD_INIT(open_buffers_list
);
66 static struct list_head ust_socks
= LIST_HEAD_INIT(ust_socks
);
68 /* volatile because shared between the listener and the main thread */
69 int buffers_to_export
= 0;
71 static long long make_pidunique(void)
76 gettimeofday(&tv
, NULL
);
85 static void print_markers(FILE *fp
)
87 struct marker_iter iter
;
90 marker_iter_reset(&iter
);
91 marker_iter_start(&iter
);
94 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
97 (int)imv_read(iter
.marker
->state
),
99 iter
.marker
->location
);
100 marker_iter_next(&iter
);
105 static void print_trace_events(FILE *fp
)
107 struct trace_event_iter iter
;
110 trace_event_iter_reset(&iter
);
111 trace_event_iter_start(&iter
);
113 while (iter
.trace_event
) {
114 fprintf(fp
, "trace_event: %s\n", iter
.trace_event
->name
);
115 trace_event_iter_next(&iter
);
117 unlock_trace_events();
120 /* Ask the daemon to collect a trace called trace_name and being
121 * produced by this pid.
123 * The trace must be at least allocated. (It can also be started.)
124 * This is because _ltt_trace_find is used.
127 static void inform_consumer_daemon(const char *trace_name
)
130 struct ust_trace
*trace
;
131 pid_t pid
= getpid();
136 trace
= _ltt_trace_find(trace_name
);
138 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
142 for (i
=0; i
< trace
->nr_channels
; i
++) {
143 if (trace
->channels
[i
].request_collection
) {
144 /* iterate on all cpus */
145 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
147 if (asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
) < 0) {
148 ERR("inform_consumer_daemon : asprintf failed (%s_%d)",
149 trace
->channels
[i
].channel_name
, j
);
152 result
= ustcomm_request_consumer(pid
, buf
);
154 WARN("Failed to request collection for channel %s. Is the daemon available?",
155 trace
->channels
[i
].channel_name
);
156 /* continue even if fail */
159 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
168 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
172 sep
= rindex(channel_and_cpu
, '_');
175 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
180 if (asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
) < 0) {
181 ERR("seperate_channel_cpu : asprintf failed (%.*s)",
182 (int)(sep
-channel_and_cpu
), channel_and_cpu
);
187 static int do_cmd_get_shmid(const char *recvbuf
, int sock
)
190 struct ust_trace
*trace
;
191 char trace_name
[] = "auto";
193 char *channel_and_cpu
;
201 channel_and_cpu
= nth_token(recvbuf
, 1);
202 if (channel_and_cpu
== NULL
) {
203 ERR("cannot parse channel");
208 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
210 ERR("problem parsing channel name");
212 goto free_short_chan_name
;
216 trace
= _ltt_trace_find(trace_name
);
220 ERR("cannot find trace!");
222 goto free_short_chan_name
;
225 for (i
=0; i
<trace
->nr_channels
; i
++) {
226 struct ust_channel
*channel
= &trace
->channels
[i
];
227 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
229 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
232 // DBG("the shmid for the requested channel is %d", buf->shmid);
233 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
234 if (asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]) < 0) {
235 ERR("do_cmd_get_shmid : asprintf failed (%d %d)",
236 buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
238 goto free_short_chan_name
;
241 result
= ustcomm_send_reply(reply
, sock
);
243 ERR("ustcomm_send_reply failed");
246 goto free_short_chan_name
;
257 ERR("channel not found (%s)", channel_and_cpu
);
260 free_short_chan_name
:
267 static int do_cmd_get_n_subbufs(const char *recvbuf
, int sock
)
270 struct ust_trace
*trace
;
271 char trace_name
[] = "auto";
273 char *channel_and_cpu
;
279 DBG("get_n_subbufs");
281 channel_and_cpu
= nth_token(recvbuf
, 1);
282 if (channel_and_cpu
== NULL
) {
283 ERR("cannot parse channel");
288 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
290 ERR("problem parsing channel name");
292 goto free_short_chan_name
;
296 trace
= _ltt_trace_find(trace_name
);
300 ERR("cannot find trace!");
302 goto free_short_chan_name
;
305 for (i
=0; i
<trace
->nr_channels
; i
++) {
306 struct ust_channel
*channel
= &trace
->channels
[i
];
308 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
311 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
312 if (asprintf(&reply
, "%d", channel
->subbuf_cnt
) < 0) {
313 ERR("do_cmd_get_n_subbufs : asprintf failed (%d)",
314 channel
->subbuf_cnt
);
316 goto free_short_chan_name
;
319 result
= ustcomm_send_reply(reply
, sock
);
321 ERR("ustcomm_send_reply failed");
324 goto free_short_chan_name
;
333 ERR("unable to find channel");
336 free_short_chan_name
:
343 static int do_cmd_get_subbuf_size(const char *recvbuf
, int sock
)
346 struct ust_trace
*trace
;
347 char trace_name
[] = "auto";
349 char *channel_and_cpu
;
355 DBG("get_subbuf_size");
357 channel_and_cpu
= nth_token(recvbuf
, 1);
358 if (channel_and_cpu
== NULL
) {
359 ERR("cannot parse channel");
364 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
366 ERR("problem parsing channel name");
368 goto free_short_chan_name
;
372 trace
= _ltt_trace_find(trace_name
);
376 ERR("cannot find trace!");
378 goto free_short_chan_name
;
381 for (i
=0; i
<trace
->nr_channels
; i
++) {
382 struct ust_channel
*channel
= &trace
->channels
[i
];
384 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
387 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
388 if (asprintf(&reply
, "%zd", channel
->subbuf_size
) < 0) {
389 ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)",
390 channel
->subbuf_size
);
392 goto free_short_chan_name
;
395 result
= ustcomm_send_reply(reply
, sock
);
397 ERR("ustcomm_send_reply failed");
400 goto free_short_chan_name
;
409 ERR("unable to find channel");
412 free_short_chan_name
:
419 /* Return the power of two which is equal or higher to v */
421 static unsigned int pow2_higher_or_eq(unsigned int v
)
424 int retval
= 1<<(hb
-1);
432 static int do_cmd_set_subbuf_size(const char *recvbuf
, int sock
)
434 char *channel_slash_size
;
435 char *ch_name
= NULL
;
436 unsigned int size
, power
;
438 struct ust_trace
*trace
;
439 char trace_name
[] = "auto";
443 DBG("set_subbuf_size");
445 channel_slash_size
= nth_token(recvbuf
, 1);
446 sscanf(channel_slash_size
, "%a[^/]/%u", &ch_name
, &size
);
448 if (ch_name
== NULL
) {
449 ERR("cannot parse channel");
454 power
= pow2_higher_or_eq(size
);
455 power
= max_t(unsigned int, 2u, power
);
457 WARN("using the next power of two for buffer size = %u\n", power
);
460 trace
= _ltt_trace_find_setup(trace_name
);
462 ERR("cannot find trace!");
467 for (i
= 0; i
< trace
->nr_channels
; i
++) {
468 struct ust_channel
*channel
= &trace
->channels
[i
];
470 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
472 channel
->subbuf_size
= power
;
473 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
480 ERR("unable to find channel");
489 static int do_cmd_set_subbuf_num(const char *recvbuf
, int sock
)
491 char *channel_slash_num
;
492 char *ch_name
= NULL
;
495 struct ust_trace
*trace
;
496 char trace_name
[] = "auto";
500 DBG("set_subbuf_num");
502 channel_slash_num
= nth_token(recvbuf
, 1);
503 sscanf(channel_slash_num
, "%a[^/]/%u", &ch_name
, &num
);
505 if (ch_name
== NULL
) {
506 ERR("cannot parse channel");
511 ERR("subbuffer count should be greater than 2");
517 trace
= _ltt_trace_find_setup(trace_name
);
519 ERR("cannot find trace!");
524 for (i
= 0; i
< trace
->nr_channels
; i
++) {
525 struct ust_channel
*channel
= &trace
->channels
[i
];
527 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
529 channel
->subbuf_cnt
= num
;
530 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
537 ERR("unable to find channel");
546 static int do_cmd_get_subbuffer(const char *recvbuf
, int sock
)
548 int retval
= 0, found
= 0;;
549 int i
, ch_cpu
, result
;
550 long consumed_old
= 0;
551 struct ust_trace
*trace
;
552 char trace_name
[] = "auto";
553 char *channel_and_cpu
;
558 channel_and_cpu
= nth_token(recvbuf
, 1);
559 if(channel_and_cpu
== NULL
) {
560 ERR("cannot parse channel");
565 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
567 ERR("problem parsing channel name");
569 goto free_short_chan_name
;
573 trace
= _ltt_trace_find(trace_name
);
578 DBG("Cannot find trace. It was likely destroyed by the user.");
579 result
= ustcomm_send_reply("NOTFOUND", sock
);
581 ERR("ustcomm_send_reply failed");
589 for(i
=0; i
<trace
->nr_channels
; i
++) {
590 struct ust_channel
*channel
= &trace
->channels
[i
];
592 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
593 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
598 result
= ust_buffers_get_subbuf(buf
, &consumed_old
);
599 if(result
== -EAGAIN
) {
600 WARN("missed buffer?");
604 } else if (result
< 0) {
605 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
610 if (asprintf(&reply
, "%s %ld", "OK", consumed_old
) < 0) {
611 ERR("do_cmd_get_subbuffer: asprintf failed (OK %ld)",
617 result
= ustcomm_send_reply(reply
, sock
);
619 ERR("ustcomm_send_reply failed");
631 result
= ustcomm_send_reply("NOTFOUND", sock
);
633 ERR("ustcomm_send_reply failed");
638 ERR("unable to find channel");
644 free_short_chan_name
:
652 static int do_cmd_get_buffer_fd(const char *recvbuf
, int sock
)
655 struct ust_trace
*trace
;
656 char trace_name
[] = "auto";
658 char *channel_and_cpu
;
662 struct ustcomm_header header
;
664 DBG("get_buffer_fd");
666 channel_and_cpu
= nth_token(recvbuf
, 1);
667 if (channel_and_cpu
== NULL
) {
668 ERR("cannot parse channel");
673 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
675 ERR("problem parsing channel name");
677 goto free_short_chan_name
;
681 trace
= _ltt_trace_find(trace_name
);
686 DBG("Cannot find trace. It was likely destroyed by the user.");
687 result
= ustcomm_send_reply("NOTFOUND", sock
);
689 ERR("ustcomm_send_reply failed");
697 for (i
=0; i
<trace
->nr_channels
; i
++) {
698 struct ust_channel
*channel
= &trace
->channels
[i
];
700 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
701 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
706 header
.fd_included
= 1;
707 if (ustcomm_send_fd(sock
, &header
, NULL
,
708 &buf
->data_ready_fd_read
) <= 0) {
709 ERR("ustcomm_send_fd failed\n");
713 /* Being here is the proof the daemon has mapped the buffer in its
714 * memory. We may now decrement buffers_to_export.
716 if (uatomic_read(&buf
->consumed
) == 0) {
717 DBG("decrementing buffers_to_export");
718 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
721 /* The buffer has been exported, ergo, we can add it to the
722 * list of open buffers
724 list_add(&buf
->open_buffers_list
, &open_buffers_list
);
729 ERR("unable to find channel");
735 free_short_chan_name
:
742 static int do_cmd_put_subbuffer(const char *recvbuf
, int sock
)
745 struct ust_trace
*trace
;
746 char trace_name
[] = "auto";
748 char *channel_and_cpu
;
754 char *consumed_old_str
;
760 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
761 if (channel_and_cpu
== NULL
) {
762 ERR("cannot parse channel");
767 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
768 if (consumed_old_str
== NULL
) {
769 ERR("cannot parse consumed_old");
771 goto free_channel_and_cpu
;
773 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
774 if (*endptr
!= '\0') {
775 ERR("invalid value for consumed_old");
777 goto free_consumed_old_str
;
780 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
782 ERR("problem parsing channel name");
784 goto free_short_chan_name
;
788 trace
= _ltt_trace_find(trace_name
);
791 DBG("Cannot find trace. It was likely destroyed by the user.");
792 result
= ustcomm_send_reply("NOTFOUND", sock
);
794 ERR("ustcomm_send_reply failed");
802 for (i
=0; i
<trace
->nr_channels
; i
++) {
803 struct ust_channel
*channel
= &trace
->channels
[i
];
805 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
806 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
810 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
812 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
813 if (asprintf(&reply
, "%s", "ERROR") < 0) {
814 ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)");
819 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
820 if (asprintf(&reply
, "%s", "OK") < 0) {
821 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
827 result
= ustcomm_send_reply(reply
, sock
);
829 ERR("ustcomm_send_reply failed");
840 ERR("unable to find channel");
845 free_short_chan_name
:
847 free_consumed_old_str
:
848 free(consumed_old_str
);
849 free_channel_and_cpu
:
850 free(channel_and_cpu
);
856 static void listener_cleanup(void *ptr
)
858 ustcomm_del_named_sock(listen_sock
, 0);
861 static void do_cmd_force_switch()
863 struct ust_buffer
*buf
;
865 list_for_each_entry(buf
, &open_buffers_list
,
867 ltt_force_switch(buf
, FORCE_FLUSH
);
871 static int process_client_cmd(char *recvbuf
, int sock
)
874 char trace_name
[] = "auto";
875 char trace_type
[] = "ustrelay";
878 len
= strlen(recvbuf
);
880 if (!strcmp(recvbuf
, "print_markers")) {
881 print_markers(stderr
);
882 } else if (!strcmp(recvbuf
, "list_markers")) {
887 fp
= open_memstream(&ptr
, &size
);
891 result
= ustcomm_send_reply(ptr
, sock
);
894 } else if (!strcmp(recvbuf
, "print_trace_events")) {
895 print_trace_events(stderr
);
897 } else if (!strcmp(recvbuf
, "list_trace_events")) {
902 fp
= open_memstream(&ptr
, &size
);
904 ERR("opening memstream failed");
907 print_trace_events(fp
);
910 result
= ustcomm_send_reply(ptr
, sock
);
912 ERR("list_trace_events failed");
916 } else if (!strcmp(recvbuf
, "start")) {
917 /* start is an operation that setups the trace, allocates it and starts it */
918 result
= ltt_trace_setup(trace_name
);
920 ERR("ltt_trace_setup failed");
924 result
= ltt_trace_set_type(trace_name
, trace_type
);
926 ERR("ltt_trace_set_type failed");
930 result
= ltt_trace_alloc(trace_name
);
932 ERR("ltt_trace_alloc failed");
936 inform_consumer_daemon(trace_name
);
938 result
= ltt_trace_start(trace_name
);
940 ERR("ltt_trace_start failed");
943 } else if (!strcmp(recvbuf
, "trace_setup")) {
946 result
= ltt_trace_setup(trace_name
);
948 ERR("ltt_trace_setup failed");
952 result
= ltt_trace_set_type(trace_name
, trace_type
);
954 ERR("ltt_trace_set_type failed");
957 } else if (!strcmp(recvbuf
, "trace_alloc")) {
960 result
= ltt_trace_alloc(trace_name
);
962 ERR("ltt_trace_alloc failed");
965 inform_consumer_daemon(trace_name
);
966 } else if (!strcmp(recvbuf
, "trace_create")) {
969 result
= ltt_trace_setup(trace_name
);
971 ERR("ltt_trace_setup failed");
975 result
= ltt_trace_set_type(trace_name
, trace_type
);
977 ERR("ltt_trace_set_type failed");
980 } else if (!strcmp(recvbuf
, "trace_start")) {
983 result
= ltt_trace_alloc(trace_name
);
985 ERR("ltt_trace_alloc failed");
989 inform_consumer_daemon(trace_name
);
992 result
= ltt_trace_start(trace_name
);
994 ERR("ltt_trace_start failed");
997 } else if (!strcmp(recvbuf
, "trace_stop")) {
1000 result
= ltt_trace_stop(trace_name
);
1002 ERR("ltt_trace_stop failed");
1005 } else if (!strcmp(recvbuf
, "trace_destroy")) {
1007 DBG("trace destroy");
1009 result
= ltt_trace_destroy(trace_name
, 0);
1011 ERR("ltt_trace_destroy failed");
1014 } else if (nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
1015 do_cmd_get_shmid(recvbuf
, sock
);
1016 } else if (nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
1017 do_cmd_get_n_subbufs(recvbuf
, sock
);
1018 } else if (nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
1019 do_cmd_get_subbuf_size(recvbuf
, sock
);
1020 } else if (nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
1023 libfile
= nth_token(recvbuf
, 1);
1025 DBG("load_probe_lib loading %s", libfile
);
1028 } else if (nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
1029 do_cmd_get_subbuffer(recvbuf
, sock
);
1031 else if(nth_token_is(recvbuf
, "get_buffer_fd", 0) == 1) {
1032 do_cmd_get_buffer_fd(recvbuf
, sock
);
1034 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
1035 do_cmd_put_subbuffer(recvbuf
, sock
);
1036 } else if (nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1037 do_cmd_set_subbuf_size(recvbuf
, sock
);
1038 } else if (nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1039 do_cmd_set_subbuf_num(recvbuf
, sock
);
1040 } else if (nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1041 char *channel_slash_name
= nth_token(recvbuf
, 1);
1042 char *channel_name
= NULL
;
1043 char *marker_name
= NULL
;
1045 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1047 if (channel_name
== NULL
|| marker_name
== NULL
) {
1048 WARN("invalid marker name");
1054 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1056 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1061 } else if (nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1062 char *channel_slash_name
= nth_token(recvbuf
, 1);
1063 char *marker_name
= NULL
;
1064 char *channel_name
= NULL
;
1066 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1068 if (channel_name
== NULL
|| marker_name
== NULL
) {
1069 WARN("invalid marker name");
1075 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1077 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1082 } else if (nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1085 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1086 ERR("process_client_cmd : asprintf failed (%lld)",
1091 result
= ustcomm_send_reply(reply
, sock
);
1093 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1098 } else if (nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1099 char *reply
= getenv("UST_DAEMON_SOCKET");
1101 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1102 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1106 result
= ustcomm_send_reply(reply
, sock
);
1109 result
= ustcomm_send_reply(reply
, sock
);
1112 ERR("ustcomm_send_reply failed");
1113 } else if (nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1114 char *sock_path
= nth_token(recvbuf
, 1);
1115 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1117 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1118 } else if (nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1119 do_cmd_force_switch();
1121 ERR("unable to parse message: %s", recvbuf
);
1129 #define MAX_EVENTS 10
1131 void *listener_main(void *p
)
1133 struct ustcomm_sock
*epoll_sock
;
1134 struct epoll_event events
[MAX_EVENTS
];
1135 struct sockaddr addr
;
1136 int accept_fd
, nfds
, result
, i
, addr_size
;
1140 pthread_cleanup_push(listener_cleanup
, NULL
);
1143 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1145 PERROR("listener_main: epoll_wait failed");
1149 for (i
= 0; i
< nfds
; i
++) {
1150 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1151 if (epoll_sock
== listen_sock
) {
1152 addr_size
= sizeof(struct sockaddr
);
1153 accept_fd
= accept(epoll_sock
->fd
,
1155 (socklen_t
*)&addr_size
);
1156 if (accept_fd
== -1) {
1157 PERROR("listener_main: accept failed");
1160 ustcomm_init_sock(accept_fd
, epoll_fd
,
1164 result
= recv_message_conn(epoll_sock
->fd
, &msg
);
1166 ustcomm_del_sock(epoll_sock
, 0);
1168 process_client_cmd(msg
, epoll_sock
->fd
);
1175 pthread_cleanup_pop(1);
1178 /* These should only be accessed in the parent thread,
1181 static volatile sig_atomic_t have_listener
= 0;
1182 static pthread_t listener_thread
;
1184 void create_listener(void)
1187 sigset_t sig_all_blocked
;
1188 sigset_t orig_parent_mask
;
1190 if (have_listener
) {
1191 WARN("not creating listener because we already had one");
1195 /* A new thread created by pthread_create inherits the signal mask
1196 * from the parent. To avoid any signal being received by the
1197 * listener thread, we block all signals temporarily in the parent,
1198 * while we create the listener thread.
1201 sigfillset(&sig_all_blocked
);
1203 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1205 PERROR("pthread_sigmask: %s", strerror(result
));
1208 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1210 PERROR("pthread_create");
1213 /* Restore original signal mask in parent */
1214 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1216 PERROR("pthread_sigmask: %s", strerror(result
));
1222 #define AUTOPROBE_DISABLED 0
1223 #define AUTOPROBE_ENABLE_ALL 1
1224 #define AUTOPROBE_ENABLE_REGEX 2
1225 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1226 static regex_t autoprobe_regex
;
1228 static void auto_probe_connect(struct marker
*m
)
1232 char* concat_name
= NULL
;
1233 const char *probe_name
= "default";
1235 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1237 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1238 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1240 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1241 m
->channel
, m
->name
);
1244 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1251 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1252 if (result
&& result
!= -EEXIST
)
1253 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1255 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1259 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1263 struct ustcomm_sock
*sock
;
1265 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1267 ERR("string overflow allocating socket name, "
1268 "UST thread bailing");
1272 result
= ensure_dir_exists(SOCK_DIR
);
1274 ERR("Unable to create socket directory %s, UST thread bailing",
1279 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1281 ERR("Error initializing named socket (%s). Check that directory"
1282 "exists and that it is writable. UST thread bailing", name
);
1294 static void __attribute__((constructor
)) init()
1297 char* autoprobe_val
= NULL
;
1298 char* subbuffer_size_val
= NULL
;
1299 char* subbuffer_count_val
= NULL
;
1300 unsigned int subbuffer_size
;
1301 unsigned int subbuffer_count
;
1304 /* Assign the pidunique, to be able to differentiate the processes with same
1305 * pid, (before and after an exec).
1307 pidunique
= make_pidunique();
1309 DBG("Tracectl constructor");
1312 epoll_fd
= epoll_create(MAX_EVENTS
);
1313 if (epoll_fd
== -1) {
1314 ERR("epoll_create failed, tracing shutting down");
1318 /* Create the socket */
1319 listen_sock
= init_app_socket(epoll_fd
);
1321 ERR("failed to create application socket,"
1322 " tracing shutting down");
1328 autoprobe_val
= getenv("UST_AUTOPROBE");
1329 if (autoprobe_val
) {
1330 struct marker_iter iter
;
1332 DBG("Autoprobe enabled.");
1334 /* Ensure markers are initialized */
1337 /* Ensure marker control is initialized, for the probe */
1338 init_marker_control();
1340 /* first, set the callback that will connect the
1341 * probe on new markers
1343 if (autoprobe_val
[0] == '/') {
1344 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1348 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1349 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1350 /* don't crash the application just for this */
1352 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1355 /* just enable all instrumentation */
1356 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1359 marker_set_new_marker_cb(auto_probe_connect
);
1361 /* Now, connect the probes that were already registered. */
1362 marker_iter_reset(&iter
);
1363 marker_iter_start(&iter
);
1365 DBG("now iterating on markers already registered");
1366 while (iter
.marker
) {
1367 DBG("now iterating on marker %s", iter
.marker
->name
);
1368 auto_probe_connect(iter
.marker
);
1369 marker_iter_next(&iter
);
1373 if (getenv("UST_OVERWRITE")) {
1374 int val
= atoi(getenv("UST_OVERWRITE"));
1375 if (val
== 0 || val
== 1) {
1376 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1378 WARN("invalid value for UST_OVERWRITE");
1382 if (getenv("UST_AUTOCOLLECT")) {
1383 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1384 if (val
== 0 || val
== 1) {
1385 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1387 WARN("invalid value for UST_AUTOCOLLECT");
1391 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1392 if (subbuffer_size_val
) {
1393 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1394 power
= pow2_higher_or_eq(subbuffer_size
);
1395 if (power
!= subbuffer_size
)
1396 WARN("using the next power of two for buffer size = %u\n", power
);
1397 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1400 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1401 if (subbuffer_count_val
) {
1402 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1403 if (subbuffer_count
< 2)
1404 subbuffer_count
= 2;
1405 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1408 if (getenv("UST_TRACE")) {
1409 char trace_name
[] = "auto";
1410 char trace_type
[] = "ustrelay";
1412 DBG("starting early tracing");
1414 /* Ensure marker control is initialized */
1415 init_marker_control();
1417 /* Ensure markers are initialized */
1420 /* Ensure buffers are initialized, for the transport to be available.
1421 * We are about to set a trace type and it will fail without this.
1423 init_ustrelay_transport();
1425 /* FIXME: When starting early tracing (here), depending on the
1426 * order of constructors, it is very well possible some marker
1427 * sections are not yet registered. Because of this, some
1428 * channels may not be registered. Yet, we are about to ask the
1429 * daemon to collect the channels. Channels which are not yet
1430 * registered will not be collected.
1432 * Currently, in LTTng, there is no way to add a channel after
1433 * trace start. The reason for this is that it induces complex
1434 * concurrency issues on the trace structures, which can only
1435 * be resolved using RCU. This has not been done yet. As a
1436 * workaround, we are forcing the registration of the "ust"
1437 * channel here. This is the only channel (apart from metadata)
1438 * that can be reliably used in early tracing.
1440 * Non-early tracing does not have this problem and can use
1441 * arbitrary channel names.
1443 ltt_channels_register("ust");
1445 result
= ltt_trace_setup(trace_name
);
1447 ERR("ltt_trace_setup failed");
1451 result
= ltt_trace_set_type(trace_name
, trace_type
);
1453 ERR("ltt_trace_set_type failed");
1457 result
= ltt_trace_alloc(trace_name
);
1459 ERR("ltt_trace_alloc failed");
1463 result
= ltt_trace_start(trace_name
);
1465 ERR("ltt_trace_start failed");
1469 /* Do this after the trace is started in order to avoid creating confusion
1470 * if the trace fails to start. */
1471 inform_consumer_daemon(trace_name
);
1476 /* should decrementally destroy stuff if error */
1480 /* This is only called if we terminate normally, not with an unhandled signal,
1481 * so we cannot rely on it. However, for now, LTTV requires that the header of
1482 * the last sub-buffer contain a valid end time for the trace. This is done
1483 * automatically only when the trace is properly stopped.
1485 * If the traced program crashed, it is always possible to manually add the
1486 * right value in the header, or to open the trace in text mode.
1488 * FIXME: Fix LTTV so it doesn't need this.
1491 static void destroy_traces(void)
1495 /* if trace running, finish it */
1497 DBG("destructor stopping traces");
1499 result
= ltt_trace_stop("auto");
1501 ERR("ltt_trace_stop error");
1504 result
= ltt_trace_destroy("auto", 0);
1506 ERR("ltt_trace_destroy error");
1510 static int trace_recording(void)
1513 struct ust_trace
*trace
;
1517 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1518 if (trace
->active
) {
1524 ltt_unlock_traces();
1529 int restarting_usleep(useconds_t usecs
)
1535 tv
.tv_nsec
= usecs
* 1000;
1538 result
= nanosleep(&tv
, &tv
);
1539 } while (result
== -1 && errno
== EINTR
);
1544 static void stop_listener(void)
1551 result
= pthread_cancel(listener_thread
);
1553 ERR("pthread_cancel: %s", strerror(result
));
1555 result
= pthread_join(listener_thread
, NULL
);
1557 ERR("pthread_join: %s", strerror(result
));
1561 /* This destructor keeps the process alive for a few seconds in order
1562 * to leave time to ustd to connect to its buffers. This is necessary
1563 * for programs whose execution is very short. It is also useful in all
1564 * programs when tracing is started close to the end of the program
1567 * FIXME: For now, this only works for the first trace created in a
1571 static void __attribute__((destructor
)) keepalive()
1573 if (trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1575 DBG("Keeping process alive for consumer daemon...");
1576 while (LOAD_SHARED(buffers_to_export
)) {
1577 const int interv
= 200000;
1578 restarting_usleep(interv
);
1581 if (total
>= 3000000) {
1582 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1586 DBG("Finally dying...");
1591 /* Ask the listener to stop and clean up. */
1595 void ust_potential_exec(void)
1597 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1604 /* Notify ust that there was a fork. This needs to be called inside
1605 * the new process, anytime a process whose memory is not shared with
1606 * the parent is created. If this function is not called, the events
1607 * of the new process will not be collected.
1609 * Signals should be disabled before the fork and reenabled only after
1610 * this call in order to guarantee tracing is not started before ust_fork()
1611 * sanitizes the new process.
1614 static void ust_fork(void)
1616 struct ust_buffer
*buf
, *buf_tmp
;
1617 struct ustcomm_sock
*sock
, *sock_tmp
;
1620 /* FIXME: technically, the locks could have been taken before the fork */
1621 DBG("ust: forking");
1623 /* break lock if necessary */
1624 ltt_unlock_traces();
1626 ltt_trace_stop("auto");
1627 ltt_trace_destroy("auto", 1);
1628 /* Delete all active connections, but leave them in the epoll set */
1629 list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1630 ustcomm_del_sock(sock
, 1);
1633 /* Delete all blocked consumers */
1634 list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1635 open_buffers_list
) {
1636 result
= close(buf
->data_ready_fd_read
);
1640 result
= close(buf
->data_ready_fd_write
);
1644 list_del(&buf
->open_buffers_list
);
1647 /* Clean up the listener socket and epoll, keeping the scoket file */
1648 ustcomm_del_named_sock(listen_sock
, 1);
1651 /* Re-start the launch sequence */
1652 STORE_SHARED(buffers_to_export
, 0);
1656 epoll_fd
= epoll_create(MAX_EVENTS
);
1657 if (epoll_fd
== -1) {
1658 ERR("epoll_create failed, tracing shutting down");
1662 /* Create the socket */
1663 listen_sock
= init_app_socket(epoll_fd
);
1665 ERR("failed to create application socket,"
1666 " tracing shutting down");
1670 ltt_trace_setup("auto");
1671 result
= ltt_trace_set_type("auto", "ustrelay");
1673 ERR("ltt_trace_set_type failed");
1677 ltt_trace_alloc("auto");
1678 ltt_trace_start("auto");
1679 inform_consumer_daemon("auto");
1682 void ust_before_fork(ust_fork_info_t
*fork_info
)
1684 /* Disable signals. This is to avoid that the child
1685 * intervenes before it is properly setup for tracing. It is
1686 * safer to disable all signals, because then we know we are not
1687 * breaking anything by restoring the original mask.
1693 - only do this if tracing is active
1696 /* Disable signals */
1697 sigfillset(&all_sigs
);
1698 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1700 PERROR("sigprocmask");
1705 /* Don't call this function directly in a traced program */
1706 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1710 /* Restore signals */
1711 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1713 PERROR("sigprocmask");
1718 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1720 /* Reenable signals */
1721 ust_after_fork_common(fork_info
);
1724 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1726 /* First sanitize the child */
1729 /* Then reenable interrupts */
1730 ust_after_fork_common(fork_info
);