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