ust_fork(): reset buffers_to_export to 0 when forking
[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 ERR("channel not found (%s)", channel_and_cpu);
362 }
363
364 free_short_chan_name:
365 free(ch_name);
366
367 end:
368 return retval;
369 }
370
371 static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
372 {
373 int retval = 0;
374 struct ust_trace *trace;
375 char trace_name[] = "auto";
376 int i;
377 char *channel_and_cpu;
378 int found = 0;
379 int result;
380 char *ch_name;
381 int ch_cpu;
382
383 DBG("get_n_subbufs");
384
385 channel_and_cpu = nth_token(recvbuf, 1);
386 if(channel_and_cpu == NULL) {
387 ERR("cannot parse channel");
388 goto end;
389 }
390
391 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
392 if(ch_cpu == -1) {
393 ERR("problem parsing channel name");
394 goto free_short_chan_name;
395 }
396
397 ltt_lock_traces();
398 trace = _ltt_trace_find(trace_name);
399 ltt_unlock_traces();
400
401 if(trace == NULL) {
402 ERR("cannot find trace!");
403 retval = -1;
404 goto free_short_chan_name;
405 }
406
407 for(i=0; i<trace->nr_channels; i++) {
408 struct ust_channel *channel = &trace->channels[i];
409
410 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
411 char *reply;
412
413 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
414 asprintf(&reply, "%d", channel->subbuf_cnt);
415
416 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
417 if(result) {
418 ERR("ustcomm_send_reply failed");
419 free(reply);
420 retval = -1;
421 goto free_short_chan_name;
422 }
423
424 free(reply);
425 found = 1;
426 break;
427 }
428 }
429 if(found == 0) {
430 ERR("unable to find channel");
431 }
432
433 free_short_chan_name:
434 free(ch_name);
435
436 end:
437 return retval;
438 }
439
440 static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
441 {
442 int retval = 0;
443 struct ust_trace *trace;
444 char trace_name[] = "auto";
445 int i;
446 char *channel_and_cpu;
447 int found = 0;
448 int result;
449 char *ch_name;
450 int ch_cpu;
451
452 DBG("get_subbuf_size");
453
454 channel_and_cpu = nth_token(recvbuf, 1);
455 if(channel_and_cpu == NULL) {
456 ERR("cannot parse channel");
457 goto end;
458 }
459
460 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
461 if(ch_cpu == -1) {
462 ERR("problem parsing channel name");
463 goto free_short_chan_name;
464 }
465
466 ltt_lock_traces();
467 trace = _ltt_trace_find(trace_name);
468 ltt_unlock_traces();
469
470 if(trace == NULL) {
471 ERR("cannot find trace!");
472 retval = -1;
473 goto free_short_chan_name;
474 }
475
476 for(i=0; i<trace->nr_channels; i++) {
477 struct ust_channel *channel = &trace->channels[i];
478
479 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
480 char *reply;
481
482 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
483 asprintf(&reply, "%zd", channel->subbuf_size);
484
485 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
486 if(result) {
487 ERR("ustcomm_send_reply failed");
488 free(reply);
489 retval = -1;
490 goto free_short_chan_name;
491 }
492
493 free(reply);
494 found = 1;
495 break;
496 }
497 }
498 if(found == 0) {
499 ERR("unable to find channel");
500 }
501
502 free_short_chan_name:
503 free(ch_name);
504
505 end:
506 return retval;
507 }
508
509 static unsigned int poweroftwo(unsigned int x)
510 {
511 unsigned int power2 = 1;
512 unsigned int hardcoded = 2147483648; /* FIX max 2^31 */
513
514 if (x < 2)
515 return 2;
516
517 while (power2 < x && power2 < hardcoded)
518 power2 *= 2;
519
520 return power2;
521 }
522
523 static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
524 {
525 char *channel_slash_size;
526 char ch_name[256]="";
527 unsigned int size, power;
528 int retval = 0;
529 struct ust_trace *trace;
530 char trace_name[] = "auto";
531 int i;
532 int found = 0;
533
534 DBG("set_subbuf_size");
535
536 channel_slash_size = nth_token(recvbuf, 1);
537 sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size);
538
539 if(ch_name == NULL) {
540 ERR("cannot parse channel");
541 goto end;
542 }
543
544 power = poweroftwo(size);
545 if (power != size)
546 WARN("using the next 2^n = %u\n", power);
547
548 ltt_lock_traces();
549 trace = _ltt_trace_find_setup(trace_name);
550 if(trace == NULL) {
551 ERR("cannot find trace!");
552 retval = -1;
553 goto end;
554 }
555
556 for(i = 0; i < trace->nr_channels; i++) {
557 struct ust_channel *channel = &trace->channels[i];
558
559 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
560
561 channel->subbuf_size = power;
562 DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size);
563
564 found = 1;
565 break;
566 }
567 }
568 if(found == 0) {
569 ERR("unable to find channel");
570 }
571
572 end:
573 ltt_unlock_traces();
574 return retval;
575 }
576
577 static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src)
578 {
579 char *channel_slash_num;
580 char ch_name[256]="";
581 unsigned int num;
582 int retval = 0;
583 struct ust_trace *trace;
584 char trace_name[] = "auto";
585 int i;
586 int found = 0;
587
588 DBG("set_subbuf_num");
589
590 channel_slash_num = nth_token(recvbuf, 1);
591 sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num);
592
593 if(ch_name == NULL) {
594 ERR("cannot parse channel");
595 goto end;
596 }
597 if (num < 2) {
598 ERR("subbuffer count should be greater than 2");
599 goto end;
600 }
601
602 ltt_lock_traces();
603 trace = _ltt_trace_find_setup(trace_name);
604 if(trace == NULL) {
605 ERR("cannot find trace!");
606 retval = -1;
607 goto end;
608 }
609
610 for(i = 0; i < trace->nr_channels; i++) {
611 struct ust_channel *channel = &trace->channels[i];
612
613 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
614
615 channel->subbuf_cnt = num;
616 DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt);
617
618 found = 1;
619 break;
620 }
621 }
622 if(found == 0) {
623 ERR("unable to find channel");
624 }
625
626 end:
627 ltt_unlock_traces();
628 return retval;
629 }
630
631 static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
632 {
633 int retval = 0;
634 struct ust_trace *trace;
635 char trace_name[] = "auto";
636 int i;
637 char *channel_and_cpu;
638 int found = 0;
639 char *ch_name;
640 int ch_cpu;
641
642 DBG("get_subbuf");
643
644 channel_and_cpu = nth_token(recvbuf, 1);
645 if(channel_and_cpu == NULL) {
646 ERR("cannot parse channel");
647 goto end;
648 }
649
650 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
651 if(ch_cpu == -1) {
652 ERR("problem parsing channel name");
653 goto free_short_chan_name;
654 }
655
656 ltt_lock_traces();
657 trace = _ltt_trace_find(trace_name);
658
659 if(trace == NULL) {
660 int result;
661
662 WARN("Cannot find trace. It was likely destroyed by the user.");
663 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
664 if(result) {
665 ERR("ustcomm_send_reply failed");
666 retval = -1;
667 goto unlock_traces;
668 }
669
670 goto unlock_traces;
671 }
672
673 for(i=0; i<trace->nr_channels; i++) {
674 struct ust_channel *channel = &trace->channels[i];
675
676 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
677 struct ust_buffer *buf = channel->buf[ch_cpu];
678 struct blocked_consumer *bc;
679
680 found = 1;
681
682 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
683 if(bc == NULL) {
684 ERR("malloc returned NULL");
685 goto unlock_traces;
686 }
687 bc->fd_consumer = src->fd;
688 bc->fd_producer = buf->data_ready_fd_read;
689 bc->buf = buf;
690 bc->src = *src;
691 bc->server = ustcomm_app.server;
692
693 list_add(&bc->list, &blocked_consumers);
694
695 /* Being here is the proof the daemon has mapped the buffer in its
696 * memory. We may now decrement buffers_to_export.
697 */
698 if(atomic_long_read(&buf->consumed) == 0) {
699 DBG("decrementing buffers_to_export");
700 buffers_to_export--;
701 }
702
703 break;
704 }
705 }
706 if(found == 0) {
707 ERR("unable to find channel");
708 }
709
710 unlock_traces:
711 ltt_unlock_traces();
712
713 free_short_chan_name:
714 free(ch_name);
715
716 end:
717 return retval;
718 }
719
720 static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src)
721 {
722 int retval = 0;
723 struct ust_trace *trace;
724 char trace_name[] = "auto";
725 int i;
726 char *channel_and_cpu;
727 int found = 0;
728 int result;
729 char *ch_name;
730 int ch_cpu;
731 long consumed_old;
732 char *consumed_old_str;
733 char *endptr;
734 char *reply = NULL;
735
736 DBG("put_subbuf");
737
738 channel_and_cpu = strdup_malloc(nth_token(recvbuf, 1));
739 if(channel_and_cpu == NULL) {
740 ERR("cannot parse channel");
741 retval = -1;
742 goto end;
743 }
744
745 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
746 if(consumed_old_str == NULL) {
747 ERR("cannot parse consumed_old");
748 retval = -1;
749 goto free_channel_and_cpu;
750 }
751 consumed_old = strtol(consumed_old_str, &endptr, 10);
752 if(*endptr != '\0') {
753 ERR("invalid value for consumed_old");
754 retval = -1;
755 goto free_consumed_old_str;
756 }
757
758 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
759 if(ch_cpu == -1) {
760 ERR("problem parsing channel name");
761 retval = -1;
762 goto free_short_chan_name;
763 }
764
765 ltt_lock_traces();
766 trace = _ltt_trace_find(trace_name);
767
768 if(trace == NULL) {
769 WARN("Cannot find trace. It was likely destroyed by the user.");
770 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
771 if(result) {
772 ERR("ustcomm_send_reply failed");
773 retval = -1;
774 goto unlock_traces;
775 }
776
777 goto unlock_traces;
778 }
779
780 for(i=0; i<trace->nr_channels; i++) {
781 struct ust_channel *channel = &trace->channels[i];
782
783 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
784 struct ust_buffer *buf = channel->buf[ch_cpu];
785
786 found = 1;
787
788 result = ust_buffers_put_subbuf(buf, consumed_old);
789 if(result < 0) {
790 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu);
791 asprintf(&reply, "%s", "ERROR");
792 }
793 else {
794 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu);
795 asprintf(&reply, "%s", "OK");
796 }
797
798 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
799 if(result) {
800 ERR("ustcomm_send_reply failed");
801 free(reply);
802 retval = -1;
803 goto unlock_traces;
804 }
805
806 free(reply);
807 break;
808 }
809 }
810 if(found == 0) {
811 ERR("unable to find channel");
812 }
813
814 unlock_traces:
815 ltt_unlock_traces();
816 free_short_chan_name:
817 free(ch_name);
818 free_consumed_old_str:
819 free(consumed_old_str);
820 free_channel_and_cpu:
821 free(channel_and_cpu);
822
823 end:
824 return retval;
825 }
826
827 void *listener_main(void *p)
828 {
829 int result;
830
831 ust_register_thread();
832
833 DBG("LISTENER");
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
1111 volatile sig_atomic_t have_listener = 0;
1112
1113 void create_listener(void)
1114 {
1115 #ifdef USE_CLONE
1116 static char listener_stack[16384];
1117 int result;
1118 #else
1119 pthread_t thread;
1120 #endif
1121
1122 if(have_listener) {
1123 WARN("not creating listener because we already had one");
1124 return;
1125 }
1126
1127 #ifdef USE_CLONE
1128 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
1129 if(result == -1) {
1130 perror("clone");
1131 return;
1132 }
1133 #else
1134
1135 pthread_create(&thread, NULL, listener_main, NULL);
1136 #endif
1137
1138 have_listener = 1;
1139 }
1140
1141 static int init_socket(void)
1142 {
1143 return ustcomm_init_app(getpid(), &ustcomm_app);
1144 }
1145
1146 #define AUTOPROBE_DISABLED 0
1147 #define AUTOPROBE_ENABLE_ALL 1
1148 #define AUTOPROBE_ENABLE_REGEX 2
1149 static int autoprobe_method = AUTOPROBE_DISABLED;
1150 static regex_t autoprobe_regex;
1151
1152 static void auto_probe_connect(struct marker *m)
1153 {
1154 int result;
1155
1156 char* concat_name = NULL;
1157 const char *probe_name = "default";
1158
1159 if(autoprobe_method == AUTOPROBE_DISABLED) {
1160 return;
1161 }
1162 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1163 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1164 if(result == -1) {
1165 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1166 m->channel, m->name);
1167 return;
1168 }
1169 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1170 free(concat_name);
1171 return;
1172 }
1173 free(concat_name);
1174 }
1175
1176 result = ltt_marker_connect(m->channel, m->name, probe_name);
1177 if(result && result != -EEXIST)
1178 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
1179
1180 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
1181
1182 }
1183
1184 static void __attribute__((constructor)) init()
1185 {
1186 int result;
1187 char* autoprobe_val = NULL;
1188
1189 /* Assign the pidunique, to be able to differentiate the processes with same
1190 * pid, (before and after an exec).
1191 */
1192 pidunique = make_pidunique();
1193
1194 /* Initialize RCU in case the constructor order is not good. */
1195 rcu_init();
1196
1197 /* It is important to do this before events start to be generated. */
1198 ust_register_thread();
1199
1200 DBG("Tracectl constructor");
1201
1202 /* Must create socket before signal handler to prevent races.
1203 */
1204 result = init_socket();
1205 if(result == -1) {
1206 ERR("init_socket error");
1207 return;
1208 }
1209
1210 create_listener();
1211
1212 autoprobe_val = getenv("UST_AUTOPROBE");
1213 if(autoprobe_val) {
1214 struct marker_iter iter;
1215
1216 DBG("Autoprobe enabled.");
1217
1218 /* Ensure markers are initialized */
1219 //init_markers();
1220
1221 /* Ensure marker control is initialized, for the probe */
1222 init_marker_control();
1223
1224 /* first, set the callback that will connect the
1225 * probe on new markers
1226 */
1227 if(autoprobe_val[0] == '/') {
1228 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1229 if (result) {
1230 char regexerr[150];
1231
1232 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1233 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1234 /* don't crash the application just for this */
1235 }
1236 else {
1237 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1238 }
1239 }
1240 else {
1241 /* just enable all instrumentation */
1242 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1243 }
1244
1245 marker_set_new_marker_cb(auto_probe_connect);
1246
1247 /* Now, connect the probes that were already registered. */
1248 marker_iter_reset(&iter);
1249 marker_iter_start(&iter);
1250
1251 DBG("now iterating on markers already registered");
1252 while(iter.marker) {
1253 DBG("now iterating on marker %s", iter.marker->name);
1254 auto_probe_connect(iter.marker);
1255 marker_iter_next(&iter);
1256 }
1257 }
1258
1259 if(getenv("UST_TRACE")) {
1260 char trace_name[] = "auto";
1261 char trace_type[] = "ustrelay";
1262
1263 DBG("starting early tracing");
1264
1265 /* Ensure marker control is initialized */
1266 init_marker_control();
1267
1268 /* Ensure markers are initialized */
1269 init_markers();
1270
1271 /* Ensure buffers are initialized, for the transport to be available.
1272 * We are about to set a trace type and it will fail without this.
1273 */
1274 init_ustrelay_transport();
1275
1276 /* FIXME: When starting early tracing (here), depending on the
1277 * order of constructors, it is very well possible some marker
1278 * sections are not yet registered. Because of this, some
1279 * channels may not be registered. Yet, we are about to ask the
1280 * daemon to collect the channels. Channels which are not yet
1281 * registered will not be collected.
1282 *
1283 * Currently, in LTTng, there is no way to add a channel after
1284 * trace start. The reason for this is that it induces complex
1285 * concurrency issues on the trace structures, which can only
1286 * be resolved using RCU. This has not been done yet. As a
1287 * workaround, we are forcing the registration of the "ust"
1288 * channel here. This is the only channel (apart from metadata)
1289 * that can be reliably used in early tracing.
1290 *
1291 * Non-early tracing does not have this problem and can use
1292 * arbitrary channel names.
1293 */
1294 ltt_channels_register("ust");
1295
1296 result = ltt_trace_setup(trace_name);
1297 if(result < 0) {
1298 ERR("ltt_trace_setup failed");
1299 return;
1300 }
1301
1302 result = ltt_trace_set_type(trace_name, trace_type);
1303 if(result < 0) {
1304 ERR("ltt_trace_set_type failed");
1305 return;
1306 }
1307
1308 result = ltt_trace_alloc(trace_name);
1309 if(result < 0) {
1310 ERR("ltt_trace_alloc failed");
1311 return;
1312 }
1313
1314 result = ltt_trace_start(trace_name);
1315 if(result < 0) {
1316 ERR("ltt_trace_start failed");
1317 return;
1318 }
1319
1320 /* Do this after the trace is started in order to avoid creating confusion
1321 * if the trace fails to start. */
1322 inform_consumer_daemon(trace_name);
1323 }
1324
1325
1326 return;
1327
1328 /* should decrementally destroy stuff if error */
1329
1330 }
1331
1332 /* This is only called if we terminate normally, not with an unhandled signal,
1333 * so we cannot rely on it. However, for now, LTTV requires that the header of
1334 * the last sub-buffer contain a valid end time for the trace. This is done
1335 * automatically only when the trace is properly stopped.
1336 *
1337 * If the traced program crashed, it is always possible to manually add the
1338 * right value in the header, or to open the trace in text mode.
1339 *
1340 * FIXME: Fix LTTV so it doesn't need this.
1341 */
1342
1343 static void destroy_traces(void)
1344 {
1345 int result;
1346
1347 /* if trace running, finish it */
1348
1349 DBG("destructor stopping traces");
1350
1351 result = ltt_trace_stop("auto");
1352 if(result == -1) {
1353 ERR("ltt_trace_stop error");
1354 }
1355
1356 result = ltt_trace_destroy("auto", 0);
1357 if(result == -1) {
1358 ERR("ltt_trace_destroy error");
1359 }
1360 }
1361
1362 static int trace_recording(void)
1363 {
1364 int retval = 0;
1365 struct ust_trace *trace;
1366
1367 ltt_lock_traces();
1368
1369 list_for_each_entry(trace, &ltt_traces.head, list) {
1370 if(trace->active) {
1371 retval = 1;
1372 break;
1373 }
1374 }
1375
1376 ltt_unlock_traces();
1377
1378 return retval;
1379 }
1380
1381 #if 0
1382 static int have_consumer(void)
1383 {
1384 return !list_empty(&blocked_consumers);
1385 }
1386 #endif
1387
1388 int restarting_usleep(useconds_t usecs)
1389 {
1390 struct timespec tv;
1391 int result;
1392
1393 tv.tv_sec = 0;
1394 tv.tv_nsec = usecs * 1000;
1395
1396 do {
1397 result = nanosleep(&tv, &tv);
1398 } while(result == -1 && errno == EINTR);
1399
1400 return result;
1401 }
1402
1403 /* This destructor keeps the process alive for a few seconds in order
1404 * to leave time to ustd to connect to its buffers. This is necessary
1405 * for programs whose execution is very short. It is also useful in all
1406 * programs when tracing is started close to the end of the program
1407 * execution.
1408 *
1409 * FIXME: For now, this only works for the first trace created in a
1410 * process.
1411 */
1412
1413 static void __attribute__((destructor)) keepalive()
1414 {
1415 if(trace_recording() && buffers_to_export) {
1416 int total = 0;
1417 DBG("Keeping process alive for consumer daemon...");
1418 while(buffers_to_export) {
1419 const int interv = 200000;
1420 restarting_usleep(interv);
1421 total += interv;
1422
1423 if(total >= 3000000) {
1424 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1425 break;
1426 }
1427 }
1428 DBG("Finally dying...");
1429 }
1430
1431 destroy_traces();
1432
1433 ustcomm_fini_app(&ustcomm_app);
1434 }
1435
1436 void ust_potential_exec(void)
1437 {
1438 trace_mark(ust, potential_exec, MARK_NOARGS);
1439
1440 DBG("test");
1441
1442 keepalive();
1443 }
1444
1445 /* Notify ust that there was a fork. This needs to be called inside
1446 * the new process, anytime a process whose memory is not shared with
1447 * the parent is created. If this function is not called, the events
1448 * of the new process will not be collected.
1449 *
1450 * Signals should be disabled before the fork and reenabled only after
1451 * this call in order to guarantee tracing is not started before ust_fork()
1452 * sanitizes the new process.
1453 */
1454
1455 static void ust_fork(void)
1456 {
1457 struct blocked_consumer *bc;
1458 struct blocked_consumer *deletable_bc = NULL;
1459 int result;
1460
1461 /* FIXME: technically, the locks could have been taken before the fork */
1462 DBG("ust: forking");
1463
1464 /* break lock if necessary */
1465 ltt_unlock_traces();
1466
1467 ltt_trace_stop("auto");
1468 ltt_trace_destroy("auto", 1);
1469 /* Delete all active connections */
1470 ustcomm_close_all_connections(&ustcomm_app.server);
1471
1472 /* Delete all blocked consumers */
1473 list_for_each_entry(bc, &blocked_consumers, list) {
1474 close(bc->fd_producer);
1475 close(bc->fd_consumer);
1476 free(deletable_bc);
1477 deletable_bc = bc;
1478 list_del(&bc->list);
1479 }
1480
1481 buffers_to_export = 0;
1482 have_listener = 0;
1483 init_socket();
1484 create_listener();
1485 ltt_trace_setup("auto");
1486 result = ltt_trace_set_type("auto", "ustrelay");
1487 if(result < 0) {
1488 ERR("ltt_trace_set_type failed");
1489 return;
1490 }
1491
1492 ltt_trace_alloc("auto");
1493 ltt_trace_start("auto");
1494 inform_consumer_daemon("auto");
1495 }
1496
1497 void ust_before_fork(ust_fork_info_t *fork_info)
1498 {
1499 /* Disable signals. This is to avoid that the child
1500 * intervenes before it is properly setup for tracing. It is
1501 * safer to disable all signals, because then we know we are not
1502 * breaking anything by restoring the original mask.
1503 */
1504 sigset_t all_sigs;
1505 int result;
1506
1507 /* FIXME:
1508 - only do this if tracing is active
1509 */
1510
1511 /* Disable signals */
1512 sigfillset(&all_sigs);
1513 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1514 if(result == -1) {
1515 PERROR("sigprocmask");
1516 return;
1517 }
1518 }
1519
1520 /* Don't call this function directly in a traced program */
1521 static void ust_after_fork_common(ust_fork_info_t *fork_info)
1522 {
1523 int result;
1524
1525 /* Restore signals */
1526 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1527 if(result == -1) {
1528 PERROR("sigprocmask");
1529 return;
1530 }
1531 }
1532
1533 void ust_after_fork_parent(ust_fork_info_t *fork_info)
1534 {
1535 /* Reenable signals */
1536 ust_after_fork_common(fork_info);
1537 }
1538
1539 void ust_after_fork_child(ust_fork_info_t *fork_info)
1540 {
1541 /* First sanitize the child */
1542 ust_fork();
1543
1544 /* Then reenable interrupts */
1545 ust_after_fork_common(fork_info);
1546 }
1547
This page took 0.062839 seconds and 5 git commands to generate.