ust: add info exchange between app and ustd
[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
10 #include "marker.h"
11 #include "tracer.h"
12 #include "localerr.h"
13 #include "ustcomm.h"
14
15 //#define USE_CLONE
16
17 #define UNIX_PATH_MAX 108
18
19 #define SOCKETDIR "/tmp/socks"
20 #define SOCKETDIRLEN sizeof(SOCKETDIR)
21 #define USTSIGNAL SIGIO
22
23 #define MAX_MSG_SIZE (100)
24 #define MSG_NOTIF 1
25 #define MSG_REGISTER_NOTIF 2
26
27 char consumer_stack[10000];
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 char mysocketfile[UNIX_PATH_MAX] = "";
51 //int pfd = -1;
52
53 struct consumer_channel {
54 int fd;
55 struct ltt_channel_struct *chan;
56 };
57
58 int consumer(void *arg)
59 {
60 int result;
61 int fd;
62 char str[] = "Hello, this is the consumer.\n";
63 struct ltt_trace_struct *trace;
64 struct consumer_channel *consumer_channels;
65 int i;
66 char trace_name[] = "auto";
67
68 ltt_lock_traces();
69 trace = _ltt_trace_find(trace_name);
70 ltt_unlock_traces();
71
72 if(trace == NULL) {
73 CPRINTF("cannot find trace!");
74 return 1;
75 }
76
77 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
78 if(consumer_channels == NULL) {
79 ERR("malloc returned NULL");
80 return 1;
81 }
82
83 CPRINTF("opening trace files");
84 for(i=0; i<trace->nr_channels; i++) {
85 char tmp[100];
86 struct ltt_channel_struct *chan = &trace->channels[i];
87
88 consumer_channels[i].chan = chan;
89
90 snprintf(tmp, sizeof(tmp), "trace/%s_0", chan->channel_name);
91 result = consumer_channels[i].fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 00644);
92 if(result == -1) {
93 perror("open");
94 return -1;
95 }
96 CPRINTF("\topened trace file %s", tmp);
97
98 }
99 CPRINTF("done opening trace files");
100
101 for(;;) {
102 /*wait*/
103
104 for(i=0; i<trace->nr_channels; i++) {
105 struct rchan *rchan = consumer_channels[i].chan->trans_channel_data;
106 struct rchan_buf *rbuf = rchan->buf;
107 struct ltt_channel_buf_struct *lttbuf = consumer_channels[i].chan->buf;
108 long consumed_old;
109
110 result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
111 if(result < 0) {
112 DBG("ltt_do_get_subbuf: error: %s", strerror(-result));
113 }
114 else {
115 DBG("success!");
116
117 result = write(consumer_channels[i].fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
118 ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
119 }
120 }
121
122 sleep(1);
123 }
124
125 // CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
126 //
127 // struct ltt_channel_struct *chan = &trace->channels[0];
128 //
129 // CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
130
131 // struct rchan *rchan = chan->trans_channel_data;
132 // struct rchan_buf *rbuf = rchan->buf;
133 // struct ltt_channel_buf_struct *lttbuf = chan->buf;
134 // long consumed_old;
135 //
136 // result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
137 // if(result == -1) {
138 // perror("open");
139 // return -1;
140 // }
141
142 // for(;;) {
143 // write(STDOUT_FILENO, str, sizeof(str));
144 //
145 // result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
146 // if(result < 0) {
147 // CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
148 // }
149 // else {
150 // CPRINTF("success!");
151 //
152 // result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
153 // ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
154 // }
155 //
156 // //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(&lttbuf->offset), rbuf->chan) - consumed_old);
157 // CPRINTF("Commit count %ld", local_read(&lttbuf->commit_count[0]));
158 //
159 //
160 // sleep(1);
161 // }
162 }
163
164 void start_consumer(void)
165 {
166 #ifdef USE_CLONE
167 int result;
168
169 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
170 if(result == -1) {
171 perror("clone");
172 }
173 #else
174 pthread_t thread;
175
176 pthread_create(&thread, NULL, consumer, NULL);
177 #endif
178 }
179
180 static void print_markers(void)
181 {
182 struct marker_iter iter;
183
184 lock_markers();
185 marker_iter_reset(&iter);
186 marker_iter_start(&iter);
187
188 while(iter.marker) {
189 fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format);
190 marker_iter_next(&iter);
191 }
192 unlock_markers();
193 }
194
195 void do_command(struct tracecmd *cmd)
196 {
197 }
198
199 void receive_commands()
200 {
201 }
202
203 int fd_notif = -1;
204 void notif_cb(void)
205 {
206 int result;
207 struct trctl_msg msg;
208
209 /* FIXME: fd_notif should probably be protected by a spinlock */
210
211 if(fd_notif == -1)
212 return;
213
214 msg.type = MSG_NOTIF;
215 msg.size = sizeof(msg.type);
216
217 /* FIXME: don't block here */
218 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
219 if(result == -1) {
220 PERROR("write");
221 return;
222 }
223 }
224
225 #define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd"
226
227 static int inform_consumer_daemon(void)
228 {
229 ustcomm_request_consumer(getpid(), "metadata");
230 ustcomm_request_consumer(getpid(), "ust");
231 }
232
233 int listener_main(void *p)
234 {
235 int result;
236
237 DBG("LISTENER");
238
239 for(;;) {
240 uint32_t size;
241 struct sockaddr_un addr;
242 socklen_t addrlen = sizeof(addr);
243 char trace_name[] = "auto";
244 char trace_type[] = "ustrelay";
245 char *recvbuf;
246 int len;
247 struct ustcomm_source src;
248
249 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src);
250 DBG("HERE");
251 if(result) {
252 WARN("error in ustcomm_app_recv_message");
253 continue;
254 }
255
256 DBG("received a message! it's: %s\n", recvbuf);
257 len = strlen(recvbuf);
258 //if(len && recvbuf[len-1] == '\n') {
259 // recvbuf[len-1] = '\0';
260 //}
261
262 if(!strcmp(recvbuf, "print_markers")) {
263 print_markers();
264 }
265 else if(!strcmp(recvbuf, "trace_setup")) {
266 DBG("trace setup");
267
268 result = ltt_trace_setup(trace_name);
269 if(result < 0) {
270 ERR("ltt_trace_setup failed");
271 return;
272 }
273
274 result = ltt_trace_set_type(trace_name, trace_type);
275 if(result < 0) {
276 ERR("ltt_trace_set_type failed");
277 return;
278 }
279 }
280 else if(!strcmp(recvbuf, "trace_alloc")) {
281 DBG("trace alloc");
282
283 result = ltt_trace_alloc(trace_name);
284 if(result < 0) {
285 ERR("ltt_trace_alloc failed");
286 return;
287 }
288 }
289 else if(!strcmp(recvbuf, "trace_start")) {
290 DBG("trace start");
291
292 result = ltt_trace_start(trace_name);
293 if(result < 0) {
294 ERR("ltt_trace_start failed");
295 continue;
296 }
297 }
298 else if(!strcmp(recvbuf, "trace_stop")) {
299 DBG("trace stop");
300
301 result = ltt_trace_stop(trace_name);
302 if(result < 0) {
303 ERR("ltt_trace_stop failed");
304 return;
305 }
306 }
307 else if(!strcmp(recvbuf, "trace_destroy")) {
308
309 DBG("trace destroy");
310
311 result = ltt_trace_destroy(trace_name);
312 if(result < 0) {
313 ERR("ltt_trace_destroy failed");
314 return;
315 }
316 }
317 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
318 struct ltt_trace_struct *trace;
319 char trace_name[] = "auto";
320 int i;
321 char *channel_name;
322
323 DBG("get_shmid");
324
325 channel_name = nth_token(recvbuf, 1);
326 if(channel_name == NULL) {
327 ERR("get_shmid: cannot parse channel");
328 goto next_cmd;
329 }
330
331 ltt_lock_traces();
332 trace = _ltt_trace_find(trace_name);
333 ltt_unlock_traces();
334
335 if(trace == NULL) {
336 CPRINTF("cannot find trace!");
337 return 1;
338 }
339
340 for(i=0; i<trace->nr_channels; i++) {
341 struct rchan *rchan = trace->channels[i].trans_channel_data;
342 struct rchan_buf *rbuf = rchan->buf;
343
344 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
345 char *reply;
346
347 DBG("the shmid for the requested channel is %d", rbuf->shmid);
348 asprintf(&reply, "%d", rbuf->shmid);
349
350 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
351 if(result) {
352 ERR("listener: get_shmid: ustcomm_send_reply failed");
353 goto next_cmd;
354 }
355
356 free(reply);
357
358 break;
359 }
360 }
361 }
362 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
363 struct ltt_trace_struct *trace;
364 char trace_name[] = "auto";
365 int i;
366 char *channel_name;
367
368 DBG("get_n_subbufs");
369
370 channel_name = nth_token(recvbuf, 1);
371 if(channel_name == NULL) {
372 ERR("get_n_subbufs: cannot parse channel");
373 goto next_cmd;
374 }
375
376 ltt_lock_traces();
377 trace = _ltt_trace_find(trace_name);
378 ltt_unlock_traces();
379
380 if(trace == NULL) {
381 CPRINTF("cannot find trace!");
382 return 1;
383 }
384
385 for(i=0; i<trace->nr_channels; i++) {
386 struct rchan *rchan = trace->channels[i].trans_channel_data;
387
388 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
389 char *reply;
390
391 DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs);
392 asprintf(&reply, "%d", rchan->n_subbufs);
393
394 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
395 if(result) {
396 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
397 goto next_cmd;
398 }
399
400 free(reply);
401
402 break;
403 }
404 }
405 }
406 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
407 struct ltt_trace_struct *trace;
408 char trace_name[] = "auto";
409 int i;
410 char *channel_name;
411
412 DBG("get_subbuf_size");
413
414 channel_name = nth_token(recvbuf, 1);
415 if(channel_name == NULL) {
416 ERR("get_subbuf_size: cannot parse channel");
417 goto next_cmd;
418 }
419
420 ltt_lock_traces();
421 trace = _ltt_trace_find(trace_name);
422 ltt_unlock_traces();
423
424 if(trace == NULL) {
425 CPRINTF("cannot find trace!");
426 return 1;
427 }
428
429 for(i=0; i<trace->nr_channels; i++) {
430 struct rchan *rchan = trace->channels[i].trans_channel_data;
431
432 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
433 char *reply;
434
435 DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size);
436 asprintf(&reply, "%d", rchan->subbuf_size);
437
438 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
439 if(result) {
440 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
441 goto next_cmd;
442 }
443
444 free(reply);
445
446 break;
447 }
448 }
449 }
450 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
451 char *libfile;
452
453 libfile = nth_token(recvbuf, 1);
454
455 DBG("load_probe_lib loading %s", libfile);
456 }
457
458 next_cmd:
459 free(recvbuf);
460 }
461 }
462
463 static char listener_stack[16384];
464
465 void create_listener(void)
466 {
467 int result;
468 static char listener_stack[16384];
469 //char *listener_stack = malloc(16384);
470
471 #ifdef USE_CLONE
472 result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
473 if(result == -1) {
474 perror("clone");
475 }
476 #else
477 pthread_t thread;
478
479 pthread_create(&thread, NULL, listener_main, NULL);
480 #endif
481 }
482
483 /* The signal handler itself. Signals must be setup so there cannot be
484 nested signals. */
485
486 void sighandler(int sig)
487 {
488 static char have_listener = 0;
489 DBG("sighandler");
490
491 if(!have_listener) {
492 create_listener();
493 have_listener = 1;
494 }
495 }
496
497 /* Called by the app signal handler to chain it to us. */
498
499 void chain_signal(void)
500 {
501 sighandler(USTSIGNAL);
502 }
503
504 static int init_socket(void)
505 {
506 return ustcomm_init_app(getpid(), &ustcomm_app);
507 }
508
509 static void destroy_socket(void)
510 {
511 int result;
512
513 if(mysocketfile[0] == '\0')
514 return;
515
516 result = unlink(mysocketfile);
517 if(result == -1) {
518 PERROR("unlink");
519 }
520 }
521
522 static int init_signal_handler(void)
523 {
524 /* Attempt to handler SIGIO. If the main program wants to
525 * handle it, fine, it'll override us. They it'll have to
526 * use the chaining function.
527 */
528
529 int result;
530 struct sigaction act;
531
532 result = sigemptyset(&act.sa_mask);
533 if(result == -1) {
534 PERROR("sigemptyset");
535 return -1;
536 }
537
538 act.sa_handler = sighandler;
539 act.sa_flags = SA_RESTART;
540
541 /* Only defer ourselves. Also, try to restart interrupted
542 * syscalls to disturb the traced program as little as possible.
543 */
544 result = sigaction(SIGIO, &act, NULL);
545 if(result == -1) {
546 PERROR("sigaction");
547 return -1;
548 }
549
550 return 0;
551 }
552
553 static void auto_probe_connect(struct marker *m)
554 {
555 int result;
556
557 result = ltt_marker_connect(m->channel, m->name, "default");
558 if(result)
559 ERR("ltt_marker_connect");
560
561 DBG("just auto connected marker %s %s to probe default", m->channel, m->name);
562 }
563
564 static void __attribute__((constructor(101))) init0()
565 {
566 DBG("UST_AUTOPROBE constructor");
567 if(getenv("UST_AUTOPROBE")) {
568 marker_set_new_marker_cb(auto_probe_connect);
569 }
570 }
571
572 static void fini(void);
573
574 static void __attribute__((constructor(1000))) init()
575 {
576 int result;
577
578 DBG("UST_TRACE constructor");
579
580 /* Must create socket before signal handler to prevent races.
581 */
582 result = init_socket();
583 if(result == -1) {
584 ERR("init_socket error");
585 return;
586 }
587 result = init_signal_handler();
588 if(result == -1) {
589 ERR("init_signal_handler error");
590 return;
591 }
592
593 if(getenv("UST_TRACE")) {
594 char trace_name[] = "auto";
595 char trace_type[] = "ustrelay";
596
597 DBG("starting early tracing");
598
599 /* Ensure marker control is initialized */
600 init_marker_control();
601
602 /* Ensure relay is initialized */
603 init_ustrelay_transport();
604
605 /* Ensure markers are initialized */
606 init_markers();
607
608 /* In case. */
609 ltt_channels_register("ust");
610
611 result = ltt_trace_setup(trace_name);
612 if(result < 0) {
613 ERR("ltt_trace_setup failed");
614 return;
615 }
616
617 result = ltt_trace_set_type(trace_name, trace_type);
618 if(result < 0) {
619 ERR("ltt_trace_set_type failed");
620 return;
621 }
622
623 result = ltt_trace_alloc(trace_name);
624 if(result < 0) {
625 ERR("ltt_trace_alloc failed");
626 return;
627 }
628
629 result = ltt_trace_start(trace_name);
630 if(result < 0) {
631 ERR("ltt_trace_start failed");
632 return;
633 }
634 //start_consumer();
635 inform_consumer_daemon();
636 }
637
638
639 return;
640
641 /* should decrementally destroy stuff if error */
642
643 }
644
645 /* This is only called if we terminate normally, not with an unhandled signal,
646 * so we cannot rely on it. */
647
648 static void __attribute__((destructor)) fini()
649 {
650 int result;
651
652 /* if trace running, finish it */
653
654 DBG("destructor stopping traces");
655
656 result = ltt_trace_stop("auto");
657 if(result == -1) {
658 ERR("ltt_trace_stop error");
659 }
660
661 result = ltt_trace_destroy("auto");
662 if(result == -1) {
663 ERR("ltt_trace_destroy error");
664 }
665
666 /* FIXME: wait for the consumer to be done */
667 sleep(1);
668
669 destroy_socket();
670 }
This page took 0.043246 seconds and 4 git commands to generate.