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
23 #include <sys/types.h>
24 #include <sys/socket.h>
29 #include <urcu/uatomic_arch.h>
31 #include <ust/marker.h>
32 #include <ust/tracectl.h>
37 #include "marker-control.h"
38 #include "multipoll.h"
40 #define USTSIGNAL SIGIO
42 #define MAX_MSG_SIZE (100)
44 #define MSG_REGISTER_NOTIF 2
46 char consumer_stack
[10000];
48 /* This should only be accessed by the constructor, before the creation
49 * of the listener, and then only by the listener.
53 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
55 static struct ustcomm_app ustcomm_app
;
57 struct tracecmd
{ /* no padding */
62 /* volatile because shared between the listener and the main thread */
63 volatile sig_atomic_t buffers_to_export
= 0;
66 /* size: the size of all the fields except size itself */
69 /* Only the necessary part of the payload is transferred. It
70 * may even be none of it.
75 struct consumer_channel
{
77 struct ltt_channel_struct
*chan
;
80 struct blocked_consumer
{
85 /* args to ustcomm_send_reply */
86 struct ustcomm_server server
;
87 struct ustcomm_source src
;
89 /* args to ust_buffers_get_subbuf */
90 struct ust_buffer
*buf
;
92 struct list_head list
;
95 static long long make_pidunique(void)
100 gettimeofday(&tv
, NULL
);
104 retval
|= tv
.tv_usec
;
109 static void print_markers(FILE *fp
)
111 struct marker_iter iter
;
114 marker_iter_reset(&iter
);
115 marker_iter_start(&iter
);
118 fprintf(fp
, "marker: %s/%s %d \"%s\" %p\n", iter
.marker
->channel
, iter
.marker
->name
, (int)imv_read(iter
.marker
->state
), iter
.marker
->format
, iter
.marker
->location
);
119 marker_iter_next(&iter
);
124 static int init_socket(void);
130 struct trctl_msg msg
;
132 /* FIXME: fd_notif should probably be protected by a spinlock */
137 msg
.type
= MSG_NOTIF
;
138 msg
.size
= sizeof(msg
.type
);
140 /* FIXME: don't block here */
141 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
148 /* Ask the daemon to collect a trace called trace_name and being
149 * produced by this pid.
151 * The trace must be at least allocated. (It can also be started.)
152 * This is because _ltt_trace_find is used.
155 static void inform_consumer_daemon(const char *trace_name
)
158 struct ust_trace
*trace
;
159 pid_t pid
= getpid();
164 trace
= _ltt_trace_find(trace_name
);
166 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
170 for(i
=0; i
< trace
->nr_channels
; i
++) {
171 /* iterate on all cpus */
172 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
174 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
175 result
= ustcomm_request_consumer(pid
, buf
);
177 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
178 /* continue even if fail */
189 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
192 long consumed_old
= 0;
194 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
197 result
= read(bc
->fd_producer
, &inbuf
, 1);
204 DBG("listener: got messsage that a buffer ended");
206 res
= close(bc
->fd_producer
);
213 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
215 ERR("ustcomm_send_reply failed");
222 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
223 if(result
== -EAGAIN
) {
224 WARN("missed buffer?");
227 else if(result
< 0) {
228 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
230 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
231 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
233 ERR("ustcomm_send_reply failed");
244 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
246 struct blocked_consumer
*bc
;
248 list_for_each_entry(bc
, &blocked_consumers
, list
) {
249 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
254 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
258 sep
= rindex(channel_and_cpu
, '_');
261 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
267 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
270 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
273 struct ust_trace
*trace
;
274 char trace_name
[] = "auto";
276 char *channel_and_cpu
;
284 channel_and_cpu
= nth_token(recvbuf
, 1);
285 if(channel_and_cpu
== NULL
) {
286 ERR("cannot parse channel");
290 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
292 ERR("problem parsing channel name");
293 goto free_short_chan_name
;
297 trace
= _ltt_trace_find(trace_name
);
301 ERR("cannot find trace!");
303 goto free_short_chan_name
;
306 for(i
=0; i
<trace
->nr_channels
; i
++) {
307 struct ust_channel
*channel
= &trace
->channels
[i
];
308 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
310 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
313 // DBG("the shmid for the requested channel is %d", buf->shmid);
314 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
315 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
317 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
319 ERR("ustcomm_send_reply failed");
322 goto free_short_chan_name
;
333 ERR("channel not found (%s)", channel_and_cpu
);
336 free_short_chan_name
:
343 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
346 struct ust_trace
*trace
;
347 char trace_name
[] = "auto";
349 char *channel_and_cpu
;
355 DBG("get_n_subbufs");
357 channel_and_cpu
= nth_token(recvbuf
, 1);
358 if(channel_and_cpu
== NULL
) {
359 ERR("cannot parse channel");
363 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
365 ERR("problem parsing channel name");
366 goto free_short_chan_name
;
370 trace
= _ltt_trace_find(trace_name
);
374 ERR("cannot find trace!");
376 goto free_short_chan_name
;
379 for(i
=0; i
<trace
->nr_channels
; i
++) {
380 struct ust_channel
*channel
= &trace
->channels
[i
];
382 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
385 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
386 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
388 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
390 ERR("ustcomm_send_reply failed");
393 goto free_short_chan_name
;
402 ERR("unable to find channel");
405 free_short_chan_name
:
412 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
415 struct ust_trace
*trace
;
416 char trace_name
[] = "auto";
418 char *channel_and_cpu
;
424 DBG("get_subbuf_size");
426 channel_and_cpu
= nth_token(recvbuf
, 1);
427 if(channel_and_cpu
== NULL
) {
428 ERR("cannot parse channel");
432 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
434 ERR("problem parsing channel name");
435 goto free_short_chan_name
;
439 trace
= _ltt_trace_find(trace_name
);
443 ERR("cannot find trace!");
445 goto free_short_chan_name
;
448 for(i
=0; i
<trace
->nr_channels
; i
++) {
449 struct ust_channel
*channel
= &trace
->channels
[i
];
451 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
454 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
455 asprintf(&reply
, "%zd", channel
->subbuf_size
);
457 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
459 ERR("ustcomm_send_reply failed");
462 goto free_short_chan_name
;
471 ERR("unable to find channel");
474 free_short_chan_name
:
481 static unsigned int poweroftwo(unsigned int x
)
483 unsigned int power2
= 1;
484 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
489 while (power2
< x
&& power2
< hardcoded
)
495 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
497 char *channel_slash_size
;
498 char ch_name
[256]="";
499 unsigned int size
, power
;
501 struct ust_trace
*trace
;
502 char trace_name
[] = "auto";
506 DBG("set_subbuf_size");
508 channel_slash_size
= nth_token(recvbuf
, 1);
509 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
511 if(ch_name
== NULL
) {
512 ERR("cannot parse channel");
516 power
= poweroftwo(size
);
518 WARN("using the next 2^n = %u\n", power
);
521 trace
= _ltt_trace_find_setup(trace_name
);
523 ERR("cannot find trace!");
528 for(i
= 0; i
< trace
->nr_channels
; i
++) {
529 struct ust_channel
*channel
= &trace
->channels
[i
];
531 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
533 channel
->subbuf_size
= power
;
534 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
541 ERR("unable to find channel");
549 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
551 char *channel_slash_num
;
552 char ch_name
[256]="";
555 struct ust_trace
*trace
;
556 char trace_name
[] = "auto";
560 DBG("set_subbuf_num");
562 channel_slash_num
= nth_token(recvbuf
, 1);
563 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
565 if(ch_name
== NULL
) {
566 ERR("cannot parse channel");
570 ERR("subbuffer count should be greater than 2");
575 trace
= _ltt_trace_find_setup(trace_name
);
577 ERR("cannot find trace!");
582 for(i
= 0; i
< trace
->nr_channels
; i
++) {
583 struct ust_channel
*channel
= &trace
->channels
[i
];
585 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
587 channel
->subbuf_cnt
= num
;
588 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
595 ERR("unable to find channel");
603 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
606 struct ust_trace
*trace
;
607 char trace_name
[] = "auto";
609 char *channel_and_cpu
;
616 channel_and_cpu
= nth_token(recvbuf
, 1);
617 if(channel_and_cpu
== NULL
) {
618 ERR("cannot parse channel");
622 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
624 ERR("problem parsing channel name");
625 goto free_short_chan_name
;
629 trace
= _ltt_trace_find(trace_name
);
634 DBG("Cannot find trace. It was likely destroyed by the user.");
635 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
637 ERR("ustcomm_send_reply failed");
645 for(i
=0; i
<trace
->nr_channels
; i
++) {
646 struct ust_channel
*channel
= &trace
->channels
[i
];
648 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
649 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
650 struct blocked_consumer
*bc
;
654 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
656 ERR("malloc returned NULL");
659 bc
->fd_consumer
= src
->fd
;
660 bc
->fd_producer
= buf
->data_ready_fd_read
;
663 bc
->server
= ustcomm_app
.server
;
665 list_add(&bc
->list
, &blocked_consumers
);
667 /* Being here is the proof the daemon has mapped the buffer in its
668 * memory. We may now decrement buffers_to_export.
670 if(uatomic_read(&buf
->consumed
) == 0) {
671 DBG("decrementing buffers_to_export");
679 ERR("unable to find channel");
685 free_short_chan_name
:
692 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
695 struct ust_trace
*trace
;
696 char trace_name
[] = "auto";
698 char *channel_and_cpu
;
704 char *consumed_old_str
;
710 channel_and_cpu
= strdup_malloc(nth_token(recvbuf
, 1));
711 if(channel_and_cpu
== NULL
) {
712 ERR("cannot parse channel");
717 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
718 if(consumed_old_str
== NULL
) {
719 ERR("cannot parse consumed_old");
721 goto free_channel_and_cpu
;
723 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
724 if(*endptr
!= '\0') {
725 ERR("invalid value for consumed_old");
727 goto free_consumed_old_str
;
730 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
732 ERR("problem parsing channel name");
734 goto free_short_chan_name
;
738 trace
= _ltt_trace_find(trace_name
);
741 DBG("Cannot find trace. It was likely destroyed by the user.");
742 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
744 ERR("ustcomm_send_reply failed");
752 for(i
=0; i
<trace
->nr_channels
; i
++) {
753 struct ust_channel
*channel
= &trace
->channels
[i
];
755 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
756 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
760 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
762 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
763 asprintf(&reply
, "%s", "ERROR");
766 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
767 asprintf(&reply
, "%s", "OK");
770 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
772 ERR("ustcomm_send_reply failed");
783 ERR("unable to find channel");
788 free_short_chan_name
:
790 free_consumed_old_str
:
791 free(consumed_old_str
);
792 free_channel_and_cpu
:
793 free(channel_and_cpu
);
799 static void listener_cleanup(void *ptr
)
801 ustcomm_fini_app(&ustcomm_app
, 0);
804 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
807 char trace_name
[] = "auto";
808 char trace_type
[] = "ustrelay";
811 DBG("received a message! it's: %s", recvbuf
);
812 len
= strlen(recvbuf
);
814 if(!strcmp(recvbuf
, "print_markers")) {
815 print_markers(stderr
);
817 else if(!strcmp(recvbuf
, "list_markers")) {
822 fp
= open_memstream(&ptr
, &size
);
826 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
830 else if(!strcmp(recvbuf
, "start")) {
831 /* start is an operation that setups the trace, allocates it and starts it */
832 result
= ltt_trace_setup(trace_name
);
834 ERR("ltt_trace_setup failed");
838 result
= ltt_trace_set_type(trace_name
, trace_type
);
840 ERR("ltt_trace_set_type failed");
844 result
= ltt_trace_alloc(trace_name
);
846 ERR("ltt_trace_alloc failed");
850 inform_consumer_daemon(trace_name
);
852 result
= ltt_trace_start(trace_name
);
854 ERR("ltt_trace_start failed");
858 else if(!strcmp(recvbuf
, "trace_setup")) {
861 result
= ltt_trace_setup(trace_name
);
863 ERR("ltt_trace_setup failed");
867 result
= ltt_trace_set_type(trace_name
, trace_type
);
869 ERR("ltt_trace_set_type failed");
873 else if(!strcmp(recvbuf
, "trace_alloc")) {
876 result
= ltt_trace_alloc(trace_name
);
878 ERR("ltt_trace_alloc failed");
881 inform_consumer_daemon(trace_name
);
883 else if(!strcmp(recvbuf
, "trace_create")) {
886 result
= ltt_trace_setup(trace_name
);
888 ERR("ltt_trace_setup failed");
892 result
= ltt_trace_set_type(trace_name
, trace_type
);
894 ERR("ltt_trace_set_type failed");
898 else if(!strcmp(recvbuf
, "trace_start")) {
901 result
= ltt_trace_alloc(trace_name
);
903 ERR("ltt_trace_alloc failed");
907 inform_consumer_daemon(trace_name
);
910 result
= ltt_trace_start(trace_name
);
912 ERR("ltt_trace_start failed");
916 else if(!strcmp(recvbuf
, "trace_stop")) {
919 result
= ltt_trace_stop(trace_name
);
921 ERR("ltt_trace_stop failed");
925 else if(!strcmp(recvbuf
, "trace_destroy")) {
927 DBG("trace destroy");
929 result
= ltt_trace_destroy(trace_name
, 0);
931 ERR("ltt_trace_destroy failed");
935 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
936 do_cmd_get_shmid(recvbuf
, src
);
938 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
939 do_cmd_get_n_subbufs(recvbuf
, src
);
941 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
942 do_cmd_get_subbuf_size(recvbuf
, src
);
944 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
947 libfile
= nth_token(recvbuf
, 1);
949 DBG("load_probe_lib loading %s", libfile
);
953 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
954 do_cmd_get_subbuffer(recvbuf
, src
);
956 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
957 do_cmd_put_subbuffer(recvbuf
, src
);
959 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
960 do_cmd_set_subbuf_size(recvbuf
, src
);
962 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
963 do_cmd_set_subbuf_num(recvbuf
, src
);
965 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
966 char *channel_slash_name
= nth_token(recvbuf
, 1);
967 char channel_name
[256]="";
968 char marker_name
[256]="";
970 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
972 if(channel_name
== NULL
|| marker_name
== NULL
) {
973 WARN("invalid marker name");
977 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
979 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
982 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
983 char *channel_slash_name
= nth_token(recvbuf
, 1);
987 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
989 if(marker_name
== NULL
) {
992 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
994 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
997 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1000 asprintf(&reply
, "%lld", pidunique
);
1002 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
1004 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1010 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1011 // struct ust_trace *trace;
1012 // char trace_name[] = "auto";
1014 // char *channel_name;
1016 // DBG("get_notifications");
1018 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1019 // if(channel_name == NULL) {
1020 // ERR("put_subbuf_size: cannot parse channel");
1024 // ltt_lock_traces();
1025 // trace = _ltt_trace_find(trace_name);
1026 // ltt_unlock_traces();
1028 // if(trace == NULL) {
1029 // ERR("cannot find trace!");
1030 // return (void *)1;
1033 // for(i=0; i<trace->nr_channels; i++) {
1034 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1037 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1038 // struct rchan_buf *rbuf = rchan->buf;
1039 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1041 // result = fd = ustcomm_app_detach_client(&ustcomm_app, src);
1042 // if(result == -1) {
1043 // ERR("ustcomm_app_detach_client failed");
1047 // lttbuf->wake_consumer_arg = (void *) fd;
1051 // lttbuf->call_wake_consumer = 1;
1057 // free(channel_name);
1060 ERR("unable to parse message: %s", recvbuf
);
1068 void *listener_main(void *p
)
1074 pthread_cleanup_push(listener_cleanup
, NULL
);
1077 struct mpentries mpent
;
1079 multipoll_init(&mpent
);
1081 blocked_consumers_add_to_mp(&mpent
);
1082 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1084 result
= multipoll_poll(&mpent
, -1);
1086 ERR("error in multipoll_poll");
1089 multipoll_destroy(&mpent
);
1092 pthread_cleanup_pop(1);
1095 /* These should only be accessed in the parent thread,
1098 static volatile sig_atomic_t have_listener
= 0;
1099 static pthread_t listener_thread
;
1101 void create_listener(void)
1106 WARN("not creating listener because we already had one");
1110 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1112 PERROR("pthread_create");
1118 static int init_socket(void)
1120 return ustcomm_init_app(getpid(), &ustcomm_app
);
1123 #define AUTOPROBE_DISABLED 0
1124 #define AUTOPROBE_ENABLE_ALL 1
1125 #define AUTOPROBE_ENABLE_REGEX 2
1126 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1127 static regex_t autoprobe_regex
;
1129 static void auto_probe_connect(struct marker
*m
)
1133 char* concat_name
= NULL
;
1134 const char *probe_name
= "default";
1136 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 void __attribute__((constructor
)) init()
1164 char* autoprobe_val
= NULL
;
1166 /* Assign the pidunique, to be able to differentiate the processes with same
1167 * pid, (before and after an exec).
1169 pidunique
= make_pidunique();
1171 DBG("Tracectl constructor");
1173 result
= init_socket();
1175 ERR("init_socket error");
1181 autoprobe_val
= getenv("UST_AUTOPROBE");
1183 struct marker_iter iter
;
1185 DBG("Autoprobe enabled.");
1187 /* Ensure markers are initialized */
1190 /* Ensure marker control is initialized, for the probe */
1191 init_marker_control();
1193 /* first, set the callback that will connect the
1194 * probe on new markers
1196 if(autoprobe_val
[0] == '/') {
1197 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1201 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1202 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1203 /* don't crash the application just for this */
1206 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1210 /* just enable all instrumentation */
1211 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1214 marker_set_new_marker_cb(auto_probe_connect
);
1216 /* Now, connect the probes that were already registered. */
1217 marker_iter_reset(&iter
);
1218 marker_iter_start(&iter
);
1220 DBG("now iterating on markers already registered");
1221 while(iter
.marker
) {
1222 DBG("now iterating on marker %s", iter
.marker
->name
);
1223 auto_probe_connect(iter
.marker
);
1224 marker_iter_next(&iter
);
1228 if(getenv("UST_TRACE")) {
1229 char trace_name
[] = "auto";
1230 char trace_type
[] = "ustrelay";
1232 DBG("starting early tracing");
1234 /* Ensure marker control is initialized */
1235 init_marker_control();
1237 /* Ensure markers are initialized */
1240 /* Ensure buffers are initialized, for the transport to be available.
1241 * We are about to set a trace type and it will fail without this.
1243 init_ustrelay_transport();
1245 /* FIXME: When starting early tracing (here), depending on the
1246 * order of constructors, it is very well possible some marker
1247 * sections are not yet registered. Because of this, some
1248 * channels may not be registered. Yet, we are about to ask the
1249 * daemon to collect the channels. Channels which are not yet
1250 * registered will not be collected.
1252 * Currently, in LTTng, there is no way to add a channel after
1253 * trace start. The reason for this is that it induces complex
1254 * concurrency issues on the trace structures, which can only
1255 * be resolved using RCU. This has not been done yet. As a
1256 * workaround, we are forcing the registration of the "ust"
1257 * channel here. This is the only channel (apart from metadata)
1258 * that can be reliably used in early tracing.
1260 * Non-early tracing does not have this problem and can use
1261 * arbitrary channel names.
1263 ltt_channels_register("ust");
1265 result
= ltt_trace_setup(trace_name
);
1267 ERR("ltt_trace_setup failed");
1271 result
= ltt_trace_set_type(trace_name
, trace_type
);
1273 ERR("ltt_trace_set_type failed");
1277 result
= ltt_trace_alloc(trace_name
);
1279 ERR("ltt_trace_alloc failed");
1283 result
= ltt_trace_start(trace_name
);
1285 ERR("ltt_trace_start failed");
1289 /* Do this after the trace is started in order to avoid creating confusion
1290 * if the trace fails to start. */
1291 inform_consumer_daemon(trace_name
);
1297 /* should decrementally destroy stuff if error */
1301 /* This is only called if we terminate normally, not with an unhandled signal,
1302 * so we cannot rely on it. However, for now, LTTV requires that the header of
1303 * the last sub-buffer contain a valid end time for the trace. This is done
1304 * automatically only when the trace is properly stopped.
1306 * If the traced program crashed, it is always possible to manually add the
1307 * right value in the header, or to open the trace in text mode.
1309 * FIXME: Fix LTTV so it doesn't need this.
1312 static void destroy_traces(void)
1316 /* if trace running, finish it */
1318 DBG("destructor stopping traces");
1320 result
= ltt_trace_stop("auto");
1322 ERR("ltt_trace_stop error");
1325 result
= ltt_trace_destroy("auto", 0);
1327 ERR("ltt_trace_destroy error");
1331 static int trace_recording(void)
1334 struct ust_trace
*trace
;
1338 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1345 ltt_unlock_traces();
1351 static int have_consumer(void)
1353 return !list_empty(&blocked_consumers
);
1357 int restarting_usleep(useconds_t usecs
)
1363 tv
.tv_nsec
= usecs
* 1000;
1366 result
= nanosleep(&tv
, &tv
);
1367 } while(result
== -1 && errno
== EINTR
);
1372 static void stop_listener()
1376 result
= pthread_cancel(listener_thread
);
1378 PERROR("pthread_cancel");
1380 result
= pthread_join(listener_thread
, NULL
);
1382 PERROR("pthread_join");
1386 /* This destructor keeps the process alive for a few seconds in order
1387 * to leave time to ustd to connect to its buffers. This is necessary
1388 * for programs whose execution is very short. It is also useful in all
1389 * programs when tracing is started close to the end of the program
1392 * FIXME: For now, this only works for the first trace created in a
1396 static void __attribute__((destructor
)) keepalive()
1398 if(trace_recording() && buffers_to_export
) {
1400 DBG("Keeping process alive for consumer daemon...");
1401 while(buffers_to_export
) {
1402 const int interv
= 200000;
1403 restarting_usleep(interv
);
1406 if(total
>= 3000000) {
1407 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1411 DBG("Finally dying...");
1416 /* Ask the listener to stop and clean up. */
1420 void ust_potential_exec(void)
1422 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1429 /* Notify ust that there was a fork. This needs to be called inside
1430 * the new process, anytime a process whose memory is not shared with
1431 * the parent is created. If this function is not called, the events
1432 * of the new process will not be collected.
1434 * Signals should be disabled before the fork and reenabled only after
1435 * this call in order to guarantee tracing is not started before ust_fork()
1436 * sanitizes the new process.
1439 static void ust_fork(void)
1441 struct blocked_consumer
*bc
;
1442 struct blocked_consumer
*deletable_bc
= NULL
;
1445 /* FIXME: technically, the locks could have been taken before the fork */
1446 DBG("ust: forking");
1448 /* break lock if necessary */
1449 ltt_unlock_traces();
1451 ltt_trace_stop("auto");
1452 ltt_trace_destroy("auto", 1);
1453 /* Delete all active connections */
1454 ustcomm_close_all_connections(&ustcomm_app
.server
);
1456 /* Delete all blocked consumers */
1457 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1458 result
= close(bc
->fd_producer
);
1464 list_del(&bc
->list
);
1467 /* free app, keeping socket file */
1468 ustcomm_fini_app(&ustcomm_app
, 1);
1470 buffers_to_export
= 0;
1474 ltt_trace_setup("auto");
1475 result
= ltt_trace_set_type("auto", "ustrelay");
1477 ERR("ltt_trace_set_type failed");
1481 ltt_trace_alloc("auto");
1482 ltt_trace_start("auto");
1483 inform_consumer_daemon("auto");
1486 void ust_before_fork(ust_fork_info_t
*fork_info
)
1488 /* Disable signals. This is to avoid that the child
1489 * intervenes before it is properly setup for tracing. It is
1490 * safer to disable all signals, because then we know we are not
1491 * breaking anything by restoring the original mask.
1497 - only do this if tracing is active
1500 /* Disable signals */
1501 sigfillset(&all_sigs
);
1502 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1504 PERROR("sigprocmask");
1509 /* Don't call this function directly in a traced program */
1510 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1514 /* Restore signals */
1515 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1517 PERROR("sigprocmask");
1522 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1524 /* Reenable signals */
1525 ust_after_fork_common(fork_info
);
1528 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1530 /* First sanitize the child */
1533 /* Then reenable interrupts */
1534 ust_after_fork_common(fork_info
);