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/tracectl.h>
43 #include "marker-control.h"
44 #include "multipoll.h"
46 #define USTSIGNAL SIGIO
48 #define MAX_MSG_SIZE (100)
50 #define MSG_REGISTER_NOTIF 2
52 /* This should only be accessed by the constructor, before the creation
53 * of the listener, and then only by the listener.
57 extern struct chan_info_struct chan_infos
[];
59 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
61 static struct ustcomm_app ustcomm_app
;
63 struct tracecmd
{ /* no padding */
68 /* volatile because shared between the listener and the main thread */
69 int buffers_to_export
= 0;
72 /* size: the size of all the fields except size itself */
75 /* Only the necessary part of the payload is transferred. It
76 * may even be none of it.
81 struct consumer_channel
{
83 struct ltt_channel_struct
*chan
;
86 struct blocked_consumer
{
91 /* args to ustcomm_send_reply */
92 struct ustcomm_server server
;
93 struct ustcomm_source src
;
95 /* args to ust_buffers_get_subbuf */
96 struct ust_buffer
*buf
;
98 struct list_head list
;
101 static long long make_pidunique(void)
106 gettimeofday(&tv
, NULL
);
110 retval
|= tv
.tv_usec
;
115 static void print_markers(FILE *fp
)
117 struct marker_iter iter
;
120 marker_iter_reset(&iter
);
121 marker_iter_start(&iter
);
124 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
);
125 marker_iter_next(&iter
);
130 static int init_socket(void);
132 /* Ask the daemon to collect a trace called trace_name and being
133 * produced by this pid.
135 * The trace must be at least allocated. (It can also be started.)
136 * This is because _ltt_trace_find is used.
139 static void inform_consumer_daemon(const char *trace_name
)
142 struct ust_trace
*trace
;
143 pid_t pid
= getpid();
148 trace
= _ltt_trace_find(trace_name
);
150 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
154 for(i
=0; i
< trace
->nr_channels
; i
++) {
155 if(trace
->channels
[i
].request_collection
) {
156 /* iterate on all cpus */
157 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
159 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
160 result
= ustcomm_request_consumer(pid
, buf
);
162 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
163 /* continue even if fail */
166 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)+1);
175 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
178 long consumed_old
= 0;
180 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
183 result
= read(bc
->fd_producer
, &inbuf
, 1);
190 DBG("listener: got messsage that a buffer ended");
192 res
= close(bc
->fd_producer
);
199 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
201 ERR("ustcomm_send_reply failed");
208 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
209 if(result
== -EAGAIN
) {
210 WARN("missed buffer?");
213 else if(result
< 0) {
214 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
216 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
217 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
219 ERR("ustcomm_send_reply failed");
230 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
232 struct blocked_consumer
*bc
;
234 list_for_each_entry(bc
, &blocked_consumers
, list
) {
235 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
240 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
244 sep
= rindex(channel_and_cpu
, '_');
247 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
253 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
256 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
259 struct ust_trace
*trace
;
260 char trace_name
[] = "auto";
262 char *channel_and_cpu
;
270 channel_and_cpu
= nth_token(recvbuf
, 1);
271 if(channel_and_cpu
== NULL
) {
272 ERR("cannot parse channel");
276 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
278 ERR("problem parsing channel name");
279 goto free_short_chan_name
;
283 trace
= _ltt_trace_find(trace_name
);
287 ERR("cannot find trace!");
289 goto free_short_chan_name
;
292 for(i
=0; i
<trace
->nr_channels
; i
++) {
293 struct ust_channel
*channel
= &trace
->channels
[i
];
294 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
296 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
299 // DBG("the shmid for the requested channel is %d", buf->shmid);
300 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
301 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
303 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
305 ERR("ustcomm_send_reply failed");
308 goto free_short_chan_name
;
319 ERR("channel not found (%s)", channel_and_cpu
);
322 free_short_chan_name
:
329 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
332 struct ust_trace
*trace
;
333 char trace_name
[] = "auto";
335 char *channel_and_cpu
;
341 DBG("get_n_subbufs");
343 channel_and_cpu
= nth_token(recvbuf
, 1);
344 if(channel_and_cpu
== NULL
) {
345 ERR("cannot parse channel");
349 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
351 ERR("problem parsing channel name");
352 goto free_short_chan_name
;
356 trace
= _ltt_trace_find(trace_name
);
360 ERR("cannot find trace!");
362 goto free_short_chan_name
;
365 for(i
=0; i
<trace
->nr_channels
; i
++) {
366 struct ust_channel
*channel
= &trace
->channels
[i
];
368 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
371 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
372 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
374 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
376 ERR("ustcomm_send_reply failed");
379 goto free_short_chan_name
;
388 ERR("unable to find channel");
391 free_short_chan_name
:
398 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
401 struct ust_trace
*trace
;
402 char trace_name
[] = "auto";
404 char *channel_and_cpu
;
410 DBG("get_subbuf_size");
412 channel_and_cpu
= nth_token(recvbuf
, 1);
413 if(channel_and_cpu
== NULL
) {
414 ERR("cannot parse channel");
418 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
420 ERR("problem parsing channel name");
421 goto free_short_chan_name
;
425 trace
= _ltt_trace_find(trace_name
);
429 ERR("cannot find trace!");
431 goto free_short_chan_name
;
434 for(i
=0; i
<trace
->nr_channels
; i
++) {
435 struct ust_channel
*channel
= &trace
->channels
[i
];
437 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
440 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
441 asprintf(&reply
, "%zd", channel
->subbuf_size
);
443 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
445 ERR("ustcomm_send_reply failed");
448 goto free_short_chan_name
;
457 ERR("unable to find channel");
460 free_short_chan_name
:
467 /* Return the power of two which is equal or higher to v */
469 static unsigned int pow2_higher_or_eq(unsigned int v
)
472 int retval
= 1<<(hb
-1);
480 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
482 char *channel_slash_size
;
483 char ch_name
[256]="";
484 unsigned int size
, power
;
486 struct ust_trace
*trace
;
487 char trace_name
[] = "auto";
491 DBG("set_subbuf_size");
493 channel_slash_size
= nth_token(recvbuf
, 1);
494 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
496 if(ch_name
== NULL
) {
497 ERR("cannot parse channel");
501 power
= pow2_higher_or_eq(size
);
502 power
= max_t(unsigned int, 2u, power
);
504 WARN("using the next power of two for buffer size = %u\n", power
);
507 trace
= _ltt_trace_find_setup(trace_name
);
509 ERR("cannot find trace!");
514 for(i
= 0; i
< trace
->nr_channels
; i
++) {
515 struct ust_channel
*channel
= &trace
->channels
[i
];
517 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
519 channel
->subbuf_size
= power
;
520 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
527 ERR("unable to find channel");
535 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
537 char *channel_slash_num
;
538 char ch_name
[256]="";
541 struct ust_trace
*trace
;
542 char trace_name
[] = "auto";
546 DBG("set_subbuf_num");
548 channel_slash_num
= nth_token(recvbuf
, 1);
549 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
551 if(ch_name
== NULL
) {
552 ERR("cannot parse channel");
556 ERR("subbuffer count should be greater than 2");
561 trace
= _ltt_trace_find_setup(trace_name
);
563 ERR("cannot find trace!");
568 for(i
= 0; i
< trace
->nr_channels
; i
++) {
569 struct ust_channel
*channel
= &trace
->channels
[i
];
571 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
573 channel
->subbuf_cnt
= num
;
574 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
581 ERR("unable to find channel");
589 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
592 struct ust_trace
*trace
;
593 char trace_name
[] = "auto";
595 char *channel_and_cpu
;
602 channel_and_cpu
= nth_token(recvbuf
, 1);
603 if(channel_and_cpu
== NULL
) {
604 ERR("cannot parse channel");
608 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
610 ERR("problem parsing channel name");
611 goto free_short_chan_name
;
615 trace
= _ltt_trace_find(trace_name
);
620 DBG("Cannot find trace. It was likely destroyed by the user.");
621 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
623 ERR("ustcomm_send_reply failed");
631 for(i
=0; i
<trace
->nr_channels
; i
++) {
632 struct ust_channel
*channel
= &trace
->channels
[i
];
634 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
635 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
636 struct blocked_consumer
*bc
;
640 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
642 ERR("malloc returned NULL");
645 bc
->fd_consumer
= src
->fd
;
646 bc
->fd_producer
= buf
->data_ready_fd_read
;
649 bc
->server
= ustcomm_app
.server
;
651 list_add(&bc
->list
, &blocked_consumers
);
653 /* Being here is the proof the daemon has mapped the buffer in its
654 * memory. We may now decrement buffers_to_export.
656 if(uatomic_read(&buf
->consumed
) == 0) {
657 DBG("decrementing buffers_to_export");
658 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
665 ERR("unable to find channel");
671 free_short_chan_name
:
678 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
681 struct ust_trace
*trace
;
682 char trace_name
[] = "auto";
684 char *channel_and_cpu
;
690 char *consumed_old_str
;
696 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
697 if(channel_and_cpu
== NULL
) {
698 ERR("cannot parse channel");
703 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
704 if(consumed_old_str
== NULL
) {
705 ERR("cannot parse consumed_old");
707 goto free_channel_and_cpu
;
709 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
710 if(*endptr
!= '\0') {
711 ERR("invalid value for consumed_old");
713 goto free_consumed_old_str
;
716 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
718 ERR("problem parsing channel name");
720 goto free_short_chan_name
;
724 trace
= _ltt_trace_find(trace_name
);
727 DBG("Cannot find trace. It was likely destroyed by the user.");
728 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
730 ERR("ustcomm_send_reply failed");
738 for(i
=0; i
<trace
->nr_channels
; i
++) {
739 struct ust_channel
*channel
= &trace
->channels
[i
];
741 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
742 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
746 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
748 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
749 asprintf(&reply
, "%s", "ERROR");
752 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
753 asprintf(&reply
, "%s", "OK");
756 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
758 ERR("ustcomm_send_reply failed");
769 ERR("unable to find channel");
774 free_short_chan_name
:
776 free_consumed_old_str
:
777 free(consumed_old_str
);
778 free_channel_and_cpu
:
779 free(channel_and_cpu
);
785 static void listener_cleanup(void *ptr
)
787 ustcomm_fini_app(&ustcomm_app
, 0);
790 static void do_cmd_force_switch()
792 struct blocked_consumer
*bc
;
794 list_for_each_entry(bc
, &blocked_consumers
, list
) {
795 ltt_force_switch(bc
->buf
, FORCE_FLUSH
);
799 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
802 char trace_name
[] = "auto";
803 char trace_type
[] = "ustrelay";
806 DBG("received a message! it's: %s", recvbuf
);
807 len
= strlen(recvbuf
);
809 if(!strcmp(recvbuf
, "print_markers")) {
810 print_markers(stderr
);
812 else if(!strcmp(recvbuf
, "list_markers")) {
817 fp
= open_memstream(&ptr
, &size
);
821 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
825 else if(!strcmp(recvbuf
, "start")) {
826 /* start is an operation that setups the trace, allocates it and starts it */
827 result
= ltt_trace_setup(trace_name
);
829 ERR("ltt_trace_setup failed");
833 result
= ltt_trace_set_type(trace_name
, trace_type
);
835 ERR("ltt_trace_set_type failed");
839 result
= ltt_trace_alloc(trace_name
);
841 ERR("ltt_trace_alloc failed");
845 inform_consumer_daemon(trace_name
);
847 result
= ltt_trace_start(trace_name
);
849 ERR("ltt_trace_start failed");
853 else if(!strcmp(recvbuf
, "trace_setup")) {
856 result
= ltt_trace_setup(trace_name
);
858 ERR("ltt_trace_setup failed");
862 result
= ltt_trace_set_type(trace_name
, trace_type
);
864 ERR("ltt_trace_set_type failed");
868 else if(!strcmp(recvbuf
, "trace_alloc")) {
871 result
= ltt_trace_alloc(trace_name
);
873 ERR("ltt_trace_alloc failed");
876 inform_consumer_daemon(trace_name
);
878 else if(!strcmp(recvbuf
, "trace_create")) {
881 result
= ltt_trace_setup(trace_name
);
883 ERR("ltt_trace_setup failed");
887 result
= ltt_trace_set_type(trace_name
, trace_type
);
889 ERR("ltt_trace_set_type failed");
893 else if(!strcmp(recvbuf
, "trace_start")) {
896 result
= ltt_trace_alloc(trace_name
);
898 ERR("ltt_trace_alloc failed");
902 inform_consumer_daemon(trace_name
);
905 result
= ltt_trace_start(trace_name
);
907 ERR("ltt_trace_start failed");
911 else if(!strcmp(recvbuf
, "trace_stop")) {
914 result
= ltt_trace_stop(trace_name
);
916 ERR("ltt_trace_stop failed");
920 else if(!strcmp(recvbuf
, "trace_destroy")) {
922 DBG("trace destroy");
924 result
= ltt_trace_destroy(trace_name
, 0);
926 ERR("ltt_trace_destroy failed");
930 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
931 do_cmd_get_shmid(recvbuf
, src
);
933 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
934 do_cmd_get_n_subbufs(recvbuf
, src
);
936 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
937 do_cmd_get_subbuf_size(recvbuf
, src
);
939 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
942 libfile
= nth_token(recvbuf
, 1);
944 DBG("load_probe_lib loading %s", libfile
);
948 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
949 do_cmd_get_subbuffer(recvbuf
, src
);
951 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
952 do_cmd_put_subbuffer(recvbuf
, src
);
954 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
955 do_cmd_set_subbuf_size(recvbuf
, src
);
957 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
958 do_cmd_set_subbuf_num(recvbuf
, src
);
960 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
961 char *channel_slash_name
= nth_token(recvbuf
, 1);
962 char channel_name
[256]="";
963 char marker_name
[256]="";
965 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
967 if(channel_name
== NULL
|| marker_name
== NULL
) {
968 WARN("invalid marker name");
972 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
974 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
977 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
978 char *channel_slash_name
= nth_token(recvbuf
, 1);
982 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
984 if(marker_name
== NULL
) {
987 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
989 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
992 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
995 asprintf(&reply
, "%lld", pidunique
);
997 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
999 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1005 else if(nth_token_is(recvbuf
, "get_sock_path", 0) == 1) {
1006 char *reply
= getenv("UST_DAEMON_SOCKET");
1008 asprintf(&reply
, "%s/%s", SOCK_DIR
, "ustd");
1009 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1013 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1016 ERR("ustcomm_send_reply failed");
1018 else if(nth_token_is(recvbuf
, "set_sock_path", 0) == 1) {
1019 char *sock_path
= nth_token(recvbuf
, 1);
1020 result
= setenv("UST_DAEMON_SOCKET", sock_path
, 1);
1022 ERR("cannot set UST_DAEMON_SOCKET environment variable");
1024 else if(nth_token_is(recvbuf
, "force_switch", 0) == 1) {
1025 do_cmd_force_switch();
1028 ERR("unable to parse message: %s", recvbuf
);
1036 void *listener_main(void *p
)
1042 pthread_cleanup_push(listener_cleanup
, NULL
);
1045 struct mpentries mpent
;
1047 multipoll_init(&mpent
);
1049 blocked_consumers_add_to_mp(&mpent
);
1050 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1052 result
= multipoll_poll(&mpent
, -1);
1054 ERR("error in multipoll_poll");
1057 multipoll_destroy(&mpent
);
1060 pthread_cleanup_pop(1);
1063 /* These should only be accessed in the parent thread,
1066 static volatile sig_atomic_t have_listener
= 0;
1067 static pthread_t listener_thread
;
1069 void create_listener(void)
1072 sigset_t sig_all_blocked
;
1073 sigset_t orig_parent_mask
;
1076 WARN("not creating listener because we already had one");
1080 /* A new thread created by pthread_create inherits the signal mask
1081 * from the parent. To avoid any signal being received by the
1082 * listener thread, we block all signals temporarily in the parent,
1083 * while we create the listener thread.
1086 sigfillset(&sig_all_blocked
);
1088 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1090 PERROR("pthread_sigmask: %s", strerror(result
));
1093 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1095 PERROR("pthread_create");
1098 /* Restore original signal mask in parent */
1099 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1101 PERROR("pthread_sigmask: %s", strerror(result
));
1108 static int init_socket(void)
1110 return ustcomm_init_app(getpid(), &ustcomm_app
);
1113 #define AUTOPROBE_DISABLED 0
1114 #define AUTOPROBE_ENABLE_ALL 1
1115 #define AUTOPROBE_ENABLE_REGEX 2
1116 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1117 static regex_t autoprobe_regex
;
1119 static void auto_probe_connect(struct marker
*m
)
1123 char* concat_name
= NULL
;
1124 const char *probe_name
= "default";
1126 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1129 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1130 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1132 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1133 m
->channel
, m
->name
);
1136 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1143 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1144 if(result
&& result
!= -EEXIST
)
1145 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1147 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1151 static void __attribute__((constructor
)) init()
1154 char* autoprobe_val
= NULL
;
1155 char* subbuffer_size_val
= NULL
;
1156 char* subbuffer_count_val
= NULL
;
1157 unsigned int subbuffer_size
;
1158 unsigned int subbuffer_count
;
1161 /* Assign the pidunique, to be able to differentiate the processes with same
1162 * pid, (before and after an exec).
1164 pidunique
= make_pidunique();
1166 DBG("Tracectl constructor");
1168 result
= init_socket();
1170 ERR("init_socket error");
1176 autoprobe_val
= getenv("UST_AUTOPROBE");
1178 struct marker_iter iter
;
1180 DBG("Autoprobe enabled.");
1182 /* Ensure markers are initialized */
1185 /* Ensure marker control is initialized, for the probe */
1186 init_marker_control();
1188 /* first, set the callback that will connect the
1189 * probe on new markers
1191 if(autoprobe_val
[0] == '/') {
1192 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1196 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1197 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1198 /* don't crash the application just for this */
1201 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1205 /* just enable all instrumentation */
1206 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1209 marker_set_new_marker_cb(auto_probe_connect
);
1211 /* Now, connect the probes that were already registered. */
1212 marker_iter_reset(&iter
);
1213 marker_iter_start(&iter
);
1215 DBG("now iterating on markers already registered");
1216 while(iter
.marker
) {
1217 DBG("now iterating on marker %s", iter
.marker
->name
);
1218 auto_probe_connect(iter
.marker
);
1219 marker_iter_next(&iter
);
1223 if(getenv("UST_OVERWRITE")) {
1224 int val
= atoi(getenv("UST_OVERWRITE"));
1225 if(val
== 0 || val
== 1) {
1226 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1229 WARN("invalid value for UST_OVERWRITE");
1233 if(getenv("UST_AUTOCOLLECT")) {
1234 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1235 if(val
== 0 || val
== 1) {
1236 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1239 WARN("invalid value for UST_AUTOCOLLECT");
1243 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1244 if(subbuffer_size_val
) {
1245 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1246 power
= pow2_higher_or_eq(subbuffer_size
);
1247 if(power
!= subbuffer_size
)
1248 WARN("using the next power of two for buffer size = %u\n", power
);
1249 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1252 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1253 if(subbuffer_count_val
) {
1254 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1255 if(subbuffer_count
< 2)
1256 subbuffer_count
= 2;
1257 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1260 if(getenv("UST_TRACE")) {
1261 char trace_name
[] = "auto";
1262 char trace_type
[] = "ustrelay";
1264 DBG("starting early tracing");
1266 /* Ensure marker control is initialized */
1267 init_marker_control();
1269 /* Ensure markers are initialized */
1272 /* Ensure buffers are initialized, for the transport to be available.
1273 * We are about to set a trace type and it will fail without this.
1275 init_ustrelay_transport();
1277 /* FIXME: When starting early tracing (here), depending on the
1278 * order of constructors, it is very well possible some marker
1279 * sections are not yet registered. Because of this, some
1280 * channels may not be registered. Yet, we are about to ask the
1281 * daemon to collect the channels. Channels which are not yet
1282 * registered will not be collected.
1284 * Currently, in LTTng, there is no way to add a channel after
1285 * trace start. The reason for this is that it induces complex
1286 * concurrency issues on the trace structures, which can only
1287 * be resolved using RCU. This has not been done yet. As a
1288 * workaround, we are forcing the registration of the "ust"
1289 * channel here. This is the only channel (apart from metadata)
1290 * that can be reliably used in early tracing.
1292 * Non-early tracing does not have this problem and can use
1293 * arbitrary channel names.
1295 ltt_channels_register("ust");
1297 result
= ltt_trace_setup(trace_name
);
1299 ERR("ltt_trace_setup failed");
1303 result
= ltt_trace_set_type(trace_name
, trace_type
);
1305 ERR("ltt_trace_set_type failed");
1309 result
= ltt_trace_alloc(trace_name
);
1311 ERR("ltt_trace_alloc failed");
1315 result
= ltt_trace_start(trace_name
);
1317 ERR("ltt_trace_start failed");
1321 /* Do this after the trace is started in order to avoid creating confusion
1322 * if the trace fails to start. */
1323 inform_consumer_daemon(trace_name
);
1328 /* should decrementally destroy stuff if error */
1332 /* This is only called if we terminate normally, not with an unhandled signal,
1333 * so we cannot rely on it. However, for now, LTTV requires that the header of
1334 * the last sub-buffer contain a valid end time for the trace. This is done
1335 * automatically only when the trace is properly stopped.
1337 * If the traced program crashed, it is always possible to manually add the
1338 * right value in the header, or to open the trace in text mode.
1340 * FIXME: Fix LTTV so it doesn't need this.
1343 static void destroy_traces(void)
1347 /* if trace running, finish it */
1349 DBG("destructor stopping traces");
1351 result
= ltt_trace_stop("auto");
1353 ERR("ltt_trace_stop error");
1356 result
= ltt_trace_destroy("auto", 0);
1358 ERR("ltt_trace_destroy error");
1362 static int trace_recording(void)
1365 struct ust_trace
*trace
;
1369 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1376 ltt_unlock_traces();
1382 static int have_consumer(void)
1384 return !list_empty(&blocked_consumers
);
1388 int restarting_usleep(useconds_t usecs
)
1394 tv
.tv_nsec
= usecs
* 1000;
1397 result
= nanosleep(&tv
, &tv
);
1398 } while(result
== -1 && errno
== EINTR
);
1403 static void stop_listener(void)
1410 result
= pthread_cancel(listener_thread
);
1412 ERR("pthread_cancel: %s", strerror(result
));
1414 result
= pthread_join(listener_thread
, NULL
);
1416 ERR("pthread_join: %s", strerror(result
));
1420 /* This destructor keeps the process alive for a few seconds in order
1421 * to leave time to ustd to connect to its buffers. This is necessary
1422 * for programs whose execution is very short. It is also useful in all
1423 * programs when tracing is started close to the end of the program
1426 * FIXME: For now, this only works for the first trace created in a
1430 static void __attribute__((destructor
)) keepalive()
1432 if(trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1434 DBG("Keeping process alive for consumer daemon...");
1435 while(LOAD_SHARED(buffers_to_export
)) {
1436 const int interv
= 200000;
1437 restarting_usleep(interv
);
1440 if(total
>= 3000000) {
1441 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1445 DBG("Finally dying...");
1450 /* Ask the listener to stop and clean up. */
1454 void ust_potential_exec(void)
1456 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1463 /* Notify ust that there was a fork. This needs to be called inside
1464 * the new process, anytime a process whose memory is not shared with
1465 * the parent is created. If this function is not called, the events
1466 * of the new process will not be collected.
1468 * Signals should be disabled before the fork and reenabled only after
1469 * this call in order to guarantee tracing is not started before ust_fork()
1470 * sanitizes the new process.
1473 static void ust_fork(void)
1475 struct blocked_consumer
*bc
;
1476 struct blocked_consumer
*deletable_bc
= NULL
;
1479 /* FIXME: technically, the locks could have been taken before the fork */
1480 DBG("ust: forking");
1482 /* break lock if necessary */
1483 ltt_unlock_traces();
1485 ltt_trace_stop("auto");
1486 ltt_trace_destroy("auto", 1);
1487 /* Delete all active connections */
1488 ustcomm_close_all_connections(&ustcomm_app
.server
);
1490 /* Delete all blocked consumers */
1491 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1492 result
= close(bc
->fd_producer
);
1498 list_del(&bc
->list
);
1501 /* free app, keeping socket file */
1502 ustcomm_fini_app(&ustcomm_app
, 1);
1504 STORE_SHARED(buffers_to_export
, 0);
1508 ltt_trace_setup("auto");
1509 result
= ltt_trace_set_type("auto", "ustrelay");
1511 ERR("ltt_trace_set_type failed");
1515 ltt_trace_alloc("auto");
1516 ltt_trace_start("auto");
1517 inform_consumer_daemon("auto");
1520 void ust_before_fork(ust_fork_info_t
*fork_info
)
1522 /* Disable signals. This is to avoid that the child
1523 * intervenes before it is properly setup for tracing. It is
1524 * safer to disable all signals, because then we know we are not
1525 * breaking anything by restoring the original mask.
1531 - only do this if tracing is active
1534 /* Disable signals */
1535 sigfillset(&all_sigs
);
1536 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1538 PERROR("sigprocmask");
1543 /* Don't call this function directly in a traced program */
1544 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1548 /* Restore signals */
1549 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1551 PERROR("sigprocmask");
1556 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1558 /* Reenable signals */
1559 ust_after_fork_common(fork_info
);
1562 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1564 /* First sanitize the child */
1567 /* Then reenable interrupts */
1568 ust_after_fork_common(fork_info
);