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