Use liburcu-bp before/after fork (parent/child) callbacks
[ust.git] / libust / tracectl.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier
2 *
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.
7 *
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.
12 *
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
16 */
17
18 /* This file contains the implementation of the UST listener thread, which
19 * receives trace control commands. It also coordinates the initialization of
20 * libust.
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <sys/epoll.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <fcntl.h>
34 #include <poll.h>
35 #include <regex.h>
36 #include <urcu/uatomic_arch.h>
37 #include <urcu/list.h>
38
39 #include <ust/marker.h>
40 #include <ust/tracepoint.h>
41 #include <ust/tracectl.h>
42 #include <ust/clock.h>
43 #include "tracer.h"
44 #include "usterr.h"
45 #include "ustcomm.h"
46 #include "buffers.h"
47 #include "marker-control.h"
48
49 /* This should only be accessed by the constructor, before the creation
50 * of the listener, and then only by the listener.
51 */
52 s64 pidunique = -1LL;
53
54 /* The process pid is used to detect a non-traceable fork
55 * and allow the non-traceable fork to be ignored
56 * by destructor sequences in libust
57 */
58 static pid_t processpid = 0;
59
60 static struct ustcomm_header _receive_header;
61 static struct ustcomm_header *receive_header = &_receive_header;
62 static char receive_buffer[USTCOMM_BUFFER_SIZE];
63 static char send_buffer[USTCOMM_BUFFER_SIZE];
64
65 static int epoll_fd;
66
67 /*
68 * Listener thread data vs fork() protection mechanism. Ensures that no listener
69 * thread mutexes and data structures are being concurrently modified or held by
70 * other threads when fork() is executed.
71 */
72 static pthread_mutex_t listener_thread_data_mutex = PTHREAD_MUTEX_INITIALIZER;
73
74 /* Mutex protecting listen_sock. Nests inside listener_thread_data_mutex. */
75 static pthread_mutex_t listen_sock_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static struct ustcomm_sock *listen_sock;
77
78 extern struct chan_info_struct chan_infos[];
79
80 static struct cds_list_head open_buffers_list = CDS_LIST_HEAD_INIT(open_buffers_list);
81
82 static struct cds_list_head ust_socks = CDS_LIST_HEAD_INIT(ust_socks);
83
84 /* volatile because shared between the listener and the main thread */
85 int buffers_to_export = 0;
86
87 int ust_clock_source;
88
89 static long long make_pidunique(void)
90 {
91 s64 retval;
92 struct timeval tv;
93
94 gettimeofday(&tv, NULL);
95
96 retval = tv.tv_sec;
97 retval <<= 32;
98 retval |= tv.tv_usec;
99
100 return retval;
101 }
102
103 static void print_markers(FILE *fp)
104 {
105 struct marker_iter iter;
106
107 lock_markers();
108 marker_iter_reset(&iter);
109 marker_iter_start(&iter);
110
111 while (iter.marker) {
112 fprintf(fp, "marker: %s/%s %d \"%s\" %p\n",
113 (*iter.marker)->channel,
114 (*iter.marker)->name,
115 (int)imv_read((*iter.marker)->state),
116 (*iter.marker)->format,
117 (*iter.marker)->location);
118 marker_iter_next(&iter);
119 }
120 unlock_markers();
121 }
122
123 static void print_trace_events(FILE *fp)
124 {
125 struct trace_event_iter iter;
126
127 lock_trace_events();
128 trace_event_iter_reset(&iter);
129 trace_event_iter_start(&iter);
130
131 while (iter.trace_event) {
132 fprintf(fp, "trace_event: %s\n", (*iter.trace_event)->name);
133 trace_event_iter_next(&iter);
134 }
135 unlock_trace_events();
136 }
137
138 static int connect_ustconsumer(void)
139 {
140 int result, fd;
141 char default_daemon_path[] = SOCK_DIR "/ustconsumer";
142 char *explicit_daemon_path, *daemon_path;
143
144 explicit_daemon_path = getenv("UST_DAEMON_SOCKET");
145 if (explicit_daemon_path) {
146 daemon_path = explicit_daemon_path;
147 } else {
148 daemon_path = default_daemon_path;
149 }
150
151 DBG("Connecting to daemon_path %s", daemon_path);
152
153 result = ustcomm_connect_path(daemon_path, &fd);
154 if (result < 0) {
155 WARN("connect_ustconsumer failed, daemon_path: %s",
156 daemon_path);
157 return result;
158 }
159
160 return fd;
161 }
162
163
164 static void request_buffer_consumer(int sock,
165 const char *trace,
166 const char *channel,
167 int cpu)
168 {
169 struct ustcomm_header send_header, recv_header;
170 struct ustcomm_buffer_info buf_inf;
171 int result = 0;
172
173 result = ustcomm_pack_buffer_info(&send_header,
174 &buf_inf,
175 trace,
176 channel,
177 cpu);
178
179 if (result < 0) {
180 ERR("failed to pack buffer info message %s_%d",
181 channel, cpu);
182 return;
183 }
184
185 buf_inf.pid = getpid();
186 send_header.command = CONSUME_BUFFER;
187
188 result = ustcomm_req(sock, &send_header, (char *) &buf_inf,
189 &recv_header, NULL);
190 if (result <= 0) {
191 PERROR("request for buffer consumer failed, is the daemon online?");
192 }
193
194 return;
195 }
196
197 /* Ask the daemon to collect a trace called trace_name and being
198 * produced by this pid.
199 *
200 * The trace must be at least allocated. (It can also be started.)
201 * This is because _ltt_trace_find is used.
202 */
203
204 static void inform_consumer_daemon(const char *trace_name)
205 {
206 int sock, i,j;
207 struct ust_trace *trace;
208 const char *ch_name;
209
210 sock = connect_ustconsumer();
211 if (sock < 0) {
212 return;
213 }
214
215 DBG("Connected to ustconsumer");
216
217 ltt_lock_traces();
218
219 trace = _ltt_trace_find(trace_name);
220 if (trace == NULL) {
221 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
222 goto unlock_traces;
223 }
224
225 for (i=0; i < trace->nr_channels; i++) {
226 if (trace->channels[i].request_collection) {
227 /* iterate on all cpus */
228 for (j=0; j<trace->channels[i].n_cpus; j++) {
229 ch_name = trace->channels[i].channel_name;
230 request_buffer_consumer(sock, trace_name,
231 ch_name, j);
232 CMM_STORE_SHARED(buffers_to_export,
233 CMM_LOAD_SHARED(buffers_to_export)+1);
234 }
235 }
236 }
237
238 unlock_traces:
239 ltt_unlock_traces();
240
241 close(sock);
242 }
243
244 static struct ust_channel *find_channel(const char *ch_name,
245 struct ust_trace *trace)
246 {
247 int i;
248
249 for (i=0; i<trace->nr_channels; i++) {
250 if (!strcmp(trace->channels[i].channel_name, ch_name)) {
251 return &trace->channels[i];
252 }
253 }
254
255 return NULL;
256 }
257
258 static int get_buffer_shmid_pipe_fd(const char *trace_name, const char *ch_name,
259 int ch_cpu,
260 int *buf_shmid,
261 int *buf_struct_shmid,
262 int *buf_pipe_fd)
263 {
264 struct ust_trace *trace;
265 struct ust_channel *channel;
266 struct ust_buffer *buf;
267
268 DBG("get_buffer_shmid_pipe_fd");
269
270 ltt_lock_traces();
271 trace = _ltt_trace_find(trace_name);
272 ltt_unlock_traces();
273
274 if (trace == NULL) {
275 ERR("cannot find trace!");
276 return -ENODATA;
277 }
278
279 channel = find_channel(ch_name, trace);
280 if (!channel) {
281 ERR("cannot find channel %s!", ch_name);
282 return -ENODATA;
283 }
284
285 buf = channel->buf[ch_cpu];
286
287 *buf_shmid = buf->shmid;
288 *buf_struct_shmid = channel->buf_struct_shmids[ch_cpu];
289 *buf_pipe_fd = buf->data_ready_fd_read;
290
291 return 0;
292 }
293
294 static int get_subbuf_num_size(const char *trace_name, const char *ch_name,
295 int *num, int *size)
296 {
297 struct ust_trace *trace;
298 struct ust_channel *channel;
299
300 DBG("get_subbuf_size");
301
302 ltt_lock_traces();
303 trace = _ltt_trace_find(trace_name);
304 ltt_unlock_traces();
305
306 if (!trace) {
307 ERR("cannot find trace!");
308 return -ENODATA;
309 }
310
311 channel = find_channel(ch_name, trace);
312 if (!channel) {
313 ERR("unable to find channel");
314 return -ENODATA;
315 }
316
317 *num = channel->subbuf_cnt;
318 *size = channel->subbuf_size;
319
320 return 0;
321 }
322
323 /* Return the power of two which is equal or higher to v */
324
325 static unsigned int pow2_higher_or_eq(unsigned int v)
326 {
327 int hb = fls(v);
328 int retval = 1<<(hb-1);
329
330 if (v-retval == 0)
331 return retval;
332 else
333 return retval<<1;
334 }
335
336 static int set_subbuf_size(const char *trace_name, const char *ch_name,
337 unsigned int size)
338 {
339 unsigned int power;
340 int retval = 0;
341 struct ust_trace *trace;
342 struct ust_channel *channel;
343
344 DBG("set_subbuf_size");
345
346 power = pow2_higher_or_eq(size);
347 power = max_t(unsigned int, 2u, power);
348 if (power != size) {
349 WARN("using the next power of two for buffer size = %u\n", power);
350 }
351
352 ltt_lock_traces();
353 trace = _ltt_trace_find_setup(trace_name);
354 if (trace == NULL) {
355 ERR("cannot find trace!");
356 retval = -ENODATA;
357 goto unlock_traces;
358 }
359
360 channel = find_channel(ch_name, trace);
361 if (!channel) {
362 ERR("unable to find channel");
363 retval = -ENODATA;
364 goto unlock_traces;
365 }
366
367 channel->subbuf_size = power;
368 DBG("the set_subbuf_size for the requested channel is %zu", channel->subbuf_size);
369
370 unlock_traces:
371 ltt_unlock_traces();
372
373 return retval;
374 }
375
376 static int set_subbuf_num(const char *trace_name, const char *ch_name,
377 unsigned int num)
378 {
379 struct ust_trace *trace;
380 struct ust_channel *channel;
381 int retval = 0;
382
383 DBG("set_subbuf_num");
384
385 if (num < 2) {
386 ERR("subbuffer count should be greater than 2");
387 return -EINVAL;
388 }
389
390 ltt_lock_traces();
391 trace = _ltt_trace_find_setup(trace_name);
392 if (trace == NULL) {
393 ERR("cannot find trace!");
394 retval = -ENODATA;
395 goto unlock_traces;
396 }
397
398 channel = find_channel(ch_name, trace);
399 if (!channel) {
400 ERR("unable to find channel");
401 retval = -ENODATA;
402 goto unlock_traces;
403 }
404
405 channel->subbuf_cnt = num;
406 DBG("the set_subbuf_cnt for the requested channel is %u", channel->subbuf_cnt);
407
408 unlock_traces:
409 ltt_unlock_traces();
410 return retval;
411 }
412
413 static int get_subbuffer(const char *trace_name, const char *ch_name,
414 int ch_cpu, long *consumed_old)
415 {
416 int retval = 0;
417 struct ust_trace *trace;
418 struct ust_channel *channel;
419 struct ust_buffer *buf;
420
421 DBG("get_subbuf");
422
423 *consumed_old = 0;
424
425 ltt_lock_traces();
426 trace = _ltt_trace_find(trace_name);
427
428 if (!trace) {
429 DBG("Cannot find trace. It was likely destroyed by the user.");
430 retval = -ENODATA;
431 goto unlock_traces;
432 }
433
434 channel = find_channel(ch_name, trace);
435 if (!channel) {
436 ERR("unable to find channel");
437 retval = -ENODATA;
438 goto unlock_traces;
439 }
440
441 buf = channel->buf[ch_cpu];
442
443 retval = ust_buffers_get_subbuf(buf, consumed_old);
444 if (retval < 0) {
445 WARN("missed buffer?");
446 }
447
448 unlock_traces:
449 ltt_unlock_traces();
450
451 return retval;
452 }
453
454
455 static int notify_buffer_mapped(const char *trace_name,
456 const char *ch_name,
457 int ch_cpu)
458 {
459 int retval = 0;
460 struct ust_trace *trace;
461 struct ust_channel *channel;
462 struct ust_buffer *buf;
463
464 DBG("get_buffer_fd");
465
466 ltt_lock_traces();
467 trace = _ltt_trace_find(trace_name);
468
469 if (!trace) {
470 retval = -ENODATA;
471 DBG("Cannot find trace. It was likely destroyed by the user.");
472 goto unlock_traces;
473 }
474
475 channel = find_channel(ch_name, trace);
476 if (!channel) {
477 retval = -ENODATA;
478 ERR("unable to find channel");
479 goto unlock_traces;
480 }
481
482 buf = channel->buf[ch_cpu];
483
484 /* Being here is the proof the daemon has mapped the buffer in its
485 * memory. We may now decrement buffers_to_export.
486 */
487 if (uatomic_read(&buf->consumed) == 0) {
488 DBG("decrementing buffers_to_export");
489 CMM_STORE_SHARED(buffers_to_export, CMM_LOAD_SHARED(buffers_to_export)-1);
490 }
491
492 /* The buffer has been exported, ergo, we can add it to the
493 * list of open buffers
494 */
495 cds_list_add(&buf->open_buffers_list, &open_buffers_list);
496
497 unlock_traces:
498 ltt_unlock_traces();
499
500 return retval;
501 }
502
503 static int put_subbuffer(const char *trace_name, const char *ch_name,
504 int ch_cpu, long consumed_old)
505 {
506 int retval = 0;
507 struct ust_trace *trace;
508 struct ust_channel *channel;
509 struct ust_buffer *buf;
510
511 DBG("put_subbuf");
512
513 ltt_lock_traces();
514 trace = _ltt_trace_find(trace_name);
515
516 if (!trace) {
517 retval = -ENODATA;
518 DBG("Cannot find trace. It was likely destroyed by the user.");
519 goto unlock_traces;
520 }
521
522 channel = find_channel(ch_name, trace);
523 if (!channel) {
524 retval = -ENODATA;
525 ERR("unable to find channel");
526 goto unlock_traces;
527 }
528
529 buf = channel->buf[ch_cpu];
530
531 retval = ust_buffers_put_subbuf(buf, consumed_old);
532 if (retval < 0) {
533 WARN("ust_buffers_put_subbuf: error (subbuf=%s_%d)",
534 ch_name, ch_cpu);
535 } else {
536 DBG("ust_buffers_put_subbuf: success (subbuf=%s_%d)",
537 ch_name, ch_cpu);
538 }
539
540 unlock_traces:
541 ltt_unlock_traces();
542
543 return retval;
544 }
545
546 static void release_listener_mutex(void *ptr)
547 {
548 pthread_mutex_unlock(&listener_thread_data_mutex);
549 }
550
551 static void listener_cleanup(void *ptr)
552 {
553 pthread_mutex_lock(&listen_sock_mutex);
554 if (listen_sock) {
555 ustcomm_del_named_sock(listen_sock, 0);
556 listen_sock = NULL;
557 }
558 pthread_mutex_unlock(&listen_sock_mutex);
559 }
560
561 static void force_subbuf_switch()
562 {
563 struct ust_buffer *buf;
564
565 cds_list_for_each_entry(buf, &open_buffers_list,
566 open_buffers_list) {
567 ltt_force_switch(buf, FORCE_FLUSH);
568 }
569 }
570
571 /* Simple commands are those which need only respond with a return value. */
572 static int process_simple_client_cmd(int command, char *recv_buf)
573 {
574 int result;
575
576 switch(command) {
577 case SET_SOCK_PATH:
578 {
579 struct ustcomm_single_field *sock_msg;
580 sock_msg = (struct ustcomm_single_field *)recv_buf;
581 result = ustcomm_unpack_single_field(sock_msg);
582 if (result < 0) {
583 return result;
584 }
585 return setenv("UST_DAEMON_SOCKET", sock_msg->field, 1);
586 }
587
588 case FORCE_SUBBUF_SWITCH:
589 /* FIXME: return codes? */
590 force_subbuf_switch();
591
592 break;
593
594 default:
595 return -EINVAL;
596 }
597
598 return 0;
599 }
600
601
602 static int process_trace_cmd(int command, char *trace_name)
603 {
604 int result;
605 char trace_type[] = "ustrelay";
606
607 switch(command) {
608 case START:
609 /* start is an operation that setups the trace, allocates it and starts it */
610 result = ltt_trace_setup(trace_name);
611 if (result < 0) {
612 ERR("ltt_trace_setup failed");
613 return result;
614 }
615
616 result = ltt_trace_set_type(trace_name, trace_type);
617 if (result < 0) {
618 ERR("ltt_trace_set_type failed");
619 return result;
620 }
621
622 result = ltt_trace_alloc(trace_name);
623 if (result < 0) {
624 ERR("ltt_trace_alloc failed");
625 return result;
626 }
627
628 inform_consumer_daemon(trace_name);
629
630 result = ltt_trace_start(trace_name);
631 if (result < 0) {
632 ERR("ltt_trace_start failed");
633 return result;
634 }
635
636 return 0;
637 case SETUP_TRACE:
638 DBG("trace setup");
639
640 result = ltt_trace_setup(trace_name);
641 if (result < 0) {
642 ERR("ltt_trace_setup failed");
643 return result;
644 }
645
646 result = ltt_trace_set_type(trace_name, trace_type);
647 if (result < 0) {
648 ERR("ltt_trace_set_type failed");
649 return result;
650 }
651
652 return 0;
653 case ALLOC_TRACE:
654 DBG("trace alloc");
655
656 result = ltt_trace_alloc(trace_name);
657 if (result < 0) {
658 ERR("ltt_trace_alloc failed");
659 return result;
660 }
661 inform_consumer_daemon(trace_name);
662
663 return 0;
664
665 case CREATE_TRACE:
666 DBG("trace create");
667
668 result = ltt_trace_setup(trace_name);
669 if (result < 0) {
670 ERR("ltt_trace_setup failed");
671 return result;
672 }
673
674 result = ltt_trace_set_type(trace_name, trace_type);
675 if (result < 0) {
676 ERR("ltt_trace_set_type failed");
677 return result;
678 }
679
680 return 0;
681 case START_TRACE:
682 DBG("trace start");
683
684 result = ltt_trace_alloc(trace_name);
685 if (result < 0) {
686 ERR("ltt_trace_alloc failed");
687 return result;
688 }
689 if (!result) {
690 inform_consumer_daemon(trace_name);
691 }
692
693 result = ltt_trace_start(trace_name);
694 if (result < 0) {
695 ERR("ltt_trace_start failed");
696 return result;
697 }
698
699 return 0;
700 case STOP_TRACE:
701 DBG("trace stop");
702
703 result = ltt_trace_stop(trace_name);
704 if (result < 0) {
705 ERR("ltt_trace_stop failed");
706 return result;
707 }
708
709 return 0;
710 case DESTROY_TRACE:
711 DBG("trace destroy");
712
713 result = ltt_trace_destroy(trace_name, 0);
714 if (result < 0) {
715 ERR("ltt_trace_destroy failed");
716 return result;
717 }
718 return 0;
719 }
720
721 return 0;
722 }
723
724
725 static void process_channel_cmd(int sock, int command,
726 struct ustcomm_channel_info *ch_inf)
727 {
728 struct ustcomm_header _reply_header;
729 struct ustcomm_header *reply_header = &_reply_header;
730 struct ustcomm_channel_info *reply_msg =
731 (struct ustcomm_channel_info *)send_buffer;
732 int result, offset = 0, num, size;
733
734 memset(reply_header, 0, sizeof(*reply_header));
735
736 switch (command) {
737 case GET_SUBBUF_NUM_SIZE:
738 result = get_subbuf_num_size(ch_inf->trace,
739 ch_inf->channel,
740 &num, &size);
741 if (result < 0) {
742 reply_header->result = result;
743 break;
744 }
745
746 reply_msg->channel = USTCOMM_POISON_PTR;
747 reply_msg->subbuf_num = num;
748 reply_msg->subbuf_size = size;
749
750
751 reply_header->size = COMPUTE_MSG_SIZE(reply_msg, offset);
752
753 break;
754 case SET_SUBBUF_NUM:
755 reply_header->result = set_subbuf_num(ch_inf->trace,
756 ch_inf->channel,
757 ch_inf->subbuf_num);
758
759 break;
760 case SET_SUBBUF_SIZE:
761 reply_header->result = set_subbuf_size(ch_inf->trace,
762 ch_inf->channel,
763 ch_inf->subbuf_size);
764
765
766 break;
767 }
768 if (ustcomm_send(sock, reply_header, (char *)reply_msg) < 0) {
769 ERR("ustcomm_send failed");
770 }
771 }
772
773 static void process_buffer_cmd(int sock, int command,
774 struct ustcomm_buffer_info *buf_inf)
775 {
776 struct ustcomm_header _reply_header;
777 struct ustcomm_header *reply_header = &_reply_header;
778 struct ustcomm_buffer_info *reply_msg =
779 (struct ustcomm_buffer_info *)send_buffer;
780 int result, offset = 0, buf_shmid, buf_struct_shmid, buf_pipe_fd;
781 long consumed_old;
782
783 memset(reply_header, 0, sizeof(*reply_header));
784
785 switch (command) {
786 case GET_BUF_SHMID_PIPE_FD:
787 result = get_buffer_shmid_pipe_fd(buf_inf->trace,
788 buf_inf->channel,
789 buf_inf->ch_cpu,
790 &buf_shmid,
791 &buf_struct_shmid,
792 &buf_pipe_fd);
793 if (result < 0) {
794 reply_header->result = result;
795 break;
796 }
797
798 reply_msg->channel = USTCOMM_POISON_PTR;
799 reply_msg->buf_shmid = buf_shmid;
800 reply_msg->buf_struct_shmid = buf_struct_shmid;
801
802 reply_header->size = COMPUTE_MSG_SIZE(reply_msg, offset);
803 reply_header->fd_included = 1;
804
805 if (ustcomm_send_fd(sock, reply_header, (char *)reply_msg,
806 &buf_pipe_fd) < 0) {
807 ERR("ustcomm_send failed");
808 }
809 return;
810
811 case NOTIFY_BUF_MAPPED:
812 reply_header->result =
813 notify_buffer_mapped(buf_inf->trace,
814 buf_inf->channel,
815 buf_inf->ch_cpu);
816 break;
817 case GET_SUBBUFFER:
818 result = get_subbuffer(buf_inf->trace, buf_inf->channel,
819 buf_inf->ch_cpu, &consumed_old);
820 if (result < 0) {
821 reply_header->result = result;
822 break;
823 }
824
825 reply_msg->channel = USTCOMM_POISON_PTR;
826 reply_msg->consumed_old = consumed_old;
827
828 reply_header->size = COMPUTE_MSG_SIZE(reply_msg, offset);
829
830 break;
831 case PUT_SUBBUFFER:
832 result = put_subbuffer(buf_inf->trace, buf_inf->channel,
833 buf_inf->ch_cpu,
834 buf_inf->consumed_old);
835 reply_header->result = result;
836
837 break;
838 }
839
840 if (ustcomm_send(sock, reply_header, (char *)reply_msg) < 0) {
841 ERR("ustcomm_send failed");
842 }
843
844 }
845
846 static void process_marker_cmd(int sock, int command,
847 struct ustcomm_marker_info *marker_inf)
848 {
849 struct ustcomm_header _reply_header;
850 struct ustcomm_header *reply_header = &_reply_header;
851 int result = 0;
852
853 memset(reply_header, 0, sizeof(*reply_header));
854
855 switch(command) {
856 case ENABLE_MARKER:
857
858 result = ltt_marker_connect(marker_inf->channel,
859 marker_inf->marker,
860 "default");
861 if (result < 0) {
862 WARN("could not enable marker; channel=%s,"
863 " name=%s",
864 marker_inf->channel,
865 marker_inf->marker);
866
867 }
868 break;
869 case DISABLE_MARKER:
870 result = ltt_marker_disconnect(marker_inf->channel,
871 marker_inf->marker,
872 "default");
873 if (result < 0) {
874 WARN("could not disable marker; channel=%s,"
875 " name=%s",
876 marker_inf->channel,
877 marker_inf->marker);
878 }
879 break;
880 }
881
882 reply_header->result = result;
883
884 if (ustcomm_send(sock, reply_header, NULL) < 0) {
885 ERR("ustcomm_send failed");
886 }
887
888 }
889 static void process_client_cmd(struct ustcomm_header *recv_header,
890 char *recv_buf, int sock)
891 {
892 int result;
893 struct ustcomm_header _reply_header;
894 struct ustcomm_header *reply_header = &_reply_header;
895 char *send_buf = send_buffer;
896
897 memset(reply_header, 0, sizeof(*reply_header));
898 memset(send_buf, 0, sizeof(send_buffer));
899
900 switch(recv_header->command) {
901 case GET_SUBBUF_NUM_SIZE:
902 case SET_SUBBUF_NUM:
903 case SET_SUBBUF_SIZE:
904 {
905 struct ustcomm_channel_info *ch_inf;
906 ch_inf = (struct ustcomm_channel_info *)recv_buf;
907 result = ustcomm_unpack_channel_info(ch_inf);
908 if (result < 0) {
909 ERR("couldn't unpack channel info");
910 reply_header->result = -EINVAL;
911 goto send_response;
912 }
913 process_channel_cmd(sock, recv_header->command, ch_inf);
914 return;
915 }
916 case GET_BUF_SHMID_PIPE_FD:
917 case NOTIFY_BUF_MAPPED:
918 case GET_SUBBUFFER:
919 case PUT_SUBBUFFER:
920 {
921 struct ustcomm_buffer_info *buf_inf;
922 buf_inf = (struct ustcomm_buffer_info *)recv_buf;
923 result = ustcomm_unpack_buffer_info(buf_inf);
924 if (result < 0) {
925 ERR("couldn't unpack buffer info");
926 reply_header->result = -EINVAL;
927 goto send_response;
928 }
929 process_buffer_cmd(sock, recv_header->command, buf_inf);
930 return;
931 }
932 case ENABLE_MARKER:
933 case DISABLE_MARKER:
934 {
935 struct ustcomm_marker_info *marker_inf;
936 marker_inf = (struct ustcomm_marker_info *)recv_buf;
937 result = ustcomm_unpack_marker_info(marker_inf);
938 if (result < 0) {
939 ERR("couldn't unpack marker info");
940 reply_header->result = -EINVAL;
941 goto send_response;
942 }
943 process_marker_cmd(sock, recv_header->command, marker_inf);
944 return;
945 }
946 case LIST_MARKERS:
947 {
948 char *ptr;
949 size_t size;
950 FILE *fp;
951
952 fp = open_memstream(&ptr, &size);
953 if (fp == NULL) {
954 ERR("opening memstream failed");
955 return;
956 }
957 print_markers(fp);
958 fclose(fp);
959
960 reply_header->size = size + 1; /* Include final \0 */
961
962 result = ustcomm_send(sock, reply_header, ptr);
963
964 free(ptr);
965
966 if (result < 0) {
967 PERROR("failed to send markers list");
968 }
969
970 break;
971 }
972 case LIST_TRACE_EVENTS:
973 {
974 char *ptr;
975 size_t size;
976 FILE *fp;
977
978 fp = open_memstream(&ptr, &size);
979 if (fp == NULL) {
980 ERR("opening memstream failed");
981 return;
982 }
983 print_trace_events(fp);
984 fclose(fp);
985
986 reply_header->size = size + 1; /* Include final \0 */
987
988 result = ustcomm_send(sock, reply_header, ptr);
989
990 free(ptr);
991
992 if (result < 0) {
993 ERR("list_trace_events failed");
994 return;
995 }
996
997 break;
998 }
999 case LOAD_PROBE_LIB:
1000 {
1001 char *libfile;
1002
1003 /* FIXME: No functionality at all... */
1004 libfile = recv_buf;
1005
1006 DBG("load_probe_lib loading %s", libfile);
1007
1008 break;
1009 }
1010 case GET_PIDUNIQUE:
1011 {
1012 struct ustcomm_pidunique *pid_msg;
1013 pid_msg = (struct ustcomm_pidunique *)send_buf;
1014
1015 pid_msg->pidunique = pidunique;
1016 reply_header->size = sizeof(pid_msg);
1017
1018 goto send_response;
1019
1020 }
1021 case GET_SOCK_PATH:
1022 {
1023 struct ustcomm_single_field *sock_msg;
1024 char *sock_path_env;
1025
1026 sock_msg = (struct ustcomm_single_field *)send_buf;
1027
1028 sock_path_env = getenv("UST_DAEMON_SOCKET");
1029
1030 if (!sock_path_env) {
1031 result = ustcomm_pack_single_field(reply_header,
1032 sock_msg,
1033 SOCK_DIR "/ustconsumer");
1034
1035 } else {
1036 result = ustcomm_pack_single_field(reply_header,
1037 sock_msg,
1038 sock_path_env);
1039 }
1040 reply_header->result = result;
1041
1042 goto send_response;
1043 }
1044 case START:
1045 case SETUP_TRACE:
1046 case ALLOC_TRACE:
1047 case CREATE_TRACE:
1048 case START_TRACE:
1049 case STOP_TRACE:
1050 case DESTROY_TRACE:
1051 {
1052 struct ustcomm_single_field *trace_inf =
1053 (struct ustcomm_single_field *)recv_buf;
1054
1055 result = ustcomm_unpack_single_field(trace_inf);
1056 if (result < 0) {
1057 ERR("couldn't unpack trace info");
1058 reply_header->result = -EINVAL;
1059 goto send_response;
1060 }
1061
1062 reply_header->result =
1063 process_trace_cmd(recv_header->command,
1064 trace_inf->field);
1065 goto send_response;
1066
1067 }
1068 default:
1069 reply_header->result =
1070 process_simple_client_cmd(recv_header->command,
1071 recv_buf);
1072 goto send_response;
1073
1074 }
1075
1076 return;
1077
1078 send_response:
1079 ustcomm_send(sock, reply_header, send_buf);
1080 }
1081
1082 #define MAX_EVENTS 10
1083
1084 void *listener_main(void *p)
1085 {
1086 struct ustcomm_sock *epoll_sock;
1087 struct epoll_event events[MAX_EVENTS];
1088 struct sockaddr addr;
1089 int accept_fd, nfds, result, i, addr_size;
1090
1091 DBG("LISTENER");
1092
1093 pthread_cleanup_push(listener_cleanup, NULL);
1094
1095 for(;;) {
1096 nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
1097 if (nfds == -1) {
1098 PERROR("listener_main: epoll_wait failed");
1099 continue;
1100 }
1101
1102 for (i = 0; i < nfds; i++) {
1103 pthread_mutex_lock(&listener_thread_data_mutex);
1104 pthread_cleanup_push(release_listener_mutex, NULL);
1105 epoll_sock = (struct ustcomm_sock *)events[i].data.ptr;
1106 if (epoll_sock == listen_sock) {
1107 addr_size = sizeof(struct sockaddr);
1108 accept_fd = accept(epoll_sock->fd,
1109 &addr,
1110 (socklen_t *)&addr_size);
1111 if (accept_fd == -1) {
1112 PERROR("listener_main: accept failed");
1113 continue;
1114 }
1115 ustcomm_init_sock(accept_fd, epoll_fd,
1116 &ust_socks);
1117 } else {
1118 memset(receive_header, 0,
1119 sizeof(*receive_header));
1120 memset(receive_buffer, 0,
1121 sizeof(receive_buffer));
1122 result = ustcomm_recv(epoll_sock->fd,
1123 receive_header,
1124 receive_buffer);
1125 if (result == 0) {
1126 ustcomm_del_sock(epoll_sock, 0);
1127 } else {
1128 process_client_cmd(receive_header,
1129 receive_buffer,
1130 epoll_sock->fd);
1131 }
1132 }
1133 pthread_cleanup_pop(1); /* release listener mutex */
1134 }
1135 }
1136
1137 pthread_cleanup_pop(1);
1138 }
1139
1140 /* These should only be accessed in the parent thread,
1141 * not the listener.
1142 */
1143 static volatile sig_atomic_t have_listener = 0;
1144 static pthread_t listener_thread;
1145
1146 void create_listener(void)
1147 {
1148 int result;
1149 sigset_t sig_all_blocked;
1150 sigset_t orig_parent_mask;
1151
1152 if (have_listener) {
1153 WARN("not creating listener because we already had one");
1154 return;
1155 }
1156
1157 /* A new thread created by pthread_create inherits the signal mask
1158 * from the parent. To avoid any signal being received by the
1159 * listener thread, we block all signals temporarily in the parent,
1160 * while we create the listener thread.
1161 */
1162
1163 sigfillset(&sig_all_blocked);
1164
1165 result = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1166 if (result) {
1167 PERROR("pthread_sigmask: %s", strerror(result));
1168 }
1169
1170 result = pthread_create(&listener_thread, NULL, listener_main, NULL);
1171 if (result == -1) {
1172 PERROR("pthread_create");
1173 }
1174
1175 /* Restore original signal mask in parent */
1176 result = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1177 if (result) {
1178 PERROR("pthread_sigmask: %s", strerror(result));
1179 } else {
1180 have_listener = 1;
1181 }
1182 }
1183
1184 #define AUTOPROBE_DISABLED 0
1185 #define AUTOPROBE_ENABLE_ALL 1
1186 #define AUTOPROBE_ENABLE_REGEX 2
1187 static int autoprobe_method = AUTOPROBE_DISABLED;
1188 static regex_t autoprobe_regex;
1189
1190 static void auto_probe_connect(struct marker *m)
1191 {
1192 int result;
1193
1194 char* concat_name = NULL;
1195 const char *probe_name = "default";
1196
1197 if (autoprobe_method == AUTOPROBE_DISABLED) {
1198 return;
1199 } else if (autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1200 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1201 if (result == -1) {
1202 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1203 m->channel, m->name);
1204 return;
1205 }
1206 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1207 free(concat_name);
1208 return;
1209 }
1210 free(concat_name);
1211 }
1212
1213 result = ltt_marker_connect(m->channel, m->name, probe_name);
1214 if (result && result != -EEXIST)
1215 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
1216
1217 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
1218
1219 }
1220
1221 static struct ustcomm_sock * init_app_socket(int epoll_fd)
1222 {
1223 char *name;
1224 int result;
1225 struct ustcomm_sock *sock;
1226
1227 result = asprintf(&name, "%s/%d", SOCK_DIR, (int)getpid());
1228 if (result < 0) {
1229 ERR("string overflow allocating socket name, "
1230 "UST thread bailing");
1231 return NULL;
1232 }
1233
1234 result = ensure_dir_exists(SOCK_DIR);
1235 if (result == -1) {
1236 ERR("Unable to create socket directory %s, UST thread bailing",
1237 SOCK_DIR);
1238 goto free_name;
1239 }
1240
1241 sock = ustcomm_init_named_socket(name, epoll_fd);
1242 if (!sock) {
1243 ERR("Error initializing named socket (%s). Check that directory"
1244 "exists and that it is writable. UST thread bailing", name);
1245 goto free_name;
1246 }
1247
1248 free(name);
1249 return sock;
1250
1251 free_name:
1252 free(name);
1253 return NULL;
1254 }
1255
1256 static void __attribute__((constructor)) init()
1257 {
1258 struct timespec ts;
1259 int result;
1260 char* autoprobe_val = NULL;
1261 char* subbuffer_size_val = NULL;
1262 char* subbuffer_count_val = NULL;
1263 unsigned int subbuffer_size;
1264 unsigned int subbuffer_count;
1265 unsigned int power;
1266
1267 /* Assign the pidunique, to be able to differentiate the processes with same
1268 * pid, (before and after an exec).
1269 */
1270 pidunique = make_pidunique();
1271 processpid = getpid();
1272
1273 DBG("Tracectl constructor");
1274
1275 /* Set up epoll */
1276 epoll_fd = epoll_create(MAX_EVENTS);
1277 if (epoll_fd == -1) {
1278 ERR("epoll_create failed, tracing shutting down");
1279 return;
1280 }
1281
1282 /* Create the socket */
1283 listen_sock = init_app_socket(epoll_fd);
1284 if (!listen_sock) {
1285 ERR("failed to create application socket,"
1286 " tracing shutting down");
1287 return;
1288 }
1289
1290 create_listener();
1291
1292 /* Get clock the clock source type */
1293
1294 /* Default clock source */
1295 ust_clock_source = CLOCK_TRACE;
1296 if (clock_gettime(ust_clock_source, &ts) != 0) {
1297 ust_clock_source = CLOCK_MONOTONIC;
1298 DBG("UST traces will not be synchronized with LTTng traces");
1299 }
1300
1301 autoprobe_val = getenv("UST_AUTOPROBE");
1302 if (autoprobe_val) {
1303 struct marker_iter iter;
1304
1305 DBG("Autoprobe enabled.");
1306
1307 /* Ensure markers are initialized */
1308 //init_markers();
1309
1310 /* Ensure marker control is initialized, for the probe */
1311 init_marker_control();
1312
1313 /* first, set the callback that will connect the
1314 * probe on new markers
1315 */
1316 if (autoprobe_val[0] == '/') {
1317 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1318 if (result) {
1319 char regexerr[150];
1320
1321 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1322 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1323 /* don't crash the application just for this */
1324 } else {
1325 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1326 }
1327 } else {
1328 /* just enable all instrumentation */
1329 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1330 }
1331
1332 marker_set_new_marker_cb(auto_probe_connect);
1333
1334 /* Now, connect the probes that were already registered. */
1335 marker_iter_reset(&iter);
1336 marker_iter_start(&iter);
1337
1338 DBG("now iterating on markers already registered");
1339 while (iter.marker) {
1340 DBG("now iterating on marker %s", (*iter.marker)->name);
1341 auto_probe_connect(*iter.marker);
1342 marker_iter_next(&iter);
1343 }
1344 }
1345
1346 if (getenv("UST_OVERWRITE")) {
1347 int val = atoi(getenv("UST_OVERWRITE"));
1348 if (val == 0 || val == 1) {
1349 CMM_STORE_SHARED(ust_channels_overwrite_by_default, val);
1350 } else {
1351 WARN("invalid value for UST_OVERWRITE");
1352 }
1353 }
1354
1355 if (getenv("UST_AUTOCOLLECT")) {
1356 int val = atoi(getenv("UST_AUTOCOLLECT"));
1357 if (val == 0 || val == 1) {
1358 CMM_STORE_SHARED(ust_channels_request_collection_by_default, val);
1359 } else {
1360 WARN("invalid value for UST_AUTOCOLLECT");
1361 }
1362 }
1363
1364 subbuffer_size_val = getenv("UST_SUBBUF_SIZE");
1365 if (subbuffer_size_val) {
1366 sscanf(subbuffer_size_val, "%u", &subbuffer_size);
1367 power = pow2_higher_or_eq(subbuffer_size);
1368 if (power != subbuffer_size)
1369 WARN("using the next power of two for buffer size = %u\n", power);
1370 chan_infos[LTT_CHANNEL_UST].def_subbufsize = power;
1371 }
1372
1373 subbuffer_count_val = getenv("UST_SUBBUF_NUM");
1374 if (subbuffer_count_val) {
1375 sscanf(subbuffer_count_val, "%u", &subbuffer_count);
1376 if (subbuffer_count < 2)
1377 subbuffer_count = 2;
1378 chan_infos[LTT_CHANNEL_UST].def_subbufcount = subbuffer_count;
1379 }
1380
1381 if (getenv("UST_TRACE")) {
1382 char trace_name[] = "auto";
1383 char trace_type[] = "ustrelay";
1384
1385 DBG("starting early tracing");
1386
1387 /* Ensure marker control is initialized */
1388 init_marker_control();
1389
1390 /* Ensure markers are initialized */
1391 init_markers();
1392
1393 /* Ensure buffers are initialized, for the transport to be available.
1394 * We are about to set a trace type and it will fail without this.
1395 */
1396 init_ustrelay_transport();
1397
1398 /* FIXME: When starting early tracing (here), depending on the
1399 * order of constructors, it is very well possible some marker
1400 * sections are not yet registered. Because of this, some
1401 * channels may not be registered. Yet, we are about to ask the
1402 * daemon to collect the channels. Channels which are not yet
1403 * registered will not be collected.
1404 *
1405 * Currently, in LTTng, there is no way to add a channel after
1406 * trace start. The reason for this is that it induces complex
1407 * concurrency issues on the trace structures, which can only
1408 * be resolved using RCU. This has not been done yet. As a
1409 * workaround, we are forcing the registration of the "ust"
1410 * channel here. This is the only channel (apart from metadata)
1411 * that can be reliably used in early tracing.
1412 *
1413 * Non-early tracing does not have this problem and can use
1414 * arbitrary channel names.
1415 */
1416 ltt_channels_register("ust");
1417
1418 result = ltt_trace_setup(trace_name);
1419 if (result < 0) {
1420 ERR("ltt_trace_setup failed");
1421 return;
1422 }
1423
1424 result = ltt_trace_set_type(trace_name, trace_type);
1425 if (result < 0) {
1426 ERR("ltt_trace_set_type failed");
1427 return;
1428 }
1429
1430 result = ltt_trace_alloc(trace_name);
1431 if (result < 0) {
1432 ERR("ltt_trace_alloc failed");
1433 return;
1434 }
1435
1436 result = ltt_trace_start(trace_name);
1437 if (result < 0) {
1438 ERR("ltt_trace_start failed");
1439 return;
1440 }
1441
1442 /* Do this after the trace is started in order to avoid creating confusion
1443 * if the trace fails to start. */
1444 inform_consumer_daemon(trace_name);
1445 }
1446
1447 return;
1448
1449 /* should decrementally destroy stuff if error */
1450
1451 }
1452
1453 /* This is only called if we terminate normally, not with an unhandled signal,
1454 * so we cannot rely on it. However, for now, LTTV requires that the header of
1455 * the last sub-buffer contain a valid end time for the trace. This is done
1456 * automatically only when the trace is properly stopped.
1457 *
1458 * If the traced program crashed, it is always possible to manually add the
1459 * right value in the header, or to open the trace in text mode.
1460 *
1461 * FIXME: Fix LTTV so it doesn't need this.
1462 */
1463
1464 static void destroy_traces(void)
1465 {
1466 int result;
1467
1468 /* if trace running, finish it */
1469
1470 DBG("destructor stopping traces");
1471
1472 result = ltt_trace_stop("auto");
1473 if (result == -1) {
1474 ERR("ltt_trace_stop error");
1475 }
1476
1477 result = ltt_trace_destroy("auto", 0);
1478 if (result == -1) {
1479 ERR("ltt_trace_destroy error");
1480 }
1481 }
1482
1483 static int trace_recording(void)
1484 {
1485 int retval = 0;
1486 struct ust_trace *trace;
1487
1488 ltt_lock_traces();
1489
1490 cds_list_for_each_entry(trace, &ltt_traces.head, list) {
1491 if (trace->active) {
1492 retval = 1;
1493 break;
1494 }
1495 }
1496
1497 ltt_unlock_traces();
1498
1499 return retval;
1500 }
1501
1502 int restarting_usleep(useconds_t usecs)
1503 {
1504 struct timespec tv;
1505 int result;
1506
1507 tv.tv_sec = 0;
1508 tv.tv_nsec = usecs * 1000;
1509
1510 do {
1511 result = nanosleep(&tv, &tv);
1512 } while (result == -1 && errno == EINTR);
1513
1514 return result;
1515 }
1516
1517 static void stop_listener(void)
1518 {
1519 int result;
1520
1521 if (!have_listener)
1522 return;
1523
1524 result = pthread_cancel(listener_thread);
1525 if (result != 0) {
1526 ERR("pthread_cancel: %s", strerror(result));
1527 }
1528 result = pthread_join(listener_thread, NULL);
1529 if (result != 0) {
1530 ERR("pthread_join: %s", strerror(result));
1531 }
1532 }
1533
1534 /* This destructor keeps the process alive for a few seconds in order
1535 * to leave time for ustconsumer to connect to its buffers. This is necessary
1536 * for programs whose execution is very short. It is also useful in all
1537 * programs when tracing is started close to the end of the program
1538 * execution.
1539 *
1540 * FIXME: For now, this only works for the first trace created in a
1541 * process.
1542 */
1543
1544 static void __attribute__((destructor)) keepalive()
1545 {
1546 if (processpid != getpid()) {
1547 return;
1548 }
1549
1550 if (trace_recording() && CMM_LOAD_SHARED(buffers_to_export)) {
1551 int total = 0;
1552 DBG("Keeping process alive for consumer daemon...");
1553 while (CMM_LOAD_SHARED(buffers_to_export)) {
1554 const int interv = 200000;
1555 restarting_usleep(interv);
1556 total += interv;
1557
1558 if (total >= 3000000) {
1559 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1560 break;
1561 }
1562 }
1563 DBG("Finally dying...");
1564 }
1565
1566 destroy_traces();
1567
1568 /* Ask the listener to stop and clean up. */
1569 stop_listener();
1570 }
1571
1572 void ust_potential_exec(void)
1573 {
1574 trace_mark(ust, potential_exec, MARK_NOARGS);
1575
1576 DBG("test");
1577
1578 keepalive();
1579 }
1580
1581 /* Notify ust that there was a fork. This needs to be called inside
1582 * the new process, anytime a process whose memory is not shared with
1583 * the parent is created. If this function is not called, the events
1584 * of the new process will not be collected.
1585 *
1586 * Signals should be disabled before the fork and reenabled only after
1587 * this call in order to guarantee tracing is not started before ust_fork()
1588 * sanitizes the new process.
1589 */
1590
1591 static void ust_fork(void)
1592 {
1593 struct ust_buffer *buf, *buf_tmp;
1594 struct ustcomm_sock *sock, *sock_tmp;
1595 struct ust_trace *trace, *trace_tmp;
1596 int result;
1597
1598 /* FIXME: technically, the locks could have been taken before the fork */
1599 DBG("ust: forking");
1600
1601 /* Get the pid of the new process */
1602 processpid = getpid();
1603
1604 /*
1605 * FIXME: This could be prettier, we loop over the list twice and
1606 * following good locking practice should lock around the loop
1607 */
1608 cds_list_for_each_entry_safe(trace, trace_tmp, &ltt_traces.head, list) {
1609 ltt_trace_stop(trace->trace_name);
1610 }
1611
1612 /* Delete all active connections, but leave them in the epoll set */
1613 cds_list_for_each_entry_safe(sock, sock_tmp, &ust_socks, list) {
1614 ustcomm_del_sock(sock, 1);
1615 }
1616
1617 /* Delete all blocked consumers */
1618 cds_list_for_each_entry_safe(buf, buf_tmp, &open_buffers_list,
1619 open_buffers_list) {
1620 cds_list_del(&buf->open_buffers_list);
1621 }
1622
1623 /*
1624 * FIXME: This could be prettier, we loop over the list twice and
1625 * following good locking practice should lock around the loop
1626 */
1627 cds_list_for_each_entry_safe(trace, trace_tmp, &ltt_traces.head, list) {
1628 ltt_trace_destroy(trace->trace_name, 1);
1629 }
1630
1631 /* Clean up the listener socket and epoll, keeping the socket file */
1632 if (listen_sock) {
1633 ustcomm_del_named_sock(listen_sock, 1);
1634 listen_sock = NULL;
1635 }
1636 close(epoll_fd);
1637
1638 /* Re-start the launch sequence */
1639 CMM_STORE_SHARED(buffers_to_export, 0);
1640 have_listener = 0;
1641
1642 /* Set up epoll */
1643 epoll_fd = epoll_create(MAX_EVENTS);
1644 if (epoll_fd == -1) {
1645 ERR("epoll_create failed, tracing shutting down");
1646 return;
1647 }
1648
1649 /* Create the socket */
1650 listen_sock = init_app_socket(epoll_fd);
1651 if (!listen_sock) {
1652 ERR("failed to create application socket,"
1653 " tracing shutting down");
1654 return;
1655 }
1656 create_listener();
1657 ltt_trace_setup("auto");
1658 result = ltt_trace_set_type("auto", "ustrelay");
1659 if (result < 0) {
1660 ERR("ltt_trace_set_type failed");
1661 return;
1662 }
1663
1664 ltt_trace_alloc("auto");
1665 ltt_trace_start("auto");
1666 inform_consumer_daemon("auto");
1667 }
1668
1669 void ust_before_fork(ust_fork_info_t *fork_info)
1670 {
1671 /* Disable signals. This is to avoid that the child
1672 * intervenes before it is properly setup for tracing. It is
1673 * safer to disable all signals, because then we know we are not
1674 * breaking anything by restoring the original mask.
1675 */
1676 sigset_t all_sigs;
1677 int result;
1678
1679 /* FIXME:
1680 - only do this if tracing is active
1681 */
1682
1683 /* Disable signals */
1684 sigfillset(&all_sigs);
1685 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1686 if (result == -1) {
1687 PERROR("sigprocmask");
1688 return;
1689 }
1690
1691 /*
1692 * Take the fork lock to make sure we are not in the middle of
1693 * something in the listener thread.
1694 */
1695 pthread_mutex_lock(&listener_thread_data_mutex);
1696 /*
1697 * Hold listen_sock_mutex to protect from listen_sock teardown.
1698 */
1699 pthread_mutex_lock(&listen_sock_mutex);
1700 rcu_bp_before_fork();
1701 }
1702
1703 /* Don't call this function directly in a traced program */
1704 static void ust_after_fork_common(ust_fork_info_t *fork_info)
1705 {
1706 int result;
1707
1708 pthread_mutex_unlock(&listen_sock_mutex);
1709 pthread_mutex_unlock(&listener_thread_data_mutex);
1710
1711 /* Restore signals */
1712 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1713 if (result == -1) {
1714 PERROR("sigprocmask");
1715 return;
1716 }
1717 }
1718
1719 void ust_after_fork_parent(ust_fork_info_t *fork_info)
1720 {
1721 rcu_bp_after_fork_parent();
1722 /* Release mutexes and reenable signals */
1723 ust_after_fork_common(fork_info);
1724 }
1725
1726 void ust_after_fork_child(ust_fork_info_t *fork_info)
1727 {
1728 /* Release urcu mutexes */
1729 rcu_bp_after_fork_child();
1730
1731 /* Sanitize the child */
1732 ust_fork();
1733
1734 /* Then release mutexes and reenable signals */
1735 ust_after_fork_common(fork_info);
1736 }
1737
This page took 0.061843 seconds and 5 git commands to generate.