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
[256]="";
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
, "%255[^/]/%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");
583 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
585 char *channel_slash_num
;
586 char ch_name
[256]="";
589 struct ust_trace
*trace
;
590 char trace_name
[] = "auto";
594 DBG("set_subbuf_num");
596 channel_slash_num
= nth_token(recvbuf
, 1);
597 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
599 if (ch_name
== NULL
) {
600 ERR("cannot parse channel");
605 ERR("subbuffer count should be greater than 2");
611 trace
= _ltt_trace_find_setup(trace_name
);
613 ERR("cannot find trace!");
618 for (i
= 0; i
< trace
->nr_channels
; i
++) {
619 struct ust_channel
*channel
= &trace
->channels
[i
];
621 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
623 channel
->subbuf_cnt
= num
;
624 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
631 ERR("unable to find channel");
639 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
642 struct ust_trace
*trace
;
643 char trace_name
[] = "auto";
645 char *channel_and_cpu
;
652 channel_and_cpu
= nth_token(recvbuf
, 1);
653 if (channel_and_cpu
== NULL
) {
654 ERR("cannot parse channel");
659 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
661 ERR("problem parsing channel name");
663 goto free_short_chan_name
;
667 trace
= _ltt_trace_find(trace_name
);
672 DBG("Cannot find trace. It was likely destroyed by the user.");
673 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
675 ERR("ustcomm_send_reply failed");
683 for (i
=0; i
<trace
->nr_channels
; i
++) {
684 struct ust_channel
*channel
= &trace
->channels
[i
];
686 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
687 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
688 struct blocked_consumer
*bc
;
692 bc
= (struct blocked_consumer
*) zmalloc(sizeof(struct blocked_consumer
));
694 ERR("zmalloc returned NULL");
697 bc
->fd_consumer
= src
->fd
;
698 bc
->fd_producer
= buf
->data_ready_fd_read
;
701 bc
->server
= ustcomm_app
.server
;
703 list_add(&bc
->list
, &blocked_consumers
);
705 /* Being here is the proof the daemon has mapped the buffer in its
706 * memory. We may now decrement buffers_to_export.
708 if (uatomic_read(&buf
->consumed
) == 0) {
709 DBG("decrementing buffers_to_export");
710 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
717 ERR("unable to find channel");
723 free_short_chan_name
:
730 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
733 struct ust_trace
*trace
;
734 char trace_name
[] = "auto";
736 char *channel_and_cpu
;
742 char *consumed_old_str
;
748 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
749 if (channel_and_cpu
== NULL
) {
750 ERR("cannot parse channel");
755 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
756 if (consumed_old_str
== NULL
) {
757 ERR("cannot parse consumed_old");
759 goto free_channel_and_cpu
;
761 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
762 if (*endptr
!= '\0') {
763 ERR("invalid value for consumed_old");
765 goto free_consumed_old_str
;
768 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
770 ERR("problem parsing channel name");
772 goto free_short_chan_name
;
776 trace
= _ltt_trace_find(trace_name
);
779 DBG("Cannot find trace. It was likely destroyed by the user.");
780 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
782 ERR("ustcomm_send_reply failed");
790 for (i
=0; i
<trace
->nr_channels
; i
++) {
791 struct ust_channel
*channel
= &trace
->channels
[i
];
793 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
794 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
798 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
800 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
801 if (asprintf(&reply
, "%s", "ERROR") < 0) {
802 ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)");
807 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
808 if (asprintf(&reply
, "%s", "OK") < 0) {
809 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
815 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
817 ERR("ustcomm_send_reply failed");
828 ERR("unable to find channel");
833 free_short_chan_name
:
835 free_consumed_old_str
:
836 free(consumed_old_str
);
837 free_channel_and_cpu
:
838 free(channel_and_cpu
);
844 static void listener_cleanup(void *ptr
)
846 ustcomm_fini_app(&ustcomm_app
, 0);
849 static void do_cmd_force_switch()
851 struct blocked_consumer
*bc
;
853 list_for_each_entry(bc
, &blocked_consumers
, list
) {
854 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
858 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
861 char trace_name
[] = "auto";
862 char trace_type
[] = "ustrelay";
865 DBG("received a message! it's: %s", recvbuf
);
866 len
= strlen(recvbuf
);
868 if (!strcmp(recvbuf
, "print_markers")) {
869 print_markers(stderr
);
870 } else if (!strcmp(recvbuf
, "list_markers")) {
875 fp
= open_memstream(&ptr
, &size
);
879 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
882 } else if (!strcmp(recvbuf
, "print_trace_events")) {
883 print_trace_events(stderr
);
885 } else if (!strcmp(recvbuf
, "list_trace_events")) {
890 fp
= open_memstream(&ptr
, &size
);
892 ERR("opening memstream failed");
895 print_trace_events(fp
);
898 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
900 ERR("list_trace_events failed");
904 } else if (!strcmp(recvbuf
, "start")) {
905 /* start is an operation that setups the trace, allocates it and starts it */
906 result
= ltt_trace_setup(trace_name
);
908 ERR("ltt_trace_setup failed");
912 result
= ltt_trace_set_type(trace_name
, trace_type
);
914 ERR("ltt_trace_set_type failed");
918 result
= ltt_trace_alloc(trace_name
);
920 ERR("ltt_trace_alloc failed");
924 inform_consumer_daemon(trace_name
);
926 result
= ltt_trace_start(trace_name
);
928 ERR("ltt_trace_start failed");
931 } else if (!strcmp(recvbuf
, "trace_setup")) {
934 result
= ltt_trace_setup(trace_name
);
936 ERR("ltt_trace_setup failed");
940 result
= ltt_trace_set_type(trace_name
, trace_type
);
942 ERR("ltt_trace_set_type failed");
945 } else if (!strcmp(recvbuf
, "trace_alloc")) {
948 result
= ltt_trace_alloc(trace_name
);
950 ERR("ltt_trace_alloc failed");
953 inform_consumer_daemon(trace_name
);
954 } else if (!strcmp(recvbuf
, "trace_create")) {
957 result
= ltt_trace_setup(trace_name
);
959 ERR("ltt_trace_setup failed");
963 result
= ltt_trace_set_type(trace_name
, trace_type
);
965 ERR("ltt_trace_set_type failed");
968 } else if (!strcmp(recvbuf
, "trace_start")) {
971 result
= ltt_trace_alloc(trace_name
);
973 ERR("ltt_trace_alloc failed");
977 inform_consumer_daemon(trace_name
);
980 result
= ltt_trace_start(trace_name
);
982 ERR("ltt_trace_start failed");
985 } else if (!strcmp(recvbuf
, "trace_stop")) {
988 result
= ltt_trace_stop(trace_name
);
990 ERR("ltt_trace_stop failed");
993 } else if (!strcmp(recvbuf
, "trace_destroy")) {
995 DBG("trace destroy");
997 result
= ltt_trace_destroy(trace_name
, 0);
999 ERR("ltt_trace_destroy failed");
1002 } else if (nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
1003 do_cmd_get_shmid(recvbuf
, src
);
1004 } else if (nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
1005 do_cmd_get_n_subbufs(recvbuf
, src
);
1006 } else if (nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
1007 do_cmd_get_subbuf_size(recvbuf
, src
);
1008 } else if (nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
1011 libfile
= nth_token(recvbuf
, 1);
1013 DBG("load_probe_lib loading %s", libfile
);
1016 } else if (nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
1017 do_cmd_get_subbuffer(recvbuf
, src
);
1018 } else if (nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
1019 do_cmd_put_subbuffer(recvbuf
, src
);
1020 } else if (nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1021 do_cmd_set_subbuf_size(recvbuf
, src
);
1022 } else if (nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1023 do_cmd_set_subbuf_num(recvbuf
, src
);
1024 } else if (nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1025 char *channel_slash_name
= nth_token(recvbuf
, 1);
1026 char channel_name
[256]="";
1027 char marker_name
[256]="";
1029 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1031 if (channel_name
== NULL
|| marker_name
== NULL
) {
1032 WARN("invalid marker name");
1036 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1038 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1040 } else if (nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1041 char *channel_slash_name
= nth_token(recvbuf
, 1);
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");
1052 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1054 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1056 } else if (nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1059 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1060 ERR("process_client_cmd : asprintf failed (%lld)",
1065 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1067 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1072 } else if (nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1073 char *reply
= getenv("UST_DAEMON_SOCKET");
1075 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1076 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1080 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1083 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1086 ERR("ustcomm_send_reply failed");
1087 } else if (nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1088 char *sock_path
= nth_token(recvbuf
, 1);
1089 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1091 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1092 } else if (nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1093 do_cmd_force_switch();
1095 ERR("unable to parse message: %s", recvbuf
);
1103 void *listener_main(void *p
)
1109 pthread_cleanup_push(listener_cleanup
, NULL
);
1112 struct mpentries mpent
;
1114 multipoll_init(&mpent
);
1116 blocked_consumers_add_to_mp(&mpent
);
1117 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1119 result
= multipoll_poll(&mpent
, -1);
1121 ERR("error in multipoll_poll");
1124 multipoll_destroy(&mpent
);
1127 pthread_cleanup_pop(1);
1130 /* These should only be accessed in the parent thread,
1133 static volatile sig_atomic_t have_listener
= 0;
1134 static pthread_t listener_thread
;
1136 void create_listener(void)
1139 sigset_t sig_all_blocked
;
1140 sigset_t orig_parent_mask
;
1142 if (have_listener
) {
1143 WARN("not creating listener because we already had one");
1147 /* A new thread created by pthread_create inherits the signal mask
1148 * from the parent. To avoid any signal being received by the
1149 * listener thread, we block all signals temporarily in the parent,
1150 * while we create the listener thread.
1153 sigfillset(&sig_all_blocked
);
1155 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1157 PERROR("pthread_sigmask: %s", strerror(result
));
1160 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1162 PERROR("pthread_create");
1165 /* Restore original signal mask in parent */
1166 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1168 PERROR("pthread_sigmask: %s", strerror(result
));
1174 static int init_socket(void)
1176 return ustcomm_init_app(getpid(), &ustcomm_app
);
1179 #define AUTOPROBE_DISABLED 0
1180 #define AUTOPROBE_ENABLE_ALL 1
1181 #define AUTOPROBE_ENABLE_REGEX 2
1182 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1183 static regex_t autoprobe_regex
;
1185 static void auto_probe_connect(struct marker
*m
)
1189 char* concat_name
= NULL
;
1190 const char *probe_name
= "default";
1192 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1194 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1195 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1197 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1198 m
->channel
, m
->name
);
1201 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1208 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1209 if (result
&& result
!= -EEXIST
)
1210 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1212 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1216 static void __attribute__((constructor
)) init()
1219 char* autoprobe_val
= NULL
;
1220 char* subbuffer_size_val
= NULL
;
1221 char* subbuffer_count_val
= NULL
;
1222 unsigned int subbuffer_size
;
1223 unsigned int subbuffer_count
;
1226 /* Assign the pidunique, to be able to differentiate the processes with same
1227 * pid, (before and after an exec).
1229 pidunique
= make_pidunique();
1231 DBG("Tracectl constructor");
1233 result
= init_socket();
1235 ERR("init_socket error");
1241 autoprobe_val
= getenv("UST_AUTOPROBE");
1242 if (autoprobe_val
) {
1243 struct marker_iter iter
;
1245 DBG("Autoprobe enabled.");
1247 /* Ensure markers are initialized */
1250 /* Ensure marker control is initialized, for the probe */
1251 init_marker_control();
1253 /* first, set the callback that will connect the
1254 * probe on new markers
1256 if (autoprobe_val
[0] == '/') {
1257 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1261 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1262 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1263 /* don't crash the application just for this */
1265 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1268 /* just enable all instrumentation */
1269 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1272 marker_set_new_marker_cb(auto_probe_connect
);
1274 /* Now, connect the probes that were already registered. */
1275 marker_iter_reset(&iter
);
1276 marker_iter_start(&iter
);
1278 DBG("now iterating on markers already registered");
1279 while (iter
.marker
) {
1280 DBG("now iterating on marker %s", iter
.marker
->name
);
1281 auto_probe_connect(iter
.marker
);
1282 marker_iter_next(&iter
);
1286 if (getenv("UST_OVERWRITE")) {
1287 int val
= atoi(getenv("UST_OVERWRITE"));
1288 if (val
== 0 || val
== 1) {
1289 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1291 WARN("invalid value for UST_OVERWRITE");
1295 if (getenv("UST_AUTOCOLLECT")) {
1296 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1297 if (val
== 0 || val
== 1) {
1298 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1300 WARN("invalid value for UST_AUTOCOLLECT");
1304 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1305 if (subbuffer_size_val
) {
1306 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1307 power
= pow2_higher_or_eq(subbuffer_size
);
1308 if (power
!= subbuffer_size
)
1309 WARN("using the next power of two for buffer size = %u\n", power
);
1310 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1313 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1314 if (subbuffer_count_val
) {
1315 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1316 if (subbuffer_count
< 2)
1317 subbuffer_count
= 2;
1318 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1321 if (getenv("UST_TRACE")) {
1322 char trace_name
[] = "auto";
1323 char trace_type
[] = "ustrelay";
1325 DBG("starting early tracing");
1327 /* Ensure marker control is initialized */
1328 init_marker_control();
1330 /* Ensure markers are initialized */
1333 /* Ensure buffers are initialized, for the transport to be available.
1334 * We are about to set a trace type and it will fail without this.
1336 init_ustrelay_transport();
1338 /* FIXME: When starting early tracing (here), depending on the
1339 * order of constructors, it is very well possible some marker
1340 * sections are not yet registered. Because of this, some
1341 * channels may not be registered. Yet, we are about to ask the
1342 * daemon to collect the channels. Channels which are not yet
1343 * registered will not be collected.
1345 * Currently, in LTTng, there is no way to add a channel after
1346 * trace start. The reason for this is that it induces complex
1347 * concurrency issues on the trace structures, which can only
1348 * be resolved using RCU. This has not been done yet. As a
1349 * workaround, we are forcing the registration of the "ust"
1350 * channel here. This is the only channel (apart from metadata)
1351 * that can be reliably used in early tracing.
1353 * Non-early tracing does not have this problem and can use
1354 * arbitrary channel names.
1356 ltt_channels_register("ust");
1358 result
= ltt_trace_setup(trace_name
);
1360 ERR("ltt_trace_setup failed");
1364 result
= ltt_trace_set_type(trace_name
, trace_type
);
1366 ERR("ltt_trace_set_type failed");
1370 result
= ltt_trace_alloc(trace_name
);
1372 ERR("ltt_trace_alloc failed");
1376 result
= ltt_trace_start(trace_name
);
1378 ERR("ltt_trace_start failed");
1382 /* Do this after the trace is started in order to avoid creating confusion
1383 * if the trace fails to start. */
1384 inform_consumer_daemon(trace_name
);
1389 /* should decrementally destroy stuff if error */
1393 /* This is only called if we terminate normally, not with an unhandled signal,
1394 * so we cannot rely on it. However, for now, LTTV requires that the header of
1395 * the last sub-buffer contain a valid end time for the trace. This is done
1396 * automatically only when the trace is properly stopped.
1398 * If the traced program crashed, it is always possible to manually add the
1399 * right value in the header, or to open the trace in text mode.
1401 * FIXME: Fix LTTV so it doesn't need this.
1404 static void destroy_traces(void)
1408 /* if trace running, finish it */
1410 DBG("destructor stopping traces");
1412 result
= ltt_trace_stop("auto");
1414 ERR("ltt_trace_stop error");
1417 result
= ltt_trace_destroy("auto", 0);
1419 ERR("ltt_trace_destroy error");
1423 static int trace_recording(void)
1426 struct ust_trace
*trace
;
1430 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1431 if (trace
->active
) {
1437 ltt_unlock_traces();
1443 static int have_consumer(void)
1445 return !list_empty(&blocked_consumers
);
1449 int restarting_usleep(useconds_t usecs
)
1455 tv
.tv_nsec
= usecs
* 1000;
1458 result
= nanosleep(&tv
, &tv
);
1459 } while (result
== -1 && errno
== EINTR
);
1464 static void stop_listener(void)
1471 result
= pthread_cancel(listener_thread
);
1473 ERR("pthread_cancel: %s", strerror(result
));
1475 result
= pthread_join(listener_thread
, NULL
);
1477 ERR("pthread_join: %s", strerror(result
));
1481 /* This destructor keeps the process alive for a few seconds in order
1482 * to leave time to ustd to connect to its buffers. This is necessary
1483 * for programs whose execution is very short. It is also useful in all
1484 * programs when tracing is started close to the end of the program
1487 * FIXME: For now, this only works for the first trace created in a
1491 static void __attribute__((destructor
)) keepalive()
1493 if (trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1495 DBG("Keeping process alive for consumer daemon...");
1496 while (LOAD_SHARED(buffers_to_export
)) {
1497 const int interv
= 200000;
1498 restarting_usleep(interv
);
1501 if (total
>= 3000000) {
1502 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1506 DBG("Finally dying...");
1511 /* Ask the listener to stop and clean up. */
1515 void ust_potential_exec(void)
1517 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1524 /* Notify ust that there was a fork. This needs to be called inside
1525 * the new process, anytime a process whose memory is not shared with
1526 * the parent is created. If this function is not called, the events
1527 * of the new process will not be collected.
1529 * Signals should be disabled before the fork and reenabled only after
1530 * this call in order to guarantee tracing is not started before ust_fork()
1531 * sanitizes the new process.
1534 static void ust_fork(void)
1536 struct blocked_consumer
*bc
;
1537 struct blocked_consumer
*deletable_bc
= NULL
;
1540 /* FIXME: technically, the locks could have been taken before the fork */
1541 DBG("ust: forking");
1543 /* break lock if necessary */
1544 ltt_unlock_traces();
1546 ltt_trace_stop("auto");
1547 ltt_trace_destroy("auto", 1);
1548 /* Delete all active connections */
1549 ustcomm_close_all_connections(&ustcomm_app
.server
);
1551 /* Delete all blocked consumers */
1552 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1553 result
= close(bc
->fd_producer
);
1559 list_del(&bc
->list
);
1562 /* free app, keeping socket file */
1563 ustcomm_fini_app(&ustcomm_app
, 1);
1565 STORE_SHARED(buffers_to_export
, 0);
1569 ltt_trace_setup("auto");
1570 result
= ltt_trace_set_type("auto", "ustrelay");
1572 ERR("ltt_trace_set_type failed");
1576 ltt_trace_alloc("auto");
1577 ltt_trace_start("auto");
1578 inform_consumer_daemon("auto");
1581 void ust_before_fork(ust_fork_info_t
*fork_info
)
1583 /* Disable signals. This is to avoid that the child
1584 * intervenes before it is properly setup for tracing. It is
1585 * safer to disable all signals, because then we know we are not
1586 * breaking anything by restoring the original mask.
1592 - only do this if tracing is active
1595 /* Disable signals */
1596 sigfillset(&all_sigs
);
1597 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1599 PERROR("sigprocmask");
1604 /* Don't call this function directly in a traced program */
1605 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1609 /* Restore signals */
1610 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1612 PERROR("sigprocmask");
1617 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1619 /* Reenable signals */
1620 ust_after_fork_common(fork_info
);
1623 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1625 /* First sanitize the child */
1628 /* Then reenable interrupts */
1629 ust_after_fork_common(fork_info
);