don't destroy app communication server in main() destructor
[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 <stdlib.h>
21 #include <stdint.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <sched.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <regex.h>
30 #include <urcu/uatomic_arch.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 int fd_notif = -1;
129 void notif_cb(void)
130 {
131 int result;
132 struct trctl_msg msg;
133
134 /* FIXME: fd_notif should probably be protected by a spinlock */
135
136 if(fd_notif == -1)
137 return;
138
139 msg.type = MSG_NOTIF;
140 msg.size = sizeof(msg.type);
141
142 /* FIXME: don't block here */
143 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
144 if(result == -1) {
145 PERROR("write");
146 return;
147 }
148 }
149
150 /* Ask the daemon to collect a trace called trace_name and being
151 * produced by this pid.
152 *
153 * The trace must be at least allocated. (It can also be started.)
154 * This is because _ltt_trace_find is used.
155 */
156
157 static void inform_consumer_daemon(const char *trace_name)
158 {
159 int i,j;
160 struct ust_trace *trace;
161 pid_t pid = getpid();
162 int result;
163
164 ltt_lock_traces();
165
166 trace = _ltt_trace_find(trace_name);
167 if(trace == NULL) {
168 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
169 goto finish;
170 }
171
172 for(i=0; i < trace->nr_channels; i++) {
173 /* iterate on all cpus */
174 for(j=0; j<trace->channels[i].n_cpus; j++) {
175 char *buf;
176 asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j);
177 result = ustcomm_request_consumer(pid, buf);
178 if(result == -1) {
179 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
180 /* continue even if fail */
181 }
182 free(buf);
183 buffers_to_export++;
184 }
185 }
186
187 finish:
188 ltt_unlock_traces();
189 }
190
191 void process_blocked_consumers(void)
192 {
193 int n_fds = 0;
194 struct pollfd *fds;
195 struct blocked_consumer *bc;
196 int idx = 0;
197 char inbuf;
198 int result;
199
200 list_for_each_entry(bc, &blocked_consumers, list) {
201 n_fds++;
202 }
203
204 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
205 if(fds == NULL) {
206 ERR("malloc returned NULL");
207 return;
208 }
209
210 list_for_each_entry(bc, &blocked_consumers, list) {
211 fds[idx].fd = bc->fd_producer;
212 fds[idx].events = POLLIN;
213 bc->tmp_poll_idx = idx;
214 idx++;
215 }
216
217 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
218 /* nothing */;
219 if(result == -1) {
220 PERROR("poll");
221 return;
222 }
223
224 list_for_each_entry(bc, &blocked_consumers, list) {
225 if(fds[bc->tmp_poll_idx].revents) {
226 long consumed_old = 0;
227 char *reply;
228
229 result = read(bc->fd_producer, &inbuf, 1);
230 if(result == -1) {
231 PERROR("read");
232 continue;
233 }
234 if(result == 0) {
235 int res;
236 DBG("PRODUCER END");
237
238 res = close(bc->fd_producer);
239 if(res == -1) {
240 PERROR("close");
241 }
242
243 list_del(&bc->list);
244
245 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
246 if(result < 0) {
247 ERR("ustcomm_send_reply failed");
248 continue;
249 }
250
251 continue;
252 }
253
254 result = ust_buffers_get_subbuf(bc->buf, &consumed_old);
255 if(result == -EAGAIN) {
256 WARN("missed buffer?");
257 continue;
258 }
259 else if(result < 0) {
260 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result));
261 }
262 asprintf(&reply, "%s %ld", "OK", consumed_old);
263 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
264 if(result < 0) {
265 ERR("ustcomm_send_reply failed");
266 free(reply);
267 continue;
268 }
269 free(reply);
270
271 list_del(&bc->list);
272 }
273 }
274
275 }
276
277 void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu)
278 {
279 const char *sep;
280
281 sep = rindex(channel_and_cpu, '_');
282 if(sep == NULL) {
283 *cpu = -1;
284 sep = channel_and_cpu + strlen(channel_and_cpu);
285 }
286 else {
287 *cpu = atoi(sep+1);
288 }
289
290 asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu);
291 }
292
293 static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src)
294 {
295 int retval = 0;
296 struct ust_trace *trace;
297 char trace_name[] = "auto";
298 int i;
299 char *channel_and_cpu;
300 int found = 0;
301 int result;
302 char *ch_name;
303 int ch_cpu;
304
305 DBG("get_shmid");
306
307 channel_and_cpu = nth_token(recvbuf, 1);
308 if(channel_and_cpu == NULL) {
309 ERR("cannot parse channel");
310 goto end;
311 }
312
313 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
314 if(ch_cpu == -1) {
315 ERR("problem parsing channel name");
316 goto free_short_chan_name;
317 }
318
319 ltt_lock_traces();
320 trace = _ltt_trace_find(trace_name);
321 ltt_unlock_traces();
322
323 if(trace == NULL) {
324 ERR("cannot find trace!");
325 retval = -1;
326 goto free_short_chan_name;
327 }
328
329 for(i=0; i<trace->nr_channels; i++) {
330 struct ust_channel *channel = &trace->channels[i];
331 struct ust_buffer *buf = channel->buf[ch_cpu];
332
333 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
334 char *reply;
335
336 // DBG("the shmid for the requested channel is %d", buf->shmid);
337 // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
338 asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]);
339
340 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
341 if(result) {
342 ERR("ustcomm_send_reply failed");
343 free(reply);
344 retval = -1;
345 goto free_short_chan_name;
346 }
347
348 free(reply);
349
350 found = 1;
351 break;
352 }
353 }
354
355 if(!found) {
356 ERR("channel not found (%s)", channel_and_cpu);
357 }
358
359 free_short_chan_name:
360 free(ch_name);
361
362 end:
363 return retval;
364 }
365
366 static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
367 {
368 int retval = 0;
369 struct ust_trace *trace;
370 char trace_name[] = "auto";
371 int i;
372 char *channel_and_cpu;
373 int found = 0;
374 int result;
375 char *ch_name;
376 int ch_cpu;
377
378 DBG("get_n_subbufs");
379
380 channel_and_cpu = nth_token(recvbuf, 1);
381 if(channel_and_cpu == NULL) {
382 ERR("cannot parse channel");
383 goto end;
384 }
385
386 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
387 if(ch_cpu == -1) {
388 ERR("problem parsing channel name");
389 goto free_short_chan_name;
390 }
391
392 ltt_lock_traces();
393 trace = _ltt_trace_find(trace_name);
394 ltt_unlock_traces();
395
396 if(trace == NULL) {
397 ERR("cannot find trace!");
398 retval = -1;
399 goto free_short_chan_name;
400 }
401
402 for(i=0; i<trace->nr_channels; i++) {
403 struct ust_channel *channel = &trace->channels[i];
404
405 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
406 char *reply;
407
408 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
409 asprintf(&reply, "%d", channel->subbuf_cnt);
410
411 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
412 if(result) {
413 ERR("ustcomm_send_reply failed");
414 free(reply);
415 retval = -1;
416 goto free_short_chan_name;
417 }
418
419 free(reply);
420 found = 1;
421 break;
422 }
423 }
424 if(found == 0) {
425 ERR("unable to find channel");
426 }
427
428 free_short_chan_name:
429 free(ch_name);
430
431 end:
432 return retval;
433 }
434
435 static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
436 {
437 int retval = 0;
438 struct ust_trace *trace;
439 char trace_name[] = "auto";
440 int i;
441 char *channel_and_cpu;
442 int found = 0;
443 int result;
444 char *ch_name;
445 int ch_cpu;
446
447 DBG("get_subbuf_size");
448
449 channel_and_cpu = nth_token(recvbuf, 1);
450 if(channel_and_cpu == NULL) {
451 ERR("cannot parse channel");
452 goto end;
453 }
454
455 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
456 if(ch_cpu == -1) {
457 ERR("problem parsing channel name");
458 goto free_short_chan_name;
459 }
460
461 ltt_lock_traces();
462 trace = _ltt_trace_find(trace_name);
463 ltt_unlock_traces();
464
465 if(trace == NULL) {
466 ERR("cannot find trace!");
467 retval = -1;
468 goto free_short_chan_name;
469 }
470
471 for(i=0; i<trace->nr_channels; i++) {
472 struct ust_channel *channel = &trace->channels[i];
473
474 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
475 char *reply;
476
477 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
478 asprintf(&reply, "%zd", channel->subbuf_size);
479
480 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
481 if(result) {
482 ERR("ustcomm_send_reply failed");
483 free(reply);
484 retval = -1;
485 goto free_short_chan_name;
486 }
487
488 free(reply);
489 found = 1;
490 break;
491 }
492 }
493 if(found == 0) {
494 ERR("unable to find channel");
495 }
496
497 free_short_chan_name:
498 free(ch_name);
499
500 end:
501 return retval;
502 }
503
504 static unsigned int poweroftwo(unsigned int x)
505 {
506 unsigned int power2 = 1;
507 unsigned int hardcoded = 2147483648; /* FIX max 2^31 */
508
509 if (x < 2)
510 return 2;
511
512 while (power2 < x && power2 < hardcoded)
513 power2 *= 2;
514
515 return power2;
516 }
517
518 static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
519 {
520 char *channel_slash_size;
521 char ch_name[256]="";
522 unsigned int size, power;
523 int retval = 0;
524 struct ust_trace *trace;
525 char trace_name[] = "auto";
526 int i;
527 int found = 0;
528
529 DBG("set_subbuf_size");
530
531 channel_slash_size = nth_token(recvbuf, 1);
532 sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size);
533
534 if(ch_name == NULL) {
535 ERR("cannot parse channel");
536 goto end;
537 }
538
539 power = poweroftwo(size);
540 if (power != size)
541 WARN("using the next 2^n = %u\n", power);
542
543 ltt_lock_traces();
544 trace = _ltt_trace_find_setup(trace_name);
545 if(trace == NULL) {
546 ERR("cannot find trace!");
547 retval = -1;
548 goto end;
549 }
550
551 for(i = 0; i < trace->nr_channels; i++) {
552 struct ust_channel *channel = &trace->channels[i];
553
554 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
555
556 channel->subbuf_size = power;
557 DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size);
558
559 found = 1;
560 break;
561 }
562 }
563 if(found == 0) {
564 ERR("unable to find channel");
565 }
566
567 end:
568 ltt_unlock_traces();
569 return retval;
570 }
571
572 static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src)
573 {
574 char *channel_slash_num;
575 char ch_name[256]="";
576 unsigned int num;
577 int retval = 0;
578 struct ust_trace *trace;
579 char trace_name[] = "auto";
580 int i;
581 int found = 0;
582
583 DBG("set_subbuf_num");
584
585 channel_slash_num = nth_token(recvbuf, 1);
586 sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num);
587
588 if(ch_name == NULL) {
589 ERR("cannot parse channel");
590 goto end;
591 }
592 if (num < 2) {
593 ERR("subbuffer count should be greater than 2");
594 goto end;
595 }
596
597 ltt_lock_traces();
598 trace = _ltt_trace_find_setup(trace_name);
599 if(trace == NULL) {
600 ERR("cannot find trace!");
601 retval = -1;
602 goto end;
603 }
604
605 for(i = 0; i < trace->nr_channels; i++) {
606 struct ust_channel *channel = &trace->channels[i];
607
608 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
609
610 channel->subbuf_cnt = num;
611 DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt);
612
613 found = 1;
614 break;
615 }
616 }
617 if(found == 0) {
618 ERR("unable to find channel");
619 }
620
621 end:
622 ltt_unlock_traces();
623 return retval;
624 }
625
626 static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
627 {
628 int retval = 0;
629 struct ust_trace *trace;
630 char trace_name[] = "auto";
631 int i;
632 char *channel_and_cpu;
633 int found = 0;
634 char *ch_name;
635 int ch_cpu;
636
637 DBG("get_subbuf");
638
639 channel_and_cpu = nth_token(recvbuf, 1);
640 if(channel_and_cpu == NULL) {
641 ERR("cannot parse channel");
642 goto end;
643 }
644
645 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
646 if(ch_cpu == -1) {
647 ERR("problem parsing channel name");
648 goto free_short_chan_name;
649 }
650
651 ltt_lock_traces();
652 trace = _ltt_trace_find(trace_name);
653
654 if(trace == NULL) {
655 int result;
656
657 DBG("Cannot find trace. It was likely destroyed by the user.");
658 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
659 if(result) {
660 ERR("ustcomm_send_reply failed");
661 retval = -1;
662 goto unlock_traces;
663 }
664
665 goto unlock_traces;
666 }
667
668 for(i=0; i<trace->nr_channels; i++) {
669 struct ust_channel *channel = &trace->channels[i];
670
671 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
672 struct ust_buffer *buf = channel->buf[ch_cpu];
673 struct blocked_consumer *bc;
674
675 found = 1;
676
677 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
678 if(bc == NULL) {
679 ERR("malloc returned NULL");
680 goto unlock_traces;
681 }
682 bc->fd_consumer = src->fd;
683 bc->fd_producer = buf->data_ready_fd_read;
684 bc->buf = buf;
685 bc->src = *src;
686 bc->server = ustcomm_app.server;
687
688 list_add(&bc->list, &blocked_consumers);
689
690 /* Being here is the proof the daemon has mapped the buffer in its
691 * memory. We may now decrement buffers_to_export.
692 */
693 if(uatomic_read(&buf->consumed) == 0) {
694 DBG("decrementing buffers_to_export");
695 buffers_to_export--;
696 }
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 DBG("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 static void listener_cleanup(void *ptr)
823 {
824 ustcomm_fini_app(&ustcomm_app, 0);
825 }
826
827 void *listener_main(void *p)
828 {
829 int result;
830
831 DBG("LISTENER");
832
833 pthread_cleanup_push(listener_cleanup, NULL);
834
835 for(;;) {
836 char trace_name[] = "auto";
837 char trace_type[] = "ustrelay";
838 char *recvbuf;
839 int len;
840 struct ustcomm_source src;
841
842 process_blocked_consumers();
843
844 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
845 if(result < 0) {
846 WARN("error in ustcomm_app_recv_message");
847 continue;
848 }
849 else if(result == 0) {
850 /* no message */
851 continue;
852 }
853
854 DBG("received a message! it's: %s", recvbuf);
855 len = strlen(recvbuf);
856
857 if(!strcmp(recvbuf, "print_markers")) {
858 print_markers(stderr);
859 }
860 else if(!strcmp(recvbuf, "list_markers")) {
861 char *ptr;
862 size_t size;
863 FILE *fp;
864
865 fp = open_memstream(&ptr, &size);
866 print_markers(fp);
867 fclose(fp);
868
869 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
870
871 free(ptr);
872 }
873 else if(!strcmp(recvbuf, "start")) {
874 /* start is an operation that setups the trace, allocates it and starts it */
875 result = ltt_trace_setup(trace_name);
876 if(result < 0) {
877 ERR("ltt_trace_setup failed");
878 return (void *)1;
879 }
880
881 result = ltt_trace_set_type(trace_name, trace_type);
882 if(result < 0) {
883 ERR("ltt_trace_set_type failed");
884 return (void *)1;
885 }
886
887 result = ltt_trace_alloc(trace_name);
888 if(result < 0) {
889 ERR("ltt_trace_alloc failed");
890 return (void *)1;
891 }
892
893 inform_consumer_daemon(trace_name);
894
895 result = ltt_trace_start(trace_name);
896 if(result < 0) {
897 ERR("ltt_trace_start failed");
898 continue;
899 }
900 }
901 else if(!strcmp(recvbuf, "trace_setup")) {
902 DBG("trace setup");
903
904 result = ltt_trace_setup(trace_name);
905 if(result < 0) {
906 ERR("ltt_trace_setup failed");
907 return (void *)1;
908 }
909
910 result = ltt_trace_set_type(trace_name, trace_type);
911 if(result < 0) {
912 ERR("ltt_trace_set_type failed");
913 return (void *)1;
914 }
915 }
916 else if(!strcmp(recvbuf, "trace_alloc")) {
917 DBG("trace alloc");
918
919 result = ltt_trace_alloc(trace_name);
920 if(result < 0) {
921 ERR("ltt_trace_alloc failed");
922 return (void *)1;
923 }
924 inform_consumer_daemon(trace_name);
925 }
926 else if(!strcmp(recvbuf, "trace_create")) {
927 DBG("trace create");
928
929 result = ltt_trace_setup(trace_name);
930 if(result < 0) {
931 ERR("ltt_trace_setup failed");
932 return (void *)1;
933 }
934
935 result = ltt_trace_set_type(trace_name, trace_type);
936 if(result < 0) {
937 ERR("ltt_trace_set_type failed");
938 return (void *)1;
939 }
940 }
941 else if(!strcmp(recvbuf, "trace_start")) {
942 DBG("trace start");
943
944 result = ltt_trace_alloc(trace_name);
945 if(result < 0) {
946 ERR("ltt_trace_alloc failed");
947 return (void *)1;
948 }
949 if(!result) {
950 inform_consumer_daemon(trace_name);
951 }
952
953 result = ltt_trace_start(trace_name);
954 if(result < 0) {
955 ERR("ltt_trace_start failed");
956 continue;
957 }
958 }
959 else if(!strcmp(recvbuf, "trace_stop")) {
960 DBG("trace stop");
961
962 result = ltt_trace_stop(trace_name);
963 if(result < 0) {
964 ERR("ltt_trace_stop failed");
965 return (void *)1;
966 }
967 }
968 else if(!strcmp(recvbuf, "trace_destroy")) {
969
970 DBG("trace destroy");
971
972 result = ltt_trace_destroy(trace_name, 0);
973 if(result < 0) {
974 ERR("ltt_trace_destroy failed");
975 return (void *)1;
976 }
977 }
978 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
979 do_cmd_get_shmid(recvbuf, &src);
980 }
981 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
982 do_cmd_get_n_subbufs(recvbuf, &src);
983 }
984 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
985 do_cmd_get_subbuf_size(recvbuf, &src);
986 }
987 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
988 char *libfile;
989
990 libfile = nth_token(recvbuf, 1);
991
992 DBG("load_probe_lib loading %s", libfile);
993
994 free(libfile);
995 }
996 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
997 do_cmd_get_subbuffer(recvbuf, &src);
998 }
999 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
1000 do_cmd_put_subbuffer(recvbuf, &src);
1001 }
1002 else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) {
1003 do_cmd_set_subbuf_size(recvbuf, &src);
1004 }
1005 else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) {
1006 do_cmd_set_subbuf_num(recvbuf, &src);
1007 }
1008 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
1009 char *channel_slash_name = nth_token(recvbuf, 1);
1010 char channel_name[256]="";
1011 char marker_name[256]="";
1012
1013 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
1014
1015 if(channel_name == NULL || marker_name == NULL) {
1016 WARN("invalid marker name");
1017 goto next_cmd;
1018 }
1019
1020 result = ltt_marker_connect(channel_name, marker_name, "default");
1021 if(result < 0) {
1022 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
1023 }
1024 }
1025 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
1026 char *channel_slash_name = nth_token(recvbuf, 1);
1027 char *marker_name;
1028 char *channel_name;
1029
1030 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
1031
1032 if(marker_name == NULL) {
1033 }
1034
1035 result = ltt_marker_disconnect(channel_name, marker_name, "default");
1036 if(result < 0) {
1037 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
1038 }
1039 }
1040 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
1041 char *reply;
1042
1043 asprintf(&reply, "%lld", pidunique);
1044
1045 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
1046 if(result) {
1047 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1048 goto next_cmd;
1049 }
1050
1051 free(reply);
1052 }
1053 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1054 // struct ust_trace *trace;
1055 // char trace_name[] = "auto";
1056 // int i;
1057 // char *channel_name;
1058 //
1059 // DBG("get_notifications");
1060 //
1061 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1062 // if(channel_name == NULL) {
1063 // ERR("put_subbuf_size: cannot parse channel");
1064 // goto next_cmd;
1065 // }
1066 //
1067 // ltt_lock_traces();
1068 // trace = _ltt_trace_find(trace_name);
1069 // ltt_unlock_traces();
1070 //
1071 // if(trace == NULL) {
1072 // ERR("cannot find trace!");
1073 // return (void *)1;
1074 // }
1075 //
1076 // for(i=0; i<trace->nr_channels; i++) {
1077 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1078 // int fd;
1079 //
1080 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1081 // struct rchan_buf *rbuf = rchan->buf;
1082 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1083 //
1084 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1085 // if(result == -1) {
1086 // ERR("ustcomm_app_detach_client failed");
1087 // goto next_cmd;
1088 // }
1089 //
1090 // lttbuf->wake_consumer_arg = (void *) fd;
1091 //
1092 // smp_wmb();
1093 //
1094 // lttbuf->call_wake_consumer = 1;
1095 //
1096 // break;
1097 // }
1098 // }
1099 //
1100 // free(channel_name);
1101 // }
1102 else {
1103 ERR("unable to parse message: %s", recvbuf);
1104 }
1105
1106 next_cmd:
1107 free(recvbuf);
1108 }
1109
1110 pthread_cleanup_pop(1);
1111 }
1112
1113 volatile sig_atomic_t have_listener = 0;
1114 #ifndef USE_CLONE
1115 static pthread_t listener_thread;
1116 #endif
1117
1118 void create_listener(void)
1119 {
1120 #ifdef USE_CLONE
1121 static char listener_stack[16384];
1122 int result;
1123 #endif
1124
1125 if(have_listener) {
1126 WARN("not creating listener because we already had one");
1127 return;
1128 }
1129
1130 #ifdef USE_CLONE
1131 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
1132 if(result == -1) {
1133 perror("clone");
1134 return;
1135 }
1136 #else
1137
1138 pthread_create(&listener_thread, NULL, listener_main, NULL);
1139 #endif
1140
1141 have_listener = 1;
1142 }
1143
1144 static int init_socket(void)
1145 {
1146 return ustcomm_init_app(getpid(), &ustcomm_app);
1147 }
1148
1149 #define AUTOPROBE_DISABLED 0
1150 #define AUTOPROBE_ENABLE_ALL 1
1151 #define AUTOPROBE_ENABLE_REGEX 2
1152 static int autoprobe_method = AUTOPROBE_DISABLED;
1153 static regex_t autoprobe_regex;
1154
1155 static void auto_probe_connect(struct marker *m)
1156 {
1157 int result;
1158
1159 char* concat_name = NULL;
1160 const char *probe_name = "default";
1161
1162 if(autoprobe_method == AUTOPROBE_DISABLED) {
1163 return;
1164 }
1165 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1166 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1167 if(result == -1) {
1168 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1169 m->channel, m->name);
1170 return;
1171 }
1172 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1173 free(concat_name);
1174 return;
1175 }
1176 free(concat_name);
1177 }
1178
1179 result = ltt_marker_connect(m->channel, m->name, probe_name);
1180 if(result && result != -EEXIST)
1181 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
1182
1183 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
1184
1185 }
1186
1187 static void __attribute__((constructor)) init()
1188 {
1189 int result;
1190 char* autoprobe_val = NULL;
1191
1192 /* Assign the pidunique, to be able to differentiate the processes with same
1193 * pid, (before and after an exec).
1194 */
1195 pidunique = make_pidunique();
1196
1197 DBG("Tracectl constructor");
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 static void stop_listener()
1399 {
1400 int result;
1401
1402 result = pthread_cancel(listener_thread);
1403 if(result == -1) {
1404 PERROR("pthread_cancel");
1405 }
1406 result = pthread_join(listener_thread, NULL);
1407 if(result == -1) {
1408 PERROR("pthread_join");
1409 }
1410 }
1411
1412 /* This destructor keeps the process alive for a few seconds in order
1413 * to leave time to ustd to connect to its buffers. This is necessary
1414 * for programs whose execution is very short. It is also useful in all
1415 * programs when tracing is started close to the end of the program
1416 * execution.
1417 *
1418 * FIXME: For now, this only works for the first trace created in a
1419 * process.
1420 */
1421
1422 static void __attribute__((destructor)) keepalive()
1423 {
1424 if(trace_recording() && buffers_to_export) {
1425 int total = 0;
1426 DBG("Keeping process alive for consumer daemon...");
1427 while(buffers_to_export) {
1428 const int interv = 200000;
1429 restarting_usleep(interv);
1430 total += interv;
1431
1432 if(total >= 3000000) {
1433 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1434 break;
1435 }
1436 }
1437 DBG("Finally dying...");
1438 }
1439
1440 destroy_traces();
1441
1442 /* Ask the listener to stop and clean up. */
1443 stop_listener();
1444 }
1445
1446 void ust_potential_exec(void)
1447 {
1448 trace_mark(ust, potential_exec, MARK_NOARGS);
1449
1450 DBG("test");
1451
1452 keepalive();
1453 }
1454
1455 /* Notify ust that there was a fork. This needs to be called inside
1456 * the new process, anytime a process whose memory is not shared with
1457 * the parent is created. If this function is not called, the events
1458 * of the new process will not be collected.
1459 *
1460 * Signals should be disabled before the fork and reenabled only after
1461 * this call in order to guarantee tracing is not started before ust_fork()
1462 * sanitizes the new process.
1463 */
1464
1465 static void ust_fork(void)
1466 {
1467 struct blocked_consumer *bc;
1468 struct blocked_consumer *deletable_bc = NULL;
1469 int result;
1470
1471 /* FIXME: technically, the locks could have been taken before the fork */
1472 DBG("ust: forking");
1473
1474 /* break lock if necessary */
1475 ltt_unlock_traces();
1476
1477 ltt_trace_stop("auto");
1478 ltt_trace_destroy("auto", 1);
1479 /* Delete all active connections */
1480 ustcomm_close_all_connections(&ustcomm_app.server);
1481
1482 /* Delete all blocked consumers */
1483 list_for_each_entry(bc, &blocked_consumers, list) {
1484 close(bc->fd_producer);
1485 close(bc->fd_consumer);
1486 free(deletable_bc);
1487 deletable_bc = bc;
1488 list_del(&bc->list);
1489 }
1490
1491 /* free app, keeping socket file */
1492 ustcomm_fini_app(&ustcomm_app, 1);
1493
1494 buffers_to_export = 0;
1495 have_listener = 0;
1496 init_socket();
1497 create_listener();
1498 ltt_trace_setup("auto");
1499 result = ltt_trace_set_type("auto", "ustrelay");
1500 if(result < 0) {
1501 ERR("ltt_trace_set_type failed");
1502 return;
1503 }
1504
1505 ltt_trace_alloc("auto");
1506 ltt_trace_start("auto");
1507 inform_consumer_daemon("auto");
1508 }
1509
1510 void ust_before_fork(ust_fork_info_t *fork_info)
1511 {
1512 /* Disable signals. This is to avoid that the child
1513 * intervenes before it is properly setup for tracing. It is
1514 * safer to disable all signals, because then we know we are not
1515 * breaking anything by restoring the original mask.
1516 */
1517 sigset_t all_sigs;
1518 int result;
1519
1520 /* FIXME:
1521 - only do this if tracing is active
1522 */
1523
1524 /* Disable signals */
1525 sigfillset(&all_sigs);
1526 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1527 if(result == -1) {
1528 PERROR("sigprocmask");
1529 return;
1530 }
1531 }
1532
1533 /* Don't call this function directly in a traced program */
1534 static void ust_after_fork_common(ust_fork_info_t *fork_info)
1535 {
1536 int result;
1537
1538 /* Restore signals */
1539 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1540 if(result == -1) {
1541 PERROR("sigprocmask");
1542 return;
1543 }
1544 }
1545
1546 void ust_after_fork_parent(ust_fork_info_t *fork_info)
1547 {
1548 /* Reenable signals */
1549 ust_after_fork_common(fork_info);
1550 }
1551
1552 void ust_after_fork_child(ust_fork_info_t *fork_info)
1553 {
1554 /* First sanitize the child */
1555 ust_fork();
1556
1557 /* Then reenable interrupts */
1558 ust_after_fork_common(fork_info);
1559 }
1560
This page took 0.065585 seconds and 5 git commands to generate.