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