Refactor libinterfork and add support for clone() interception
[ust.git] / libust / tracectl.c
CommitLineData
c39c72ee
PMF
1/* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
872037bb 18#define _GNU_SOURCE
68c1021b
PMF
19#include <stdio.h>
20#include <stdint.h>
21#include <signal.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/un.h>
98963de4 25#include <sched.h>
a584bc4e 26#include <fcntl.h>
3a7b90de 27#include <poll.h>
ef290fca 28#include <regex.h>
fbd8191b 29
b7ea1a1c 30#include <urcu-bp.h>
26cc7017 31
93d0f2ea 32#include <ust/marker.h>
616ed36a 33#include <ust/tracectl.h>
c93858f1 34#include "tracer.h"
6af64c43 35#include "usterr.h"
d0b5f2b9 36#include "ustcomm.h"
b5b073e2 37#include "buffers.h" /* FIXME: remove */
9160b4e4 38#include "marker-control.h"
fbd8191b 39
b02e31e5 40//#define USE_CLONE
3847c3ba 41
68c1021b
PMF
42#define USTSIGNAL SIGIO
43
98963de4
PMF
44#define MAX_MSG_SIZE (100)
45#define MSG_NOTIF 1
46#define MSG_REGISTER_NOTIF 2
47
a584bc4e
PMF
48char consumer_stack[10000];
49
ed1317e7
PMF
50/* This should only be accessed by the constructor, before the creation
51 * of the listener, and then only by the listener.
52 */
53s64 pidunique = -1LL;
54
3a7b90de
PMF
55struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers);
56
d0b5f2b9
PMF
57static struct ustcomm_app ustcomm_app;
58
68c1021b
PMF
59struct tracecmd { /* no padding */
60 uint32_t size;
61 uint16_t command;
62};
63
f293009f
PMF
64/* volatile because shared between the listener and the main thread */
65volatile sig_atomic_t buffers_to_export = 0;
66
98963de4
PMF
67struct trctl_msg {
68 /* size: the size of all the fields except size itself */
69 uint32_t size;
70 uint16_t type;
71 /* Only the necessary part of the payload is transferred. It
72 * may even be none of it.
73 */
74 char payload[94];
75};
68c1021b 76
a584bc4e
PMF
77struct consumer_channel {
78 int fd;
79 struct ltt_channel_struct *chan;
80};
81
3a7b90de
PMF
82struct blocked_consumer {
83 int fd_consumer;
84 int fd_producer;
85 int tmp_poll_idx;
86
87 /* args to ustcomm_send_reply */
88 struct ustcomm_server server;
89 struct ustcomm_source src;
90
b5b073e2
PMF
91 /* args to ust_buffers_do_get_subbuf */
92 struct ust_buffer *buf;
3a7b90de
PMF
93
94 struct list_head list;
95};
96
ed1317e7
PMF
97static long long make_pidunique(void)
98{
99 s64 retval;
100 struct timeval tv;
101
102 gettimeofday(&tv, NULL);
103
104 retval = tv.tv_sec;
105 retval <<= 32;
106 retval |= tv.tv_usec;
107
108 return retval;
109}
110
52c51a47 111static void print_markers(FILE *fp)
fbd8191b
PMF
112{
113 struct marker_iter iter;
114
d0b5f2b9 115 lock_markers();
fbd8191b
PMF
116 marker_iter_reset(&iter);
117 marker_iter_start(&iter);
118
119 while(iter.marker) {
3ea1e2fc 120 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
121 marker_iter_next(&iter);
122 }
d0b5f2b9 123 unlock_markers();
fbd8191b
PMF
124}
125
4440ebcb 126static int init_socket(void);
68c1021b 127
26cc7017
PMF
128/* This needs to be called whenever a new thread is created. It notifies
129 * liburcu of the new thread.
130 */
131
132void ust_register_thread(void)
68c1021b 133{
26cc7017 134 rcu_register_thread();
68c1021b
PMF
135}
136
98963de4
PMF
137int fd_notif = -1;
138void notif_cb(void)
139{
140 int result;
141 struct trctl_msg msg;
142
143 /* FIXME: fd_notif should probably be protected by a spinlock */
144
145 if(fd_notif == -1)
146 return;
147
148 msg.type = MSG_NOTIF;
149 msg.size = sizeof(msg.type);
150
151 /* FIXME: don't block here */
152 result = write(fd_notif, &msg, msg.size+sizeof(msg.size));
153 if(result == -1) {
154 PERROR("write");
155 return;
156 }
157}
158
ad45e833
PMF
159/* Ask the daemon to collect a trace called trace_name and being
160 * produced by this pid.
161 *
162 * The trace must be at least allocated. (It can also be started.)
163 * This is because _ltt_trace_find is used.
164 */
165
166static void inform_consumer_daemon(const char *trace_name)
d0b5f2b9 167{
ad45e833
PMF
168 int i;
169 struct ltt_trace_struct *trace;
170 pid_t pid = getpid();
171 int result;
172
173 ltt_lock_traces();
174
175 trace = _ltt_trace_find(trace_name);
176 if(trace == NULL) {
177 WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
178 goto finish;
179 }
180
181 for(i=0; i < trace->nr_channels; i++) {
182 result = ustcomm_request_consumer(pid, trace->channels[i].channel_name);
183 if(result == -1) {
184 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
185 /* continue even if fail */
186 }
f293009f 187 buffers_to_export++;
ad45e833
PMF
188 }
189
190 finish:
191 ltt_unlock_traces();
d0b5f2b9 192}
fbd8191b 193
3a7b90de
PMF
194void process_blocked_consumers(void)
195{
196 int n_fds = 0;
197 struct pollfd *fds;
198 struct blocked_consumer *bc;
199 int idx = 0;
200 char inbuf;
201 int result;
202
203 list_for_each_entry(bc, &blocked_consumers, list) {
204 n_fds++;
205 }
206
207 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
208 if(fds == NULL) {
209 ERR("malloc returned NULL");
210 return;
211 }
212
213 list_for_each_entry(bc, &blocked_consumers, list) {
214 fds[idx].fd = bc->fd_producer;
215 fds[idx].events = POLLIN;
216 bc->tmp_poll_idx = idx;
217 idx++;
218 }
219
69ba0156
PMF
220 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
221 /* nothing */;
3a7b90de
PMF
222 if(result == -1) {
223 PERROR("poll");
872037bb 224 return;
3a7b90de
PMF
225 }
226
227 list_for_each_entry(bc, &blocked_consumers, list) {
228 if(fds[bc->tmp_poll_idx].revents) {
229 long consumed_old = 0;
230 char *reply;
231
232 result = read(bc->fd_producer, &inbuf, 1);
233 if(result == -1) {
234 PERROR("read");
235 continue;
236 }
237 if(result == 0) {
238 DBG("PRODUCER END");
239
240 close(bc->fd_producer);
241
769d0157 242 list_del(&bc->list);
3a7b90de
PMF
243
244 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
245 if(result < 0) {
246 ERR("ustcomm_send_reply failed");
247 continue;
248 }
249
250 continue;
251 }
252
b5b073e2 253 result = ust_buffers_do_get_subbuf(bc->buf, &consumed_old);
3a7b90de
PMF
254 if(result == -EAGAIN) {
255 WARN("missed buffer?");
256 continue;
257 }
258 else if(result < 0) {
b5b073e2 259 DBG("ust_buffers_do_get_subbuf: error: %s", strerror(-result));
3a7b90de
PMF
260 }
261 asprintf(&reply, "%s %ld", "OK", consumed_old);
262 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
263 if(result < 0) {
264 ERR("ustcomm_send_reply failed");
265 free(reply);
266 continue;
267 }
268 free(reply);
269
769d0157 270 list_del(&bc->list);
3a7b90de
PMF
271 }
272 }
273
274}
275
872037bb 276void *listener_main(void *p)
98963de4
PMF
277{
278 int result;
279
26cc7017
PMF
280 ust_register_thread();
281
b0540e11
PMF
282 DBG("LISTENER");
283
98963de4 284 for(;;) {
aafb1650
PMF
285 char trace_name[] = "auto";
286 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
287 char *recvbuf;
288 int len;
b02e31e5 289 struct ustcomm_source src;
98963de4 290
3a7b90de
PMF
291 process_blocked_consumers();
292
293 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
294 if(result < 0) {
d0b5f2b9
PMF
295 WARN("error in ustcomm_app_recv_message");
296 continue;
297 }
3a7b90de
PMF
298 else if(result == 0) {
299 /* no message */
300 continue;
301 }
98963de4 302
08230db7 303 DBG("received a message! it's: %s", recvbuf);
d0b5f2b9 304 len = strlen(recvbuf);
98963de4 305
d0b5f2b9 306 if(!strcmp(recvbuf, "print_markers")) {
52c51a47
PMF
307 print_markers(stderr);
308 }
309 else if(!strcmp(recvbuf, "list_markers")) {
310 char *ptr;
311 size_t size;
312 FILE *fp;
313
314 fp = open_memstream(&ptr, &size);
315 print_markers(fp);
316 fclose(fp);
317
318 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
319
320 free(ptr);
321 }
322 else if(!strcmp(recvbuf, "start")) {
323 /* start is an operation that setups the trace, allocates it and starts it */
324 result = ltt_trace_setup(trace_name);
325 if(result < 0) {
326 ERR("ltt_trace_setup failed");
872037bb 327 return (void *)1;
52c51a47
PMF
328 }
329
330 result = ltt_trace_set_type(trace_name, trace_type);
331 if(result < 0) {
332 ERR("ltt_trace_set_type failed");
872037bb 333 return (void *)1;
52c51a47
PMF
334 }
335
336 result = ltt_trace_alloc(trace_name);
337 if(result < 0) {
338 ERR("ltt_trace_alloc failed");
872037bb 339 return (void *)1;
52c51a47
PMF
340 }
341
ad45e833 342 inform_consumer_daemon(trace_name);
52c51a47
PMF
343
344 result = ltt_trace_start(trace_name);
345 if(result < 0) {
346 ERR("ltt_trace_start failed");
347 continue;
348 }
d0b5f2b9
PMF
349 }
350 else if(!strcmp(recvbuf, "trace_setup")) {
351 DBG("trace setup");
fbd8191b 352
d0b5f2b9
PMF
353 result = ltt_trace_setup(trace_name);
354 if(result < 0) {
355 ERR("ltt_trace_setup failed");
872037bb 356 return (void *)1;
fbd8191b 357 }
d0b5f2b9
PMF
358
359 result = ltt_trace_set_type(trace_name, trace_type);
360 if(result < 0) {
361 ERR("ltt_trace_set_type failed");
872037bb 362 return (void *)1;
fbd8191b 363 }
d0b5f2b9
PMF
364 }
365 else if(!strcmp(recvbuf, "trace_alloc")) {
366 DBG("trace alloc");
367
368 result = ltt_trace_alloc(trace_name);
369 if(result < 0) {
370 ERR("ltt_trace_alloc failed");
872037bb 371 return (void *)1;
fbd8191b 372 }
d0b5f2b9 373 }
62ec620f
PMF
374 else if(!strcmp(recvbuf, "trace_create")) {
375 DBG("trace create");
376
377 result = ltt_trace_setup(trace_name);
378 if(result < 0) {
379 ERR("ltt_trace_setup failed");
380 return (void *)1;
381 }
382
383 result = ltt_trace_set_type(trace_name, trace_type);
384 if(result < 0) {
385 ERR("ltt_trace_set_type failed");
386 return (void *)1;
387 }
388
389 result = ltt_trace_alloc(trace_name);
390 if(result < 0) {
391 ERR("ltt_trace_alloc failed");
392 return (void *)1;
393 }
394
395 inform_consumer_daemon(trace_name);
396 }
d0b5f2b9
PMF
397 else if(!strcmp(recvbuf, "trace_start")) {
398 DBG("trace start");
399
400 result = ltt_trace_start(trace_name);
401 if(result < 0) {
402 ERR("ltt_trace_start failed");
403 continue;
fbd8191b 404 }
d0b5f2b9
PMF
405 }
406 else if(!strcmp(recvbuf, "trace_stop")) {
407 DBG("trace stop");
408
409 result = ltt_trace_stop(trace_name);
410 if(result < 0) {
411 ERR("ltt_trace_stop failed");
872037bb 412 return (void *)1;
aafb1650 413 }
d0b5f2b9
PMF
414 }
415 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 416
d0b5f2b9 417 DBG("trace destroy");
aafb1650 418
d0b5f2b9
PMF
419 result = ltt_trace_destroy(trace_name);
420 if(result < 0) {
421 ERR("ltt_trace_destroy failed");
872037bb 422 return (void *)1;
fbd8191b 423 }
98963de4 424 }
b02e31e5 425 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
3847c3ba
PMF
426 struct ltt_trace_struct *trace;
427 char trace_name[] = "auto";
428 int i;
811e4b93 429 char *channel_name;
3847c3ba
PMF
430
431 DBG("get_shmid");
432
811e4b93
PMF
433 channel_name = nth_token(recvbuf, 1);
434 if(channel_name == NULL) {
435 ERR("get_shmid: cannot parse channel");
436 goto next_cmd;
437 }
438
3847c3ba
PMF
439 ltt_lock_traces();
440 trace = _ltt_trace_find(trace_name);
441 ltt_unlock_traces();
442
443 if(trace == NULL) {
63fe14e5 444 ERR("cannot find trace!");
872037bb 445 return (void *)1;
3847c3ba
PMF
446 }
447
448 for(i=0; i<trace->nr_channels; i++) {
b5b073e2
PMF
449 struct ust_channel *channel = &trace->channels[i];
450 struct ust_buffer *buf = channel->buf;
3847c3ba 451
811e4b93
PMF
452 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
453 char *reply;
454
b5b073e2
PMF
455 DBG("the shmid for the requested channel is %d", buf->shmid);
456 DBG("the shmid for its buffer structure is %d", channel->buf_shmid);
457 asprintf(&reply, "%d %d", buf->shmid, channel->buf_shmid);
811e4b93
PMF
458
459 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
460 if(result) {
461 ERR("listener: get_shmid: ustcomm_send_reply failed");
462 goto next_cmd;
463 }
464
465 free(reply);
466
467 break;
468 }
469 }
f293009f
PMF
470
471 buffers_to_export--;
811e4b93
PMF
472 }
473 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
474 struct ltt_trace_struct *trace;
475 char trace_name[] = "auto";
476 int i;
477 char *channel_name;
478
479 DBG("get_n_subbufs");
480
481 channel_name = nth_token(recvbuf, 1);
482 if(channel_name == NULL) {
483 ERR("get_n_subbufs: cannot parse channel");
484 goto next_cmd;
485 }
486
487 ltt_lock_traces();
488 trace = _ltt_trace_find(trace_name);
489 ltt_unlock_traces();
490
491 if(trace == NULL) {
63fe14e5 492 ERR("cannot find trace!");
872037bb 493 return (void *)1;
811e4b93
PMF
494 }
495
496 for(i=0; i<trace->nr_channels; i++) {
b5b073e2 497 struct ust_channel *channel = &trace->channels[i];
811e4b93
PMF
498
499 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
500 char *reply;
501
b5b073e2
PMF
502 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
503 asprintf(&reply, "%d", channel->subbuf_cnt);
811e4b93
PMF
504
505 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
506 if(result) {
507 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
508 goto next_cmd;
509 }
510
511 free(reply);
512
513 break;
514 }
515 }
516 }
517 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
518 struct ltt_trace_struct *trace;
519 char trace_name[] = "auto";
520 int i;
521 char *channel_name;
522
523 DBG("get_subbuf_size");
524
525 channel_name = nth_token(recvbuf, 1);
526 if(channel_name == NULL) {
527 ERR("get_subbuf_size: cannot parse channel");
528 goto next_cmd;
529 }
530
531 ltt_lock_traces();
532 trace = _ltt_trace_find(trace_name);
533 ltt_unlock_traces();
534
535 if(trace == NULL) {
63fe14e5 536 ERR("cannot find trace!");
872037bb 537 return (void *)1;
811e4b93
PMF
538 }
539
540 for(i=0; i<trace->nr_channels; i++) {
b5b073e2 541 struct ust_channel *channel = &trace->channels[i];
811e4b93
PMF
542
543 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
544 char *reply;
545
b5b073e2
PMF
546 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
547 asprintf(&reply, "%zd", channel->subbuf_size);
811e4b93
PMF
548
549 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
550 if(result) {
551 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
552 goto next_cmd;
553 }
554
555 free(reply);
3847c3ba 556
811e4b93
PMF
557 break;
558 }
3847c3ba
PMF
559 }
560 }
b02e31e5
PMF
561 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
562 char *libfile;
563
564 libfile = nth_token(recvbuf, 1);
565
566 DBG("load_probe_lib loading %s", libfile);
567 }
688760ef
PMF
568 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
569 struct ltt_trace_struct *trace;
570 char trace_name[] = "auto";
571 int i;
572 char *channel_name;
573
574 DBG("get_subbuf");
575
576 channel_name = nth_token(recvbuf, 1);
577 if(channel_name == NULL) {
578 ERR("get_subbuf: cannot parse channel");
579 goto next_cmd;
580 }
581
582 ltt_lock_traces();
583 trace = _ltt_trace_find(trace_name);
584 ltt_unlock_traces();
585
586 if(trace == NULL) {
63fe14e5 587 ERR("cannot find trace!");
872037bb 588 return (void *)1;
688760ef
PMF
589 }
590
591 for(i=0; i<trace->nr_channels; i++) {
b5b073e2 592 struct ust_channel *channel = &trace->channels[i];
688760ef
PMF
593
594 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
b5b073e2 595 struct ust_buffer *buf = channel->buf;
3a7b90de 596 struct blocked_consumer *bc;
688760ef 597
3a7b90de
PMF
598 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
599 if(bc == NULL) {
600 ERR("malloc returned NULL");
688760ef
PMF
601 goto next_cmd;
602 }
3a7b90de 603 bc->fd_consumer = src.fd;
b5b073e2
PMF
604 bc->fd_producer = buf->data_ready_fd_read;
605 bc->buf = buf;
3a7b90de
PMF
606 bc->src = src;
607 bc->server = ustcomm_app.server;
688760ef 608
3a7b90de 609 list_add(&bc->list, &blocked_consumers);
688760ef
PMF
610
611 break;
612 }
613 }
614 }
615 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
616 struct ltt_trace_struct *trace;
617 char trace_name[] = "auto";
618 int i;
619 char *channel_name;
620 long consumed_old;
621 char *consumed_old_str;
622 char *endptr;
623
624 DBG("put_subbuf");
625
626 channel_name = strdup_malloc(nth_token(recvbuf, 1));
627 if(channel_name == NULL) {
628 ERR("put_subbuf_size: cannot parse channel");
629 goto next_cmd;
630 }
631
632 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
633 if(consumed_old_str == NULL) {
634 ERR("put_subbuf: cannot parse consumed_old");
635 goto next_cmd;
636 }
637 consumed_old = strtol(consumed_old_str, &endptr, 10);
638 if(*endptr != '\0') {
639 ERR("put_subbuf: invalid value for consumed_old");
640 goto next_cmd;
641 }
642
643 ltt_lock_traces();
644 trace = _ltt_trace_find(trace_name);
645 ltt_unlock_traces();
646
647 if(trace == NULL) {
63fe14e5 648 ERR("cannot find trace!");
872037bb 649 return (void *)1;
688760ef
PMF
650 }
651
652 for(i=0; i<trace->nr_channels; i++) {
b5b073e2 653 struct ust_channel *channel = &trace->channels[i];
688760ef
PMF
654
655 if(!strcmp(trace->channels[i].channel_name, channel_name)) {
b5b073e2 656 struct ust_buffer *buf = channel->buf;
688760ef
PMF
657 char *reply;
658 long consumed_old=0;
659
b5b073e2 660 result = ust_buffers_do_put_subbuf(buf, consumed_old);
688760ef 661 if(result < 0) {
b5b073e2 662 WARN("ust_buffers_do_put_subbuf: error (subbuf=%s)", channel_name);
872037bb 663 asprintf(&reply, "%s", "ERROR");
688760ef
PMF
664 }
665 else {
b5b073e2 666 DBG("ust_buffers_do_put_subbuf: success (subbuf=%s)", channel_name);
872037bb 667 asprintf(&reply, "%s", "OK");
688760ef 668 }
688760ef
PMF
669
670 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
671 if(result) {
672 ERR("listener: put_subbuf: ustcomm_send_reply failed");
673 goto next_cmd;
674 }
675
676 free(reply);
677
678 break;
679 }
680 }
681
682 free(channel_name);
683 free(consumed_old_str);
684 }
52c51a47
PMF
685 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
686 char *channel_slash_name = nth_token(recvbuf, 1);
687 char channel_name[256]="";
688 char marker_name[256]="";
52c51a47
PMF
689
690 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
691
692 if(channel_name == NULL || marker_name == NULL) {
693 WARN("invalid marker name");
694 goto next_cmd;
695 }
696 printf("%s %s\n", channel_name, marker_name);
697
698 result = ltt_marker_connect(channel_name, marker_name, "default");
699 if(result < 0) {
700 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
701 }
702 }
703 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
704 char *channel_slash_name = nth_token(recvbuf, 1);
705 char *marker_name;
706 char *channel_name;
52c51a47
PMF
707
708 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
709
710 if(marker_name == NULL) {
711 }
712 printf("%s %s\n", channel_name, marker_name);
713
714 result = ltt_marker_disconnect(channel_name, marker_name, "default");
715 if(result < 0) {
716 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
717 }
718 }
ed1317e7
PMF
719 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
720 char *reply;
721
722 asprintf(&reply, "%lld", pidunique);
723
724 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
725 if(result) {
726 ERR("listener: get_pidunique: ustcomm_send_reply failed");
727 goto next_cmd;
728 }
729
730 free(reply);
731 }
3a7b90de
PMF
732// else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
733// struct ltt_trace_struct *trace;
734// char trace_name[] = "auto";
735// int i;
736// char *channel_name;
737//
738// DBG("get_notifications");
739//
740// channel_name = strdup_malloc(nth_token(recvbuf, 1));
741// if(channel_name == NULL) {
742// ERR("put_subbuf_size: cannot parse channel");
743// goto next_cmd;
744// }
745//
746// ltt_lock_traces();
747// trace = _ltt_trace_find(trace_name);
748// ltt_unlock_traces();
749//
750// if(trace == NULL) {
63fe14e5 751// ERR("cannot find trace!");
872037bb 752// return (void *)1;
3a7b90de
PMF
753// }
754//
755// for(i=0; i<trace->nr_channels; i++) {
756// struct rchan *rchan = trace->channels[i].trans_channel_data;
757// int fd;
758//
759// if(!strcmp(trace->channels[i].channel_name, channel_name)) {
760// struct rchan_buf *rbuf = rchan->buf;
761// struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
762//
763// result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
764// if(result == -1) {
765// ERR("ustcomm_app_detach_client failed");
766// goto next_cmd;
767// }
768//
769// lttbuf->wake_consumer_arg = (void *) fd;
770//
771// smp_wmb();
772//
773// lttbuf->call_wake_consumer = 1;
774//
775// break;
776// }
777// }
778//
779// free(channel_name);
780// }
688760ef
PMF
781 else {
782 ERR("unable to parse message: %s", recvbuf);
783 }
d0b5f2b9 784
811e4b93 785 next_cmd:
d0b5f2b9 786 free(recvbuf);
98963de4
PMF
787 }
788}
789
7958de40 790volatile sig_atomic_t have_listener = 0;
4440ebcb 791
98963de4
PMF
792void create_listener(void)
793{
9160b4e4 794#ifdef USE_CLONE
98963de4 795 static char listener_stack[16384];
c5fdc888 796 int result;
4440ebcb
PMF
797#else
798 pthread_t thread;
9160b4e4 799#endif
98963de4 800
c5fdc888
PMF
801 if(have_listener) {
802 WARN("not creating listener because we already had one");
4440ebcb 803 return;
c5fdc888 804 }
4440ebcb 805
3847c3ba 806#ifdef USE_CLONE
c5fdc888 807 result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
98963de4
PMF
808 if(result == -1) {
809 perror("clone");
4440ebcb 810 return;
98963de4 811 }
3847c3ba 812#else
b0540e11 813
3847c3ba
PMF
814 pthread_create(&thread, NULL, listener_main, NULL);
815#endif
4440ebcb
PMF
816
817 have_listener = 1;
98963de4
PMF
818}
819
98963de4 820static int init_socket(void)
68c1021b 821{
d0b5f2b9 822 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
823}
824
5de74e51
PMF
825#define AUTOPROBE_DISABLED 0
826#define AUTOPROBE_ENABLE_ALL 1
827#define AUTOPROBE_ENABLE_REGEX 2
828static int autoprobe_method = AUTOPROBE_DISABLED;
829static regex_t autoprobe_regex;
ef290fca 830
20b37a31 831static void auto_probe_connect(struct marker *m)
68c1021b
PMF
832{
833 int result;
834
5de74e51
PMF
835 char* concat_name = NULL;
836 const char *probe_name = "default";
ef290fca 837
5de74e51 838 if(autoprobe_method == AUTOPROBE_DISABLED) {
ef290fca
PMF
839 return;
840 }
5de74e51
PMF
841 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
842 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
843 if(result == -1) {
844 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
845 m->channel, m->name);
846 return;
847 }
848 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
849 free(concat_name);
850 return;
851 }
852 free(concat_name);
ef290fca
PMF
853 }
854
5de74e51 855 result = ltt_marker_connect(m->channel, m->name, probe_name);
acbf228b
PMF
856 if(result && result != -EEXIST)
857 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
20b37a31 858
3ea1e2fc 859 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
ef290fca 860
20b37a31
PMF
861}
862
c1083aa8 863static void __attribute__((constructor)) init()
20b37a31
PMF
864{
865 int result;
5de74e51 866 char* autoprobe_val = NULL;
20b37a31 867
ed1317e7
PMF
868 /* Assign the pidunique, to be able to differentiate the processes with same
869 * pid, (before and after an exec).
870 */
871 pidunique = make_pidunique();
872
4440ebcb
PMF
873 /* Initialize RCU in case the constructor order is not good. */
874 urcu_init();
875
876 /* It is important to do this before events start to be generated. */
877 ust_register_thread();
878
5de74e51 879 DBG("Tracectl constructor");
20b37a31 880
3847c3ba
PMF
881 /* Must create socket before signal handler to prevent races.
882 */
883 result = init_socket();
884 if(result == -1) {
885 ERR("init_socket error");
886 return;
887 }
2944a629
PMF
888
889 create_listener();
68c1021b 890
5de74e51
PMF
891 autoprobe_val = getenv("UST_AUTOPROBE");
892 if(autoprobe_val) {
4440ebcb
PMF
893 struct marker_iter iter;
894
08230db7 895 DBG("Autoprobe enabled.");
4440ebcb
PMF
896
897 /* Ensure markers are initialized */
898 //init_markers();
899
900 /* Ensure marker control is initialized, for the probe */
901 init_marker_control();
902
903 /* first, set the callback that will connect the
904 * probe on new markers
905 */
5de74e51
PMF
906 if(autoprobe_val[0] == '/') {
907 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
908 if (result) {
909 char regexerr[150];
910
911 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
912 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
913 /* don't crash the application just for this */
914 }
915 else {
916 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
917 }
ef290fca 918 }
5de74e51
PMF
919 else {
920 /* just enable all instrumentation */
921 autoprobe_method = AUTOPROBE_ENABLE_ALL;
922 }
923
924 marker_set_new_marker_cb(auto_probe_connect);
925
4440ebcb
PMF
926 /* Now, connect the probes that were already registered. */
927 marker_iter_reset(&iter);
928 marker_iter_start(&iter);
929
08230db7 930 DBG("now iterating on markers already registered");
4440ebcb 931 while(iter.marker) {
08230db7 932 DBG("now iterating on marker %s", iter.marker->name);
4440ebcb
PMF
933 auto_probe_connect(iter.marker);
934 marker_iter_next(&iter);
935 }
936 }
937
4db647c5
PMF
938 if(getenv("UST_TRACE")) {
939 char trace_name[] = "auto";
940 char trace_type[] = "ustrelay";
941
942 DBG("starting early tracing");
943
944 /* Ensure marker control is initialized */
945 init_marker_control();
946
947 /* Ensure relay is initialized */
948 init_ustrelay_transport();
949
950 /* Ensure markers are initialized */
951 init_markers();
952
027ffc9d
PMF
953 /* FIXME: When starting early tracing (here), depending on the
954 * order of constructors, it is very well possible some marker
955 * sections are not yet registered. Because of this, some
956 * channels may not be registered. Yet, we are about to ask the
957 * daemon to collect the channels. Channels which are not yet
958 * registered will not be collected.
959 *
960 * Currently, in LTTng, there is no way to add a channel after
961 * trace start. The reason for this is that it induces complex
962 * concurrency issues on the trace structures, which can only
963 * be resolved using RCU. This has not been done yet. As a
964 * workaround, we are forcing the registration of the "ust"
965 * channel here. This is the only channel (apart from metadata)
966 * that can be reliably used in early tracing.
967 *
968 * Non-early tracing does not have this problem and can use
969 * arbitrary channel names.
970 */
20b37a31 971 ltt_channels_register("ust");
4db647c5
PMF
972
973 result = ltt_trace_setup(trace_name);
974 if(result < 0) {
975 ERR("ltt_trace_setup failed");
976 return;
977 }
978
979 result = ltt_trace_set_type(trace_name, trace_type);
980 if(result < 0) {
981 ERR("ltt_trace_set_type failed");
982 return;
983 }
984
985 result = ltt_trace_alloc(trace_name);
986 if(result < 0) {
987 ERR("ltt_trace_alloc failed");
988 return;
989 }
990
991 result = ltt_trace_start(trace_name);
992 if(result < 0) {
993 ERR("ltt_trace_start failed");
994 return;
995 }
60e57148
PMF
996
997 /* Do this after the trace is started in order to avoid creating confusion
998 * if the trace fails to start. */
999 inform_consumer_daemon(trace_name);
4db647c5
PMF
1000 }
1001
68c1021b
PMF
1002
1003 return;
1004
1005 /* should decrementally destroy stuff if error */
1006
1007}
1008
1009/* This is only called if we terminate normally, not with an unhandled signal,
6d45c11a
PMF
1010 * so we cannot rely on it. However, for now, LTTV requires that the header of
1011 * the last sub-buffer contain a valid end time for the trace. This is done
1012 * automatically only when the trace is properly stopped.
1013 *
1014 * If the traced program crashed, it is always possible to manually add the
1015 * right value in the header, or to open the trace in text mode.
1016 *
1017 * FIXME: Fix LTTV so it doesn't need this.
1018 */
68c1021b 1019
6d45c11a 1020static void destroy_traces(void)
68c1021b 1021{
6d45c11a 1022 int result;
a584bc4e
PMF
1023
1024 /* if trace running, finish it */
1025
6d45c11a 1026 DBG("destructor stopping traces");
a584bc4e 1027
6d45c11a
PMF
1028 result = ltt_trace_stop("auto");
1029 if(result == -1) {
1030 ERR("ltt_trace_stop error");
1031 }
1032
1033 result = ltt_trace_destroy("auto");
1034 if(result == -1) {
1035 ERR("ltt_trace_destroy error");
1036 }
68c1021b 1037}
1e2944cb 1038
97d9b88b
PMF
1039static int trace_recording(void)
1040{
1041 int retval = 0;
1042 struct ltt_trace_struct *trace;
1043
1044 ltt_lock_traces();
1045
1046 list_for_each_entry(trace, &ltt_traces.head, list) {
1047 if(trace->active) {
1048 retval = 1;
1049 break;
1050 }
1051 }
1052
1053 ltt_unlock_traces();
1054
1055 return retval;
1056}
1057
f293009f 1058#if 0
97d9b88b
PMF
1059static int have_consumer(void)
1060{
1061 return !list_empty(&blocked_consumers);
1062}
f293009f 1063#endif
97d9b88b 1064
f293009f 1065int restarting_usleep(useconds_t usecs)
97d9b88b
PMF
1066{
1067 struct timespec tv;
1068 int result;
1069
f293009f
PMF
1070 tv.tv_sec = 0;
1071 tv.tv_nsec = usecs * 1000;
97d9b88b
PMF
1072
1073 do {
1074 result = nanosleep(&tv, &tv);
1075 } while(result == -1 && errno == EINTR);
1076
1077 return result;
1078}
1079
f293009f
PMF
1080/* This destructor keeps the process alive for a few seconds in order
1081 * to leave time to ustd to connect to its buffers. This is necessary
1082 * for programs whose execution is very short. It is also useful in all
1083 * programs when tracing is started close to the end of the program
1084 * execution.
1085 *
1086 * FIXME: For now, this only works for the first trace created in a
1087 * process.
1088 */
1089
97d9b88b
PMF
1090static void __attribute__((destructor)) keepalive()
1091{
f293009f 1092 if(trace_recording() && buffers_to_export) {
c472cce0 1093 int total = 0;
f293009f
PMF
1094 DBG("Keeping process alive for consumer daemon...");
1095 while(buffers_to_export) {
1096 const int interv = 200000;
c472cce0 1097 restarting_usleep(interv);
f293009f
PMF
1098 total += interv;
1099
1100 if(total >= 3000000) {
c472cce0 1101 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
f293009f
PMF
1102 break;
1103 }
1104 }
1105 DBG("Finally dying...");
1106 }
6d45c11a
PMF
1107
1108 destroy_traces();
1109
1110 ustcomm_fini_app(&ustcomm_app);
97d9b88b 1111}
97d9b88b 1112
775c8a3f 1113void ust_potential_exec(void)
c396a841
PMF
1114{
1115 trace_mark(ust, potential_exec, MARK_NOARGS);
1116
775c8a3f
PMF
1117 DBG("test");
1118
c396a841
PMF
1119 keepalive();
1120}
1121
1e2944cb
PMF
1122/* Notify ust that there was a fork. This needs to be called inside
1123 * the new process, anytime a process whose memory is not shared with
1124 * the parent is created. If this function is not called, the events
1125 * of the new process will not be collected.
616ed36a
PMF
1126 *
1127 * Signals should be disabled before the fork and reenabled only after
1128 * this call in order to guarantee tracing is not started before ust_fork()
1129 * sanitizes the new process.
1e2944cb
PMF
1130 */
1131
616ed36a 1132static void ust_fork(void)
1e2944cb 1133{
99b72dc0
PMF
1134 struct blocked_consumer *bc;
1135 struct blocked_consumer *deletable_bc = NULL;
1136 int result;
1137
1e2944cb
PMF
1138 DBG("ust: forking");
1139 ltt_trace_stop("auto");
1140 ltt_trace_destroy("auto");
99b72dc0
PMF
1141 /* Delete all active connections */
1142 ustcomm_close_all_connections(&ustcomm_app.server);
1143
1144 /* Delete all blocked consumers */
1145 list_for_each_entry(bc, &blocked_consumers, list) {
d9ce395d
PMF
1146 close(bc->fd_producer);
1147 close(bc->fd_consumer);
99b72dc0
PMF
1148 free(deletable_bc);
1149 deletable_bc = bc;
1150 list_del(&bc->list);
1151 }
1152
1e2944cb
PMF
1153 have_listener = 0;
1154 create_listener();
99b72dc0
PMF
1155 init_socket();
1156 ltt_trace_setup("auto");
1157 result = ltt_trace_set_type("auto", "ustrelay");
1158 if(result < 0) {
1159 ERR("ltt_trace_set_type failed");
036db133 1160 return;
99b72dc0
PMF
1161 }
1162
1163 ltt_trace_alloc("auto");
1164 ltt_trace_start("auto");
ad45e833 1165 inform_consumer_daemon("auto");
1e2944cb
PMF
1166}
1167
616ed36a
PMF
1168void ust_before_fork(ust_fork_info_t *fork_info)
1169{
1170 /* Disable signals. This is to avoid that the child
1171 * intervenes before it is properly setup for tracing. It is
1172 * safer to disable all signals, because then we know we are not
1173 * breaking anything by restoring the original mask.
1174 */
1175 sigset_t all_sigs;
1176 int result;
1177
1178 /* FIXME:
1179 - only do this if tracing is active
1180 */
1181
1182 /* Disable signals */
1183 sigfillset(&all_sigs);
1184 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1185 if(result == -1) {
1186 PERROR("sigprocmask");
1187 return;
1188 }
1189}
1190
1191/* Don't call this function directly in a traced program */
1192static void ust_after_fork_common(ust_fork_info_t *fork_info)
1193{
1194 int result;
1195 sigset_t orig_sigs;
1196
1197 /* Restore signals */
1198 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1199 if(result == -1) {
1200 PERROR("sigprocmask");
1201 return;
1202 }
1203}
1204
1205void ust_after_fork_parent(ust_fork_info_t *fork_info)
1206{
1207 /* Reenable signals */
1208 ust_after_fork_common(fork_info);
1209}
1210
1211void ust_after_fork_child(ust_fork_info_t *fork_info)
1212{
1213 /* First sanitize the child */
1214 ust_fork();
1215
1216 /* Then reenable interrupts */
1217 ust_after_fork_common(fork_info);
1218}
1219
This page took 0.081212 seconds and 4 git commands to generate.