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