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>
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);
128 /* This needs to be called whenever a new thread is created. It notifies
129 * liburcu of the new thread.
132 void ust_register_thread(void)
134 rcu_register_thread();
141 struct trctl_msg msg
;
143 /* FIXME: fd_notif should probably be protected by a spinlock */
148 msg
.type
= MSG_NOTIF
;
149 msg
.size
= sizeof(msg
.type
);
151 /* FIXME: don't block here */
152 result
= write(fd_notif
, &msg
, msg
.size
+sizeof(msg
.size
));
159 /* Ask the daemon to collect a trace called trace_name and being
160 * produced by this pid.
162 * The trace must be at least allocated. (It can also be started.)
163 * This is because _ltt_trace_find is used.
166 static void inform_consumer_daemon(const char *trace_name
)
169 struct ust_trace
*trace
;
170 pid_t pid
= getpid();
175 trace
= _ltt_trace_find(trace_name
);
177 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name
);
181 for(i
=0; i
< trace
->nr_channels
; i
++) {
182 /* iterate on all cpus */
183 for(j
=0; j
<trace
->channels
[i
].n_cpus
; j
++) {
185 asprintf(&buf
, "%s_%d", trace
->channels
[i
].channel_name
, j
);
186 result
= ustcomm_request_consumer(pid
, buf
);
188 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
189 /* continue even if fail */
200 void process_blocked_consumers(void)
204 struct blocked_consumer
*bc
;
209 list_for_each_entry(bc
, &blocked_consumers
, list
) {
213 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
215 ERR("malloc returned NULL");
219 list_for_each_entry(bc
, &blocked_consumers
, list
) {
220 fds
[idx
].fd
= bc
->fd_producer
;
221 fds
[idx
].events
= POLLIN
;
222 bc
->tmp_poll_idx
= idx
;
226 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
233 list_for_each_entry(bc
, &blocked_consumers
, list
) {
234 if(fds
[bc
->tmp_poll_idx
].revents
) {
235 long consumed_old
= 0;
238 result
= read(bc
->fd_producer
, &inbuf
, 1);
246 close(bc
->fd_producer
);
250 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
252 ERR("ustcomm_send_reply failed");
259 result
= ust_buffers_get_subbuf(bc
->buf
, &consumed_old
);
260 if(result
== -EAGAIN
) {
261 WARN("missed buffer?");
264 else if(result
< 0) {
265 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result
));
267 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
268 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
270 ERR("ustcomm_send_reply failed");
282 void seperate_channel_cpu(const char *channel_and_cpu
, char **channel
, int *cpu
)
286 sep
= rindex(channel_and_cpu
, '_');
289 sep
= channel_and_cpu
+ strlen(channel_and_cpu
);
295 asprintf(channel
, "%.*s", (int)(sep
-channel_and_cpu
), channel_and_cpu
);
298 static int do_cmd_get_shmid(const char *recvbuf
, struct ustcomm_source
*src
)
301 struct ust_trace
*trace
;
302 char trace_name
[] = "auto";
304 char *channel_and_cpu
;
312 channel_and_cpu
= nth_token(recvbuf
, 1);
313 if(channel_and_cpu
== NULL
) {
314 ERR("cannot parse channel");
318 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
320 ERR("problem parsing channel name");
321 goto free_short_chan_name
;
325 trace
= _ltt_trace_find(trace_name
);
329 ERR("cannot find trace!");
331 goto free_short_chan_name
;
334 for(i
=0; i
<trace
->nr_channels
; i
++) {
335 struct ust_channel
*channel
= &trace
->channels
[i
];
336 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
338 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
341 // DBG("the shmid for the requested channel is %d", buf->shmid);
342 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
343 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_struct_shmids
[ch_cpu
]);
345 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
347 ERR("ustcomm_send_reply failed");
350 goto free_short_chan_name
;
361 ERR("channel not found (%s)", channel_and_cpu
);
364 free_short_chan_name
:
371 static int do_cmd_get_n_subbufs(const char *recvbuf
, struct ustcomm_source
*src
)
374 struct ust_trace
*trace
;
375 char trace_name
[] = "auto";
377 char *channel_and_cpu
;
383 DBG("get_n_subbufs");
385 channel_and_cpu
= nth_token(recvbuf
, 1);
386 if(channel_and_cpu
== NULL
) {
387 ERR("cannot parse channel");
391 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
393 ERR("problem parsing channel name");
394 goto free_short_chan_name
;
398 trace
= _ltt_trace_find(trace_name
);
402 ERR("cannot find trace!");
404 goto free_short_chan_name
;
407 for(i
=0; i
<trace
->nr_channels
; i
++) {
408 struct ust_channel
*channel
= &trace
->channels
[i
];
410 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
413 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
414 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
416 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
418 ERR("ustcomm_send_reply failed");
421 goto free_short_chan_name
;
430 ERR("unable to find channel");
433 free_short_chan_name
:
440 static int do_cmd_get_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
443 struct ust_trace
*trace
;
444 char trace_name
[] = "auto";
446 char *channel_and_cpu
;
452 DBG("get_subbuf_size");
454 channel_and_cpu
= nth_token(recvbuf
, 1);
455 if(channel_and_cpu
== NULL
) {
456 ERR("cannot parse channel");
460 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
462 ERR("problem parsing channel name");
463 goto free_short_chan_name
;
467 trace
= _ltt_trace_find(trace_name
);
471 ERR("cannot find trace!");
473 goto free_short_chan_name
;
476 for(i
=0; i
<trace
->nr_channels
; i
++) {
477 struct ust_channel
*channel
= &trace
->channels
[i
];
479 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
482 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
483 asprintf(&reply
, "%zd", channel
->subbuf_size
);
485 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
487 ERR("ustcomm_send_reply failed");
490 goto free_short_chan_name
;
499 ERR("unable to find channel");
502 free_short_chan_name
:
509 static unsigned int poweroftwo(unsigned int x
)
511 unsigned int power2
= 1;
512 unsigned int hardcoded
= 2147483648; /* FIX max 2^31 */
517 while (power2
< x
&& power2
< hardcoded
)
523 static int do_cmd_set_subbuf_size(const char *recvbuf
, struct ustcomm_source
*src
)
525 char *channel_slash_size
;
526 char ch_name
[256]="";
527 unsigned int size
, power
;
529 struct ust_trace
*trace
;
530 char trace_name
[] = "auto";
534 DBG("set_subbuf_size");
536 channel_slash_size
= nth_token(recvbuf
, 1);
537 sscanf(channel_slash_size
, "%255[^/]/%u", ch_name
, &size
);
539 if(ch_name
== NULL
) {
540 ERR("cannot parse channel");
544 power
= poweroftwo(size
);
546 WARN("using the next 2^n = %u\n", power
);
549 trace
= _ltt_trace_find_setup(trace_name
);
551 ERR("cannot find trace!");
556 for(i
= 0; i
< trace
->nr_channels
; i
++) {
557 struct ust_channel
*channel
= &trace
->channels
[i
];
559 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
561 channel
->subbuf_size
= power
;
562 DBG("the set_subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
569 ERR("unable to find channel");
577 static int do_cmd_set_subbuf_num(const char *recvbuf
, struct ustcomm_source
*src
)
579 char *channel_slash_num
;
580 char ch_name
[256]="";
583 struct ust_trace
*trace
;
584 char trace_name
[] = "auto";
588 DBG("set_subbuf_num");
590 channel_slash_num
= nth_token(recvbuf
, 1);
591 sscanf(channel_slash_num
, "%255[^/]/%u", ch_name
, &num
);
593 if(ch_name
== NULL
) {
594 ERR("cannot parse channel");
598 ERR("subbuffer count should be greater than 2");
603 trace
= _ltt_trace_find_setup(trace_name
);
605 ERR("cannot find trace!");
610 for(i
= 0; i
< trace
->nr_channels
; i
++) {
611 struct ust_channel
*channel
= &trace
->channels
[i
];
613 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
615 channel
->subbuf_cnt
= num
;
616 DBG("the set_subbuf_cnt for the requested channel is %zd", channel
->subbuf_cnt
);
623 ERR("unable to find channel");
631 static int do_cmd_get_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
634 struct ust_trace
*trace
;
635 char trace_name
[] = "auto";
637 char *channel_and_cpu
;
644 channel_and_cpu
= nth_token(recvbuf
, 1);
645 if(channel_and_cpu
== NULL
) {
646 ERR("cannot parse channel");
650 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
652 ERR("problem parsing channel name");
653 goto free_short_chan_name
;
657 trace
= _ltt_trace_find(trace_name
);
662 WARN("Cannot find trace. It was likely destroyed by the user.");
663 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
665 ERR("ustcomm_send_reply failed");
673 for(i
=0; i
<trace
->nr_channels
; i
++) {
674 struct ust_channel
*channel
= &trace
->channels
[i
];
676 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
677 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
678 struct blocked_consumer
*bc
;
682 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
684 ERR("malloc returned NULL");
687 bc
->fd_consumer
= src
->fd
;
688 bc
->fd_producer
= buf
->data_ready_fd_read
;
691 bc
->server
= ustcomm_app
.server
;
693 list_add(&bc
->list
, &blocked_consumers
);
695 /* Being here is the proof the daemon has mapped the buffer in its
696 * memory. We may now decrement buffers_to_export.
698 if(atomic_long_read(&buf
->consumed
) == 0) {
699 DBG("decrementing buffers_to_export");
707 ERR("unable to find channel");
713 free_short_chan_name
:
720 static int do_cmd_put_subbuffer(const char *recvbuf
, struct ustcomm_source
*src
)
723 struct ust_trace
*trace
;
724 char trace_name
[] = "auto";
726 char *channel_and_cpu
;
732 char *consumed_old_str
;
738 channel_and_cpu
= strdup_malloc(nth_token(recvbuf
, 1));
739 if(channel_and_cpu
== NULL
) {
740 ERR("cannot parse channel");
745 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
746 if(consumed_old_str
== NULL
) {
747 ERR("cannot parse consumed_old");
749 goto free_channel_and_cpu
;
751 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
752 if(*endptr
!= '\0') {
753 ERR("invalid value for consumed_old");
755 goto free_consumed_old_str
;
758 seperate_channel_cpu(channel_and_cpu
, &ch_name
, &ch_cpu
);
760 ERR("problem parsing channel name");
762 goto free_short_chan_name
;
766 trace
= _ltt_trace_find(trace_name
);
769 WARN("Cannot find trace. It was likely destroyed by the user.");
770 result
= ustcomm_send_reply(&ustcomm_app
.server
, "NOTFOUND", src
);
772 ERR("ustcomm_send_reply failed");
780 for(i
=0; i
<trace
->nr_channels
; i
++) {
781 struct ust_channel
*channel
= &trace
->channels
[i
];
783 if(!strcmp(trace
->channels
[i
].channel_name
, ch_name
)) {
784 struct ust_buffer
*buf
= channel
->buf
[ch_cpu
];
788 result
= ust_buffers_put_subbuf(buf
, consumed_old
);
790 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu
);
791 asprintf(&reply
, "%s", "ERROR");
794 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu
);
795 asprintf(&reply
, "%s", "OK");
798 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, src
);
800 ERR("ustcomm_send_reply failed");
811 ERR("unable to find channel");
816 free_short_chan_name
:
818 free_consumed_old_str
:
819 free(consumed_old_str
);
820 free_channel_and_cpu
:
821 free(channel_and_cpu
);
827 void *listener_main(void *p
)
831 ust_register_thread();
836 char trace_name
[] = "auto";
837 char trace_type
[] = "ustrelay";
840 struct ustcomm_source src
;
842 process_blocked_consumers();
844 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
846 WARN("error in ustcomm_app_recv_message");
849 else if(result
== 0) {
854 DBG("received a message! it's: %s", recvbuf
);
855 len
= strlen(recvbuf
);
857 if(!strcmp(recvbuf
, "print_markers")) {
858 print_markers(stderr
);
860 else if(!strcmp(recvbuf
, "list_markers")) {
865 fp
= open_memstream(&ptr
, &size
);
869 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
873 else if(!strcmp(recvbuf
, "start")) {
874 /* start is an operation that setups the trace, allocates it and starts it */
875 result
= ltt_trace_setup(trace_name
);
877 ERR("ltt_trace_setup failed");
881 result
= ltt_trace_set_type(trace_name
, trace_type
);
883 ERR("ltt_trace_set_type failed");
887 result
= ltt_trace_alloc(trace_name
);
889 ERR("ltt_trace_alloc failed");
893 inform_consumer_daemon(trace_name
);
895 result
= ltt_trace_start(trace_name
);
897 ERR("ltt_trace_start failed");
901 else if(!strcmp(recvbuf
, "trace_setup")) {
904 result
= ltt_trace_setup(trace_name
);
906 ERR("ltt_trace_setup failed");
910 result
= ltt_trace_set_type(trace_name
, trace_type
);
912 ERR("ltt_trace_set_type failed");
916 else if(!strcmp(recvbuf
, "trace_alloc")) {
919 result
= ltt_trace_alloc(trace_name
);
921 ERR("ltt_trace_alloc failed");
924 inform_consumer_daemon(trace_name
);
926 else if(!strcmp(recvbuf
, "trace_create")) {
929 result
= ltt_trace_setup(trace_name
);
931 ERR("ltt_trace_setup failed");
935 result
= ltt_trace_set_type(trace_name
, trace_type
);
937 ERR("ltt_trace_set_type failed");
941 else if(!strcmp(recvbuf
, "trace_start")) {
944 result
= ltt_trace_alloc(trace_name
);
946 ERR("ltt_trace_alloc failed");
950 inform_consumer_daemon(trace_name
);
953 result
= ltt_trace_start(trace_name
);
955 ERR("ltt_trace_start failed");
959 else if(!strcmp(recvbuf
, "trace_stop")) {
962 result
= ltt_trace_stop(trace_name
);
964 ERR("ltt_trace_stop failed");
968 else if(!strcmp(recvbuf
, "trace_destroy")) {
970 DBG("trace destroy");
972 result
= ltt_trace_destroy(trace_name
, 0);
974 ERR("ltt_trace_destroy failed");
978 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
979 do_cmd_get_shmid(recvbuf
, &src
);
981 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
982 do_cmd_get_n_subbufs(recvbuf
, &src
);
984 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
985 do_cmd_get_subbuf_size(recvbuf
, &src
);
987 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
990 libfile
= nth_token(recvbuf
, 1);
992 DBG("load_probe_lib loading %s", libfile
);
996 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
997 do_cmd_get_subbuffer(recvbuf
, &src
);
999 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
1000 do_cmd_put_subbuffer(recvbuf
, &src
);
1002 else if(nth_token_is(recvbuf
, "set_subbuf_size", 0) == 1) {
1003 do_cmd_set_subbuf_size(recvbuf
, &src
);
1005 else if(nth_token_is(recvbuf
, "set_subbuf_num", 0) == 1) {
1006 do_cmd_set_subbuf_num(recvbuf
, &src
);
1008 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
1009 char *channel_slash_name
= nth_token(recvbuf
, 1);
1010 char channel_name
[256]="";
1011 char marker_name
[256]="";
1013 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
1015 if(channel_name
== NULL
|| marker_name
== NULL
) {
1016 WARN("invalid marker name");
1020 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
1022 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
1025 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
1026 char *channel_slash_name
= nth_token(recvbuf
, 1);
1030 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
1032 if(marker_name
== NULL
) {
1035 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
1037 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
1040 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
1043 asprintf(&reply
, "%lld", pidunique
);
1045 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
1047 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1053 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1054 // struct ust_trace *trace;
1055 // char trace_name[] = "auto";
1057 // char *channel_name;
1059 // DBG("get_notifications");
1061 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1062 // if(channel_name == NULL) {
1063 // ERR("put_subbuf_size: cannot parse channel");
1067 // ltt_lock_traces();
1068 // trace = _ltt_trace_find(trace_name);
1069 // ltt_unlock_traces();
1071 // if(trace == NULL) {
1072 // ERR("cannot find trace!");
1073 // return (void *)1;
1076 // for(i=0; i<trace->nr_channels; i++) {
1077 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1080 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1081 // struct rchan_buf *rbuf = rchan->buf;
1082 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1084 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1085 // if(result == -1) {
1086 // ERR("ustcomm_app_detach_client failed");
1090 // lttbuf->wake_consumer_arg = (void *) fd;
1094 // lttbuf->call_wake_consumer = 1;
1100 // free(channel_name);
1103 ERR("unable to parse message: %s", recvbuf
);
1111 volatile sig_atomic_t have_listener
= 0;
1113 void create_listener(void)
1116 static char listener_stack
[16384];
1123 WARN("not creating listener because we already had one");
1128 result
= clone((int (*)(void *)) listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
1135 pthread_create(&thread
, NULL
, listener_main
, NULL
);
1141 static int init_socket(void)
1143 return ustcomm_init_app(getpid(), &ustcomm_app
);
1146 #define AUTOPROBE_DISABLED 0
1147 #define AUTOPROBE_ENABLE_ALL 1
1148 #define AUTOPROBE_ENABLE_REGEX 2
1149 static int autoprobe_method
= AUTOPROBE_DISABLED
;
1150 static regex_t autoprobe_regex
;
1152 static void auto_probe_connect(struct marker
*m
)
1156 char* concat_name
= NULL
;
1157 const char *probe_name
= "default";
1159 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
1162 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
1163 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
1165 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1166 m
->channel
, m
->name
);
1169 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
1176 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
1177 if(result
&& result
!= -EEXIST
)
1178 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
1180 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
1184 static void __attribute__((constructor
)) init()
1187 char* autoprobe_val
= NULL
;
1189 /* Assign the pidunique, to be able to differentiate the processes with same
1190 * pid, (before and after an exec).
1192 pidunique
= make_pidunique();
1194 /* Initialize RCU in case the constructor order is not good. */
1197 /* It is important to do this before events start to be generated. */
1198 ust_register_thread();
1200 DBG("Tracectl constructor");
1202 /* Must create socket before signal handler to prevent races.
1204 result
= init_socket();
1206 ERR("init_socket error");
1212 autoprobe_val
= getenv("UST_AUTOPROBE");
1214 struct marker_iter iter
;
1216 DBG("Autoprobe enabled.");
1218 /* Ensure markers are initialized */
1221 /* Ensure marker control is initialized, for the probe */
1222 init_marker_control();
1224 /* first, set the callback that will connect the
1225 * probe on new markers
1227 if(autoprobe_val
[0] == '/') {
1228 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
1232 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
1233 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
1234 /* don't crash the application just for this */
1237 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
1241 /* just enable all instrumentation */
1242 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
1245 marker_set_new_marker_cb(auto_probe_connect
);
1247 /* Now, connect the probes that were already registered. */
1248 marker_iter_reset(&iter
);
1249 marker_iter_start(&iter
);
1251 DBG("now iterating on markers already registered");
1252 while(iter
.marker
) {
1253 DBG("now iterating on marker %s", iter
.marker
->name
);
1254 auto_probe_connect(iter
.marker
);
1255 marker_iter_next(&iter
);
1259 if(getenv("UST_TRACE")) {
1260 char trace_name
[] = "auto";
1261 char trace_type
[] = "ustrelay";
1263 DBG("starting early tracing");
1265 /* Ensure marker control is initialized */
1266 init_marker_control();
1268 /* Ensure markers are initialized */
1271 /* Ensure buffers are initialized, for the transport to be available.
1272 * We are about to set a trace type and it will fail without this.
1274 init_ustrelay_transport();
1276 /* FIXME: When starting early tracing (here), depending on the
1277 * order of constructors, it is very well possible some marker
1278 * sections are not yet registered. Because of this, some
1279 * channels may not be registered. Yet, we are about to ask the
1280 * daemon to collect the channels. Channels which are not yet
1281 * registered will not be collected.
1283 * Currently, in LTTng, there is no way to add a channel after
1284 * trace start. The reason for this is that it induces complex
1285 * concurrency issues on the trace structures, which can only
1286 * be resolved using RCU. This has not been done yet. As a
1287 * workaround, we are forcing the registration of the "ust"
1288 * channel here. This is the only channel (apart from metadata)
1289 * that can be reliably used in early tracing.
1291 * Non-early tracing does not have this problem and can use
1292 * arbitrary channel names.
1294 ltt_channels_register("ust");
1296 result
= ltt_trace_setup(trace_name
);
1298 ERR("ltt_trace_setup failed");
1302 result
= ltt_trace_set_type(trace_name
, trace_type
);
1304 ERR("ltt_trace_set_type failed");
1308 result
= ltt_trace_alloc(trace_name
);
1310 ERR("ltt_trace_alloc failed");
1314 result
= ltt_trace_start(trace_name
);
1316 ERR("ltt_trace_start failed");
1320 /* Do this after the trace is started in order to avoid creating confusion
1321 * if the trace fails to start. */
1322 inform_consumer_daemon(trace_name
);
1328 /* should decrementally destroy stuff if error */
1332 /* This is only called if we terminate normally, not with an unhandled signal,
1333 * so we cannot rely on it. However, for now, LTTV requires that the header of
1334 * the last sub-buffer contain a valid end time for the trace. This is done
1335 * automatically only when the trace is properly stopped.
1337 * If the traced program crashed, it is always possible to manually add the
1338 * right value in the header, or to open the trace in text mode.
1340 * FIXME: Fix LTTV so it doesn't need this.
1343 static void destroy_traces(void)
1347 /* if trace running, finish it */
1349 DBG("destructor stopping traces");
1351 result
= ltt_trace_stop("auto");
1353 ERR("ltt_trace_stop error");
1356 result
= ltt_trace_destroy("auto", 0);
1358 ERR("ltt_trace_destroy error");
1362 static int trace_recording(void)
1365 struct ust_trace
*trace
;
1369 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1376 ltt_unlock_traces();
1382 static int have_consumer(void)
1384 return !list_empty(&blocked_consumers
);
1388 int restarting_usleep(useconds_t usecs
)
1394 tv
.tv_nsec
= usecs
* 1000;
1397 result
= nanosleep(&tv
, &tv
);
1398 } while(result
== -1 && errno
== EINTR
);
1403 /* This destructor keeps the process alive for a few seconds in order
1404 * to leave time to ustd to connect to its buffers. This is necessary
1405 * for programs whose execution is very short. It is also useful in all
1406 * programs when tracing is started close to the end of the program
1409 * FIXME: For now, this only works for the first trace created in a
1413 static void __attribute__((destructor
)) keepalive()
1415 if(trace_recording() && buffers_to_export
) {
1417 DBG("Keeping process alive for consumer daemon...");
1418 while(buffers_to_export
) {
1419 const int interv
= 200000;
1420 restarting_usleep(interv
);
1423 if(total
>= 3000000) {
1424 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1428 DBG("Finally dying...");
1433 ustcomm_fini_app(&ustcomm_app
);
1436 void ust_potential_exec(void)
1438 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1445 /* Notify ust that there was a fork. This needs to be called inside
1446 * the new process, anytime a process whose memory is not shared with
1447 * the parent is created. If this function is not called, the events
1448 * of the new process will not be collected.
1450 * Signals should be disabled before the fork and reenabled only after
1451 * this call in order to guarantee tracing is not started before ust_fork()
1452 * sanitizes the new process.
1455 static void ust_fork(void)
1457 struct blocked_consumer
*bc
;
1458 struct blocked_consumer
*deletable_bc
= NULL
;
1461 /* FIXME: technically, the locks could have been taken before the fork */
1462 DBG("ust: forking");
1464 /* break lock if necessary */
1465 ltt_unlock_traces();
1467 ltt_trace_stop("auto");
1468 ltt_trace_destroy("auto", 1);
1469 /* Delete all active connections */
1470 ustcomm_close_all_connections(&ustcomm_app
.server
);
1472 /* Delete all blocked consumers */
1473 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1474 close(bc
->fd_producer
);
1475 close(bc
->fd_consumer
);
1478 list_del(&bc
->list
);
1484 ltt_trace_setup("auto");
1485 result
= ltt_trace_set_type("auto", "ustrelay");
1487 ERR("ltt_trace_set_type failed");
1491 ltt_trace_alloc("auto");
1492 ltt_trace_start("auto");
1493 inform_consumer_daemon("auto");
1496 void ust_before_fork(ust_fork_info_t
*fork_info
)
1498 /* Disable signals. This is to avoid that the child
1499 * intervenes before it is properly setup for tracing. It is
1500 * safer to disable all signals, because then we know we are not
1501 * breaking anything by restoring the original mask.
1507 - only do this if tracing is active
1510 /* Disable signals */
1511 sigfillset(&all_sigs
);
1512 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1514 PERROR("sigprocmask");
1519 /* Don't call this function directly in a traced program */
1520 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1524 /* Restore signals */
1525 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1527 PERROR("sigprocmask");
1532 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1534 /* Reenable signals */
1535 ust_after_fork_common(fork_info
);
1538 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1540 /* First sanitize the child */
1543 /* Then reenable interrupts */
1544 ust_after_fork_common(fork_info
);