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