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