| 1 | #define _GNU_SOURCE |
| 2 | #include <stdio.h> |
| 3 | #include <stdint.h> |
| 4 | #include <signal.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <sys/un.h> |
| 8 | #include <sched.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <poll.h> |
| 11 | |
| 12 | #include "marker.h" |
| 13 | #include "tracer.h" |
| 14 | #include "localerr.h" |
| 15 | #include "ustcomm.h" |
| 16 | #include "relay.h" /* FIXME: remove */ |
| 17 | |
| 18 | //#define USE_CLONE |
| 19 | |
| 20 | #define USTSIGNAL SIGIO |
| 21 | |
| 22 | #define MAX_MSG_SIZE (100) |
| 23 | #define MSG_NOTIF 1 |
| 24 | #define MSG_REGISTER_NOTIF 2 |
| 25 | |
| 26 | char consumer_stack[10000]; |
| 27 | |
| 28 | struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers); |
| 29 | |
| 30 | static struct ustcomm_app ustcomm_app; |
| 31 | |
| 32 | struct tracecmd { /* no padding */ |
| 33 | uint32_t size; |
| 34 | uint16_t command; |
| 35 | }; |
| 36 | |
| 37 | //struct listener_arg { |
| 38 | // int pipe_fd; |
| 39 | //}; |
| 40 | |
| 41 | struct trctl_msg { |
| 42 | /* size: the size of all the fields except size itself */ |
| 43 | uint32_t size; |
| 44 | uint16_t type; |
| 45 | /* Only the necessary part of the payload is transferred. It |
| 46 | * may even be none of it. |
| 47 | */ |
| 48 | char payload[94]; |
| 49 | }; |
| 50 | |
| 51 | struct consumer_channel { |
| 52 | int fd; |
| 53 | struct ltt_channel_struct *chan; |
| 54 | }; |
| 55 | |
| 56 | struct blocked_consumer { |
| 57 | int fd_consumer; |
| 58 | int fd_producer; |
| 59 | int tmp_poll_idx; |
| 60 | |
| 61 | /* args to ustcomm_send_reply */ |
| 62 | struct ustcomm_server server; |
| 63 | struct ustcomm_source src; |
| 64 | |
| 65 | /* args to ltt_do_get_subbuf */ |
| 66 | struct rchan_buf *rbuf; |
| 67 | struct ltt_channel_buf_struct *lttbuf; |
| 68 | |
| 69 | struct list_head list; |
| 70 | }; |
| 71 | |
| 72 | static void print_markers(FILE *fp) |
| 73 | { |
| 74 | struct marker_iter iter; |
| 75 | |
| 76 | lock_markers(); |
| 77 | marker_iter_reset(&iter); |
| 78 | marker_iter_start(&iter); |
| 79 | |
| 80 | while(iter.marker) { |
| 81 | fprintf(fp, "marker: %s_%s %d \"%s\"\n", iter.marker->channel, iter.marker->name, (int)imv_read(iter.marker->state), iter.marker->format); |
| 82 | marker_iter_next(&iter); |
| 83 | } |
| 84 | unlock_markers(); |
| 85 | } |
| 86 | |
| 87 | void do_command(struct tracecmd *cmd) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | void receive_commands() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | int fd_notif = -1; |
| 96 | void notif_cb(void) |
| 97 | { |
| 98 | int result; |
| 99 | struct trctl_msg msg; |
| 100 | |
| 101 | /* FIXME: fd_notif should probably be protected by a spinlock */ |
| 102 | |
| 103 | if(fd_notif == -1) |
| 104 | return; |
| 105 | |
| 106 | msg.type = MSG_NOTIF; |
| 107 | msg.size = sizeof(msg.type); |
| 108 | |
| 109 | /* FIXME: don't block here */ |
| 110 | result = write(fd_notif, &msg, msg.size+sizeof(msg.size)); |
| 111 | if(result == -1) { |
| 112 | PERROR("write"); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void inform_consumer_daemon(void) |
| 118 | { |
| 119 | ustcomm_request_consumer(getpid(), "metadata"); |
| 120 | ustcomm_request_consumer(getpid(), "ust"); |
| 121 | } |
| 122 | |
| 123 | void process_blocked_consumers(void) |
| 124 | { |
| 125 | int n_fds = 0; |
| 126 | struct pollfd *fds; |
| 127 | struct blocked_consumer *bc; |
| 128 | int idx = 0; |
| 129 | char inbuf; |
| 130 | int result; |
| 131 | |
| 132 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 133 | n_fds++; |
| 134 | } |
| 135 | |
| 136 | fds = (struct pollfd *) malloc(n_fds * sizeof(struct pollfd)); |
| 137 | if(fds == NULL) { |
| 138 | ERR("malloc returned NULL"); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 143 | fds[idx].fd = bc->fd_producer; |
| 144 | fds[idx].events = POLLIN; |
| 145 | bc->tmp_poll_idx = idx; |
| 146 | idx++; |
| 147 | } |
| 148 | |
| 149 | result = poll(fds, n_fds, 0); |
| 150 | if(result == -1) { |
| 151 | PERROR("poll"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 156 | if(fds[bc->tmp_poll_idx].revents) { |
| 157 | long consumed_old = 0; |
| 158 | char *reply; |
| 159 | |
| 160 | result = read(bc->fd_producer, &inbuf, 1); |
| 161 | if(result == -1) { |
| 162 | PERROR("read"); |
| 163 | continue; |
| 164 | } |
| 165 | if(result == 0) { |
| 166 | DBG("PRODUCER END"); |
| 167 | |
| 168 | close(bc->fd_producer); |
| 169 | |
| 170 | list_del(&bc->list); |
| 171 | |
| 172 | result = ustcomm_send_reply(&bc->server, "END", &bc->src); |
| 173 | if(result < 0) { |
| 174 | ERR("ustcomm_send_reply failed"); |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | result = ltt_do_get_subbuf(bc->rbuf, bc->lttbuf, &consumed_old); |
| 182 | if(result == -EAGAIN) { |
| 183 | WARN("missed buffer?"); |
| 184 | continue; |
| 185 | } |
| 186 | else if(result < 0) { |
| 187 | DBG("ltt_do_get_subbuf: error: %s", strerror(-result)); |
| 188 | } |
| 189 | asprintf(&reply, "%s %ld", "OK", consumed_old); |
| 190 | result = ustcomm_send_reply(&bc->server, reply, &bc->src); |
| 191 | if(result < 0) { |
| 192 | ERR("ustcomm_send_reply failed"); |
| 193 | free(reply); |
| 194 | continue; |
| 195 | } |
| 196 | free(reply); |
| 197 | |
| 198 | list_del(&bc->list); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | void *listener_main(void *p) |
| 205 | { |
| 206 | int result; |
| 207 | |
| 208 | DBG("LISTENER"); |
| 209 | |
| 210 | for(;;) { |
| 211 | uint32_t size; |
| 212 | struct sockaddr_un addr; |
| 213 | socklen_t addrlen = sizeof(addr); |
| 214 | char trace_name[] = "auto"; |
| 215 | char trace_type[] = "ustrelay"; |
| 216 | char *recvbuf; |
| 217 | int len; |
| 218 | struct ustcomm_source src; |
| 219 | |
| 220 | process_blocked_consumers(); |
| 221 | |
| 222 | result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src, 5); |
| 223 | if(result < 0) { |
| 224 | WARN("error in ustcomm_app_recv_message"); |
| 225 | continue; |
| 226 | } |
| 227 | else if(result == 0) { |
| 228 | /* no message */ |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | DBG("received a message! it's: %s\n", recvbuf); |
| 233 | len = strlen(recvbuf); |
| 234 | |
| 235 | if(!strcmp(recvbuf, "print_markers")) { |
| 236 | print_markers(stderr); |
| 237 | } |
| 238 | else if(!strcmp(recvbuf, "list_markers")) { |
| 239 | char *ptr; |
| 240 | size_t size; |
| 241 | FILE *fp; |
| 242 | |
| 243 | fp = open_memstream(&ptr, &size); |
| 244 | print_markers(fp); |
| 245 | fclose(fp); |
| 246 | |
| 247 | result = ustcomm_send_reply(&ustcomm_app.server, ptr, &src); |
| 248 | |
| 249 | free(ptr); |
| 250 | } |
| 251 | else if(!strcmp(recvbuf, "start")) { |
| 252 | /* start is an operation that setups the trace, allocates it and starts it */ |
| 253 | result = ltt_trace_setup(trace_name); |
| 254 | if(result < 0) { |
| 255 | ERR("ltt_trace_setup failed"); |
| 256 | return (void *)1; |
| 257 | } |
| 258 | |
| 259 | result = ltt_trace_set_type(trace_name, trace_type); |
| 260 | if(result < 0) { |
| 261 | ERR("ltt_trace_set_type failed"); |
| 262 | return (void *)1; |
| 263 | } |
| 264 | |
| 265 | result = ltt_trace_alloc(trace_name); |
| 266 | if(result < 0) { |
| 267 | ERR("ltt_trace_alloc failed"); |
| 268 | return (void *)1; |
| 269 | } |
| 270 | |
| 271 | inform_consumer_daemon(); |
| 272 | |
| 273 | result = ltt_trace_start(trace_name); |
| 274 | if(result < 0) { |
| 275 | ERR("ltt_trace_start failed"); |
| 276 | continue; |
| 277 | } |
| 278 | } |
| 279 | else if(!strcmp(recvbuf, "trace_setup")) { |
| 280 | DBG("trace setup"); |
| 281 | |
| 282 | result = ltt_trace_setup(trace_name); |
| 283 | if(result < 0) { |
| 284 | ERR("ltt_trace_setup failed"); |
| 285 | return (void *)1; |
| 286 | } |
| 287 | |
| 288 | result = ltt_trace_set_type(trace_name, trace_type); |
| 289 | if(result < 0) { |
| 290 | ERR("ltt_trace_set_type failed"); |
| 291 | return (void *)1; |
| 292 | } |
| 293 | } |
| 294 | else if(!strcmp(recvbuf, "trace_alloc")) { |
| 295 | DBG("trace alloc"); |
| 296 | |
| 297 | result = ltt_trace_alloc(trace_name); |
| 298 | if(result < 0) { |
| 299 | ERR("ltt_trace_alloc failed"); |
| 300 | return (void *)1; |
| 301 | } |
| 302 | } |
| 303 | else if(!strcmp(recvbuf, "trace_start")) { |
| 304 | DBG("trace start"); |
| 305 | |
| 306 | result = ltt_trace_start(trace_name); |
| 307 | if(result < 0) { |
| 308 | ERR("ltt_trace_start failed"); |
| 309 | continue; |
| 310 | } |
| 311 | } |
| 312 | else if(!strcmp(recvbuf, "trace_stop")) { |
| 313 | DBG("trace stop"); |
| 314 | |
| 315 | result = ltt_trace_stop(trace_name); |
| 316 | if(result < 0) { |
| 317 | ERR("ltt_trace_stop failed"); |
| 318 | return (void *)1; |
| 319 | } |
| 320 | } |
| 321 | else if(!strcmp(recvbuf, "trace_destroy")) { |
| 322 | |
| 323 | DBG("trace destroy"); |
| 324 | |
| 325 | result = ltt_trace_destroy(trace_name); |
| 326 | if(result < 0) { |
| 327 | ERR("ltt_trace_destroy failed"); |
| 328 | return (void *)1; |
| 329 | } |
| 330 | } |
| 331 | else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) { |
| 332 | struct ltt_trace_struct *trace; |
| 333 | char trace_name[] = "auto"; |
| 334 | int i; |
| 335 | char *channel_name; |
| 336 | |
| 337 | DBG("get_shmid"); |
| 338 | |
| 339 | channel_name = nth_token(recvbuf, 1); |
| 340 | if(channel_name == NULL) { |
| 341 | ERR("get_shmid: cannot parse channel"); |
| 342 | goto next_cmd; |
| 343 | } |
| 344 | |
| 345 | ltt_lock_traces(); |
| 346 | trace = _ltt_trace_find(trace_name); |
| 347 | ltt_unlock_traces(); |
| 348 | |
| 349 | if(trace == NULL) { |
| 350 | ERR("cannot find trace!"); |
| 351 | return (void *)1; |
| 352 | } |
| 353 | |
| 354 | for(i=0; i<trace->nr_channels; i++) { |
| 355 | struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 356 | struct rchan_buf *rbuf = rchan->buf; |
| 357 | struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)rchan->private_data; |
| 358 | struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf; |
| 359 | |
| 360 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 361 | char *reply; |
| 362 | |
| 363 | DBG("the shmid for the requested channel is %d", rbuf->shmid); |
| 364 | DBG("the shmid for its buffer structure is %d", ltt_channel->buf_shmid); |
| 365 | asprintf(&reply, "%d %d", rbuf->shmid, ltt_channel->buf_shmid); |
| 366 | |
| 367 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); |
| 368 | if(result) { |
| 369 | ERR("listener: get_shmid: ustcomm_send_reply failed"); |
| 370 | goto next_cmd; |
| 371 | } |
| 372 | |
| 373 | free(reply); |
| 374 | |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) { |
| 380 | struct ltt_trace_struct *trace; |
| 381 | char trace_name[] = "auto"; |
| 382 | int i; |
| 383 | char *channel_name; |
| 384 | |
| 385 | DBG("get_n_subbufs"); |
| 386 | |
| 387 | channel_name = nth_token(recvbuf, 1); |
| 388 | if(channel_name == NULL) { |
| 389 | ERR("get_n_subbufs: cannot parse channel"); |
| 390 | goto next_cmd; |
| 391 | } |
| 392 | |
| 393 | ltt_lock_traces(); |
| 394 | trace = _ltt_trace_find(trace_name); |
| 395 | ltt_unlock_traces(); |
| 396 | |
| 397 | if(trace == NULL) { |
| 398 | ERR("cannot find trace!"); |
| 399 | return (void *)1; |
| 400 | } |
| 401 | |
| 402 | for(i=0; i<trace->nr_channels; i++) { |
| 403 | struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 404 | |
| 405 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 406 | char *reply; |
| 407 | |
| 408 | DBG("the n_subbufs for the requested channel is %zd", rchan->n_subbufs); |
| 409 | asprintf(&reply, "%zd", rchan->n_subbufs); |
| 410 | |
| 411 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); |
| 412 | if(result) { |
| 413 | ERR("listener: get_n_subbufs: ustcomm_send_reply failed"); |
| 414 | goto next_cmd; |
| 415 | } |
| 416 | |
| 417 | free(reply); |
| 418 | |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) { |
| 424 | struct ltt_trace_struct *trace; |
| 425 | char trace_name[] = "auto"; |
| 426 | int i; |
| 427 | char *channel_name; |
| 428 | |
| 429 | DBG("get_subbuf_size"); |
| 430 | |
| 431 | channel_name = nth_token(recvbuf, 1); |
| 432 | if(channel_name == NULL) { |
| 433 | ERR("get_subbuf_size: cannot parse channel"); |
| 434 | goto next_cmd; |
| 435 | } |
| 436 | |
| 437 | ltt_lock_traces(); |
| 438 | trace = _ltt_trace_find(trace_name); |
| 439 | ltt_unlock_traces(); |
| 440 | |
| 441 | if(trace == NULL) { |
| 442 | ERR("cannot find trace!"); |
| 443 | return (void *)1; |
| 444 | } |
| 445 | |
| 446 | for(i=0; i<trace->nr_channels; i++) { |
| 447 | struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 448 | |
| 449 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 450 | char *reply; |
| 451 | |
| 452 | DBG("the subbuf_size for the requested channel is %zd", rchan->subbuf_size); |
| 453 | asprintf(&reply, "%zd", rchan->subbuf_size); |
| 454 | |
| 455 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); |
| 456 | if(result) { |
| 457 | ERR("listener: get_subbuf_size: ustcomm_send_reply failed"); |
| 458 | goto next_cmd; |
| 459 | } |
| 460 | |
| 461 | free(reply); |
| 462 | |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { |
| 468 | char *libfile; |
| 469 | |
| 470 | libfile = nth_token(recvbuf, 1); |
| 471 | |
| 472 | DBG("load_probe_lib loading %s", libfile); |
| 473 | } |
| 474 | else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) { |
| 475 | struct ltt_trace_struct *trace; |
| 476 | char trace_name[] = "auto"; |
| 477 | int i; |
| 478 | char *channel_name; |
| 479 | |
| 480 | DBG("get_subbuf"); |
| 481 | |
| 482 | channel_name = nth_token(recvbuf, 1); |
| 483 | if(channel_name == NULL) { |
| 484 | ERR("get_subbuf: cannot parse channel"); |
| 485 | goto next_cmd; |
| 486 | } |
| 487 | |
| 488 | ltt_lock_traces(); |
| 489 | trace = _ltt_trace_find(trace_name); |
| 490 | ltt_unlock_traces(); |
| 491 | |
| 492 | if(trace == NULL) { |
| 493 | ERR("cannot find trace!"); |
| 494 | return (void *)1; |
| 495 | } |
| 496 | |
| 497 | for(i=0; i<trace->nr_channels; i++) { |
| 498 | struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 499 | |
| 500 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 501 | struct rchan_buf *rbuf = rchan->buf; |
| 502 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; |
| 503 | char *reply; |
| 504 | long consumed_old=0; |
| 505 | int fd; |
| 506 | struct blocked_consumer *bc; |
| 507 | |
| 508 | bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer)); |
| 509 | if(bc == NULL) { |
| 510 | ERR("malloc returned NULL"); |
| 511 | goto next_cmd; |
| 512 | } |
| 513 | bc->fd_consumer = src.fd; |
| 514 | bc->fd_producer = lttbuf->data_ready_fd_read; |
| 515 | bc->rbuf = rbuf; |
| 516 | bc->lttbuf = lttbuf; |
| 517 | bc->src = src; |
| 518 | bc->server = ustcomm_app.server; |
| 519 | |
| 520 | list_add(&bc->list, &blocked_consumers); |
| 521 | |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) { |
| 527 | struct ltt_trace_struct *trace; |
| 528 | char trace_name[] = "auto"; |
| 529 | int i; |
| 530 | char *channel_name; |
| 531 | long consumed_old; |
| 532 | char *consumed_old_str; |
| 533 | char *endptr; |
| 534 | |
| 535 | DBG("put_subbuf"); |
| 536 | |
| 537 | channel_name = strdup_malloc(nth_token(recvbuf, 1)); |
| 538 | if(channel_name == NULL) { |
| 539 | ERR("put_subbuf_size: cannot parse channel"); |
| 540 | goto next_cmd; |
| 541 | } |
| 542 | |
| 543 | consumed_old_str = strdup_malloc(nth_token(recvbuf, 2)); |
| 544 | if(consumed_old_str == NULL) { |
| 545 | ERR("put_subbuf: cannot parse consumed_old"); |
| 546 | goto next_cmd; |
| 547 | } |
| 548 | consumed_old = strtol(consumed_old_str, &endptr, 10); |
| 549 | if(*endptr != '\0') { |
| 550 | ERR("put_subbuf: invalid value for consumed_old"); |
| 551 | goto next_cmd; |
| 552 | } |
| 553 | |
| 554 | ltt_lock_traces(); |
| 555 | trace = _ltt_trace_find(trace_name); |
| 556 | ltt_unlock_traces(); |
| 557 | |
| 558 | if(trace == NULL) { |
| 559 | ERR("cannot find trace!"); |
| 560 | return (void *)1; |
| 561 | } |
| 562 | |
| 563 | for(i=0; i<trace->nr_channels; i++) { |
| 564 | struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 565 | |
| 566 | if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 567 | struct rchan_buf *rbuf = rchan->buf; |
| 568 | struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; |
| 569 | char *reply; |
| 570 | long consumed_old=0; |
| 571 | |
| 572 | result = ltt_do_put_subbuf(rbuf, lttbuf, consumed_old); |
| 573 | if(result < 0) { |
| 574 | WARN("ltt_do_put_subbuf: error (subbuf=%s)", channel_name); |
| 575 | asprintf(&reply, "%s", "ERROR"); |
| 576 | } |
| 577 | else { |
| 578 | DBG("ltt_do_put_subbuf: success (subbuf=%s)", channel_name); |
| 579 | asprintf(&reply, "%s", "OK"); |
| 580 | } |
| 581 | |
| 582 | result = ustcomm_send_reply(&ustcomm_app.server, reply, &src); |
| 583 | if(result) { |
| 584 | ERR("listener: put_subbuf: ustcomm_send_reply failed"); |
| 585 | goto next_cmd; |
| 586 | } |
| 587 | |
| 588 | free(reply); |
| 589 | |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | free(channel_name); |
| 595 | free(consumed_old_str); |
| 596 | } |
| 597 | else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) { |
| 598 | char *channel_slash_name = nth_token(recvbuf, 1); |
| 599 | char channel_name[256]=""; |
| 600 | char marker_name[256]=""; |
| 601 | struct marker_iter iter; |
| 602 | |
| 603 | result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name); |
| 604 | |
| 605 | if(channel_name == NULL || marker_name == NULL) { |
| 606 | WARN("invalid marker name"); |
| 607 | goto next_cmd; |
| 608 | } |
| 609 | printf("%s %s\n", channel_name, marker_name); |
| 610 | |
| 611 | result = ltt_marker_connect(channel_name, marker_name, "default"); |
| 612 | if(result < 0) { |
| 613 | WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name); |
| 614 | } |
| 615 | } |
| 616 | else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) { |
| 617 | char *channel_slash_name = nth_token(recvbuf, 1); |
| 618 | char *marker_name; |
| 619 | char *channel_name; |
| 620 | struct marker_iter iter; |
| 621 | |
| 622 | result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name); |
| 623 | |
| 624 | if(marker_name == NULL) { |
| 625 | } |
| 626 | printf("%s %s\n", channel_name, marker_name); |
| 627 | |
| 628 | result = ltt_marker_disconnect(channel_name, marker_name, "default"); |
| 629 | if(result < 0) { |
| 630 | WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name); |
| 631 | } |
| 632 | } |
| 633 | // else if(nth_token_is(recvbuf, "get_notifications", 0) == 1) { |
| 634 | // struct ltt_trace_struct *trace; |
| 635 | // char trace_name[] = "auto"; |
| 636 | // int i; |
| 637 | // char *channel_name; |
| 638 | // |
| 639 | // DBG("get_notifications"); |
| 640 | // |
| 641 | // channel_name = strdup_malloc(nth_token(recvbuf, 1)); |
| 642 | // if(channel_name == NULL) { |
| 643 | // ERR("put_subbuf_size: cannot parse channel"); |
| 644 | // goto next_cmd; |
| 645 | // } |
| 646 | // |
| 647 | // ltt_lock_traces(); |
| 648 | // trace = _ltt_trace_find(trace_name); |
| 649 | // ltt_unlock_traces(); |
| 650 | // |
| 651 | // if(trace == NULL) { |
| 652 | // ERR("cannot find trace!"); |
| 653 | // return (void *)1; |
| 654 | // } |
| 655 | // |
| 656 | // for(i=0; i<trace->nr_channels; i++) { |
| 657 | // struct rchan *rchan = trace->channels[i].trans_channel_data; |
| 658 | // int fd; |
| 659 | // |
| 660 | // if(!strcmp(trace->channels[i].channel_name, channel_name)) { |
| 661 | // struct rchan_buf *rbuf = rchan->buf; |
| 662 | // struct ltt_channel_buf_struct *lttbuf = trace->channels[i].buf; |
| 663 | // |
| 664 | // result = fd = ustcomm_app_detach_client(&ustcomm_app, &src); |
| 665 | // if(result == -1) { |
| 666 | // ERR("ustcomm_app_detach_client failed"); |
| 667 | // goto next_cmd; |
| 668 | // } |
| 669 | // |
| 670 | // lttbuf->wake_consumer_arg = (void *) fd; |
| 671 | // |
| 672 | // smp_wmb(); |
| 673 | // |
| 674 | // lttbuf->call_wake_consumer = 1; |
| 675 | // |
| 676 | // break; |
| 677 | // } |
| 678 | // } |
| 679 | // |
| 680 | // free(channel_name); |
| 681 | // } |
| 682 | else { |
| 683 | ERR("unable to parse message: %s", recvbuf); |
| 684 | } |
| 685 | |
| 686 | next_cmd: |
| 687 | free(recvbuf); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | static char listener_stack[16384]; |
| 692 | |
| 693 | void create_listener(void) |
| 694 | { |
| 695 | int result; |
| 696 | static char listener_stack[16384]; |
| 697 | //char *listener_stack = malloc(16384); |
| 698 | |
| 699 | #ifdef USE_CLONE |
| 700 | result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); |
| 701 | if(result == -1) { |
| 702 | perror("clone"); |
| 703 | } |
| 704 | #else |
| 705 | pthread_t thread; |
| 706 | |
| 707 | pthread_create(&thread, NULL, listener_main, NULL); |
| 708 | #endif |
| 709 | } |
| 710 | |
| 711 | /* The signal handler itself. Signals must be setup so there cannot be |
| 712 | nested signals. */ |
| 713 | |
| 714 | void sighandler(int sig) |
| 715 | { |
| 716 | static char have_listener = 0; |
| 717 | DBG("sighandler"); |
| 718 | |
| 719 | if(!have_listener) { |
| 720 | create_listener(); |
| 721 | have_listener = 1; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /* Called by the app signal handler to chain it to us. */ |
| 726 | |
| 727 | void chain_signal(void) |
| 728 | { |
| 729 | sighandler(USTSIGNAL); |
| 730 | } |
| 731 | |
| 732 | static int init_socket(void) |
| 733 | { |
| 734 | return ustcomm_init_app(getpid(), &ustcomm_app); |
| 735 | } |
| 736 | |
| 737 | static void destroy_socket(void) |
| 738 | { |
| 739 | // int result; |
| 740 | // |
| 741 | // if(mysocketfile[0] == '\0') |
| 742 | // return; |
| 743 | // |
| 744 | // result = unlink(mysocketfile); |
| 745 | // if(result == -1) { |
| 746 | // PERROR("unlink"); |
| 747 | // } |
| 748 | } |
| 749 | |
| 750 | static int init_signal_handler(void) |
| 751 | { |
| 752 | /* Attempt to handler SIGIO. If the main program wants to |
| 753 | * handle it, fine, it'll override us. They it'll have to |
| 754 | * use the chaining function. |
| 755 | */ |
| 756 | |
| 757 | int result; |
| 758 | struct sigaction act; |
| 759 | |
| 760 | result = sigemptyset(&act.sa_mask); |
| 761 | if(result == -1) { |
| 762 | PERROR("sigemptyset"); |
| 763 | return -1; |
| 764 | } |
| 765 | |
| 766 | act.sa_handler = sighandler; |
| 767 | act.sa_flags = SA_RESTART; |
| 768 | |
| 769 | /* Only defer ourselves. Also, try to restart interrupted |
| 770 | * syscalls to disturb the traced program as little as possible. |
| 771 | */ |
| 772 | result = sigaction(SIGIO, &act, NULL); |
| 773 | if(result == -1) { |
| 774 | PERROR("sigaction"); |
| 775 | return -1; |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static void auto_probe_connect(struct marker *m) |
| 782 | { |
| 783 | int result; |
| 784 | |
| 785 | result = ltt_marker_connect(m->channel, m->name, "default"); |
| 786 | if(result) |
| 787 | ERR("ltt_marker_connect"); |
| 788 | |
| 789 | DBG("just auto connected marker %s %s to probe default", m->channel, m->name); |
| 790 | } |
| 791 | |
| 792 | static void __attribute__((constructor(101))) init0() |
| 793 | { |
| 794 | DBG("UST_AUTOPROBE constructor"); |
| 795 | if(getenv("UST_AUTOPROBE")) { |
| 796 | marker_set_new_marker_cb(auto_probe_connect); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | static void fini(void); |
| 801 | |
| 802 | static void __attribute__((constructor(1000))) init() |
| 803 | { |
| 804 | int result; |
| 805 | |
| 806 | DBG("UST_TRACE constructor"); |
| 807 | |
| 808 | /* Must create socket before signal handler to prevent races. |
| 809 | */ |
| 810 | result = init_socket(); |
| 811 | if(result == -1) { |
| 812 | ERR("init_socket error"); |
| 813 | return; |
| 814 | } |
| 815 | result = init_signal_handler(); |
| 816 | if(result == -1) { |
| 817 | ERR("init_signal_handler error"); |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | if(getenv("UST_TRACE")) { |
| 822 | char trace_name[] = "auto"; |
| 823 | char trace_type[] = "ustrelay"; |
| 824 | |
| 825 | DBG("starting early tracing"); |
| 826 | |
| 827 | /* Ensure marker control is initialized */ |
| 828 | init_marker_control(); |
| 829 | |
| 830 | /* Ensure relay is initialized */ |
| 831 | init_ustrelay_transport(); |
| 832 | |
| 833 | /* Ensure markers are initialized */ |
| 834 | init_markers(); |
| 835 | |
| 836 | /* In case. */ |
| 837 | ltt_channels_register("ust"); |
| 838 | |
| 839 | result = ltt_trace_setup(trace_name); |
| 840 | if(result < 0) { |
| 841 | ERR("ltt_trace_setup failed"); |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | result = ltt_trace_set_type(trace_name, trace_type); |
| 846 | if(result < 0) { |
| 847 | ERR("ltt_trace_set_type failed"); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | result = ltt_trace_alloc(trace_name); |
| 852 | if(result < 0) { |
| 853 | ERR("ltt_trace_alloc failed"); |
| 854 | return; |
| 855 | } |
| 856 | |
| 857 | result = ltt_trace_start(trace_name); |
| 858 | if(result < 0) { |
| 859 | ERR("ltt_trace_start failed"); |
| 860 | return; |
| 861 | } |
| 862 | inform_consumer_daemon(); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | return; |
| 867 | |
| 868 | /* should decrementally destroy stuff if error */ |
| 869 | |
| 870 | } |
| 871 | |
| 872 | /* This is only called if we terminate normally, not with an unhandled signal, |
| 873 | * so we cannot rely on it. */ |
| 874 | |
| 875 | /* This destructor probably isn't needed, because ustd can do crash recovery. */ |
| 876 | #if 0 |
| 877 | static void __attribute__((destructor)) fini() |
| 878 | { |
| 879 | int result; |
| 880 | |
| 881 | /* if trace running, finish it */ |
| 882 | |
| 883 | DBG("destructor stopping traces"); |
| 884 | |
| 885 | result = ltt_trace_stop("auto"); |
| 886 | if(result == -1) { |
| 887 | ERR("ltt_trace_stop error"); |
| 888 | } |
| 889 | |
| 890 | result = ltt_trace_destroy("auto"); |
| 891 | if(result == -1) { |
| 892 | ERR("ltt_trace_destroy error"); |
| 893 | } |
| 894 | |
| 895 | destroy_socket(); |
| 896 | } |
| 897 | #endif |