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