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