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
);
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?");
233 else if(result
< 0) {
234 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
236 if (asprintf(&reply
, "%s %ld", "OK", consumed_old
) < 0) {
237 ERR("process_blkd_consumer_act : asprintf failed (OK %ld)",
241 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
243 ERR("ustcomm_send_reply failed");
254 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
256 struct blocked_consumer
*bc
;
258 list_for_each_entry(bc
, &blocked_consumers
, list
) {
259 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
264 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
268 sep
= rindex(channel_and_cpu
, '_');
271 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
277 if (asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
) < 0) {
278 ERR("seperate_channel_cpu : asprintf failed (%.*s)",
279 (int)(sep
-channel_and_cpu
), channel_and_cpu
);
284 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
287 struct ust_trace
*trace
;
288 char trace_name
[] = "auto";
290 char *channel_and_cpu
;
298 channel_and_cpu
= nth_token(recvbuf
, 1);
299 if(channel_and_cpu
== NULL
) {
300 ERR("cannot parse channel");
305 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
307 ERR("problem parsing channel name");
309 goto free_short_chan_name
;
313 trace
= _ltt_trace_find(trace_name
);
317 ERR("cannot find trace!");
319 goto free_short_chan_name
;
322 for(i
=0; i
<trace
->nr_channels
; i
++) {
323 struct ust_channel
*channel
= &trace
->channels
[i
];
324 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
326 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
329 // DBG("the shmid for the requested channel is %d", buf->shmid);
330 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
331 if (asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]) < 0) {
332 ERR("do_cmd_get_shmid : asprintf failed (%d %d)",
333 buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
335 goto free_short_chan_name
;
338 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
340 ERR("ustcomm_send_reply failed");
343 goto free_short_chan_name
;
354 ERR("channel not found (%s)", channel_and_cpu
);
357 free_short_chan_name
:
364 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
367 struct ust_trace
*trace
;
368 char trace_name
[] = "auto";
370 char *channel_and_cpu
;
376 DBG("get_n_subbufs");
378 channel_and_cpu
= nth_token(recvbuf
, 1);
379 if(channel_and_cpu
== NULL
) {
380 ERR("cannot parse channel");
385 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
387 ERR("problem parsing channel name");
389 goto free_short_chan_name
;
393 trace
= _ltt_trace_find(trace_name
);
397 ERR("cannot find trace!");
399 goto free_short_chan_name
;
402 for(i
=0; i
<trace
->nr_channels
; i
++) {
403 struct ust_channel
*channel
= &trace
->channels
[i
];
405 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
408 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
409 if (asprintf(&reply
, "%d", channel
->subbuf_cnt
) < 0) {
410 ERR("do_cmd_get_n_subbufs : asprintf failed (%d)",
411 channel
->subbuf_cnt
);
413 goto free_short_chan_name
;
416 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
418 ERR("ustcomm_send_reply failed");
421 goto free_short_chan_name
;
430 ERR("unable to find channel");
433 free_short_chan_name
:
440 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
443 struct ust_trace
*trace
;
444 char trace_name
[] = "auto";
446 char *channel_and_cpu
;
452 DBG("get_subbuf_size");
454 channel_and_cpu
= nth_token(recvbuf
, 1);
455 if(channel_and_cpu
== NULL
) {
456 ERR("cannot parse channel");
461 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
463 ERR("problem parsing channel name");
465 goto free_short_chan_name
;
469 trace
= _ltt_trace_find(trace_name
);
473 ERR("cannot find trace!");
475 goto free_short_chan_name
;
478 for(i
=0; i
<trace
->nr_channels
; i
++) {
479 struct ust_channel
*channel
= &trace
->channels
[i
];
481 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
484 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
485 if (asprintf(&reply
, "%zd", channel
->subbuf_size
) < 0) {
486 ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)",
487 channel
->subbuf_size
);
489 goto free_short_chan_name
;
492 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
494 ERR("ustcomm_send_reply failed");
497 goto free_short_chan_name
;
506 ERR("unable to find channel");
509 free_short_chan_name
:
516 /* Return the power of two which is equal or higher to v */
518 static unsigned int pow2_higher_or_eq(unsigned int v
)
521 int retval
= 1<<(hb
-1);
529 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
531 char *channel_slash_size
;
532 char ch_name
[256]="";
533 unsigned int size
, power
;
535 struct ust_trace
*trace
;
536 char trace_name
[] = "auto";
540 DBG("set_subbuf_size");
542 channel_slash_size
= nth_token(recvbuf
, 1);
543 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
545 if(ch_name
== NULL
) {
546 ERR("cannot parse channel");
551 power
= pow2_higher_or_eq(size
);
552 power
= max_t(unsigned int, 2u, power
);
554 WARN("using the next power of two for buffer size = %u\n", power
);
557 trace
= _ltt_trace_find_setup(trace_name
);
559 ERR("cannot find trace!");
564 for(i
= 0; i
< trace
->nr_channels
; i
++) {
565 struct ust_channel
*channel
= &trace
->channels
[i
];
567 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
569 channel
->subbuf_size
= power
;
570 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
577 ERR("unable to find channel");
585 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
587 char *channel_slash_num
;
588 char ch_name
[256]="";
591 struct ust_trace
*trace
;
592 char trace_name
[] = "auto";
596 DBG("set_subbuf_num");
598 channel_slash_num
= nth_token(recvbuf
, 1);
599 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
601 if(ch_name
== NULL
) {
602 ERR("cannot parse channel");
607 ERR("subbuffer count should be greater than 2");
613 trace
= _ltt_trace_find_setup(trace_name
);
615 ERR("cannot find trace!");
620 for(i
= 0; i
< trace
->nr_channels
; i
++) {
621 struct ust_channel
*channel
= &trace
->channels
[i
];
623 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
625 channel
->subbuf_cnt
= num
;
626 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
633 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)");
810 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
811 if (asprintf(&reply
, "%s", "OK") < 0) {
812 ERR("do_cmd_put_subbuffer : asprintf failed (OK)");
818 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
820 ERR("ustcomm_send_reply failed");
831 ERR("unable to find channel");
836 free_short_chan_name
:
838 free_consumed_old_str
:
839 free(consumed_old_str
);
840 free_channel_and_cpu
:
841 free(channel_and_cpu
);
847 static void listener_cleanup(void *ptr
)
849 ustcomm_fini_app(&ustcomm_app
, 0);
852 static void do_cmd_force_switch()
854 struct blocked_consumer
*bc
;
856 list_for_each_entry(bc
, &blocked_consumers
, list
) {
857 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
861 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
864 char trace_name
[] = "auto";
865 char trace_type
[] = "ustrelay";
868 DBG("received a message! it's: %s", recvbuf
);
869 len
= strlen(recvbuf
);
871 if(!strcmp(recvbuf
, "print_markers")) {
872 print_markers(stderr
);
874 else if(!strcmp(recvbuf
, "list_markers")) {
879 fp
= open_memstream(&ptr
, &size
);
883 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
886 } else if (!strcmp(recvbuf
, "print_trace_events")) {
887 print_trace_events(stderr
);
889 } else if(!strcmp(recvbuf
, "list_trace_events")) {
894 fp
= open_memstream(&ptr
, &size
);
896 ERR("opening memstream failed");
899 print_trace_events(fp
);
902 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
904 ERR("list_trace_events failed");
908 } else if(!strcmp(recvbuf
, "start")) {
909 /* start is an operation that setups the trace, allocates it and starts it */
910 result
= ltt_trace_setup(trace_name
);
912 ERR("ltt_trace_setup failed");
916 result
= ltt_trace_set_type(trace_name
, trace_type
);
918 ERR("ltt_trace_set_type failed");
922 result
= ltt_trace_alloc(trace_name
);
924 ERR("ltt_trace_alloc failed");
928 inform_consumer_daemon(trace_name
);
930 result
= ltt_trace_start(trace_name
);
932 ERR("ltt_trace_start failed");
936 else if(!strcmp(recvbuf
, "trace_setup")) {
939 result
= ltt_trace_setup(trace_name
);
941 ERR("ltt_trace_setup failed");
945 result
= ltt_trace_set_type(trace_name
, trace_type
);
947 ERR("ltt_trace_set_type failed");
951 else if(!strcmp(recvbuf
, "trace_alloc")) {
954 result
= ltt_trace_alloc(trace_name
);
956 ERR("ltt_trace_alloc failed");
959 inform_consumer_daemon(trace_name
);
961 else if(!strcmp(recvbuf
, "trace_create")) {
964 result
= ltt_trace_setup(trace_name
);
966 ERR("ltt_trace_setup failed");
970 result
= ltt_trace_set_type(trace_name
, trace_type
);
972 ERR("ltt_trace_set_type failed");
976 else if(!strcmp(recvbuf
, "trace_start")) {
979 result
= ltt_trace_alloc(trace_name
);
981 ERR("ltt_trace_alloc failed");
985 inform_consumer_daemon(trace_name
);
988 result
= ltt_trace_start(trace_name
);
990 ERR("ltt_trace_start failed");
994 else if(!strcmp(recvbuf
, "trace_stop")) {
997 result
= ltt_trace_stop(trace_name
);
999 ERR("ltt_trace_stop failed");
1003 else if(!strcmp(recvbuf
, "trace_destroy")) {
1005 DBG("trace destroy");
1007 result
= ltt_trace_destroy(trace_name
, 0);
1009 ERR("ltt_trace_destroy failed");
1013 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
1014 do_cmd_get_shmid(recvbuf
, src
);
1016 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
1017 do_cmd_get_n_subbufs(recvbuf
, src
);
1019 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
1020 do_cmd_get_subbuf_size(recvbuf
, src
);
1022 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
1025 libfile
= nth_token(recvbuf
, 1);
1027 DBG("load_probe_lib loading %s", libfile
);
1031 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
1032 do_cmd_get_subbuffer(recvbuf
, src
);
1034 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
1035 do_cmd_put_subbuffer(recvbuf
, src
);
1037 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1038 do_cmd_set_subbuf_size(recvbuf
, src
);
1040 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1041 do_cmd_set_subbuf_num(recvbuf
, src
);
1043 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1044 char *channel_slash_name
= nth_token(recvbuf
, 1);
1045 char channel_name
[256]="";
1046 char marker_name
[256]="";
1048 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1050 if(channel_name
== NULL
|| marker_name
== NULL
) {
1051 WARN("invalid marker name");
1055 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1057 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1060 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1061 char *channel_slash_name
= nth_token(recvbuf
, 1);
1065 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1067 if(marker_name
== NULL
) {
1070 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1072 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1075 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1078 if (asprintf(&reply
, "%lld", pidunique
) < 0) {
1079 ERR("process_client_cmd : asprintf failed (%lld)",
1084 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1086 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1092 else if(nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1093 char *reply
= getenv("UST_DAEMON_SOCKET");
1095 if (asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd") < 0) {
1096 ERR("process_client_cmd : asprintf failed (%s/ustd)",
1100 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1104 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1107 ERR("ustcomm_send_reply failed");
1109 else if(nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1110 char *sock_path
= nth_token(recvbuf
, 1);
1111 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1113 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1115 else if(nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1116 do_cmd_force_switch();
1119 ERR("unable to parse message: %s", recvbuf
);
1127 void *listener_main(void *p
)
1133 pthread_cleanup_push(listener_cleanup
, NULL
);
1136 struct mpentries mpent
;
1138 multipoll_init(&mpent
);
1140 blocked_consumers_add_to_mp(&mpent
);
1141 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1143 result
= multipoll_poll(&mpent
, -1);
1145 ERR("error in multipoll_poll");
1148 multipoll_destroy(&mpent
);
1151 pthread_cleanup_pop(1);
1154 /* These should only be accessed in the parent thread,
1157 static volatile sig_atomic_t have_listener
= 0;
1158 static pthread_t listener_thread
;
1160 void create_listener(void)
1163 sigset_t sig_all_blocked
;
1164 sigset_t orig_parent_mask
;
1167 WARN("not creating listener because we already had one");
1171 /* A new thread created by pthread_create inherits the signal mask
1172 * from the parent. To avoid any signal being received by the
1173 * listener thread, we block all signals temporarily in the parent,
1174 * while we create the listener thread.
1177 sigfillset(&sig_all_blocked
);
1179 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1181 PERROR("pthread_sigmask: %s", strerror(result
));
1184 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1186 PERROR("pthread_create");
1189 /* Restore original signal mask in parent */
1190 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1192 PERROR("pthread_sigmask: %s", strerror(result
));
1199 static int init_socket(void)
1201 return ustcomm_init_app(getpid(), &ustcomm_app
);
1204 #define AUTOPROBE_DISABLED 0
1205 #define AUTOPROBE_ENABLE_ALL 1
1206 #define AUTOPROBE_ENABLE_REGEX 2
1207 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1208 static regex_t autoprobe_regex
;
1210 static void auto_probe_connect(struct marker
*m
)
1214 char* concat_name
= NULL
;
1215 const char *probe_name
= "default";
1217 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1220 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1221 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1223 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1224 m
->channel
, m
->name
);
1227 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1234 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1235 if(result
&& result
!= -EEXIST
)
1236 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1238 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1242 static void __attribute__((constructor
)) init()
1245 char* autoprobe_val
= NULL
;
1246 char* subbuffer_size_val
= NULL
;
1247 char* subbuffer_count_val
= NULL
;
1248 unsigned int subbuffer_size
;
1249 unsigned int subbuffer_count
;
1252 /* Assign the pidunique, to be able to differentiate the processes with same
1253 * pid, (before and after an exec).
1255 pidunique
= make_pidunique();
1257 DBG("Tracectl constructor");
1259 result
= init_socket();
1261 ERR("init_socket error");
1267 autoprobe_val
= getenv("UST_AUTOPROBE");
1269 struct marker_iter iter
;
1271 DBG("Autoprobe enabled.");
1273 /* Ensure markers are initialized */
1276 /* Ensure marker control is initialized, for the probe */
1277 init_marker_control();
1279 /* first, set the callback that will connect the
1280 * probe on new markers
1282 if(autoprobe_val
[0] == '/') {
1283 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1287 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1288 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1289 /* don't crash the application just for this */
1292 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1296 /* just enable all instrumentation */
1297 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1300 marker_set_new_marker_cb(auto_probe_connect
);
1302 /* Now, connect the probes that were already registered. */
1303 marker_iter_reset(&iter
);
1304 marker_iter_start(&iter
);
1306 DBG("now iterating on markers already registered");
1307 while(iter
.marker
) {
1308 DBG("now iterating on marker %s", iter
.marker
->name
);
1309 auto_probe_connect(iter
.marker
);
1310 marker_iter_next(&iter
);
1314 if(getenv("UST_OVERWRITE")) {
1315 int val
= atoi(getenv("UST_OVERWRITE"));
1316 if(val
== 0 || val
== 1) {
1317 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1320 WARN("invalid value for UST_OVERWRITE");
1324 if(getenv("UST_AUTOCOLLECT")) {
1325 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1326 if(val
== 0 || val
== 1) {
1327 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1330 WARN("invalid value for UST_AUTOCOLLECT");
1334 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1335 if(subbuffer_size_val
) {
1336 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1337 power
= pow2_higher_or_eq(subbuffer_size
);
1338 if(power
!= subbuffer_size
)
1339 WARN("using the next power of two for buffer size = %u\n", power
);
1340 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1343 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1344 if(subbuffer_count_val
) {
1345 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1346 if(subbuffer_count
< 2)
1347 subbuffer_count
= 2;
1348 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1351 if(getenv("UST_TRACE")) {
1352 char trace_name
[] = "auto";
1353 char trace_type
[] = "ustrelay";
1355 DBG("starting early tracing");
1357 /* Ensure marker control is initialized */
1358 init_marker_control();
1360 /* Ensure markers are initialized */
1363 /* Ensure buffers are initialized, for the transport to be available.
1364 * We are about to set a trace type and it will fail without this.
1366 init_ustrelay_transport();
1368 /* FIXME: When starting early tracing (here), depending on the
1369 * order of constructors, it is very well possible some marker
1370 * sections are not yet registered. Because of this, some
1371 * channels may not be registered. Yet, we are about to ask the
1372 * daemon to collect the channels. Channels which are not yet
1373 * registered will not be collected.
1375 * Currently, in LTTng, there is no way to add a channel after
1376 * trace start. The reason for this is that it induces complex
1377 * concurrency issues on the trace structures, which can only
1378 * be resolved using RCU. This has not been done yet. As a
1379 * workaround, we are forcing the registration of the "ust"
1380 * channel here. This is the only channel (apart from metadata)
1381 * that can be reliably used in early tracing.
1383 * Non-early tracing does not have this problem and can use
1384 * arbitrary channel names.
1386 ltt_channels_register("ust");
1388 result
= ltt_trace_setup(trace_name
);
1390 ERR("ltt_trace_setup failed");
1394 result
= ltt_trace_set_type(trace_name
, trace_type
);
1396 ERR("ltt_trace_set_type failed");
1400 result
= ltt_trace_alloc(trace_name
);
1402 ERR("ltt_trace_alloc failed");
1406 result
= ltt_trace_start(trace_name
);
1408 ERR("ltt_trace_start failed");
1412 /* Do this after the trace is started in order to avoid creating confusion
1413 * if the trace fails to start. */
1414 inform_consumer_daemon(trace_name
);
1419 /* should decrementally destroy stuff if error */
1423 /* This is only called if we terminate normally, not with an unhandled signal,
1424 * so we cannot rely on it. However, for now, LTTV requires that the header of
1425 * the last sub-buffer contain a valid end time for the trace. This is done
1426 * automatically only when the trace is properly stopped.
1428 * If the traced program crashed, it is always possible to manually add the
1429 * right value in the header, or to open the trace in text mode.
1431 * FIXME: Fix LTTV so it doesn't need this.
1434 static void destroy_traces(void)
1438 /* if trace running, finish it */
1440 DBG("destructor stopping traces");
1442 result
= ltt_trace_stop("auto");
1444 ERR("ltt_trace_stop error");
1447 result
= ltt_trace_destroy("auto", 0);
1449 ERR("ltt_trace_destroy error");
1453 static int trace_recording(void)
1456 struct ust_trace
*trace
;
1460 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1467 ltt_unlock_traces();
1473 static int have_consumer(void)
1475 return !list_empty(&blocked_consumers
);
1479 int restarting_usleep(useconds_t usecs
)
1485 tv
.tv_nsec
= usecs
* 1000;
1488 result
= nanosleep(&tv
, &tv
);
1489 } while(result
== -1 && errno
== EINTR
);
1494 static void stop_listener(void)
1501 result
= pthread_cancel(listener_thread
);
1503 ERR("pthread_cancel: %s", strerror(result
));
1505 result
= pthread_join(listener_thread
, NULL
);
1507 ERR("pthread_join: %s", strerror(result
));
1511 /* This destructor keeps the process alive for a few seconds in order
1512 * to leave time to ustd to connect to its buffers. This is necessary
1513 * for programs whose execution is very short. It is also useful in all
1514 * programs when tracing is started close to the end of the program
1517 * FIXME: For now, this only works for the first trace created in a
1521 static void __attribute__((destructor
)) keepalive()
1523 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1525 DBG("Keeping process alive for consumer daemon...");
1526 while(LOAD_SHARED(buffers_to_export
)) {
1527 const int interv
= 200000;
1528 restarting_usleep(interv
);
1531 if(total
>= 3000000) {
1532 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1536 DBG("Finally dying...");
1541 /* Ask the listener to stop and clean up. */
1545 void ust_potential_exec(void)
1547 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1554 /* Notify ust that there was a fork. This needs to be called inside
1555 * the new process, anytime a process whose memory is not shared with
1556 * the parent is created. If this function is not called, the events
1557 * of the new process will not be collected.
1559 * Signals should be disabled before the fork and reenabled only after
1560 * this call in order to guarantee tracing is not started before ust_fork()
1561 * sanitizes the new process.
1564 static void ust_fork(void)
1566 struct blocked_consumer
*bc
;
1567 struct blocked_consumer
*deletable_bc
= NULL
;
1570 /* FIXME: technically, the locks could have been taken before the fork */
1571 DBG("ust: forking");
1573 /* break lock if necessary */
1574 ltt_unlock_traces();
1576 ltt_trace_stop("auto");
1577 ltt_trace_destroy("auto", 1);
1578 /* Delete all active connections */
1579 ustcomm_close_all_connections(&ustcomm_app
.server
);
1581 /* Delete all blocked consumers */
1582 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1583 result
= close(bc
->fd_producer
);
1589 list_del(&bc
->list
);
1592 /* free app, keeping socket file */
1593 ustcomm_fini_app(&ustcomm_app
, 1);
1595 STORE_SHARED(buffers_to_export
, 0);
1599 ltt_trace_setup("auto");
1600 result
= ltt_trace_set_type("auto", "ustrelay");
1602 ERR("ltt_trace_set_type failed");
1606 ltt_trace_alloc("auto");
1607 ltt_trace_start("auto");
1608 inform_consumer_daemon("auto");
1611 void ust_before_fork(ust_fork_info_t
*fork_info
)
1613 /* Disable signals. This is to avoid that the child
1614 * intervenes before it is properly setup for tracing. It is
1615 * safer to disable all signals, because then we know we are not
1616 * breaking anything by restoring the original mask.
1622 - only do this if tracing is active
1625 /* Disable signals */
1626 sigfillset(&all_sigs
);
1627 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1629 PERROR("sigprocmask");
1634 /* Don't call this function directly in a traced program */
1635 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1639 /* Restore signals */
1640 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1642 PERROR("sigprocmask");
1647 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1649 /* Reenable signals */
1650 ust_after_fork_common(fork_info
);
1653 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1655 /* First sanitize the child */
1658 /* Then reenable interrupts */
1659 ust_after_fork_common(fork_info
);