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