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