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