Commit | Line | Data |
---|---|---|
68c1021b PMF |
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> | |
98963de4 | 7 | #include <sched.h> |
a584bc4e | 8 | #include <fcntl.h> |
3a7b90de | 9 | #include <poll.h> |
fbd8191b PMF |
10 | |
11 | #include "marker.h" | |
a584bc4e | 12 | #include "tracer.h" |
d0b5f2b9 PMF |
13 | #include "localerr.h" |
14 | #include "ustcomm.h" | |
46ef48cd | 15 | #include "relay.h" /* FIXME: remove */ |
fbd8191b | 16 | |
b02e31e5 | 17 | //#define USE_CLONE |
3847c3ba | 18 | |
68c1021b PMF |
19 | #define USTSIGNAL SIGIO |
20 | ||
98963de4 PMF |
21 | #define MAX_MSG_SIZE (100) |
22 | #define MSG_NOTIF 1 | |
23 | #define MSG_REGISTER_NOTIF 2 | |
24 | ||
a584bc4e PMF |
25 | char consumer_stack[10000]; |
26 | ||
3a7b90de PMF |
27 | struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers); |
28 | ||
d0b5f2b9 PMF |
29 | static struct ustcomm_app ustcomm_app; |
30 | ||
68c1021b PMF |
31 | struct tracecmd { /* no padding */ |
32 | uint32_t size; | |
33 | uint16_t command; | |
34 | }; | |
35 | ||
98963de4 PMF |
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 | }; | |
68c1021b | 49 | |
a584bc4e PMF |
50 | struct consumer_channel { |
51 | int fd; | |
52 | struct ltt_channel_struct *chan; | |
53 | }; | |
54 | ||
3a7b90de PMF |
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 | ||
fbd8191b PMF |
71 | static void print_markers(void) |
72 | { | |
73 | struct marker_iter iter; | |
74 | ||
d0b5f2b9 | 75 | lock_markers(); |
fbd8191b PMF |
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 | } | |
d0b5f2b9 | 83 | unlock_markers(); |
fbd8191b PMF |
84 | } |
85 | ||
68c1021b PMF |
86 | void do_command(struct tracecmd *cmd) |
87 | { | |
88 | } | |
89 | ||
90 | void receive_commands() | |
91 | { | |
92 | } | |
93 | ||
98963de4 PMF |
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 | ||
d0b5f2b9 PMF |
116 | static int inform_consumer_daemon(void) |
117 | { | |
3847c3ba PMF |
118 | ustcomm_request_consumer(getpid(), "metadata"); |
119 | ustcomm_request_consumer(getpid(), "ust"); | |
d0b5f2b9 | 120 | } |
fbd8191b | 121 | |
3a7b90de PMF |
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 | ||
98963de4 PMF |
203 | int listener_main(void *p) |
204 | { | |
205 | int result; | |
206 | ||
b0540e11 PMF |
207 | DBG("LISTENER"); |
208 | ||
98963de4 | 209 | for(;;) { |
98963de4 | 210 | uint32_t size; |
98963de4 PMF |
211 | struct sockaddr_un addr; |
212 | socklen_t addrlen = sizeof(addr); | |
aafb1650 PMF |
213 | char trace_name[] = "auto"; |
214 | char trace_type[] = "ustrelay"; | |
d0b5f2b9 PMF |
215 | char *recvbuf; |
216 | int len; | |
b02e31e5 | 217 | struct ustcomm_source src; |
98963de4 | 218 | |
3a7b90de PMF |
219 | process_blocked_consumers(); |
220 | ||
221 | result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5); | |
222 | if(result < 0) { | |
d0b5f2b9 PMF |
223 | WARN("error in ustcomm_app_recv_message"); |
224 | continue; | |
225 | } | |
3a7b90de PMF |
226 | else if(result == 0) { |
227 | /* no message */ | |
228 | continue; | |
229 | } | |
98963de4 | 230 | |
d0b5f2b9 PMF |
231 | DBG("received a message! it's: %s\n", recvbuf); |
232 | len = strlen(recvbuf); | |
98963de4 | 233 | |
d0b5f2b9 PMF |
234 | if(!strcmp(recvbuf, "print_markers")) { |
235 | print_markers(); | |
236 | } | |
237 | else if(!strcmp(recvbuf, "trace_setup")) { | |
238 | DBG("trace setup"); | |
fbd8191b | 239 | |
d0b5f2b9 PMF |
240 | result = ltt_trace_setup(trace_name); |
241 | if(result < 0) { | |
242 | ERR("ltt_trace_setup failed"); | |
243 | return; | |
fbd8191b | 244 | } |
d0b5f2b9 PMF |
245 | |
246 | result = ltt_trace_set_type(trace_name, trace_type); | |
247 | if(result < 0) { | |
248 | ERR("ltt_trace_set_type failed"); | |
249 | return; | |
fbd8191b | 250 | } |
d0b5f2b9 PMF |
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; | |
fbd8191b | 259 | } |
d0b5f2b9 PMF |
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; | |
fbd8191b | 268 | } |
d0b5f2b9 PMF |
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; | |
aafb1650 | 277 | } |
d0b5f2b9 PMF |
278 | } |
279 | else if(!strcmp(recvbuf, "trace_destroy")) { | |
aafb1650 | 280 | |
d0b5f2b9 | 281 | DBG("trace destroy"); |
aafb1650 | 282 | |
d0b5f2b9 PMF |
283 | result = ltt_trace_destroy(trace_name); |
284 | if(result < 0) { | |
285 | ERR("ltt_trace_destroy failed"); | |
286 | return; | |
fbd8191b | 287 | } |
98963de4 | 288 | } |
b02e31e5 | 289 | else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) { |
3847c3ba PMF |
290 | struct ltt_trace_struct *trace; |
291 | char trace_name[] = "auto"; | |
292 | int i; | |
811e4b93 | 293 | char *channel_name; |
3847c3ba PMF |
294 | |
295 | DBG("get_shmid"); | |
296 | ||
811e4b93 PMF |
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 | ||
3847c3ba PMF |
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 | ||
811e4b93 PMF |
316 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
317 | char *reply; | |
318 | ||
319 | DBG("the shmid for the requested channel is %d", rbuf->shmid); | |
320 | asprintf(&reply, "%d", rbuf->shmid); | |
321 | ||
322 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
323 | if(result) { | |
324 | ERR("listener: get_shmid: ustcomm_send_reply failed"); | |
325 | goto next_cmd; | |
326 | } | |
327 | ||
328 | free(reply); | |
329 | ||
330 | break; | |
331 | } | |
332 | } | |
333 | } | |
334 | else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) { | |
335 | struct ltt_trace_struct *trace; | |
336 | char trace_name[] = "auto"; | |
337 | int i; | |
338 | char *channel_name; | |
339 | ||
340 | DBG("get_n_subbufs"); | |
341 | ||
342 | channel_name = nth_token(recvbuf, 1); | |
343 | if(channel_name == NULL) { | |
344 | ERR("get_n_subbufs: cannot parse channel"); | |
345 | goto next_cmd; | |
346 | } | |
347 | ||
348 | ltt_lock_traces(); | |
349 | trace = _ltt_trace_find(trace_name); | |
350 | ltt_unlock_traces(); | |
351 | ||
352 | if(trace == NULL) { | |
353 | CPRINTF("cannot find trace!"); | |
354 | return 1; | |
355 | } | |
356 | ||
357 | for(i=0; i<trace->nr_channels; i++) { | |
358 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
359 | ||
360 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
361 | char *reply; | |
362 | ||
363 | DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs); | |
364 | asprintf(&reply, "%d", rchan->n_subbufs); | |
365 | ||
366 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
367 | if(result) { | |
368 | ERR("listener: get_n_subbufs: ustcomm_send_reply failed"); | |
369 | goto next_cmd; | |
370 | } | |
371 | ||
372 | free(reply); | |
373 | ||
374 | break; | |
375 | } | |
376 | } | |
377 | } | |
378 | else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) { | |
379 | struct ltt_trace_struct *trace; | |
380 | char trace_name[] = "auto"; | |
381 | int i; | |
382 | char *channel_name; | |
383 | ||
384 | DBG("get_subbuf_size"); | |
385 | ||
386 | channel_name = nth_token(recvbuf, 1); | |
387 | if(channel_name == NULL) { | |
388 | ERR("get_subbuf_size: cannot parse channel"); | |
389 | goto next_cmd; | |
390 | } | |
391 | ||
392 | ltt_lock_traces(); | |
393 | trace = _ltt_trace_find(trace_name); | |
394 | ltt_unlock_traces(); | |
395 | ||
396 | if(trace == NULL) { | |
397 | CPRINTF("cannot find trace!"); | |
398 | return 1; | |
399 | } | |
400 | ||
401 | for(i=0; i<trace->nr_channels; i++) { | |
402 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
403 | ||
404 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
405 | char *reply; | |
406 | ||
407 | DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size); | |
408 | asprintf(&reply, "%d", rchan->subbuf_size); | |
409 | ||
410 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
411 | if(result) { | |
412 | ERR("listener: get_subbuf_size: ustcomm_send_reply failed"); | |
413 | goto next_cmd; | |
414 | } | |
415 | ||
416 | free(reply); | |
3847c3ba | 417 | |
811e4b93 PMF |
418 | break; |
419 | } | |
3847c3ba PMF |
420 | } |
421 | } | |
b02e31e5 PMF |
422 | else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { |
423 | char *libfile; | |
424 | ||
425 | libfile = nth_token(recvbuf, 1); | |
426 | ||
427 | DBG("load_probe_lib loading %s", libfile); | |
428 | } | |
688760ef PMF |
429 | else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) { |
430 | struct ltt_trace_struct *trace; | |
431 | char trace_name[] = "auto"; | |
432 | int i; | |
433 | char *channel_name; | |
434 | ||
435 | DBG("get_subbuf"); | |
436 | ||
437 | channel_name = nth_token(recvbuf, 1); | |
438 | if(channel_name == NULL) { | |
439 | ERR("get_subbuf: cannot parse channel"); | |
440 | goto next_cmd; | |
441 | } | |
442 | ||
443 | ltt_lock_traces(); | |
444 | trace = _ltt_trace_find(trace_name); | |
445 | ltt_unlock_traces(); | |
446 | ||
447 | if(trace == NULL) { | |
448 | CPRINTF("cannot find trace!"); | |
449 | return 1; | |
450 | } | |
451 | ||
452 | for(i=0; i<trace->nr_channels; i++) { | |
453 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
454 | ||
455 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
456 | struct rchan_buf *rbuf = rchan->buf; | |
457 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
458 | char *reply; | |
459 | long consumed_old=0; | |
3a7b90de PMF |
460 | int fd; |
461 | struct blocked_consumer *bc; | |
688760ef | 462 | |
3a7b90de PMF |
463 | bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer)); |
464 | if(bc == NULL) { | |
465 | ERR("malloc returned NULL"); | |
688760ef PMF |
466 | goto next_cmd; |
467 | } | |
3a7b90de PMF |
468 | bc->fd_consumer = src.fd; |
469 | bc->fd_producer = lttbuf->data_ready_fd_read; | |
470 | bc->rbuf = rbuf; | |
471 | bc->lttbuf = lttbuf; | |
472 | bc->src = src; | |
473 | bc->server = ustcomm_app.server; | |
688760ef | 474 | |
3a7b90de | 475 | list_add(&bc->list, &blocked_consumers); |
688760ef PMF |
476 | |
477 | break; | |
478 | } | |
479 | } | |
480 | } | |
481 | else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) { | |
482 | struct ltt_trace_struct *trace; | |
483 | char trace_name[] = "auto"; | |
484 | int i; | |
485 | char *channel_name; | |
486 | long consumed_old; | |
487 | char *consumed_old_str; | |
488 | char *endptr; | |
489 | ||
490 | DBG("put_subbuf"); | |
491 | ||
492 | channel_name = strdup_malloc(nth_token(recvbuf, 1)); | |
493 | if(channel_name == NULL) { | |
494 | ERR("put_subbuf_size: cannot parse channel"); | |
495 | goto next_cmd; | |
496 | } | |
497 | ||
498 | consumed_old_str = strdup_malloc(nth_token(recvbuf, 2)); | |
499 | if(consumed_old_str == NULL) { | |
500 | ERR("put_subbuf: cannot parse consumed_old"); | |
501 | goto next_cmd; | |
502 | } | |
503 | consumed_old = strtol(consumed_old_str, &endptr, 10); | |
504 | if(*endptr != '\0') { | |
505 | ERR("put_subbuf: invalid value for consumed_old"); | |
506 | goto next_cmd; | |
507 | } | |
508 | ||
509 | ltt_lock_traces(); | |
510 | trace = _ltt_trace_find(trace_name); | |
511 | ltt_unlock_traces(); | |
512 | ||
513 | if(trace == NULL) { | |
514 | CPRINTF("cannot find trace!"); | |
515 | return 1; | |
516 | } | |
517 | ||
518 | for(i=0; i<trace->nr_channels; i++) { | |
519 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
520 | ||
521 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
522 | struct rchan_buf *rbuf = rchan->buf; | |
523 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
524 | char *reply; | |
525 | long consumed_old=0; | |
526 | ||
527 | result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old); | |
528 | if(result < 0) { | |
529 | WARN("ltt_do_put_subbuf: error"); | |
530 | } | |
531 | else { | |
532 | DBG("ltt_do_put_subbuf: success"); | |
533 | } | |
534 | asprintf(&reply, "%s", "OK", consumed_old); | |
535 | ||
536 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
537 | if(result) { | |
538 | ERR("listener: put_subbuf: ustcomm_send_reply failed"); | |
539 | goto next_cmd; | |
540 | } | |
541 | ||
542 | free(reply); | |
543 | ||
544 | break; | |
545 | } | |
546 | } | |
547 | ||
548 | free(channel_name); | |
549 | free(consumed_old_str); | |
550 | } | |
3a7b90de PMF |
551 | // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) { |
552 | // struct ltt_trace_struct *trace; | |
553 | // char trace_name[] = "auto"; | |
554 | // int i; | |
555 | // char *channel_name; | |
556 | // | |
557 | // DBG("get_notifications"); | |
558 | // | |
559 | // channel_name = strdup_malloc(nth_token(recvbuf, 1)); | |
560 | // if(channel_name == NULL) { | |
561 | // ERR("put_subbuf_size: cannot parse channel"); | |
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) { | |
570 | // CPRINTF("cannot find trace!"); | |
571 | // return 1; | |
572 | // } | |
573 | // | |
574 | // for(i=0; i<trace->nr_channels; i++) { | |
575 | // struct rchan *rchan = trace->channels[i].trans_channel_data; | |
576 | // int fd; | |
577 | // | |
578 | // if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
579 | // struct rchan_buf *rbuf = rchan->buf; | |
580 | // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
581 | // | |
582 | // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src); | |
583 | // if(result == -1) { | |
584 | // ERR("ustcomm_app_detach_client failed"); | |
585 | // goto next_cmd; | |
586 | // } | |
587 | // | |
588 | // lttbuf->wake_consumer_arg = (void *) fd; | |
589 | // | |
590 | // smp_wmb(); | |
591 | // | |
592 | // lttbuf->call_wake_consumer = 1; | |
593 | // | |
594 | // break; | |
595 | // } | |
596 | // } | |
597 | // | |
598 | // free(channel_name); | |
599 | // } | |
688760ef PMF |
600 | else { |
601 | ERR("unable to parse message: %s", recvbuf); | |
602 | } | |
d0b5f2b9 | 603 | |
811e4b93 | 604 | next_cmd: |
d0b5f2b9 | 605 | free(recvbuf); |
98963de4 PMF |
606 | } |
607 | } | |
608 | ||
b0540e11 PMF |
609 | static char listener_stack[16384]; |
610 | ||
98963de4 PMF |
611 | void create_listener(void) |
612 | { | |
613 | int result; | |
614 | static char listener_stack[16384]; | |
b0540e11 | 615 | //char *listener_stack = malloc(16384); |
98963de4 | 616 | |
3847c3ba | 617 | #ifdef USE_CLONE |
fbd8191b | 618 | result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); |
98963de4 PMF |
619 | if(result == -1) { |
620 | perror("clone"); | |
621 | } | |
3847c3ba PMF |
622 | #else |
623 | pthread_t thread; | |
b0540e11 | 624 | |
3847c3ba PMF |
625 | pthread_create(&thread, NULL, listener_main, NULL); |
626 | #endif | |
98963de4 PMF |
627 | } |
628 | ||
d0b5f2b9 PMF |
629 | /* The signal handler itself. Signals must be setup so there cannot be |
630 | nested signals. */ | |
68c1021b PMF |
631 | |
632 | void sighandler(int sig) | |
633 | { | |
d0b5f2b9 | 634 | static char have_listener = 0; |
68c1021b | 635 | DBG("sighandler"); |
d0b5f2b9 PMF |
636 | |
637 | if(!have_listener) { | |
638 | create_listener(); | |
639 | have_listener = 1; | |
640 | } | |
68c1021b PMF |
641 | } |
642 | ||
643 | /* Called by the app signal handler to chain it to us. */ | |
644 | ||
98963de4 | 645 | void chain_signal(void) |
68c1021b PMF |
646 | { |
647 | sighandler(USTSIGNAL); | |
648 | } | |
649 | ||
98963de4 | 650 | static int init_socket(void) |
68c1021b | 651 | { |
d0b5f2b9 | 652 | return ustcomm_init_app(getpid(), &ustcomm_app); |
68c1021b PMF |
653 | } |
654 | ||
98963de4 | 655 | static void destroy_socket(void) |
68c1021b | 656 | { |
46ef48cd PMF |
657 | // int result; |
658 | // | |
659 | // if(mysocketfile[0] == '\0') | |
660 | // return; | |
661 | // | |
662 | // result = unlink(mysocketfile); | |
663 | // if(result == -1) { | |
664 | // PERROR("unlink"); | |
665 | // } | |
68c1021b PMF |
666 | } |
667 | ||
98963de4 | 668 | static int init_signal_handler(void) |
68c1021b PMF |
669 | { |
670 | /* Attempt to handler SIGIO. If the main program wants to | |
671 | * handle it, fine, it'll override us. They it'll have to | |
672 | * use the chaining function. | |
673 | */ | |
674 | ||
675 | int result; | |
676 | struct sigaction act; | |
677 | ||
678 | result = sigemptyset(&act.sa_mask); | |
679 | if(result == -1) { | |
680 | PERROR("sigemptyset"); | |
681 | return -1; | |
682 | } | |
683 | ||
684 | act.sa_handler = sighandler; | |
685 | act.sa_flags = SA_RESTART; | |
686 | ||
687 | /* Only defer ourselves. Also, try to restart interrupted | |
688 | * syscalls to disturb the traced program as little as possible. | |
689 | */ | |
690 | result = sigaction(SIGIO, &act, NULL); | |
691 | if(result == -1) { | |
692 | PERROR("sigaction"); | |
693 | return -1; | |
694 | } | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
20b37a31 | 699 | static void auto_probe_connect(struct marker *m) |
68c1021b PMF |
700 | { |
701 | int result; | |
702 | ||
20b37a31 PMF |
703 | result = ltt_marker_connect(m->channel, m->name, "default"); |
704 | if(result) | |
705 | ERR("ltt_marker_connect"); | |
706 | ||
707 | DBG("just auto connected marker %s %s to probe default", m->channel, m->name); | |
708 | } | |
709 | ||
710 | static void __attribute__((constructor(101))) init0() | |
711 | { | |
712 | DBG("UST_AUTOPROBE constructor"); | |
713 | if(getenv("UST_AUTOPROBE")) { | |
714 | marker_set_new_marker_cb(auto_probe_connect); | |
715 | } | |
716 | } | |
717 | ||
a584bc4e PMF |
718 | static void fini(void); |
719 | ||
20b37a31 PMF |
720 | static void __attribute__((constructor(1000))) init() |
721 | { | |
722 | int result; | |
723 | ||
724 | DBG("UST_TRACE constructor"); | |
725 | ||
3847c3ba PMF |
726 | /* Must create socket before signal handler to prevent races. |
727 | */ | |
728 | result = init_socket(); | |
729 | if(result == -1) { | |
730 | ERR("init_socket error"); | |
731 | return; | |
732 | } | |
733 | result = init_signal_handler(); | |
734 | if(result == -1) { | |
735 | ERR("init_signal_handler error"); | |
736 | return; | |
737 | } | |
68c1021b | 738 | |
4db647c5 PMF |
739 | if(getenv("UST_TRACE")) { |
740 | char trace_name[] = "auto"; | |
741 | char trace_type[] = "ustrelay"; | |
742 | ||
743 | DBG("starting early tracing"); | |
744 | ||
745 | /* Ensure marker control is initialized */ | |
746 | init_marker_control(); | |
747 | ||
748 | /* Ensure relay is initialized */ | |
749 | init_ustrelay_transport(); | |
750 | ||
751 | /* Ensure markers are initialized */ | |
752 | init_markers(); | |
753 | ||
20b37a31 PMF |
754 | /* In case. */ |
755 | ltt_channels_register("ust"); | |
4db647c5 PMF |
756 | |
757 | result = ltt_trace_setup(trace_name); | |
758 | if(result < 0) { | |
759 | ERR("ltt_trace_setup failed"); | |
760 | return; | |
761 | } | |
762 | ||
763 | result = ltt_trace_set_type(trace_name, trace_type); | |
764 | if(result < 0) { | |
765 | ERR("ltt_trace_set_type failed"); | |
766 | return; | |
767 | } | |
768 | ||
769 | result = ltt_trace_alloc(trace_name); | |
770 | if(result < 0) { | |
771 | ERR("ltt_trace_alloc failed"); | |
772 | return; | |
773 | } | |
774 | ||
775 | result = ltt_trace_start(trace_name); | |
776 | if(result < 0) { | |
777 | ERR("ltt_trace_start failed"); | |
778 | return; | |
779 | } | |
3847c3ba PMF |
780 | //start_consumer(); |
781 | inform_consumer_daemon(); | |
4db647c5 PMF |
782 | } |
783 | ||
68c1021b PMF |
784 | |
785 | return; | |
786 | ||
787 | /* should decrementally destroy stuff if error */ | |
788 | ||
789 | } | |
790 | ||
791 | /* This is only called if we terminate normally, not with an unhandled signal, | |
792 | * so we cannot rely on it. */ | |
793 | ||
98963de4 | 794 | static void __attribute__((destructor)) fini() |
68c1021b | 795 | { |
a584bc4e PMF |
796 | int result; |
797 | ||
798 | /* if trace running, finish it */ | |
799 | ||
800 | DBG("destructor stopping traces"); | |
801 | ||
802 | result = ltt_trace_stop("auto"); | |
803 | if(result == -1) { | |
804 | ERR("ltt_trace_stop error"); | |
805 | } | |
806 | ||
807 | result = ltt_trace_destroy("auto"); | |
808 | if(result == -1) { | |
809 | ERR("ltt_trace_destroy error"); | |
810 | } | |
811 | ||
812 | /* FIXME: wait for the consumer to be done */ | |
f20de35f PMF |
813 | //DBG("waiting 5 sec for consume"); |
814 | //sleep(5); | |
a584bc4e | 815 | |
68c1021b PMF |
816 | destroy_socket(); |
817 | } |