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