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 list_head open_buffers_list
= LIST_HEAD_INIT(open_buffers_list
);
71 static struct list_head ust_socks
= 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
,
155 struct ustcomm_header send_header
, recv_header
;
156 struct ustcomm_buffer_info buf_inf
;
159 result
= ustcomm_pack_buffer_info(&send_header
,
165 ERR("failed to pack buffer info message %s_%d",
170 buf_inf
.pid
= getpid();
171 send_header
.command
= CONSUME_BUFFER
;
173 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
176 PERROR("request for buffer consumer failed, is the daemon online?");
182 /* Ask the daemon to collect a trace called trace_name and being
183 * produced by this pid.
185 * The trace must be at least allocated. (It can also be started.)
186 * This is because _ltt_trace_find is used.
189 static void inform_consumer_daemon(const char *trace_name
)
192 struct ust_trace
*trace
;
195 sock
= connect_ustd();
200 DBG("Connected to ustd");
204 trace
= _ltt_trace_find(trace_name
);
206 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
210 for (i
=0; i
< trace
->nr_channels
; i
++) {
211 if (trace
->channels
[i
].request_collection
) {
212 /* iterate on all cpus */
213 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
214 ch_name
= trace
->channels
[i
].channel_name
;
215 request_buffer_consumer(sock
, ch_name
, j
);
216 STORE_SHARED(buffers_to_export
,
217 LOAD_SHARED(buffers_to_export
)+1);
228 static struct ust_channel
*find_channel(const char *ch_name
,
229 struct ust_trace
*trace
)
233 for (i
=0; i
<trace
->nr_channels
; i
++) {
234 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
235 return &trace
->channels
[i
];
242 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
245 int *buf_struct_shmid
,
248 struct ust_trace
*trace
;
249 struct ust_channel
*channel
;
250 struct ust_buffer
*buf
;
252 DBG("get_buffer_shmid_pipe_fd");
255 trace
= _ltt_trace_find(trace_name
);
259 ERR("cannot find trace!");
263 channel
= find_channel(ch_name
, trace
);
265 ERR("cannot find channel %s!", ch_name
);
269 buf
= channel
->buf
[ch_cpu
];
271 *buf_shmid
= buf
->shmid
;
272 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
273 *buf_pipe_fd
= buf
->data_ready_fd_read
;
278 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
281 struct ust_trace
*trace
;
282 struct ust_channel
*channel
;
284 DBG("get_subbuf_size");
287 trace
= _ltt_trace_find(trace_name
);
291 ERR("cannot find trace!");
295 channel
= find_channel(ch_name
, trace
);
297 ERR("unable to find channel");
301 *num
= channel
->subbuf_cnt
;
302 *size
= channel
->subbuf_size
;
307 /* Return the power of two which is equal or higher to v */
309 static unsigned int pow2_higher_or_eq(unsigned int v
)
312 int retval
= 1<<(hb
-1);
320 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
325 struct ust_trace
*trace
;
326 struct ust_channel
*channel
;
328 DBG("set_subbuf_size");
330 power
= pow2_higher_or_eq(size
);
331 power
= max_t(unsigned int, 2u, power
);
333 WARN("using the next power of two for buffer size = %u\n", power
);
337 trace
= _ltt_trace_find_setup(trace_name
);
339 ERR("cannot find trace!");
344 channel
= find_channel(ch_name
, trace
);
346 ERR("unable to find channel");
351 channel
->subbuf_size
= power
;
352 DBG("the set_subbuf_size for the requested channel is %u", channel
->subbuf_size
);
360 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
363 struct ust_trace
*trace
;
364 struct ust_channel
*channel
;
367 DBG("set_subbuf_num");
370 ERR("subbuffer count should be greater than 2");
375 trace
= _ltt_trace_find_setup(trace_name
);
377 ERR("cannot find trace!");
382 channel
= find_channel(ch_name
, trace
);
384 ERR("unable to find channel");
389 channel
->subbuf_cnt
= num
;
390 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
397 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
398 int ch_cpu
, long *consumed_old
)
401 struct ust_trace
*trace
;
402 struct ust_channel
*channel
;
403 struct ust_buffer
*buf
;
410 trace
= _ltt_trace_find(trace_name
);
413 DBG("Cannot find trace. It was likely destroyed by the user.");
418 channel
= find_channel(ch_name
, trace
);
420 ERR("unable to find channel");
425 buf
= channel
->buf
[ch_cpu
];
427 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
429 WARN("missed buffer?");
439 static int notify_buffer_mapped(const char *trace_name
,
444 struct ust_trace
*trace
;
445 struct ust_channel
*channel
;
446 struct ust_buffer
*buf
;
448 DBG("get_buffer_fd");
451 trace
= _ltt_trace_find(trace_name
);
455 DBG("Cannot find trace. It was likely destroyed by the user.");
459 channel
= find_channel(ch_name
, trace
);
462 ERR("unable to find channel");
466 buf
= channel
->buf
[ch_cpu
];
468 /* Being here is the proof the daemon has mapped the buffer in its
469 * memory. We may now decrement buffers_to_export.
471 if (uatomic_read(&buf
->consumed
) == 0) {
472 DBG("decrementing buffers_to_export");
473 STORE_SHARED(buffers_to_export
, LOAD_SHARED(buffers_to_export
)-1);
476 /* The buffer has been exported, ergo, we can add it to the
477 * list of open buffers
479 list_add(&buf
->open_buffers_list
, &open_buffers_list
);
487 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
488 int ch_cpu
, long consumed_old
)
491 struct ust_trace
*trace
;
492 struct ust_channel
*channel
;
493 struct ust_buffer
*buf
;
498 trace
= _ltt_trace_find(trace_name
);
502 DBG("Cannot find trace. It was likely destroyed by the user.");
506 channel
= find_channel(ch_name
, trace
);
509 ERR("unable to find channel");
513 buf
= channel
->buf
[ch_cpu
];
515 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
517 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
520 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
530 static void listener_cleanup(void *ptr
)
532 ustcomm_del_named_sock(listen_sock
, 0);
535 static void force_subbuf_switch()
537 struct ust_buffer
*buf
;
539 list_for_each_entry(buf
, &open_buffers_list
,
541 ltt_force_switch(buf
, FORCE_FLUSH
);
545 /* Simple commands are those which need only respond with a return value. */
546 static int process_simple_client_cmd(int command
, char *recv_buf
)
549 char trace_type
[] = "ustrelay";
550 char trace_name
[] = "auto";
555 struct ustcomm_sock_path
*sock_msg
;
556 sock_msg
= (struct ustcomm_sock_path
*)recv_buf
;
557 sock_msg
->sock_path
=
558 ustcomm_restore_ptr(sock_msg
->sock_path
,
560 sizeof(sock_msg
->data
));
561 if (!sock_msg
->sock_path
) {
565 return setenv("UST_DAEMON_SOCKET", sock_msg
->sock_path
, 1);
568 /* start is an operation that setups the trace, allocates it and starts it */
569 result
= ltt_trace_setup(trace_name
);
571 ERR("ltt_trace_setup failed");
575 result
= ltt_trace_set_type(trace_name
, trace_type
);
577 ERR("ltt_trace_set_type failed");
581 result
= ltt_trace_alloc(trace_name
);
583 ERR("ltt_trace_alloc failed");
587 inform_consumer_daemon(trace_name
);
589 result
= ltt_trace_start(trace_name
);
591 ERR("ltt_trace_start failed");
599 result
= ltt_trace_setup(trace_name
);
601 ERR("ltt_trace_setup failed");
605 result
= ltt_trace_set_type(trace_name
, trace_type
);
607 ERR("ltt_trace_set_type failed");
615 result
= ltt_trace_alloc(trace_name
);
617 ERR("ltt_trace_alloc failed");
620 inform_consumer_daemon(trace_name
);
627 result
= ltt_trace_setup(trace_name
);
629 ERR("ltt_trace_setup failed");
633 result
= ltt_trace_set_type(trace_name
, trace_type
);
635 ERR("ltt_trace_set_type failed");
643 result
= ltt_trace_alloc(trace_name
);
645 ERR("ltt_trace_alloc failed");
649 inform_consumer_daemon(trace_name
);
652 result
= ltt_trace_start(trace_name
);
654 ERR("ltt_trace_start failed");
662 result
= ltt_trace_stop(trace_name
);
664 ERR("ltt_trace_stop failed");
670 DBG("trace destroy");
672 result
= ltt_trace_destroy(trace_name
, 0);
674 ERR("ltt_trace_destroy failed");
678 case FORCE_SUBBUF_SWITCH
:
679 /* FIXME: return codes? */
680 force_subbuf_switch();
691 static void process_channel_cmd(int sock
, int command
,
692 struct ustcomm_channel_info
*ch_inf
)
694 struct ustcomm_header _reply_header
;
695 struct ustcomm_header
*reply_header
= &_reply_header
;
696 struct ustcomm_channel_info
*reply_msg
=
697 (struct ustcomm_channel_info
*)send_buffer
;
698 char trace_name
[] = "auto";
699 int result
, offset
= 0, num
, size
;
701 memset(reply_header
, 0, sizeof(*reply_header
));
704 case GET_SUBBUF_NUM_SIZE
:
705 result
= get_subbuf_num_size(trace_name
,
709 reply_header
->result
= result
;
713 reply_msg
->channel
= USTCOMM_POISON_PTR
;
714 reply_msg
->subbuf_num
= num
;
715 reply_msg
->subbuf_size
= size
;
718 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
722 reply_header
->result
= set_subbuf_num(trace_name
,
727 case SET_SUBBUF_SIZE
:
728 reply_header
->result
= set_subbuf_size(trace_name
,
730 ch_inf
->subbuf_size
);
735 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
736 ERR("ustcomm_send failed");
740 static void process_buffer_cmd(int sock
, int command
,
741 struct ustcomm_buffer_info
*buf_inf
)
743 struct ustcomm_header _reply_header
;
744 struct ustcomm_header
*reply_header
= &_reply_header
;
745 struct ustcomm_buffer_info
*reply_msg
=
746 (struct ustcomm_buffer_info
*)send_buffer
;
747 char trace_name
[] = "auto";
748 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
751 memset(reply_header
, 0, sizeof(*reply_header
));
754 case GET_BUF_SHMID_PIPE_FD
:
755 result
= get_buffer_shmid_pipe_fd(trace_name
, buf_inf
->channel
,
761 reply_header
->result
= result
;
765 reply_msg
->channel
= USTCOMM_POISON_PTR
;
766 reply_msg
->buf_shmid
= buf_shmid
;
767 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
769 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
770 reply_header
->fd_included
= 1;
772 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
774 ERR("ustcomm_send failed");
778 case NOTIFY_BUF_MAPPED
:
779 reply_header
->result
=
780 notify_buffer_mapped(trace_name
,
785 result
= get_subbuffer(trace_name
, buf_inf
->channel
,
786 buf_inf
->ch_cpu
, &consumed_old
);
788 reply_header
->result
= result
;
792 reply_msg
->channel
= USTCOMM_POISON_PTR
;
793 reply_msg
->consumed_old
= consumed_old
;
795 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
799 result
= put_subbuffer(trace_name
, buf_inf
->channel
,
801 buf_inf
->consumed_old
);
802 reply_header
->result
= result
;
807 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
808 ERR("ustcomm_send failed");
813 static void process_marker_cmd(int sock
, int command
,
814 struct ustcomm_marker_info
*marker_inf
)
816 struct ustcomm_header _reply_header
;
817 struct ustcomm_header
*reply_header
= &_reply_header
;
820 memset(reply_header
, 0, sizeof(*reply_header
));
825 result
= ltt_marker_connect(marker_inf
->channel
,
829 WARN("could not enable marker; channel=%s,"
837 result
= ltt_marker_disconnect(marker_inf
->channel
,
841 WARN("could not disable marker; channel=%s,"
849 reply_header
->result
= result
;
851 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
852 ERR("ustcomm_send failed");
856 static void process_client_cmd(struct ustcomm_header
*recv_header
,
857 char *recv_buf
, int sock
)
860 struct ustcomm_header _reply_header
;
861 struct ustcomm_header
*reply_header
= &_reply_header
;
862 char *send_buf
= send_buffer
;
864 memset(reply_header
, 0, sizeof(*reply_header
));
865 memset(send_buf
, 0, sizeof(send_buffer
));
867 switch(recv_header
->command
) {
868 case GET_SUBBUF_NUM_SIZE
:
870 case SET_SUBBUF_SIZE
:
872 struct ustcomm_channel_info
*ch_inf
;
873 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
874 result
= ustcomm_unpack_channel_info(ch_inf
);
876 ERR("couldn't unpack channel info");
877 reply_header
->result
= -EINVAL
;
880 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
883 case GET_BUF_SHMID_PIPE_FD
:
884 case NOTIFY_BUF_MAPPED
:
888 struct ustcomm_buffer_info
*buf_inf
;
889 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
890 result
= ustcomm_unpack_buffer_info(buf_inf
);
892 ERR("couldn't unpack buffer info");
893 reply_header
->result
= -EINVAL
;
896 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
902 struct ustcomm_marker_info
*marker_inf
;
903 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
904 result
= ustcomm_unpack_marker_info(marker_inf
);
906 ERR("couldn't unpack marker info");
907 reply_header
->result
= -EINVAL
;
910 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
919 fp
= open_memstream(&ptr
, &size
);
921 ERR("opening memstream failed");
927 reply_header
->size
= size
;
929 result
= ustcomm_send(sock
, reply_header
, ptr
);
934 PERROR("failed to send markers list");
939 case LIST_TRACE_EVENTS
:
945 fp
= open_memstream(&ptr
, &size
);
947 ERR("opening memstream failed");
950 print_trace_events(fp
);
953 reply_header
->size
= size
;
955 result
= ustcomm_send(sock
, reply_header
, ptr
);
960 ERR("list_trace_events failed");
970 /* FIXME: No functionality at all... */
973 DBG("load_probe_lib loading %s", libfile
);
979 struct ustcomm_pidunique
*pid_msg
;
980 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
982 pid_msg
->pidunique
= pidunique
;
983 reply_header
->size
= sizeof(pid_msg
);
990 struct ustcomm_sock_path
*sock_msg
;
993 sock_msg
= (struct ustcomm_sock_path
*)send_buf
;
995 sock_path_env
= getenv("UST_DAEMON_SOCKET");
997 if (!sock_path_env
) {
998 result
= ustcomm_pack_sock_path(reply_header
,
1003 result
= ustcomm_pack_sock_path(reply_header
,
1007 reply_header
->result
= result
;
1012 reply_header
->result
=
1013 process_simple_client_cmd(recv_header
->command
,
1022 ustcomm_send(sock
, reply_header
, send_buf
);
1025 #define MAX_EVENTS 10
1027 void *listener_main(void *p
)
1029 struct ustcomm_sock
*epoll_sock
;
1030 struct epoll_event events
[MAX_EVENTS
];
1031 struct sockaddr addr
;
1032 int accept_fd
, nfds
, result
, i
, addr_size
;
1036 pthread_cleanup_push(listener_cleanup
, NULL
);
1039 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1041 PERROR("listener_main: epoll_wait failed");
1045 for (i
= 0; i
< nfds
; i
++) {
1046 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1047 if (epoll_sock
== listen_sock
) {
1048 addr_size
= sizeof(struct sockaddr
);
1049 accept_fd
= accept(epoll_sock
->fd
,
1051 (socklen_t
*)&addr_size
);
1052 if (accept_fd
== -1) {
1053 PERROR("listener_main: accept failed");
1056 ustcomm_init_sock(accept_fd
, epoll_fd
,
1059 memset(receive_header
, 0,
1060 sizeof(*receive_header
));
1061 memset(receive_buffer
, 0,
1062 sizeof(receive_buffer
));
1063 result
= ustcomm_recv(epoll_sock
->fd
,
1067 ustcomm_del_sock(epoll_sock
, 0);
1069 process_client_cmd(receive_header
,
1077 pthread_cleanup_pop(1);
1080 /* These should only be accessed in the parent thread,
1083 static volatile sig_atomic_t have_listener
= 0;
1084 static pthread_t listener_thread
;
1086 void create_listener(void)
1089 sigset_t sig_all_blocked
;
1090 sigset_t orig_parent_mask
;
1092 if (have_listener
) {
1093 WARN("not creating listener because we already had one");
1097 /* A new thread created by pthread_create inherits the signal mask
1098 * from the parent. To avoid any signal being received by the
1099 * listener thread, we block all signals temporarily in the parent,
1100 * while we create the listener thread.
1103 sigfillset(&sig_all_blocked
);
1105 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1107 PERROR("pthread_sigmask: %s", strerror(result
));
1110 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1112 PERROR("pthread_create");
1115 /* Restore original signal mask in parent */
1116 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1118 PERROR("pthread_sigmask: %s", strerror(result
));
1124 #define AUTOPROBE_DISABLED 0
1125 #define AUTOPROBE_ENABLE_ALL 1
1126 #define AUTOPROBE_ENABLE_REGEX 2
1127 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1128 static regex_t autoprobe_regex
;
1130 static void auto_probe_connect(struct marker
*m
)
1134 char* concat_name
= NULL
;
1135 const char *probe_name
= "default";
1137 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1139 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1140 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1142 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1143 m
->channel
, m
->name
);
1146 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1153 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1154 if (result
&& result
!= -EEXIST
)
1155 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1157 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1161 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1165 struct ustcomm_sock
*sock
;
1167 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1169 ERR("string overflow allocating socket name, "
1170 "UST thread bailing");
1174 result
= ensure_dir_exists(SOCK_DIR
);
1176 ERR("Unable to create socket directory %s, UST thread bailing",
1181 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1183 ERR("Error initializing named socket (%s). Check that directory"
1184 "exists and that it is writable. UST thread bailing", name
);
1196 static void __attribute__((constructor
)) init()
1199 char* autoprobe_val
= NULL
;
1200 char* subbuffer_size_val
= NULL
;
1201 char* subbuffer_count_val
= NULL
;
1202 unsigned int subbuffer_size
;
1203 unsigned int subbuffer_count
;
1206 /* Assign the pidunique, to be able to differentiate the processes with same
1207 * pid, (before and after an exec).
1209 pidunique
= make_pidunique();
1210 processpid
= getpid();
1212 DBG("Tracectl constructor");
1215 epoll_fd
= epoll_create(MAX_EVENTS
);
1216 if (epoll_fd
== -1) {
1217 ERR("epoll_create failed, tracing shutting down");
1221 /* Create the socket */
1222 listen_sock
= init_app_socket(epoll_fd
);
1224 ERR("failed to create application socket,"
1225 " tracing shutting down");
1231 autoprobe_val
= getenv("UST_AUTOPROBE");
1232 if (autoprobe_val
) {
1233 struct marker_iter iter
;
1235 DBG("Autoprobe enabled.");
1237 /* Ensure markers are initialized */
1240 /* Ensure marker control is initialized, for the probe */
1241 init_marker_control();
1243 /* first, set the callback that will connect the
1244 * probe on new markers
1246 if (autoprobe_val
[0] == '/') {
1247 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1251 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1252 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1253 /* don't crash the application just for this */
1255 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1258 /* just enable all instrumentation */
1259 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1262 marker_set_new_marker_cb(auto_probe_connect
);
1264 /* Now, connect the probes that were already registered. */
1265 marker_iter_reset(&iter
);
1266 marker_iter_start(&iter
);
1268 DBG("now iterating on markers already registered");
1269 while (iter
.marker
) {
1270 DBG("now iterating on marker %s", iter
.marker
->name
);
1271 auto_probe_connect(iter
.marker
);
1272 marker_iter_next(&iter
);
1276 if (getenv("UST_OVERWRITE")) {
1277 int val
= atoi(getenv("UST_OVERWRITE"));
1278 if (val
== 0 || val
== 1) {
1279 STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1281 WARN("invalid value for UST_OVERWRITE");
1285 if (getenv("UST_AUTOCOLLECT")) {
1286 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1287 if (val
== 0 || val
== 1) {
1288 STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1290 WARN("invalid value for UST_AUTOCOLLECT");
1294 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1295 if (subbuffer_size_val
) {
1296 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1297 power
= pow2_higher_or_eq(subbuffer_size
);
1298 if (power
!= subbuffer_size
)
1299 WARN("using the next power of two for buffer size = %u\n", power
);
1300 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1303 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1304 if (subbuffer_count_val
) {
1305 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1306 if (subbuffer_count
< 2)
1307 subbuffer_count
= 2;
1308 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1311 if (getenv("UST_TRACE")) {
1312 char trace_name
[] = "auto";
1313 char trace_type
[] = "ustrelay";
1315 DBG("starting early tracing");
1317 /* Ensure marker control is initialized */
1318 init_marker_control();
1320 /* Ensure markers are initialized */
1323 /* Ensure buffers are initialized, for the transport to be available.
1324 * We are about to set a trace type and it will fail without this.
1326 init_ustrelay_transport();
1328 /* FIXME: When starting early tracing (here), depending on the
1329 * order of constructors, it is very well possible some marker
1330 * sections are not yet registered. Because of this, some
1331 * channels may not be registered. Yet, we are about to ask the
1332 * daemon to collect the channels. Channels which are not yet
1333 * registered will not be collected.
1335 * Currently, in LTTng, there is no way to add a channel after
1336 * trace start. The reason for this is that it induces complex
1337 * concurrency issues on the trace structures, which can only
1338 * be resolved using RCU. This has not been done yet. As a
1339 * workaround, we are forcing the registration of the "ust"
1340 * channel here. This is the only channel (apart from metadata)
1341 * that can be reliably used in early tracing.
1343 * Non-early tracing does not have this problem and can use
1344 * arbitrary channel names.
1346 ltt_channels_register("ust");
1348 result
= ltt_trace_setup(trace_name
);
1350 ERR("ltt_trace_setup failed");
1354 result
= ltt_trace_set_type(trace_name
, trace_type
);
1356 ERR("ltt_trace_set_type failed");
1360 result
= ltt_trace_alloc(trace_name
);
1362 ERR("ltt_trace_alloc failed");
1366 result
= ltt_trace_start(trace_name
);
1368 ERR("ltt_trace_start failed");
1372 /* Do this after the trace is started in order to avoid creating confusion
1373 * if the trace fails to start. */
1374 inform_consumer_daemon(trace_name
);
1379 /* should decrementally destroy stuff if error */
1383 /* This is only called if we terminate normally, not with an unhandled signal,
1384 * so we cannot rely on it. However, for now, LTTV requires that the header of
1385 * the last sub-buffer contain a valid end time for the trace. This is done
1386 * automatically only when the trace is properly stopped.
1388 * If the traced program crashed, it is always possible to manually add the
1389 * right value in the header, or to open the trace in text mode.
1391 * FIXME: Fix LTTV so it doesn't need this.
1394 static void destroy_traces(void)
1398 /* if trace running, finish it */
1400 DBG("destructor stopping traces");
1402 result
= ltt_trace_stop("auto");
1404 ERR("ltt_trace_stop error");
1407 result
= ltt_trace_destroy("auto", 0);
1409 ERR("ltt_trace_destroy error");
1413 static int trace_recording(void)
1416 struct ust_trace
*trace
;
1420 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1421 if (trace
->active
) {
1427 ltt_unlock_traces();
1432 int restarting_usleep(useconds_t usecs
)
1438 tv
.tv_nsec
= usecs
* 1000;
1441 result
= nanosleep(&tv
, &tv
);
1442 } while (result
== -1 && errno
== EINTR
);
1447 static void stop_listener(void)
1454 result
= pthread_cancel(listener_thread
);
1456 ERR("pthread_cancel: %s", strerror(result
));
1458 result
= pthread_join(listener_thread
, NULL
);
1460 ERR("pthread_join: %s", strerror(result
));
1464 /* This destructor keeps the process alive for a few seconds in order
1465 * to leave time to ustd to connect to its buffers. This is necessary
1466 * for programs whose execution is very short. It is also useful in all
1467 * programs when tracing is started close to the end of the program
1470 * FIXME: For now, this only works for the first trace created in a
1474 static void __attribute__((destructor
)) keepalive()
1476 if (processpid
!= getpid()) {
1480 if (trace_recording() && LOAD_SHARED(buffers_to_export
)) {
1482 DBG("Keeping process alive for consumer daemon...");
1483 while (LOAD_SHARED(buffers_to_export
)) {
1484 const int interv
= 200000;
1485 restarting_usleep(interv
);
1488 if (total
>= 3000000) {
1489 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1493 DBG("Finally dying...");
1498 /* Ask the listener to stop and clean up. */
1502 void ust_potential_exec(void)
1504 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1511 /* Notify ust that there was a fork. This needs to be called inside
1512 * the new process, anytime a process whose memory is not shared with
1513 * the parent is created. If this function is not called, the events
1514 * of the new process will not be collected.
1516 * Signals should be disabled before the fork and reenabled only after
1517 * this call in order to guarantee tracing is not started before ust_fork()
1518 * sanitizes the new process.
1521 static void ust_fork(void)
1523 struct ust_buffer
*buf
, *buf_tmp
;
1524 struct ustcomm_sock
*sock
, *sock_tmp
;
1527 /* FIXME: technically, the locks could have been taken before the fork */
1528 DBG("ust: forking");
1530 /* Get the pid of the new process */
1531 processpid
= getpid();
1533 /* break lock if necessary */
1534 ltt_unlock_traces();
1536 ltt_trace_stop("auto");
1537 ltt_trace_destroy("auto", 1);
1538 /* Delete all active connections, but leave them in the epoll set */
1539 list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1540 ustcomm_del_sock(sock
, 1);
1543 /* Delete all blocked consumers */
1544 list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1545 open_buffers_list
) {
1546 result
= close(buf
->data_ready_fd_read
);
1550 result
= close(buf
->data_ready_fd_write
);
1554 list_del(&buf
->open_buffers_list
);
1557 /* Clean up the listener socket and epoll, keeping the scoket file */
1558 ustcomm_del_named_sock(listen_sock
, 1);
1561 /* Re-start the launch sequence */
1562 STORE_SHARED(buffers_to_export
, 0);
1566 epoll_fd
= epoll_create(MAX_EVENTS
);
1567 if (epoll_fd
== -1) {
1568 ERR("epoll_create failed, tracing shutting down");
1572 /* Create the socket */
1573 listen_sock
= init_app_socket(epoll_fd
);
1575 ERR("failed to create application socket,"
1576 " tracing shutting down");
1580 ltt_trace_setup("auto");
1581 result
= ltt_trace_set_type("auto", "ustrelay");
1583 ERR("ltt_trace_set_type failed");
1587 ltt_trace_alloc("auto");
1588 ltt_trace_start("auto");
1589 inform_consumer_daemon("auto");
1592 void ust_before_fork(ust_fork_info_t
*fork_info
)
1594 /* Disable signals. This is to avoid that the child
1595 * intervenes before it is properly setup for tracing. It is
1596 * safer to disable all signals, because then we know we are not
1597 * breaking anything by restoring the original mask.
1603 - only do this if tracing is active
1606 /* Disable signals */
1607 sigfillset(&all_sigs
);
1608 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1610 PERROR("sigprocmask");
1615 /* Don't call this function directly in a traced program */
1616 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1620 /* Restore signals */
1621 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1623 PERROR("sigprocmask");
1628 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1630 /* Reenable signals */
1631 ust_after_fork_common(fork_info
);
1634 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1636 /* First sanitize the child */
1639 /* Then reenable interrupts */
1640 ust_after_fork_common(fork_info
);