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
24 #include <sys/types.h>
25 #include <sys/socket.h>
30 #include <urcu/uatomic_arch.h>
32 #include <ust/marker.h>
33 #include <ust/tracectl.h>
38 #include "marker-control.h"
39 #include "multipoll.h"
41 #define USTSIGNAL SIGIO
43 #define MAX_MSG_SIZE (100)
45 #define MSG_REGISTER_NOTIF 2
47 /* This should only be accessed by the constructor, before the creation
48 * of the listener, and then only by the listener.
52 extern struct chan_info_struct chan_infos
[];
54 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
56 static struct ustcomm_app ustcomm_app
;
58 struct tracecmd
{ /* no padding */
63 /* volatile because shared between the listener and the main thread */
64 int buffers_to_export
= 0;
67 /* size: the size of all the fields except size itself */
70 /* Only the necessary part of the payload is transferred. It
71 * may even be none of it.
76 struct consumer_channel
{
78 struct ltt_channel_struct
*chan
;
81 struct blocked_consumer
{
86 /* args to ustcomm_send_reply */
87 struct ustcomm_server server
;
88 struct ustcomm_source src
;
90 /* args to ust_buffers_get_subbuf */
91 struct ust_buffer
*buf
;
93 struct list_head list
;
96 static long long make_pidunique(void)
101 gettimeofday(&tv
, NULL
);
105 retval
|= tv
.tv_usec
;
110 static void print_markers(FILE *fp
)
112 struct marker_iter iter
;
115 marker_iter_reset(&iter
);
116 marker_iter_start(&iter
);
119 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
);
120 marker_iter_next(&iter
);
125 static int init_socket(void);
127 /* Ask the daemon to collect a trace called trace_name and being
128 * produced by this pid.
130 * The trace must be at least allocated. (It can also be started.)
131 * This is because _ltt_trace_find is used.
134 static void inform_consumer_daemon(const char *trace_name
)
137 struct ust_trace
*trace
;
138 pid_t pid
= getpid();
143 trace
= _ltt_trace_find(trace_name
);
145 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
149 for(i
=0; i
< trace
->nr_channels
; i
++) {
150 if(trace
->channels
[i
].request_collection
) {
151 /* iterate on all cpus */
152 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
154 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
155 result
= ustcomm_request_consumer(pid
, buf
);
157 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
158 /* continue even if fail */
161 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
170 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
173 long consumed_old
= 0;
175 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
178 result
= read(bc
->fd_producer
, &inbuf
, 1);
185 DBG("listener: got messsage that a buffer ended");
187 res
= close(bc
->fd_producer
);
194 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
196 ERR("ustcomm_send_reply failed");
203 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
204 if(result
== -EAGAIN
) {
205 WARN("missed buffer?");
208 else if(result
< 0) {
209 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
211 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
212 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
214 ERR("ustcomm_send_reply failed");
225 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
227 struct blocked_consumer
*bc
;
229 list_for_each_entry(bc
, &blocked_consumers
, list
) {
230 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
235 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
239 sep
= rindex(channel_and_cpu
, '_');
242 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
248 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
251 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
254 struct ust_trace
*trace
;
255 char trace_name
[] = "auto";
257 char *channel_and_cpu
;
265 channel_and_cpu
= nth_token(recvbuf
, 1);
266 if(channel_and_cpu
== NULL
) {
267 ERR("cannot parse channel");
271 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
273 ERR("problem parsing channel name");
274 goto free_short_chan_name
;
278 trace
= _ltt_trace_find(trace_name
);
282 ERR("cannot find trace!");
284 goto free_short_chan_name
;
287 for(i
=0; i
<trace
->nr_channels
; i
++) {
288 struct ust_channel
*channel
= &trace
->channels
[i
];
289 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
291 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
294 // DBG("the shmid for the requested channel is %d", buf->shmid);
295 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
296 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
298 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
300 ERR("ustcomm_send_reply failed");
303 goto free_short_chan_name
;
314 ERR("channel not found (%s)", channel_and_cpu
);
317 free_short_chan_name
:
324 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
327 struct ust_trace
*trace
;
328 char trace_name
[] = "auto";
330 char *channel_and_cpu
;
336 DBG("get_n_subbufs");
338 channel_and_cpu
= nth_token(recvbuf
, 1);
339 if(channel_and_cpu
== NULL
) {
340 ERR("cannot parse channel");
344 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
346 ERR("problem parsing channel name");
347 goto free_short_chan_name
;
351 trace
= _ltt_trace_find(trace_name
);
355 ERR("cannot find trace!");
357 goto free_short_chan_name
;
360 for(i
=0; i
<trace
->nr_channels
; i
++) {
361 struct ust_channel
*channel
= &trace
->channels
[i
];
363 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
366 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
367 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
369 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
371 ERR("ustcomm_send_reply failed");
374 goto free_short_chan_name
;
383 ERR("unable to find channel");
386 free_short_chan_name
:
393 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
396 struct ust_trace
*trace
;
397 char trace_name
[] = "auto";
399 char *channel_and_cpu
;
405 DBG("get_subbuf_size");
407 channel_and_cpu
= nth_token(recvbuf
, 1);
408 if(channel_and_cpu
== NULL
) {
409 ERR("cannot parse channel");
413 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
415 ERR("problem parsing channel name");
416 goto free_short_chan_name
;
420 trace
= _ltt_trace_find(trace_name
);
424 ERR("cannot find trace!");
426 goto free_short_chan_name
;
429 for(i
=0; i
<trace
->nr_channels
; i
++) {
430 struct ust_channel
*channel
= &trace
->channels
[i
];
432 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
435 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
436 asprintf(&reply
, "%zd", channel
->subbuf_size
);
438 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
440 ERR("ustcomm_send_reply failed");
443 goto free_short_chan_name
;
452 ERR("unable to find channel");
455 free_short_chan_name
:
462 /* Return the power of two which is equal or higher to v */
464 static unsigned int pow2_higher_or_eq(unsigned int v
)
468 int retval
= 1<<(hb
-1);
476 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
478 char *channel_slash_size
;
479 char ch_name
[256]="";
480 unsigned int size
, power
;
482 struct ust_trace
*trace
;
483 char trace_name
[] = "auto";
487 DBG("set_subbuf_size");
489 channel_slash_size
= nth_token(recvbuf
, 1);
490 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
492 if(ch_name
== NULL
) {
493 ERR("cannot parse channel");
497 power
= pow2_higher_or_eq(size
);
498 power
= max_t(unsigned int, 2u, power
);
500 WARN("using the next power of two for buffer size = %u\n", power
);
503 trace
= _ltt_trace_find_setup(trace_name
);
505 ERR("cannot find trace!");
510 for(i
= 0; i
< trace
->nr_channels
; i
++) {
511 struct ust_channel
*channel
= &trace
->channels
[i
];
513 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
515 channel
->subbuf_size
= power
;
516 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
523 ERR("unable to find channel");
531 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
533 char *channel_slash_num
;
534 char ch_name
[256]="";
537 struct ust_trace
*trace
;
538 char trace_name
[] = "auto";
542 DBG("set_subbuf_num");
544 channel_slash_num
= nth_token(recvbuf
, 1);
545 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
547 if(ch_name
== NULL
) {
548 ERR("cannot parse channel");
552 ERR("subbuffer count should be greater than 2");
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_cnt
= num
;
570 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
577 ERR("unable to find channel");
585 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
588 struct ust_trace
*trace
;
589 char trace_name
[] = "auto";
591 char *channel_and_cpu
;
598 channel_and_cpu
= nth_token(recvbuf
, 1);
599 if(channel_and_cpu
== NULL
) {
600 ERR("cannot parse channel");
604 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
606 ERR("problem parsing channel name");
607 goto free_short_chan_name
;
611 trace
= _ltt_trace_find(trace_name
);
616 DBG("Cannot find trace. It was likely destroyed by the user.");
617 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
619 ERR("ustcomm_send_reply failed");
627 for(i
=0; i
<trace
->nr_channels
; i
++) {
628 struct ust_channel
*channel
= &trace
->channels
[i
];
630 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
631 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
632 struct blocked_consumer
*bc
;
636 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
638 ERR("malloc returned NULL");
641 bc
->fd_consumer
= src
->fd
;
642 bc
->fd_producer
= buf
->data_ready_fd_read
;
645 bc
->server
= ustcomm_app
.server
;
647 list_add(&bc
->list
, &blocked_consumers
);
649 /* Being here is the proof the daemon has mapped the buffer in its
650 * memory. We may now decrement buffers_to_export.
652 if(uatomic_read(&buf
->consumed
) == 0) {
653 DBG("decrementing buffers_to_export");
654 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
661 ERR("unable to find channel");
667 free_short_chan_name
:
674 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
677 struct ust_trace
*trace
;
678 char trace_name
[] = "auto";
680 char *channel_and_cpu
;
686 char *consumed_old_str
;
692 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
693 if(channel_and_cpu
== NULL
) {
694 ERR("cannot parse channel");
699 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
700 if(consumed_old_str
== NULL
) {
701 ERR("cannot parse consumed_old");
703 goto free_channel_and_cpu
;
705 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
706 if(*endptr
!= '\0') {
707 ERR("invalid value for consumed_old");
709 goto free_consumed_old_str
;
712 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
714 ERR("problem parsing channel name");
716 goto free_short_chan_name
;
720 trace
= _ltt_trace_find(trace_name
);
723 DBG("Cannot find trace. It was likely destroyed by the user.");
724 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
726 ERR("ustcomm_send_reply failed");
734 for(i
=0; i
<trace
->nr_channels
; i
++) {
735 struct ust_channel
*channel
= &trace
->channels
[i
];
737 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
738 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
742 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
744 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
745 asprintf(&reply
, "%s", "ERROR");
748 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
749 asprintf(&reply
, "%s", "OK");
752 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
754 ERR("ustcomm_send_reply failed");
765 ERR("unable to find channel");
770 free_short_chan_name
:
772 free_consumed_old_str
:
773 free(consumed_old_str
);
774 free_channel_and_cpu
:
775 free(channel_and_cpu
);
781 static void listener_cleanup(void *ptr
)
783 ustcomm_fini_app(&ustcomm_app
, 0);
786 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
789 char trace_name
[] = "auto";
790 char trace_type
[] = "ustrelay";
793 DBG("received a message! it's: %s", recvbuf
);
794 len
= strlen(recvbuf
);
796 if(!strcmp(recvbuf
, "print_markers")) {
797 print_markers(stderr
);
799 else if(!strcmp(recvbuf
, "list_markers")) {
804 fp
= open_memstream(&ptr
, &size
);
808 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
812 else if(!strcmp(recvbuf
, "start")) {
813 /* start is an operation that setups the trace, allocates it and starts it */
814 result
= ltt_trace_setup(trace_name
);
816 ERR("ltt_trace_setup failed");
820 result
= ltt_trace_set_type(trace_name
, trace_type
);
822 ERR("ltt_trace_set_type failed");
826 result
= ltt_trace_alloc(trace_name
);
828 ERR("ltt_trace_alloc failed");
832 inform_consumer_daemon(trace_name
);
834 result
= ltt_trace_start(trace_name
);
836 ERR("ltt_trace_start failed");
840 else if(!strcmp(recvbuf
, "trace_setup")) {
843 result
= ltt_trace_setup(trace_name
);
845 ERR("ltt_trace_setup failed");
849 result
= ltt_trace_set_type(trace_name
, trace_type
);
851 ERR("ltt_trace_set_type failed");
855 else if(!strcmp(recvbuf
, "trace_alloc")) {
858 result
= ltt_trace_alloc(trace_name
);
860 ERR("ltt_trace_alloc failed");
863 inform_consumer_daemon(trace_name
);
865 else if(!strcmp(recvbuf
, "trace_create")) {
868 result
= ltt_trace_setup(trace_name
);
870 ERR("ltt_trace_setup failed");
874 result
= ltt_trace_set_type(trace_name
, trace_type
);
876 ERR("ltt_trace_set_type failed");
880 else if(!strcmp(recvbuf
, "trace_start")) {
883 result
= ltt_trace_alloc(trace_name
);
885 ERR("ltt_trace_alloc failed");
889 inform_consumer_daemon(trace_name
);
892 result
= ltt_trace_start(trace_name
);
894 ERR("ltt_trace_start failed");
898 else if(!strcmp(recvbuf
, "trace_stop")) {
901 result
= ltt_trace_stop(trace_name
);
903 ERR("ltt_trace_stop failed");
907 else if(!strcmp(recvbuf
, "trace_destroy")) {
909 DBG("trace destroy");
911 result
= ltt_trace_destroy(trace_name
, 0);
913 ERR("ltt_trace_destroy failed");
917 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
918 do_cmd_get_shmid(recvbuf
, src
);
920 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
921 do_cmd_get_n_subbufs(recvbuf
, src
);
923 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
924 do_cmd_get_subbuf_size(recvbuf
, src
);
926 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
929 libfile
= nth_token(recvbuf
, 1);
931 DBG("load_probe_lib loading %s", libfile
);
935 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
936 do_cmd_get_subbuffer(recvbuf
, src
);
938 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
939 do_cmd_put_subbuffer(recvbuf
, src
);
941 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
942 do_cmd_set_subbuf_size(recvbuf
, src
);
944 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
945 do_cmd_set_subbuf_num(recvbuf
, src
);
947 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
948 char *channel_slash_name
= nth_token(recvbuf
, 1);
949 char channel_name
[256]="";
950 char marker_name
[256]="";
952 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
954 if(channel_name
== NULL
|| marker_name
== NULL
) {
955 WARN("invalid marker name");
959 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
961 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
964 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
965 char *channel_slash_name
= nth_token(recvbuf
, 1);
969 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
971 if(marker_name
== NULL
) {
974 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
976 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
979 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
982 asprintf(&reply
, "%lld", pidunique
);
984 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
986 ERR("listener: get_pidunique: ustcomm_send_reply failed");
993 ERR("unable to parse message: %s", recvbuf
);
1001 void *listener_main(void *p
)
1007 pthread_cleanup_push(listener_cleanup
, NULL
);
1010 struct mpentries mpent
;
1012 multipoll_init(&mpent
);
1014 blocked_consumers_add_to_mp(&mpent
);
1015 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1017 result
= multipoll_poll(&mpent
, -1);
1019 ERR("error in multipoll_poll");
1022 multipoll_destroy(&mpent
);
1025 pthread_cleanup_pop(1);
1028 /* These should only be accessed in the parent thread,
1031 static volatile sig_atomic_t have_listener
= 0;
1032 static pthread_t listener_thread
;
1034 void create_listener(void)
1037 sigset_t sig_all_blocked
;
1038 sigset_t orig_parent_mask
;
1041 WARN("not creating listener because we already had one");
1045 /* A new thread created by pthread_create inherits the signal mask
1046 * from the parent. To avoid any signal being received by the
1047 * listener thread, we block all signals temporarily in the parent,
1048 * while we create the listener thread.
1051 sigfillset(&sig_all_blocked
);
1053 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1055 PERROR("pthread_sigmask: %s", strerror(result
));
1058 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1060 PERROR("pthread_create");
1063 /* Restore original signal mask in parent */
1064 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1066 PERROR("pthread_sigmask: %s", strerror(result
));
1072 static int init_socket(void)
1074 return ustcomm_init_app(getpid(), &ustcomm_app
);
1077 #define AUTOPROBE_DISABLED 0
1078 #define AUTOPROBE_ENABLE_ALL 1
1079 #define AUTOPROBE_ENABLE_REGEX 2
1080 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1081 static regex_t autoprobe_regex
;
1083 static void auto_probe_connect(struct marker
*m
)
1087 char* concat_name
= NULL
;
1088 const char *probe_name
= "default";
1090 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1093 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1094 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1096 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1097 m
->channel
, m
->name
);
1100 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1107 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1108 if(result
&& result
!= -EEXIST
)
1109 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1111 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1115 static void __attribute__((constructor
)) init()
1118 char* autoprobe_val
= NULL
;
1119 char* subbuffer_size_val
= NULL
;
1120 char* subbuffer_count_val
= NULL
;
1121 unsigned int subbuffer_size
;
1122 unsigned int subbuffer_count
;
1125 /* Assign the pidunique, to be able to differentiate the processes with same
1126 * pid, (before and after an exec).
1128 pidunique
= make_pidunique();
1130 DBG("Tracectl constructor");
1132 result
= init_socket();
1134 ERR("init_socket error");
1140 autoprobe_val
= getenv("UST_AUTOPROBE");
1142 struct marker_iter iter
;
1144 DBG("Autoprobe enabled.");
1146 /* Ensure markers are initialized */
1149 /* Ensure marker control is initialized, for the probe */
1150 init_marker_control();
1152 /* first, set the callback that will connect the
1153 * probe on new markers
1155 if(autoprobe_val
[0] == '/') {
1156 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1160 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1161 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1162 /* don't crash the application just for this */
1165 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1169 /* just enable all instrumentation */
1170 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1173 marker_set_new_marker_cb(auto_probe_connect
);
1175 /* Now, connect the probes that were already registered. */
1176 marker_iter_reset(&iter
);
1177 marker_iter_start(&iter
);
1179 DBG("now iterating on markers already registered");
1180 while(iter
.marker
) {
1181 DBG("now iterating on marker %s", iter
.marker
->name
);
1182 auto_probe_connect(iter
.marker
);
1183 marker_iter_next(&iter
);
1187 if(getenv("UST_OVERWRITE")) {
1188 int val
= atoi(getenv("UST_OVERWRITE"));
1189 if(val
== 0 || val
== 1) {
1190 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1193 WARN("invalid value for UST_OVERWRITE");
1197 if(getenv("UST_AUTOCOLLECT")) {
1198 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1199 if(val
== 0 || val
== 1) {
1200 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1203 WARN("invalid value for UST_AUTOCOLLECT");
1207 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1208 if(subbuffer_size_val
) {
1209 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1210 power
= pow2_higher_or_eq(subbuffer_size
);
1211 if(power
!= subbuffer_size
)
1212 WARN("using the next power of two for buffer size = %u\n", power
);
1213 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1216 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1217 if(subbuffer_count_val
) {
1218 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1219 if(subbuffer_count
< 2)
1220 subbuffer_count
= 2;
1221 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1224 if(getenv("UST_TRACE")) {
1225 char trace_name
[] = "auto";
1226 char trace_type
[] = "ustrelay";
1228 DBG("starting early tracing");
1230 /* Ensure marker control is initialized */
1231 init_marker_control();
1233 /* Ensure markers are initialized */
1236 /* Ensure buffers are initialized, for the transport to be available.
1237 * We are about to set a trace type and it will fail without this.
1239 init_ustrelay_transport();
1241 /* FIXME: When starting early tracing (here), depending on the
1242 * order of constructors, it is very well possible some marker
1243 * sections are not yet registered. Because of this, some
1244 * channels may not be registered. Yet, we are about to ask the
1245 * daemon to collect the channels. Channels which are not yet
1246 * registered will not be collected.
1248 * Currently, in LTTng, there is no way to add a channel after
1249 * trace start. The reason for this is that it induces complex
1250 * concurrency issues on the trace structures, which can only
1251 * be resolved using RCU. This has not been done yet. As a
1252 * workaround, we are forcing the registration of the "ust"
1253 * channel here. This is the only channel (apart from metadata)
1254 * that can be reliably used in early tracing.
1256 * Non-early tracing does not have this problem and can use
1257 * arbitrary channel names.
1259 ltt_channels_register("ust");
1261 result
= ltt_trace_setup(trace_name
);
1263 ERR("ltt_trace_setup failed");
1267 result
= ltt_trace_set_type(trace_name
, trace_type
);
1269 ERR("ltt_trace_set_type failed");
1273 result
= ltt_trace_alloc(trace_name
);
1275 ERR("ltt_trace_alloc failed");
1279 result
= ltt_trace_start(trace_name
);
1281 ERR("ltt_trace_start failed");
1285 /* Do this after the trace is started in order to avoid creating confusion
1286 * if the trace fails to start. */
1287 inform_consumer_daemon(trace_name
);
1292 /* should decrementally destroy stuff if error */
1296 /* This is only called if we terminate normally, not with an unhandled signal,
1297 * so we cannot rely on it. However, for now, LTTV requires that the header of
1298 * the last sub-buffer contain a valid end time for the trace. This is done
1299 * automatically only when the trace is properly stopped.
1301 * If the traced program crashed, it is always possible to manually add the
1302 * right value in the header, or to open the trace in text mode.
1304 * FIXME: Fix LTTV so it doesn't need this.
1307 static void destroy_traces(void)
1311 /* if trace running, finish it */
1313 DBG("destructor stopping traces");
1315 result
= ltt_trace_stop("auto");
1317 ERR("ltt_trace_stop error");
1320 result
= ltt_trace_destroy("auto", 0);
1322 ERR("ltt_trace_destroy error");
1326 static int trace_recording(void)
1329 struct ust_trace
*trace
;
1333 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1340 ltt_unlock_traces();
1346 static int have_consumer(void)
1348 return !list_empty(&blocked_consumers
);
1352 int restarting_usleep(useconds_t usecs
)
1358 tv
.tv_nsec
= usecs
* 1000;
1361 result
= nanosleep(&tv
, &tv
);
1362 } while(result
== -1 && errno
== EINTR
);
1367 static void stop_listener()
1371 result
= pthread_cancel(listener_thread
);
1373 ERR("pthread_cancel: %s", strerror(result
));
1375 result
= pthread_join(listener_thread
, NULL
);
1377 ERR("pthread_join: %s", strerror(result
));
1381 /* This destructor keeps the process alive for a few seconds in order
1382 * to leave time to ustd to connect to its buffers. This is necessary
1383 * for programs whose execution is very short. It is also useful in all
1384 * programs when tracing is started close to the end of the program
1387 * FIXME: For now, this only works for the first trace created in a
1391 static void __attribute__((destructor
)) keepalive()
1393 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1395 DBG("Keeping process alive for consumer daemon...");
1396 while(LOAD_SHARED(buffers_to_export
)) {
1397 const int interv
= 200000;
1398 restarting_usleep(interv
);
1401 if(total
>= 3000000) {
1402 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1406 DBG("Finally dying...");
1411 /* Ask the listener to stop and clean up. */
1415 void ust_potential_exec(void)
1417 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1424 /* Notify ust that there was a fork. This needs to be called inside
1425 * the new process, anytime a process whose memory is not shared with
1426 * the parent is created. If this function is not called, the events
1427 * of the new process will not be collected.
1429 * Signals should be disabled before the fork and reenabled only after
1430 * this call in order to guarantee tracing is not started before ust_fork()
1431 * sanitizes the new process.
1434 static void ust_fork(void)
1436 struct blocked_consumer
*bc
;
1437 struct blocked_consumer
*deletable_bc
= NULL
;
1440 /* FIXME: technically, the locks could have been taken before the fork */
1441 DBG("ust: forking");
1443 /* break lock if necessary */
1444 ltt_unlock_traces();
1446 ltt_trace_stop("auto");
1447 ltt_trace_destroy("auto", 1);
1448 /* Delete all active connections */
1449 ustcomm_close_all_connections(&ustcomm_app
.server
);
1451 /* Delete all blocked consumers */
1452 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1453 result
= close(bc
->fd_producer
);
1459 list_del(&bc
->list
);
1462 /* free app, keeping socket file */
1463 ustcomm_fini_app(&ustcomm_app
, 1);
1465 STORE_SHARED(buffers_to_export
, 0);
1469 ltt_trace_setup("auto");
1470 result
= ltt_trace_set_type("auto", "ustrelay");
1472 ERR("ltt_trace_set_type failed");
1476 ltt_trace_alloc("auto");
1477 ltt_trace_start("auto");
1478 inform_consumer_daemon("auto");
1481 void ust_before_fork(ust_fork_info_t
*fork_info
)
1483 /* Disable signals. This is to avoid that the child
1484 * intervenes before it is properly setup for tracing. It is
1485 * safer to disable all signals, because then we know we are not
1486 * breaking anything by restoring the original mask.
1492 - only do this if tracing is active
1495 /* Disable signals */
1496 sigfillset(&all_sigs
);
1497 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1499 PERROR("sigprocmask");
1504 /* Don't call this function directly in a traced program */
1505 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1509 /* Restore signals */
1510 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1512 PERROR("sigprocmask");
1517 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1519 /* Reenable signals */
1520 ust_after_fork_common(fork_info
);
1523 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1525 /* First sanitize the child */
1528 /* Then reenable interrupts */
1529 ust_after_fork_common(fork_info
);