ust_fork(): close the ustcomm_app socket in the child on fork()
[ust.git] / libust / tracectl.c
CommitLineData
c39c72ee
PMF
1/* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
872037bb 18#define _GNU_SOURCE
68c1021b
PMF
19#include <stdio.h>
20#include <stdint.h>
21#include <signal.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/un.h>
98963de4 25#include <sched.h>
a584bc4e 26#include <fcntl.h>
3a7b90de 27#include <poll.h>
ef290fca 28#include <regex.h>
fbd8191b 29
b7ea1a1c 30#include <urcu-bp.h>
26cc7017 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
26cc7017
PMF
128/* This needs to be called whenever a new thread is created. It notifies
129 * liburcu of the new thread.
130 */
131
132void ust_register_thread(void)
68c1021b 133{
26cc7017 134 rcu_register_thread();
68c1021b
PMF
135}
136
98963de4
PMF
137int fd_notif = -1;
138void notif_cb(void)
139{
140 int result;
141 struct trctl_msg msg;
142
143 /* FIXME: fd_notif should probably be protected by a spinlock */
144
145 if(fd_notif == -1)
146 return;
147
148 msg.type = MSG_NOTIF;
149 msg.size = sizeof(msg.type);
150
151 /* FIXME: don't block here */
152 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
153 if(result == -1) {
154 PERROR("write");
155 return;
156 }
157}
158
ad45e833
PMF
159/* Ask the daemon to collect a trace called trace_name and being
160 * produced by this pid.
161 *
162 * The trace must be at least allocated. (It can also be started.)
163 * This is because _ltt_trace_find is used.
164 */
165
166static void inform_consumer_daemon(const char *trace_name)
d0b5f2b9 167{
204141ee 168 int i,j;
b73a4c47 169 struct ust_trace *trace;
ad45e833
PMF
170 pid_t pid = getpid();
171 int result;
172
173 ltt_lock_traces();
174
175 trace = _ltt_trace_find(trace_name);
176 if(trace == NULL) {
177 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
178 goto finish;
179 }
180
181 for(i=0; i < trace->nr_channels; i++) {
204141ee
PMF
182 /* iterate on all cpus */
183 for(j=0; j<trace->channels[i].n_cpus; j++) {
184 char *buf;
185 asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j);
186 result = ustcomm_request_consumer(pid, buf);
187 if(result == -1) {
188 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
189 /* continue even if fail */
190 }
191 free(buf);
192 buffers_to_export++;
ad45e833
PMF
193 }
194 }
195
196 finish:
197 ltt_unlock_traces();
d0b5f2b9 198}
fbd8191b 199
3a7b90de
PMF
200void process_blocked_consumers(void)
201{
202 int n_fds = 0;
203 struct pollfd *fds;
204 struct blocked_consumer *bc;
205 int idx = 0;
206 char inbuf;
207 int result;
208
209 list_for_each_entry(bc, &blocked_consumers, list) {
210 n_fds++;
211 }
212
213 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
214 if(fds == NULL) {
215 ERR("malloc returned NULL");
216 return;
217 }
218
219 list_for_each_entry(bc, &blocked_consumers, list) {
220 fds[idx].fd = bc->fd_producer;
221 fds[idx].events = POLLIN;
222 bc->tmp_poll_idx = idx;
223 idx++;
224 }
225
69ba0156
PMF
226 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
227 /* nothing */;
3a7b90de
PMF
228 if(result == -1) {
229 PERROR("poll");
872037bb 230 return;
3a7b90de
PMF
231 }
232
233 list_for_each_entry(bc, &blocked_consumers, list) {
234 if(fds[bc->tmp_poll_idx].revents) {
235 long consumed_old = 0;
236 char *reply;
237
238 result = read(bc->fd_producer, &inbuf, 1);
239 if(result == -1) {
240 PERROR("read");
241 continue;
242 }
243 if(result == 0) {
244 DBG("PRODUCER END");
245
246 close(bc->fd_producer);
247
769d0157 248 list_del(&bc->list);
3a7b90de
PMF
249
250 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
251 if(result < 0) {
252 ERR("ustcomm_send_reply failed");
253 continue;
254 }
255
256 continue;
257 }
258
b73a4c47 259 result = ust_buffers_get_subbuf(bc->buf, &consumed_old);
3a7b90de
PMF
260 if(result == -EAGAIN) {
261 WARN("missed buffer?");
262 continue;
263 }
264 else if(result < 0) {
b73a4c47 265 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result));
3a7b90de
PMF
266 }
267 asprintf(&reply, "%s %ld", "OK", consumed_old);
268 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
269 if(result < 0) {
270 ERR("ustcomm_send_reply failed");
271 free(reply);
272 continue;
273 }
274 free(reply);
275
769d0157 276 list_del(&bc->list);
3a7b90de
PMF
277 }
278 }
279
280}
281
204141ee
PMF
282void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu)
283{
284 const char *sep;
285
286 sep = rindex(channel_and_cpu, '_');
287 if(sep == NULL) {
288 *cpu = -1;
289 sep = channel_and_cpu + strlen(channel_and_cpu);
290 }
291 else {
292 *cpu = atoi(sep+1);
293 }
294
295 asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu);
296}
297
298static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src)
299{
300 int retval = 0;
b73a4c47 301 struct ust_trace *trace;
204141ee
PMF
302 char trace_name[] = "auto";
303 int i;
304 char *channel_and_cpu;
305 int found = 0;
306 int result;
307 char *ch_name;
308 int ch_cpu;
309
310 DBG("get_shmid");
311
312 channel_and_cpu = nth_token(recvbuf, 1);
313 if(channel_and_cpu == NULL) {
27e84572 314 ERR("cannot parse channel");
204141ee
PMF
315 goto end;
316 }
317
318 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
319 if(ch_cpu == -1) {
27e84572 320 ERR("problem parsing channel name");
204141ee
PMF
321 goto free_short_chan_name;
322 }
323
324 ltt_lock_traces();
325 trace = _ltt_trace_find(trace_name);
326 ltt_unlock_traces();
327
328 if(trace == NULL) {
329 ERR("cannot find trace!");
330 retval = -1;
331 goto free_short_chan_name;
332 }
333
334 for(i=0; i<trace->nr_channels; i++) {
335 struct ust_channel *channel = &trace->channels[i];
336 struct ust_buffer *buf = channel->buf[ch_cpu];
337
338 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
339 char *reply;
340
341// DBG("the shmid for the requested channel is %d", buf->shmid);
342// DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
343 asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]);
344
345 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
346 if(result) {
27e84572 347 ERR("ustcomm_send_reply failed");
204141ee
PMF
348 free(reply);
349 retval = -1;
350 goto free_short_chan_name;
351 }
352
353 free(reply);
354
355 found = 1;
356 break;
357 }
358 }
359
d5adede0 360 if(!found) {
27e84572 361 ERR("channel not found (%s)", channel_and_cpu);
204141ee
PMF
362 }
363
364 free_short_chan_name:
365 free(ch_name);
366
367 end:
368 return retval;
369}
370
371static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
372{
373 int retval = 0;
b73a4c47 374 struct ust_trace *trace;
204141ee
PMF
375 char trace_name[] = "auto";
376 int i;
377 char *channel_and_cpu;
378 int found = 0;
379 int result;
380 char *ch_name;
381 int ch_cpu;
382
383 DBG("get_n_subbufs");
384
385 channel_and_cpu = nth_token(recvbuf, 1);
386 if(channel_and_cpu == NULL) {
27e84572 387 ERR("cannot parse channel");
204141ee
PMF
388 goto end;
389 }
390
391 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
392 if(ch_cpu == -1) {
27e84572 393 ERR("problem parsing channel name");
204141ee
PMF
394 goto free_short_chan_name;
395 }
396
397 ltt_lock_traces();
398 trace = _ltt_trace_find(trace_name);
399 ltt_unlock_traces();
400
401 if(trace == NULL) {
402 ERR("cannot find trace!");
403 retval = -1;
404 goto free_short_chan_name;
405 }
406
407 for(i=0; i<trace->nr_channels; i++) {
408 struct ust_channel *channel = &trace->channels[i];
409
410 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
411 char *reply;
412
413 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
414 asprintf(&reply, "%d", channel->subbuf_cnt);
415
416 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
417 if(result) {
27e84572 418 ERR("ustcomm_send_reply failed");
204141ee
PMF
419 free(reply);
420 retval = -1;
421 goto free_short_chan_name;
422 }
423
424 free(reply);
425 found = 1;
426 break;
427 }
428 }
429 if(found == 0) {
27e84572 430 ERR("unable to find channel");
204141ee
PMF
431 }
432
433 free_short_chan_name:
434 free(ch_name);
435
436 end:
437 return retval;
438}
439
440static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
441{
442 int retval = 0;
b73a4c47 443 struct ust_trace *trace;
204141ee
PMF
444 char trace_name[] = "auto";
445 int i;
446 char *channel_and_cpu;
447 int found = 0;
448 int result;
449 char *ch_name;
450 int ch_cpu;
451
452 DBG("get_subbuf_size");
453
454 channel_and_cpu = nth_token(recvbuf, 1);
455 if(channel_and_cpu == NULL) {
27e84572 456 ERR("cannot parse channel");
204141ee
PMF
457 goto end;
458 }
459
460 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
461 if(ch_cpu == -1) {
27e84572 462 ERR("problem parsing channel name");
204141ee
PMF
463 goto free_short_chan_name;
464 }
465
466 ltt_lock_traces();
467 trace = _ltt_trace_find(trace_name);
468 ltt_unlock_traces();
469
470 if(trace == NULL) {
471 ERR("cannot find trace!");
472 retval = -1;
473 goto free_short_chan_name;
474 }
475
476 for(i=0; i<trace->nr_channels; i++) {
477 struct ust_channel *channel = &trace->channels[i];
478
479 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
480 char *reply;
481
482 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
483 asprintf(&reply, "%zd", channel->subbuf_size);
484
485 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
486 if(result) {
27e84572 487 ERR("ustcomm_send_reply failed");
204141ee
PMF
488 free(reply);
489 retval = -1;
490 goto free_short_chan_name;
491 }
492
493 free(reply);
494 found = 1;
495 break;
496 }
497 }
498 if(found == 0) {
27e84572 499 ERR("unable to find channel");
204141ee
PMF
500 }
501
502 free_short_chan_name:
503 free(ch_name);
504
505 end:
506 return retval;
507}
508
e43efba9 509static unsigned int poweroftwo(unsigned int x)
763f41e5
DS
510{
511 unsigned int power2 = 1;
512 unsigned int hardcoded = 2147483648; /* FIX max 2^31 */
513
514 if (x < 2)
515 return 2;
516
517 while (power2 < x && power2 < hardcoded)
518 power2 *= 2;
519
520 return power2;
521}
522
523static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
524{
525 char *channel_slash_size;
526 char ch_name[256]="";
527 unsigned int size, power;
528 int retval = 0;
529 struct ust_trace *trace;
530 char trace_name[] = "auto";
531 int i;
532 int found = 0;
533
534 DBG("set_subbuf_size");
535
536 channel_slash_size = nth_token(recvbuf, 1);
537 sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size);
538
539 if(ch_name == NULL) {
540 ERR("cannot parse channel");
541 goto end;
542 }
543
544 power = poweroftwo(size);
545 if (power != size)
546 WARN("using the next 2^n = %u\n", power);
547
548 ltt_lock_traces();
549 trace = _ltt_trace_find_setup(trace_name);
550 if(trace == NULL) {
551 ERR("cannot find trace!");
763f41e5
DS
552 retval = -1;
553 goto end;
554 }
555
556 for(i = 0; i < trace->nr_channels; i++) {
557 struct ust_channel *channel = &trace->channels[i];
558
559 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
560
561 channel->subbuf_size = power;
562 DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size);
563
564 found = 1;
565 break;
566 }
567 }
568 if(found == 0) {
569 ERR("unable to find channel");
570 }
571
763f41e5 572 end:
86dd0ebc 573 ltt_unlock_traces();
763f41e5
DS
574 return retval;
575}
576
577static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src)
578{
579 char *channel_slash_num;
580 char ch_name[256]="";
581 unsigned int num;
582 int retval = 0;
583 struct ust_trace *trace;
584 char trace_name[] = "auto";
585 int i;
586 int found = 0;
587
588 DBG("set_subbuf_num");
589
590 channel_slash_num = nth_token(recvbuf, 1);
591 sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num);
592
593 if(ch_name == NULL) {
594 ERR("cannot parse channel");
595 goto end;
596 }
597 if (num < 2) {
598 ERR("subbuffer count should be greater than 2");
599 goto end;
600 }
601
602 ltt_lock_traces();
603 trace = _ltt_trace_find_setup(trace_name);
604 if(trace == NULL) {
605 ERR("cannot find trace!");
763f41e5
DS
606 retval = -1;
607 goto end;
608 }
609
610 for(i = 0; i < trace->nr_channels; i++) {
611 struct ust_channel *channel = &trace->channels[i];
612
613 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
614
615 channel->subbuf_cnt = num;
616 DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt);
617
618 found = 1;
619 break;
620 }
621 }
622 if(found == 0) {
623 ERR("unable to find channel");
624 }
625
763f41e5 626 end:
86dd0ebc 627 ltt_unlock_traces();
763f41e5
DS
628 return retval;
629}
630
204141ee
PMF
631static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
632{
633 int retval = 0;
b73a4c47 634 struct ust_trace *trace;
204141ee
PMF
635 char trace_name[] = "auto";
636 int i;
637 char *channel_and_cpu;
638 int found = 0;
639 char *ch_name;
640 int ch_cpu;
641
642 DBG("get_subbuf");
643
644 channel_and_cpu = nth_token(recvbuf, 1);
645 if(channel_and_cpu == NULL) {
27e84572 646 ERR("cannot parse channel");
204141ee
PMF
647 goto end;
648 }
649
650 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
651 if(ch_cpu == -1) {
27e84572 652 ERR("problem parsing channel name");
204141ee
PMF
653 goto free_short_chan_name;
654 }
655
656 ltt_lock_traces();
657 trace = _ltt_trace_find(trace_name);
204141ee
PMF
658
659 if(trace == NULL) {
2ddb81a8
PMF
660 int result;
661
662 WARN("Cannot find trace. It was likely destroyed by the user.");
86dd0ebc 663 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
2ddb81a8
PMF
664 if(result) {
665 ERR("ustcomm_send_reply failed");
2ddb81a8 666 retval = -1;
86dd0ebc 667 goto unlock_traces;
2ddb81a8
PMF
668 }
669
86dd0ebc 670 goto unlock_traces;
204141ee
PMF
671 }
672
673 for(i=0; i<trace->nr_channels; i++) {
674 struct ust_channel *channel = &trace->channels[i];
675
676 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
677 struct ust_buffer *buf = channel->buf[ch_cpu];
678 struct blocked_consumer *bc;
679
680 found = 1;
681
682 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
683 if(bc == NULL) {
684 ERR("malloc returned NULL");
86dd0ebc 685 goto unlock_traces;
204141ee
PMF
686 }
687 bc->fd_consumer = src->fd;
688 bc->fd_producer = buf->data_ready_fd_read;
689 bc->buf = buf;
690 bc->src = *src;
691 bc->server = ustcomm_app.server;
692
693 list_add(&bc->list, &blocked_consumers);
694
d5adede0
PMF
695 /* Being here is the proof the daemon has mapped the buffer in its
696 * memory. We may now decrement buffers_to_export.
697 */
698 if(atomic_long_read(&buf->consumed) == 0) {
699 DBG("decrementing buffers_to_export");
700 buffers_to_export--;
701 }
702
204141ee
PMF
703 break;
704 }
705 }
706 if(found == 0) {
27e84572 707 ERR("unable to find channel");
204141ee
PMF
708 }
709
86dd0ebc
PMF
710 unlock_traces:
711 ltt_unlock_traces();
712
204141ee
PMF
713 free_short_chan_name:
714 free(ch_name);
715
716 end:
717 return retval;
718}
719
720static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src)
721{
722 int retval = 0;
b73a4c47 723 struct ust_trace *trace;
204141ee
PMF
724 char trace_name[] = "auto";
725 int i;
726 char *channel_and_cpu;
727 int found = 0;
728 int result;
729 char *ch_name;
730 int ch_cpu;
731 long consumed_old;
732 char *consumed_old_str;
733 char *endptr;
2ddb81a8 734 char *reply = NULL;
204141ee
PMF
735
736 DBG("put_subbuf");
737
738 channel_and_cpu = strdup_malloc(nth_token(recvbuf, 1));
739 if(channel_and_cpu == NULL) {
27e84572 740 ERR("cannot parse channel");
204141ee
PMF
741 retval = -1;
742 goto end;
743 }
744
745 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
746 if(consumed_old_str == NULL) {
27e84572 747 ERR("cannot parse consumed_old");
204141ee
PMF
748 retval = -1;
749 goto free_channel_and_cpu;
750 }
751 consumed_old = strtol(consumed_old_str, &endptr, 10);
752 if(*endptr != '\0') {
27e84572 753 ERR("invalid value for consumed_old");
204141ee
PMF
754 retval = -1;
755 goto free_consumed_old_str;
756 }
757
758 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
759 if(ch_cpu == -1) {
27e84572 760 ERR("problem parsing channel name");
204141ee
PMF
761 retval = -1;
762 goto free_short_chan_name;
763 }
764
765 ltt_lock_traces();
766 trace = _ltt_trace_find(trace_name);
204141ee
PMF
767
768 if(trace == NULL) {
2ddb81a8 769 WARN("Cannot find trace. It was likely destroyed by the user.");
86dd0ebc 770 result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src);
2ddb81a8
PMF
771 if(result) {
772 ERR("ustcomm_send_reply failed");
2ddb81a8 773 retval = -1;
86dd0ebc 774 goto unlock_traces;
2ddb81a8
PMF
775 }
776
86dd0ebc 777 goto unlock_traces;
204141ee
PMF
778 }
779
780 for(i=0; i<trace->nr_channels; i++) {
781 struct ust_channel *channel = &trace->channels[i];
782
783 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
784 struct ust_buffer *buf = channel->buf[ch_cpu];
204141ee
PMF
785
786 found = 1;
787
b73a4c47 788 result = ust_buffers_put_subbuf(buf, consumed_old);
204141ee 789 if(result < 0) {
b73a4c47 790 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu);
204141ee
PMF
791 asprintf(&reply, "%s", "ERROR");
792 }
793 else {
b73a4c47 794 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu);
204141ee
PMF
795 asprintf(&reply, "%s", "OK");
796 }
797
798 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
799 if(result) {
27e84572 800 ERR("ustcomm_send_reply failed");
204141ee
PMF
801 free(reply);
802 retval = -1;
86dd0ebc 803 goto unlock_traces;
204141ee
PMF
804 }
805
806 free(reply);
807 break;
808 }
809 }
810 if(found == 0) {
2ddb81a8 811 ERR("unable to find channel");
204141ee
PMF
812 }
813
86dd0ebc
PMF
814 unlock_traces:
815 ltt_unlock_traces();
204141ee
PMF
816 free_short_chan_name:
817 free(ch_name);
2ddb81a8
PMF
818 free_consumed_old_str:
819 free(consumed_old_str);
820 free_channel_and_cpu:
821 free(channel_and_cpu);
204141ee
PMF
822
823 end:
824 return retval;
825}
826
872037bb 827void *listener_main(void *p)
98963de4
PMF
828{
829 int result;
830
26cc7017
PMF
831 ust_register_thread();
832
b0540e11
PMF
833 DBG("LISTENER");
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
PMF
1108 }
1109}
1110
7958de40 1111volatile sig_atomic_t have_listener = 0;
4440ebcb 1112
98963de4
PMF
1113void create_listener(void)
1114{
9160b4e4 1115#ifdef USE_CLONE
98963de4 1116 static char listener_stack[16384];
c5fdc888 1117 int result;
4440ebcb
PMF
1118#else
1119 pthread_t thread;
9160b4e4 1120#endif
98963de4 1121
c5fdc888
PMF
1122 if(have_listener) {
1123 WARN("not creating listener because we already had one");
4440ebcb 1124 return;
c5fdc888 1125 }
4440ebcb 1126
3847c3ba 1127#ifdef USE_CLONE
c5fdc888 1128 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
1129 if(result == -1) {
1130 perror("clone");
4440ebcb 1131 return;
98963de4 1132 }
3847c3ba 1133#else
b0540e11 1134
3847c3ba
PMF
1135 pthread_create(&thread, NULL, listener_main, NULL);
1136#endif
4440ebcb
PMF
1137
1138 have_listener = 1;
98963de4
PMF
1139}
1140
98963de4 1141static int init_socket(void)
68c1021b 1142{
d0b5f2b9 1143 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
1144}
1145
5de74e51
PMF
1146#define AUTOPROBE_DISABLED 0
1147#define AUTOPROBE_ENABLE_ALL 1
1148#define AUTOPROBE_ENABLE_REGEX 2
1149static int autoprobe_method = AUTOPROBE_DISABLED;
1150static regex_t autoprobe_regex;
ef290fca 1151
20b37a31 1152static void auto_probe_connect(struct marker *m)
68c1021b
PMF
1153{
1154 int result;
1155
5de74e51
PMF
1156 char* concat_name = NULL;
1157 const char *probe_name = "default";
ef290fca 1158
5de74e51 1159 if(autoprobe_method == AUTOPROBE_DISABLED) {
ef290fca
PMF
1160 return;
1161 }
5de74e51
PMF
1162 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1163 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1164 if(result == -1) {
1165 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1166 m->channel, m->name);
1167 return;
1168 }
1169 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1170 free(concat_name);
1171 return;
1172 }
1173 free(concat_name);
ef290fca
PMF
1174 }
1175
5de74e51 1176 result = ltt_marker_connect(m->channel, m->name, probe_name);
acbf228b
PMF
1177 if(result && result != -EEXIST)
1178 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
20b37a31 1179
3ea1e2fc 1180 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
ef290fca 1181
20b37a31
PMF
1182}
1183
c1083aa8 1184static void __attribute__((constructor)) init()
20b37a31
PMF
1185{
1186 int result;
5de74e51 1187 char* autoprobe_val = NULL;
20b37a31 1188
ed1317e7
PMF
1189 /* Assign the pidunique, to be able to differentiate the processes with same
1190 * pid, (before and after an exec).
1191 */
1192 pidunique = make_pidunique();
1193
4440ebcb 1194 /* Initialize RCU in case the constructor order is not good. */
5a66a1f2 1195 rcu_init();
4440ebcb
PMF
1196
1197 /* It is important to do this before events start to be generated. */
1198 ust_register_thread();
1199
5de74e51 1200 DBG("Tracectl constructor");
20b37a31 1201
3847c3ba
PMF
1202 /* Must create socket before signal handler to prevent races.
1203 */
1204 result = init_socket();
1205 if(result == -1) {
1206 ERR("init_socket error");
1207 return;
1208 }
2944a629
PMF
1209
1210 create_listener();
68c1021b 1211
5de74e51
PMF
1212 autoprobe_val = getenv("UST_AUTOPROBE");
1213 if(autoprobe_val) {
4440ebcb
PMF
1214 struct marker_iter iter;
1215
08230db7 1216 DBG("Autoprobe enabled.");
4440ebcb
PMF
1217
1218 /* Ensure markers are initialized */
1219 //init_markers();
1220
1221 /* Ensure marker control is initialized, for the probe */
1222 init_marker_control();
1223
1224 /* first, set the callback that will connect the
1225 * probe on new markers
1226 */
5de74e51
PMF
1227 if(autoprobe_val[0] == '/') {
1228 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1229 if (result) {
1230 char regexerr[150];
1231
1232 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1233 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1234 /* don't crash the application just for this */
1235 }
1236 else {
1237 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1238 }
ef290fca 1239 }
5de74e51
PMF
1240 else {
1241 /* just enable all instrumentation */
1242 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1243 }
1244
1245 marker_set_new_marker_cb(auto_probe_connect);
1246
4440ebcb
PMF
1247 /* Now, connect the probes that were already registered. */
1248 marker_iter_reset(&iter);
1249 marker_iter_start(&iter);
1250
08230db7 1251 DBG("now iterating on markers already registered");
4440ebcb 1252 while(iter.marker) {
08230db7 1253 DBG("now iterating on marker %s", iter.marker->name);
4440ebcb
PMF
1254 auto_probe_connect(iter.marker);
1255 marker_iter_next(&iter);
1256 }
1257 }
1258
4db647c5
PMF
1259 if(getenv("UST_TRACE")) {
1260 char trace_name[] = "auto";
1261 char trace_type[] = "ustrelay";
1262
1263 DBG("starting early tracing");
1264
1265 /* Ensure marker control is initialized */
1266 init_marker_control();
1267
4db647c5
PMF
1268 /* Ensure markers are initialized */
1269 init_markers();
1270
e17571a5
PMF
1271 /* Ensure buffers are initialized, for the transport to be available.
1272 * We are about to set a trace type and it will fail without this.
1273 */
1274 init_ustrelay_transport();
1275
027ffc9d
PMF
1276 /* FIXME: When starting early tracing (here), depending on the
1277 * order of constructors, it is very well possible some marker
1278 * sections are not yet registered. Because of this, some
1279 * channels may not be registered. Yet, we are about to ask the
1280 * daemon to collect the channels. Channels which are not yet
1281 * registered will not be collected.
1282 *
1283 * Currently, in LTTng, there is no way to add a channel after
1284 * trace start. The reason for this is that it induces complex
1285 * concurrency issues on the trace structures, which can only
1286 * be resolved using RCU. This has not been done yet. As a
1287 * workaround, we are forcing the registration of the "ust"
1288 * channel here. This is the only channel (apart from metadata)
1289 * that can be reliably used in early tracing.
1290 *
1291 * Non-early tracing does not have this problem and can use
1292 * arbitrary channel names.
1293 */
20b37a31 1294 ltt_channels_register("ust");
4db647c5
PMF
1295
1296 result = ltt_trace_setup(trace_name);
1297 if(result < 0) {
1298 ERR("ltt_trace_setup failed");
1299 return;
1300 }
1301
1302 result = ltt_trace_set_type(trace_name, trace_type);
1303 if(result < 0) {
1304 ERR("ltt_trace_set_type failed");
1305 return;
1306 }
1307
1308 result = ltt_trace_alloc(trace_name);
1309 if(result < 0) {
1310 ERR("ltt_trace_alloc failed");
1311 return;
1312 }
1313
1314 result = ltt_trace_start(trace_name);
1315 if(result < 0) {
1316 ERR("ltt_trace_start failed");
1317 return;
1318 }
60e57148
PMF
1319
1320 /* Do this after the trace is started in order to avoid creating confusion
1321 * if the trace fails to start. */
1322 inform_consumer_daemon(trace_name);
4db647c5
PMF
1323 }
1324
68c1021b
PMF
1325
1326 return;
1327
1328 /* should decrementally destroy stuff if error */
1329
1330}
1331
1332/* This is only called if we terminate normally, not with an unhandled signal,
6d45c11a
PMF
1333 * so we cannot rely on it. However, for now, LTTV requires that the header of
1334 * the last sub-buffer contain a valid end time for the trace. This is done
1335 * automatically only when the trace is properly stopped.
1336 *
1337 * If the traced program crashed, it is always possible to manually add the
1338 * right value in the header, or to open the trace in text mode.
1339 *
1340 * FIXME: Fix LTTV so it doesn't need this.
1341 */
68c1021b 1342
6d45c11a 1343static void destroy_traces(void)
68c1021b 1344{
6d45c11a 1345 int result;
a584bc4e
PMF
1346
1347 /* if trace running, finish it */
1348
6d45c11a 1349 DBG("destructor stopping traces");
a584bc4e 1350
6d45c11a
PMF
1351 result = ltt_trace_stop("auto");
1352 if(result == -1) {
1353 ERR("ltt_trace_stop error");
1354 }
1355
31d392f1 1356 result = ltt_trace_destroy("auto", 0);
6d45c11a
PMF
1357 if(result == -1) {
1358 ERR("ltt_trace_destroy error");
1359 }
68c1021b 1360}
1e2944cb 1361
97d9b88b
PMF
1362static int trace_recording(void)
1363{
1364 int retval = 0;
b73a4c47 1365 struct ust_trace *trace;
97d9b88b
PMF
1366
1367 ltt_lock_traces();
1368
1369 list_for_each_entry(trace, &ltt_traces.head, list) {
1370 if(trace->active) {
1371 retval = 1;
1372 break;
1373 }
1374 }
1375
1376 ltt_unlock_traces();
1377
1378 return retval;
1379}
1380
f293009f 1381#if 0
97d9b88b
PMF
1382static int have_consumer(void)
1383{
1384 return !list_empty(&blocked_consumers);
1385}
f293009f 1386#endif
97d9b88b 1387
f293009f 1388int restarting_usleep(useconds_t usecs)
97d9b88b
PMF
1389{
1390 struct timespec tv;
1391 int result;
1392
f293009f
PMF
1393 tv.tv_sec = 0;
1394 tv.tv_nsec = usecs * 1000;
97d9b88b
PMF
1395
1396 do {
1397 result = nanosleep(&tv, &tv);
1398 } while(result == -1 && errno == EINTR);
1399
1400 return result;
1401}
1402
f293009f
PMF
1403/* This destructor keeps the process alive for a few seconds in order
1404 * to leave time to ustd to connect to its buffers. This is necessary
1405 * for programs whose execution is very short. It is also useful in all
1406 * programs when tracing is started close to the end of the program
1407 * execution.
1408 *
1409 * FIXME: For now, this only works for the first trace created in a
1410 * process.
1411 */
1412
97d9b88b
PMF
1413static void __attribute__((destructor)) keepalive()
1414{
f293009f 1415 if(trace_recording() && buffers_to_export) {
c472cce0 1416 int total = 0;
f293009f
PMF
1417 DBG("Keeping process alive for consumer daemon...");
1418 while(buffers_to_export) {
1419 const int interv = 200000;
c472cce0 1420 restarting_usleep(interv);
f293009f
PMF
1421 total += interv;
1422
1423 if(total >= 3000000) {
c472cce0 1424 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
f293009f
PMF
1425 break;
1426 }
1427 }
1428 DBG("Finally dying...");
1429 }
6d45c11a
PMF
1430
1431 destroy_traces();
1432
1433 ustcomm_fini_app(&ustcomm_app);
97d9b88b 1434}
97d9b88b 1435
775c8a3f 1436void ust_potential_exec(void)
c396a841
PMF
1437{
1438 trace_mark(ust, potential_exec, MARK_NOARGS);
1439
775c8a3f
PMF
1440 DBG("test");
1441
c396a841
PMF
1442 keepalive();
1443}
1444
1e2944cb
PMF
1445/* Notify ust that there was a fork. This needs to be called inside
1446 * the new process, anytime a process whose memory is not shared with
1447 * the parent is created. If this function is not called, the events
1448 * of the new process will not be collected.
616ed36a
PMF
1449 *
1450 * Signals should be disabled before the fork and reenabled only after
1451 * this call in order to guarantee tracing is not started before ust_fork()
1452 * sanitizes the new process.
1e2944cb
PMF
1453 */
1454
616ed36a 1455static void ust_fork(void)
1e2944cb 1456{
99b72dc0
PMF
1457 struct blocked_consumer *bc;
1458 struct blocked_consumer *deletable_bc = NULL;
1459 int result;
1460
31d392f1 1461 /* FIXME: technically, the locks could have been taken before the fork */
1e2944cb 1462 DBG("ust: forking");
73850001
PMF
1463
1464 /* break lock if necessary */
1465 ltt_unlock_traces();
1466
1e2944cb 1467 ltt_trace_stop("auto");
31d392f1 1468 ltt_trace_destroy("auto", 1);
99b72dc0
PMF
1469 /* Delete all active connections */
1470 ustcomm_close_all_connections(&ustcomm_app.server);
1471
1472 /* Delete all blocked consumers */
1473 list_for_each_entry(bc, &blocked_consumers, list) {
d9ce395d
PMF
1474 close(bc->fd_producer);
1475 close(bc->fd_consumer);
99b72dc0
PMF
1476 free(deletable_bc);
1477 deletable_bc = bc;
1478 list_del(&bc->list);
1479 }
1480
393bc391
PMF
1481 ustcomm_free_app(&ustcomm_app);
1482
cedc0155 1483 buffers_to_export = 0;
1e2944cb 1484 have_listener = 0;
99b72dc0 1485 init_socket();
9fb49d0e 1486 create_listener();
99b72dc0
PMF
1487 ltt_trace_setup("auto");
1488 result = ltt_trace_set_type("auto", "ustrelay");
1489 if(result < 0) {
1490 ERR("ltt_trace_set_type failed");
036db133 1491 return;
99b72dc0
PMF
1492 }
1493
1494 ltt_trace_alloc("auto");
1495 ltt_trace_start("auto");
ad45e833 1496 inform_consumer_daemon("auto");
1e2944cb
PMF
1497}
1498
616ed36a
PMF
1499void ust_before_fork(ust_fork_info_t *fork_info)
1500{
1501 /* Disable signals. This is to avoid that the child
1502 * intervenes before it is properly setup for tracing. It is
1503 * safer to disable all signals, because then we know we are not
1504 * breaking anything by restoring the original mask.
1505 */
1506 sigset_t all_sigs;
1507 int result;
1508
1509 /* FIXME:
1510 - only do this if tracing is active
1511 */
1512
1513 /* Disable signals */
1514 sigfillset(&all_sigs);
1515 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1516 if(result == -1) {
1517 PERROR("sigprocmask");
1518 return;
1519 }
1520}
1521
1522/* Don't call this function directly in a traced program */
1523static void ust_after_fork_common(ust_fork_info_t *fork_info)
1524{
1525 int result;
616ed36a
PMF
1526
1527 /* Restore signals */
1528 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1529 if(result == -1) {
1530 PERROR("sigprocmask");
1531 return;
1532 }
1533}
1534
1535void ust_after_fork_parent(ust_fork_info_t *fork_info)
1536{
1537 /* Reenable signals */
1538 ust_after_fork_common(fork_info);
1539}
1540
1541void ust_after_fork_child(ust_fork_info_t *fork_info)
1542{
1543 /* First sanitize the child */
1544 ust_fork();
1545
1546 /* Then reenable interrupts */
1547 ust_after_fork_common(fork_info);
1548}
1549
This page took 0.112142 seconds and 4 git commands to generate.