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