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
22 #include <sys/types.h>
23 #include <sys/socket.h>
30 #include <ust/marker.h>
31 #include <ust/tracectl.h>
36 #include "marker-control.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 void process_blocked_consumers(void)
193 struct blocked_consumer
*bc
;
198 list_for_each_entry(bc
, &blocked_consumers
, list
) {
202 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
204 ERR("malloc returned NULL");
208 list_for_each_entry(bc
, &blocked_consumers
, list
) {
209 fds
[idx
].fd
= bc
->fd_producer
;
210 fds
[idx
].events
= POLLIN
;
211 bc
->tmp_poll_idx
= idx
;
215 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
222 list_for_each_entry(bc
, &blocked_consumers
, list
) {
223 if(fds
[bc
->tmp_poll_idx
].revents
) {
224 long consumed_old
= 0;
227 result
= read(bc
->fd_producer
, &inbuf
, 1);
235 close(bc
->fd_producer
);
239 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
241 ERR("ustcomm_send_reply failed");
248 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
249 if(result
== -EAGAIN
) {
250 WARN("missed buffer?");
253 else if(result
< 0) {
254 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result
));
256 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
257 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
259 ERR("ustcomm_send_reply failed");
271 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
275 sep
= rindex(channel_and_cpu
, '_');
278 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
284 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
287 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
290 struct ust_trace
*trace
;
291 char trace_name
[] = "auto";
293 char *channel_and_cpu
;
301 channel_and_cpu
= nth_token(recvbuf
, 1);
302 if(channel_and_cpu
== NULL
) {
303 ERR("cannot parse channel");
307 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
309 ERR("problem parsing channel name");
310 goto free_short_chan_name
;
314 trace
= _ltt_trace_find(trace_name
);
318 ERR("cannot find trace!");
320 goto free_short_chan_name
;
323 for(i
=0; i
<trace
->nr_channels
; i
++) {
324 struct ust_channel
*channel
= &trace
->channels
[i
];
325 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
327 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
330 // DBG("the shmid for the requested channel is %d", buf->shmid);
331 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
332 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
334 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
336 ERR("ustcomm_send_reply failed");
339 goto free_short_chan_name
;
350 ERR("channel not found (%s)", channel_and_cpu
);
353 free_short_chan_name
:
360 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
363 struct ust_trace
*trace
;
364 char trace_name
[] = "auto";
366 char *channel_and_cpu
;
372 DBG("get_n_subbufs");
374 channel_and_cpu
= nth_token(recvbuf
, 1);
375 if(channel_and_cpu
== NULL
) {
376 ERR("cannot parse channel");
380 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
382 ERR("problem parsing channel name");
383 goto free_short_chan_name
;
387 trace
= _ltt_trace_find(trace_name
);
391 ERR("cannot find trace!");
393 goto free_short_chan_name
;
396 for(i
=0; i
<trace
->nr_channels
; i
++) {
397 struct ust_channel
*channel
= &trace
->channels
[i
];
399 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
402 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
403 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
405 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
407 ERR("ustcomm_send_reply failed");
410 goto free_short_chan_name
;
419 ERR("unable to find channel");
422 free_short_chan_name
:
429 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
432 struct ust_trace
*trace
;
433 char trace_name
[] = "auto";
435 char *channel_and_cpu
;
441 DBG("get_subbuf_size");
443 channel_and_cpu
= nth_token(recvbuf
, 1);
444 if(channel_and_cpu
== NULL
) {
445 ERR("cannot parse channel");
449 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
451 ERR("problem parsing channel name");
452 goto free_short_chan_name
;
456 trace
= _ltt_trace_find(trace_name
);
460 ERR("cannot find trace!");
462 goto free_short_chan_name
;
465 for(i
=0; i
<trace
->nr_channels
; i
++) {
466 struct ust_channel
*channel
= &trace
->channels
[i
];
468 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
471 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
472 asprintf(&reply
, "%zd", channel
->subbuf_size
);
474 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
476 ERR("ustcomm_send_reply failed");
479 goto free_short_chan_name
;
488 ERR("unable to find channel");
491 free_short_chan_name
:
498 static unsigned int poweroftwo(unsigned int x
)
500 unsigned int power2
= 1;
501 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
506 while (power2
< x
&& power2
< hardcoded
)
512 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
514 char *channel_slash_size
;
515 char ch_name
[256]="";
516 unsigned int size
, power
;
518 struct ust_trace
*trace
;
519 char trace_name
[] = "auto";
523 DBG("set_subbuf_size");
525 channel_slash_size
= nth_token(recvbuf
, 1);
526 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
528 if(ch_name
== NULL
) {
529 ERR("cannot parse channel");
533 power
= poweroftwo(size
);
535 WARN("using the next 2^n = %u\n", power
);
538 trace
= _ltt_trace_find_setup(trace_name
);
540 ERR("cannot find trace!");
545 for(i
= 0; i
< trace
->nr_channels
; i
++) {
546 struct ust_channel
*channel
= &trace
->channels
[i
];
548 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
550 channel
->subbuf_size
= power
;
551 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
558 ERR("unable to find channel");
566 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
568 char *channel_slash_num
;
569 char ch_name
[256]="";
572 struct ust_trace
*trace
;
573 char trace_name
[] = "auto";
577 DBG("set_subbuf_num");
579 channel_slash_num
= nth_token(recvbuf
, 1);
580 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
582 if(ch_name
== NULL
) {
583 ERR("cannot parse channel");
587 ERR("subbuffer count should be greater than 2");
592 trace
= _ltt_trace_find_setup(trace_name
);
594 ERR("cannot find trace!");
599 for(i
= 0; i
< trace
->nr_channels
; i
++) {
600 struct ust_channel
*channel
= &trace
->channels
[i
];
602 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
604 channel
->subbuf_cnt
= num
;
605 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
612 ERR("unable to find channel");
620 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
623 struct ust_trace
*trace
;
624 char trace_name
[] = "auto";
626 char *channel_and_cpu
;
633 channel_and_cpu
= nth_token(recvbuf
, 1);
634 if(channel_and_cpu
== NULL
) {
635 ERR("cannot parse channel");
639 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
641 ERR("problem parsing channel name");
642 goto free_short_chan_name
;
646 trace
= _ltt_trace_find(trace_name
);
651 WARN("Cannot find trace. It was likely destroyed by the user.");
652 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
654 ERR("ustcomm_send_reply failed");
662 for(i
=0; i
<trace
->nr_channels
; i
++) {
663 struct ust_channel
*channel
= &trace
->channels
[i
];
665 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
666 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
667 struct blocked_consumer
*bc
;
671 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
673 ERR("malloc returned NULL");
676 bc
->fd_consumer
= src
->fd
;
677 bc
->fd_producer
= buf
->data_ready_fd_read
;
680 bc
->server
= ustcomm_app
.server
;
682 list_add(&bc
->list
, &blocked_consumers
);
684 /* Being here is the proof the daemon has mapped the buffer in its
685 * memory. We may now decrement buffers_to_export.
687 if(atomic_long_read(&buf
->consumed
) == 0) {
688 DBG("decrementing buffers_to_export");
696 ERR("unable to find channel");
702 free_short_chan_name
:
709 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
712 struct ust_trace
*trace
;
713 char trace_name
[] = "auto";
715 char *channel_and_cpu
;
721 char *consumed_old_str
;
727 channel_and_cpu
= strdup_malloc(nth_token(recvbuf
, 1));
728 if(channel_and_cpu
== NULL
) {
729 ERR("cannot parse channel");
734 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
735 if(consumed_old_str
== NULL
) {
736 ERR("cannot parse consumed_old");
738 goto free_channel_and_cpu
;
740 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
741 if(*endptr
!= '\0') {
742 ERR("invalid value for consumed_old");
744 goto free_consumed_old_str
;
747 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
749 ERR("problem parsing channel name");
751 goto free_short_chan_name
;
755 trace
= _ltt_trace_find(trace_name
);
758 WARN("Cannot find trace. It was likely destroyed by the user.");
759 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
761 ERR("ustcomm_send_reply failed");
769 for(i
=0; i
<trace
->nr_channels
; i
++) {
770 struct ust_channel
*channel
= &trace
->channels
[i
];
772 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
773 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
777 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
779 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
780 asprintf(&reply
, "%s", "ERROR");
783 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
784 asprintf(&reply
, "%s", "OK");
787 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
789 ERR("ustcomm_send_reply failed");
800 ERR("unable to find channel");
805 free_short_chan_name
:
807 free_consumed_old_str
:
808 free(consumed_old_str
);
809 free_channel_and_cpu
:
810 free(channel_and_cpu
);
816 void *listener_main(void *p
)
823 char trace_name
[] = "auto";
824 char trace_type
[] = "ustrelay";
827 struct ustcomm_source src
;
829 process_blocked_consumers();
831 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
833 WARN("error in ustcomm_app_recv_message");
836 else if(result
== 0) {
841 DBG("received a message! it's: %s", recvbuf
);
842 len
= strlen(recvbuf
);
844 if(!strcmp(recvbuf
, "print_markers")) {
845 print_markers(stderr
);
847 else if(!strcmp(recvbuf
, "list_markers")) {
852 fp
= open_memstream(&ptr
, &size
);
856 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
860 else if(!strcmp(recvbuf
, "start")) {
861 /* start is an operation that setups the trace, allocates it and starts it */
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 result
= ltt_trace_alloc(trace_name
);
876 ERR("ltt_trace_alloc failed");
880 inform_consumer_daemon(trace_name
);
882 result
= ltt_trace_start(trace_name
);
884 ERR("ltt_trace_start failed");
888 else if(!strcmp(recvbuf
, "trace_setup")) {
891 result
= ltt_trace_setup(trace_name
);
893 ERR("ltt_trace_setup failed");
897 result
= ltt_trace_set_type(trace_name
, trace_type
);
899 ERR("ltt_trace_set_type failed");
903 else if(!strcmp(recvbuf
, "trace_alloc")) {
906 result
= ltt_trace_alloc(trace_name
);
908 ERR("ltt_trace_alloc failed");
911 inform_consumer_daemon(trace_name
);
913 else if(!strcmp(recvbuf
, "trace_create")) {
916 result
= ltt_trace_setup(trace_name
);
918 ERR("ltt_trace_setup failed");
922 result
= ltt_trace_set_type(trace_name
, trace_type
);
924 ERR("ltt_trace_set_type failed");
928 else if(!strcmp(recvbuf
, "trace_start")) {
931 result
= ltt_trace_alloc(trace_name
);
933 ERR("ltt_trace_alloc failed");
937 inform_consumer_daemon(trace_name
);
940 result
= ltt_trace_start(trace_name
);
942 ERR("ltt_trace_start failed");
946 else if(!strcmp(recvbuf
, "trace_stop")) {
949 result
= ltt_trace_stop(trace_name
);
951 ERR("ltt_trace_stop failed");
955 else if(!strcmp(recvbuf
, "trace_destroy")) {
957 DBG("trace destroy");
959 result
= ltt_trace_destroy(trace_name
, 0);
961 ERR("ltt_trace_destroy failed");
965 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
966 do_cmd_get_shmid(recvbuf
, &src
);
968 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
969 do_cmd_get_n_subbufs(recvbuf
, &src
);
971 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
972 do_cmd_get_subbuf_size(recvbuf
, &src
);
974 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
977 libfile
= nth_token(recvbuf
, 1);
979 DBG("load_probe_lib loading %s", libfile
);
983 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
984 do_cmd_get_subbuffer(recvbuf
, &src
);
986 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
987 do_cmd_put_subbuffer(recvbuf
, &src
);
989 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
990 do_cmd_set_subbuf_size(recvbuf
, &src
);
992 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
993 do_cmd_set_subbuf_num(recvbuf
, &src
);
995 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
996 char *channel_slash_name
= nth_token(recvbuf
, 1);
997 char channel_name
[256]="";
998 char marker_name
[256]="";
1000 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1002 if(channel_name
== NULL
|| marker_name
== NULL
) {
1003 WARN("invalid marker name");
1007 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1009 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1012 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1013 char *channel_slash_name
= nth_token(recvbuf
, 1);
1017 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1019 if(marker_name
== NULL
) {
1022 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1024 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1027 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1030 asprintf(&reply
, "%lld", pidunique
);
1032 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
1034 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1040 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1041 // struct ust_trace *trace;
1042 // char trace_name[] = "auto";
1044 // char *channel_name;
1046 // DBG("get_notifications");
1048 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1049 // if(channel_name == NULL) {
1050 // ERR("put_subbuf_size: cannot parse channel");
1054 // ltt_lock_traces();
1055 // trace = _ltt_trace_find(trace_name);
1056 // ltt_unlock_traces();
1058 // if(trace == NULL) {
1059 // ERR("cannot find trace!");
1060 // return (void *)1;
1063 // for(i=0; i<trace->nr_channels; i++) {
1064 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1067 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1068 // struct rchan_buf *rbuf = rchan->buf;
1069 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1071 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1072 // if(result == -1) {
1073 // ERR("ustcomm_app_detach_client failed");
1077 // lttbuf->wake_consumer_arg = (void *) fd;
1081 // lttbuf->call_wake_consumer = 1;
1087 // free(channel_name);
1090 ERR("unable to parse message: %s", recvbuf
);
1098 volatile sig_atomic_t have_listener
= 0;
1100 void create_listener(void)
1103 static char listener_stack
[16384];
1110 WARN("not creating listener because we already had one");
1115 result
= clone((int (*)(void *)) listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
1122 pthread_create(&thread
, NULL
, listener_main
, NULL
);
1128 static int init_socket(void)
1130 return ustcomm_init_app(getpid(), &ustcomm_app
);
1133 #define AUTOPROBE_DISABLED 0
1134 #define AUTOPROBE_ENABLE_ALL 1
1135 #define AUTOPROBE_ENABLE_REGEX 2
1136 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1137 static regex_t autoprobe_regex
;
1139 static void auto_probe_connect(struct marker
*m
)
1143 char* concat_name
= NULL
;
1144 const char *probe_name
= "default";
1146 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1149 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1150 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1152 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1153 m
->channel
, m
->name
);
1156 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1163 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1164 if(result
&& result
!= -EEXIST
)
1165 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1167 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1171 static void __attribute__((constructor
)) init()
1174 char* autoprobe_val
= NULL
;
1176 /* Assign the pidunique, to be able to differentiate the processes with same
1177 * pid, (before and after an exec).
1179 pidunique
= make_pidunique();
1181 DBG("Tracectl constructor");
1183 result
= init_socket();
1185 ERR("init_socket error");
1191 autoprobe_val
= getenv("UST_AUTOPROBE");
1193 struct marker_iter iter
;
1195 DBG("Autoprobe enabled.");
1197 /* Ensure markers are initialized */
1200 /* Ensure marker control is initialized, for the probe */
1201 init_marker_control();
1203 /* first, set the callback that will connect the
1204 * probe on new markers
1206 if(autoprobe_val
[0] == '/') {
1207 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1211 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1212 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1213 /* don't crash the application just for this */
1216 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1220 /* just enable all instrumentation */
1221 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1224 marker_set_new_marker_cb(auto_probe_connect
);
1226 /* Now, connect the probes that were already registered. */
1227 marker_iter_reset(&iter
);
1228 marker_iter_start(&iter
);
1230 DBG("now iterating on markers already registered");
1231 while(iter
.marker
) {
1232 DBG("now iterating on marker %s", iter
.marker
->name
);
1233 auto_probe_connect(iter
.marker
);
1234 marker_iter_next(&iter
);
1238 if(getenv("UST_TRACE")) {
1239 char trace_name
[] = "auto";
1240 char trace_type
[] = "ustrelay";
1242 DBG("starting early tracing");
1244 /* Ensure marker control is initialized */
1245 init_marker_control();
1247 /* Ensure markers are initialized */
1250 /* Ensure buffers are initialized, for the transport to be available.
1251 * We are about to set a trace type and it will fail without this.
1253 init_ustrelay_transport();
1255 /* FIXME: When starting early tracing (here), depending on the
1256 * order of constructors, it is very well possible some marker
1257 * sections are not yet registered. Because of this, some
1258 * channels may not be registered. Yet, we are about to ask the
1259 * daemon to collect the channels. Channels which are not yet
1260 * registered will not be collected.
1262 * Currently, in LTTng, there is no way to add a channel after
1263 * trace start. The reason for this is that it induces complex
1264 * concurrency issues on the trace structures, which can only
1265 * be resolved using RCU. This has not been done yet. As a
1266 * workaround, we are forcing the registration of the "ust"
1267 * channel here. This is the only channel (apart from metadata)
1268 * that can be reliably used in early tracing.
1270 * Non-early tracing does not have this problem and can use
1271 * arbitrary channel names.
1273 ltt_channels_register("ust");
1275 result
= ltt_trace_setup(trace_name
);
1277 ERR("ltt_trace_setup failed");
1281 result
= ltt_trace_set_type(trace_name
, trace_type
);
1283 ERR("ltt_trace_set_type failed");
1287 result
= ltt_trace_alloc(trace_name
);
1289 ERR("ltt_trace_alloc failed");
1293 result
= ltt_trace_start(trace_name
);
1295 ERR("ltt_trace_start failed");
1299 /* Do this after the trace is started in order to avoid creating confusion
1300 * if the trace fails to start. */
1301 inform_consumer_daemon(trace_name
);
1307 /* should decrementally destroy stuff if error */
1311 /* This is only called if we terminate normally, not with an unhandled signal,
1312 * so we cannot rely on it. However, for now, LTTV requires that the header of
1313 * the last sub-buffer contain a valid end time for the trace. This is done
1314 * automatically only when the trace is properly stopped.
1316 * If the traced program crashed, it is always possible to manually add the
1317 * right value in the header, or to open the trace in text mode.
1319 * FIXME: Fix LTTV so it doesn't need this.
1322 static void destroy_traces(void)
1326 /* if trace running, finish it */
1328 DBG("destructor stopping traces");
1330 result
= ltt_trace_stop("auto");
1332 ERR("ltt_trace_stop error");
1335 result
= ltt_trace_destroy("auto", 0);
1337 ERR("ltt_trace_destroy error");
1341 static int trace_recording(void)
1344 struct ust_trace
*trace
;
1348 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1355 ltt_unlock_traces();
1361 static int have_consumer(void)
1363 return !list_empty(&blocked_consumers
);
1367 int restarting_usleep(useconds_t usecs
)
1373 tv
.tv_nsec
= usecs
* 1000;
1376 result
= nanosleep(&tv
, &tv
);
1377 } while(result
== -1 && errno
== EINTR
);
1382 /* This destructor keeps the process alive for a few seconds in order
1383 * to leave time to ustd to connect to its buffers. This is necessary
1384 * for programs whose execution is very short. It is also useful in all
1385 * programs when tracing is started close to the end of the program
1388 * FIXME: For now, this only works for the first trace created in a
1392 static void __attribute__((destructor
)) keepalive()
1394 if(trace_recording() && buffers_to_export
) {
1396 DBG("Keeping process alive for consumer daemon...");
1397 while(buffers_to_export
) {
1398 const int interv
= 200000;
1399 restarting_usleep(interv
);
1402 if(total
>= 3000000) {
1403 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1407 DBG("Finally dying...");
1412 ustcomm_fini_app(&ustcomm_app
);
1415 void ust_potential_exec(void)
1417 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1424 /* Notify ust that there was a fork. This needs to be called inside
1425 * the new process, anytime a process whose memory is not shared with
1426 * the parent is created. If this function is not called, the events
1427 * of the new process will not be collected.
1429 * Signals should be disabled before the fork and reenabled only after
1430 * this call in order to guarantee tracing is not started before ust_fork()
1431 * sanitizes the new process.
1434 static void ust_fork(void)
1436 struct blocked_consumer
*bc
;
1437 struct blocked_consumer
*deletable_bc
= NULL
;
1440 /* FIXME: technically, the locks could have been taken before the fork */
1441 DBG("ust: forking");
1443 /* break lock if necessary */
1444 ltt_unlock_traces();
1446 ltt_trace_stop("auto");
1447 ltt_trace_destroy("auto", 1);
1448 /* Delete all active connections */
1449 ustcomm_close_all_connections(&ustcomm_app
.server
);
1451 /* Delete all blocked consumers */
1452 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1453 close(bc
->fd_producer
);
1454 close(bc
->fd_consumer
);
1457 list_del(&bc
->list
);
1460 ustcomm_free_app(&ustcomm_app
);
1462 buffers_to_export
= 0;
1466 ltt_trace_setup("auto");
1467 result
= ltt_trace_set_type("auto", "ustrelay");
1469 ERR("ltt_trace_set_type failed");
1473 ltt_trace_alloc("auto");
1474 ltt_trace_start("auto");
1475 inform_consumer_daemon("auto");
1478 void ust_before_fork(ust_fork_info_t
*fork_info
)
1480 /* Disable signals. This is to avoid that the child
1481 * intervenes before it is properly setup for tracing. It is
1482 * safer to disable all signals, because then we know we are not
1483 * breaking anything by restoring the original mask.
1489 - only do this if tracing is active
1492 /* Disable signals */
1493 sigfillset(&all_sigs
);
1494 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1496 PERROR("sigprocmask");
1501 /* Don't call this function directly in a traced program */
1502 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1506 /* Restore signals */
1507 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1509 PERROR("sigprocmask");
1514 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1516 /* Reenable signals */
1517 ust_after_fork_common(fork_info
);
1520 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1522 /* First sanitize the child */
1525 /* Then reenable interrupts */
1526 ust_after_fork_common(fork_info
);