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