ustcomm: move function to destroy app socket to ustcomm
[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.h>
31
32 #include "marker.h"
33 #include "tracer.h"
34 #include "localerr.h"
35 #include "ustcomm.h"
36 #include "relay.h" /* FIXME: remove */
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 struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers);
50
51 static struct ustcomm_app ustcomm_app;
52
53 struct tracecmd { /* no padding */
54 uint32_t size;
55 uint16_t command;
56 };
57
58 /* volatile because shared between the listener and the main thread */
59 volatile sig_atomic_t buffers_to_export = 0;
60
61 //struct listener_arg {
62 // int pipe_fd;
63 //};
64
65 struct trctl_msg {
66 /* size: the size of all the fields except size itself */
67 uint32_t size;
68 uint16_t type;
69 /* Only the necessary part of the payload is transferred. It
70 * may even be none of it.
71 */
72 char payload[94];
73 };
74
75 struct consumer_channel {
76 int fd;
77 struct ltt_channel_struct *chan;
78 };
79
80 struct blocked_consumer {
81 int fd_consumer;
82 int fd_producer;
83 int tmp_poll_idx;
84
85 /* args to ustcomm_send_reply */
86 struct ustcomm_server server;
87 struct ustcomm_source src;
88
89 /* args to ltt_do_get_subbuf */
90 struct rchan_buf *rbuf;
91 struct ltt_channel_buf_struct *lttbuf;
92
93 struct list_head list;
94 };
95
96 static void print_markers(FILE *fp)
97 {
98 struct marker_iter iter;
99
100 lock_markers();
101 marker_iter_reset(&iter);
102 marker_iter_start(&iter);
103
104 while(iter.marker) {
105 fprintf(fp, "marker: %s_%s %d \"%s\"\n", iter.marker->channel, iter.marker->name, (int)imv_read(iter.marker->state), iter.marker->format);
106 marker_iter_next(&iter);
107 }
108 unlock_markers();
109 }
110
111 static int init_socket(void);
112
113 /* This needs to be called whenever a new thread is created. It notifies
114 * liburcu of the new thread.
115 */
116
117 void ust_register_thread(void)
118 {
119 rcu_register_thread();
120 }
121
122 int fd_notif = -1;
123 void notif_cb(void)
124 {
125 int result;
126 struct trctl_msg msg;
127
128 /* FIXME: fd_notif should probably be protected by a spinlock */
129
130 if(fd_notif == -1)
131 return;
132
133 msg.type = MSG_NOTIF;
134 msg.size = sizeof(msg.type);
135
136 /* FIXME: don't block here */
137 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
138 if(result == -1) {
139 PERROR("write");
140 return;
141 }
142 }
143
144 /* Ask the daemon to collect a trace called trace_name and being
145 * produced by this pid.
146 *
147 * The trace must be at least allocated. (It can also be started.)
148 * This is because _ltt_trace_find is used.
149 */
150
151 static void inform_consumer_daemon(const char *trace_name)
152 {
153 int i;
154 struct ltt_trace_struct *trace;
155 pid_t pid = getpid();
156 int result;
157
158 ltt_lock_traces();
159
160 trace = _ltt_trace_find(trace_name);
161 if(trace == NULL) {
162 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
163 goto finish;
164 }
165
166 for(i=0; i < trace->nr_channels; i++) {
167 result = ustcomm_request_consumer(pid, trace->channels[i].channel_name);
168 if(result == -1) {
169 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
170 /* continue even if fail */
171 }
172 buffers_to_export++;
173 }
174
175 finish:
176 ltt_unlock_traces();
177 }
178
179 void process_blocked_consumers(void)
180 {
181 int n_fds = 0;
182 struct pollfd *fds;
183 struct blocked_consumer *bc;
184 int idx = 0;
185 char inbuf;
186 int result;
187
188 list_for_each_entry(bc, &blocked_consumers, list) {
189 n_fds++;
190 }
191
192 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
193 if(fds == NULL) {
194 ERR("malloc returned NULL");
195 return;
196 }
197
198 list_for_each_entry(bc, &blocked_consumers, list) {
199 fds[idx].fd = bc->fd_producer;
200 fds[idx].events = POLLIN;
201 bc->tmp_poll_idx = idx;
202 idx++;
203 }
204
205 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
206 /* nothing */;
207 if(result == -1) {
208 PERROR("poll");
209 return;
210 }
211
212 list_for_each_entry(bc, &blocked_consumers, list) {
213 if(fds[bc->tmp_poll_idx].revents) {
214 long consumed_old = 0;
215 char *reply;
216
217 result = read(bc->fd_producer, &inbuf, 1);
218 if(result == -1) {
219 PERROR("read");
220 continue;
221 }
222 if(result == 0) {
223 DBG("PRODUCER END");
224
225 close(bc->fd_producer);
226
227 list_del(&bc->list);
228
229 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
230 if(result < 0) {
231 ERR("ustcomm_send_reply failed");
232 continue;
233 }
234
235 continue;
236 }
237
238 result = ltt_do_get_subbuf(bc->rbuf, bc->lttbuf, &consumed_old);
239 if(result == -EAGAIN) {
240 WARN("missed buffer?");
241 continue;
242 }
243 else if(result < 0) {
244 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
245 }
246 asprintf(&reply, "%s %ld", "OK", consumed_old);
247 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
248 if(result < 0) {
249 ERR("ustcomm_send_reply failed");
250 free(reply);
251 continue;
252 }
253 free(reply);
254
255 list_del(&bc->list);
256 }
257 }
258
259 }
260
261 void *listener_main(void *p)
262 {
263 int result;
264
265 ust_register_thread();
266
267 DBG("LISTENER");
268
269 for(;;) {
270 char trace_name[] = "auto";
271 char trace_type[] = "ustrelay";
272 char *recvbuf;
273 int len;
274 struct ustcomm_source src;
275
276 process_blocked_consumers();
277
278 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
279 if(result < 0) {
280 WARN("error in ustcomm_app_recv_message");
281 continue;
282 }
283 else if(result == 0) {
284 /* no message */
285 continue;
286 }
287
288 DBG("received a message! it's: %s", recvbuf);
289 len = strlen(recvbuf);
290
291 if(!strcmp(recvbuf, "print_markers")) {
292 print_markers(stderr);
293 }
294 else if(!strcmp(recvbuf, "list_markers")) {
295 char *ptr;
296 size_t size;
297 FILE *fp;
298
299 fp = open_memstream(&ptr, &size);
300 print_markers(fp);
301 fclose(fp);
302
303 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
304
305 free(ptr);
306 }
307 else if(!strcmp(recvbuf, "start")) {
308 /* start is an operation that setups the trace, allocates it and starts it */
309 result = ltt_trace_setup(trace_name);
310 if(result < 0) {
311 ERR("ltt_trace_setup failed");
312 return (void *)1;
313 }
314
315 result = ltt_trace_set_type(trace_name, trace_type);
316 if(result < 0) {
317 ERR("ltt_trace_set_type failed");
318 return (void *)1;
319 }
320
321 result = ltt_trace_alloc(trace_name);
322 if(result < 0) {
323 ERR("ltt_trace_alloc failed");
324 return (void *)1;
325 }
326
327 inform_consumer_daemon(trace_name);
328
329 result = ltt_trace_start(trace_name);
330 if(result < 0) {
331 ERR("ltt_trace_start failed");
332 continue;
333 }
334 }
335 else if(!strcmp(recvbuf, "trace_setup")) {
336 DBG("trace setup");
337
338 result = ltt_trace_setup(trace_name);
339 if(result < 0) {
340 ERR("ltt_trace_setup failed");
341 return (void *)1;
342 }
343
344 result = ltt_trace_set_type(trace_name, trace_type);
345 if(result < 0) {
346 ERR("ltt_trace_set_type failed");
347 return (void *)1;
348 }
349 }
350 else if(!strcmp(recvbuf, "trace_alloc")) {
351 DBG("trace alloc");
352
353 result = ltt_trace_alloc(trace_name);
354 if(result < 0) {
355 ERR("ltt_trace_alloc failed");
356 return (void *)1;
357 }
358 }
359 else if(!strcmp(recvbuf, "trace_start")) {
360 DBG("trace start");
361
362 result = ltt_trace_start(trace_name);
363 if(result < 0) {
364 ERR("ltt_trace_start failed");
365 continue;
366 }
367 }
368 else if(!strcmp(recvbuf, "trace_stop")) {
369 DBG("trace stop");
370
371 result = ltt_trace_stop(trace_name);
372 if(result < 0) {
373 ERR("ltt_trace_stop failed");
374 return (void *)1;
375 }
376 }
377 else if(!strcmp(recvbuf, "trace_destroy")) {
378
379 DBG("trace destroy");
380
381 result = ltt_trace_destroy(trace_name);
382 if(result < 0) {
383 ERR("ltt_trace_destroy failed");
384 return (void *)1;
385 }
386 }
387 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
388 struct ltt_trace_struct *trace;
389 char trace_name[] = "auto";
390 int i;
391 char *channel_name;
392
393 DBG("get_shmid");
394
395 channel_name = nth_token(recvbuf, 1);
396 if(channel_name == NULL) {
397 ERR("get_shmid: cannot parse channel");
398 goto next_cmd;
399 }
400
401 ltt_lock_traces();
402 trace = _ltt_trace_find(trace_name);
403 ltt_unlock_traces();
404
405 if(trace == NULL) {
406 ERR("cannot find trace!");
407 return (void *)1;
408 }
409
410 for(i=0; i<trace->nr_channels; i++) {
411 struct rchan *rchan = trace->channels[i].trans_channel_data;
412 struct rchan_buf *rbuf = rchan->buf;
413 struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)rchan->private_data;
414
415 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
416 char *reply;
417
418 DBG("the shmid for the requested channel is %d", rbuf->shmid);
419 DBG("the shmid for its buffer structure is %d", ltt_channel->buf_shmid);
420 asprintf(&reply, "%d %d", rbuf->shmid, ltt_channel->buf_shmid);
421
422 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
423 if(result) {
424 ERR("listener: get_shmid: ustcomm_send_reply failed");
425 goto next_cmd;
426 }
427
428 free(reply);
429
430 break;
431 }
432 }
433
434 buffers_to_export--;
435 }
436 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
437 struct ltt_trace_struct *trace;
438 char trace_name[] = "auto";
439 int i;
440 char *channel_name;
441
442 DBG("get_n_subbufs");
443
444 channel_name = nth_token(recvbuf, 1);
445 if(channel_name == NULL) {
446 ERR("get_n_subbufs: cannot parse channel");
447 goto next_cmd;
448 }
449
450 ltt_lock_traces();
451 trace = _ltt_trace_find(trace_name);
452 ltt_unlock_traces();
453
454 if(trace == NULL) {
455 ERR("cannot find trace!");
456 return (void *)1;
457 }
458
459 for(i=0; i<trace->nr_channels; i++) {
460 struct rchan *rchan = trace->channels[i].trans_channel_data;
461
462 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
463 char *reply;
464
465 DBG("the n_subbufs for the requested channel is %zd", rchan->n_subbufs);
466 asprintf(&reply, "%zd", rchan->n_subbufs);
467
468 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
469 if(result) {
470 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
471 goto next_cmd;
472 }
473
474 free(reply);
475
476 break;
477 }
478 }
479 }
480 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
481 struct ltt_trace_struct *trace;
482 char trace_name[] = "auto";
483 int i;
484 char *channel_name;
485
486 DBG("get_subbuf_size");
487
488 channel_name = nth_token(recvbuf, 1);
489 if(channel_name == NULL) {
490 ERR("get_subbuf_size: cannot parse channel");
491 goto next_cmd;
492 }
493
494 ltt_lock_traces();
495 trace = _ltt_trace_find(trace_name);
496 ltt_unlock_traces();
497
498 if(trace == NULL) {
499 ERR("cannot find trace!");
500 return (void *)1;
501 }
502
503 for(i=0; i<trace->nr_channels; i++) {
504 struct rchan *rchan = trace->channels[i].trans_channel_data;
505
506 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
507 char *reply;
508
509 DBG("the subbuf_size for the requested channel is %zd", rchan->subbuf_size);
510 asprintf(&reply, "%zd", rchan->subbuf_size);
511
512 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
513 if(result) {
514 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
515 goto next_cmd;
516 }
517
518 free(reply);
519
520 break;
521 }
522 }
523 }
524 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
525 char *libfile;
526
527 libfile = nth_token(recvbuf, 1);
528
529 DBG("load_probe_lib loading %s", libfile);
530 }
531 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
532 struct ltt_trace_struct *trace;
533 char trace_name[] = "auto";
534 int i;
535 char *channel_name;
536
537 DBG("get_subbuf");
538
539 channel_name = nth_token(recvbuf, 1);
540 if(channel_name == NULL) {
541 ERR("get_subbuf: cannot parse channel");
542 goto next_cmd;
543 }
544
545 ltt_lock_traces();
546 trace = _ltt_trace_find(trace_name);
547 ltt_unlock_traces();
548
549 if(trace == NULL) {
550 ERR("cannot find trace!");
551 return (void *)1;
552 }
553
554 for(i=0; i<trace->nr_channels; i++) {
555 struct rchan *rchan = trace->channels[i].trans_channel_data;
556
557 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
558 struct rchan_buf *rbuf = rchan->buf;
559 struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
560 struct blocked_consumer *bc;
561
562 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
563 if(bc == NULL) {
564 ERR("malloc returned NULL");
565 goto next_cmd;
566 }
567 bc->fd_consumer = src.fd;
568 bc->fd_producer = lttbuf->data_ready_fd_read;
569 bc->rbuf = rbuf;
570 bc->lttbuf = lttbuf;
571 bc->src = src;
572 bc->server = ustcomm_app.server;
573
574 list_add(&bc->list, &blocked_consumers);
575
576 break;
577 }
578 }
579 }
580 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
581 struct ltt_trace_struct *trace;
582 char trace_name[] = "auto";
583 int i;
584 char *channel_name;
585 long consumed_old;
586 char *consumed_old_str;
587 char *endptr;
588
589 DBG("put_subbuf");
590
591 channel_name = strdup_malloc(nth_token(recvbuf, 1));
592 if(channel_name == NULL) {
593 ERR("put_subbuf_size: cannot parse channel");
594 goto next_cmd;
595 }
596
597 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
598 if(consumed_old_str == NULL) {
599 ERR("put_subbuf: cannot parse consumed_old");
600 goto next_cmd;
601 }
602 consumed_old = strtol(consumed_old_str, &endptr, 10);
603 if(*endptr != '\0') {
604 ERR("put_subbuf: invalid value for consumed_old");
605 goto next_cmd;
606 }
607
608 ltt_lock_traces();
609 trace = _ltt_trace_find(trace_name);
610 ltt_unlock_traces();
611
612 if(trace == NULL) {
613 ERR("cannot find trace!");
614 return (void *)1;
615 }
616
617 for(i=0; i<trace->nr_channels; i++) {
618 struct rchan *rchan = trace->channels[i].trans_channel_data;
619
620 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
621 struct rchan_buf *rbuf = rchan->buf;
622 struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
623 char *reply;
624 long consumed_old=0;
625
626 result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
627 if(result < 0) {
628 WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name);
629 asprintf(&reply, "%s", "ERROR");
630 }
631 else {
632 DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name);
633 asprintf(&reply, "%s", "OK");
634 }
635
636 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
637 if(result) {
638 ERR("listener: put_subbuf: ustcomm_send_reply failed");
639 goto next_cmd;
640 }
641
642 free(reply);
643
644 break;
645 }
646 }
647
648 free(channel_name);
649 free(consumed_old_str);
650 }
651 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
652 char *channel_slash_name = nth_token(recvbuf, 1);
653 char channel_name[256]="";
654 char marker_name[256]="";
655
656 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
657
658 if(channel_name == NULL || marker_name == NULL) {
659 WARN("invalid marker name");
660 goto next_cmd;
661 }
662 printf("%s %s\n", channel_name, marker_name);
663
664 result = ltt_marker_connect(channel_name, marker_name, "default");
665 if(result < 0) {
666 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
667 }
668 }
669 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
670 char *channel_slash_name = nth_token(recvbuf, 1);
671 char *marker_name;
672 char *channel_name;
673
674 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
675
676 if(marker_name == NULL) {
677 }
678 printf("%s %s\n", channel_name, marker_name);
679
680 result = ltt_marker_disconnect(channel_name, marker_name, "default");
681 if(result < 0) {
682 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
683 }
684 }
685 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
686 // struct ltt_trace_struct *trace;
687 // char trace_name[] = "auto";
688 // int i;
689 // char *channel_name;
690 //
691 // DBG("get_notifications");
692 //
693 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
694 // if(channel_name == NULL) {
695 // ERR("put_subbuf_size: cannot parse channel");
696 // goto next_cmd;
697 // }
698 //
699 // ltt_lock_traces();
700 // trace = _ltt_trace_find(trace_name);
701 // ltt_unlock_traces();
702 //
703 // if(trace == NULL) {
704 // ERR("cannot find trace!");
705 // return (void *)1;
706 // }
707 //
708 // for(i=0; i<trace->nr_channels; i++) {
709 // struct rchan *rchan = trace->channels[i].trans_channel_data;
710 // int fd;
711 //
712 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
713 // struct rchan_buf *rbuf = rchan->buf;
714 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
715 //
716 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
717 // if(result == -1) {
718 // ERR("ustcomm_app_detach_client failed");
719 // goto next_cmd;
720 // }
721 //
722 // lttbuf->wake_consumer_arg = (void *) fd;
723 //
724 // smp_wmb();
725 //
726 // lttbuf->call_wake_consumer = 1;
727 //
728 // break;
729 // }
730 // }
731 //
732 // free(channel_name);
733 // }
734 else {
735 ERR("unable to parse message: %s", recvbuf);
736 }
737
738 next_cmd:
739 free(recvbuf);
740 }
741 }
742
743 int have_listener = 0;
744
745 void create_listener(void)
746 {
747 #ifdef USE_CLONE
748 static char listener_stack[16384];
749 #else
750 pthread_t thread;
751 #endif
752
753 if(have_listener)
754 return;
755
756 #ifdef USE_CLONE
757 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
758 if(result == -1) {
759 perror("clone");
760 return;
761 }
762 #else
763
764 pthread_create(&thread, NULL, listener_main, NULL);
765 #endif
766
767 have_listener = 1;
768 }
769
770 /* The signal handler itself. Signals must be setup so there cannot be
771 nested signals. */
772
773 void sighandler(int sig)
774 {
775 static char have_listener = 0;
776 DBG("sighandler");
777
778 if(!have_listener) {
779 create_listener();
780 have_listener = 1;
781 }
782 }
783
784 /* Called by the app signal handler to chain it to us. */
785
786 void chain_signal(void)
787 {
788 sighandler(USTSIGNAL);
789 }
790
791 static int init_socket(void)
792 {
793 return ustcomm_init_app(getpid(), &ustcomm_app);
794 }
795
796 static int init_signal_handler(void)
797 {
798 /* Attempt to handler SIGIO. If the main program wants to
799 * handle it, fine, it'll override us. They it'll have to
800 * use the chaining function.
801 */
802
803 int result;
804 struct sigaction act;
805
806 result = sigemptyset(&act.sa_mask);
807 if(result == -1) {
808 PERROR("sigemptyset");
809 return -1;
810 }
811
812 act.sa_handler = sighandler;
813 act.sa_flags = SA_RESTART;
814
815 /* Only defer ourselves. Also, try to restart interrupted
816 * syscalls to disturb the traced program as little as possible.
817 */
818 result = sigaction(SIGIO, &act, NULL);
819 if(result == -1) {
820 PERROR("sigaction");
821 return -1;
822 }
823
824 return 0;
825 }
826
827 #define AUTOPROBE_DISABLED 0
828 #define AUTOPROBE_ENABLE_ALL 1
829 #define AUTOPROBE_ENABLE_REGEX 2
830 static int autoprobe_method = AUTOPROBE_DISABLED;
831 static regex_t autoprobe_regex;
832
833 static void auto_probe_connect(struct marker *m)
834 {
835 int result;
836
837 char* concat_name = NULL;
838 const char *probe_name = "default";
839
840 if(autoprobe_method == AUTOPROBE_DISABLED) {
841 return;
842 }
843 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
844 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
845 if(result == -1) {
846 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
847 m->channel, m->name);
848 return;
849 }
850 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
851 free(concat_name);
852 return;
853 }
854 free(concat_name);
855 }
856
857 result = ltt_marker_connect(m->channel, m->name, probe_name);
858 if(result && result != -EEXIST)
859 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
860
861 DBG("auto connected marker %s %s to probe default", m->channel, m->name);
862
863 }
864
865 static void __attribute__((constructor(1000))) init()
866 {
867 int result;
868 char* autoprobe_val = NULL;
869
870 /* Initialize RCU in case the constructor order is not good. */
871 urcu_init();
872
873 /* It is important to do this before events start to be generated. */
874 ust_register_thread();
875
876 DBG("Tracectl constructor");
877
878 /* Must create socket before signal handler to prevent races.
879 */
880 result = init_socket();
881 if(result == -1) {
882 ERR("init_socket error");
883 return;
884 }
885 result = init_signal_handler();
886 if(result == -1) {
887 ERR("init_signal_handler error");
888 return;
889 }
890
891 autoprobe_val = getenv("UST_AUTOPROBE");
892 if(autoprobe_val) {
893 struct marker_iter iter;
894
895 DBG("Autoprobe enabled.");
896
897 /* Ensure markers are initialized */
898 //init_markers();
899
900 /* Ensure marker control is initialized, for the probe */
901 init_marker_control();
902
903 /* first, set the callback that will connect the
904 * probe on new markers
905 */
906 if(autoprobe_val[0] == '/') {
907 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
908 if (result) {
909 char regexerr[150];
910
911 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
912 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
913 /* don't crash the application just for this */
914 }
915 else {
916 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
917 }
918 }
919 else {
920 /* just enable all instrumentation */
921 autoprobe_method = AUTOPROBE_ENABLE_ALL;
922 }
923
924 marker_set_new_marker_cb(auto_probe_connect);
925
926 /* Now, connect the probes that were already registered. */
927 marker_iter_reset(&iter);
928 marker_iter_start(&iter);
929
930 DBG("now iterating on markers already registered");
931 while(iter.marker) {
932 DBG("now iterating on marker %s", iter.marker->name);
933 auto_probe_connect(iter.marker);
934 marker_iter_next(&iter);
935 }
936 }
937
938 if(getenv("UST_TRACE")) {
939 char trace_name[] = "auto";
940 char trace_type[] = "ustrelay";
941
942 DBG("starting early tracing");
943
944 /* Ensure marker control is initialized */
945 init_marker_control();
946
947 /* Ensure relay is initialized */
948 init_ustrelay_transport();
949
950 /* Ensure markers are initialized */
951 init_markers();
952
953 /* In case. */
954 ltt_channels_register("ust");
955
956 result = ltt_trace_setup(trace_name);
957 if(result < 0) {
958 ERR("ltt_trace_setup failed");
959 return;
960 }
961
962 result = ltt_trace_set_type(trace_name, trace_type);
963 if(result < 0) {
964 ERR("ltt_trace_set_type failed");
965 return;
966 }
967
968 result = ltt_trace_alloc(trace_name);
969 if(result < 0) {
970 ERR("ltt_trace_alloc failed");
971 return;
972 }
973
974 inform_consumer_daemon(trace_name);
975
976 result = ltt_trace_start(trace_name);
977 if(result < 0) {
978 ERR("ltt_trace_start failed");
979 return;
980 }
981 }
982
983
984 return;
985
986 /* should decrementally destroy stuff if error */
987
988 }
989
990 /* This is only called if we terminate normally, not with an unhandled signal,
991 * so we cannot rely on it. */
992
993 /* This destructor probably isn't needed, because ustd can do crash recovery. */
994 #if 0
995 static void __attribute__((destructor)) fini()
996 {
997 // int result;
998
999 /* if trace running, finish it */
1000
1001 // DBG("destructor stopping traces");
1002
1003 // result = ltt_trace_stop("auto");
1004 // if(result == -1) {
1005 // ERR("ltt_trace_stop error");
1006 // }
1007 //
1008 // result = ltt_trace_destroy("auto");
1009 // if(result == -1) {
1010 // ERR("ltt_trace_destroy error");
1011 // }
1012 //
1013 // destroy_socket();
1014 }
1015 #endif
1016
1017 static int trace_recording(void)
1018 {
1019 int retval = 0;
1020 struct ltt_trace_struct *trace;
1021
1022 ltt_lock_traces();
1023
1024 list_for_each_entry(trace, &ltt_traces.head, list) {
1025 if(trace->active) {
1026 retval = 1;
1027 break;
1028 }
1029 }
1030
1031 ltt_unlock_traces();
1032
1033 return retval;
1034 }
1035
1036 #if 0
1037 static int have_consumer(void)
1038 {
1039 return !list_empty(&blocked_consumers);
1040 }
1041 #endif
1042
1043 int restarting_usleep(useconds_t usecs)
1044 {
1045 struct timespec tv;
1046 int result;
1047
1048 tv.tv_sec = 0;
1049 tv.tv_nsec = usecs * 1000;
1050
1051 do {
1052 result = nanosleep(&tv, &tv);
1053 } while(result == -1 && errno == EINTR);
1054
1055 return result;
1056 }
1057
1058 /* This destructor keeps the process alive for a few seconds in order
1059 * to leave time to ustd to connect to its buffers. This is necessary
1060 * for programs whose execution is very short. It is also useful in all
1061 * programs when tracing is started close to the end of the program
1062 * execution.
1063 *
1064 * FIXME: For now, this only works for the first trace created in a
1065 * process.
1066 */
1067
1068 static void __attribute__((destructor)) keepalive()
1069 {
1070 if(trace_recording() && buffers_to_export) {
1071 int total = 0;
1072 DBG("Keeping process alive for consumer daemon...");
1073 while(buffers_to_export) {
1074 const int interv = 200000;
1075 restarting_usleep(interv);
1076 total += interv;
1077
1078 if(total >= 3000000) {
1079 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
1080 break;
1081 }
1082 }
1083 DBG("Finally dying...");
1084 }
1085 }
1086
1087 /* Notify ust that there was a fork. This needs to be called inside
1088 * the new process, anytime a process whose memory is not shared with
1089 * the parent is created. If this function is not called, the events
1090 * of the new process will not be collected.
1091 */
1092
1093 void ust_fork(void)
1094 {
1095 struct blocked_consumer *bc;
1096 struct blocked_consumer *deletable_bc = NULL;
1097 int result;
1098
1099 DBG("ust: forking");
1100 ltt_trace_stop("auto");
1101 ltt_trace_destroy("auto");
1102 /* Delete all active connections */
1103 ustcomm_close_all_connections(&ustcomm_app.server);
1104
1105 /* Delete all blocked consumers */
1106 list_for_each_entry(bc, &blocked_consumers, list) {
1107 free(deletable_bc);
1108 deletable_bc = bc;
1109 list_del(&bc->list);
1110 }
1111
1112 have_listener = 0;
1113 create_listener();
1114 init_socket();
1115 ltt_trace_setup("auto");
1116 result = ltt_trace_set_type("auto", "ustrelay");
1117 if(result < 0) {
1118 ERR("ltt_trace_set_type failed");
1119 return (void *)1;
1120 }
1121
1122 ltt_trace_alloc("auto");
1123 ltt_trace_start("auto");
1124 inform_consumer_daemon("auto");
1125 }
1126
This page took 0.073562 seconds and 4 git commands to generate.