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>
30 #include <urcu/uatomic_arch.h>
32 #include <ust/marker.h>
33 #include <ust/tracectl.h>
38 #include "marker-control.h"
42 #define USTSIGNAL SIGIO
44 #define MAX_MSG_SIZE (100)
46 #define MSG_REGISTER_NOTIF 2
48 char consumer_stack
[10000];
50 /* This should only be accessed by the constructor, before the creation
51 * of the listener, and then only by the listener.
55 struct list_head blocked_consumers
= LIST_HEAD_INIT(blocked_consumers
);
57 static struct ustcomm_app ustcomm_app
;
59 struct tracecmd
{ /* no padding */
64 /* volatile because shared between the listener and the main thread */
65 volatile sig_atomic_t buffers_to_export
= 0;
68 /* size: the size of all the fields except size itself */
71 /* Only the necessary part of the payload is transferred. It
72 * may even be none of it.
77 struct consumer_channel
{
79 struct ltt_channel_struct
*chan
;
82 struct blocked_consumer
{
87 /* args to ustcomm_send_reply */
88 struct ustcomm_server server
;
89 struct ustcomm_source src
;
91 /* args to ust_buffers_get_subbuf */
92 struct ust_buffer
*buf
;
94 struct list_head list
;
97 static long long make_pidunique(void)
102 gettimeofday(&tv
, NULL
);
106 retval
|= tv
.tv_usec
;
111 static void print_markers(FILE *fp
)
113 struct marker_iter iter
;
116 marker_iter_reset(&iter
);
117 marker_iter_start(&iter
);
120 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
);
121 marker_iter_next(&iter
);
126 static int init_socket(void);
132 struct trctl_msg msg
;
134 /* FIXME: fd_notif should probably be protected by a spinlock */
139 msg
.type
= MSG_NOTIF
;
140 msg
.size
= sizeof(msg
.type
);
142 /* FIXME: don't block here */
143 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
150 /* Ask the daemon to collect a trace called trace_name and being
151 * produced by this pid.
153 * The trace must be at least allocated. (It can also be started.)
154 * This is because _ltt_trace_find is used.
157 static void inform_consumer_daemon(const char *trace_name
)
160 struct ust_trace
*trace
;
161 pid_t pid
= getpid();
166 trace
= _ltt_trace_find(trace_name
);
168 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
172 for(i
=0; i
< trace
->nr_channels
; i
++) {
173 /* iterate on all cpus */
174 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
176 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
177 result
= ustcomm_request_consumer(pid
, buf
);
179 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
180 /* continue even if fail */
191 void process_blocked_consumers(void)
195 struct blocked_consumer
*bc
;
200 list_for_each_entry(bc
, &blocked_consumers
, list
) {
204 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
206 ERR("malloc returned NULL");
210 list_for_each_entry(bc
, &blocked_consumers
, list
) {
211 fds
[idx
].fd
= bc
->fd_producer
;
212 fds
[idx
].events
= POLLIN
;
213 bc
->tmp_poll_idx
= idx
;
217 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
224 list_for_each_entry(bc
, &blocked_consumers
, list
) {
225 if(fds
[bc
->tmp_poll_idx
].revents
) {
226 long consumed_old
= 0;
229 result
= read(bc
->fd_producer
, &inbuf
, 1);
237 close(bc
->fd_producer
);
241 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
243 ERR("ustcomm_send_reply failed");
250 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
251 if(result
== -EAGAIN
) {
252 WARN("missed buffer?");
255 else if(result
< 0) {
256 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result
));
258 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
259 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
261 ERR("ustcomm_send_reply failed");
273 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
277 sep
= rindex(channel_and_cpu
, '_');
280 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
286 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
289 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
292 struct ust_trace
*trace
;
293 char trace_name
[] = "auto";
295 char *channel_and_cpu
;
303 channel_and_cpu
= nth_token(recvbuf
, 1);
304 if(channel_and_cpu
== NULL
) {
305 ERR("cannot parse channel");
309 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
311 ERR("problem parsing channel name");
312 goto free_short_chan_name
;
316 trace
= _ltt_trace_find(trace_name
);
320 ERR("cannot find trace!");
322 goto free_short_chan_name
;
325 for(i
=0; i
<trace
->nr_channels
; i
++) {
326 struct ust_channel
*channel
= &trace
->channels
[i
];
327 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
329 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
332 // DBG("the shmid for the requested channel is %d", buf->shmid);
333 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
334 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
336 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
338 ERR("ustcomm_send_reply failed");
341 goto free_short_chan_name
;
352 ERR("channel not found (%s)", channel_and_cpu
);
355 free_short_chan_name
:
362 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
365 struct ust_trace
*trace
;
366 char trace_name
[] = "auto";
368 char *channel_and_cpu
;
374 DBG("get_n_subbufs");
376 channel_and_cpu
= nth_token(recvbuf
, 1);
377 if(channel_and_cpu
== NULL
) {
378 ERR("cannot parse channel");
382 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
384 ERR("problem parsing channel name");
385 goto free_short_chan_name
;
389 trace
= _ltt_trace_find(trace_name
);
393 ERR("cannot find trace!");
395 goto free_short_chan_name
;
398 for(i
=0; i
<trace
->nr_channels
; i
++) {
399 struct ust_channel
*channel
= &trace
->channels
[i
];
401 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
404 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
405 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
407 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
409 ERR("ustcomm_send_reply failed");
412 goto free_short_chan_name
;
421 ERR("unable to find channel");
424 free_short_chan_name
:
431 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
434 struct ust_trace
*trace
;
435 char trace_name
[] = "auto";
437 char *channel_and_cpu
;
443 DBG("get_subbuf_size");
445 channel_and_cpu
= nth_token(recvbuf
, 1);
446 if(channel_and_cpu
== NULL
) {
447 ERR("cannot parse channel");
451 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
453 ERR("problem parsing channel name");
454 goto free_short_chan_name
;
458 trace
= _ltt_trace_find(trace_name
);
462 ERR("cannot find trace!");
464 goto free_short_chan_name
;
467 for(i
=0; i
<trace
->nr_channels
; i
++) {
468 struct ust_channel
*channel
= &trace
->channels
[i
];
470 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
473 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
474 asprintf(&reply
, "%zd", channel
->subbuf_size
);
476 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
478 ERR("ustcomm_send_reply failed");
481 goto free_short_chan_name
;
490 ERR("unable to find channel");
493 free_short_chan_name
:
500 static unsigned int poweroftwo(unsigned int x
)
502 unsigned int power2
= 1;
503 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
508 while (power2
< x
&& power2
< hardcoded
)
514 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
516 char *channel_slash_size
;
517 char ch_name
[256]="";
518 unsigned int size
, power
;
520 struct ust_trace
*trace
;
521 char trace_name
[] = "auto";
525 DBG("set_subbuf_size");
527 channel_slash_size
= nth_token(recvbuf
, 1);
528 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
530 if(ch_name
== NULL
) {
531 ERR("cannot parse channel");
535 power
= poweroftwo(size
);
537 WARN("using the next 2^n = %u\n", power
);
540 trace
= _ltt_trace_find_setup(trace_name
);
542 ERR("cannot find trace!");
547 for(i
= 0; i
< trace
->nr_channels
; i
++) {
548 struct ust_channel
*channel
= &trace
->channels
[i
];
550 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
552 channel
->subbuf_size
= power
;
553 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
560 ERR("unable to find channel");
568 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
570 char *channel_slash_num
;
571 char ch_name
[256]="";
574 struct ust_trace
*trace
;
575 char trace_name
[] = "auto";
579 DBG("set_subbuf_num");
581 channel_slash_num
= nth_token(recvbuf
, 1);
582 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
584 if(ch_name
== NULL
) {
585 ERR("cannot parse channel");
589 ERR("subbuffer count should be greater than 2");
594 trace
= _ltt_trace_find_setup(trace_name
);
596 ERR("cannot find trace!");
601 for(i
= 0; i
< trace
->nr_channels
; i
++) {
602 struct ust_channel
*channel
= &trace
->channels
[i
];
604 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
606 channel
->subbuf_cnt
= num
;
607 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
614 ERR("unable to find channel");
622 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
625 struct ust_trace
*trace
;
626 char trace_name
[] = "auto";
628 char *channel_and_cpu
;
635 channel_and_cpu
= nth_token(recvbuf
, 1);
636 if(channel_and_cpu
== NULL
) {
637 ERR("cannot parse channel");
641 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
643 ERR("problem parsing channel name");
644 goto free_short_chan_name
;
648 trace
= _ltt_trace_find(trace_name
);
653 WARN("Cannot find trace. It was likely destroyed by the user.");
654 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
656 ERR("ustcomm_send_reply failed");
664 for(i
=0; i
<trace
->nr_channels
; i
++) {
665 struct ust_channel
*channel
= &trace
->channels
[i
];
667 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
668 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
669 struct blocked_consumer
*bc
;
673 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
675 ERR("malloc returned NULL");
678 bc
->fd_consumer
= src
->fd
;
679 bc
->fd_producer
= buf
->data_ready_fd_read
;
682 bc
->server
= ustcomm_app
.server
;
684 list_add(&bc
->list
, &blocked_consumers
);
686 /* Being here is the proof the daemon has mapped the buffer in its
687 * memory. We may now decrement buffers_to_export.
689 if(uatomic_read(&buf
->consumed
) == 0) {
690 DBG("decrementing buffers_to_export");
698 ERR("unable to find channel");
704 free_short_chan_name
:
711 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
714 struct ust_trace
*trace
;
715 char trace_name
[] = "auto";
717 char *channel_and_cpu
;
723 char *consumed_old_str
;
729 channel_and_cpu
= strdup_malloc(nth_token(recvbuf
, 1));
730 if(channel_and_cpu
== NULL
) {
731 ERR("cannot parse channel");
736 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
737 if(consumed_old_str
== NULL
) {
738 ERR("cannot parse consumed_old");
740 goto free_channel_and_cpu
;
742 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
743 if(*endptr
!= '\0') {
744 ERR("invalid value for consumed_old");
746 goto free_consumed_old_str
;
749 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
751 ERR("problem parsing channel name");
753 goto free_short_chan_name
;
757 trace
= _ltt_trace_find(trace_name
);
760 WARN("Cannot find trace. It was likely destroyed by the user.");
761 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
763 ERR("ustcomm_send_reply failed");
771 for(i
=0; i
<trace
->nr_channels
; i
++) {
772 struct ust_channel
*channel
= &trace
->channels
[i
];
774 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
775 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
779 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
781 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
782 asprintf(&reply
, "%s", "ERROR");
785 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
786 asprintf(&reply
, "%s", "OK");
789 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
791 ERR("ustcomm_send_reply failed");
802 ERR("unable to find channel");
807 free_short_chan_name
:
809 free_consumed_old_str
:
810 free(consumed_old_str
);
811 free_channel_and_cpu
:
812 free(channel_and_cpu
);
818 void *listener_main(void *p
)
825 char trace_name
[] = "auto";
826 char trace_type
[] = "ustrelay";
829 struct ustcomm_source src
;
831 process_blocked_consumers();
833 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
835 WARN("error in ustcomm_app_recv_message");
838 else if(result
== 0) {
843 DBG("received a message! it's: %s", recvbuf
);
844 len
= strlen(recvbuf
);
846 if(!strcmp(recvbuf
, "print_markers")) {
847 print_markers(stderr
);
849 else if(!strcmp(recvbuf
, "list_markers")) {
854 fp
= open_memstream(&ptr
, &size
);
858 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
862 else if(!strcmp(recvbuf
, "start")) {
863 /* start is an operation that setups the trace, allocates it and starts it */
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 result
= ltt_trace_alloc(trace_name
);
878 ERR("ltt_trace_alloc failed");
882 inform_consumer_daemon(trace_name
);
884 result
= ltt_trace_start(trace_name
);
886 ERR("ltt_trace_start failed");
890 else if(!strcmp(recvbuf
, "trace_setup")) {
893 result
= ltt_trace_setup(trace_name
);
895 ERR("ltt_trace_setup failed");
899 result
= ltt_trace_set_type(trace_name
, trace_type
);
901 ERR("ltt_trace_set_type failed");
905 else if(!strcmp(recvbuf
, "trace_alloc")) {
908 result
= ltt_trace_alloc(trace_name
);
910 ERR("ltt_trace_alloc failed");
913 inform_consumer_daemon(trace_name
);
915 else if(!strcmp(recvbuf
, "trace_create")) {
918 result
= ltt_trace_setup(trace_name
);
920 ERR("ltt_trace_setup failed");
924 result
= ltt_trace_set_type(trace_name
, trace_type
);
926 ERR("ltt_trace_set_type failed");
930 else if(!strcmp(recvbuf
, "trace_start")) {
933 result
= ltt_trace_alloc(trace_name
);
935 ERR("ltt_trace_alloc failed");
939 inform_consumer_daemon(trace_name
);
942 result
= ltt_trace_start(trace_name
);
944 ERR("ltt_trace_start failed");
948 else if(!strcmp(recvbuf
, "trace_stop")) {
951 result
= ltt_trace_stop(trace_name
);
953 ERR("ltt_trace_stop failed");
957 else if(!strcmp(recvbuf
, "trace_destroy")) {
959 DBG("trace destroy");
961 result
= ltt_trace_destroy(trace_name
, 0);
963 ERR("ltt_trace_destroy failed");
967 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
968 do_cmd_get_shmid(recvbuf
, &src
);
970 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
971 do_cmd_get_n_subbufs(recvbuf
, &src
);
973 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
974 do_cmd_get_subbuf_size(recvbuf
, &src
);
976 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
979 libfile
= nth_token(recvbuf
, 1);
981 DBG("load_probe_lib loading %s", libfile
);
985 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
986 do_cmd_get_subbuffer(recvbuf
, &src
);
988 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
989 do_cmd_put_subbuffer(recvbuf
, &src
);
991 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
992 do_cmd_set_subbuf_size(recvbuf
, &src
);
994 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
995 do_cmd_set_subbuf_num(recvbuf
, &src
);
997 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
998 char *channel_slash_name
= nth_token(recvbuf
, 1);
999 char channel_name
[256]="";
1000 char marker_name
[256]="";
1002 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1004 if(channel_name
== NULL
|| marker_name
== NULL
) {
1005 WARN("invalid marker name");
1009 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1011 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1014 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1015 char *channel_slash_name
= nth_token(recvbuf
, 1);
1019 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1021 if(marker_name
== NULL
) {
1024 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1026 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1029 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1032 asprintf(&reply
, "%lld", pidunique
);
1034 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
1036 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1042 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1043 // struct ust_trace *trace;
1044 // char trace_name[] = "auto";
1046 // char *channel_name;
1048 // DBG("get_notifications");
1050 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1051 // if(channel_name == NULL) {
1052 // ERR("put_subbuf_size: cannot parse channel");
1056 // ltt_lock_traces();
1057 // trace = _ltt_trace_find(trace_name);
1058 // ltt_unlock_traces();
1060 // if(trace == NULL) {
1061 // ERR("cannot find trace!");
1062 // return (void *)1;
1065 // for(i=0; i<trace->nr_channels; i++) {
1066 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1069 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1070 // struct rchan_buf *rbuf = rchan->buf;
1071 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1073 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1074 // if(result == -1) {
1075 // ERR("ustcomm_app_detach_client failed");
1079 // lttbuf->wake_consumer_arg = (void *) fd;
1083 // lttbuf->call_wake_consumer = 1;
1089 // free(channel_name);
1092 ERR("unable to parse message: %s", recvbuf
);
1100 volatile sig_atomic_t have_listener
= 0;
1102 void create_listener(void)
1105 static char listener_stack
[16384];
1112 WARN("not creating listener because we already had one");
1117 result
= clone((int (*)(void *)) listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
1124 pthread_create(&thread
, NULL
, listener_main
, NULL
);
1130 static int init_socket(void)
1132 return ustcomm_init_app(getpid(), &ustcomm_app
);
1135 #define AUTOPROBE_DISABLED 0
1136 #define AUTOPROBE_ENABLE_ALL 1
1137 #define AUTOPROBE_ENABLE_REGEX 2
1138 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1139 static regex_t autoprobe_regex
;
1141 static void auto_probe_connect(struct marker
*m
)
1145 char* concat_name
= NULL
;
1146 const char *probe_name
= "default";
1148 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1151 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1152 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1154 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1155 m
->channel
, m
->name
);
1158 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1165 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1166 if(result
&& result
!= -EEXIST
)
1167 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1169 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1173 static void __attribute__((constructor
)) init()
1176 char* autoprobe_val
= NULL
;
1178 /* Assign the pidunique, to be able to differentiate the processes with same
1179 * pid, (before and after an exec).
1181 pidunique
= make_pidunique();
1183 DBG("Tracectl constructor");
1185 result
= init_socket();
1187 ERR("init_socket error");
1193 autoprobe_val
= getenv("UST_AUTOPROBE");
1195 struct marker_iter iter
;
1197 DBG("Autoprobe enabled.");
1199 /* Ensure markers are initialized */
1202 /* Ensure marker control is initialized, for the probe */
1203 init_marker_control();
1205 /* first, set the callback that will connect the
1206 * probe on new markers
1208 if(autoprobe_val
[0] == '/') {
1209 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1213 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1214 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1215 /* don't crash the application just for this */
1218 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1222 /* just enable all instrumentation */
1223 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1226 marker_set_new_marker_cb(auto_probe_connect
);
1228 /* Now, connect the probes that were already registered. */
1229 marker_iter_reset(&iter
);
1230 marker_iter_start(&iter
);
1232 DBG("now iterating on markers already registered");
1233 while(iter
.marker
) {
1234 DBG("now iterating on marker %s", iter
.marker
->name
);
1235 auto_probe_connect(iter
.marker
);
1236 marker_iter_next(&iter
);
1240 if(getenv("UST_TRACE")) {
1241 char trace_name
[] = "auto";
1242 char trace_type
[] = "ustrelay";
1244 DBG("starting early tracing");
1246 /* Ensure marker control is initialized */
1247 init_marker_control();
1249 /* Ensure markers are initialized */
1252 /* Ensure buffers are initialized, for the transport to be available.
1253 * We are about to set a trace type and it will fail without this.
1255 init_ustrelay_transport();
1257 /* FIXME: When starting early tracing (here), depending on the
1258 * order of constructors, it is very well possible some marker
1259 * sections are not yet registered. Because of this, some
1260 * channels may not be registered. Yet, we are about to ask the
1261 * daemon to collect the channels. Channels which are not yet
1262 * registered will not be collected.
1264 * Currently, in LTTng, there is no way to add a channel after
1265 * trace start. The reason for this is that it induces complex
1266 * concurrency issues on the trace structures, which can only
1267 * be resolved using RCU. This has not been done yet. As a
1268 * workaround, we are forcing the registration of the "ust"
1269 * channel here. This is the only channel (apart from metadata)
1270 * that can be reliably used in early tracing.
1272 * Non-early tracing does not have this problem and can use
1273 * arbitrary channel names.
1275 ltt_channels_register("ust");
1277 result
= ltt_trace_setup(trace_name
);
1279 ERR("ltt_trace_setup failed");
1283 result
= ltt_trace_set_type(trace_name
, trace_type
);
1285 ERR("ltt_trace_set_type failed");
1289 result
= ltt_trace_alloc(trace_name
);
1291 ERR("ltt_trace_alloc failed");
1295 result
= ltt_trace_start(trace_name
);
1297 ERR("ltt_trace_start failed");
1301 /* Do this after the trace is started in order to avoid creating confusion
1302 * if the trace fails to start. */
1303 inform_consumer_daemon(trace_name
);
1309 /* should decrementally destroy stuff if error */
1313 /* This is only called if we terminate normally, not with an unhandled signal,
1314 * so we cannot rely on it. However, for now, LTTV requires that the header of
1315 * the last sub-buffer contain a valid end time for the trace. This is done
1316 * automatically only when the trace is properly stopped.
1318 * If the traced program crashed, it is always possible to manually add the
1319 * right value in the header, or to open the trace in text mode.
1321 * FIXME: Fix LTTV so it doesn't need this.
1324 static void destroy_traces(void)
1328 /* if trace running, finish it */
1330 DBG("destructor stopping traces");
1332 result
= ltt_trace_stop("auto");
1334 ERR("ltt_trace_stop error");
1337 result
= ltt_trace_destroy("auto", 0);
1339 ERR("ltt_trace_destroy error");
1343 static int trace_recording(void)
1346 struct ust_trace
*trace
;
1350 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1357 ltt_unlock_traces();
1363 static int have_consumer(void)
1365 return !list_empty(&blocked_consumers
);
1369 int restarting_usleep(useconds_t usecs
)
1375 tv
.tv_nsec
= usecs
* 1000;
1378 result
= nanosleep(&tv
, &tv
);
1379 } while(result
== -1 && errno
== EINTR
);
1384 /* This destructor keeps the process alive for a few seconds in order
1385 * to leave time to ustd to connect to its buffers. This is necessary
1386 * for programs whose execution is very short. It is also useful in all
1387 * programs when tracing is started close to the end of the program
1390 * FIXME: For now, this only works for the first trace created in a
1394 static void __attribute__((destructor
)) keepalive()
1396 if(trace_recording() && buffers_to_export
) {
1398 DBG("Keeping process alive for consumer daemon...");
1399 while(buffers_to_export
) {
1400 const int interv
= 200000;
1401 restarting_usleep(interv
);
1404 if(total
>= 3000000) {
1405 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1409 DBG("Finally dying...");
1414 ustcomm_fini_app(&ustcomm_app
);
1417 void ust_potential_exec(void)
1419 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1426 /* Notify ust that there was a fork. This needs to be called inside
1427 * the new process, anytime a process whose memory is not shared with
1428 * the parent is created. If this function is not called, the events
1429 * of the new process will not be collected.
1431 * Signals should be disabled before the fork and reenabled only after
1432 * this call in order to guarantee tracing is not started before ust_fork()
1433 * sanitizes the new process.
1436 static void ust_fork(void)
1438 struct blocked_consumer
*bc
;
1439 struct blocked_consumer
*deletable_bc
= NULL
;
1442 /* FIXME: technically, the locks could have been taken before the fork */
1443 DBG("ust: forking");
1445 /* break lock if necessary */
1446 ltt_unlock_traces();
1448 ltt_trace_stop("auto");
1449 ltt_trace_destroy("auto", 1);
1450 /* Delete all active connections */
1451 ustcomm_close_all_connections(&ustcomm_app
.server
);
1453 /* Delete all blocked consumers */
1454 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1455 close(bc
->fd_producer
);
1456 close(bc
->fd_consumer
);
1459 list_del(&bc
->list
);
1462 ustcomm_free_app(&ustcomm_app
);
1464 buffers_to_export
= 0;
1468 ltt_trace_setup("auto");
1469 result
= ltt_trace_set_type("auto", "ustrelay");
1471 ERR("ltt_trace_set_type failed");
1475 ltt_trace_alloc("auto");
1476 ltt_trace_start("auto");
1477 inform_consumer_daemon("auto");
1480 void ust_before_fork(ust_fork_info_t
*fork_info
)
1482 /* Disable signals. This is to avoid that the child
1483 * intervenes before it is properly setup for tracing. It is
1484 * safer to disable all signals, because then we know we are not
1485 * breaking anything by restoring the original mask.
1491 - only do this if tracing is active
1494 /* Disable signals */
1495 sigfillset(&all_sigs
);
1496 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1498 PERROR("sigprocmask");
1503 /* Don't call this function directly in a traced program */
1504 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1508 /* Restore signals */
1509 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1511 PERROR("sigprocmask");
1516 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1518 /* Reenable signals */
1519 ust_after_fork_common(fork_info
);
1522 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1524 /* First sanitize the child */
1527 /* Then reenable interrupts */
1528 ust_after_fork_common(fork_info
);