1 /* Copyright (C) 2009 Pierre-Marc Fournier
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 /* This file contains the implementation of the UST listener thread, which
19 * receives trace control commands. It also coordinates the initialization of
29 #include <sys/epoll.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <urcu/uatomic_arch.h>
37 #include <urcu/list.h>
39 #include <ust/marker.h>
40 #include <ust/tracepoint.h>
41 #include <ust/tracectl.h>
42 #include <ust/clock.h>
47 #include "marker-control.h"
49 /* This should only be accessed by the constructor, before the creation
50 * of the listener, and then only by the listener.
54 /* The process pid is used to detect a non-traceable fork
55 * and allow the non-traceable fork to be ignored
56 * by destructor sequences in libust
58 static pid_t processpid
= 0;
60 static struct ustcomm_header _receive_header
;
61 static struct ustcomm_header
*receive_header
= &_receive_header
;
62 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
63 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
68 * Listener thread data vs fork() protection mechanism. Ensures that no listener
69 * thread mutexes and data structures are being concurrently modified or held by
70 * other threads when fork() is executed.
72 static pthread_mutex_t listener_thread_data_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 /* Mutex protecting listen_sock. Nests inside listener_thread_data_mutex. */
75 static pthread_mutex_t listen_sock_mutex
= PTHREAD_MUTEX_INITIALIZER
;
76 static struct ustcomm_sock
*listen_sock
;
78 extern struct chan_info_struct chan_infos
[];
80 static struct cds_list_head 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_markers(FILE *fp
)
103 struct marker_iter iter
;
106 marker_iter_reset(&iter
);
107 marker_iter_start(&iter
);
109 while (iter
.marker
) {
110 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
111 (*iter
.marker
)->channel
,
112 (*iter
.marker
)->name
,
113 (int)imv_read((*iter
.marker
)->state
),
114 (*iter
.marker
)->format
,
115 (*iter
.marker
)->location
);
116 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_marker_cmd(int sock
, int command
,
834 struct ustcomm_marker_info
*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_marker_connect(marker_inf
->channel
,
849 WARN("could not enable marker; channel=%s,"
857 result
= ltt_marker_disconnect(marker_inf
->channel
,
861 WARN("could not disable marker; channel=%s,"
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_marker_info
*marker_inf
;
923 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
924 result
= ustcomm_unpack_marker_info(marker_inf
);
926 ERR("couldn't unpack marker info");
927 reply_header
->result
= -EINVAL
;
930 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
939 fp
= open_memstream(&ptr
, &size
);
941 ERR("opening memstream failed");
947 reply_header
->size
= size
+ 1; /* Include final \0 */
949 result
= ustcomm_send(sock
, reply_header
, ptr
);
954 PERROR("failed to send markers 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 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 (marker %s/%s)",
1204 m
->channel
, m
->name
);
1207 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1214 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1215 if (result
&& result
!= -EEXIST
)
1216 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1218 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1222 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1226 struct ustcomm_sock
*sock
;
1228 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1230 ERR("string overflow allocating socket name, "
1231 "UST thread bailing");
1235 result
= ensure_dir_exists(SOCK_DIR
);
1237 ERR("Unable to create socket directory %s, UST thread bailing",
1242 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1244 ERR("Error initializing named socket (%s). Check that directory"
1245 "exists and that it is writable. UST thread bailing", name
);
1257 static void __attribute__((constructor
)) init()
1261 char* autoprobe_val
= NULL
;
1262 char* subbuffer_size_val
= NULL
;
1263 char* subbuffer_count_val
= NULL
;
1264 unsigned int subbuffer_size
;
1265 unsigned int subbuffer_count
;
1268 /* Assign the pidunique, to be able to differentiate the processes with same
1269 * pid, (before and after an exec).
1271 pidunique
= make_pidunique();
1272 processpid
= getpid();
1274 DBG("Tracectl constructor");
1277 epoll_fd
= epoll_create(MAX_EVENTS
);
1278 if (epoll_fd
== -1) {
1279 ERR("epoll_create failed, tracing shutting down");
1283 /* Create the socket */
1284 listen_sock
= init_app_socket(epoll_fd
);
1286 ERR("failed to create application socket,"
1287 " tracing shutting down");
1293 /* Get clock the clock source type */
1295 /* Default clock source */
1296 ust_clock_source
= CLOCK_TRACE
;
1297 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1298 ust_clock_source
= CLOCK_MONOTONIC
;
1299 DBG("UST traces will not be synchronized with LTTng traces");
1302 autoprobe_val
= getenv("UST_AUTOPROBE");
1303 if (autoprobe_val
) {
1304 struct marker_iter iter
;
1306 DBG("Autoprobe enabled.");
1308 /* Ensure markers are initialized */
1311 /* Ensure marker control is initialized, for the probe */
1312 init_marker_control();
1314 /* first, set the callback that will connect the
1315 * probe on new markers
1317 if (autoprobe_val
[0] == '/') {
1318 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1322 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1323 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1324 /* don't crash the application just for this */
1326 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1329 /* just enable all instrumentation */
1330 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1333 marker_set_new_marker_cb(auto_probe_connect
);
1335 /* Now, connect the probes that were already registered. */
1336 marker_iter_reset(&iter
);
1337 marker_iter_start(&iter
);
1339 DBG("now iterating on markers already registered");
1340 while (iter
.marker
) {
1341 DBG("now iterating on marker %s", (*iter
.marker
)->name
);
1342 auto_probe_connect(*iter
.marker
);
1343 marker_iter_next(&iter
);
1347 if (getenv("UST_OVERWRITE")) {
1348 int val
= atoi(getenv("UST_OVERWRITE"));
1349 if (val
== 0 || val
== 1) {
1350 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1352 WARN("invalid value for UST_OVERWRITE");
1356 if (getenv("UST_AUTOCOLLECT")) {
1357 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1358 if (val
== 0 || val
== 1) {
1359 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1361 WARN("invalid value for UST_AUTOCOLLECT");
1365 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1366 if (subbuffer_size_val
) {
1367 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1368 power
= pow2_higher_or_eq(subbuffer_size
);
1369 if (power
!= subbuffer_size
)
1370 WARN("using the next power of two for buffer size = %u\n", power
);
1371 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1374 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1375 if (subbuffer_count_val
) {
1376 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1377 if (subbuffer_count
< 2)
1378 subbuffer_count
= 2;
1379 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1382 if (getenv("UST_TRACE")) {
1383 char trace_name
[] = "auto";
1384 char trace_type
[] = "ustrelay";
1386 DBG("starting early tracing");
1388 /* Ensure marker control is initialized */
1389 init_marker_control();
1391 /* Ensure markers are initialized */
1394 /* Ensure buffers are initialized, for the transport to be available.
1395 * We are about to set a trace type and it will fail without this.
1397 init_ustrelay_transport();
1399 /* FIXME: When starting early tracing (here), depending on the
1400 * order of constructors, it is very well possible some marker
1401 * sections are not yet registered. Because of this, some
1402 * channels may not be registered. Yet, we are about to ask the
1403 * daemon to collect the channels. Channels which are not yet
1404 * registered will not be collected.
1406 * Currently, in LTTng, there is no way to add a channel after
1407 * trace start. The reason for this is that it induces complex
1408 * concurrency issues on the trace structures, which can only
1409 * be resolved using RCU. This has not been done yet. As a
1410 * workaround, we are forcing the registration of the "ust"
1411 * channel here. This is the only channel (apart from metadata)
1412 * that can be reliably used in early tracing.
1414 * Non-early tracing does not have this problem and can use
1415 * arbitrary channel names.
1417 ltt_channels_register("ust");
1419 result
= ltt_trace_setup(trace_name
);
1421 ERR("ltt_trace_setup failed");
1425 result
= ltt_trace_set_type(trace_name
, trace_type
);
1427 ERR("ltt_trace_set_type failed");
1431 result
= ltt_trace_alloc(trace_name
);
1433 ERR("ltt_trace_alloc failed");
1437 result
= ltt_trace_start(trace_name
);
1439 ERR("ltt_trace_start failed");
1443 /* Do this after the trace is started in order to avoid creating confusion
1444 * if the trace fails to start. */
1445 inform_consumer_daemon(trace_name
);
1450 /* should decrementally destroy stuff if error */
1454 /* This is only called if we terminate normally, not with an unhandled signal,
1455 * so we cannot rely on it. However, for now, LTTV requires that the header of
1456 * the last sub-buffer contain a valid end time for the trace. This is done
1457 * automatically only when the trace is properly stopped.
1459 * If the traced program crashed, it is always possible to manually add the
1460 * right value in the header, or to open the trace in text mode.
1462 * FIXME: Fix LTTV so it doesn't need this.
1465 static void destroy_traces(void)
1469 /* if trace running, finish it */
1471 DBG("destructor stopping traces");
1473 result
= ltt_trace_stop("auto");
1475 ERR("ltt_trace_stop error");
1478 result
= ltt_trace_destroy("auto", 0);
1480 ERR("ltt_trace_destroy error");
1484 static int trace_recording(void)
1487 struct ust_trace
*trace
;
1491 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1492 if (trace
->active
) {
1498 ltt_unlock_traces();
1503 int restarting_usleep(useconds_t usecs
)
1509 tv
.tv_nsec
= usecs
* 1000;
1512 result
= nanosleep(&tv
, &tv
);
1513 } while (result
== -1 && errno
== EINTR
);
1518 static void stop_listener(void)
1525 result
= pthread_cancel(listener_thread
);
1527 ERR("pthread_cancel: %s", strerror(result
));
1529 result
= pthread_join(listener_thread
, NULL
);
1531 ERR("pthread_join: %s", strerror(result
));
1535 /* This destructor keeps the process alive for a few seconds in order
1536 * to leave time for ustconsumer to connect to its buffers. This is necessary
1537 * for programs whose execution is very short. It is also useful in all
1538 * programs when tracing is started close to the end of the program
1541 * FIXME: For now, this only works for the first trace created in a
1545 static void __attribute__((destructor
)) keepalive()
1547 if (processpid
!= getpid()) {
1551 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1553 DBG("Keeping process alive for consumer daemon...");
1554 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1555 const int interv
= 200000;
1556 restarting_usleep(interv
);
1559 if (total
>= 3000000) {
1560 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1564 DBG("Finally dying...");
1569 /* Ask the listener to stop and clean up. */
1573 void ust_potential_exec(void)
1575 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1582 /* Notify ust that there was a fork. This needs to be called inside
1583 * the new process, anytime a process whose memory is not shared with
1584 * the parent is created. If this function is not called, the events
1585 * of the new process will not be collected.
1587 * Signals should be disabled before the fork and reenabled only after
1588 * this call in order to guarantee tracing is not started before ust_fork()
1589 * sanitizes the new process.
1592 static void ust_fork(void)
1594 struct ust_buffer
*buf
, *buf_tmp
;
1595 struct ustcomm_sock
*sock
, *sock_tmp
;
1596 struct ust_trace
*trace
, *trace_tmp
;
1599 /* FIXME: technically, the locks could have been taken before the fork */
1600 DBG("ust: forking");
1602 /* Get the pid of the new process */
1603 processpid
= getpid();
1606 * FIXME: This could be prettier, we loop over the list twice and
1607 * following good locking practice should lock around the loop
1609 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1610 ltt_trace_stop(trace
->trace_name
);
1613 /* Delete all active connections, but leave them in the epoll set */
1614 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1615 ustcomm_del_sock(sock
, 1);
1619 * FIXME: This could be prettier, we loop over the list twice and
1620 * following good locking practice should lock around the loop
1622 cds_list_for_each_entry_safe(trace
, trace_tmp
, <t_traces
.head
, list
) {
1623 ltt_trace_destroy(trace
->trace_name
, 1);
1626 /* Clean up the listener socket and epoll, keeping the socket file */
1628 ustcomm_del_named_sock(listen_sock
, 1);
1633 /* Re-start the launch sequence */
1634 CMM_STORE_SHARED(buffers_to_export
, 0);
1638 epoll_fd
= epoll_create(MAX_EVENTS
);
1639 if (epoll_fd
== -1) {
1640 ERR("epoll_create failed, tracing shutting down");
1644 /* Create the socket */
1645 listen_sock
= init_app_socket(epoll_fd
);
1647 ERR("failed to create application socket,"
1648 " tracing shutting down");
1652 ltt_trace_setup("auto");
1653 result
= ltt_trace_set_type("auto", "ustrelay");
1655 ERR("ltt_trace_set_type failed");
1659 ltt_trace_alloc("auto");
1660 ltt_trace_start("auto");
1661 inform_consumer_daemon("auto");
1664 void ust_before_fork(ust_fork_info_t
*fork_info
)
1666 /* Disable signals. This is to avoid that the child
1667 * intervenes before it is properly setup for tracing. It is
1668 * safer to disable all signals, because then we know we are not
1669 * breaking anything by restoring the original mask.
1675 - only do this if tracing is active
1678 /* Disable signals */
1679 sigfillset(&all_sigs
);
1680 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1682 PERROR("sigprocmask");
1687 * Take the fork lock to make sure we are not in the middle of
1688 * something in the listener thread.
1690 pthread_mutex_lock(&listener_thread_data_mutex
);
1692 * Hold listen_sock_mutex to protect from listen_sock teardown.
1694 pthread_mutex_lock(&listen_sock_mutex
);
1695 rcu_bp_before_fork();
1698 /* Don't call this function directly in a traced program */
1699 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1703 pthread_mutex_unlock(&listen_sock_mutex
);
1704 pthread_mutex_unlock(&listener_thread_data_mutex
);
1706 /* Restore signals */
1707 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1709 PERROR("sigprocmask");
1714 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1716 rcu_bp_after_fork_parent();
1717 /* Release mutexes and reenable signals */
1718 ust_after_fork_common(fork_info
);
1721 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1723 /* Release urcu mutexes */
1724 rcu_bp_after_fork_child();
1726 /* Sanitize the child */
1729 /* Then release mutexes and reenable signals */
1730 ust_after_fork_common(fork_info
);