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