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