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