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>
37 #include "buffers.h" /* FIXME: remove */
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_do_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 ltt_trace_struct
*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 result
= ustcomm_request_consumer(pid
, trace
->channels
[i
].channel_name
);
184 WARN("Failed to request collection for channel %s. Is the daemon available?", trace
->channels
[i
].channel_name
);
185 /* continue even if fail */
194 void process_blocked_consumers(void)
198 struct blocked_consumer
*bc
;
203 list_for_each_entry(bc
, &blocked_consumers
, list
) {
207 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
209 ERR("malloc returned NULL");
213 list_for_each_entry(bc
, &blocked_consumers
, list
) {
214 fds
[idx
].fd
= bc
->fd_producer
;
215 fds
[idx
].events
= POLLIN
;
216 bc
->tmp_poll_idx
= idx
;
220 while((result
= poll(fds
, n_fds
, 0)) == -1 && errno
== EINTR
)
227 list_for_each_entry(bc
, &blocked_consumers
, list
) {
228 if(fds
[bc
->tmp_poll_idx
].revents
) {
229 long consumed_old
= 0;
232 result
= read(bc
->fd_producer
, &inbuf
, 1);
240 close(bc
->fd_producer
);
244 result
= ustcomm_send_reply(&bc
->server
, "END", &bc
->src
);
246 ERR("ustcomm_send_reply failed");
253 result
= ust_buffers_do_get_subbuf(bc
->buf
, &consumed_old
);
254 if(result
== -EAGAIN
) {
255 WARN("missed buffer?");
258 else if(result
< 0) {
259 DBG("ust_buffers_do_get_subbuf: error: %s", strerror(-result
));
261 asprintf(&reply
, "%s %ld", "OK", consumed_old
);
262 result
= ustcomm_send_reply(&bc
->server
, reply
, &bc
->src
);
264 ERR("ustcomm_send_reply failed");
276 void *listener_main(void *p
)
280 ust_register_thread();
285 char trace_name
[] = "auto";
286 char trace_type
[] = "ustrelay";
289 struct ustcomm_source src
;
291 process_blocked_consumers();
293 result
= ustcomm_app_recv_message(&ustcomm_app
, &recvbuf
, &src
, 5);
295 WARN("error in ustcomm_app_recv_message");
298 else if(result
== 0) {
303 DBG("received a message! it's: %s", recvbuf
);
304 len
= strlen(recvbuf
);
306 if(!strcmp(recvbuf
, "print_markers")) {
307 print_markers(stderr
);
309 else if(!strcmp(recvbuf
, "list_markers")) {
314 fp
= open_memstream(&ptr
, &size
);
318 result
= ustcomm_send_reply(&ustcomm_app
.server
, ptr
, &src
);
322 else if(!strcmp(recvbuf
, "start")) {
323 /* start is an operation that setups the trace, allocates it and starts it */
324 result
= ltt_trace_setup(trace_name
);
326 ERR("ltt_trace_setup failed");
330 result
= ltt_trace_set_type(trace_name
, trace_type
);
332 ERR("ltt_trace_set_type failed");
336 result
= ltt_trace_alloc(trace_name
);
338 ERR("ltt_trace_alloc failed");
342 inform_consumer_daemon(trace_name
);
344 result
= ltt_trace_start(trace_name
);
346 ERR("ltt_trace_start failed");
350 else if(!strcmp(recvbuf
, "trace_setup")) {
353 result
= ltt_trace_setup(trace_name
);
355 ERR("ltt_trace_setup failed");
359 result
= ltt_trace_set_type(trace_name
, trace_type
);
361 ERR("ltt_trace_set_type failed");
365 else if(!strcmp(recvbuf
, "trace_alloc")) {
368 result
= ltt_trace_alloc(trace_name
);
370 ERR("ltt_trace_alloc failed");
374 else if(!strcmp(recvbuf
, "trace_create")) {
377 result
= ltt_trace_setup(trace_name
);
379 ERR("ltt_trace_setup failed");
383 result
= ltt_trace_set_type(trace_name
, trace_type
);
385 ERR("ltt_trace_set_type failed");
389 result
= ltt_trace_alloc(trace_name
);
391 ERR("ltt_trace_alloc failed");
395 inform_consumer_daemon(trace_name
);
397 else if(!strcmp(recvbuf
, "trace_start")) {
400 result
= ltt_trace_start(trace_name
);
402 ERR("ltt_trace_start failed");
406 else if(!strcmp(recvbuf
, "trace_stop")) {
409 result
= ltt_trace_stop(trace_name
);
411 ERR("ltt_trace_stop failed");
415 else if(!strcmp(recvbuf
, "trace_destroy")) {
417 DBG("trace destroy");
419 result
= ltt_trace_destroy(trace_name
);
421 ERR("ltt_trace_destroy failed");
425 else if(nth_token_is(recvbuf
, "get_shmid", 0) == 1) {
426 struct ltt_trace_struct
*trace
;
427 char trace_name
[] = "auto";
433 channel_name
= nth_token(recvbuf
, 1);
434 if(channel_name
== NULL
) {
435 ERR("get_shmid: cannot parse channel");
440 trace
= _ltt_trace_find(trace_name
);
444 ERR("cannot find trace!");
448 for(i
=0; i
<trace
->nr_channels
; i
++) {
449 struct ust_channel
*channel
= &trace
->channels
[i
];
450 struct ust_buffer
*buf
= channel
->buf
;
452 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
455 DBG("the shmid for the requested channel is %d", buf
->shmid
);
456 DBG("the shmid for its buffer structure is %d", channel
->buf_shmid
);
457 asprintf(&reply
, "%d %d", buf
->shmid
, channel
->buf_shmid
);
459 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
461 ERR("listener: get_shmid: ustcomm_send_reply failed");
473 else if(nth_token_is(recvbuf
, "get_n_subbufs", 0) == 1) {
474 struct ltt_trace_struct
*trace
;
475 char trace_name
[] = "auto";
479 DBG("get_n_subbufs");
481 channel_name
= nth_token(recvbuf
, 1);
482 if(channel_name
== NULL
) {
483 ERR("get_n_subbufs: cannot parse channel");
488 trace
= _ltt_trace_find(trace_name
);
492 ERR("cannot find trace!");
496 for(i
=0; i
<trace
->nr_channels
; i
++) {
497 struct ust_channel
*channel
= &trace
->channels
[i
];
499 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
502 DBG("the n_subbufs for the requested channel is %d", channel
->subbuf_cnt
);
503 asprintf(&reply
, "%d", channel
->subbuf_cnt
);
505 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
507 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
517 else if(nth_token_is(recvbuf
, "get_subbuf_size", 0) == 1) {
518 struct ltt_trace_struct
*trace
;
519 char trace_name
[] = "auto";
523 DBG("get_subbuf_size");
525 channel_name
= nth_token(recvbuf
, 1);
526 if(channel_name
== NULL
) {
527 ERR("get_subbuf_size: cannot parse channel");
532 trace
= _ltt_trace_find(trace_name
);
536 ERR("cannot find trace!");
540 for(i
=0; i
<trace
->nr_channels
; i
++) {
541 struct ust_channel
*channel
= &trace
->channels
[i
];
543 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
546 DBG("the subbuf_size for the requested channel is %zd", channel
->subbuf_size
);
547 asprintf(&reply
, "%zd", channel
->subbuf_size
);
549 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
551 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
561 else if(nth_token_is(recvbuf
, "load_probe_lib", 0) == 1) {
564 libfile
= nth_token(recvbuf
, 1);
566 DBG("load_probe_lib loading %s", libfile
);
568 else if(nth_token_is(recvbuf
, "get_subbuffer", 0) == 1) {
569 struct ltt_trace_struct
*trace
;
570 char trace_name
[] = "auto";
576 channel_name
= nth_token(recvbuf
, 1);
577 if(channel_name
== NULL
) {
578 ERR("get_subbuf: cannot parse channel");
583 trace
= _ltt_trace_find(trace_name
);
587 ERR("cannot find trace!");
591 for(i
=0; i
<trace
->nr_channels
; i
++) {
592 struct ust_channel
*channel
= &trace
->channels
[i
];
594 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
595 struct ust_buffer
*buf
= channel
->buf
;
596 struct blocked_consumer
*bc
;
598 bc
= (struct blocked_consumer
*) malloc(sizeof(struct blocked_consumer
));
600 ERR("malloc returned NULL");
603 bc
->fd_consumer
= src
.fd
;
604 bc
->fd_producer
= buf
->data_ready_fd_read
;
607 bc
->server
= ustcomm_app
.server
;
609 list_add(&bc
->list
, &blocked_consumers
);
615 else if(nth_token_is(recvbuf
, "put_subbuffer", 0) == 1) {
616 struct ltt_trace_struct
*trace
;
617 char trace_name
[] = "auto";
621 char *consumed_old_str
;
626 channel_name
= strdup_malloc(nth_token(recvbuf
, 1));
627 if(channel_name
== NULL
) {
628 ERR("put_subbuf_size: cannot parse channel");
632 consumed_old_str
= strdup_malloc(nth_token(recvbuf
, 2));
633 if(consumed_old_str
== NULL
) {
634 ERR("put_subbuf: cannot parse consumed_old");
637 consumed_old
= strtol(consumed_old_str
, &endptr
, 10);
638 if(*endptr
!= '\0') {
639 ERR("put_subbuf: invalid value for consumed_old");
644 trace
= _ltt_trace_find(trace_name
);
648 ERR("cannot find trace!");
652 for(i
=0; i
<trace
->nr_channels
; i
++) {
653 struct ust_channel
*channel
= &trace
->channels
[i
];
655 if(!strcmp(trace
->channels
[i
].channel_name
, channel_name
)) {
656 struct ust_buffer
*buf
= channel
->buf
;
660 result
= ust_buffers_do_put_subbuf(buf
, consumed_old
);
662 WARN("ust_buffers_do_put_subbuf: error (subbuf=%s)", channel_name
);
663 asprintf(&reply
, "%s", "ERROR");
666 DBG("ust_buffers_do_put_subbuf: success (subbuf=%s)", channel_name
);
667 asprintf(&reply
, "%s", "OK");
670 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
672 ERR("listener: put_subbuf: ustcomm_send_reply failed");
683 free(consumed_old_str
);
685 else if(nth_token_is(recvbuf
, "enable_marker", 0) == 1) {
686 char *channel_slash_name
= nth_token(recvbuf
, 1);
687 char channel_name
[256]="";
688 char marker_name
[256]="";
690 result
= sscanf(channel_slash_name
, "%255[^/]/%255s", channel_name
, marker_name
);
692 if(channel_name
== NULL
|| marker_name
== NULL
) {
693 WARN("invalid marker name");
697 result
= ltt_marker_connect(channel_name
, marker_name
, "default");
699 WARN("could not enable marker; channel=%s, name=%s", channel_name
, marker_name
);
702 else if(nth_token_is(recvbuf
, "disable_marker", 0) == 1) {
703 char *channel_slash_name
= nth_token(recvbuf
, 1);
707 result
= sscanf(channel_slash_name
, "%a[^/]/%as", &channel_name
, &marker_name
);
709 if(marker_name
== NULL
) {
712 result
= ltt_marker_disconnect(channel_name
, marker_name
, "default");
714 WARN("could not disable marker; channel=%s, name=%s", channel_name
, marker_name
);
717 else if(nth_token_is(recvbuf
, "get_pidunique", 0) == 1) {
720 asprintf(&reply
, "%lld", pidunique
);
722 result
= ustcomm_send_reply(&ustcomm_app
.server
, reply
, &src
);
724 ERR("listener: get_pidunique: ustcomm_send_reply failed");
730 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
731 // struct ltt_trace_struct *trace;
732 // char trace_name[] = "auto";
734 // char *channel_name;
736 // DBG("get_notifications");
738 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
739 // if(channel_name == NULL) {
740 // ERR("put_subbuf_size: cannot parse channel");
744 // ltt_lock_traces();
745 // trace = _ltt_trace_find(trace_name);
746 // ltt_unlock_traces();
748 // if(trace == NULL) {
749 // ERR("cannot find trace!");
753 // for(i=0; i<trace->nr_channels; i++) {
754 // struct rchan *rchan = trace->channels[i].trans_channel_data;
757 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
758 // struct rchan_buf *rbuf = rchan->buf;
759 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
761 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
762 // if(result == -1) {
763 // ERR("ustcomm_app_detach_client failed");
767 // lttbuf->wake_consumer_arg = (void *) fd;
771 // lttbuf->call_wake_consumer = 1;
777 // free(channel_name);
780 ERR("unable to parse message: %s", recvbuf
);
788 volatile sig_atomic_t have_listener
= 0;
790 void create_listener(void)
793 static char listener_stack
[16384];
800 WARN("not creating listener because we already had one");
805 result
= clone((int (*)(void *)) listener_main
, listener_stack
+sizeof(listener_stack
)-1, CLONE_FS
| CLONE_FILES
| CLONE_VM
| CLONE_SIGHAND
| CLONE_THREAD
, NULL
);
812 pthread_create(&thread
, NULL
, listener_main
, NULL
);
818 static int init_socket(void)
820 return ustcomm_init_app(getpid(), &ustcomm_app
);
823 #define AUTOPROBE_DISABLED 0
824 #define AUTOPROBE_ENABLE_ALL 1
825 #define AUTOPROBE_ENABLE_REGEX 2
826 static int autoprobe_method
= AUTOPROBE_DISABLED
;
827 static regex_t autoprobe_regex
;
829 static void auto_probe_connect(struct marker
*m
)
833 char* concat_name
= NULL
;
834 const char *probe_name
= "default";
836 if(autoprobe_method
== AUTOPROBE_DISABLED
) {
839 else if(autoprobe_method
== AUTOPROBE_ENABLE_REGEX
) {
840 result
= asprintf(&concat_name
, "%s/%s", m
->channel
, m
->name
);
842 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
843 m
->channel
, m
->name
);
846 if (regexec(&autoprobe_regex
, concat_name
, 0, NULL
, 0)) {
853 result
= ltt_marker_connect(m
->channel
, m
->name
, probe_name
);
854 if(result
&& result
!= -EEXIST
)
855 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m
->channel
, m
->name
, -result
);
857 DBG("auto connected marker %s (addr: %p) %s to probe default", m
->channel
, m
, m
->name
);
861 static void __attribute__((constructor
)) init()
864 char* autoprobe_val
= NULL
;
866 /* Assign the pidunique, to be able to differentiate the processes with same
867 * pid, (before and after an exec).
869 pidunique
= make_pidunique();
871 /* Initialize RCU in case the constructor order is not good. */
874 /* It is important to do this before events start to be generated. */
875 ust_register_thread();
877 DBG("Tracectl constructor");
879 /* Must create socket before signal handler to prevent races.
881 result
= init_socket();
883 ERR("init_socket error");
889 autoprobe_val
= getenv("UST_AUTOPROBE");
891 struct marker_iter iter
;
893 DBG("Autoprobe enabled.");
895 /* Ensure markers are initialized */
898 /* Ensure marker control is initialized, for the probe */
899 init_marker_control();
901 /* first, set the callback that will connect the
902 * probe on new markers
904 if(autoprobe_val
[0] == '/') {
905 result
= regcomp(&autoprobe_regex
, autoprobe_val
+1, 0);
909 regerror(result
, &autoprobe_regex
, regexerr
, sizeof(regexerr
));
910 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val
, regexerr
);
911 /* don't crash the application just for this */
914 autoprobe_method
= AUTOPROBE_ENABLE_REGEX
;
918 /* just enable all instrumentation */
919 autoprobe_method
= AUTOPROBE_ENABLE_ALL
;
922 marker_set_new_marker_cb(auto_probe_connect
);
924 /* Now, connect the probes that were already registered. */
925 marker_iter_reset(&iter
);
926 marker_iter_start(&iter
);
928 DBG("now iterating on markers already registered");
930 DBG("now iterating on marker %s", iter
.marker
->name
);
931 auto_probe_connect(iter
.marker
);
932 marker_iter_next(&iter
);
936 if(getenv("UST_TRACE")) {
937 char trace_name
[] = "auto";
938 char trace_type
[] = "ustrelay";
940 DBG("starting early tracing");
942 /* Ensure marker control is initialized */
943 init_marker_control();
945 /* Ensure relay is initialized */
946 init_ustrelay_transport();
948 /* Ensure markers are initialized */
951 /* FIXME: When starting early tracing (here), depending on the
952 * order of constructors, it is very well possible some marker
953 * sections are not yet registered. Because of this, some
954 * channels may not be registered. Yet, we are about to ask the
955 * daemon to collect the channels. Channels which are not yet
956 * registered will not be collected.
958 * Currently, in LTTng, there is no way to add a channel after
959 * trace start. The reason for this is that it induces complex
960 * concurrency issues on the trace structures, which can only
961 * be resolved using RCU. This has not been done yet. As a
962 * workaround, we are forcing the registration of the "ust"
963 * channel here. This is the only channel (apart from metadata)
964 * that can be reliably used in early tracing.
966 * Non-early tracing does not have this problem and can use
967 * arbitrary channel names.
969 ltt_channels_register("ust");
971 result
= ltt_trace_setup(trace_name
);
973 ERR("ltt_trace_setup failed");
977 result
= ltt_trace_set_type(trace_name
, trace_type
);
979 ERR("ltt_trace_set_type failed");
983 result
= ltt_trace_alloc(trace_name
);
985 ERR("ltt_trace_alloc failed");
989 result
= ltt_trace_start(trace_name
);
991 ERR("ltt_trace_start failed");
995 /* Do this after the trace is started in order to avoid creating confusion
996 * if the trace fails to start. */
997 inform_consumer_daemon(trace_name
);
1003 /* should decrementally destroy stuff if error */
1007 /* This is only called if we terminate normally, not with an unhandled signal,
1008 * so we cannot rely on it. However, for now, LTTV requires that the header of
1009 * the last sub-buffer contain a valid end time for the trace. This is done
1010 * automatically only when the trace is properly stopped.
1012 * If the traced program crashed, it is always possible to manually add the
1013 * right value in the header, or to open the trace in text mode.
1015 * FIXME: Fix LTTV so it doesn't need this.
1018 static void destroy_traces(void)
1022 /* if trace running, finish it */
1024 DBG("destructor stopping traces");
1026 result
= ltt_trace_stop("auto");
1028 ERR("ltt_trace_stop error");
1031 result
= ltt_trace_destroy("auto");
1033 ERR("ltt_trace_destroy error");
1037 static int trace_recording(void)
1040 struct ltt_trace_struct
*trace
;
1044 list_for_each_entry(trace
, <t_traces
.head
, list
) {
1051 ltt_unlock_traces();
1057 static int have_consumer(void)
1059 return !list_empty(&blocked_consumers
);
1063 int restarting_usleep(useconds_t usecs
)
1069 tv
.tv_nsec
= usecs
* 1000;
1072 result
= nanosleep(&tv
, &tv
);
1073 } while(result
== -1 && errno
== EINTR
);
1078 /* This destructor keeps the process alive for a few seconds in order
1079 * to leave time to ustd to connect to its buffers. This is necessary
1080 * for programs whose execution is very short. It is also useful in all
1081 * programs when tracing is started close to the end of the program
1084 * FIXME: For now, this only works for the first trace created in a
1088 static void __attribute__((destructor
)) keepalive()
1090 if(trace_recording() && buffers_to_export
) {
1092 DBG("Keeping process alive for consumer daemon...");
1093 while(buffers_to_export
) {
1094 const int interv
= 200000;
1095 restarting_usleep(interv
);
1098 if(total
>= 3000000) {
1099 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1103 DBG("Finally dying...");
1108 ustcomm_fini_app(&ustcomm_app
);
1111 void ust_potential_exec(void)
1113 trace_mark(ust
, potential_exec
, MARK_NOARGS
);
1120 /* Notify ust that there was a fork. This needs to be called inside
1121 * the new process, anytime a process whose memory is not shared with
1122 * the parent is created. If this function is not called, the events
1123 * of the new process will not be collected.
1125 * Signals should be disabled before the fork and reenabled only after
1126 * this call in order to guarantee tracing is not started before ust_fork()
1127 * sanitizes the new process.
1130 static void ust_fork(void)
1132 struct blocked_consumer
*bc
;
1133 struct blocked_consumer
*deletable_bc
= NULL
;
1136 DBG("ust: forking");
1137 ltt_trace_stop("auto");
1138 ltt_trace_destroy("auto");
1139 /* Delete all active connections */
1140 ustcomm_close_all_connections(&ustcomm_app
.server
);
1142 /* Delete all blocked consumers */
1143 list_for_each_entry(bc
, &blocked_consumers
, list
) {
1144 close(bc
->fd_producer
);
1145 close(bc
->fd_consumer
);
1148 list_del(&bc
->list
);
1154 ltt_trace_setup("auto");
1155 result
= ltt_trace_set_type("auto", "ustrelay");
1157 ERR("ltt_trace_set_type failed");
1161 ltt_trace_alloc("auto");
1162 ltt_trace_start("auto");
1163 inform_consumer_daemon("auto");
1166 void ust_before_fork(ust_fork_info_t
*fork_info
)
1168 /* Disable signals. This is to avoid that the child
1169 * intervenes before it is properly setup for tracing. It is
1170 * safer to disable all signals, because then we know we are not
1171 * breaking anything by restoring the original mask.
1177 - only do this if tracing is active
1180 /* Disable signals */
1181 sigfillset(&all_sigs
);
1182 result
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
1184 PERROR("sigprocmask");
1189 /* Don't call this function directly in a traced program */
1190 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
1195 /* Restore signals */
1196 result
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
1198 PERROR("sigprocmask");
1203 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
1205 /* Reenable signals */
1206 ust_after_fork_common(fork_info
);
1209 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
1211 /* First sanitize the child */
1214 /* Then reenable interrupts */
1215 ust_after_fork_common(fork_info
);