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