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