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