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