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