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