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