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