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>
46 #include "marker-control.h"
48 /* This should only be accessed by the constructor, before the creation
49 * of the listener, and then only by the listener.
53 /* The process pid is used to detect a non-traceable fork
54 * and allow the non-traceable fork to be ignored
55 * by destructor sequences in libust
57 static pid_t processpid
= 0;
59 static struct ustcomm_header _receive_header
;
60 static struct ustcomm_header
*receive_header
= &_receive_header
;
61 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
62 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
65 static struct ustcomm_sock
*listen_sock
;
67 extern struct chan_info_struct chan_infos
[];
69 static struct cds_list_head open_buffers_list
= CDS_LIST_HEAD_INIT(open_buffers_list
);
71 static struct cds_list_head ust_socks
= CDS_LIST_HEAD_INIT(ust_socks
);
73 /* volatile because shared between the listener and the main thread */
74 int buffers_to_export
= 0;
76 static long long make_pidunique(void)
81 gettimeofday(&tv
, NULL
);
90 static void print_markers(FILE *fp
)
92 struct marker_iter iter
;
95 marker_iter_reset(&iter
);
96 marker_iter_start(&iter
);
99 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
100 iter
.marker
->channel
,
102 (int)imv_read(iter
.marker
->state
),
104 iter
.marker
->location
);
105 marker_iter_next(&iter
);
110 static void print_trace_events(FILE *fp
)
112 struct trace_event_iter iter
;
115 trace_event_iter_reset(&iter
);
116 trace_event_iter_start(&iter
);
118 while (iter
.trace_event
) {
119 fprintf(fp
, "trace_event: %s\n", iter
.trace_event
->name
);
120 trace_event_iter_next(&iter
);
122 unlock_trace_events();
125 static int connect_ustd(void)
128 char default_daemon_path
[] = SOCK_DIR
"/ustd";
129 char *explicit_daemon_path
, *daemon_path
;
131 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
132 if (explicit_daemon_path
) {
133 daemon_path
= explicit_daemon_path
;
135 daemon_path
= default_daemon_path
;
138 DBG("Connecting to daemon_path %s", daemon_path
);
140 result
= ustcomm_connect_path(daemon_path
, &fd
);
142 WARN("connect_ustd failed, daemon_path: %s",
151 static void request_buffer_consumer(int sock
,
156 struct ustcomm_header send_header
, recv_header
;
157 struct ustcomm_buffer_info buf_inf
;
160 result
= ustcomm_pack_buffer_info(&send_header
,
167 ERR("failed to pack buffer info message %s_%d",
172 buf_inf
.pid
= getpid();
173 send_header
.command
= CONSUME_BUFFER
;
175 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
178 PERROR("request for buffer consumer failed, is the daemon online?");
184 /* Ask the daemon to collect a trace called trace_name and being
185 * produced by this pid.
187 * The trace must be at least allocated. (It can also be started.)
188 * This is because _ltt_trace_find is used.
191 static void inform_consumer_daemon(const char *trace_name
)
194 struct ust_trace
*trace
;
197 sock
= connect_ustd();
202 DBG("Connected to ustd");
206 trace
= _ltt_trace_find(trace_name
);
208 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
212 for (i
=0; i
< trace
->nr_channels
; i
++) {
213 if (trace
->channels
[i
].request_collection
) {
214 /* iterate on all cpus */
215 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
216 ch_name
= trace
->channels
[i
].channel_name
;
217 request_buffer_consumer(sock
, trace_name
,
219 CMM_STORE_SHARED(buffers_to_export
,
220 CMM_LOAD_SHARED(buffers_to_export
)+1);
231 static struct ust_channel
*find_channel(const char *ch_name
,
232 struct ust_trace
*trace
)
236 for (i
=0; i
<trace
->nr_channels
; i
++) {
237 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
238 return &trace
->channels
[i
];
245 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
248 int *buf_struct_shmid
,
251 struct ust_trace
*trace
;
252 struct ust_channel
*channel
;
253 struct ust_buffer
*buf
;
255 DBG("get_buffer_shmid_pipe_fd");
258 trace
= _ltt_trace_find(trace_name
);
262 ERR("cannot find trace!");
266 channel
= find_channel(ch_name
, trace
);
268 ERR("cannot find channel %s!", ch_name
);
272 buf
= channel
->buf
[ch_cpu
];
274 *buf_shmid
= buf
->shmid
;
275 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
276 *buf_pipe_fd
= buf
->data_ready_fd_read
;
281 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
284 struct ust_trace
*trace
;
285 struct ust_channel
*channel
;
287 DBG("get_subbuf_size");
290 trace
= _ltt_trace_find(trace_name
);
294 ERR("cannot find trace!");
298 channel
= find_channel(ch_name
, trace
);
300 ERR("unable to find channel");
304 *num
= channel
->subbuf_cnt
;
305 *size
= channel
->subbuf_size
;
310 /* Return the power of two which is equal or higher to v */
312 static unsigned int pow2_higher_or_eq(unsigned int v
)
315 int retval
= 1<<(hb
-1);
323 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
328 struct ust_trace
*trace
;
329 struct ust_channel
*channel
;
331 DBG("set_subbuf_size");
333 power
= pow2_higher_or_eq(size
);
334 power
= max_t(unsigned int, 2u, power
);
336 WARN("using the next power of two for buffer size = %u\n", power
);
340 trace
= _ltt_trace_find_setup(trace_name
);
342 ERR("cannot find trace!");
347 channel
= find_channel(ch_name
, trace
);
349 ERR("unable to find channel");
354 channel
->subbuf_size
= power
;
355 DBG("the set_subbuf_size for the requested channel is %u", channel
->subbuf_size
);
363 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
366 struct ust_trace
*trace
;
367 struct ust_channel
*channel
;
370 DBG("set_subbuf_num");
373 ERR("subbuffer count should be greater than 2");
378 trace
= _ltt_trace_find_setup(trace_name
);
380 ERR("cannot find trace!");
385 channel
= find_channel(ch_name
, trace
);
387 ERR("unable to find channel");
392 channel
->subbuf_cnt
= num
;
393 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
400 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
401 int ch_cpu
, long *consumed_old
)
404 struct ust_trace
*trace
;
405 struct ust_channel
*channel
;
406 struct ust_buffer
*buf
;
413 trace
= _ltt_trace_find(trace_name
);
416 DBG("Cannot find trace. It was likely destroyed by the user.");
421 channel
= find_channel(ch_name
, trace
);
423 ERR("unable to find channel");
428 buf
= channel
->buf
[ch_cpu
];
430 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
432 WARN("missed buffer?");
442 static int notify_buffer_mapped(const char *trace_name
,
447 struct ust_trace
*trace
;
448 struct ust_channel
*channel
;
449 struct ust_buffer
*buf
;
451 DBG("get_buffer_fd");
454 trace
= _ltt_trace_find(trace_name
);
458 DBG("Cannot find trace. It was likely destroyed by the user.");
462 channel
= find_channel(ch_name
, trace
);
465 ERR("unable to find channel");
469 buf
= channel
->buf
[ch_cpu
];
471 /* Being here is the proof the daemon has mapped the buffer in its
472 * memory. We may now decrement buffers_to_export.
474 if (uatomic_read(&buf
->consumed
) == 0) {
475 DBG("decrementing buffers_to_export");
476 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
479 /* The buffer has been exported, ergo, we can add it to the
480 * list of open buffers
482 cds_list_add(&buf
->open_buffers_list
, &open_buffers_list
);
490 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
491 int ch_cpu
, long consumed_old
)
494 struct ust_trace
*trace
;
495 struct ust_channel
*channel
;
496 struct ust_buffer
*buf
;
501 trace
= _ltt_trace_find(trace_name
);
505 DBG("Cannot find trace. It was likely destroyed by the user.");
509 channel
= find_channel(ch_name
, trace
);
512 ERR("unable to find channel");
516 buf
= channel
->buf
[ch_cpu
];
518 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
520 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
523 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
533 static void listener_cleanup(void *ptr
)
535 ustcomm_del_named_sock(listen_sock
, 0);
538 static void force_subbuf_switch()
540 struct ust_buffer
*buf
;
542 cds_list_for_each_entry(buf
, &open_buffers_list
,
544 ltt_force_switch(buf
, FORCE_FLUSH
);
548 /* Simple commands are those which need only respond with a return value. */
549 static int process_simple_client_cmd(int command
, char *recv_buf
)
556 struct ustcomm_single_field
*sock_msg
;
557 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
558 result
= ustcomm_unpack_single_field(sock_msg
);
562 return setenv("UST_DAEMON_SOCKET", sock_msg
->field
, 1);
565 case FORCE_SUBBUF_SWITCH
:
566 /* FIXME: return codes? */
567 force_subbuf_switch();
579 static int process_trace_cmd(int command
, char *trace_name
)
582 char trace_type
[] = "ustrelay";
586 /* start is an operation that setups the trace, allocates it and starts it */
587 result
= ltt_trace_setup(trace_name
);
589 ERR("ltt_trace_setup failed");
593 result
= ltt_trace_set_type(trace_name
, trace_type
);
595 ERR("ltt_trace_set_type failed");
599 result
= ltt_trace_alloc(trace_name
);
601 ERR("ltt_trace_alloc failed");
605 inform_consumer_daemon(trace_name
);
607 result
= ltt_trace_start(trace_name
);
609 ERR("ltt_trace_start failed");
617 result
= ltt_trace_setup(trace_name
);
619 ERR("ltt_trace_setup failed");
623 result
= ltt_trace_set_type(trace_name
, trace_type
);
625 ERR("ltt_trace_set_type failed");
633 result
= ltt_trace_alloc(trace_name
);
635 ERR("ltt_trace_alloc failed");
638 inform_consumer_daemon(trace_name
);
645 result
= ltt_trace_setup(trace_name
);
647 ERR("ltt_trace_setup failed");
651 result
= ltt_trace_set_type(trace_name
, trace_type
);
653 ERR("ltt_trace_set_type failed");
661 result
= ltt_trace_alloc(trace_name
);
663 ERR("ltt_trace_alloc failed");
667 inform_consumer_daemon(trace_name
);
670 result
= ltt_trace_start(trace_name
);
672 ERR("ltt_trace_start failed");
680 result
= ltt_trace_stop(trace_name
);
682 ERR("ltt_trace_stop failed");
688 DBG("trace destroy");
690 result
= ltt_trace_destroy(trace_name
, 0);
692 ERR("ltt_trace_destroy failed");
702 static void process_channel_cmd(int sock
, int command
,
703 struct ustcomm_channel_info
*ch_inf
)
705 struct ustcomm_header _reply_header
;
706 struct ustcomm_header
*reply_header
= &_reply_header
;
707 struct ustcomm_channel_info
*reply_msg
=
708 (struct ustcomm_channel_info
*)send_buffer
;
709 int result
, offset
= 0, num
, size
;
711 memset(reply_header
, 0, sizeof(*reply_header
));
714 case GET_SUBBUF_NUM_SIZE
:
715 result
= get_subbuf_num_size(ch_inf
->trace
,
719 reply_header
->result
= result
;
723 reply_msg
->channel
= USTCOMM_POISON_PTR
;
724 reply_msg
->subbuf_num
= num
;
725 reply_msg
->subbuf_size
= size
;
728 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
732 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
737 case SET_SUBBUF_SIZE
:
738 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
740 ch_inf
->subbuf_size
);
745 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
746 ERR("ustcomm_send failed");
750 static void process_buffer_cmd(int sock
, int command
,
751 struct ustcomm_buffer_info
*buf_inf
)
753 struct ustcomm_header _reply_header
;
754 struct ustcomm_header
*reply_header
= &_reply_header
;
755 struct ustcomm_buffer_info
*reply_msg
=
756 (struct ustcomm_buffer_info
*)send_buffer
;
757 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
760 memset(reply_header
, 0, sizeof(*reply_header
));
763 case GET_BUF_SHMID_PIPE_FD
:
764 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
771 reply_header
->result
= result
;
775 reply_msg
->channel
= USTCOMM_POISON_PTR
;
776 reply_msg
->buf_shmid
= buf_shmid
;
777 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
779 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
780 reply_header
->fd_included
= 1;
782 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
784 ERR("ustcomm_send failed");
788 case NOTIFY_BUF_MAPPED
:
789 reply_header
->result
=
790 notify_buffer_mapped(buf_inf
->trace
,
795 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
796 buf_inf
->ch_cpu
, &consumed_old
);
798 reply_header
->result
= result
;
802 reply_msg
->channel
= USTCOMM_POISON_PTR
;
803 reply_msg
->consumed_old
= consumed_old
;
805 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
809 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
811 buf_inf
->consumed_old
);
812 reply_header
->result
= result
;
817 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
818 ERR("ustcomm_send failed");
823 static void process_marker_cmd(int sock
, int command
,
824 struct ustcomm_marker_info
*marker_inf
)
826 struct ustcomm_header _reply_header
;
827 struct ustcomm_header
*reply_header
= &_reply_header
;
830 memset(reply_header
, 0, sizeof(*reply_header
));
835 result
= ltt_marker_connect(marker_inf
->channel
,
839 WARN("could not enable marker; channel=%s,"
847 result
= ltt_marker_disconnect(marker_inf
->channel
,
851 WARN("could not disable marker; channel=%s,"
859 reply_header
->result
= result
;
861 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
862 ERR("ustcomm_send failed");
866 static void process_client_cmd(struct ustcomm_header
*recv_header
,
867 char *recv_buf
, int sock
)
870 struct ustcomm_header _reply_header
;
871 struct ustcomm_header
*reply_header
= &_reply_header
;
872 char *send_buf
= send_buffer
;
874 memset(reply_header
, 0, sizeof(*reply_header
));
875 memset(send_buf
, 0, sizeof(send_buffer
));
877 switch(recv_header
->command
) {
878 case GET_SUBBUF_NUM_SIZE
:
880 case SET_SUBBUF_SIZE
:
882 struct ustcomm_channel_info
*ch_inf
;
883 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
884 result
= ustcomm_unpack_channel_info(ch_inf
);
886 ERR("couldn't unpack channel info");
887 reply_header
->result
= -EINVAL
;
890 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
893 case GET_BUF_SHMID_PIPE_FD
:
894 case NOTIFY_BUF_MAPPED
:
898 struct ustcomm_buffer_info
*buf_inf
;
899 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
900 result
= ustcomm_unpack_buffer_info(buf_inf
);
902 ERR("couldn't unpack buffer info");
903 reply_header
->result
= -EINVAL
;
906 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
912 struct ustcomm_marker_info
*marker_inf
;
913 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
914 result
= ustcomm_unpack_marker_info(marker_inf
);
916 ERR("couldn't unpack marker info");
917 reply_header
->result
= -EINVAL
;
920 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
929 fp
= open_memstream(&ptr
, &size
);
931 ERR("opening memstream failed");
937 reply_header
->size
= size
;
939 result
= ustcomm_send(sock
, reply_header
, ptr
);
944 PERROR("failed to send markers list");
949 case LIST_TRACE_EVENTS
:
955 fp
= open_memstream(&ptr
, &size
);
957 ERR("opening memstream failed");
960 print_trace_events(fp
);
963 reply_header
->size
= size
;
965 result
= ustcomm_send(sock
, reply_header
, ptr
);
970 ERR("list_trace_events failed");
980 /* FIXME: No functionality at all... */
983 DBG("load_probe_lib loading %s", libfile
);
989 struct ustcomm_pidunique
*pid_msg
;
990 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
992 pid_msg
->pidunique
= pidunique
;
993 reply_header
->size
= sizeof(pid_msg
);
1000 struct ustcomm_single_field
*sock_msg
;
1001 char *sock_path_env
;
1003 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1005 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1007 if (!sock_path_env
) {
1008 result
= ustcomm_pack_single_field(reply_header
,
1013 result
= ustcomm_pack_single_field(reply_header
,
1017 reply_header
->result
= result
;
1029 struct ustcomm_single_field
*trace_inf
=
1030 (struct ustcomm_single_field
*)recv_buf
;
1032 result
= ustcomm_unpack_single_field(trace_inf
);
1034 ERR("couldn't unpack trace info");
1035 reply_header
->result
= -EINVAL
;
1039 reply_header
->result
=
1040 process_trace_cmd(recv_header
->command
,
1046 reply_header
->result
=
1047 process_simple_client_cmd(recv_header
->command
,
1056 ustcomm_send(sock
, reply_header
, send_buf
);
1059 #define MAX_EVENTS 10
1061 void *listener_main(void *p
)
1063 struct ustcomm_sock
*epoll_sock
;
1064 struct epoll_event events
[MAX_EVENTS
];
1065 struct sockaddr addr
;
1066 int accept_fd
, nfds
, result
, i
, addr_size
;
1070 pthread_cleanup_push(listener_cleanup
, NULL
);
1073 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1075 PERROR("listener_main: epoll_wait failed");
1079 for (i
= 0; i
< nfds
; i
++) {
1080 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1081 if (epoll_sock
== listen_sock
) {
1082 addr_size
= sizeof(struct sockaddr
);
1083 accept_fd
= accept(epoll_sock
->fd
,
1085 (socklen_t
*)&addr_size
);
1086 if (accept_fd
== -1) {
1087 PERROR("listener_main: accept failed");
1090 ustcomm_init_sock(accept_fd
, epoll_fd
,
1093 memset(receive_header
, 0,
1094 sizeof(*receive_header
));
1095 memset(receive_buffer
, 0,
1096 sizeof(receive_buffer
));
1097 result
= ustcomm_recv(epoll_sock
->fd
,
1101 ustcomm_del_sock(epoll_sock
, 0);
1103 process_client_cmd(receive_header
,
1111 pthread_cleanup_pop(1);
1114 /* These should only be accessed in the parent thread,
1117 static volatile sig_atomic_t have_listener
= 0;
1118 static pthread_t listener_thread
;
1120 void create_listener(void)
1123 sigset_t sig_all_blocked
;
1124 sigset_t orig_parent_mask
;
1126 if (have_listener
) {
1127 WARN("not creating listener because we already had one");
1131 /* A new thread created by pthread_create inherits the signal mask
1132 * from the parent. To avoid any signal being received by the
1133 * listener thread, we block all signals temporarily in the parent,
1134 * while we create the listener thread.
1137 sigfillset(&sig_all_blocked
);
1139 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1141 PERROR("pthread_sigmask: %s", strerror(result
));
1144 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1146 PERROR("pthread_create");
1149 /* Restore original signal mask in parent */
1150 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1152 PERROR("pthread_sigmask: %s", strerror(result
));
1158 #define AUTOPROBE_DISABLED 0
1159 #define AUTOPROBE_ENABLE_ALL 1
1160 #define AUTOPROBE_ENABLE_REGEX 2
1161 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1162 static regex_t autoprobe_regex
;
1164 static void auto_probe_connect(struct marker
*m
)
1168 char* concat_name
= NULL
;
1169 const char *probe_name
= "default";
1171 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1173 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1174 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1176 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1177 m
->channel
, m
->name
);
1180 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1187 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1188 if (result
&& result
!= -EEXIST
)
1189 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1191 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1195 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1199 struct ustcomm_sock
*sock
;
1201 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1203 ERR("string overflow allocating socket name, "
1204 "UST thread bailing");
1208 result
= ensure_dir_exists(SOCK_DIR
);
1210 ERR("Unable to create socket directory %s, UST thread bailing",
1215 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1217 ERR("Error initializing named socket (%s). Check that directory"
1218 "exists and that it is writable. UST thread bailing", name
);
1230 static void __attribute__((constructor
)) init()
1233 char* autoprobe_val
= NULL
;
1234 char* subbuffer_size_val
= NULL
;
1235 char* subbuffer_count_val
= NULL
;
1236 unsigned int subbuffer_size
;
1237 unsigned int subbuffer_count
;
1240 /* Assign the pidunique, to be able to differentiate the processes with same
1241 * pid, (before and after an exec).
1243 pidunique
= make_pidunique();
1244 processpid
= getpid();
1246 DBG("Tracectl constructor");
1249 epoll_fd
= epoll_create(MAX_EVENTS
);
1250 if (epoll_fd
== -1) {
1251 ERR("epoll_create failed, tracing shutting down");
1255 /* Create the socket */
1256 listen_sock
= init_app_socket(epoll_fd
);
1258 ERR("failed to create application socket,"
1259 " tracing shutting down");
1265 autoprobe_val
= getenv("UST_AUTOPROBE");
1266 if (autoprobe_val
) {
1267 struct marker_iter iter
;
1269 DBG("Autoprobe enabled.");
1271 /* Ensure markers are initialized */
1274 /* Ensure marker control is initialized, for the probe */
1275 init_marker_control();
1277 /* first, set the callback that will connect the
1278 * probe on new markers
1280 if (autoprobe_val
[0] == '/') {
1281 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1285 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1286 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1287 /* don't crash the application just for this */
1289 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1292 /* just enable all instrumentation */
1293 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1296 marker_set_new_marker_cb(auto_probe_connect
);
1298 /* Now, connect the probes that were already registered. */
1299 marker_iter_reset(&iter
);
1300 marker_iter_start(&iter
);
1302 DBG("now iterating on markers already registered");
1303 while (iter
.marker
) {
1304 DBG("now iterating on marker %s", iter
.marker
->name
);
1305 auto_probe_connect(iter
.marker
);
1306 marker_iter_next(&iter
);
1310 if (getenv("UST_OVERWRITE")) {
1311 int val
= atoi(getenv("UST_OVERWRITE"));
1312 if (val
== 0 || val
== 1) {
1313 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1315 WARN("invalid value for UST_OVERWRITE");
1319 if (getenv("UST_AUTOCOLLECT")) {
1320 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1321 if (val
== 0 || val
== 1) {
1322 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1324 WARN("invalid value for UST_AUTOCOLLECT");
1328 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1329 if (subbuffer_size_val
) {
1330 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1331 power
= pow2_higher_or_eq(subbuffer_size
);
1332 if (power
!= subbuffer_size
)
1333 WARN("using the next power of two for buffer size = %u\n", power
);
1334 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1337 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1338 if (subbuffer_count_val
) {
1339 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1340 if (subbuffer_count
< 2)
1341 subbuffer_count
= 2;
1342 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1345 if (getenv("UST_TRACE")) {
1346 char trace_name
[] = "auto";
1347 char trace_type
[] = "ustrelay";
1349 DBG("starting early tracing");
1351 /* Ensure marker control is initialized */
1352 init_marker_control();
1354 /* Ensure markers are initialized */
1357 /* Ensure buffers are initialized, for the transport to be available.
1358 * We are about to set a trace type and it will fail without this.
1360 init_ustrelay_transport();
1362 /* FIXME: When starting early tracing (here), depending on the
1363 * order of constructors, it is very well possible some marker
1364 * sections are not yet registered. Because of this, some
1365 * channels may not be registered. Yet, we are about to ask the
1366 * daemon to collect the channels. Channels which are not yet
1367 * registered will not be collected.
1369 * Currently, in LTTng, there is no way to add a channel after
1370 * trace start. The reason for this is that it induces complex
1371 * concurrency issues on the trace structures, which can only
1372 * be resolved using RCU. This has not been done yet. As a
1373 * workaround, we are forcing the registration of the "ust"
1374 * channel here. This is the only channel (apart from metadata)
1375 * that can be reliably used in early tracing.
1377 * Non-early tracing does not have this problem and can use
1378 * arbitrary channel names.
1380 ltt_channels_register("ust");
1382 result
= ltt_trace_setup(trace_name
);
1384 ERR("ltt_trace_setup failed");
1388 result
= ltt_trace_set_type(trace_name
, trace_type
);
1390 ERR("ltt_trace_set_type failed");
1394 result
= ltt_trace_alloc(trace_name
);
1396 ERR("ltt_trace_alloc failed");
1400 result
= ltt_trace_start(trace_name
);
1402 ERR("ltt_trace_start failed");
1406 /* Do this after the trace is started in order to avoid creating confusion
1407 * if the trace fails to start. */
1408 inform_consumer_daemon(trace_name
);
1413 /* should decrementally destroy stuff if error */
1417 /* This is only called if we terminate normally, not with an unhandled signal,
1418 * so we cannot rely on it. However, for now, LTTV requires that the header of
1419 * the last sub-buffer contain a valid end time for the trace. This is done
1420 * automatically only when the trace is properly stopped.
1422 * If the traced program crashed, it is always possible to manually add the
1423 * right value in the header, or to open the trace in text mode.
1425 * FIXME: Fix LTTV so it doesn't need this.
1428 static void destroy_traces(void)
1432 /* if trace running, finish it */
1434 DBG("destructor stopping traces");
1436 result
= ltt_trace_stop("auto");
1438 ERR("ltt_trace_stop error");
1441 result
= ltt_trace_destroy("auto", 0);
1443 ERR("ltt_trace_destroy error");
1447 static int trace_recording(void)
1450 struct ust_trace
*trace
;
1454 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1455 if (trace
->active
) {
1461 ltt_unlock_traces();
1466 int restarting_usleep(useconds_t usecs
)
1472 tv
.tv_nsec
= usecs
* 1000;
1475 result
= nanosleep(&tv
, &tv
);
1476 } while (result
== -1 && errno
== EINTR
);
1481 static void stop_listener(void)
1488 result
= pthread_cancel(listener_thread
);
1490 ERR("pthread_cancel: %s", strerror(result
));
1492 result
= pthread_join(listener_thread
, NULL
);
1494 ERR("pthread_join: %s", strerror(result
));
1498 /* This destructor keeps the process alive for a few seconds in order
1499 * to leave time to ustd to connect to its buffers. This is necessary
1500 * for programs whose execution is very short. It is also useful in all
1501 * programs when tracing is started close to the end of the program
1504 * FIXME: For now, this only works for the first trace created in a
1508 static void __attribute__((destructor
)) keepalive()
1510 if (processpid
!= getpid()) {
1514 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1516 DBG("Keeping process alive for consumer daemon...");
1517 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1518 const int interv
= 200000;
1519 restarting_usleep(interv
);
1522 if (total
>= 3000000) {
1523 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1527 DBG("Finally dying...");
1532 /* Ask the listener to stop and clean up. */
1536 void ust_potential_exec(void)
1538 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1545 /* Notify ust that there was a fork. This needs to be called inside
1546 * the new process, anytime a process whose memory is not shared with
1547 * the parent is created. If this function is not called, the events
1548 * of the new process will not be collected.
1550 * Signals should be disabled before the fork and reenabled only after
1551 * this call in order to guarantee tracing is not started before ust_fork()
1552 * sanitizes the new process.
1555 static void ust_fork(void)
1557 struct ust_buffer
*buf
, *buf_tmp
;
1558 struct ustcomm_sock
*sock
, *sock_tmp
;
1561 /* FIXME: technically, the locks could have been taken before the fork */
1562 DBG("ust: forking");
1564 /* Get the pid of the new process */
1565 processpid
= getpid();
1567 /* break lock if necessary */
1568 ltt_unlock_traces();
1570 ltt_trace_stop("auto");
1571 ltt_trace_destroy("auto", 1);
1572 /* Delete all active connections, but leave them in the epoll set */
1573 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1574 ustcomm_del_sock(sock
, 1);
1577 /* Delete all blocked consumers */
1578 cds_list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1579 open_buffers_list
) {
1580 result
= close(buf
->data_ready_fd_read
);
1584 result
= close(buf
->data_ready_fd_write
);
1588 cds_list_del(&buf
->open_buffers_list
);
1591 /* Clean up the listener socket and epoll, keeping the scoket file */
1592 ustcomm_del_named_sock(listen_sock
, 1);
1595 /* Re-start the launch sequence */
1596 CMM_STORE_SHARED(buffers_to_export
, 0);
1600 epoll_fd
= epoll_create(MAX_EVENTS
);
1601 if (epoll_fd
== -1) {
1602 ERR("epoll_create failed, tracing shutting down");
1606 /* Create the socket */
1607 listen_sock
= init_app_socket(epoll_fd
);
1609 ERR("failed to create application socket,"
1610 " tracing shutting down");
1614 ltt_trace_setup("auto");
1615 result
= ltt_trace_set_type("auto", "ustrelay");
1617 ERR("ltt_trace_set_type failed");
1621 ltt_trace_alloc("auto");
1622 ltt_trace_start("auto");
1623 inform_consumer_daemon("auto");
1626 void ust_before_fork(ust_fork_info_t
*fork_info
)
1628 /* Disable signals. This is to avoid that the child
1629 * intervenes before it is properly setup for tracing. It is
1630 * safer to disable all signals, because then we know we are not
1631 * breaking anything by restoring the original mask.
1637 - only do this if tracing is active
1640 /* Disable signals */
1641 sigfillset(&all_sigs
);
1642 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1644 PERROR("sigprocmask");
1649 /* Don't call this function directly in a traced program */
1650 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1654 /* Restore signals */
1655 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1657 PERROR("sigprocmask");
1662 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1664 /* Reenable signals */
1665 ust_after_fork_common(fork_info
);
1668 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1670 /* First sanitize the child */
1673 /* Then reenable interrupts */
1674 ust_after_fork_common(fork_info
);