change benign WARN()s to DBG()s
[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
872037bb 822void *listener_main(void *p)
98963de4
PMF
823{
824 int result;
825
b0540e11
PMF
826 DBG("LISTENER");
827
98963de4 828 for(;;) {
aafb1650
PMF
829 char trace_name[] = "auto";
830 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
831 char *recvbuf;
832 int len;
b02e31e5 833 struct ustcomm_source src;
98963de4 834
3a7b90de
PMF
835 process_blocked_consumers();
836
837 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
838 if(result < 0) {
d0b5f2b9
PMF
839 WARN("error in ustcomm_app_recv_message");
840 continue;
841 }
3a7b90de
PMF
842 else if(result == 0) {
843 /* no message */
844 continue;
845 }
98963de4 846
08230db7 847 DBG("received a message! it's: %s", recvbuf);
d0b5f2b9 848 len = strlen(recvbuf);
98963de4 849
d0b5f2b9 850 if(!strcmp(recvbuf, "print_markers")) {
52c51a47
PMF
851 print_markers(stderr);
852 }
853 else if(!strcmp(recvbuf, "list_markers")) {
854 char *ptr;
855 size_t size;
856 FILE *fp;
857
858 fp = open_memstream(&ptr, &size);
859 print_markers(fp);
860 fclose(fp);
861
862 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
863
864 free(ptr);
865 }
866 else if(!strcmp(recvbuf, "start")) {
867 /* start is an operation that setups the trace, allocates it and starts it */
868 result = ltt_trace_setup(trace_name);
869 if(result < 0) {
870 ERR("ltt_trace_setup failed");
872037bb 871 return (void *)1;
52c51a47
PMF
872 }
873
874 result = ltt_trace_set_type(trace_name, trace_type);
875 if(result < 0) {
876 ERR("ltt_trace_set_type failed");
872037bb 877 return (void *)1;
52c51a47
PMF
878 }
879
880 result = ltt_trace_alloc(trace_name);
881 if(result < 0) {
882 ERR("ltt_trace_alloc failed");
872037bb 883 return (void *)1;
52c51a47
PMF
884 }
885
ad45e833 886 inform_consumer_daemon(trace_name);
52c51a47
PMF
887
888 result = ltt_trace_start(trace_name);
889 if(result < 0) {
890 ERR("ltt_trace_start failed");
891 continue;
892 }
d0b5f2b9
PMF
893 }
894 else if(!strcmp(recvbuf, "trace_setup")) {
895 DBG("trace setup");
fbd8191b 896
d0b5f2b9
PMF
897 result = ltt_trace_setup(trace_name);
898 if(result < 0) {
899 ERR("ltt_trace_setup failed");
872037bb 900 return (void *)1;
fbd8191b 901 }
d0b5f2b9
PMF
902
903 result = ltt_trace_set_type(trace_name, trace_type);
904 if(result < 0) {
905 ERR("ltt_trace_set_type failed");
872037bb 906 return (void *)1;
fbd8191b 907 }
d0b5f2b9
PMF
908 }
909 else if(!strcmp(recvbuf, "trace_alloc")) {
910 DBG("trace alloc");
911
912 result = ltt_trace_alloc(trace_name);
913 if(result < 0) {
914 ERR("ltt_trace_alloc failed");
872037bb 915 return (void *)1;
fbd8191b 916 }
763f41e5 917 inform_consumer_daemon(trace_name);
d0b5f2b9 918 }
62ec620f
PMF
919 else if(!strcmp(recvbuf, "trace_create")) {
920 DBG("trace create");
921
922 result = ltt_trace_setup(trace_name);
923 if(result < 0) {
924 ERR("ltt_trace_setup failed");
925 return (void *)1;
926 }
927
928 result = ltt_trace_set_type(trace_name, trace_type);
929 if(result < 0) {
930 ERR("ltt_trace_set_type failed");
931 return (void *)1;
932 }
763f41e5
DS
933 }
934 else if(!strcmp(recvbuf, "trace_start")) {
935 DBG("trace start");
62ec620f
PMF
936
937 result = ltt_trace_alloc(trace_name);
938 if(result < 0) {
939 ERR("ltt_trace_alloc failed");
940 return (void *)1;
941 }
763f41e5
DS
942 if(!result) {
943 inform_consumer_daemon(trace_name);
944 }
d0b5f2b9
PMF
945
946 result = ltt_trace_start(trace_name);
947 if(result < 0) {
948 ERR("ltt_trace_start failed");
949 continue;
fbd8191b 950 }
d0b5f2b9
PMF
951 }
952 else if(!strcmp(recvbuf, "trace_stop")) {
953 DBG("trace stop");
954
955 result = ltt_trace_stop(trace_name);
956 if(result < 0) {
957 ERR("ltt_trace_stop failed");
872037bb 958 return (void *)1;
aafb1650 959 }
d0b5f2b9
PMF
960 }
961 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 962
d0b5f2b9 963 DBG("trace destroy");
aafb1650 964
31d392f1 965 result = ltt_trace_destroy(trace_name, 0);
d0b5f2b9
PMF
966 if(result < 0) {
967 ERR("ltt_trace_destroy failed");
872037bb 968 return (void *)1;
fbd8191b 969 }
98963de4 970 }
b02e31e5 971 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
204141ee 972 do_cmd_get_shmid(recvbuf, &src);
811e4b93
PMF
973 }
974 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
204141ee 975 do_cmd_get_n_subbufs(recvbuf, &src);
811e4b93
PMF
976 }
977 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
204141ee 978 do_cmd_get_subbuf_size(recvbuf, &src);
3847c3ba 979 }
b02e31e5
PMF
980 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
981 char *libfile;
982
983 libfile = nth_token(recvbuf, 1);
984
985 DBG("load_probe_lib loading %s", libfile);
204141ee
PMF
986
987 free(libfile);
b02e31e5 988 }
688760ef 989 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
204141ee 990 do_cmd_get_subbuffer(recvbuf, &src);
688760ef
PMF
991 }
992 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
204141ee 993 do_cmd_put_subbuffer(recvbuf, &src);
688760ef 994 }
763f41e5
DS
995 else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) {
996 do_cmd_set_subbuf_size(recvbuf, &src);
997 }
998 else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) {
999 do_cmd_set_subbuf_num(recvbuf, &src);
1000 }
52c51a47
PMF
1001 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
1002 char *channel_slash_name = nth_token(recvbuf, 1);
1003 char channel_name[256]="";
1004 char marker_name[256]="";
52c51a47
PMF
1005
1006 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
1007
1008 if(channel_name == NULL || marker_name == NULL) {
1009 WARN("invalid marker name");
1010 goto next_cmd;
1011 }
52c51a47
PMF
1012
1013 result = ltt_marker_connect(channel_name, marker_name, "default");
1014 if(result < 0) {
1015 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
1016 }
1017 }
1018 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
1019 char *channel_slash_name = nth_token(recvbuf, 1);
1020 char *marker_name;
1021 char *channel_name;
52c51a47
PMF
1022
1023 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
1024
1025 if(marker_name == NULL) {
1026 }
52c51a47
PMF
1027
1028 result = ltt_marker_disconnect(channel_name, marker_name, "default");
1029 if(result < 0) {
1030 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
1031 }
1032 }
ed1317e7
PMF
1033 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
1034 char *reply;
1035
1036 asprintf(&reply, "%lld", pidunique);
1037
1038 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
1039 if(result) {
1040 ERR("listener: get_pidunique: ustcomm_send_reply failed");
1041 goto next_cmd;
1042 }
1043
1044 free(reply);
1045 }
3a7b90de 1046// else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
b73a4c47 1047// struct ust_trace *trace;
3a7b90de
PMF
1048// char trace_name[] = "auto";
1049// int i;
1050// char *channel_name;
1051//
1052// DBG("get_notifications");
1053//
1054// channel_name = strdup_malloc(nth_token(recvbuf, 1));
1055// if(channel_name == NULL) {
1056// ERR("put_subbuf_size: cannot parse channel");
1057// goto next_cmd;
1058// }
1059//
1060// ltt_lock_traces();
1061// trace = _ltt_trace_find(trace_name);
1062// ltt_unlock_traces();
1063//
1064// if(trace == NULL) {
63fe14e5 1065// ERR("cannot find trace!");
872037bb 1066// return (void *)1;
3a7b90de
PMF
1067// }
1068//
1069// for(i=0; i<trace->nr_channels; i++) {
1070// struct rchan *rchan = trace->channels[i].trans_channel_data;
1071// int fd;
1072//
1073// if(!strcmp(trace->channels[i].channel_name, channel_name)) {
1074// struct rchan_buf *rbuf = rchan->buf;
1075// struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
1076//
1077// result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
1078// if(result == -1) {
1079// ERR("ustcomm_app_detach_client failed");
1080// goto next_cmd;
1081// }
1082//
1083// lttbuf->wake_consumer_arg = (void *) fd;
1084//
1085// smp_wmb();
1086//
1087// lttbuf->call_wake_consumer = 1;
1088//
1089// break;
1090// }
1091// }
1092//
1093// free(channel_name);
1094// }
688760ef
PMF
1095 else {
1096 ERR("unable to parse message: %s", recvbuf);
1097 }
d0b5f2b9 1098
811e4b93 1099 next_cmd:
d0b5f2b9 1100 free(recvbuf);
98963de4
PMF
1101 }
1102}
1103
7958de40 1104volatile sig_atomic_t have_listener = 0;
4440ebcb 1105
98963de4
PMF
1106void create_listener(void)
1107{
9160b4e4 1108#ifdef USE_CLONE
98963de4 1109 static char listener_stack[16384];
c5fdc888 1110 int result;
4440ebcb
PMF
1111#else
1112 pthread_t thread;
9160b4e4 1113#endif
98963de4 1114
c5fdc888
PMF
1115 if(have_listener) {
1116 WARN("not creating listener because we already had one");
4440ebcb 1117 return;
c5fdc888 1118 }
4440ebcb 1119
3847c3ba 1120#ifdef USE_CLONE
c5fdc888 1121 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
1122 if(result == -1) {
1123 perror("clone");
4440ebcb 1124 return;
98963de4 1125 }
3847c3ba 1126#else
b0540e11 1127
3847c3ba
PMF
1128 pthread_create(&thread, NULL, listener_main, NULL);
1129#endif
4440ebcb
PMF
1130
1131 have_listener = 1;
98963de4
PMF
1132}
1133
98963de4 1134static int init_socket(void)
68c1021b 1135{
d0b5f2b9 1136 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
1137}
1138
5de74e51
PMF
1139#define AUTOPROBE_DISABLED 0
1140#define AUTOPROBE_ENABLE_ALL 1
1141#define AUTOPROBE_ENABLE_REGEX 2
1142static int autoprobe_method = AUTOPROBE_DISABLED;
1143static regex_t autoprobe_regex;
ef290fca 1144
20b37a31 1145static void auto_probe_connect(struct marker *m)
68c1021b
PMF
1146{
1147 int result;
1148
5de74e51
PMF
1149 char* concat_name = NULL;
1150 const char *probe_name = "default";
ef290fca 1151
5de74e51 1152 if(autoprobe_method == AUTOPROBE_DISABLED) {
ef290fca
PMF
1153 return;
1154 }
5de74e51
PMF
1155 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1156 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1157 if(result == -1) {
1158 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1159 m->channel, m->name);
1160 return;
1161 }
1162 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1163 free(concat_name);
1164 return;
1165 }
1166 free(concat_name);
ef290fca
PMF
1167 }
1168
5de74e51 1169 result = ltt_marker_connect(m->channel, m->name, probe_name);
acbf228b
PMF
1170 if(result && result != -EEXIST)
1171 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
20b37a31 1172
3ea1e2fc 1173 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
ef290fca 1174
20b37a31
PMF
1175}
1176
c1083aa8 1177static void __attribute__((constructor)) init()
20b37a31
PMF
1178{
1179 int result;
5de74e51 1180 char* autoprobe_val = NULL;
20b37a31 1181
ed1317e7
PMF
1182 /* Assign the pidunique, to be able to differentiate the processes with same
1183 * pid, (before and after an exec).
1184 */
1185 pidunique = make_pidunique();
1186
5de74e51 1187 DBG("Tracectl constructor");
20b37a31 1188
3847c3ba
PMF
1189 result = init_socket();
1190 if(result == -1) {
1191 ERR("init_socket error");
1192 return;
1193 }
2944a629
PMF
1194
1195 create_listener();
68c1021b 1196
5de74e51
PMF
1197 autoprobe_val = getenv("UST_AUTOPROBE");
1198 if(autoprobe_val) {
4440ebcb
PMF
1199 struct marker_iter iter;
1200
08230db7 1201 DBG("Autoprobe enabled.");
4440ebcb
PMF
1202
1203 /* Ensure markers are initialized */
1204 //init_markers();
1205
1206 /* Ensure marker control is initialized, for the probe */
1207 init_marker_control();
1208
1209 /* first, set the callback that will connect the
1210 * probe on new markers
1211 */
5de74e51
PMF
1212 if(autoprobe_val[0] == '/') {
1213 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1214 if (result) {
1215 char regexerr[150];
1216
1217 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1218 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1219 /* don't crash the application just for this */
1220 }
1221 else {
1222 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1223 }
ef290fca 1224 }
5de74e51
PMF
1225 else {
1226 /* just enable all instrumentation */
1227 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1228 }
1229
1230 marker_set_new_marker_cb(auto_probe_connect);
1231
4440ebcb
PMF
1232 /* Now, connect the probes that were already registered. */
1233 marker_iter_reset(&iter);
1234 marker_iter_start(&iter);
1235
08230db7 1236 DBG("now iterating on markers already registered");
4440ebcb 1237 while(iter.marker) {
08230db7 1238 DBG("now iterating on marker %s", iter.marker->name);
4440ebcb
PMF
1239 auto_probe_connect(iter.marker);
1240 marker_iter_next(&iter);
1241 }
1242 }
1243
4db647c5
PMF
1244 if(getenv("UST_TRACE")) {
1245 char trace_name[] = "auto";
1246 char trace_type[] = "ustrelay";
1247
1248 DBG("starting early tracing");
1249
1250 /* Ensure marker control is initialized */
1251 init_marker_control();
1252
4db647c5
PMF
1253 /* Ensure markers are initialized */
1254 init_markers();
1255
e17571a5
PMF
1256 /* Ensure buffers are initialized, for the transport to be available.
1257 * We are about to set a trace type and it will fail without this.
1258 */
1259 init_ustrelay_transport();
1260
027ffc9d
PMF
1261 /* FIXME: When starting early tracing (here), depending on the
1262 * order of constructors, it is very well possible some marker
1263 * sections are not yet registered. Because of this, some
1264 * channels may not be registered. Yet, we are about to ask the
1265 * daemon to collect the channels. Channels which are not yet
1266 * registered will not be collected.
1267 *
1268 * Currently, in LTTng, there is no way to add a channel after
1269 * trace start. The reason for this is that it induces complex
1270 * concurrency issues on the trace structures, which can only
1271 * be resolved using RCU. This has not been done yet. As a
1272 * workaround, we are forcing the registration of the "ust"
1273 * channel here. This is the only channel (apart from metadata)
1274 * that can be reliably used in early tracing.
1275 *
1276 * Non-early tracing does not have this problem and can use
1277 * arbitrary channel names.
1278 */
20b37a31 1279 ltt_channels_register("ust");
4db647c5
PMF
1280
1281 result = ltt_trace_setup(trace_name);
1282 if(result < 0) {
1283 ERR("ltt_trace_setup failed");
1284 return;
1285 }
1286
1287 result = ltt_trace_set_type(trace_name, trace_type);
1288 if(result < 0) {
1289 ERR("ltt_trace_set_type failed");
1290 return;
1291 }
1292
1293 result = ltt_trace_alloc(trace_name);
1294 if(result < 0) {
1295 ERR("ltt_trace_alloc failed");
1296 return;
1297 }
1298
1299 result = ltt_trace_start(trace_name);
1300 if(result < 0) {
1301 ERR("ltt_trace_start failed");
1302 return;
1303 }
60e57148
PMF
1304
1305 /* Do this after the trace is started in order to avoid creating confusion
1306 * if the trace fails to start. */
1307 inform_consumer_daemon(trace_name);
4db647c5
PMF
1308 }
1309
68c1021b
PMF
1310
1311 return;
1312
1313 /* should decrementally destroy stuff if error */
1314
1315}
1316
1317/* This is only called if we terminate normally, not with an unhandled signal,
6d45c11a
PMF
1318 * so we cannot rely on it. However, for now, LTTV requires that the header of
1319 * the last sub-buffer contain a valid end time for the trace. This is done
1320 * automatically only when the trace is properly stopped.
1321 *
1322 * If the traced program crashed, it is always possible to manually add the
1323 * right value in the header, or to open the trace in text mode.
1324 *
1325 * FIXME: Fix LTTV so it doesn't need this.
1326 */
68c1021b 1327
6d45c11a 1328static void destroy_traces(void)
68c1021b 1329{
6d45c11a 1330 int result;
a584bc4e
PMF
1331
1332 /* if trace running, finish it */
1333
6d45c11a 1334 DBG("destructor stopping traces");
a584bc4e 1335
6d45c11a
PMF
1336 result = ltt_trace_stop("auto");
1337 if(result == -1) {
1338 ERR("ltt_trace_stop error");
1339 }
1340
31d392f1 1341 result = ltt_trace_destroy("auto", 0);
6d45c11a
PMF
1342 if(result == -1) {
1343 ERR("ltt_trace_destroy error");
1344 }
68c1021b 1345}
1e2944cb 1346
97d9b88b
PMF
1347static int trace_recording(void)
1348{
1349 int retval = 0;
b73a4c47 1350 struct ust_trace *trace;
97d9b88b
PMF
1351
1352 ltt_lock_traces();
1353
1354 list_for_each_entry(trace, &ltt_traces.head, list) {
1355 if(trace->active) {
1356 retval = 1;
1357 break;
1358 }
1359 }
1360
1361 ltt_unlock_traces();
1362
1363 return retval;
1364}
1365
f293009f 1366#if 0
97d9b88b
PMF
1367static int have_consumer(void)
1368{
1369 return !list_empty(&blocked_consumers);
1370}
f293009f 1371#endif
97d9b88b 1372
f293009f 1373int restarting_usleep(useconds_t usecs)
97d9b88b
PMF
1374{
1375 struct timespec tv;
1376 int result;
1377
f293009f
PMF
1378 tv.tv_sec = 0;
1379 tv.tv_nsec = usecs * 1000;
97d9b88b
PMF
1380
1381 do {
1382 result = nanosleep(&tv, &tv);
1383 } while(result == -1 && errno == EINTR);
1384
1385 return result;
1386}
1387
f293009f
PMF
1388/* This destructor keeps the process alive for a few seconds in order
1389 * to leave time to ustd to connect to its buffers. This is necessary
1390 * for programs whose execution is very short. It is also useful in all
1391 * programs when tracing is started close to the end of the program
1392 * execution.
1393 *
1394 * FIXME: For now, this only works for the first trace created in a
1395 * process.
1396 */
1397
97d9b88b
PMF
1398static void __attribute__((destructor)) keepalive()
1399{
f293009f 1400 if(trace_recording() && buffers_to_export) {
c472cce0 1401 int total = 0;
f293009f
PMF
1402 DBG("Keeping process alive for consumer daemon...");
1403 while(buffers_to_export) {
1404 const int interv = 200000;
c472cce0 1405 restarting_usleep(interv);
f293009f
PMF
1406 total += interv;
1407
1408 if(total >= 3000000) {
c472cce0 1409 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
f293009f
PMF
1410 break;
1411 }
1412 }
1413 DBG("Finally dying...");
1414 }
6d45c11a
PMF
1415
1416 destroy_traces();
1417
2a79ceeb 1418 ustcomm_fini_app(&ustcomm_app, 0);
97d9b88b 1419}
97d9b88b 1420
775c8a3f 1421void ust_potential_exec(void)
c396a841
PMF
1422{
1423 trace_mark(ust, potential_exec, MARK_NOARGS);
1424
775c8a3f
PMF
1425 DBG("test");
1426
c396a841
PMF
1427 keepalive();
1428}
1429
1e2944cb
PMF
1430/* Notify ust that there was a fork. This needs to be called inside
1431 * the new process, anytime a process whose memory is not shared with
1432 * the parent is created. If this function is not called, the events
1433 * of the new process will not be collected.
616ed36a
PMF
1434 *
1435 * Signals should be disabled before the fork and reenabled only after
1436 * this call in order to guarantee tracing is not started before ust_fork()
1437 * sanitizes the new process.
1e2944cb
PMF
1438 */
1439
616ed36a 1440static void ust_fork(void)
1e2944cb 1441{
99b72dc0
PMF
1442 struct blocked_consumer *bc;
1443 struct blocked_consumer *deletable_bc = NULL;
1444 int result;
1445
31d392f1 1446 /* FIXME: technically, the locks could have been taken before the fork */
1e2944cb 1447 DBG("ust: forking");
73850001
PMF
1448
1449 /* break lock if necessary */
1450 ltt_unlock_traces();
1451
1e2944cb 1452 ltt_trace_stop("auto");
31d392f1 1453 ltt_trace_destroy("auto", 1);
99b72dc0
PMF
1454 /* Delete all active connections */
1455 ustcomm_close_all_connections(&ustcomm_app.server);
1456
1457 /* Delete all blocked consumers */
1458 list_for_each_entry(bc, &blocked_consumers, list) {
d9ce395d
PMF
1459 close(bc->fd_producer);
1460 close(bc->fd_consumer);
99b72dc0
PMF
1461 free(deletable_bc);
1462 deletable_bc = bc;
1463 list_del(&bc->list);
1464 }
1465
2a79ceeb
PMF
1466 /* free app, keeping socket file */
1467 ustcomm_fini_app(&ustcomm_app, 1);
393bc391 1468
cedc0155 1469 buffers_to_export = 0;
1e2944cb 1470 have_listener = 0;
99b72dc0 1471 init_socket();
9fb49d0e 1472 create_listener();
99b72dc0
PMF
1473 ltt_trace_setup("auto");
1474 result = ltt_trace_set_type("auto", "ustrelay");
1475 if(result < 0) {
1476 ERR("ltt_trace_set_type failed");
036db133 1477 return;
99b72dc0
PMF
1478 }
1479
1480 ltt_trace_alloc("auto");
1481 ltt_trace_start("auto");
ad45e833 1482 inform_consumer_daemon("auto");
1e2944cb
PMF
1483}
1484
616ed36a
PMF
1485void ust_before_fork(ust_fork_info_t *fork_info)
1486{
1487 /* Disable signals. This is to avoid that the child
1488 * intervenes before it is properly setup for tracing. It is
1489 * safer to disable all signals, because then we know we are not
1490 * breaking anything by restoring the original mask.
1491 */
1492 sigset_t all_sigs;
1493 int result;
1494
1495 /* FIXME:
1496 - only do this if tracing is active
1497 */
1498
1499 /* Disable signals */
1500 sigfillset(&all_sigs);
1501 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1502 if(result == -1) {
1503 PERROR("sigprocmask");
1504 return;
1505 }
1506}
1507
1508/* Don't call this function directly in a traced program */
1509static void ust_after_fork_common(ust_fork_info_t *fork_info)
1510{
1511 int result;
616ed36a
PMF
1512
1513 /* Restore signals */
1514 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1515 if(result == -1) {
1516 PERROR("sigprocmask");
1517 return;
1518 }
1519}
1520
1521void ust_after_fork_parent(ust_fork_info_t *fork_info)
1522{
1523 /* Reenable signals */
1524 ust_after_fork_common(fork_info);
1525}
1526
1527void ust_after_fork_child(ust_fork_info_t *fork_info)
1528{
1529 /* First sanitize the child */
1530 ust_fork();
1531
1532 /* Then reenable interrupts */
1533 ust_after_fork_common(fork_info);
1534}
1535
This page took 0.483892 seconds and 4 git commands to generate.