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