add libtracing Makefile.am
[lttng-ust.git] / libtracectl / tracectl.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <signal.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7 #include <sched.h>
8 #include <fcntl.h>
9 #include <poll.h>
10
11 #include "marker.h"
12 #include "tracer.h"
13 #include "localerr.h"
14 #include "ustcomm.h"
15 #include "relay.h" /* FIXME: remove */
16
17 //#define USE_CLONE
18
19 #define USTSIGNAL SIGIO
20
21 #define MAX_MSG_SIZE (100)
22 #define MSG_NOTIF 1
23 #define MSG_REGISTER_NOTIF 2
24
25 char consumer_stack[10000];
26
27 struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers);
28
29 static struct ustcomm_app ustcomm_app;
30
31 struct tracecmd { /* no padding */
32 uint32_t size;
33 uint16_t command;
34 };
35
36 //struct listener_arg {
37 // int pipe_fd;
38 //};
39
40 struct trctl_msg {
41 /* size: the size of all the fields except size itself */
42 uint32_t size;
43 uint16_t type;
44 /* Only the necessary part of the payload is transferred. It
45 * may even be none of it.
46 */
47 char payload[94];
48 };
49
50 struct consumer_channel {
51 int fd;
52 struct ltt_channel_struct *chan;
53 };
54
55 struct blocked_consumer {
56 int fd_consumer;
57 int fd_producer;
58 int tmp_poll_idx;
59
60 /* args to ustcomm_send_reply */
61 struct ustcomm_server server;
62 struct ustcomm_source src;
63
64 /* args to ltt_do_get_subbuf */
65 struct rchan_buf *rbuf;
66 struct ltt_channel_buf_struct *lttbuf;
67
68 struct list_head list;
69 };
70
71 static void print_markers(void)
72 {
73 struct marker_iter iter;
74
75 lock_markers();
76 marker_iter_reset(&iter);
77 marker_iter_start(&iter);
78
79 while(iter.marker) {
80 fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format);
81 marker_iter_next(&iter);
82 }
83 unlock_markers();
84 }
85
86 void do_command(struct tracecmd *cmd)
87 {
88 }
89
90 void receive_commands()
91 {
92 }
93
94 int fd_notif = -1;
95 void notif_cb(void)
96 {
97 int result;
98 struct trctl_msg msg;
99
100 /* FIXME: fd_notif should probably be protected by a spinlock */
101
102 if(fd_notif == -1)
103 return;
104
105 msg.type = MSG_NOTIF;
106 msg.size = sizeof(msg.type);
107
108 /* FIXME: don't block here */
109 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
110 if(result == -1) {
111 PERROR("write");
112 return;
113 }
114 }
115
116 static int inform_consumer_daemon(void)
117 {
118 ustcomm_request_consumer(getpid(), "metadata");
119 ustcomm_request_consumer(getpid(), "ust");
120 }
121
122 void process_blocked_consumers(void)
123 {
124 int n_fds = 0;
125 struct pollfd *fds;
126 struct blocked_consumer *bc;
127 int idx = 0;
128 char inbuf;
129 int result;
130
131 list_for_each_entry(bc, &blocked_consumers, list) {
132 n_fds++;
133 }
134
135 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
136 if(fds == NULL) {
137 ERR("malloc returned NULL");
138 return;
139 }
140
141 list_for_each_entry(bc, &blocked_consumers, list) {
142 fds[idx].fd = bc->fd_producer;
143 fds[idx].events = POLLIN;
144 bc->tmp_poll_idx = idx;
145 idx++;
146 }
147
148 result = poll(fds, n_fds, 0);
149 if(result == -1) {
150 PERROR("poll");
151 return -1;
152 }
153
154 list_for_each_entry(bc, &blocked_consumers, list) {
155 if(fds[bc->tmp_poll_idx].revents) {
156 long consumed_old = 0;
157 char *reply;
158
159 result = read(bc->fd_producer, &inbuf, 1);
160 if(result == -1) {
161 PERROR("read");
162 continue;
163 }
164 if(result == 0) {
165 DBG("PRODUCER END");
166
167 close(bc->fd_producer);
168
169 __list_del(bc->list.prev, bc->list.next);
170
171 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
172 if(result < 0) {
173 ERR("ustcomm_send_reply failed");
174 continue;
175 }
176
177 continue;
178 }
179
180 result = ltt_do_get_subbuf(bc->rbuf, bc->lttbuf, &consumed_old);
181 if(result == -EAGAIN) {
182 WARN("missed buffer?");
183 continue;
184 }
185 else if(result < 0) {
186 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
187 }
188 asprintf(&reply, "%s %ld", "OK", consumed_old);
189 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
190 if(result < 0) {
191 ERR("ustcomm_send_reply failed");
192 free(reply);
193 continue;
194 }
195 free(reply);
196
197 __list_del(bc->list.prev, bc->list.next);
198 }
199 }
200
201 }
202
203 int listener_main(void *p)
204 {
205 int result;
206
207 DBG("LISTENER");
208
209 for(;;) {
210 uint32_t size;
211 struct sockaddr_un addr;
212 socklen_t addrlen = sizeof(addr);
213 char trace_name[] = "auto";
214 char trace_type[] = "ustrelay";
215 char *recvbuf;
216 int len;
217 struct ustcomm_source src;
218
219 process_blocked_consumers();
220
221 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
222 if(result < 0) {
223 WARN("error in ustcomm_app_recv_message");
224 continue;
225 }
226 else if(result == 0) {
227 /* no message */
228 continue;
229 }
230
231 DBG("received a message! it's: %s\n", recvbuf);
232 len = strlen(recvbuf);
233
234 if(!strcmp(recvbuf, "print_markers")) {
235 print_markers();
236 }
237 else if(!strcmp(recvbuf, "trace_setup")) {
238 DBG("trace setup");
239
240 result = ltt_trace_setup(trace_name);
241 if(result < 0) {
242 ERR("ltt_trace_setup failed");
243 return;
244 }
245
246 result = ltt_trace_set_type(trace_name, trace_type);
247 if(result < 0) {
248 ERR("ltt_trace_set_type failed");
249 return;
250 }
251 }
252 else if(!strcmp(recvbuf, "trace_alloc")) {
253 DBG("trace alloc");
254
255 result = ltt_trace_alloc(trace_name);
256 if(result < 0) {
257 ERR("ltt_trace_alloc failed");
258 return;
259 }
260 }
261 else if(!strcmp(recvbuf, "trace_start")) {
262 DBG("trace start");
263
264 result = ltt_trace_start(trace_name);
265 if(result < 0) {
266 ERR("ltt_trace_start failed");
267 continue;
268 }
269 }
270 else if(!strcmp(recvbuf, "trace_stop")) {
271 DBG("trace stop");
272
273 result = ltt_trace_stop(trace_name);
274 if(result < 0) {
275 ERR("ltt_trace_stop failed");
276 return;
277 }
278 }
279 else if(!strcmp(recvbuf, "trace_destroy")) {
280
281 DBG("trace destroy");
282
283 result = ltt_trace_destroy(trace_name);
284 if(result < 0) {
285 ERR("ltt_trace_destroy failed");
286 return;
287 }
288 }
289 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
290 struct ltt_trace_struct *trace;
291 char trace_name[] = "auto";
292 int i;
293 char *channel_name;
294
295 DBG("get_shmid");
296
297 channel_name = nth_token(recvbuf, 1);
298 if(channel_name == NULL) {
299 ERR("get_shmid: cannot parse channel");
300 goto next_cmd;
301 }
302
303 ltt_lock_traces();
304 trace = _ltt_trace_find(trace_name);
305 ltt_unlock_traces();
306
307 if(trace == NULL) {
308 CPRINTF("cannot find trace!");
309 return 1;
310 }
311
312 for(i=0; i<trace->nr_channels; i++) {
313 struct rchan *rchan = trace->channels[i].trans_channel_data;
314 struct rchan_buf *rbuf = rchan->buf;
315 struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)rchan->private_data;
316 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
317
318 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
319 char *reply;
320
321 DBG("the shmid for the requested channel is %d", rbuf->shmid);
322 DBG("the shmid for its buffer structure is %d", ltt_channel->buf_shmid);
323 asprintf(&reply, "%d %d", rbuf->shmid, ltt_channel->buf_shmid);
324
325 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
326 if(result) {
327 ERR("listener: get_shmid: ustcomm_send_reply failed");
328 goto next_cmd;
329 }
330
331 free(reply);
332
333 break;
334 }
335 }
336 }
337 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
338 struct ltt_trace_struct *trace;
339 char trace_name[] = "auto";
340 int i;
341 char *channel_name;
342
343 DBG("get_n_subbufs");
344
345 channel_name = nth_token(recvbuf, 1);
346 if(channel_name == NULL) {
347 ERR("get_n_subbufs: cannot parse channel");
348 goto next_cmd;
349 }
350
351 ltt_lock_traces();
352 trace = _ltt_trace_find(trace_name);
353 ltt_unlock_traces();
354
355 if(trace == NULL) {
356 CPRINTF("cannot find trace!");
357 return 1;
358 }
359
360 for(i=0; i<trace->nr_channels; i++) {
361 struct rchan *rchan = trace->channels[i].trans_channel_data;
362
363 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
364 char *reply;
365
366 DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs);
367 asprintf(&reply, "%d", rchan->n_subbufs);
368
369 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
370 if(result) {
371 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
372 goto next_cmd;
373 }
374
375 free(reply);
376
377 break;
378 }
379 }
380 }
381 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
382 struct ltt_trace_struct *trace;
383 char trace_name[] = "auto";
384 int i;
385 char *channel_name;
386
387 DBG("get_subbuf_size");
388
389 channel_name = nth_token(recvbuf, 1);
390 if(channel_name == NULL) {
391 ERR("get_subbuf_size: cannot parse channel");
392 goto next_cmd;
393 }
394
395 ltt_lock_traces();
396 trace = _ltt_trace_find(trace_name);
397 ltt_unlock_traces();
398
399 if(trace == NULL) {
400 CPRINTF("cannot find trace!");
401 return 1;
402 }
403
404 for(i=0; i<trace->nr_channels; i++) {
405 struct rchan *rchan = trace->channels[i].trans_channel_data;
406
407 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
408 char *reply;
409
410 DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size);
411 asprintf(&reply, "%d", rchan->subbuf_size);
412
413 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
414 if(result) {
415 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
416 goto next_cmd;
417 }
418
419 free(reply);
420
421 break;
422 }
423 }
424 }
425 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
426 char *libfile;
427
428 libfile = nth_token(recvbuf, 1);
429
430 DBG("load_probe_lib loading %s", libfile);
431 }
432 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
433 struct ltt_trace_struct *trace;
434 char trace_name[] = "auto";
435 int i;
436 char *channel_name;
437
438 DBG("get_subbuf");
439
440 channel_name = nth_token(recvbuf, 1);
441 if(channel_name == NULL) {
442 ERR("get_subbuf: cannot parse channel");
443 goto next_cmd;
444 }
445
446 ltt_lock_traces();
447 trace = _ltt_trace_find(trace_name);
448 ltt_unlock_traces();
449
450 if(trace == NULL) {
451 CPRINTF("cannot find trace!");
452 return 1;
453 }
454
455 for(i=0; i<trace->nr_channels; i++) {
456 struct rchan *rchan = trace->channels[i].trans_channel_data;
457
458 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
459 struct rchan_buf *rbuf = rchan->buf;
460 struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
461 char *reply;
462 long consumed_old=0;
463 int fd;
464 struct blocked_consumer *bc;
465
466 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
467 if(bc == NULL) {
468 ERR("malloc returned NULL");
469 goto next_cmd;
470 }
471 bc->fd_consumer = src.fd;
472 bc->fd_producer = lttbuf->data_ready_fd_read;
473 bc->rbuf = rbuf;
474 bc->lttbuf = lttbuf;
475 bc->src = src;
476 bc->server = ustcomm_app.server;
477
478 list_add(&bc->list, &blocked_consumers);
479
480 break;
481 }
482 }
483 }
484 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
485 struct ltt_trace_struct *trace;
486 char trace_name[] = "auto";
487 int i;
488 char *channel_name;
489 long consumed_old;
490 char *consumed_old_str;
491 char *endptr;
492
493 DBG("put_subbuf");
494
495 channel_name = strdup_malloc(nth_token(recvbuf, 1));
496 if(channel_name == NULL) {
497 ERR("put_subbuf_size: cannot parse channel");
498 goto next_cmd;
499 }
500
501 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
502 if(consumed_old_str == NULL) {
503 ERR("put_subbuf: cannot parse consumed_old");
504 goto next_cmd;
505 }
506 consumed_old = strtol(consumed_old_str, &endptr, 10);
507 if(*endptr != '\0') {
508 ERR("put_subbuf: invalid value for consumed_old");
509 goto next_cmd;
510 }
511
512 ltt_lock_traces();
513 trace = _ltt_trace_find(trace_name);
514 ltt_unlock_traces();
515
516 if(trace == NULL) {
517 CPRINTF("cannot find trace!");
518 return 1;
519 }
520
521 for(i=0; i<trace->nr_channels; i++) {
522 struct rchan *rchan = trace->channels[i].trans_channel_data;
523
524 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
525 struct rchan_buf *rbuf = rchan->buf;
526 struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
527 char *reply;
528 long consumed_old=0;
529
530 result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
531 if(result < 0) {
532 WARN("ltt_do_put_subbuf: error");
533 }
534 else {
535 DBG("ltt_do_put_subbuf: success");
536 }
537 asprintf(&reply, "%s", "OK", consumed_old);
538
539 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
540 if(result) {
541 ERR("listener: put_subbuf: ustcomm_send_reply failed");
542 goto next_cmd;
543 }
544
545 free(reply);
546
547 break;
548 }
549 }
550
551 free(channel_name);
552 free(consumed_old_str);
553 }
554 // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
555 // struct ltt_trace_struct *trace;
556 // char trace_name[] = "auto";
557 // int i;
558 // char *channel_name;
559 //
560 // DBG("get_notifications");
561 //
562 // channel_name = strdup_malloc(nth_token(recvbuf, 1));
563 // if(channel_name == NULL) {
564 // ERR("put_subbuf_size: cannot parse channel");
565 // goto next_cmd;
566 // }
567 //
568 // ltt_lock_traces();
569 // trace = _ltt_trace_find(trace_name);
570 // ltt_unlock_traces();
571 //
572 // if(trace == NULL) {
573 // CPRINTF("cannot find trace!");
574 // return 1;
575 // }
576 //
577 // for(i=0; i<trace->nr_channels; i++) {
578 // struct rchan *rchan = trace->channels[i].trans_channel_data;
579 // int fd;
580 //
581 // if(!strcmp(trace->channels[i].channel_name, channel_name)) {
582 // struct rchan_buf *rbuf = rchan->buf;
583 // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
584 //
585 // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
586 // if(result == -1) {
587 // ERR("ustcomm_app_detach_client failed");
588 // goto next_cmd;
589 // }
590 //
591 // lttbuf->wake_consumer_arg = (void *) fd;
592 //
593 // smp_wmb();
594 //
595 // lttbuf->call_wake_consumer = 1;
596 //
597 // break;
598 // }
599 // }
600 //
601 // free(channel_name);
602 // }
603 else {
604 ERR("unable to parse message: %s", recvbuf);
605 }
606
607 next_cmd:
608 free(recvbuf);
609 }
610 }
611
612 static char listener_stack[16384];
613
614 void create_listener(void)
615 {
616 int result;
617 static char listener_stack[16384];
618 //char *listener_stack = malloc(16384);
619
620 #ifdef USE_CLONE
621 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
622 if(result == -1) {
623 perror("clone");
624 }
625 #else
626 pthread_t thread;
627
628 pthread_create(&thread, NULL, listener_main, NULL);
629 #endif
630 }
631
632 /* The signal handler itself. Signals must be setup so there cannot be
633 nested signals. */
634
635 void sighandler(int sig)
636 {
637 static char have_listener = 0;
638 DBG("sighandler");
639
640 if(!have_listener) {
641 create_listener();
642 have_listener = 1;
643 }
644 }
645
646 /* Called by the app signal handler to chain it to us. */
647
648 void chain_signal(void)
649 {
650 sighandler(USTSIGNAL);
651 }
652
653 static int init_socket(void)
654 {
655 return ustcomm_init_app(getpid(), &ustcomm_app);
656 }
657
658 static void destroy_socket(void)
659 {
660 // int result;
661 //
662 // if(mysocketfile[0] == '\0')
663 // return;
664 //
665 // result = unlink(mysocketfile);
666 // if(result == -1) {
667 // PERROR("unlink");
668 // }
669 }
670
671 static int init_signal_handler(void)
672 {
673 /* Attempt to handler SIGIO. If the main program wants to
674 * handle it, fine, it'll override us. They it'll have to
675 * use the chaining function.
676 */
677
678 int result;
679 struct sigaction act;
680
681 result = sigemptyset(&act.sa_mask);
682 if(result == -1) {
683 PERROR("sigemptyset");
684 return -1;
685 }
686
687 act.sa_handler = sighandler;
688 act.sa_flags = SA_RESTART;
689
690 /* Only defer ourselves. Also, try to restart interrupted
691 * syscalls to disturb the traced program as little as possible.
692 */
693 result = sigaction(SIGIO, &act, NULL);
694 if(result == -1) {
695 PERROR("sigaction");
696 return -1;
697 }
698
699 return 0;
700 }
701
702 static void auto_probe_connect(struct marker *m)
703 {
704 int result;
705
706 result = ltt_marker_connect(m->channel, m->name, "default");
707 if(result)
708 ERR("ltt_marker_connect");
709
710 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
711 }
712
713 static void __attribute__((constructor(101))) init0()
714 {
715 DBG("UST_AUTOPROBE constructor");
716 if(getenv("UST_AUTOPROBE")) {
717 marker_set_new_marker_cb(auto_probe_connect);
718 }
719 }
720
721 static void fini(void);
722
723 static void __attribute__((constructor(1000))) init()
724 {
725 int result;
726
727 DBG("UST_TRACE constructor");
728
729 /* Must create socket before signal handler to prevent races.
730 */
731 result = init_socket();
732 if(result == -1) {
733 ERR("init_socket error");
734 return;
735 }
736 result = init_signal_handler();
737 if(result == -1) {
738 ERR("init_signal_handler error");
739 return;
740 }
741
742 if(getenv("UST_TRACE")) {
743 char trace_name[] = "auto";
744 char trace_type[] = "ustrelay";
745
746 DBG("starting early tracing");
747
748 /* Ensure marker control is initialized */
749 init_marker_control();
750
751 /* Ensure relay is initialized */
752 init_ustrelay_transport();
753
754 /* Ensure markers are initialized */
755 init_markers();
756
757 /* In case. */
758 ltt_channels_register("ust");
759
760 result = ltt_trace_setup(trace_name);
761 if(result < 0) {
762 ERR("ltt_trace_setup failed");
763 return;
764 }
765
766 result = ltt_trace_set_type(trace_name, trace_type);
767 if(result < 0) {
768 ERR("ltt_trace_set_type failed");
769 return;
770 }
771
772 result = ltt_trace_alloc(trace_name);
773 if(result < 0) {
774 ERR("ltt_trace_alloc failed");
775 return;
776 }
777
778 result = ltt_trace_start(trace_name);
779 if(result < 0) {
780 ERR("ltt_trace_start failed");
781 return;
782 }
783 //start_consumer();
784 inform_consumer_daemon();
785 }
786
787
788 return;
789
790 /* should decrementally destroy stuff if error */
791
792 }
793
794 /* This is only called if we terminate normally, not with an unhandled signal,
795 * so we cannot rely on it. */
796
797 static void __attribute__((destructor)) fini()
798 {
799 int result;
800
801 /* if trace running, finish it */
802
803 DBG("destructor stopping traces");
804
805 result = ltt_trace_stop("auto");
806 if(result == -1) {
807 ERR("ltt_trace_stop error");
808 }
809
810 result = ltt_trace_destroy("auto");
811 if(result == -1) {
812 ERR("ltt_trace_destroy error");
813 }
814
815 /* FIXME: wait for the consumer to be done */
816 //DBG("waiting 5 sec for consume");
817 //sleep(5);
818
819 destroy_socket();
820 }
This page took 0.045559 seconds and 4 git commands to generate.