add error handling for close() call
[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 WARN("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 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 DBG("LISTENER");
827
828 for(;;) {
829 char trace_name[] = "auto";
830 char trace_type[] = "ustrelay";
831 char *recvbuf;
832 int len;
833 struct ustcomm_source src;
834
835 process_blocked_consumers();
836
837 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
838 if(result < 0) {
839 WARN("error in ustcomm_app_recv_message");
840 continue;
841 }
842 else if(result == 0) {
843 /* no message */
844 continue;
845 }
846
847 DBG("received a message! it's: %s", recvbuf);
848 len = strlen(recvbuf);
849
850 if(!strcmp(recvbuf, "print_markers")) {
851 print_markers(stderr);
852 }
853 else if(!strcmp(recvbuf, "list_markers")) {
854 char *ptr;
855 size_t size;
856 FILE *fp;
857
858 fp = open_memstream(&ptr, &size);
859 print_markers(fp);
860 fclose(fp);
861
862 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
863
864 free(ptr);
865 }
866 else if(!strcmp(recvbuf, "start")) {
867 /* start is an operation that setups the trace, allocates it and starts it */
868 result = ltt_trace_setup(trace_name);
869 if(result < 0) {
870 ERR("ltt_trace_setup failed");
871 return (void *)1;
872 }
873
874 result = ltt_trace_set_type(trace_name, trace_type);
875 if(result < 0) {
876 ERR("ltt_trace_set_type failed");
877 return (void *)1;
878 }
879
880 result = ltt_trace_alloc(trace_name);
881 if(result < 0) {
882 ERR("ltt_trace_alloc failed");
883 return (void *)1;
884 }
885
886 inform_consumer_daemon(trace_name);
887
888 result = ltt_trace_start(trace_name);
889 if(result < 0) {
890 ERR("ltt_trace_start failed");
891 continue;
892 }
893 }
894 else if(!strcmp(recvbuf, "trace_setup")) {
895 DBG("trace setup");
896
897 result = ltt_trace_setup(trace_name);
898 if(result < 0) {
899 ERR("ltt_trace_setup failed");
900 return (void *)1;
901 }
902
903 result = ltt_trace_set_type(trace_name, trace_type);
904 if(result < 0) {
905 ERR("ltt_trace_set_type failed");
906 return (void *)1;
907 }
908 }
909 else if(!strcmp(recvbuf, "trace_alloc")) {
910 DBG("trace alloc");
911
912 result = ltt_trace_alloc(trace_name);
913 if(result < 0) {
914 ERR("ltt_trace_alloc failed");
915 return (void *)1;
916 }
917 inform_consumer_daemon(trace_name);
918 }
919 else if(!strcmp(recvbuf, "trace_create")) {
920 DBG("trace create");
921
922 result = ltt_trace_setup(trace_name);
923 if(result < 0) {
924 ERR("ltt_trace_setup failed");
925 return (void *)1;
926 }
927
928 result = ltt_trace_set_type(trace_name, trace_type);
929 if(result < 0) {
930 ERR("ltt_trace_set_type failed");
931 return (void *)1;
932 }
933 }
934 else if(!strcmp(recvbuf, "trace_start")) {
935 DBG("trace start");
936
937 result = ltt_trace_alloc(trace_name);
938 if(result < 0) {
939 ERR("ltt_trace_alloc failed");
940 return (void *)1;
941 }
942 if(!result) {
943 inform_consumer_daemon(trace_name);
944 }
945
946 result = ltt_trace_start(trace_name);
947 if(result < 0) {
948 ERR("ltt_trace_start failed");
949 continue;
950 }
951 }
952 else if(!strcmp(recvbuf, "trace_stop")) {
953 DBG("trace stop");
954
955 result = ltt_trace_stop(trace_name);
956 if(result < 0) {
957 ERR("ltt_trace_stop failed");
958 return (void *)1;
959 }
960 }
961 else if(!strcmp(recvbuf, "trace_destroy")) {
962
963 DBG("trace destroy");
964
965 result = ltt_trace_destroy(trace_name, 0);
966 if(result < 0) {
967 ERR("ltt_trace_destroy failed");
968 return (void *)1;
969 }
970 }
971 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
972 do_cmd_get_shmid(recvbuf, &src);
973 }
974 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
975 do_cmd_get_n_subbufs(recvbuf, &src);
976 }
977 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
978 do_cmd_get_subbuf_size(recvbuf, &src);
979 }
980 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
981 char *libfile;
982
983 libfile = nth_token(recvbuf, 1);
984
985 DBG("load_probe_lib loading %s", libfile);
986
987 free(libfile);
988 }
989 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
990 do_cmd_get_subbuffer(recvbuf, &src);
991 }
992 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
993 do_cmd_put_subbuffer(recvbuf, &src);
994 }
995 else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) {
996 do_cmd_set_subbuf_size(recvbuf, &src);
997 }
998 else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) {
999 do_cmd_set_subbuf_num(recvbuf, &src);
1000 }
1001 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
1002 char *channel_slash_name = nth_token(recvbuf, 1);
1003 char channel_name[256]="";
1004 char marker_name[256]="";
1005
1006 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
1007
1008 if(channel_name == NULL || marker_name == NULL) {
1009 WARN("invalid marker name");
1010 goto next_cmd;
1011 }
1012
1013 result = ltt_marker_connect(channel_name, marker_name, "default");
1014 if(result < 0) {
1015 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
1016 }
1017 }
1018 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
1019 char *channel_slash_name = nth_token(recvbuf, 1);
1020 char *marker_name;
1021 char *channel_name;
1022
1023 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
1024
1025 if(marker_name == NULL) {
1026 }
1027
1028 result = ltt_marker_disconnect(channel_name, marker_name, "default");
1029 if(result < 0) {
1030 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
1031 }
1032 }
1033 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
1034 char *reply;
1035
1036 asprintf(&reply, "%lld", pidunique);
1037
1038 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
1039 if(result) {
1040 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1041 goto next_cmd;
1042 }
1043
1044 free(reply);
1045 }
1046 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
1047 // struct ust_trace *trace;
1048 // char trace_name[] = "auto";
1049 // int i;
1050 // char *channel_name;
1051 //
1052 // DBG("get_notifications");
1053 //
1054 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
1055 // if(channel_name == NULL) {
1056 // ERR("put_subbuf_size: cannot parse channel");
1057 // goto next_cmd;
1058 // }
1059 //
1060 // ltt_lock_traces();
1061 // trace = _ltt_trace_find(trace_name);
1062 // ltt_unlock_traces();
1063 //
1064 // if(trace == NULL) {
1065 // ERR("cannot find trace!");
1066 // return (void *)1;
1067 // }
1068 //
1069 // for(i=0; i<trace->nr_channels; i++) {
1070 // struct rchan *rchan = trace->channels[i].trans_channel_data;
1071 // int fd;
1072 //
1073 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1074 // struct rchan_buf *rbuf = rchan->buf;
1075 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1076 //
1077 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1078 // if(result == -1) {
1079 // ERR("ustcomm_app_detach_client failed");
1080 // goto next_cmd;
1081 // }
1082 //
1083 // lttbuf->wake_consumer_arg = (void *) fd;
1084 //
1085 // smp_wmb();
1086 //
1087 // lttbuf->call_wake_consumer = 1;
1088 //
1089 // break;
1090 // }
1091 // }
1092 //
1093 // free(channel_name);
1094 // }
1095 else {
1096 ERR("unable to parse message: %s", recvbuf);
1097 }
1098
1099 next_cmd:
1100 free(recvbuf);
1101 }
1102 }
1103
1104 volatile sig_atomic_t have_listener = 0;
1105
1106 void create_listener(void)
1107 {
1108 #ifdef USE_CLONE
1109 static char listener_stack[16384];
1110 int result;
1111 #else
1112 pthread_t thread;
1113 #endif
1114
1115 if(have_listener) {
1116 WARN("not creating listener because we already had one");
1117 return;
1118 }
1119
1120 #ifdef USE_CLONE
1121 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
1122 if(result == -1) {
1123 perror("clone");
1124 return;
1125 }
1126 #else
1127
1128 pthread_create(&thread, NULL, listener_main, NULL);
1129 #endif
1130
1131 have_listener = 1;
1132 }
1133
1134 static int init_socket(void)
1135 {
1136 return ustcomm_init_app(getpid(), &ustcomm_app);
1137 }
1138
1139 #define AUTOPROBE_DISABLED 0
1140 #define AUTOPROBE_ENABLE_ALL 1
1141 #define AUTOPROBE_ENABLE_REGEX 2
1142 static int autoprobe_method = AUTOPROBE_DISABLED;
1143 static regex_t autoprobe_regex;
1144
1145 static void auto_probe_connect(struct marker *m)
1146 {
1147 int result;
1148
1149 char* concat_name = NULL;
1150 const char *probe_name = "default";
1151
1152 if(autoprobe_method == AUTOPROBE_DISABLED) {
1153 return;
1154 }
1155 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1156 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1157 if(result == -1) {
1158 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1159 m->channel, m->name);
1160 return;
1161 }
1162 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1163 free(concat_name);
1164 return;
1165 }
1166 free(concat_name);
1167 }
1168
1169 result = ltt_marker_connect(m->channel, m->name, probe_name);
1170 if(result && result != -EEXIST)
1171 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
1172
1173 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
1174
1175 }
1176
1177 static void __attribute__((constructor)) init()
1178 {
1179 int result;
1180 char* autoprobe_val = NULL;
1181
1182 /* Assign the pidunique, to be able to differentiate the processes with same
1183 * pid, (before and after an exec).
1184 */
1185 pidunique = make_pidunique();
1186
1187 DBG("Tracectl constructor");
1188
1189 result = init_socket();
1190 if(result == -1) {
1191 ERR("init_socket error");
1192 return;
1193 }
1194
1195 create_listener();
1196
1197 autoprobe_val = getenv("UST_AUTOPROBE");
1198 if(autoprobe_val) {
1199 struct marker_iter iter;
1200
1201 DBG("Autoprobe enabled.");
1202
1203 /* Ensure markers are initialized */
1204 //init_markers();
1205
1206 /* Ensure marker control is initialized, for the probe */
1207 init_marker_control();
1208
1209 /* first, set the callback that will connect the
1210 * probe on new markers
1211 */
1212 if(autoprobe_val[0] == '/') {
1213 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1214 if (result) {
1215 char regexerr[150];
1216
1217 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1218 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1219 /* don't crash the application just for this */
1220 }
1221 else {
1222 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1223 }
1224 }
1225 else {
1226 /* just enable all instrumentation */
1227 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1228 }
1229
1230 marker_set_new_marker_cb(auto_probe_connect);
1231
1232 /* Now, connect the probes that were already registered. */
1233 marker_iter_reset(&iter);
1234 marker_iter_start(&iter);
1235
1236 DBG("now iterating on markers already registered");
1237 while(iter.marker) {
1238 DBG("now iterating on marker %s", iter.marker->name);
1239 auto_probe_connect(iter.marker);
1240 marker_iter_next(&iter);
1241 }
1242 }
1243
1244 if(getenv("UST_TRACE")) {
1245 char trace_name[] = "auto";
1246 char trace_type[] = "ustrelay";
1247
1248 DBG("starting early tracing");
1249
1250 /* Ensure marker control is initialized */
1251 init_marker_control();
1252
1253 /* Ensure markers are initialized */
1254 init_markers();
1255
1256 /* Ensure buffers are initialized, for the transport to be available.
1257 * We are about to set a trace type and it will fail without this.
1258 */
1259 init_ustrelay_transport();
1260
1261 /* FIXME: When starting early tracing (here), depending on the
1262 * order of constructors, it is very well possible some marker
1263 * sections are not yet registered. Because of this, some
1264 * channels may not be registered. Yet, we are about to ask the
1265 * daemon to collect the channels. Channels which are not yet
1266 * registered will not be collected.
1267 *
1268 * Currently, in LTTng, there is no way to add a channel after
1269 * trace start. The reason for this is that it induces complex
1270 * concurrency issues on the trace structures, which can only
1271 * be resolved using RCU. This has not been done yet. As a
1272 * workaround, we are forcing the registration of the "ust"
1273 * channel here. This is the only channel (apart from metadata)
1274 * that can be reliably used in early tracing.
1275 *
1276 * Non-early tracing does not have this problem and can use
1277 * arbitrary channel names.
1278 */
1279 ltt_channels_register("ust");
1280
1281 result = ltt_trace_setup(trace_name);
1282 if(result < 0) {
1283 ERR("ltt_trace_setup failed");
1284 return;
1285 }
1286
1287 result = ltt_trace_set_type(trace_name, trace_type);
1288 if(result < 0) {
1289 ERR("ltt_trace_set_type failed");
1290 return;
1291 }
1292
1293 result = ltt_trace_alloc(trace_name);
1294 if(result < 0) {
1295 ERR("ltt_trace_alloc failed");
1296 return;
1297 }
1298
1299 result = ltt_trace_start(trace_name);
1300 if(result < 0) {
1301 ERR("ltt_trace_start failed");
1302 return;
1303 }
1304
1305 /* Do this after the trace is started in order to avoid creating confusion
1306 * if the trace fails to start. */
1307 inform_consumer_daemon(trace_name);
1308 }
1309
1310
1311 return;
1312
1313 /* should decrementally destroy stuff if error */
1314
1315 }
1316
1317 /* This is only called if we terminate normally, not with an unhandled signal,
1318 * so we cannot rely on it. However, for now, LTTV requires that the header of
1319 * the last sub-buffer contain a valid end time for the trace. This is done
1320 * automatically only when the trace is properly stopped.
1321 *
1322 * If the traced program crashed, it is always possible to manually add the
1323 * right value in the header, or to open the trace in text mode.
1324 *
1325 * FIXME: Fix LTTV so it doesn't need this.
1326 */
1327
1328 static void destroy_traces(void)
1329 {
1330 int result;
1331
1332 /* if trace running, finish it */
1333
1334 DBG("destructor stopping traces");
1335
1336 result = ltt_trace_stop("auto");
1337 if(result == -1) {
1338 ERR("ltt_trace_stop error");
1339 }
1340
1341 result = ltt_trace_destroy("auto", 0);
1342 if(result == -1) {
1343 ERR("ltt_trace_destroy error");
1344 }
1345 }
1346
1347 static int trace_recording(void)
1348 {
1349 int retval = 0;
1350 struct ust_trace *trace;
1351
1352 ltt_lock_traces();
1353
1354 list_for_each_entry(trace, &ltt_traces.head, list) {
1355 if(trace->active) {
1356 retval = 1;
1357 break;
1358 }
1359 }
1360
1361 ltt_unlock_traces();
1362
1363 return retval;
1364 }
1365
1366 #if 0
1367 static int have_consumer(void)
1368 {
1369 return !list_empty(&blocked_consumers);
1370 }
1371 #endif
1372
1373 int restarting_usleep(useconds_t usecs)
1374 {
1375 struct timespec tv;
1376 int result;
1377
1378 tv.tv_sec = 0;
1379 tv.tv_nsec = usecs * 1000;
1380
1381 do {
1382 result = nanosleep(&tv, &tv);
1383 } while(result == -1 && errno == EINTR);
1384
1385 return result;
1386 }
1387
1388 /* This destructor keeps the process alive for a few seconds in order
1389 * to leave time to ustd to connect to its buffers. This is necessary
1390 * for programs whose execution is very short. It is also useful in all
1391 * programs when tracing is started close to the end of the program
1392 * execution.
1393 *
1394 * FIXME: For now, this only works for the first trace created in a
1395 * process.
1396 */
1397
1398 static void __attribute__((destructor)) keepalive()
1399 {
1400 if(trace_recording() && buffers_to_export) {
1401 int total = 0;
1402 DBG("Keeping process alive for consumer daemon...");
1403 while(buffers_to_export) {
1404 const int interv = 200000;
1405 restarting_usleep(interv);
1406 total += interv;
1407
1408 if(total >= 3000000) {
1409 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1410 break;
1411 }
1412 }
1413 DBG("Finally dying...");
1414 }
1415
1416 destroy_traces();
1417
1418 ustcomm_fini_app(&ustcomm_app, 0);
1419 }
1420
1421 void ust_potential_exec(void)
1422 {
1423 trace_mark(ust, potential_exec, MARK_NOARGS);
1424
1425 DBG("test");
1426
1427 keepalive();
1428 }
1429
1430 /* Notify ust that there was a fork. This needs to be called inside
1431 * the new process, anytime a process whose memory is not shared with
1432 * the parent is created. If this function is not called, the events
1433 * of the new process will not be collected.
1434 *
1435 * Signals should be disabled before the fork and reenabled only after
1436 * this call in order to guarantee tracing is not started before ust_fork()
1437 * sanitizes the new process.
1438 */
1439
1440 static void ust_fork(void)
1441 {
1442 struct blocked_consumer *bc;
1443 struct blocked_consumer *deletable_bc = NULL;
1444 int result;
1445
1446 /* FIXME: technically, the locks could have been taken before the fork */
1447 DBG("ust: forking");
1448
1449 /* break lock if necessary */
1450 ltt_unlock_traces();
1451
1452 ltt_trace_stop("auto");
1453 ltt_trace_destroy("auto", 1);
1454 /* Delete all active connections */
1455 ustcomm_close_all_connections(&ustcomm_app.server);
1456
1457 /* Delete all blocked consumers */
1458 list_for_each_entry(bc, &blocked_consumers, list) {
1459 close(bc->fd_producer);
1460 close(bc->fd_consumer);
1461 free(deletable_bc);
1462 deletable_bc = bc;
1463 list_del(&bc->list);
1464 }
1465
1466 /* free app, keeping socket file */
1467 ustcomm_fini_app(&ustcomm_app, 1);
1468
1469 buffers_to_export = 0;
1470 have_listener = 0;
1471 init_socket();
1472 create_listener();
1473 ltt_trace_setup("auto");
1474 result = ltt_trace_set_type("auto", "ustrelay");
1475 if(result < 0) {
1476 ERR("ltt_trace_set_type failed");
1477 return;
1478 }
1479
1480 ltt_trace_alloc("auto");
1481 ltt_trace_start("auto");
1482 inform_consumer_daemon("auto");
1483 }
1484
1485 void ust_before_fork(ust_fork_info_t *fork_info)
1486 {
1487 /* Disable signals. This is to avoid that the child
1488 * intervenes before it is properly setup for tracing. It is
1489 * safer to disable all signals, because then we know we are not
1490 * breaking anything by restoring the original mask.
1491 */
1492 sigset_t all_sigs;
1493 int result;
1494
1495 /* FIXME:
1496 - only do this if tracing is active
1497 */
1498
1499 /* Disable signals */
1500 sigfillset(&all_sigs);
1501 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1502 if(result == -1) {
1503 PERROR("sigprocmask");
1504 return;
1505 }
1506 }
1507
1508 /* Don't call this function directly in a traced program */
1509 static void ust_after_fork_common(ust_fork_info_t *fork_info)
1510 {
1511 int result;
1512
1513 /* Restore signals */
1514 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1515 if(result == -1) {
1516 PERROR("sigprocmask");
1517 return;
1518 }
1519 }
1520
1521 void ust_after_fork_parent(ust_fork_info_t *fork_info)
1522 {
1523 /* Reenable signals */
1524 ust_after_fork_common(fork_info);
1525 }
1526
1527 void ust_after_fork_child(ust_fork_info_t *fork_info)
1528 {
1529 /* First sanitize the child */
1530 ust_fork();
1531
1532 /* Then reenable interrupts */
1533 ust_after_fork_common(fork_info);
1534 }
1535
This page took 0.062594 seconds and 5 git commands to generate.