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 static struct ustcomm_header _receive_header
;
54 static struct ustcomm_header
*receive_header
= &_receive_header
;
55 static char receive_buffer
[USTCOMM_BUFFER_SIZE
];
56 static char send_buffer
[USTCOMM_BUFFER_SIZE
];
59 static struct ustcomm_sock
*listen_sock
;
61 extern struct chan_info_struct chan_infos
[];
63 static struct list_head open_buffers_list
= LIST_HEAD_INIT(open_buffers_list
);
65 static struct list_head ust_socks
= LIST_HEAD_INIT(ust_socks
);
67 /* volatile because shared between the listener and the main thread */
68 int buffers_to_export
= 0;
70 static long long make_pidunique(void)
75 gettimeofday(&tv
, NULL
);
84 static void print_markers(FILE *fp
)
86 struct marker_iter iter
;
89 marker_iter_reset(&iter
);
90 marker_iter_start(&iter
);
93 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
96 (int)imv_read(iter
.marker
->state
),
98 iter
.marker
->location
);
99 marker_iter_next(&iter
);
104 static void print_trace_events(FILE *fp
)
106 struct trace_event_iter iter
;
109 trace_event_iter_reset(&iter
);
110 trace_event_iter_start(&iter
);
112 while (iter
.trace_event
) {
113 fprintf(fp
, "trace_event: %s\n", iter
.trace_event
->name
);
114 trace_event_iter_next(&iter
);
116 unlock_trace_events();
119 static int connect_ustd(void)
122 char default_daemon_path
[] = SOCK_DIR
"/ustd";
123 char *explicit_daemon_path
, *daemon_path
;
125 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
126 if (explicit_daemon_path
) {
127 daemon_path
= explicit_daemon_path
;
129 daemon_path
= default_daemon_path
;
132 DBG("Connecting to daemon_path %s", daemon_path
);
134 result
= ustcomm_connect_path(daemon_path
, &fd
);
136 WARN("connect_ustd failed, daemon_path: %s",
145 static void request_buffer_consumer(int sock
,
149 struct ustcomm_header send_header
, recv_header
;
150 struct ustcomm_buffer_info buf_inf
;
153 result
= ustcomm_pack_buffer_info(&send_header
,
159 ERR("failed to pack buffer info message %s_%d",
164 buf_inf
.pid
= getpid();
165 send_header
.command
= CONSUME_BUFFER
;
167 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
170 PERROR("request for buffer consumer failed, is the daemon online?");
176 /* Ask the daemon to collect a trace called trace_name and being
177 * produced by this pid.
179 * The trace must be at least allocated. (It can also be started.)
180 * This is because _ltt_trace_find is used.
183 static void inform_consumer_daemon(const char *trace_name
)
186 struct ust_trace
*trace
;
189 sock
= connect_ustd();
194 DBG("Connected to ustd");
198 trace
= _ltt_trace_find(trace_name
);
200 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
204 for (i
=0; i
< trace
->nr_channels
; i
++) {
205 if (trace
->channels
[i
].request_collection
) {
206 /* iterate on all cpus */
207 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
208 ch_name
= trace
->channels
[i
].channel_name
;
209 request_buffer_consumer(sock
, ch_name
, j
);
210 STORE_SHARED(buffers_to_export
,
211 LOAD_SHARED(buffers_to_export
)+1);
222 static struct ust_channel
*find_channel(const char *ch_name
,
223 struct ust_trace
*trace
)
227 for (i
=0; i
<trace
->nr_channels
; i
++) {
228 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
229 return &trace
->channels
[i
];
236 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
239 int *buf_struct_shmid
,
242 struct ust_trace
*trace
;
243 struct ust_channel
*channel
;
244 struct ust_buffer
*buf
;
246 DBG("get_buffer_shmid_pipe_fd");
249 trace
= _ltt_trace_find(trace_name
);
253 ERR("cannot find trace!");
257 channel
= find_channel(ch_name
, trace
);
259 ERR("cannot find channel %s!", ch_name
);
263 buf
= channel
->buf
[ch_cpu
];
265 *buf_shmid
= buf
->shmid
;
266 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
267 *buf_pipe_fd
= buf
->data_ready_fd_read
;
272 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
275 struct ust_trace
*trace
;
276 struct ust_channel
*channel
;
278 DBG("get_subbuf_size");
281 trace
= _ltt_trace_find(trace_name
);
285 ERR("cannot find trace!");
289 channel
= find_channel(ch_name
, trace
);
291 ERR("unable to find channel");
295 *num
= channel
->subbuf_cnt
;
296 *size
= channel
->subbuf_size
;
301 /* Return the power of two which is equal or higher to v */
303 static unsigned int pow2_higher_or_eq(unsigned int v
)
306 int retval
= 1<<(hb
-1);
314 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
319 struct ust_trace
*trace
;
320 struct ust_channel
*channel
;
322 DBG("set_subbuf_size");
324 power
= pow2_higher_or_eq(size
);
325 power
= max_t(unsigned int, 2u, power
);
327 WARN("using the next power of two for buffer size = %u\n", power
);
331 trace
= _ltt_trace_find_setup(trace_name
);
333 ERR("cannot find trace!");
338 channel
= find_channel(ch_name
, trace
);
340 ERR("unable to find channel");
345 channel
->subbuf_size
= power
;
346 DBG("the set_subbuf_size for the requested channel is %u", channel
->subbuf_size
);
354 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
357 struct ust_trace
*trace
;
358 struct ust_channel
*channel
;
361 DBG("set_subbuf_num");
364 ERR("subbuffer count should be greater than 2");
369 trace
= _ltt_trace_find_setup(trace_name
);
371 ERR("cannot find trace!");
376 channel
= find_channel(ch_name
, trace
);
378 ERR("unable to find channel");
383 channel
->subbuf_cnt
= num
;
384 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
391 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
392 int ch_cpu
, long *consumed_old
)
395 struct ust_trace
*trace
;
396 struct ust_channel
*channel
;
397 struct ust_buffer
*buf
;
404 trace
= _ltt_trace_find(trace_name
);
407 DBG("Cannot find trace. It was likely destroyed by the user.");
412 channel
= find_channel(ch_name
, trace
);
414 ERR("unable to find channel");
419 buf
= channel
->buf
[ch_cpu
];
421 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
423 WARN("missed buffer?");
433 static int notify_buffer_mapped(const char *trace_name
,
438 struct ust_trace
*trace
;
439 struct ust_channel
*channel
;
440 struct ust_buffer
*buf
;
442 DBG("get_buffer_fd");
445 trace
= _ltt_trace_find(trace_name
);
449 DBG("Cannot find trace. It was likely destroyed by the user.");
453 channel
= find_channel(ch_name
, trace
);
456 ERR("unable to find channel");
460 buf
= channel
->buf
[ch_cpu
];
462 /* Being here is the proof the daemon has mapped the buffer in its
463 * memory. We may now decrement buffers_to_export.
465 if (uatomic_read(&buf
->consumed
) == 0) {
466 DBG("decrementing buffers_to_export");
467 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
470 /* The buffer has been exported, ergo, we can add it to the
471 * list of open buffers
473 list_add(&buf
->open_buffers_list
, &open_buffers_list
);
481 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
482 int ch_cpu
, long consumed_old
)
485 struct ust_trace
*trace
;
486 struct ust_channel
*channel
;
487 struct ust_buffer
*buf
;
492 trace
= _ltt_trace_find(trace_name
);
496 DBG("Cannot find trace. It was likely destroyed by the user.");
500 channel
= find_channel(ch_name
, trace
);
503 ERR("unable to find channel");
507 buf
= channel
->buf
[ch_cpu
];
509 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
511 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
514 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
524 static void listener_cleanup(void *ptr
)
526 ustcomm_del_named_sock(listen_sock
, 0);
529 static void force_subbuf_switch()
531 struct ust_buffer
*buf
;
533 list_for_each_entry(buf
, &open_buffers_list
,
535 ltt_force_switch(buf
, FORCE_FLUSH
);
539 /* Simple commands are those which need only respond with a return value. */
540 static int process_simple_client_cmd(int command
, char *recv_buf
)
543 char trace_type
[] = "ustrelay";
544 char trace_name
[] = "auto";
549 struct ustcomm_sock_path
*sock_msg
;
550 sock_msg
= (struct ustcomm_sock_path
*)recv_buf
;
551 sock_msg
->sock_path
=
552 ustcomm_restore_ptr(sock_msg
->sock_path
,
554 sizeof(sock_msg
->data
));
555 if (!sock_msg
->sock_path
) {
559 return setenv("UST_DAEMON_SOCKET", sock_msg
->sock_path
, 1);
562 /* start is an operation that setups the trace, allocates it and starts it */
563 result
= ltt_trace_setup(trace_name
);
565 ERR("ltt_trace_setup failed");
569 result
= ltt_trace_set_type(trace_name
, trace_type
);
571 ERR("ltt_trace_set_type failed");
575 result
= ltt_trace_alloc(trace_name
);
577 ERR("ltt_trace_alloc failed");
581 inform_consumer_daemon(trace_name
);
583 result
= ltt_trace_start(trace_name
);
585 ERR("ltt_trace_start failed");
593 result
= ltt_trace_setup(trace_name
);
595 ERR("ltt_trace_setup failed");
599 result
= ltt_trace_set_type(trace_name
, trace_type
);
601 ERR("ltt_trace_set_type failed");
609 result
= ltt_trace_alloc(trace_name
);
611 ERR("ltt_trace_alloc failed");
614 inform_consumer_daemon(trace_name
);
621 result
= ltt_trace_setup(trace_name
);
623 ERR("ltt_trace_setup failed");
627 result
= ltt_trace_set_type(trace_name
, trace_type
);
629 ERR("ltt_trace_set_type failed");
637 result
= ltt_trace_alloc(trace_name
);
639 ERR("ltt_trace_alloc failed");
643 inform_consumer_daemon(trace_name
);
646 result
= ltt_trace_start(trace_name
);
648 ERR("ltt_trace_start failed");
656 result
= ltt_trace_stop(trace_name
);
658 ERR("ltt_trace_stop failed");
664 DBG("trace destroy");
666 result
= ltt_trace_destroy(trace_name
, 0);
668 ERR("ltt_trace_destroy failed");
672 case FORCE_SUBBUF_SWITCH
:
673 /* FIXME: return codes? */
674 force_subbuf_switch();
685 static void process_channel_cmd(int sock
, int command
,
686 struct ustcomm_channel_info
*ch_inf
)
688 struct ustcomm_header _reply_header
;
689 struct ustcomm_header
*reply_header
= &_reply_header
;
690 struct ustcomm_channel_info
*reply_msg
=
691 (struct ustcomm_channel_info
*)send_buffer
;
692 char trace_name
[] = "auto";
693 int result
, offset
= 0, num
, size
;
695 memset(reply_header
, 0, sizeof(*reply_header
));
698 case GET_SUBBUF_NUM_SIZE
:
699 result
= get_subbuf_num_size(trace_name
,
703 reply_header
->result
= result
;
707 reply_msg
->channel
= USTCOMM_POISON_PTR
;
708 reply_msg
->subbuf_num
= num
;
709 reply_msg
->subbuf_size
= size
;
712 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
716 reply_header
->result
= set_subbuf_num(trace_name
,
721 case SET_SUBBUF_SIZE
:
722 reply_header
->result
= set_subbuf_size(trace_name
,
724 ch_inf
->subbuf_size
);
729 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
730 ERR("ustcomm_send failed");
734 static void process_buffer_cmd(int sock
, int command
,
735 struct ustcomm_buffer_info
*buf_inf
)
737 struct ustcomm_header _reply_header
;
738 struct ustcomm_header
*reply_header
= &_reply_header
;
739 struct ustcomm_buffer_info
*reply_msg
=
740 (struct ustcomm_buffer_info
*)send_buffer
;
741 char trace_name
[] = "auto";
742 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
745 memset(reply_header
, 0, sizeof(*reply_header
));
748 case GET_BUF_SHMID_PIPE_FD
:
749 result
= get_buffer_shmid_pipe_fd(trace_name
, buf_inf
->channel
,
755 reply_header
->result
= result
;
759 reply_msg
->channel
= USTCOMM_POISON_PTR
;
760 reply_msg
->buf_shmid
= buf_shmid
;
761 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
763 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
764 reply_header
->fd_included
= 1;
766 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
768 ERR("ustcomm_send failed");
772 case NOTIFY_BUF_MAPPED
:
773 reply_header
->result
=
774 notify_buffer_mapped(trace_name
,
779 result
= get_subbuffer(trace_name
, buf_inf
->channel
,
780 buf_inf
->ch_cpu
, &consumed_old
);
782 reply_header
->result
= result
;
786 reply_msg
->channel
= USTCOMM_POISON_PTR
;
787 reply_msg
->consumed_old
= consumed_old
;
789 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
793 result
= put_subbuffer(trace_name
, buf_inf
->channel
,
795 buf_inf
->consumed_old
);
796 reply_header
->result
= result
;
801 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
802 ERR("ustcomm_send failed");
807 static void process_marker_cmd(int sock
, int command
,
808 struct ustcomm_marker_info
*marker_inf
)
810 struct ustcomm_header _reply_header
;
811 struct ustcomm_header
*reply_header
= &_reply_header
;
814 memset(reply_header
, 0, sizeof(*reply_header
));
819 result
= ltt_marker_connect(marker_inf
->channel
,
823 WARN("could not enable marker; channel=%s,"
831 result
= ltt_marker_disconnect(marker_inf
->channel
,
835 WARN("could not disable marker; channel=%s,"
843 reply_header
->result
= result
;
845 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
846 ERR("ustcomm_send failed");
850 static void process_client_cmd(struct ustcomm_header
*recv_header
,
851 char *recv_buf
, int sock
)
854 struct ustcomm_header _reply_header
;
855 struct ustcomm_header
*reply_header
= &_reply_header
;
856 char *send_buf
= send_buffer
;
858 memset(reply_header
, 0, sizeof(*reply_header
));
859 memset(send_buf
, 0, sizeof(send_buffer
));
861 switch(recv_header
->command
) {
862 case GET_SUBBUF_NUM_SIZE
:
864 case SET_SUBBUF_SIZE
:
866 struct ustcomm_channel_info
*ch_inf
;
867 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
868 result
= ustcomm_unpack_channel_info(ch_inf
);
870 ERR("couldn't unpack channel info");
871 reply_header
->result
= -EINVAL
;
874 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
877 case GET_BUF_SHMID_PIPE_FD
:
878 case NOTIFY_BUF_MAPPED
:
882 struct ustcomm_buffer_info
*buf_inf
;
883 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
884 result
= ustcomm_unpack_buffer_info(buf_inf
);
886 ERR("couldn't unpack buffer info");
887 reply_header
->result
= -EINVAL
;
890 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
896 struct ustcomm_marker_info
*marker_inf
;
897 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
898 result
= ustcomm_unpack_marker_info(marker_inf
);
900 ERR("couldn't unpack marker info");
901 reply_header
->result
= -EINVAL
;
904 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
913 fp
= open_memstream(&ptr
, &size
);
915 ERR("opening memstream failed");
921 reply_header
->size
= size
;
923 result
= ustcomm_send(sock
, reply_header
, ptr
);
928 PERROR("failed to send markers list");
933 case LIST_TRACE_EVENTS
:
939 fp
= open_memstream(&ptr
, &size
);
941 ERR("opening memstream failed");
944 print_trace_events(fp
);
947 reply_header
->size
= size
;
949 result
= ustcomm_send(sock
, reply_header
, ptr
);
954 ERR("list_trace_events failed");
964 /* FIXME: No functionality at all... */
967 DBG("load_probe_lib loading %s", libfile
);
973 struct ustcomm_pidunique
*pid_msg
;
974 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
976 pid_msg
->pidunique
= pidunique
;
977 reply_header
->size
= sizeof(pid_msg
);
984 struct ustcomm_sock_path
*sock_msg
;
987 sock_msg
= (struct ustcomm_sock_path
*)send_buf
;
989 sock_path_env
= getenv("UST_DAEMON_SOCKET");
991 if (!sock_path_env
) {
992 result
= ustcomm_pack_sock_path(reply_header
,
997 result
= ustcomm_pack_sock_path(reply_header
,
1001 reply_header
->result
= result
;
1006 reply_header
->result
=
1007 process_simple_client_cmd(recv_header
->command
,
1016 ustcomm_send(sock
, reply_header
, send_buf
);
1019 #define MAX_EVENTS 10
1021 void *listener_main(void *p
)
1023 struct ustcomm_sock
*epoll_sock
;
1024 struct epoll_event events
[MAX_EVENTS
];
1025 struct sockaddr addr
;
1026 int accept_fd
, nfds
, result
, i
, addr_size
;
1030 pthread_cleanup_push(listener_cleanup
, NULL
);
1033 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1035 PERROR("listener_main: epoll_wait failed");
1039 for (i
= 0; i
< nfds
; i
++) {
1040 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1041 if (epoll_sock
== listen_sock
) {
1042 addr_size
= sizeof(struct sockaddr
);
1043 accept_fd
= accept(epoll_sock
->fd
,
1045 (socklen_t
*)&addr_size
);
1046 if (accept_fd
== -1) {
1047 PERROR("listener_main: accept failed");
1050 ustcomm_init_sock(accept_fd
, epoll_fd
,
1053 memset(receive_header
, 0,
1054 sizeof(*receive_header
));
1055 memset(receive_buffer
, 0,
1056 sizeof(receive_buffer
));
1057 result
= ustcomm_recv(epoll_sock
->fd
,
1061 ustcomm_del_sock(epoll_sock
, 0);
1063 process_client_cmd(receive_header
,
1071 pthread_cleanup_pop(1);
1074 /* These should only be accessed in the parent thread,
1077 static volatile sig_atomic_t have_listener
= 0;
1078 static pthread_t listener_thread
;
1080 void create_listener(void)
1083 sigset_t sig_all_blocked
;
1084 sigset_t orig_parent_mask
;
1086 if (have_listener
) {
1087 WARN("not creating listener because we already had one");
1091 /* A new thread created by pthread_create inherits the signal mask
1092 * from the parent. To avoid any signal being received by the
1093 * listener thread, we block all signals temporarily in the parent,
1094 * while we create the listener thread.
1097 sigfillset(&sig_all_blocked
);
1099 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1101 PERROR("pthread_sigmask: %s", strerror(result
));
1104 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1106 PERROR("pthread_create");
1109 /* Restore original signal mask in parent */
1110 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1112 PERROR("pthread_sigmask: %s", strerror(result
));
1118 #define AUTOPROBE_DISABLED 0
1119 #define AUTOPROBE_ENABLE_ALL 1
1120 #define AUTOPROBE_ENABLE_REGEX 2
1121 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1122 static regex_t autoprobe_regex
;
1124 static void auto_probe_connect(struct marker
*m
)
1128 char* concat_name
= NULL
;
1129 const char *probe_name
= "default";
1131 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1133 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1134 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1136 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1137 m
->channel
, m
->name
);
1140 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1147 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1148 if (result
&& result
!= -EEXIST
)
1149 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1151 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1155 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1159 struct ustcomm_sock
*sock
;
1161 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1163 ERR("string overflow allocating socket name, "
1164 "UST thread bailing");
1168 result
= ensure_dir_exists(SOCK_DIR
);
1170 ERR("Unable to create socket directory %s, UST thread bailing",
1175 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1177 ERR("Error initializing named socket (%s). Check that directory"
1178 "exists and that it is writable. UST thread bailing", name
);
1190 static void __attribute__((constructor
)) init()
1193 char* autoprobe_val
= NULL
;
1194 char* subbuffer_size_val
= NULL
;
1195 char* subbuffer_count_val
= NULL
;
1196 unsigned int subbuffer_size
;
1197 unsigned int subbuffer_count
;
1200 /* Assign the pidunique, to be able to differentiate the processes with same
1201 * pid, (before and after an exec).
1203 pidunique
= make_pidunique();
1205 DBG("Tracectl constructor");
1208 epoll_fd
= epoll_create(MAX_EVENTS
);
1209 if (epoll_fd
== -1) {
1210 ERR("epoll_create failed, tracing shutting down");
1214 /* Create the socket */
1215 listen_sock
= init_app_socket(epoll_fd
);
1217 ERR("failed to create application socket,"
1218 " tracing shutting down");
1224 autoprobe_val
= getenv("UST_AUTOPROBE");
1225 if (autoprobe_val
) {
1226 struct marker_iter iter
;
1228 DBG("Autoprobe enabled.");
1230 /* Ensure markers are initialized */
1233 /* Ensure marker control is initialized, for the probe */
1234 init_marker_control();
1236 /* first, set the callback that will connect the
1237 * probe on new markers
1239 if (autoprobe_val
[0] == '/') {
1240 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1244 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1245 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1246 /* don't crash the application just for this */
1248 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1251 /* just enable all instrumentation */
1252 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1255 marker_set_new_marker_cb(auto_probe_connect
);
1257 /* Now, connect the probes that were already registered. */
1258 marker_iter_reset(&iter
);
1259 marker_iter_start(&iter
);
1261 DBG("now iterating on markers already registered");
1262 while (iter
.marker
) {
1263 DBG("now iterating on marker %s", iter
.marker
->name
);
1264 auto_probe_connect(iter
.marker
);
1265 marker_iter_next(&iter
);
1269 if (getenv("UST_OVERWRITE")) {
1270 int val
= atoi(getenv("UST_OVERWRITE"));
1271 if (val
== 0 || val
== 1) {
1272 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1274 WARN("invalid value for UST_OVERWRITE");
1278 if (getenv("UST_AUTOCOLLECT")) {
1279 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1280 if (val
== 0 || val
== 1) {
1281 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1283 WARN("invalid value for UST_AUTOCOLLECT");
1287 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1288 if (subbuffer_size_val
) {
1289 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1290 power
= pow2_higher_or_eq(subbuffer_size
);
1291 if (power
!= subbuffer_size
)
1292 WARN("using the next power of two for buffer size = %u\n", power
);
1293 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1296 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1297 if (subbuffer_count_val
) {
1298 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1299 if (subbuffer_count
< 2)
1300 subbuffer_count
= 2;
1301 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1304 if (getenv("UST_TRACE")) {
1305 char trace_name
[] = "auto";
1306 char trace_type
[] = "ustrelay";
1308 DBG("starting early tracing");
1310 /* Ensure marker control is initialized */
1311 init_marker_control();
1313 /* Ensure markers are initialized */
1316 /* Ensure buffers are initialized, for the transport to be available.
1317 * We are about to set a trace type and it will fail without this.
1319 init_ustrelay_transport();
1321 /* FIXME: When starting early tracing (here), depending on the
1322 * order of constructors, it is very well possible some marker
1323 * sections are not yet registered. Because of this, some
1324 * channels may not be registered. Yet, we are about to ask the
1325 * daemon to collect the channels. Channels which are not yet
1326 * registered will not be collected.
1328 * Currently, in LTTng, there is no way to add a channel after
1329 * trace start. The reason for this is that it induces complex
1330 * concurrency issues on the trace structures, which can only
1331 * be resolved using RCU. This has not been done yet. As a
1332 * workaround, we are forcing the registration of the "ust"
1333 * channel here. This is the only channel (apart from metadata)
1334 * that can be reliably used in early tracing.
1336 * Non-early tracing does not have this problem and can use
1337 * arbitrary channel names.
1339 ltt_channels_register("ust");
1341 result
= ltt_trace_setup(trace_name
);
1343 ERR("ltt_trace_setup failed");
1347 result
= ltt_trace_set_type(trace_name
, trace_type
);
1349 ERR("ltt_trace_set_type failed");
1353 result
= ltt_trace_alloc(trace_name
);
1355 ERR("ltt_trace_alloc failed");
1359 result
= ltt_trace_start(trace_name
);
1361 ERR("ltt_trace_start failed");
1365 /* Do this after the trace is started in order to avoid creating confusion
1366 * if the trace fails to start. */
1367 inform_consumer_daemon(trace_name
);
1372 /* should decrementally destroy stuff if error */
1376 /* This is only called if we terminate normally, not with an unhandled signal,
1377 * so we cannot rely on it. However, for now, LTTV requires that the header of
1378 * the last sub-buffer contain a valid end time for the trace. This is done
1379 * automatically only when the trace is properly stopped.
1381 * If the traced program crashed, it is always possible to manually add the
1382 * right value in the header, or to open the trace in text mode.
1384 * FIXME: Fix LTTV so it doesn't need this.
1387 static void destroy_traces(void)
1391 /* if trace running, finish it */
1393 DBG("destructor stopping traces");
1395 result
= ltt_trace_stop("auto");
1397 ERR("ltt_trace_stop error");
1400 result
= ltt_trace_destroy("auto", 0);
1402 ERR("ltt_trace_destroy error");
1406 static int trace_recording(void)
1409 struct ust_trace
*trace
;
1413 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1414 if (trace
->active
) {
1420 ltt_unlock_traces();
1425 int restarting_usleep(useconds_t usecs
)
1431 tv
.tv_nsec
= usecs
* 1000;
1434 result
= nanosleep(&tv
, &tv
);
1435 } while (result
== -1 && errno
== EINTR
);
1440 static void stop_listener(void)
1447 result
= pthread_cancel(listener_thread
);
1449 ERR("pthread_cancel: %s", strerror(result
));
1451 result
= pthread_join(listener_thread
, NULL
);
1453 ERR("pthread_join: %s", strerror(result
));
1457 /* This destructor keeps the process alive for a few seconds in order
1458 * to leave time to ustd to connect to its buffers. This is necessary
1459 * for programs whose execution is very short. It is also useful in all
1460 * programs when tracing is started close to the end of the program
1463 * FIXME: For now, this only works for the first trace created in a
1467 static void __attribute__((destructor
)) keepalive()
1469 if (trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1471 DBG("Keeping process alive for consumer daemon...");
1472 while (LOAD_SHARED(buffers_to_export
)) {
1473 const int interv
= 200000;
1474 restarting_usleep(interv
);
1477 if (total
>= 3000000) {
1478 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1482 DBG("Finally dying...");
1487 /* Ask the listener to stop and clean up. */
1491 void ust_potential_exec(void)
1493 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1500 /* Notify ust that there was a fork. This needs to be called inside
1501 * the new process, anytime a process whose memory is not shared with
1502 * the parent is created. If this function is not called, the events
1503 * of the new process will not be collected.
1505 * Signals should be disabled before the fork and reenabled only after
1506 * this call in order to guarantee tracing is not started before ust_fork()
1507 * sanitizes the new process.
1510 static void ust_fork(void)
1512 struct ust_buffer
*buf
, *buf_tmp
;
1513 struct ustcomm_sock
*sock
, *sock_tmp
;
1516 /* FIXME: technically, the locks could have been taken before the fork */
1517 DBG("ust: forking");
1519 /* break lock if necessary */
1520 ltt_unlock_traces();
1522 ltt_trace_stop("auto");
1523 ltt_trace_destroy("auto", 1);
1524 /* Delete all active connections, but leave them in the epoll set */
1525 list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1526 ustcomm_del_sock(sock
, 1);
1529 /* Delete all blocked consumers */
1530 list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1531 open_buffers_list
) {
1532 result
= close(buf
->data_ready_fd_read
);
1536 result
= close(buf
->data_ready_fd_write
);
1540 list_del(&buf
->open_buffers_list
);
1543 /* Clean up the listener socket and epoll, keeping the scoket file */
1544 ustcomm_del_named_sock(listen_sock
, 1);
1547 /* Re-start the launch sequence */
1548 STORE_SHARED(buffers_to_export
, 0);
1552 epoll_fd
= epoll_create(MAX_EVENTS
);
1553 if (epoll_fd
== -1) {
1554 ERR("epoll_create failed, tracing shutting down");
1558 /* Create the socket */
1559 listen_sock
= init_app_socket(epoll_fd
);
1561 ERR("failed to create application socket,"
1562 " tracing shutting down");
1566 ltt_trace_setup("auto");
1567 result
= ltt_trace_set_type("auto", "ustrelay");
1569 ERR("ltt_trace_set_type failed");
1573 ltt_trace_alloc("auto");
1574 ltt_trace_start("auto");
1575 inform_consumer_daemon("auto");
1578 void ust_before_fork(ust_fork_info_t
*fork_info
)
1580 /* Disable signals. This is to avoid that the child
1581 * intervenes before it is properly setup for tracing. It is
1582 * safer to disable all signals, because then we know we are not
1583 * breaking anything by restoring the original mask.
1589 - only do this if tracing is active
1592 /* Disable signals */
1593 sigfillset(&all_sigs
);
1594 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1596 PERROR("sigprocmask");
1601 /* Don't call this function directly in a traced program */
1602 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1606 /* Restore signals */
1607 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1609 PERROR("sigprocmask");
1614 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1616 /* Reenable signals */
1617 ust_after_fork_common(fork_info
);
1620 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1622 /* First sanitize the child */
1625 /* Then reenable interrupts */
1626 ust_after_fork_common(fork_info
);