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 PMF |
19 | #include <stdio.h> |
20 | #include <stdint.h> | |
21 | #include <signal.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/socket.h> | |
24 | #include <sys/un.h> | |
98963de4 | 25 | #include <sched.h> |
a584bc4e | 26 | #include <fcntl.h> |
3a7b90de | 27 | #include <poll.h> |
ef290fca | 28 | #include <regex.h> |
fbd8191b | 29 | |
b7ea1a1c | 30 | #include <urcu-bp.h> |
26cc7017 | 31 | |
fbd8191b | 32 | #include "marker.h" |
a584bc4e | 33 | #include "tracer.h" |
d0b5f2b9 PMF |
34 | #include "localerr.h" |
35 | #include "ustcomm.h" | |
46ef48cd | 36 | #include "relay.h" /* FIXME: remove */ |
9160b4e4 | 37 | #include "marker-control.h" |
fbd8191b | 38 | |
b02e31e5 | 39 | //#define USE_CLONE |
3847c3ba | 40 | |
68c1021b PMF |
41 | #define USTSIGNAL SIGIO |
42 | ||
98963de4 PMF |
43 | #define MAX_MSG_SIZE (100) |
44 | #define MSG_NOTIF 1 | |
45 | #define MSG_REGISTER_NOTIF 2 | |
46 | ||
a584bc4e PMF |
47 | char consumer_stack[10000]; |
48 | ||
ed1317e7 PMF |
49 | /* This should only be accessed by the constructor, before the creation |
50 | * of the listener, and then only by the listener. | |
51 | */ | |
52 | s64 pidunique = -1LL; | |
53 | ||
3a7b90de PMF |
54 | struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers); |
55 | ||
d0b5f2b9 PMF |
56 | static struct ustcomm_app ustcomm_app; |
57 | ||
68c1021b PMF |
58 | struct tracecmd { /* no padding */ |
59 | uint32_t size; | |
60 | uint16_t command; | |
61 | }; | |
62 | ||
f293009f PMF |
63 | /* volatile because shared between the listener and the main thread */ |
64 | volatile sig_atomic_t buffers_to_export = 0; | |
65 | ||
98963de4 PMF |
66 | struct trctl_msg { |
67 | /* size: the size of all the fields except size itself */ | |
68 | uint32_t size; | |
69 | uint16_t type; | |
70 | /* Only the necessary part of the payload is transferred. It | |
71 | * may even be none of it. | |
72 | */ | |
73 | char payload[94]; | |
74 | }; | |
68c1021b | 75 | |
a584bc4e PMF |
76 | struct consumer_channel { |
77 | int fd; | |
78 | struct ltt_channel_struct *chan; | |
79 | }; | |
80 | ||
3a7b90de PMF |
81 | struct blocked_consumer { |
82 | int fd_consumer; | |
83 | int fd_producer; | |
84 | int tmp_poll_idx; | |
85 | ||
86 | /* args to ustcomm_send_reply */ | |
87 | struct ustcomm_server server; | |
88 | struct ustcomm_source src; | |
89 | ||
90 | /* args to ltt_do_get_subbuf */ | |
91 | struct rchan_buf *rbuf; | |
92 | struct ltt_channel_buf_struct *lttbuf; | |
93 | ||
94 | struct list_head list; | |
95 | }; | |
96 | ||
ed1317e7 PMF |
97 | static long long make_pidunique(void) |
98 | { | |
99 | s64 retval; | |
100 | struct timeval tv; | |
101 | ||
102 | gettimeofday(&tv, NULL); | |
103 | ||
104 | retval = tv.tv_sec; | |
105 | retval <<= 32; | |
106 | retval |= tv.tv_usec; | |
107 | ||
108 | return retval; | |
109 | } | |
110 | ||
52c51a47 | 111 | static void print_markers(FILE *fp) |
fbd8191b PMF |
112 | { |
113 | struct marker_iter iter; | |
114 | ||
d0b5f2b9 | 115 | lock_markers(); |
fbd8191b PMF |
116 | marker_iter_reset(&iter); |
117 | marker_iter_start(&iter); | |
118 | ||
119 | while(iter.marker) { | |
3ea1e2fc | 120 | 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 |
121 | marker_iter_next(&iter); |
122 | } | |
d0b5f2b9 | 123 | unlock_markers(); |
fbd8191b PMF |
124 | } |
125 | ||
4440ebcb | 126 | static int init_socket(void); |
68c1021b | 127 | |
26cc7017 PMF |
128 | /* This needs to be called whenever a new thread is created. It notifies |
129 | * liburcu of the new thread. | |
130 | */ | |
131 | ||
132 | void ust_register_thread(void) | |
68c1021b | 133 | { |
26cc7017 | 134 | rcu_register_thread(); |
68c1021b PMF |
135 | } |
136 | ||
98963de4 PMF |
137 | int fd_notif = -1; |
138 | void notif_cb(void) | |
139 | { | |
140 | int result; | |
141 | struct trctl_msg msg; | |
142 | ||
143 | /* FIXME: fd_notif should probably be protected by a spinlock */ | |
144 | ||
145 | if(fd_notif == -1) | |
146 | return; | |
147 | ||
148 | msg.type = MSG_NOTIF; | |
149 | msg.size = sizeof(msg.type); | |
150 | ||
151 | /* FIXME: don't block here */ | |
152 | result = write(fd_notif, &msg, msg.size+sizeof(msg.size)); | |
153 | if(result == -1) { | |
154 | PERROR("write"); | |
155 | return; | |
156 | } | |
157 | } | |
158 | ||
ad45e833 PMF |
159 | /* Ask the daemon to collect a trace called trace_name and being |
160 | * produced by this pid. | |
161 | * | |
162 | * The trace must be at least allocated. (It can also be started.) | |
163 | * This is because _ltt_trace_find is used. | |
164 | */ | |
165 | ||
166 | static void inform_consumer_daemon(const char *trace_name) | |
d0b5f2b9 | 167 | { |
ad45e833 PMF |
168 | int i; |
169 | struct ltt_trace_struct *trace; | |
170 | pid_t pid = getpid(); | |
171 | int result; | |
172 | ||
173 | ltt_lock_traces(); | |
174 | ||
175 | trace = _ltt_trace_find(trace_name); | |
176 | if(trace == NULL) { | |
177 | WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name); | |
178 | goto finish; | |
179 | } | |
180 | ||
181 | for(i=0; i < trace->nr_channels; i++) { | |
182 | result = ustcomm_request_consumer(pid, trace->channels[i].channel_name); | |
183 | if(result == -1) { | |
184 | WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name); | |
185 | /* continue even if fail */ | |
186 | } | |
f293009f | 187 | buffers_to_export++; |
ad45e833 PMF |
188 | } |
189 | ||
190 | finish: | |
191 | ltt_unlock_traces(); | |
d0b5f2b9 | 192 | } |
fbd8191b | 193 | |
3a7b90de PMF |
194 | void process_blocked_consumers(void) |
195 | { | |
196 | int n_fds = 0; | |
197 | struct pollfd *fds; | |
198 | struct blocked_consumer *bc; | |
199 | int idx = 0; | |
200 | char inbuf; | |
201 | int result; | |
202 | ||
203 | list_for_each_entry(bc, &blocked_consumers, list) { | |
204 | n_fds++; | |
205 | } | |
206 | ||
207 | fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd)); | |
208 | if(fds == NULL) { | |
209 | ERR("malloc returned NULL"); | |
210 | return; | |
211 | } | |
212 | ||
213 | list_for_each_entry(bc, &blocked_consumers, list) { | |
214 | fds[idx].fd = bc->fd_producer; | |
215 | fds[idx].events = POLLIN; | |
216 | bc->tmp_poll_idx = idx; | |
217 | idx++; | |
218 | } | |
219 | ||
69ba0156 PMF |
220 | while((result = poll(fds, n_fds, 0)) == -1 && errno == EINTR) |
221 | /* nothing */; | |
3a7b90de PMF |
222 | if(result == -1) { |
223 | PERROR("poll"); | |
872037bb | 224 | return; |
3a7b90de PMF |
225 | } |
226 | ||
227 | list_for_each_entry(bc, &blocked_consumers, list) { | |
228 | if(fds[bc->tmp_poll_idx].revents) { | |
229 | long consumed_old = 0; | |
230 | char *reply; | |
231 | ||
232 | result = read(bc->fd_producer, &inbuf, 1); | |
233 | if(result == -1) { | |
234 | PERROR("read"); | |
235 | continue; | |
236 | } | |
237 | if(result == 0) { | |
238 | DBG("PRODUCER END"); | |
239 | ||
240 | close(bc->fd_producer); | |
241 | ||
769d0157 | 242 | list_del(&bc->list); |
3a7b90de PMF |
243 | |
244 | result = ustcomm_send_reply(&bc->server, "END", &bc->src); | |
245 | if(result < 0) { | |
246 | ERR("ustcomm_send_reply failed"); | |
247 | continue; | |
248 | } | |
249 | ||
250 | continue; | |
251 | } | |
252 | ||
253 | result = ltt_do_get_subbuf(bc->rbuf, bc->lttbuf, &consumed_old); | |
254 | if(result == -EAGAIN) { | |
255 | WARN("missed buffer?"); | |
256 | continue; | |
257 | } | |
258 | else if(result < 0) { | |
259 | DBG("ltt_do_get_subbuf: error: %s", strerror(-result)); | |
260 | } | |
261 | asprintf(&reply, "%s %ld", "OK", consumed_old); | |
262 | result = ustcomm_send_reply(&bc->server, reply, &bc->src); | |
263 | if(result < 0) { | |
264 | ERR("ustcomm_send_reply failed"); | |
265 | free(reply); | |
266 | continue; | |
267 | } | |
268 | free(reply); | |
269 | ||
769d0157 | 270 | list_del(&bc->list); |
3a7b90de PMF |
271 | } |
272 | } | |
273 | ||
274 | } | |
275 | ||
872037bb | 276 | void *listener_main(void *p) |
98963de4 PMF |
277 | { |
278 | int result; | |
279 | ||
26cc7017 PMF |
280 | ust_register_thread(); |
281 | ||
b0540e11 PMF |
282 | DBG("LISTENER"); |
283 | ||
98963de4 | 284 | for(;;) { |
aafb1650 PMF |
285 | char trace_name[] = "auto"; |
286 | char trace_type[] = "ustrelay"; | |
d0b5f2b9 PMF |
287 | char *recvbuf; |
288 | int len; | |
b02e31e5 | 289 | struct ustcomm_source src; |
98963de4 | 290 | |
3a7b90de PMF |
291 | process_blocked_consumers(); |
292 | ||
293 | result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5); | |
294 | if(result < 0) { | |
d0b5f2b9 PMF |
295 | WARN("error in ustcomm_app_recv_message"); |
296 | continue; | |
297 | } | |
3a7b90de PMF |
298 | else if(result == 0) { |
299 | /* no message */ | |
300 | continue; | |
301 | } | |
98963de4 | 302 | |
08230db7 | 303 | DBG("received a message! it's: %s", recvbuf); |
d0b5f2b9 | 304 | len = strlen(recvbuf); |
98963de4 | 305 | |
d0b5f2b9 | 306 | if(!strcmp(recvbuf, "print_markers")) { |
52c51a47 PMF |
307 | print_markers(stderr); |
308 | } | |
309 | else if(!strcmp(recvbuf, "list_markers")) { | |
310 | char *ptr; | |
311 | size_t size; | |
312 | FILE *fp; | |
313 | ||
314 | fp = open_memstream(&ptr, &size); | |
315 | print_markers(fp); | |
316 | fclose(fp); | |
317 | ||
318 | result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src); | |
319 | ||
320 | free(ptr); | |
321 | } | |
322 | else if(!strcmp(recvbuf, "start")) { | |
323 | /* start is an operation that setups the trace, allocates it and starts it */ | |
324 | result = ltt_trace_setup(trace_name); | |
325 | if(result < 0) { | |
326 | ERR("ltt_trace_setup failed"); | |
872037bb | 327 | return (void *)1; |
52c51a47 PMF |
328 | } |
329 | ||
330 | result = ltt_trace_set_type(trace_name, trace_type); | |
331 | if(result < 0) { | |
332 | ERR("ltt_trace_set_type failed"); | |
872037bb | 333 | return (void *)1; |
52c51a47 PMF |
334 | } |
335 | ||
336 | result = ltt_trace_alloc(trace_name); | |
337 | if(result < 0) { | |
338 | ERR("ltt_trace_alloc failed"); | |
872037bb | 339 | return (void *)1; |
52c51a47 PMF |
340 | } |
341 | ||
ad45e833 | 342 | inform_consumer_daemon(trace_name); |
52c51a47 PMF |
343 | |
344 | result = ltt_trace_start(trace_name); | |
345 | if(result < 0) { | |
346 | ERR("ltt_trace_start failed"); | |
347 | continue; | |
348 | } | |
d0b5f2b9 PMF |
349 | } |
350 | else if(!strcmp(recvbuf, "trace_setup")) { | |
351 | DBG("trace setup"); | |
fbd8191b | 352 | |
d0b5f2b9 PMF |
353 | result = ltt_trace_setup(trace_name); |
354 | if(result < 0) { | |
355 | ERR("ltt_trace_setup failed"); | |
872037bb | 356 | return (void *)1; |
fbd8191b | 357 | } |
d0b5f2b9 PMF |
358 | |
359 | result = ltt_trace_set_type(trace_name, trace_type); | |
360 | if(result < 0) { | |
361 | ERR("ltt_trace_set_type failed"); | |
872037bb | 362 | return (void *)1; |
fbd8191b | 363 | } |
d0b5f2b9 PMF |
364 | } |
365 | else if(!strcmp(recvbuf, "trace_alloc")) { | |
366 | DBG("trace alloc"); | |
367 | ||
368 | result = ltt_trace_alloc(trace_name); | |
369 | if(result < 0) { | |
370 | ERR("ltt_trace_alloc failed"); | |
872037bb | 371 | return (void *)1; |
fbd8191b | 372 | } |
d0b5f2b9 PMF |
373 | } |
374 | else if(!strcmp(recvbuf, "trace_start")) { | |
375 | DBG("trace start"); | |
376 | ||
377 | result = ltt_trace_start(trace_name); | |
378 | if(result < 0) { | |
379 | ERR("ltt_trace_start failed"); | |
380 | continue; | |
fbd8191b | 381 | } |
d0b5f2b9 PMF |
382 | } |
383 | else if(!strcmp(recvbuf, "trace_stop")) { | |
384 | DBG("trace stop"); | |
385 | ||
386 | result = ltt_trace_stop(trace_name); | |
387 | if(result < 0) { | |
388 | ERR("ltt_trace_stop failed"); | |
872037bb | 389 | return (void *)1; |
aafb1650 | 390 | } |
d0b5f2b9 PMF |
391 | } |
392 | else if(!strcmp(recvbuf, "trace_destroy")) { | |
aafb1650 | 393 | |
d0b5f2b9 | 394 | DBG("trace destroy"); |
aafb1650 | 395 | |
d0b5f2b9 PMF |
396 | result = ltt_trace_destroy(trace_name); |
397 | if(result < 0) { | |
398 | ERR("ltt_trace_destroy failed"); | |
872037bb | 399 | return (void *)1; |
fbd8191b | 400 | } |
98963de4 | 401 | } |
b02e31e5 | 402 | else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) { |
3847c3ba PMF |
403 | struct ltt_trace_struct *trace; |
404 | char trace_name[] = "auto"; | |
405 | int i; | |
811e4b93 | 406 | char *channel_name; |
3847c3ba PMF |
407 | |
408 | DBG("get_shmid"); | |
409 | ||
811e4b93 PMF |
410 | channel_name = nth_token(recvbuf, 1); |
411 | if(channel_name == NULL) { | |
412 | ERR("get_shmid: cannot parse channel"); | |
413 | goto next_cmd; | |
414 | } | |
415 | ||
3847c3ba PMF |
416 | ltt_lock_traces(); |
417 | trace = _ltt_trace_find(trace_name); | |
418 | ltt_unlock_traces(); | |
419 | ||
420 | if(trace == NULL) { | |
63fe14e5 | 421 | ERR("cannot find trace!"); |
872037bb | 422 | return (void *)1; |
3847c3ba PMF |
423 | } |
424 | ||
425 | for(i=0; i<trace->nr_channels; i++) { | |
426 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
427 | struct rchan_buf *rbuf = rchan->buf; | |
8cefc145 | 428 | struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)rchan->private_data; |
3847c3ba | 429 | |
811e4b93 PMF |
430 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
431 | char *reply; | |
432 | ||
433 | DBG("the shmid for the requested channel is %d", rbuf->shmid); | |
8cefc145 PMF |
434 | DBG("the shmid for its buffer structure is %d", ltt_channel->buf_shmid); |
435 | asprintf(&reply, "%d %d", rbuf->shmid, ltt_channel->buf_shmid); | |
811e4b93 PMF |
436 | |
437 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
438 | if(result) { | |
439 | ERR("listener: get_shmid: ustcomm_send_reply failed"); | |
440 | goto next_cmd; | |
441 | } | |
442 | ||
443 | free(reply); | |
444 | ||
445 | break; | |
446 | } | |
447 | } | |
f293009f PMF |
448 | |
449 | buffers_to_export--; | |
811e4b93 PMF |
450 | } |
451 | else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) { | |
452 | struct ltt_trace_struct *trace; | |
453 | char trace_name[] = "auto"; | |
454 | int i; | |
455 | char *channel_name; | |
456 | ||
457 | DBG("get_n_subbufs"); | |
458 | ||
459 | channel_name = nth_token(recvbuf, 1); | |
460 | if(channel_name == NULL) { | |
461 | ERR("get_n_subbufs: cannot parse channel"); | |
462 | goto next_cmd; | |
463 | } | |
464 | ||
465 | ltt_lock_traces(); | |
466 | trace = _ltt_trace_find(trace_name); | |
467 | ltt_unlock_traces(); | |
468 | ||
469 | if(trace == NULL) { | |
63fe14e5 | 470 | ERR("cannot find trace!"); |
872037bb | 471 | return (void *)1; |
811e4b93 PMF |
472 | } |
473 | ||
474 | for(i=0; i<trace->nr_channels; i++) { | |
475 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
476 | ||
477 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
478 | char *reply; | |
479 | ||
872037bb PMF |
480 | DBG("the n_subbufs for the requested channel is %zd", rchan->n_subbufs); |
481 | asprintf(&reply, "%zd", rchan->n_subbufs); | |
811e4b93 PMF |
482 | |
483 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
484 | if(result) { | |
485 | ERR("listener: get_n_subbufs: ustcomm_send_reply failed"); | |
486 | goto next_cmd; | |
487 | } | |
488 | ||
489 | free(reply); | |
490 | ||
491 | break; | |
492 | } | |
493 | } | |
494 | } | |
495 | else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) { | |
496 | struct ltt_trace_struct *trace; | |
497 | char trace_name[] = "auto"; | |
498 | int i; | |
499 | char *channel_name; | |
500 | ||
501 | DBG("get_subbuf_size"); | |
502 | ||
503 | channel_name = nth_token(recvbuf, 1); | |
504 | if(channel_name == NULL) { | |
505 | ERR("get_subbuf_size: cannot parse channel"); | |
506 | goto next_cmd; | |
507 | } | |
508 | ||
509 | ltt_lock_traces(); | |
510 | trace = _ltt_trace_find(trace_name); | |
511 | ltt_unlock_traces(); | |
512 | ||
513 | if(trace == NULL) { | |
63fe14e5 | 514 | ERR("cannot find trace!"); |
872037bb | 515 | return (void *)1; |
811e4b93 PMF |
516 | } |
517 | ||
518 | for(i=0; i<trace->nr_channels; i++) { | |
519 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
520 | ||
521 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
522 | char *reply; | |
523 | ||
872037bb PMF |
524 | DBG("the subbuf_size for the requested channel is %zd", rchan->subbuf_size); |
525 | asprintf(&reply, "%zd", rchan->subbuf_size); | |
811e4b93 PMF |
526 | |
527 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
528 | if(result) { | |
529 | ERR("listener: get_subbuf_size: ustcomm_send_reply failed"); | |
530 | goto next_cmd; | |
531 | } | |
532 | ||
533 | free(reply); | |
3847c3ba | 534 | |
811e4b93 PMF |
535 | break; |
536 | } | |
3847c3ba PMF |
537 | } |
538 | } | |
b02e31e5 PMF |
539 | else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { |
540 | char *libfile; | |
541 | ||
542 | libfile = nth_token(recvbuf, 1); | |
543 | ||
544 | DBG("load_probe_lib loading %s", libfile); | |
545 | } | |
688760ef PMF |
546 | else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) { |
547 | struct ltt_trace_struct *trace; | |
548 | char trace_name[] = "auto"; | |
549 | int i; | |
550 | char *channel_name; | |
551 | ||
552 | DBG("get_subbuf"); | |
553 | ||
554 | channel_name = nth_token(recvbuf, 1); | |
555 | if(channel_name == NULL) { | |
556 | ERR("get_subbuf: cannot parse channel"); | |
557 | goto next_cmd; | |
558 | } | |
559 | ||
560 | ltt_lock_traces(); | |
561 | trace = _ltt_trace_find(trace_name); | |
562 | ltt_unlock_traces(); | |
563 | ||
564 | if(trace == NULL) { | |
63fe14e5 | 565 | ERR("cannot find trace!"); |
872037bb | 566 | return (void *)1; |
688760ef PMF |
567 | } |
568 | ||
569 | for(i=0; i<trace->nr_channels; i++) { | |
570 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
571 | ||
572 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
573 | struct rchan_buf *rbuf = rchan->buf; | |
574 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
3a7b90de | 575 | struct blocked_consumer *bc; |
688760ef | 576 | |
3a7b90de PMF |
577 | bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer)); |
578 | if(bc == NULL) { | |
579 | ERR("malloc returned NULL"); | |
688760ef PMF |
580 | goto next_cmd; |
581 | } | |
3a7b90de PMF |
582 | bc->fd_consumer = src.fd; |
583 | bc->fd_producer = lttbuf->data_ready_fd_read; | |
584 | bc->rbuf = rbuf; | |
585 | bc->lttbuf = lttbuf; | |
586 | bc->src = src; | |
587 | bc->server = ustcomm_app.server; | |
688760ef | 588 | |
3a7b90de | 589 | list_add(&bc->list, &blocked_consumers); |
688760ef PMF |
590 | |
591 | break; | |
592 | } | |
593 | } | |
594 | } | |
595 | else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) { | |
596 | struct ltt_trace_struct *trace; | |
597 | char trace_name[] = "auto"; | |
598 | int i; | |
599 | char *channel_name; | |
600 | long consumed_old; | |
601 | char *consumed_old_str; | |
602 | char *endptr; | |
603 | ||
604 | DBG("put_subbuf"); | |
605 | ||
606 | channel_name = strdup_malloc(nth_token(recvbuf, 1)); | |
607 | if(channel_name == NULL) { | |
608 | ERR("put_subbuf_size: cannot parse channel"); | |
609 | goto next_cmd; | |
610 | } | |
611 | ||
612 | consumed_old_str = strdup_malloc(nth_token(recvbuf, 2)); | |
613 | if(consumed_old_str == NULL) { | |
614 | ERR("put_subbuf: cannot parse consumed_old"); | |
615 | goto next_cmd; | |
616 | } | |
617 | consumed_old = strtol(consumed_old_str, &endptr, 10); | |
618 | if(*endptr != '\0') { | |
619 | ERR("put_subbuf: invalid value for consumed_old"); | |
620 | goto next_cmd; | |
621 | } | |
622 | ||
623 | ltt_lock_traces(); | |
624 | trace = _ltt_trace_find(trace_name); | |
625 | ltt_unlock_traces(); | |
626 | ||
627 | if(trace == NULL) { | |
63fe14e5 | 628 | ERR("cannot find trace!"); |
872037bb | 629 | return (void *)1; |
688760ef PMF |
630 | } |
631 | ||
632 | for(i=0; i<trace->nr_channels; i++) { | |
633 | struct rchan *rchan = trace->channels[i].trans_channel_data; | |
634 | ||
635 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
636 | struct rchan_buf *rbuf = rchan->buf; | |
637 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
638 | char *reply; | |
639 | long consumed_old=0; | |
640 | ||
641 | result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old); | |
642 | if(result < 0) { | |
0a58610f | 643 | WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name); |
872037bb | 644 | asprintf(&reply, "%s", "ERROR"); |
688760ef PMF |
645 | } |
646 | else { | |
0a58610f | 647 | DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name); |
872037bb | 648 | asprintf(&reply, "%s", "OK"); |
688760ef | 649 | } |
688760ef PMF |
650 | |
651 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
652 | if(result) { | |
653 | ERR("listener: put_subbuf: ustcomm_send_reply failed"); | |
654 | goto next_cmd; | |
655 | } | |
656 | ||
657 | free(reply); | |
658 | ||
659 | break; | |
660 | } | |
661 | } | |
662 | ||
663 | free(channel_name); | |
664 | free(consumed_old_str); | |
665 | } | |
52c51a47 PMF |
666 | else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) { |
667 | char *channel_slash_name = nth_token(recvbuf, 1); | |
668 | char channel_name[256]=""; | |
669 | char marker_name[256]=""; | |
52c51a47 PMF |
670 | |
671 | result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name); | |
672 | ||
673 | if(channel_name == NULL || marker_name == NULL) { | |
674 | WARN("invalid marker name"); | |
675 | goto next_cmd; | |
676 | } | |
677 | printf("%s %s\n", channel_name, marker_name); | |
678 | ||
679 | result = ltt_marker_connect(channel_name, marker_name, "default"); | |
680 | if(result < 0) { | |
681 | WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name); | |
682 | } | |
683 | } | |
684 | else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) { | |
685 | char *channel_slash_name = nth_token(recvbuf, 1); | |
686 | char *marker_name; | |
687 | char *channel_name; | |
52c51a47 PMF |
688 | |
689 | result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name); | |
690 | ||
691 | if(marker_name == NULL) { | |
692 | } | |
693 | printf("%s %s\n", channel_name, marker_name); | |
694 | ||
695 | result = ltt_marker_disconnect(channel_name, marker_name, "default"); | |
696 | if(result < 0) { | |
697 | WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name); | |
698 | } | |
699 | } | |
ed1317e7 PMF |
700 | else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) { |
701 | char *reply; | |
702 | ||
703 | asprintf(&reply, "%lld", pidunique); | |
704 | ||
705 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); | |
706 | if(result) { | |
707 | ERR("listener: get_pidunique: ustcomm_send_reply failed"); | |
708 | goto next_cmd; | |
709 | } | |
710 | ||
711 | free(reply); | |
712 | } | |
3a7b90de PMF |
713 | // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) { |
714 | // struct ltt_trace_struct *trace; | |
715 | // char trace_name[] = "auto"; | |
716 | // int i; | |
717 | // char *channel_name; | |
718 | // | |
719 | // DBG("get_notifications"); | |
720 | // | |
721 | // channel_name = strdup_malloc(nth_token(recvbuf, 1)); | |
722 | // if(channel_name == NULL) { | |
723 | // ERR("put_subbuf_size: cannot parse channel"); | |
724 | // goto next_cmd; | |
725 | // } | |
726 | // | |
727 | // ltt_lock_traces(); | |
728 | // trace = _ltt_trace_find(trace_name); | |
729 | // ltt_unlock_traces(); | |
730 | // | |
731 | // if(trace == NULL) { | |
63fe14e5 | 732 | // ERR("cannot find trace!"); |
872037bb | 733 | // return (void *)1; |
3a7b90de PMF |
734 | // } |
735 | // | |
736 | // for(i=0; i<trace->nr_channels; i++) { | |
737 | // struct rchan *rchan = trace->channels[i].trans_channel_data; | |
738 | // int fd; | |
739 | // | |
740 | // if(!strcmp(trace->channels[i].channel_name, channel_name)) { | |
741 | // struct rchan_buf *rbuf = rchan->buf; | |
742 | // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; | |
743 | // | |
744 | // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src); | |
745 | // if(result == -1) { | |
746 | // ERR("ustcomm_app_detach_client failed"); | |
747 | // goto next_cmd; | |
748 | // } | |
749 | // | |
750 | // lttbuf->wake_consumer_arg = (void *) fd; | |
751 | // | |
752 | // smp_wmb(); | |
753 | // | |
754 | // lttbuf->call_wake_consumer = 1; | |
755 | // | |
756 | // break; | |
757 | // } | |
758 | // } | |
759 | // | |
760 | // free(channel_name); | |
761 | // } | |
688760ef PMF |
762 | else { |
763 | ERR("unable to parse message: %s", recvbuf); | |
764 | } | |
d0b5f2b9 | 765 | |
811e4b93 | 766 | next_cmd: |
d0b5f2b9 | 767 | free(recvbuf); |
98963de4 PMF |
768 | } |
769 | } | |
770 | ||
7958de40 | 771 | volatile sig_atomic_t have_listener = 0; |
4440ebcb | 772 | |
98963de4 PMF |
773 | void create_listener(void) |
774 | { | |
9160b4e4 | 775 | #ifdef USE_CLONE |
98963de4 | 776 | static char listener_stack[16384]; |
c5fdc888 | 777 | int result; |
4440ebcb PMF |
778 | #else |
779 | pthread_t thread; | |
9160b4e4 | 780 | #endif |
98963de4 | 781 | |
c5fdc888 PMF |
782 | if(have_listener) { |
783 | WARN("not creating listener because we already had one"); | |
4440ebcb | 784 | return; |
c5fdc888 | 785 | } |
4440ebcb | 786 | |
3847c3ba | 787 | #ifdef USE_CLONE |
c5fdc888 | 788 | result = clone((int (*)(void *)) listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); |
98963de4 PMF |
789 | if(result == -1) { |
790 | perror("clone"); | |
4440ebcb | 791 | return; |
98963de4 | 792 | } |
3847c3ba | 793 | #else |
b0540e11 | 794 | |
3847c3ba PMF |
795 | pthread_create(&thread, NULL, listener_main, NULL); |
796 | #endif | |
4440ebcb PMF |
797 | |
798 | have_listener = 1; | |
98963de4 PMF |
799 | } |
800 | ||
98963de4 | 801 | static int init_socket(void) |
68c1021b | 802 | { |
d0b5f2b9 | 803 | return ustcomm_init_app(getpid(), &ustcomm_app); |
68c1021b PMF |
804 | } |
805 | ||
5de74e51 PMF |
806 | #define AUTOPROBE_DISABLED 0 |
807 | #define AUTOPROBE_ENABLE_ALL 1 | |
808 | #define AUTOPROBE_ENABLE_REGEX 2 | |
809 | static int autoprobe_method = AUTOPROBE_DISABLED; | |
810 | static regex_t autoprobe_regex; | |
ef290fca | 811 | |
20b37a31 | 812 | static void auto_probe_connect(struct marker *m) |
68c1021b PMF |
813 | { |
814 | int result; | |
815 | ||
5de74e51 PMF |
816 | char* concat_name = NULL; |
817 | const char *probe_name = "default"; | |
ef290fca | 818 | |
5de74e51 | 819 | if(autoprobe_method == AUTOPROBE_DISABLED) { |
ef290fca PMF |
820 | return; |
821 | } | |
5de74e51 PMF |
822 | else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) { |
823 | result = asprintf(&concat_name, "%s/%s", m->channel, m->name); | |
824 | if(result == -1) { | |
825 | ERR("auto_probe_connect: asprintf failed (marker %s/%s)", | |
826 | m->channel, m->name); | |
827 | return; | |
828 | } | |
829 | if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) { | |
830 | free(concat_name); | |
831 | return; | |
832 | } | |
833 | free(concat_name); | |
ef290fca PMF |
834 | } |
835 | ||
5de74e51 | 836 | result = ltt_marker_connect(m->channel, m->name, probe_name); |
acbf228b PMF |
837 | if(result && result != -EEXIST) |
838 | ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result); | |
20b37a31 | 839 | |
3ea1e2fc | 840 | DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name); |
ef290fca | 841 | |
20b37a31 PMF |
842 | } |
843 | ||
c1083aa8 | 844 | static void __attribute__((constructor)) init() |
20b37a31 PMF |
845 | { |
846 | int result; | |
5de74e51 | 847 | char* autoprobe_val = NULL; |
20b37a31 | 848 | |
ed1317e7 PMF |
849 | /* Assign the pidunique, to be able to differentiate the processes with same |
850 | * pid, (before and after an exec). | |
851 | */ | |
852 | pidunique = make_pidunique(); | |
853 | ||
4440ebcb PMF |
854 | /* Initialize RCU in case the constructor order is not good. */ |
855 | urcu_init(); | |
856 | ||
857 | /* It is important to do this before events start to be generated. */ | |
858 | ust_register_thread(); | |
859 | ||
5de74e51 | 860 | DBG("Tracectl constructor"); |
20b37a31 | 861 | |
3847c3ba PMF |
862 | /* Must create socket before signal handler to prevent races. |
863 | */ | |
864 | result = init_socket(); | |
865 | if(result == -1) { | |
866 | ERR("init_socket error"); | |
867 | return; | |
868 | } | |
2944a629 PMF |
869 | |
870 | create_listener(); | |
68c1021b | 871 | |
5de74e51 PMF |
872 | autoprobe_val = getenv("UST_AUTOPROBE"); |
873 | if(autoprobe_val) { | |
4440ebcb PMF |
874 | struct marker_iter iter; |
875 | ||
08230db7 | 876 | DBG("Autoprobe enabled."); |
4440ebcb PMF |
877 | |
878 | /* Ensure markers are initialized */ | |
879 | //init_markers(); | |
880 | ||
881 | /* Ensure marker control is initialized, for the probe */ | |
882 | init_marker_control(); | |
883 | ||
884 | /* first, set the callback that will connect the | |
885 | * probe on new markers | |
886 | */ | |
5de74e51 PMF |
887 | if(autoprobe_val[0] == '/') { |
888 | result = regcomp(&autoprobe_regex, autoprobe_val+1, 0); | |
889 | if (result) { | |
890 | char regexerr[150]; | |
891 | ||
892 | regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr)); | |
893 | ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr); | |
894 | /* don't crash the application just for this */ | |
895 | } | |
896 | else { | |
897 | autoprobe_method = AUTOPROBE_ENABLE_REGEX; | |
898 | } | |
ef290fca | 899 | } |
5de74e51 PMF |
900 | else { |
901 | /* just enable all instrumentation */ | |
902 | autoprobe_method = AUTOPROBE_ENABLE_ALL; | |
903 | } | |
904 | ||
905 | marker_set_new_marker_cb(auto_probe_connect); | |
906 | ||
4440ebcb PMF |
907 | /* Now, connect the probes that were already registered. */ |
908 | marker_iter_reset(&iter); | |
909 | marker_iter_start(&iter); | |
910 | ||
08230db7 | 911 | DBG("now iterating on markers already registered"); |
4440ebcb | 912 | while(iter.marker) { |
08230db7 | 913 | DBG("now iterating on marker %s", iter.marker->name); |
4440ebcb PMF |
914 | auto_probe_connect(iter.marker); |
915 | marker_iter_next(&iter); | |
916 | } | |
917 | } | |
918 | ||
4db647c5 PMF |
919 | if(getenv("UST_TRACE")) { |
920 | char trace_name[] = "auto"; | |
921 | char trace_type[] = "ustrelay"; | |
922 | ||
923 | DBG("starting early tracing"); | |
924 | ||
925 | /* Ensure marker control is initialized */ | |
926 | init_marker_control(); | |
927 | ||
928 | /* Ensure relay is initialized */ | |
929 | init_ustrelay_transport(); | |
930 | ||
931 | /* Ensure markers are initialized */ | |
932 | init_markers(); | |
933 | ||
20b37a31 PMF |
934 | /* In case. */ |
935 | ltt_channels_register("ust"); | |
4db647c5 PMF |
936 | |
937 | result = ltt_trace_setup(trace_name); | |
938 | if(result < 0) { | |
939 | ERR("ltt_trace_setup failed"); | |
940 | return; | |
941 | } | |
942 | ||
943 | result = ltt_trace_set_type(trace_name, trace_type); | |
944 | if(result < 0) { | |
945 | ERR("ltt_trace_set_type failed"); | |
946 | return; | |
947 | } | |
948 | ||
949 | result = ltt_trace_alloc(trace_name); | |
950 | if(result < 0) { | |
951 | ERR("ltt_trace_alloc failed"); | |
952 | return; | |
953 | } | |
954 | ||
955 | result = ltt_trace_start(trace_name); | |
956 | if(result < 0) { | |
957 | ERR("ltt_trace_start failed"); | |
958 | return; | |
959 | } | |
60e57148 PMF |
960 | |
961 | /* Do this after the trace is started in order to avoid creating confusion | |
962 | * if the trace fails to start. */ | |
963 | inform_consumer_daemon(trace_name); | |
4db647c5 PMF |
964 | } |
965 | ||
68c1021b PMF |
966 | |
967 | return; | |
968 | ||
969 | /* should decrementally destroy stuff if error */ | |
970 | ||
971 | } | |
972 | ||
973 | /* This is only called if we terminate normally, not with an unhandled signal, | |
6d45c11a PMF |
974 | * so we cannot rely on it. However, for now, LTTV requires that the header of |
975 | * the last sub-buffer contain a valid end time for the trace. This is done | |
976 | * automatically only when the trace is properly stopped. | |
977 | * | |
978 | * If the traced program crashed, it is always possible to manually add the | |
979 | * right value in the header, or to open the trace in text mode. | |
980 | * | |
981 | * FIXME: Fix LTTV so it doesn't need this. | |
982 | */ | |
68c1021b | 983 | |
6d45c11a | 984 | static void destroy_traces(void) |
68c1021b | 985 | { |
6d45c11a | 986 | int result; |
a584bc4e PMF |
987 | |
988 | /* if trace running, finish it */ | |
989 | ||
6d45c11a | 990 | DBG("destructor stopping traces"); |
a584bc4e | 991 | |
6d45c11a PMF |
992 | result = ltt_trace_stop("auto"); |
993 | if(result == -1) { | |
994 | ERR("ltt_trace_stop error"); | |
995 | } | |
996 | ||
997 | result = ltt_trace_destroy("auto"); | |
998 | if(result == -1) { | |
999 | ERR("ltt_trace_destroy error"); | |
1000 | } | |
68c1021b | 1001 | } |
1e2944cb | 1002 | |
97d9b88b PMF |
1003 | static int trace_recording(void) |
1004 | { | |
1005 | int retval = 0; | |
1006 | struct ltt_trace_struct *trace; | |
1007 | ||
1008 | ltt_lock_traces(); | |
1009 | ||
1010 | list_for_each_entry(trace, <t_traces.head, list) { | |
1011 | if(trace->active) { | |
1012 | retval = 1; | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | ltt_unlock_traces(); | |
1018 | ||
1019 | return retval; | |
1020 | } | |
1021 | ||
f293009f | 1022 | #if 0 |
97d9b88b PMF |
1023 | static int have_consumer(void) |
1024 | { | |
1025 | return !list_empty(&blocked_consumers); | |
1026 | } | |
f293009f | 1027 | #endif |
97d9b88b | 1028 | |
f293009f | 1029 | int restarting_usleep(useconds_t usecs) |
97d9b88b PMF |
1030 | { |
1031 | struct timespec tv; | |
1032 | int result; | |
1033 | ||
f293009f PMF |
1034 | tv.tv_sec = 0; |
1035 | tv.tv_nsec = usecs * 1000; | |
97d9b88b PMF |
1036 | |
1037 | do { | |
1038 | result = nanosleep(&tv, &tv); | |
1039 | } while(result == -1 && errno == EINTR); | |
1040 | ||
1041 | return result; | |
1042 | } | |
1043 | ||
f293009f PMF |
1044 | /* This destructor keeps the process alive for a few seconds in order |
1045 | * to leave time to ustd to connect to its buffers. This is necessary | |
1046 | * for programs whose execution is very short. It is also useful in all | |
1047 | * programs when tracing is started close to the end of the program | |
1048 | * execution. | |
1049 | * | |
1050 | * FIXME: For now, this only works for the first trace created in a | |
1051 | * process. | |
1052 | */ | |
1053 | ||
97d9b88b PMF |
1054 | static void __attribute__((destructor)) keepalive() |
1055 | { | |
f293009f | 1056 | if(trace_recording() && buffers_to_export) { |
c472cce0 | 1057 | int total = 0; |
f293009f PMF |
1058 | DBG("Keeping process alive for consumer daemon..."); |
1059 | while(buffers_to_export) { | |
1060 | const int interv = 200000; | |
c472cce0 | 1061 | restarting_usleep(interv); |
f293009f PMF |
1062 | total += interv; |
1063 | ||
1064 | if(total >= 3000000) { | |
c472cce0 | 1065 | WARN("non-consumed buffers remaining after wait limit; not waiting anymore"); |
f293009f PMF |
1066 | break; |
1067 | } | |
1068 | } | |
1069 | DBG("Finally dying..."); | |
1070 | } | |
6d45c11a PMF |
1071 | |
1072 | destroy_traces(); | |
1073 | ||
1074 | ustcomm_fini_app(&ustcomm_app); | |
97d9b88b | 1075 | } |
97d9b88b | 1076 | |
775c8a3f | 1077 | void ust_potential_exec(void) |
c396a841 PMF |
1078 | { |
1079 | trace_mark(ust, potential_exec, MARK_NOARGS); | |
1080 | ||
775c8a3f PMF |
1081 | DBG("test"); |
1082 | ||
c396a841 PMF |
1083 | keepalive(); |
1084 | } | |
1085 | ||
1e2944cb PMF |
1086 | /* Notify ust that there was a fork. This needs to be called inside |
1087 | * the new process, anytime a process whose memory is not shared with | |
1088 | * the parent is created. If this function is not called, the events | |
1089 | * of the new process will not be collected. | |
1090 | */ | |
1091 | ||
775c8a3f | 1092 | void ust_fork(void) |
1e2944cb | 1093 | { |
99b72dc0 PMF |
1094 | struct blocked_consumer *bc; |
1095 | struct blocked_consumer *deletable_bc = NULL; | |
1096 | int result; | |
1097 | ||
1e2944cb PMF |
1098 | DBG("ust: forking"); |
1099 | ltt_trace_stop("auto"); | |
1100 | ltt_trace_destroy("auto"); | |
99b72dc0 PMF |
1101 | /* Delete all active connections */ |
1102 | ustcomm_close_all_connections(&ustcomm_app.server); | |
1103 | ||
1104 | /* Delete all blocked consumers */ | |
1105 | list_for_each_entry(bc, &blocked_consumers, list) { | |
d9ce395d PMF |
1106 | close(bc->fd_producer); |
1107 | close(bc->fd_consumer); | |
99b72dc0 PMF |
1108 | free(deletable_bc); |
1109 | deletable_bc = bc; | |
1110 | list_del(&bc->list); | |
1111 | } | |
1112 | ||
1e2944cb PMF |
1113 | have_listener = 0; |
1114 | create_listener(); | |
99b72dc0 PMF |
1115 | init_socket(); |
1116 | ltt_trace_setup("auto"); | |
1117 | result = ltt_trace_set_type("auto", "ustrelay"); | |
1118 | if(result < 0) { | |
1119 | ERR("ltt_trace_set_type failed"); | |
036db133 | 1120 | return; |
99b72dc0 PMF |
1121 | } |
1122 | ||
1123 | ltt_trace_alloc("auto"); | |
1124 | ltt_trace_start("auto"); | |
ad45e833 | 1125 | inform_consumer_daemon("auto"); |
1e2944cb PMF |
1126 | } |
1127 |