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.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
;
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
,
117 * location is null for now, will be added
118 * to a different table.
120 ust_marker_iter_next(&iter
);
122 ust_marker_iter_stop(&iter
);
125 static void print_trace_events(FILE *fp
)
127 struct trace_event_iter iter
;
129 trace_event_iter_reset(&iter
);
130 trace_event_iter_start(&iter
);
132 while (iter
.trace_event
) {
133 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
134 trace_event_iter_next(&iter
);
136 trace_event_iter_stop(&iter
);
139 static int connect_ustconsumer(void)
142 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
143 char *explicit_daemon_path
, *daemon_path
;
145 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
146 if (explicit_daemon_path
) {
147 daemon_path
= explicit_daemon_path
;
149 daemon_path
= default_daemon_path
;
152 DBG("Connecting to daemon_path %s", daemon_path
);
154 result
= ustcomm_connect_path(daemon_path
, &fd
);
156 WARN("connect_ustconsumer failed, daemon_path: %s",
165 static void request_buffer_consumer(int sock
,
170 struct ustcomm_header send_header
, recv_header
;
171 struct ustcomm_buffer_info buf_inf
;
174 result
= ustcomm_pack_buffer_info(&send_header
,
181 ERR("failed to pack buffer info message %s_%d",
186 buf_inf
.pid
= getpid();
187 send_header
.command
= CONSUME_BUFFER
;
189 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
192 PERROR("request for buffer consumer failed, is the daemon online?");
198 /* Ask the daemon to collect a trace called trace_name and being
199 * produced by this pid.
201 * The trace must be at least allocated. (It can also be started.)
202 * This is because _ltt_trace_find is used.
205 static void inform_consumer_daemon(const char *trace_name
)
208 struct ust_trace
*trace
;
211 sock
= connect_ustconsumer();
216 DBG("Connected to ustconsumer");
220 trace
= _ltt_trace_find(trace_name
);
222 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
226 for (i
=0; i
< trace
->nr_channels
; i
++) {
227 if (trace
->channels
[i
].request_collection
) {
228 /* iterate on all cpus */
229 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
230 ch_name
= trace
->channels
[i
].channel_name
;
231 request_buffer_consumer(sock
, trace_name
,
233 CMM_STORE_SHARED(buffers_to_export
,
234 CMM_LOAD_SHARED(buffers_to_export
)+1);
245 static struct ust_channel
*find_channel(const char *ch_name
,
246 struct ust_trace
*trace
)
250 for (i
=0; i
<trace
->nr_channels
; i
++) {
251 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
252 return &trace
->channels
[i
];
259 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
262 int *buf_struct_shmid
,
265 struct ust_trace
*trace
;
266 struct ust_channel
*channel
;
267 struct ust_buffer
*buf
;
269 DBG("get_buffer_shmid_pipe_fd");
272 trace
= _ltt_trace_find(trace_name
);
276 ERR("cannot find trace!");
280 channel
= find_channel(ch_name
, trace
);
282 ERR("cannot find channel %s!", ch_name
);
286 buf
= channel
->buf
[ch_cpu
];
288 *buf_shmid
= buf
->shmid
;
289 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
290 *buf_pipe_fd
= buf
->data_ready_fd_read
;
295 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
298 struct ust_trace
*trace
;
299 struct ust_channel
*channel
;
301 DBG("get_subbuf_size");
304 trace
= _ltt_trace_find(trace_name
);
308 ERR("cannot find trace!");
312 channel
= find_channel(ch_name
, trace
);
314 ERR("unable to find channel");
318 *num
= channel
->subbuf_cnt
;
319 *size
= channel
->subbuf_size
;
324 /* Return the power of two which is equal or higher to v */
326 static unsigned int pow2_higher_or_eq(unsigned int v
)
329 int retval
= 1<<(hb
-1);
337 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
342 struct ust_trace
*trace
;
343 struct ust_channel
*channel
;
345 DBG("set_subbuf_size");
347 power
= pow2_higher_or_eq(size
);
348 power
= max_t(unsigned int, 2u, power
);
350 WARN("using the next power of two for buffer size = %u\n", power
);
354 trace
= _ltt_trace_find_setup(trace_name
);
356 ERR("cannot find trace!");
361 channel
= find_channel(ch_name
, trace
);
363 ERR("unable to find channel");
368 channel
->subbuf_size
= power
;
369 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
377 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
380 struct ust_trace
*trace
;
381 struct ust_channel
*channel
;
384 DBG("set_subbuf_num");
387 ERR("subbuffer count should be greater than 2");
392 trace
= _ltt_trace_find_setup(trace_name
);
394 ERR("cannot find trace!");
399 channel
= find_channel(ch_name
, trace
);
401 ERR("unable to find channel");
406 channel
->subbuf_cnt
= num
;
407 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
414 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
415 int ch_cpu
, long *consumed_old
)
418 struct ust_trace
*trace
;
419 struct ust_channel
*channel
;
420 struct ust_buffer
*buf
;
427 trace
= _ltt_trace_find(trace_name
);
430 DBG("Cannot find trace. It was likely destroyed by the user.");
435 channel
= find_channel(ch_name
, trace
);
437 ERR("unable to find channel");
442 buf
= channel
->buf
[ch_cpu
];
444 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
446 WARN("missed buffer?");
456 static int notify_buffer_mapped(const char *trace_name
,
461 struct ust_trace
*trace
;
462 struct ust_channel
*channel
;
463 struct ust_buffer
*buf
;
465 DBG("get_buffer_fd");
468 trace
= _ltt_trace_find(trace_name
);
472 DBG("Cannot find trace. It was likely destroyed by the user.");
476 channel
= find_channel(ch_name
, trace
);
479 ERR("unable to find channel");
483 buf
= channel
->buf
[ch_cpu
];
485 /* Being here is the proof the daemon has mapped the buffer in its
486 * memory. We may now decrement buffers_to_export.
488 if (uatomic_read(&buf
->consumed
) == 0) {
489 DBG("decrementing buffers_to_export");
490 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
499 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
500 int ch_cpu
, long consumed_old
)
503 struct ust_trace
*trace
;
504 struct ust_channel
*channel
;
505 struct ust_buffer
*buf
;
510 trace
= _ltt_trace_find(trace_name
);
514 DBG("Cannot find trace. It was likely destroyed by the user.");
518 channel
= find_channel(ch_name
, trace
);
521 ERR("unable to find channel");
525 buf
= channel
->buf
[ch_cpu
];
527 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
529 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
532 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
542 static void release_listener_mutex(void *ptr
)
544 pthread_mutex_unlock(&listener_thread_data_mutex
);
547 static void listener_cleanup(void *ptr
)
549 pthread_mutex_lock(&listen_sock_mutex
);
551 ustcomm_del_named_sock(listen_sock
, 0);
554 pthread_mutex_unlock(&listen_sock_mutex
);
557 static int force_subbuf_switch(const char *trace_name
)
559 struct ust_trace
*trace
;
560 int i
, j
, retval
= 0;
563 trace
= _ltt_trace_find(trace_name
);
566 DBG("Cannot find trace. It was likely destroyed by the user.");
570 for (i
= 0; i
< trace
->nr_channels
; i
++) {
571 for (j
= 0; j
< trace
->channels
[i
].n_cpus
; j
++) {
572 ltt_force_switch(trace
->channels
[i
].buf
[j
],
583 static int process_trace_cmd(int command
, char *trace_name
)
586 char trace_type
[] = "ustrelay";
590 /* start is an operation that setups the trace, allocates it and starts it */
591 result
= ltt_trace_setup(trace_name
);
593 ERR("ltt_trace_setup failed");
597 result
= ltt_trace_set_type(trace_name
, trace_type
);
599 ERR("ltt_trace_set_type failed");
603 result
= ltt_trace_alloc(trace_name
);
605 ERR("ltt_trace_alloc failed");
609 inform_consumer_daemon(trace_name
);
611 result
= ltt_trace_start(trace_name
);
613 ERR("ltt_trace_start failed");
621 result
= ltt_trace_setup(trace_name
);
623 ERR("ltt_trace_setup failed");
627 result
= ltt_trace_set_type(trace_name
, trace_type
);
629 ERR("ltt_trace_set_type failed");
637 result
= ltt_trace_alloc(trace_name
);
639 ERR("ltt_trace_alloc failed");
642 inform_consumer_daemon(trace_name
);
649 result
= ltt_trace_setup(trace_name
);
651 ERR("ltt_trace_setup failed");
655 result
= ltt_trace_set_type(trace_name
, trace_type
);
657 ERR("ltt_trace_set_type failed");
665 result
= ltt_trace_alloc(trace_name
);
667 ERR("ltt_trace_alloc failed");
671 inform_consumer_daemon(trace_name
);
674 result
= ltt_trace_start(trace_name
);
676 ERR("ltt_trace_start failed");
684 result
= ltt_trace_stop(trace_name
);
686 ERR("ltt_trace_stop failed");
692 DBG("trace destroy");
694 result
= ltt_trace_destroy(trace_name
, 0);
696 ERR("ltt_trace_destroy failed");
700 case FORCE_SUBBUF_SWITCH
:
703 result
= force_subbuf_switch(trace_name
);
705 ERR("force_subbuf_switch failed");
715 static void process_channel_cmd(int sock
, int command
,
716 struct ustcomm_channel_info
*ch_inf
)
718 struct ustcomm_header _reply_header
;
719 struct ustcomm_header
*reply_header
= &_reply_header
;
720 struct ustcomm_channel_info
*reply_msg
=
721 (struct ustcomm_channel_info
*)send_buffer
;
722 int result
, offset
= 0, num
, size
;
724 memset(reply_header
, 0, sizeof(*reply_header
));
727 case GET_SUBBUF_NUM_SIZE
:
728 result
= get_subbuf_num_size(ch_inf
->trace
,
732 reply_header
->result
= result
;
736 reply_msg
->channel
= USTCOMM_POISON_PTR
;
737 reply_msg
->subbuf_num
= num
;
738 reply_msg
->subbuf_size
= size
;
741 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
745 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
750 case SET_SUBBUF_SIZE
:
751 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
753 ch_inf
->subbuf_size
);
758 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
759 ERR("ustcomm_send failed");
763 static void process_buffer_cmd(int sock
, int command
,
764 struct ustcomm_buffer_info
*buf_inf
)
766 struct ustcomm_header _reply_header
;
767 struct ustcomm_header
*reply_header
= &_reply_header
;
768 struct ustcomm_buffer_info
*reply_msg
=
769 (struct ustcomm_buffer_info
*)send_buffer
;
770 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
773 memset(reply_header
, 0, sizeof(*reply_header
));
776 case GET_BUF_SHMID_PIPE_FD
:
777 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
784 reply_header
->result
= result
;
788 reply_msg
->channel
= USTCOMM_POISON_PTR
;
789 reply_msg
->buf_shmid
= buf_shmid
;
790 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
792 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
793 reply_header
->fd_included
= 1;
795 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
797 ERR("ustcomm_send failed");
801 case NOTIFY_BUF_MAPPED
:
802 reply_header
->result
=
803 notify_buffer_mapped(buf_inf
->trace
,
808 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
809 buf_inf
->ch_cpu
, &consumed_old
);
811 reply_header
->result
= result
;
815 reply_msg
->channel
= USTCOMM_POISON_PTR
;
816 reply_msg
->consumed_old
= consumed_old
;
818 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
822 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
824 buf_inf
->consumed_old
);
825 reply_header
->result
= result
;
830 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
831 ERR("ustcomm_send failed");
836 static void process_ust_marker_cmd(int sock
, int command
,
837 struct ustcomm_ust_marker_info
*ust_marker_inf
)
839 struct ustcomm_header _reply_header
;
840 struct ustcomm_header
*reply_header
= &_reply_header
;
843 memset(reply_header
, 0, sizeof(*reply_header
));
848 result
= ltt_ust_marker_connect(ust_marker_inf
->channel
,
849 ust_marker_inf
->ust_marker
,
852 WARN("could not enable ust_marker; channel=%s,"
854 ust_marker_inf
->channel
,
855 ust_marker_inf
->ust_marker
);
860 result
= ltt_ust_marker_disconnect(ust_marker_inf
->channel
,
861 ust_marker_inf
->ust_marker
,
864 WARN("could not disable ust_marker; channel=%s,"
866 ust_marker_inf
->channel
,
867 ust_marker_inf
->ust_marker
);
872 reply_header
->result
= result
;
874 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
875 ERR("ustcomm_send failed");
879 static void process_client_cmd(struct ustcomm_header
*recv_header
,
880 char *recv_buf
, int sock
)
883 struct ustcomm_header _reply_header
;
884 struct ustcomm_header
*reply_header
= &_reply_header
;
885 char *send_buf
= send_buffer
;
887 memset(reply_header
, 0, sizeof(*reply_header
));
888 memset(send_buf
, 0, sizeof(send_buffer
));
890 switch(recv_header
->command
) {
891 case GET_SUBBUF_NUM_SIZE
:
893 case SET_SUBBUF_SIZE
:
895 struct ustcomm_channel_info
*ch_inf
;
896 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
897 result
= ustcomm_unpack_channel_info(ch_inf
);
899 ERR("couldn't unpack channel info");
900 reply_header
->result
= -EINVAL
;
903 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
906 case GET_BUF_SHMID_PIPE_FD
:
907 case NOTIFY_BUF_MAPPED
:
911 struct ustcomm_buffer_info
*buf_inf
;
912 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
913 result
= ustcomm_unpack_buffer_info(buf_inf
);
915 ERR("couldn't unpack buffer info");
916 reply_header
->result
= -EINVAL
;
919 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
925 struct ustcomm_ust_marker_info
*ust_marker_inf
;
926 ust_marker_inf
= (struct ustcomm_ust_marker_info
*)recv_buf
;
927 result
= ustcomm_unpack_ust_marker_info(ust_marker_inf
);
929 ERR("couldn't unpack ust_marker info");
930 reply_header
->result
= -EINVAL
;
933 process_ust_marker_cmd(sock
, recv_header
->command
, ust_marker_inf
);
942 fp
= open_memstream(&ptr
, &size
);
944 ERR("opening memstream failed");
947 print_ust_marker(fp
);
950 reply_header
->size
= size
+ 1; /* Include final \0 */
952 result
= ustcomm_send(sock
, reply_header
, ptr
);
957 PERROR("failed to send ust_marker list");
962 case LIST_TRACE_EVENTS
:
968 fp
= open_memstream(&ptr
, &size
);
970 ERR("opening memstream failed");
973 print_trace_events(fp
);
976 reply_header
->size
= size
+ 1; /* Include final \0 */
978 result
= ustcomm_send(sock
, reply_header
, ptr
);
983 ERR("list_trace_events failed");
993 /* FIXME: No functionality at all... */
996 DBG("load_probe_lib loading %s", libfile
);
1002 struct ustcomm_pidunique
*pid_msg
;
1003 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
1005 pid_msg
->pidunique
= pidunique
;
1006 reply_header
->size
= sizeof(pid_msg
);
1013 struct ustcomm_single_field
*sock_msg
;
1014 char *sock_path_env
;
1016 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1018 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1020 if (!sock_path_env
) {
1021 result
= ustcomm_pack_single_field(reply_header
,
1023 SOCK_DIR
"/ustconsumer");
1026 result
= ustcomm_pack_single_field(reply_header
,
1030 reply_header
->result
= result
;
1036 struct ustcomm_single_field
*sock_msg
;
1037 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
1038 result
= ustcomm_unpack_single_field(sock_msg
);
1040 reply_header
->result
= -EINVAL
;
1044 reply_header
->result
= setenv("UST_DAEMON_SOCKET",
1045 sock_msg
->field
, 1);
1056 case FORCE_SUBBUF_SWITCH
:
1058 struct ustcomm_single_field
*trace_inf
=
1059 (struct ustcomm_single_field
*)recv_buf
;
1061 result
= ustcomm_unpack_single_field(trace_inf
);
1063 ERR("couldn't unpack trace info");
1064 reply_header
->result
= -EINVAL
;
1068 reply_header
->result
=
1069 process_trace_cmd(recv_header
->command
,
1075 reply_header
->result
= -EINVAL
;
1083 ustcomm_send(sock
, reply_header
, send_buf
);
1086 #define MAX_EVENTS 10
1088 void *listener_main(void *p
)
1090 struct ustcomm_sock
*epoll_sock
;
1091 struct epoll_event events
[MAX_EVENTS
];
1092 struct sockaddr addr
;
1093 int accept_fd
, nfds
, result
, i
, addr_size
;
1097 pthread_cleanup_push(listener_cleanup
, NULL
);
1100 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1102 PERROR("listener_main: epoll_wait failed");
1106 for (i
= 0; i
< nfds
; i
++) {
1107 pthread_mutex_lock(&listener_thread_data_mutex
);
1108 pthread_cleanup_push(release_listener_mutex
, NULL
);
1109 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1110 if (epoll_sock
== listen_sock
) {
1111 addr_size
= sizeof(struct sockaddr
);
1112 accept_fd
= accept(epoll_sock
->fd
,
1114 (socklen_t
*)&addr_size
);
1115 if (accept_fd
== -1) {
1116 PERROR("listener_main: accept failed");
1119 ustcomm_init_sock(accept_fd
, epoll_fd
,
1122 memset(receive_header
, 0,
1123 sizeof(*receive_header
));
1124 memset(receive_buffer
, 0,
1125 sizeof(receive_buffer
));
1126 result
= ustcomm_recv(epoll_sock
->fd
,
1130 ustcomm_del_sock(epoll_sock
, 0);
1132 process_client_cmd(receive_header
,
1137 pthread_cleanup_pop(1); /* release listener mutex */
1141 pthread_cleanup_pop(1);
1144 /* These should only be accessed in the parent thread,
1147 static volatile sig_atomic_t have_listener
= 0;
1148 static pthread_t listener_thread
;
1150 void create_listener(void)
1153 sigset_t sig_all_blocked
;
1154 sigset_t orig_parent_mask
;
1156 if (have_listener
) {
1157 WARN("not creating listener because we already had one");
1161 /* A new thread created by pthread_create inherits the signal mask
1162 * from the parent. To avoid any signal being received by the
1163 * listener thread, we block all signals temporarily in the parent,
1164 * while we create the listener thread.
1167 sigfillset(&sig_all_blocked
);
1169 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1171 PERROR("pthread_sigmask: %s", strerror(result
));
1174 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1176 PERROR("pthread_create");
1179 /* Restore original signal mask in parent */
1180 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1182 PERROR("pthread_sigmask: %s", strerror(result
));
1188 #define AUTOPROBE_DISABLED 0
1189 #define AUTOPROBE_ENABLE_ALL 1
1190 #define AUTOPROBE_ENABLE_REGEX 2
1191 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1192 static regex_t autoprobe_regex
;
1194 static void auto_probe_connect(struct ust_marker
*m
)
1198 char* concat_name
= NULL
;
1199 const char *probe_name
= "default";
1201 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1203 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1204 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1206 ERR("auto_probe_connect: asprintf failed (ust_marker %s/%s)",
1207 m
->channel
, m
->name
);
1210 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1217 result
= ltt_ust_marker_connect(m
->channel
, m
->name
, probe_name
);
1218 if (result
&& result
!= -EEXIST
)
1219 ERR("ltt_ust_marker_connect (ust_marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1221 DBG("auto connected ust_marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1225 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1227 char *dir_name
, *sock_name
;
1229 struct ustcomm_sock
*sock
= NULL
;
1232 dir_name
= ustcomm_user_sock_dir();
1236 mtime
= ustcomm_pid_st_mtime(getpid());
1241 result
= asprintf(&sock_name
, "%s/%d.%ld", dir_name
,
1242 (int) getpid(), (long) mtime
);
1244 ERR("string overflow allocating socket name, "
1245 "UST thread bailing");
1249 result
= ensure_dir_exists(dir_name
, S_IRWXU
);
1251 ERR("Unable to create socket directory %s, UST thread bailing",
1253 goto free_sock_name
;
1256 sock
= ustcomm_init_named_socket(sock_name
, epoll_fd
);
1258 ERR("Error initializing named socket (%s). Check that directory"
1259 "exists and that it is writable. UST thread bailing", sock_name
);
1260 goto free_sock_name
;
1271 static void __attribute__((constructor
)) init()
1275 char* autoprobe_val
= NULL
;
1276 char* subbuffer_size_val
= NULL
;
1277 char* subbuffer_count_val
= NULL
;
1278 unsigned int subbuffer_size
;
1279 unsigned int subbuffer_count
;
1282 /* Assign the pidunique, to be able to differentiate the processes with same
1283 * pid, (before and after an exec).
1285 pidunique
= make_pidunique();
1286 processpid
= getpid();
1288 DBG("Tracectl constructor");
1291 epoll_fd
= epoll_create(MAX_EVENTS
);
1292 if (epoll_fd
== -1) {
1293 ERR("epoll_create failed, tracing shutting down");
1297 /* Create the socket */
1298 listen_sock
= init_app_socket(epoll_fd
);
1300 ERR("failed to create application socket,"
1301 " tracing shutting down");
1307 /* Get clock the clock source type */
1309 /* Default clock source */
1310 ust_clock_source
= CLOCK_TRACE
;
1311 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1312 ust_clock_source
= CLOCK_MONOTONIC
;
1313 DBG("UST traces will not be synchronized with LTTng traces");
1316 if (getenv("UST_TRACE") || getenv("UST_AUTOPROBE")) {
1317 /* Ensure ust_marker control is initialized */
1318 init_ust_marker_control();
1321 autoprobe_val
= getenv("UST_AUTOPROBE");
1322 if (autoprobe_val
) {
1323 struct ust_marker_iter iter
;
1325 DBG("Autoprobe enabled.");
1327 /* first, set the callback that will connect the
1328 * probe on new ust_marker
1330 if (autoprobe_val
[0] == '/') {
1331 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1335 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1336 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1337 /* don't crash the application just for this */
1339 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1342 /* just enable all instrumentation */
1343 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1346 ust_marker_set_new_ust_marker_cb(auto_probe_connect
);
1348 /* Now, connect the probes that were already registered. */
1349 ust_marker_iter_reset(&iter
);
1350 ust_marker_iter_start(&iter
);
1352 DBG("now iterating on ust_marker already registered");
1353 while (iter
.ust_marker
) {
1354 DBG("now iterating on ust_marker %s", (*iter
.ust_marker
)->name
);
1355 auto_probe_connect(*iter
.ust_marker
);
1356 ust_marker_iter_next(&iter
);
1358 ust_marker_iter_stop(&iter
);
1361 if (getenv("UST_OVERWRITE")) {
1362 int val
= atoi(getenv("UST_OVERWRITE"));
1363 if (val
== 0 || val
== 1) {
1364 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1366 WARN("invalid value for UST_OVERWRITE");
1370 if (getenv("UST_AUTOCOLLECT")) {
1371 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1372 if (val
== 0 || val
== 1) {
1373 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1375 WARN("invalid value for UST_AUTOCOLLECT");
1379 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1380 if (subbuffer_size_val
) {
1381 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1382 power
= pow2_higher_or_eq(subbuffer_size
);
1383 if (power
!= subbuffer_size
)
1384 WARN("using the next power of two for buffer size = %u\n", power
);
1385 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1388 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1389 if (subbuffer_count_val
) {
1390 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1391 if (subbuffer_count
< 2)
1392 subbuffer_count
= 2;
1393 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1396 if (getenv("UST_TRACE")) {
1397 char trace_name
[] = "auto";
1398 char trace_type
[] = "ustrelay";
1400 DBG("starting early tracing");
1402 /* Ensure buffers are initialized, for the transport to be available.
1403 * We are about to set a trace type and it will fail without this.
1405 init_ustrelay_transport();
1407 /* FIXME: When starting early tracing (here), depending on the
1408 * order of constructors, it is very well possible some ust_marker
1409 * sections are not yet registered. Because of this, some
1410 * channels may not be registered. Yet, we are about to ask the
1411 * daemon to collect the channels. Channels which are not yet
1412 * registered will not be collected.
1414 * Currently, in LTTng, there is no way to add a channel after
1415 * trace start. The reason for this is that it induces complex
1416 * concurrency issues on the trace structures, which can only
1417 * be resolved using RCU. This has not been done yet. As a
1418 * workaround, we are forcing the registration of the "ust"
1419 * channel here. This is the only channel (apart from metadata)
1420 * that can be reliably used in early tracing.
1422 * Non-early tracing does not have this problem and can use
1423 * arbitrary channel names.
1425 ltt_channels_register("ust");
1427 result
= ltt_trace_setup(trace_name
);
1429 ERR("ltt_trace_setup failed");
1433 result
= ltt_trace_set_type(trace_name
, trace_type
);
1435 ERR("ltt_trace_set_type failed");
1439 result
= ltt_trace_alloc(trace_name
);
1441 ERR("ltt_trace_alloc failed");
1445 result
= ltt_trace_start(trace_name
);
1447 ERR("ltt_trace_start failed");
1451 /* Do this after the trace is started in order to avoid creating confusion
1452 * if the trace fails to start. */
1453 inform_consumer_daemon(trace_name
);
1458 /* should decrementally destroy stuff if error */
1462 /* This is only called if we terminate normally, not with an unhandled signal,
1463 * so we cannot rely on it. However, for now, LTTV requires that the header of
1464 * the last sub-buffer contain a valid end time for the trace. This is done
1465 * automatically only when the trace is properly stopped.
1467 * If the traced program crashed, it is always possible to manually add the
1468 * right value in the header, or to open the trace in text mode.
1470 * FIXME: Fix LTTV so it doesn't need this.
1473 static void destroy_traces(void)
1477 /* if trace running, finish it */
1479 DBG("destructor stopping traces");
1481 result
= ltt_trace_stop("auto");
1483 ERR("ltt_trace_stop error");
1486 result
= ltt_trace_destroy("auto", 0);
1488 ERR("ltt_trace_destroy error");
1492 static int trace_recording(void)
1495 struct ust_trace
*trace
;
1499 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1500 if (trace
->active
) {
1506 ltt_unlock_traces();
1511 int restarting_usleep(useconds_t usecs
)
1517 tv
.tv_nsec
= usecs
* 1000;
1520 result
= nanosleep(&tv
, &tv
);
1521 } while (result
== -1 && errno
== EINTR
);
1526 static void stop_listener(void)
1533 result
= pthread_cancel(listener_thread
);
1535 ERR("pthread_cancel: %s", strerror(result
));
1537 result
= pthread_join(listener_thread
, NULL
);
1539 ERR("pthread_join: %s", strerror(result
));
1543 /* This destructor keeps the process alive for a few seconds in order
1544 * to leave time for ustconsumer to connect to its buffers. This is necessary
1545 * for programs whose execution is very short. It is also useful in all
1546 * programs when tracing is started close to the end of the program
1549 * FIXME: For now, this only works for the first trace created in a
1553 static void __attribute__((destructor
)) keepalive()
1555 if (processpid
!= getpid()) {
1559 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1561 DBG("Keeping process alive for consumer daemon...");
1562 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1563 const int interv
= 200000;
1564 restarting_usleep(interv
);
1567 if (total
>= 3000000) {
1568 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1572 DBG("Finally dying...");
1577 /* Ask the listener to stop and clean up. */
1581 void ust_potential_exec(void)
1583 ust_marker(potential_exec
, UST_MARKER_NOARGS
);
1590 /* Notify ust that there was a fork. This needs to be called inside
1591 * the new process, anytime a process whose memory is not shared with
1592 * the parent is created. If this function is not called, the events
1593 * of the new process will not be collected.
1595 * Signals should be disabled before the fork and reenabled only after
1596 * this call in order to guarantee tracing is not started before ust_fork()
1597 * sanitizes the new process.
1600 static void ust_fork(void)
1602 struct ustcomm_sock
*sock
, *sock_tmp
;
1603 struct ust_trace
*trace
, *trace_tmp
;
1606 /* FIXME: technically, the locks could have been taken before the fork */
1607 DBG("ust: forking");
1609 /* Get the pid of the new process */
1610 processpid
= getpid();
1613 * FIXME: This could be prettier, we loop over the list twice and
1614 * following good locking practice should lock around the loop
1616 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1617 ltt_trace_stop(trace
->trace_name
);
1620 /* Delete all active connections, but leave them in the epoll set */
1621 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1622 ustcomm_del_sock(sock
, 1);
1626 * FIXME: This could be prettier, we loop over the list twice and
1627 * following good locking practice should lock around the loop
1629 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1630 ltt_trace_destroy(trace
->trace_name
, 1);
1633 /* Clean up the listener socket and epoll, keeping the socket file */
1635 ustcomm_del_named_sock(listen_sock
, 1);
1640 /* Re-start the launch sequence */
1641 CMM_STORE_SHARED(buffers_to_export
, 0);
1645 epoll_fd
= epoll_create(MAX_EVENTS
);
1646 if (epoll_fd
== -1) {
1647 ERR("epoll_create failed, tracing shutting down");
1651 /* Create the socket */
1652 listen_sock
= init_app_socket(epoll_fd
);
1654 ERR("failed to create application socket,"
1655 " tracing shutting down");
1659 ltt_trace_setup("auto");
1660 result
= ltt_trace_set_type("auto", "ustrelay");
1662 ERR("ltt_trace_set_type failed");
1666 ltt_trace_alloc("auto");
1667 ltt_trace_start("auto");
1668 inform_consumer_daemon("auto");
1671 void ust_before_fork(ust_fork_info_t
*fork_info
)
1673 /* Disable signals. This is to avoid that the child
1674 * intervenes before it is properly setup for tracing. It is
1675 * safer to disable all signals, because then we know we are not
1676 * breaking anything by restoring the original mask.
1682 - only do this if tracing is active
1685 /* Disable signals */
1686 sigfillset(&all_sigs
);
1687 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1689 PERROR("sigprocmask");
1694 * Take the fork lock to make sure we are not in the middle of
1695 * something in the listener thread.
1697 pthread_mutex_lock(&listener_thread_data_mutex
);
1699 * Hold listen_sock_mutex to protect from listen_sock teardown.
1701 pthread_mutex_lock(&listen_sock_mutex
);
1702 rcu_bp_before_fork();
1705 /* Don't call this function directly in a traced program */
1706 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1710 pthread_mutex_unlock(&listen_sock_mutex
);
1711 pthread_mutex_unlock(&listener_thread_data_mutex
);
1712 /* Restore signals */
1713 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1715 PERROR("sigprocmask");
1720 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1722 rcu_bp_after_fork_parent();
1723 /* Release mutexes and reenable signals */
1724 ust_after_fork_common(fork_info
);
1727 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1729 /* Release urcu mutexes */
1730 rcu_bp_after_fork_child();
1732 /* Sanitize the child */
1735 /* Release mutexes and reenable signals */
1736 ust_after_fork_common(fork_info
);