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