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