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