ust_fork(): prevent deadlock by forcing release of trace_lock after fork in child
[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 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sched.h>
26 #include <fcntl.h>
27 #include <poll.h>
28 #include <regex.h>
29
30 #include <urcu-bp.h>
31
32 #include <ust/marker.h>
33 #include <ust/tracectl.h>
34 #include "tracer.h"
35 #include "usterr.h"
36 #include "ustcomm.h"
37 #include "buffers.h"
38 #include "marker-control.h"
39
40 //#define USE_CLONE
41
42 #define USTSIGNAL SIGIO
43
44 #define MAX_MSG_SIZE (100)
45 #define MSG_NOTIF 1
46 #define MSG_REGISTER_NOTIF 2
47
48 char consumer_stack[10000];
49
50 /* This should only be accessed by the constructor, before the creation
51 * of the listener, and then only by the listener.
52 */
53 s64 pidunique = -1LL;
54
55 struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers);
56
57 static struct ustcomm_app ustcomm_app;
58
59 struct tracecmd { /* no padding */
60 uint32_t size;
61 uint16_t command;
62 };
63
64 /* volatile because shared between the listener and the main thread */
65 volatile sig_atomic_t buffers_to_export = 0;
66
67 struct trctl_msg {
68 /* size: the size of all the fields except size itself */
69 uint32_t size;
70 uint16_t type;
71 /* Only the necessary part of the payload is transferred. It
72 * may even be none of it.
73 */
74 char payload[94];
75 };
76
77 struct consumer_channel {
78 int fd;
79 struct ltt_channel_struct *chan;
80 };
81
82 struct blocked_consumer {
83 int fd_consumer;
84 int fd_producer;
85 int tmp_poll_idx;
86
87 /* args to ustcomm_send_reply */
88 struct ustcomm_server server;
89 struct ustcomm_source src;
90
91 /* args to ust_buffers_get_subbuf */
92 struct ust_buffer *buf;
93
94 struct list_head list;
95 };
96
97 static long long make_pidunique(void)
98 {
99 s64 retval;
100 struct timeval tv;
101
102 gettimeofday(&tv, NULL);
103
104 retval = tv.tv_sec;
105 retval <<= 32;
106 retval |= tv.tv_usec;
107
108 return retval;
109 }
110
111 static void print_markers(FILE *fp)
112 {
113 struct marker_iter iter;
114
115 lock_markers();
116 marker_iter_reset(&iter);
117 marker_iter_start(&iter);
118
119 while(iter.marker) {
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);
122 }
123 unlock_markers();
124 }
125
126 static int init_socket(void);
127
128 /* This needs to be called whenever a new thread is created. It notifies
129 * liburcu of the new thread.
130 */
131
132 void ust_register_thread(void)
133 {
134 rcu_register_thread();
135 }
136
137 int fd_notif = -1;
138 void notif_cb(void)
139 {
140 int result;
141 struct trctl_msg msg;
142
143 /* FIXME: fd_notif should probably be protected by a spinlock */
144
145 if(fd_notif == -1)
146 return;
147
148 msg.type = MSG_NOTIF;
149 msg.size = sizeof(msg.type);
150
151 /* FIXME: don't block here */
152 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
153 if(result == -1) {
154 PERROR("write");
155 return;
156 }
157 }
158
159 /* Ask the daemon to collect a trace called trace_name and being
160 * produced by this pid.
161 *
162 * The trace must be at least allocated. (It can also be started.)
163 * This is because _ltt_trace_find is used.
164 */
165
166 static void inform_consumer_daemon(const char *trace_name)
167 {
168 int i,j;
169 struct ust_trace *trace;
170 pid_t pid = getpid();
171 int result;
172
173 ltt_lock_traces();
174
175 trace = _ltt_trace_find(trace_name);
176 if(trace == NULL) {
177 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
178 goto finish;
179 }
180
181 for(i=0; i < trace->nr_channels; i++) {
182 /* iterate on all cpus */
183 for(j=0; j<trace->channels[i].n_cpus; j++) {
184 char *buf;
185 asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j);
186 result = ustcomm_request_consumer(pid, buf);
187 if(result == -1) {
188 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
189 /* continue even if fail */
190 }
191 free(buf);
192 buffers_to_export++;
193 }
194 }
195
196 finish:
197 ltt_unlock_traces();
198 }
199
200 void process_blocked_consumers(void)
201 {
202 int n_fds = 0;
203 struct pollfd *fds;
204 struct blocked_consumer *bc;
205 int idx = 0;
206 char inbuf;
207 int result;
208
209 list_for_each_entry(bc, &blocked_consumers, list) {
210 n_fds++;
211 }
212
213 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
214 if(fds == NULL) {
215 ERR("malloc returned NULL");
216 return;
217 }
218
219 list_for_each_entry(bc, &blocked_consumers, list) {
220 fds[idx].fd = bc->fd_producer;
221 fds[idx].events = POLLIN;
222 bc->tmp_poll_idx = idx;
223 idx++;
224 }
225
226 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
227 /* nothing */;
228 if(result == -1) {
229 PERROR("poll");
230 return;
231 }
232
233 list_for_each_entry(bc, &blocked_consumers, list) {
234 if(fds[bc->tmp_poll_idx].revents) {
235 long consumed_old = 0;
236 char *reply;
237
238 result = read(bc->fd_producer, &inbuf, 1);
239 if(result == -1) {
240 PERROR("read");
241 continue;
242 }
243 if(result == 0) {
244 DBG("PRODUCER END");
245
246 close(bc->fd_producer);
247
248 list_del(&bc->list);
249
250 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
251 if(result < 0) {
252 ERR("ustcomm_send_reply failed");
253 continue;
254 }
255
256 continue;
257 }
258
259 result = ust_buffers_get_subbuf(bc->buf, &consumed_old);
260 if(result == -EAGAIN) {
261 WARN("missed buffer?");
262 continue;
263 }
264 else if(result < 0) {
265 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result));
266 }
267 asprintf(&reply, "%s %ld", "OK", consumed_old);
268 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
269 if(result < 0) {
270 ERR("ustcomm_send_reply failed");
271 free(reply);
272 continue;
273 }
274 free(reply);
275
276 list_del(&bc->list);
277 }
278 }
279
280 }
281
282 void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu)
283 {
284 const char *sep;
285
286 sep = rindex(channel_and_cpu, '_');
287 if(sep == NULL) {
288 *cpu = -1;
289 sep = channel_and_cpu + strlen(channel_and_cpu);
290 }
291 else {
292 *cpu = atoi(sep+1);
293 }
294
295 asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu);
296 }
297
298 static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src)
299 {
300 int retval = 0;
301 struct ust_trace *trace;
302 char trace_name[] = "auto";
303 int i;
304 char *channel_and_cpu;
305 int found = 0;
306 int result;
307 char *ch_name;
308 int ch_cpu;
309
310 DBG("get_shmid");
311
312 channel_and_cpu = nth_token(recvbuf, 1);
313 if(channel_and_cpu == NULL) {
314 ERR("cannot parse channel");
315 goto end;
316 }
317
318 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
319 if(ch_cpu == -1) {
320 ERR("problem parsing channel name");
321 goto free_short_chan_name;
322 }
323
324 ltt_lock_traces();
325 trace = _ltt_trace_find(trace_name);
326 ltt_unlock_traces();
327
328 if(trace == NULL) {
329 ERR("cannot find trace!");
330 retval = -1;
331 goto free_short_chan_name;
332 }
333
334 for(i=0; i<trace->nr_channels; i++) {
335 struct ust_channel *channel = &trace->channels[i];
336 struct ust_buffer *buf = channel->buf[ch_cpu];
337
338 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
339 char *reply;
340
341 // DBG("the shmid for the requested channel is %d", buf->shmid);
342 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
343 asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]);
344
345 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
346 if(result) {
347 ERR("ustcomm_send_reply failed");
348 free(reply);
349 retval = -1;
350 goto free_short_chan_name;
351 }
352
353 free(reply);
354
355 found = 1;
356 break;
357 }
358 }
359
360 if(found) {
361 buffers_to_export--;
362 }
363 else {
364 ERR("channel not found (%s)", channel_and_cpu);
365 }
366
367 free_short_chan_name:
368 free(ch_name);
369
370 end:
371 return retval;
372 }
373
374 static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
375 {
376 int retval = 0;
377 struct ust_trace *trace;
378 char trace_name[] = "auto";
379 int i;
380 char *channel_and_cpu;
381 int found = 0;
382 int result;
383 char *ch_name;
384 int ch_cpu;
385
386 DBG("get_n_subbufs");
387
388 channel_and_cpu = nth_token(recvbuf, 1);
389 if(channel_and_cpu == NULL) {
390 ERR("cannot parse channel");
391 goto end;
392 }
393
394 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
395 if(ch_cpu == -1) {
396 ERR("problem parsing channel name");
397 goto free_short_chan_name;
398 }
399
400 ltt_lock_traces();
401 trace = _ltt_trace_find(trace_name);
402 ltt_unlock_traces();
403
404 if(trace == NULL) {
405 ERR("cannot find trace!");
406 retval = -1;
407 goto free_short_chan_name;
408 }
409
410 for(i=0; i<trace->nr_channels; i++) {
411 struct ust_channel *channel = &trace->channels[i];
412
413 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
414 char *reply;
415
416 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
417 asprintf(&reply, "%d", channel->subbuf_cnt);
418
419 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
420 if(result) {
421 ERR("ustcomm_send_reply failed");
422 free(reply);
423 retval = -1;
424 goto free_short_chan_name;
425 }
426
427 free(reply);
428 found = 1;
429 break;
430 }
431 }
432 if(found == 0) {
433 ERR("unable to find channel");
434 }
435
436 free_short_chan_name:
437 free(ch_name);
438
439 end:
440 return retval;
441 }
442
443 static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
444 {
445 int retval = 0;
446 struct ust_trace *trace;
447 char trace_name[] = "auto";
448 int i;
449 char *channel_and_cpu;
450 int found = 0;
451 int result;
452 char *ch_name;
453 int ch_cpu;
454
455 DBG("get_subbuf_size");
456
457 channel_and_cpu = nth_token(recvbuf, 1);
458 if(channel_and_cpu == NULL) {
459 ERR("cannot parse channel");
460 goto end;
461 }
462
463 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
464 if(ch_cpu == -1) {
465 ERR("problem parsing channel name");
466 goto free_short_chan_name;
467 }
468
469 ltt_lock_traces();
470 trace = _ltt_trace_find(trace_name);
471 ltt_unlock_traces();
472
473 if(trace == NULL) {
474 ERR("cannot find trace!");
475 retval = -1;
476 goto free_short_chan_name;
477 }
478
479 for(i=0; i<trace->nr_channels; i++) {
480 struct ust_channel *channel = &trace->channels[i];
481
482 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
483 char *reply;
484
485 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
486 asprintf(&reply, "%zd", channel->subbuf_size);
487
488 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
489 if(result) {
490 ERR("ustcomm_send_reply failed");
491 free(reply);
492 retval = -1;
493 goto free_short_chan_name;
494 }
495
496 free(reply);
497 found = 1;
498 break;
499 }
500 }
501 if(found == 0) {
502 ERR("unable to find channel");
503 }
504
505 free_short_chan_name:
506 free(ch_name);
507
508 end:
509 return retval;
510 }
511
512 static unsigned int poweroftwo(unsigned int x)
513 {
514 unsigned int power2 = 1;
515 unsigned int hardcoded = 2147483648; /* FIX max 2^31 */
516
517 if (x < 2)
518 return 2;
519
520 while (power2 < x && power2 < hardcoded)
521 power2 *= 2;
522
523 return power2;
524 }
525
526 static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
527 {
528 char *channel_slash_size;
529 char ch_name[256]="";
530 unsigned int size, power;
531 int retval = 0;
532 struct ust_trace *trace;
533 char trace_name[] = "auto";
534 int i;
535 int found = 0;
536
537 DBG("set_subbuf_size");
538
539 channel_slash_size = nth_token(recvbuf, 1);
540 sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size);
541
542 if(ch_name == NULL) {
543 ERR("cannot parse channel");
544 goto end;
545 }
546
547 power = poweroftwo(size);
548 if (power != size)
549 WARN("using the next 2^n = %u\n", power);
550
551 ltt_lock_traces();
552 trace = _ltt_trace_find_setup(trace_name);
553 if(trace == NULL) {
554 ERR("cannot find trace!");
555 retval = -1;
556 goto end;
557 }
558
559 for(i = 0; i < trace->nr_channels; i++) {
560 struct ust_channel *channel = &trace->channels[i];
561
562 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
563
564 channel->subbuf_size = power;
565 DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size);
566
567 found = 1;
568 break;
569 }
570 }
571 if(found == 0) {
572 ERR("unable to find channel");
573 }
574
575 end:
576 ltt_unlock_traces();
577 return retval;
578 }
579
580 static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src)
581 {
582 char *channel_slash_num;
583 char ch_name[256]="";
584 unsigned int num;
585 int retval = 0;
586 struct ust_trace *trace;
587 char trace_name[] = "auto";
588 int i;
589 int found = 0;
590
591 DBG("set_subbuf_num");
592
593 channel_slash_num = nth_token(recvbuf, 1);
594 sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num);
595
596 if(ch_name == NULL) {
597 ERR("cannot parse channel");
598 goto end;
599 }
600 if (num < 2) {
601 ERR("subbuffer count should be greater than 2");
602 goto end;
603 }
604
605 ltt_lock_traces();
606 trace = _ltt_trace_find_setup(trace_name);
607 if(trace == NULL) {
608 ERR("cannot find trace!");
609 retval = -1;
610 goto end;
611 }
612
613 for(i = 0; i < trace->nr_channels; i++) {
614 struct ust_channel *channel = &trace->channels[i];
615
616 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
617
618 channel->subbuf_cnt = num;
619 DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt);
620
621 found = 1;
622 break;
623 }
624 }
625 if(found == 0) {
626 ERR("unable to find channel");
627 }
628
629 end:
630 ltt_unlock_traces();
631 return retval;
632 }
633
634 static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
635 {
636 int retval = 0;
637 struct ust_trace *trace;
638 char trace_name[] = "auto";
639 int i;
640 char *channel_and_cpu;
641 int found = 0;
642 char *ch_name;
643 int ch_cpu;
644
645 DBG("get_subbuf");
646
647 channel_and_cpu = nth_token(recvbuf, 1);
648 if(channel_and_cpu == NULL) {
649 ERR("cannot parse channel");
650 goto end;
651 }
652
653 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
654 if(ch_cpu == -1) {
655 ERR("problem parsing channel name");
656 goto free_short_chan_name;
657 }
658
659 ltt_lock_traces();
660 trace = _ltt_trace_find(trace_name);
661
662 if(trace == NULL) {
663 int result;
664
665 WARN("Cannot find trace. It was likely destroyed by the user.");
666 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
667 if(result) {
668 ERR("ustcomm_send_reply failed");
669 retval = -1;
670 goto unlock_traces;
671 }
672
673 goto unlock_traces;
674 }
675
676 for(i=0; i<trace->nr_channels; i++) {
677 struct ust_channel *channel = &trace->channels[i];
678
679 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
680 struct ust_buffer *buf = channel->buf[ch_cpu];
681 struct blocked_consumer *bc;
682
683 found = 1;
684
685 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
686 if(bc == NULL) {
687 ERR("malloc returned NULL");
688 goto unlock_traces;
689 }
690 bc->fd_consumer = src->fd;
691 bc->fd_producer = buf->data_ready_fd_read;
692 bc->buf = buf;
693 bc->src = *src;
694 bc->server = ustcomm_app.server;
695
696 list_add(&bc->list, &blocked_consumers);
697
698 break;
699 }
700 }
701 if(found == 0) {
702 ERR("unable to find channel");
703 }
704
705 unlock_traces:
706 ltt_unlock_traces();
707
708 free_short_chan_name:
709 free(ch_name);
710
711 end:
712 return retval;
713 }
714
715 static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src)
716 {
717 int retval = 0;
718 struct ust_trace *trace;
719 char trace_name[] = "auto";
720 int i;
721 char *channel_and_cpu;
722 int found = 0;
723 int result;
724 char *ch_name;
725 int ch_cpu;
726 long consumed_old;
727 char *consumed_old_str;
728 char *endptr;
729 char *reply = NULL;
730
731 DBG("put_subbuf");
732
733 channel_and_cpu = strdup_malloc(nth_token(recvbuf, 1));
734 if(channel_and_cpu == NULL) {
735 ERR("cannot parse channel");
736 retval = -1;
737 goto end;
738 }
739
740 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
741 if(consumed_old_str == NULL) {
742 ERR("cannot parse consumed_old");
743 retval = -1;
744 goto free_channel_and_cpu;
745 }
746 consumed_old = strtol(consumed_old_str, &endptr, 10);
747 if(*endptr != '\0') {
748 ERR("invalid value for consumed_old");
749 retval = -1;
750 goto free_consumed_old_str;
751 }
752
753 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
754 if(ch_cpu == -1) {
755 ERR("problem parsing channel name");
756 retval = -1;
757 goto free_short_chan_name;
758 }
759
760 ltt_lock_traces();
761 trace = _ltt_trace_find(trace_name);
762
763 if(trace == NULL) {
764 WARN("Cannot find trace. It was likely destroyed by the user.");
765 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
766 if(result) {
767 ERR("ustcomm_send_reply failed");
768 retval = -1;
769 goto unlock_traces;
770 }
771
772 goto unlock_traces;
773 }
774
775 for(i=0; i<trace->nr_channels; i++) {
776 struct ust_channel *channel = &trace->channels[i];
777
778 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
779 struct ust_buffer *buf = channel->buf[ch_cpu];
780
781 found = 1;
782
783 result = ust_buffers_put_subbuf(buf, consumed_old);
784 if(result < 0) {
785 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu);
786 asprintf(&reply, "%s", "ERROR");
787 }
788 else {
789 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu);
790 asprintf(&reply, "%s", "OK");
791 }
792
793 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
794 if(result) {
795 ERR("ustcomm_send_reply failed");
796 free(reply);
797 retval = -1;
798 goto unlock_traces;
799 }
800
801 free(reply);
802 break;
803 }
804 }
805 if(found == 0) {
806 ERR("unable to find channel");
807 }
808
809 unlock_traces:
810 ltt_unlock_traces();
811 free_short_chan_name:
812 free(ch_name);
813 free_consumed_old_str:
814 free(consumed_old_str);
815 free_channel_and_cpu:
816 free(channel_and_cpu);
817
818 end:
819 return retval;
820 }
821
822 void *listener_main(void *p)
823 {
824 int result;
825
826 ust_register_thread();
827
828 DBG("LISTENER");
829
830 for(;;) {
831 char trace_name[] = "auto";
832 char trace_type[] = "ustrelay";
833 char *recvbuf;
834 int len;
835 struct ustcomm_source src;
836
837 process_blocked_consumers();
838
839 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
840 if(result < 0) {
841 WARN("error in ustcomm_app_recv_message");
842 continue;
843 }
844 else if(result == 0) {
845 /* no message */
846 continue;
847 }
848
849 DBG("received a message! it's: %s", recvbuf);
850 len = strlen(recvbuf);
851
852 if(!strcmp(recvbuf, "print_markers")) {
853 print_markers(stderr);
854 }
855 else if(!strcmp(recvbuf, "list_markers")) {
856 char *ptr;
857 size_t size;
858 FILE *fp;
859
860 fp = open_memstream(&ptr, &size);
861 print_markers(fp);
862 fclose(fp);
863
864 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
865
866 free(ptr);
867 }
868 else if(!strcmp(recvbuf, "start")) {
869 /* start is an operation that setups the trace, allocates it and starts it */
870 result = ltt_trace_setup(trace_name);
871 if(result < 0) {
872 ERR("ltt_trace_setup failed");
873 return (void *)1;
874 }
875
876 result = ltt_trace_set_type(trace_name, trace_type);
877 if(result < 0) {
878 ERR("ltt_trace_set_type failed");
879 return (void *)1;
880 }
881
882 result = ltt_trace_alloc(trace_name);
883 if(result < 0) {
884 ERR("ltt_trace_alloc failed");
885 return (void *)1;
886 }
887
888 inform_consumer_daemon(trace_name);
889
890 result = ltt_trace_start(trace_name);
891 if(result < 0) {
892 ERR("ltt_trace_start failed");
893 continue;
894 }
895 }
896 else if(!strcmp(recvbuf, "trace_setup")) {
897 DBG("trace setup");
898
899 result = ltt_trace_setup(trace_name);
900 if(result < 0) {
901 ERR("ltt_trace_setup failed");
902 return (void *)1;
903 }
904
905 result = ltt_trace_set_type(trace_name, trace_type);
906 if(result < 0) {
907 ERR("ltt_trace_set_type failed");
908 return (void *)1;
909 }
910 }
911 else if(!strcmp(recvbuf, "trace_alloc")) {
912 DBG("trace alloc");
913
914 result = ltt_trace_alloc(trace_name);
915 if(result < 0) {
916 ERR("ltt_trace_alloc failed");
917 return (void *)1;
918 }
919 inform_consumer_daemon(trace_name);
920 }
921 else if(!strcmp(recvbuf, "trace_create")) {
922 DBG("trace create");
923
924 result = ltt_trace_setup(trace_name);
925 if(result < 0) {
926 ERR("ltt_trace_setup failed");
927 return (void *)1;
928 }
929
930 result = ltt_trace_set_type(trace_name, trace_type);
931 if(result < 0) {
932 ERR("ltt_trace_set_type failed");
933 return (void *)1;
934 }
935 }
936 else if(!strcmp(recvbuf, "trace_start")) {
937 DBG("trace start");
938
939 result = ltt_trace_alloc(trace_name);
940 if(result < 0) {
941 ERR("ltt_trace_alloc failed");
942 return (void *)1;
943 }
944 if(!result) {
945 inform_consumer_daemon(trace_name);
946 }
947
948 result = ltt_trace_start(trace_name);
949 if(result < 0) {
950 ERR("ltt_trace_start failed");
951 continue;
952 }
953 }
954 else if(!strcmp(recvbuf, "trace_stop")) {
955 DBG("trace stop");
956
957 result = ltt_trace_stop(trace_name);
958 if(result < 0) {
959 ERR("ltt_trace_stop failed");
960 return (void *)1;
961 }
962 }
963 else if(!strcmp(recvbuf, "trace_destroy")) {
964
965 DBG("trace destroy");
966
967 result = ltt_trace_destroy(trace_name, 0);
968 if(result < 0) {
969 ERR("ltt_trace_destroy failed");
970 return (void *)1;
971 }
972 }
973 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
974 do_cmd_get_shmid(recvbuf, &src);
975 }
976 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
977 do_cmd_get_n_subbufs(recvbuf, &src);
978 }
979 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
980 do_cmd_get_subbuf_size(recvbuf, &src);
981 }
982 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
983 char *libfile;
984
985 libfile = nth_token(recvbuf, 1);
986
987 DBG("load_probe_lib loading %s", libfile);
988
989 free(libfile);
990 }
991 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
992 do_cmd_get_subbuffer(recvbuf, &src);
993 }
994 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
995 do_cmd_put_subbuffer(recvbuf, &src);
996 }
997 else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) {
998 do_cmd_set_subbuf_size(recvbuf, &src);
999 }
1000 else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) {
1001 do_cmd_set_subbuf_num(recvbuf, &src);
1002 }
1003 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
1004 char *channel_slash_name = nth_token(recvbuf, 1);
1005 char channel_name[256]="";
1006 char marker_name[256]="";
1007
1008 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
1009
1010 if(channel_name == NULL || marker_name == NULL) {
1011 WARN("invalid marker name");
1012 goto next_cmd;
1013 }
1014
1015 result = ltt_marker_connect(channel_name, marker_name, "default");
1016 if(result < 0) {
1017 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
1018 }
1019 }
1020 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
1021 char *channel_slash_name = nth_token(recvbuf, 1);
1022 char *marker_name;
1023 char *channel_name;
1024
1025 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
1026
1027 if(marker_name == NULL) {
1028 }
1029
1030 result = ltt_marker_disconnect(channel_name, marker_name, "default");
1031 if(result < 0) {
1032 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
1033 }
1034 }
1035 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
1036 char *reply;
1037
1038 asprintf(&reply, "%lld", pidunique);
1039
1040 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
1041 if(result) {
1042 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1043 goto next_cmd;
1044 }
1045
1046 free(reply);
1047 }
1048 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1049 // struct ust_trace *trace;
1050 // char trace_name[] = "auto";
1051 // int i;
1052 // char *channel_name;
1053 //
1054 // DBG("get_notifications");
1055 //
1056 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1057 // if(channel_name == NULL) {
1058 // ERR("put_subbuf_size: cannot parse channel");
1059 // goto next_cmd;
1060 // }
1061 //
1062 // ltt_lock_traces();
1063 // trace = _ltt_trace_find(trace_name);
1064 // ltt_unlock_traces();
1065 //
1066 // if(trace == NULL) {
1067 // ERR("cannot find trace!");
1068 // return (void *)1;
1069 // }
1070 //
1071 // for(i=0; i<trace->nr_channels; i++) {
1072 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1073 // int fd;
1074 //
1075 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1076 // struct rchan_buf *rbuf = rchan->buf;
1077 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1078 //
1079 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1080 // if(result == -1) {
1081 // ERR("ustcomm_app_detach_client failed");
1082 // goto next_cmd;
1083 // }
1084 //
1085 // lttbuf->wake_consumer_arg = (void *) fd;
1086 //
1087 // smp_wmb();
1088 //
1089 // lttbuf->call_wake_consumer = 1;
1090 //
1091 // break;
1092 // }
1093 // }
1094 //
1095 // free(channel_name);
1096 // }
1097 else {
1098 ERR("unable to parse message: %s", recvbuf);
1099 }
1100
1101 next_cmd:
1102 free(recvbuf);
1103 }
1104 }
1105
1106 volatile sig_atomic_t have_listener = 0;
1107
1108 void create_listener(void)
1109 {
1110 #ifdef USE_CLONE
1111 static char listener_stack[16384];
1112 int result;
1113 #else
1114 pthread_t thread;
1115 #endif
1116
1117 if(have_listener) {
1118 WARN("not creating listener because we already had one");
1119 return;
1120 }
1121
1122 #ifdef USE_CLONE
1123 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
1124 if(result == -1) {
1125 perror("clone");
1126 return;
1127 }
1128 #else
1129
1130 pthread_create(&thread, NULL, listener_main, NULL);
1131 #endif
1132
1133 have_listener = 1;
1134 }
1135
1136 static int init_socket(void)
1137 {
1138 return ustcomm_init_app(getpid(), &ustcomm_app);
1139 }
1140
1141 #define AUTOPROBE_DISABLED 0
1142 #define AUTOPROBE_ENABLE_ALL 1
1143 #define AUTOPROBE_ENABLE_REGEX 2
1144 static int autoprobe_method = AUTOPROBE_DISABLED;
1145 static regex_t autoprobe_regex;
1146
1147 static void auto_probe_connect(struct marker *m)
1148 {
1149 int result;
1150
1151 char* concat_name = NULL;
1152 const char *probe_name = "default";
1153
1154 if(autoprobe_method == AUTOPROBE_DISABLED) {
1155 return;
1156 }
1157 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1158 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1159 if(result == -1) {
1160 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1161 m->channel, m->name);
1162 return;
1163 }
1164 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1165 free(concat_name);
1166 return;
1167 }
1168 free(concat_name);
1169 }
1170
1171 result = ltt_marker_connect(m->channel, m->name, probe_name);
1172 if(result && result != -EEXIST)
1173 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
1174
1175 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
1176
1177 }
1178
1179 static void __attribute__((constructor)) init()
1180 {
1181 int result;
1182 char* autoprobe_val = NULL;
1183
1184 /* Assign the pidunique, to be able to differentiate the processes with same
1185 * pid, (before and after an exec).
1186 */
1187 pidunique = make_pidunique();
1188
1189 /* Initialize RCU in case the constructor order is not good. */
1190 rcu_init();
1191
1192 /* It is important to do this before events start to be generated. */
1193 ust_register_thread();
1194
1195 DBG("Tracectl constructor");
1196
1197 /* Must create socket before signal handler to prevent races.
1198 */
1199 result = init_socket();
1200 if(result == -1) {
1201 ERR("init_socket error");
1202 return;
1203 }
1204
1205 create_listener();
1206
1207 autoprobe_val = getenv("UST_AUTOPROBE");
1208 if(autoprobe_val) {
1209 struct marker_iter iter;
1210
1211 DBG("Autoprobe enabled.");
1212
1213 /* Ensure markers are initialized */
1214 //init_markers();
1215
1216 /* Ensure marker control is initialized, for the probe */
1217 init_marker_control();
1218
1219 /* first, set the callback that will connect the
1220 * probe on new markers
1221 */
1222 if(autoprobe_val[0] == '/') {
1223 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1224 if (result) {
1225 char regexerr[150];
1226
1227 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1228 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1229 /* don't crash the application just for this */
1230 }
1231 else {
1232 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1233 }
1234 }
1235 else {
1236 /* just enable all instrumentation */
1237 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1238 }
1239
1240 marker_set_new_marker_cb(auto_probe_connect);
1241
1242 /* Now, connect the probes that were already registered. */
1243 marker_iter_reset(&iter);
1244 marker_iter_start(&iter);
1245
1246 DBG("now iterating on markers already registered");
1247 while(iter.marker) {
1248 DBG("now iterating on marker %s", iter.marker->name);
1249 auto_probe_connect(iter.marker);
1250 marker_iter_next(&iter);
1251 }
1252 }
1253
1254 if(getenv("UST_TRACE")) {
1255 char trace_name[] = "auto";
1256 char trace_type[] = "ustrelay";
1257
1258 DBG("starting early tracing");
1259
1260 /* Ensure marker control is initialized */
1261 init_marker_control();
1262
1263 /* Ensure markers are initialized */
1264 init_markers();
1265
1266 /* Ensure buffers are initialized, for the transport to be available.
1267 * We are about to set a trace type and it will fail without this.
1268 */
1269 init_ustrelay_transport();
1270
1271 /* FIXME: When starting early tracing (here), depending on the
1272 * order of constructors, it is very well possible some marker
1273 * sections are not yet registered. Because of this, some
1274 * channels may not be registered. Yet, we are about to ask the
1275 * daemon to collect the channels. Channels which are not yet
1276 * registered will not be collected.
1277 *
1278 * Currently, in LTTng, there is no way to add a channel after
1279 * trace start. The reason for this is that it induces complex
1280 * concurrency issues on the trace structures, which can only
1281 * be resolved using RCU. This has not been done yet. As a
1282 * workaround, we are forcing the registration of the "ust"
1283 * channel here. This is the only channel (apart from metadata)
1284 * that can be reliably used in early tracing.
1285 *
1286 * Non-early tracing does not have this problem and can use
1287 * arbitrary channel names.
1288 */
1289 ltt_channels_register("ust");
1290
1291 result = ltt_trace_setup(trace_name);
1292 if(result < 0) {
1293 ERR("ltt_trace_setup failed");
1294 return;
1295 }
1296
1297 result = ltt_trace_set_type(trace_name, trace_type);
1298 if(result < 0) {
1299 ERR("ltt_trace_set_type failed");
1300 return;
1301 }
1302
1303 result = ltt_trace_alloc(trace_name);
1304 if(result < 0) {
1305 ERR("ltt_trace_alloc failed");
1306 return;
1307 }
1308
1309 result = ltt_trace_start(trace_name);
1310 if(result < 0) {
1311 ERR("ltt_trace_start failed");
1312 return;
1313 }
1314
1315 /* Do this after the trace is started in order to avoid creating confusion
1316 * if the trace fails to start. */
1317 inform_consumer_daemon(trace_name);
1318 }
1319
1320
1321 return;
1322
1323 /* should decrementally destroy stuff if error */
1324
1325 }
1326
1327 /* This is only called if we terminate normally, not with an unhandled signal,
1328 * so we cannot rely on it. However, for now, LTTV requires that the header of
1329 * the last sub-buffer contain a valid end time for the trace. This is done
1330 * automatically only when the trace is properly stopped.
1331 *
1332 * If the traced program crashed, it is always possible to manually add the
1333 * right value in the header, or to open the trace in text mode.
1334 *
1335 * FIXME: Fix LTTV so it doesn't need this.
1336 */
1337
1338 static void destroy_traces(void)
1339 {
1340 int result;
1341
1342 /* if trace running, finish it */
1343
1344 DBG("destructor stopping traces");
1345
1346 result = ltt_trace_stop("auto");
1347 if(result == -1) {
1348 ERR("ltt_trace_stop error");
1349 }
1350
1351 result = ltt_trace_destroy("auto", 0);
1352 if(result == -1) {
1353 ERR("ltt_trace_destroy error");
1354 }
1355 }
1356
1357 static int trace_recording(void)
1358 {
1359 int retval = 0;
1360 struct ust_trace *trace;
1361
1362 ltt_lock_traces();
1363
1364 list_for_each_entry(trace, &ltt_traces.head, list) {
1365 if(trace->active) {
1366 retval = 1;
1367 break;
1368 }
1369 }
1370
1371 ltt_unlock_traces();
1372
1373 return retval;
1374 }
1375
1376 #if 0
1377 static int have_consumer(void)
1378 {
1379 return !list_empty(&blocked_consumers);
1380 }
1381 #endif
1382
1383 int restarting_usleep(useconds_t usecs)
1384 {
1385 struct timespec tv;
1386 int result;
1387
1388 tv.tv_sec = 0;
1389 tv.tv_nsec = usecs * 1000;
1390
1391 do {
1392 result = nanosleep(&tv, &tv);
1393 } while(result == -1 && errno == EINTR);
1394
1395 return result;
1396 }
1397
1398 /* This destructor keeps the process alive for a few seconds in order
1399 * to leave time to ustd to connect to its buffers. This is necessary
1400 * for programs whose execution is very short. It is also useful in all
1401 * programs when tracing is started close to the end of the program
1402 * execution.
1403 *
1404 * FIXME: For now, this only works for the first trace created in a
1405 * process.
1406 */
1407
1408 static void __attribute__((destructor)) keepalive()
1409 {
1410 if(trace_recording() && buffers_to_export) {
1411 int total = 0;
1412 DBG("Keeping process alive for consumer daemon...");
1413 while(buffers_to_export) {
1414 const int interv = 200000;
1415 restarting_usleep(interv);
1416 total += interv;
1417
1418 if(total >= 3000000) {
1419 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1420 break;
1421 }
1422 }
1423 DBG("Finally dying...");
1424 }
1425
1426 destroy_traces();
1427
1428 ustcomm_fini_app(&ustcomm_app);
1429 }
1430
1431 void ust_potential_exec(void)
1432 {
1433 trace_mark(ust, potential_exec, MARK_NOARGS);
1434
1435 DBG("test");
1436
1437 keepalive();
1438 }
1439
1440 /* Notify ust that there was a fork. This needs to be called inside
1441 * the new process, anytime a process whose memory is not shared with
1442 * the parent is created. If this function is not called, the events
1443 * of the new process will not be collected.
1444 *
1445 * Signals should be disabled before the fork and reenabled only after
1446 * this call in order to guarantee tracing is not started before ust_fork()
1447 * sanitizes the new process.
1448 */
1449
1450 static void ust_fork(void)
1451 {
1452 struct blocked_consumer *bc;
1453 struct blocked_consumer *deletable_bc = NULL;
1454 int result;
1455
1456 /* FIXME: technically, the locks could have been taken before the fork */
1457 DBG("ust: forking");
1458
1459 /* break lock if necessary */
1460 ltt_unlock_traces();
1461
1462 ltt_trace_stop("auto");
1463 ltt_trace_destroy("auto", 1);
1464 /* Delete all active connections */
1465 ustcomm_close_all_connections(&ustcomm_app.server);
1466
1467 /* Delete all blocked consumers */
1468 list_for_each_entry(bc, &blocked_consumers, list) {
1469 close(bc->fd_producer);
1470 close(bc->fd_consumer);
1471 free(deletable_bc);
1472 deletable_bc = bc;
1473 list_del(&bc->list);
1474 }
1475
1476 have_listener = 0;
1477 create_listener();
1478 init_socket();
1479 ltt_trace_setup("auto");
1480 result = ltt_trace_set_type("auto", "ustrelay");
1481 if(result < 0) {
1482 ERR("ltt_trace_set_type failed");
1483 return;
1484 }
1485
1486 ltt_trace_alloc("auto");
1487 ltt_trace_start("auto");
1488 inform_consumer_daemon("auto");
1489 }
1490
1491 void ust_before_fork(ust_fork_info_t *fork_info)
1492 {
1493 /* Disable signals. This is to avoid that the child
1494 * intervenes before it is properly setup for tracing. It is
1495 * safer to disable all signals, because then we know we are not
1496 * breaking anything by restoring the original mask.
1497 */
1498 sigset_t all_sigs;
1499 int result;
1500
1501 /* FIXME:
1502 - only do this if tracing is active
1503 */
1504
1505 /* Disable signals */
1506 sigfillset(&all_sigs);
1507 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1508 if(result == -1) {
1509 PERROR("sigprocmask");
1510 return;
1511 }
1512 }
1513
1514 /* Don't call this function directly in a traced program */
1515 static void ust_after_fork_common(ust_fork_info_t *fork_info)
1516 {
1517 int result;
1518
1519 /* Restore signals */
1520 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1521 if(result == -1) {
1522 PERROR("sigprocmask");
1523 return;
1524 }
1525 }
1526
1527 void ust_after_fork_parent(ust_fork_info_t *fork_info)
1528 {
1529 /* Reenable signals */
1530 ust_after_fork_common(fork_info);
1531 }
1532
1533 void ust_after_fork_child(ust_fork_info_t *fork_info)
1534 {
1535 /* First sanitize the child */
1536 ust_fork();
1537
1538 /* Then reenable interrupts */
1539 ust_after_fork_common(fork_info);
1540 }
1541
This page took 0.060417 seconds and 4 git commands to generate.