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"
39 #define USTSIGNAL SIGIO
41 #define MAX_MSG_SIZE (100)
43 #define MSG_REGISTER_NOTIF 2
45 char consumer_stack
[10000];
47 /* This should only be accessed by the constructor, before the creation
48 * of the listener, and then only by the listener.
52 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
54 static struct ustcomm_app ustcomm_app
;
56 struct tracecmd
{ /* no padding */
61 /* volatile because shared between the listener and the main thread */
62 volatile sig_atomic_t buffers_to_export
= 0;
65 /* size: the size of all the fields except size itself */
68 /* Only the necessary part of the payload is transferred. It
69 * may even be none of it.
74 struct consumer_channel
{
76 struct ltt_channel_struct
*chan
;
79 struct blocked_consumer
{
84 /* args to ustcomm_send_reply */
85 struct ustcomm_server server
;
86 struct ustcomm_source src
;
88 /* args to ust_buffers_get_subbuf */
89 struct ust_buffer
*buf
;
91 struct list_head list
;
94 static long long make_pidunique(void)
99 gettimeofday(&tv
, NULL
);
103 retval
|= tv
.tv_usec
;
108 static void print_markers(FILE *fp
)
110 struct marker_iter iter
;
113 marker_iter_reset(&iter
);
114 marker_iter_start(&iter
);
117 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
);
118 marker_iter_next(&iter
);
123 static int init_socket(void);
129 struct trctl_msg msg
;
131 /* FIXME: fd_notif should probably be protected by a spinlock */
136 msg
.type
= MSG_NOTIF
;
137 msg
.size
= sizeof(msg
.type
);
139 /* FIXME: don't block here */
140 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
147 /* Ask the daemon to collect a trace called trace_name and being
148 * produced by this pid.
150 * The trace must be at least allocated. (It can also be started.)
151 * This is because _ltt_trace_find is used.
154 static void inform_consumer_daemon(const char *trace_name
)
157 struct ust_trace
*trace
;
158 pid_t pid
= getpid();
163 trace
= _ltt_trace_find(trace_name
);
165 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
169 for(i
=0; i
< trace
->nr_channels
; i
++) {
170 /* iterate on all cpus */
171 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
173 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
174 result
= ustcomm_request_consumer(pid
, buf
);
176 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
177 /* continue even if fail */
188 void process_blocked_consumers(void)
192 struct blocked_consumer
*bc
;
197 list_for_each_entry(bc
, &blocked_consumers
, list
) {
201 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
203 ERR("malloc returned NULL");
207 list_for_each_entry(bc
, &blocked_consumers
, list
) {
208 fds
[idx
].fd
= bc
->fd_producer
;
209 fds
[idx
].events
= POLLIN
;
210 bc
->tmp_poll_idx
= idx
;
214 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
221 list_for_each_entry(bc
, &blocked_consumers
, list
) {
222 if(fds
[bc
->tmp_poll_idx
].revents
) {
223 long consumed_old
= 0;
226 result
= read(bc
->fd_producer
, &inbuf
, 1);
235 res
= close(bc
->fd_producer
);
242 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
244 ERR("ustcomm_send_reply failed");
251 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
252 if(result
== -EAGAIN
) {
253 WARN("missed buffer?");
256 else if(result
< 0) {
257 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result
));
259 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
260 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
262 ERR("ustcomm_send_reply failed");
274 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
278 sep
= rindex(channel_and_cpu
, '_');
281 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
287 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
290 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
293 struct ust_trace
*trace
;
294 char trace_name
[] = "auto";
296 char *channel_and_cpu
;
304 channel_and_cpu
= nth_token(recvbuf
, 1);
305 if(channel_and_cpu
== NULL
) {
306 ERR("cannot parse channel");
310 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
312 ERR("problem parsing channel name");
313 goto free_short_chan_name
;
317 trace
= _ltt_trace_find(trace_name
);
321 ERR("cannot find trace!");
323 goto free_short_chan_name
;
326 for(i
=0; i
<trace
->nr_channels
; i
++) {
327 struct ust_channel
*channel
= &trace
->channels
[i
];
328 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
330 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
333 // DBG("the shmid for the requested channel is %d", buf->shmid);
334 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
335 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
337 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
339 ERR("ustcomm_send_reply failed");
342 goto free_short_chan_name
;
353 ERR("channel not found (%s)", channel_and_cpu
);
356 free_short_chan_name
:
363 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
366 struct ust_trace
*trace
;
367 char trace_name
[] = "auto";
369 char *channel_and_cpu
;
375 DBG("get_n_subbufs");
377 channel_and_cpu
= nth_token(recvbuf
, 1);
378 if(channel_and_cpu
== NULL
) {
379 ERR("cannot parse channel");
383 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
385 ERR("problem parsing channel name");
386 goto free_short_chan_name
;
390 trace
= _ltt_trace_find(trace_name
);
394 ERR("cannot find trace!");
396 goto free_short_chan_name
;
399 for(i
=0; i
<trace
->nr_channels
; i
++) {
400 struct ust_channel
*channel
= &trace
->channels
[i
];
402 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
405 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
406 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
408 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
410 ERR("ustcomm_send_reply failed");
413 goto free_short_chan_name
;
422 ERR("unable to find channel");
425 free_short_chan_name
:
432 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
435 struct ust_trace
*trace
;
436 char trace_name
[] = "auto";
438 char *channel_and_cpu
;
444 DBG("get_subbuf_size");
446 channel_and_cpu
= nth_token(recvbuf
, 1);
447 if(channel_and_cpu
== NULL
) {
448 ERR("cannot parse channel");
452 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
454 ERR("problem parsing channel name");
455 goto free_short_chan_name
;
459 trace
= _ltt_trace_find(trace_name
);
463 ERR("cannot find trace!");
465 goto free_short_chan_name
;
468 for(i
=0; i
<trace
->nr_channels
; i
++) {
469 struct ust_channel
*channel
= &trace
->channels
[i
];
471 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
474 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
475 asprintf(&reply
, "%zd", channel
->subbuf_size
);
477 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
479 ERR("ustcomm_send_reply failed");
482 goto free_short_chan_name
;
491 ERR("unable to find channel");
494 free_short_chan_name
:
501 static unsigned int poweroftwo(unsigned int x
)
503 unsigned int power2
= 1;
504 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
509 while (power2
< x
&& power2
< hardcoded
)
515 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
517 char *channel_slash_size
;
518 char ch_name
[256]="";
519 unsigned int size
, power
;
521 struct ust_trace
*trace
;
522 char trace_name
[] = "auto";
526 DBG("set_subbuf_size");
528 channel_slash_size
= nth_token(recvbuf
, 1);
529 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
531 if(ch_name
== NULL
) {
532 ERR("cannot parse channel");
536 power
= poweroftwo(size
);
538 WARN("using the next 2^n = %u\n", power
);
541 trace
= _ltt_trace_find_setup(trace_name
);
543 ERR("cannot find trace!");
548 for(i
= 0; i
< trace
->nr_channels
; i
++) {
549 struct ust_channel
*channel
= &trace
->channels
[i
];
551 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
553 channel
->subbuf_size
= power
;
554 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
561 ERR("unable to find channel");
569 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
571 char *channel_slash_num
;
572 char ch_name
[256]="";
575 struct ust_trace
*trace
;
576 char trace_name
[] = "auto";
580 DBG("set_subbuf_num");
582 channel_slash_num
= nth_token(recvbuf
, 1);
583 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
585 if(ch_name
== NULL
) {
586 ERR("cannot parse channel");
590 ERR("subbuffer count should be greater than 2");
595 trace
= _ltt_trace_find_setup(trace_name
);
597 ERR("cannot find trace!");
602 for(i
= 0; i
< trace
->nr_channels
; i
++) {
603 struct ust_channel
*channel
= &trace
->channels
[i
];
605 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
607 channel
->subbuf_cnt
= num
;
608 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
615 ERR("unable to find channel");
623 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
626 struct ust_trace
*trace
;
627 char trace_name
[] = "auto";
629 char *channel_and_cpu
;
636 channel_and_cpu
= nth_token(recvbuf
, 1);
637 if(channel_and_cpu
== NULL
) {
638 ERR("cannot parse channel");
642 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
644 ERR("problem parsing channel name");
645 goto free_short_chan_name
;
649 trace
= _ltt_trace_find(trace_name
);
654 DBG("Cannot find trace. It was likely destroyed by the user.");
655 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
657 ERR("ustcomm_send_reply failed");
665 for(i
=0; i
<trace
->nr_channels
; i
++) {
666 struct ust_channel
*channel
= &trace
->channels
[i
];
668 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
669 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
670 struct blocked_consumer
*bc
;
674 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
676 ERR("malloc returned NULL");
679 bc
->fd_consumer
= src
->fd
;
680 bc
->fd_producer
= buf
->data_ready_fd_read
;
683 bc
->server
= ustcomm_app
.server
;
685 list_add(&bc
->list
, &blocked_consumers
);
687 /* Being here is the proof the daemon has mapped the buffer in its
688 * memory. We may now decrement buffers_to_export.
690 if(uatomic_read(&buf
->consumed
) == 0) {
691 DBG("decrementing buffers_to_export");
699 ERR("unable to find channel");
705 free_short_chan_name
:
712 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
715 struct ust_trace
*trace
;
716 char trace_name
[] = "auto";
718 char *channel_and_cpu
;
724 char *consumed_old_str
;
730 channel_and_cpu
= strdup_malloc(nth_token(recvbuf
, 1));
731 if(channel_and_cpu
== NULL
) {
732 ERR("cannot parse channel");
737 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
738 if(consumed_old_str
== NULL
) {
739 ERR("cannot parse consumed_old");
741 goto free_channel_and_cpu
;
743 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
744 if(*endptr
!= '\0') {
745 ERR("invalid value for consumed_old");
747 goto free_consumed_old_str
;
750 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
752 ERR("problem parsing channel name");
754 goto free_short_chan_name
;
758 trace
= _ltt_trace_find(trace_name
);
761 DBG("Cannot find trace. It was likely destroyed by the user.");
762 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
764 ERR("ustcomm_send_reply failed");
772 for(i
=0; i
<trace
->nr_channels
; i
++) {
773 struct ust_channel
*channel
= &trace
->channels
[i
];
775 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
776 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
780 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
782 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
783 asprintf(&reply
, "%s", "ERROR");
786 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
787 asprintf(&reply
, "%s", "OK");
790 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
792 ERR("ustcomm_send_reply failed");
803 ERR("unable to find channel");
808 free_short_chan_name
:
810 free_consumed_old_str
:
811 free(consumed_old_str
);
812 free_channel_and_cpu
:
813 free(channel_and_cpu
);
819 static void listener_cleanup(void *ptr
)
821 ustcomm_fini_app(&ustcomm_app
, 0);
824 void *listener_main(void *p
)
830 pthread_cleanup_push(listener_cleanup
, NULL
);
833 char trace_name
[] = "auto";
834 char trace_type
[] = "ustrelay";
837 struct ustcomm_source src
;
839 process_blocked_consumers();
841 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
843 WARN("error in ustcomm_app_recv_message");
846 else if(result
== 0) {
851 DBG("received a message! it's: %s", recvbuf
);
852 len
= strlen(recvbuf
);
854 if(!strcmp(recvbuf
, "print_markers")) {
855 print_markers(stderr
);
857 else if(!strcmp(recvbuf
, "list_markers")) {
862 fp
= open_memstream(&ptr
, &size
);
866 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
870 else if(!strcmp(recvbuf
, "start")) {
871 /* start is an operation that setups the trace, allocates it and starts it */
872 result
= ltt_trace_setup(trace_name
);
874 ERR("ltt_trace_setup failed");
878 result
= ltt_trace_set_type(trace_name
, trace_type
);
880 ERR("ltt_trace_set_type failed");
884 result
= ltt_trace_alloc(trace_name
);
886 ERR("ltt_trace_alloc failed");
890 inform_consumer_daemon(trace_name
);
892 result
= ltt_trace_start(trace_name
);
894 ERR("ltt_trace_start failed");
898 else if(!strcmp(recvbuf
, "trace_setup")) {
901 result
= ltt_trace_setup(trace_name
);
903 ERR("ltt_trace_setup failed");
907 result
= ltt_trace_set_type(trace_name
, trace_type
);
909 ERR("ltt_trace_set_type failed");
913 else if(!strcmp(recvbuf
, "trace_alloc")) {
916 result
= ltt_trace_alloc(trace_name
);
918 ERR("ltt_trace_alloc failed");
921 inform_consumer_daemon(trace_name
);
923 else if(!strcmp(recvbuf
, "trace_create")) {
926 result
= ltt_trace_setup(trace_name
);
928 ERR("ltt_trace_setup failed");
932 result
= ltt_trace_set_type(trace_name
, trace_type
);
934 ERR("ltt_trace_set_type failed");
938 else if(!strcmp(recvbuf
, "trace_start")) {
941 result
= ltt_trace_alloc(trace_name
);
943 ERR("ltt_trace_alloc failed");
947 inform_consumer_daemon(trace_name
);
950 result
= ltt_trace_start(trace_name
);
952 ERR("ltt_trace_start failed");
956 else if(!strcmp(recvbuf
, "trace_stop")) {
959 result
= ltt_trace_stop(trace_name
);
961 ERR("ltt_trace_stop failed");
965 else if(!strcmp(recvbuf
, "trace_destroy")) {
967 DBG("trace destroy");
969 result
= ltt_trace_destroy(trace_name
, 0);
971 ERR("ltt_trace_destroy failed");
975 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
976 do_cmd_get_shmid(recvbuf
, &src
);
978 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
979 do_cmd_get_n_subbufs(recvbuf
, &src
);
981 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
982 do_cmd_get_subbuf_size(recvbuf
, &src
);
984 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
987 libfile
= nth_token(recvbuf
, 1);
989 DBG("load_probe_lib loading %s", libfile
);
993 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
994 do_cmd_get_subbuffer(recvbuf
, &src
);
996 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
997 do_cmd_put_subbuffer(recvbuf
, &src
);
999 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1000 do_cmd_set_subbuf_size(recvbuf
, &src
);
1002 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1003 do_cmd_set_subbuf_num(recvbuf
, &src
);
1005 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1006 char *channel_slash_name
= nth_token(recvbuf
, 1);
1007 char channel_name
[256]="";
1008 char marker_name
[256]="";
1010 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1012 if(channel_name
== NULL
|| marker_name
== NULL
) {
1013 WARN("invalid marker name");
1017 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1019 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1022 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1023 char *channel_slash_name
= nth_token(recvbuf
, 1);
1027 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1029 if(marker_name
== NULL
) {
1032 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1034 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1037 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1040 asprintf(&reply
, "%lld", pidunique
);
1042 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
1044 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1050 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1051 // struct ust_trace *trace;
1052 // char trace_name[] = "auto";
1054 // char *channel_name;
1056 // DBG("get_notifications");
1058 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1059 // if(channel_name == NULL) {
1060 // ERR("put_subbuf_size: cannot parse channel");
1064 // ltt_lock_traces();
1065 // trace = _ltt_trace_find(trace_name);
1066 // ltt_unlock_traces();
1068 // if(trace == NULL) {
1069 // ERR("cannot find trace!");
1070 // return (void *)1;
1073 // for(i=0; i<trace->nr_channels; i++) {
1074 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1077 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1078 // struct rchan_buf *rbuf = rchan->buf;
1079 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1081 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1082 // if(result == -1) {
1083 // ERR("ustcomm_app_detach_client failed");
1087 // lttbuf->wake_consumer_arg = (void *) fd;
1091 // lttbuf->call_wake_consumer = 1;
1097 // free(channel_name);
1100 ERR("unable to parse message: %s", recvbuf
);
1107 pthread_cleanup_pop(1);
1110 volatile sig_atomic_t have_listener
= 0;
1111 /* These should only be accessed in the parent thread,
1114 static pthread_t listener_thread
;
1116 void create_listener(void)
1121 WARN("not creating listener because we already had one");
1125 result
= pthread_create(&listener_thread
, NULL
, listener_main
, NULL
);
1127 PERROR("pthread_create");
1133 static int init_socket(void)
1135 return ustcomm_init_app(getpid(), &ustcomm_app
);
1138 #define AUTOPROBE_DISABLED 0
1139 #define AUTOPROBE_ENABLE_ALL 1
1140 #define AUTOPROBE_ENABLE_REGEX 2
1141 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1142 static regex_t autoprobe_regex
;
1144 static void auto_probe_connect(struct marker
*m
)
1148 char* concat_name
= NULL
;
1149 const char *probe_name
= "default";
1151 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1154 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1155 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1157 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1158 m
->channel
, m
->name
);
1161 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1168 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1169 if(result
&& result
!= -EEXIST
)
1170 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1172 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1176 static void __attribute__((constructor
)) init()
1179 char* autoprobe_val
= NULL
;
1181 /* Assign the pidunique, to be able to differentiate the processes with same
1182 * pid, (before and after an exec).
1184 pidunique
= make_pidunique();
1186 DBG("Tracectl constructor");
1188 result
= init_socket();
1190 ERR("init_socket error");
1196 autoprobe_val
= getenv("UST_AUTOPROBE");
1198 struct marker_iter iter
;
1200 DBG("Autoprobe enabled.");
1202 /* Ensure markers are initialized */
1205 /* Ensure marker control is initialized, for the probe */
1206 init_marker_control();
1208 /* first, set the callback that will connect the
1209 * probe on new markers
1211 if(autoprobe_val
[0] == '/') {
1212 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1216 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1217 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1218 /* don't crash the application just for this */
1221 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1225 /* just enable all instrumentation */
1226 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1229 marker_set_new_marker_cb(auto_probe_connect
);
1231 /* Now, connect the probes that were already registered. */
1232 marker_iter_reset(&iter
);
1233 marker_iter_start(&iter
);
1235 DBG("now iterating on markers already registered");
1236 while(iter
.marker
) {
1237 DBG("now iterating on marker %s", iter
.marker
->name
);
1238 auto_probe_connect(iter
.marker
);
1239 marker_iter_next(&iter
);
1243 if(getenv("UST_TRACE")) {
1244 char trace_name
[] = "auto";
1245 char trace_type
[] = "ustrelay";
1247 DBG("starting early tracing");
1249 /* Ensure marker control is initialized */
1250 init_marker_control();
1252 /* Ensure markers are initialized */
1255 /* Ensure buffers are initialized, for the transport to be available.
1256 * We are about to set a trace type and it will fail without this.
1258 init_ustrelay_transport();
1260 /* FIXME: When starting early tracing (here), depending on the
1261 * order of constructors, it is very well possible some marker
1262 * sections are not yet registered. Because of this, some
1263 * channels may not be registered. Yet, we are about to ask the
1264 * daemon to collect the channels. Channels which are not yet
1265 * registered will not be collected.
1267 * Currently, in LTTng, there is no way to add a channel after
1268 * trace start. The reason for this is that it induces complex
1269 * concurrency issues on the trace structures, which can only
1270 * be resolved using RCU. This has not been done yet. As a
1271 * workaround, we are forcing the registration of the "ust"
1272 * channel here. This is the only channel (apart from metadata)
1273 * that can be reliably used in early tracing.
1275 * Non-early tracing does not have this problem and can use
1276 * arbitrary channel names.
1278 ltt_channels_register("ust");
1280 result
= ltt_trace_setup(trace_name
);
1282 ERR("ltt_trace_setup failed");
1286 result
= ltt_trace_set_type(trace_name
, trace_type
);
1288 ERR("ltt_trace_set_type failed");
1292 result
= ltt_trace_alloc(trace_name
);
1294 ERR("ltt_trace_alloc failed");
1298 result
= ltt_trace_start(trace_name
);
1300 ERR("ltt_trace_start failed");
1304 /* Do this after the trace is started in order to avoid creating confusion
1305 * if the trace fails to start. */
1306 inform_consumer_daemon(trace_name
);
1312 /* should decrementally destroy stuff if error */
1316 /* This is only called if we terminate normally, not with an unhandled signal,
1317 * so we cannot rely on it. However, for now, LTTV requires that the header of
1318 * the last sub-buffer contain a valid end time for the trace. This is done
1319 * automatically only when the trace is properly stopped.
1321 * If the traced program crashed, it is always possible to manually add the
1322 * right value in the header, or to open the trace in text mode.
1324 * FIXME: Fix LTTV so it doesn't need this.
1327 static void destroy_traces(void)
1331 /* if trace running, finish it */
1333 DBG("destructor stopping traces");
1335 result
= ltt_trace_stop("auto");
1337 ERR("ltt_trace_stop error");
1340 result
= ltt_trace_destroy("auto", 0);
1342 ERR("ltt_trace_destroy error");
1346 static int trace_recording(void)
1349 struct ust_trace
*trace
;
1353 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1360 ltt_unlock_traces();
1366 static int have_consumer(void)
1368 return !list_empty(&blocked_consumers
);
1372 int restarting_usleep(useconds_t usecs
)
1378 tv
.tv_nsec
= usecs
* 1000;
1381 result
= nanosleep(&tv
, &tv
);
1382 } while(result
== -1 && errno
== EINTR
);
1387 static void stop_listener()
1391 result
= pthread_cancel(listener_thread
);
1393 PERROR("pthread_cancel");
1395 result
= pthread_join(listener_thread
, NULL
);
1397 PERROR("pthread_join");
1401 /* This destructor keeps the process alive for a few seconds in order
1402 * to leave time to ustd to connect to its buffers. This is necessary
1403 * for programs whose execution is very short. It is also useful in all
1404 * programs when tracing is started close to the end of the program
1407 * FIXME: For now, this only works for the first trace created in a
1411 static void __attribute__((destructor
)) keepalive()
1413 if(trace_recording() && buffers_to_export
) {
1415 DBG("Keeping process alive for consumer daemon...");
1416 while(buffers_to_export
) {
1417 const int interv
= 200000;
1418 restarting_usleep(interv
);
1421 if(total
>= 3000000) {
1422 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1426 DBG("Finally dying...");
1431 /* Ask the listener to stop and clean up. */
1435 void ust_potential_exec(void)
1437 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1444 /* Notify ust that there was a fork. This needs to be called inside
1445 * the new process, anytime a process whose memory is not shared with
1446 * the parent is created. If this function is not called, the events
1447 * of the new process will not be collected.
1449 * Signals should be disabled before the fork and reenabled only after
1450 * this call in order to guarantee tracing is not started before ust_fork()
1451 * sanitizes the new process.
1454 static void ust_fork(void)
1456 struct blocked_consumer
*bc
;
1457 struct blocked_consumer
*deletable_bc
= NULL
;
1460 /* FIXME: technically, the locks could have been taken before the fork */
1461 DBG("ust: forking");
1463 /* break lock if necessary */
1464 ltt_unlock_traces();
1466 ltt_trace_stop("auto");
1467 ltt_trace_destroy("auto", 1);
1468 /* Delete all active connections */
1469 ustcomm_close_all_connections(&ustcomm_app
.server
);
1471 /* Delete all blocked consumers */
1472 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1473 result
= close(bc
->fd_producer
);
1479 list_del(&bc
->list
);
1482 /* free app, keeping socket file */
1483 ustcomm_fini_app(&ustcomm_app
, 1);
1485 buffers_to_export
= 0;
1489 ltt_trace_setup("auto");
1490 result
= ltt_trace_set_type("auto", "ustrelay");
1492 ERR("ltt_trace_set_type failed");
1496 ltt_trace_alloc("auto");
1497 ltt_trace_start("auto");
1498 inform_consumer_daemon("auto");
1501 void ust_before_fork(ust_fork_info_t
*fork_info
)
1503 /* Disable signals. This is to avoid that the child
1504 * intervenes before it is properly setup for tracing. It is
1505 * safer to disable all signals, because then we know we are not
1506 * breaking anything by restoring the original mask.
1512 - only do this if tracing is active
1515 /* Disable signals */
1516 sigfillset(&all_sigs
);
1517 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1519 PERROR("sigprocmask");
1524 /* Don't call this function directly in a traced program */
1525 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1529 /* Restore signals */
1530 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1532 PERROR("sigprocmask");
1537 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1539 /* Reenable signals */
1540 ust_after_fork_common(fork_info
);
1543 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1545 /* First sanitize the child */
1548 /* Then reenable interrupts */
1549 ust_after_fork_common(fork_info
);