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/epoll.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <urcu/uatomic_arch.h>
37 #include <urcu/list.h>
39 #include <ust/marker.h>
40 #include <ust/tracepoint.h>
41 #include <ust/tracectl.h>
42 #include <ust/clock.h>
47 #include "marker-control.h"
49 /* This should only be accessed by the constructor, before the creation
50 * of the listener, and then only by the listener.
54 /* The process pid is used to detect a non-traceable fork
55 * and allow the non-traceable fork to be ignored
56 * by destructor sequences in libust
58 static pid_t processpid
= 0;
60 static struct ustcomm_header _receive_header
;
61 static struct ustcomm_header
*receive_header
= &_receive_header
;
62 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
63 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
68 * Listener thread data vs fork() protection mechanism. Ensures that no listener
69 * thread mutexes and data structures are being concurrently modified or held by
70 * other threads when fork() is executed.
72 static pthread_mutex_t listener_thread_data_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 /* Mutex protecting listen_sock. Nests inside listener_thread_data_mutex. */
75 static pthread_mutex_t listen_sock_mutex
= PTHREAD_MUTEX_INITIALIZER
;
76 static struct ustcomm_sock
*listen_sock
;
78 extern struct chan_info_struct chan_infos
[];
80 static struct cds_list_head open_buffers_list
= CDS_LIST_HEAD_INIT(open_buffers_list
);
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_markers(FILE *fp
)
105 struct marker_iter iter
;
108 marker_iter_reset(&iter
);
109 marker_iter_start(&iter
);
111 while (iter
.marker
) {
112 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
113 (*iter
.marker
)->channel
,
114 (*iter
.marker
)->name
,
115 (int)imv_read((*iter
.marker
)->state
),
116 (*iter
.marker
)->format
,
117 (*iter
.marker
)->location
);
118 marker_iter_next(&iter
);
123 static void print_trace_events(FILE *fp
)
125 struct trace_event_iter iter
;
128 trace_event_iter_reset(&iter
);
129 trace_event_iter_start(&iter
);
131 while (iter
.trace_event
) {
132 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
133 trace_event_iter_next(&iter
);
135 unlock_trace_events();
138 static int connect_ustconsumer(void)
141 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
142 char *explicit_daemon_path
, *daemon_path
;
144 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
145 if (explicit_daemon_path
) {
146 daemon_path
= explicit_daemon_path
;
148 daemon_path
= default_daemon_path
;
151 DBG("Connecting to daemon_path %s", daemon_path
);
153 result
= ustcomm_connect_path(daemon_path
, &fd
);
155 WARN("connect_ustconsumer failed, daemon_path: %s",
164 static void request_buffer_consumer(int sock
,
169 struct ustcomm_header send_header
, recv_header
;
170 struct ustcomm_buffer_info buf_inf
;
173 result
= ustcomm_pack_buffer_info(&send_header
,
180 ERR("failed to pack buffer info message %s_%d",
185 buf_inf
.pid
= getpid();
186 send_header
.command
= CONSUME_BUFFER
;
188 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
191 PERROR("request for buffer consumer failed, is the daemon online?");
197 /* Ask the daemon to collect a trace called trace_name and being
198 * produced by this pid.
200 * The trace must be at least allocated. (It can also be started.)
201 * This is because _ltt_trace_find is used.
204 static void inform_consumer_daemon(const char *trace_name
)
207 struct ust_trace
*trace
;
210 sock
= connect_ustconsumer();
215 DBG("Connected to ustconsumer");
219 trace
= _ltt_trace_find(trace_name
);
221 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
225 for (i
=0; i
< trace
->nr_channels
; i
++) {
226 if (trace
->channels
[i
].request_collection
) {
227 /* iterate on all cpus */
228 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
229 ch_name
= trace
->channels
[i
].channel_name
;
230 request_buffer_consumer(sock
, trace_name
,
232 CMM_STORE_SHARED(buffers_to_export
,
233 CMM_LOAD_SHARED(buffers_to_export
)+1);
244 static struct ust_channel
*find_channel(const char *ch_name
,
245 struct ust_trace
*trace
)
249 for (i
=0; i
<trace
->nr_channels
; i
++) {
250 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
251 return &trace
->channels
[i
];
258 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
261 int *buf_struct_shmid
,
264 struct ust_trace
*trace
;
265 struct ust_channel
*channel
;
266 struct ust_buffer
*buf
;
268 DBG("get_buffer_shmid_pipe_fd");
271 trace
= _ltt_trace_find(trace_name
);
275 ERR("cannot find trace!");
279 channel
= find_channel(ch_name
, trace
);
281 ERR("cannot find channel %s!", ch_name
);
285 buf
= channel
->buf
[ch_cpu
];
287 *buf_shmid
= buf
->shmid
;
288 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
289 *buf_pipe_fd
= buf
->data_ready_fd_read
;
294 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
297 struct ust_trace
*trace
;
298 struct ust_channel
*channel
;
300 DBG("get_subbuf_size");
303 trace
= _ltt_trace_find(trace_name
);
307 ERR("cannot find trace!");
311 channel
= find_channel(ch_name
, trace
);
313 ERR("unable to find channel");
317 *num
= channel
->subbuf_cnt
;
318 *size
= channel
->subbuf_size
;
323 /* Return the power of two which is equal or higher to v */
325 static unsigned int pow2_higher_or_eq(unsigned int v
)
328 int retval
= 1<<(hb
-1);
336 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
341 struct ust_trace
*trace
;
342 struct ust_channel
*channel
;
344 DBG("set_subbuf_size");
346 power
= pow2_higher_or_eq(size
);
347 power
= max_t(unsigned int, 2u, power
);
349 WARN("using the next power of two for buffer size = %u\n", power
);
353 trace
= _ltt_trace_find_setup(trace_name
);
355 ERR("cannot find trace!");
360 channel
= find_channel(ch_name
, trace
);
362 ERR("unable to find channel");
367 channel
->subbuf_size
= power
;
368 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
376 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
379 struct ust_trace
*trace
;
380 struct ust_channel
*channel
;
383 DBG("set_subbuf_num");
386 ERR("subbuffer count should be greater than 2");
391 trace
= _ltt_trace_find_setup(trace_name
);
393 ERR("cannot find trace!");
398 channel
= find_channel(ch_name
, trace
);
400 ERR("unable to find channel");
405 channel
->subbuf_cnt
= num
;
406 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
413 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
414 int ch_cpu
, long *consumed_old
)
417 struct ust_trace
*trace
;
418 struct ust_channel
*channel
;
419 struct ust_buffer
*buf
;
426 trace
= _ltt_trace_find(trace_name
);
429 DBG("Cannot find trace. It was likely destroyed by the user.");
434 channel
= find_channel(ch_name
, trace
);
436 ERR("unable to find channel");
441 buf
= channel
->buf
[ch_cpu
];
443 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
445 WARN("missed buffer?");
455 static int notify_buffer_mapped(const char *trace_name
,
460 struct ust_trace
*trace
;
461 struct ust_channel
*channel
;
462 struct ust_buffer
*buf
;
464 DBG("get_buffer_fd");
467 trace
= _ltt_trace_find(trace_name
);
471 DBG("Cannot find trace. It was likely destroyed by the user.");
475 channel
= find_channel(ch_name
, trace
);
478 ERR("unable to find channel");
482 buf
= channel
->buf
[ch_cpu
];
484 /* Being here is the proof the daemon has mapped the buffer in its
485 * memory. We may now decrement buffers_to_export.
487 if (uatomic_read(&buf
->consumed
) == 0) {
488 DBG("decrementing buffers_to_export");
489 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
492 /* The buffer has been exported, ergo, we can add it to the
493 * list of open buffers
495 cds_list_add(&buf
->open_buffers_list
, &open_buffers_list
);
503 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
504 int ch_cpu
, long consumed_old
)
507 struct ust_trace
*trace
;
508 struct ust_channel
*channel
;
509 struct ust_buffer
*buf
;
514 trace
= _ltt_trace_find(trace_name
);
518 DBG("Cannot find trace. It was likely destroyed by the user.");
522 channel
= find_channel(ch_name
, trace
);
525 ERR("unable to find channel");
529 buf
= channel
->buf
[ch_cpu
];
531 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
533 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
536 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
546 static void release_listener_mutex(void *ptr
)
548 pthread_mutex_unlock(&listener_thread_data_mutex
);
551 static void listener_cleanup(void *ptr
)
553 pthread_mutex_lock(&listen_sock_mutex
);
555 ustcomm_del_named_sock(listen_sock
, 0);
558 pthread_mutex_unlock(&listen_sock_mutex
);
561 static int force_subbuf_switch(const char *trace_name
)
563 struct ust_trace
*trace
;
564 int i
, j
, retval
= 0;
567 trace
= _ltt_trace_find(trace_name
);
570 DBG("Cannot find trace. It was likely destroyed by the user.");
574 for (i
= 0; i
< trace
->nr_channels
; i
++) {
575 for (j
= 0; j
< trace
->channels
[i
].n_cpus
; j
++) {
576 ltt_force_switch(trace
->channels
[i
].buf
[j
],
587 /* Simple commands are those which need only respond with a return value. */
588 static int process_simple_client_cmd(int command
, char *recv_buf
)
595 struct ustcomm_single_field
*sock_msg
;
596 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
597 result
= ustcomm_unpack_single_field(sock_msg
);
601 return setenv("UST_DAEMON_SOCKET", sock_msg
->field
, 1);
612 static int process_trace_cmd(int command
, char *trace_name
)
615 char trace_type
[] = "ustrelay";
619 /* start is an operation that setups the trace, allocates it and starts it */
620 result
= ltt_trace_setup(trace_name
);
622 ERR("ltt_trace_setup failed");
626 result
= ltt_trace_set_type(trace_name
, trace_type
);
628 ERR("ltt_trace_set_type failed");
632 result
= ltt_trace_alloc(trace_name
);
634 ERR("ltt_trace_alloc failed");
638 inform_consumer_daemon(trace_name
);
640 result
= ltt_trace_start(trace_name
);
642 ERR("ltt_trace_start failed");
650 result
= ltt_trace_setup(trace_name
);
652 ERR("ltt_trace_setup failed");
656 result
= ltt_trace_set_type(trace_name
, trace_type
);
658 ERR("ltt_trace_set_type failed");
666 result
= ltt_trace_alloc(trace_name
);
668 ERR("ltt_trace_alloc failed");
671 inform_consumer_daemon(trace_name
);
678 result
= ltt_trace_setup(trace_name
);
680 ERR("ltt_trace_setup failed");
684 result
= ltt_trace_set_type(trace_name
, trace_type
);
686 ERR("ltt_trace_set_type failed");
694 result
= ltt_trace_alloc(trace_name
);
696 ERR("ltt_trace_alloc failed");
700 inform_consumer_daemon(trace_name
);
703 result
= ltt_trace_start(trace_name
);
705 ERR("ltt_trace_start failed");
713 result
= ltt_trace_stop(trace_name
);
715 ERR("ltt_trace_stop failed");
721 DBG("trace destroy");
723 result
= ltt_trace_destroy(trace_name
, 0);
725 ERR("ltt_trace_destroy failed");
729 case FORCE_SUBBUF_SWITCH
:
732 result
= force_subbuf_switch(trace_name
);
734 ERR("force_subbuf_switch failed");
744 static void process_channel_cmd(int sock
, int command
,
745 struct ustcomm_channel_info
*ch_inf
)
747 struct ustcomm_header _reply_header
;
748 struct ustcomm_header
*reply_header
= &_reply_header
;
749 struct ustcomm_channel_info
*reply_msg
=
750 (struct ustcomm_channel_info
*)send_buffer
;
751 int result
, offset
= 0, num
, size
;
753 memset(reply_header
, 0, sizeof(*reply_header
));
756 case GET_SUBBUF_NUM_SIZE
:
757 result
= get_subbuf_num_size(ch_inf
->trace
,
761 reply_header
->result
= result
;
765 reply_msg
->channel
= USTCOMM_POISON_PTR
;
766 reply_msg
->subbuf_num
= num
;
767 reply_msg
->subbuf_size
= size
;
770 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
774 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
779 case SET_SUBBUF_SIZE
:
780 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
782 ch_inf
->subbuf_size
);
787 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
788 ERR("ustcomm_send failed");
792 static void process_buffer_cmd(int sock
, int command
,
793 struct ustcomm_buffer_info
*buf_inf
)
795 struct ustcomm_header _reply_header
;
796 struct ustcomm_header
*reply_header
= &_reply_header
;
797 struct ustcomm_buffer_info
*reply_msg
=
798 (struct ustcomm_buffer_info
*)send_buffer
;
799 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
802 memset(reply_header
, 0, sizeof(*reply_header
));
805 case GET_BUF_SHMID_PIPE_FD
:
806 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
813 reply_header
->result
= result
;
817 reply_msg
->channel
= USTCOMM_POISON_PTR
;
818 reply_msg
->buf_shmid
= buf_shmid
;
819 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
821 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
822 reply_header
->fd_included
= 1;
824 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
826 ERR("ustcomm_send failed");
830 case NOTIFY_BUF_MAPPED
:
831 reply_header
->result
=
832 notify_buffer_mapped(buf_inf
->trace
,
837 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
838 buf_inf
->ch_cpu
, &consumed_old
);
840 reply_header
->result
= result
;
844 reply_msg
->channel
= USTCOMM_POISON_PTR
;
845 reply_msg
->consumed_old
= consumed_old
;
847 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
851 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
853 buf_inf
->consumed_old
);
854 reply_header
->result
= result
;
859 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
860 ERR("ustcomm_send failed");
865 static void process_marker_cmd(int sock
, int command
,
866 struct ustcomm_marker_info
*marker_inf
)
868 struct ustcomm_header _reply_header
;
869 struct ustcomm_header
*reply_header
= &_reply_header
;
872 memset(reply_header
, 0, sizeof(*reply_header
));
877 result
= ltt_marker_connect(marker_inf
->channel
,
881 WARN("could not enable marker; channel=%s,"
889 result
= ltt_marker_disconnect(marker_inf
->channel
,
893 WARN("could not disable marker; channel=%s,"
901 reply_header
->result
= result
;
903 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
904 ERR("ustcomm_send failed");
908 static void process_client_cmd(struct ustcomm_header
*recv_header
,
909 char *recv_buf
, int sock
)
912 struct ustcomm_header _reply_header
;
913 struct ustcomm_header
*reply_header
= &_reply_header
;
914 char *send_buf
= send_buffer
;
916 memset(reply_header
, 0, sizeof(*reply_header
));
917 memset(send_buf
, 0, sizeof(send_buffer
));
919 switch(recv_header
->command
) {
920 case GET_SUBBUF_NUM_SIZE
:
922 case SET_SUBBUF_SIZE
:
924 struct ustcomm_channel_info
*ch_inf
;
925 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
926 result
= ustcomm_unpack_channel_info(ch_inf
);
928 ERR("couldn't unpack channel info");
929 reply_header
->result
= -EINVAL
;
932 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
935 case GET_BUF_SHMID_PIPE_FD
:
936 case NOTIFY_BUF_MAPPED
:
940 struct ustcomm_buffer_info
*buf_inf
;
941 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
942 result
= ustcomm_unpack_buffer_info(buf_inf
);
944 ERR("couldn't unpack buffer info");
945 reply_header
->result
= -EINVAL
;
948 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
954 struct ustcomm_marker_info
*marker_inf
;
955 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
956 result
= ustcomm_unpack_marker_info(marker_inf
);
958 ERR("couldn't unpack marker info");
959 reply_header
->result
= -EINVAL
;
962 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
971 fp
= open_memstream(&ptr
, &size
);
973 ERR("opening memstream failed");
979 reply_header
->size
= size
+ 1; /* Include final \0 */
981 result
= ustcomm_send(sock
, reply_header
, ptr
);
986 PERROR("failed to send markers list");
991 case LIST_TRACE_EVENTS
:
997 fp
= open_memstream(&ptr
, &size
);
999 ERR("opening memstream failed");
1002 print_trace_events(fp
);
1005 reply_header
->size
= size
+ 1; /* Include final \0 */
1007 result
= ustcomm_send(sock
, reply_header
, ptr
);
1012 ERR("list_trace_events failed");
1018 case LOAD_PROBE_LIB
:
1022 /* FIXME: No functionality at all... */
1025 DBG("load_probe_lib loading %s", libfile
);
1031 struct ustcomm_pidunique
*pid_msg
;
1032 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
1034 pid_msg
->pidunique
= pidunique
;
1035 reply_header
->size
= sizeof(pid_msg
);
1042 struct ustcomm_single_field
*sock_msg
;
1043 char *sock_path_env
;
1045 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1047 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1049 if (!sock_path_env
) {
1050 result
= ustcomm_pack_single_field(reply_header
,
1052 SOCK_DIR
"/ustconsumer");
1055 result
= ustcomm_pack_single_field(reply_header
,
1059 reply_header
->result
= result
;
1070 case FORCE_SUBBUF_SWITCH
:
1072 struct ustcomm_single_field
*trace_inf
=
1073 (struct ustcomm_single_field
*)recv_buf
;
1075 result
= ustcomm_unpack_single_field(trace_inf
);
1077 ERR("couldn't unpack trace info");
1078 reply_header
->result
= -EINVAL
;
1082 reply_header
->result
=
1083 process_trace_cmd(recv_header
->command
,
1089 reply_header
->result
=
1090 process_simple_client_cmd(recv_header
->command
,
1099 ustcomm_send(sock
, reply_header
, send_buf
);
1102 #define MAX_EVENTS 10
1104 void *listener_main(void *p
)
1106 struct ustcomm_sock
*epoll_sock
;
1107 struct epoll_event events
[MAX_EVENTS
];
1108 struct sockaddr addr
;
1109 int accept_fd
, nfds
, result
, i
, addr_size
;
1113 pthread_cleanup_push(listener_cleanup
, NULL
);
1116 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1118 PERROR("listener_main: epoll_wait failed");
1122 for (i
= 0; i
< nfds
; i
++) {
1123 pthread_mutex_lock(&listener_thread_data_mutex
);
1124 pthread_cleanup_push(release_listener_mutex
, NULL
);
1125 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1126 if (epoll_sock
== listen_sock
) {
1127 addr_size
= sizeof(struct sockaddr
);
1128 accept_fd
= accept(epoll_sock
->fd
,
1130 (socklen_t
*)&addr_size
);
1131 if (accept_fd
== -1) {
1132 PERROR("listener_main: accept failed");
1135 ustcomm_init_sock(accept_fd
, epoll_fd
,
1138 memset(receive_header
, 0,
1139 sizeof(*receive_header
));
1140 memset(receive_buffer
, 0,
1141 sizeof(receive_buffer
));
1142 result
= ustcomm_recv(epoll_sock
->fd
,
1146 ustcomm_del_sock(epoll_sock
, 0);
1148 process_client_cmd(receive_header
,
1153 pthread_cleanup_pop(1); /* release listener mutex */
1157 pthread_cleanup_pop(1);
1160 /* These should only be accessed in the parent thread,
1163 static volatile sig_atomic_t have_listener
= 0;
1164 static pthread_t listener_thread
;
1166 void create_listener(void)
1169 sigset_t sig_all_blocked
;
1170 sigset_t orig_parent_mask
;
1172 if (have_listener
) {
1173 WARN("not creating listener because we already had one");
1177 /* A new thread created by pthread_create inherits the signal mask
1178 * from the parent. To avoid any signal being received by the
1179 * listener thread, we block all signals temporarily in the parent,
1180 * while we create the listener thread.
1183 sigfillset(&sig_all_blocked
);
1185 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1187 PERROR("pthread_sigmask: %s", strerror(result
));
1190 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1192 PERROR("pthread_create");
1195 /* Restore original signal mask in parent */
1196 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1198 PERROR("pthread_sigmask: %s", strerror(result
));
1204 #define AUTOPROBE_DISABLED 0
1205 #define AUTOPROBE_ENABLE_ALL 1
1206 #define AUTOPROBE_ENABLE_REGEX 2
1207 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1208 static regex_t autoprobe_regex
;
1210 static void auto_probe_connect(struct marker
*m
)
1214 char* concat_name
= NULL
;
1215 const char *probe_name
= "default";
1217 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1219 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1220 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1222 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1223 m
->channel
, m
->name
);
1226 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1233 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1234 if (result
&& result
!= -EEXIST
)
1235 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1237 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1241 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1245 struct ustcomm_sock
*sock
;
1247 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1249 ERR("string overflow allocating socket name, "
1250 "UST thread bailing");
1254 result
= ensure_dir_exists(SOCK_DIR
);
1256 ERR("Unable to create socket directory %s, UST thread bailing",
1261 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1263 ERR("Error initializing named socket (%s). Check that directory"
1264 "exists and that it is writable. UST thread bailing", name
);
1276 static void __attribute__((constructor
)) init()
1280 char* autoprobe_val
= NULL
;
1281 char* subbuffer_size_val
= NULL
;
1282 char* subbuffer_count_val
= NULL
;
1283 unsigned int subbuffer_size
;
1284 unsigned int subbuffer_count
;
1287 /* Assign the pidunique, to be able to differentiate the processes with same
1288 * pid, (before and after an exec).
1290 pidunique
= make_pidunique();
1291 processpid
= getpid();
1293 DBG("Tracectl constructor");
1296 epoll_fd
= epoll_create(MAX_EVENTS
);
1297 if (epoll_fd
== -1) {
1298 ERR("epoll_create failed, tracing shutting down");
1302 /* Create the socket */
1303 listen_sock
= init_app_socket(epoll_fd
);
1305 ERR("failed to create application socket,"
1306 " tracing shutting down");
1312 /* Get clock the clock source type */
1314 /* Default clock source */
1315 ust_clock_source
= CLOCK_TRACE
;
1316 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1317 ust_clock_source
= CLOCK_MONOTONIC
;
1318 DBG("UST traces will not be synchronized with LTTng traces");
1321 autoprobe_val
= getenv("UST_AUTOPROBE");
1322 if (autoprobe_val
) {
1323 struct marker_iter iter
;
1325 DBG("Autoprobe enabled.");
1327 /* Ensure markers are initialized */
1330 /* Ensure marker control is initialized, for the probe */
1331 init_marker_control();
1333 /* first, set the callback that will connect the
1334 * probe on new markers
1336 if (autoprobe_val
[0] == '/') {
1337 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1341 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1342 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1343 /* don't crash the application just for this */
1345 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1348 /* just enable all instrumentation */
1349 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1352 marker_set_new_marker_cb(auto_probe_connect
);
1354 /* Now, connect the probes that were already registered. */
1355 marker_iter_reset(&iter
);
1356 marker_iter_start(&iter
);
1358 DBG("now iterating on markers already registered");
1359 while (iter
.marker
) {
1360 DBG("now iterating on marker %s", (*iter
.marker
)->name
);
1361 auto_probe_connect(*iter
.marker
);
1362 marker_iter_next(&iter
);
1366 if (getenv("UST_OVERWRITE")) {
1367 int val
= atoi(getenv("UST_OVERWRITE"));
1368 if (val
== 0 || val
== 1) {
1369 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1371 WARN("invalid value for UST_OVERWRITE");
1375 if (getenv("UST_AUTOCOLLECT")) {
1376 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1377 if (val
== 0 || val
== 1) {
1378 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1380 WARN("invalid value for UST_AUTOCOLLECT");
1384 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1385 if (subbuffer_size_val
) {
1386 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1387 power
= pow2_higher_or_eq(subbuffer_size
);
1388 if (power
!= subbuffer_size
)
1389 WARN("using the next power of two for buffer size = %u\n", power
);
1390 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1393 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1394 if (subbuffer_count_val
) {
1395 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1396 if (subbuffer_count
< 2)
1397 subbuffer_count
= 2;
1398 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1401 if (getenv("UST_TRACE")) {
1402 char trace_name
[] = "auto";
1403 char trace_type
[] = "ustrelay";
1405 DBG("starting early tracing");
1407 /* Ensure marker control is initialized */
1408 init_marker_control();
1410 /* Ensure markers are initialized */
1413 /* Ensure buffers are initialized, for the transport to be available.
1414 * We are about to set a trace type and it will fail without this.
1416 init_ustrelay_transport();
1418 /* FIXME: When starting early tracing (here), depending on the
1419 * order of constructors, it is very well possible some marker
1420 * sections are not yet registered. Because of this, some
1421 * channels may not be registered. Yet, we are about to ask the
1422 * daemon to collect the channels. Channels which are not yet
1423 * registered will not be collected.
1425 * Currently, in LTTng, there is no way to add a channel after
1426 * trace start. The reason for this is that it induces complex
1427 * concurrency issues on the trace structures, which can only
1428 * be resolved using RCU. This has not been done yet. As a
1429 * workaround, we are forcing the registration of the "ust"
1430 * channel here. This is the only channel (apart from metadata)
1431 * that can be reliably used in early tracing.
1433 * Non-early tracing does not have this problem and can use
1434 * arbitrary channel names.
1436 ltt_channels_register("ust");
1438 result
= ltt_trace_setup(trace_name
);
1440 ERR("ltt_trace_setup failed");
1444 result
= ltt_trace_set_type(trace_name
, trace_type
);
1446 ERR("ltt_trace_set_type failed");
1450 result
= ltt_trace_alloc(trace_name
);
1452 ERR("ltt_trace_alloc failed");
1456 result
= ltt_trace_start(trace_name
);
1458 ERR("ltt_trace_start failed");
1462 /* Do this after the trace is started in order to avoid creating confusion
1463 * if the trace fails to start. */
1464 inform_consumer_daemon(trace_name
);
1469 /* should decrementally destroy stuff if error */
1473 /* This is only called if we terminate normally, not with an unhandled signal,
1474 * so we cannot rely on it. However, for now, LTTV requires that the header of
1475 * the last sub-buffer contain a valid end time for the trace. This is done
1476 * automatically only when the trace is properly stopped.
1478 * If the traced program crashed, it is always possible to manually add the
1479 * right value in the header, or to open the trace in text mode.
1481 * FIXME: Fix LTTV so it doesn't need this.
1484 static void destroy_traces(void)
1488 /* if trace running, finish it */
1490 DBG("destructor stopping traces");
1492 result
= ltt_trace_stop("auto");
1494 ERR("ltt_trace_stop error");
1497 result
= ltt_trace_destroy("auto", 0);
1499 ERR("ltt_trace_destroy error");
1503 static int trace_recording(void)
1506 struct ust_trace
*trace
;
1510 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1511 if (trace
->active
) {
1517 ltt_unlock_traces();
1522 int restarting_usleep(useconds_t usecs
)
1528 tv
.tv_nsec
= usecs
* 1000;
1531 result
= nanosleep(&tv
, &tv
);
1532 } while (result
== -1 && errno
== EINTR
);
1537 static void stop_listener(void)
1544 result
= pthread_cancel(listener_thread
);
1546 ERR("pthread_cancel: %s", strerror(result
));
1548 result
= pthread_join(listener_thread
, NULL
);
1550 ERR("pthread_join: %s", strerror(result
));
1554 /* This destructor keeps the process alive for a few seconds in order
1555 * to leave time for ustconsumer to connect to its buffers. This is necessary
1556 * for programs whose execution is very short. It is also useful in all
1557 * programs when tracing is started close to the end of the program
1560 * FIXME: For now, this only works for the first trace created in a
1564 static void __attribute__((destructor
)) keepalive()
1566 if (processpid
!= getpid()) {
1570 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1572 DBG("Keeping process alive for consumer daemon...");
1573 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1574 const int interv
= 200000;
1575 restarting_usleep(interv
);
1578 if (total
>= 3000000) {
1579 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1583 DBG("Finally dying...");
1588 /* Ask the listener to stop and clean up. */
1592 void ust_potential_exec(void)
1594 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1601 /* Notify ust that there was a fork. This needs to be called inside
1602 * the new process, anytime a process whose memory is not shared with
1603 * the parent is created. If this function is not called, the events
1604 * of the new process will not be collected.
1606 * Signals should be disabled before the fork and reenabled only after
1607 * this call in order to guarantee tracing is not started before ust_fork()
1608 * sanitizes the new process.
1611 static void ust_fork(void)
1613 struct ust_buffer
*buf
, *buf_tmp
;
1614 struct ustcomm_sock
*sock
, *sock_tmp
;
1615 struct ust_trace
*trace
, *trace_tmp
;
1618 /* FIXME: technically, the locks could have been taken before the fork */
1619 DBG("ust: forking");
1621 /* Get the pid of the new process */
1622 processpid
= getpid();
1625 * FIXME: This could be prettier, we loop over the list twice and
1626 * following good locking practice should lock around the loop
1628 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1629 ltt_trace_stop(trace
->trace_name
);
1632 /* Delete all active connections, but leave them in the epoll set */
1633 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1634 ustcomm_del_sock(sock
, 1);
1637 /* Delete all blocked consumers */
1638 cds_list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1639 open_buffers_list
) {
1640 cds_list_del(&buf
->open_buffers_list
);
1644 * FIXME: This could be prettier, we loop over the list twice and
1645 * following good locking practice should lock around the loop
1647 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1648 ltt_trace_destroy(trace
->trace_name
, 1);
1651 /* Clean up the listener socket and epoll, keeping the socket file */
1653 ustcomm_del_named_sock(listen_sock
, 1);
1658 /* Re-start the launch sequence */
1659 CMM_STORE_SHARED(buffers_to_export
, 0);
1663 epoll_fd
= epoll_create(MAX_EVENTS
);
1664 if (epoll_fd
== -1) {
1665 ERR("epoll_create failed, tracing shutting down");
1669 /* Create the socket */
1670 listen_sock
= init_app_socket(epoll_fd
);
1672 ERR("failed to create application socket,"
1673 " tracing shutting down");
1677 ltt_trace_setup("auto");
1678 result
= ltt_trace_set_type("auto", "ustrelay");
1680 ERR("ltt_trace_set_type failed");
1684 ltt_trace_alloc("auto");
1685 ltt_trace_start("auto");
1686 inform_consumer_daemon("auto");
1689 void ust_before_fork(ust_fork_info_t
*fork_info
)
1691 /* Disable signals. This is to avoid that the child
1692 * intervenes before it is properly setup for tracing. It is
1693 * safer to disable all signals, because then we know we are not
1694 * breaking anything by restoring the original mask.
1700 - only do this if tracing is active
1703 /* Disable signals */
1704 sigfillset(&all_sigs
);
1705 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1707 PERROR("sigprocmask");
1712 * Take the fork lock to make sure we are not in the middle of
1713 * something in the listener thread.
1715 pthread_mutex_lock(&listener_thread_data_mutex
);
1717 * Hold listen_sock_mutex to protect from listen_sock teardown.
1719 pthread_mutex_lock(&listen_sock_mutex
);
1720 rcu_bp_before_fork();
1723 /* Don't call this function directly in a traced program */
1724 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1728 pthread_mutex_unlock(&listen_sock_mutex
);
1729 pthread_mutex_unlock(&listener_thread_data_mutex
);
1731 /* Restore signals */
1732 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1734 PERROR("sigprocmask");
1739 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1741 rcu_bp_after_fork_parent();
1742 /* Release mutexes and reenable signals */
1743 ust_after_fork_common(fork_info
);
1746 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1748 /* Release urcu mutexes */
1749 rcu_bp_after_fork_child();
1751 /* Sanitize the child */
1754 /* Then release mutexes and reenable signals */
1755 ust_after_fork_common(fork_info
);