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