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