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