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>
44 #include "usterr_signal_safe.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 ust_socks
= CDS_LIST_HEAD_INIT(ust_socks
);
82 /* volatile because shared between the listener and the main thread */
83 int buffers_to_export
= 0;
87 static long long make_pidunique(void)
92 gettimeofday(&tv
, NULL
);
101 static void print_ust_marker(FILE *fp
)
103 struct ust_marker_iter iter
;
106 ust_marker_iter_reset(&iter
);
107 ust_marker_iter_start(&iter
);
109 while (iter
.ust_marker
) {
110 fprintf(fp
, "ust_marker: %s/%s %d \"%s\" %p\n",
111 (*iter
.ust_marker
)->channel
,
112 (*iter
.ust_marker
)->name
,
113 (int)(*iter
.ust_marker
)->state
,
114 (*iter
.ust_marker
)->format
,
115 (*iter
.ust_marker
)->location
);
116 ust_marker_iter_next(&iter
);
121 static void print_trace_events(FILE *fp
)
123 struct trace_event_iter iter
;
126 trace_event_iter_reset(&iter
);
127 trace_event_iter_start(&iter
);
129 while (iter
.trace_event
) {
130 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
131 trace_event_iter_next(&iter
);
133 unlock_trace_events();
136 static int connect_ustconsumer(void)
139 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
140 char *explicit_daemon_path
, *daemon_path
;
142 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
143 if (explicit_daemon_path
) {
144 daemon_path
= explicit_daemon_path
;
146 daemon_path
= default_daemon_path
;
149 DBG("Connecting to daemon_path %s", daemon_path
);
151 result
= ustcomm_connect_path(daemon_path
, &fd
);
153 WARN("connect_ustconsumer failed, daemon_path: %s",
162 static void request_buffer_consumer(int sock
,
167 struct ustcomm_header send_header
, recv_header
;
168 struct ustcomm_buffer_info buf_inf
;
171 result
= ustcomm_pack_buffer_info(&send_header
,
178 ERR("failed to pack buffer info message %s_%d",
183 buf_inf
.pid
= getpid();
184 send_header
.command
= CONSUME_BUFFER
;
186 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
189 PERROR("request for buffer consumer failed, is the daemon online?");
195 /* Ask the daemon to collect a trace called trace_name and being
196 * produced by this pid.
198 * The trace must be at least allocated. (It can also be started.)
199 * This is because _ltt_trace_find is used.
202 static void inform_consumer_daemon(const char *trace_name
)
205 struct ust_trace
*trace
;
208 sock
= connect_ustconsumer();
213 DBG("Connected to ustconsumer");
217 trace
= _ltt_trace_find(trace_name
);
219 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
223 for (i
=0; i
< trace
->nr_channels
; i
++) {
224 if (trace
->channels
[i
].request_collection
) {
225 /* iterate on all cpus */
226 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
227 ch_name
= trace
->channels
[i
].channel_name
;
228 request_buffer_consumer(sock
, trace_name
,
230 CMM_STORE_SHARED(buffers_to_export
,
231 CMM_LOAD_SHARED(buffers_to_export
)+1);
242 static struct ust_channel
*find_channel(const char *ch_name
,
243 struct ust_trace
*trace
)
247 for (i
=0; i
<trace
->nr_channels
; i
++) {
248 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
249 return &trace
->channels
[i
];
256 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
259 int *buf_struct_shmid
,
262 struct ust_trace
*trace
;
263 struct ust_channel
*channel
;
264 struct ust_buffer
*buf
;
266 DBG("get_buffer_shmid_pipe_fd");
269 trace
= _ltt_trace_find(trace_name
);
273 ERR("cannot find trace!");
277 channel
= find_channel(ch_name
, trace
);
279 ERR("cannot find channel %s!", ch_name
);
283 buf
= channel
->buf
[ch_cpu
];
285 *buf_shmid
= buf
->shmid
;
286 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
287 *buf_pipe_fd
= buf
->data_ready_fd_read
;
292 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
295 struct ust_trace
*trace
;
296 struct ust_channel
*channel
;
298 DBG("get_subbuf_size");
301 trace
= _ltt_trace_find(trace_name
);
305 ERR("cannot find trace!");
309 channel
= find_channel(ch_name
, trace
);
311 ERR("unable to find channel");
315 *num
= channel
->subbuf_cnt
;
316 *size
= channel
->subbuf_size
;
321 /* Return the power of two which is equal or higher to v */
323 static unsigned int pow2_higher_or_eq(unsigned int v
)
326 int retval
= 1<<(hb
-1);
334 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
339 struct ust_trace
*trace
;
340 struct ust_channel
*channel
;
342 DBG("set_subbuf_size");
344 power
= pow2_higher_or_eq(size
);
345 power
= max_t(unsigned int, 2u, power
);
347 WARN("using the next power of two for buffer size = %u\n", power
);
351 trace
= _ltt_trace_find_setup(trace_name
);
353 ERR("cannot find trace!");
358 channel
= find_channel(ch_name
, trace
);
360 ERR("unable to find channel");
365 channel
->subbuf_size
= power
;
366 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
374 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
377 struct ust_trace
*trace
;
378 struct ust_channel
*channel
;
381 DBG("set_subbuf_num");
384 ERR("subbuffer count should be greater than 2");
389 trace
= _ltt_trace_find_setup(trace_name
);
391 ERR("cannot find trace!");
396 channel
= find_channel(ch_name
, trace
);
398 ERR("unable to find channel");
403 channel
->subbuf_cnt
= num
;
404 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
411 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
412 int ch_cpu
, long *consumed_old
)
415 struct ust_trace
*trace
;
416 struct ust_channel
*channel
;
417 struct ust_buffer
*buf
;
424 trace
= _ltt_trace_find(trace_name
);
427 DBG("Cannot find trace. It was likely destroyed by the user.");
432 channel
= find_channel(ch_name
, trace
);
434 ERR("unable to find channel");
439 buf
= channel
->buf
[ch_cpu
];
441 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
443 WARN("missed buffer?");
453 static int notify_buffer_mapped(const char *trace_name
,
458 struct ust_trace
*trace
;
459 struct ust_channel
*channel
;
460 struct ust_buffer
*buf
;
462 DBG("get_buffer_fd");
465 trace
= _ltt_trace_find(trace_name
);
469 DBG("Cannot find trace. It was likely destroyed by the user.");
473 channel
= find_channel(ch_name
, trace
);
476 ERR("unable to find channel");
480 buf
= channel
->buf
[ch_cpu
];
482 /* Being here is the proof the daemon has mapped the buffer in its
483 * memory. We may now decrement buffers_to_export.
485 if (uatomic_read(&buf
->consumed
) == 0) {
486 DBG("decrementing buffers_to_export");
487 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
496 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
497 int ch_cpu
, long consumed_old
)
500 struct ust_trace
*trace
;
501 struct ust_channel
*channel
;
502 struct ust_buffer
*buf
;
507 trace
= _ltt_trace_find(trace_name
);
511 DBG("Cannot find trace. It was likely destroyed by the user.");
515 channel
= find_channel(ch_name
, trace
);
518 ERR("unable to find channel");
522 buf
= channel
->buf
[ch_cpu
];
524 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
526 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
529 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
539 static void release_listener_mutex(void *ptr
)
541 pthread_mutex_unlock(&listener_thread_data_mutex
);
544 static void listener_cleanup(void *ptr
)
546 pthread_mutex_lock(&listen_sock_mutex
);
548 ustcomm_del_named_sock(listen_sock
, 0);
551 pthread_mutex_unlock(&listen_sock_mutex
);
554 static int force_subbuf_switch(const char *trace_name
)
556 struct ust_trace
*trace
;
557 int i
, j
, retval
= 0;
560 trace
= _ltt_trace_find(trace_name
);
563 DBG("Cannot find trace. It was likely destroyed by the user.");
567 for (i
= 0; i
< trace
->nr_channels
; i
++) {
568 for (j
= 0; j
< trace
->channels
[i
].n_cpus
; j
++) {
569 ltt_force_switch(trace
->channels
[i
].buf
[j
],
580 static int process_trace_cmd(int command
, char *trace_name
)
583 char trace_type
[] = "ustrelay";
587 /* start is an operation that setups the trace, allocates it and starts it */
588 result
= ltt_trace_setup(trace_name
);
590 ERR("ltt_trace_setup failed");
594 result
= ltt_trace_set_type(trace_name
, trace_type
);
596 ERR("ltt_trace_set_type failed");
600 result
= ltt_trace_alloc(trace_name
);
602 ERR("ltt_trace_alloc failed");
606 inform_consumer_daemon(trace_name
);
608 result
= ltt_trace_start(trace_name
);
610 ERR("ltt_trace_start failed");
618 result
= ltt_trace_setup(trace_name
);
620 ERR("ltt_trace_setup failed");
624 result
= ltt_trace_set_type(trace_name
, trace_type
);
626 ERR("ltt_trace_set_type failed");
634 result
= ltt_trace_alloc(trace_name
);
636 ERR("ltt_trace_alloc failed");
639 inform_consumer_daemon(trace_name
);
646 result
= ltt_trace_setup(trace_name
);
648 ERR("ltt_trace_setup failed");
652 result
= ltt_trace_set_type(trace_name
, trace_type
);
654 ERR("ltt_trace_set_type failed");
662 result
= ltt_trace_alloc(trace_name
);
664 ERR("ltt_trace_alloc failed");
668 inform_consumer_daemon(trace_name
);
671 result
= ltt_trace_start(trace_name
);
673 ERR("ltt_trace_start failed");
681 result
= ltt_trace_stop(trace_name
);
683 ERR("ltt_trace_stop failed");
689 DBG("trace destroy");
691 result
= ltt_trace_destroy(trace_name
, 0);
693 ERR("ltt_trace_destroy failed");
697 case FORCE_SUBBUF_SWITCH
:
700 result
= force_subbuf_switch(trace_name
);
702 ERR("force_subbuf_switch failed");
712 static void process_channel_cmd(int sock
, int command
,
713 struct ustcomm_channel_info
*ch_inf
)
715 struct ustcomm_header _reply_header
;
716 struct ustcomm_header
*reply_header
= &_reply_header
;
717 struct ustcomm_channel_info
*reply_msg
=
718 (struct ustcomm_channel_info
*)send_buffer
;
719 int result
, offset
= 0, num
, size
;
721 memset(reply_header
, 0, sizeof(*reply_header
));
724 case GET_SUBBUF_NUM_SIZE
:
725 result
= get_subbuf_num_size(ch_inf
->trace
,
729 reply_header
->result
= result
;
733 reply_msg
->channel
= USTCOMM_POISON_PTR
;
734 reply_msg
->subbuf_num
= num
;
735 reply_msg
->subbuf_size
= size
;
738 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
742 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
747 case SET_SUBBUF_SIZE
:
748 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
750 ch_inf
->subbuf_size
);
755 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
756 ERR("ustcomm_send failed");
760 static void process_buffer_cmd(int sock
, int command
,
761 struct ustcomm_buffer_info
*buf_inf
)
763 struct ustcomm_header _reply_header
;
764 struct ustcomm_header
*reply_header
= &_reply_header
;
765 struct ustcomm_buffer_info
*reply_msg
=
766 (struct ustcomm_buffer_info
*)send_buffer
;
767 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
770 memset(reply_header
, 0, sizeof(*reply_header
));
773 case GET_BUF_SHMID_PIPE_FD
:
774 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
781 reply_header
->result
= result
;
785 reply_msg
->channel
= USTCOMM_POISON_PTR
;
786 reply_msg
->buf_shmid
= buf_shmid
;
787 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
789 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
790 reply_header
->fd_included
= 1;
792 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
794 ERR("ustcomm_send failed");
798 case NOTIFY_BUF_MAPPED
:
799 reply_header
->result
=
800 notify_buffer_mapped(buf_inf
->trace
,
805 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
806 buf_inf
->ch_cpu
, &consumed_old
);
808 reply_header
->result
= result
;
812 reply_msg
->channel
= USTCOMM_POISON_PTR
;
813 reply_msg
->consumed_old
= consumed_old
;
815 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
819 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
821 buf_inf
->consumed_old
);
822 reply_header
->result
= result
;
827 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
828 ERR("ustcomm_send failed");
833 static void process_ust_marker_cmd(int sock
, int command
,
834 struct ustcomm_ust_marker_info
*ust_marker_inf
)
836 struct ustcomm_header _reply_header
;
837 struct ustcomm_header
*reply_header
= &_reply_header
;
840 memset(reply_header
, 0, sizeof(*reply_header
));
845 result
= ltt_ust_marker_connect(ust_marker_inf
->channel
,
846 ust_marker_inf
->ust_marker
,
849 WARN("could not enable ust_marker; channel=%s,"
851 ust_marker_inf
->channel
,
852 ust_marker_inf
->ust_marker
);
857 result
= ltt_ust_marker_disconnect(ust_marker_inf
->channel
,
858 ust_marker_inf
->ust_marker
,
861 WARN("could not disable ust_marker; channel=%s,"
863 ust_marker_inf
->channel
,
864 ust_marker_inf
->ust_marker
);
869 reply_header
->result
= result
;
871 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
872 ERR("ustcomm_send failed");
876 static void process_client_cmd(struct ustcomm_header
*recv_header
,
877 char *recv_buf
, int sock
)
880 struct ustcomm_header _reply_header
;
881 struct ustcomm_header
*reply_header
= &_reply_header
;
882 char *send_buf
= send_buffer
;
884 memset(reply_header
, 0, sizeof(*reply_header
));
885 memset(send_buf
, 0, sizeof(send_buffer
));
887 switch(recv_header
->command
) {
888 case GET_SUBBUF_NUM_SIZE
:
890 case SET_SUBBUF_SIZE
:
892 struct ustcomm_channel_info
*ch_inf
;
893 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
894 result
= ustcomm_unpack_channel_info(ch_inf
);
896 ERR("couldn't unpack channel info");
897 reply_header
->result
= -EINVAL
;
900 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
903 case GET_BUF_SHMID_PIPE_FD
:
904 case NOTIFY_BUF_MAPPED
:
908 struct ustcomm_buffer_info
*buf_inf
;
909 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
910 result
= ustcomm_unpack_buffer_info(buf_inf
);
912 ERR("couldn't unpack buffer info");
913 reply_header
->result
= -EINVAL
;
916 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
922 struct ustcomm_ust_marker_info
*ust_marker_inf
;
923 ust_marker_inf
= (struct ustcomm_ust_marker_info
*)recv_buf
;
924 result
= ustcomm_unpack_ust_marker_info(ust_marker_inf
);
926 ERR("couldn't unpack ust_marker info");
927 reply_header
->result
= -EINVAL
;
930 process_ust_marker_cmd(sock
, recv_header
->command
, ust_marker_inf
);
939 fp
= open_memstream(&ptr
, &size
);
941 ERR("opening memstream failed");
944 print_ust_marker(fp
);
947 reply_header
->size
= size
+ 1; /* Include final \0 */
949 result
= ustcomm_send(sock
, reply_header
, ptr
);
954 PERROR("failed to send ust_marker list");
959 case LIST_TRACE_EVENTS
:
965 fp
= open_memstream(&ptr
, &size
);
967 ERR("opening memstream failed");
970 print_trace_events(fp
);
973 reply_header
->size
= size
+ 1; /* Include final \0 */
975 result
= ustcomm_send(sock
, reply_header
, ptr
);
980 ERR("list_trace_events failed");
990 /* FIXME: No functionality at all... */
993 DBG("load_probe_lib loading %s", libfile
);
999 struct ustcomm_pidunique
*pid_msg
;
1000 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
1002 pid_msg
->pidunique
= pidunique
;
1003 reply_header
->size
= sizeof(pid_msg
);
1010 struct ustcomm_single_field
*sock_msg
;
1011 char *sock_path_env
;
1013 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1015 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1017 if (!sock_path_env
) {
1018 result
= ustcomm_pack_single_field(reply_header
,
1020 SOCK_DIR
"/ustconsumer");
1023 result
= ustcomm_pack_single_field(reply_header
,
1027 reply_header
->result
= result
;
1033 struct ustcomm_single_field
*sock_msg
;
1034 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
1035 result
= ustcomm_unpack_single_field(sock_msg
);
1037 reply_header
->result
= -EINVAL
;
1041 reply_header
->result
= setenv("UST_DAEMON_SOCKET",
1042 sock_msg
->field
, 1);
1053 case FORCE_SUBBUF_SWITCH
:
1055 struct ustcomm_single_field
*trace_inf
=
1056 (struct ustcomm_single_field
*)recv_buf
;
1058 result
= ustcomm_unpack_single_field(trace_inf
);
1060 ERR("couldn't unpack trace info");
1061 reply_header
->result
= -EINVAL
;
1065 reply_header
->result
=
1066 process_trace_cmd(recv_header
->command
,
1072 reply_header
->result
= -EINVAL
;
1080 ustcomm_send(sock
, reply_header
, send_buf
);
1083 #define MAX_EVENTS 10
1085 void *listener_main(void *p
)
1087 struct ustcomm_sock
*epoll_sock
;
1088 struct epoll_event events
[MAX_EVENTS
];
1089 struct sockaddr addr
;
1090 int accept_fd
, nfds
, result
, i
, addr_size
;
1094 pthread_cleanup_push(listener_cleanup
, NULL
);
1097 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1099 PERROR("listener_main: epoll_wait failed");
1103 for (i
= 0; i
< nfds
; i
++) {
1104 pthread_mutex_lock(&listener_thread_data_mutex
);
1105 pthread_cleanup_push(release_listener_mutex
, NULL
);
1106 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1107 if (epoll_sock
== listen_sock
) {
1108 addr_size
= sizeof(struct sockaddr
);
1109 accept_fd
= accept(epoll_sock
->fd
,
1111 (socklen_t
*)&addr_size
);
1112 if (accept_fd
== -1) {
1113 PERROR("listener_main: accept failed");
1116 ustcomm_init_sock(accept_fd
, epoll_fd
,
1119 memset(receive_header
, 0,
1120 sizeof(*receive_header
));
1121 memset(receive_buffer
, 0,
1122 sizeof(receive_buffer
));
1123 result
= ustcomm_recv(epoll_sock
->fd
,
1127 ustcomm_del_sock(epoll_sock
, 0);
1129 process_client_cmd(receive_header
,
1134 pthread_cleanup_pop(1); /* release listener mutex */
1138 pthread_cleanup_pop(1);
1141 /* These should only be accessed in the parent thread,
1144 static volatile sig_atomic_t have_listener
= 0;
1145 static pthread_t listener_thread
;
1147 void create_listener(void)
1150 sigset_t sig_all_blocked
;
1151 sigset_t orig_parent_mask
;
1153 if (have_listener
) {
1154 WARN("not creating listener because we already had one");
1158 /* A new thread created by pthread_create inherits the signal mask
1159 * from the parent. To avoid any signal being received by the
1160 * listener thread, we block all signals temporarily in the parent,
1161 * while we create the listener thread.
1164 sigfillset(&sig_all_blocked
);
1166 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1168 PERROR("pthread_sigmask: %s", strerror(result
));
1171 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1173 PERROR("pthread_create");
1176 /* Restore original signal mask in parent */
1177 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1179 PERROR("pthread_sigmask: %s", strerror(result
));
1185 #define AUTOPROBE_DISABLED 0
1186 #define AUTOPROBE_ENABLE_ALL 1
1187 #define AUTOPROBE_ENABLE_REGEX 2
1188 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1189 static regex_t autoprobe_regex
;
1191 static void auto_probe_connect(struct ust_marker
*m
)
1195 char* concat_name
= NULL
;
1196 const char *probe_name
= "default";
1198 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1200 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1201 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1203 ERR("auto_probe_connect: asprintf failed (ust_marker %s/%s)",
1204 m
->channel
, m
->name
);
1207 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1214 result
= ltt_ust_marker_connect(m
->channel
, m
->name
, probe_name
);
1215 if (result
&& result
!= -EEXIST
)
1216 ERR("ltt_ust_marker_connect (ust_marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1218 DBG("auto connected ust_marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1222 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1224 char *dir_name
, *sock_name
;
1226 struct ustcomm_sock
*sock
= NULL
;
1228 dir_name
= ustcomm_user_sock_dir();
1232 result
= asprintf(&sock_name
, "%s/%d", dir_name
, (int)getpid());
1234 ERR("string overflow allocating socket name, "
1235 "UST thread bailing");
1239 result
= ensure_dir_exists(dir_name
, S_IRWXU
);
1241 ERR("Unable to create socket directory %s, UST thread bailing",
1243 goto free_sock_name
;
1246 sock
= ustcomm_init_named_socket(sock_name
, epoll_fd
);
1248 ERR("Error initializing named socket (%s). Check that directory"
1249 "exists and that it is writable. UST thread bailing", sock_name
);
1250 goto free_sock_name
;
1261 static void __attribute__((constructor
)) init()
1265 char* autoprobe_val
= NULL
;
1266 char* subbuffer_size_val
= NULL
;
1267 char* subbuffer_count_val
= NULL
;
1268 unsigned int subbuffer_size
;
1269 unsigned int subbuffer_count
;
1272 /* Assign the pidunique, to be able to differentiate the processes with same
1273 * pid, (before and after an exec).
1275 pidunique
= make_pidunique();
1276 processpid
= getpid();
1278 DBG("Tracectl constructor");
1281 epoll_fd
= epoll_create(MAX_EVENTS
);
1282 if (epoll_fd
== -1) {
1283 ERR("epoll_create failed, tracing shutting down");
1287 /* Create the socket */
1288 listen_sock
= init_app_socket(epoll_fd
);
1290 ERR("failed to create application socket,"
1291 " tracing shutting down");
1297 /* Get clock the clock source type */
1299 /* Default clock source */
1300 ust_clock_source
= CLOCK_TRACE
;
1301 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1302 ust_clock_source
= CLOCK_MONOTONIC
;
1303 DBG("UST traces will not be synchronized with LTTng traces");
1306 autoprobe_val
= getenv("UST_AUTOPROBE");
1307 if (autoprobe_val
) {
1308 struct ust_marker_iter iter
;
1310 DBG("Autoprobe enabled.");
1312 /* Ensure ust_marker are initialized */
1313 //init_ust_marker();
1315 /* Ensure ust_marker control is initialized, for the probe */
1316 init_ust_marker_control();
1318 /* first, set the callback that will connect the
1319 * probe on new ust_marker
1321 if (autoprobe_val
[0] == '/') {
1322 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1326 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1327 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1328 /* don't crash the application just for this */
1330 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1333 /* just enable all instrumentation */
1334 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1337 ust_marker_set_new_ust_marker_cb(auto_probe_connect
);
1339 /* Now, connect the probes that were already registered. */
1340 ust_marker_iter_reset(&iter
);
1341 ust_marker_iter_start(&iter
);
1343 DBG("now iterating on ust_marker already registered");
1344 while (iter
.ust_marker
) {
1345 DBG("now iterating on ust_marker %s", (*iter
.ust_marker
)->name
);
1346 auto_probe_connect(*iter
.ust_marker
);
1347 ust_marker_iter_next(&iter
);
1351 if (getenv("UST_OVERWRITE")) {
1352 int val
= atoi(getenv("UST_OVERWRITE"));
1353 if (val
== 0 || val
== 1) {
1354 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1356 WARN("invalid value for UST_OVERWRITE");
1360 if (getenv("UST_AUTOCOLLECT")) {
1361 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1362 if (val
== 0 || val
== 1) {
1363 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1365 WARN("invalid value for UST_AUTOCOLLECT");
1369 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1370 if (subbuffer_size_val
) {
1371 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1372 power
= pow2_higher_or_eq(subbuffer_size
);
1373 if (power
!= subbuffer_size
)
1374 WARN("using the next power of two for buffer size = %u\n", power
);
1375 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1378 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1379 if (subbuffer_count_val
) {
1380 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1381 if (subbuffer_count
< 2)
1382 subbuffer_count
= 2;
1383 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1386 if (getenv("UST_TRACE")) {
1387 char trace_name
[] = "auto";
1388 char trace_type
[] = "ustrelay";
1390 DBG("starting early tracing");
1392 /* Ensure ust_marker control is initialized */
1393 init_ust_marker_control();
1395 /* Ensure ust_marker are initialized */
1398 /* Ensure buffers are initialized, for the transport to be available.
1399 * We are about to set a trace type and it will fail without this.
1401 init_ustrelay_transport();
1403 /* FIXME: When starting early tracing (here), depending on the
1404 * order of constructors, it is very well possible some ust_marker
1405 * sections are not yet registered. Because of this, some
1406 * channels may not be registered. Yet, we are about to ask the
1407 * daemon to collect the channels. Channels which are not yet
1408 * registered will not be collected.
1410 * Currently, in LTTng, there is no way to add a channel after
1411 * trace start. The reason for this is that it induces complex
1412 * concurrency issues on the trace structures, which can only
1413 * be resolved using RCU. This has not been done yet. As a
1414 * workaround, we are forcing the registration of the "ust"
1415 * channel here. This is the only channel (apart from metadata)
1416 * that can be reliably used in early tracing.
1418 * Non-early tracing does not have this problem and can use
1419 * arbitrary channel names.
1421 ltt_channels_register("ust");
1423 result
= ltt_trace_setup(trace_name
);
1425 ERR("ltt_trace_setup failed");
1429 result
= ltt_trace_set_type(trace_name
, trace_type
);
1431 ERR("ltt_trace_set_type failed");
1435 result
= ltt_trace_alloc(trace_name
);
1437 ERR("ltt_trace_alloc failed");
1441 result
= ltt_trace_start(trace_name
);
1443 ERR("ltt_trace_start failed");
1447 /* Do this after the trace is started in order to avoid creating confusion
1448 * if the trace fails to start. */
1449 inform_consumer_daemon(trace_name
);
1454 /* should decrementally destroy stuff if error */
1458 /* This is only called if we terminate normally, not with an unhandled signal,
1459 * so we cannot rely on it. However, for now, LTTV requires that the header of
1460 * the last sub-buffer contain a valid end time for the trace. This is done
1461 * automatically only when the trace is properly stopped.
1463 * If the traced program crashed, it is always possible to manually add the
1464 * right value in the header, or to open the trace in text mode.
1466 * FIXME: Fix LTTV so it doesn't need this.
1469 static void destroy_traces(void)
1473 /* if trace running, finish it */
1475 DBG("destructor stopping traces");
1477 result
= ltt_trace_stop("auto");
1479 ERR("ltt_trace_stop error");
1482 result
= ltt_trace_destroy("auto", 0);
1484 ERR("ltt_trace_destroy error");
1488 static int trace_recording(void)
1491 struct ust_trace
*trace
;
1495 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1496 if (trace
->active
) {
1502 ltt_unlock_traces();
1507 int restarting_usleep(useconds_t usecs
)
1513 tv
.tv_nsec
= usecs
* 1000;
1516 result
= nanosleep(&tv
, &tv
);
1517 } while (result
== -1 && errno
== EINTR
);
1522 static void stop_listener(void)
1529 result
= pthread_cancel(listener_thread
);
1531 ERR("pthread_cancel: %s", strerror(result
));
1533 result
= pthread_join(listener_thread
, NULL
);
1535 ERR("pthread_join: %s", strerror(result
));
1539 /* This destructor keeps the process alive for a few seconds in order
1540 * to leave time for ustconsumer to connect to its buffers. This is necessary
1541 * for programs whose execution is very short. It is also useful in all
1542 * programs when tracing is started close to the end of the program
1545 * FIXME: For now, this only works for the first trace created in a
1549 static void __attribute__((destructor
)) keepalive()
1551 if (processpid
!= getpid()) {
1555 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1557 DBG("Keeping process alive for consumer daemon...");
1558 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1559 const int interv
= 200000;
1560 restarting_usleep(interv
);
1563 if (total
>= 3000000) {
1564 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1568 DBG("Finally dying...");
1573 /* Ask the listener to stop and clean up. */
1577 void ust_potential_exec(void)
1579 ust_marker(potential_exec
, UST_MARKER_NOARGS
);
1586 /* Notify ust that there was a fork. This needs to be called inside
1587 * the new process, anytime a process whose memory is not shared with
1588 * the parent is created. If this function is not called, the events
1589 * of the new process will not be collected.
1591 * Signals should be disabled before the fork and reenabled only after
1592 * this call in order to guarantee tracing is not started before ust_fork()
1593 * sanitizes the new process.
1596 static void ust_fork(void)
1598 struct ustcomm_sock
*sock
, *sock_tmp
;
1599 struct ust_trace
*trace
, *trace_tmp
;
1602 /* FIXME: technically, the locks could have been taken before the fork */
1603 DBG("ust: forking");
1605 /* Get the pid of the new process */
1606 processpid
= getpid();
1609 * FIXME: This could be prettier, we loop over the list twice and
1610 * following good locking practice should lock around the loop
1612 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1613 ltt_trace_stop(trace
->trace_name
);
1616 /* Delete all active connections, but leave them in the epoll set */
1617 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1618 ustcomm_del_sock(sock
, 1);
1622 * FIXME: This could be prettier, we loop over the list twice and
1623 * following good locking practice should lock around the loop
1625 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1626 ltt_trace_destroy(trace
->trace_name
, 1);
1629 /* Clean up the listener socket and epoll, keeping the socket file */
1631 ustcomm_del_named_sock(listen_sock
, 1);
1636 /* Re-start the launch sequence */
1637 CMM_STORE_SHARED(buffers_to_export
, 0);
1641 epoll_fd
= epoll_create(MAX_EVENTS
);
1642 if (epoll_fd
== -1) {
1643 ERR("epoll_create failed, tracing shutting down");
1647 /* Create the socket */
1648 listen_sock
= init_app_socket(epoll_fd
);
1650 ERR("failed to create application socket,"
1651 " tracing shutting down");
1655 ltt_trace_setup("auto");
1656 result
= ltt_trace_set_type("auto", "ustrelay");
1658 ERR("ltt_trace_set_type failed");
1662 ltt_trace_alloc("auto");
1663 ltt_trace_start("auto");
1664 inform_consumer_daemon("auto");
1667 void ust_before_fork(ust_fork_info_t
*fork_info
)
1669 /* Disable signals. This is to avoid that the child
1670 * intervenes before it is properly setup for tracing. It is
1671 * safer to disable all signals, because then we know we are not
1672 * breaking anything by restoring the original mask.
1678 - only do this if tracing is active
1681 /* Disable signals */
1682 sigfillset(&all_sigs
);
1683 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1685 PERROR("sigprocmask");
1690 * Take the fork lock to make sure we are not in the middle of
1691 * something in the listener thread.
1693 pthread_mutex_lock(&listener_thread_data_mutex
);
1695 * Hold listen_sock_mutex to protect from listen_sock teardown.
1697 pthread_mutex_lock(&listen_sock_mutex
);
1698 rcu_bp_before_fork();
1701 /* Don't call this function directly in a traced program */
1702 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1706 pthread_mutex_unlock(&listen_sock_mutex
);
1707 pthread_mutex_unlock(&listener_thread_data_mutex
);
1709 /* Restore signals */
1710 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1712 PERROR("sigprocmask");
1717 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1719 rcu_bp_after_fork_parent();
1720 /* Release mutexes and reenable signals */
1721 ust_after_fork_common(fork_info
);
1724 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1726 /* Release urcu mutexes */
1727 rcu_bp_after_fork_child();
1729 /* Sanitize the child */
1732 /* Then release mutexes and reenable signals */
1733 ust_after_fork_common(fork_info
);