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/tracectl.h>
43 #include <ust/clock.h>
45 #include "usterr_signal_safe.h"
48 #include "marker-control.h"
50 /* This should only be accessed by the constructor, before the creation
51 * of the listener, and then only by the listener.
55 /* The process pid is used to detect a non-traceable fork
56 * and allow the non-traceable fork to be ignored
57 * by destructor sequences in libust
59 static pid_t processpid
= 0;
61 static struct ustcomm_header _receive_header
;
62 static struct ustcomm_header
*receive_header
= &_receive_header
;
63 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
64 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
69 * Listener thread data vs fork() protection mechanism. Ensures that no listener
70 * thread mutexes and data structures are being concurrently modified or held by
71 * other threads when fork() is executed.
73 static pthread_mutex_t listener_thread_data_mutex
= PTHREAD_MUTEX_INITIALIZER
;
75 /* Mutex protecting listen_sock. Nests inside listener_thread_data_mutex. */
76 static pthread_mutex_t listen_sock_mutex
= PTHREAD_MUTEX_INITIALIZER
;
77 static struct ustcomm_sock
*listen_sock
;
79 extern struct chan_info_struct chan_infos
[];
81 static struct cds_list_head ust_socks
= CDS_LIST_HEAD_INIT(ust_socks
);
83 /* volatile because shared between the listener and the main thread */
84 int buffers_to_export
= 0;
88 static long long make_pidunique(void)
93 gettimeofday(&tv
, NULL
);
102 static void print_ust_marker(FILE *fp
)
104 struct ust_marker_iter iter
;
107 ust_marker_iter_reset(&iter
);
108 ust_marker_iter_start(&iter
);
110 while (iter
.ust_marker
) {
111 fprintf(fp
, "ust_marker: %s/%s %d \"%s\" %p\n",
112 (*iter
.ust_marker
)->channel
,
113 (*iter
.ust_marker
)->name
,
114 (int)(*iter
.ust_marker
)->state
,
115 (*iter
.ust_marker
)->format
,
116 (*iter
.ust_marker
)->location
);
117 ust_marker_iter_next(&iter
);
122 static void print_trace_events(FILE *fp
)
124 struct trace_event_iter iter
;
127 trace_event_iter_reset(&iter
);
128 trace_event_iter_start(&iter
);
130 while (iter
.trace_event
) {
131 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
132 trace_event_iter_next(&iter
);
134 unlock_trace_events();
137 static int connect_ustconsumer(void)
140 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
141 char *explicit_daemon_path
, *daemon_path
;
143 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
144 if (explicit_daemon_path
) {
145 daemon_path
= explicit_daemon_path
;
147 daemon_path
= default_daemon_path
;
150 DBG("Connecting to daemon_path %s", daemon_path
);
152 result
= ustcomm_connect_path(daemon_path
, &fd
);
154 WARN("connect_ustconsumer failed, daemon_path: %s",
163 static void request_buffer_consumer(int sock
,
168 struct ustcomm_header send_header
, recv_header
;
169 struct ustcomm_buffer_info buf_inf
;
172 result
= ustcomm_pack_buffer_info(&send_header
,
179 ERR("failed to pack buffer info message %s_%d",
184 buf_inf
.pid
= getpid();
185 send_header
.command
= CONSUME_BUFFER
;
187 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
190 PERROR("request for buffer consumer failed, is the daemon online?");
196 /* Ask the daemon to collect a trace called trace_name and being
197 * produced by this pid.
199 * The trace must be at least allocated. (It can also be started.)
200 * This is because _ltt_trace_find is used.
203 static void inform_consumer_daemon(const char *trace_name
)
206 struct ust_trace
*trace
;
209 sock
= connect_ustconsumer();
214 DBG("Connected to ustconsumer");
218 trace
= _ltt_trace_find(trace_name
);
220 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
224 for (i
=0; i
< trace
->nr_channels
; i
++) {
225 if (trace
->channels
[i
].request_collection
) {
226 /* iterate on all cpus */
227 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
228 ch_name
= trace
->channels
[i
].channel_name
;
229 request_buffer_consumer(sock
, trace_name
,
231 CMM_STORE_SHARED(buffers_to_export
,
232 CMM_LOAD_SHARED(buffers_to_export
)+1);
243 static struct ust_channel
*find_channel(const char *ch_name
,
244 struct ust_trace
*trace
)
248 for (i
=0; i
<trace
->nr_channels
; i
++) {
249 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
250 return &trace
->channels
[i
];
257 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
260 int *buf_struct_shmid
,
263 struct ust_trace
*trace
;
264 struct ust_channel
*channel
;
265 struct ust_buffer
*buf
;
267 DBG("get_buffer_shmid_pipe_fd");
270 trace
= _ltt_trace_find(trace_name
);
274 ERR("cannot find trace!");
278 channel
= find_channel(ch_name
, trace
);
280 ERR("cannot find channel %s!", ch_name
);
284 buf
= channel
->buf
[ch_cpu
];
286 *buf_shmid
= buf
->shmid
;
287 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
288 *buf_pipe_fd
= buf
->data_ready_fd_read
;
293 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
296 struct ust_trace
*trace
;
297 struct ust_channel
*channel
;
299 DBG("get_subbuf_size");
302 trace
= _ltt_trace_find(trace_name
);
306 ERR("cannot find trace!");
310 channel
= find_channel(ch_name
, trace
);
312 ERR("unable to find channel");
316 *num
= channel
->subbuf_cnt
;
317 *size
= channel
->subbuf_size
;
322 /* Return the power of two which is equal or higher to v */
324 static unsigned int pow2_higher_or_eq(unsigned int v
)
327 int retval
= 1<<(hb
-1);
335 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
340 struct ust_trace
*trace
;
341 struct ust_channel
*channel
;
343 DBG("set_subbuf_size");
345 power
= pow2_higher_or_eq(size
);
346 power
= max_t(unsigned int, 2u, power
);
348 WARN("using the next power of two for buffer size = %u\n", power
);
352 trace
= _ltt_trace_find_setup(trace_name
);
354 ERR("cannot find trace!");
359 channel
= find_channel(ch_name
, trace
);
361 ERR("unable to find channel");
366 channel
->subbuf_size
= power
;
367 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
375 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
378 struct ust_trace
*trace
;
379 struct ust_channel
*channel
;
382 DBG("set_subbuf_num");
385 ERR("subbuffer count should be greater than 2");
390 trace
= _ltt_trace_find_setup(trace_name
);
392 ERR("cannot find trace!");
397 channel
= find_channel(ch_name
, trace
);
399 ERR("unable to find channel");
404 channel
->subbuf_cnt
= num
;
405 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
412 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
413 int ch_cpu
, long *consumed_old
)
416 struct ust_trace
*trace
;
417 struct ust_channel
*channel
;
418 struct ust_buffer
*buf
;
425 trace
= _ltt_trace_find(trace_name
);
428 DBG("Cannot find trace. It was likely destroyed by the user.");
433 channel
= find_channel(ch_name
, trace
);
435 ERR("unable to find channel");
440 buf
= channel
->buf
[ch_cpu
];
442 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
444 WARN("missed buffer?");
454 static int notify_buffer_mapped(const char *trace_name
,
459 struct ust_trace
*trace
;
460 struct ust_channel
*channel
;
461 struct ust_buffer
*buf
;
463 DBG("get_buffer_fd");
466 trace
= _ltt_trace_find(trace_name
);
470 DBG("Cannot find trace. It was likely destroyed by the user.");
474 channel
= find_channel(ch_name
, trace
);
477 ERR("unable to find channel");
481 buf
= channel
->buf
[ch_cpu
];
483 /* Being here is the proof the daemon has mapped the buffer in its
484 * memory. We may now decrement buffers_to_export.
486 if (uatomic_read(&buf
->consumed
) == 0) {
487 DBG("decrementing buffers_to_export");
488 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
497 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
498 int ch_cpu
, long consumed_old
)
501 struct ust_trace
*trace
;
502 struct ust_channel
*channel
;
503 struct ust_buffer
*buf
;
508 trace
= _ltt_trace_find(trace_name
);
512 DBG("Cannot find trace. It was likely destroyed by the user.");
516 channel
= find_channel(ch_name
, trace
);
519 ERR("unable to find channel");
523 buf
= channel
->buf
[ch_cpu
];
525 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
527 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
530 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
540 static void release_listener_mutex(void *ptr
)
542 pthread_mutex_unlock(&listener_thread_data_mutex
);
545 static void listener_cleanup(void *ptr
)
547 pthread_mutex_lock(&listen_sock_mutex
);
549 ustcomm_del_named_sock(listen_sock
, 0);
552 pthread_mutex_unlock(&listen_sock_mutex
);
555 static int force_subbuf_switch(const char *trace_name
)
557 struct ust_trace
*trace
;
558 int i
, j
, retval
= 0;
561 trace
= _ltt_trace_find(trace_name
);
564 DBG("Cannot find trace. It was likely destroyed by the user.");
568 for (i
= 0; i
< trace
->nr_channels
; i
++) {
569 for (j
= 0; j
< trace
->channels
[i
].n_cpus
; j
++) {
570 ltt_force_switch(trace
->channels
[i
].buf
[j
],
581 static int process_trace_cmd(int command
, char *trace_name
)
584 char trace_type
[] = "ustrelay";
588 /* start is an operation that setups the trace, allocates it and starts it */
589 result
= ltt_trace_setup(trace_name
);
591 ERR("ltt_trace_setup failed");
595 result
= ltt_trace_set_type(trace_name
, trace_type
);
597 ERR("ltt_trace_set_type failed");
601 result
= ltt_trace_alloc(trace_name
);
603 ERR("ltt_trace_alloc failed");
607 inform_consumer_daemon(trace_name
);
609 result
= ltt_trace_start(trace_name
);
611 ERR("ltt_trace_start failed");
619 result
= ltt_trace_setup(trace_name
);
621 ERR("ltt_trace_setup failed");
625 result
= ltt_trace_set_type(trace_name
, trace_type
);
627 ERR("ltt_trace_set_type failed");
635 result
= ltt_trace_alloc(trace_name
);
637 ERR("ltt_trace_alloc failed");
640 inform_consumer_daemon(trace_name
);
647 result
= ltt_trace_setup(trace_name
);
649 ERR("ltt_trace_setup failed");
653 result
= ltt_trace_set_type(trace_name
, trace_type
);
655 ERR("ltt_trace_set_type failed");
663 result
= ltt_trace_alloc(trace_name
);
665 ERR("ltt_trace_alloc failed");
669 inform_consumer_daemon(trace_name
);
672 result
= ltt_trace_start(trace_name
);
674 ERR("ltt_trace_start failed");
682 result
= ltt_trace_stop(trace_name
);
684 ERR("ltt_trace_stop failed");
690 DBG("trace destroy");
692 result
= ltt_trace_destroy(trace_name
, 0);
694 ERR("ltt_trace_destroy failed");
698 case FORCE_SUBBUF_SWITCH
:
701 result
= force_subbuf_switch(trace_name
);
703 ERR("force_subbuf_switch failed");
713 static void process_channel_cmd(int sock
, int command
,
714 struct ustcomm_channel_info
*ch_inf
)
716 struct ustcomm_header _reply_header
;
717 struct ustcomm_header
*reply_header
= &_reply_header
;
718 struct ustcomm_channel_info
*reply_msg
=
719 (struct ustcomm_channel_info
*)send_buffer
;
720 int result
, offset
= 0, num
, size
;
722 memset(reply_header
, 0, sizeof(*reply_header
));
725 case GET_SUBBUF_NUM_SIZE
:
726 result
= get_subbuf_num_size(ch_inf
->trace
,
730 reply_header
->result
= result
;
734 reply_msg
->channel
= USTCOMM_POISON_PTR
;
735 reply_msg
->subbuf_num
= num
;
736 reply_msg
->subbuf_size
= size
;
739 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
743 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
748 case SET_SUBBUF_SIZE
:
749 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
751 ch_inf
->subbuf_size
);
756 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
757 ERR("ustcomm_send failed");
761 static void process_buffer_cmd(int sock
, int command
,
762 struct ustcomm_buffer_info
*buf_inf
)
764 struct ustcomm_header _reply_header
;
765 struct ustcomm_header
*reply_header
= &_reply_header
;
766 struct ustcomm_buffer_info
*reply_msg
=
767 (struct ustcomm_buffer_info
*)send_buffer
;
768 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
771 memset(reply_header
, 0, sizeof(*reply_header
));
774 case GET_BUF_SHMID_PIPE_FD
:
775 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
782 reply_header
->result
= result
;
786 reply_msg
->channel
= USTCOMM_POISON_PTR
;
787 reply_msg
->buf_shmid
= buf_shmid
;
788 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
790 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
791 reply_header
->fd_included
= 1;
793 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
795 ERR("ustcomm_send failed");
799 case NOTIFY_BUF_MAPPED
:
800 reply_header
->result
=
801 notify_buffer_mapped(buf_inf
->trace
,
806 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
807 buf_inf
->ch_cpu
, &consumed_old
);
809 reply_header
->result
= result
;
813 reply_msg
->channel
= USTCOMM_POISON_PTR
;
814 reply_msg
->consumed_old
= consumed_old
;
816 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
820 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
822 buf_inf
->consumed_old
);
823 reply_header
->result
= result
;
828 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
829 ERR("ustcomm_send failed");
834 static void process_ust_marker_cmd(int sock
, int command
,
835 struct ustcomm_ust_marker_info
*ust_marker_inf
)
837 struct ustcomm_header _reply_header
;
838 struct ustcomm_header
*reply_header
= &_reply_header
;
841 memset(reply_header
, 0, sizeof(*reply_header
));
846 result
= ltt_ust_marker_connect(ust_marker_inf
->channel
,
847 ust_marker_inf
->ust_marker
,
850 WARN("could not enable ust_marker; channel=%s,"
852 ust_marker_inf
->channel
,
853 ust_marker_inf
->ust_marker
);
858 result
= ltt_ust_marker_disconnect(ust_marker_inf
->channel
,
859 ust_marker_inf
->ust_marker
,
862 WARN("could not disable ust_marker; channel=%s,"
864 ust_marker_inf
->channel
,
865 ust_marker_inf
->ust_marker
);
870 reply_header
->result
= result
;
872 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
873 ERR("ustcomm_send failed");
877 static void process_client_cmd(struct ustcomm_header
*recv_header
,
878 char *recv_buf
, int sock
)
881 struct ustcomm_header _reply_header
;
882 struct ustcomm_header
*reply_header
= &_reply_header
;
883 char *send_buf
= send_buffer
;
885 memset(reply_header
, 0, sizeof(*reply_header
));
886 memset(send_buf
, 0, sizeof(send_buffer
));
888 switch(recv_header
->command
) {
889 case GET_SUBBUF_NUM_SIZE
:
891 case SET_SUBBUF_SIZE
:
893 struct ustcomm_channel_info
*ch_inf
;
894 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
895 result
= ustcomm_unpack_channel_info(ch_inf
);
897 ERR("couldn't unpack channel info");
898 reply_header
->result
= -EINVAL
;
901 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
904 case GET_BUF_SHMID_PIPE_FD
:
905 case NOTIFY_BUF_MAPPED
:
909 struct ustcomm_buffer_info
*buf_inf
;
910 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
911 result
= ustcomm_unpack_buffer_info(buf_inf
);
913 ERR("couldn't unpack buffer info");
914 reply_header
->result
= -EINVAL
;
917 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
923 struct ustcomm_ust_marker_info
*ust_marker_inf
;
924 ust_marker_inf
= (struct ustcomm_ust_marker_info
*)recv_buf
;
925 result
= ustcomm_unpack_ust_marker_info(ust_marker_inf
);
927 ERR("couldn't unpack ust_marker info");
928 reply_header
->result
= -EINVAL
;
931 process_ust_marker_cmd(sock
, recv_header
->command
, ust_marker_inf
);
940 fp
= open_memstream(&ptr
, &size
);
942 ERR("opening memstream failed");
945 print_ust_marker(fp
);
948 reply_header
->size
= size
+ 1; /* Include final \0 */
950 result
= ustcomm_send(sock
, reply_header
, ptr
);
955 PERROR("failed to send ust_marker list");
960 case LIST_TRACE_EVENTS
:
966 fp
= open_memstream(&ptr
, &size
);
968 ERR("opening memstream failed");
971 print_trace_events(fp
);
974 reply_header
->size
= size
+ 1; /* Include final \0 */
976 result
= ustcomm_send(sock
, reply_header
, ptr
);
981 ERR("list_trace_events failed");
991 /* FIXME: No functionality at all... */
994 DBG("load_probe_lib loading %s", libfile
);
1000 struct ustcomm_pidunique
*pid_msg
;
1001 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
1003 pid_msg
->pidunique
= pidunique
;
1004 reply_header
->size
= sizeof(pid_msg
);
1011 struct ustcomm_single_field
*sock_msg
;
1012 char *sock_path_env
;
1014 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1016 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1018 if (!sock_path_env
) {
1019 result
= ustcomm_pack_single_field(reply_header
,
1021 SOCK_DIR
"/ustconsumer");
1024 result
= ustcomm_pack_single_field(reply_header
,
1028 reply_header
->result
= result
;
1034 struct ustcomm_single_field
*sock_msg
;
1035 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
1036 result
= ustcomm_unpack_single_field(sock_msg
);
1038 reply_header
->result
= -EINVAL
;
1042 reply_header
->result
= setenv("UST_DAEMON_SOCKET",
1043 sock_msg
->field
, 1);
1054 case FORCE_SUBBUF_SWITCH
:
1056 struct ustcomm_single_field
*trace_inf
=
1057 (struct ustcomm_single_field
*)recv_buf
;
1059 result
= ustcomm_unpack_single_field(trace_inf
);
1061 ERR("couldn't unpack trace info");
1062 reply_header
->result
= -EINVAL
;
1066 reply_header
->result
=
1067 process_trace_cmd(recv_header
->command
,
1073 reply_header
->result
= -EINVAL
;
1081 ustcomm_send(sock
, reply_header
, send_buf
);
1084 #define MAX_EVENTS 10
1086 void *listener_main(void *p
)
1088 struct ustcomm_sock
*epoll_sock
;
1089 struct epoll_event events
[MAX_EVENTS
];
1090 struct sockaddr addr
;
1091 int accept_fd
, nfds
, result
, i
, addr_size
;
1095 pthread_cleanup_push(listener_cleanup
, NULL
);
1098 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1100 PERROR("listener_main: epoll_wait failed");
1104 for (i
= 0; i
< nfds
; i
++) {
1105 pthread_mutex_lock(&listener_thread_data_mutex
);
1106 pthread_cleanup_push(release_listener_mutex
, NULL
);
1107 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1108 if (epoll_sock
== listen_sock
) {
1109 addr_size
= sizeof(struct sockaddr
);
1110 accept_fd
= accept(epoll_sock
->fd
,
1112 (socklen_t
*)&addr_size
);
1113 if (accept_fd
== -1) {
1114 PERROR("listener_main: accept failed");
1117 ustcomm_init_sock(accept_fd
, epoll_fd
,
1120 memset(receive_header
, 0,
1121 sizeof(*receive_header
));
1122 memset(receive_buffer
, 0,
1123 sizeof(receive_buffer
));
1124 result
= ustcomm_recv(epoll_sock
->fd
,
1128 ustcomm_del_sock(epoll_sock
, 0);
1130 process_client_cmd(receive_header
,
1135 pthread_cleanup_pop(1); /* release listener mutex */
1139 pthread_cleanup_pop(1);
1142 /* These should only be accessed in the parent thread,
1145 static volatile sig_atomic_t have_listener
= 0;
1146 static pthread_t listener_thread
;
1148 void create_listener(void)
1151 sigset_t sig_all_blocked
;
1152 sigset_t orig_parent_mask
;
1154 if (have_listener
) {
1155 WARN("not creating listener because we already had one");
1159 /* A new thread created by pthread_create inherits the signal mask
1160 * from the parent. To avoid any signal being received by the
1161 * listener thread, we block all signals temporarily in the parent,
1162 * while we create the listener thread.
1165 sigfillset(&sig_all_blocked
);
1167 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1169 PERROR("pthread_sigmask: %s", strerror(result
));
1172 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1174 PERROR("pthread_create");
1177 /* Restore original signal mask in parent */
1178 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1180 PERROR("pthread_sigmask: %s", strerror(result
));
1186 #define AUTOPROBE_DISABLED 0
1187 #define AUTOPROBE_ENABLE_ALL 1
1188 #define AUTOPROBE_ENABLE_REGEX 2
1189 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1190 static regex_t autoprobe_regex
;
1192 static void auto_probe_connect(struct ust_marker
*m
)
1196 char* concat_name
= NULL
;
1197 const char *probe_name
= "default";
1199 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1201 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1202 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1204 ERR("auto_probe_connect: asprintf failed (ust_marker %s/%s)",
1205 m
->channel
, m
->name
);
1208 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1215 result
= ltt_ust_marker_connect(m
->channel
, m
->name
, probe_name
);
1216 if (result
&& result
!= -EEXIST
)
1217 ERR("ltt_ust_marker_connect (ust_marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1219 DBG("auto connected ust_marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1223 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1225 char *dir_name
, *sock_name
;
1227 struct ustcomm_sock
*sock
= NULL
;
1229 dir_name
= ustcomm_user_sock_dir();
1233 result
= asprintf(&sock_name
, "%s/%d", dir_name
, (int)getpid());
1235 ERR("string overflow allocating socket name, "
1236 "UST thread bailing");
1240 result
= ensure_dir_exists(dir_name
, S_IRWXU
);
1242 ERR("Unable to create socket directory %s, UST thread bailing",
1244 goto free_sock_name
;
1247 sock
= ustcomm_init_named_socket(sock_name
, epoll_fd
);
1249 ERR("Error initializing named socket (%s). Check that directory"
1250 "exists and that it is writable. UST thread bailing", sock_name
);
1251 goto free_sock_name
;
1262 static void __attribute__((constructor
)) init()
1266 char* autoprobe_val
= NULL
;
1267 char* subbuffer_size_val
= NULL
;
1268 char* subbuffer_count_val
= NULL
;
1269 unsigned int subbuffer_size
;
1270 unsigned int subbuffer_count
;
1273 /* Assign the pidunique, to be able to differentiate the processes with same
1274 * pid, (before and after an exec).
1276 pidunique
= make_pidunique();
1277 processpid
= getpid();
1279 DBG("Tracectl constructor");
1282 epoll_fd
= epoll_create(MAX_EVENTS
);
1283 if (epoll_fd
== -1) {
1284 ERR("epoll_create failed, tracing shutting down");
1288 /* Create the socket */
1289 listen_sock
= init_app_socket(epoll_fd
);
1291 ERR("failed to create application socket,"
1292 " tracing shutting down");
1298 /* Get clock the clock source type */
1300 /* Default clock source */
1301 ust_clock_source
= CLOCK_TRACE
;
1302 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1303 ust_clock_source
= CLOCK_MONOTONIC
;
1304 DBG("UST traces will not be synchronized with LTTng traces");
1307 autoprobe_val
= getenv("UST_AUTOPROBE");
1308 if (autoprobe_val
) {
1309 struct ust_marker_iter iter
;
1311 DBG("Autoprobe enabled.");
1313 /* Ensure ust_marker are initialized */
1314 //init_ust_marker();
1316 /* Ensure ust_marker control is initialized, for the probe */
1317 init_ust_marker_control();
1319 /* first, set the callback that will connect the
1320 * probe on new ust_marker
1322 if (autoprobe_val
[0] == '/') {
1323 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1327 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1328 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1329 /* don't crash the application just for this */
1331 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1334 /* just enable all instrumentation */
1335 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1338 ust_marker_set_new_ust_marker_cb(auto_probe_connect
);
1340 /* Now, connect the probes that were already registered. */
1341 ust_marker_iter_reset(&iter
);
1342 ust_marker_iter_start(&iter
);
1344 DBG("now iterating on ust_marker already registered");
1345 while (iter
.ust_marker
) {
1346 DBG("now iterating on ust_marker %s", (*iter
.ust_marker
)->name
);
1347 auto_probe_connect(*iter
.ust_marker
);
1348 ust_marker_iter_next(&iter
);
1352 if (getenv("UST_OVERWRITE")) {
1353 int val
= atoi(getenv("UST_OVERWRITE"));
1354 if (val
== 0 || val
== 1) {
1355 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1357 WARN("invalid value for UST_OVERWRITE");
1361 if (getenv("UST_AUTOCOLLECT")) {
1362 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1363 if (val
== 0 || val
== 1) {
1364 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1366 WARN("invalid value for UST_AUTOCOLLECT");
1370 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1371 if (subbuffer_size_val
) {
1372 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1373 power
= pow2_higher_or_eq(subbuffer_size
);
1374 if (power
!= subbuffer_size
)
1375 WARN("using the next power of two for buffer size = %u\n", power
);
1376 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1379 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1380 if (subbuffer_count_val
) {
1381 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1382 if (subbuffer_count
< 2)
1383 subbuffer_count
= 2;
1384 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1387 if (getenv("UST_TRACE")) {
1388 char trace_name
[] = "auto";
1389 char trace_type
[] = "ustrelay";
1391 DBG("starting early tracing");
1393 /* Ensure ust_marker control is initialized */
1394 init_ust_marker_control();
1396 /* Ensure ust_marker are initialized */
1399 /* Ensure buffers are initialized, for the transport to be available.
1400 * We are about to set a trace type and it will fail without this.
1402 init_ustrelay_transport();
1404 /* FIXME: When starting early tracing (here), depending on the
1405 * order of constructors, it is very well possible some ust_marker
1406 * sections are not yet registered. Because of this, some
1407 * channels may not be registered. Yet, we are about to ask the
1408 * daemon to collect the channels. Channels which are not yet
1409 * registered will not be collected.
1411 * Currently, in LTTng, there is no way to add a channel after
1412 * trace start. The reason for this is that it induces complex
1413 * concurrency issues on the trace structures, which can only
1414 * be resolved using RCU. This has not been done yet. As a
1415 * workaround, we are forcing the registration of the "ust"
1416 * channel here. This is the only channel (apart from metadata)
1417 * that can be reliably used in early tracing.
1419 * Non-early tracing does not have this problem and can use
1420 * arbitrary channel names.
1422 ltt_channels_register("ust");
1424 result
= ltt_trace_setup(trace_name
);
1426 ERR("ltt_trace_setup failed");
1430 result
= ltt_trace_set_type(trace_name
, trace_type
);
1432 ERR("ltt_trace_set_type failed");
1436 result
= ltt_trace_alloc(trace_name
);
1438 ERR("ltt_trace_alloc failed");
1442 result
= ltt_trace_start(trace_name
);
1444 ERR("ltt_trace_start failed");
1448 /* Do this after the trace is started in order to avoid creating confusion
1449 * if the trace fails to start. */
1450 inform_consumer_daemon(trace_name
);
1455 /* should decrementally destroy stuff if error */
1459 /* This is only called if we terminate normally, not with an unhandled signal,
1460 * so we cannot rely on it. However, for now, LTTV requires that the header of
1461 * the last sub-buffer contain a valid end time for the trace. This is done
1462 * automatically only when the trace is properly stopped.
1464 * If the traced program crashed, it is always possible to manually add the
1465 * right value in the header, or to open the trace in text mode.
1467 * FIXME: Fix LTTV so it doesn't need this.
1470 static void destroy_traces(void)
1474 /* if trace running, finish it */
1476 DBG("destructor stopping traces");
1478 result
= ltt_trace_stop("auto");
1480 ERR("ltt_trace_stop error");
1483 result
= ltt_trace_destroy("auto", 0);
1485 ERR("ltt_trace_destroy error");
1489 static int trace_recording(void)
1492 struct ust_trace
*trace
;
1496 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1497 if (trace
->active
) {
1503 ltt_unlock_traces();
1508 int restarting_usleep(useconds_t usecs
)
1514 tv
.tv_nsec
= usecs
* 1000;
1517 result
= nanosleep(&tv
, &tv
);
1518 } while (result
== -1 && errno
== EINTR
);
1523 static void stop_listener(void)
1530 result
= pthread_cancel(listener_thread
);
1532 ERR("pthread_cancel: %s", strerror(result
));
1534 result
= pthread_join(listener_thread
, NULL
);
1536 ERR("pthread_join: %s", strerror(result
));
1540 /* This destructor keeps the process alive for a few seconds in order
1541 * to leave time for ustconsumer to connect to its buffers. This is necessary
1542 * for programs whose execution is very short. It is also useful in all
1543 * programs when tracing is started close to the end of the program
1546 * FIXME: For now, this only works for the first trace created in a
1550 static void __attribute__((destructor
)) keepalive()
1552 if (processpid
!= getpid()) {
1556 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1558 DBG("Keeping process alive for consumer daemon...");
1559 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1560 const int interv
= 200000;
1561 restarting_usleep(interv
);
1564 if (total
>= 3000000) {
1565 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1569 DBG("Finally dying...");
1574 /* Ask the listener to stop and clean up. */
1578 void ust_potential_exec(void)
1580 ust_marker(potential_exec
, UST_MARKER_NOARGS
);
1587 /* Notify ust that there was a fork. This needs to be called inside
1588 * the new process, anytime a process whose memory is not shared with
1589 * the parent is created. If this function is not called, the events
1590 * of the new process will not be collected.
1592 * Signals should be disabled before the fork and reenabled only after
1593 * this call in order to guarantee tracing is not started before ust_fork()
1594 * sanitizes the new process.
1597 static void ust_fork(void)
1599 struct ustcomm_sock
*sock
, *sock_tmp
;
1600 struct ust_trace
*trace
, *trace_tmp
;
1603 /* FIXME: technically, the locks could have been taken before the fork */
1604 DBG("ust: forking");
1606 /* Get the pid of the new process */
1607 processpid
= getpid();
1610 * FIXME: This could be prettier, we loop over the list twice and
1611 * following good locking practice should lock around the loop
1613 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1614 ltt_trace_stop(trace
->trace_name
);
1617 /* Delete all active connections, but leave them in the epoll set */
1618 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1619 ustcomm_del_sock(sock
, 1);
1623 * FIXME: This could be prettier, we loop over the list twice and
1624 * following good locking practice should lock around the loop
1626 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1627 ltt_trace_destroy(trace
->trace_name
, 1);
1630 /* Clean up the listener socket and epoll, keeping the socket file */
1632 ustcomm_del_named_sock(listen_sock
, 1);
1637 /* Re-start the launch sequence */
1638 CMM_STORE_SHARED(buffers_to_export
, 0);
1642 epoll_fd
= epoll_create(MAX_EVENTS
);
1643 if (epoll_fd
== -1) {
1644 ERR("epoll_create failed, tracing shutting down");
1648 /* Create the socket */
1649 listen_sock
= init_app_socket(epoll_fd
);
1651 ERR("failed to create application socket,"
1652 " tracing shutting down");
1656 ltt_trace_setup("auto");
1657 result
= ltt_trace_set_type("auto", "ustrelay");
1659 ERR("ltt_trace_set_type failed");
1663 ltt_trace_alloc("auto");
1664 ltt_trace_start("auto");
1665 inform_consumer_daemon("auto");
1668 void ust_before_fork(ust_fork_info_t
*fork_info
)
1670 /* Disable signals. This is to avoid that the child
1671 * intervenes before it is properly setup for tracing. It is
1672 * safer to disable all signals, because then we know we are not
1673 * breaking anything by restoring the original mask.
1679 - only do this if tracing is active
1682 /* Disable signals */
1683 sigfillset(&all_sigs
);
1684 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1686 PERROR("sigprocmask");
1691 * Take the fork lock to make sure we are not in the middle of
1692 * something in the listener thread.
1694 pthread_mutex_lock(&listener_thread_data_mutex
);
1696 * Hold listen_sock_mutex to protect from listen_sock teardown.
1698 pthread_mutex_lock(&listen_sock_mutex
);
1699 rcu_bp_before_fork();
1702 /* Don't call this function directly in a traced program */
1703 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1707 pthread_mutex_unlock(&listen_sock_mutex
);
1708 pthread_mutex_unlock(&listener_thread_data_mutex
);
1710 /* Restore signals */
1711 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1713 PERROR("sigprocmask");
1718 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1720 rcu_bp_after_fork_parent();
1721 /* Release mutexes and reenable signals */
1722 ust_after_fork_common(fork_info
);
1725 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1727 /* Release urcu mutexes */
1728 rcu_bp_after_fork_child();
1730 /* Sanitize the child */
1733 /* Then release mutexes and reenable signals */
1734 ust_after_fork_common(fork_info
);