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
30 #include <sys/epoll.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
37 #include <urcu/uatomic_arch.h>
38 #include <urcu/list.h>
40 #include <ust/marker.h>
41 #include <ust/tracepoint.h>
42 #include <ust/tracepoint-internal.h>
43 #include <ust/tracectl.h>
44 #include <ust/clock.h>
46 #include "usterr_signal_safe.h"
49 #include "marker-control.h"
51 /* This should only be accessed by the constructor, before the creation
52 * of the listener, and then only by the listener.
56 /* The process pid is used to detect a non-traceable fork
57 * and allow the non-traceable fork to be ignored
58 * by destructor sequences in libust
60 static pid_t processpid
= 0;
62 static struct ustcomm_header _receive_header
;
63 static struct ustcomm_header
*receive_header
= &_receive_header
;
64 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
65 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
70 * Listener thread data vs fork() protection mechanism. Ensures that no listener
71 * thread mutexes and data structures are being concurrently modified or held by
72 * other threads when fork() is executed.
74 static pthread_mutex_t listener_thread_data_mutex
= PTHREAD_MUTEX_INITIALIZER
;
76 /* Mutex protecting listen_sock. Nests inside listener_thread_data_mutex. */
77 static pthread_mutex_t listen_sock_mutex
= PTHREAD_MUTEX_INITIALIZER
;
78 static struct ustcomm_sock
*listen_sock
;
80 extern struct chan_info_struct chan_infos
[];
82 static struct cds_list_head ust_socks
= CDS_LIST_HEAD_INIT(ust_socks
);
84 /* volatile because shared between the listener and the main thread */
85 int buffers_to_export
= 0;
89 static long long make_pidunique(void)
94 gettimeofday(&tv
, NULL
);
103 static void print_ust_marker(FILE *fp
)
105 struct ust_marker_iter iter
;
108 ust_marker_iter_reset(&iter
);
109 ust_marker_iter_start(&iter
);
111 while (iter
.ust_marker
) {
112 fprintf(fp
, "ust_marker: %s/%s %d \"%s\" %p\n",
113 (*iter
.ust_marker
)->channel
,
114 (*iter
.ust_marker
)->name
,
115 (int)(*iter
.ust_marker
)->state
,
116 (*iter
.ust_marker
)->format
,
118 * location is null for now, will be added
119 * to a different table.
121 ust_marker_iter_next(&iter
);
126 static void print_trace_events(FILE *fp
)
128 struct trace_event_iter iter
;
131 trace_event_iter_reset(&iter
);
132 trace_event_iter_start(&iter
);
134 while (iter
.trace_event
) {
135 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
136 trace_event_iter_next(&iter
);
138 unlock_trace_events();
141 static int connect_ustconsumer(void)
144 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
145 char *explicit_daemon_path
, *daemon_path
;
147 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
148 if (explicit_daemon_path
) {
149 daemon_path
= explicit_daemon_path
;
151 daemon_path
= default_daemon_path
;
154 DBG("Connecting to daemon_path %s", daemon_path
);
156 result
= ustcomm_connect_path(daemon_path
, &fd
);
158 WARN("connect_ustconsumer failed, daemon_path: %s",
167 static void request_buffer_consumer(int sock
,
172 struct ustcomm_header send_header
, recv_header
;
173 struct ustcomm_buffer_info buf_inf
;
176 result
= ustcomm_pack_buffer_info(&send_header
,
183 ERR("failed to pack buffer info message %s_%d",
188 buf_inf
.pid
= getpid();
189 send_header
.command
= CONSUME_BUFFER
;
191 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
194 PERROR("request for buffer consumer failed, is the daemon online?");
200 /* Ask the daemon to collect a trace called trace_name and being
201 * produced by this pid.
203 * The trace must be at least allocated. (It can also be started.)
204 * This is because _ltt_trace_find is used.
207 static void inform_consumer_daemon(const char *trace_name
)
210 struct ust_trace
*trace
;
213 sock
= connect_ustconsumer();
218 DBG("Connected to ustconsumer");
222 trace
= _ltt_trace_find(trace_name
);
224 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
228 for (i
=0; i
< trace
->nr_channels
; i
++) {
229 if (trace
->channels
[i
].request_collection
) {
230 /* iterate on all cpus */
231 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
232 ch_name
= trace
->channels
[i
].channel_name
;
233 request_buffer_consumer(sock
, trace_name
,
235 CMM_STORE_SHARED(buffers_to_export
,
236 CMM_LOAD_SHARED(buffers_to_export
)+1);
247 static struct ust_channel
*find_channel(const char *ch_name
,
248 struct ust_trace
*trace
)
252 for (i
=0; i
<trace
->nr_channels
; i
++) {
253 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
254 return &trace
->channels
[i
];
261 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
264 int *buf_struct_shmid
,
267 struct ust_trace
*trace
;
268 struct ust_channel
*channel
;
269 struct ust_buffer
*buf
;
271 DBG("get_buffer_shmid_pipe_fd");
274 trace
= _ltt_trace_find(trace_name
);
278 ERR("cannot find trace!");
282 channel
= find_channel(ch_name
, trace
);
284 ERR("cannot find channel %s!", ch_name
);
288 buf
= channel
->buf
[ch_cpu
];
290 *buf_shmid
= buf
->shmid
;
291 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
292 *buf_pipe_fd
= buf
->data_ready_fd_read
;
297 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
300 struct ust_trace
*trace
;
301 struct ust_channel
*channel
;
303 DBG("get_subbuf_size");
306 trace
= _ltt_trace_find(trace_name
);
310 ERR("cannot find trace!");
314 channel
= find_channel(ch_name
, trace
);
316 ERR("unable to find channel");
320 *num
= channel
->subbuf_cnt
;
321 *size
= channel
->subbuf_size
;
326 /* Return the power of two which is equal or higher to v */
328 static unsigned int pow2_higher_or_eq(unsigned int v
)
331 int retval
= 1<<(hb
-1);
339 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
344 struct ust_trace
*trace
;
345 struct ust_channel
*channel
;
347 DBG("set_subbuf_size");
349 power
= pow2_higher_or_eq(size
);
350 power
= max_t(unsigned int, 2u, power
);
352 WARN("using the next power of two for buffer size = %u\n", power
);
356 trace
= _ltt_trace_find_setup(trace_name
);
358 ERR("cannot find trace!");
363 channel
= find_channel(ch_name
, trace
);
365 ERR("unable to find channel");
370 channel
->subbuf_size
= power
;
371 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
379 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
382 struct ust_trace
*trace
;
383 struct ust_channel
*channel
;
386 DBG("set_subbuf_num");
389 ERR("subbuffer count should be greater than 2");
394 trace
= _ltt_trace_find_setup(trace_name
);
396 ERR("cannot find trace!");
401 channel
= find_channel(ch_name
, trace
);
403 ERR("unable to find channel");
408 channel
->subbuf_cnt
= num
;
409 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
416 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
417 int ch_cpu
, long *consumed_old
)
420 struct ust_trace
*trace
;
421 struct ust_channel
*channel
;
422 struct ust_buffer
*buf
;
429 trace
= _ltt_trace_find(trace_name
);
432 DBG("Cannot find trace. It was likely destroyed by the user.");
437 channel
= find_channel(ch_name
, trace
);
439 ERR("unable to find channel");
444 buf
= channel
->buf
[ch_cpu
];
446 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
448 WARN("missed buffer?");
458 static int notify_buffer_mapped(const char *trace_name
,
463 struct ust_trace
*trace
;
464 struct ust_channel
*channel
;
465 struct ust_buffer
*buf
;
467 DBG("get_buffer_fd");
470 trace
= _ltt_trace_find(trace_name
);
474 DBG("Cannot find trace. It was likely destroyed by the user.");
478 channel
= find_channel(ch_name
, trace
);
481 ERR("unable to find channel");
485 buf
= channel
->buf
[ch_cpu
];
487 /* Being here is the proof the daemon has mapped the buffer in its
488 * memory. We may now decrement buffers_to_export.
490 if (uatomic_read(&buf
->consumed
) == 0) {
491 DBG("decrementing buffers_to_export");
492 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
501 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
502 int ch_cpu
, long consumed_old
)
505 struct ust_trace
*trace
;
506 struct ust_channel
*channel
;
507 struct ust_buffer
*buf
;
512 trace
= _ltt_trace_find(trace_name
);
516 DBG("Cannot find trace. It was likely destroyed by the user.");
520 channel
= find_channel(ch_name
, trace
);
523 ERR("unable to find channel");
527 buf
= channel
->buf
[ch_cpu
];
529 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
531 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
534 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
544 static void release_listener_mutex(void *ptr
)
546 pthread_mutex_unlock(&listener_thread_data_mutex
);
549 static void listener_cleanup(void *ptr
)
551 pthread_mutex_lock(&listen_sock_mutex
);
553 ustcomm_del_named_sock(listen_sock
, 0);
556 pthread_mutex_unlock(&listen_sock_mutex
);
559 static int force_subbuf_switch(const char *trace_name
)
561 struct ust_trace
*trace
;
562 int i
, j
, retval
= 0;
565 trace
= _ltt_trace_find(trace_name
);
568 DBG("Cannot find trace. It was likely destroyed by the user.");
572 for (i
= 0; i
< trace
->nr_channels
; i
++) {
573 for (j
= 0; j
< trace
->channels
[i
].n_cpus
; j
++) {
574 ltt_force_switch(trace
->channels
[i
].buf
[j
],
585 static int process_trace_cmd(int command
, char *trace_name
)
588 char trace_type
[] = "ustrelay";
592 /* start is an operation that setups the trace, allocates it and starts it */
593 result
= ltt_trace_setup(trace_name
);
595 ERR("ltt_trace_setup failed");
599 result
= ltt_trace_set_type(trace_name
, trace_type
);
601 ERR("ltt_trace_set_type failed");
605 result
= ltt_trace_alloc(trace_name
);
607 ERR("ltt_trace_alloc failed");
611 inform_consumer_daemon(trace_name
);
613 result
= ltt_trace_start(trace_name
);
615 ERR("ltt_trace_start failed");
623 result
= ltt_trace_setup(trace_name
);
625 ERR("ltt_trace_setup failed");
629 result
= ltt_trace_set_type(trace_name
, trace_type
);
631 ERR("ltt_trace_set_type failed");
639 result
= ltt_trace_alloc(trace_name
);
641 ERR("ltt_trace_alloc failed");
644 inform_consumer_daemon(trace_name
);
651 result
= ltt_trace_setup(trace_name
);
653 ERR("ltt_trace_setup failed");
657 result
= ltt_trace_set_type(trace_name
, trace_type
);
659 ERR("ltt_trace_set_type failed");
667 result
= ltt_trace_alloc(trace_name
);
669 ERR("ltt_trace_alloc failed");
673 inform_consumer_daemon(trace_name
);
676 result
= ltt_trace_start(trace_name
);
678 ERR("ltt_trace_start failed");
686 result
= ltt_trace_stop(trace_name
);
688 ERR("ltt_trace_stop failed");
694 DBG("trace destroy");
696 result
= ltt_trace_destroy(trace_name
, 0);
698 ERR("ltt_trace_destroy failed");
702 case FORCE_SUBBUF_SWITCH
:
705 result
= force_subbuf_switch(trace_name
);
707 ERR("force_subbuf_switch failed");
717 static void process_channel_cmd(int sock
, int command
,
718 struct ustcomm_channel_info
*ch_inf
)
720 struct ustcomm_header _reply_header
;
721 struct ustcomm_header
*reply_header
= &_reply_header
;
722 struct ustcomm_channel_info
*reply_msg
=
723 (struct ustcomm_channel_info
*)send_buffer
;
724 int result
, offset
= 0, num
, size
;
726 memset(reply_header
, 0, sizeof(*reply_header
));
729 case GET_SUBBUF_NUM_SIZE
:
730 result
= get_subbuf_num_size(ch_inf
->trace
,
734 reply_header
->result
= result
;
738 reply_msg
->channel
= USTCOMM_POISON_PTR
;
739 reply_msg
->subbuf_num
= num
;
740 reply_msg
->subbuf_size
= size
;
743 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
747 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
752 case SET_SUBBUF_SIZE
:
753 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
755 ch_inf
->subbuf_size
);
760 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
761 ERR("ustcomm_send failed");
765 static void process_buffer_cmd(int sock
, int command
,
766 struct ustcomm_buffer_info
*buf_inf
)
768 struct ustcomm_header _reply_header
;
769 struct ustcomm_header
*reply_header
= &_reply_header
;
770 struct ustcomm_buffer_info
*reply_msg
=
771 (struct ustcomm_buffer_info
*)send_buffer
;
772 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
775 memset(reply_header
, 0, sizeof(*reply_header
));
778 case GET_BUF_SHMID_PIPE_FD
:
779 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
786 reply_header
->result
= result
;
790 reply_msg
->channel
= USTCOMM_POISON_PTR
;
791 reply_msg
->buf_shmid
= buf_shmid
;
792 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
794 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
795 reply_header
->fd_included
= 1;
797 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
799 ERR("ustcomm_send failed");
803 case NOTIFY_BUF_MAPPED
:
804 reply_header
->result
=
805 notify_buffer_mapped(buf_inf
->trace
,
810 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
811 buf_inf
->ch_cpu
, &consumed_old
);
813 reply_header
->result
= result
;
817 reply_msg
->channel
= USTCOMM_POISON_PTR
;
818 reply_msg
->consumed_old
= consumed_old
;
820 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
824 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
826 buf_inf
->consumed_old
);
827 reply_header
->result
= result
;
832 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
833 ERR("ustcomm_send failed");
838 static void process_ust_marker_cmd(int sock
, int command
,
839 struct ustcomm_ust_marker_info
*ust_marker_inf
)
841 struct ustcomm_header _reply_header
;
842 struct ustcomm_header
*reply_header
= &_reply_header
;
845 memset(reply_header
, 0, sizeof(*reply_header
));
850 result
= ltt_ust_marker_connect(ust_marker_inf
->channel
,
851 ust_marker_inf
->ust_marker
,
854 WARN("could not enable ust_marker; channel=%s,"
856 ust_marker_inf
->channel
,
857 ust_marker_inf
->ust_marker
);
862 result
= ltt_ust_marker_disconnect(ust_marker_inf
->channel
,
863 ust_marker_inf
->ust_marker
,
866 WARN("could not disable ust_marker; channel=%s,"
868 ust_marker_inf
->channel
,
869 ust_marker_inf
->ust_marker
);
874 reply_header
->result
= result
;
876 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
877 ERR("ustcomm_send failed");
881 static void process_client_cmd(struct ustcomm_header
*recv_header
,
882 char *recv_buf
, int sock
)
885 struct ustcomm_header _reply_header
;
886 struct ustcomm_header
*reply_header
= &_reply_header
;
887 char *send_buf
= send_buffer
;
889 memset(reply_header
, 0, sizeof(*reply_header
));
890 memset(send_buf
, 0, sizeof(send_buffer
));
892 switch(recv_header
->command
) {
893 case GET_SUBBUF_NUM_SIZE
:
895 case SET_SUBBUF_SIZE
:
897 struct ustcomm_channel_info
*ch_inf
;
898 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
899 result
= ustcomm_unpack_channel_info(ch_inf
);
901 ERR("couldn't unpack channel info");
902 reply_header
->result
= -EINVAL
;
905 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
908 case GET_BUF_SHMID_PIPE_FD
:
909 case NOTIFY_BUF_MAPPED
:
913 struct ustcomm_buffer_info
*buf_inf
;
914 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
915 result
= ustcomm_unpack_buffer_info(buf_inf
);
917 ERR("couldn't unpack buffer info");
918 reply_header
->result
= -EINVAL
;
921 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
927 struct ustcomm_ust_marker_info
*ust_marker_inf
;
928 ust_marker_inf
= (struct ustcomm_ust_marker_info
*)recv_buf
;
929 result
= ustcomm_unpack_ust_marker_info(ust_marker_inf
);
931 ERR("couldn't unpack ust_marker info");
932 reply_header
->result
= -EINVAL
;
935 process_ust_marker_cmd(sock
, recv_header
->command
, ust_marker_inf
);
944 fp
= open_memstream(&ptr
, &size
);
946 ERR("opening memstream failed");
949 print_ust_marker(fp
);
952 reply_header
->size
= size
+ 1; /* Include final \0 */
954 result
= ustcomm_send(sock
, reply_header
, ptr
);
959 PERROR("failed to send ust_marker list");
964 case LIST_TRACE_EVENTS
:
970 fp
= open_memstream(&ptr
, &size
);
972 ERR("opening memstream failed");
975 print_trace_events(fp
);
978 reply_header
->size
= size
+ 1; /* Include final \0 */
980 result
= ustcomm_send(sock
, reply_header
, ptr
);
985 ERR("list_trace_events failed");
995 /* FIXME: No functionality at all... */
998 DBG("load_probe_lib loading %s", libfile
);
1004 struct ustcomm_pidunique
*pid_msg
;
1005 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
1007 pid_msg
->pidunique
= pidunique
;
1008 reply_header
->size
= sizeof(pid_msg
);
1015 struct ustcomm_single_field
*sock_msg
;
1016 char *sock_path_env
;
1018 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1020 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1022 if (!sock_path_env
) {
1023 result
= ustcomm_pack_single_field(reply_header
,
1025 SOCK_DIR
"/ustconsumer");
1028 result
= ustcomm_pack_single_field(reply_header
,
1032 reply_header
->result
= result
;
1038 struct ustcomm_single_field
*sock_msg
;
1039 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
1040 result
= ustcomm_unpack_single_field(sock_msg
);
1042 reply_header
->result
= -EINVAL
;
1046 reply_header
->result
= setenv("UST_DAEMON_SOCKET",
1047 sock_msg
->field
, 1);
1058 case FORCE_SUBBUF_SWITCH
:
1060 struct ustcomm_single_field
*trace_inf
=
1061 (struct ustcomm_single_field
*)recv_buf
;
1063 result
= ustcomm_unpack_single_field(trace_inf
);
1065 ERR("couldn't unpack trace info");
1066 reply_header
->result
= -EINVAL
;
1070 reply_header
->result
=
1071 process_trace_cmd(recv_header
->command
,
1077 reply_header
->result
= -EINVAL
;
1085 ustcomm_send(sock
, reply_header
, send_buf
);
1088 #define MAX_EVENTS 10
1090 void *listener_main(void *p
)
1092 struct ustcomm_sock
*epoll_sock
;
1093 struct epoll_event events
[MAX_EVENTS
];
1094 struct sockaddr addr
;
1095 int accept_fd
, nfds
, result
, i
, addr_size
;
1099 pthread_cleanup_push(listener_cleanup
, NULL
);
1102 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1104 PERROR("listener_main: epoll_wait failed");
1108 for (i
= 0; i
< nfds
; i
++) {
1109 pthread_mutex_lock(&listener_thread_data_mutex
);
1110 pthread_cleanup_push(release_listener_mutex
, NULL
);
1111 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1112 if (epoll_sock
== listen_sock
) {
1113 addr_size
= sizeof(struct sockaddr
);
1114 accept_fd
= accept(epoll_sock
->fd
,
1116 (socklen_t
*)&addr_size
);
1117 if (accept_fd
== -1) {
1118 PERROR("listener_main: accept failed");
1121 ustcomm_init_sock(accept_fd
, epoll_fd
,
1124 memset(receive_header
, 0,
1125 sizeof(*receive_header
));
1126 memset(receive_buffer
, 0,
1127 sizeof(receive_buffer
));
1128 result
= ustcomm_recv(epoll_sock
->fd
,
1132 ustcomm_del_sock(epoll_sock
, 0);
1134 process_client_cmd(receive_header
,
1139 pthread_cleanup_pop(1); /* release listener mutex */
1143 pthread_cleanup_pop(1);
1146 /* These should only be accessed in the parent thread,
1149 static volatile sig_atomic_t have_listener
= 0;
1150 static pthread_t listener_thread
;
1152 void create_listener(void)
1155 sigset_t sig_all_blocked
;
1156 sigset_t orig_parent_mask
;
1158 if (have_listener
) {
1159 WARN("not creating listener because we already had one");
1163 /* A new thread created by pthread_create inherits the signal mask
1164 * from the parent. To avoid any signal being received by the
1165 * listener thread, we block all signals temporarily in the parent,
1166 * while we create the listener thread.
1169 sigfillset(&sig_all_blocked
);
1171 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1173 PERROR("pthread_sigmask: %s", strerror(result
));
1176 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1178 PERROR("pthread_create");
1181 /* Restore original signal mask in parent */
1182 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1184 PERROR("pthread_sigmask: %s", strerror(result
));
1190 #define AUTOPROBE_DISABLED 0
1191 #define AUTOPROBE_ENABLE_ALL 1
1192 #define AUTOPROBE_ENABLE_REGEX 2
1193 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1194 static regex_t autoprobe_regex
;
1196 static void auto_probe_connect(struct ust_marker
*m
)
1200 char* concat_name
= NULL
;
1201 const char *probe_name
= "default";
1203 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1205 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1206 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1208 ERR("auto_probe_connect: asprintf failed (ust_marker %s/%s)",
1209 m
->channel
, m
->name
);
1212 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1219 result
= ltt_ust_marker_connect(m
->channel
, m
->name
, probe_name
);
1220 if (result
&& result
!= -EEXIST
)
1221 ERR("ltt_ust_marker_connect (ust_marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1223 DBG("auto connected ust_marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1227 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1229 char *dir_name
, *sock_name
;
1231 struct ustcomm_sock
*sock
= NULL
;
1234 dir_name
= ustcomm_user_sock_dir();
1238 mtime
= ustcomm_pid_st_mtime(getpid());
1243 result
= asprintf(&sock_name
, "%s/%d.%ld", dir_name
,
1244 (int) getpid(), (long) mtime
);
1246 ERR("string overflow allocating socket name, "
1247 "UST thread bailing");
1251 result
= ensure_dir_exists(dir_name
, S_IRWXU
);
1253 ERR("Unable to create socket directory %s, UST thread bailing",
1255 goto free_sock_name
;
1258 sock
= ustcomm_init_named_socket(sock_name
, epoll_fd
);
1260 ERR("Error initializing named socket (%s). Check that directory"
1261 "exists and that it is writable. UST thread bailing", sock_name
);
1262 goto free_sock_name
;
1273 static void __attribute__((constructor
)) init()
1277 char* autoprobe_val
= NULL
;
1278 char* subbuffer_size_val
= NULL
;
1279 char* subbuffer_count_val
= NULL
;
1280 unsigned int subbuffer_size
;
1281 unsigned int subbuffer_count
;
1284 /* Assign the pidunique, to be able to differentiate the processes with same
1285 * pid, (before and after an exec).
1287 pidunique
= make_pidunique();
1288 processpid
= getpid();
1290 DBG("Tracectl constructor");
1293 epoll_fd
= epoll_create(MAX_EVENTS
);
1294 if (epoll_fd
== -1) {
1295 ERR("epoll_create failed, tracing shutting down");
1299 /* Create the socket */
1300 listen_sock
= init_app_socket(epoll_fd
);
1302 ERR("failed to create application socket,"
1303 " tracing shutting down");
1309 /* Get clock the clock source type */
1311 /* Default clock source */
1312 ust_clock_source
= CLOCK_TRACE
;
1313 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1314 ust_clock_source
= CLOCK_MONOTONIC
;
1315 DBG("UST traces will not be synchronized with LTTng traces");
1318 autoprobe_val
= getenv("UST_AUTOPROBE");
1319 if (autoprobe_val
) {
1320 struct ust_marker_iter iter
;
1322 DBG("Autoprobe enabled.");
1324 /* Ensure ust_marker are initialized */
1325 //init_ust_marker();
1327 /* Ensure ust_marker control is initialized, for the probe */
1328 init_ust_marker_control();
1330 /* first, set the callback that will connect the
1331 * probe on new ust_marker
1333 if (autoprobe_val
[0] == '/') {
1334 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1338 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1339 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1340 /* don't crash the application just for this */
1342 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1345 /* just enable all instrumentation */
1346 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1349 ust_marker_set_new_ust_marker_cb(auto_probe_connect
);
1351 /* Now, connect the probes that were already registered. */
1352 ust_marker_iter_reset(&iter
);
1353 ust_marker_iter_start(&iter
);
1355 DBG("now iterating on ust_marker already registered");
1356 while (iter
.ust_marker
) {
1357 DBG("now iterating on ust_marker %s", (*iter
.ust_marker
)->name
);
1358 auto_probe_connect(*iter
.ust_marker
);
1359 ust_marker_iter_next(&iter
);
1363 if (getenv("UST_OVERWRITE")) {
1364 int val
= atoi(getenv("UST_OVERWRITE"));
1365 if (val
== 0 || val
== 1) {
1366 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1368 WARN("invalid value for UST_OVERWRITE");
1372 if (getenv("UST_AUTOCOLLECT")) {
1373 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1374 if (val
== 0 || val
== 1) {
1375 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1377 WARN("invalid value for UST_AUTOCOLLECT");
1381 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1382 if (subbuffer_size_val
) {
1383 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1384 power
= pow2_higher_or_eq(subbuffer_size
);
1385 if (power
!= subbuffer_size
)
1386 WARN("using the next power of two for buffer size = %u\n", power
);
1387 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1390 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1391 if (subbuffer_count_val
) {
1392 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1393 if (subbuffer_count
< 2)
1394 subbuffer_count
= 2;
1395 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1398 if (getenv("UST_TRACE")) {
1399 char trace_name
[] = "auto";
1400 char trace_type
[] = "ustrelay";
1402 DBG("starting early tracing");
1404 /* Ensure ust_marker control is initialized */
1405 init_ust_marker_control();
1407 /* Ensure ust_marker are initialized */
1410 /* Ensure buffers are initialized, for the transport to be available.
1411 * We are about to set a trace type and it will fail without this.
1413 init_ustrelay_transport();
1415 /* FIXME: When starting early tracing (here), depending on the
1416 * order of constructors, it is very well possible some ust_marker
1417 * sections are not yet registered. Because of this, some
1418 * channels may not be registered. Yet, we are about to ask the
1419 * daemon to collect the channels. Channels which are not yet
1420 * registered will not be collected.
1422 * Currently, in LTTng, there is no way to add a channel after
1423 * trace start. The reason for this is that it induces complex
1424 * concurrency issues on the trace structures, which can only
1425 * be resolved using RCU. This has not been done yet. As a
1426 * workaround, we are forcing the registration of the "ust"
1427 * channel here. This is the only channel (apart from metadata)
1428 * that can be reliably used in early tracing.
1430 * Non-early tracing does not have this problem and can use
1431 * arbitrary channel names.
1433 ltt_channels_register("ust");
1435 result
= ltt_trace_setup(trace_name
);
1437 ERR("ltt_trace_setup failed");
1441 result
= ltt_trace_set_type(trace_name
, trace_type
);
1443 ERR("ltt_trace_set_type failed");
1447 result
= ltt_trace_alloc(trace_name
);
1449 ERR("ltt_trace_alloc failed");
1453 result
= ltt_trace_start(trace_name
);
1455 ERR("ltt_trace_start failed");
1459 /* Do this after the trace is started in order to avoid creating confusion
1460 * if the trace fails to start. */
1461 inform_consumer_daemon(trace_name
);
1466 /* should decrementally destroy stuff if error */
1470 /* This is only called if we terminate normally, not with an unhandled signal,
1471 * so we cannot rely on it. However, for now, LTTV requires that the header of
1472 * the last sub-buffer contain a valid end time for the trace. This is done
1473 * automatically only when the trace is properly stopped.
1475 * If the traced program crashed, it is always possible to manually add the
1476 * right value in the header, or to open the trace in text mode.
1478 * FIXME: Fix LTTV so it doesn't need this.
1481 static void destroy_traces(void)
1485 /* if trace running, finish it */
1487 DBG("destructor stopping traces");
1489 result
= ltt_trace_stop("auto");
1491 ERR("ltt_trace_stop error");
1494 result
= ltt_trace_destroy("auto", 0);
1496 ERR("ltt_trace_destroy error");
1500 static int trace_recording(void)
1503 struct ust_trace
*trace
;
1507 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1508 if (trace
->active
) {
1514 ltt_unlock_traces();
1519 int restarting_usleep(useconds_t usecs
)
1525 tv
.tv_nsec
= usecs
* 1000;
1528 result
= nanosleep(&tv
, &tv
);
1529 } while (result
== -1 && errno
== EINTR
);
1534 static void stop_listener(void)
1541 result
= pthread_cancel(listener_thread
);
1543 ERR("pthread_cancel: %s", strerror(result
));
1545 result
= pthread_join(listener_thread
, NULL
);
1547 ERR("pthread_join: %s", strerror(result
));
1551 /* This destructor keeps the process alive for a few seconds in order
1552 * to leave time for ustconsumer to connect to its buffers. This is necessary
1553 * for programs whose execution is very short. It is also useful in all
1554 * programs when tracing is started close to the end of the program
1557 * FIXME: For now, this only works for the first trace created in a
1561 static void __attribute__((destructor
)) keepalive()
1563 if (processpid
!= getpid()) {
1567 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1569 DBG("Keeping process alive for consumer daemon...");
1570 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1571 const int interv
= 200000;
1572 restarting_usleep(interv
);
1575 if (total
>= 3000000) {
1576 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1580 DBG("Finally dying...");
1585 /* Ask the listener to stop and clean up. */
1589 void ust_potential_exec(void)
1591 ust_marker(potential_exec
, UST_MARKER_NOARGS
);
1598 /* Notify ust that there was a fork. This needs to be called inside
1599 * the new process, anytime a process whose memory is not shared with
1600 * the parent is created. If this function is not called, the events
1601 * of the new process will not be collected.
1603 * Signals should be disabled before the fork and reenabled only after
1604 * this call in order to guarantee tracing is not started before ust_fork()
1605 * sanitizes the new process.
1608 static void ust_fork(void)
1610 struct ustcomm_sock
*sock
, *sock_tmp
;
1611 struct ust_trace
*trace
, *trace_tmp
;
1614 /* FIXME: technically, the locks could have been taken before the fork */
1615 DBG("ust: forking");
1617 /* Get the pid of the new process */
1618 processpid
= getpid();
1621 * FIXME: This could be prettier, we loop over the list twice and
1622 * following good locking practice should lock around the loop
1624 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1625 ltt_trace_stop(trace
->trace_name
);
1628 /* Delete all active connections, but leave them in the epoll set */
1629 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1630 ustcomm_del_sock(sock
, 1);
1634 * FIXME: This could be prettier, we loop over the list twice and
1635 * following good locking practice should lock around the loop
1637 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1638 ltt_trace_destroy(trace
->trace_name
, 1);
1641 /* Clean up the listener socket and epoll, keeping the socket file */
1643 ustcomm_del_named_sock(listen_sock
, 1);
1648 /* Re-start the launch sequence */
1649 CMM_STORE_SHARED(buffers_to_export
, 0);
1653 epoll_fd
= epoll_create(MAX_EVENTS
);
1654 if (epoll_fd
== -1) {
1655 ERR("epoll_create failed, tracing shutting down");
1659 /* Create the socket */
1660 listen_sock
= init_app_socket(epoll_fd
);
1662 ERR("failed to create application socket,"
1663 " tracing shutting down");
1667 ltt_trace_setup("auto");
1668 result
= ltt_trace_set_type("auto", "ustrelay");
1670 ERR("ltt_trace_set_type failed");
1674 ltt_trace_alloc("auto");
1675 ltt_trace_start("auto");
1676 inform_consumer_daemon("auto");
1679 void ust_before_fork(ust_fork_info_t
*fork_info
)
1681 /* Disable signals. This is to avoid that the child
1682 * intervenes before it is properly setup for tracing. It is
1683 * safer to disable all signals, because then we know we are not
1684 * breaking anything by restoring the original mask.
1690 - only do this if tracing is active
1693 /* Disable signals */
1694 sigfillset(&all_sigs
);
1695 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1697 PERROR("sigprocmask");
1702 * Take the fork lock to make sure we are not in the middle of
1703 * something in the listener thread.
1705 pthread_mutex_lock(&listener_thread_data_mutex
);
1707 * Hold listen_sock_mutex to protect from listen_sock teardown.
1709 pthread_mutex_lock(&listen_sock_mutex
);
1710 rcu_bp_before_fork();
1713 /* Don't call this function directly in a traced program */
1714 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1718 pthread_mutex_unlock(&listen_sock_mutex
);
1719 pthread_mutex_unlock(&listener_thread_data_mutex
);
1721 /* Restore signals */
1722 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1724 PERROR("sigprocmask");
1729 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1731 rcu_bp_after_fork_parent();
1732 /* Release mutexes and reenable signals */
1733 ust_after_fork_common(fork_info
);
1736 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1738 /* Release urcu mutexes */
1739 rcu_bp_after_fork_child();
1741 /* Sanitize the child */
1744 /* Then release mutexes and reenable signals */
1745 ust_after_fork_common(fork_info
);