| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <urcu/list.h> |
| 22 | #include <urcu/uatomic.h> |
| 23 | |
| 24 | #include <common/defaults.h> |
| 25 | #include <common/common.h> |
| 26 | #include <common/sessiond-comm/sessiond-comm.h> |
| 27 | #include <common/relayd/relayd.h> |
| 28 | #include <common/utils.h> |
| 29 | |
| 30 | #include "channel.h" |
| 31 | #include "consumer.h" |
| 32 | #include "event.h" |
| 33 | #include "health-sessiond.h" |
| 34 | #include "kernel.h" |
| 35 | #include "kernel-consumer.h" |
| 36 | #include "lttng-sessiond.h" |
| 37 | #include "utils.h" |
| 38 | |
| 39 | #include "cmd.h" |
| 40 | |
| 41 | /* |
| 42 | * Used to keep a unique index for each relayd socket created where this value |
| 43 | * is associated with streams on the consumer so it can match the right relayd |
| 44 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
| 45 | * held. |
| 46 | */ |
| 47 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
| 48 | static uint64_t relayd_net_seq_idx; |
| 49 | |
| 50 | /* |
| 51 | * Create a session path used by list_lttng_sessions for the case that the |
| 52 | * session consumer is on the network. |
| 53 | */ |
| 54 | static int build_network_session_path(char *dst, size_t size, |
| 55 | struct ltt_session *session) |
| 56 | { |
| 57 | int ret, kdata_port, udata_port; |
| 58 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; |
| 59 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; |
| 60 | |
| 61 | assert(session); |
| 62 | assert(dst); |
| 63 | |
| 64 | memset(tmp_urls, 0, sizeof(tmp_urls)); |
| 65 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); |
| 66 | |
| 67 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; |
| 68 | |
| 69 | if (session->kernel_session && session->kernel_session->consumer) { |
| 70 | kuri = &session->kernel_session->consumer->dst.net.control; |
| 71 | kdata_port = session->kernel_session->consumer->dst.net.data.port; |
| 72 | } |
| 73 | |
| 74 | if (session->ust_session && session->ust_session->consumer) { |
| 75 | uuri = &session->ust_session->consumer->dst.net.control; |
| 76 | udata_port = session->ust_session->consumer->dst.net.data.port; |
| 77 | } |
| 78 | |
| 79 | if (uuri == NULL && kuri == NULL) { |
| 80 | uri = &session->consumer->dst.net.control; |
| 81 | kdata_port = session->consumer->dst.net.data.port; |
| 82 | } else if (kuri && uuri) { |
| 83 | ret = uri_compare(kuri, uuri); |
| 84 | if (ret) { |
| 85 | /* Not Equal */ |
| 86 | uri = kuri; |
| 87 | /* Build uuri URL string */ |
| 88 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); |
| 89 | if (ret < 0) { |
| 90 | goto error; |
| 91 | } |
| 92 | } else { |
| 93 | uri = kuri; |
| 94 | } |
| 95 | } else if (kuri && uuri == NULL) { |
| 96 | uri = kuri; |
| 97 | } else if (uuri && kuri == NULL) { |
| 98 | uri = uuri; |
| 99 | } |
| 100 | |
| 101 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); |
| 102 | if (ret < 0) { |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Do we have a UST url set. If yes, this means we have both kernel and UST |
| 108 | * to print. |
| 109 | */ |
| 110 | if (*tmp_uurl != '\0') { |
| 111 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
| 112 | tmp_urls, kdata_port, tmp_uurl, udata_port); |
| 113 | } else { |
| 114 | int dport; |
| 115 | if (kuri || (!kuri && !uuri)) { |
| 116 | dport = kdata_port; |
| 117 | } else { |
| 118 | /* No kernel URI, use the UST port. */ |
| 119 | dport = udata_port; |
| 120 | } |
| 121 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); |
| 122 | } |
| 123 | |
| 124 | error: |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Fill lttng_channel array of all channels. |
| 130 | */ |
| 131 | static void list_lttng_channels(int domain, struct ltt_session *session, |
| 132 | struct lttng_channel *channels) |
| 133 | { |
| 134 | int i = 0; |
| 135 | struct ltt_kernel_channel *kchan; |
| 136 | |
| 137 | DBG("Listing channels for session %s", session->name); |
| 138 | |
| 139 | switch (domain) { |
| 140 | case LTTNG_DOMAIN_KERNEL: |
| 141 | /* Kernel channels */ |
| 142 | if (session->kernel_session != NULL) { |
| 143 | cds_list_for_each_entry(kchan, |
| 144 | &session->kernel_session->channel_list.head, list) { |
| 145 | /* Copy lttng_channel struct to array */ |
| 146 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); |
| 147 | channels[i].enabled = kchan->enabled; |
| 148 | i++; |
| 149 | } |
| 150 | } |
| 151 | break; |
| 152 | case LTTNG_DOMAIN_UST: |
| 153 | { |
| 154 | struct lttng_ht_iter iter; |
| 155 | struct ltt_ust_channel *uchan; |
| 156 | |
| 157 | rcu_read_lock(); |
| 158 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
| 159 | &iter.iter, uchan, node.node) { |
| 160 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); |
| 161 | channels[i].attr.overwrite = uchan->attr.overwrite; |
| 162 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; |
| 163 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; |
| 164 | channels[i].attr.switch_timer_interval = |
| 165 | uchan->attr.switch_timer_interval; |
| 166 | channels[i].attr.read_timer_interval = |
| 167 | uchan->attr.read_timer_interval; |
| 168 | channels[i].enabled = uchan->enabled; |
| 169 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
| 170 | channels[i].attr.tracefile_count = uchan->tracefile_count; |
| 171 | switch (uchan->attr.output) { |
| 172 | case LTTNG_UST_MMAP: |
| 173 | default: |
| 174 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
| 175 | break; |
| 176 | } |
| 177 | i++; |
| 178 | } |
| 179 | rcu_read_unlock(); |
| 180 | break; |
| 181 | } |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Create a list of JUL domain events. |
| 189 | * |
| 190 | * Return number of events in list on success or else a negative value. |
| 191 | */ |
| 192 | static int list_lttng_jul_events(struct jul_domain *dom, |
| 193 | struct lttng_event **events) |
| 194 | { |
| 195 | int i = 0, ret = 0; |
| 196 | unsigned int nb_event = 0; |
| 197 | struct jul_event *event; |
| 198 | struct lttng_event *tmp_events; |
| 199 | struct lttng_ht_iter iter; |
| 200 | |
| 201 | assert(dom); |
| 202 | assert(events); |
| 203 | |
| 204 | DBG3("Listing JUL events"); |
| 205 | |
| 206 | nb_event = lttng_ht_get_count(dom->events); |
| 207 | if (nb_event == 0) { |
| 208 | ret = nb_event; |
| 209 | goto error; |
| 210 | } |
| 211 | |
| 212 | tmp_events = zmalloc(nb_event * sizeof(*tmp_events)); |
| 213 | if (!tmp_events) { |
| 214 | PERROR("zmalloc JUL events session"); |
| 215 | ret = -LTTNG_ERR_FATAL; |
| 216 | goto error; |
| 217 | } |
| 218 | |
| 219 | rcu_read_lock(); |
| 220 | cds_lfht_for_each_entry(dom->events->ht, &iter.iter, event, node.node) { |
| 221 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
| 222 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; |
| 223 | tmp_events[i].enabled = event->enabled; |
| 224 | tmp_events[i].loglevel = event->loglevel; |
| 225 | tmp_events[i].loglevel_type = event->loglevel_type; |
| 226 | i++; |
| 227 | } |
| 228 | rcu_read_unlock(); |
| 229 | |
| 230 | *events = tmp_events; |
| 231 | ret = nb_event; |
| 232 | |
| 233 | error: |
| 234 | assert(nb_event == i); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Create a list of ust global domain events. |
| 240 | */ |
| 241 | static int list_lttng_ust_global_events(char *channel_name, |
| 242 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) |
| 243 | { |
| 244 | int i = 0, ret = 0; |
| 245 | unsigned int nb_event = 0; |
| 246 | struct lttng_ht_iter iter; |
| 247 | struct lttng_ht_node_str *node; |
| 248 | struct ltt_ust_channel *uchan; |
| 249 | struct ltt_ust_event *uevent; |
| 250 | struct lttng_event *tmp; |
| 251 | |
| 252 | DBG("Listing UST global events for channel %s", channel_name); |
| 253 | |
| 254 | rcu_read_lock(); |
| 255 | |
| 256 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); |
| 257 | node = lttng_ht_iter_get_node_str(&iter); |
| 258 | if (node == NULL) { |
| 259 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 260 | goto error; |
| 261 | } |
| 262 | |
| 263 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
| 264 | |
| 265 | nb_event += lttng_ht_get_count(uchan->events); |
| 266 | |
| 267 | if (nb_event == 0) { |
| 268 | ret = nb_event; |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | DBG3("Listing UST global %d events", nb_event); |
| 273 | |
| 274 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); |
| 275 | if (tmp == NULL) { |
| 276 | ret = LTTNG_ERR_FATAL; |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
| 281 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
| 282 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 283 | tmp[i].enabled = uevent->enabled; |
| 284 | |
| 285 | switch (uevent->attr.instrumentation) { |
| 286 | case LTTNG_UST_TRACEPOINT: |
| 287 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; |
| 288 | break; |
| 289 | case LTTNG_UST_PROBE: |
| 290 | tmp[i].type = LTTNG_EVENT_PROBE; |
| 291 | break; |
| 292 | case LTTNG_UST_FUNCTION: |
| 293 | tmp[i].type = LTTNG_EVENT_FUNCTION; |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | tmp[i].loglevel = uevent->attr.loglevel; |
| 298 | switch (uevent->attr.loglevel_type) { |
| 299 | case LTTNG_UST_LOGLEVEL_ALL: |
| 300 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 301 | break; |
| 302 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 303 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 304 | break; |
| 305 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 306 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 307 | break; |
| 308 | } |
| 309 | if (uevent->filter) { |
| 310 | tmp[i].filter = 1; |
| 311 | } |
| 312 | if (uevent->exclusion) { |
| 313 | tmp[i].exclusion = 1; |
| 314 | } |
| 315 | i++; |
| 316 | } |
| 317 | |
| 318 | ret = nb_event; |
| 319 | *events = tmp; |
| 320 | |
| 321 | error: |
| 322 | rcu_read_unlock(); |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Fill lttng_event array of all kernel events in the channel. |
| 328 | */ |
| 329 | static int list_lttng_kernel_events(char *channel_name, |
| 330 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) |
| 331 | { |
| 332 | int i = 0, ret; |
| 333 | unsigned int nb_event; |
| 334 | struct ltt_kernel_event *event; |
| 335 | struct ltt_kernel_channel *kchan; |
| 336 | |
| 337 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); |
| 338 | if (kchan == NULL) { |
| 339 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 340 | goto error; |
| 341 | } |
| 342 | |
| 343 | nb_event = kchan->event_count; |
| 344 | |
| 345 | DBG("Listing events for channel %s", kchan->channel->name); |
| 346 | |
| 347 | if (nb_event == 0) { |
| 348 | goto end; |
| 349 | } |
| 350 | |
| 351 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); |
| 352 | if (*events == NULL) { |
| 353 | ret = LTTNG_ERR_FATAL; |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | /* Kernel channels */ |
| 358 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { |
| 359 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); |
| 360 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 361 | (*events)[i].enabled = event->enabled; |
| 362 | |
| 363 | switch (event->event->instrumentation) { |
| 364 | case LTTNG_KERNEL_TRACEPOINT: |
| 365 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; |
| 366 | break; |
| 367 | case LTTNG_KERNEL_KRETPROBE: |
| 368 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
| 369 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, |
| 370 | sizeof(struct lttng_kernel_kprobe)); |
| 371 | break; |
| 372 | case LTTNG_KERNEL_KPROBE: |
| 373 | (*events)[i].type = LTTNG_EVENT_PROBE; |
| 374 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, |
| 375 | sizeof(struct lttng_kernel_kprobe)); |
| 376 | break; |
| 377 | case LTTNG_KERNEL_FUNCTION: |
| 378 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
| 379 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, |
| 380 | sizeof(struct lttng_kernel_function)); |
| 381 | break; |
| 382 | case LTTNG_KERNEL_NOOP: |
| 383 | (*events)[i].type = LTTNG_EVENT_NOOP; |
| 384 | break; |
| 385 | case LTTNG_KERNEL_SYSCALL: |
| 386 | (*events)[i].type = LTTNG_EVENT_SYSCALL; |
| 387 | break; |
| 388 | case LTTNG_KERNEL_ALL: |
| 389 | assert(0); |
| 390 | break; |
| 391 | } |
| 392 | i++; |
| 393 | } |
| 394 | |
| 395 | end: |
| 396 | return nb_event; |
| 397 | |
| 398 | error: |
| 399 | /* Negate the error code to differentiate the size from an error */ |
| 400 | return -ret; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Add URI so the consumer output object. Set the correct path depending on the |
| 405 | * domain adding the default trace directory. |
| 406 | */ |
| 407 | static int add_uri_to_consumer(struct consumer_output *consumer, |
| 408 | struct lttng_uri *uri, int domain, const char *session_name) |
| 409 | { |
| 410 | int ret = LTTNG_OK; |
| 411 | const char *default_trace_dir; |
| 412 | |
| 413 | assert(uri); |
| 414 | |
| 415 | if (consumer == NULL) { |
| 416 | DBG("No consumer detected. Don't add URI. Stopping."); |
| 417 | ret = LTTNG_ERR_NO_CONSUMER; |
| 418 | goto error; |
| 419 | } |
| 420 | |
| 421 | switch (domain) { |
| 422 | case LTTNG_DOMAIN_KERNEL: |
| 423 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; |
| 424 | break; |
| 425 | case LTTNG_DOMAIN_UST: |
| 426 | default_trace_dir = DEFAULT_UST_TRACE_DIR; |
| 427 | break; |
| 428 | default: |
| 429 | /* |
| 430 | * This case is possible is we try to add the URI to the global tracing |
| 431 | * session consumer object which in this case there is no subdir. |
| 432 | */ |
| 433 | default_trace_dir = ""; |
| 434 | } |
| 435 | |
| 436 | switch (uri->dtype) { |
| 437 | case LTTNG_DST_IPV4: |
| 438 | case LTTNG_DST_IPV6: |
| 439 | DBG2("Setting network URI to consumer"); |
| 440 | |
| 441 | if (consumer->type == CONSUMER_DST_NET) { |
| 442 | if ((uri->stype == LTTNG_STREAM_CONTROL && |
| 443 | consumer->dst.net.control_isset) || |
| 444 | (uri->stype == LTTNG_STREAM_DATA && |
| 445 | consumer->dst.net.data_isset)) { |
| 446 | ret = LTTNG_ERR_URL_EXIST; |
| 447 | goto error; |
| 448 | } |
| 449 | } else { |
| 450 | memset(&consumer->dst.net, 0, sizeof(consumer->dst.net)); |
| 451 | } |
| 452 | |
| 453 | consumer->type = CONSUMER_DST_NET; |
| 454 | |
| 455 | /* Set URI into consumer output object */ |
| 456 | ret = consumer_set_network_uri(consumer, uri); |
| 457 | if (ret < 0) { |
| 458 | ret = -ret; |
| 459 | goto error; |
| 460 | } else if (ret == 1) { |
| 461 | /* |
| 462 | * URI was the same in the consumer so we do not append the subdir |
| 463 | * again so to not duplicate output dir. |
| 464 | */ |
| 465 | ret = LTTNG_OK; |
| 466 | goto error; |
| 467 | } |
| 468 | |
| 469 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { |
| 470 | ret = consumer_set_subdir(consumer, session_name); |
| 471 | if (ret < 0) { |
| 472 | ret = LTTNG_ERR_FATAL; |
| 473 | goto error; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 478 | /* On a new subdir, reappend the default trace dir. */ |
| 479 | strncat(consumer->subdir, default_trace_dir, |
| 480 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); |
| 481 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
| 482 | } |
| 483 | |
| 484 | break; |
| 485 | case LTTNG_DST_PATH: |
| 486 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); |
| 487 | memset(consumer->dst.trace_path, 0, |
| 488 | sizeof(consumer->dst.trace_path)); |
| 489 | strncpy(consumer->dst.trace_path, uri->dst.path, |
| 490 | sizeof(consumer->dst.trace_path)); |
| 491 | /* Append default trace dir */ |
| 492 | strncat(consumer->dst.trace_path, default_trace_dir, |
| 493 | sizeof(consumer->dst.trace_path) - |
| 494 | strlen(consumer->dst.trace_path) - 1); |
| 495 | /* Flag consumer as local. */ |
| 496 | consumer->type = CONSUMER_DST_LOCAL; |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | ret = LTTNG_OK; |
| 501 | |
| 502 | error: |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Init tracing by creating trace directory and sending fds kernel consumer. |
| 508 | */ |
| 509 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
| 510 | { |
| 511 | int ret = 0; |
| 512 | struct lttng_ht_iter iter; |
| 513 | struct consumer_socket *socket; |
| 514 | |
| 515 | assert(session); |
| 516 | |
| 517 | rcu_read_lock(); |
| 518 | |
| 519 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
| 520 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, |
| 521 | socket, node.node) { |
| 522 | pthread_mutex_lock(socket->lock); |
| 523 | ret = kernel_consumer_send_session(socket, session); |
| 524 | pthread_mutex_unlock(socket->lock); |
| 525 | if (ret < 0) { |
| 526 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 527 | goto error; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | error: |
| 533 | rcu_read_unlock(); |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Create a socket to the relayd using the URI. |
| 539 | * |
| 540 | * On success, the relayd_sock pointer is set to the created socket. |
| 541 | * Else, it's stays untouched and a lttcomm error code is returned. |
| 542 | */ |
| 543 | static int create_connect_relayd(struct lttng_uri *uri, |
| 544 | struct lttcomm_relayd_sock **relayd_sock) |
| 545 | { |
| 546 | int ret; |
| 547 | struct lttcomm_relayd_sock *rsock; |
| 548 | |
| 549 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
| 550 | RELAYD_VERSION_COMM_MINOR); |
| 551 | if (!rsock) { |
| 552 | ret = LTTNG_ERR_FATAL; |
| 553 | goto error; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Connect to relayd so we can proceed with a session creation. This call |
| 558 | * can possibly block for an arbitrary amount of time to set the health |
| 559 | * state to be in poll execution. |
| 560 | */ |
| 561 | health_poll_entry(); |
| 562 | ret = relayd_connect(rsock); |
| 563 | health_poll_exit(); |
| 564 | if (ret < 0) { |
| 565 | ERR("Unable to reach lttng-relayd"); |
| 566 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 567 | goto free_sock; |
| 568 | } |
| 569 | |
| 570 | /* Create socket for control stream. */ |
| 571 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 572 | DBG3("Creating relayd stream socket from URI"); |
| 573 | |
| 574 | /* Check relayd version */ |
| 575 | ret = relayd_version_check(rsock); |
| 576 | if (ret < 0) { |
| 577 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 578 | goto close_sock; |
| 579 | } |
| 580 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
| 581 | DBG3("Creating relayd data socket from URI"); |
| 582 | } else { |
| 583 | /* Command is not valid */ |
| 584 | ERR("Relayd invalid stream type: %d", uri->stype); |
| 585 | ret = LTTNG_ERR_INVALID; |
| 586 | goto close_sock; |
| 587 | } |
| 588 | |
| 589 | *relayd_sock = rsock; |
| 590 | |
| 591 | return LTTNG_OK; |
| 592 | |
| 593 | close_sock: |
| 594 | /* The returned value is not useful since we are on an error path. */ |
| 595 | (void) relayd_close(rsock); |
| 596 | free_sock: |
| 597 | free(rsock); |
| 598 | error: |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Connect to the relayd using URI and send the socket to the right consumer. |
| 604 | */ |
| 605 | static int send_consumer_relayd_socket(int domain, unsigned int session_id, |
| 606 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, |
| 607 | struct consumer_socket *consumer_sock, |
| 608 | char *session_name, char *hostname, int session_live_timer) |
| 609 | { |
| 610 | int ret; |
| 611 | struct lttcomm_relayd_sock *rsock = NULL; |
| 612 | |
| 613 | /* Connect to relayd and make version check if uri is the control. */ |
| 614 | ret = create_connect_relayd(relayd_uri, &rsock); |
| 615 | if (ret != LTTNG_OK) { |
| 616 | goto error; |
| 617 | } |
| 618 | assert(rsock); |
| 619 | |
| 620 | /* Set the network sequence index if not set. */ |
| 621 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
| 622 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 623 | /* |
| 624 | * Increment net_seq_idx because we are about to transfer the |
| 625 | * new relayd socket to the consumer. |
| 626 | * Assign unique key so the consumer can match streams. |
| 627 | */ |
| 628 | consumer->net_seq_index = ++relayd_net_seq_idx; |
| 629 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 630 | } |
| 631 | |
| 632 | /* Send relayd socket to consumer. */ |
| 633 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
| 634 | relayd_uri->stype, session_id, |
| 635 | session_name, hostname, session_live_timer); |
| 636 | if (ret < 0) { |
| 637 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
| 638 | goto close_sock; |
| 639 | } |
| 640 | |
| 641 | /* Flag that the corresponding socket was sent. */ |
| 642 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { |
| 643 | consumer_sock->control_sock_sent = 1; |
| 644 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
| 645 | consumer_sock->data_sock_sent = 1; |
| 646 | } |
| 647 | |
| 648 | ret = LTTNG_OK; |
| 649 | |
| 650 | /* |
| 651 | * Close socket which was dup on the consumer side. The session daemon does |
| 652 | * NOT keep track of the relayd socket(s) once transfer to the consumer. |
| 653 | */ |
| 654 | |
| 655 | close_sock: |
| 656 | (void) relayd_close(rsock); |
| 657 | free(rsock); |
| 658 | |
| 659 | error: |
| 660 | if (ret != LTTNG_OK) { |
| 661 | /* |
| 662 | * The consumer output for this session should not be used anymore |
| 663 | * since the relayd connection failed thus making any tracing or/and |
| 664 | * streaming not usable. |
| 665 | */ |
| 666 | consumer->enabled = 0; |
| 667 | } |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Send both relayd sockets to a specific consumer and domain. This is a |
| 673 | * helper function to facilitate sending the information to the consumer for a |
| 674 | * session. |
| 675 | */ |
| 676 | static int send_consumer_relayd_sockets(int domain, unsigned int session_id, |
| 677 | struct consumer_output *consumer, struct consumer_socket *sock, |
| 678 | char *session_name, char *hostname, int session_live_timer) |
| 679 | { |
| 680 | int ret = LTTNG_OK; |
| 681 | |
| 682 | assert(consumer); |
| 683 | assert(sock); |
| 684 | |
| 685 | /* Sending control relayd socket. */ |
| 686 | if (!sock->control_sock_sent) { |
| 687 | ret = send_consumer_relayd_socket(domain, session_id, |
| 688 | &consumer->dst.net.control, consumer, sock, |
| 689 | session_name, hostname, session_live_timer); |
| 690 | if (ret != LTTNG_OK) { |
| 691 | goto error; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* Sending data relayd socket. */ |
| 696 | if (!sock->data_sock_sent) { |
| 697 | ret = send_consumer_relayd_socket(domain, session_id, |
| 698 | &consumer->dst.net.data, consumer, sock, |
| 699 | session_name, hostname, session_live_timer); |
| 700 | if (ret != LTTNG_OK) { |
| 701 | goto error; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | error: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Setup relayd connections for a tracing session. First creates the socket to |
| 711 | * the relayd and send them to the right domain consumer. Consumer type MUST be |
| 712 | * network. |
| 713 | */ |
| 714 | int cmd_setup_relayd(struct ltt_session *session) |
| 715 | { |
| 716 | int ret = LTTNG_OK; |
| 717 | struct ltt_ust_session *usess; |
| 718 | struct ltt_kernel_session *ksess; |
| 719 | struct consumer_socket *socket; |
| 720 | struct lttng_ht_iter iter; |
| 721 | |
| 722 | assert(session); |
| 723 | |
| 724 | usess = session->ust_session; |
| 725 | ksess = session->kernel_session; |
| 726 | |
| 727 | DBG("Setting relayd for session %s", session->name); |
| 728 | |
| 729 | rcu_read_lock(); |
| 730 | |
| 731 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
| 732 | && usess->consumer->enabled) { |
| 733 | /* For each consumer socket, send relayd sockets */ |
| 734 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, |
| 735 | socket, node.node) { |
| 736 | pthread_mutex_lock(socket->lock); |
| 737 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
| 738 | usess->consumer, socket, |
| 739 | session->name, session->hostname, |
| 740 | session->live_timer); |
| 741 | pthread_mutex_unlock(socket->lock); |
| 742 | if (ret != LTTNG_OK) { |
| 743 | goto error; |
| 744 | } |
| 745 | /* Session is now ready for network streaming. */ |
| 746 | session->net_handle = 1; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET |
| 751 | && ksess->consumer->enabled) { |
| 752 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
| 753 | socket, node.node) { |
| 754 | pthread_mutex_lock(socket->lock); |
| 755 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
| 756 | ksess->consumer, socket, |
| 757 | session->name, session->hostname, |
| 758 | session->live_timer); |
| 759 | pthread_mutex_unlock(socket->lock); |
| 760 | if (ret != LTTNG_OK) { |
| 761 | goto error; |
| 762 | } |
| 763 | /* Session is now ready for network streaming. */ |
| 764 | session->net_handle = 1; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | error: |
| 769 | rcu_read_unlock(); |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Start a kernel session by opening all necessary streams. |
| 775 | */ |
| 776 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) |
| 777 | { |
| 778 | int ret; |
| 779 | struct ltt_kernel_channel *kchan; |
| 780 | |
| 781 | /* Open kernel metadata */ |
| 782 | if (ksess->metadata == NULL && ksess->output_traces) { |
| 783 | ret = kernel_open_metadata(ksess); |
| 784 | if (ret < 0) { |
| 785 | ret = LTTNG_ERR_KERN_META_FAIL; |
| 786 | goto error; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* Open kernel metadata stream */ |
| 791 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
| 792 | ret = kernel_open_metadata_stream(ksess); |
| 793 | if (ret < 0) { |
| 794 | ERR("Kernel create metadata stream failed"); |
| 795 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 796 | goto error; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* For each channel */ |
| 801 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { |
| 802 | if (kchan->stream_count == 0) { |
| 803 | ret = kernel_open_channel_stream(kchan); |
| 804 | if (ret < 0) { |
| 805 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 806 | goto error; |
| 807 | } |
| 808 | /* Update the stream global counter */ |
| 809 | ksess->stream_count_global += ret; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* Setup kernel consumer socket and send fds to it */ |
| 814 | ret = init_kernel_tracing(ksess); |
| 815 | if (ret != 0) { |
| 816 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | /* This start the kernel tracing */ |
| 821 | ret = kernel_start_session(ksess); |
| 822 | if (ret < 0) { |
| 823 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 824 | goto error; |
| 825 | } |
| 826 | |
| 827 | /* Quiescent wait after starting trace */ |
| 828 | kernel_wait_quiescent(kernel_tracer_fd); |
| 829 | |
| 830 | ksess->active = 1; |
| 831 | |
| 832 | ret = LTTNG_OK; |
| 833 | |
| 834 | error: |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
| 840 | */ |
| 841 | int cmd_disable_channel(struct ltt_session *session, int domain, |
| 842 | char *channel_name) |
| 843 | { |
| 844 | int ret; |
| 845 | struct ltt_ust_session *usess; |
| 846 | |
| 847 | usess = session->ust_session; |
| 848 | |
| 849 | rcu_read_lock(); |
| 850 | |
| 851 | switch (domain) { |
| 852 | case LTTNG_DOMAIN_KERNEL: |
| 853 | { |
| 854 | ret = channel_kernel_disable(session->kernel_session, |
| 855 | channel_name); |
| 856 | if (ret != LTTNG_OK) { |
| 857 | goto error; |
| 858 | } |
| 859 | |
| 860 | kernel_wait_quiescent(kernel_tracer_fd); |
| 861 | break; |
| 862 | } |
| 863 | case LTTNG_DOMAIN_UST: |
| 864 | { |
| 865 | struct ltt_ust_channel *uchan; |
| 866 | struct lttng_ht *chan_ht; |
| 867 | |
| 868 | chan_ht = usess->domain_global.channels; |
| 869 | |
| 870 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 871 | if (uchan == NULL) { |
| 872 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 873 | goto error; |
| 874 | } |
| 875 | |
| 876 | ret = channel_ust_disable(usess, uchan); |
| 877 | if (ret != LTTNG_OK) { |
| 878 | goto error; |
| 879 | } |
| 880 | break; |
| 881 | } |
| 882 | #if 0 |
| 883 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 884 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 885 | case LTTNG_DOMAIN_UST_PID: |
| 886 | #endif |
| 887 | default: |
| 888 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 889 | goto error; |
| 890 | } |
| 891 | |
| 892 | ret = LTTNG_OK; |
| 893 | |
| 894 | error: |
| 895 | rcu_read_unlock(); |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. |
| 901 | * |
| 902 | * The wpipe arguments is used as a notifier for the kernel thread. |
| 903 | */ |
| 904 | int cmd_enable_channel(struct ltt_session *session, |
| 905 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
| 906 | { |
| 907 | int ret; |
| 908 | struct ltt_ust_session *usess = session->ust_session; |
| 909 | struct lttng_ht *chan_ht; |
| 910 | |
| 911 | assert(session); |
| 912 | assert(attr); |
| 913 | assert(domain); |
| 914 | |
| 915 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
| 916 | |
| 917 | rcu_read_lock(); |
| 918 | |
| 919 | /* |
| 920 | * Don't try to enable a channel if the session has been started at |
| 921 | * some point in time before. The tracer does not allow it. |
| 922 | */ |
| 923 | if (session->has_been_started) { |
| 924 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 925 | goto error; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * If the session is a live session, remove the switch timer, the |
| 930 | * live timer does the same thing but sends also synchronisation |
| 931 | * beacons for inactive streams. |
| 932 | */ |
| 933 | if (session->live_timer > 0) { |
| 934 | attr->attr.live_timer_interval = session->live_timer; |
| 935 | attr->attr.switch_timer_interval = 0; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * The ringbuffer (both in user space and kernel) behave badly in overwrite |
| 940 | * mode and with less than 2 subbuf so block it right away and send back an |
| 941 | * invalid attribute error. |
| 942 | */ |
| 943 | if (attr->attr.overwrite && attr->attr.num_subbuf < 2) { |
| 944 | ret = LTTNG_ERR_INVALID; |
| 945 | goto error; |
| 946 | } |
| 947 | |
| 948 | switch (domain->type) { |
| 949 | case LTTNG_DOMAIN_KERNEL: |
| 950 | { |
| 951 | struct ltt_kernel_channel *kchan; |
| 952 | |
| 953 | kchan = trace_kernel_get_channel_by_name(attr->name, |
| 954 | session->kernel_session); |
| 955 | if (kchan == NULL) { |
| 956 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); |
| 957 | if (attr->name[0] != '\0') { |
| 958 | session->kernel_session->has_non_default_channel = 1; |
| 959 | } |
| 960 | } else { |
| 961 | ret = channel_kernel_enable(session->kernel_session, kchan); |
| 962 | } |
| 963 | |
| 964 | if (ret != LTTNG_OK) { |
| 965 | goto error; |
| 966 | } |
| 967 | |
| 968 | kernel_wait_quiescent(kernel_tracer_fd); |
| 969 | break; |
| 970 | } |
| 971 | case LTTNG_DOMAIN_UST: |
| 972 | { |
| 973 | struct ltt_ust_channel *uchan; |
| 974 | |
| 975 | chan_ht = usess->domain_global.channels; |
| 976 | |
| 977 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
| 978 | if (uchan == NULL) { |
| 979 | ret = channel_ust_create(usess, attr, domain->buf_type); |
| 980 | if (attr->name[0] != '\0') { |
| 981 | usess->has_non_default_channel = 1; |
| 982 | } |
| 983 | } else { |
| 984 | ret = channel_ust_enable(usess, uchan); |
| 985 | } |
| 986 | break; |
| 987 | } |
| 988 | default: |
| 989 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 990 | goto error; |
| 991 | } |
| 992 | |
| 993 | error: |
| 994 | rcu_read_unlock(); |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | /* |
| 1000 | * Command LTTNG_DISABLE_EVENT processed by the client thread. |
| 1001 | */ |
| 1002 | int cmd_disable_event(struct ltt_session *session, int domain, |
| 1003 | char *channel_name, char *event_name) |
| 1004 | { |
| 1005 | int ret; |
| 1006 | |
| 1007 | rcu_read_lock(); |
| 1008 | |
| 1009 | switch (domain) { |
| 1010 | case LTTNG_DOMAIN_KERNEL: |
| 1011 | { |
| 1012 | struct ltt_kernel_channel *kchan; |
| 1013 | struct ltt_kernel_session *ksess; |
| 1014 | |
| 1015 | ksess = session->kernel_session; |
| 1016 | |
| 1017 | /* |
| 1018 | * If a non-default channel has been created in the |
| 1019 | * session, explicitely require that -c chan_name needs |
| 1020 | * to be provided. |
| 1021 | */ |
| 1022 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { |
| 1023 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1024 | goto error; |
| 1025 | } |
| 1026 | |
| 1027 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 1028 | if (kchan == NULL) { |
| 1029 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 1030 | goto error; |
| 1031 | } |
| 1032 | |
| 1033 | ret = event_kernel_disable_tracepoint(kchan, event_name); |
| 1034 | if (ret != LTTNG_OK) { |
| 1035 | goto error; |
| 1036 | } |
| 1037 | |
| 1038 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1039 | break; |
| 1040 | } |
| 1041 | case LTTNG_DOMAIN_UST: |
| 1042 | { |
| 1043 | struct ltt_ust_channel *uchan; |
| 1044 | struct ltt_ust_session *usess; |
| 1045 | |
| 1046 | usess = session->ust_session; |
| 1047 | |
| 1048 | /* |
| 1049 | * If a non-default channel has been created in the |
| 1050 | * session, explicitely require that -c chan_name needs |
| 1051 | * to be provided. |
| 1052 | */ |
| 1053 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1054 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1055 | goto error; |
| 1056 | } |
| 1057 | |
| 1058 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 1059 | channel_name); |
| 1060 | if (uchan == NULL) { |
| 1061 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1062 | goto error; |
| 1063 | } |
| 1064 | |
| 1065 | ret = event_ust_disable_tracepoint(usess, uchan, event_name); |
| 1066 | if (ret != LTTNG_OK) { |
| 1067 | goto error; |
| 1068 | } |
| 1069 | |
| 1070 | DBG3("Disable UST event %s in channel %s completed", event_name, |
| 1071 | channel_name); |
| 1072 | break; |
| 1073 | } |
| 1074 | case LTTNG_DOMAIN_JUL: |
| 1075 | { |
| 1076 | struct ltt_ust_session *usess = session->ust_session; |
| 1077 | |
| 1078 | assert(usess); |
| 1079 | |
| 1080 | ret = event_jul_disable(usess, event_name); |
| 1081 | if (ret != LTTNG_OK) { |
| 1082 | goto error; |
| 1083 | } |
| 1084 | |
| 1085 | break; |
| 1086 | } |
| 1087 | #if 0 |
| 1088 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 1089 | case LTTNG_DOMAIN_UST_PID: |
| 1090 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 1091 | #endif |
| 1092 | default: |
| 1093 | ret = LTTNG_ERR_UND; |
| 1094 | goto error; |
| 1095 | } |
| 1096 | |
| 1097 | ret = LTTNG_OK; |
| 1098 | |
| 1099 | error: |
| 1100 | rcu_read_unlock(); |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. |
| 1106 | */ |
| 1107 | int cmd_disable_event_all(struct ltt_session *session, int domain, |
| 1108 | char *channel_name) |
| 1109 | { |
| 1110 | int ret; |
| 1111 | |
| 1112 | rcu_read_lock(); |
| 1113 | |
| 1114 | switch (domain) { |
| 1115 | case LTTNG_DOMAIN_KERNEL: |
| 1116 | { |
| 1117 | struct ltt_kernel_session *ksess; |
| 1118 | struct ltt_kernel_channel *kchan; |
| 1119 | |
| 1120 | ksess = session->kernel_session; |
| 1121 | |
| 1122 | /* |
| 1123 | * If a non-default channel has been created in the |
| 1124 | * session, explicitely require that -c chan_name needs |
| 1125 | * to be provided. |
| 1126 | */ |
| 1127 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { |
| 1128 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1129 | goto error; |
| 1130 | } |
| 1131 | |
| 1132 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 1133 | if (kchan == NULL) { |
| 1134 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 1135 | goto error; |
| 1136 | } |
| 1137 | |
| 1138 | ret = event_kernel_disable_all(kchan); |
| 1139 | if (ret != LTTNG_OK) { |
| 1140 | goto error; |
| 1141 | } |
| 1142 | |
| 1143 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1144 | break; |
| 1145 | } |
| 1146 | case LTTNG_DOMAIN_UST: |
| 1147 | { |
| 1148 | struct ltt_ust_session *usess; |
| 1149 | struct ltt_ust_channel *uchan; |
| 1150 | |
| 1151 | usess = session->ust_session; |
| 1152 | |
| 1153 | /* |
| 1154 | * If a non-default channel has been created in the |
| 1155 | * session, explicitely require that -c chan_name needs |
| 1156 | * to be provided. |
| 1157 | */ |
| 1158 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1159 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1160 | goto error; |
| 1161 | } |
| 1162 | |
| 1163 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 1164 | channel_name); |
| 1165 | if (uchan == NULL) { |
| 1166 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1167 | goto error; |
| 1168 | } |
| 1169 | |
| 1170 | ret = event_ust_disable_all_tracepoints(usess, uchan); |
| 1171 | if (ret != 0) { |
| 1172 | goto error; |
| 1173 | } |
| 1174 | |
| 1175 | DBG3("Disable all UST events in channel %s completed", channel_name); |
| 1176 | |
| 1177 | break; |
| 1178 | } |
| 1179 | case LTTNG_DOMAIN_JUL: |
| 1180 | { |
| 1181 | struct ltt_ust_session *usess = session->ust_session; |
| 1182 | |
| 1183 | assert(usess); |
| 1184 | |
| 1185 | ret = event_jul_disable_all(usess); |
| 1186 | if (ret != LTTNG_OK) { |
| 1187 | goto error; |
| 1188 | } |
| 1189 | |
| 1190 | break; |
| 1191 | } |
| 1192 | #if 0 |
| 1193 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 1194 | case LTTNG_DOMAIN_UST_PID: |
| 1195 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 1196 | #endif |
| 1197 | default: |
| 1198 | ret = LTTNG_ERR_UND; |
| 1199 | goto error; |
| 1200 | } |
| 1201 | |
| 1202 | ret = LTTNG_OK; |
| 1203 | |
| 1204 | error: |
| 1205 | rcu_read_unlock(); |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Command LTTNG_ADD_CONTEXT processed by the client thread. |
| 1211 | */ |
| 1212 | int cmd_add_context(struct ltt_session *session, int domain, |
| 1213 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
| 1214 | { |
| 1215 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
| 1216 | |
| 1217 | switch (domain) { |
| 1218 | case LTTNG_DOMAIN_KERNEL: |
| 1219 | assert(session->kernel_session); |
| 1220 | |
| 1221 | if (session->kernel_session->channel_count == 0) { |
| 1222 | /* Create default channel */ |
| 1223 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); |
| 1224 | if (ret != LTTNG_OK) { |
| 1225 | goto error; |
| 1226 | } |
| 1227 | chan_kern_created = 1; |
| 1228 | } |
| 1229 | /* Add kernel context to kernel tracer */ |
| 1230 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
| 1231 | if (ret != LTTNG_OK) { |
| 1232 | goto error; |
| 1233 | } |
| 1234 | break; |
| 1235 | case LTTNG_DOMAIN_UST: |
| 1236 | { |
| 1237 | struct ltt_ust_session *usess = session->ust_session; |
| 1238 | unsigned int chan_count; |
| 1239 | |
| 1240 | assert(usess); |
| 1241 | |
| 1242 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
| 1243 | if (chan_count == 0) { |
| 1244 | struct lttng_channel *attr; |
| 1245 | /* Create default channel */ |
| 1246 | attr = channel_new_default_attr(domain, usess->buffer_type); |
| 1247 | if (attr == NULL) { |
| 1248 | ret = LTTNG_ERR_FATAL; |
| 1249 | goto error; |
| 1250 | } |
| 1251 | |
| 1252 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
| 1253 | if (ret != LTTNG_OK) { |
| 1254 | free(attr); |
| 1255 | goto error; |
| 1256 | } |
| 1257 | free(attr); |
| 1258 | chan_ust_created = 1; |
| 1259 | } |
| 1260 | |
| 1261 | ret = context_ust_add(usess, domain, ctx, channel_name); |
| 1262 | if (ret != LTTNG_OK) { |
| 1263 | goto error; |
| 1264 | } |
| 1265 | break; |
| 1266 | } |
| 1267 | #if 0 |
| 1268 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 1269 | case LTTNG_DOMAIN_UST_PID: |
| 1270 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 1271 | #endif |
| 1272 | default: |
| 1273 | ret = LTTNG_ERR_UND; |
| 1274 | goto error; |
| 1275 | } |
| 1276 | |
| 1277 | return LTTNG_OK; |
| 1278 | |
| 1279 | error: |
| 1280 | if (chan_kern_created) { |
| 1281 | struct ltt_kernel_channel *kchan = |
| 1282 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, |
| 1283 | session->kernel_session); |
| 1284 | /* Created previously, this should NOT fail. */ |
| 1285 | assert(kchan); |
| 1286 | kernel_destroy_channel(kchan); |
| 1287 | } |
| 1288 | |
| 1289 | if (chan_ust_created) { |
| 1290 | struct ltt_ust_channel *uchan = |
| 1291 | trace_ust_find_channel_by_name( |
| 1292 | session->ust_session->domain_global.channels, |
| 1293 | DEFAULT_CHANNEL_NAME); |
| 1294 | /* Created previously, this should NOT fail. */ |
| 1295 | assert(uchan); |
| 1296 | /* Remove from the channel list of the session. */ |
| 1297 | trace_ust_delete_channel(session->ust_session->domain_global.channels, |
| 1298 | uchan); |
| 1299 | trace_ust_destroy_channel(uchan); |
| 1300 | } |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Command LTTNG_ENABLE_EVENT processed by the client thread. |
| 1306 | */ |
| 1307 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, |
| 1308 | char *channel_name, struct lttng_event *event, |
| 1309 | char *filter_expression, |
| 1310 | struct lttng_filter_bytecode *filter, |
| 1311 | struct lttng_event_exclusion *exclusion, |
| 1312 | int wpipe) |
| 1313 | { |
| 1314 | int ret, channel_created = 0; |
| 1315 | struct lttng_channel *attr; |
| 1316 | |
| 1317 | assert(session); |
| 1318 | assert(event); |
| 1319 | assert(channel_name); |
| 1320 | |
| 1321 | rcu_read_lock(); |
| 1322 | |
| 1323 | switch (domain->type) { |
| 1324 | case LTTNG_DOMAIN_KERNEL: |
| 1325 | { |
| 1326 | struct ltt_kernel_channel *kchan; |
| 1327 | |
| 1328 | /* |
| 1329 | * If a non-default channel has been created in the |
| 1330 | * session, explicitely require that -c chan_name needs |
| 1331 | * to be provided. |
| 1332 | */ |
| 1333 | if (session->kernel_session->has_non_default_channel |
| 1334 | && channel_name[0] == '\0') { |
| 1335 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1336 | goto error; |
| 1337 | } |
| 1338 | |
| 1339 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 1340 | session->kernel_session); |
| 1341 | if (kchan == NULL) { |
| 1342 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
| 1343 | LTTNG_BUFFER_GLOBAL); |
| 1344 | if (attr == NULL) { |
| 1345 | ret = LTTNG_ERR_FATAL; |
| 1346 | goto error; |
| 1347 | } |
| 1348 | strncpy(attr->name, channel_name, sizeof(attr->name)); |
| 1349 | |
| 1350 | ret = cmd_enable_channel(session, domain, attr, wpipe); |
| 1351 | if (ret != LTTNG_OK) { |
| 1352 | free(attr); |
| 1353 | goto error; |
| 1354 | } |
| 1355 | free(attr); |
| 1356 | |
| 1357 | channel_created = 1; |
| 1358 | } |
| 1359 | |
| 1360 | /* Get the newly created kernel channel pointer */ |
| 1361 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 1362 | session->kernel_session); |
| 1363 | if (kchan == NULL) { |
| 1364 | /* This sould not happen... */ |
| 1365 | ret = LTTNG_ERR_FATAL; |
| 1366 | goto error; |
| 1367 | } |
| 1368 | |
| 1369 | ret = event_kernel_enable_tracepoint(kchan, event); |
| 1370 | if (ret != LTTNG_OK) { |
| 1371 | if (channel_created) { |
| 1372 | /* Let's not leak a useless channel. */ |
| 1373 | kernel_destroy_channel(kchan); |
| 1374 | } |
| 1375 | goto error; |
| 1376 | } |
| 1377 | |
| 1378 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1379 | break; |
| 1380 | } |
| 1381 | case LTTNG_DOMAIN_UST: |
| 1382 | { |
| 1383 | struct ltt_ust_channel *uchan; |
| 1384 | struct ltt_ust_session *usess = session->ust_session; |
| 1385 | |
| 1386 | assert(usess); |
| 1387 | |
| 1388 | /* |
| 1389 | * If a non-default channel has been created in the |
| 1390 | * session, explicitely require that -c chan_name needs |
| 1391 | * to be provided. |
| 1392 | */ |
| 1393 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1394 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1395 | goto error; |
| 1396 | } |
| 1397 | |
| 1398 | /* Get channel from global UST domain */ |
| 1399 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 1400 | channel_name); |
| 1401 | if (uchan == NULL) { |
| 1402 | /* Create default channel */ |
| 1403 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
| 1404 | usess->buffer_type); |
| 1405 | if (attr == NULL) { |
| 1406 | ret = LTTNG_ERR_FATAL; |
| 1407 | goto error; |
| 1408 | } |
| 1409 | strncpy(attr->name, channel_name, sizeof(attr->name)); |
| 1410 | |
| 1411 | ret = cmd_enable_channel(session, domain, attr, wpipe); |
| 1412 | if (ret != LTTNG_OK) { |
| 1413 | free(attr); |
| 1414 | goto error; |
| 1415 | } |
| 1416 | free(attr); |
| 1417 | |
| 1418 | /* Get the newly created channel reference back */ |
| 1419 | uchan = trace_ust_find_channel_by_name( |
| 1420 | usess->domain_global.channels, channel_name); |
| 1421 | assert(uchan); |
| 1422 | } |
| 1423 | |
| 1424 | /* At this point, the session and channel exist on the tracer */ |
| 1425 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
| 1426 | filter_expression, filter, exclusion); |
| 1427 | if (ret != LTTNG_OK) { |
| 1428 | goto error; |
| 1429 | } |
| 1430 | break; |
| 1431 | } |
| 1432 | case LTTNG_DOMAIN_JUL: |
| 1433 | { |
| 1434 | struct lttng_event uevent; |
| 1435 | struct lttng_domain tmp_dom; |
| 1436 | struct ltt_ust_session *usess = session->ust_session; |
| 1437 | |
| 1438 | assert(usess); |
| 1439 | |
| 1440 | /* Create the default JUL tracepoint. */ |
| 1441 | memset(&uevent, 0, sizeof(uevent)); |
| 1442 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
| 1443 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 1444 | if (is_root) { |
| 1445 | strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME, |
| 1446 | sizeof(uevent.name)); |
| 1447 | } else { |
| 1448 | strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME, |
| 1449 | sizeof(uevent.name)); |
| 1450 | } |
| 1451 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
| 1452 | |
| 1453 | /* |
| 1454 | * The domain type is changed because we are about to enable the |
| 1455 | * default channel and event for the JUL domain that are hardcoded. |
| 1456 | * This happens in the UST domain. |
| 1457 | */ |
| 1458 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); |
| 1459 | tmp_dom.type = LTTNG_DOMAIN_UST; |
| 1460 | |
| 1461 | ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME, |
| 1462 | &uevent, filter_expression, filter, NULL, wpipe); |
| 1463 | if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) { |
| 1464 | goto error; |
| 1465 | } |
| 1466 | |
| 1467 | /* The wild card * means that everything should be enabled. */ |
| 1468 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { |
| 1469 | ret = event_jul_enable_all(usess, event, filter); |
| 1470 | } else { |
| 1471 | ret = event_jul_enable(usess, event, filter); |
| 1472 | } |
| 1473 | if (ret != LTTNG_OK) { |
| 1474 | goto error; |
| 1475 | } |
| 1476 | |
| 1477 | break; |
| 1478 | } |
| 1479 | #if 0 |
| 1480 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 1481 | case LTTNG_DOMAIN_UST_PID: |
| 1482 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 1483 | #endif |
| 1484 | default: |
| 1485 | ret = LTTNG_ERR_UND; |
| 1486 | goto error; |
| 1487 | } |
| 1488 | |
| 1489 | ret = LTTNG_OK; |
| 1490 | |
| 1491 | error: |
| 1492 | rcu_read_unlock(); |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. |
| 1498 | */ |
| 1499 | int cmd_enable_event_all(struct ltt_session *session, |
| 1500 | struct lttng_domain *domain, char *channel_name, int event_type, |
| 1501 | char *filter_expression, |
| 1502 | struct lttng_filter_bytecode *filter, int wpipe) |
| 1503 | { |
| 1504 | int ret; |
| 1505 | struct lttng_channel *attr; |
| 1506 | |
| 1507 | assert(session); |
| 1508 | assert(channel_name); |
| 1509 | |
| 1510 | rcu_read_lock(); |
| 1511 | |
| 1512 | switch (domain->type) { |
| 1513 | case LTTNG_DOMAIN_KERNEL: |
| 1514 | { |
| 1515 | struct ltt_kernel_channel *kchan; |
| 1516 | |
| 1517 | assert(session->kernel_session); |
| 1518 | |
| 1519 | /* |
| 1520 | * If a non-default channel has been created in the |
| 1521 | * session, explicitely require that -c chan_name needs |
| 1522 | * to be provided. |
| 1523 | */ |
| 1524 | if (session->kernel_session->has_non_default_channel |
| 1525 | && channel_name[0] == '\0') { |
| 1526 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1527 | goto error; |
| 1528 | } |
| 1529 | |
| 1530 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 1531 | session->kernel_session); |
| 1532 | if (kchan == NULL) { |
| 1533 | /* Create default channel */ |
| 1534 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
| 1535 | LTTNG_BUFFER_GLOBAL); |
| 1536 | if (attr == NULL) { |
| 1537 | ret = LTTNG_ERR_FATAL; |
| 1538 | goto error; |
| 1539 | } |
| 1540 | strncpy(attr->name, channel_name, sizeof(attr->name)); |
| 1541 | |
| 1542 | ret = cmd_enable_channel(session, domain, attr, wpipe); |
| 1543 | if (ret != LTTNG_OK) { |
| 1544 | free(attr); |
| 1545 | goto error; |
| 1546 | } |
| 1547 | free(attr); |
| 1548 | |
| 1549 | /* Get the newly created kernel channel pointer */ |
| 1550 | kchan = trace_kernel_get_channel_by_name(channel_name, |
| 1551 | session->kernel_session); |
| 1552 | assert(kchan); |
| 1553 | } |
| 1554 | |
| 1555 | switch (event_type) { |
| 1556 | case LTTNG_EVENT_SYSCALL: |
| 1557 | ret = event_kernel_enable_all_syscalls(kchan, kernel_tracer_fd); |
| 1558 | break; |
| 1559 | case LTTNG_EVENT_TRACEPOINT: |
| 1560 | /* |
| 1561 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
| 1562 | * events already registered to the channel. |
| 1563 | */ |
| 1564 | ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd); |
| 1565 | break; |
| 1566 | case LTTNG_EVENT_ALL: |
| 1567 | /* Enable syscalls and tracepoints */ |
| 1568 | ret = event_kernel_enable_all(kchan, kernel_tracer_fd); |
| 1569 | break; |
| 1570 | default: |
| 1571 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 1572 | goto error; |
| 1573 | } |
| 1574 | |
| 1575 | /* Manage return value */ |
| 1576 | if (ret != LTTNG_OK) { |
| 1577 | /* |
| 1578 | * On error, cmd_enable_channel call will take care of destroying |
| 1579 | * the created channel if it was needed. |
| 1580 | */ |
| 1581 | goto error; |
| 1582 | } |
| 1583 | |
| 1584 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1585 | break; |
| 1586 | } |
| 1587 | case LTTNG_DOMAIN_UST: |
| 1588 | { |
| 1589 | struct ltt_ust_channel *uchan; |
| 1590 | struct ltt_ust_session *usess = session->ust_session; |
| 1591 | |
| 1592 | assert(usess); |
| 1593 | |
| 1594 | /* |
| 1595 | * If a non-default channel has been created in the |
| 1596 | * session, explicitely require that -c chan_name needs |
| 1597 | * to be provided. |
| 1598 | */ |
| 1599 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1600 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1601 | goto error; |
| 1602 | } |
| 1603 | |
| 1604 | /* Get channel from global UST domain */ |
| 1605 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 1606 | channel_name); |
| 1607 | if (uchan == NULL) { |
| 1608 | /* Create default channel */ |
| 1609 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
| 1610 | usess->buffer_type); |
| 1611 | if (attr == NULL) { |
| 1612 | ret = LTTNG_ERR_FATAL; |
| 1613 | goto error; |
| 1614 | } |
| 1615 | strncpy(attr->name, channel_name, sizeof(attr->name)); |
| 1616 | |
| 1617 | ret = cmd_enable_channel(session, domain, attr, wpipe); |
| 1618 | if (ret != LTTNG_OK) { |
| 1619 | free(attr); |
| 1620 | goto error; |
| 1621 | } |
| 1622 | free(attr); |
| 1623 | |
| 1624 | /* Get the newly created channel reference back */ |
| 1625 | uchan = trace_ust_find_channel_by_name( |
| 1626 | usess->domain_global.channels, channel_name); |
| 1627 | assert(uchan); |
| 1628 | } |
| 1629 | |
| 1630 | /* At this point, the session and channel exist on the tracer */ |
| 1631 | |
| 1632 | switch (event_type) { |
| 1633 | case LTTNG_EVENT_ALL: |
| 1634 | case LTTNG_EVENT_TRACEPOINT: |
| 1635 | ret = event_ust_enable_all_tracepoints(usess, uchan, |
| 1636 | filter_expression, filter); |
| 1637 | if (ret != LTTNG_OK) { |
| 1638 | goto error; |
| 1639 | } |
| 1640 | break; |
| 1641 | default: |
| 1642 | ret = LTTNG_ERR_UST_ENABLE_FAIL; |
| 1643 | goto error; |
| 1644 | } |
| 1645 | |
| 1646 | /* Manage return value */ |
| 1647 | if (ret != LTTNG_OK) { |
| 1648 | goto error; |
| 1649 | } |
| 1650 | |
| 1651 | break; |
| 1652 | } |
| 1653 | case LTTNG_DOMAIN_JUL: |
| 1654 | { |
| 1655 | struct lttng_event uevent, event; |
| 1656 | struct lttng_domain tmp_dom; |
| 1657 | struct ltt_ust_session *usess = session->ust_session; |
| 1658 | |
| 1659 | assert(usess); |
| 1660 | |
| 1661 | /* Create the default JUL tracepoint. */ |
| 1662 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
| 1663 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 1664 | if (is_root) { |
| 1665 | strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME, |
| 1666 | sizeof(uevent.name)); |
| 1667 | } else { |
| 1668 | strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME, |
| 1669 | sizeof(uevent.name)); |
| 1670 | } |
| 1671 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
| 1672 | |
| 1673 | /* |
| 1674 | * The domain type is changed because we are about to enable the |
| 1675 | * default channel and event for the JUL domain that are hardcoded. |
| 1676 | * This happens in the UST domain. |
| 1677 | */ |
| 1678 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); |
| 1679 | tmp_dom.type = LTTNG_DOMAIN_UST; |
| 1680 | |
| 1681 | ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME, |
| 1682 | &uevent, NULL, NULL, NULL, wpipe); |
| 1683 | if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) { |
| 1684 | goto error; |
| 1685 | } |
| 1686 | |
| 1687 | event.loglevel = LTTNG_LOGLEVEL_JUL_ALL; |
| 1688 | event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 1689 | strncpy(event.name, "*", sizeof(event.name)); |
| 1690 | event.name[sizeof(event.name) - 1] = '\0'; |
| 1691 | |
| 1692 | ret = event_jul_enable_all(usess, &event, filter); |
| 1693 | if (ret != LTTNG_OK) { |
| 1694 | goto error; |
| 1695 | } |
| 1696 | |
| 1697 | break; |
| 1698 | } |
| 1699 | #if 0 |
| 1700 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 1701 | case LTTNG_DOMAIN_UST_PID: |
| 1702 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 1703 | #endif |
| 1704 | default: |
| 1705 | ret = LTTNG_ERR_UND; |
| 1706 | goto error; |
| 1707 | } |
| 1708 | |
| 1709 | ret = LTTNG_OK; |
| 1710 | |
| 1711 | error: |
| 1712 | rcu_read_unlock(); |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | /* |
| 1718 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. |
| 1719 | */ |
| 1720 | ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) |
| 1721 | { |
| 1722 | int ret; |
| 1723 | ssize_t nb_events = 0; |
| 1724 | |
| 1725 | switch (domain) { |
| 1726 | case LTTNG_DOMAIN_KERNEL: |
| 1727 | nb_events = kernel_list_events(kernel_tracer_fd, events); |
| 1728 | if (nb_events < 0) { |
| 1729 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
| 1730 | goto error; |
| 1731 | } |
| 1732 | break; |
| 1733 | case LTTNG_DOMAIN_UST: |
| 1734 | nb_events = ust_app_list_events(events); |
| 1735 | if (nb_events < 0) { |
| 1736 | ret = LTTNG_ERR_UST_LIST_FAIL; |
| 1737 | goto error; |
| 1738 | } |
| 1739 | break; |
| 1740 | case LTTNG_DOMAIN_JUL: |
| 1741 | nb_events = jul_list_events(events); |
| 1742 | if (nb_events < 0) { |
| 1743 | ret = LTTNG_ERR_UST_LIST_FAIL; |
| 1744 | goto error; |
| 1745 | } |
| 1746 | break; |
| 1747 | default: |
| 1748 | ret = LTTNG_ERR_UND; |
| 1749 | goto error; |
| 1750 | } |
| 1751 | |
| 1752 | return nb_events; |
| 1753 | |
| 1754 | error: |
| 1755 | /* Return negative value to differentiate return code */ |
| 1756 | return -ret; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. |
| 1761 | */ |
| 1762 | ssize_t cmd_list_tracepoint_fields(int domain, |
| 1763 | struct lttng_event_field **fields) |
| 1764 | { |
| 1765 | int ret; |
| 1766 | ssize_t nb_fields = 0; |
| 1767 | |
| 1768 | switch (domain) { |
| 1769 | case LTTNG_DOMAIN_UST: |
| 1770 | nb_fields = ust_app_list_event_fields(fields); |
| 1771 | if (nb_fields < 0) { |
| 1772 | ret = LTTNG_ERR_UST_LIST_FAIL; |
| 1773 | goto error; |
| 1774 | } |
| 1775 | break; |
| 1776 | case LTTNG_DOMAIN_KERNEL: |
| 1777 | default: /* fall-through */ |
| 1778 | ret = LTTNG_ERR_UND; |
| 1779 | goto error; |
| 1780 | } |
| 1781 | |
| 1782 | return nb_fields; |
| 1783 | |
| 1784 | error: |
| 1785 | /* Return negative value to differentiate return code */ |
| 1786 | return -ret; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Command LTTNG_START_TRACE processed by the client thread. |
| 1791 | */ |
| 1792 | int cmd_start_trace(struct ltt_session *session) |
| 1793 | { |
| 1794 | int ret; |
| 1795 | unsigned long nb_chan = 0; |
| 1796 | struct ltt_kernel_session *ksession; |
| 1797 | struct ltt_ust_session *usess; |
| 1798 | |
| 1799 | assert(session); |
| 1800 | |
| 1801 | /* Ease our life a bit ;) */ |
| 1802 | ksession = session->kernel_session; |
| 1803 | usess = session->ust_session; |
| 1804 | |
| 1805 | /* Is the session already started? */ |
| 1806 | if (session->active) { |
| 1807 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1808 | goto error; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * Starting a session without channel is useless since after that it's not |
| 1813 | * possible to enable channel thus inform the client. |
| 1814 | */ |
| 1815 | if (usess && usess->domain_global.channels) { |
| 1816 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); |
| 1817 | } |
| 1818 | if (ksession) { |
| 1819 | nb_chan += ksession->channel_count; |
| 1820 | } |
| 1821 | if (!nb_chan) { |
| 1822 | ret = LTTNG_ERR_NO_CHANNEL; |
| 1823 | goto error; |
| 1824 | } |
| 1825 | |
| 1826 | /* Kernel tracing */ |
| 1827 | if (ksession != NULL) { |
| 1828 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
| 1829 | if (ret != LTTNG_OK) { |
| 1830 | goto error; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | /* Flag session that trace should start automatically */ |
| 1835 | if (usess) { |
| 1836 | /* |
| 1837 | * Even though the start trace might fail, flag this session active so |
| 1838 | * other application coming in are started by default. |
| 1839 | */ |
| 1840 | usess->active = 1; |
| 1841 | |
| 1842 | ret = ust_app_start_trace_all(usess); |
| 1843 | if (ret < 0) { |
| 1844 | ret = LTTNG_ERR_UST_START_FAIL; |
| 1845 | goto error; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | /* Flag this after a successful start. */ |
| 1850 | session->has_been_started = 1; |
| 1851 | session->active = 1; |
| 1852 | |
| 1853 | ret = LTTNG_OK; |
| 1854 | |
| 1855 | error: |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * Command LTTNG_STOP_TRACE processed by the client thread. |
| 1861 | */ |
| 1862 | int cmd_stop_trace(struct ltt_session *session) |
| 1863 | { |
| 1864 | int ret; |
| 1865 | struct ltt_kernel_channel *kchan; |
| 1866 | struct ltt_kernel_session *ksession; |
| 1867 | struct ltt_ust_session *usess; |
| 1868 | |
| 1869 | assert(session); |
| 1870 | |
| 1871 | /* Short cut */ |
| 1872 | ksession = session->kernel_session; |
| 1873 | usess = session->ust_session; |
| 1874 | |
| 1875 | /* Session is not active. Skip everythong and inform the client. */ |
| 1876 | if (!session->active) { |
| 1877 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
| 1878 | goto error; |
| 1879 | } |
| 1880 | |
| 1881 | /* Kernel tracer */ |
| 1882 | if (ksession && ksession->active) { |
| 1883 | DBG("Stop kernel tracing"); |
| 1884 | |
| 1885 | /* Flush metadata if exist */ |
| 1886 | if (ksession->metadata_stream_fd >= 0) { |
| 1887 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); |
| 1888 | if (ret < 0) { |
| 1889 | ERR("Kernel metadata flush failed"); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* Flush all buffers before stopping */ |
| 1894 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 1895 | ret = kernel_flush_buffer(kchan); |
| 1896 | if (ret < 0) { |
| 1897 | ERR("Kernel flush buffer error"); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | ret = kernel_stop_session(ksession); |
| 1902 | if (ret < 0) { |
| 1903 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
| 1904 | goto error; |
| 1905 | } |
| 1906 | |
| 1907 | kernel_wait_quiescent(kernel_tracer_fd); |
| 1908 | |
| 1909 | ksession->active = 0; |
| 1910 | } |
| 1911 | |
| 1912 | if (usess && usess->active) { |
| 1913 | /* |
| 1914 | * Even though the stop trace might fail, flag this session inactive so |
| 1915 | * other application coming in are not started by default. |
| 1916 | */ |
| 1917 | usess->active = 0; |
| 1918 | |
| 1919 | ret = ust_app_stop_trace_all(usess); |
| 1920 | if (ret < 0) { |
| 1921 | ret = LTTNG_ERR_UST_STOP_FAIL; |
| 1922 | goto error; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | /* Flag inactive after a successful stop. */ |
| 1927 | session->active = 0; |
| 1928 | ret = LTTNG_OK; |
| 1929 | |
| 1930 | error: |
| 1931 | return ret; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. |
| 1936 | */ |
| 1937 | int cmd_set_consumer_uri(int domain, struct ltt_session *session, |
| 1938 | size_t nb_uri, struct lttng_uri *uris) |
| 1939 | { |
| 1940 | int ret, i; |
| 1941 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 1942 | struct ltt_ust_session *usess = session->ust_session; |
| 1943 | struct consumer_output *consumer = NULL; |
| 1944 | |
| 1945 | assert(session); |
| 1946 | assert(uris); |
| 1947 | assert(nb_uri > 0); |
| 1948 | |
| 1949 | /* Can't set consumer URI if the session is active. */ |
| 1950 | if (session->active) { |
| 1951 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1952 | goto error; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * This case switch makes sure the domain session has a temporary consumer |
| 1957 | * so the URL can be set. |
| 1958 | */ |
| 1959 | switch (domain) { |
| 1960 | case 0: |
| 1961 | /* Code flow error. A session MUST always have a consumer object */ |
| 1962 | assert(session->consumer); |
| 1963 | /* |
| 1964 | * The URL will be added to the tracing session consumer instead of a |
| 1965 | * specific domain consumer. |
| 1966 | */ |
| 1967 | consumer = session->consumer; |
| 1968 | break; |
| 1969 | case LTTNG_DOMAIN_KERNEL: |
| 1970 | /* Code flow error if we don't have a kernel session here. */ |
| 1971 | assert(ksess); |
| 1972 | assert(ksess->consumer); |
| 1973 | consumer = ksess->consumer; |
| 1974 | break; |
| 1975 | case LTTNG_DOMAIN_UST: |
| 1976 | /* Code flow error if we don't have a kernel session here. */ |
| 1977 | assert(usess); |
| 1978 | assert(usess->consumer); |
| 1979 | consumer = usess->consumer; |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | for (i = 0; i < nb_uri; i++) { |
| 1984 | ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name); |
| 1985 | if (ret != LTTNG_OK) { |
| 1986 | goto error; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * Make sure to set the session in output mode after we set URI since a |
| 1992 | * session can be created without URL (thus flagged in no output mode). |
| 1993 | */ |
| 1994 | session->output_traces = 1; |
| 1995 | if (ksess) { |
| 1996 | ksess->output_traces = 1; |
| 1997 | } else if (usess) { |
| 1998 | usess->output_traces = 1; |
| 1999 | } |
| 2000 | |
| 2001 | /* All good! */ |
| 2002 | ret = LTTNG_OK; |
| 2003 | |
| 2004 | error: |
| 2005 | return ret; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Command LTTNG_CREATE_SESSION processed by the client thread. |
| 2010 | */ |
| 2011 | int cmd_create_session_uri(char *name, struct lttng_uri *uris, |
| 2012 | size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer) |
| 2013 | { |
| 2014 | int ret; |
| 2015 | struct ltt_session *session; |
| 2016 | |
| 2017 | assert(name); |
| 2018 | assert(creds); |
| 2019 | |
| 2020 | /* |
| 2021 | * Verify if the session already exist |
| 2022 | * |
| 2023 | * XXX: There is no need for the session lock list here since the caller |
| 2024 | * (process_client_msg) is holding it. We might want to change that so a |
| 2025 | * single command does not lock the entire session list. |
| 2026 | */ |
| 2027 | session = session_find_by_name(name); |
| 2028 | if (session != NULL) { |
| 2029 | ret = LTTNG_ERR_EXIST_SESS; |
| 2030 | goto find_error; |
| 2031 | } |
| 2032 | |
| 2033 | /* Create tracing session in the registry */ |
| 2034 | ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds), |
| 2035 | LTTNG_SOCK_GET_GID_CRED(creds)); |
| 2036 | if (ret != LTTNG_OK) { |
| 2037 | goto session_error; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * Get the newly created session pointer back |
| 2042 | * |
| 2043 | * XXX: There is no need for the session lock list here since the caller |
| 2044 | * (process_client_msg) is holding it. We might want to change that so a |
| 2045 | * single command does not lock the entire session list. |
| 2046 | */ |
| 2047 | session = session_find_by_name(name); |
| 2048 | assert(session); |
| 2049 | |
| 2050 | session->live_timer = live_timer; |
| 2051 | /* Create default consumer output for the session not yet created. */ |
| 2052 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 2053 | if (session->consumer == NULL) { |
| 2054 | ret = LTTNG_ERR_FATAL; |
| 2055 | goto consumer_error; |
| 2056 | } |
| 2057 | |
| 2058 | if (uris) { |
| 2059 | ret = cmd_set_consumer_uri(0, session, nb_uri, uris); |
| 2060 | if (ret != LTTNG_OK) { |
| 2061 | goto consumer_error; |
| 2062 | } |
| 2063 | session->output_traces = 1; |
| 2064 | } else { |
| 2065 | session->output_traces = 0; |
| 2066 | DBG2("Session %s created with no output", session->name); |
| 2067 | } |
| 2068 | |
| 2069 | session->consumer->enabled = 1; |
| 2070 | |
| 2071 | return LTTNG_OK; |
| 2072 | |
| 2073 | consumer_error: |
| 2074 | session_destroy(session); |
| 2075 | session_error: |
| 2076 | find_error: |
| 2077 | return ret; |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread. |
| 2082 | */ |
| 2083 | int cmd_create_session_snapshot(char *name, struct lttng_uri *uris, |
| 2084 | size_t nb_uri, lttng_sock_cred *creds) |
| 2085 | { |
| 2086 | int ret; |
| 2087 | struct ltt_session *session; |
| 2088 | struct snapshot_output *new_output = NULL; |
| 2089 | |
| 2090 | assert(name); |
| 2091 | assert(creds); |
| 2092 | |
| 2093 | /* |
| 2094 | * Create session in no output mode with URIs set to NULL. The uris we've |
| 2095 | * received are for a default snapshot output if one. |
| 2096 | */ |
| 2097 | ret = cmd_create_session_uri(name, NULL, 0, creds, -1); |
| 2098 | if (ret != LTTNG_OK) { |
| 2099 | goto error; |
| 2100 | } |
| 2101 | |
| 2102 | /* Get the newly created session pointer back. This should NEVER fail. */ |
| 2103 | session = session_find_by_name(name); |
| 2104 | assert(session); |
| 2105 | |
| 2106 | /* Flag session for snapshot mode. */ |
| 2107 | session->snapshot_mode = 1; |
| 2108 | |
| 2109 | /* Skip snapshot output creation if no URI is given. */ |
| 2110 | if (nb_uri == 0) { |
| 2111 | goto end; |
| 2112 | } |
| 2113 | |
| 2114 | new_output = snapshot_output_alloc(); |
| 2115 | if (!new_output) { |
| 2116 | ret = LTTNG_ERR_NOMEM; |
| 2117 | goto error_snapshot_alloc; |
| 2118 | } |
| 2119 | |
| 2120 | ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL, |
| 2121 | uris, nb_uri, session->consumer, new_output, &session->snapshot); |
| 2122 | if (ret < 0) { |
| 2123 | if (ret == -ENOMEM) { |
| 2124 | ret = LTTNG_ERR_NOMEM; |
| 2125 | } else { |
| 2126 | ret = LTTNG_ERR_INVALID; |
| 2127 | } |
| 2128 | goto error_snapshot; |
| 2129 | } |
| 2130 | |
| 2131 | rcu_read_lock(); |
| 2132 | snapshot_add_output(&session->snapshot, new_output); |
| 2133 | rcu_read_unlock(); |
| 2134 | |
| 2135 | end: |
| 2136 | return LTTNG_OK; |
| 2137 | |
| 2138 | error_snapshot: |
| 2139 | snapshot_output_destroy(new_output); |
| 2140 | error_snapshot_alloc: |
| 2141 | session_destroy(session); |
| 2142 | error: |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * Command LTTNG_DESTROY_SESSION processed by the client thread. |
| 2148 | */ |
| 2149 | int cmd_destroy_session(struct ltt_session *session, int wpipe) |
| 2150 | { |
| 2151 | int ret; |
| 2152 | struct ltt_ust_session *usess; |
| 2153 | struct ltt_kernel_session *ksess; |
| 2154 | |
| 2155 | /* Safety net */ |
| 2156 | assert(session); |
| 2157 | |
| 2158 | usess = session->ust_session; |
| 2159 | ksess = session->kernel_session; |
| 2160 | |
| 2161 | /* Clean kernel session teardown */ |
| 2162 | kernel_destroy_session(ksess); |
| 2163 | |
| 2164 | /* UST session teardown */ |
| 2165 | if (usess) { |
| 2166 | /* Close any relayd session */ |
| 2167 | consumer_output_send_destroy_relayd(usess->consumer); |
| 2168 | |
| 2169 | /* Destroy every UST application related to this session. */ |
| 2170 | ret = ust_app_destroy_trace_all(usess); |
| 2171 | if (ret) { |
| 2172 | ERR("Error in ust_app_destroy_trace_all"); |
| 2173 | } |
| 2174 | |
| 2175 | /* Clean up the rest. */ |
| 2176 | trace_ust_destroy_session(usess); |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * Must notify the kernel thread here to update it's poll set in order to |
| 2181 | * remove the channel(s)' fd just destroyed. |
| 2182 | */ |
| 2183 | ret = notify_thread_pipe(wpipe); |
| 2184 | if (ret < 0) { |
| 2185 | PERROR("write kernel poll pipe"); |
| 2186 | } |
| 2187 | |
| 2188 | ret = session_destroy(session); |
| 2189 | |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * Command LTTNG_CALIBRATE processed by the client thread. |
| 2195 | */ |
| 2196 | int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) |
| 2197 | { |
| 2198 | int ret; |
| 2199 | |
| 2200 | switch (domain) { |
| 2201 | case LTTNG_DOMAIN_KERNEL: |
| 2202 | { |
| 2203 | struct lttng_kernel_calibrate kcalibrate; |
| 2204 | |
| 2205 | switch (calibrate->type) { |
| 2206 | case LTTNG_CALIBRATE_FUNCTION: |
| 2207 | default: |
| 2208 | /* Default and only possible calibrate option. */ |
| 2209 | kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE; |
| 2210 | break; |
| 2211 | } |
| 2212 | |
| 2213 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); |
| 2214 | if (ret < 0) { |
| 2215 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
| 2216 | goto error; |
| 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | case LTTNG_DOMAIN_UST: |
| 2221 | { |
| 2222 | struct lttng_ust_calibrate ucalibrate; |
| 2223 | |
| 2224 | switch (calibrate->type) { |
| 2225 | case LTTNG_CALIBRATE_FUNCTION: |
| 2226 | default: |
| 2227 | /* Default and only possible calibrate option. */ |
| 2228 | ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT; |
| 2229 | break; |
| 2230 | } |
| 2231 | |
| 2232 | ret = ust_app_calibrate_glb(&ucalibrate); |
| 2233 | if (ret < 0) { |
| 2234 | ret = LTTNG_ERR_UST_CALIBRATE_FAIL; |
| 2235 | goto error; |
| 2236 | } |
| 2237 | break; |
| 2238 | } |
| 2239 | default: |
| 2240 | ret = LTTNG_ERR_UND; |
| 2241 | goto error; |
| 2242 | } |
| 2243 | |
| 2244 | ret = LTTNG_OK; |
| 2245 | |
| 2246 | error: |
| 2247 | return ret; |
| 2248 | } |
| 2249 | |
| 2250 | /* |
| 2251 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. |
| 2252 | */ |
| 2253 | int cmd_register_consumer(struct ltt_session *session, int domain, |
| 2254 | const char *sock_path, struct consumer_data *cdata) |
| 2255 | { |
| 2256 | int ret, sock; |
| 2257 | struct consumer_socket *socket = NULL; |
| 2258 | |
| 2259 | assert(session); |
| 2260 | assert(cdata); |
| 2261 | assert(sock_path); |
| 2262 | |
| 2263 | switch (domain) { |
| 2264 | case LTTNG_DOMAIN_KERNEL: |
| 2265 | { |
| 2266 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2267 | |
| 2268 | assert(ksess); |
| 2269 | |
| 2270 | /* Can't register a consumer if there is already one */ |
| 2271 | if (ksess->consumer_fds_sent != 0) { |
| 2272 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 2273 | goto error; |
| 2274 | } |
| 2275 | |
| 2276 | sock = lttcomm_connect_unix_sock(sock_path); |
| 2277 | if (sock < 0) { |
| 2278 | ret = LTTNG_ERR_CONNECT_FAIL; |
| 2279 | goto error; |
| 2280 | } |
| 2281 | cdata->cmd_sock = sock; |
| 2282 | |
| 2283 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
| 2284 | if (socket == NULL) { |
| 2285 | ret = close(sock); |
| 2286 | if (ret < 0) { |
| 2287 | PERROR("close register consumer"); |
| 2288 | } |
| 2289 | cdata->cmd_sock = -1; |
| 2290 | ret = LTTNG_ERR_FATAL; |
| 2291 | goto error; |
| 2292 | } |
| 2293 | |
| 2294 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); |
| 2295 | if (socket->lock == NULL) { |
| 2296 | PERROR("zmalloc pthread mutex"); |
| 2297 | ret = LTTNG_ERR_FATAL; |
| 2298 | goto error; |
| 2299 | } |
| 2300 | pthread_mutex_init(socket->lock, NULL); |
| 2301 | socket->registered = 1; |
| 2302 | |
| 2303 | rcu_read_lock(); |
| 2304 | consumer_add_socket(socket, ksess->consumer); |
| 2305 | rcu_read_unlock(); |
| 2306 | |
| 2307 | pthread_mutex_lock(&cdata->pid_mutex); |
| 2308 | cdata->pid = -1; |
| 2309 | pthread_mutex_unlock(&cdata->pid_mutex); |
| 2310 | |
| 2311 | break; |
| 2312 | } |
| 2313 | default: |
| 2314 | /* TODO: Userspace tracing */ |
| 2315 | ret = LTTNG_ERR_UND; |
| 2316 | goto error; |
| 2317 | } |
| 2318 | |
| 2319 | return LTTNG_OK; |
| 2320 | |
| 2321 | error: |
| 2322 | if (socket) { |
| 2323 | consumer_destroy_socket(socket); |
| 2324 | } |
| 2325 | return ret; |
| 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * Command LTTNG_LIST_DOMAINS processed by the client thread. |
| 2330 | */ |
| 2331 | ssize_t cmd_list_domains(struct ltt_session *session, |
| 2332 | struct lttng_domain **domains) |
| 2333 | { |
| 2334 | int ret, index = 0; |
| 2335 | ssize_t nb_dom = 0; |
| 2336 | |
| 2337 | if (session->kernel_session != NULL) { |
| 2338 | DBG3("Listing domains found kernel domain"); |
| 2339 | nb_dom++; |
| 2340 | } |
| 2341 | |
| 2342 | if (session->ust_session != NULL) { |
| 2343 | DBG3("Listing domains found UST global domain"); |
| 2344 | nb_dom++; |
| 2345 | |
| 2346 | if (session->ust_session->domain_jul.being_used) { |
| 2347 | nb_dom++; |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
| 2352 | if (*domains == NULL) { |
| 2353 | ret = LTTNG_ERR_FATAL; |
| 2354 | goto error; |
| 2355 | } |
| 2356 | |
| 2357 | if (session->kernel_session != NULL) { |
| 2358 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; |
| 2359 | index++; |
| 2360 | } |
| 2361 | |
| 2362 | if (session->ust_session != NULL) { |
| 2363 | (*domains)[index].type = LTTNG_DOMAIN_UST; |
| 2364 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
| 2365 | index++; |
| 2366 | |
| 2367 | if (session->ust_session->domain_jul.being_used) { |
| 2368 | (*domains)[index].type = LTTNG_DOMAIN_JUL; |
| 2369 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
| 2370 | index++; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | return nb_dom; |
| 2375 | |
| 2376 | error: |
| 2377 | /* Return negative value to differentiate return code */ |
| 2378 | return -ret; |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | /* |
| 2383 | * Command LTTNG_LIST_CHANNELS processed by the client thread. |
| 2384 | */ |
| 2385 | ssize_t cmd_list_channels(int domain, struct ltt_session *session, |
| 2386 | struct lttng_channel **channels) |
| 2387 | { |
| 2388 | int ret; |
| 2389 | ssize_t nb_chan = 0; |
| 2390 | |
| 2391 | switch (domain) { |
| 2392 | case LTTNG_DOMAIN_KERNEL: |
| 2393 | if (session->kernel_session != NULL) { |
| 2394 | nb_chan = session->kernel_session->channel_count; |
| 2395 | } |
| 2396 | DBG3("Number of kernel channels %zd", nb_chan); |
| 2397 | if (nb_chan <= 0) { |
| 2398 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 2399 | } |
| 2400 | break; |
| 2401 | case LTTNG_DOMAIN_UST: |
| 2402 | if (session->ust_session != NULL) { |
| 2403 | nb_chan = lttng_ht_get_count( |
| 2404 | session->ust_session->domain_global.channels); |
| 2405 | } |
| 2406 | DBG3("Number of UST global channels %zd", nb_chan); |
| 2407 | if (nb_chan < 0) { |
| 2408 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 2409 | goto error; |
| 2410 | } |
| 2411 | break; |
| 2412 | default: |
| 2413 | ret = LTTNG_ERR_UND; |
| 2414 | goto error; |
| 2415 | } |
| 2416 | |
| 2417 | if (nb_chan > 0) { |
| 2418 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); |
| 2419 | if (*channels == NULL) { |
| 2420 | ret = LTTNG_ERR_FATAL; |
| 2421 | goto error; |
| 2422 | } |
| 2423 | |
| 2424 | list_lttng_channels(domain, session, *channels); |
| 2425 | } |
| 2426 | |
| 2427 | return nb_chan; |
| 2428 | |
| 2429 | error: |
| 2430 | /* Return negative value to differentiate return code */ |
| 2431 | return -ret; |
| 2432 | } |
| 2433 | |
| 2434 | /* |
| 2435 | * Command LTTNG_LIST_EVENTS processed by the client thread. |
| 2436 | */ |
| 2437 | ssize_t cmd_list_events(int domain, struct ltt_session *session, |
| 2438 | char *channel_name, struct lttng_event **events) |
| 2439 | { |
| 2440 | int ret = 0; |
| 2441 | ssize_t nb_event = 0; |
| 2442 | |
| 2443 | switch (domain) { |
| 2444 | case LTTNG_DOMAIN_KERNEL: |
| 2445 | if (session->kernel_session != NULL) { |
| 2446 | nb_event = list_lttng_kernel_events(channel_name, |
| 2447 | session->kernel_session, events); |
| 2448 | } |
| 2449 | break; |
| 2450 | case LTTNG_DOMAIN_UST: |
| 2451 | { |
| 2452 | if (session->ust_session != NULL) { |
| 2453 | nb_event = list_lttng_ust_global_events(channel_name, |
| 2454 | &session->ust_session->domain_global, events); |
| 2455 | } |
| 2456 | break; |
| 2457 | } |
| 2458 | case LTTNG_DOMAIN_JUL: |
| 2459 | if (session->ust_session) { |
| 2460 | nb_event = list_lttng_jul_events( |
| 2461 | &session->ust_session->domain_jul, events); |
| 2462 | } |
| 2463 | break; |
| 2464 | default: |
| 2465 | ret = LTTNG_ERR_UND; |
| 2466 | goto error; |
| 2467 | } |
| 2468 | |
| 2469 | return nb_event; |
| 2470 | |
| 2471 | error: |
| 2472 | /* Return negative value to differentiate return code */ |
| 2473 | return -ret; |
| 2474 | } |
| 2475 | |
| 2476 | /* |
| 2477 | * Using the session list, filled a lttng_session array to send back to the |
| 2478 | * client for session listing. |
| 2479 | * |
| 2480 | * The session list lock MUST be acquired before calling this function. Use |
| 2481 | * session_lock_list() and session_unlock_list(). |
| 2482 | */ |
| 2483 | void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid, |
| 2484 | gid_t gid) |
| 2485 | { |
| 2486 | int ret; |
| 2487 | unsigned int i = 0; |
| 2488 | struct ltt_session *session; |
| 2489 | struct ltt_session_list *list = session_get_list(); |
| 2490 | |
| 2491 | DBG("Getting all available session for UID %d GID %d", |
| 2492 | uid, gid); |
| 2493 | /* |
| 2494 | * Iterate over session list and append data after the control struct in |
| 2495 | * the buffer. |
| 2496 | */ |
| 2497 | cds_list_for_each_entry(session, &list->head, list) { |
| 2498 | /* |
| 2499 | * Only list the sessions the user can control. |
| 2500 | */ |
| 2501 | if (!session_access_ok(session, uid, gid)) { |
| 2502 | continue; |
| 2503 | } |
| 2504 | |
| 2505 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2506 | struct ltt_ust_session *usess = session->ust_session; |
| 2507 | |
| 2508 | if (session->consumer->type == CONSUMER_DST_NET || |
| 2509 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || |
| 2510 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { |
| 2511 | ret = build_network_session_path(sessions[i].path, |
| 2512 | sizeof(sessions[i].path), session); |
| 2513 | } else { |
| 2514 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
| 2515 | session->consumer->dst.trace_path); |
| 2516 | } |
| 2517 | if (ret < 0) { |
| 2518 | PERROR("snprintf session path"); |
| 2519 | continue; |
| 2520 | } |
| 2521 | |
| 2522 | strncpy(sessions[i].name, session->name, NAME_MAX); |
| 2523 | sessions[i].name[NAME_MAX - 1] = '\0'; |
| 2524 | sessions[i].enabled = session->active; |
| 2525 | sessions[i].snapshot_mode = session->snapshot_mode; |
| 2526 | sessions[i].live_timer_interval = session->live_timer; |
| 2527 | i++; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
| 2533 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
| 2534 | */ |
| 2535 | int cmd_data_pending(struct ltt_session *session) |
| 2536 | { |
| 2537 | int ret; |
| 2538 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2539 | struct ltt_ust_session *usess = session->ust_session; |
| 2540 | |
| 2541 | assert(session); |
| 2542 | |
| 2543 | /* Session MUST be stopped to ask for data availability. */ |
| 2544 | if (session->active) { |
| 2545 | ret = LTTNG_ERR_SESSION_STARTED; |
| 2546 | goto error; |
| 2547 | } else { |
| 2548 | /* |
| 2549 | * If stopped, just make sure we've started before else the above call |
| 2550 | * will always send that there is data pending. |
| 2551 | * |
| 2552 | * The consumer assumes that when the data pending command is received, |
| 2553 | * the trace has been started before or else no output data is written |
| 2554 | * by the streams which is a condition for data pending. So, this is |
| 2555 | * *VERY* important that we don't ask the consumer before a start |
| 2556 | * trace. |
| 2557 | */ |
| 2558 | if (!session->has_been_started) { |
| 2559 | ret = 0; |
| 2560 | goto error; |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | if (ksess && ksess->consumer) { |
| 2565 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
| 2566 | if (ret == 1) { |
| 2567 | /* Data is still being extracted for the kernel. */ |
| 2568 | goto error; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | if (usess && usess->consumer) { |
| 2573 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
| 2574 | if (ret == 1) { |
| 2575 | /* Data is still being extracted for the kernel. */ |
| 2576 | goto error; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | /* Data is ready to be read by a viewer */ |
| 2581 | ret = 0; |
| 2582 | |
| 2583 | error: |
| 2584 | return ret; |
| 2585 | } |
| 2586 | |
| 2587 | /* |
| 2588 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. |
| 2589 | * |
| 2590 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 2591 | */ |
| 2592 | int cmd_snapshot_add_output(struct ltt_session *session, |
| 2593 | struct lttng_snapshot_output *output, uint32_t *id) |
| 2594 | { |
| 2595 | int ret; |
| 2596 | struct snapshot_output *new_output; |
| 2597 | |
| 2598 | assert(session); |
| 2599 | assert(output); |
| 2600 | |
| 2601 | DBG("Cmd snapshot add output for session %s", session->name); |
| 2602 | |
| 2603 | /* |
| 2604 | * Permission denied to create an output if the session is not |
| 2605 | * set in no output mode. |
| 2606 | */ |
| 2607 | if (session->output_traces) { |
| 2608 | ret = LTTNG_ERR_EPERM; |
| 2609 | goto error; |
| 2610 | } |
| 2611 | |
| 2612 | /* Only one output is allowed until we have the "tee" feature. */ |
| 2613 | if (session->snapshot.nb_output == 1) { |
| 2614 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; |
| 2615 | goto error; |
| 2616 | } |
| 2617 | |
| 2618 | new_output = snapshot_output_alloc(); |
| 2619 | if (!new_output) { |
| 2620 | ret = LTTNG_ERR_NOMEM; |
| 2621 | goto error; |
| 2622 | } |
| 2623 | |
| 2624 | ret = snapshot_output_init(output->max_size, output->name, |
| 2625 | output->ctrl_url, output->data_url, session->consumer, new_output, |
| 2626 | &session->snapshot); |
| 2627 | if (ret < 0) { |
| 2628 | if (ret == -ENOMEM) { |
| 2629 | ret = LTTNG_ERR_NOMEM; |
| 2630 | } else { |
| 2631 | ret = LTTNG_ERR_INVALID; |
| 2632 | } |
| 2633 | goto free_error; |
| 2634 | } |
| 2635 | |
| 2636 | rcu_read_lock(); |
| 2637 | snapshot_add_output(&session->snapshot, new_output); |
| 2638 | if (id) { |
| 2639 | *id = new_output->id; |
| 2640 | } |
| 2641 | rcu_read_unlock(); |
| 2642 | |
| 2643 | return LTTNG_OK; |
| 2644 | |
| 2645 | free_error: |
| 2646 | snapshot_output_destroy(new_output); |
| 2647 | error: |
| 2648 | return ret; |
| 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. |
| 2653 | * |
| 2654 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 2655 | */ |
| 2656 | int cmd_snapshot_del_output(struct ltt_session *session, |
| 2657 | struct lttng_snapshot_output *output) |
| 2658 | { |
| 2659 | int ret; |
| 2660 | struct snapshot_output *sout = NULL; |
| 2661 | |
| 2662 | assert(session); |
| 2663 | assert(output); |
| 2664 | |
| 2665 | rcu_read_lock(); |
| 2666 | |
| 2667 | /* |
| 2668 | * Permission denied to create an output if the session is not |
| 2669 | * set in no output mode. |
| 2670 | */ |
| 2671 | if (session->output_traces) { |
| 2672 | ret = LTTNG_ERR_EPERM; |
| 2673 | goto error; |
| 2674 | } |
| 2675 | |
| 2676 | if (output->id) { |
| 2677 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, |
| 2678 | session->name); |
| 2679 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); |
| 2680 | } else if (*output->name != '\0') { |
| 2681 | DBG("Cmd snapshot del output name %s for session %s", output->name, |
| 2682 | session->name); |
| 2683 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); |
| 2684 | } |
| 2685 | if (!sout) { |
| 2686 | ret = LTTNG_ERR_INVALID; |
| 2687 | goto error; |
| 2688 | } |
| 2689 | |
| 2690 | snapshot_delete_output(&session->snapshot, sout); |
| 2691 | snapshot_output_destroy(sout); |
| 2692 | ret = LTTNG_OK; |
| 2693 | |
| 2694 | error: |
| 2695 | rcu_read_unlock(); |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. |
| 2701 | * |
| 2702 | * If no output is available, outputs is untouched and 0 is returned. |
| 2703 | * |
| 2704 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. |
| 2705 | */ |
| 2706 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, |
| 2707 | struct lttng_snapshot_output **outputs) |
| 2708 | { |
| 2709 | int ret, idx = 0; |
| 2710 | struct lttng_snapshot_output *list; |
| 2711 | struct lttng_ht_iter iter; |
| 2712 | struct snapshot_output *output; |
| 2713 | |
| 2714 | assert(session); |
| 2715 | assert(outputs); |
| 2716 | |
| 2717 | DBG("Cmd snapshot list outputs for session %s", session->name); |
| 2718 | |
| 2719 | /* |
| 2720 | * Permission denied to create an output if the session is not |
| 2721 | * set in no output mode. |
| 2722 | */ |
| 2723 | if (session->output_traces) { |
| 2724 | ret = LTTNG_ERR_EPERM; |
| 2725 | goto error; |
| 2726 | } |
| 2727 | |
| 2728 | if (session->snapshot.nb_output == 0) { |
| 2729 | ret = 0; |
| 2730 | goto error; |
| 2731 | } |
| 2732 | |
| 2733 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); |
| 2734 | if (!list) { |
| 2735 | ret = LTTNG_ERR_NOMEM; |
| 2736 | goto error; |
| 2737 | } |
| 2738 | |
| 2739 | /* Copy list from session to the new list object. */ |
| 2740 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
| 2741 | output, node.node) { |
| 2742 | assert(output->consumer); |
| 2743 | list[idx].id = output->id; |
| 2744 | list[idx].max_size = output->max_size; |
| 2745 | strncpy(list[idx].name, output->name, sizeof(list[idx].name)); |
| 2746 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
| 2747 | strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path, |
| 2748 | sizeof(list[idx].ctrl_url)); |
| 2749 | } else { |
| 2750 | /* Control URI. */ |
| 2751 | ret = uri_to_str_url(&output->consumer->dst.net.control, |
| 2752 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); |
| 2753 | if (ret < 0) { |
| 2754 | ret = LTTNG_ERR_NOMEM; |
| 2755 | goto free_error; |
| 2756 | } |
| 2757 | |
| 2758 | /* Data URI. */ |
| 2759 | ret = uri_to_str_url(&output->consumer->dst.net.data, |
| 2760 | list[idx].data_url, sizeof(list[idx].data_url)); |
| 2761 | if (ret < 0) { |
| 2762 | ret = LTTNG_ERR_NOMEM; |
| 2763 | goto free_error; |
| 2764 | } |
| 2765 | } |
| 2766 | idx++; |
| 2767 | } |
| 2768 | |
| 2769 | *outputs = list; |
| 2770 | return session->snapshot.nb_output; |
| 2771 | |
| 2772 | free_error: |
| 2773 | free(list); |
| 2774 | error: |
| 2775 | return -ret; |
| 2776 | } |
| 2777 | |
| 2778 | /* |
| 2779 | * Send relayd sockets from snapshot output to consumer. Ignore request if the |
| 2780 | * snapshot output is *not* set with a remote destination. |
| 2781 | * |
| 2782 | * Return 0 on success or a LTTNG_ERR code. |
| 2783 | */ |
| 2784 | static int set_relayd_for_snapshot(struct consumer_output *consumer, |
| 2785 | struct snapshot_output *snap_output, struct ltt_session *session) |
| 2786 | { |
| 2787 | int ret = LTTNG_OK; |
| 2788 | struct lttng_ht_iter iter; |
| 2789 | struct consumer_socket *socket; |
| 2790 | |
| 2791 | assert(consumer); |
| 2792 | assert(snap_output); |
| 2793 | assert(session); |
| 2794 | |
| 2795 | DBG2("Set relayd object from snapshot output"); |
| 2796 | |
| 2797 | /* Ignore if snapshot consumer output is not network. */ |
| 2798 | if (snap_output->consumer->type != CONSUMER_DST_NET) { |
| 2799 | goto error; |
| 2800 | } |
| 2801 | |
| 2802 | /* |
| 2803 | * For each consumer socket, create and send the relayd object of the |
| 2804 | * snapshot output. |
| 2805 | */ |
| 2806 | rcu_read_lock(); |
| 2807 | cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter, |
| 2808 | socket, node.node) { |
| 2809 | ret = send_consumer_relayd_sockets(0, session->id, |
| 2810 | snap_output->consumer, socket, |
| 2811 | session->name, session->hostname, |
| 2812 | session->live_timer); |
| 2813 | if (ret != LTTNG_OK) { |
| 2814 | rcu_read_unlock(); |
| 2815 | goto error; |
| 2816 | } |
| 2817 | } |
| 2818 | rcu_read_unlock(); |
| 2819 | |
| 2820 | error: |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | /* |
| 2825 | * Record a kernel snapshot. |
| 2826 | * |
| 2827 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
| 2828 | */ |
| 2829 | static int record_kernel_snapshot(struct ltt_kernel_session *ksess, |
| 2830 | struct snapshot_output *output, struct ltt_session *session, |
| 2831 | int wait, uint64_t max_stream_size) |
| 2832 | { |
| 2833 | int ret; |
| 2834 | |
| 2835 | assert(ksess); |
| 2836 | assert(output); |
| 2837 | assert(session); |
| 2838 | |
| 2839 | /* Get the datetime for the snapshot output directory. */ |
| 2840 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, |
| 2841 | sizeof(output->datetime)); |
| 2842 | if (!ret) { |
| 2843 | ret = LTTNG_ERR_INVALID; |
| 2844 | goto error; |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Copy kernel session sockets so we can communicate with the right |
| 2849 | * consumer for the snapshot record command. |
| 2850 | */ |
| 2851 | ret = consumer_copy_sockets(output->consumer, ksess->consumer); |
| 2852 | if (ret < 0) { |
| 2853 | ret = LTTNG_ERR_NOMEM; |
| 2854 | goto error; |
| 2855 | } |
| 2856 | |
| 2857 | ret = set_relayd_for_snapshot(ksess->consumer, output, session); |
| 2858 | if (ret != LTTNG_OK) { |
| 2859 | goto error_snapshot; |
| 2860 | } |
| 2861 | |
| 2862 | ret = kernel_snapshot_record(ksess, output, wait, max_stream_size); |
| 2863 | if (ret != LTTNG_OK) { |
| 2864 | goto error_snapshot; |
| 2865 | } |
| 2866 | |
| 2867 | ret = LTTNG_OK; |
| 2868 | |
| 2869 | error_snapshot: |
| 2870 | /* Clean up copied sockets so this output can use some other later on. */ |
| 2871 | consumer_destroy_output_sockets(output->consumer); |
| 2872 | error: |
| 2873 | return ret; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
| 2877 | * Record a UST snapshot. |
| 2878 | * |
| 2879 | * Return 0 on success or a LTTNG_ERR error code. |
| 2880 | */ |
| 2881 | static int record_ust_snapshot(struct ltt_ust_session *usess, |
| 2882 | struct snapshot_output *output, struct ltt_session *session, |
| 2883 | int wait, uint64_t max_stream_size) |
| 2884 | { |
| 2885 | int ret; |
| 2886 | |
| 2887 | assert(usess); |
| 2888 | assert(output); |
| 2889 | assert(session); |
| 2890 | |
| 2891 | /* Get the datetime for the snapshot output directory. */ |
| 2892 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, |
| 2893 | sizeof(output->datetime)); |
| 2894 | if (!ret) { |
| 2895 | ret = LTTNG_ERR_INVALID; |
| 2896 | goto error; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * Copy UST session sockets so we can communicate with the right |
| 2901 | * consumer for the snapshot record command. |
| 2902 | */ |
| 2903 | ret = consumer_copy_sockets(output->consumer, usess->consumer); |
| 2904 | if (ret < 0) { |
| 2905 | ret = LTTNG_ERR_NOMEM; |
| 2906 | goto error; |
| 2907 | } |
| 2908 | |
| 2909 | ret = set_relayd_for_snapshot(usess->consumer, output, session); |
| 2910 | if (ret != LTTNG_OK) { |
| 2911 | goto error_snapshot; |
| 2912 | } |
| 2913 | |
| 2914 | ret = ust_app_snapshot_record(usess, output, wait, max_stream_size); |
| 2915 | if (ret < 0) { |
| 2916 | switch (-ret) { |
| 2917 | case EINVAL: |
| 2918 | ret = LTTNG_ERR_INVALID; |
| 2919 | break; |
| 2920 | case ENODATA: |
| 2921 | ret = LTTNG_ERR_SNAPSHOT_NODATA; |
| 2922 | break; |
| 2923 | default: |
| 2924 | ret = LTTNG_ERR_SNAPSHOT_FAIL; |
| 2925 | break; |
| 2926 | } |
| 2927 | goto error_snapshot; |
| 2928 | } |
| 2929 | |
| 2930 | ret = LTTNG_OK; |
| 2931 | |
| 2932 | error_snapshot: |
| 2933 | /* Clean up copied sockets so this output can use some other later on. */ |
| 2934 | consumer_destroy_output_sockets(output->consumer); |
| 2935 | error: |
| 2936 | return ret; |
| 2937 | } |
| 2938 | |
| 2939 | /* |
| 2940 | * Return the biggest subbuffer size of all channels in the given session. |
| 2941 | */ |
| 2942 | static uint64_t get_session_max_subbuf_size(struct ltt_session *session) |
| 2943 | { |
| 2944 | uint64_t max_size = 0; |
| 2945 | |
| 2946 | assert(session); |
| 2947 | |
| 2948 | if (session->kernel_session) { |
| 2949 | struct ltt_kernel_channel *chan; |
| 2950 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2951 | |
| 2952 | /* |
| 2953 | * For each channel, add to the max size the size of each subbuffer |
| 2954 | * multiplied by their sized. |
| 2955 | */ |
| 2956 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
| 2957 | if (chan->channel->attr.subbuf_size > max_size) { |
| 2958 | max_size = chan->channel->attr.subbuf_size; |
| 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | if (session->ust_session) { |
| 2964 | struct lttng_ht_iter iter; |
| 2965 | struct ltt_ust_channel *uchan; |
| 2966 | struct ltt_ust_session *usess = session->ust_session; |
| 2967 | |
| 2968 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
| 2969 | uchan, node.node) { |
| 2970 | if (uchan->attr.subbuf_size > max_size) { |
| 2971 | max_size = uchan->attr.subbuf_size; |
| 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | return max_size; |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Returns the total number of streams for a session or a negative value |
| 2981 | * on error. |
| 2982 | */ |
| 2983 | static unsigned int get_session_nb_streams(struct ltt_session *session) |
| 2984 | { |
| 2985 | unsigned int total_streams = 0; |
| 2986 | |
| 2987 | if (session->kernel_session) { |
| 2988 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2989 | |
| 2990 | total_streams += ksess->stream_count_global; |
| 2991 | } |
| 2992 | |
| 2993 | if (session->ust_session) { |
| 2994 | struct ltt_ust_session *usess = session->ust_session; |
| 2995 | |
| 2996 | total_streams += ust_app_get_nb_stream(usess); |
| 2997 | } |
| 2998 | |
| 2999 | return total_streams; |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. |
| 3004 | * |
| 3005 | * The wait parameter is ignored so this call always wait for the snapshot to |
| 3006 | * complete before returning. |
| 3007 | * |
| 3008 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 3009 | */ |
| 3010 | int cmd_snapshot_record(struct ltt_session *session, |
| 3011 | struct lttng_snapshot_output *output, int wait) |
| 3012 | { |
| 3013 | int ret = LTTNG_OK; |
| 3014 | unsigned int use_tmp_output = 0; |
| 3015 | struct snapshot_output tmp_output; |
| 3016 | unsigned int nb_streams, snapshot_success = 0; |
| 3017 | uint64_t session_max_size = 0, max_stream_size = 0; |
| 3018 | |
| 3019 | assert(session); |
| 3020 | |
| 3021 | DBG("Cmd snapshot record for session %s", session->name); |
| 3022 | |
| 3023 | /* |
| 3024 | * Permission denied to create an output if the session is not |
| 3025 | * set in no output mode. |
| 3026 | */ |
| 3027 | if (session->output_traces) { |
| 3028 | ret = LTTNG_ERR_EPERM; |
| 3029 | goto error; |
| 3030 | } |
| 3031 | |
| 3032 | /* The session needs to be started at least once. */ |
| 3033 | if (!session->has_been_started) { |
| 3034 | ret = LTTNG_ERR_START_SESSION_ONCE; |
| 3035 | goto error; |
| 3036 | } |
| 3037 | |
| 3038 | /* Use temporary output for the session. */ |
| 3039 | if (output && *output->ctrl_url != '\0') { |
| 3040 | ret = snapshot_output_init(output->max_size, output->name, |
| 3041 | output->ctrl_url, output->data_url, session->consumer, |
| 3042 | &tmp_output, NULL); |
| 3043 | if (ret < 0) { |
| 3044 | if (ret == -ENOMEM) { |
| 3045 | ret = LTTNG_ERR_NOMEM; |
| 3046 | } else { |
| 3047 | ret = LTTNG_ERR_INVALID; |
| 3048 | } |
| 3049 | goto error; |
| 3050 | } |
| 3051 | /* Use the global session count for the temporary snapshot. */ |
| 3052 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
| 3053 | use_tmp_output = 1; |
| 3054 | } |
| 3055 | |
| 3056 | /* |
| 3057 | * Get the session maximum size for a snapshot meaning it will compute the |
| 3058 | * size of all streams from all domain. |
| 3059 | */ |
| 3060 | max_stream_size = get_session_max_subbuf_size(session); |
| 3061 | |
| 3062 | nb_streams = get_session_nb_streams(session); |
| 3063 | if (nb_streams) { |
| 3064 | /* |
| 3065 | * The maximum size of the snapshot is the number of streams multiplied |
| 3066 | * by the biggest subbuf size of all channels in a session which is the |
| 3067 | * maximum stream size available for each stream. The session max size |
| 3068 | * is now checked against the snapshot max size value given by the user |
| 3069 | * and if lower, an error is returned. |
| 3070 | */ |
| 3071 | session_max_size = max_stream_size * nb_streams; |
| 3072 | } |
| 3073 | |
| 3074 | DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64, |
| 3075 | session_max_size, max_stream_size); |
| 3076 | |
| 3077 | /* |
| 3078 | * If we use a temporary output, check right away if the max size fits else |
| 3079 | * for each output the max size will be checked. |
| 3080 | */ |
| 3081 | if (use_tmp_output && |
| 3082 | (tmp_output.max_size != 0 && |
| 3083 | tmp_output.max_size < session_max_size)) { |
| 3084 | ret = LTTNG_ERR_MAX_SIZE_INVALID; |
| 3085 | goto error; |
| 3086 | } |
| 3087 | |
| 3088 | if (session->kernel_session) { |
| 3089 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3090 | |
| 3091 | if (use_tmp_output) { |
| 3092 | ret = record_kernel_snapshot(ksess, &tmp_output, session, |
| 3093 | wait, max_stream_size); |
| 3094 | if (ret != LTTNG_OK) { |
| 3095 | goto error; |
| 3096 | } |
| 3097 | snapshot_success = 1; |
| 3098 | } else { |
| 3099 | struct snapshot_output *sout; |
| 3100 | struct lttng_ht_iter iter; |
| 3101 | |
| 3102 | rcu_read_lock(); |
| 3103 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, |
| 3104 | &iter.iter, sout, node.node) { |
| 3105 | /* |
| 3106 | * Make a local copy of the output and assign the possible |
| 3107 | * temporary value given by the caller. |
| 3108 | */ |
| 3109 | memset(&tmp_output, 0, sizeof(tmp_output)); |
| 3110 | memcpy(&tmp_output, sout, sizeof(tmp_output)); |
| 3111 | |
| 3112 | /* Use temporary max size. */ |
| 3113 | if (output->max_size != (uint64_t) -1ULL) { |
| 3114 | tmp_output.max_size = output->max_size; |
| 3115 | } |
| 3116 | |
| 3117 | if (tmp_output.max_size != 0 && |
| 3118 | tmp_output.max_size < session_max_size) { |
| 3119 | rcu_read_unlock(); |
| 3120 | ret = LTTNG_ERR_MAX_SIZE_INVALID; |
| 3121 | goto error; |
| 3122 | } |
| 3123 | |
| 3124 | /* Use temporary name. */ |
| 3125 | if (*output->name != '\0') { |
| 3126 | strncpy(tmp_output.name, output->name, |
| 3127 | sizeof(tmp_output.name)); |
| 3128 | } |
| 3129 | |
| 3130 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
| 3131 | |
| 3132 | ret = record_kernel_snapshot(ksess, &tmp_output, |
| 3133 | session, wait, max_stream_size); |
| 3134 | if (ret != LTTNG_OK) { |
| 3135 | rcu_read_unlock(); |
| 3136 | goto error; |
| 3137 | } |
| 3138 | snapshot_success = 1; |
| 3139 | } |
| 3140 | rcu_read_unlock(); |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | if (session->ust_session) { |
| 3145 | struct ltt_ust_session *usess = session->ust_session; |
| 3146 | |
| 3147 | if (use_tmp_output) { |
| 3148 | ret = record_ust_snapshot(usess, &tmp_output, session, |
| 3149 | wait, max_stream_size); |
| 3150 | if (ret != LTTNG_OK) { |
| 3151 | goto error; |
| 3152 | } |
| 3153 | snapshot_success = 1; |
| 3154 | } else { |
| 3155 | struct snapshot_output *sout; |
| 3156 | struct lttng_ht_iter iter; |
| 3157 | |
| 3158 | rcu_read_lock(); |
| 3159 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, |
| 3160 | &iter.iter, sout, node.node) { |
| 3161 | /* |
| 3162 | * Make a local copy of the output and assign the possible |
| 3163 | * temporary value given by the caller. |
| 3164 | */ |
| 3165 | memset(&tmp_output, 0, sizeof(tmp_output)); |
| 3166 | memcpy(&tmp_output, sout, sizeof(tmp_output)); |
| 3167 | |
| 3168 | /* Use temporary max size. */ |
| 3169 | if (output->max_size != (uint64_t) -1ULL) { |
| 3170 | tmp_output.max_size = output->max_size; |
| 3171 | } |
| 3172 | |
| 3173 | if (tmp_output.max_size != 0 && |
| 3174 | tmp_output.max_size < session_max_size) { |
| 3175 | rcu_read_unlock(); |
| 3176 | ret = LTTNG_ERR_MAX_SIZE_INVALID; |
| 3177 | goto error; |
| 3178 | } |
| 3179 | |
| 3180 | /* Use temporary name. */ |
| 3181 | if (*output->name != '\0') { |
| 3182 | strncpy(tmp_output.name, output->name, |
| 3183 | sizeof(tmp_output.name)); |
| 3184 | } |
| 3185 | |
| 3186 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
| 3187 | |
| 3188 | ret = record_ust_snapshot(usess, &tmp_output, session, |
| 3189 | wait, max_stream_size); |
| 3190 | if (ret != LTTNG_OK) { |
| 3191 | rcu_read_unlock(); |
| 3192 | goto error; |
| 3193 | } |
| 3194 | snapshot_success = 1; |
| 3195 | } |
| 3196 | rcu_read_unlock(); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | if (snapshot_success) { |
| 3201 | session->snapshot.nb_snapshot++; |
| 3202 | } else { |
| 3203 | ret = LTTNG_ERR_SNAPSHOT_FAIL; |
| 3204 | } |
| 3205 | |
| 3206 | error: |
| 3207 | return ret; |
| 3208 | } |
| 3209 | |
| 3210 | /* |
| 3211 | * Init command subsystem. |
| 3212 | */ |
| 3213 | void cmd_init(void) |
| 3214 | { |
| 3215 | /* |
| 3216 | * Set network sequence index to 1 for streams to match a relayd |
| 3217 | * socket on the consumer side. |
| 3218 | */ |
| 3219 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 3220 | relayd_net_seq_idx = 1; |
| 3221 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 3222 | |
| 3223 | DBG("Command subsystem initialized"); |
| 3224 | } |