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 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
54 static struct ustcomm_app ustcomm_app
;
56 struct tracecmd
{ /* no padding */
61 /* volatile because shared between the listener and the main thread */
62 int buffers_to_export
= 0;
65 /* size: the size of all the fields except size itself */
68 /* Only the necessary part of the payload is transferred. It
69 * may even be none of it.
74 struct consumer_channel
{
76 struct ltt_channel_struct
*chan
;
79 struct blocked_consumer
{
84 /* args to ustcomm_send_reply */
85 struct ustcomm_server server
;
86 struct ustcomm_source src
;
88 /* args to ust_buffers_get_subbuf */
89 struct ust_buffer
*buf
;
91 struct list_head list
;
94 static long long make_pidunique(void)
99 gettimeofday(&tv
, NULL
);
103 retval
|= tv
.tv_usec
;
108 static void print_markers(FILE *fp
)
110 struct marker_iter iter
;
113 marker_iter_reset(&iter
);
114 marker_iter_start(&iter
);
117 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
);
118 marker_iter_next(&iter
);
123 static int init_socket(void);
125 /* Ask the daemon to collect a trace called trace_name and being
126 * produced by this pid.
128 * The trace must be at least allocated. (It can also be started.)
129 * This is because _ltt_trace_find is used.
132 static void inform_consumer_daemon(const char *trace_name
)
135 struct ust_trace
*trace
;
136 pid_t pid
= getpid();
141 trace
= _ltt_trace_find(trace_name
);
143 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
147 for(i
=0; i
< trace
->nr_channels
; i
++) {
148 if(trace
->channels
[i
].request_collection
) {
149 /* iterate on all cpus */
150 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
152 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
153 result
= ustcomm_request_consumer(pid
, buf
);
155 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
156 /* continue even if fail */
159 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
168 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
171 long consumed_old
= 0;
173 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
176 result
= read(bc
->fd_producer
, &inbuf
, 1);
183 DBG("listener: got messsage that a buffer ended");
185 res
= close(bc
->fd_producer
);
192 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
194 ERR("ustcomm_send_reply failed");
201 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
202 if(result
== -EAGAIN
) {
203 WARN("missed buffer?");
206 else if(result
< 0) {
207 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
209 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
210 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
212 ERR("ustcomm_send_reply failed");
223 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
225 struct blocked_consumer
*bc
;
227 list_for_each_entry(bc
, &blocked_consumers
, list
) {
228 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
233 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
237 sep
= rindex(channel_and_cpu
, '_');
240 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
246 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
249 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
252 struct ust_trace
*trace
;
253 char trace_name
[] = "auto";
255 char *channel_and_cpu
;
263 channel_and_cpu
= nth_token(recvbuf
, 1);
264 if(channel_and_cpu
== NULL
) {
265 ERR("cannot parse channel");
269 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
271 ERR("problem parsing channel name");
272 goto free_short_chan_name
;
276 trace
= _ltt_trace_find(trace_name
);
280 ERR("cannot find trace!");
282 goto free_short_chan_name
;
285 for(i
=0; i
<trace
->nr_channels
; i
++) {
286 struct ust_channel
*channel
= &trace
->channels
[i
];
287 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
289 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
292 // DBG("the shmid for the requested channel is %d", buf->shmid);
293 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
294 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
296 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
298 ERR("ustcomm_send_reply failed");
301 goto free_short_chan_name
;
312 ERR("channel not found (%s)", channel_and_cpu
);
315 free_short_chan_name
:
322 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
325 struct ust_trace
*trace
;
326 char trace_name
[] = "auto";
328 char *channel_and_cpu
;
334 DBG("get_n_subbufs");
336 channel_and_cpu
= nth_token(recvbuf
, 1);
337 if(channel_and_cpu
== NULL
) {
338 ERR("cannot parse channel");
342 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
344 ERR("problem parsing channel name");
345 goto free_short_chan_name
;
349 trace
= _ltt_trace_find(trace_name
);
353 ERR("cannot find trace!");
355 goto free_short_chan_name
;
358 for(i
=0; i
<trace
->nr_channels
; i
++) {
359 struct ust_channel
*channel
= &trace
->channels
[i
];
361 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
364 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
365 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
367 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
369 ERR("ustcomm_send_reply failed");
372 goto free_short_chan_name
;
381 ERR("unable to find channel");
384 free_short_chan_name
:
391 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
394 struct ust_trace
*trace
;
395 char trace_name
[] = "auto";
397 char *channel_and_cpu
;
403 DBG("get_subbuf_size");
405 channel_and_cpu
= nth_token(recvbuf
, 1);
406 if(channel_and_cpu
== NULL
) {
407 ERR("cannot parse channel");
411 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
413 ERR("problem parsing channel name");
414 goto free_short_chan_name
;
418 trace
= _ltt_trace_find(trace_name
);
422 ERR("cannot find trace!");
424 goto free_short_chan_name
;
427 for(i
=0; i
<trace
->nr_channels
; i
++) {
428 struct ust_channel
*channel
= &trace
->channels
[i
];
430 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
433 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
434 asprintf(&reply
, "%zd", channel
->subbuf_size
);
436 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
438 ERR("ustcomm_send_reply failed");
441 goto free_short_chan_name
;
450 ERR("unable to find channel");
453 free_short_chan_name
:
460 /* Return the power of two which is equal or higher to v */
462 static unsigned int pow2_higher_or_eq(unsigned int v
)
466 int retval
= 1<<(hb
-1);
474 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
476 char *channel_slash_size
;
477 char ch_name
[256]="";
478 unsigned int size
, power
;
480 struct ust_trace
*trace
;
481 char trace_name
[] = "auto";
485 DBG("set_subbuf_size");
487 channel_slash_size
= nth_token(recvbuf
, 1);
488 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
490 if(ch_name
== NULL
) {
491 ERR("cannot parse channel");
495 power
= pow2_higher_or_eq(size
);
496 power
= max_t(unsigned int, 2u, power
);
498 WARN("using the next power of two for buffer size = %u\n", power
);
501 trace
= _ltt_trace_find_setup(trace_name
);
503 ERR("cannot find trace!");
508 for(i
= 0; i
< trace
->nr_channels
; i
++) {
509 struct ust_channel
*channel
= &trace
->channels
[i
];
511 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
513 channel
->subbuf_size
= power
;
514 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
521 ERR("unable to find channel");
529 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
531 char *channel_slash_num
;
532 char ch_name
[256]="";
535 struct ust_trace
*trace
;
536 char trace_name
[] = "auto";
540 DBG("set_subbuf_num");
542 channel_slash_num
= nth_token(recvbuf
, 1);
543 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
545 if(ch_name
== NULL
) {
546 ERR("cannot parse channel");
550 ERR("subbuffer count should be greater than 2");
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_cnt
= num
;
568 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
575 ERR("unable to find channel");
583 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
586 struct ust_trace
*trace
;
587 char trace_name
[] = "auto";
589 char *channel_and_cpu
;
596 channel_and_cpu
= nth_token(recvbuf
, 1);
597 if(channel_and_cpu
== NULL
) {
598 ERR("cannot parse channel");
602 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
604 ERR("problem parsing channel name");
605 goto free_short_chan_name
;
609 trace
= _ltt_trace_find(trace_name
);
614 DBG("Cannot find trace. It was likely destroyed by the user.");
615 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
617 ERR("ustcomm_send_reply failed");
625 for(i
=0; i
<trace
->nr_channels
; i
++) {
626 struct ust_channel
*channel
= &trace
->channels
[i
];
628 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
629 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
630 struct blocked_consumer
*bc
;
634 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
636 ERR("malloc returned NULL");
639 bc
->fd_consumer
= src
->fd
;
640 bc
->fd_producer
= buf
->data_ready_fd_read
;
643 bc
->server
= ustcomm_app
.server
;
645 list_add(&bc
->list
, &blocked_consumers
);
647 /* Being here is the proof the daemon has mapped the buffer in its
648 * memory. We may now decrement buffers_to_export.
650 if(uatomic_read(&buf
->consumed
) == 0) {
651 DBG("decrementing buffers_to_export");
652 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
659 ERR("unable to find channel");
665 free_short_chan_name
:
672 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
675 struct ust_trace
*trace
;
676 char trace_name
[] = "auto";
678 char *channel_and_cpu
;
684 char *consumed_old_str
;
690 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
691 if(channel_and_cpu
== NULL
) {
692 ERR("cannot parse channel");
697 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
698 if(consumed_old_str
== NULL
) {
699 ERR("cannot parse consumed_old");
701 goto free_channel_and_cpu
;
703 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
704 if(*endptr
!= '\0') {
705 ERR("invalid value for consumed_old");
707 goto free_consumed_old_str
;
710 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
712 ERR("problem parsing channel name");
714 goto free_short_chan_name
;
718 trace
= _ltt_trace_find(trace_name
);
721 DBG("Cannot find trace. It was likely destroyed by the user.");
722 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
724 ERR("ustcomm_send_reply failed");
732 for(i
=0; i
<trace
->nr_channels
; i
++) {
733 struct ust_channel
*channel
= &trace
->channels
[i
];
735 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
736 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
740 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
742 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
743 asprintf(&reply
, "%s", "ERROR");
746 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
747 asprintf(&reply
, "%s", "OK");
750 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
752 ERR("ustcomm_send_reply failed");
763 ERR("unable to find channel");
768 free_short_chan_name
:
770 free_consumed_old_str
:
771 free(consumed_old_str
);
772 free_channel_and_cpu
:
773 free(channel_and_cpu
);
779 static void listener_cleanup(void *ptr
)
781 ustcomm_fini_app(&ustcomm_app
, 0);
784 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
787 char trace_name
[] = "auto";
788 char trace_type
[] = "ustrelay";
791 DBG("received a message! it's: %s", recvbuf
);
792 len
= strlen(recvbuf
);
794 if(!strcmp(recvbuf
, "print_markers")) {
795 print_markers(stderr
);
797 else if(!strcmp(recvbuf
, "list_markers")) {
802 fp
= open_memstream(&ptr
, &size
);
806 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
810 else if(!strcmp(recvbuf
, "start")) {
811 /* start is an operation that setups the trace, allocates it and starts it */
812 result
= ltt_trace_setup(trace_name
);
814 ERR("ltt_trace_setup failed");
818 result
= ltt_trace_set_type(trace_name
, trace_type
);
820 ERR("ltt_trace_set_type failed");
824 result
= ltt_trace_alloc(trace_name
);
826 ERR("ltt_trace_alloc failed");
830 inform_consumer_daemon(trace_name
);
832 result
= ltt_trace_start(trace_name
);
834 ERR("ltt_trace_start failed");
838 else if(!strcmp(recvbuf
, "trace_setup")) {
841 result
= ltt_trace_setup(trace_name
);
843 ERR("ltt_trace_setup failed");
847 result
= ltt_trace_set_type(trace_name
, trace_type
);
849 ERR("ltt_trace_set_type failed");
853 else if(!strcmp(recvbuf
, "trace_alloc")) {
856 result
= ltt_trace_alloc(trace_name
);
858 ERR("ltt_trace_alloc failed");
861 inform_consumer_daemon(trace_name
);
863 else if(!strcmp(recvbuf
, "trace_create")) {
866 result
= ltt_trace_setup(trace_name
);
868 ERR("ltt_trace_setup failed");
872 result
= ltt_trace_set_type(trace_name
, trace_type
);
874 ERR("ltt_trace_set_type failed");
878 else if(!strcmp(recvbuf
, "trace_start")) {
881 result
= ltt_trace_alloc(trace_name
);
883 ERR("ltt_trace_alloc failed");
887 inform_consumer_daemon(trace_name
);
890 result
= ltt_trace_start(trace_name
);
892 ERR("ltt_trace_start failed");
896 else if(!strcmp(recvbuf
, "trace_stop")) {
899 result
= ltt_trace_stop(trace_name
);
901 ERR("ltt_trace_stop failed");
905 else if(!strcmp(recvbuf
, "trace_destroy")) {
907 DBG("trace destroy");
909 result
= ltt_trace_destroy(trace_name
, 0);
911 ERR("ltt_trace_destroy failed");
915 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
916 do_cmd_get_shmid(recvbuf
, src
);
918 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
919 do_cmd_get_n_subbufs(recvbuf
, src
);
921 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
922 do_cmd_get_subbuf_size(recvbuf
, src
);
924 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
927 libfile
= nth_token(recvbuf
, 1);
929 DBG("load_probe_lib loading %s", libfile
);
933 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
934 do_cmd_get_subbuffer(recvbuf
, src
);
936 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
937 do_cmd_put_subbuffer(recvbuf
, src
);
939 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
940 do_cmd_set_subbuf_size(recvbuf
, src
);
942 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
943 do_cmd_set_subbuf_num(recvbuf
, src
);
945 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
946 char *channel_slash_name
= nth_token(recvbuf
, 1);
947 char channel_name
[256]="";
948 char marker_name
[256]="";
950 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
952 if(channel_name
== NULL
|| marker_name
== NULL
) {
953 WARN("invalid marker name");
957 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
959 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
962 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
963 char *channel_slash_name
= nth_token(recvbuf
, 1);
967 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
969 if(marker_name
== NULL
) {
972 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
974 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
977 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
980 asprintf(&reply
, "%lld", pidunique
);
982 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
984 ERR("listener: get_pidunique: ustcomm_send_reply failed");
991 ERR("unable to parse message: %s", recvbuf
);
999 void *listener_main(void *p
)
1005 pthread_cleanup_push(listener_cleanup
, NULL
);
1008 struct mpentries mpent
;
1010 multipoll_init(&mpent
);
1012 blocked_consumers_add_to_mp(&mpent
);
1013 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1015 result
= multipoll_poll(&mpent
, -1);
1017 ERR("error in multipoll_poll");
1020 multipoll_destroy(&mpent
);
1023 pthread_cleanup_pop(1);
1026 /* These should only be accessed in the parent thread,
1029 static volatile sig_atomic_t have_listener
= 0;
1030 static pthread_t listener_thread
;
1032 void create_listener(void)
1035 sigset_t sig_all_blocked
;
1036 sigset_t orig_parent_mask
;
1039 WARN("not creating listener because we already had one");
1043 /* A new thread created by pthread_create inherits the signal mask
1044 * from the parent. To avoid any signal being received by the
1045 * listener thread, we block all signals temporarily in the parent,
1046 * while we create the listener thread.
1049 sigfillset(&sig_all_blocked
);
1051 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1053 PERROR("pthread_sigmask: %s", strerror(result
));
1056 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1058 PERROR("pthread_create");
1061 /* Restore original signal mask in parent */
1062 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1064 PERROR("pthread_sigmask: %s", strerror(result
));
1070 static int init_socket(void)
1072 return ustcomm_init_app(getpid(), &ustcomm_app
);
1075 #define AUTOPROBE_DISABLED 0
1076 #define AUTOPROBE_ENABLE_ALL 1
1077 #define AUTOPROBE_ENABLE_REGEX 2
1078 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1079 static regex_t autoprobe_regex
;
1081 static void auto_probe_connect(struct marker
*m
)
1085 char* concat_name
= NULL
;
1086 const char *probe_name
= "default";
1088 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1091 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1092 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1094 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1095 m
->channel
, m
->name
);
1098 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1105 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1106 if(result
&& result
!= -EEXIST
)
1107 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1109 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1113 static void __attribute__((constructor
)) init()
1116 char* autoprobe_val
= NULL
;
1118 /* Assign the pidunique, to be able to differentiate the processes with same
1119 * pid, (before and after an exec).
1121 pidunique
= make_pidunique();
1123 DBG("Tracectl constructor");
1125 result
= init_socket();
1127 ERR("init_socket error");
1133 autoprobe_val
= getenv("UST_AUTOPROBE");
1135 struct marker_iter iter
;
1137 DBG("Autoprobe enabled.");
1139 /* Ensure markers are initialized */
1142 /* Ensure marker control is initialized, for the probe */
1143 init_marker_control();
1145 /* first, set the callback that will connect the
1146 * probe on new markers
1148 if(autoprobe_val
[0] == '/') {
1149 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1153 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1154 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1155 /* don't crash the application just for this */
1158 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1162 /* just enable all instrumentation */
1163 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1166 marker_set_new_marker_cb(auto_probe_connect
);
1168 /* Now, connect the probes that were already registered. */
1169 marker_iter_reset(&iter
);
1170 marker_iter_start(&iter
);
1172 DBG("now iterating on markers already registered");
1173 while(iter
.marker
) {
1174 DBG("now iterating on marker %s", iter
.marker
->name
);
1175 auto_probe_connect(iter
.marker
);
1176 marker_iter_next(&iter
);
1180 if(getenv("UST_OVERWRITE")) {
1181 int val
= atoi(getenv("UST_OVERWRITE"));
1182 if(val
== 0 || val
== 1) {
1183 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1186 WARN("invalid value for UST_OVERWRITE");
1190 if(getenv("UST_AUTOCOLLECT")) {
1191 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1192 if(val
== 0 || val
== 1) {
1193 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1196 WARN("invalid value for UST_AUTOCOLLECT");
1200 if(getenv("UST_TRACE")) {
1201 char trace_name
[] = "auto";
1202 char trace_type
[] = "ustrelay";
1204 DBG("starting early tracing");
1206 /* Ensure marker control is initialized */
1207 init_marker_control();
1209 /* Ensure markers are initialized */
1212 /* Ensure buffers are initialized, for the transport to be available.
1213 * We are about to set a trace type and it will fail without this.
1215 init_ustrelay_transport();
1217 /* FIXME: When starting early tracing (here), depending on the
1218 * order of constructors, it is very well possible some marker
1219 * sections are not yet registered. Because of this, some
1220 * channels may not be registered. Yet, we are about to ask the
1221 * daemon to collect the channels. Channels which are not yet
1222 * registered will not be collected.
1224 * Currently, in LTTng, there is no way to add a channel after
1225 * trace start. The reason for this is that it induces complex
1226 * concurrency issues on the trace structures, which can only
1227 * be resolved using RCU. This has not been done yet. As a
1228 * workaround, we are forcing the registration of the "ust"
1229 * channel here. This is the only channel (apart from metadata)
1230 * that can be reliably used in early tracing.
1232 * Non-early tracing does not have this problem and can use
1233 * arbitrary channel names.
1235 ltt_channels_register("ust");
1237 result
= ltt_trace_setup(trace_name
);
1239 ERR("ltt_trace_setup failed");
1243 result
= ltt_trace_set_type(trace_name
, trace_type
);
1245 ERR("ltt_trace_set_type failed");
1249 result
= ltt_trace_alloc(trace_name
);
1251 ERR("ltt_trace_alloc failed");
1255 result
= ltt_trace_start(trace_name
);
1257 ERR("ltt_trace_start failed");
1261 /* Do this after the trace is started in order to avoid creating confusion
1262 * if the trace fails to start. */
1263 inform_consumer_daemon(trace_name
);
1269 /* should decrementally destroy stuff if error */
1273 /* This is only called if we terminate normally, not with an unhandled signal,
1274 * so we cannot rely on it. However, for now, LTTV requires that the header of
1275 * the last sub-buffer contain a valid end time for the trace. This is done
1276 * automatically only when the trace is properly stopped.
1278 * If the traced program crashed, it is always possible to manually add the
1279 * right value in the header, or to open the trace in text mode.
1281 * FIXME: Fix LTTV so it doesn't need this.
1284 static void destroy_traces(void)
1288 /* if trace running, finish it */
1290 DBG("destructor stopping traces");
1292 result
= ltt_trace_stop("auto");
1294 ERR("ltt_trace_stop error");
1297 result
= ltt_trace_destroy("auto", 0);
1299 ERR("ltt_trace_destroy error");
1303 static int trace_recording(void)
1306 struct ust_trace
*trace
;
1310 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1317 ltt_unlock_traces();
1323 static int have_consumer(void)
1325 return !list_empty(&blocked_consumers
);
1329 int restarting_usleep(useconds_t usecs
)
1335 tv
.tv_nsec
= usecs
* 1000;
1338 result
= nanosleep(&tv
, &tv
);
1339 } while(result
== -1 && errno
== EINTR
);
1344 static void stop_listener()
1348 result
= pthread_cancel(listener_thread
);
1350 ERR("pthread_cancel: %s", strerror(result
));
1352 result
= pthread_join(listener_thread
, NULL
);
1354 ERR("pthread_join: %s", strerror(result
));
1358 /* This destructor keeps the process alive for a few seconds in order
1359 * to leave time to ustd to connect to its buffers. This is necessary
1360 * for programs whose execution is very short. It is also useful in all
1361 * programs when tracing is started close to the end of the program
1364 * FIXME: For now, this only works for the first trace created in a
1368 static void __attribute__((destructor
)) keepalive()
1370 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1372 DBG("Keeping process alive for consumer daemon...");
1373 while(LOAD_SHARED(buffers_to_export
)) {
1374 const int interv
= 200000;
1375 restarting_usleep(interv
);
1378 if(total
>= 3000000) {
1379 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1383 DBG("Finally dying...");
1388 /* Ask the listener to stop and clean up. */
1392 void ust_potential_exec(void)
1394 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1401 /* Notify ust that there was a fork. This needs to be called inside
1402 * the new process, anytime a process whose memory is not shared with
1403 * the parent is created. If this function is not called, the events
1404 * of the new process will not be collected.
1406 * Signals should be disabled before the fork and reenabled only after
1407 * this call in order to guarantee tracing is not started before ust_fork()
1408 * sanitizes the new process.
1411 static void ust_fork(void)
1413 struct blocked_consumer
*bc
;
1414 struct blocked_consumer
*deletable_bc
= NULL
;
1417 /* FIXME: technically, the locks could have been taken before the fork */
1418 DBG("ust: forking");
1420 /* break lock if necessary */
1421 ltt_unlock_traces();
1423 ltt_trace_stop("auto");
1424 ltt_trace_destroy("auto", 1);
1425 /* Delete all active connections */
1426 ustcomm_close_all_connections(&ustcomm_app
.server
);
1428 /* Delete all blocked consumers */
1429 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1430 result
= close(bc
->fd_producer
);
1436 list_del(&bc
->list
);
1439 /* free app, keeping socket file */
1440 ustcomm_fini_app(&ustcomm_app
, 1);
1442 STORE_SHARED(buffers_to_export
, 0);
1446 ltt_trace_setup("auto");
1447 result
= ltt_trace_set_type("auto", "ustrelay");
1449 ERR("ltt_trace_set_type failed");
1453 ltt_trace_alloc("auto");
1454 ltt_trace_start("auto");
1455 inform_consumer_daemon("auto");
1458 void ust_before_fork(ust_fork_info_t
*fork_info
)
1460 /* Disable signals. This is to avoid that the child
1461 * intervenes before it is properly setup for tracing. It is
1462 * safer to disable all signals, because then we know we are not
1463 * breaking anything by restoring the original mask.
1469 - only do this if tracing is active
1472 /* Disable signals */
1473 sigfillset(&all_sigs
);
1474 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1476 PERROR("sigprocmask");
1481 /* Don't call this function directly in a traced program */
1482 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1486 /* Restore signals */
1487 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1489 PERROR("sigprocmask");
1494 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1496 /* Reenable signals */
1497 ust_after_fork_common(fork_info
);
1500 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1502 /* First sanitize the child */
1505 /* Then reenable interrupts */
1506 ust_after_fork_common(fork_info
);