don't destroy app communication server in main() destructor
[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 19#include <stdio.h>
909bc43f 20#include <stdlib.h>
68c1021b
PMF
21#include <stdint.h>
22#include <signal.h>
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <sys/un.h>
98963de4 26#include <sched.h>
a584bc4e 27#include <fcntl.h>
3a7b90de 28#include <poll.h>
ef290fca 29#include <regex.h>
b102c2b0 30#include <urcu/uatomic_arch.h>
fbd8191b 31
93d0f2ea 32#include <ust/marker.h>
616ed36a 33#include <ust/tracectl.h>
c93858f1 34#include "tracer.h"
6af64c43 35#include "usterr.h"
d0b5f2b9 36#include "ustcomm.h"
b73a4c47 37#include "buffers.h"
9160b4e4 38#include "marker-control.h"
fbd8191b 39
b02e31e5 40//#define USE_CLONE
3847c3ba 41
68c1021b
PMF
42#define USTSIGNAL SIGIO
43
98963de4
PMF
44#define MAX_MSG_SIZE (100)
45#define MSG_NOTIF 1
46#define MSG_REGISTER_NOTIF 2
47
a584bc4e
PMF
48char consumer_stack[10000];
49
ed1317e7
PMF
50/* This should only be accessed by the constructor, before the creation
51 * of the listener, and then only by the listener.
52 */
53s64 pidunique = -1LL;
54
3a7b90de
PMF
55struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers);
56
d0b5f2b9
PMF
57static struct ustcomm_app ustcomm_app;
58
68c1021b
PMF
59struct tracecmd { /* no padding */
60 uint32_t size;
61 uint16_t command;
62};
63
f293009f
PMF
64/* volatile because shared between the listener and the main thread */
65volatile sig_atomic_t buffers_to_export = 0;
66
98963de4
PMF
67struct trctl_msg {
68 /* size: the size of all the fields except size itself */
69 uint32_t size;
70 uint16_t type;
71 /* Only the necessary part of the payload is transferred. It
72 * may even be none of it.
73 */
74 char payload[94];
75};
68c1021b 76
a584bc4e
PMF
77struct consumer_channel {
78 int fd;
79 struct ltt_channel_struct *chan;
80};
81
3a7b90de
PMF
82struct blocked_consumer {
83 int fd_consumer;
84 int fd_producer;
85 int tmp_poll_idx;
86
87 /* args to ustcomm_send_reply */
88 struct ustcomm_server server;
89 struct ustcomm_source src;
90
b73a4c47 91 /* args to ust_buffers_get_subbuf */
b5b073e2 92 struct ust_buffer *buf;
3a7b90de
PMF
93
94 struct list_head list;
95};
96
ed1317e7
PMF
97static long long make_pidunique(void)
98{
99 s64 retval;
100 struct timeval tv;
101
102 gettimeofday(&tv, NULL);
103
104 retval = tv.tv_sec;
105 retval <<= 32;
106 retval |= tv.tv_usec;
107
108 return retval;
109}
110
52c51a47 111static void print_markers(FILE *fp)
fbd8191b
PMF
112{
113 struct marker_iter iter;
114
d0b5f2b9 115 lock_markers();
fbd8191b
PMF
116 marker_iter_reset(&iter);
117 marker_iter_start(&iter);
118
119 while(iter.marker) {
3ea1e2fc 120 fprintf(fp, "marker: %s/%s %d \"%s\" %p\n", iter.marker->channel, iter.marker->name, (int)imv_read(iter.marker->state), iter.marker->format, iter.marker->location);
fbd8191b
PMF
121 marker_iter_next(&iter);
122 }
d0b5f2b9 123 unlock_markers();
fbd8191b
PMF
124}
125
4440ebcb 126static int init_socket(void);
68c1021b 127
98963de4
PMF
128int fd_notif = -1;
129void notif_cb(void)
130{
131 int result;
132 struct trctl_msg msg;
133
134 /* FIXME: fd_notif should probably be protected by a spinlock */
135
136 if(fd_notif == -1)
137 return;
138
139 msg.type = MSG_NOTIF;
140 msg.size = sizeof(msg.type);
141
142 /* FIXME: don't block here */
143 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
144 if(result == -1) {
145 PERROR("write");
146 return;
147 }
148}
149
ad45e833
PMF
150/* Ask the daemon to collect a trace called trace_name and being
151 * produced by this pid.
152 *
153 * The trace must be at least allocated. (It can also be started.)
154 * This is because _ltt_trace_find is used.
155 */
156
157static void inform_consumer_daemon(const char *trace_name)
d0b5f2b9 158{
204141ee 159 int i,j;
b73a4c47 160 struct ust_trace *trace;
ad45e833
PMF
161 pid_t pid = getpid();
162 int result;
163
164 ltt_lock_traces();
165
166 trace = _ltt_trace_find(trace_name);
167 if(trace == NULL) {
168 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
169 goto finish;
170 }
171
172 for(i=0; i < trace->nr_channels; i++) {
204141ee
PMF
173 /* iterate on all cpus */
174 for(j=0; j<trace->channels[i].n_cpus; j++) {
175 char *buf;
176 asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j);
177 result = ustcomm_request_consumer(pid, buf);
178 if(result == -1) {
179 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
180 /* continue even if fail */
181 }
182 free(buf);
183 buffers_to_export++;
ad45e833
PMF
184 }
185 }
186
187 finish:
188 ltt_unlock_traces();
d0b5f2b9 189}
fbd8191b 190
3a7b90de
PMF
191void process_blocked_consumers(void)
192{
193 int n_fds = 0;
194 struct pollfd *fds;
195 struct blocked_consumer *bc;
196 int idx = 0;
197 char inbuf;
198 int result;
199
200 list_for_each_entry(bc, &blocked_consumers, list) {
201 n_fds++;
202 }
203
204 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
205 if(fds == NULL) {
206 ERR("malloc returned NULL");
207 return;
208 }
209
210 list_for_each_entry(bc, &blocked_consumers, list) {
211 fds[idx].fd = bc->fd_producer;
212 fds[idx].events = POLLIN;
213 bc->tmp_poll_idx = idx;
214 idx++;
215 }
216
69ba0156
PMF
217 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
218 /* nothing */;
3a7b90de
PMF
219 if(result == -1) {
220 PERROR("poll");
872037bb 221 return;
3a7b90de
PMF
222 }
223
224 list_for_each_entry(bc, &blocked_consumers, list) {
225 if(fds[bc->tmp_poll_idx].revents) {
226 long consumed_old = 0;
227 char *reply;
228
229 result = read(bc->fd_producer, &inbuf, 1);
230 if(result == -1) {
231 PERROR("read");
232 continue;
233 }
234 if(result == 0) {
78cc99db 235 int res;
3a7b90de
PMF
236 DBG("PRODUCER END");
237
78cc99db
PMF
238 res = close(bc->fd_producer);
239 if(res == -1) {
240 PERROR("close");
241 }
3a7b90de 242
769d0157 243 list_del(&bc->list);
3a7b90de
PMF
244
245 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
246 if(result < 0) {
247 ERR("ustcomm_send_reply failed");
248 continue;
249 }
250
251 continue;
252 }
253
b73a4c47 254 result = ust_buffers_get_subbuf(bc->buf, &consumed_old);
3a7b90de
PMF
255 if(result == -EAGAIN) {
256 WARN("missed buffer?");
257 continue;
258 }
259 else if(result < 0) {
b73a4c47 260 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result));
3a7b90de
PMF
261 }
262 asprintf(&reply, "%s %ld", "OK", consumed_old);
263 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
264 if(result < 0) {
265 ERR("ustcomm_send_reply failed");
266 free(reply);
267 continue;
268 }
269 free(reply);
270
769d0157 271 list_del(&bc->list);
3a7b90de
PMF
272 }
273 }
274
275}
276
204141ee
PMF
277void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu)
278{
279 const char *sep;
280
281 sep = rindex(channel_and_cpu, '_');
282 if(sep == NULL) {
283 *cpu = -1;
284 sep = channel_and_cpu + strlen(channel_and_cpu);
285 }
286 else {
287 *cpu = atoi(sep+1);
288 }
289
290 asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu);
291}
292
293static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src)
294{
295 int retval = 0;
b73a4c47 296 struct ust_trace *trace;
204141ee
PMF
297 char trace_name[] = "auto";
298 int i;
299 char *channel_and_cpu;
300 int found = 0;
301 int result;
302 char *ch_name;
303 int ch_cpu;
304
305 DBG("get_shmid");
306
307 channel_and_cpu = nth_token(recvbuf, 1);
308 if(channel_and_cpu == NULL) {
27e84572 309 ERR("cannot parse channel");
204141ee
PMF
310 goto end;
311 }
312
313 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
314 if(ch_cpu == -1) {
27e84572 315 ERR("problem parsing channel name");
204141ee
PMF
316 goto free_short_chan_name;
317 }
318
319 ltt_lock_traces();
320 trace = _ltt_trace_find(trace_name);
321 ltt_unlock_traces();
322
323 if(trace == NULL) {
324 ERR("cannot find trace!");
325 retval = -1;
326 goto free_short_chan_name;
327 }
328
329 for(i=0; i<trace->nr_channels; i++) {
330 struct ust_channel *channel = &trace->channels[i];
331 struct ust_buffer *buf = channel->buf[ch_cpu];
332
333 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
334 char *reply;
335
336// DBG("the shmid for the requested channel is %d", buf->shmid);
337// DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
338 asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]);
339
340 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
341 if(result) {
27e84572 342 ERR("ustcomm_send_reply failed");
204141ee
PMF
343 free(reply);
344 retval = -1;
345 goto free_short_chan_name;
346 }
347
348 free(reply);
349
350 found = 1;
351 break;
352 }
353 }
354
d5adede0 355 if(!found) {
27e84572 356 ERR("channel not found (%s)", channel_and_cpu);
204141ee
PMF
357 }
358
359 free_short_chan_name:
360 free(ch_name);
361
362 end:
363 return retval;
364}
365
366static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
367{
368 int retval = 0;
b73a4c47 369 struct ust_trace *trace;
204141ee
PMF
370 char trace_name[] = "auto";
371 int i;
372 char *channel_and_cpu;
373 int found = 0;
374 int result;
375 char *ch_name;
376 int ch_cpu;
377
378 DBG("get_n_subbufs");
379
380 channel_and_cpu = nth_token(recvbuf, 1);
381 if(channel_and_cpu == NULL) {
27e84572 382 ERR("cannot parse channel");
204141ee
PMF
383 goto end;
384 }
385
386 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
387 if(ch_cpu == -1) {
27e84572 388 ERR("problem parsing channel name");
204141ee
PMF
389 goto free_short_chan_name;
390 }
391
392 ltt_lock_traces();
393 trace = _ltt_trace_find(trace_name);
394 ltt_unlock_traces();
395
396 if(trace == NULL) {
397 ERR("cannot find trace!");
398 retval = -1;
399 goto free_short_chan_name;
400 }
401
402 for(i=0; i<trace->nr_channels; i++) {
403 struct ust_channel *channel = &trace->channels[i];
404
405 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
406 char *reply;
407
408 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
409 asprintf(&reply, "%d", channel->subbuf_cnt);
410
411 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
412 if(result) {
27e84572 413 ERR("ustcomm_send_reply failed");
204141ee
PMF
414 free(reply);
415 retval = -1;
416 goto free_short_chan_name;
417 }
418
419 free(reply);
420 found = 1;
421 break;
422 }
423 }
424 if(found == 0) {
27e84572 425 ERR("unable to find channel");
204141ee
PMF
426 }
427
428 free_short_chan_name:
429 free(ch_name);
430
431 end:
432 return retval;
433}
434
435static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
436{
437 int retval = 0;
b73a4c47 438 struct ust_trace *trace;
204141ee
PMF
439 char trace_name[] = "auto";
440 int i;
441 char *channel_and_cpu;
442 int found = 0;
443 int result;
444 char *ch_name;
445 int ch_cpu;
446
447 DBG("get_subbuf_size");
448
449 channel_and_cpu = nth_token(recvbuf, 1);
450 if(channel_and_cpu == NULL) {
27e84572 451 ERR("cannot parse channel");
204141ee
PMF
452 goto end;
453 }
454
455 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
456 if(ch_cpu == -1) {
27e84572 457 ERR("problem parsing channel name");
204141ee
PMF
458 goto free_short_chan_name;
459 }
460
461 ltt_lock_traces();
462 trace = _ltt_trace_find(trace_name);
463 ltt_unlock_traces();
464
465 if(trace == NULL) {
466 ERR("cannot find trace!");
467 retval = -1;
468 goto free_short_chan_name;
469 }
470
471 for(i=0; i<trace->nr_channels; i++) {
472 struct ust_channel *channel = &trace->channels[i];
473
474 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
475 char *reply;
476
477 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
478 asprintf(&reply, "%zd", channel->subbuf_size);
479
480 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
481 if(result) {
27e84572 482 ERR("ustcomm_send_reply failed");
204141ee
PMF
483 free(reply);
484 retval = -1;
485 goto free_short_chan_name;
486 }
487
488 free(reply);
489 found = 1;
490 break;
491 }
492 }
493 if(found == 0) {
27e84572 494 ERR("unable to find channel");
204141ee
PMF
495 }
496
497 free_short_chan_name:
498 free(ch_name);
499
500 end:
501 return retval;
502}
503
e43efba9 504static unsigned int poweroftwo(unsigned int x)
763f41e5
DS
505{
506 unsigned int power2 = 1;
507 unsigned int hardcoded = 2147483648; /* FIX max 2^31 */
508
509 if (x < 2)
510 return 2;
511
512 while (power2 < x && power2 < hardcoded)
513 power2 *= 2;
514
515 return power2;
516}
517
518static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
519{
520 char *channel_slash_size;
521 char ch_name[256]="";
522 unsigned int size, power;
523 int retval = 0;
524 struct ust_trace *trace;
525 char trace_name[] = "auto";
526 int i;
527 int found = 0;
528
529 DBG("set_subbuf_size");
530
531 channel_slash_size = nth_token(recvbuf, 1);
532 sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size);
533
534 if(ch_name == NULL) {
535 ERR("cannot parse channel");
536 goto end;
537 }
538
539 power = poweroftwo(size);
540 if (power != size)
541 WARN("using the next 2^n = %u\n", power);
542
543 ltt_lock_traces();
544 trace = _ltt_trace_find_setup(trace_name);
545 if(trace == NULL) {
546 ERR("cannot find trace!");
763f41e5
DS
547 retval = -1;
548 goto end;
549 }
550
551 for(i = 0; i < trace->nr_channels; i++) {
552 struct ust_channel *channel = &trace->channels[i];
553
554 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
555
556 channel->subbuf_size = power;
557 DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size);
558
559 found = 1;
560 break;
561 }
562 }
563 if(found == 0) {
564 ERR("unable to find channel");
565 }
566
763f41e5 567 end:
86dd0ebc 568 ltt_unlock_traces();
763f41e5
DS
569 return retval;
570}
571
572static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src)
573{
574 char *channel_slash_num;
575 char ch_name[256]="";
576 unsigned int num;
577 int retval = 0;
578 struct ust_trace *trace;
579 char trace_name[] = "auto";
580 int i;
581 int found = 0;
582
583 DBG("set_subbuf_num");
584
585 channel_slash_num = nth_token(recvbuf, 1);
586 sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num);
587
588 if(ch_name == NULL) {
589 ERR("cannot parse channel");
590 goto end;
591 }
592 if (num < 2) {
593 ERR("subbuffer count should be greater than 2");
594 goto end;
595 }
596
597 ltt_lock_traces();
598 trace = _ltt_trace_find_setup(trace_name);
599 if(trace == NULL) {
600 ERR("cannot find trace!");
763f41e5
DS
601 retval = -1;
602 goto end;
603 }
604
605 for(i = 0; i < trace->nr_channels; i++) {
606 struct ust_channel *channel = &trace->channels[i];
607
608 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
609
610 channel->subbuf_cnt = num;
611 DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt);
612
613 found = 1;
614 break;
615 }
616 }
617 if(found == 0) {
618 ERR("unable to find channel");
619 }
620
763f41e5 621 end:
86dd0ebc 622 ltt_unlock_traces();
763f41e5
DS
623 return retval;
624}
625
204141ee
PMF
626static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
627{
628 int retval = 0;
b73a4c47 629 struct ust_trace *trace;
204141ee
PMF
630 char trace_name[] = "auto";
631 int i;
632 char *channel_and_cpu;
633 int found = 0;
634 char *ch_name;
635 int ch_cpu;
636
637 DBG("get_subbuf");
638
639 channel_and_cpu = nth_token(recvbuf, 1);
640 if(channel_and_cpu == NULL) {
27e84572 641 ERR("cannot parse channel");
204141ee
PMF
642 goto end;
643 }
644
645 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
646 if(ch_cpu == -1) {
27e84572 647 ERR("problem parsing channel name");
204141ee
PMF
648 goto free_short_chan_name;
649 }
650
651 ltt_lock_traces();
652 trace = _ltt_trace_find(trace_name);
204141ee
PMF
653
654 if(trace == NULL) {
2ddb81a8
PMF
655 int result;
656
753e1b51 657 DBG("Cannot find trace. It was likely destroyed by the user.");
86dd0ebc 658 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
2ddb81a8
PMF
659 if(result) {
660 ERR("ustcomm_send_reply failed");
2ddb81a8 661 retval = -1;
86dd0ebc 662 goto unlock_traces;
2ddb81a8
PMF
663 }
664
86dd0ebc 665 goto unlock_traces;
204141ee
PMF
666 }
667
668 for(i=0; i<trace->nr_channels; i++) {
669 struct ust_channel *channel = &trace->channels[i];
670
671 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
672 struct ust_buffer *buf = channel->buf[ch_cpu];
673 struct blocked_consumer *bc;
674
675 found = 1;
676
677 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
678 if(bc == NULL) {
679 ERR("malloc returned NULL");
86dd0ebc 680 goto unlock_traces;
204141ee
PMF
681 }
682 bc->fd_consumer = src->fd;
683 bc->fd_producer = buf->data_ready_fd_read;
684 bc->buf = buf;
685 bc->src = *src;
686 bc->server = ustcomm_app.server;
687
688 list_add(&bc->list, &blocked_consumers);
689
d5adede0
PMF
690 /* Being here is the proof the daemon has mapped the buffer in its
691 * memory. We may now decrement buffers_to_export.
692 */
b102c2b0 693 if(uatomic_read(&buf->consumed) == 0) {
d5adede0
PMF
694 DBG("decrementing buffers_to_export");
695 buffers_to_export--;
696 }
697
204141ee
PMF
698 break;
699 }
700 }
701 if(found == 0) {
27e84572 702 ERR("unable to find channel");
204141ee
PMF
703 }
704
86dd0ebc
PMF
705 unlock_traces:
706 ltt_unlock_traces();
707
204141ee
PMF
708 free_short_chan_name:
709 free(ch_name);
710
711 end:
712 return retval;
713}
714
715static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src)
716{
717 int retval = 0;
b73a4c47 718 struct ust_trace *trace;
204141ee
PMF
719 char trace_name[] = "auto";
720 int i;
721 char *channel_and_cpu;
722 int found = 0;
723 int result;
724 char *ch_name;
725 int ch_cpu;
726 long consumed_old;
727 char *consumed_old_str;
728 char *endptr;
2ddb81a8 729 char *reply = NULL;
204141ee
PMF
730
731 DBG("put_subbuf");
732
733 channel_and_cpu = strdup_malloc(nth_token(recvbuf, 1));
734 if(channel_and_cpu == NULL) {
27e84572 735 ERR("cannot parse channel");
204141ee
PMF
736 retval = -1;
737 goto end;
738 }
739
740 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
741 if(consumed_old_str == NULL) {
27e84572 742 ERR("cannot parse consumed_old");
204141ee
PMF
743 retval = -1;
744 goto free_channel_and_cpu;
745 }
746 consumed_old = strtol(consumed_old_str, &endptr, 10);
747 if(*endptr != '\0') {
27e84572 748 ERR("invalid value for consumed_old");
204141ee
PMF
749 retval = -1;
750 goto free_consumed_old_str;
751 }
752
753 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
754 if(ch_cpu == -1) {
27e84572 755 ERR("problem parsing channel name");
204141ee
PMF
756 retval = -1;
757 goto free_short_chan_name;
758 }
759
760 ltt_lock_traces();
761 trace = _ltt_trace_find(trace_name);
204141ee
PMF
762
763 if(trace == NULL) {
753e1b51 764 DBG("Cannot find trace. It was likely destroyed by the user.");
86dd0ebc 765 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
2ddb81a8
PMF
766 if(result) {
767 ERR("ustcomm_send_reply failed");
2ddb81a8 768 retval = -1;
86dd0ebc 769 goto unlock_traces;
2ddb81a8
PMF
770 }
771
86dd0ebc 772 goto unlock_traces;
204141ee
PMF
773 }
774
775 for(i=0; i<trace->nr_channels; i++) {
776 struct ust_channel *channel = &trace->channels[i];
777
778 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
779 struct ust_buffer *buf = channel->buf[ch_cpu];
204141ee
PMF
780
781 found = 1;
782
b73a4c47 783 result = ust_buffers_put_subbuf(buf, consumed_old);
204141ee 784 if(result < 0) {
b73a4c47 785 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu);
204141ee
PMF
786 asprintf(&reply, "%s", "ERROR");
787 }
788 else {
b73a4c47 789 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu);
204141ee
PMF
790 asprintf(&reply, "%s", "OK");
791 }
792
793 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
794 if(result) {
27e84572 795 ERR("ustcomm_send_reply failed");
204141ee
PMF
796 free(reply);
797 retval = -1;
86dd0ebc 798 goto unlock_traces;
204141ee
PMF
799 }
800
801 free(reply);
802 break;
803 }
804 }
805 if(found == 0) {
2ddb81a8 806 ERR("unable to find channel");
204141ee
PMF
807 }
808
86dd0ebc
PMF
809 unlock_traces:
810 ltt_unlock_traces();
204141ee
PMF
811 free_short_chan_name:
812 free(ch_name);
2ddb81a8
PMF
813 free_consumed_old_str:
814 free(consumed_old_str);
815 free_channel_and_cpu:
816 free(channel_and_cpu);
204141ee
PMF
817
818 end:
819 return retval;
820}
821
fc253ce0
PMF
822static void listener_cleanup(void *ptr)
823{
824 ustcomm_fini_app(&ustcomm_app, 0);
825}
826
872037bb 827void *listener_main(void *p)
98963de4
PMF
828{
829 int result;
830
b0540e11
PMF
831 DBG("LISTENER");
832
fc253ce0
PMF
833 pthread_cleanup_push(listener_cleanup, NULL);
834
98963de4 835 for(;;) {
aafb1650
PMF
836 char trace_name[] = "auto";
837 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
838 char *recvbuf;
839 int len;
b02e31e5 840 struct ustcomm_source src;
98963de4 841
3a7b90de
PMF
842 process_blocked_consumers();
843
844 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
845 if(result < 0) {
d0b5f2b9
PMF
846 WARN("error in ustcomm_app_recv_message");
847 continue;
848 }
3a7b90de
PMF
849 else if(result == 0) {
850 /* no message */
851 continue;
852 }
98963de4 853
08230db7 854 DBG("received a message! it's: %s", recvbuf);
d0b5f2b9 855 len = strlen(recvbuf);
98963de4 856
d0b5f2b9 857 if(!strcmp(recvbuf, "print_markers")) {
52c51a47
PMF
858 print_markers(stderr);
859 }
860 else if(!strcmp(recvbuf, "list_markers")) {
861 char *ptr;
862 size_t size;
863 FILE *fp;
864
865 fp = open_memstream(&ptr, &size);
866 print_markers(fp);
867 fclose(fp);
868
869 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
870
871 free(ptr);
872 }
873 else if(!strcmp(recvbuf, "start")) {
874 /* start is an operation that setups the trace, allocates it and starts it */
875 result = ltt_trace_setup(trace_name);
876 if(result < 0) {
877 ERR("ltt_trace_setup failed");
872037bb 878 return (void *)1;
52c51a47
PMF
879 }
880
881 result = ltt_trace_set_type(trace_name, trace_type);
882 if(result < 0) {
883 ERR("ltt_trace_set_type failed");
872037bb 884 return (void *)1;
52c51a47
PMF
885 }
886
887 result = ltt_trace_alloc(trace_name);
888 if(result < 0) {
889 ERR("ltt_trace_alloc failed");
872037bb 890 return (void *)1;
52c51a47
PMF
891 }
892
ad45e833 893 inform_consumer_daemon(trace_name);
52c51a47
PMF
894
895 result = ltt_trace_start(trace_name);
896 if(result < 0) {
897 ERR("ltt_trace_start failed");
898 continue;
899 }
d0b5f2b9
PMF
900 }
901 else if(!strcmp(recvbuf, "trace_setup")) {
902 DBG("trace setup");
fbd8191b 903
d0b5f2b9
PMF
904 result = ltt_trace_setup(trace_name);
905 if(result < 0) {
906 ERR("ltt_trace_setup failed");
872037bb 907 return (void *)1;
fbd8191b 908 }
d0b5f2b9
PMF
909
910 result = ltt_trace_set_type(trace_name, trace_type);
911 if(result < 0) {
912 ERR("ltt_trace_set_type failed");
872037bb 913 return (void *)1;
fbd8191b 914 }
d0b5f2b9
PMF
915 }
916 else if(!strcmp(recvbuf, "trace_alloc")) {
917 DBG("trace alloc");
918
919 result = ltt_trace_alloc(trace_name);
920 if(result < 0) {
921 ERR("ltt_trace_alloc failed");
872037bb 922 return (void *)1;
fbd8191b 923 }
763f41e5 924 inform_consumer_daemon(trace_name);
d0b5f2b9 925 }
62ec620f
PMF
926 else if(!strcmp(recvbuf, "trace_create")) {
927 DBG("trace create");
928
929 result = ltt_trace_setup(trace_name);
930 if(result < 0) {
931 ERR("ltt_trace_setup failed");
932 return (void *)1;
933 }
934
935 result = ltt_trace_set_type(trace_name, trace_type);
936 if(result < 0) {
937 ERR("ltt_trace_set_type failed");
938 return (void *)1;
939 }
763f41e5
DS
940 }
941 else if(!strcmp(recvbuf, "trace_start")) {
942 DBG("trace start");
62ec620f
PMF
943
944 result = ltt_trace_alloc(trace_name);
945 if(result < 0) {
946 ERR("ltt_trace_alloc failed");
947 return (void *)1;
948 }
763f41e5
DS
949 if(!result) {
950 inform_consumer_daemon(trace_name);
951 }
d0b5f2b9
PMF
952
953 result = ltt_trace_start(trace_name);
954 if(result < 0) {
955 ERR("ltt_trace_start failed");
956 continue;
fbd8191b 957 }
d0b5f2b9
PMF
958 }
959 else if(!strcmp(recvbuf, "trace_stop")) {
960 DBG("trace stop");
961
962 result = ltt_trace_stop(trace_name);
963 if(result < 0) {
964 ERR("ltt_trace_stop failed");
872037bb 965 return (void *)1;
aafb1650 966 }
d0b5f2b9
PMF
967 }
968 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 969
d0b5f2b9 970 DBG("trace destroy");
aafb1650 971
31d392f1 972 result = ltt_trace_destroy(trace_name, 0);
d0b5f2b9
PMF
973 if(result < 0) {
974 ERR("ltt_trace_destroy failed");
872037bb 975 return (void *)1;
fbd8191b 976 }
98963de4 977 }
b02e31e5 978 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
204141ee 979 do_cmd_get_shmid(recvbuf, &src);
811e4b93
PMF
980 }
981 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
204141ee 982 do_cmd_get_n_subbufs(recvbuf, &src);
811e4b93
PMF
983 }
984 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
204141ee 985 do_cmd_get_subbuf_size(recvbuf, &src);
3847c3ba 986 }
b02e31e5
PMF
987 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
988 char *libfile;
989
990 libfile = nth_token(recvbuf, 1);
991
992 DBG("load_probe_lib loading %s", libfile);
204141ee
PMF
993
994 free(libfile);
b02e31e5 995 }
688760ef 996 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
204141ee 997 do_cmd_get_subbuffer(recvbuf, &src);
688760ef
PMF
998 }
999 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
204141ee 1000 do_cmd_put_subbuffer(recvbuf, &src);
688760ef 1001 }
763f41e5
DS
1002 else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) {
1003 do_cmd_set_subbuf_size(recvbuf, &src);
1004 }
1005 else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) {
1006 do_cmd_set_subbuf_num(recvbuf, &src);
1007 }
52c51a47
PMF
1008 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
1009 char *channel_slash_name = nth_token(recvbuf, 1);
1010 char channel_name[256]="";
1011 char marker_name[256]="";
52c51a47
PMF
1012
1013 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
1014
1015 if(channel_name == NULL || marker_name == NULL) {
1016 WARN("invalid marker name");
1017 goto next_cmd;
1018 }
52c51a47
PMF
1019
1020 result = ltt_marker_connect(channel_name, marker_name, "default");
1021 if(result < 0) {
1022 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
1023 }
1024 }
1025 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
1026 char *channel_slash_name = nth_token(recvbuf, 1);
1027 char *marker_name;
1028 char *channel_name;
52c51a47
PMF
1029
1030 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
1031
1032 if(marker_name == NULL) {
1033 }
52c51a47
PMF
1034
1035 result = ltt_marker_disconnect(channel_name, marker_name, "default");
1036 if(result < 0) {
1037 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
1038 }
1039 }
ed1317e7
PMF
1040 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
1041 char *reply;
1042
1043 asprintf(&reply, "%lld", pidunique);
1044
1045 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
1046 if(result) {
1047 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1048 goto next_cmd;
1049 }
1050
1051 free(reply);
1052 }
3a7b90de 1053// else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
b73a4c47 1054// struct ust_trace *trace;
3a7b90de
PMF
1055// char trace_name[] = "auto";
1056// int i;
1057// char *channel_name;
1058//
1059// DBG("get_notifications");
1060//
1061// channel_name = strdup_malloc(nth_token(recvbuf, 1));
1062// if(channel_name == NULL) {
1063// ERR("put_subbuf_size: cannot parse channel");
1064// goto next_cmd;
1065// }
1066//
1067// ltt_lock_traces();
1068// trace = _ltt_trace_find(trace_name);
1069// ltt_unlock_traces();
1070//
1071// if(trace == NULL) {
63fe14e5 1072// ERR("cannot find trace!");
872037bb 1073// return (void *)1;
3a7b90de
PMF
1074// }
1075//
1076// for(i=0; i<trace->nr_channels; i++) {
1077// struct rchan *rchan = trace->channels[i].trans_channel_data;
1078// int fd;
1079//
1080// if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1081// struct rchan_buf *rbuf = rchan->buf;
1082// struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1083//
1084// result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1085// if(result == -1) {
1086// ERR("ustcomm_app_detach_client failed");
1087// goto next_cmd;
1088// }
1089//
1090// lttbuf->wake_consumer_arg = (void *) fd;
1091//
1092// smp_wmb();
1093//
1094// lttbuf->call_wake_consumer = 1;
1095//
1096// break;
1097// }
1098// }
1099//
1100// free(channel_name);
1101// }
688760ef
PMF
1102 else {
1103 ERR("unable to parse message: %s", recvbuf);
1104 }
d0b5f2b9 1105
811e4b93 1106 next_cmd:
d0b5f2b9 1107 free(recvbuf);
98963de4 1108 }
fc253ce0
PMF
1109
1110 pthread_cleanup_pop(1);
98963de4
PMF
1111}
1112
7958de40 1113volatile sig_atomic_t have_listener = 0;
fc253ce0
PMF
1114#ifndef USE_CLONE
1115static pthread_t listener_thread;
1116#endif
4440ebcb 1117
98963de4
PMF
1118void create_listener(void)
1119{
9160b4e4 1120#ifdef USE_CLONE
98963de4 1121 static char listener_stack[16384];
c5fdc888 1122 int result;
9160b4e4 1123#endif
98963de4 1124
c5fdc888
PMF
1125 if(have_listener) {
1126 WARN("not creating listener because we already had one");
4440ebcb 1127 return;
c5fdc888 1128 }
4440ebcb 1129
3847c3ba 1130#ifdef USE_CLONE
c5fdc888 1131 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
1132 if(result == -1) {
1133 perror("clone");
4440ebcb 1134 return;
98963de4 1135 }
3847c3ba 1136#else
b0540e11 1137
fc253ce0 1138 pthread_create(&listener_thread, NULL, listener_main, NULL);
3847c3ba 1139#endif
4440ebcb
PMF
1140
1141 have_listener = 1;
98963de4
PMF
1142}
1143
98963de4 1144static int init_socket(void)
68c1021b 1145{
d0b5f2b9 1146 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
1147}
1148
5de74e51
PMF
1149#define AUTOPROBE_DISABLED 0
1150#define AUTOPROBE_ENABLE_ALL 1
1151#define AUTOPROBE_ENABLE_REGEX 2
1152static int autoprobe_method = AUTOPROBE_DISABLED;
1153static regex_t autoprobe_regex;
ef290fca 1154
20b37a31 1155static void auto_probe_connect(struct marker *m)
68c1021b
PMF
1156{
1157 int result;
1158
5de74e51
PMF
1159 char* concat_name = NULL;
1160 const char *probe_name = "default";
ef290fca 1161
5de74e51 1162 if(autoprobe_method == AUTOPROBE_DISABLED) {
ef290fca
PMF
1163 return;
1164 }
5de74e51
PMF
1165 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1166 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1167 if(result == -1) {
1168 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1169 m->channel, m->name);
1170 return;
1171 }
1172 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1173 free(concat_name);
1174 return;
1175 }
1176 free(concat_name);
ef290fca
PMF
1177 }
1178
5de74e51 1179 result = ltt_marker_connect(m->channel, m->name, probe_name);
acbf228b
PMF
1180 if(result && result != -EEXIST)
1181 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
20b37a31 1182
3ea1e2fc 1183 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
ef290fca 1184
20b37a31
PMF
1185}
1186
c1083aa8 1187static void __attribute__((constructor)) init()
20b37a31
PMF
1188{
1189 int result;
5de74e51 1190 char* autoprobe_val = NULL;
20b37a31 1191
ed1317e7
PMF
1192 /* Assign the pidunique, to be able to differentiate the processes with same
1193 * pid, (before and after an exec).
1194 */
1195 pidunique = make_pidunique();
1196
5de74e51 1197 DBG("Tracectl constructor");
20b37a31 1198
3847c3ba
PMF
1199 result = init_socket();
1200 if(result == -1) {
1201 ERR("init_socket error");
1202 return;
1203 }
2944a629
PMF
1204
1205 create_listener();
68c1021b 1206
5de74e51
PMF
1207 autoprobe_val = getenv("UST_AUTOPROBE");
1208 if(autoprobe_val) {
4440ebcb
PMF
1209 struct marker_iter iter;
1210
08230db7 1211 DBG("Autoprobe enabled.");
4440ebcb
PMF
1212
1213 /* Ensure markers are initialized */
1214 //init_markers();
1215
1216 /* Ensure marker control is initialized, for the probe */
1217 init_marker_control();
1218
1219 /* first, set the callback that will connect the
1220 * probe on new markers
1221 */
5de74e51
PMF
1222 if(autoprobe_val[0] == '/') {
1223 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1224 if (result) {
1225 char regexerr[150];
1226
1227 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1228 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1229 /* don't crash the application just for this */
1230 }
1231 else {
1232 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1233 }
ef290fca 1234 }
5de74e51
PMF
1235 else {
1236 /* just enable all instrumentation */
1237 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1238 }
1239
1240 marker_set_new_marker_cb(auto_probe_connect);
1241
4440ebcb
PMF
1242 /* Now, connect the probes that were already registered. */
1243 marker_iter_reset(&iter);
1244 marker_iter_start(&iter);
1245
08230db7 1246 DBG("now iterating on markers already registered");
4440ebcb 1247 while(iter.marker) {
08230db7 1248 DBG("now iterating on marker %s", iter.marker->name);
4440ebcb
PMF
1249 auto_probe_connect(iter.marker);
1250 marker_iter_next(&iter);
1251 }
1252 }
1253
4db647c5
PMF
1254 if(getenv("UST_TRACE")) {
1255 char trace_name[] = "auto";
1256 char trace_type[] = "ustrelay";
1257
1258 DBG("starting early tracing");
1259
1260 /* Ensure marker control is initialized */
1261 init_marker_control();
1262
4db647c5
PMF
1263 /* Ensure markers are initialized */
1264 init_markers();
1265
e17571a5
PMF
1266 /* Ensure buffers are initialized, for the transport to be available.
1267 * We are about to set a trace type and it will fail without this.
1268 */
1269 init_ustrelay_transport();
1270
027ffc9d
PMF
1271 /* FIXME: When starting early tracing (here), depending on the
1272 * order of constructors, it is very well possible some marker
1273 * sections are not yet registered. Because of this, some
1274 * channels may not be registered. Yet, we are about to ask the
1275 * daemon to collect the channels. Channels which are not yet
1276 * registered will not be collected.
1277 *
1278 * Currently, in LTTng, there is no way to add a channel after
1279 * trace start. The reason for this is that it induces complex
1280 * concurrency issues on the trace structures, which can only
1281 * be resolved using RCU. This has not been done yet. As a
1282 * workaround, we are forcing the registration of the "ust"
1283 * channel here. This is the only channel (apart from metadata)
1284 * that can be reliably used in early tracing.
1285 *
1286 * Non-early tracing does not have this problem and can use
1287 * arbitrary channel names.
1288 */
20b37a31 1289 ltt_channels_register("ust");
4db647c5
PMF
1290
1291 result = ltt_trace_setup(trace_name);
1292 if(result < 0) {
1293 ERR("ltt_trace_setup failed");
1294 return;
1295 }
1296
1297 result = ltt_trace_set_type(trace_name, trace_type);
1298 if(result < 0) {
1299 ERR("ltt_trace_set_type failed");
1300 return;
1301 }
1302
1303 result = ltt_trace_alloc(trace_name);
1304 if(result < 0) {
1305 ERR("ltt_trace_alloc failed");
1306 return;
1307 }
1308
1309 result = ltt_trace_start(trace_name);
1310 if(result < 0) {
1311 ERR("ltt_trace_start failed");
1312 return;
1313 }
60e57148
PMF
1314
1315 /* Do this after the trace is started in order to avoid creating confusion
1316 * if the trace fails to start. */
1317 inform_consumer_daemon(trace_name);
4db647c5
PMF
1318 }
1319
68c1021b
PMF
1320
1321 return;
1322
1323 /* should decrementally destroy stuff if error */
1324
1325}
1326
1327/* This is only called if we terminate normally, not with an unhandled signal,
6d45c11a
PMF
1328 * so we cannot rely on it. However, for now, LTTV requires that the header of
1329 * the last sub-buffer contain a valid end time for the trace. This is done
1330 * automatically only when the trace is properly stopped.
1331 *
1332 * If the traced program crashed, it is always possible to manually add the
1333 * right value in the header, or to open the trace in text mode.
1334 *
1335 * FIXME: Fix LTTV so it doesn't need this.
1336 */
68c1021b 1337
6d45c11a 1338static void destroy_traces(void)
68c1021b 1339{
6d45c11a 1340 int result;
a584bc4e
PMF
1341
1342 /* if trace running, finish it */
1343
6d45c11a 1344 DBG("destructor stopping traces");
a584bc4e 1345
6d45c11a
PMF
1346 result = ltt_trace_stop("auto");
1347 if(result == -1) {
1348 ERR("ltt_trace_stop error");
1349 }
1350
31d392f1 1351 result = ltt_trace_destroy("auto", 0);
6d45c11a
PMF
1352 if(result == -1) {
1353 ERR("ltt_trace_destroy error");
1354 }
68c1021b 1355}
1e2944cb 1356
97d9b88b
PMF
1357static int trace_recording(void)
1358{
1359 int retval = 0;
b73a4c47 1360 struct ust_trace *trace;
97d9b88b
PMF
1361
1362 ltt_lock_traces();
1363
1364 list_for_each_entry(trace, &ltt_traces.head, list) {
1365 if(trace->active) {
1366 retval = 1;
1367 break;
1368 }
1369 }
1370
1371 ltt_unlock_traces();
1372
1373 return retval;
1374}
1375
f293009f 1376#if 0
97d9b88b
PMF
1377static int have_consumer(void)
1378{
1379 return !list_empty(&blocked_consumers);
1380}
f293009f 1381#endif
97d9b88b 1382
f293009f 1383int restarting_usleep(useconds_t usecs)
97d9b88b
PMF
1384{
1385 struct timespec tv;
1386 int result;
1387
f293009f
PMF
1388 tv.tv_sec = 0;
1389 tv.tv_nsec = usecs * 1000;
97d9b88b
PMF
1390
1391 do {
1392 result = nanosleep(&tv, &tv);
1393 } while(result == -1 && errno == EINTR);
1394
1395 return result;
1396}
1397
fc253ce0
PMF
1398static void stop_listener()
1399{
1400 int result;
1401
1402 result = pthread_cancel(listener_thread);
1403 if(result == -1) {
1404 PERROR("pthread_cancel");
1405 }
1406 result = pthread_join(listener_thread, NULL);
1407 if(result == -1) {
1408 PERROR("pthread_join");
1409 }
1410}
1411
f293009f
PMF
1412/* This destructor keeps the process alive for a few seconds in order
1413 * to leave time to ustd to connect to its buffers. This is necessary
1414 * for programs whose execution is very short. It is also useful in all
1415 * programs when tracing is started close to the end of the program
1416 * execution.
1417 *
1418 * FIXME: For now, this only works for the first trace created in a
1419 * process.
1420 */
1421
97d9b88b
PMF
1422static void __attribute__((destructor)) keepalive()
1423{
f293009f 1424 if(trace_recording() && buffers_to_export) {
c472cce0 1425 int total = 0;
f293009f
PMF
1426 DBG("Keeping process alive for consumer daemon...");
1427 while(buffers_to_export) {
1428 const int interv = 200000;
c472cce0 1429 restarting_usleep(interv);
f293009f
PMF
1430 total += interv;
1431
1432 if(total >= 3000000) {
c472cce0 1433 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
f293009f
PMF
1434 break;
1435 }
1436 }
1437 DBG("Finally dying...");
1438 }
6d45c11a
PMF
1439
1440 destroy_traces();
1441
fc253ce0
PMF
1442 /* Ask the listener to stop and clean up. */
1443 stop_listener();
97d9b88b 1444}
97d9b88b 1445
775c8a3f 1446void ust_potential_exec(void)
c396a841
PMF
1447{
1448 trace_mark(ust, potential_exec, MARK_NOARGS);
1449
775c8a3f
PMF
1450 DBG("test");
1451
c396a841
PMF
1452 keepalive();
1453}
1454
1e2944cb
PMF
1455/* Notify ust that there was a fork. This needs to be called inside
1456 * the new process, anytime a process whose memory is not shared with
1457 * the parent is created. If this function is not called, the events
1458 * of the new process will not be collected.
616ed36a
PMF
1459 *
1460 * Signals should be disabled before the fork and reenabled only after
1461 * this call in order to guarantee tracing is not started before ust_fork()
1462 * sanitizes the new process.
1e2944cb
PMF
1463 */
1464
616ed36a 1465static void ust_fork(void)
1e2944cb 1466{
99b72dc0
PMF
1467 struct blocked_consumer *bc;
1468 struct blocked_consumer *deletable_bc = NULL;
1469 int result;
1470
31d392f1 1471 /* FIXME: technically, the locks could have been taken before the fork */
1e2944cb 1472 DBG("ust: forking");
73850001
PMF
1473
1474 /* break lock if necessary */
1475 ltt_unlock_traces();
1476
1e2944cb 1477 ltt_trace_stop("auto");
31d392f1 1478 ltt_trace_destroy("auto", 1);
99b72dc0
PMF
1479 /* Delete all active connections */
1480 ustcomm_close_all_connections(&ustcomm_app.server);
1481
1482 /* Delete all blocked consumers */
1483 list_for_each_entry(bc, &blocked_consumers, list) {
d9ce395d
PMF
1484 close(bc->fd_producer);
1485 close(bc->fd_consumer);
99b72dc0
PMF
1486 free(deletable_bc);
1487 deletable_bc = bc;
1488 list_del(&bc->list);
1489 }
1490
2a79ceeb
PMF
1491 /* free app, keeping socket file */
1492 ustcomm_fini_app(&ustcomm_app, 1);
393bc391 1493
cedc0155 1494 buffers_to_export = 0;
1e2944cb 1495 have_listener = 0;
99b72dc0 1496 init_socket();
9fb49d0e 1497 create_listener();
99b72dc0
PMF
1498 ltt_trace_setup("auto");
1499 result = ltt_trace_set_type("auto", "ustrelay");
1500 if(result < 0) {
1501 ERR("ltt_trace_set_type failed");
036db133 1502 return;
99b72dc0
PMF
1503 }
1504
1505 ltt_trace_alloc("auto");
1506 ltt_trace_start("auto");
ad45e833 1507 inform_consumer_daemon("auto");
1e2944cb
PMF
1508}
1509
616ed36a
PMF
1510void ust_before_fork(ust_fork_info_t *fork_info)
1511{
1512 /* Disable signals. This is to avoid that the child
1513 * intervenes before it is properly setup for tracing. It is
1514 * safer to disable all signals, because then we know we are not
1515 * breaking anything by restoring the original mask.
1516 */
1517 sigset_t all_sigs;
1518 int result;
1519
1520 /* FIXME:
1521 - only do this if tracing is active
1522 */
1523
1524 /* Disable signals */
1525 sigfillset(&all_sigs);
1526 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1527 if(result == -1) {
1528 PERROR("sigprocmask");
1529 return;
1530 }
1531}
1532
1533/* Don't call this function directly in a traced program */
1534static void ust_after_fork_common(ust_fork_info_t *fork_info)
1535{
1536 int result;
616ed36a
PMF
1537
1538 /* Restore signals */
1539 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1540 if(result == -1) {
1541 PERROR("sigprocmask");
1542 return;
1543 }
1544}
1545
1546void ust_after_fork_parent(ust_fork_info_t *fork_info)
1547{
1548 /* Reenable signals */
1549 ust_after_fork_common(fork_info);
1550}
1551
1552void ust_after_fork_child(ust_fork_info_t *fork_info)
1553{
1554 /* First sanitize the child */
1555 ust_fork();
1556
1557 /* Then reenable interrupts */
1558 ust_after_fork_common(fork_info);
1559}
1560
This page took 0.124483 seconds and 4 git commands to generate.