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