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 /* This should only be accessed by the constructor, before the creation
47 * of the listener, and then only by the listener.
51 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
53 static struct ustcomm_app ustcomm_app
;
55 struct tracecmd
{ /* no padding */
60 /* volatile because shared between the listener and the main thread */
61 volatile sig_atomic_t buffers_to_export
= 0;
64 /* size: the size of all the fields except size itself */
67 /* Only the necessary part of the payload is transferred. It
68 * may even be none of it.
73 struct consumer_channel
{
75 struct ltt_channel_struct
*chan
;
78 struct blocked_consumer
{
83 /* args to ustcomm_send_reply */
84 struct ustcomm_server server
;
85 struct ustcomm_source src
;
87 /* args to ust_buffers_get_subbuf */
88 struct ust_buffer
*buf
;
90 struct list_head list
;
93 static long long make_pidunique(void)
98 gettimeofday(&tv
, NULL
);
102 retval
|= tv
.tv_usec
;
107 static void print_markers(FILE *fp
)
109 struct marker_iter iter
;
112 marker_iter_reset(&iter
);
113 marker_iter_start(&iter
);
116 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
);
117 marker_iter_next(&iter
);
122 static int init_socket(void);
124 /* Ask the daemon to collect a trace called trace_name and being
125 * produced by this pid.
127 * The trace must be at least allocated. (It can also be started.)
128 * This is because _ltt_trace_find is used.
131 static void inform_consumer_daemon(const char *trace_name
)
134 struct ust_trace
*trace
;
135 pid_t pid
= getpid();
140 trace
= _ltt_trace_find(trace_name
);
142 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
146 for(i
=0; i
< trace
->nr_channels
; i
++) {
147 /* iterate on all cpus */
148 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
150 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
151 result
= ustcomm_request_consumer(pid
, buf
);
153 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
154 /* continue even if fail */
165 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
168 long consumed_old
= 0;
170 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
173 result
= read(bc
->fd_producer
, &inbuf
, 1);
180 DBG("listener: got messsage that a buffer ended");
182 res
= close(bc
->fd_producer
);
189 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
191 ERR("ustcomm_send_reply failed");
198 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
199 if(result
== -EAGAIN
) {
200 WARN("missed buffer?");
203 else if(result
< 0) {
204 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
206 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
207 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
209 ERR("ustcomm_send_reply failed");
220 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
222 struct blocked_consumer
*bc
;
224 list_for_each_entry(bc
, &blocked_consumers
, list
) {
225 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
230 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
234 sep
= rindex(channel_and_cpu
, '_');
237 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
243 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
246 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
249 struct ust_trace
*trace
;
250 char trace_name
[] = "auto";
252 char *channel_and_cpu
;
260 channel_and_cpu
= nth_token(recvbuf
, 1);
261 if(channel_and_cpu
== NULL
) {
262 ERR("cannot parse channel");
266 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
268 ERR("problem parsing channel name");
269 goto free_short_chan_name
;
273 trace
= _ltt_trace_find(trace_name
);
277 ERR("cannot find trace!");
279 goto free_short_chan_name
;
282 for(i
=0; i
<trace
->nr_channels
; i
++) {
283 struct ust_channel
*channel
= &trace
->channels
[i
];
284 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
286 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
289 // DBG("the shmid for the requested channel is %d", buf->shmid);
290 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
291 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
293 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
295 ERR("ustcomm_send_reply failed");
298 goto free_short_chan_name
;
309 ERR("channel not found (%s)", channel_and_cpu
);
312 free_short_chan_name
:
319 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
322 struct ust_trace
*trace
;
323 char trace_name
[] = "auto";
325 char *channel_and_cpu
;
331 DBG("get_n_subbufs");
333 channel_and_cpu
= nth_token(recvbuf
, 1);
334 if(channel_and_cpu
== NULL
) {
335 ERR("cannot parse channel");
339 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
341 ERR("problem parsing channel name");
342 goto free_short_chan_name
;
346 trace
= _ltt_trace_find(trace_name
);
350 ERR("cannot find trace!");
352 goto free_short_chan_name
;
355 for(i
=0; i
<trace
->nr_channels
; i
++) {
356 struct ust_channel
*channel
= &trace
->channels
[i
];
358 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
361 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
362 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
364 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
366 ERR("ustcomm_send_reply failed");
369 goto free_short_chan_name
;
378 ERR("unable to find channel");
381 free_short_chan_name
:
388 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
391 struct ust_trace
*trace
;
392 char trace_name
[] = "auto";
394 char *channel_and_cpu
;
400 DBG("get_subbuf_size");
402 channel_and_cpu
= nth_token(recvbuf
, 1);
403 if(channel_and_cpu
== NULL
) {
404 ERR("cannot parse channel");
408 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
410 ERR("problem parsing channel name");
411 goto free_short_chan_name
;
415 trace
= _ltt_trace_find(trace_name
);
419 ERR("cannot find trace!");
421 goto free_short_chan_name
;
424 for(i
=0; i
<trace
->nr_channels
; i
++) {
425 struct ust_channel
*channel
= &trace
->channels
[i
];
427 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
430 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
431 asprintf(&reply
, "%zd", channel
->subbuf_size
);
433 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
435 ERR("ustcomm_send_reply failed");
438 goto free_short_chan_name
;
447 ERR("unable to find channel");
450 free_short_chan_name
:
457 static unsigned int poweroftwo(unsigned int x
)
459 unsigned int power2
= 1;
460 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
465 while (power2
< x
&& power2
< hardcoded
)
471 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
473 char *channel_slash_size
;
474 char ch_name
[256]="";
475 unsigned int size
, power
;
477 struct ust_trace
*trace
;
478 char trace_name
[] = "auto";
482 DBG("set_subbuf_size");
484 channel_slash_size
= nth_token(recvbuf
, 1);
485 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
487 if(ch_name
== NULL
) {
488 ERR("cannot parse channel");
492 power
= poweroftwo(size
);
494 WARN("using the next 2^n = %u\n", power
);
497 trace
= _ltt_trace_find_setup(trace_name
);
499 ERR("cannot find trace!");
504 for(i
= 0; i
< trace
->nr_channels
; i
++) {
505 struct ust_channel
*channel
= &trace
->channels
[i
];
507 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
509 channel
->subbuf_size
= power
;
510 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
517 ERR("unable to find channel");
525 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
527 char *channel_slash_num
;
528 char ch_name
[256]="";
531 struct ust_trace
*trace
;
532 char trace_name
[] = "auto";
536 DBG("set_subbuf_num");
538 channel_slash_num
= nth_token(recvbuf
, 1);
539 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
541 if(ch_name
== NULL
) {
542 ERR("cannot parse channel");
546 ERR("subbuffer count should be greater than 2");
551 trace
= _ltt_trace_find_setup(trace_name
);
553 ERR("cannot find trace!");
558 for(i
= 0; i
< trace
->nr_channels
; i
++) {
559 struct ust_channel
*channel
= &trace
->channels
[i
];
561 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
563 channel
->subbuf_cnt
= num
;
564 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
571 ERR("unable to find channel");
579 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
582 struct ust_trace
*trace
;
583 char trace_name
[] = "auto";
585 char *channel_and_cpu
;
592 channel_and_cpu
= nth_token(recvbuf
, 1);
593 if(channel_and_cpu
== NULL
) {
594 ERR("cannot parse channel");
598 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
600 ERR("problem parsing channel name");
601 goto free_short_chan_name
;
605 trace
= _ltt_trace_find(trace_name
);
610 DBG("Cannot find trace. It was likely destroyed by the user.");
611 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
613 ERR("ustcomm_send_reply failed");
621 for(i
=0; i
<trace
->nr_channels
; i
++) {
622 struct ust_channel
*channel
= &trace
->channels
[i
];
624 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
625 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
626 struct blocked_consumer
*bc
;
630 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
632 ERR("malloc returned NULL");
635 bc
->fd_consumer
= src
->fd
;
636 bc
->fd_producer
= buf
->data_ready_fd_read
;
639 bc
->server
= ustcomm_app
.server
;
641 list_add(&bc
->list
, &blocked_consumers
);
643 /* Being here is the proof the daemon has mapped the buffer in its
644 * memory. We may now decrement buffers_to_export.
646 if(uatomic_read(&buf
->consumed
) == 0) {
647 DBG("decrementing buffers_to_export");
655 ERR("unable to find channel");
661 free_short_chan_name
:
668 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
671 struct ust_trace
*trace
;
672 char trace_name
[] = "auto";
674 char *channel_and_cpu
;
680 char *consumed_old_str
;
686 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
687 if(channel_and_cpu
== NULL
) {
688 ERR("cannot parse channel");
693 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
694 if(consumed_old_str
== NULL
) {
695 ERR("cannot parse consumed_old");
697 goto free_channel_and_cpu
;
699 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
700 if(*endptr
!= '\0') {
701 ERR("invalid value for consumed_old");
703 goto free_consumed_old_str
;
706 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
708 ERR("problem parsing channel name");
710 goto free_short_chan_name
;
714 trace
= _ltt_trace_find(trace_name
);
717 DBG("Cannot find trace. It was likely destroyed by the user.");
718 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
720 ERR("ustcomm_send_reply failed");
728 for(i
=0; i
<trace
->nr_channels
; i
++) {
729 struct ust_channel
*channel
= &trace
->channels
[i
];
731 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
732 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
736 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
738 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
739 asprintf(&reply
, "%s", "ERROR");
742 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
743 asprintf(&reply
, "%s", "OK");
746 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
748 ERR("ustcomm_send_reply failed");
759 ERR("unable to find channel");
764 free_short_chan_name
:
766 free_consumed_old_str
:
767 free(consumed_old_str
);
768 free_channel_and_cpu
:
769 free(channel_and_cpu
);
775 static void listener_cleanup(void *ptr
)
777 ustcomm_fini_app(&ustcomm_app
, 0);
780 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
783 char trace_name
[] = "auto";
784 char trace_type
[] = "ustrelay";
787 DBG("received a message! it's: %s", recvbuf
);
788 len
= strlen(recvbuf
);
790 if(!strcmp(recvbuf
, "print_markers")) {
791 print_markers(stderr
);
793 else if(!strcmp(recvbuf
, "list_markers")) {
798 fp
= open_memstream(&ptr
, &size
);
802 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
806 else if(!strcmp(recvbuf
, "start")) {
807 /* start is an operation that setups the trace, allocates it and starts it */
808 result
= ltt_trace_setup(trace_name
);
810 ERR("ltt_trace_setup failed");
814 result
= ltt_trace_set_type(trace_name
, trace_type
);
816 ERR("ltt_trace_set_type failed");
820 result
= ltt_trace_alloc(trace_name
);
822 ERR("ltt_trace_alloc failed");
826 inform_consumer_daemon(trace_name
);
828 result
= ltt_trace_start(trace_name
);
830 ERR("ltt_trace_start failed");
834 else if(!strcmp(recvbuf
, "trace_setup")) {
837 result
= ltt_trace_setup(trace_name
);
839 ERR("ltt_trace_setup failed");
843 result
= ltt_trace_set_type(trace_name
, trace_type
);
845 ERR("ltt_trace_set_type failed");
849 else if(!strcmp(recvbuf
, "trace_alloc")) {
852 result
= ltt_trace_alloc(trace_name
);
854 ERR("ltt_trace_alloc failed");
857 inform_consumer_daemon(trace_name
);
859 else if(!strcmp(recvbuf
, "trace_create")) {
862 result
= ltt_trace_setup(trace_name
);
864 ERR("ltt_trace_setup failed");
868 result
= ltt_trace_set_type(trace_name
, trace_type
);
870 ERR("ltt_trace_set_type failed");
874 else if(!strcmp(recvbuf
, "trace_start")) {
877 result
= ltt_trace_alloc(trace_name
);
879 ERR("ltt_trace_alloc failed");
883 inform_consumer_daemon(trace_name
);
886 result
= ltt_trace_start(trace_name
);
888 ERR("ltt_trace_start failed");
892 else if(!strcmp(recvbuf
, "trace_stop")) {
895 result
= ltt_trace_stop(trace_name
);
897 ERR("ltt_trace_stop failed");
901 else if(!strcmp(recvbuf
, "trace_destroy")) {
903 DBG("trace destroy");
905 result
= ltt_trace_destroy(trace_name
, 0);
907 ERR("ltt_trace_destroy failed");
911 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
912 do_cmd_get_shmid(recvbuf
, src
);
914 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
915 do_cmd_get_n_subbufs(recvbuf
, src
);
917 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
918 do_cmd_get_subbuf_size(recvbuf
, src
);
920 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
923 libfile
= nth_token(recvbuf
, 1);
925 DBG("load_probe_lib loading %s", libfile
);
929 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
930 do_cmd_get_subbuffer(recvbuf
, src
);
932 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
933 do_cmd_put_subbuffer(recvbuf
, src
);
935 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
936 do_cmd_set_subbuf_size(recvbuf
, src
);
938 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
939 do_cmd_set_subbuf_num(recvbuf
, src
);
941 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
942 char *channel_slash_name
= nth_token(recvbuf
, 1);
943 char channel_name
[256]="";
944 char marker_name
[256]="";
946 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
948 if(channel_name
== NULL
|| marker_name
== NULL
) {
949 WARN("invalid marker name");
953 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
955 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
958 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
959 char *channel_slash_name
= nth_token(recvbuf
, 1);
963 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
965 if(marker_name
== NULL
) {
968 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
970 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
973 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
976 asprintf(&reply
, "%lld", pidunique
);
978 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
980 ERR("listener: get_pidunique: ustcomm_send_reply failed");
987 ERR("unable to parse message: %s", recvbuf
);
995 void *listener_main(void *p
)
1001 pthread_cleanup_push(listener_cleanup
, NULL
);
1004 struct mpentries mpent
;
1006 multipoll_init(&mpent
);
1008 blocked_consumers_add_to_mp(&mpent
);
1009 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1011 result
= multipoll_poll(&mpent
, -1);
1013 ERR("error in multipoll_poll");
1016 multipoll_destroy(&mpent
);
1019 pthread_cleanup_pop(1);
1022 /* These should only be accessed in the parent thread,
1025 static volatile sig_atomic_t have_listener
= 0;
1026 static pthread_t listener_thread
;
1028 void create_listener(void)
1033 WARN("not creating listener because we already had one");
1037 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1039 PERROR("pthread_create");
1045 static int init_socket(void)
1047 return ustcomm_init_app(getpid(), &ustcomm_app
);
1050 #define AUTOPROBE_DISABLED 0
1051 #define AUTOPROBE_ENABLE_ALL 1
1052 #define AUTOPROBE_ENABLE_REGEX 2
1053 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1054 static regex_t autoprobe_regex
;
1056 static void auto_probe_connect(struct marker
*m
)
1060 char* concat_name
= NULL
;
1061 const char *probe_name
= "default";
1063 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1066 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1067 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1069 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1070 m
->channel
, m
->name
);
1073 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1080 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1081 if(result
&& result
!= -EEXIST
)
1082 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1084 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1088 static void __attribute__((constructor
)) init()
1091 char* autoprobe_val
= NULL
;
1093 /* Assign the pidunique, to be able to differentiate the processes with same
1094 * pid, (before and after an exec).
1096 pidunique
= make_pidunique();
1098 DBG("Tracectl constructor");
1100 result
= init_socket();
1102 ERR("init_socket error");
1108 autoprobe_val
= getenv("UST_AUTOPROBE");
1110 struct marker_iter iter
;
1112 DBG("Autoprobe enabled.");
1114 /* Ensure markers are initialized */
1117 /* Ensure marker control is initialized, for the probe */
1118 init_marker_control();
1120 /* first, set the callback that will connect the
1121 * probe on new markers
1123 if(autoprobe_val
[0] == '/') {
1124 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1128 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1129 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1130 /* don't crash the application just for this */
1133 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1137 /* just enable all instrumentation */
1138 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1141 marker_set_new_marker_cb(auto_probe_connect
);
1143 /* Now, connect the probes that were already registered. */
1144 marker_iter_reset(&iter
);
1145 marker_iter_start(&iter
);
1147 DBG("now iterating on markers already registered");
1148 while(iter
.marker
) {
1149 DBG("now iterating on marker %s", iter
.marker
->name
);
1150 auto_probe_connect(iter
.marker
);
1151 marker_iter_next(&iter
);
1155 if(getenv("UST_TRACE")) {
1156 char trace_name
[] = "auto";
1157 char trace_type
[] = "ustrelay";
1159 DBG("starting early tracing");
1161 /* Ensure marker control is initialized */
1162 init_marker_control();
1164 /* Ensure markers are initialized */
1167 /* Ensure buffers are initialized, for the transport to be available.
1168 * We are about to set a trace type and it will fail without this.
1170 init_ustrelay_transport();
1172 /* FIXME: When starting early tracing (here), depending on the
1173 * order of constructors, it is very well possible some marker
1174 * sections are not yet registered. Because of this, some
1175 * channels may not be registered. Yet, we are about to ask the
1176 * daemon to collect the channels. Channels which are not yet
1177 * registered will not be collected.
1179 * Currently, in LTTng, there is no way to add a channel after
1180 * trace start. The reason for this is that it induces complex
1181 * concurrency issues on the trace structures, which can only
1182 * be resolved using RCU. This has not been done yet. As a
1183 * workaround, we are forcing the registration of the "ust"
1184 * channel here. This is the only channel (apart from metadata)
1185 * that can be reliably used in early tracing.
1187 * Non-early tracing does not have this problem and can use
1188 * arbitrary channel names.
1190 ltt_channels_register("ust");
1192 result
= ltt_trace_setup(trace_name
);
1194 ERR("ltt_trace_setup failed");
1198 result
= ltt_trace_set_type(trace_name
, trace_type
);
1200 ERR("ltt_trace_set_type failed");
1204 result
= ltt_trace_alloc(trace_name
);
1206 ERR("ltt_trace_alloc failed");
1210 result
= ltt_trace_start(trace_name
);
1212 ERR("ltt_trace_start failed");
1216 /* Do this after the trace is started in order to avoid creating confusion
1217 * if the trace fails to start. */
1218 inform_consumer_daemon(trace_name
);
1224 /* should decrementally destroy stuff if error */
1228 /* This is only called if we terminate normally, not with an unhandled signal,
1229 * so we cannot rely on it. However, for now, LTTV requires that the header of
1230 * the last sub-buffer contain a valid end time for the trace. This is done
1231 * automatically only when the trace is properly stopped.
1233 * If the traced program crashed, it is always possible to manually add the
1234 * right value in the header, or to open the trace in text mode.
1236 * FIXME: Fix LTTV so it doesn't need this.
1239 static void destroy_traces(void)
1243 /* if trace running, finish it */
1245 DBG("destructor stopping traces");
1247 result
= ltt_trace_stop("auto");
1249 ERR("ltt_trace_stop error");
1252 result
= ltt_trace_destroy("auto", 0);
1254 ERR("ltt_trace_destroy error");
1258 static int trace_recording(void)
1261 struct ust_trace
*trace
;
1265 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1272 ltt_unlock_traces();
1278 static int have_consumer(void)
1280 return !list_empty(&blocked_consumers
);
1284 int restarting_usleep(useconds_t usecs
)
1290 tv
.tv_nsec
= usecs
* 1000;
1293 result
= nanosleep(&tv
, &tv
);
1294 } while(result
== -1 && errno
== EINTR
);
1299 static void stop_listener()
1303 result
= pthread_cancel(listener_thread
);
1305 ERR("pthread_cancel: %s", strerror(result
));
1307 result
= pthread_join(listener_thread
, NULL
);
1309 ERR("pthread_join: %s", strerror(result
));
1313 /* This destructor keeps the process alive for a few seconds in order
1314 * to leave time to ustd to connect to its buffers. This is necessary
1315 * for programs whose execution is very short. It is also useful in all
1316 * programs when tracing is started close to the end of the program
1319 * FIXME: For now, this only works for the first trace created in a
1323 static void __attribute__((destructor
)) keepalive()
1325 if(trace_recording() && buffers_to_export
) {
1327 DBG("Keeping process alive for consumer daemon...");
1328 while(buffers_to_export
) {
1329 const int interv
= 200000;
1330 restarting_usleep(interv
);
1333 if(total
>= 3000000) {
1334 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1338 DBG("Finally dying...");
1343 /* Ask the listener to stop and clean up. */
1347 void ust_potential_exec(void)
1349 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1356 /* Notify ust that there was a fork. This needs to be called inside
1357 * the new process, anytime a process whose memory is not shared with
1358 * the parent is created. If this function is not called, the events
1359 * of the new process will not be collected.
1361 * Signals should be disabled before the fork and reenabled only after
1362 * this call in order to guarantee tracing is not started before ust_fork()
1363 * sanitizes the new process.
1366 static void ust_fork(void)
1368 struct blocked_consumer
*bc
;
1369 struct blocked_consumer
*deletable_bc
= NULL
;
1372 /* FIXME: technically, the locks could have been taken before the fork */
1373 DBG("ust: forking");
1375 /* break lock if necessary */
1376 ltt_unlock_traces();
1378 ltt_trace_stop("auto");
1379 ltt_trace_destroy("auto", 1);
1380 /* Delete all active connections */
1381 ustcomm_close_all_connections(&ustcomm_app
.server
);
1383 /* Delete all blocked consumers */
1384 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1385 result
= close(bc
->fd_producer
);
1391 list_del(&bc
->list
);
1394 /* free app, keeping socket file */
1395 ustcomm_fini_app(&ustcomm_app
, 1);
1397 buffers_to_export
= 0;
1401 ltt_trace_setup("auto");
1402 result
= ltt_trace_set_type("auto", "ustrelay");
1404 ERR("ltt_trace_set_type failed");
1408 ltt_trace_alloc("auto");
1409 ltt_trace_start("auto");
1410 inform_consumer_daemon("auto");
1413 void ust_before_fork(ust_fork_info_t
*fork_info
)
1415 /* Disable signals. This is to avoid that the child
1416 * intervenes before it is properly setup for tracing. It is
1417 * safer to disable all signals, because then we know we are not
1418 * breaking anything by restoring the original mask.
1424 - only do this if tracing is active
1427 /* Disable signals */
1428 sigfillset(&all_sigs
);
1429 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1431 PERROR("sigprocmask");
1436 /* Don't call this function directly in a traced program */
1437 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1441 /* Restore signals */
1442 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1444 PERROR("sigprocmask");
1449 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1451 /* Reenable signals */
1452 ust_after_fork_common(fork_info
);
1455 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1457 /* First sanitize the child */
1460 /* Then reenable interrupts */
1461 ust_after_fork_common(fork_info
);