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