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;
79 static long long make_pidunique(void)
84 gettimeofday(&tv
, NULL
);
93 static void print_markers(FILE *fp
)
95 struct marker_iter iter
;
98 marker_iter_reset(&iter
);
99 marker_iter_start(&iter
);
101 while (iter
.marker
) {
102 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n",
103 (*iter
.marker
)->channel
,
104 (*iter
.marker
)->name
,
105 (int)imv_read((*iter
.marker
)->state
),
106 (*iter
.marker
)->format
,
107 (*iter
.marker
)->location
);
108 marker_iter_next(&iter
);
113 static void print_trace_events(FILE *fp
)
115 struct trace_event_iter iter
;
118 trace_event_iter_reset(&iter
);
119 trace_event_iter_start(&iter
);
121 while (iter
.trace_event
) {
122 fprintf(fp
, "trace_event: %s\n", (*iter
.trace_event
)->name
);
123 trace_event_iter_next(&iter
);
125 unlock_trace_events();
128 static int connect_ustconsumer(void)
131 char default_daemon_path
[] = SOCK_DIR
"/ustconsumer";
132 char *explicit_daemon_path
, *daemon_path
;
134 explicit_daemon_path
= getenv("UST_DAEMON_SOCKET");
135 if (explicit_daemon_path
) {
136 daemon_path
= explicit_daemon_path
;
138 daemon_path
= default_daemon_path
;
141 DBG("Connecting to daemon_path %s", daemon_path
);
143 result
= ustcomm_connect_path(daemon_path
, &fd
);
145 WARN("connect_ustconsumer failed, daemon_path: %s",
154 static void request_buffer_consumer(int sock
,
159 struct ustcomm_header send_header
, recv_header
;
160 struct ustcomm_buffer_info buf_inf
;
163 result
= ustcomm_pack_buffer_info(&send_header
,
170 ERR("failed to pack buffer info message %s_%d",
175 buf_inf
.pid
= getpid();
176 send_header
.command
= CONSUME_BUFFER
;
178 result
= ustcomm_req(sock
, &send_header
, (char *) &buf_inf
,
181 PERROR("request for buffer consumer failed, is the daemon online?");
187 /* Ask the daemon to collect a trace called trace_name and being
188 * produced by this pid.
190 * The trace must be at least allocated. (It can also be started.)
191 * This is because _ltt_trace_find is used.
194 static void inform_consumer_daemon(const char *trace_name
)
197 struct ust_trace
*trace
;
200 sock
= connect_ustconsumer();
205 DBG("Connected to ustconsumer");
209 trace
= _ltt_trace_find(trace_name
);
211 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
215 for (i
=0; i
< trace
->nr_channels
; i
++) {
216 if (trace
->channels
[i
].request_collection
) {
217 /* iterate on all cpus */
218 for (j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
219 ch_name
= trace
->channels
[i
].channel_name
;
220 request_buffer_consumer(sock
, trace_name
,
222 CMM_STORE_SHARED(buffers_to_export
,
223 CMM_LOAD_SHARED(buffers_to_export
)+1);
234 static struct ust_channel
*find_channel(const char *ch_name
,
235 struct ust_trace
*trace
)
239 for (i
=0; i
<trace
->nr_channels
; i
++) {
240 if (!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
241 return &trace
->channels
[i
];
248 static int get_buffer_shmid_pipe_fd(const char *trace_name
, const char *ch_name
,
251 int *buf_struct_shmid
,
254 struct ust_trace
*trace
;
255 struct ust_channel
*channel
;
256 struct ust_buffer
*buf
;
258 DBG("get_buffer_shmid_pipe_fd");
261 trace
= _ltt_trace_find(trace_name
);
265 ERR("cannot find trace!");
269 channel
= find_channel(ch_name
, trace
);
271 ERR("cannot find channel %s!", ch_name
);
275 buf
= channel
->buf
[ch_cpu
];
277 *buf_shmid
= buf
->shmid
;
278 *buf_struct_shmid
= channel
->buf_struct_shmids
[ch_cpu
];
279 *buf_pipe_fd
= buf
->data_ready_fd_read
;
284 static int get_subbuf_num_size(const char *trace_name
, const char *ch_name
,
287 struct ust_trace
*trace
;
288 struct ust_channel
*channel
;
290 DBG("get_subbuf_size");
293 trace
= _ltt_trace_find(trace_name
);
297 ERR("cannot find trace!");
301 channel
= find_channel(ch_name
, trace
);
303 ERR("unable to find channel");
307 *num
= channel
->subbuf_cnt
;
308 *size
= channel
->subbuf_size
;
313 /* Return the power of two which is equal or higher to v */
315 static unsigned int pow2_higher_or_eq(unsigned int v
)
318 int retval
= 1<<(hb
-1);
326 static int set_subbuf_size(const char *trace_name
, const char *ch_name
,
331 struct ust_trace
*trace
;
332 struct ust_channel
*channel
;
334 DBG("set_subbuf_size");
336 power
= pow2_higher_or_eq(size
);
337 power
= max_t(unsigned int, 2u, power
);
339 WARN("using the next power of two for buffer size = %u\n", power
);
343 trace
= _ltt_trace_find_setup(trace_name
);
345 ERR("cannot find trace!");
350 channel
= find_channel(ch_name
, trace
);
352 ERR("unable to find channel");
357 channel
->subbuf_size
= power
;
358 DBG("the set_subbuf_size for the requested channel is %zu", channel
->subbuf_size
);
366 static int set_subbuf_num(const char *trace_name
, const char *ch_name
,
369 struct ust_trace
*trace
;
370 struct ust_channel
*channel
;
373 DBG("set_subbuf_num");
376 ERR("subbuffer count should be greater than 2");
381 trace
= _ltt_trace_find_setup(trace_name
);
383 ERR("cannot find trace!");
388 channel
= find_channel(ch_name
, trace
);
390 ERR("unable to find channel");
395 channel
->subbuf_cnt
= num
;
396 DBG("the set_subbuf_cnt for the requested channel is %u", channel
->subbuf_cnt
);
403 static int get_subbuffer(const char *trace_name
, const char *ch_name
,
404 int ch_cpu
, long *consumed_old
)
407 struct ust_trace
*trace
;
408 struct ust_channel
*channel
;
409 struct ust_buffer
*buf
;
416 trace
= _ltt_trace_find(trace_name
);
419 DBG("Cannot find trace. It was likely destroyed by the user.");
424 channel
= find_channel(ch_name
, trace
);
426 ERR("unable to find channel");
431 buf
= channel
->buf
[ch_cpu
];
433 retval
= ust_buffers_get_subbuf(buf
, consumed_old
);
435 WARN("missed buffer?");
445 static int notify_buffer_mapped(const char *trace_name
,
450 struct ust_trace
*trace
;
451 struct ust_channel
*channel
;
452 struct ust_buffer
*buf
;
454 DBG("get_buffer_fd");
457 trace
= _ltt_trace_find(trace_name
);
461 DBG("Cannot find trace. It was likely destroyed by the user.");
465 channel
= find_channel(ch_name
, trace
);
468 ERR("unable to find channel");
472 buf
= channel
->buf
[ch_cpu
];
474 /* Being here is the proof the daemon has mapped the buffer in its
475 * memory. We may now decrement buffers_to_export.
477 if (uatomic_read(&buf
->consumed
) == 0) {
478 DBG("decrementing buffers_to_export");
479 CMM_STORE_SHARED(buffers_to_export
, CMM_LOAD_SHARED(buffers_to_export
)-1);
482 /* The buffer has been exported, ergo, we can add it to the
483 * list of open buffers
485 cds_list_add(&buf
->open_buffers_list
, &open_buffers_list
);
493 static int put_subbuffer(const char *trace_name
, const char *ch_name
,
494 int ch_cpu
, long consumed_old
)
497 struct ust_trace
*trace
;
498 struct ust_channel
*channel
;
499 struct ust_buffer
*buf
;
504 trace
= _ltt_trace_find(trace_name
);
508 DBG("Cannot find trace. It was likely destroyed by the user.");
512 channel
= find_channel(ch_name
, trace
);
515 ERR("unable to find channel");
519 buf
= channel
->buf
[ch_cpu
];
521 retval
= ust_buffers_put_subbuf(buf
, consumed_old
);
523 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
526 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
536 static void listener_cleanup(void *ptr
)
538 ustcomm_del_named_sock(listen_sock
, 0);
541 static void force_subbuf_switch()
543 struct ust_buffer
*buf
;
545 cds_list_for_each_entry(buf
, &open_buffers_list
,
547 ltt_force_switch(buf
, FORCE_FLUSH
);
551 /* Simple commands are those which need only respond with a return value. */
552 static int process_simple_client_cmd(int command
, char *recv_buf
)
559 struct ustcomm_single_field
*sock_msg
;
560 sock_msg
= (struct ustcomm_single_field
*)recv_buf
;
561 result
= ustcomm_unpack_single_field(sock_msg
);
565 return setenv("UST_DAEMON_SOCKET", sock_msg
->field
, 1);
568 case FORCE_SUBBUF_SWITCH
:
569 /* FIXME: return codes? */
570 force_subbuf_switch();
582 static int process_trace_cmd(int command
, char *trace_name
)
585 char trace_type
[] = "ustrelay";
589 /* start is an operation that setups the trace, allocates it and starts it */
590 result
= ltt_trace_setup(trace_name
);
592 ERR("ltt_trace_setup failed");
596 result
= ltt_trace_set_type(trace_name
, trace_type
);
598 ERR("ltt_trace_set_type failed");
602 result
= ltt_trace_alloc(trace_name
);
604 ERR("ltt_trace_alloc failed");
608 inform_consumer_daemon(trace_name
);
610 result
= ltt_trace_start(trace_name
);
612 ERR("ltt_trace_start failed");
620 result
= ltt_trace_setup(trace_name
);
622 ERR("ltt_trace_setup failed");
626 result
= ltt_trace_set_type(trace_name
, trace_type
);
628 ERR("ltt_trace_set_type failed");
636 result
= ltt_trace_alloc(trace_name
);
638 ERR("ltt_trace_alloc failed");
641 inform_consumer_daemon(trace_name
);
648 result
= ltt_trace_setup(trace_name
);
650 ERR("ltt_trace_setup failed");
654 result
= ltt_trace_set_type(trace_name
, trace_type
);
656 ERR("ltt_trace_set_type failed");
664 result
= ltt_trace_alloc(trace_name
);
666 ERR("ltt_trace_alloc failed");
670 inform_consumer_daemon(trace_name
);
673 result
= ltt_trace_start(trace_name
);
675 ERR("ltt_trace_start failed");
683 result
= ltt_trace_stop(trace_name
);
685 ERR("ltt_trace_stop failed");
691 DBG("trace destroy");
693 result
= ltt_trace_destroy(trace_name
, 0);
695 ERR("ltt_trace_destroy failed");
705 static void process_channel_cmd(int sock
, int command
,
706 struct ustcomm_channel_info
*ch_inf
)
708 struct ustcomm_header _reply_header
;
709 struct ustcomm_header
*reply_header
= &_reply_header
;
710 struct ustcomm_channel_info
*reply_msg
=
711 (struct ustcomm_channel_info
*)send_buffer
;
712 int result
, offset
= 0, num
, size
;
714 memset(reply_header
, 0, sizeof(*reply_header
));
717 case GET_SUBBUF_NUM_SIZE
:
718 result
= get_subbuf_num_size(ch_inf
->trace
,
722 reply_header
->result
= result
;
726 reply_msg
->channel
= USTCOMM_POISON_PTR
;
727 reply_msg
->subbuf_num
= num
;
728 reply_msg
->subbuf_size
= size
;
731 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
735 reply_header
->result
= set_subbuf_num(ch_inf
->trace
,
740 case SET_SUBBUF_SIZE
:
741 reply_header
->result
= set_subbuf_size(ch_inf
->trace
,
743 ch_inf
->subbuf_size
);
748 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
749 ERR("ustcomm_send failed");
753 static void process_buffer_cmd(int sock
, int command
,
754 struct ustcomm_buffer_info
*buf_inf
)
756 struct ustcomm_header _reply_header
;
757 struct ustcomm_header
*reply_header
= &_reply_header
;
758 struct ustcomm_buffer_info
*reply_msg
=
759 (struct ustcomm_buffer_info
*)send_buffer
;
760 int result
, offset
= 0, buf_shmid
, buf_struct_shmid
, buf_pipe_fd
;
763 memset(reply_header
, 0, sizeof(*reply_header
));
766 case GET_BUF_SHMID_PIPE_FD
:
767 result
= get_buffer_shmid_pipe_fd(buf_inf
->trace
,
774 reply_header
->result
= result
;
778 reply_msg
->channel
= USTCOMM_POISON_PTR
;
779 reply_msg
->buf_shmid
= buf_shmid
;
780 reply_msg
->buf_struct_shmid
= buf_struct_shmid
;
782 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
783 reply_header
->fd_included
= 1;
785 if (ustcomm_send_fd(sock
, reply_header
, (char *)reply_msg
,
787 ERR("ustcomm_send failed");
791 case NOTIFY_BUF_MAPPED
:
792 reply_header
->result
=
793 notify_buffer_mapped(buf_inf
->trace
,
798 result
= get_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
799 buf_inf
->ch_cpu
, &consumed_old
);
801 reply_header
->result
= result
;
805 reply_msg
->channel
= USTCOMM_POISON_PTR
;
806 reply_msg
->consumed_old
= consumed_old
;
808 reply_header
->size
= COMPUTE_MSG_SIZE(reply_msg
, offset
);
812 result
= put_subbuffer(buf_inf
->trace
, buf_inf
->channel
,
814 buf_inf
->consumed_old
);
815 reply_header
->result
= result
;
820 if (ustcomm_send(sock
, reply_header
, (char *)reply_msg
) < 0) {
821 ERR("ustcomm_send failed");
826 static void process_marker_cmd(int sock
, int command
,
827 struct ustcomm_marker_info
*marker_inf
)
829 struct ustcomm_header _reply_header
;
830 struct ustcomm_header
*reply_header
= &_reply_header
;
833 memset(reply_header
, 0, sizeof(*reply_header
));
838 result
= ltt_marker_connect(marker_inf
->channel
,
842 WARN("could not enable marker; channel=%s,"
850 result
= ltt_marker_disconnect(marker_inf
->channel
,
854 WARN("could not disable marker; channel=%s,"
862 reply_header
->result
= result
;
864 if (ustcomm_send(sock
, reply_header
, NULL
) < 0) {
865 ERR("ustcomm_send failed");
869 static void process_client_cmd(struct ustcomm_header
*recv_header
,
870 char *recv_buf
, int sock
)
873 struct ustcomm_header _reply_header
;
874 struct ustcomm_header
*reply_header
= &_reply_header
;
875 char *send_buf
= send_buffer
;
877 memset(reply_header
, 0, sizeof(*reply_header
));
878 memset(send_buf
, 0, sizeof(send_buffer
));
880 switch(recv_header
->command
) {
881 case GET_SUBBUF_NUM_SIZE
:
883 case SET_SUBBUF_SIZE
:
885 struct ustcomm_channel_info
*ch_inf
;
886 ch_inf
= (struct ustcomm_channel_info
*)recv_buf
;
887 result
= ustcomm_unpack_channel_info(ch_inf
);
889 ERR("couldn't unpack channel info");
890 reply_header
->result
= -EINVAL
;
893 process_channel_cmd(sock
, recv_header
->command
, ch_inf
);
896 case GET_BUF_SHMID_PIPE_FD
:
897 case NOTIFY_BUF_MAPPED
:
901 struct ustcomm_buffer_info
*buf_inf
;
902 buf_inf
= (struct ustcomm_buffer_info
*)recv_buf
;
903 result
= ustcomm_unpack_buffer_info(buf_inf
);
905 ERR("couldn't unpack buffer info");
906 reply_header
->result
= -EINVAL
;
909 process_buffer_cmd(sock
, recv_header
->command
, buf_inf
);
915 struct ustcomm_marker_info
*marker_inf
;
916 marker_inf
= (struct ustcomm_marker_info
*)recv_buf
;
917 result
= ustcomm_unpack_marker_info(marker_inf
);
919 ERR("couldn't unpack marker info");
920 reply_header
->result
= -EINVAL
;
923 process_marker_cmd(sock
, recv_header
->command
, marker_inf
);
932 fp
= open_memstream(&ptr
, &size
);
934 ERR("opening memstream failed");
940 reply_header
->size
= size
;
942 result
= ustcomm_send(sock
, reply_header
, ptr
);
947 PERROR("failed to send markers list");
952 case LIST_TRACE_EVENTS
:
958 fp
= open_memstream(&ptr
, &size
);
960 ERR("opening memstream failed");
963 print_trace_events(fp
);
966 reply_header
->size
= size
;
968 result
= ustcomm_send(sock
, reply_header
, ptr
);
973 ERR("list_trace_events failed");
983 /* FIXME: No functionality at all... */
986 DBG("load_probe_lib loading %s", libfile
);
992 struct ustcomm_pidunique
*pid_msg
;
993 pid_msg
= (struct ustcomm_pidunique
*)send_buf
;
995 pid_msg
->pidunique
= pidunique
;
996 reply_header
->size
= sizeof(pid_msg
);
1003 struct ustcomm_single_field
*sock_msg
;
1004 char *sock_path_env
;
1006 sock_msg
= (struct ustcomm_single_field
*)send_buf
;
1008 sock_path_env
= getenv("UST_DAEMON_SOCKET");
1010 if (!sock_path_env
) {
1011 result
= ustcomm_pack_single_field(reply_header
,
1013 SOCK_DIR
"/ustconsumer");
1016 result
= ustcomm_pack_single_field(reply_header
,
1020 reply_header
->result
= result
;
1032 struct ustcomm_single_field
*trace_inf
=
1033 (struct ustcomm_single_field
*)recv_buf
;
1035 result
= ustcomm_unpack_single_field(trace_inf
);
1037 ERR("couldn't unpack trace info");
1038 reply_header
->result
= -EINVAL
;
1042 reply_header
->result
=
1043 process_trace_cmd(recv_header
->command
,
1049 reply_header
->result
=
1050 process_simple_client_cmd(recv_header
->command
,
1059 ustcomm_send(sock
, reply_header
, send_buf
);
1062 #define MAX_EVENTS 10
1064 void *listener_main(void *p
)
1066 struct ustcomm_sock
*epoll_sock
;
1067 struct epoll_event events
[MAX_EVENTS
];
1068 struct sockaddr addr
;
1069 int accept_fd
, nfds
, result
, i
, addr_size
;
1073 pthread_cleanup_push(listener_cleanup
, NULL
);
1076 nfds
= epoll_wait(epoll_fd
, events
, MAX_EVENTS
, -1);
1078 PERROR("listener_main: epoll_wait failed");
1082 for (i
= 0; i
< nfds
; i
++) {
1083 epoll_sock
= (struct ustcomm_sock
*)events
[i
].data
.ptr
;
1084 if (epoll_sock
== listen_sock
) {
1085 addr_size
= sizeof(struct sockaddr
);
1086 accept_fd
= accept(epoll_sock
->fd
,
1088 (socklen_t
*)&addr_size
);
1089 if (accept_fd
== -1) {
1090 PERROR("listener_main: accept failed");
1093 ustcomm_init_sock(accept_fd
, epoll_fd
,
1096 memset(receive_header
, 0,
1097 sizeof(*receive_header
));
1098 memset(receive_buffer
, 0,
1099 sizeof(receive_buffer
));
1100 result
= ustcomm_recv(epoll_sock
->fd
,
1104 ustcomm_del_sock(epoll_sock
, 0);
1106 process_client_cmd(receive_header
,
1114 pthread_cleanup_pop(1);
1117 /* These should only be accessed in the parent thread,
1120 static volatile sig_atomic_t have_listener
= 0;
1121 static pthread_t listener_thread
;
1123 void create_listener(void)
1126 sigset_t sig_all_blocked
;
1127 sigset_t orig_parent_mask
;
1129 if (have_listener
) {
1130 WARN("not creating listener because we already had one");
1134 /* A new thread created by pthread_create inherits the signal mask
1135 * from the parent. To avoid any signal being received by the
1136 * listener thread, we block all signals temporarily in the parent,
1137 * while we create the listener thread.
1140 sigfillset(&sig_all_blocked
);
1142 result
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1144 PERROR("pthread_sigmask: %s", strerror(result
));
1147 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1149 PERROR("pthread_create");
1152 /* Restore original signal mask in parent */
1153 result
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1155 PERROR("pthread_sigmask: %s", strerror(result
));
1161 #define AUTOPROBE_DISABLED 0
1162 #define AUTOPROBE_ENABLE_ALL 1
1163 #define AUTOPROBE_ENABLE_REGEX 2
1164 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1165 static regex_t autoprobe_regex
;
1167 static void auto_probe_connect(struct marker
*m
)
1171 char* concat_name
= NULL
;
1172 const char *probe_name
= "default";
1174 if (autoprobe_method
== AUTOPROBE_DISABLED
) {
1176 } else if (autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1177 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1179 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1180 m
->channel
, m
->name
);
1183 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1190 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1191 if (result
&& result
!= -EEXIST
)
1192 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1194 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1198 static struct ustcomm_sock
* init_app_socket(int epoll_fd
)
1202 struct ustcomm_sock
*sock
;
1204 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)getpid());
1206 ERR("string overflow allocating socket name, "
1207 "UST thread bailing");
1211 result
= ensure_dir_exists(SOCK_DIR
);
1213 ERR("Unable to create socket directory %s, UST thread bailing",
1218 sock
= ustcomm_init_named_socket(name
, epoll_fd
);
1220 ERR("Error initializing named socket (%s). Check that directory"
1221 "exists and that it is writable. UST thread bailing", name
);
1233 static void __attribute__((constructor
)) init()
1237 char* autoprobe_val
= NULL
;
1238 char* subbuffer_size_val
= NULL
;
1239 char* subbuffer_count_val
= NULL
;
1240 unsigned int subbuffer_size
;
1241 unsigned int subbuffer_count
;
1244 /* Assign the pidunique, to be able to differentiate the processes with same
1245 * pid, (before and after an exec).
1247 pidunique
= make_pidunique();
1248 processpid
= getpid();
1250 DBG("Tracectl constructor");
1253 epoll_fd
= epoll_create(MAX_EVENTS
);
1254 if (epoll_fd
== -1) {
1255 ERR("epoll_create failed, tracing shutting down");
1259 /* Create the socket */
1260 listen_sock
= init_app_socket(epoll_fd
);
1262 ERR("failed to create application socket,"
1263 " tracing shutting down");
1269 /* Get clock the clock source type */
1271 /* Default clock source */
1272 ust_clock_source
= CLOCK_TRACE
;
1273 if (clock_gettime(ust_clock_source
, &ts
) != 0) {
1274 ust_clock_source
= CLOCK_MONOTONIC
;
1275 DBG("UST traces will not be synchronized with LTTng traces");
1278 autoprobe_val
= getenv("UST_AUTOPROBE");
1279 if (autoprobe_val
) {
1280 struct marker_iter iter
;
1282 DBG("Autoprobe enabled.");
1284 /* Ensure markers are initialized */
1287 /* Ensure marker control is initialized, for the probe */
1288 init_marker_control();
1290 /* first, set the callback that will connect the
1291 * probe on new markers
1293 if (autoprobe_val
[0] == '/') {
1294 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1298 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1299 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1300 /* don't crash the application just for this */
1302 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1305 /* just enable all instrumentation */
1306 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1309 marker_set_new_marker_cb(auto_probe_connect
);
1311 /* Now, connect the probes that were already registered. */
1312 marker_iter_reset(&iter
);
1313 marker_iter_start(&iter
);
1315 DBG("now iterating on markers already registered");
1316 while (iter
.marker
) {
1317 DBG("now iterating on marker %s", (*iter
.marker
)->name
);
1318 auto_probe_connect(*iter
.marker
);
1319 marker_iter_next(&iter
);
1323 if (getenv("UST_OVERWRITE")) {
1324 int val
= atoi(getenv("UST_OVERWRITE"));
1325 if (val
== 0 || val
== 1) {
1326 CMM_STORE_SHARED(ust_channels_overwrite_by_default
, val
);
1328 WARN("invalid value for UST_OVERWRITE");
1332 if (getenv("UST_AUTOCOLLECT")) {
1333 int val
= atoi(getenv("UST_AUTOCOLLECT"));
1334 if (val
== 0 || val
== 1) {
1335 CMM_STORE_SHARED(ust_channels_request_collection_by_default
, val
);
1337 WARN("invalid value for UST_AUTOCOLLECT");
1341 subbuffer_size_val
= getenv("UST_SUBBUF_SIZE");
1342 if (subbuffer_size_val
) {
1343 sscanf(subbuffer_size_val
, "%u", &subbuffer_size
);
1344 power
= pow2_higher_or_eq(subbuffer_size
);
1345 if (power
!= subbuffer_size
)
1346 WARN("using the next power of two for buffer size = %u\n", power
);
1347 chan_infos
[LTT_CHANNEL_UST
].def_subbufsize
= power
;
1350 subbuffer_count_val
= getenv("UST_SUBBUF_NUM");
1351 if (subbuffer_count_val
) {
1352 sscanf(subbuffer_count_val
, "%u", &subbuffer_count
);
1353 if (subbuffer_count
< 2)
1354 subbuffer_count
= 2;
1355 chan_infos
[LTT_CHANNEL_UST
].def_subbufcount
= subbuffer_count
;
1358 if (getenv("UST_TRACE")) {
1359 char trace_name
[] = "auto";
1360 char trace_type
[] = "ustrelay";
1362 DBG("starting early tracing");
1364 /* Ensure marker control is initialized */
1365 init_marker_control();
1367 /* Ensure markers are initialized */
1370 /* Ensure buffers are initialized, for the transport to be available.
1371 * We are about to set a trace type and it will fail without this.
1373 init_ustrelay_transport();
1375 /* FIXME: When starting early tracing (here), depending on the
1376 * order of constructors, it is very well possible some marker
1377 * sections are not yet registered. Because of this, some
1378 * channels may not be registered. Yet, we are about to ask the
1379 * daemon to collect the channels. Channels which are not yet
1380 * registered will not be collected.
1382 * Currently, in LTTng, there is no way to add a channel after
1383 * trace start. The reason for this is that it induces complex
1384 * concurrency issues on the trace structures, which can only
1385 * be resolved using RCU. This has not been done yet. As a
1386 * workaround, we are forcing the registration of the "ust"
1387 * channel here. This is the only channel (apart from metadata)
1388 * that can be reliably used in early tracing.
1390 * Non-early tracing does not have this problem and can use
1391 * arbitrary channel names.
1393 ltt_channels_register("ust");
1395 result
= ltt_trace_setup(trace_name
);
1397 ERR("ltt_trace_setup failed");
1401 result
= ltt_trace_set_type(trace_name
, trace_type
);
1403 ERR("ltt_trace_set_type failed");
1407 result
= ltt_trace_alloc(trace_name
);
1409 ERR("ltt_trace_alloc failed");
1413 result
= ltt_trace_start(trace_name
);
1415 ERR("ltt_trace_start failed");
1419 /* Do this after the trace is started in order to avoid creating confusion
1420 * if the trace fails to start. */
1421 inform_consumer_daemon(trace_name
);
1426 /* should decrementally destroy stuff if error */
1430 /* This is only called if we terminate normally, not with an unhandled signal,
1431 * so we cannot rely on it. However, for now, LTTV requires that the header of
1432 * the last sub-buffer contain a valid end time for the trace. This is done
1433 * automatically only when the trace is properly stopped.
1435 * If the traced program crashed, it is always possible to manually add the
1436 * right value in the header, or to open the trace in text mode.
1438 * FIXME: Fix LTTV so it doesn't need this.
1441 static void destroy_traces(void)
1445 /* if trace running, finish it */
1447 DBG("destructor stopping traces");
1449 result
= ltt_trace_stop("auto");
1451 ERR("ltt_trace_stop error");
1454 result
= ltt_trace_destroy("auto", 0);
1456 ERR("ltt_trace_destroy error");
1460 static int trace_recording(void)
1463 struct ust_trace
*trace
;
1467 cds_list_for_each_entry(trace
, <t_traces
.head
, list
) {
1468 if (trace
->active
) {
1474 ltt_unlock_traces();
1479 int restarting_usleep(useconds_t usecs
)
1485 tv
.tv_nsec
= usecs
* 1000;
1488 result
= nanosleep(&tv
, &tv
);
1489 } while (result
== -1 && errno
== EINTR
);
1494 static void stop_listener(void)
1501 result
= pthread_cancel(listener_thread
);
1503 ERR("pthread_cancel: %s", strerror(result
));
1505 result
= pthread_join(listener_thread
, NULL
);
1507 ERR("pthread_join: %s", strerror(result
));
1511 /* This destructor keeps the process alive for a few seconds in order
1512 * to leave time for ustconsumer to connect to its buffers. This is necessary
1513 * for programs whose execution is very short. It is also useful in all
1514 * programs when tracing is started close to the end of the program
1517 * FIXME: For now, this only works for the first trace created in a
1521 static void __attribute__((destructor
)) keepalive()
1523 if (processpid
!= getpid()) {
1527 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export
)) {
1529 DBG("Keeping process alive for consumer daemon...");
1530 while (CMM_LOAD_SHARED(buffers_to_export
)) {
1531 const int interv
= 200000;
1532 restarting_usleep(interv
);
1535 if (total
>= 3000000) {
1536 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1540 DBG("Finally dying...");
1545 /* Ask the listener to stop and clean up. */
1549 void ust_potential_exec(void)
1551 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1558 /* Notify ust that there was a fork. This needs to be called inside
1559 * the new process, anytime a process whose memory is not shared with
1560 * the parent is created. If this function is not called, the events
1561 * of the new process will not be collected.
1563 * Signals should be disabled before the fork and reenabled only after
1564 * this call in order to guarantee tracing is not started before ust_fork()
1565 * sanitizes the new process.
1568 static void ust_fork(void)
1570 struct ust_buffer
*buf
, *buf_tmp
;
1571 struct ustcomm_sock
*sock
, *sock_tmp
;
1574 /* FIXME: technically, the locks could have been taken before the fork */
1575 DBG("ust: forking");
1577 /* Get the pid of the new process */
1578 processpid
= getpid();
1580 /* break lock if necessary */
1581 ltt_unlock_traces();
1583 ltt_trace_stop("auto");
1584 ltt_trace_destroy("auto", 1);
1585 /* Delete all active connections, but leave them in the epoll set */
1586 cds_list_for_each_entry_safe(sock
, sock_tmp
, &ust_socks
, list
) {
1587 ustcomm_del_sock(sock
, 1);
1590 /* Delete all blocked consumers */
1591 cds_list_for_each_entry_safe(buf
, buf_tmp
, &open_buffers_list
,
1592 open_buffers_list
) {
1593 result
= close(buf
->data_ready_fd_read
);
1597 result
= close(buf
->data_ready_fd_write
);
1601 cds_list_del(&buf
->open_buffers_list
);
1604 /* Clean up the listener socket and epoll, keeping the scoket file */
1605 ustcomm_del_named_sock(listen_sock
, 1);
1608 /* Re-start the launch sequence */
1609 CMM_STORE_SHARED(buffers_to_export
, 0);
1613 epoll_fd
= epoll_create(MAX_EVENTS
);
1614 if (epoll_fd
== -1) {
1615 ERR("epoll_create failed, tracing shutting down");
1619 /* Create the socket */
1620 listen_sock
= init_app_socket(epoll_fd
);
1622 ERR("failed to create application socket,"
1623 " tracing shutting down");
1627 ltt_trace_setup("auto");
1628 result
= ltt_trace_set_type("auto", "ustrelay");
1630 ERR("ltt_trace_set_type failed");
1634 ltt_trace_alloc("auto");
1635 ltt_trace_start("auto");
1636 inform_consumer_daemon("auto");
1639 void ust_before_fork(ust_fork_info_t
*fork_info
)
1641 /* Disable signals. This is to avoid that the child
1642 * intervenes before it is properly setup for tracing. It is
1643 * safer to disable all signals, because then we know we are not
1644 * breaking anything by restoring the original mask.
1650 - only do this if tracing is active
1653 /* Disable signals */
1654 sigfillset(&all_sigs
);
1655 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1657 PERROR("sigprocmask");
1662 /* Don't call this function directly in a traced program */
1663 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1667 /* Restore signals */
1668 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1670 PERROR("sigprocmask");
1675 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1677 /* Reenable signals */
1678 ust_after_fork_common(fork_info
);
1681 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1683 /* First sanitize the child */
1686 /* Then reenable interrupts */
1687 ust_after_fork_common(fork_info
);