| 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 | /* This file contains the implementation of the UST listener thread, which |
| 19 | * receives trace control commands. It also coordinates the initialization of |
| 20 | * libust. |
| 21 | */ |
| 22 | |
| 23 | #define _GNU_SOURCE |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
| 27 | #include <pthread.h> |
| 28 | #include <signal.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/un.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <poll.h> |
| 34 | #include <regex.h> |
| 35 | #include <urcu/uatomic_arch.h> |
| 36 | |
| 37 | #include <ust/marker.h> |
| 38 | #include <ust/tracectl.h> |
| 39 | #include "tracer.h" |
| 40 | #include "usterr.h" |
| 41 | #include "ustcomm.h" |
| 42 | #include "buffers.h" |
| 43 | #include "marker-control.h" |
| 44 | #include "multipoll.h" |
| 45 | |
| 46 | #define USTSIGNAL SIGIO |
| 47 | |
| 48 | #define MAX_MSG_SIZE (100) |
| 49 | #define MSG_NOTIF 1 |
| 50 | #define MSG_REGISTER_NOTIF 2 |
| 51 | |
| 52 | /* This should only be accessed by the constructor, before the creation |
| 53 | * of the listener, and then only by the listener. |
| 54 | */ |
| 55 | s64 pidunique = -1LL; |
| 56 | |
| 57 | extern struct chan_info_struct chan_infos[]; |
| 58 | |
| 59 | struct list_head blocked_consumers = LIST_HEAD_INIT(blocked_consumers); |
| 60 | |
| 61 | static struct ustcomm_app ustcomm_app; |
| 62 | |
| 63 | struct tracecmd { /* no padding */ |
| 64 | uint32_t size; |
| 65 | uint16_t command; |
| 66 | }; |
| 67 | |
| 68 | /* volatile because shared between the listener and the main thread */ |
| 69 | int buffers_to_export = 0; |
| 70 | |
| 71 | struct trctl_msg { |
| 72 | /* size: the size of all the fields except size itself */ |
| 73 | uint32_t size; |
| 74 | uint16_t type; |
| 75 | /* Only the necessary part of the payload is transferred. It |
| 76 | * may even be none of it. |
| 77 | */ |
| 78 | char payload[94]; |
| 79 | }; |
| 80 | |
| 81 | struct consumer_channel { |
| 82 | int fd; |
| 83 | struct ltt_channel_struct *chan; |
| 84 | }; |
| 85 | |
| 86 | struct blocked_consumer { |
| 87 | int fd_consumer; |
| 88 | int fd_producer; |
| 89 | int tmp_poll_idx; |
| 90 | |
| 91 | /* args to ustcomm_send_reply */ |
| 92 | struct ustcomm_server server; |
| 93 | struct ustcomm_source src; |
| 94 | |
| 95 | /* args to ust_buffers_get_subbuf */ |
| 96 | struct ust_buffer *buf; |
| 97 | |
| 98 | struct list_head list; |
| 99 | }; |
| 100 | |
| 101 | static long long make_pidunique(void) |
| 102 | { |
| 103 | s64 retval; |
| 104 | struct timeval tv; |
| 105 | |
| 106 | gettimeofday(&tv, NULL); |
| 107 | |
| 108 | retval = tv.tv_sec; |
| 109 | retval <<= 32; |
| 110 | retval |= tv.tv_usec; |
| 111 | |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | static void print_markers(FILE *fp) |
| 116 | { |
| 117 | struct marker_iter iter; |
| 118 | |
| 119 | lock_markers(); |
| 120 | marker_iter_reset(&iter); |
| 121 | marker_iter_start(&iter); |
| 122 | |
| 123 | while(iter.marker) { |
| 124 | fprintf(fp, "marker: %s/%s %d \"%s\" %p\n", iter.marker->channel, iter.marker->name, (int)imv_read(iter.marker->state), iter.marker->format, iter.marker->location); |
| 125 | marker_iter_next(&iter); |
| 126 | } |
| 127 | unlock_markers(); |
| 128 | } |
| 129 | |
| 130 | static int init_socket(void); |
| 131 | |
| 132 | /* Ask the daemon to collect a trace called trace_name and being |
| 133 | * produced by this pid. |
| 134 | * |
| 135 | * The trace must be at least allocated. (It can also be started.) |
| 136 | * This is because _ltt_trace_find is used. |
| 137 | */ |
| 138 | |
| 139 | static void inform_consumer_daemon(const char *trace_name) |
| 140 | { |
| 141 | int i,j; |
| 142 | struct ust_trace *trace; |
| 143 | pid_t pid = getpid(); |
| 144 | int result; |
| 145 | |
| 146 | ltt_lock_traces(); |
| 147 | |
| 148 | trace = _ltt_trace_find(trace_name); |
| 149 | if(trace == NULL) { |
| 150 | WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name); |
| 151 | goto finish; |
| 152 | } |
| 153 | |
| 154 | for(i=0; i < trace->nr_channels; i++) { |
| 155 | if(trace->channels[i].request_collection) { |
| 156 | /* iterate on all cpus */ |
| 157 | for(j=0; j<trace->channels[i].n_cpus; j++) { |
| 158 | char *buf; |
| 159 | if (asprintf(&buf, "%s_%d", trace->channels[i].channel_name, j) < 0) { |
| 160 | ERR("inform_consumer_daemon : asprintf failed (%s_%d)", |
| 161 | trace->channels[i].channel_name, j); |
| 162 | goto finish; |
| 163 | } |
| 164 | result = ustcomm_request_consumer(pid, buf); |
| 165 | if(result == -1) { |
| 166 | WARN("Failed to request collection for channel %s. Is the daemon available?", trace->channels[i].channel_name); |
| 167 | /* continue even if fail */ |
| 168 | } |
| 169 | free(buf); |
| 170 | STORE_SHARED(buffers_to_export, LOAD_SHARED(buffers_to_export)+1); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | finish: |
| 176 | ltt_unlock_traces(); |
| 177 | } |
| 178 | |
| 179 | int process_blkd_consumer_act(void *priv, int fd, short events) |
| 180 | { |
| 181 | int result; |
| 182 | long consumed_old = 0; |
| 183 | char *reply; |
| 184 | struct blocked_consumer *bc = (struct blocked_consumer *) priv; |
| 185 | char inbuf; |
| 186 | |
| 187 | result = read(bc->fd_producer, &inbuf, 1); |
| 188 | if(result == -1) { |
| 189 | PERROR("read"); |
| 190 | return -1; |
| 191 | } |
| 192 | if(result == 0) { |
| 193 | int res; |
| 194 | DBG("listener: got messsage that a buffer ended"); |
| 195 | |
| 196 | res = close(bc->fd_producer); |
| 197 | if(res == -1) { |
| 198 | PERROR("close"); |
| 199 | } |
| 200 | |
| 201 | list_del(&bc->list); |
| 202 | |
| 203 | result = ustcomm_send_reply(&bc->server, "END", &bc->src); |
| 204 | if(result < 0) { |
| 205 | ERR("ustcomm_send_reply failed"); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | result = ust_buffers_get_subbuf(bc->buf, &consumed_old); |
| 213 | if(result == -EAGAIN) { |
| 214 | WARN("missed buffer?"); |
| 215 | return 0; |
| 216 | } |
| 217 | else if(result < 0) { |
| 218 | ERR("ust_buffers_get_subbuf: error: %s", strerror(-result)); |
| 219 | } |
| 220 | if (asprintf(&reply, "%s %ld", "OK", consumed_old) < 0) { |
| 221 | ERR("process_blkd_consumer_act : asprintf failed (OK %ld)", |
| 222 | consumed_old); |
| 223 | return -1; |
| 224 | } |
| 225 | result = ustcomm_send_reply(&bc->server, reply, &bc->src); |
| 226 | if(result < 0) { |
| 227 | ERR("ustcomm_send_reply failed"); |
| 228 | free(reply); |
| 229 | return -1; |
| 230 | } |
| 231 | free(reply); |
| 232 | |
| 233 | list_del(&bc->list); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void blocked_consumers_add_to_mp(struct mpentries *ent) |
| 239 | { |
| 240 | struct blocked_consumer *bc; |
| 241 | |
| 242 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 243 | multipoll_add(ent, bc->fd_producer, POLLIN, process_blkd_consumer_act, bc, NULL); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | void seperate_channel_cpu(const char *channel_and_cpu, char **channel, int *cpu) |
| 249 | { |
| 250 | const char *sep; |
| 251 | |
| 252 | sep = rindex(channel_and_cpu, '_'); |
| 253 | if(sep == NULL) { |
| 254 | *cpu = -1; |
| 255 | sep = channel_and_cpu + strlen(channel_and_cpu); |
| 256 | } |
| 257 | else { |
| 258 | *cpu = atoi(sep+1); |
| 259 | } |
| 260 | |
| 261 | if (asprintf(channel, "%.*s", (int)(sep-channel_and_cpu), channel_and_cpu) < 0) { |
| 262 | ERR("seperate_channel_cpu : asprintf failed (%.*s)", |
| 263 | (int)(sep-channel_and_cpu), channel_and_cpu); |
| 264 | return; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static int do_cmd_get_shmid(const char *recvbuf, struct ustcomm_source *src) |
| 269 | { |
| 270 | int retval = 0; |
| 271 | struct ust_trace *trace; |
| 272 | char trace_name[] = "auto"; |
| 273 | int i; |
| 274 | char *channel_and_cpu; |
| 275 | int found = 0; |
| 276 | int result; |
| 277 | char *ch_name; |
| 278 | int ch_cpu; |
| 279 | |
| 280 | DBG("get_shmid"); |
| 281 | |
| 282 | channel_and_cpu = nth_token(recvbuf, 1); |
| 283 | if(channel_and_cpu == NULL) { |
| 284 | ERR("cannot parse channel"); |
| 285 | goto end; |
| 286 | } |
| 287 | |
| 288 | seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu); |
| 289 | if(ch_cpu == -1) { |
| 290 | ERR("problem parsing channel name"); |
| 291 | goto free_short_chan_name; |
| 292 | } |
| 293 | |
| 294 | ltt_lock_traces(); |
| 295 | trace = _ltt_trace_find(trace_name); |
| 296 | ltt_unlock_traces(); |
| 297 | |
| 298 | if(trace == NULL) { |
| 299 | ERR("cannot find trace!"); |
| 300 | retval = -1; |
| 301 | goto free_short_chan_name; |
| 302 | } |
| 303 | |
| 304 | for(i=0; i<trace->nr_channels; i++) { |
| 305 | struct ust_channel *channel = &trace->channels[i]; |
| 306 | struct ust_buffer *buf = channel->buf[ch_cpu]; |
| 307 | |
| 308 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 309 | char *reply; |
| 310 | |
| 311 | // DBG("the shmid for the requested channel is %d", buf->shmid); |
| 312 | // DBG("the shmid for its buffer structure is %d", channel->buf_struct_shmids); |
| 313 | if (asprintf(&reply, "%d %d", buf->shmid, channel->buf_struct_shmids[ch_cpu]) < 0) { |
| 314 | ERR("do_cmd_get_shmid : asprintf failed (%d %d)", |
| 315 | buf->shmid, channel->buf_struct_shmids[ch_cpu]); |
| 316 | retval = -1; |
| 317 | goto free_short_chan_name; |
| 318 | } |
| 319 | |
| 320 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 321 | if(result) { |
| 322 | ERR("ustcomm_send_reply failed"); |
| 323 | free(reply); |
| 324 | retval = -1; |
| 325 | goto free_short_chan_name; |
| 326 | } |
| 327 | |
| 328 | free(reply); |
| 329 | |
| 330 | found = 1; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if(!found) { |
| 336 | ERR("channel not found (%s)", channel_and_cpu); |
| 337 | } |
| 338 | |
| 339 | free_short_chan_name: |
| 340 | free(ch_name); |
| 341 | |
| 342 | end: |
| 343 | return retval; |
| 344 | } |
| 345 | |
| 346 | static int do_cmd_get_n_subbufs(const char *recvbuf, struct ustcomm_source *src) |
| 347 | { |
| 348 | int retval = 0; |
| 349 | struct ust_trace *trace; |
| 350 | char trace_name[] = "auto"; |
| 351 | int i; |
| 352 | char *channel_and_cpu; |
| 353 | int found = 0; |
| 354 | int result; |
| 355 | char *ch_name; |
| 356 | int ch_cpu; |
| 357 | |
| 358 | DBG("get_n_subbufs"); |
| 359 | |
| 360 | channel_and_cpu = nth_token(recvbuf, 1); |
| 361 | if(channel_and_cpu == NULL) { |
| 362 | ERR("cannot parse channel"); |
| 363 | goto end; |
| 364 | } |
| 365 | |
| 366 | seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu); |
| 367 | if(ch_cpu == -1) { |
| 368 | ERR("problem parsing channel name"); |
| 369 | goto free_short_chan_name; |
| 370 | } |
| 371 | |
| 372 | ltt_lock_traces(); |
| 373 | trace = _ltt_trace_find(trace_name); |
| 374 | ltt_unlock_traces(); |
| 375 | |
| 376 | if(trace == NULL) { |
| 377 | ERR("cannot find trace!"); |
| 378 | retval = -1; |
| 379 | goto free_short_chan_name; |
| 380 | } |
| 381 | |
| 382 | for(i=0; i<trace->nr_channels; i++) { |
| 383 | struct ust_channel *channel = &trace->channels[i]; |
| 384 | |
| 385 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 386 | char *reply; |
| 387 | |
| 388 | DBG("the n_subbufs for the requested channel is %d", channel->subbuf_cnt); |
| 389 | if (asprintf(&reply, "%d", channel->subbuf_cnt) < 0) { |
| 390 | ERR("do_cmd_get_n_subbufs : asprintf failed (%d)", |
| 391 | channel->subbuf_cnt); |
| 392 | retval = -1; |
| 393 | goto free_short_chan_name; |
| 394 | } |
| 395 | |
| 396 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 397 | if(result) { |
| 398 | ERR("ustcomm_send_reply failed"); |
| 399 | free(reply); |
| 400 | retval = -1; |
| 401 | goto free_short_chan_name; |
| 402 | } |
| 403 | |
| 404 | free(reply); |
| 405 | found = 1; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | if(found == 0) { |
| 410 | ERR("unable to find channel"); |
| 411 | } |
| 412 | |
| 413 | free_short_chan_name: |
| 414 | free(ch_name); |
| 415 | |
| 416 | end: |
| 417 | return retval; |
| 418 | } |
| 419 | |
| 420 | static int do_cmd_get_subbuf_size(const char *recvbuf, struct ustcomm_source *src) |
| 421 | { |
| 422 | int retval = 0; |
| 423 | struct ust_trace *trace; |
| 424 | char trace_name[] = "auto"; |
| 425 | int i; |
| 426 | char *channel_and_cpu; |
| 427 | int found = 0; |
| 428 | int result; |
| 429 | char *ch_name; |
| 430 | int ch_cpu; |
| 431 | |
| 432 | DBG("get_subbuf_size"); |
| 433 | |
| 434 | channel_and_cpu = nth_token(recvbuf, 1); |
| 435 | if(channel_and_cpu == NULL) { |
| 436 | ERR("cannot parse channel"); |
| 437 | goto end; |
| 438 | } |
| 439 | |
| 440 | seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu); |
| 441 | if(ch_cpu == -1) { |
| 442 | ERR("problem parsing channel name"); |
| 443 | goto free_short_chan_name; |
| 444 | } |
| 445 | |
| 446 | ltt_lock_traces(); |
| 447 | trace = _ltt_trace_find(trace_name); |
| 448 | ltt_unlock_traces(); |
| 449 | |
| 450 | if(trace == NULL) { |
| 451 | ERR("cannot find trace!"); |
| 452 | retval = -1; |
| 453 | goto free_short_chan_name; |
| 454 | } |
| 455 | |
| 456 | for(i=0; i<trace->nr_channels; i++) { |
| 457 | struct ust_channel *channel = &trace->channels[i]; |
| 458 | |
| 459 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 460 | char *reply; |
| 461 | |
| 462 | DBG("the subbuf_size for the requested channel is %zd", channel->subbuf_size); |
| 463 | if (asprintf(&reply, "%zd", channel->subbuf_size) < 0) { |
| 464 | ERR("do_cmd_get_subbuf_size : asprintf failed (%zd)", |
| 465 | channel->subbuf_size); |
| 466 | retval = -1; |
| 467 | goto free_short_chan_name; |
| 468 | } |
| 469 | |
| 470 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 471 | if(result) { |
| 472 | ERR("ustcomm_send_reply failed"); |
| 473 | free(reply); |
| 474 | retval = -1; |
| 475 | goto free_short_chan_name; |
| 476 | } |
| 477 | |
| 478 | free(reply); |
| 479 | found = 1; |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | if(found == 0) { |
| 484 | ERR("unable to find channel"); |
| 485 | } |
| 486 | |
| 487 | free_short_chan_name: |
| 488 | free(ch_name); |
| 489 | |
| 490 | end: |
| 491 | return retval; |
| 492 | } |
| 493 | |
| 494 | /* Return the power of two which is equal or higher to v */ |
| 495 | |
| 496 | static unsigned int pow2_higher_or_eq(unsigned int v) |
| 497 | { |
| 498 | int hb = fls(v); |
| 499 | int retval = 1<<(hb-1); |
| 500 | |
| 501 | if(v-retval == 0) |
| 502 | return retval; |
| 503 | else |
| 504 | return retval<<1; |
| 505 | } |
| 506 | |
| 507 | static int do_cmd_set_subbuf_size(const char *recvbuf, struct ustcomm_source *src) |
| 508 | { |
| 509 | char *channel_slash_size; |
| 510 | char ch_name[256]=""; |
| 511 | unsigned int size, power; |
| 512 | int retval = 0; |
| 513 | struct ust_trace *trace; |
| 514 | char trace_name[] = "auto"; |
| 515 | int i; |
| 516 | int found = 0; |
| 517 | |
| 518 | DBG("set_subbuf_size"); |
| 519 | |
| 520 | channel_slash_size = nth_token(recvbuf, 1); |
| 521 | sscanf(channel_slash_size, "%255[^/]/%u", ch_name, &size); |
| 522 | |
| 523 | if(ch_name == NULL) { |
| 524 | ERR("cannot parse channel"); |
| 525 | goto end; |
| 526 | } |
| 527 | |
| 528 | power = pow2_higher_or_eq(size); |
| 529 | power = max_t(unsigned int, 2u, power); |
| 530 | if (power != size) |
| 531 | WARN("using the next power of two for buffer size = %u\n", power); |
| 532 | |
| 533 | ltt_lock_traces(); |
| 534 | trace = _ltt_trace_find_setup(trace_name); |
| 535 | if(trace == NULL) { |
| 536 | ERR("cannot find trace!"); |
| 537 | retval = -1; |
| 538 | goto end; |
| 539 | } |
| 540 | |
| 541 | for(i = 0; i < trace->nr_channels; i++) { |
| 542 | struct ust_channel *channel = &trace->channels[i]; |
| 543 | |
| 544 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 545 | |
| 546 | channel->subbuf_size = power; |
| 547 | DBG("the set_subbuf_size for the requested channel is %zd", channel->subbuf_size); |
| 548 | |
| 549 | found = 1; |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | if(found == 0) { |
| 554 | ERR("unable to find channel"); |
| 555 | } |
| 556 | |
| 557 | end: |
| 558 | ltt_unlock_traces(); |
| 559 | return retval; |
| 560 | } |
| 561 | |
| 562 | static int do_cmd_set_subbuf_num(const char *recvbuf, struct ustcomm_source *src) |
| 563 | { |
| 564 | char *channel_slash_num; |
| 565 | char ch_name[256]=""; |
| 566 | unsigned int num; |
| 567 | int retval = 0; |
| 568 | struct ust_trace *trace; |
| 569 | char trace_name[] = "auto"; |
| 570 | int i; |
| 571 | int found = 0; |
| 572 | |
| 573 | DBG("set_subbuf_num"); |
| 574 | |
| 575 | channel_slash_num = nth_token(recvbuf, 1); |
| 576 | sscanf(channel_slash_num, "%255[^/]/%u", ch_name, &num); |
| 577 | |
| 578 | if(ch_name == NULL) { |
| 579 | ERR("cannot parse channel"); |
| 580 | goto end; |
| 581 | } |
| 582 | if (num < 2) { |
| 583 | ERR("subbuffer count should be greater than 2"); |
| 584 | goto end; |
| 585 | } |
| 586 | |
| 587 | ltt_lock_traces(); |
| 588 | trace = _ltt_trace_find_setup(trace_name); |
| 589 | if(trace == NULL) { |
| 590 | ERR("cannot find trace!"); |
| 591 | retval = -1; |
| 592 | goto end; |
| 593 | } |
| 594 | |
| 595 | for(i = 0; i < trace->nr_channels; i++) { |
| 596 | struct ust_channel *channel = &trace->channels[i]; |
| 597 | |
| 598 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 599 | |
| 600 | channel->subbuf_cnt = num; |
| 601 | DBG("the set_subbuf_cnt for the requested channel is %zd", channel->subbuf_cnt); |
| 602 | |
| 603 | found = 1; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | if(found == 0) { |
| 608 | ERR("unable to find channel"); |
| 609 | } |
| 610 | |
| 611 | end: |
| 612 | ltt_unlock_traces(); |
| 613 | return retval; |
| 614 | } |
| 615 | |
| 616 | static int do_cmd_get_subbuffer(const char *recvbuf, struct ustcomm_source *src) |
| 617 | { |
| 618 | int retval = 0; |
| 619 | struct ust_trace *trace; |
| 620 | char trace_name[] = "auto"; |
| 621 | int i; |
| 622 | char *channel_and_cpu; |
| 623 | int found = 0; |
| 624 | char *ch_name; |
| 625 | int ch_cpu; |
| 626 | |
| 627 | DBG("get_subbuf"); |
| 628 | |
| 629 | channel_and_cpu = nth_token(recvbuf, 1); |
| 630 | if(channel_and_cpu == NULL) { |
| 631 | ERR("cannot parse channel"); |
| 632 | goto end; |
| 633 | } |
| 634 | |
| 635 | seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu); |
| 636 | if(ch_cpu == -1) { |
| 637 | ERR("problem parsing channel name"); |
| 638 | goto free_short_chan_name; |
| 639 | } |
| 640 | |
| 641 | ltt_lock_traces(); |
| 642 | trace = _ltt_trace_find(trace_name); |
| 643 | |
| 644 | if(trace == NULL) { |
| 645 | int result; |
| 646 | |
| 647 | DBG("Cannot find trace. It was likely destroyed by the user."); |
| 648 | result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src); |
| 649 | if(result) { |
| 650 | ERR("ustcomm_send_reply failed"); |
| 651 | retval = -1; |
| 652 | goto unlock_traces; |
| 653 | } |
| 654 | |
| 655 | goto unlock_traces; |
| 656 | } |
| 657 | |
| 658 | for(i=0; i<trace->nr_channels; i++) { |
| 659 | struct ust_channel *channel = &trace->channels[i]; |
| 660 | |
| 661 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 662 | struct ust_buffer *buf = channel->buf[ch_cpu]; |
| 663 | struct blocked_consumer *bc; |
| 664 | |
| 665 | found = 1; |
| 666 | |
| 667 | bc = (struct blocked_consumer *) malloc(sizeof(struct blocked_consumer)); |
| 668 | if(bc == NULL) { |
| 669 | ERR("malloc returned NULL"); |
| 670 | goto unlock_traces; |
| 671 | } |
| 672 | bc->fd_consumer = src->fd; |
| 673 | bc->fd_producer = buf->data_ready_fd_read; |
| 674 | bc->buf = buf; |
| 675 | bc->src = *src; |
| 676 | bc->server = ustcomm_app.server; |
| 677 | |
| 678 | list_add(&bc->list, &blocked_consumers); |
| 679 | |
| 680 | /* Being here is the proof the daemon has mapped the buffer in its |
| 681 | * memory. We may now decrement buffers_to_export. |
| 682 | */ |
| 683 | if(uatomic_read(&buf->consumed) == 0) { |
| 684 | DBG("decrementing buffers_to_export"); |
| 685 | STORE_SHARED(buffers_to_export, LOAD_SHARED(buffers_to_export)-1); |
| 686 | } |
| 687 | |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | if(found == 0) { |
| 692 | ERR("unable to find channel"); |
| 693 | } |
| 694 | |
| 695 | unlock_traces: |
| 696 | ltt_unlock_traces(); |
| 697 | |
| 698 | free_short_chan_name: |
| 699 | free(ch_name); |
| 700 | |
| 701 | end: |
| 702 | return retval; |
| 703 | } |
| 704 | |
| 705 | static int do_cmd_put_subbuffer(const char *recvbuf, struct ustcomm_source *src) |
| 706 | { |
| 707 | int retval = 0; |
| 708 | struct ust_trace *trace; |
| 709 | char trace_name[] = "auto"; |
| 710 | int i; |
| 711 | char *channel_and_cpu; |
| 712 | int found = 0; |
| 713 | int result; |
| 714 | char *ch_name; |
| 715 | int ch_cpu; |
| 716 | long consumed_old; |
| 717 | char *consumed_old_str; |
| 718 | char *endptr; |
| 719 | char *reply = NULL; |
| 720 | |
| 721 | DBG("put_subbuf"); |
| 722 | |
| 723 | channel_and_cpu = strdup(nth_token(recvbuf, 1)); |
| 724 | if(channel_and_cpu == NULL) { |
| 725 | ERR("cannot parse channel"); |
| 726 | retval = -1; |
| 727 | goto end; |
| 728 | } |
| 729 | |
| 730 | consumed_old_str = strdup(nth_token(recvbuf, 2)); |
| 731 | if(consumed_old_str == NULL) { |
| 732 | ERR("cannot parse consumed_old"); |
| 733 | retval = -1; |
| 734 | goto free_channel_and_cpu; |
| 735 | } |
| 736 | consumed_old = strtol(consumed_old_str, &endptr, 10); |
| 737 | if(*endptr != '\0') { |
| 738 | ERR("invalid value for consumed_old"); |
| 739 | retval = -1; |
| 740 | goto free_consumed_old_str; |
| 741 | } |
| 742 | |
| 743 | seperate_channel_cpu(channel_and_cpu, &ch_name, &ch_cpu); |
| 744 | if(ch_cpu == -1) { |
| 745 | ERR("problem parsing channel name"); |
| 746 | retval = -1; |
| 747 | goto free_short_chan_name; |
| 748 | } |
| 749 | |
| 750 | ltt_lock_traces(); |
| 751 | trace = _ltt_trace_find(trace_name); |
| 752 | |
| 753 | if(trace == NULL) { |
| 754 | DBG("Cannot find trace. It was likely destroyed by the user."); |
| 755 | result = ustcomm_send_reply(&ustcomm_app.server, "NOTFOUND", src); |
| 756 | if(result) { |
| 757 | ERR("ustcomm_send_reply failed"); |
| 758 | retval = -1; |
| 759 | goto unlock_traces; |
| 760 | } |
| 761 | |
| 762 | goto unlock_traces; |
| 763 | } |
| 764 | |
| 765 | for(i=0; i<trace->nr_channels; i++) { |
| 766 | struct ust_channel *channel = &trace->channels[i]; |
| 767 | |
| 768 | if(!strcmp(trace->channels[i].channel_name, ch_name)) { |
| 769 | struct ust_buffer *buf = channel->buf[ch_cpu]; |
| 770 | |
| 771 | found = 1; |
| 772 | |
| 773 | result = ust_buffers_put_subbuf(buf, consumed_old); |
| 774 | if(result < 0) { |
| 775 | WARN("ust_buffers_put_subbuf: error (subbuf=%s)", channel_and_cpu); |
| 776 | if (asprintf(&reply, "%s", "ERROR") < 0) { |
| 777 | ERR("do_cmd_put_subbuffer : asprintf failed (ERROR)"); |
| 778 | retval = -1; |
| 779 | goto unlock_traces; |
| 780 | } |
| 781 | } |
| 782 | else { |
| 783 | DBG("ust_buffers_put_subbuf: success (subbuf=%s)", channel_and_cpu); |
| 784 | if (asprintf(&reply, "%s", "OK") < 0) { |
| 785 | ERR("do_cmd_put_subbuffer : asprintf failed (OK)"); |
| 786 | retval = -1; |
| 787 | goto unlock_traces; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 792 | if(result) { |
| 793 | ERR("ustcomm_send_reply failed"); |
| 794 | free(reply); |
| 795 | retval = -1; |
| 796 | goto unlock_traces; |
| 797 | } |
| 798 | |
| 799 | free(reply); |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | if(found == 0) { |
| 804 | ERR("unable to find channel"); |
| 805 | } |
| 806 | |
| 807 | unlock_traces: |
| 808 | ltt_unlock_traces(); |
| 809 | free_short_chan_name: |
| 810 | free(ch_name); |
| 811 | free_consumed_old_str: |
| 812 | free(consumed_old_str); |
| 813 | free_channel_and_cpu: |
| 814 | free(channel_and_cpu); |
| 815 | |
| 816 | end: |
| 817 | return retval; |
| 818 | } |
| 819 | |
| 820 | static void listener_cleanup(void *ptr) |
| 821 | { |
| 822 | ustcomm_fini_app(&ustcomm_app, 0); |
| 823 | } |
| 824 | |
| 825 | static void do_cmd_force_switch() |
| 826 | { |
| 827 | struct blocked_consumer *bc; |
| 828 | |
| 829 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 830 | ltt_force_switch(bc->buf, FORCE_FLUSH); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | int process_client_cmd(char *recvbuf, struct ustcomm_source *src) |
| 835 | { |
| 836 | int result; |
| 837 | char trace_name[] = "auto"; |
| 838 | char trace_type[] = "ustrelay"; |
| 839 | int len; |
| 840 | |
| 841 | DBG("received a message! it's: %s", recvbuf); |
| 842 | len = strlen(recvbuf); |
| 843 | |
| 844 | if(!strcmp(recvbuf, "print_markers")) { |
| 845 | print_markers(stderr); |
| 846 | } |
| 847 | else if(!strcmp(recvbuf, "list_markers")) { |
| 848 | char *ptr; |
| 849 | size_t size; |
| 850 | FILE *fp; |
| 851 | |
| 852 | fp = open_memstream(&ptr, &size); |
| 853 | print_markers(fp); |
| 854 | fclose(fp); |
| 855 | |
| 856 | result = ustcomm_send_reply(&ustcomm_app.server, ptr, src); |
| 857 | |
| 858 | free(ptr); |
| 859 | } |
| 860 | else if(!strcmp(recvbuf, "start")) { |
| 861 | /* start is an operation that setups the trace, allocates it and starts it */ |
| 862 | result = ltt_trace_setup(trace_name); |
| 863 | if(result < 0) { |
| 864 | ERR("ltt_trace_setup failed"); |
| 865 | return -1; |
| 866 | } |
| 867 | |
| 868 | result = ltt_trace_set_type(trace_name, trace_type); |
| 869 | if(result < 0) { |
| 870 | ERR("ltt_trace_set_type failed"); |
| 871 | return -1; |
| 872 | } |
| 873 | |
| 874 | result = ltt_trace_alloc(trace_name); |
| 875 | if(result < 0) { |
| 876 | ERR("ltt_trace_alloc failed"); |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | inform_consumer_daemon(trace_name); |
| 881 | |
| 882 | result = ltt_trace_start(trace_name); |
| 883 | if(result < 0) { |
| 884 | ERR("ltt_trace_start failed"); |
| 885 | return -1; |
| 886 | } |
| 887 | } |
| 888 | else if(!strcmp(recvbuf, "trace_setup")) { |
| 889 | DBG("trace setup"); |
| 890 | |
| 891 | result = ltt_trace_setup(trace_name); |
| 892 | if(result < 0) { |
| 893 | ERR("ltt_trace_setup failed"); |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | result = ltt_trace_set_type(trace_name, trace_type); |
| 898 | if(result < 0) { |
| 899 | ERR("ltt_trace_set_type failed"); |
| 900 | return -1; |
| 901 | } |
| 902 | } |
| 903 | else if(!strcmp(recvbuf, "trace_alloc")) { |
| 904 | DBG("trace alloc"); |
| 905 | |
| 906 | result = ltt_trace_alloc(trace_name); |
| 907 | if(result < 0) { |
| 908 | ERR("ltt_trace_alloc failed"); |
| 909 | return -1; |
| 910 | } |
| 911 | inform_consumer_daemon(trace_name); |
| 912 | } |
| 913 | else if(!strcmp(recvbuf, "trace_create")) { |
| 914 | DBG("trace create"); |
| 915 | |
| 916 | result = ltt_trace_setup(trace_name); |
| 917 | if(result < 0) { |
| 918 | ERR("ltt_trace_setup failed"); |
| 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | result = ltt_trace_set_type(trace_name, trace_type); |
| 923 | if(result < 0) { |
| 924 | ERR("ltt_trace_set_type failed"); |
| 925 | return -1; |
| 926 | } |
| 927 | } |
| 928 | else if(!strcmp(recvbuf, "trace_start")) { |
| 929 | DBG("trace start"); |
| 930 | |
| 931 | result = ltt_trace_alloc(trace_name); |
| 932 | if(result < 0) { |
| 933 | ERR("ltt_trace_alloc failed"); |
| 934 | return -1; |
| 935 | } |
| 936 | if(!result) { |
| 937 | inform_consumer_daemon(trace_name); |
| 938 | } |
| 939 | |
| 940 | result = ltt_trace_start(trace_name); |
| 941 | if(result < 0) { |
| 942 | ERR("ltt_trace_start failed"); |
| 943 | return -1; |
| 944 | } |
| 945 | } |
| 946 | else if(!strcmp(recvbuf, "trace_stop")) { |
| 947 | DBG("trace stop"); |
| 948 | |
| 949 | result = ltt_trace_stop(trace_name); |
| 950 | if(result < 0) { |
| 951 | ERR("ltt_trace_stop failed"); |
| 952 | return -1; |
| 953 | } |
| 954 | } |
| 955 | else if(!strcmp(recvbuf, "trace_destroy")) { |
| 956 | |
| 957 | DBG("trace destroy"); |
| 958 | |
| 959 | result = ltt_trace_destroy(trace_name, 0); |
| 960 | if(result < 0) { |
| 961 | ERR("ltt_trace_destroy failed"); |
| 962 | return -1; |
| 963 | } |
| 964 | } |
| 965 | else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) { |
| 966 | do_cmd_get_shmid(recvbuf, src); |
| 967 | } |
| 968 | else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) { |
| 969 | do_cmd_get_n_subbufs(recvbuf, src); |
| 970 | } |
| 971 | else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) { |
| 972 | do_cmd_get_subbuf_size(recvbuf, src); |
| 973 | } |
| 974 | else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) { |
| 975 | char *libfile; |
| 976 | |
| 977 | libfile = nth_token(recvbuf, 1); |
| 978 | |
| 979 | DBG("load_probe_lib loading %s", libfile); |
| 980 | |
| 981 | free(libfile); |
| 982 | } |
| 983 | else if(nth_token_is(recvbuf, "get_subbuffer", 0) == 1) { |
| 984 | do_cmd_get_subbuffer(recvbuf, src); |
| 985 | } |
| 986 | else if(nth_token_is(recvbuf, "put_subbuffer", 0) == 1) { |
| 987 | do_cmd_put_subbuffer(recvbuf, src); |
| 988 | } |
| 989 | else if(nth_token_is(recvbuf, "set_subbuf_size", 0) == 1) { |
| 990 | do_cmd_set_subbuf_size(recvbuf, src); |
| 991 | } |
| 992 | else if(nth_token_is(recvbuf, "set_subbuf_num", 0) == 1) { |
| 993 | do_cmd_set_subbuf_num(recvbuf, src); |
| 994 | } |
| 995 | else if(nth_token_is(recvbuf, "enable_marker", 0) == 1) { |
| 996 | char *channel_slash_name = nth_token(recvbuf, 1); |
| 997 | char channel_name[256]=""; |
| 998 | char marker_name[256]=""; |
| 999 | |
| 1000 | result = sscanf(channel_slash_name, "%255[^/]/%255s", channel_name, marker_name); |
| 1001 | |
| 1002 | if(channel_name == NULL || marker_name == NULL) { |
| 1003 | WARN("invalid marker name"); |
| 1004 | goto next_cmd; |
| 1005 | } |
| 1006 | |
| 1007 | result = ltt_marker_connect(channel_name, marker_name, "default"); |
| 1008 | if(result < 0) { |
| 1009 | WARN("could not enable marker; channel=%s, name=%s", channel_name, marker_name); |
| 1010 | } |
| 1011 | } |
| 1012 | else if(nth_token_is(recvbuf, "disable_marker", 0) == 1) { |
| 1013 | char *channel_slash_name = nth_token(recvbuf, 1); |
| 1014 | char *marker_name; |
| 1015 | char *channel_name; |
| 1016 | |
| 1017 | result = sscanf(channel_slash_name, "%a[^/]/%as", &channel_name, &marker_name); |
| 1018 | |
| 1019 | if(marker_name == NULL) { |
| 1020 | } |
| 1021 | |
| 1022 | result = ltt_marker_disconnect(channel_name, marker_name, "default"); |
| 1023 | if(result < 0) { |
| 1024 | WARN("could not disable marker; channel=%s, name=%s", channel_name, marker_name); |
| 1025 | } |
| 1026 | } |
| 1027 | else if(nth_token_is(recvbuf, "get_pidunique", 0) == 1) { |
| 1028 | char *reply; |
| 1029 | |
| 1030 | if (asprintf(&reply, "%lld", pidunique) < 0) { |
| 1031 | ERR("process_client_cmd : asprintf failed (%lld)", |
| 1032 | pidunique); |
| 1033 | goto next_cmd; |
| 1034 | } |
| 1035 | |
| 1036 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 1037 | if(result) { |
| 1038 | ERR("listener: get_pidunique: ustcomm_send_reply failed"); |
| 1039 | goto next_cmd; |
| 1040 | } |
| 1041 | |
| 1042 | free(reply); |
| 1043 | } |
| 1044 | else if(nth_token_is(recvbuf, "get_sock_path", 0) == 1) { |
| 1045 | char *reply = getenv("UST_DAEMON_SOCKET"); |
| 1046 | if(!reply) { |
| 1047 | if (asprintf(&reply, "%s/%s", SOCK_DIR, "ustd") < 0) { |
| 1048 | ERR("process_client_cmd : asprintf failed (%s/ustd)", |
| 1049 | SOCK_DIR); |
| 1050 | goto next_cmd; |
| 1051 | } |
| 1052 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 1053 | free(reply); |
| 1054 | } |
| 1055 | else { |
| 1056 | result = ustcomm_send_reply(&ustcomm_app.server, reply, src); |
| 1057 | } |
| 1058 | if(result) |
| 1059 | ERR("ustcomm_send_reply failed"); |
| 1060 | } |
| 1061 | else if(nth_token_is(recvbuf, "set_sock_path", 0) == 1) { |
| 1062 | char *sock_path = nth_token(recvbuf, 1); |
| 1063 | result = setenv("UST_DAEMON_SOCKET", sock_path, 1); |
| 1064 | if(result) |
| 1065 | ERR("cannot set UST_DAEMON_SOCKET environment variable"); |
| 1066 | } |
| 1067 | else if(nth_token_is(recvbuf, "force_switch", 0) == 1) { |
| 1068 | do_cmd_force_switch(); |
| 1069 | } |
| 1070 | else { |
| 1071 | ERR("unable to parse message: %s", recvbuf); |
| 1072 | } |
| 1073 | |
| 1074 | next_cmd: |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | void *listener_main(void *p) |
| 1080 | { |
| 1081 | int result; |
| 1082 | |
| 1083 | DBG("LISTENER"); |
| 1084 | |
| 1085 | pthread_cleanup_push(listener_cleanup, NULL); |
| 1086 | |
| 1087 | for(;;) { |
| 1088 | struct mpentries mpent; |
| 1089 | |
| 1090 | multipoll_init(&mpent); |
| 1091 | |
| 1092 | blocked_consumers_add_to_mp(&mpent); |
| 1093 | ustcomm_mp_add_app_clients(&mpent, &ustcomm_app, process_client_cmd); |
| 1094 | |
| 1095 | result = multipoll_poll(&mpent, -1); |
| 1096 | if(result == -1) { |
| 1097 | ERR("error in multipoll_poll"); |
| 1098 | } |
| 1099 | |
| 1100 | multipoll_destroy(&mpent); |
| 1101 | } |
| 1102 | |
| 1103 | pthread_cleanup_pop(1); |
| 1104 | } |
| 1105 | |
| 1106 | /* These should only be accessed in the parent thread, |
| 1107 | * not the listener. |
| 1108 | */ |
| 1109 | static volatile sig_atomic_t have_listener = 0; |
| 1110 | static pthread_t listener_thread; |
| 1111 | |
| 1112 | void create_listener(void) |
| 1113 | { |
| 1114 | int result; |
| 1115 | sigset_t sig_all_blocked; |
| 1116 | sigset_t orig_parent_mask; |
| 1117 | |
| 1118 | if(have_listener) { |
| 1119 | WARN("not creating listener because we already had one"); |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | /* A new thread created by pthread_create inherits the signal mask |
| 1124 | * from the parent. To avoid any signal being received by the |
| 1125 | * listener thread, we block all signals temporarily in the parent, |
| 1126 | * while we create the listener thread. |
| 1127 | */ |
| 1128 | |
| 1129 | sigfillset(&sig_all_blocked); |
| 1130 | |
| 1131 | result = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); |
| 1132 | if(result) { |
| 1133 | PERROR("pthread_sigmask: %s", strerror(result)); |
| 1134 | } |
| 1135 | |
| 1136 | result = pthread_create(&listener_thread, NULL, listener_main, NULL); |
| 1137 | if(result == -1) { |
| 1138 | PERROR("pthread_create"); |
| 1139 | } |
| 1140 | |
| 1141 | /* Restore original signal mask in parent */ |
| 1142 | result = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); |
| 1143 | if(result) { |
| 1144 | PERROR("pthread_sigmask: %s", strerror(result)); |
| 1145 | } |
| 1146 | else { |
| 1147 | have_listener = 1; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | static int init_socket(void) |
| 1152 | { |
| 1153 | return ustcomm_init_app(getpid(), &ustcomm_app); |
| 1154 | } |
| 1155 | |
| 1156 | #define AUTOPROBE_DISABLED 0 |
| 1157 | #define AUTOPROBE_ENABLE_ALL 1 |
| 1158 | #define AUTOPROBE_ENABLE_REGEX 2 |
| 1159 | static int autoprobe_method = AUTOPROBE_DISABLED; |
| 1160 | static regex_t autoprobe_regex; |
| 1161 | |
| 1162 | static void auto_probe_connect(struct marker *m) |
| 1163 | { |
| 1164 | int result; |
| 1165 | |
| 1166 | char* concat_name = NULL; |
| 1167 | const char *probe_name = "default"; |
| 1168 | |
| 1169 | if(autoprobe_method == AUTOPROBE_DISABLED) { |
| 1170 | return; |
| 1171 | } |
| 1172 | else if(autoprobe_method == AUTOPROBE_ENABLE_REGEX) { |
| 1173 | result = asprintf(&concat_name, "%s/%s", m->channel, m->name); |
| 1174 | if(result == -1) { |
| 1175 | ERR("auto_probe_connect: asprintf failed (marker %s/%s)", |
| 1176 | m->channel, m->name); |
| 1177 | return; |
| 1178 | } |
| 1179 | if (regexec(&autoprobe_regex, concat_name, 0, NULL, 0)) { |
| 1180 | free(concat_name); |
| 1181 | return; |
| 1182 | } |
| 1183 | free(concat_name); |
| 1184 | } |
| 1185 | |
| 1186 | result = ltt_marker_connect(m->channel, m->name, probe_name); |
| 1187 | if(result && result != -EEXIST) |
| 1188 | ERR("ltt_marker_connect (marker = %s/%s, errno = %d)", m->channel, m->name, -result); |
| 1189 | |
| 1190 | DBG("auto connected marker %s (addr: %p) %s to probe default", m->channel, m, m->name); |
| 1191 | |
| 1192 | } |
| 1193 | |
| 1194 | static void __attribute__((constructor)) init() |
| 1195 | { |
| 1196 | int result; |
| 1197 | char* autoprobe_val = NULL; |
| 1198 | char* subbuffer_size_val = NULL; |
| 1199 | char* subbuffer_count_val = NULL; |
| 1200 | unsigned int subbuffer_size; |
| 1201 | unsigned int subbuffer_count; |
| 1202 | unsigned int power; |
| 1203 | |
| 1204 | /* Assign the pidunique, to be able to differentiate the processes with same |
| 1205 | * pid, (before and after an exec). |
| 1206 | */ |
| 1207 | pidunique = make_pidunique(); |
| 1208 | |
| 1209 | DBG("Tracectl constructor"); |
| 1210 | |
| 1211 | result = init_socket(); |
| 1212 | if(result == -1) { |
| 1213 | ERR("init_socket error"); |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | create_listener(); |
| 1218 | |
| 1219 | autoprobe_val = getenv("UST_AUTOPROBE"); |
| 1220 | if(autoprobe_val) { |
| 1221 | struct marker_iter iter; |
| 1222 | |
| 1223 | DBG("Autoprobe enabled."); |
| 1224 | |
| 1225 | /* Ensure markers are initialized */ |
| 1226 | //init_markers(); |
| 1227 | |
| 1228 | /* Ensure marker control is initialized, for the probe */ |
| 1229 | init_marker_control(); |
| 1230 | |
| 1231 | /* first, set the callback that will connect the |
| 1232 | * probe on new markers |
| 1233 | */ |
| 1234 | if(autoprobe_val[0] == '/') { |
| 1235 | result = regcomp(&autoprobe_regex, autoprobe_val+1, 0); |
| 1236 | if (result) { |
| 1237 | char regexerr[150]; |
| 1238 | |
| 1239 | regerror(result, &autoprobe_regex, regexerr, sizeof(regexerr)); |
| 1240 | ERR("cannot parse regex %s (%s), will ignore UST_AUTOPROBE", autoprobe_val, regexerr); |
| 1241 | /* don't crash the application just for this */ |
| 1242 | } |
| 1243 | else { |
| 1244 | autoprobe_method = AUTOPROBE_ENABLE_REGEX; |
| 1245 | } |
| 1246 | } |
| 1247 | else { |
| 1248 | /* just enable all instrumentation */ |
| 1249 | autoprobe_method = AUTOPROBE_ENABLE_ALL; |
| 1250 | } |
| 1251 | |
| 1252 | marker_set_new_marker_cb(auto_probe_connect); |
| 1253 | |
| 1254 | /* Now, connect the probes that were already registered. */ |
| 1255 | marker_iter_reset(&iter); |
| 1256 | marker_iter_start(&iter); |
| 1257 | |
| 1258 | DBG("now iterating on markers already registered"); |
| 1259 | while(iter.marker) { |
| 1260 | DBG("now iterating on marker %s", iter.marker->name); |
| 1261 | auto_probe_connect(iter.marker); |
| 1262 | marker_iter_next(&iter); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | if(getenv("UST_OVERWRITE")) { |
| 1267 | int val = atoi(getenv("UST_OVERWRITE")); |
| 1268 | if(val == 0 || val == 1) { |
| 1269 | STORE_SHARED(ust_channels_overwrite_by_default, val); |
| 1270 | } |
| 1271 | else { |
| 1272 | WARN("invalid value for UST_OVERWRITE"); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | if(getenv("UST_AUTOCOLLECT")) { |
| 1277 | int val = atoi(getenv("UST_AUTOCOLLECT")); |
| 1278 | if(val == 0 || val == 1) { |
| 1279 | STORE_SHARED(ust_channels_request_collection_by_default, val); |
| 1280 | } |
| 1281 | else { |
| 1282 | WARN("invalid value for UST_AUTOCOLLECT"); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | subbuffer_size_val = getenv("UST_SUBBUF_SIZE"); |
| 1287 | if(subbuffer_size_val) { |
| 1288 | sscanf(subbuffer_size_val, "%u", &subbuffer_size); |
| 1289 | power = pow2_higher_or_eq(subbuffer_size); |
| 1290 | if(power != subbuffer_size) |
| 1291 | WARN("using the next power of two for buffer size = %u\n", power); |
| 1292 | chan_infos[LTT_CHANNEL_UST].def_subbufsize = power; |
| 1293 | } |
| 1294 | |
| 1295 | subbuffer_count_val = getenv("UST_SUBBUF_NUM"); |
| 1296 | if(subbuffer_count_val) { |
| 1297 | sscanf(subbuffer_count_val, "%u", &subbuffer_count); |
| 1298 | if(subbuffer_count < 2) |
| 1299 | subbuffer_count = 2; |
| 1300 | chan_infos[LTT_CHANNEL_UST].def_subbufcount = subbuffer_count; |
| 1301 | } |
| 1302 | |
| 1303 | if(getenv("UST_TRACE")) { |
| 1304 | char trace_name[] = "auto"; |
| 1305 | char trace_type[] = "ustrelay"; |
| 1306 | |
| 1307 | DBG("starting early tracing"); |
| 1308 | |
| 1309 | /* Ensure marker control is initialized */ |
| 1310 | init_marker_control(); |
| 1311 | |
| 1312 | /* Ensure markers are initialized */ |
| 1313 | init_markers(); |
| 1314 | |
| 1315 | /* Ensure buffers are initialized, for the transport to be available. |
| 1316 | * We are about to set a trace type and it will fail without this. |
| 1317 | */ |
| 1318 | init_ustrelay_transport(); |
| 1319 | |
| 1320 | /* FIXME: When starting early tracing (here), depending on the |
| 1321 | * order of constructors, it is very well possible some marker |
| 1322 | * sections are not yet registered. Because of this, some |
| 1323 | * channels may not be registered. Yet, we are about to ask the |
| 1324 | * daemon to collect the channels. Channels which are not yet |
| 1325 | * registered will not be collected. |
| 1326 | * |
| 1327 | * Currently, in LTTng, there is no way to add a channel after |
| 1328 | * trace start. The reason for this is that it induces complex |
| 1329 | * concurrency issues on the trace structures, which can only |
| 1330 | * be resolved using RCU. This has not been done yet. As a |
| 1331 | * workaround, we are forcing the registration of the "ust" |
| 1332 | * channel here. This is the only channel (apart from metadata) |
| 1333 | * that can be reliably used in early tracing. |
| 1334 | * |
| 1335 | * Non-early tracing does not have this problem and can use |
| 1336 | * arbitrary channel names. |
| 1337 | */ |
| 1338 | ltt_channels_register("ust"); |
| 1339 | |
| 1340 | result = ltt_trace_setup(trace_name); |
| 1341 | if(result < 0) { |
| 1342 | ERR("ltt_trace_setup failed"); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
| 1346 | result = ltt_trace_set_type(trace_name, trace_type); |
| 1347 | if(result < 0) { |
| 1348 | ERR("ltt_trace_set_type failed"); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | result = ltt_trace_alloc(trace_name); |
| 1353 | if(result < 0) { |
| 1354 | ERR("ltt_trace_alloc failed"); |
| 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | result = ltt_trace_start(trace_name); |
| 1359 | if(result < 0) { |
| 1360 | ERR("ltt_trace_start failed"); |
| 1361 | return; |
| 1362 | } |
| 1363 | |
| 1364 | /* Do this after the trace is started in order to avoid creating confusion |
| 1365 | * if the trace fails to start. */ |
| 1366 | inform_consumer_daemon(trace_name); |
| 1367 | } |
| 1368 | |
| 1369 | return; |
| 1370 | |
| 1371 | /* should decrementally destroy stuff if error */ |
| 1372 | |
| 1373 | } |
| 1374 | |
| 1375 | /* This is only called if we terminate normally, not with an unhandled signal, |
| 1376 | * so we cannot rely on it. However, for now, LTTV requires that the header of |
| 1377 | * the last sub-buffer contain a valid end time for the trace. This is done |
| 1378 | * automatically only when the trace is properly stopped. |
| 1379 | * |
| 1380 | * If the traced program crashed, it is always possible to manually add the |
| 1381 | * right value in the header, or to open the trace in text mode. |
| 1382 | * |
| 1383 | * FIXME: Fix LTTV so it doesn't need this. |
| 1384 | */ |
| 1385 | |
| 1386 | static void destroy_traces(void) |
| 1387 | { |
| 1388 | int result; |
| 1389 | |
| 1390 | /* if trace running, finish it */ |
| 1391 | |
| 1392 | DBG("destructor stopping traces"); |
| 1393 | |
| 1394 | result = ltt_trace_stop("auto"); |
| 1395 | if(result == -1) { |
| 1396 | ERR("ltt_trace_stop error"); |
| 1397 | } |
| 1398 | |
| 1399 | result = ltt_trace_destroy("auto", 0); |
| 1400 | if(result == -1) { |
| 1401 | ERR("ltt_trace_destroy error"); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | static int trace_recording(void) |
| 1406 | { |
| 1407 | int retval = 0; |
| 1408 | struct ust_trace *trace; |
| 1409 | |
| 1410 | ltt_lock_traces(); |
| 1411 | |
| 1412 | list_for_each_entry(trace, <t_traces.head, list) { |
| 1413 | if(trace->active) { |
| 1414 | retval = 1; |
| 1415 | break; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | ltt_unlock_traces(); |
| 1420 | |
| 1421 | return retval; |
| 1422 | } |
| 1423 | |
| 1424 | #if 0 |
| 1425 | static int have_consumer(void) |
| 1426 | { |
| 1427 | return !list_empty(&blocked_consumers); |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
| 1431 | int restarting_usleep(useconds_t usecs) |
| 1432 | { |
| 1433 | struct timespec tv; |
| 1434 | int result; |
| 1435 | |
| 1436 | tv.tv_sec = 0; |
| 1437 | tv.tv_nsec = usecs * 1000; |
| 1438 | |
| 1439 | do { |
| 1440 | result = nanosleep(&tv, &tv); |
| 1441 | } while(result == -1 && errno == EINTR); |
| 1442 | |
| 1443 | return result; |
| 1444 | } |
| 1445 | |
| 1446 | static void stop_listener(void) |
| 1447 | { |
| 1448 | int result; |
| 1449 | |
| 1450 | if(!have_listener) |
| 1451 | return; |
| 1452 | |
| 1453 | result = pthread_cancel(listener_thread); |
| 1454 | if(result != 0) { |
| 1455 | ERR("pthread_cancel: %s", strerror(result)); |
| 1456 | } |
| 1457 | result = pthread_join(listener_thread, NULL); |
| 1458 | if(result != 0) { |
| 1459 | ERR("pthread_join: %s", strerror(result)); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /* This destructor keeps the process alive for a few seconds in order |
| 1464 | * to leave time to ustd to connect to its buffers. This is necessary |
| 1465 | * for programs whose execution is very short. It is also useful in all |
| 1466 | * programs when tracing is started close to the end of the program |
| 1467 | * execution. |
| 1468 | * |
| 1469 | * FIXME: For now, this only works for the first trace created in a |
| 1470 | * process. |
| 1471 | */ |
| 1472 | |
| 1473 | static void __attribute__((destructor)) keepalive() |
| 1474 | { |
| 1475 | if(trace_recording() && LOAD_SHARED(buffers_to_export)) { |
| 1476 | int total = 0; |
| 1477 | DBG("Keeping process alive for consumer daemon..."); |
| 1478 | while(LOAD_SHARED(buffers_to_export)) { |
| 1479 | const int interv = 200000; |
| 1480 | restarting_usleep(interv); |
| 1481 | total += interv; |
| 1482 | |
| 1483 | if(total >= 3000000) { |
| 1484 | WARN("non-consumed buffers remaining after wait limit; not waiting anymore"); |
| 1485 | break; |
| 1486 | } |
| 1487 | } |
| 1488 | DBG("Finally dying..."); |
| 1489 | } |
| 1490 | |
| 1491 | destroy_traces(); |
| 1492 | |
| 1493 | /* Ask the listener to stop and clean up. */ |
| 1494 | stop_listener(); |
| 1495 | } |
| 1496 | |
| 1497 | void ust_potential_exec(void) |
| 1498 | { |
| 1499 | trace_mark(ust, potential_exec, MARK_NOARGS); |
| 1500 | |
| 1501 | DBG("test"); |
| 1502 | |
| 1503 | keepalive(); |
| 1504 | } |
| 1505 | |
| 1506 | /* Notify ust that there was a fork. This needs to be called inside |
| 1507 | * the new process, anytime a process whose memory is not shared with |
| 1508 | * the parent is created. If this function is not called, the events |
| 1509 | * of the new process will not be collected. |
| 1510 | * |
| 1511 | * Signals should be disabled before the fork and reenabled only after |
| 1512 | * this call in order to guarantee tracing is not started before ust_fork() |
| 1513 | * sanitizes the new process. |
| 1514 | */ |
| 1515 | |
| 1516 | static void ust_fork(void) |
| 1517 | { |
| 1518 | struct blocked_consumer *bc; |
| 1519 | struct blocked_consumer *deletable_bc = NULL; |
| 1520 | int result; |
| 1521 | |
| 1522 | /* FIXME: technically, the locks could have been taken before the fork */ |
| 1523 | DBG("ust: forking"); |
| 1524 | |
| 1525 | /* break lock if necessary */ |
| 1526 | ltt_unlock_traces(); |
| 1527 | |
| 1528 | ltt_trace_stop("auto"); |
| 1529 | ltt_trace_destroy("auto", 1); |
| 1530 | /* Delete all active connections */ |
| 1531 | ustcomm_close_all_connections(&ustcomm_app.server); |
| 1532 | |
| 1533 | /* Delete all blocked consumers */ |
| 1534 | list_for_each_entry(bc, &blocked_consumers, list) { |
| 1535 | result = close(bc->fd_producer); |
| 1536 | if(result == -1) { |
| 1537 | PERROR("close"); |
| 1538 | } |
| 1539 | free(deletable_bc); |
| 1540 | deletable_bc = bc; |
| 1541 | list_del(&bc->list); |
| 1542 | } |
| 1543 | |
| 1544 | /* free app, keeping socket file */ |
| 1545 | ustcomm_fini_app(&ustcomm_app, 1); |
| 1546 | |
| 1547 | STORE_SHARED(buffers_to_export, 0); |
| 1548 | have_listener = 0; |
| 1549 | init_socket(); |
| 1550 | create_listener(); |
| 1551 | ltt_trace_setup("auto"); |
| 1552 | result = ltt_trace_set_type("auto", "ustrelay"); |
| 1553 | if(result < 0) { |
| 1554 | ERR("ltt_trace_set_type failed"); |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | ltt_trace_alloc("auto"); |
| 1559 | ltt_trace_start("auto"); |
| 1560 | inform_consumer_daemon("auto"); |
| 1561 | } |
| 1562 | |
| 1563 | void ust_before_fork(ust_fork_info_t *fork_info) |
| 1564 | { |
| 1565 | /* Disable signals. This is to avoid that the child |
| 1566 | * intervenes before it is properly setup for tracing. It is |
| 1567 | * safer to disable all signals, because then we know we are not |
| 1568 | * breaking anything by restoring the original mask. |
| 1569 | */ |
| 1570 | sigset_t all_sigs; |
| 1571 | int result; |
| 1572 | |
| 1573 | /* FIXME: |
| 1574 | - only do this if tracing is active |
| 1575 | */ |
| 1576 | |
| 1577 | /* Disable signals */ |
| 1578 | sigfillset(&all_sigs); |
| 1579 | result = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs); |
| 1580 | if(result == -1) { |
| 1581 | PERROR("sigprocmask"); |
| 1582 | return; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /* Don't call this function directly in a traced program */ |
| 1587 | static void ust_after_fork_common(ust_fork_info_t *fork_info) |
| 1588 | { |
| 1589 | int result; |
| 1590 | |
| 1591 | /* Restore signals */ |
| 1592 | result = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL); |
| 1593 | if(result == -1) { |
| 1594 | PERROR("sigprocmask"); |
| 1595 | return; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | void ust_after_fork_parent(ust_fork_info_t *fork_info) |
| 1600 | { |
| 1601 | /* Reenable signals */ |
| 1602 | ust_after_fork_common(fork_info); |
| 1603 | } |
| 1604 | |
| 1605 | void ust_after_fork_child(ust_fork_info_t *fork_info) |
| 1606 | { |
| 1607 | /* First sanitize the child */ |
| 1608 | ust_fork(); |
| 1609 | |
| 1610 | /* Then reenable interrupts */ |
| 1611 | ust_after_fork_common(fork_info); |
| 1612 | } |
| 1613 | |