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/tracepoint.h>
39 #include <ust/tracectl.h>
44 #include "marker-control.h"
45 #include "multipoll.h"
47 #define USTSIGNAL SIGIO
49 #define MAX_MSG_SIZE (100)
51 #define MSG_REGISTER_NOTIF 2
53 /* This should only be accessed by the constructor, before the creation
54 * of the listener, and then only by the listener.
58 extern struct chan_info_struct chan_infos
[];
60 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
62 static struct ustcomm_app ustcomm_app
;
64 struct tracecmd
{ /* no padding */
69 /* volatile because shared between the listener and the main thread */
70 int buffers_to_export
= 0;
73 /* size: the size of all the fields except size itself */
76 /* Only the necessary part of the payload is transferred. It
77 * may even be none of it.
82 struct consumer_channel
{
84 struct ltt_channel_struct
*chan
;
87 struct blocked_consumer
{
92 /* args to ustcomm_send_reply */
93 struct ustcomm_server server
;
94 struct ustcomm_source src
;
96 /* args to ust_buffers_get_subbuf */
97 struct ust_buffer
*buf
;
99 struct list_head list
;
102 static long long make_pidunique(void)
107 gettimeofday(&tv
, NULL
);
111 retval
|= tv
.tv_usec
;
116 static void print_markers(FILE *fp
)
118 struct marker_iter iter
;
121 marker_iter_reset(&iter
);
122 marker_iter_start(&iter
);
124 while (iter
.marker
) {
125 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
);
126 marker_iter_next(&iter
);
131 static void print_trace_events(FILE *fp
)
133 struct trace_event_iter iter
;
136 trace_event_iter_reset(&iter
);
137 trace_event_iter_start(&iter
);
139 while (iter
.trace_event
) {
140 fprintf(fp
, "trace_event: %s\n", iter
.trace_event
->name
);
141 trace_event_iter_next(&iter
);
143 unlock_trace_events();
146 static int init_socket(void);
148 /* Ask the daemon to collect a trace called trace_name and being
149 * produced by this pid.
151 * The trace must be at least allocated. (It can also be started.)
152 * This is because _ltt_trace_find is used.
155 static void inform_consumer_daemon(const char *trace_name
)
158 struct ust_trace
*trace
;
159 pid_t pid
= getpid();
164 trace
= _ltt_trace_find(trace_name
);
166 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
170 for (i
=0; i
< trace
->nr_channels
; i
++) {
171 if (trace
->channels
[i
].request_collection
) {
172 /* iterate on all cpus */
173 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
175 if (asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
) < 0) {
176 ERR("inform_consumer_daemon : asprintf failed (%s_%d)",
177 trace
->channels
[i
].channel_name
, j
);
180 result
= ustcomm_request_consumer(pid
, buf
);
182 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
183 /* continue even if fail */
186 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
195 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
198 long consumed_old
= 0;
200 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
203 result
= read(bc
->fd_producer
, &inbuf
, 1);
210 DBG("listener: got messsage that a buffer ended");
212 res
= close(bc
->fd_producer
);
219 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
221 ERR("ustcomm_send_reply failed");
228 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
229 if (result
== -EAGAIN
) {
230 WARN("missed buffer?");
232 } else if (result
< 0) {
233 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
235 if (asprintf(&reply
, "%s %ld", "OK", consumed_old
) < 0) {
236 ERR("process_blkd_consumer_act : asprintf failed (OK %ld)",
240 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
242 ERR("ustcomm_send_reply failed");
253 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
255 struct blocked_consumer
*bc
;
257 list_for_each_entry(bc
, &blocked_consumers
, list
) {
258 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
263 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
267 sep
= rindex(channel_and_cpu
, '_');
270 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
275 if (asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
) < 0) {
276 ERR("seperate_channel_cpu : asprintf failed (%.*s)",
277 (int)(sep
-channel_and_cpu
), channel_and_cpu
);
282 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
285 struct ust_trace
*trace
;
286 char trace_name
[] = "auto";
288 char *channel_and_cpu
;
296 channel_and_cpu
= nth_token(recvbuf
, 1);
297 if (channel_and_cpu
== NULL
) {
298 ERR("cannot parse channel");
303 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
305 ERR("problem parsing channel name");
307 goto free_short_chan_name
;
311 trace
= _ltt_trace_find(trace_name
);
315 ERR("cannot find trace!");
317 goto free_short_chan_name
;
320 for (i
=0; i
<trace
->nr_channels
; i
++) {
321 struct ust_channel
*channel
= &trace
->channels
[i
];
322 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
324 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
327 // DBG("the shmid for the requested channel is %d", buf->shmid);
328 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
329 if (asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]) < 0) {
330 ERR("do_cmd_get_shmid : asprintf failed (%d %d)",
331 buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
333 goto free_short_chan_name
;
336 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
338 ERR("ustcomm_send_reply failed");
341 goto free_short_chan_name
;
352 ERR("channel not found (%s)", channel_and_cpu
);
355 free_short_chan_name
:
362 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
365 struct ust_trace
*trace
;
366 char trace_name
[] = "auto";
368 char *channel_and_cpu
;
374 DBG("get_n_subbufs");
376 channel_and_cpu
= nth_token(recvbuf
, 1);
377 if (channel_and_cpu
== NULL
) {
378 ERR("cannot parse channel");
383 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
385 ERR("problem parsing channel name");
387 goto free_short_chan_name
;
391 trace
= _ltt_trace_find(trace_name
);
395 ERR("cannot find trace!");
397 goto free_short_chan_name
;
400 for (i
=0; i
<trace
->nr_channels
; i
++) {
401 struct ust_channel
*channel
= &trace
->channels
[i
];
403 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
406 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
407 if (asprintf(&reply
, "%d", channel
->subbuf_cnt
) < 0) {
408 ERR("do_cmd_get_n_subbufs : asprintf failed (%d)",
409 channel
->subbuf_cnt
);
411 goto free_short_chan_name
;
414 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
416 ERR("ustcomm_send_reply failed");
419 goto free_short_chan_name
;
428 ERR("unable to find channel");
431 free_short_chan_name
:
438 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
441 struct ust_trace
*trace
;
442 char trace_name
[] = "auto";
444 char *channel_and_cpu
;
450 DBG("get_subbuf_size");
452 channel_and_cpu
= nth_token(recvbuf
, 1);
453 if (channel_and_cpu
== NULL
) {
454 ERR("cannot parse channel");
459 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
461 ERR("problem parsing channel name");
463 goto free_short_chan_name
;
467 trace
= _ltt_trace_find(trace_name
);
471 ERR("cannot find trace!");
473 goto free_short_chan_name
;
476 for (i
=0; i
<trace
->nr_channels
; i
++) {
477 struct ust_channel
*channel
= &trace
->channels
[i
];
479 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
482 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
483 if (asprintf(&reply
, "%zd", channel
->subbuf_size
) < 0) {
484 ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)",
485 channel
->subbuf_size
);
487 goto free_short_chan_name
;
490 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
492 ERR("ustcomm_send_reply failed");
495 goto free_short_chan_name
;
504 ERR("unable to find channel");
507 free_short_chan_name
:
514 /* Return the power of two which is equal or higher to v */
516 static unsigned int pow2_higher_or_eq(unsigned int v
)
519 int retval
= 1<<(hb
-1);
527 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
529 char *channel_slash_size
;
530 char *ch_name
= NULL
;
531 unsigned int size
, power
;
533 struct ust_trace
*trace
;
534 char trace_name
[] = "auto";
538 DBG("set_subbuf_size");
540 channel_slash_size
= nth_token(recvbuf
, 1);
541 sscanf(channel_slash_size
, "%a[^/]/%u", &ch_name
, &size
);
543 if (ch_name
== NULL
) {
544 ERR("cannot parse channel");
549 power
= pow2_higher_or_eq(size
);
550 power
= max_t(unsigned int, 2u, power
);
552 WARN("using the next power of two for buffer size = %u\n", power
);
555 trace
= _ltt_trace_find_setup(trace_name
);
557 ERR("cannot find trace!");
562 for (i
= 0; i
< trace
->nr_channels
; i
++) {
563 struct ust_channel
*channel
= &trace
->channels
[i
];
565 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
567 channel
->subbuf_size
= power
;
568 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
575 ERR("unable to find channel");
584 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
586 char *channel_slash_num
;
587 char *ch_name
= NULL
;
590 struct ust_trace
*trace
;
591 char trace_name
[] = "auto";
595 DBG("set_subbuf_num");
597 channel_slash_num
= nth_token(recvbuf
, 1);
598 sscanf(channel_slash_num
, "%a[^/]/%u", &ch_name
, &num
);
600 if (ch_name
== NULL
) {
601 ERR("cannot parse channel");
606 ERR("subbuffer count should be greater than 2");
612 trace
= _ltt_trace_find_setup(trace_name
);
614 ERR("cannot find trace!");
619 for (i
= 0; i
< trace
->nr_channels
; i
++) {
620 struct ust_channel
*channel
= &trace
->channels
[i
];
622 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
624 channel
->subbuf_cnt
= num
;
625 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
632 ERR("unable to find channel");
641 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
644 struct ust_trace
*trace
;
645 char trace_name
[] = "auto";
647 char *channel_and_cpu
;
654 channel_and_cpu
= nth_token(recvbuf
, 1);
655 if (channel_and_cpu
== NULL
) {
656 ERR("cannot parse channel");
661 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
663 ERR("problem parsing channel name");
665 goto free_short_chan_name
;
669 trace
= _ltt_trace_find(trace_name
);
674 DBG("Cannot find trace. It was likely destroyed by the user.");
675 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
677 ERR("ustcomm_send_reply failed");
685 for (i
=0; i
<trace
->nr_channels
; i
++) {
686 struct ust_channel
*channel
= &trace
->channels
[i
];
688 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
689 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
690 struct blocked_consumer
*bc
;
694 bc
= (struct blocked_consumer
*) zmalloc(sizeof(struct blocked_consumer
));
696 ERR("zmalloc returned NULL");
699 bc
->fd_consumer
= src
->fd
;
700 bc
->fd_producer
= buf
->data_ready_fd_read
;
703 bc
->server
= ustcomm_app
.server
;
705 list_add(&bc
->list
, &blocked_consumers
);
707 /* Being here is the proof the daemon has mapped the buffer in its
708 * memory. We may now decrement buffers_to_export.
710 if (uatomic_read(&buf
->consumed
) == 0) {
711 DBG("decrementing buffers_to_export");
712 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
719 ERR("unable to find channel");
725 free_short_chan_name
:
732 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
735 struct ust_trace
*trace
;
736 char trace_name
[] = "auto";
738 char *channel_and_cpu
;
744 char *consumed_old_str
;
750 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
751 if (channel_and_cpu
== NULL
) {
752 ERR("cannot parse channel");
757 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
758 if (consumed_old_str
== NULL
) {
759 ERR("cannot parse consumed_old");
761 goto free_channel_and_cpu
;
763 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
764 if (*endptr
!= '\0') {
765 ERR("invalid value for consumed_old");
767 goto free_consumed_old_str
;
770 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
772 ERR("problem parsing channel name");
774 goto free_short_chan_name
;
778 trace
= _ltt_trace_find(trace_name
);
781 DBG("Cannot find trace. It was likely destroyed by the user.");
782 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
784 ERR("ustcomm_send_reply failed");
792 for (i
=0; i
<trace
->nr_channels
; i
++) {
793 struct ust_channel
*channel
= &trace
->channels
[i
];
795 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
796 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
800 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
802 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
803 if (asprintf(&reply
, "%s", "ERROR") < 0) {
804 ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)");
809 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
810 if (asprintf(&reply
, "%s", "OK") < 0) {
811 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
817 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
819 ERR("ustcomm_send_reply failed");
830 ERR("unable to find channel");
835 free_short_chan_name
:
837 free_consumed_old_str
:
838 free(consumed_old_str
);
839 free_channel_and_cpu
:
840 free(channel_and_cpu
);
846 static void listener_cleanup(void *ptr
)
848 ustcomm_fini_app(&ustcomm_app
, 0);
851 static void do_cmd_force_switch()
853 struct blocked_consumer
*bc
;
855 list_for_each_entry(bc
, &blocked_consumers
, list
) {
856 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
860 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
863 char trace_name
[] = "auto";
864 char trace_type
[] = "ustrelay";
867 DBG("received a message! it's: %s", recvbuf
);
868 len
= strlen(recvbuf
);
870 if (!strcmp(recvbuf
, "print_markers")) {
871 print_markers(stderr
);
872 } else if (!strcmp(recvbuf
, "list_markers")) {
877 fp
= open_memstream(&ptr
, &size
);
881 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
884 } else if (!strcmp(recvbuf
, "print_trace_events")) {
885 print_trace_events(stderr
);
887 } else if (!strcmp(recvbuf
, "list_trace_events")) {
892 fp
= open_memstream(&ptr
, &size
);
894 ERR("opening memstream failed");
897 print_trace_events(fp
);
900 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
902 ERR("list_trace_events failed");
906 } else if (!strcmp(recvbuf
, "start")) {
907 /* start is an operation that setups the trace, allocates it and starts it */
908 result
= ltt_trace_setup(trace_name
);
910 ERR("ltt_trace_setup failed");
914 result
= ltt_trace_set_type(trace_name
, trace_type
);
916 ERR("ltt_trace_set_type failed");
920 result
= ltt_trace_alloc(trace_name
);
922 ERR("ltt_trace_alloc failed");
926 inform_consumer_daemon(trace_name
);
928 result
= ltt_trace_start(trace_name
);
930 ERR("ltt_trace_start failed");
933 } else if (!strcmp(recvbuf
, "trace_setup")) {
936 result
= ltt_trace_setup(trace_name
);
938 ERR("ltt_trace_setup failed");
942 result
= ltt_trace_set_type(trace_name
, trace_type
);
944 ERR("ltt_trace_set_type failed");
947 } else if (!strcmp(recvbuf
, "trace_alloc")) {
950 result
= ltt_trace_alloc(trace_name
);
952 ERR("ltt_trace_alloc failed");
955 inform_consumer_daemon(trace_name
);
956 } else if (!strcmp(recvbuf
, "trace_create")) {
959 result
= ltt_trace_setup(trace_name
);
961 ERR("ltt_trace_setup failed");
965 result
= ltt_trace_set_type(trace_name
, trace_type
);
967 ERR("ltt_trace_set_type failed");
970 } else if (!strcmp(recvbuf
, "trace_start")) {
973 result
= ltt_trace_alloc(trace_name
);
975 ERR("ltt_trace_alloc failed");
979 inform_consumer_daemon(trace_name
);
982 result
= ltt_trace_start(trace_name
);
984 ERR("ltt_trace_start failed");
987 } else if (!strcmp(recvbuf
, "trace_stop")) {
990 result
= ltt_trace_stop(trace_name
);
992 ERR("ltt_trace_stop failed");
995 } else if (!strcmp(recvbuf
, "trace_destroy")) {
997 DBG("trace destroy");
999 result
= ltt_trace_destroy(trace_name
, 0);
1001 ERR("ltt_trace_destroy failed");
1004 } else if (nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
1005 do_cmd_get_shmid(recvbuf
, src
);
1006 } else if (nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
1007 do_cmd_get_n_subbufs(recvbuf
, src
);
1008 } else if (nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
1009 do_cmd_get_subbuf_size(recvbuf
, src
);
1010 } else if (nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
1013 libfile
= nth_token(recvbuf
, 1);
1015 DBG("load_probe_lib loading %s", libfile
);
1018 } else if (nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
1019 do_cmd_get_subbuffer(recvbuf
, src
);
1020 } else if (nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
1021 do_cmd_put_subbuffer(recvbuf
, src
);
1022 } else if (nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1023 do_cmd_set_subbuf_size(recvbuf
, src
);
1024 } else if (nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1025 do_cmd_set_subbuf_num(recvbuf
, src
);
1026 } else if (nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1027 char *channel_slash_name
= nth_token(recvbuf
, 1);
1028 char *channel_name
= NULL
;
1029 char *marker_name
= NULL
;
1031 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1033 if (channel_name
== NULL
|| marker_name
== NULL
) {
1034 WARN("invalid marker name");
1040 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1042 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1047 } else if (nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1048 char *channel_slash_name
= nth_token(recvbuf
, 1);
1049 char *marker_name
= NULL
;
1050 char *channel_name
= NULL
;
1052 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1054 if (channel_name
== NULL
|| marker_name
== NULL
) {
1055 WARN("invalid marker name");
1061 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1063 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1068 } else if (nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1071 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1072 ERR("process_client_cmd : asprintf failed (%lld)",
1077 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1079 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1084 } else if (nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1085 char *reply
= getenv("UST_DAEMON_SOCKET");
1087 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1088 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1092 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1095 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1098 ERR("ustcomm_send_reply failed");
1099 } else if (nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1100 char *sock_path
= nth_token(recvbuf
, 1);
1101 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1103 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1104 } else if (nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1105 do_cmd_force_switch();
1107 ERR("unable to parse message: %s", recvbuf
);
1115 void *listener_main(void *p
)
1121 pthread_cleanup_push(listener_cleanup
, NULL
);
1124 struct mpentries mpent
;
1126 multipoll_init(&mpent
);
1128 blocked_consumers_add_to_mp(&mpent
);
1129 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1131 result
= multipoll_poll(&mpent
, -1);
1133 ERR("error in multipoll_poll");
1136 multipoll_destroy(&mpent
);
1139 pthread_cleanup_pop(1);
1142 /* These should only be accessed in the parent thread,
1145 static volatile sig_atomic_t have_listener
= 0;
1146 static pthread_t listener_thread
;
1148 void create_listener(void)
1151 sigset_t sig_all_blocked
;
1152 sigset_t orig_parent_mask
;
1154 if (have_listener
) {
1155 WARN("not creating listener because we already had one");
1159 /* A new thread created by pthread_create inherits the signal mask
1160 * from the parent. To avoid any signal being received by the
1161 * listener thread, we block all signals temporarily in the parent,
1162 * while we create the listener thread.
1165 sigfillset(&sig_all_blocked
);
1167 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1169 PERROR("pthread_sigmask: %s", strerror(result
));
1172 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1174 PERROR("pthread_create");
1177 /* Restore original signal mask in parent */
1178 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1180 PERROR("pthread_sigmask: %s", strerror(result
));
1186 static int init_socket(void)
1188 return ustcomm_init_app(getpid(), &ustcomm_app
);
1191 #define AUTOPROBE_DISABLED 0
1192 #define AUTOPROBE_ENABLE_ALL 1
1193 #define AUTOPROBE_ENABLE_REGEX 2
1194 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1195 static regex_t autoprobe_regex
;
1197 static void auto_probe_connect(struct marker
*m
)
1201 char* concat_name
= NULL
;
1202 const char *probe_name
= "default";
1204 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1206 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1207 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1209 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1210 m
->channel
, m
->name
);
1213 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1220 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1221 if (result
&& result
!= -EEXIST
)
1222 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1224 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1228 static void __attribute__((constructor
)) init()
1231 char* autoprobe_val
= NULL
;
1232 char* subbuffer_size_val
= NULL
;
1233 char* subbuffer_count_val
= NULL
;
1234 unsigned int subbuffer_size
;
1235 unsigned int subbuffer_count
;
1238 /* Assign the pidunique, to be able to differentiate the processes with same
1239 * pid, (before and after an exec).
1241 pidunique
= make_pidunique();
1243 DBG("Tracectl constructor");
1245 result
= init_socket();
1247 ERR("init_socket error");
1253 autoprobe_val
= getenv("UST_AUTOPROBE");
1254 if (autoprobe_val
) {
1255 struct marker_iter iter
;
1257 DBG("Autoprobe enabled.");
1259 /* Ensure markers are initialized */
1262 /* Ensure marker control is initialized, for the probe */
1263 init_marker_control();
1265 /* first, set the callback that will connect the
1266 * probe on new markers
1268 if (autoprobe_val
[0] == '/') {
1269 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1273 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1274 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1275 /* don't crash the application just for this */
1277 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1280 /* just enable all instrumentation */
1281 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1284 marker_set_new_marker_cb(auto_probe_connect
);
1286 /* Now, connect the probes that were already registered. */
1287 marker_iter_reset(&iter
);
1288 marker_iter_start(&iter
);
1290 DBG("now iterating on markers already registered");
1291 while (iter
.marker
) {
1292 DBG("now iterating on marker %s", iter
.marker
->name
);
1293 auto_probe_connect(iter
.marker
);
1294 marker_iter_next(&iter
);
1298 if (getenv("UST_OVERWRITE")) {
1299 int val
= atoi(getenv("UST_OVERWRITE"));
1300 if (val
== 0 || val
== 1) {
1301 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1303 WARN("invalid value for UST_OVERWRITE");
1307 if (getenv("UST_AUTOCOLLECT")) {
1308 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1309 if (val
== 0 || val
== 1) {
1310 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1312 WARN("invalid value for UST_AUTOCOLLECT");
1316 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1317 if (subbuffer_size_val
) {
1318 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1319 power
= pow2_higher_or_eq(subbuffer_size
);
1320 if (power
!= subbuffer_size
)
1321 WARN("using the next power of two for buffer size = %u\n", power
);
1322 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1325 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1326 if (subbuffer_count_val
) {
1327 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1328 if (subbuffer_count
< 2)
1329 subbuffer_count
= 2;
1330 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1333 if (getenv("UST_TRACE")) {
1334 char trace_name
[] = "auto";
1335 char trace_type
[] = "ustrelay";
1337 DBG("starting early tracing");
1339 /* Ensure marker control is initialized */
1340 init_marker_control();
1342 /* Ensure markers are initialized */
1345 /* Ensure buffers are initialized, for the transport to be available.
1346 * We are about to set a trace type and it will fail without this.
1348 init_ustrelay_transport();
1350 /* FIXME: When starting early tracing (here), depending on the
1351 * order of constructors, it is very well possible some marker
1352 * sections are not yet registered. Because of this, some
1353 * channels may not be registered. Yet, we are about to ask the
1354 * daemon to collect the channels. Channels which are not yet
1355 * registered will not be collected.
1357 * Currently, in LTTng, there is no way to add a channel after
1358 * trace start. The reason for this is that it induces complex
1359 * concurrency issues on the trace structures, which can only
1360 * be resolved using RCU. This has not been done yet. As a
1361 * workaround, we are forcing the registration of the "ust"
1362 * channel here. This is the only channel (apart from metadata)
1363 * that can be reliably used in early tracing.
1365 * Non-early tracing does not have this problem and can use
1366 * arbitrary channel names.
1368 ltt_channels_register("ust");
1370 result
= ltt_trace_setup(trace_name
);
1372 ERR("ltt_trace_setup failed");
1376 result
= ltt_trace_set_type(trace_name
, trace_type
);
1378 ERR("ltt_trace_set_type failed");
1382 result
= ltt_trace_alloc(trace_name
);
1384 ERR("ltt_trace_alloc failed");
1388 result
= ltt_trace_start(trace_name
);
1390 ERR("ltt_trace_start failed");
1394 /* Do this after the trace is started in order to avoid creating confusion
1395 * if the trace fails to start. */
1396 inform_consumer_daemon(trace_name
);
1401 /* should decrementally destroy stuff if error */
1405 /* This is only called if we terminate normally, not with an unhandled signal,
1406 * so we cannot rely on it. However, for now, LTTV requires that the header of
1407 * the last sub-buffer contain a valid end time for the trace. This is done
1408 * automatically only when the trace is properly stopped.
1410 * If the traced program crashed, it is always possible to manually add the
1411 * right value in the header, or to open the trace in text mode.
1413 * FIXME: Fix LTTV so it doesn't need this.
1416 static void destroy_traces(void)
1420 /* if trace running, finish it */
1422 DBG("destructor stopping traces");
1424 result
= ltt_trace_stop("auto");
1426 ERR("ltt_trace_stop error");
1429 result
= ltt_trace_destroy("auto", 0);
1431 ERR("ltt_trace_destroy error");
1435 static int trace_recording(void)
1438 struct ust_trace
*trace
;
1442 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1443 if (trace
->active
) {
1449 ltt_unlock_traces();
1455 static int have_consumer(void)
1457 return !list_empty(&blocked_consumers
);
1461 int restarting_usleep(useconds_t usecs
)
1467 tv
.tv_nsec
= usecs
* 1000;
1470 result
= nanosleep(&tv
, &tv
);
1471 } while (result
== -1 && errno
== EINTR
);
1476 static void stop_listener(void)
1483 result
= pthread_cancel(listener_thread
);
1485 ERR("pthread_cancel: %s", strerror(result
));
1487 result
= pthread_join(listener_thread
, NULL
);
1489 ERR("pthread_join: %s", strerror(result
));
1493 /* This destructor keeps the process alive for a few seconds in order
1494 * to leave time to ustd to connect to its buffers. This is necessary
1495 * for programs whose execution is very short. It is also useful in all
1496 * programs when tracing is started close to the end of the program
1499 * FIXME: For now, this only works for the first trace created in a
1503 static void __attribute__((destructor
)) keepalive()
1505 if (trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1507 DBG("Keeping process alive for consumer daemon...");
1508 while (LOAD_SHARED(buffers_to_export
)) {
1509 const int interv
= 200000;
1510 restarting_usleep(interv
);
1513 if (total
>= 3000000) {
1514 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1518 DBG("Finally dying...");
1523 /* Ask the listener to stop and clean up. */
1527 void ust_potential_exec(void)
1529 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1536 /* Notify ust that there was a fork. This needs to be called inside
1537 * the new process, anytime a process whose memory is not shared with
1538 * the parent is created. If this function is not called, the events
1539 * of the new process will not be collected.
1541 * Signals should be disabled before the fork and reenabled only after
1542 * this call in order to guarantee tracing is not started before ust_fork()
1543 * sanitizes the new process.
1546 static void ust_fork(void)
1548 struct blocked_consumer
*bc
;
1549 struct blocked_consumer
*deletable_bc
= NULL
;
1552 /* FIXME: technically, the locks could have been taken before the fork */
1553 DBG("ust: forking");
1555 /* break lock if necessary */
1556 ltt_unlock_traces();
1558 ltt_trace_stop("auto");
1559 ltt_trace_destroy("auto", 1);
1560 /* Delete all active connections */
1561 ustcomm_close_all_connections(&ustcomm_app
.server
);
1563 /* Delete all blocked consumers */
1564 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1565 result
= close(bc
->fd_producer
);
1571 list_del(&bc
->list
);
1574 /* free app, keeping socket file */
1575 ustcomm_fini_app(&ustcomm_app
, 1);
1577 STORE_SHARED(buffers_to_export
, 0);
1581 ltt_trace_setup("auto");
1582 result
= ltt_trace_set_type("auto", "ustrelay");
1584 ERR("ltt_trace_set_type failed");
1588 ltt_trace_alloc("auto");
1589 ltt_trace_start("auto");
1590 inform_consumer_daemon("auto");
1593 void ust_before_fork(ust_fork_info_t
*fork_info
)
1595 /* Disable signals. This is to avoid that the child
1596 * intervenes before it is properly setup for tracing. It is
1597 * safer to disable all signals, because then we know we are not
1598 * breaking anything by restoring the original mask.
1604 - only do this if tracing is active
1607 /* Disable signals */
1608 sigfillset(&all_sigs
);
1609 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1611 PERROR("sigprocmask");
1616 /* Don't call this function directly in a traced program */
1617 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1621 /* Restore signals */
1622 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1624 PERROR("sigprocmask");
1629 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1631 /* Reenable signals */
1632 ust_after_fork_common(fork_info
);
1635 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1637 /* First sanitize the child */
1640 /* Then reenable interrupts */
1641 ust_after_fork_common(fork_info
);