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