add NOTFOUND reply for get_subbuf and put_subbuf commands
[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"
b73a4c47 37#include "buffers.h"
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
b73a4c47 91 /* args to ust_buffers_get_subbuf */
b5b073e2 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{
204141ee 168 int i,j;
b73a4c47 169 struct ust_trace *trace;
ad45e833
PMF
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++) {
204141ee
PMF
182 /* iterate on all cpus */
183 for(j=0; j<trace->channels[i].n_cpus; j++) {
184 char *buf;
185 asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j);
186 result = ustcomm_request_consumer(pid, buf);
187 if(result == -1) {
188 WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name);
189 /* continue even if fail */
190 }
191 free(buf);
192 buffers_to_export++;
ad45e833
PMF
193 }
194 }
195
196 finish:
197 ltt_unlock_traces();
d0b5f2b9 198}
fbd8191b 199
3a7b90de
PMF
200void process_blocked_consumers(void)
201{
202 int n_fds = 0;
203 struct pollfd *fds;
204 struct blocked_consumer *bc;
205 int idx = 0;
206 char inbuf;
207 int result;
208
209 list_for_each_entry(bc, &blocked_consumers, list) {
210 n_fds++;
211 }
212
213 fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd));
214 if(fds == NULL) {
215 ERR("malloc returned NULL");
216 return;
217 }
218
219 list_for_each_entry(bc, &blocked_consumers, list) {
220 fds[idx].fd = bc->fd_producer;
221 fds[idx].events = POLLIN;
222 bc->tmp_poll_idx = idx;
223 idx++;
224 }
225
69ba0156
PMF
226 while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR)
227 /* nothing */;
3a7b90de
PMF
228 if(result == -1) {
229 PERROR("poll");
872037bb 230 return;
3a7b90de
PMF
231 }
232
233 list_for_each_entry(bc, &blocked_consumers, list) {
234 if(fds[bc->tmp_poll_idx].revents) {
235 long consumed_old = 0;
236 char *reply;
237
238 result = read(bc->fd_producer, &inbuf, 1);
239 if(result == -1) {
240 PERROR("read");
241 continue;
242 }
243 if(result == 0) {
244 DBG("PRODUCER END");
245
246 close(bc->fd_producer);
247
769d0157 248 list_del(&bc->list);
3a7b90de
PMF
249
250 result = ustcomm_send_reply(&bc->server, "END", &bc->src);
251 if(result < 0) {
252 ERR("ustcomm_send_reply failed");
253 continue;
254 }
255
256 continue;
257 }
258
b73a4c47 259 result = ust_buffers_get_subbuf(bc->buf, &consumed_old);
3a7b90de
PMF
260 if(result == -EAGAIN) {
261 WARN("missed buffer?");
262 continue;
263 }
264 else if(result < 0) {
b73a4c47 265 DBG("ust_buffers_get_subbuf: error: %s", strerror(-result));
3a7b90de
PMF
266 }
267 asprintf(&reply, "%s %ld", "OK", consumed_old);
268 result = ustcomm_send_reply(&bc->server, reply, &bc->src);
269 if(result < 0) {
270 ERR("ustcomm_send_reply failed");
271 free(reply);
272 continue;
273 }
274 free(reply);
275
769d0157 276 list_del(&bc->list);
3a7b90de
PMF
277 }
278 }
279
280}
281
204141ee
PMF
282void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu)
283{
284 const char *sep;
285
286 sep = rindex(channel_and_cpu, '_');
287 if(sep == NULL) {
288 *cpu = -1;
289 sep = channel_and_cpu + strlen(channel_and_cpu);
290 }
291 else {
292 *cpu = atoi(sep+1);
293 }
294
295 asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu);
296}
297
298static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src)
299{
300 int retval = 0;
b73a4c47 301 struct ust_trace *trace;
204141ee
PMF
302 char trace_name[] = "auto";
303 int i;
304 char *channel_and_cpu;
305 int found = 0;
306 int result;
307 char *ch_name;
308 int ch_cpu;
309
310 DBG("get_shmid");
311
312 channel_and_cpu = nth_token(recvbuf, 1);
313 if(channel_and_cpu == NULL) {
314 ERR("get_shmid: cannot parse channel");
315 goto end;
316 }
317
318 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
319 if(ch_cpu == -1) {
320 ERR("Problem parsing channel name");
321 goto free_short_chan_name;
322 }
323
324 ltt_lock_traces();
325 trace = _ltt_trace_find(trace_name);
326 ltt_unlock_traces();
327
328 if(trace == NULL) {
329 ERR("cannot find trace!");
330 retval = -1;
331 goto free_short_chan_name;
332 }
333
334 for(i=0; i<trace->nr_channels; i++) {
335 struct ust_channel *channel = &trace->channels[i];
336 struct ust_buffer *buf = channel->buf[ch_cpu];
337
338 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
339 char *reply;
340
341// DBG("the shmid for the requested channel is %d", buf->shmid);
342// DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids);
343 asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]);
344
345 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
346 if(result) {
347 ERR("listener: get_shmid: ustcomm_send_reply failed");
348 free(reply);
349 retval = -1;
350 goto free_short_chan_name;
351 }
352
353 free(reply);
354
355 found = 1;
356 break;
357 }
358 }
359
360 if(found) {
361 buffers_to_export--;
362 }
363 else {
364 ERR("get_shmid: channel not found (%s)", channel_and_cpu);
365 }
366
367 free_short_chan_name:
368 free(ch_name);
369
370 end:
371 return retval;
372}
373
374static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src)
375{
376 int retval = 0;
b73a4c47 377 struct ust_trace *trace;
204141ee
PMF
378 char trace_name[] = "auto";
379 int i;
380 char *channel_and_cpu;
381 int found = 0;
382 int result;
383 char *ch_name;
384 int ch_cpu;
385
386 DBG("get_n_subbufs");
387
388 channel_and_cpu = nth_token(recvbuf, 1);
389 if(channel_and_cpu == NULL) {
390 ERR("get_n_subbufs: cannot parse channel");
391 goto end;
392 }
393
394 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
395 if(ch_cpu == -1) {
396 ERR("Problem parsing channel name");
397 goto free_short_chan_name;
398 }
399
400 ltt_lock_traces();
401 trace = _ltt_trace_find(trace_name);
402 ltt_unlock_traces();
403
404 if(trace == NULL) {
405 ERR("cannot find trace!");
406 retval = -1;
407 goto free_short_chan_name;
408 }
409
410 for(i=0; i<trace->nr_channels; i++) {
411 struct ust_channel *channel = &trace->channels[i];
412
413 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
414 char *reply;
415
416 DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt);
417 asprintf(&reply, "%d", channel->subbuf_cnt);
418
419 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
420 if(result) {
421 ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
422 free(reply);
423 retval = -1;
424 goto free_short_chan_name;
425 }
426
427 free(reply);
428 found = 1;
429 break;
430 }
431 }
432 if(found == 0) {
433 ERR("get_n_subbufs: unable to find channel");
434 }
435
436 free_short_chan_name:
437 free(ch_name);
438
439 end:
440 return retval;
441}
442
443static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src)
444{
445 int retval = 0;
b73a4c47 446 struct ust_trace *trace;
204141ee
PMF
447 char trace_name[] = "auto";
448 int i;
449 char *channel_and_cpu;
450 int found = 0;
451 int result;
452 char *ch_name;
453 int ch_cpu;
454
455 DBG("get_subbuf_size");
456
457 channel_and_cpu = nth_token(recvbuf, 1);
458 if(channel_and_cpu == NULL) {
459 ERR("get_subbuf_size: cannot parse channel");
460 goto end;
461 }
462
463 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
464 if(ch_cpu == -1) {
465 ERR("Problem parsing channel name");
466 goto free_short_chan_name;
467 }
468
469 ltt_lock_traces();
470 trace = _ltt_trace_find(trace_name);
471 ltt_unlock_traces();
472
473 if(trace == NULL) {
474 ERR("cannot find trace!");
475 retval = -1;
476 goto free_short_chan_name;
477 }
478
479 for(i=0; i<trace->nr_channels; i++) {
480 struct ust_channel *channel = &trace->channels[i];
481
482 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
483 char *reply;
484
485 DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size);
486 asprintf(&reply, "%zd", channel->subbuf_size);
487
488 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
489 if(result) {
490 ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
491 free(reply);
492 retval = -1;
493 goto free_short_chan_name;
494 }
495
496 free(reply);
497 found = 1;
498 break;
499 }
500 }
501 if(found == 0) {
502 ERR("get_subbuf_size: unable to find channel");
503 }
504
505 free_short_chan_name:
506 free(ch_name);
507
508 end:
509 return retval;
510}
511
512static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src)
513{
514 int retval = 0;
b73a4c47 515 struct ust_trace *trace;
204141ee
PMF
516 char trace_name[] = "auto";
517 int i;
518 char *channel_and_cpu;
519 int found = 0;
520 char *ch_name;
521 int ch_cpu;
522
523 DBG("get_subbuf");
524
525 channel_and_cpu = nth_token(recvbuf, 1);
526 if(channel_and_cpu == NULL) {
527 ERR("get_subbuf: cannot parse channel");
528 goto end;
529 }
530
531 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
532 if(ch_cpu == -1) {
533 ERR("Problem parsing channel name");
534 goto free_short_chan_name;
535 }
536
537 ltt_lock_traces();
538 trace = _ltt_trace_find(trace_name);
539 ltt_unlock_traces();
540
541 if(trace == NULL) {
2ddb81a8
PMF
542 char *reply;
543 int result;
544
545 WARN("Cannot find trace. It was likely destroyed by the user.");
546 asprintf(&reply, "%s", "NOTFOUND");
547 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
548 if(result) {
549 ERR("ustcomm_send_reply failed");
550 free(reply);
551 retval = -1;
552 goto free_short_chan_name;
553 }
554
555 free(reply);
204141ee
PMF
556 goto free_short_chan_name;
557 }
558
559 for(i=0; i<trace->nr_channels; i++) {
560 struct ust_channel *channel = &trace->channels[i];
561
562 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
563 struct ust_buffer *buf = channel->buf[ch_cpu];
564 struct blocked_consumer *bc;
565
566 found = 1;
567
568 bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer));
569 if(bc == NULL) {
570 ERR("malloc returned NULL");
571 goto free_short_chan_name;
572 }
573 bc->fd_consumer = src->fd;
574 bc->fd_producer = buf->data_ready_fd_read;
575 bc->buf = buf;
576 bc->src = *src;
577 bc->server = ustcomm_app.server;
578
579 list_add(&bc->list, &blocked_consumers);
580
581 break;
582 }
583 }
584 if(found == 0) {
585 ERR("get_subbuf: unable to find channel");
586 }
587
588 free_short_chan_name:
589 free(ch_name);
590
591 end:
592 return retval;
593}
594
595static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src)
596{
597 int retval = 0;
b73a4c47 598 struct ust_trace *trace;
204141ee
PMF
599 char trace_name[] = "auto";
600 int i;
601 char *channel_and_cpu;
602 int found = 0;
603 int result;
604 char *ch_name;
605 int ch_cpu;
606 long consumed_old;
607 char *consumed_old_str;
608 char *endptr;
2ddb81a8 609 char *reply = NULL;
204141ee
PMF
610
611 DBG("put_subbuf");
612
613 channel_and_cpu = strdup_malloc(nth_token(recvbuf, 1));
614 if(channel_and_cpu == NULL) {
615 ERR("put_subbuf_size: cannot parse channel");
616 retval = -1;
617 goto end;
618 }
619
620 consumed_old_str = strdup_malloc(nth_token(recvbuf, 2));
621 if(consumed_old_str == NULL) {
622 ERR("put_subbuf: cannot parse consumed_old");
623 retval = -1;
624 goto free_channel_and_cpu;
625 }
626 consumed_old = strtol(consumed_old_str, &endptr, 10);
627 if(*endptr != '\0') {
628 ERR("put_subbuf: invalid value for consumed_old");
629 retval = -1;
630 goto free_consumed_old_str;
631 }
632
633 seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu);
634 if(ch_cpu == -1) {
635 ERR("Problem parsing channel name");
636 retval = -1;
637 goto free_short_chan_name;
638 }
639
640 ltt_lock_traces();
641 trace = _ltt_trace_find(trace_name);
642 ltt_unlock_traces();
643
644 if(trace == NULL) {
2ddb81a8
PMF
645 WARN("Cannot find trace. It was likely destroyed by the user.");
646 asprintf(&reply, "%s", "NOTFOUND");
647 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
648 if(result) {
649 ERR("ustcomm_send_reply failed");
650 free(reply);
651 retval = -1;
652 goto free_short_chan_name;
653 }
654
655 free(reply);
204141ee
PMF
656 goto free_short_chan_name;
657 }
658
659 for(i=0; i<trace->nr_channels; i++) {
660 struct ust_channel *channel = &trace->channels[i];
661
662 if(!strcmp(trace->channels[i].channel_name, ch_name)) {
663 struct ust_buffer *buf = channel->buf[ch_cpu];
204141ee
PMF
664
665 found = 1;
666
b73a4c47 667 result = ust_buffers_put_subbuf(buf, consumed_old);
204141ee 668 if(result < 0) {
b73a4c47 669 WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu);
204141ee
PMF
670 asprintf(&reply, "%s", "ERROR");
671 }
672 else {
b73a4c47 673 DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu);
204141ee
PMF
674 asprintf(&reply, "%s", "OK");
675 }
676
677 result = ustcomm_send_reply(&ustcomm_app.server, reply, src);
678 if(result) {
679 ERR("listener: put_subbuf: ustcomm_send_reply failed");
680 free(reply);
681 retval = -1;
682 goto free_channel_and_cpu;
683 }
684
685 free(reply);
686 break;
687 }
688 }
689 if(found == 0) {
2ddb81a8 690 ERR("unable to find channel");
204141ee
PMF
691 }
692
204141ee
PMF
693 free_short_chan_name:
694 free(ch_name);
2ddb81a8
PMF
695 free_consumed_old_str:
696 free(consumed_old_str);
697 free_channel_and_cpu:
698 free(channel_and_cpu);
204141ee
PMF
699
700 end:
701 return retval;
702}
703
872037bb 704void *listener_main(void *p)
98963de4
PMF
705{
706 int result;
707
26cc7017
PMF
708 ust_register_thread();
709
b0540e11
PMF
710 DBG("LISTENER");
711
98963de4 712 for(;;) {
aafb1650
PMF
713 char trace_name[] = "auto";
714 char trace_type[] = "ustrelay";
d0b5f2b9
PMF
715 char *recvbuf;
716 int len;
b02e31e5 717 struct ustcomm_source src;
98963de4 718
3a7b90de
PMF
719 process_blocked_consumers();
720
721 result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5);
722 if(result < 0) {
d0b5f2b9
PMF
723 WARN("error in ustcomm_app_recv_message");
724 continue;
725 }
3a7b90de
PMF
726 else if(result == 0) {
727 /* no message */
728 continue;
729 }
98963de4 730
08230db7 731 DBG("received a message! it's: %s", recvbuf);
d0b5f2b9 732 len = strlen(recvbuf);
98963de4 733
d0b5f2b9 734 if(!strcmp(recvbuf, "print_markers")) {
52c51a47
PMF
735 print_markers(stderr);
736 }
737 else if(!strcmp(recvbuf, "list_markers")) {
738 char *ptr;
739 size_t size;
740 FILE *fp;
741
742 fp = open_memstream(&ptr, &size);
743 print_markers(fp);
744 fclose(fp);
745
746 result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src);
747
748 free(ptr);
749 }
750 else if(!strcmp(recvbuf, "start")) {
751 /* start is an operation that setups the trace, allocates it and starts it */
752 result = ltt_trace_setup(trace_name);
753 if(result < 0) {
754 ERR("ltt_trace_setup failed");
872037bb 755 return (void *)1;
52c51a47
PMF
756 }
757
758 result = ltt_trace_set_type(trace_name, trace_type);
759 if(result < 0) {
760 ERR("ltt_trace_set_type failed");
872037bb 761 return (void *)1;
52c51a47
PMF
762 }
763
764 result = ltt_trace_alloc(trace_name);
765 if(result < 0) {
766 ERR("ltt_trace_alloc failed");
872037bb 767 return (void *)1;
52c51a47
PMF
768 }
769
ad45e833 770 inform_consumer_daemon(trace_name);
52c51a47
PMF
771
772 result = ltt_trace_start(trace_name);
773 if(result < 0) {
774 ERR("ltt_trace_start failed");
775 continue;
776 }
d0b5f2b9
PMF
777 }
778 else if(!strcmp(recvbuf, "trace_setup")) {
779 DBG("trace setup");
fbd8191b 780
d0b5f2b9
PMF
781 result = ltt_trace_setup(trace_name);
782 if(result < 0) {
783 ERR("ltt_trace_setup failed");
872037bb 784 return (void *)1;
fbd8191b 785 }
d0b5f2b9
PMF
786
787 result = ltt_trace_set_type(trace_name, trace_type);
788 if(result < 0) {
789 ERR("ltt_trace_set_type failed");
872037bb 790 return (void *)1;
fbd8191b 791 }
d0b5f2b9
PMF
792 }
793 else if(!strcmp(recvbuf, "trace_alloc")) {
794 DBG("trace alloc");
795
796 result = ltt_trace_alloc(trace_name);
797 if(result < 0) {
798 ERR("ltt_trace_alloc failed");
872037bb 799 return (void *)1;
fbd8191b 800 }
d0b5f2b9 801 }
62ec620f
PMF
802 else if(!strcmp(recvbuf, "trace_create")) {
803 DBG("trace create");
804
805 result = ltt_trace_setup(trace_name);
806 if(result < 0) {
807 ERR("ltt_trace_setup failed");
808 return (void *)1;
809 }
810
811 result = ltt_trace_set_type(trace_name, trace_type);
812 if(result < 0) {
813 ERR("ltt_trace_set_type failed");
814 return (void *)1;
815 }
816
817 result = ltt_trace_alloc(trace_name);
818 if(result < 0) {
819 ERR("ltt_trace_alloc failed");
820 return (void *)1;
821 }
822
823 inform_consumer_daemon(trace_name);
824 }
d0b5f2b9
PMF
825 else if(!strcmp(recvbuf, "trace_start")) {
826 DBG("trace start");
827
828 result = ltt_trace_start(trace_name);
829 if(result < 0) {
830 ERR("ltt_trace_start failed");
831 continue;
fbd8191b 832 }
d0b5f2b9
PMF
833 }
834 else if(!strcmp(recvbuf, "trace_stop")) {
835 DBG("trace stop");
836
837 result = ltt_trace_stop(trace_name);
838 if(result < 0) {
839 ERR("ltt_trace_stop failed");
872037bb 840 return (void *)1;
aafb1650 841 }
d0b5f2b9
PMF
842 }
843 else if(!strcmp(recvbuf, "trace_destroy")) {
aafb1650 844
d0b5f2b9 845 DBG("trace destroy");
aafb1650 846
d0b5f2b9
PMF
847 result = ltt_trace_destroy(trace_name);
848 if(result < 0) {
849 ERR("ltt_trace_destroy failed");
872037bb 850 return (void *)1;
fbd8191b 851 }
98963de4 852 }
b02e31e5 853 else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
204141ee 854 do_cmd_get_shmid(recvbuf, &src);
811e4b93
PMF
855 }
856 else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
204141ee 857 do_cmd_get_n_subbufs(recvbuf, &src);
811e4b93
PMF
858 }
859 else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
204141ee 860 do_cmd_get_subbuf_size(recvbuf, &src);
3847c3ba 861 }
b02e31e5
PMF
862 else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
863 char *libfile;
864
865 libfile = nth_token(recvbuf, 1);
866
867 DBG("load_probe_lib loading %s", libfile);
204141ee
PMF
868
869 free(libfile);
b02e31e5 870 }
688760ef 871 else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) {
204141ee 872 do_cmd_get_subbuffer(recvbuf, &src);
688760ef
PMF
873 }
874 else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) {
204141ee 875 do_cmd_put_subbuffer(recvbuf, &src);
688760ef 876 }
52c51a47
PMF
877 else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) {
878 char *channel_slash_name = nth_token(recvbuf, 1);
879 char channel_name[256]="";
880 char marker_name[256]="";
52c51a47
PMF
881
882 result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name);
883
884 if(channel_name == NULL || marker_name == NULL) {
885 WARN("invalid marker name");
886 goto next_cmd;
887 }
52c51a47
PMF
888
889 result = ltt_marker_connect(channel_name, marker_name, "default");
890 if(result < 0) {
891 WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name);
892 }
893 }
894 else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) {
895 char *channel_slash_name = nth_token(recvbuf, 1);
896 char *marker_name;
897 char *channel_name;
52c51a47
PMF
898
899 result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name);
900
901 if(marker_name == NULL) {
902 }
52c51a47
PMF
903
904 result = ltt_marker_disconnect(channel_name, marker_name, "default");
905 if(result < 0) {
906 WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name);
907 }
908 }
ed1317e7
PMF
909 else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) {
910 char *reply;
911
912 asprintf(&reply, "%lld", pidunique);
913
914 result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
915 if(result) {
916 ERR("listener: get_pidunique: ustcomm_send_reply failed");
917 goto next_cmd;
918 }
919
920 free(reply);
921 }
3a7b90de 922// else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) {
b73a4c47 923// struct ust_trace *trace;
3a7b90de
PMF
924// char trace_name[] = "auto";
925// int i;
926// char *channel_name;
927//
928// DBG("get_notifications");
929//
930// channel_name = strdup_malloc(nth_token(recvbuf, 1));
931// if(channel_name == NULL) {
932// ERR("put_subbuf_size: cannot parse channel");
933// goto next_cmd;
934// }
935//
936// ltt_lock_traces();
937// trace = _ltt_trace_find(trace_name);
938// ltt_unlock_traces();
939//
940// if(trace == NULL) {
63fe14e5 941// ERR("cannot find trace!");
872037bb 942// return (void *)1;
3a7b90de
PMF
943// }
944//
945// for(i=0; i<trace->nr_channels; i++) {
946// struct rchan *rchan = trace->channels[i].trans_channel_data;
947// int fd;
948//
949// if(!strcmp(trace->channels[i].channel_name, channel_name)) {
950// struct rchan_buf *rbuf = rchan->buf;
951// struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf;
952//
953// result = fd = ustcomm_app_detach_client(&ustcomm_app, &src);
954// if(result == -1) {
955// ERR("ustcomm_app_detach_client failed");
956// goto next_cmd;
957// }
958//
959// lttbuf->wake_consumer_arg = (void *) fd;
960//
961// smp_wmb();
962//
963// lttbuf->call_wake_consumer = 1;
964//
965// break;
966// }
967// }
968//
969// free(channel_name);
970// }
688760ef
PMF
971 else {
972 ERR("unable to parse message: %s", recvbuf);
973 }
d0b5f2b9 974
811e4b93 975 next_cmd:
d0b5f2b9 976 free(recvbuf);
98963de4
PMF
977 }
978}
979
7958de40 980volatile sig_atomic_t have_listener = 0;
4440ebcb 981
98963de4
PMF
982void create_listener(void)
983{
9160b4e4 984#ifdef USE_CLONE
98963de4 985 static char listener_stack[16384];
c5fdc888 986 int result;
4440ebcb
PMF
987#else
988 pthread_t thread;
9160b4e4 989#endif
98963de4 990
c5fdc888
PMF
991 if(have_listener) {
992 WARN("not creating listener because we already had one");
4440ebcb 993 return;
c5fdc888 994 }
4440ebcb 995
3847c3ba 996#ifdef USE_CLONE
c5fdc888 997 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
998 if(result == -1) {
999 perror("clone");
4440ebcb 1000 return;
98963de4 1001 }
3847c3ba 1002#else
b0540e11 1003
3847c3ba
PMF
1004 pthread_create(&thread, NULL, listener_main, NULL);
1005#endif
4440ebcb
PMF
1006
1007 have_listener = 1;
98963de4
PMF
1008}
1009
98963de4 1010static int init_socket(void)
68c1021b 1011{
d0b5f2b9 1012 return ustcomm_init_app(getpid(), &ustcomm_app);
68c1021b
PMF
1013}
1014
5de74e51
PMF
1015#define AUTOPROBE_DISABLED 0
1016#define AUTOPROBE_ENABLE_ALL 1
1017#define AUTOPROBE_ENABLE_REGEX 2
1018static int autoprobe_method = AUTOPROBE_DISABLED;
1019static regex_t autoprobe_regex;
ef290fca 1020
20b37a31 1021static void auto_probe_connect(struct marker *m)
68c1021b
PMF
1022{
1023 int result;
1024
5de74e51
PMF
1025 char* concat_name = NULL;
1026 const char *probe_name = "default";
ef290fca 1027
5de74e51 1028 if(autoprobe_method == AUTOPROBE_DISABLED) {
ef290fca
PMF
1029 return;
1030 }
5de74e51
PMF
1031 else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) {
1032 result = asprintf(&concat_name, "%s/%s", m->channel, m->name);
1033 if(result == -1) {
1034 ERR("auto_probe_connect: asprintf failed (marker %s/%s)",
1035 m->channel, m->name);
1036 return;
1037 }
1038 if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) {
1039 free(concat_name);
1040 return;
1041 }
1042 free(concat_name);
ef290fca
PMF
1043 }
1044
5de74e51 1045 result = ltt_marker_connect(m->channel, m->name, probe_name);
acbf228b
PMF
1046 if(result && result != -EEXIST)
1047 ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result);
20b37a31 1048
3ea1e2fc 1049 DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name);
ef290fca 1050
20b37a31
PMF
1051}
1052
c1083aa8 1053static void __attribute__((constructor)) init()
20b37a31
PMF
1054{
1055 int result;
5de74e51 1056 char* autoprobe_val = NULL;
20b37a31 1057
ed1317e7
PMF
1058 /* Assign the pidunique, to be able to differentiate the processes with same
1059 * pid, (before and after an exec).
1060 */
1061 pidunique = make_pidunique();
1062
4440ebcb 1063 /* Initialize RCU in case the constructor order is not good. */
5a66a1f2 1064 rcu_init();
4440ebcb
PMF
1065
1066 /* It is important to do this before events start to be generated. */
1067 ust_register_thread();
1068
5de74e51 1069 DBG("Tracectl constructor");
20b37a31 1070
3847c3ba
PMF
1071 /* Must create socket before signal handler to prevent races.
1072 */
1073 result = init_socket();
1074 if(result == -1) {
1075 ERR("init_socket error");
1076 return;
1077 }
2944a629
PMF
1078
1079 create_listener();
68c1021b 1080
5de74e51
PMF
1081 autoprobe_val = getenv("UST_AUTOPROBE");
1082 if(autoprobe_val) {
4440ebcb
PMF
1083 struct marker_iter iter;
1084
08230db7 1085 DBG("Autoprobe enabled.");
4440ebcb
PMF
1086
1087 /* Ensure markers are initialized */
1088 //init_markers();
1089
1090 /* Ensure marker control is initialized, for the probe */
1091 init_marker_control();
1092
1093 /* first, set the callback that will connect the
1094 * probe on new markers
1095 */
5de74e51
PMF
1096 if(autoprobe_val[0] == '/') {
1097 result = regcomp(&autoprobe_regex, autoprobe_val+1, 0);
1098 if (result) {
1099 char regexerr[150];
1100
1101 regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr));
1102 ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr);
1103 /* don't crash the application just for this */
1104 }
1105 else {
1106 autoprobe_method = AUTOPROBE_ENABLE_REGEX;
1107 }
ef290fca 1108 }
5de74e51
PMF
1109 else {
1110 /* just enable all instrumentation */
1111 autoprobe_method = AUTOPROBE_ENABLE_ALL;
1112 }
1113
1114 marker_set_new_marker_cb(auto_probe_connect);
1115
4440ebcb
PMF
1116 /* Now, connect the probes that were already registered. */
1117 marker_iter_reset(&iter);
1118 marker_iter_start(&iter);
1119
08230db7 1120 DBG("now iterating on markers already registered");
4440ebcb 1121 while(iter.marker) {
08230db7 1122 DBG("now iterating on marker %s", iter.marker->name);
4440ebcb
PMF
1123 auto_probe_connect(iter.marker);
1124 marker_iter_next(&iter);
1125 }
1126 }
1127
4db647c5
PMF
1128 if(getenv("UST_TRACE")) {
1129 char trace_name[] = "auto";
1130 char trace_type[] = "ustrelay";
1131
1132 DBG("starting early tracing");
1133
1134 /* Ensure marker control is initialized */
1135 init_marker_control();
1136
4db647c5
PMF
1137 /* Ensure markers are initialized */
1138 init_markers();
1139
e17571a5
PMF
1140 /* Ensure buffers are initialized, for the transport to be available.
1141 * We are about to set a trace type and it will fail without this.
1142 */
1143 init_ustrelay_transport();
1144
027ffc9d
PMF
1145 /* FIXME: When starting early tracing (here), depending on the
1146 * order of constructors, it is very well possible some marker
1147 * sections are not yet registered. Because of this, some
1148 * channels may not be registered. Yet, we are about to ask the
1149 * daemon to collect the channels. Channels which are not yet
1150 * registered will not be collected.
1151 *
1152 * Currently, in LTTng, there is no way to add a channel after
1153 * trace start. The reason for this is that it induces complex
1154 * concurrency issues on the trace structures, which can only
1155 * be resolved using RCU. This has not been done yet. As a
1156 * workaround, we are forcing the registration of the "ust"
1157 * channel here. This is the only channel (apart from metadata)
1158 * that can be reliably used in early tracing.
1159 *
1160 * Non-early tracing does not have this problem and can use
1161 * arbitrary channel names.
1162 */
20b37a31 1163 ltt_channels_register("ust");
4db647c5
PMF
1164
1165 result = ltt_trace_setup(trace_name);
1166 if(result < 0) {
1167 ERR("ltt_trace_setup failed");
1168 return;
1169 }
1170
1171 result = ltt_trace_set_type(trace_name, trace_type);
1172 if(result < 0) {
1173 ERR("ltt_trace_set_type failed");
1174 return;
1175 }
1176
1177 result = ltt_trace_alloc(trace_name);
1178 if(result < 0) {
1179 ERR("ltt_trace_alloc failed");
1180 return;
1181 }
1182
1183 result = ltt_trace_start(trace_name);
1184 if(result < 0) {
1185 ERR("ltt_trace_start failed");
1186 return;
1187 }
60e57148
PMF
1188
1189 /* Do this after the trace is started in order to avoid creating confusion
1190 * if the trace fails to start. */
1191 inform_consumer_daemon(trace_name);
4db647c5
PMF
1192 }
1193
68c1021b
PMF
1194
1195 return;
1196
1197 /* should decrementally destroy stuff if error */
1198
1199}
1200
1201/* This is only called if we terminate normally, not with an unhandled signal,
6d45c11a
PMF
1202 * so we cannot rely on it. However, for now, LTTV requires that the header of
1203 * the last sub-buffer contain a valid end time for the trace. This is done
1204 * automatically only when the trace is properly stopped.
1205 *
1206 * If the traced program crashed, it is always possible to manually add the
1207 * right value in the header, or to open the trace in text mode.
1208 *
1209 * FIXME: Fix LTTV so it doesn't need this.
1210 */
68c1021b 1211
6d45c11a 1212static void destroy_traces(void)
68c1021b 1213{
6d45c11a 1214 int result;
a584bc4e
PMF
1215
1216 /* if trace running, finish it */
1217
6d45c11a 1218 DBG("destructor stopping traces");
a584bc4e 1219
6d45c11a
PMF
1220 result = ltt_trace_stop("auto");
1221 if(result == -1) {
1222 ERR("ltt_trace_stop error");
1223 }
1224
1225 result = ltt_trace_destroy("auto");
1226 if(result == -1) {
1227 ERR("ltt_trace_destroy error");
1228 }
68c1021b 1229}
1e2944cb 1230
97d9b88b
PMF
1231static int trace_recording(void)
1232{
1233 int retval = 0;
b73a4c47 1234 struct ust_trace *trace;
97d9b88b
PMF
1235
1236 ltt_lock_traces();
1237
1238 list_for_each_entry(trace, &ltt_traces.head, list) {
1239 if(trace->active) {
1240 retval = 1;
1241 break;
1242 }
1243 }
1244
1245 ltt_unlock_traces();
1246
1247 return retval;
1248}
1249
f293009f 1250#if 0
97d9b88b
PMF
1251static int have_consumer(void)
1252{
1253 return !list_empty(&blocked_consumers);
1254}
f293009f 1255#endif
97d9b88b 1256
f293009f 1257int restarting_usleep(useconds_t usecs)
97d9b88b
PMF
1258{
1259 struct timespec tv;
1260 int result;
1261
f293009f
PMF
1262 tv.tv_sec = 0;
1263 tv.tv_nsec = usecs * 1000;
97d9b88b
PMF
1264
1265 do {
1266 result = nanosleep(&tv, &tv);
1267 } while(result == -1 && errno == EINTR);
1268
1269 return result;
1270}
1271
f293009f
PMF
1272/* This destructor keeps the process alive for a few seconds in order
1273 * to leave time to ustd to connect to its buffers. This is necessary
1274 * for programs whose execution is very short. It is also useful in all
1275 * programs when tracing is started close to the end of the program
1276 * execution.
1277 *
1278 * FIXME: For now, this only works for the first trace created in a
1279 * process.
1280 */
1281
97d9b88b
PMF
1282static void __attribute__((destructor)) keepalive()
1283{
f293009f 1284 if(trace_recording() && buffers_to_export) {
c472cce0 1285 int total = 0;
f293009f
PMF
1286 DBG("Keeping process alive for consumer daemon...");
1287 while(buffers_to_export) {
1288 const int interv = 200000;
c472cce0 1289 restarting_usleep(interv);
f293009f
PMF
1290 total += interv;
1291
1292 if(total >= 3000000) {
c472cce0 1293 WARN("non-consumed buffers remaining after wait limit; not waiting anymore");
f293009f
PMF
1294 break;
1295 }
1296 }
1297 DBG("Finally dying...");
1298 }
6d45c11a
PMF
1299
1300 destroy_traces();
1301
1302 ustcomm_fini_app(&ustcomm_app);
97d9b88b 1303}
97d9b88b 1304
775c8a3f 1305void ust_potential_exec(void)
c396a841
PMF
1306{
1307 trace_mark(ust, potential_exec, MARK_NOARGS);
1308
775c8a3f
PMF
1309 DBG("test");
1310
c396a841
PMF
1311 keepalive();
1312}
1313
1e2944cb
PMF
1314/* Notify ust that there was a fork. This needs to be called inside
1315 * the new process, anytime a process whose memory is not shared with
1316 * the parent is created. If this function is not called, the events
1317 * of the new process will not be collected.
616ed36a
PMF
1318 *
1319 * Signals should be disabled before the fork and reenabled only after
1320 * this call in order to guarantee tracing is not started before ust_fork()
1321 * sanitizes the new process.
1e2944cb
PMF
1322 */
1323
616ed36a 1324static void ust_fork(void)
1e2944cb 1325{
99b72dc0
PMF
1326 struct blocked_consumer *bc;
1327 struct blocked_consumer *deletable_bc = NULL;
1328 int result;
1329
1e2944cb
PMF
1330 DBG("ust: forking");
1331 ltt_trace_stop("auto");
1332 ltt_trace_destroy("auto");
99b72dc0
PMF
1333 /* Delete all active connections */
1334 ustcomm_close_all_connections(&ustcomm_app.server);
1335
1336 /* Delete all blocked consumers */
1337 list_for_each_entry(bc, &blocked_consumers, list) {
d9ce395d
PMF
1338 close(bc->fd_producer);
1339 close(bc->fd_consumer);
99b72dc0
PMF
1340 free(deletable_bc);
1341 deletable_bc = bc;
1342 list_del(&bc->list);
1343 }
1344
1e2944cb
PMF
1345 have_listener = 0;
1346 create_listener();
99b72dc0
PMF
1347 init_socket();
1348 ltt_trace_setup("auto");
1349 result = ltt_trace_set_type("auto", "ustrelay");
1350 if(result < 0) {
1351 ERR("ltt_trace_set_type failed");
036db133 1352 return;
99b72dc0
PMF
1353 }
1354
1355 ltt_trace_alloc("auto");
1356 ltt_trace_start("auto");
ad45e833 1357 inform_consumer_daemon("auto");
1e2944cb
PMF
1358}
1359
616ed36a
PMF
1360void ust_before_fork(ust_fork_info_t *fork_info)
1361{
1362 /* Disable signals. This is to avoid that the child
1363 * intervenes before it is properly setup for tracing. It is
1364 * safer to disable all signals, because then we know we are not
1365 * breaking anything by restoring the original mask.
1366 */
1367 sigset_t all_sigs;
1368 int result;
1369
1370 /* FIXME:
1371 - only do this if tracing is active
1372 */
1373
1374 /* Disable signals */
1375 sigfillset(&all_sigs);
1376 result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
1377 if(result == -1) {
1378 PERROR("sigprocmask");
1379 return;
1380 }
1381}
1382
1383/* Don't call this function directly in a traced program */
1384static void ust_after_fork_common(ust_fork_info_t *fork_info)
1385{
1386 int result;
616ed36a
PMF
1387
1388 /* Restore signals */
1389 result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
1390 if(result == -1) {
1391 PERROR("sigprocmask");
1392 return;
1393 }
1394}
1395
1396void ust_after_fork_parent(ust_fork_info_t *fork_info)
1397{
1398 /* Reenable signals */
1399 ust_after_fork_common(fork_info);
1400}
1401
1402void ust_after_fork_child(ust_fork_info_t *fork_info)
1403{
1404 /* First sanitize the child */
1405 ust_fork();
1406
1407 /* Then reenable interrupts */
1408 ust_after_fork_common(fork_info);
1409}
1410
This page took 0.092606 seconds and 4 git commands to generate.