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
];
66 static struct ustcomm_sock
*listen_sock
;
68 extern struct chan_info_struct chan_infos
[];
70 static struct cds_list_head open_buffers_list
= CDS_LIST_HEAD_INIT(open_buffers_list
);
72 static struct cds_list_head ust_socks
= CDS_LIST_HEAD_INIT(ust_socks
);
74 /* volatile because shared between the listener and the main thread */
75 int buffers_to_export
= 0;
77 static long long make_pidunique(void)
82 gettimeofday(&tv
, NULL
);
91 static void print_markers(FILE *fp
)
93 struct marker_iter iter
;
96 marker_iter_reset(&iter
);
97 marker_iter_start(&iter
);
100 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
101 iter
.marker
->channel
,
103 (int)imv_read(iter
.marker
->state
),
105 iter
.marker
->location
);
106 marker_iter_next(&iter
);
111 static void print_trace_events(FILE *fp
)
113 struct trace_event_iter iter
;
116 trace_event_iter_reset(&iter
);
117 trace_event_iter_start(&iter
);
119 while (iter
.trace_event
) {
120 fprintf(fp
, "trace_event: %s\n", iter
.trace_event
->name
);
121 trace_event_iter_next(&iter
);
123 unlock_trace_events();
126 static int connect_ustconsumer(void)
129 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
130 char *explicit_daemon_path
, *daemon_path
;
132 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
133 if (explicit_daemon_path
) {
134 daemon_path
= explicit_daemon_path
;
136 daemon_path
= default_daemon_path
;
139 DBG("Connecting to daemon_path %s", daemon_path
);
141 result
= ustcomm_connect_path(daemon_path
, &fd
);
143 WARN("connect_ustconsumer failed, daemon_path: %s",
152 static void request_buffer_consumer(int sock
,
157 struct ustcomm_header send_header
, recv_header
;
158 struct ustcomm_buffer_info buf_inf
;
161 result
= ustcomm_pack_buffer_info(&send_header
,
168 ERR("failed to pack buffer info message %s_%d",
173 buf_inf
.pid
= getpid();
174 send_header
.command
= CONSUME_BUFFER
;
176 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
179 PERROR("request for buffer consumer failed, is the daemon online?");
185 /* Ask the daemon to collect a trace called trace_name and being
186 * produced by this pid.
188 * The trace must be at least allocated. (It can also be started.)
189 * This is because _ltt_trace_find is used.
192 static void inform_consumer_daemon(const char *trace_name
)
195 struct ust_trace
*trace
;
198 sock
= connect_ustconsumer();
203 DBG("Connected to ustconsumer");
207 trace
= _ltt_trace_find(trace_name
);
209 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
213 for (i
=0; i
< trace
->nr_channels
; i
++) {
214 if (trace
->channels
[i
].request_collection
) {
215 /* iterate on all cpus */
216 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
217 ch_name
= trace
->channels
[i
].channel_name
;
218 request_buffer_consumer(sock
, trace_name
,
220 CMM_STORE_SHARED(buffers_to_export
,
221 CMM_LOAD_SHARED(buffers_to_export
)+1);
232 static struct ust_channel
*find_channel(const char *ch_name
,
233 struct ust_trace
*trace
)
237 for (i
=0; i
<trace
->nr_channels
; i
++) {
238 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
239 return &trace
->channels
[i
];
246 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
249 int *buf_struct_shmid
,
252 struct ust_trace
*trace
;
253 struct ust_channel
*channel
;
254 struct ust_buffer
*buf
;
256 DBG("get_buffer_shmid_pipe_fd");
259 trace
= _ltt_trace_find(trace_name
);
263 ERR("cannot find trace!");
267 channel
= find_channel(ch_name
, trace
);
269 ERR("cannot find channel %s!", ch_name
);
273 buf
= channel
->buf
[ch_cpu
];
275 *buf_shmid
= buf
->shmid
;
276 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
277 *buf_pipe_fd
= buf
->data_ready_fd_read
;
282 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
285 struct ust_trace
*trace
;
286 struct ust_channel
*channel
;
288 DBG("get_subbuf_size");
291 trace
= _ltt_trace_find(trace_name
);
295 ERR("cannot find trace!");
299 channel
= find_channel(ch_name
, trace
);
301 ERR("unable to find channel");
305 *num
= channel
->subbuf_cnt
;
306 *size
= channel
->subbuf_size
;
311 /* Return the power of two which is equal or higher to v */
313 static unsigned int pow2_higher_or_eq(unsigned int v
)
316 int retval
= 1<<(hb
-1);
324 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
329 struct ust_trace
*trace
;
330 struct ust_channel
*channel
;
332 DBG("set_subbuf_size");
334 power
= pow2_higher_or_eq(size
);
335 power
= max_t(unsigned int, 2u, power
);
337 WARN("using the next power of two for buffer size = %u\n", power
);
341 trace
= _ltt_trace_find_setup(trace_name
);
343 ERR("cannot find trace!");
348 channel
= find_channel(ch_name
, trace
);
350 ERR("unable to find channel");
355 channel
->subbuf_size
= power
;
356 DBG("the set_subbuf_size for the requested channel is %u", channel
->subbuf_size
);
364 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
367 struct ust_trace
*trace
;
368 struct ust_channel
*channel
;
371 DBG("set_subbuf_num");
374 ERR("subbuffer count should be greater than 2");
379 trace
= _ltt_trace_find_setup(trace_name
);
381 ERR("cannot find trace!");
386 channel
= find_channel(ch_name
, trace
);
388 ERR("unable to find channel");
393 channel
->subbuf_cnt
= num
;
394 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
401 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
402 int ch_cpu
, long *consumed_old
)
405 struct ust_trace
*trace
;
406 struct ust_channel
*channel
;
407 struct ust_buffer
*buf
;
414 trace
= _ltt_trace_find(trace_name
);
417 DBG("Cannot find trace. It was likely destroyed by the user.");
422 channel
= find_channel(ch_name
, trace
);
424 ERR("unable to find channel");
429 buf
= channel
->buf
[ch_cpu
];
431 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
433 WARN("missed buffer?");
443 static int notify_buffer_mapped(const char *trace_name
,
448 struct ust_trace
*trace
;
449 struct ust_channel
*channel
;
450 struct ust_buffer
*buf
;
452 DBG("get_buffer_fd");
455 trace
= _ltt_trace_find(trace_name
);
459 DBG("Cannot find trace. It was likely destroyed by the user.");
463 channel
= find_channel(ch_name
, trace
);
466 ERR("unable to find channel");
470 buf
= channel
->buf
[ch_cpu
];
472 /* Being here is the proof the daemon has mapped the buffer in its
473 * memory. We may now decrement buffers_to_export.
475 if (uatomic_read(&buf
->consumed
) == 0) {
476 DBG("decrementing buffers_to_export");
477 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
480 /* The buffer has been exported, ergo, we can add it to the
481 * list of open buffers
483 cds_list_add(&buf
->open_buffers_list
, &open_buffers_list
);
491 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
492 int ch_cpu
, long consumed_old
)
495 struct ust_trace
*trace
;
496 struct ust_channel
*channel
;
497 struct ust_buffer
*buf
;
502 trace
= _ltt_trace_find(trace_name
);
506 DBG("Cannot find trace. It was likely destroyed by the user.");
510 channel
= find_channel(ch_name
, trace
);
513 ERR("unable to find channel");
517 buf
= channel
->buf
[ch_cpu
];
519 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
521 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
524 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
534 static void listener_cleanup(void *ptr
)
536 ustcomm_del_named_sock(listen_sock
, 0);
539 static void force_subbuf_switch()
541 struct ust_buffer
*buf
;
543 cds_list_for_each_entry(buf
, &open_buffers_list
,
545 ltt_force_switch(buf
, FORCE_FLUSH
);
549 /* Simple commands are those which need only respond with a return value. */
550 static int process_simple_client_cmd(int command
, char *recv_buf
)
557 struct ustcomm_single_field
*sock_msg
;
558 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
559 result
= ustcomm_unpack_single_field(sock_msg
);
563 return setenv("UST_DAEMON_SOCKET", sock_msg
->field
, 1);
566 case FORCE_SUBBUF_SWITCH
:
567 /* FIXME: return codes? */
568 force_subbuf_switch();
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");
703 static void process_channel_cmd(int sock
, int command
,
704 struct ustcomm_channel_info
*ch_inf
)
706 struct ustcomm_header _reply_header
;
707 struct ustcomm_header
*reply_header
= &_reply_header
;
708 struct ustcomm_channel_info
*reply_msg
=
709 (struct ustcomm_channel_info
*)send_buffer
;
710 int result
, offset
= 0, num
, size
;
712 memset(reply_header
, 0, sizeof(*reply_header
));
715 case GET_SUBBUF_NUM_SIZE
:
716 result
= get_subbuf_num_size(ch_inf
->trace
,
720 reply_header
->result
= result
;
724 reply_msg
->channel
= USTCOMM_POISON_PTR
;
725 reply_msg
->subbuf_num
= num
;
726 reply_msg
->subbuf_size
= size
;
729 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
733 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
738 case SET_SUBBUF_SIZE
:
739 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
741 ch_inf
->subbuf_size
);
746 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
747 ERR("ustcomm_send failed");
751 static void process_buffer_cmd(int sock
, int command
,
752 struct ustcomm_buffer_info
*buf_inf
)
754 struct ustcomm_header _reply_header
;
755 struct ustcomm_header
*reply_header
= &_reply_header
;
756 struct ustcomm_buffer_info
*reply_msg
=
757 (struct ustcomm_buffer_info
*)send_buffer
;
758 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
761 memset(reply_header
, 0, sizeof(*reply_header
));
764 case GET_BUF_SHMID_PIPE_FD
:
765 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
772 reply_header
->result
= result
;
776 reply_msg
->channel
= USTCOMM_POISON_PTR
;
777 reply_msg
->buf_shmid
= buf_shmid
;
778 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
780 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
781 reply_header
->fd_included
= 1;
783 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
785 ERR("ustcomm_send failed");
789 case NOTIFY_BUF_MAPPED
:
790 reply_header
->result
=
791 notify_buffer_mapped(buf_inf
->trace
,
796 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
797 buf_inf
->ch_cpu
, &consumed_old
);
799 reply_header
->result
= result
;
803 reply_msg
->channel
= USTCOMM_POISON_PTR
;
804 reply_msg
->consumed_old
= consumed_old
;
806 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
810 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
812 buf_inf
->consumed_old
);
813 reply_header
->result
= result
;
818 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
819 ERR("ustcomm_send failed");
824 static void process_marker_cmd(int sock
, int command
,
825 struct ustcomm_marker_info
*marker_inf
)
827 struct ustcomm_header _reply_header
;
828 struct ustcomm_header
*reply_header
= &_reply_header
;
831 memset(reply_header
, 0, sizeof(*reply_header
));
836 result
= ltt_marker_connect(marker_inf
->channel
,
840 WARN("could not enable marker; channel=%s,"
848 result
= ltt_marker_disconnect(marker_inf
->channel
,
852 WARN("could not disable marker; channel=%s,"
860 reply_header
->result
= result
;
862 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
863 ERR("ustcomm_send failed");
867 static void process_client_cmd(struct ustcomm_header
*recv_header
,
868 char *recv_buf
, int sock
)
871 struct ustcomm_header _reply_header
;
872 struct ustcomm_header
*reply_header
= &_reply_header
;
873 char *send_buf
= send_buffer
;
875 memset(reply_header
, 0, sizeof(*reply_header
));
876 memset(send_buf
, 0, sizeof(send_buffer
));
878 switch(recv_header
->command
) {
879 case GET_SUBBUF_NUM_SIZE
:
881 case SET_SUBBUF_SIZE
:
883 struct ustcomm_channel_info
*ch_inf
;
884 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
885 result
= ustcomm_unpack_channel_info(ch_inf
);
887 ERR("couldn't unpack channel info");
888 reply_header
->result
= -EINVAL
;
891 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
894 case GET_BUF_SHMID_PIPE_FD
:
895 case NOTIFY_BUF_MAPPED
:
899 struct ustcomm_buffer_info
*buf_inf
;
900 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
901 result
= ustcomm_unpack_buffer_info(buf_inf
);
903 ERR("couldn't unpack buffer info");
904 reply_header
->result
= -EINVAL
;
907 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
913 struct ustcomm_marker_info
*marker_inf
;
914 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
915 result
= ustcomm_unpack_marker_info(marker_inf
);
917 ERR("couldn't unpack marker info");
918 reply_header
->result
= -EINVAL
;
921 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
930 fp
= open_memstream(&ptr
, &size
);
932 ERR("opening memstream failed");
938 reply_header
->size
= size
;
940 result
= ustcomm_send(sock
, reply_header
, ptr
);
945 PERROR("failed to send markers list");
950 case LIST_TRACE_EVENTS
:
956 fp
= open_memstream(&ptr
, &size
);
958 ERR("opening memstream failed");
961 print_trace_events(fp
);
964 reply_header
->size
= size
;
966 result
= ustcomm_send(sock
, reply_header
, ptr
);
971 ERR("list_trace_events failed");
981 /* FIXME: No functionality at all... */
984 DBG("load_probe_lib loading %s", libfile
);
990 struct ustcomm_pidunique
*pid_msg
;
991 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
993 pid_msg
->pidunique
= pidunique
;
994 reply_header
->size
= sizeof(pid_msg
);
1001 struct ustcomm_single_field
*sock_msg
;
1002 char *sock_path_env
;
1004 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1006 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1008 if (!sock_path_env
) {
1009 result
= ustcomm_pack_single_field(reply_header
,
1011 SOCK_DIR
"/ustconsumer");
1014 result
= ustcomm_pack_single_field(reply_header
,
1018 reply_header
->result
= result
;
1030 struct ustcomm_single_field
*trace_inf
=
1031 (struct ustcomm_single_field
*)recv_buf
;
1033 result
= ustcomm_unpack_single_field(trace_inf
);
1035 ERR("couldn't unpack trace info");
1036 reply_header
->result
= -EINVAL
;
1040 reply_header
->result
=
1041 process_trace_cmd(recv_header
->command
,
1047 reply_header
->result
=
1048 process_simple_client_cmd(recv_header
->command
,
1057 ustcomm_send(sock
, reply_header
, send_buf
);
1060 #define MAX_EVENTS 10
1062 void *listener_main(void *p
)
1064 struct ustcomm_sock
*epoll_sock
;
1065 struct epoll_event events
[MAX_EVENTS
];
1066 struct sockaddr addr
;
1067 int accept_fd
, nfds
, result
, i
, addr_size
;
1071 pthread_cleanup_push(listener_cleanup
, NULL
);
1074 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1076 PERROR("listener_main: epoll_wait failed");
1080 for (i
= 0; i
< nfds
; i
++) {
1081 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1082 if (epoll_sock
== listen_sock
) {
1083 addr_size
= sizeof(struct sockaddr
);
1084 accept_fd
= accept(epoll_sock
->fd
,
1086 (socklen_t
*)&addr_size
);
1087 if (accept_fd
== -1) {
1088 PERROR("listener_main: accept failed");
1091 ustcomm_init_sock(accept_fd
, epoll_fd
,
1094 memset(receive_header
, 0,
1095 sizeof(*receive_header
));
1096 memset(receive_buffer
, 0,
1097 sizeof(receive_buffer
));
1098 result
= ustcomm_recv(epoll_sock
->fd
,
1102 ustcomm_del_sock(epoll_sock
, 0);
1104 process_client_cmd(receive_header
,
1112 pthread_cleanup_pop(1);
1115 /* These should only be accessed in the parent thread,
1118 static volatile sig_atomic_t have_listener
= 0;
1119 static pthread_t listener_thread
;
1121 void create_listener(void)
1124 sigset_t sig_all_blocked
;
1125 sigset_t orig_parent_mask
;
1127 if (have_listener
) {
1128 WARN("not creating listener because we already had one");
1132 /* A new thread created by pthread_create inherits the signal mask
1133 * from the parent. To avoid any signal being received by the
1134 * listener thread, we block all signals temporarily in the parent,
1135 * while we create the listener thread.
1138 sigfillset(&sig_all_blocked
);
1140 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1142 PERROR("pthread_sigmask: %s", strerror(result
));
1145 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1147 PERROR("pthread_create");
1150 /* Restore original signal mask in parent */
1151 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1153 PERROR("pthread_sigmask: %s", strerror(result
));
1159 #define AUTOPROBE_DISABLED 0
1160 #define AUTOPROBE_ENABLE_ALL 1
1161 #define AUTOPROBE_ENABLE_REGEX 2
1162 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1163 static regex_t autoprobe_regex
;
1165 static void auto_probe_connect(struct marker
*m
)
1169 char* concat_name
= NULL
;
1170 const char *probe_name
= "default";
1172 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1174 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1175 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1177 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1178 m
->channel
, m
->name
);
1181 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1188 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1189 if (result
&& result
!= -EEXIST
)
1190 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1192 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1196 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1200 struct ustcomm_sock
*sock
;
1202 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1204 ERR("string overflow allocating socket name, "
1205 "UST thread bailing");
1209 result
= ensure_dir_exists(SOCK_DIR
);
1211 ERR("Unable to create socket directory %s, UST thread bailing",
1216 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1218 ERR("Error initializing named socket (%s). Check that directory"
1219 "exists and that it is writable. UST thread bailing", name
);
1231 static void __attribute__((constructor
)) init()
1234 char* autoprobe_val
= NULL
;
1235 char* subbuffer_size_val
= NULL
;
1236 char* subbuffer_count_val
= NULL
;
1237 unsigned int subbuffer_size
;
1238 unsigned int subbuffer_count
;
1241 /* Assign the pidunique, to be able to differentiate the processes with same
1242 * pid, (before and after an exec).
1244 pidunique
= make_pidunique();
1245 processpid
= getpid();
1247 DBG("Tracectl constructor");
1250 epoll_fd
= epoll_create(MAX_EVENTS
);
1251 if (epoll_fd
== -1) {
1252 ERR("epoll_create failed, tracing shutting down");
1256 /* Create the socket */
1257 listen_sock
= init_app_socket(epoll_fd
);
1259 ERR("failed to create application socket,"
1260 " tracing shutting down");
1266 /* Get clock the clock source type */
1268 /* Default clock source */
1269 ust_clock_source
= CLOCK_TRACE
;
1270 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1271 ust_clock_source
= CLOCK_MONOTONIC
;
1272 DBG("UST traces will not be synchronized with LTTng traces");
1275 autoprobe_val
= getenv("UST_AUTOPROBE");
1276 if (autoprobe_val
) {
1277 struct marker_iter iter
;
1279 DBG("Autoprobe enabled.");
1281 /* Ensure markers are initialized */
1284 /* Ensure marker control is initialized, for the probe */
1285 init_marker_control();
1287 /* first, set the callback that will connect the
1288 * probe on new markers
1290 if (autoprobe_val
[0] == '/') {
1291 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1295 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1296 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1297 /* don't crash the application just for this */
1299 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1302 /* just enable all instrumentation */
1303 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1306 marker_set_new_marker_cb(auto_probe_connect
);
1308 /* Now, connect the probes that were already registered. */
1309 marker_iter_reset(&iter
);
1310 marker_iter_start(&iter
);
1312 DBG("now iterating on markers already registered");
1313 while (iter
.marker
) {
1314 DBG("now iterating on marker %s", iter
.marker
->name
);
1315 auto_probe_connect(iter
.marker
);
1316 marker_iter_next(&iter
);
1320 if (getenv("UST_OVERWRITE")) {
1321 int val
= atoi(getenv("UST_OVERWRITE"));
1322 if (val
== 0 || val
== 1) {
1323 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1325 WARN("invalid value for UST_OVERWRITE");
1329 if (getenv("UST_AUTOCOLLECT")) {
1330 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1331 if (val
== 0 || val
== 1) {
1332 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1334 WARN("invalid value for UST_AUTOCOLLECT");
1338 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1339 if (subbuffer_size_val
) {
1340 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1341 power
= pow2_higher_or_eq(subbuffer_size
);
1342 if (power
!= subbuffer_size
)
1343 WARN("using the next power of two for buffer size = %u\n", power
);
1344 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1347 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1348 if (subbuffer_count_val
) {
1349 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1350 if (subbuffer_count
< 2)
1351 subbuffer_count
= 2;
1352 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1355 if (getenv("UST_TRACE")) {
1356 char trace_name
[] = "auto";
1357 char trace_type
[] = "ustrelay";
1359 DBG("starting early tracing");
1361 /* Ensure marker control is initialized */
1362 init_marker_control();
1364 /* Ensure markers are initialized */
1367 /* Ensure buffers are initialized, for the transport to be available.
1368 * We are about to set a trace type and it will fail without this.
1370 init_ustrelay_transport();
1372 /* FIXME: When starting early tracing (here), depending on the
1373 * order of constructors, it is very well possible some marker
1374 * sections are not yet registered. Because of this, some
1375 * channels may not be registered. Yet, we are about to ask the
1376 * daemon to collect the channels. Channels which are not yet
1377 * registered will not be collected.
1379 * Currently, in LTTng, there is no way to add a channel after
1380 * trace start. The reason for this is that it induces complex
1381 * concurrency issues on the trace structures, which can only
1382 * be resolved using RCU. This has not been done yet. As a
1383 * workaround, we are forcing the registration of the "ust"
1384 * channel here. This is the only channel (apart from metadata)
1385 * that can be reliably used in early tracing.
1387 * Non-early tracing does not have this problem and can use
1388 * arbitrary channel names.
1390 ltt_channels_register("ust");
1392 result
= ltt_trace_setup(trace_name
);
1394 ERR("ltt_trace_setup failed");
1398 result
= ltt_trace_set_type(trace_name
, trace_type
);
1400 ERR("ltt_trace_set_type failed");
1404 result
= ltt_trace_alloc(trace_name
);
1406 ERR("ltt_trace_alloc failed");
1410 result
= ltt_trace_start(trace_name
);
1412 ERR("ltt_trace_start failed");
1416 /* Do this after the trace is started in order to avoid creating confusion
1417 * if the trace fails to start. */
1418 inform_consumer_daemon(trace_name
);
1423 /* should decrementally destroy stuff if error */
1427 /* This is only called if we terminate normally, not with an unhandled signal,
1428 * so we cannot rely on it. However, for now, LTTV requires that the header of
1429 * the last sub-buffer contain a valid end time for the trace. This is done
1430 * automatically only when the trace is properly stopped.
1432 * If the traced program crashed, it is always possible to manually add the
1433 * right value in the header, or to open the trace in text mode.
1435 * FIXME: Fix LTTV so it doesn't need this.
1438 static void destroy_traces(void)
1442 /* if trace running, finish it */
1444 DBG("destructor stopping traces");
1446 result
= ltt_trace_stop("auto");
1448 ERR("ltt_trace_stop error");
1451 result
= ltt_trace_destroy("auto", 0);
1453 ERR("ltt_trace_destroy error");
1457 static int trace_recording(void)
1460 struct ust_trace
*trace
;
1464 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1465 if (trace
->active
) {
1471 ltt_unlock_traces();
1476 int restarting_usleep(useconds_t usecs
)
1482 tv
.tv_nsec
= usecs
* 1000;
1485 result
= nanosleep(&tv
, &tv
);
1486 } while (result
== -1 && errno
== EINTR
);
1491 static void stop_listener(void)
1498 result
= pthread_cancel(listener_thread
);
1500 ERR("pthread_cancel: %s", strerror(result
));
1502 result
= pthread_join(listener_thread
, NULL
);
1504 ERR("pthread_join: %s", strerror(result
));
1508 /* This destructor keeps the process alive for a few seconds in order
1509 * to leave time for ustconsumer to connect to its buffers. This is necessary
1510 * for programs whose execution is very short. It is also useful in all
1511 * programs when tracing is started close to the end of the program
1514 * FIXME: For now, this only works for the first trace created in a
1518 static void __attribute__((destructor
)) keepalive()
1520 if (processpid
!= getpid()) {
1524 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1526 DBG("Keeping process alive for consumer daemon...");
1527 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1528 const int interv
= 200000;
1529 restarting_usleep(interv
);
1532 if (total
>= 3000000) {
1533 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1537 DBG("Finally dying...");
1542 /* Ask the listener to stop and clean up. */
1546 void ust_potential_exec(void)
1548 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1555 /* Notify ust that there was a fork. This needs to be called inside
1556 * the new process, anytime a process whose memory is not shared with
1557 * the parent is created. If this function is not called, the events
1558 * of the new process will not be collected.
1560 * Signals should be disabled before the fork and reenabled only after
1561 * this call in order to guarantee tracing is not started before ust_fork()
1562 * sanitizes the new process.
1565 static void ust_fork(void)
1567 struct ust_buffer
*buf
, *buf_tmp
;
1568 struct ustcomm_sock
*sock
, *sock_tmp
;
1571 /* FIXME: technically, the locks could have been taken before the fork */
1572 DBG("ust: forking");
1574 /* Get the pid of the new process */
1575 processpid
= getpid();
1577 /* break lock if necessary */
1578 ltt_unlock_traces();
1580 ltt_trace_stop("auto");
1581 ltt_trace_destroy("auto", 1);
1582 /* Delete all active connections, but leave them in the epoll set */
1583 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1584 ustcomm_del_sock(sock
, 1);
1587 /* Delete all blocked consumers */
1588 cds_list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1589 open_buffers_list
) {
1590 result
= close(buf
->data_ready_fd_read
);
1594 result
= close(buf
->data_ready_fd_write
);
1598 cds_list_del(&buf
->open_buffers_list
);
1601 /* Clean up the listener socket and epoll, keeping the scoket file */
1602 ustcomm_del_named_sock(listen_sock
, 1);
1605 /* Re-start the launch sequence */
1606 CMM_STORE_SHARED(buffers_to_export
, 0);
1610 epoll_fd
= epoll_create(MAX_EVENTS
);
1611 if (epoll_fd
== -1) {
1612 ERR("epoll_create failed, tracing shutting down");
1616 /* Create the socket */
1617 listen_sock
= init_app_socket(epoll_fd
);
1619 ERR("failed to create application socket,"
1620 " tracing shutting down");
1624 ltt_trace_setup("auto");
1625 result
= ltt_trace_set_type("auto", "ustrelay");
1627 ERR("ltt_trace_set_type failed");
1631 ltt_trace_alloc("auto");
1632 ltt_trace_start("auto");
1633 inform_consumer_daemon("auto");
1636 void ust_before_fork(ust_fork_info_t
*fork_info
)
1638 /* Disable signals. This is to avoid that the child
1639 * intervenes before it is properly setup for tracing. It is
1640 * safer to disable all signals, because then we know we are not
1641 * breaking anything by restoring the original mask.
1647 - only do this if tracing is active
1650 /* Disable signals */
1651 sigfillset(&all_sigs
);
1652 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1654 PERROR("sigprocmask");
1659 /* Don't call this function directly in a traced program */
1660 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1664 /* Restore signals */
1665 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1667 PERROR("sigprocmask");
1672 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1674 /* Reenable signals */
1675 ust_after_fork_common(fork_info
);
1678 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1680 /* First sanitize the child */
1683 /* Then reenable interrupts */
1684 ust_after_fork_common(fork_info
);