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