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);
126 /* Ask the daemon to collect a trace called trace_name and being
127 * produced by this pid.
129 * The trace must be at least allocated. (It can also be started.)
130 * This is because _ltt_trace_find is used.
133 static void inform_consumer_daemon(const char *trace_name
)
136 struct ust_trace
*trace
;
137 pid_t pid
= getpid();
142 trace
= _ltt_trace_find(trace_name
);
144 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
148 for(i
=0; i
< trace
->nr_channels
; i
++) {
149 /* iterate on all cpus */
150 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
152 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
153 result
= ustcomm_request_consumer(pid
, buf
);
155 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
156 /* continue even if fail */
167 int process_blkd_consumer_act(void *priv
, int fd
, short events
)
170 long consumed_old
= 0;
172 struct blocked_consumer
*bc
= (struct blocked_consumer
*) priv
;
175 result
= read(bc
->fd_producer
, &inbuf
, 1);
182 DBG("listener: got messsage that a buffer ended");
184 res
= close(bc
->fd_producer
);
191 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
193 ERR("ustcomm_send_reply failed");
200 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
201 if(result
== -EAGAIN
) {
202 WARN("missed buffer?");
205 else if(result
< 0) {
206 ERR("ust_buffers_get_subbuf: error: %s", strerror(-result
));
208 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
209 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
211 ERR("ustcomm_send_reply failed");
222 void blocked_consumers_add_to_mp(struct mpentries
*ent
)
224 struct blocked_consumer
*bc
;
226 list_for_each_entry(bc
, &blocked_consumers
, list
) {
227 multipoll_add(ent
, bc
->fd_producer
, POLLIN
, process_blkd_consumer_act
, bc
, NULL
);
232 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
236 sep
= rindex(channel_and_cpu
, '_');
239 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
245 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
248 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
251 struct ust_trace
*trace
;
252 char trace_name
[] = "auto";
254 char *channel_and_cpu
;
262 channel_and_cpu
= nth_token(recvbuf
, 1);
263 if(channel_and_cpu
== NULL
) {
264 ERR("cannot parse channel");
268 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
270 ERR("problem parsing channel name");
271 goto free_short_chan_name
;
275 trace
= _ltt_trace_find(trace_name
);
279 ERR("cannot find trace!");
281 goto free_short_chan_name
;
284 for(i
=0; i
<trace
->nr_channels
; i
++) {
285 struct ust_channel
*channel
= &trace
->channels
[i
];
286 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
288 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
291 // DBG("the shmid for the requested channel is %d", buf->shmid);
292 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
293 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
295 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
297 ERR("ustcomm_send_reply failed");
300 goto free_short_chan_name
;
311 ERR("channel not found (%s)", channel_and_cpu
);
314 free_short_chan_name
:
321 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
324 struct ust_trace
*trace
;
325 char trace_name
[] = "auto";
327 char *channel_and_cpu
;
333 DBG("get_n_subbufs");
335 channel_and_cpu
= nth_token(recvbuf
, 1);
336 if(channel_and_cpu
== NULL
) {
337 ERR("cannot parse channel");
341 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
343 ERR("problem parsing channel name");
344 goto free_short_chan_name
;
348 trace
= _ltt_trace_find(trace_name
);
352 ERR("cannot find trace!");
354 goto free_short_chan_name
;
357 for(i
=0; i
<trace
->nr_channels
; i
++) {
358 struct ust_channel
*channel
= &trace
->channels
[i
];
360 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
363 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
364 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
366 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
368 ERR("ustcomm_send_reply failed");
371 goto free_short_chan_name
;
380 ERR("unable to find channel");
383 free_short_chan_name
:
390 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
393 struct ust_trace
*trace
;
394 char trace_name
[] = "auto";
396 char *channel_and_cpu
;
402 DBG("get_subbuf_size");
404 channel_and_cpu
= nth_token(recvbuf
, 1);
405 if(channel_and_cpu
== NULL
) {
406 ERR("cannot parse channel");
410 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
412 ERR("problem parsing channel name");
413 goto free_short_chan_name
;
417 trace
= _ltt_trace_find(trace_name
);
421 ERR("cannot find trace!");
423 goto free_short_chan_name
;
426 for(i
=0; i
<trace
->nr_channels
; i
++) {
427 struct ust_channel
*channel
= &trace
->channels
[i
];
429 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
432 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
433 asprintf(&reply
, "%zd", channel
->subbuf_size
);
435 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
437 ERR("ustcomm_send_reply failed");
440 goto free_short_chan_name
;
449 ERR("unable to find channel");
452 free_short_chan_name
:
459 static unsigned int poweroftwo(unsigned int x
)
461 unsigned int power2
= 1;
462 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
467 while (power2
< x
&& power2
< hardcoded
)
473 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
475 char *channel_slash_size
;
476 char ch_name
[256]="";
477 unsigned int size
, power
;
479 struct ust_trace
*trace
;
480 char trace_name
[] = "auto";
484 DBG("set_subbuf_size");
486 channel_slash_size
= nth_token(recvbuf
, 1);
487 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
489 if(ch_name
== NULL
) {
490 ERR("cannot parse channel");
494 power
= poweroftwo(size
);
496 WARN("using the next 2^n = %u\n", power
);
499 trace
= _ltt_trace_find_setup(trace_name
);
501 ERR("cannot find trace!");
506 for(i
= 0; i
< trace
->nr_channels
; i
++) {
507 struct ust_channel
*channel
= &trace
->channels
[i
];
509 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
511 channel
->subbuf_size
= power
;
512 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
519 ERR("unable to find channel");
527 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
529 char *channel_slash_num
;
530 char ch_name
[256]="";
533 struct ust_trace
*trace
;
534 char trace_name
[] = "auto";
538 DBG("set_subbuf_num");
540 channel_slash_num
= nth_token(recvbuf
, 1);
541 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
543 if(ch_name
== NULL
) {
544 ERR("cannot parse channel");
548 ERR("subbuffer count should be greater than 2");
553 trace
= _ltt_trace_find_setup(trace_name
);
555 ERR("cannot find trace!");
560 for(i
= 0; i
< trace
->nr_channels
; i
++) {
561 struct ust_channel
*channel
= &trace
->channels
[i
];
563 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
565 channel
->subbuf_cnt
= num
;
566 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
573 ERR("unable to find channel");
581 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
584 struct ust_trace
*trace
;
585 char trace_name
[] = "auto";
587 char *channel_and_cpu
;
594 channel_and_cpu
= nth_token(recvbuf
, 1);
595 if(channel_and_cpu
== NULL
) {
596 ERR("cannot parse channel");
600 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
602 ERR("problem parsing channel name");
603 goto free_short_chan_name
;
607 trace
= _ltt_trace_find(trace_name
);
612 DBG("Cannot find trace. It was likely destroyed by the user.");
613 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
615 ERR("ustcomm_send_reply failed");
623 for(i
=0; i
<trace
->nr_channels
; i
++) {
624 struct ust_channel
*channel
= &trace
->channels
[i
];
626 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
627 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
628 struct blocked_consumer
*bc
;
632 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
634 ERR("malloc returned NULL");
637 bc
->fd_consumer
= src
->fd
;
638 bc
->fd_producer
= buf
->data_ready_fd_read
;
641 bc
->server
= ustcomm_app
.server
;
643 list_add(&bc
->list
, &blocked_consumers
);
645 /* Being here is the proof the daemon has mapped the buffer in its
646 * memory. We may now decrement buffers_to_export.
648 if(uatomic_read(&buf
->consumed
) == 0) {
649 DBG("decrementing buffers_to_export");
657 ERR("unable to find channel");
663 free_short_chan_name
:
670 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
673 struct ust_trace
*trace
;
674 char trace_name
[] = "auto";
676 char *channel_and_cpu
;
682 char *consumed_old_str
;
688 channel_and_cpu
= strdup(nth_token(recvbuf
, 1));
689 if(channel_and_cpu
== NULL
) {
690 ERR("cannot parse channel");
695 consumed_old_str
= strdup(nth_token(recvbuf
, 2));
696 if(consumed_old_str
== NULL
) {
697 ERR("cannot parse consumed_old");
699 goto free_channel_and_cpu
;
701 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
702 if(*endptr
!= '\0') {
703 ERR("invalid value for consumed_old");
705 goto free_consumed_old_str
;
708 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
710 ERR("problem parsing channel name");
712 goto free_short_chan_name
;
716 trace
= _ltt_trace_find(trace_name
);
719 DBG("Cannot find trace. It was likely destroyed by the user.");
720 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
722 ERR("ustcomm_send_reply failed");
730 for(i
=0; i
<trace
->nr_channels
; i
++) {
731 struct ust_channel
*channel
= &trace
->channels
[i
];
733 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
734 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
738 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
740 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
741 asprintf(&reply
, "%s", "ERROR");
744 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
745 asprintf(&reply
, "%s", "OK");
748 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
750 ERR("ustcomm_send_reply failed");
761 ERR("unable to find channel");
766 free_short_chan_name
:
768 free_consumed_old_str
:
769 free(consumed_old_str
);
770 free_channel_and_cpu
:
771 free(channel_and_cpu
);
777 static void listener_cleanup(void *ptr
)
779 ustcomm_fini_app(&ustcomm_app
, 0);
782 int process_client_cmd(char *recvbuf
, struct ustcomm_source
*src
)
785 char trace_name
[] = "auto";
786 char trace_type
[] = "ustrelay";
789 DBG("received a message! it's: %s", recvbuf
);
790 len
= strlen(recvbuf
);
792 if(!strcmp(recvbuf
, "print_markers")) {
793 print_markers(stderr
);
795 else if(!strcmp(recvbuf
, "list_markers")) {
800 fp
= open_memstream(&ptr
, &size
);
804 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, src
);
808 else if(!strcmp(recvbuf
, "start")) {
809 /* start is an operation that setups the trace, allocates it and starts it */
810 result
= ltt_trace_setup(trace_name
);
812 ERR("ltt_trace_setup failed");
816 result
= ltt_trace_set_type(trace_name
, trace_type
);
818 ERR("ltt_trace_set_type failed");
822 result
= ltt_trace_alloc(trace_name
);
824 ERR("ltt_trace_alloc failed");
828 inform_consumer_daemon(trace_name
);
830 result
= ltt_trace_start(trace_name
);
832 ERR("ltt_trace_start failed");
836 else if(!strcmp(recvbuf
, "trace_setup")) {
839 result
= ltt_trace_setup(trace_name
);
841 ERR("ltt_trace_setup failed");
845 result
= ltt_trace_set_type(trace_name
, trace_type
);
847 ERR("ltt_trace_set_type failed");
851 else if(!strcmp(recvbuf
, "trace_alloc")) {
854 result
= ltt_trace_alloc(trace_name
);
856 ERR("ltt_trace_alloc failed");
859 inform_consumer_daemon(trace_name
);
861 else if(!strcmp(recvbuf
, "trace_create")) {
864 result
= ltt_trace_setup(trace_name
);
866 ERR("ltt_trace_setup failed");
870 result
= ltt_trace_set_type(trace_name
, trace_type
);
872 ERR("ltt_trace_set_type failed");
876 else if(!strcmp(recvbuf
, "trace_start")) {
879 result
= ltt_trace_alloc(trace_name
);
881 ERR("ltt_trace_alloc failed");
885 inform_consumer_daemon(trace_name
);
888 result
= ltt_trace_start(trace_name
);
890 ERR("ltt_trace_start failed");
894 else if(!strcmp(recvbuf
, "trace_stop")) {
897 result
= ltt_trace_stop(trace_name
);
899 ERR("ltt_trace_stop failed");
903 else if(!strcmp(recvbuf
, "trace_destroy")) {
905 DBG("trace destroy");
907 result
= ltt_trace_destroy(trace_name
, 0);
909 ERR("ltt_trace_destroy failed");
913 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
914 do_cmd_get_shmid(recvbuf
, src
);
916 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
917 do_cmd_get_n_subbufs(recvbuf
, src
);
919 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
920 do_cmd_get_subbuf_size(recvbuf
, src
);
922 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
925 libfile
= nth_token(recvbuf
, 1);
927 DBG("load_probe_lib loading %s", libfile
);
931 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
932 do_cmd_get_subbuffer(recvbuf
, src
);
934 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
935 do_cmd_put_subbuffer(recvbuf
, src
);
937 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
938 do_cmd_set_subbuf_size(recvbuf
, src
);
940 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
941 do_cmd_set_subbuf_num(recvbuf
, src
);
943 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
944 char *channel_slash_name
= nth_token(recvbuf
, 1);
945 char channel_name
[256]="";
946 char marker_name
[256]="";
948 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
950 if(channel_name
== NULL
|| marker_name
== NULL
) {
951 WARN("invalid marker name");
955 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
957 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
960 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
961 char *channel_slash_name
= nth_token(recvbuf
, 1);
965 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
967 if(marker_name
== NULL
) {
970 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
972 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
975 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
978 asprintf(&reply
, "%lld", pidunique
);
980 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
982 ERR("listener: get_pidunique: ustcomm_send_reply failed");
989 ERR("unable to parse message: %s", recvbuf
);
997 void *listener_main(void *p
)
1003 pthread_cleanup_push(listener_cleanup
, NULL
);
1006 struct mpentries mpent
;
1008 multipoll_init(&mpent
);
1010 blocked_consumers_add_to_mp(&mpent
);
1011 ustcomm_mp_add_app_clients(&mpent
, &ustcomm_app
, process_client_cmd
);
1013 result
= multipoll_poll(&mpent
, -1);
1015 ERR("error in multipoll_poll");
1018 multipoll_destroy(&mpent
);
1021 pthread_cleanup_pop(1);
1024 /* These should only be accessed in the parent thread,
1027 static volatile sig_atomic_t have_listener
= 0;
1028 static pthread_t listener_thread
;
1030 void create_listener(void)
1035 WARN("not creating listener because we already had one");
1039 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1041 PERROR("pthread_create");
1047 static int init_socket(void)
1049 return ustcomm_init_app(getpid(), &ustcomm_app
);
1052 #define AUTOPROBE_DISABLED 0
1053 #define AUTOPROBE_ENABLE_ALL 1
1054 #define AUTOPROBE_ENABLE_REGEX 2
1055 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1056 static regex_t autoprobe_regex
;
1058 static void auto_probe_connect(struct marker
*m
)
1062 char* concat_name
= NULL
;
1063 const char *probe_name
= "default";
1065 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1068 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1069 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1071 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1072 m
->channel
, m
->name
);
1075 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1082 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1083 if(result
&& result
!= -EEXIST
)
1084 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1086 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1090 static void __attribute__((constructor
)) init()
1093 char* autoprobe_val
= NULL
;
1095 /* Assign the pidunique, to be able to differentiate the processes with same
1096 * pid, (before and after an exec).
1098 pidunique
= make_pidunique();
1100 DBG("Tracectl constructor");
1102 result
= init_socket();
1104 ERR("init_socket error");
1110 autoprobe_val
= getenv("UST_AUTOPROBE");
1112 struct marker_iter iter
;
1114 DBG("Autoprobe enabled.");
1116 /* Ensure markers are initialized */
1119 /* Ensure marker control is initialized, for the probe */
1120 init_marker_control();
1122 /* first, set the callback that will connect the
1123 * probe on new markers
1125 if(autoprobe_val
[0] == '/') {
1126 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1130 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1131 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1132 /* don't crash the application just for this */
1135 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1139 /* just enable all instrumentation */
1140 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1143 marker_set_new_marker_cb(auto_probe_connect
);
1145 /* Now, connect the probes that were already registered. */
1146 marker_iter_reset(&iter
);
1147 marker_iter_start(&iter
);
1149 DBG("now iterating on markers already registered");
1150 while(iter
.marker
) {
1151 DBG("now iterating on marker %s", iter
.marker
->name
);
1152 auto_probe_connect(iter
.marker
);
1153 marker_iter_next(&iter
);
1157 if(getenv("UST_TRACE")) {
1158 char trace_name
[] = "auto";
1159 char trace_type
[] = "ustrelay";
1161 DBG("starting early tracing");
1163 /* Ensure marker control is initialized */
1164 init_marker_control();
1166 /* Ensure markers are initialized */
1169 /* Ensure buffers are initialized, for the transport to be available.
1170 * We are about to set a trace type and it will fail without this.
1172 init_ustrelay_transport();
1174 /* FIXME: When starting early tracing (here), depending on the
1175 * order of constructors, it is very well possible some marker
1176 * sections are not yet registered. Because of this, some
1177 * channels may not be registered. Yet, we are about to ask the
1178 * daemon to collect the channels. Channels which are not yet
1179 * registered will not be collected.
1181 * Currently, in LTTng, there is no way to add a channel after
1182 * trace start. The reason for this is that it induces complex
1183 * concurrency issues on the trace structures, which can only
1184 * be resolved using RCU. This has not been done yet. As a
1185 * workaround, we are forcing the registration of the "ust"
1186 * channel here. This is the only channel (apart from metadata)
1187 * that can be reliably used in early tracing.
1189 * Non-early tracing does not have this problem and can use
1190 * arbitrary channel names.
1192 ltt_channels_register("ust");
1194 result
= ltt_trace_setup(trace_name
);
1196 ERR("ltt_trace_setup failed");
1200 result
= ltt_trace_set_type(trace_name
, trace_type
);
1202 ERR("ltt_trace_set_type failed");
1206 result
= ltt_trace_alloc(trace_name
);
1208 ERR("ltt_trace_alloc failed");
1212 result
= ltt_trace_start(trace_name
);
1214 ERR("ltt_trace_start failed");
1218 /* Do this after the trace is started in order to avoid creating confusion
1219 * if the trace fails to start. */
1220 inform_consumer_daemon(trace_name
);
1226 /* should decrementally destroy stuff if error */
1230 /* This is only called if we terminate normally, not with an unhandled signal,
1231 * so we cannot rely on it. However, for now, LTTV requires that the header of
1232 * the last sub-buffer contain a valid end time for the trace. This is done
1233 * automatically only when the trace is properly stopped.
1235 * If the traced program crashed, it is always possible to manually add the
1236 * right value in the header, or to open the trace in text mode.
1238 * FIXME: Fix LTTV so it doesn't need this.
1241 static void destroy_traces(void)
1245 /* if trace running, finish it */
1247 DBG("destructor stopping traces");
1249 result
= ltt_trace_stop("auto");
1251 ERR("ltt_trace_stop error");
1254 result
= ltt_trace_destroy("auto", 0);
1256 ERR("ltt_trace_destroy error");
1260 static int trace_recording(void)
1263 struct ust_trace
*trace
;
1267 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1274 ltt_unlock_traces();
1280 static int have_consumer(void)
1282 return !list_empty(&blocked_consumers
);
1286 int restarting_usleep(useconds_t usecs
)
1292 tv
.tv_nsec
= usecs
* 1000;
1295 result
= nanosleep(&tv
, &tv
);
1296 } while(result
== -1 && errno
== EINTR
);
1301 static void stop_listener()
1305 result
= pthread_cancel(listener_thread
);
1307 ERR("pthread_cancel: %s", strerror(result
));
1309 result
= pthread_join(listener_thread
, NULL
);
1311 ERR("pthread_join: %s", strerror(result
));
1315 /* This destructor keeps the process alive for a few seconds in order
1316 * to leave time to ustd to connect to its buffers. This is necessary
1317 * for programs whose execution is very short. It is also useful in all
1318 * programs when tracing is started close to the end of the program
1321 * FIXME: For now, this only works for the first trace created in a
1325 static void __attribute__((destructor
)) keepalive()
1327 if(trace_recording() && buffers_to_export
) {
1329 DBG("Keeping process alive for consumer daemon...");
1330 while(buffers_to_export
) {
1331 const int interv
= 200000;
1332 restarting_usleep(interv
);
1335 if(total
>= 3000000) {
1336 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1340 DBG("Finally dying...");
1345 /* Ask the listener to stop and clean up. */
1349 void ust_potential_exec(void)
1351 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1358 /* Notify ust that there was a fork. This needs to be called inside
1359 * the new process, anytime a process whose memory is not shared with
1360 * the parent is created. If this function is not called, the events
1361 * of the new process will not be collected.
1363 * Signals should be disabled before the fork and reenabled only after
1364 * this call in order to guarantee tracing is not started before ust_fork()
1365 * sanitizes the new process.
1368 static void ust_fork(void)
1370 struct blocked_consumer
*bc
;
1371 struct blocked_consumer
*deletable_bc
= NULL
;
1374 /* FIXME: technically, the locks could have been taken before the fork */
1375 DBG("ust: forking");
1377 /* break lock if necessary */
1378 ltt_unlock_traces();
1380 ltt_trace_stop("auto");
1381 ltt_trace_destroy("auto", 1);
1382 /* Delete all active connections */
1383 ustcomm_close_all_connections(&ustcomm_app
.server
);
1385 /* Delete all blocked consumers */
1386 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1387 result
= close(bc
->fd_producer
);
1393 list_del(&bc
->list
);
1396 /* free app, keeping socket file */
1397 ustcomm_fini_app(&ustcomm_app
, 1);
1399 buffers_to_export
= 0;
1403 ltt_trace_setup("auto");
1404 result
= ltt_trace_set_type("auto", "ustrelay");
1406 ERR("ltt_trace_set_type failed");
1410 ltt_trace_alloc("auto");
1411 ltt_trace_start("auto");
1412 inform_consumer_daemon("auto");
1415 void ust_before_fork(ust_fork_info_t
*fork_info
)
1417 /* Disable signals. This is to avoid that the child
1418 * intervenes before it is properly setup for tracing. It is
1419 * safer to disable all signals, because then we know we are not
1420 * breaking anything by restoring the original mask.
1426 - only do this if tracing is active
1429 /* Disable signals */
1430 sigfillset(&all_sigs
);
1431 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1433 PERROR("sigprocmask");
1438 /* Don't call this function directly in a traced program */
1439 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1443 /* Restore signals */
1444 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1446 PERROR("sigprocmask");
1451 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1453 /* Reenable signals */
1454 ust_after_fork_common(fork_info
);
1457 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1459 /* First sanitize the child */
1462 /* Then reenable interrupts */
1463 ust_after_fork_common(fork_info
);