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