c9924b326190dd4e765ff81af7175bf00a0b1410
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.cpp
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include "agent-thread.hpp"
11 #include "agent.hpp"
12 #include "buffer-registry.hpp"
13 #include "channel.hpp"
14 #include "cmd.hpp"
15 #include "consumer.hpp"
16 #include "event-notifier-error-accounting.hpp"
17 #include "event.hpp"
18 #include "health-sessiond.hpp"
19 #include "kernel-consumer.hpp"
20 #include "kernel.hpp"
21 #include "lttng-sessiond.hpp"
22 #include "lttng-syscall.hpp"
23 #include "notification-thread-commands.hpp"
24 #include "notification-thread.hpp"
25 #include "rotation-thread.hpp"
26 #include "session.hpp"
27 #include "timer.hpp"
28 #include "tracker.hpp"
29 #include "utils.hpp"
30
31 #include <common/buffer-view.hpp>
32 #include <common/common.hpp>
33 #include <common/compat/string.hpp>
34 #include <common/defaults.hpp>
35 #include <common/dynamic-buffer.hpp>
36 #include <common/kernel-ctl/kernel-ctl.hpp>
37 #include <common/payload-view.hpp>
38 #include <common/payload.hpp>
39 #include <common/relayd/relayd.hpp>
40 #include <common/sessiond-comm/sessiond-comm.hpp>
41 #include <common/string-utils/string-utils.hpp>
42 #include <common/trace-chunk.hpp>
43 #include <common/urcu.hpp>
44 #include <common/utils.hpp>
45
46 #include <lttng/action/action-internal.hpp>
47 #include <lttng/action/action.h>
48 #include <lttng/channel-internal.hpp>
49 #include <lttng/channel.h>
50 #include <lttng/condition/condition-internal.hpp>
51 #include <lttng/condition/condition.h>
52 #include <lttng/condition/event-rule-matches-internal.hpp>
53 #include <lttng/condition/event-rule-matches.h>
54 #include <lttng/error-query-internal.hpp>
55 #include <lttng/event-internal.hpp>
56 #include <lttng/event-rule/event-rule-internal.hpp>
57 #include <lttng/event-rule/event-rule.h>
58 #include <lttng/location-internal.hpp>
59 #include <lttng/lttng-error.h>
60 #include <lttng/rotate-internal.hpp>
61 #include <lttng/session-descriptor-internal.hpp>
62 #include <lttng/session-internal.hpp>
63 #include <lttng/tracker.h>
64 #include <lttng/trigger/trigger-internal.hpp>
65 #include <lttng/userspace-probe-internal.hpp>
66
67 #include <algorithm>
68 #include <inttypes.h>
69 #include <stdio.h>
70 #include <sys/stat.h>
71 #include <urcu/list.h>
72 #include <urcu/uatomic.h>
73
74 /* Sleep for 100ms between each check for the shm path's deletion. */
75 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
76
77 namespace lsu = lttng::sessiond::ust;
78
79 static enum lttng_error_code wait_on_path(void *path);
80
81 namespace {
82 struct cmd_destroy_session_reply_context {
83 int reply_sock_fd;
84 bool implicit_rotation_on_destroy;
85 /*
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
88 */
89 enum lttng_error_code destruction_status;
90 };
91
92 /*
93 * Command completion handler that is used by the destroy command
94 * when a session that has a non-default shm_path is being destroyed.
95 *
96 * See comment in cmd_destroy_session() for the rationale.
97 */
98 struct destroy_completion_handler {
99 struct cmd_completion_handler handler;
100 char shm_path[member_sizeof(struct ltt_session, shm_path)];
101 } destroy_completion_handler = {
102 .handler = { .run = wait_on_path, .data = destroy_completion_handler.shm_path },
103 .shm_path = { 0 },
104 };
105
106 /*
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 * held.
111 */
112 pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
113 uint64_t relayd_net_seq_idx;
114 } /* namespace */
115
116 static struct cmd_completion_handler *current_completion_handler;
117 static int validate_ust_event_name(const char *);
118 static int cmd_enable_event_internal(struct ltt_session *session,
119 const struct lttng_domain *domain,
120 char *channel_name,
121 struct lttng_event *event,
122 char *filter_expression,
123 struct lttng_bytecode *filter,
124 struct lttng_event_exclusion *exclusion,
125 int wpipe);
126 static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
127 const struct lttng_domain *domain,
128 const struct lttng_channel *_attr,
129 int wpipe);
130
131 /*
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
134 */
135 static int build_network_session_path(char *dst, size_t size, struct ltt_session *session)
136 {
137 int ret, kdata_port, udata_port;
138 struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr;
139 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
140
141 LTTNG_ASSERT(session);
142 LTTNG_ASSERT(dst);
143
144 memset(tmp_urls, 0, sizeof(tmp_urls));
145 memset(tmp_uurl, 0, sizeof(tmp_uurl));
146
147 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
148
149 if (session->kernel_session && session->kernel_session->consumer) {
150 kuri = &session->kernel_session->consumer->dst.net.control;
151 kdata_port = session->kernel_session->consumer->dst.net.data.port;
152 }
153
154 if (session->ust_session && session->ust_session->consumer) {
155 uuri = &session->ust_session->consumer->dst.net.control;
156 udata_port = session->ust_session->consumer->dst.net.data.port;
157 }
158
159 if (uuri == nullptr && kuri == nullptr) {
160 uri = &session->consumer->dst.net.control;
161 kdata_port = session->consumer->dst.net.data.port;
162 } else if (kuri && uuri) {
163 ret = uri_compare(kuri, uuri);
164 if (ret) {
165 /* Not Equal */
166 uri = kuri;
167 /* Build uuri URL string */
168 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
169 if (ret < 0) {
170 goto error;
171 }
172 } else {
173 uri = kuri;
174 }
175 } else if (kuri && uuri == nullptr) {
176 uri = kuri;
177 } else if (uuri && kuri == nullptr) {
178 uri = uuri;
179 }
180
181 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
182 if (ret < 0) {
183 goto error;
184 }
185
186 /*
187 * Do we have a UST url set. If yes, this means we have both kernel and UST
188 * to print.
189 */
190 if (*tmp_uurl != '\0') {
191 ret = snprintf(dst,
192 size,
193 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
194 tmp_urls,
195 kdata_port,
196 tmp_uurl,
197 udata_port);
198 } else {
199 int dport;
200 if (kuri || (!kuri && !uuri)) {
201 dport = kdata_port;
202 } else {
203 /* No kernel URI, use the UST port. */
204 dport = udata_port;
205 }
206 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
207 }
208
209 error:
210 return ret;
211 }
212
213 /*
214 * Get run-time attributes if the session has been started (discarded events,
215 * lost packets).
216 */
217 static int get_kernel_runtime_stats(struct ltt_session *session,
218 struct ltt_kernel_channel *kchan,
219 uint64_t *discarded_events,
220 uint64_t *lost_packets)
221 {
222 int ret;
223
224 if (!session->has_been_started) {
225 ret = 0;
226 *discarded_events = 0;
227 *lost_packets = 0;
228 goto end;
229 }
230
231 ret = consumer_get_discarded_events(
232 session->id, kchan->key, session->kernel_session->consumer, discarded_events);
233 if (ret < 0) {
234 goto end;
235 }
236
237 ret = consumer_get_lost_packets(
238 session->id, kchan->key, session->kernel_session->consumer, lost_packets);
239 if (ret < 0) {
240 goto end;
241 }
242
243 end:
244 return ret;
245 }
246
247 /*
248 * Get run-time attributes if the session has been started (discarded events,
249 * lost packets).
250 */
251 static int get_ust_runtime_stats(struct ltt_session *session,
252 struct ltt_ust_channel *uchan,
253 uint64_t *discarded_events,
254 uint64_t *lost_packets)
255 {
256 int ret;
257 struct ltt_ust_session *usess;
258
259 if (!discarded_events || !lost_packets) {
260 ret = -1;
261 goto end;
262 }
263
264 usess = session->ust_session;
265 LTTNG_ASSERT(discarded_events);
266 LTTNG_ASSERT(lost_packets);
267
268 if (!usess || !session->has_been_started) {
269 *discarded_events = 0;
270 *lost_packets = 0;
271 ret = 0;
272 goto end;
273 }
274
275 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
276 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
277 &usess->buffer_reg_uid_list,
278 usess->consumer,
279 uchan->id,
280 uchan->attr.overwrite,
281 discarded_events,
282 lost_packets);
283 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
284 ret = ust_app_pid_get_channel_runtime_stats(usess,
285 uchan,
286 usess->consumer,
287 uchan->attr.overwrite,
288 discarded_events,
289 lost_packets);
290 if (ret < 0) {
291 goto end;
292 }
293 *discarded_events += uchan->per_pid_closed_app_discarded;
294 *lost_packets += uchan->per_pid_closed_app_lost;
295 } else {
296 ERR("Unsupported buffer type");
297 abort();
298 ret = -1;
299 goto end;
300 }
301
302 end:
303 return ret;
304 }
305
306 /*
307 * Create a list of agent domain events.
308 *
309 * Return number of events in list on success or else a negative value.
310 */
311 static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
312 struct lttng_payload *reply_payload,
313 unsigned int *nb_events)
314 {
315 enum lttng_error_code ret_code;
316 int ret = 0;
317 unsigned int local_nb_events = 0;
318 struct agent_event *event;
319 struct lttng_ht_iter iter;
320 unsigned long agent_event_count;
321
322 assert(agt);
323 assert(reply_payload);
324
325 DBG3("Listing agent events");
326
327 agent_event_count = lttng_ht_get_count(agt->events);
328 if (agent_event_count == 0) {
329 /* Early exit. */
330 goto end;
331 }
332
333 if (agent_event_count > UINT_MAX) {
334 ret_code = LTTNG_ERR_OVERFLOW;
335 goto error;
336 }
337
338 local_nb_events = (unsigned int) agent_event_count;
339
340 {
341 lttng::urcu::read_lock_guard read_lock;
342
343 cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) {
344 struct lttng_event *tmp_event = lttng_event_create();
345
346 if (!tmp_event) {
347 ret_code = LTTNG_ERR_NOMEM;
348 goto error;
349 }
350
351 if (lttng_strncpy(tmp_event->name, event->name, sizeof(tmp_event->name))) {
352 lttng_event_destroy(tmp_event);
353 ret_code = LTTNG_ERR_FATAL;
354 goto error;
355 }
356
357 tmp_event->name[sizeof(tmp_event->name) - 1] = '\0';
358 tmp_event->enabled = !!event->enabled_count;
359 tmp_event->loglevel = event->loglevel_value;
360 tmp_event->loglevel_type = event->loglevel_type;
361
362 ret = lttng_event_serialize(tmp_event,
363 0,
364 nullptr,
365 event->filter_expression,
366 0,
367 nullptr,
368 reply_payload);
369 lttng_event_destroy(tmp_event);
370 if (ret) {
371 ret_code = LTTNG_ERR_FATAL;
372 goto error;
373 }
374 }
375 }
376 end:
377 ret_code = LTTNG_OK;
378 *nb_events = local_nb_events;
379 error:
380 return ret_code;
381 }
382
383 /*
384 * Create a list of ust global domain events.
385 */
386 static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
387 struct ltt_ust_domain_global *ust_global,
388 struct lttng_payload *reply_payload,
389 unsigned int *nb_events)
390 {
391 enum lttng_error_code ret_code;
392 int ret;
393 struct lttng_ht_iter iter;
394 struct lttng_ht_node_str *node;
395 struct ltt_ust_channel *uchan;
396 struct ltt_ust_event *uevent;
397 unsigned long channel_event_count;
398 unsigned int local_nb_events = 0;
399
400 assert(reply_payload);
401 assert(nb_events);
402
403 DBG("Listing UST global events for channel %s", channel_name);
404
405 lttng::urcu::read_lock_guard read_lock;
406
407 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
408 node = lttng_ht_iter_get_node_str(&iter);
409 if (node == nullptr) {
410 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
411 goto error;
412 }
413
414 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
415
416 channel_event_count = lttng_ht_get_count(uchan->events);
417 if (channel_event_count == 0) {
418 /* Early exit. */
419 ret_code = LTTNG_OK;
420 goto end;
421 }
422
423 if (channel_event_count > UINT_MAX) {
424 ret_code = LTTNG_ERR_OVERFLOW;
425 goto error;
426 }
427
428 local_nb_events = (unsigned int) channel_event_count;
429
430 DBG3("Listing UST global %d events", *nb_events);
431
432 cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) {
433 struct lttng_event *tmp_event = nullptr;
434
435 if (uevent->internal) {
436 /* This event should remain hidden from clients */
437 local_nb_events--;
438 continue;
439 }
440
441 tmp_event = lttng_event_create();
442 if (!tmp_event) {
443 ret_code = LTTNG_ERR_NOMEM;
444 goto error;
445 }
446
447 if (lttng_strncpy(tmp_event->name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN)) {
448 ret_code = LTTNG_ERR_FATAL;
449 lttng_event_destroy(tmp_event);
450 goto error;
451 }
452
453 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
454 tmp_event->enabled = uevent->enabled;
455
456 switch (uevent->attr.instrumentation) {
457 case LTTNG_UST_ABI_TRACEPOINT:
458 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
459 break;
460 case LTTNG_UST_ABI_PROBE:
461 tmp_event->type = LTTNG_EVENT_PROBE;
462 break;
463 case LTTNG_UST_ABI_FUNCTION:
464 tmp_event->type = LTTNG_EVENT_FUNCTION;
465 break;
466 }
467
468 tmp_event->loglevel = uevent->attr.loglevel;
469 switch (uevent->attr.loglevel_type) {
470 case LTTNG_UST_ABI_LOGLEVEL_ALL:
471 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
472 break;
473 case LTTNG_UST_ABI_LOGLEVEL_RANGE:
474 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
475 break;
476 case LTTNG_UST_ABI_LOGLEVEL_SINGLE:
477 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
478 break;
479 }
480 if (uevent->filter) {
481 tmp_event->filter = 1;
482 }
483 if (uevent->exclusion) {
484 tmp_event->exclusion = 1;
485 }
486
487 std::vector<const char *> exclusion_names;
488 if (uevent->exclusion) {
489 for (int i = 0; i < uevent->exclusion->count; i++) {
490 exclusion_names.emplace_back(
491 LTTNG_EVENT_EXCLUSION_NAME_AT(uevent->exclusion, i));
492 }
493 }
494
495 /*
496 * We do not care about the filter bytecode and the fd from the
497 * userspace_probe_location.
498 */
499 ret = lttng_event_serialize(tmp_event,
500 exclusion_names.size(),
501 exclusion_names.size() ? exclusion_names.data() :
502 nullptr,
503 uevent->filter_expression,
504 0,
505 nullptr,
506 reply_payload);
507 lttng_event_destroy(tmp_event);
508 if (ret) {
509 ret_code = LTTNG_ERR_FATAL;
510 goto error;
511 }
512 }
513
514 end:
515 /* nb_events is already set at this point. */
516 ret_code = LTTNG_OK;
517 *nb_events = local_nb_events;
518 error:
519 return ret_code;
520 }
521
522 /*
523 * Fill lttng_event array of all kernel events in the channel.
524 */
525 static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
526 struct ltt_kernel_session *kernel_session,
527 struct lttng_payload *reply_payload,
528 unsigned int *nb_events)
529 {
530 enum lttng_error_code ret_code;
531 int ret;
532 struct ltt_kernel_event *event;
533 struct ltt_kernel_channel *kchan;
534
535 assert(reply_payload);
536
537 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
538 if (kchan == nullptr) {
539 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
540 goto end;
541 }
542
543 *nb_events = kchan->event_count;
544
545 DBG("Listing events for channel %s", kchan->channel->name);
546
547 if (*nb_events == 0) {
548 ret_code = LTTNG_OK;
549 goto end;
550 }
551
552 /* Kernel channels */
553 cds_list_for_each_entry (event, &kchan->events_list.head, list) {
554 struct lttng_event *tmp_event = lttng_event_create();
555
556 if (!tmp_event) {
557 ret_code = LTTNG_ERR_NOMEM;
558 goto end;
559 }
560
561 if (lttng_strncpy(tmp_event->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) {
562 lttng_event_destroy(tmp_event);
563 ret_code = LTTNG_ERR_FATAL;
564 goto end;
565 }
566
567 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
568 tmp_event->enabled = event->enabled;
569 tmp_event->filter = (unsigned char) !!event->filter_expression;
570
571 switch (event->event->instrumentation) {
572 case LTTNG_KERNEL_ABI_TRACEPOINT:
573 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
574 break;
575 case LTTNG_KERNEL_ABI_KRETPROBE:
576 tmp_event->type = LTTNG_EVENT_FUNCTION;
577 memcpy(&tmp_event->attr.probe,
578 &event->event->u.kprobe,
579 sizeof(struct lttng_kernel_abi_kprobe));
580 break;
581 case LTTNG_KERNEL_ABI_KPROBE:
582 tmp_event->type = LTTNG_EVENT_PROBE;
583 memcpy(&tmp_event->attr.probe,
584 &event->event->u.kprobe,
585 sizeof(struct lttng_kernel_abi_kprobe));
586 break;
587 case LTTNG_KERNEL_ABI_UPROBE:
588 tmp_event->type = LTTNG_EVENT_USERSPACE_PROBE;
589 break;
590 case LTTNG_KERNEL_ABI_FUNCTION:
591 tmp_event->type = LTTNG_EVENT_FUNCTION;
592 memcpy(&(tmp_event->attr.ftrace),
593 &event->event->u.ftrace,
594 sizeof(struct lttng_kernel_abi_function));
595 break;
596 case LTTNG_KERNEL_ABI_NOOP:
597 tmp_event->type = LTTNG_EVENT_NOOP;
598 break;
599 case LTTNG_KERNEL_ABI_SYSCALL:
600 tmp_event->type = LTTNG_EVENT_SYSCALL;
601 break;
602 case LTTNG_KERNEL_ABI_ALL:
603 /* fall-through. */
604 default:
605 abort();
606 break;
607 }
608
609 if (event->userspace_probe_location) {
610 struct lttng_userspace_probe_location *location_copy =
611 lttng_userspace_probe_location_copy(
612 event->userspace_probe_location);
613
614 if (!location_copy) {
615 lttng_event_destroy(tmp_event);
616 ret_code = LTTNG_ERR_NOMEM;
617 goto end;
618 }
619
620 ret = lttng_event_set_userspace_probe_location(tmp_event, location_copy);
621 if (ret) {
622 lttng_event_destroy(tmp_event);
623 lttng_userspace_probe_location_destroy(location_copy);
624 ret_code = LTTNG_ERR_INVALID;
625 goto end;
626 }
627 }
628
629 ret = lttng_event_serialize(
630 tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload);
631 lttng_event_destroy(tmp_event);
632 if (ret) {
633 ret_code = LTTNG_ERR_FATAL;
634 goto end;
635 }
636 }
637
638 ret_code = LTTNG_OK;
639 end:
640 return ret_code;
641 }
642
643 /*
644 * Add URI so the consumer output object. Set the correct path depending on the
645 * domain adding the default trace directory.
646 */
647 static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session,
648 struct consumer_output *consumer,
649 struct lttng_uri *uri,
650 enum lttng_domain_type domain)
651 {
652 int ret;
653 enum lttng_error_code ret_code = LTTNG_OK;
654
655 LTTNG_ASSERT(uri);
656
657 if (consumer == nullptr) {
658 DBG("No consumer detected. Don't add URI. Stopping.");
659 ret_code = LTTNG_ERR_NO_CONSUMER;
660 goto error;
661 }
662
663 switch (domain) {
664 case LTTNG_DOMAIN_KERNEL:
665 ret = lttng_strncpy(consumer->domain_subdir,
666 DEFAULT_KERNEL_TRACE_DIR,
667 sizeof(consumer->domain_subdir));
668 break;
669 case LTTNG_DOMAIN_UST:
670 ret = lttng_strncpy(consumer->domain_subdir,
671 DEFAULT_UST_TRACE_DIR,
672 sizeof(consumer->domain_subdir));
673 break;
674 default:
675 /*
676 * This case is possible is we try to add the URI to the global
677 * tracing session consumer object which in this case there is
678 * no subdir.
679 */
680 memset(consumer->domain_subdir, 0, sizeof(consumer->domain_subdir));
681 ret = 0;
682 }
683 if (ret) {
684 ERR("Failed to initialize consumer output domain subdirectory");
685 ret_code = LTTNG_ERR_FATAL;
686 goto error;
687 }
688
689 switch (uri->dtype) {
690 case LTTNG_DST_IPV4:
691 case LTTNG_DST_IPV6:
692 DBG2("Setting network URI to consumer");
693
694 if (consumer->type == CONSUMER_DST_NET) {
695 if ((uri->stype == LTTNG_STREAM_CONTROL &&
696 consumer->dst.net.control_isset) ||
697 (uri->stype == LTTNG_STREAM_DATA && consumer->dst.net.data_isset)) {
698 ret_code = LTTNG_ERR_URL_EXIST;
699 goto error;
700 }
701 } else {
702 memset(&consumer->dst, 0, sizeof(consumer->dst));
703 }
704
705 /* Set URI into consumer output object */
706 ret = consumer_set_network_uri(session, consumer, uri);
707 if (ret < 0) {
708 ret_code = (lttng_error_code) -ret;
709 goto error;
710 } else if (ret == 1) {
711 /*
712 * URI was the same in the consumer so we do not append the subdir
713 * again so to not duplicate output dir.
714 */
715 ret_code = LTTNG_OK;
716 goto error;
717 }
718 break;
719 case LTTNG_DST_PATH:
720 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
721 ret_code = LTTNG_ERR_INVALID;
722 goto error;
723 }
724 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
725 memset(&consumer->dst, 0, sizeof(consumer->dst));
726
727 ret = lttng_strncpy(consumer->dst.session_root_path,
728 uri->dst.path,
729 sizeof(consumer->dst.session_root_path));
730 if (ret) {
731 ret_code = LTTNG_ERR_FATAL;
732 goto error;
733 }
734 consumer->type = CONSUMER_DST_LOCAL;
735 break;
736 }
737
738 ret_code = LTTNG_OK;
739 error:
740 return ret_code;
741 }
742
743 /*
744 * Init tracing by creating trace directory and sending fds kernel consumer.
745 */
746 static int init_kernel_tracing(struct ltt_kernel_session *session)
747 {
748 int ret = 0;
749 struct lttng_ht_iter iter;
750 struct consumer_socket *socket;
751
752 LTTNG_ASSERT(session);
753
754 if (session->consumer_fds_sent == 0 && session->consumer != nullptr) {
755 lttng::urcu::read_lock_guard read_lock;
756
757 cds_lfht_for_each_entry (
758 session->consumer->socks->ht, &iter.iter, socket, node.node) {
759 pthread_mutex_lock(socket->lock);
760 ret = kernel_consumer_send_session(socket, session);
761 pthread_mutex_unlock(socket->lock);
762 if (ret < 0) {
763 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
764 goto error;
765 }
766 }
767 }
768
769 error:
770 return ret;
771 }
772
773 /*
774 * Create a socket to the relayd using the URI.
775 *
776 * On success, the relayd_sock pointer is set to the created socket.
777 * Else, it remains untouched and an LTTng error code is returned.
778 */
779 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
780 struct lttcomm_relayd_sock **relayd_sock,
781 struct consumer_output *consumer)
782 {
783 int ret;
784 enum lttng_error_code status = LTTNG_OK;
785 struct lttcomm_relayd_sock *rsock;
786
787 rsock = lttcomm_alloc_relayd_sock(
788 uri, RELAYD_VERSION_COMM_MAJOR, RELAYD_VERSION_COMM_MINOR);
789 if (!rsock) {
790 status = LTTNG_ERR_FATAL;
791 goto error;
792 }
793
794 /*
795 * Connect to relayd so we can proceed with a session creation. This call
796 * can possibly block for an arbitrary amount of time to set the health
797 * state to be in poll execution.
798 */
799 health_poll_entry();
800 ret = relayd_connect(rsock);
801 health_poll_exit();
802 if (ret < 0) {
803 ERR("Unable to reach lttng-relayd");
804 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
805 goto free_sock;
806 }
807
808 /* Create socket for control stream. */
809 if (uri->stype == LTTNG_STREAM_CONTROL) {
810 uint64_t result_flags;
811
812 DBG3("Creating relayd stream socket from URI");
813
814 /* Check relayd version */
815 ret = relayd_version_check(rsock);
816 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
817 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
818 goto close_sock;
819 } else if (ret < 0) {
820 ERR("Unable to reach lttng-relayd");
821 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
822 goto close_sock;
823 }
824 consumer->relay_major_version = rsock->major;
825 consumer->relay_minor_version = rsock->minor;
826 ret = relayd_get_configuration(rsock, 0, &result_flags);
827 if (ret < 0) {
828 ERR("Unable to get relayd configuration");
829 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
830 goto close_sock;
831 }
832 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
833 consumer->relay_allows_clear = true;
834 }
835 } else if (uri->stype == LTTNG_STREAM_DATA) {
836 DBG3("Creating relayd data socket from URI");
837 } else {
838 /* Command is not valid */
839 ERR("Relayd invalid stream type: %d", uri->stype);
840 status = LTTNG_ERR_INVALID;
841 goto close_sock;
842 }
843
844 *relayd_sock = rsock;
845
846 return status;
847
848 close_sock:
849 /* The returned value is not useful since we are on an error path. */
850 (void) relayd_close(rsock);
851 free_sock:
852 free(rsock);
853 error:
854 return status;
855 }
856
857 /*
858 * Connect to the relayd using URI and send the socket to the right consumer.
859 *
860 * The consumer socket lock must be held by the caller.
861 *
862 * Returns LTTNG_OK on success or an LTTng error code on failure.
863 */
864 static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id,
865 struct lttng_uri *relayd_uri,
866 struct consumer_output *consumer,
867 struct consumer_socket *consumer_sock,
868 const char *session_name,
869 const char *hostname,
870 const char *base_path,
871 int session_live_timer,
872 const uint64_t *current_chunk_id,
873 time_t session_creation_time,
874 bool session_name_contains_creation_time)
875 {
876 int ret;
877 struct lttcomm_relayd_sock *rsock = nullptr;
878 enum lttng_error_code status;
879
880 /* Connect to relayd and make version check if uri is the control. */
881 status = create_connect_relayd(relayd_uri, &rsock, consumer);
882 if (status != LTTNG_OK) {
883 goto relayd_comm_error;
884 }
885 LTTNG_ASSERT(rsock);
886
887 /* Set the network sequence index if not set. */
888 if (consumer->net_seq_index == (uint64_t) -1ULL) {
889 pthread_mutex_lock(&relayd_net_seq_idx_lock);
890 /*
891 * Increment net_seq_idx because we are about to transfer the
892 * new relayd socket to the consumer.
893 * Assign unique key so the consumer can match streams.
894 */
895 consumer->net_seq_index = ++relayd_net_seq_idx;
896 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
897 }
898
899 /* Send relayd socket to consumer. */
900 ret = consumer_send_relayd_socket(consumer_sock,
901 rsock,
902 consumer,
903 relayd_uri->stype,
904 session_id,
905 session_name,
906 hostname,
907 base_path,
908 session_live_timer,
909 current_chunk_id,
910 session_creation_time,
911 session_name_contains_creation_time);
912 if (ret < 0) {
913 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
914 goto close_sock;
915 }
916
917 /* Flag that the corresponding socket was sent. */
918 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
919 consumer_sock->control_sock_sent = 1;
920 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
921 consumer_sock->data_sock_sent = 1;
922 }
923
924 /*
925 * Close socket which was dup on the consumer side. The session daemon does
926 * NOT keep track of the relayd socket(s) once transfer to the consumer.
927 */
928
929 close_sock:
930 if (status != LTTNG_OK) {
931 /*
932 * The consumer output for this session should not be used anymore
933 * since the relayd connection failed thus making any tracing or/and
934 * streaming not usable.
935 */
936 consumer->enabled = false;
937 }
938 (void) relayd_close(rsock);
939 free(rsock);
940
941 relayd_comm_error:
942 return status;
943 }
944
945 /*
946 * Send both relayd sockets to a specific consumer and domain. This is a
947 * helper function to facilitate sending the information to the consumer for a
948 * session.
949 *
950 * The consumer socket lock must be held by the caller.
951 *
952 * Returns LTTNG_OK, or an LTTng error code on failure.
953 */
954 static enum lttng_error_code send_consumer_relayd_sockets(unsigned int session_id,
955 struct consumer_output *consumer,
956 struct consumer_socket *sock,
957 const char *session_name,
958 const char *hostname,
959 const char *base_path,
960 int session_live_timer,
961 const uint64_t *current_chunk_id,
962 time_t session_creation_time,
963 bool session_name_contains_creation_time)
964 {
965 enum lttng_error_code status = LTTNG_OK;
966
967 LTTNG_ASSERT(consumer);
968 LTTNG_ASSERT(sock);
969
970 /* Sending control relayd socket. */
971 if (!sock->control_sock_sent) {
972 status = send_consumer_relayd_socket(session_id,
973 &consumer->dst.net.control,
974 consumer,
975 sock,
976 session_name,
977 hostname,
978 base_path,
979 session_live_timer,
980 current_chunk_id,
981 session_creation_time,
982 session_name_contains_creation_time);
983 if (status != LTTNG_OK) {
984 goto error;
985 }
986 }
987
988 /* Sending data relayd socket. */
989 if (!sock->data_sock_sent) {
990 status = send_consumer_relayd_socket(session_id,
991 &consumer->dst.net.data,
992 consumer,
993 sock,
994 session_name,
995 hostname,
996 base_path,
997 session_live_timer,
998 current_chunk_id,
999 session_creation_time,
1000 session_name_contains_creation_time);
1001 if (status != LTTNG_OK) {
1002 goto error;
1003 }
1004 }
1005
1006 error:
1007 return status;
1008 }
1009
1010 /*
1011 * Setup relayd connections for a tracing session. First creates the socket to
1012 * the relayd and send them to the right domain consumer. Consumer type MUST be
1013 * network.
1014 */
1015 int cmd_setup_relayd(struct ltt_session *session)
1016 {
1017 int ret = LTTNG_OK;
1018 struct ltt_ust_session *usess;
1019 struct ltt_kernel_session *ksess;
1020 struct consumer_socket *socket;
1021 struct lttng_ht_iter iter;
1022 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1023
1024 LTTNG_ASSERT(session);
1025
1026 usess = session->ust_session;
1027 ksess = session->kernel_session;
1028
1029 DBG("Setting relayd for session %s", session->name);
1030
1031 if (session->current_trace_chunk) {
1032 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1033 session->current_trace_chunk, &current_chunk_id.value);
1034
1035 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1036 current_chunk_id.is_set = true;
1037 } else {
1038 ERR("Failed to get current trace chunk id");
1039 ret = LTTNG_ERR_UNK;
1040 goto error;
1041 }
1042 }
1043
1044 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET &&
1045 usess->consumer->enabled) {
1046 /* For each consumer socket, send relayd sockets */
1047 lttng::urcu::read_lock_guard read_lock;
1048
1049 cds_lfht_for_each_entry (
1050 usess->consumer->socks->ht, &iter.iter, socket, node.node) {
1051 pthread_mutex_lock(socket->lock);
1052 ret = send_consumer_relayd_sockets(
1053 session->id,
1054 usess->consumer,
1055 socket,
1056 session->name,
1057 session->hostname,
1058 session->base_path,
1059 session->live_timer,
1060 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
1061 session->creation_time,
1062 session->name_contains_creation_time);
1063 pthread_mutex_unlock(socket->lock);
1064 if (ret != LTTNG_OK) {
1065 goto error;
1066 }
1067 /* Session is now ready for network streaming. */
1068 session->net_handle = 1;
1069 }
1070
1071 session->consumer->relay_major_version = usess->consumer->relay_major_version;
1072 session->consumer->relay_minor_version = usess->consumer->relay_minor_version;
1073 session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear;
1074 }
1075
1076 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET &&
1077 ksess->consumer->enabled) {
1078 lttng::urcu::read_lock_guard read_lock;
1079
1080 cds_lfht_for_each_entry (
1081 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
1082 pthread_mutex_lock(socket->lock);
1083 ret = send_consumer_relayd_sockets(
1084 session->id,
1085 ksess->consumer,
1086 socket,
1087 session->name,
1088 session->hostname,
1089 session->base_path,
1090 session->live_timer,
1091 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
1092 session->creation_time,
1093 session->name_contains_creation_time);
1094 pthread_mutex_unlock(socket->lock);
1095 if (ret != LTTNG_OK) {
1096 goto error;
1097 }
1098 /* Session is now ready for network streaming. */
1099 session->net_handle = 1;
1100 }
1101
1102 session->consumer->relay_major_version = ksess->consumer->relay_major_version;
1103 session->consumer->relay_minor_version = ksess->consumer->relay_minor_version;
1104 session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear;
1105 }
1106
1107 error:
1108 return ret;
1109 }
1110
1111 /*
1112 * Start a kernel session by opening all necessary streams.
1113 */
1114 int start_kernel_session(struct ltt_kernel_session *ksess)
1115 {
1116 int ret;
1117 struct ltt_kernel_channel *kchan;
1118
1119 /* Open kernel metadata */
1120 if (ksess->metadata == nullptr && ksess->output_traces) {
1121 ret = kernel_open_metadata(ksess);
1122 if (ret < 0) {
1123 ret = LTTNG_ERR_KERN_META_FAIL;
1124 goto error;
1125 }
1126 }
1127
1128 /* Open kernel metadata stream */
1129 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1130 ret = kernel_open_metadata_stream(ksess);
1131 if (ret < 0) {
1132 ERR("Kernel create metadata stream failed");
1133 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1134 goto error;
1135 }
1136 }
1137
1138 /* For each channel */
1139 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
1140 if (kchan->stream_count == 0) {
1141 ret = kernel_open_channel_stream(kchan);
1142 if (ret < 0) {
1143 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1144 goto error;
1145 }
1146 /* Update the stream global counter */
1147 ksess->stream_count_global += ret;
1148 }
1149 }
1150
1151 /* Setup kernel consumer socket and send fds to it */
1152 ret = init_kernel_tracing(ksess);
1153 if (ret != 0) {
1154 ret = LTTNG_ERR_KERN_START_FAIL;
1155 goto error;
1156 }
1157
1158 /* This start the kernel tracing */
1159 ret = kernel_start_session(ksess);
1160 if (ret < 0) {
1161 ret = LTTNG_ERR_KERN_START_FAIL;
1162 goto error;
1163 }
1164
1165 /* Quiescent wait after starting trace */
1166 kernel_wait_quiescent();
1167
1168 ksess->active = true;
1169
1170 ret = LTTNG_OK;
1171
1172 error:
1173 return ret;
1174 }
1175
1176 int stop_kernel_session(struct ltt_kernel_session *ksess)
1177 {
1178 struct ltt_kernel_channel *kchan;
1179 bool error_occurred = false;
1180 int ret;
1181
1182 if (!ksess || !ksess->active) {
1183 return LTTNG_OK;
1184 }
1185 DBG("Stopping kernel tracing");
1186
1187 ret = kernel_stop_session(ksess);
1188 if (ret < 0) {
1189 ret = LTTNG_ERR_KERN_STOP_FAIL;
1190 goto error;
1191 }
1192
1193 kernel_wait_quiescent();
1194
1195 /* Flush metadata after stopping (if exists) */
1196 if (ksess->metadata_stream_fd >= 0) {
1197 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1198 if (ret < 0) {
1199 ERR("Kernel metadata flush failed");
1200 error_occurred = true;
1201 }
1202 }
1203
1204 /* Flush all buffers after stopping */
1205 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
1206 ret = kernel_flush_buffer(kchan);
1207 if (ret < 0) {
1208 ERR("Kernel flush buffer error");
1209 error_occurred = true;
1210 }
1211 }
1212
1213 ksess->active = false;
1214 if (error_occurred) {
1215 ret = LTTNG_ERR_UNK;
1216 } else {
1217 ret = LTTNG_OK;
1218 }
1219 error:
1220 return ret;
1221 }
1222
1223 /*
1224 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1225 */
1226 int cmd_disable_channel(struct ltt_session *session,
1227 enum lttng_domain_type domain,
1228 char *channel_name)
1229 {
1230 int ret;
1231 struct ltt_ust_session *usess;
1232
1233 usess = session->ust_session;
1234
1235 lttng::urcu::read_lock_guard read_lock;
1236
1237 switch (domain) {
1238 case LTTNG_DOMAIN_KERNEL:
1239 {
1240 ret = channel_kernel_disable(session->kernel_session, channel_name);
1241 if (ret != LTTNG_OK) {
1242 goto error;
1243 }
1244
1245 kernel_wait_quiescent();
1246 break;
1247 }
1248 case LTTNG_DOMAIN_UST:
1249 {
1250 struct ltt_ust_channel *uchan;
1251 struct lttng_ht *chan_ht;
1252
1253 chan_ht = usess->domain_global.channels;
1254
1255 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1256 if (uchan == nullptr) {
1257 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1258 goto error;
1259 }
1260
1261 ret = channel_ust_disable(usess, uchan);
1262 if (ret != LTTNG_OK) {
1263 goto error;
1264 }
1265 break;
1266 }
1267 default:
1268 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1269 goto error;
1270 }
1271
1272 ret = LTTNG_OK;
1273
1274 error:
1275 return ret;
1276 }
1277
1278 /*
1279 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1280 *
1281 * The wpipe arguments is used as a notifier for the kernel thread.
1282 */
1283 int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
1284 {
1285 int ret;
1286 size_t channel_len;
1287 ssize_t sock_recv_len;
1288 struct lttng_channel *channel = nullptr;
1289 struct lttng_buffer_view view;
1290 struct lttng_dynamic_buffer channel_buffer;
1291 const struct lttng_domain command_domain = cmd_ctx->lsm.domain;
1292
1293 lttng_dynamic_buffer_init(&channel_buffer);
1294 channel_len = (size_t) cmd_ctx->lsm.u.channel.length;
1295 ret = lttng_dynamic_buffer_set_size(&channel_buffer, channel_len);
1296 if (ret) {
1297 ret = LTTNG_ERR_NOMEM;
1298 goto end;
1299 }
1300
1301 sock_recv_len = lttcomm_recv_unix_sock(sock, channel_buffer.data, channel_len);
1302 if (sock_recv_len < 0 || sock_recv_len != channel_len) {
1303 ERR("Failed to receive \"enable channel\" command payload");
1304 ret = LTTNG_ERR_INVALID;
1305 goto end;
1306 }
1307
1308 view = lttng_buffer_view_from_dynamic_buffer(&channel_buffer, 0, channel_len);
1309 if (!lttng_buffer_view_is_valid(&view)) {
1310 ret = LTTNG_ERR_INVALID;
1311 goto end;
1312 }
1313
1314 if (lttng_channel_create_from_buffer(&view, &channel) != channel_len) {
1315 ERR("Invalid channel payload received in \"enable channel\" command");
1316 ret = LTTNG_ERR_INVALID;
1317 goto end;
1318 }
1319
1320 ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe);
1321
1322 end:
1323 lttng_dynamic_buffer_reset(&channel_buffer);
1324 lttng_channel_destroy(channel);
1325 return ret;
1326 }
1327
1328 static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
1329 const struct lttng_domain *domain,
1330 const struct lttng_channel *_attr,
1331 int wpipe)
1332 {
1333 enum lttng_error_code ret_code;
1334 struct ltt_ust_session *usess = session->ust_session;
1335 struct lttng_ht *chan_ht;
1336 size_t len;
1337 struct lttng_channel *attr = nullptr;
1338
1339 LTTNG_ASSERT(session);
1340 LTTNG_ASSERT(_attr);
1341 LTTNG_ASSERT(domain);
1342
1343 lttng::urcu::read_lock_guard read_lock;
1344
1345 attr = lttng_channel_copy(_attr);
1346 if (!attr) {
1347 ret_code = LTTNG_ERR_NOMEM;
1348 goto end;
1349 }
1350
1351 len = lttng_strnlen(attr->name, sizeof(attr->name));
1352
1353 /* Validate channel name */
1354 if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) {
1355 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1356 goto end;
1357 }
1358
1359 DBG("Enabling channel %s for session %s", attr->name, session->name);
1360
1361 /*
1362 * If the session is a live session, remove the switch timer, the
1363 * live timer does the same thing but sends also synchronisation
1364 * beacons for inactive streams.
1365 */
1366 if (session->live_timer > 0) {
1367 attr->attr.live_timer_interval = session->live_timer;
1368 attr->attr.switch_timer_interval = 0;
1369 }
1370
1371 /* Check for feature support */
1372 switch (domain->type) {
1373 case LTTNG_DOMAIN_KERNEL:
1374 {
1375 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1376 /* Sampling position of buffer is not supported */
1377 WARN("Kernel tracer does not support buffer monitoring. "
1378 "Setting the monitor interval timer to 0 "
1379 "(disabled) for channel '%s' of session '%s'",
1380 attr->name,
1381 session->name);
1382 lttng_channel_set_monitor_timer_interval(attr, 0);
1383 }
1384 break;
1385 }
1386 case LTTNG_DOMAIN_UST:
1387 break;
1388 case LTTNG_DOMAIN_JUL:
1389 case LTTNG_DOMAIN_LOG4J:
1390 case LTTNG_DOMAIN_PYTHON:
1391 if (!agent_tracing_is_enabled()) {
1392 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1393 ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED;
1394 goto error;
1395 }
1396 break;
1397 default:
1398 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
1399 goto error;
1400 }
1401
1402 switch (domain->type) {
1403 case LTTNG_DOMAIN_KERNEL:
1404 {
1405 struct ltt_kernel_channel *kchan;
1406
1407 kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session);
1408 if (kchan == nullptr) {
1409 /*
1410 * Don't try to create a channel if the session has been started at
1411 * some point in time before. The tracer does not allow it.
1412 */
1413 if (session->has_been_started) {
1414 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
1415 goto error;
1416 }
1417
1418 if (session->snapshot.nb_output > 0 || session->snapshot_mode) {
1419 /* Enforce mmap output for snapshot sessions. */
1420 attr->attr.output = LTTNG_EVENT_MMAP;
1421 }
1422 ret_code = channel_kernel_create(session->kernel_session, attr, wpipe);
1423 if (attr->name[0] != '\0') {
1424 session->kernel_session->has_non_default_channel = 1;
1425 }
1426 } else {
1427 ret_code = channel_kernel_enable(session->kernel_session, kchan);
1428 }
1429
1430 if (ret_code != LTTNG_OK) {
1431 goto error;
1432 }
1433
1434 kernel_wait_quiescent();
1435 break;
1436 }
1437 case LTTNG_DOMAIN_UST:
1438 case LTTNG_DOMAIN_JUL:
1439 case LTTNG_DOMAIN_LOG4J:
1440 case LTTNG_DOMAIN_PYTHON:
1441 {
1442 struct ltt_ust_channel *uchan;
1443
1444 /*
1445 * FIXME
1446 *
1447 * Current agent implementation limitations force us to allow
1448 * only one channel at once in "agent" subdomains. Each
1449 * subdomain has a default channel name which must be strictly
1450 * adhered to.
1451 */
1452 if (domain->type == LTTNG_DOMAIN_JUL) {
1453 if (strncmp(attr->name,
1454 DEFAULT_JUL_CHANNEL_NAME,
1455 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1456 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1457 goto error;
1458 }
1459 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1460 if (strncmp(attr->name,
1461 DEFAULT_LOG4J_CHANNEL_NAME,
1462 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1463 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1464 goto error;
1465 }
1466 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1467 if (strncmp(attr->name,
1468 DEFAULT_PYTHON_CHANNEL_NAME,
1469 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1470 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1471 goto error;
1472 }
1473 }
1474
1475 chan_ht = usess->domain_global.channels;
1476
1477 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1478 if (uchan == nullptr) {
1479 /*
1480 * Don't try to create a channel if the session has been started at
1481 * some point in time before. The tracer does not allow it.
1482 */
1483 if (session->has_been_started) {
1484 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
1485 goto error;
1486 }
1487
1488 ret_code = channel_ust_create(usess, attr, domain->buf_type);
1489 if (attr->name[0] != '\0') {
1490 usess->has_non_default_channel = 1;
1491 }
1492 } else {
1493 ret_code = channel_ust_enable(usess, uchan);
1494 }
1495 break;
1496 }
1497 default:
1498 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
1499 goto error;
1500 }
1501
1502 if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) {
1503 session->has_non_mmap_channel = true;
1504 }
1505 error:
1506 end:
1507 lttng_channel_destroy(attr);
1508 return ret_code;
1509 }
1510
1511 enum lttng_error_code
1512 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session,
1513 enum lttng_domain_type domain,
1514 enum lttng_process_attr process_attr,
1515 enum lttng_tracking_policy *policy)
1516 {
1517 enum lttng_error_code ret_code = LTTNG_OK;
1518 const struct process_attr_tracker *tracker;
1519
1520 switch (domain) {
1521 case LTTNG_DOMAIN_KERNEL:
1522 if (!session->kernel_session) {
1523 ret_code = LTTNG_ERR_INVALID;
1524 goto end;
1525 }
1526 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
1527 break;
1528 case LTTNG_DOMAIN_UST:
1529 if (!session->ust_session) {
1530 ret_code = LTTNG_ERR_INVALID;
1531 goto end;
1532 }
1533 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
1534 break;
1535 default:
1536 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1537 goto end;
1538 }
1539 if (tracker) {
1540 *policy = process_attr_tracker_get_tracking_policy(tracker);
1541 } else {
1542 ret_code = LTTNG_ERR_INVALID;
1543 }
1544 end:
1545 return ret_code;
1546 }
1547
1548 enum lttng_error_code
1549 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session,
1550 enum lttng_domain_type domain,
1551 enum lttng_process_attr process_attr,
1552 enum lttng_tracking_policy policy)
1553 {
1554 enum lttng_error_code ret_code = LTTNG_OK;
1555
1556 switch (policy) {
1557 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1558 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1559 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1560 break;
1561 default:
1562 ret_code = LTTNG_ERR_INVALID;
1563 goto end;
1564 }
1565
1566 switch (domain) {
1567 case LTTNG_DOMAIN_KERNEL:
1568 if (!session->kernel_session) {
1569 ret_code = LTTNG_ERR_INVALID;
1570 goto end;
1571 }
1572 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1573 session->kernel_session, process_attr, policy);
1574 break;
1575 case LTTNG_DOMAIN_UST:
1576 if (!session->ust_session) {
1577 ret_code = LTTNG_ERR_INVALID;
1578 goto end;
1579 }
1580 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1581 session->ust_session, process_attr, policy);
1582 break;
1583 default:
1584 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1585 break;
1586 }
1587 end:
1588 return ret_code;
1589 }
1590
1591 enum lttng_error_code
1592 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session,
1593 enum lttng_domain_type domain,
1594 enum lttng_process_attr process_attr,
1595 const struct process_attr_value *value)
1596 {
1597 enum lttng_error_code ret_code = LTTNG_OK;
1598
1599 switch (domain) {
1600 case LTTNG_DOMAIN_KERNEL:
1601 if (!session->kernel_session) {
1602 ret_code = LTTNG_ERR_INVALID;
1603 goto end;
1604 }
1605 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1606 session->kernel_session, process_attr, value);
1607 break;
1608 case LTTNG_DOMAIN_UST:
1609 if (!session->ust_session) {
1610 ret_code = LTTNG_ERR_INVALID;
1611 goto end;
1612 }
1613 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1614 session->ust_session, process_attr, value);
1615 break;
1616 default:
1617 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1618 break;
1619 }
1620 end:
1621 return ret_code;
1622 }
1623
1624 enum lttng_error_code
1625 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session,
1626 enum lttng_domain_type domain,
1627 enum lttng_process_attr process_attr,
1628 const struct process_attr_value *value)
1629 {
1630 enum lttng_error_code ret_code = LTTNG_OK;
1631
1632 switch (domain) {
1633 case LTTNG_DOMAIN_KERNEL:
1634 if (!session->kernel_session) {
1635 ret_code = LTTNG_ERR_INVALID;
1636 goto end;
1637 }
1638 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1639 session->kernel_session, process_attr, value);
1640 break;
1641 case LTTNG_DOMAIN_UST:
1642 if (!session->ust_session) {
1643 ret_code = LTTNG_ERR_INVALID;
1644 goto end;
1645 }
1646 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1647 session->ust_session, process_attr, value);
1648 break;
1649 default:
1650 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1651 break;
1652 }
1653 end:
1654 return ret_code;
1655 }
1656
1657 enum lttng_error_code
1658 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session,
1659 enum lttng_domain_type domain,
1660 enum lttng_process_attr process_attr,
1661 struct lttng_process_attr_values **values)
1662 {
1663 enum lttng_error_code ret_code = LTTNG_OK;
1664 const struct process_attr_tracker *tracker;
1665 enum process_attr_tracker_status status;
1666
1667 switch (domain) {
1668 case LTTNG_DOMAIN_KERNEL:
1669 if (!session->kernel_session) {
1670 ret_code = LTTNG_ERR_INVALID;
1671 goto end;
1672 }
1673 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
1674 break;
1675 case LTTNG_DOMAIN_UST:
1676 if (!session->ust_session) {
1677 ret_code = LTTNG_ERR_INVALID;
1678 goto end;
1679 }
1680 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
1681 break;
1682 default:
1683 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1684 goto end;
1685 }
1686
1687 if (!tracker) {
1688 ret_code = LTTNG_ERR_INVALID;
1689 goto end;
1690 }
1691
1692 status = process_attr_tracker_get_inclusion_set(tracker, values);
1693 switch (status) {
1694 case PROCESS_ATTR_TRACKER_STATUS_OK:
1695 ret_code = LTTNG_OK;
1696 break;
1697 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1698 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1699 break;
1700 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1701 ret_code = LTTNG_ERR_NOMEM;
1702 break;
1703 default:
1704 ret_code = LTTNG_ERR_UNK;
1705 break;
1706 }
1707
1708 end:
1709 return ret_code;
1710 }
1711
1712 /*
1713 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1714 */
1715 int cmd_disable_event(struct command_ctx *cmd_ctx,
1716 struct lttng_event *event,
1717 char *filter_expression,
1718 struct lttng_bytecode *bytecode,
1719 struct lttng_event_exclusion *exclusion)
1720 {
1721 int ret;
1722 const char *event_name;
1723 const struct ltt_session *session = cmd_ctx->session;
1724 const char *channel_name = cmd_ctx->lsm.u.disable.channel_name;
1725 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1726
1727 DBG("Disable event command for event \'%s\'", event->name);
1728
1729 /*
1730 * Filter and exclusions are simply not handled by the
1731 * disable event command at this time.
1732 *
1733 * FIXME
1734 */
1735 (void) filter_expression;
1736 (void) exclusion;
1737
1738 /* Ignore the presence of filter or exclusion for the event */
1739 event->filter = 0;
1740 event->exclusion = 0;
1741
1742 event_name = event->name;
1743
1744 lttng::urcu::read_lock_guard read_lock;
1745
1746 /* Error out on unhandled search criteria */
1747 if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid ||
1748 event->filter || event->exclusion) {
1749 ret = LTTNG_ERR_UNK;
1750 goto error;
1751 }
1752
1753 switch (domain) {
1754 case LTTNG_DOMAIN_KERNEL:
1755 {
1756 struct ltt_kernel_channel *kchan;
1757 struct ltt_kernel_session *ksess;
1758
1759 ksess = session->kernel_session;
1760
1761 /*
1762 * If a non-default channel has been created in the
1763 * session, explicitely require that -c chan_name needs
1764 * to be provided.
1765 */
1766 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1767 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1768 goto error_unlock;
1769 }
1770
1771 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1772 if (kchan == nullptr) {
1773 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1774 goto error_unlock;
1775 }
1776
1777 switch (event->type) {
1778 case LTTNG_EVENT_ALL:
1779 case LTTNG_EVENT_TRACEPOINT:
1780 case LTTNG_EVENT_SYSCALL:
1781 case LTTNG_EVENT_PROBE:
1782 case LTTNG_EVENT_FUNCTION:
1783 case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */
1784 if (event_name[0] == '\0') {
1785 ret = event_kernel_disable_event(kchan, nullptr, event->type);
1786 } else {
1787 ret = event_kernel_disable_event(kchan, event_name, event->type);
1788 }
1789 if (ret != LTTNG_OK) {
1790 goto error_unlock;
1791 }
1792 break;
1793 default:
1794 ret = LTTNG_ERR_UNK;
1795 goto error_unlock;
1796 }
1797
1798 kernel_wait_quiescent();
1799 break;
1800 }
1801 case LTTNG_DOMAIN_UST:
1802 {
1803 struct ltt_ust_channel *uchan;
1804 struct ltt_ust_session *usess;
1805
1806 usess = session->ust_session;
1807
1808 if (validate_ust_event_name(event_name)) {
1809 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1810 goto error_unlock;
1811 }
1812
1813 /*
1814 * If a non-default channel has been created in the
1815 * session, explicitly require that -c chan_name needs
1816 * to be provided.
1817 */
1818 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1819 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1820 goto error_unlock;
1821 }
1822
1823 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
1824 if (uchan == nullptr) {
1825 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1826 goto error_unlock;
1827 }
1828
1829 switch (event->type) {
1830 case LTTNG_EVENT_ALL:
1831 /*
1832 * An empty event name means that everything
1833 * should be disabled.
1834 */
1835 if (event->name[0] == '\0') {
1836 ret = event_ust_disable_all_tracepoints(usess, uchan);
1837 } else {
1838 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
1839 }
1840 if (ret != LTTNG_OK) {
1841 goto error_unlock;
1842 }
1843 break;
1844 default:
1845 ret = LTTNG_ERR_UNK;
1846 goto error_unlock;
1847 }
1848
1849 DBG3("Disable UST event %s in channel %s completed", event_name, channel_name);
1850 break;
1851 }
1852 case LTTNG_DOMAIN_LOG4J:
1853 case LTTNG_DOMAIN_JUL:
1854 case LTTNG_DOMAIN_PYTHON:
1855 {
1856 struct agent *agt;
1857 struct ltt_ust_session *usess = session->ust_session;
1858
1859 LTTNG_ASSERT(usess);
1860
1861 switch (event->type) {
1862 case LTTNG_EVENT_ALL:
1863 break;
1864 default:
1865 ret = LTTNG_ERR_UNK;
1866 goto error_unlock;
1867 }
1868
1869 agt = trace_ust_find_agent(usess, domain);
1870 if (!agt) {
1871 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1872 goto error_unlock;
1873 }
1874 /*
1875 * An empty event name means that everything
1876 * should be disabled.
1877 */
1878 if (event->name[0] == '\0') {
1879 ret = event_agent_disable_all(usess, agt);
1880 } else {
1881 ret = event_agent_disable(usess, agt, event_name);
1882 }
1883 if (ret != LTTNG_OK) {
1884 goto error_unlock;
1885 }
1886
1887 break;
1888 }
1889 default:
1890 ret = LTTNG_ERR_UND;
1891 goto error_unlock;
1892 }
1893
1894 ret = LTTNG_OK;
1895
1896 error_unlock:
1897 error:
1898 free(exclusion);
1899 free(bytecode);
1900 free(filter_expression);
1901 return ret;
1902 }
1903
1904 /*
1905 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1906 */
1907 int cmd_add_context(struct command_ctx *cmd_ctx,
1908 const struct lttng_event_context *event_context,
1909 int kwpipe)
1910 {
1911 int ret, chan_kern_created = 0, chan_ust_created = 0;
1912 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1913 const struct ltt_session *session = cmd_ctx->session;
1914 const char *channel_name = cmd_ctx->lsm.u.context.channel_name;
1915
1916 /*
1917 * Don't try to add a context if the session has been started at
1918 * some point in time before. The tracer does not allow it and would
1919 * result in a corrupted trace.
1920 */
1921 if (cmd_ctx->session->has_been_started) {
1922 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1923 goto end;
1924 }
1925
1926 switch (domain) {
1927 case LTTNG_DOMAIN_KERNEL:
1928 LTTNG_ASSERT(session->kernel_session);
1929
1930 if (session->kernel_session->channel_count == 0) {
1931 /* Create default channel */
1932 ret = channel_kernel_create(session->kernel_session, nullptr, kwpipe);
1933 if (ret != LTTNG_OK) {
1934 goto error;
1935 }
1936 chan_kern_created = 1;
1937 }
1938 /* Add kernel context to kernel tracer */
1939 ret = context_kernel_add(session->kernel_session, event_context, channel_name);
1940 if (ret != LTTNG_OK) {
1941 goto error;
1942 }
1943 break;
1944 case LTTNG_DOMAIN_JUL:
1945 case LTTNG_DOMAIN_LOG4J:
1946 {
1947 /*
1948 * Validate channel name.
1949 * If no channel name is given and the domain is JUL or LOG4J,
1950 * set it to the appropriate domain-specific channel name. If
1951 * a name is provided but does not match the expexted channel
1952 * name, return an error.
1953 */
1954 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1955 strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME) != 0) {
1956 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1957 goto error;
1958 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1959 strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME) != 0) {
1960 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1961 goto error;
1962 }
1963 }
1964 /* fall through */
1965 case LTTNG_DOMAIN_UST:
1966 {
1967 struct ltt_ust_session *usess = session->ust_session;
1968 unsigned int chan_count;
1969
1970 LTTNG_ASSERT(usess);
1971
1972 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1973 if (chan_count == 0) {
1974 struct lttng_channel *attr;
1975 /* Create default channel */
1976 attr = channel_new_default_attr(domain, usess->buffer_type);
1977 if (attr == nullptr) {
1978 ret = LTTNG_ERR_FATAL;
1979 goto error;
1980 }
1981
1982 ret = channel_ust_create(usess, attr, usess->buffer_type);
1983 if (ret != LTTNG_OK) {
1984 free(attr);
1985 goto error;
1986 }
1987 channel_attr_destroy(attr);
1988 chan_ust_created = 1;
1989 }
1990
1991 ret = context_ust_add(usess, domain, event_context, channel_name);
1992 if (ret != LTTNG_OK) {
1993 goto error;
1994 }
1995 break;
1996 }
1997 default:
1998 ret = LTTNG_ERR_UND;
1999 goto error;
2000 }
2001
2002 ret = LTTNG_OK;
2003 goto end;
2004
2005 error:
2006 if (chan_kern_created) {
2007 struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name(
2008 DEFAULT_CHANNEL_NAME, session->kernel_session);
2009 /* Created previously, this should NOT fail. */
2010 LTTNG_ASSERT(kchan);
2011 kernel_destroy_channel(kchan);
2012 }
2013
2014 if (chan_ust_created) {
2015 struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name(
2016 session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
2017 /* Created previously, this should NOT fail. */
2018 LTTNG_ASSERT(uchan);
2019 /* Remove from the channel list of the session. */
2020 trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan);
2021 trace_ust_destroy_channel(uchan);
2022 }
2023 end:
2024 return ret;
2025 }
2026
2027 static inline bool name_starts_with(const char *name, const char *prefix)
2028 {
2029 const size_t max_cmp_len = std::min(strlen(prefix), (size_t) LTTNG_SYMBOL_NAME_LEN);
2030
2031 return !strncmp(name, prefix, max_cmp_len);
2032 }
2033
2034 /* Perform userspace-specific event name validation */
2035 static int validate_ust_event_name(const char *name)
2036 {
2037 int ret = 0;
2038
2039 if (!name) {
2040 ret = -1;
2041 goto end;
2042 }
2043
2044 /*
2045 * Check name against all internal UST event component namespaces used
2046 * by the agents.
2047 */
2048 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2049 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2050 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2051 ret = -1;
2052 }
2053
2054 end:
2055 return ret;
2056 }
2057
2058 /*
2059 * Internal version of cmd_enable_event() with a supplemental
2060 * "internal_event" flag which is used to enable internal events which should
2061 * be hidden from clients. Such events are used in the agent implementation to
2062 * enable the events through which all "agent" events are funeled.
2063 */
2064 static int _cmd_enable_event(struct ltt_session *session,
2065 const struct lttng_domain *domain,
2066 char *channel_name,
2067 struct lttng_event *event,
2068 char *filter_expression,
2069 struct lttng_bytecode *filter,
2070 struct lttng_event_exclusion *exclusion,
2071 int wpipe,
2072 bool internal_event)
2073 {
2074 int ret = 0, channel_created = 0;
2075 struct lttng_channel *attr = nullptr;
2076
2077 LTTNG_ASSERT(session);
2078 LTTNG_ASSERT(event);
2079 LTTNG_ASSERT(channel_name);
2080
2081 /* If we have a filter, we must have its filter expression */
2082 LTTNG_ASSERT(!(!!filter_expression ^ !!filter));
2083
2084 /* Normalize event name as a globbing pattern */
2085 strutils_normalize_star_glob_pattern(event->name);
2086
2087 /* Normalize exclusion names as globbing patterns */
2088 if (exclusion) {
2089 size_t i;
2090
2091 for (i = 0; i < exclusion->count; i++) {
2092 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2093
2094 strutils_normalize_star_glob_pattern(name);
2095 }
2096 }
2097
2098 DBG("Enable event command for event \'%s\'", event->name);
2099
2100 lttng::urcu::read_lock_guard read_lock;
2101
2102 switch (domain->type) {
2103 case LTTNG_DOMAIN_KERNEL:
2104 {
2105 struct ltt_kernel_channel *kchan;
2106
2107 /*
2108 * If a non-default channel has been created in the
2109 * session, explicitely require that -c chan_name needs
2110 * to be provided.
2111 */
2112 if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
2113 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2114 goto error;
2115 }
2116
2117 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
2118 if (kchan == nullptr) {
2119 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
2120 if (attr == nullptr) {
2121 ret = LTTNG_ERR_FATAL;
2122 goto error;
2123 }
2124 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
2125 ret = LTTNG_ERR_INVALID;
2126 goto error;
2127 }
2128
2129 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
2130 if (ret != LTTNG_OK) {
2131 goto error;
2132 }
2133 channel_created = 1;
2134 }
2135
2136 /* Get the newly created kernel channel pointer */
2137 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
2138 if (kchan == nullptr) {
2139 /* This sould not happen... */
2140 ret = LTTNG_ERR_FATAL;
2141 goto error;
2142 }
2143
2144 switch (event->type) {
2145 case LTTNG_EVENT_ALL:
2146 {
2147 char *filter_expression_a = nullptr;
2148 struct lttng_bytecode *filter_a = nullptr;
2149
2150 /*
2151 * We need to duplicate filter_expression and filter,
2152 * because ownership is passed to first enable
2153 * event.
2154 */
2155 if (filter_expression) {
2156 filter_expression_a = strdup(filter_expression);
2157 if (!filter_expression_a) {
2158 ret = LTTNG_ERR_FATAL;
2159 goto error;
2160 }
2161 }
2162 if (filter) {
2163 filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len);
2164 if (!filter_a) {
2165 free(filter_expression_a);
2166 ret = LTTNG_ERR_FATAL;
2167 goto error;
2168 }
2169 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2170 }
2171 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2172 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2173 /* We have passed ownership */
2174 filter_expression = nullptr;
2175 filter = nullptr;
2176 if (ret != LTTNG_OK) {
2177 if (channel_created) {
2178 /* Let's not leak a useless channel. */
2179 kernel_destroy_channel(kchan);
2180 }
2181 free(filter_expression_a);
2182 free(filter_a);
2183 goto error;
2184 }
2185 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2186 ret = event_kernel_enable_event(
2187 kchan, event, filter_expression_a, filter_a);
2188 /* We have passed ownership */
2189 filter_expression_a = nullptr;
2190 filter_a = nullptr;
2191 if (ret != LTTNG_OK) {
2192 goto error;
2193 }
2194 break;
2195 }
2196 case LTTNG_EVENT_PROBE:
2197 case LTTNG_EVENT_USERSPACE_PROBE:
2198 case LTTNG_EVENT_FUNCTION:
2199 case LTTNG_EVENT_FUNCTION_ENTRY:
2200 case LTTNG_EVENT_TRACEPOINT:
2201 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2202 /* We have passed ownership */
2203 filter_expression = nullptr;
2204 filter = nullptr;
2205 if (ret != LTTNG_OK) {
2206 if (channel_created) {
2207 /* Let's not leak a useless channel. */
2208 kernel_destroy_channel(kchan);
2209 }
2210 goto error;
2211 }
2212 break;
2213 case LTTNG_EVENT_SYSCALL:
2214 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2215 /* We have passed ownership */
2216 filter_expression = nullptr;
2217 filter = nullptr;
2218 if (ret != LTTNG_OK) {
2219 goto error;
2220 }
2221 break;
2222 default:
2223 ret = LTTNG_ERR_UNK;
2224 goto error;
2225 }
2226
2227 kernel_wait_quiescent();
2228 break;
2229 }
2230 case LTTNG_DOMAIN_UST:
2231 {
2232 struct ltt_ust_channel *uchan;
2233 struct ltt_ust_session *usess = session->ust_session;
2234
2235 LTTNG_ASSERT(usess);
2236
2237 /*
2238 * If a non-default channel has been created in the
2239 * session, explicitely require that -c chan_name needs
2240 * to be provided.
2241 */
2242 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2243 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2244 goto error;
2245 }
2246
2247 /* Get channel from global UST domain */
2248 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
2249 if (uchan == nullptr) {
2250 /* Create default channel */
2251 attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type);
2252 if (attr == nullptr) {
2253 ret = LTTNG_ERR_FATAL;
2254 goto error;
2255 }
2256 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
2257 ret = LTTNG_ERR_INVALID;
2258 goto error;
2259 }
2260
2261 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
2262 if (ret != LTTNG_OK) {
2263 goto error;
2264 }
2265
2266 /* Get the newly created channel reference back */
2267 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2268 channel_name);
2269 LTTNG_ASSERT(uchan);
2270 }
2271
2272 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2273 /*
2274 * Don't allow users to add UST events to channels which
2275 * are assigned to a userspace subdomain (JUL, Log4J,
2276 * Python, etc.).
2277 */
2278 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2279 goto error;
2280 }
2281
2282 if (!internal_event) {
2283 /*
2284 * Ensure the event name is not reserved for internal
2285 * use.
2286 */
2287 ret = validate_ust_event_name(event->name);
2288 if (ret) {
2289 WARN("Userspace event name %s failed validation.", event->name);
2290 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2291 goto error;
2292 }
2293 }
2294
2295 /* At this point, the session and channel exist on the tracer */
2296 ret = event_ust_enable_tracepoint(
2297 usess, uchan, event, filter_expression, filter, exclusion, internal_event);
2298 /* We have passed ownership */
2299 filter_expression = nullptr;
2300 filter = nullptr;
2301 exclusion = nullptr;
2302 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2303 goto already_enabled;
2304 } else if (ret != LTTNG_OK) {
2305 goto error;
2306 }
2307 break;
2308 }
2309 case LTTNG_DOMAIN_LOG4J:
2310 case LTTNG_DOMAIN_JUL:
2311 case LTTNG_DOMAIN_PYTHON:
2312 {
2313 const char *default_event_name, *default_chan_name;
2314 struct agent *agt;
2315 struct lttng_event uevent;
2316 struct lttng_domain tmp_dom;
2317 struct ltt_ust_session *usess = session->ust_session;
2318
2319 LTTNG_ASSERT(usess);
2320
2321 if (!agent_tracing_is_enabled()) {
2322 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2323 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2324 goto error;
2325 }
2326
2327 agt = trace_ust_find_agent(usess, domain->type);
2328 if (!agt) {
2329 agt = agent_create(domain->type);
2330 if (!agt) {
2331 ret = LTTNG_ERR_NOMEM;
2332 goto error;
2333 }
2334 agent_add(agt, usess->agents);
2335 }
2336
2337 /* Create the default tracepoint. */
2338 memset(&uevent, 0, sizeof(uevent));
2339 uevent.type = LTTNG_EVENT_TRACEPOINT;
2340 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2341 default_event_name = event_get_default_agent_ust_name(domain->type);
2342 if (!default_event_name) {
2343 ret = LTTNG_ERR_FATAL;
2344 goto error;
2345 }
2346 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2347 uevent.name[sizeof(uevent.name) - 1] = '\0';
2348
2349 /*
2350 * The domain type is changed because we are about to enable the
2351 * default channel and event for the JUL domain that are hardcoded.
2352 * This happens in the UST domain.
2353 */
2354 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2355 tmp_dom.type = LTTNG_DOMAIN_UST;
2356
2357 switch (domain->type) {
2358 case LTTNG_DOMAIN_LOG4J:
2359 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2360 break;
2361 case LTTNG_DOMAIN_JUL:
2362 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2363 break;
2364 case LTTNG_DOMAIN_PYTHON:
2365 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2366 break;
2367 default:
2368 /* The switch/case we are in makes this impossible */
2369 abort();
2370 }
2371
2372 {
2373 char *filter_expression_copy = nullptr;
2374 struct lttng_bytecode *filter_copy = nullptr;
2375
2376 if (filter) {
2377 const size_t filter_size =
2378 sizeof(struct lttng_bytecode) + filter->len;
2379
2380 filter_copy = zmalloc<lttng_bytecode>(filter_size);
2381 if (!filter_copy) {
2382 ret = LTTNG_ERR_NOMEM;
2383 goto error;
2384 }
2385 memcpy(filter_copy, filter, filter_size);
2386
2387 filter_expression_copy = strdup(filter_expression);
2388 if (!filter_expression) {
2389 ret = LTTNG_ERR_NOMEM;
2390 }
2391
2392 if (!filter_expression_copy || !filter_copy) {
2393 free(filter_expression_copy);
2394 free(filter_copy);
2395 goto error;
2396 }
2397 }
2398
2399 ret = cmd_enable_event_internal(session,
2400 &tmp_dom,
2401 (char *) default_chan_name,
2402 &uevent,
2403 filter_expression_copy,
2404 filter_copy,
2405 nullptr,
2406 wpipe);
2407 }
2408
2409 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2410 goto already_enabled;
2411 } else if (ret != LTTNG_OK) {
2412 goto error;
2413 }
2414
2415 /* The wild card * means that everything should be enabled. */
2416 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2417 ret = event_agent_enable_all(usess, agt, event, filter, filter_expression);
2418 } else {
2419 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
2420 }
2421 filter = nullptr;
2422 filter_expression = nullptr;
2423 if (ret != LTTNG_OK) {
2424 goto error;
2425 }
2426
2427 break;
2428 }
2429 default:
2430 ret = LTTNG_ERR_UND;
2431 goto error;
2432 }
2433
2434 ret = LTTNG_OK;
2435
2436 already_enabled:
2437 error:
2438 free(filter_expression);
2439 free(filter);
2440 free(exclusion);
2441 channel_attr_destroy(attr);
2442 return ret;
2443 }
2444
2445 /*
2446 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2447 * We own filter, exclusion, and filter_expression.
2448 */
2449 int cmd_enable_event(struct command_ctx *cmd_ctx,
2450 struct lttng_event *event,
2451 char *filter_expression,
2452 struct lttng_event_exclusion *exclusion,
2453 struct lttng_bytecode *bytecode,
2454 int wpipe)
2455 {
2456 int ret;
2457 /*
2458 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2459 */
2460 const lttng_domain command_domain = cmd_ctx->lsm.domain;
2461
2462 /*
2463 * The ownership of the following parameters is transferred to
2464 * _cmd_enable_event:
2465 *
2466 * - filter_expression,
2467 * - bytecode,
2468 * - exclusion
2469 */
2470 ret = _cmd_enable_event(cmd_ctx->session,
2471 &command_domain,
2472 cmd_ctx->lsm.u.enable.channel_name,
2473 event,
2474 filter_expression,
2475 bytecode,
2476 exclusion,
2477 wpipe,
2478 false);
2479 filter_expression = nullptr;
2480 bytecode = nullptr;
2481 exclusion = nullptr;
2482 return ret;
2483 }
2484
2485 /*
2486 * Enable an event which is internal to LTTng. An internal should
2487 * never be made visible to clients and are immune to checks such as
2488 * reserved names.
2489 */
2490 static int cmd_enable_event_internal(struct ltt_session *session,
2491 const struct lttng_domain *domain,
2492 char *channel_name,
2493 struct lttng_event *event,
2494 char *filter_expression,
2495 struct lttng_bytecode *filter,
2496 struct lttng_event_exclusion *exclusion,
2497 int wpipe)
2498 {
2499 return _cmd_enable_event(session,
2500 domain,
2501 channel_name,
2502 event,
2503 filter_expression,
2504 filter,
2505 exclusion,
2506 wpipe,
2507 true);
2508 }
2509
2510 /*
2511 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2512 */
2513 enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
2514 struct lttng_payload *reply_payload)
2515 {
2516 enum lttng_error_code ret_code;
2517 int ret;
2518 ssize_t i, nb_events = 0;
2519 struct lttng_event *events = nullptr;
2520 struct lttcomm_list_command_header reply_command_header = {};
2521 size_t reply_command_header_offset;
2522
2523 assert(reply_payload);
2524
2525 /* Reserve space for command reply header. */
2526 reply_command_header_offset = reply_payload->buffer.size;
2527 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2528 reply_command_header_offset +
2529 sizeof(struct lttcomm_list_command_header));
2530 if (ret) {
2531 ret_code = LTTNG_ERR_NOMEM;
2532 goto error;
2533 }
2534
2535 switch (domain) {
2536 case LTTNG_DOMAIN_KERNEL:
2537 nb_events = kernel_list_events(&events);
2538 if (nb_events < 0) {
2539 ret_code = LTTNG_ERR_KERN_LIST_FAIL;
2540 goto error;
2541 }
2542 break;
2543 case LTTNG_DOMAIN_UST:
2544 nb_events = ust_app_list_events(&events);
2545 if (nb_events < 0) {
2546 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2547 goto error;
2548 }
2549 break;
2550 case LTTNG_DOMAIN_LOG4J:
2551 case LTTNG_DOMAIN_JUL:
2552 case LTTNG_DOMAIN_PYTHON:
2553 nb_events = agent_list_events(&events, domain);
2554 if (nb_events < 0) {
2555 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2556 goto error;
2557 }
2558 break;
2559 default:
2560 ret_code = LTTNG_ERR_UND;
2561 goto error;
2562 }
2563
2564 for (i = 0; i < nb_events; i++) {
2565 ret = lttng_event_serialize(
2566 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
2567 if (ret) {
2568 ret_code = LTTNG_ERR_NOMEM;
2569 goto error;
2570 }
2571 }
2572
2573 if (nb_events > UINT32_MAX) {
2574 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2575 ret_code = LTTNG_ERR_OVERFLOW;
2576 goto error;
2577 }
2578
2579 /* Update command reply header. */
2580 reply_command_header.count = (uint32_t) nb_events;
2581 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2582 &reply_command_header,
2583 sizeof(reply_command_header));
2584
2585 ret_code = LTTNG_OK;
2586 error:
2587 free(events);
2588 return ret_code;
2589 }
2590
2591 /*
2592 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2593 */
2594 enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2595 struct lttng_payload *reply)
2596 {
2597 enum lttng_error_code ret_code;
2598 int ret;
2599 unsigned int i, nb_fields;
2600 struct lttng_event_field *fields = nullptr;
2601 struct lttcomm_list_command_header reply_command_header = {};
2602 size_t reply_command_header_offset;
2603
2604 assert(reply);
2605
2606 /* Reserve space for command reply header. */
2607 reply_command_header_offset = reply->buffer.size;
2608 ret = lttng_dynamic_buffer_set_size(&reply->buffer,
2609 reply_command_header_offset +
2610 sizeof(struct lttcomm_list_command_header));
2611 if (ret) {
2612 ret_code = LTTNG_ERR_NOMEM;
2613 goto error;
2614 }
2615
2616 switch (domain) {
2617 case LTTNG_DOMAIN_UST:
2618 ret = ust_app_list_event_fields(&fields);
2619 if (ret < 0) {
2620 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2621 goto error;
2622 }
2623
2624 break;
2625 case LTTNG_DOMAIN_KERNEL:
2626 default: /* fall-through */
2627 ret_code = LTTNG_ERR_UND;
2628 goto error;
2629 }
2630
2631 nb_fields = ret;
2632
2633 for (i = 0; i < nb_fields; i++) {
2634 ret = lttng_event_field_serialize(&fields[i], reply);
2635 if (ret) {
2636 ret_code = LTTNG_ERR_NOMEM;
2637 goto error;
2638 }
2639 }
2640
2641 if (nb_fields > UINT32_MAX) {
2642 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2643 ret_code = LTTNG_ERR_OVERFLOW;
2644 goto error;
2645 }
2646
2647 /* Update command reply header. */
2648 reply_command_header.count = (uint32_t) nb_fields;
2649
2650 memcpy(reply->buffer.data + reply_command_header_offset,
2651 &reply_command_header,
2652 sizeof(reply_command_header));
2653
2654 ret_code = LTTNG_OK;
2655
2656 error:
2657 free(fields);
2658 return ret_code;
2659 }
2660
2661 enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload)
2662 {
2663 enum lttng_error_code ret_code;
2664 ssize_t nb_events, i;
2665 int ret;
2666 struct lttng_event *events = nullptr;
2667 struct lttcomm_list_command_header reply_command_header = {};
2668 size_t reply_command_header_offset;
2669
2670 assert(reply_payload);
2671
2672 /* Reserve space for command reply header. */
2673 reply_command_header_offset = reply_payload->buffer.size;
2674 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2675 reply_command_header_offset +
2676 sizeof(struct lttcomm_list_command_header));
2677 if (ret) {
2678 ret_code = LTTNG_ERR_NOMEM;
2679 goto end;
2680 }
2681
2682 nb_events = syscall_table_list(&events);
2683 if (nb_events < 0) {
2684 ret_code = (enum lttng_error_code) - nb_events;
2685 goto end;
2686 }
2687
2688 for (i = 0; i < nb_events; i++) {
2689 ret = lttng_event_serialize(
2690 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
2691 if (ret) {
2692 ret_code = LTTNG_ERR_NOMEM;
2693 goto end;
2694 }
2695 }
2696
2697 if (nb_events > UINT32_MAX) {
2698 ERR("Syscall count would overflow the syscall listing command's reply");
2699 ret_code = LTTNG_ERR_OVERFLOW;
2700 goto end;
2701 }
2702
2703 /* Update command reply header. */
2704 reply_command_header.count = (uint32_t) nb_events;
2705 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2706 &reply_command_header,
2707 sizeof(reply_command_header));
2708
2709 ret_code = LTTNG_OK;
2710 end:
2711 free(events);
2712 return ret_code;
2713 }
2714
2715 /*
2716 * Command LTTNG_START_TRACE processed by the client thread.
2717 *
2718 * Called with session mutex held.
2719 */
2720 int cmd_start_trace(struct ltt_session *session)
2721 {
2722 enum lttng_error_code ret;
2723 unsigned long nb_chan = 0;
2724 struct ltt_kernel_session *ksession;
2725 struct ltt_ust_session *usess;
2726 const bool session_rotated_after_last_stop = session->rotated_after_last_stop;
2727 const bool session_cleared_after_last_stop = session->cleared_after_last_stop;
2728
2729 LTTNG_ASSERT(session);
2730
2731 /* Ease our life a bit ;) */
2732 ksession = session->kernel_session;
2733 usess = session->ust_session;
2734
2735 /* Is the session already started? */
2736 if (session->active) {
2737 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2738 /* Perform nothing */
2739 goto end;
2740 }
2741
2742 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2743 !session->current_trace_chunk) {
2744 /*
2745 * A rotation was launched while the session was stopped and
2746 * it has not been completed yet. It is not possible to start
2747 * the session since starting the session here would require a
2748 * rotation from "NULL" to a new trace chunk. That rotation
2749 * would overlap with the ongoing rotation, which is not
2750 * supported.
2751 */
2752 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2753 session->name);
2754 ret = LTTNG_ERR_ROTATION_PENDING;
2755 goto error;
2756 }
2757
2758 /*
2759 * Starting a session without channel is useless since after that it's not
2760 * possible to enable channel thus inform the client.
2761 */
2762 if (usess && usess->domain_global.channels) {
2763 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2764 }
2765 if (ksession) {
2766 nb_chan += ksession->channel_count;
2767 }
2768 if (!nb_chan) {
2769 ret = LTTNG_ERR_NO_CHANNEL;
2770 goto error;
2771 }
2772
2773 session->active = true;
2774 session->rotated_after_last_stop = false;
2775 session->cleared_after_last_stop = false;
2776 if (session->output_traces && !session->current_trace_chunk) {
2777 if (!session->has_been_started) {
2778 struct lttng_trace_chunk *trace_chunk;
2779
2780 DBG("Creating initial trace chunk of session \"%s\"", session->name);
2781 trace_chunk =
2782 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
2783 if (!trace_chunk) {
2784 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2785 goto error;
2786 }
2787 LTTNG_ASSERT(!session->current_trace_chunk);
2788 ret = (lttng_error_code) session_set_trace_chunk(
2789 session, trace_chunk, nullptr);
2790 lttng_trace_chunk_put(trace_chunk);
2791 if (ret) {
2792 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2793 goto error;
2794 }
2795 } else {
2796 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2797 session->name);
2798 /*
2799 * Rotate existing streams into the new chunk.
2800 * This is a "quiet" rotation has no client has
2801 * explicitly requested this operation.
2802 *
2803 * There is also no need to wait for the rotation
2804 * to complete as it will happen immediately. No data
2805 * was produced as the session was stopped, so the
2806 * rotation should happen on reception of the command.
2807 */
2808 ret = (lttng_error_code) cmd_rotate_session(
2809 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2810 if (ret != LTTNG_OK) {
2811 goto error;
2812 }
2813 }
2814 }
2815
2816 /* Kernel tracing */
2817 if (ksession != nullptr) {
2818 DBG("Start kernel tracing session %s", session->name);
2819 ret = (lttng_error_code) start_kernel_session(ksession);
2820 if (ret != LTTNG_OK) {
2821 goto error;
2822 }
2823 }
2824
2825 /* Flag session that trace should start automatically */
2826 if (usess) {
2827 int int_ret = ust_app_start_trace_all(usess);
2828
2829 if (int_ret < 0) {
2830 ret = LTTNG_ERR_UST_START_FAIL;
2831 goto error;
2832 }
2833 }
2834
2835 /*
2836 * Open a packet in every stream of the session to ensure that viewers
2837 * can correctly identify the boundaries of the periods during which
2838 * tracing was active for this session.
2839 */
2840 ret = session_open_packets(session);
2841 if (ret != LTTNG_OK) {
2842 goto error;
2843 }
2844
2845 /*
2846 * Clear the flag that indicates that a rotation was done while the
2847 * session was stopped.
2848 */
2849 session->rotated_after_last_stop = false;
2850
2851 if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) {
2852 int int_ret = timer_session_rotation_schedule_timer_start(
2853 session, session->rotate_timer_period);
2854
2855 if (int_ret < 0) {
2856 ERR("Failed to enable rotate timer");
2857 ret = LTTNG_ERR_UNK;
2858 goto error;
2859 }
2860 }
2861
2862 ret = LTTNG_OK;
2863
2864 error:
2865 if (ret == LTTNG_OK) {
2866 /* Flag this after a successful start. */
2867 session->has_been_started = true;
2868 } else {
2869 session->active = false;
2870 /* Restore initial state on error. */
2871 session->rotated_after_last_stop = session_rotated_after_last_stop;
2872 session->cleared_after_last_stop = session_cleared_after_last_stop;
2873 }
2874 end:
2875 return ret;
2876 }
2877
2878 /*
2879 * Command LTTNG_STOP_TRACE processed by the client thread.
2880 */
2881 int cmd_stop_trace(struct ltt_session *session)
2882 {
2883 int ret;
2884 struct ltt_kernel_session *ksession;
2885 struct ltt_ust_session *usess;
2886
2887 LTTNG_ASSERT(session);
2888
2889 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2890 /* Short cut */
2891 ksession = session->kernel_session;
2892 usess = session->ust_session;
2893
2894 /* Session is not active. Skip everything and inform the client. */
2895 if (!session->active) {
2896 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2897 goto error;
2898 }
2899
2900 ret = stop_kernel_session(ksession);
2901 if (ret != LTTNG_OK) {
2902 goto error;
2903 }
2904
2905 if (usess && usess->active) {
2906 ret = ust_app_stop_trace_all(usess);
2907 if (ret < 0) {
2908 ret = LTTNG_ERR_UST_STOP_FAIL;
2909 goto error;
2910 }
2911 }
2912
2913 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2914 /* Flag inactive after a successful stop. */
2915 session->active = false;
2916 ret = LTTNG_OK;
2917
2918 error:
2919 return ret;
2920 }
2921
2922 /*
2923 * Set the base_path of the session only if subdir of a control uris is set.
2924 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2925 */
2926 static int
2927 set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
2928 {
2929 int ret;
2930 size_t i;
2931
2932 for (i = 0; i < nb_uri; i++) {
2933 if (uris[i].stype != LTTNG_STREAM_CONTROL || uris[i].subdir[0] == '\0') {
2934 /* Not interested in these URIs */
2935 continue;
2936 }
2937
2938 if (session->base_path != nullptr) {
2939 free(session->base_path);
2940 session->base_path = nullptr;
2941 }
2942
2943 /* Set session base_path */
2944 session->base_path = strdup(uris[i].subdir);
2945 if (!session->base_path) {
2946 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2947 uris[i].subdir,
2948 session->name);
2949 ret = LTTNG_ERR_NOMEM;
2950 goto error;
2951 }
2952 DBG2("Setting base path \"%s\" for session \"%s\"",
2953 session->base_path,
2954 session->name);
2955 }
2956 ret = LTTNG_OK;
2957 error:
2958 return ret;
2959 }
2960
2961 /*
2962 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2963 */
2964 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
2965 {
2966 int ret, i;
2967 struct ltt_kernel_session *ksess = session->kernel_session;
2968 struct ltt_ust_session *usess = session->ust_session;
2969
2970 LTTNG_ASSERT(session);
2971 LTTNG_ASSERT(uris);
2972 LTTNG_ASSERT(nb_uri > 0);
2973
2974 /* Can't set consumer URI if the session is active. */
2975 if (session->active) {
2976 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2977 goto error;
2978 }
2979
2980 /*
2981 * Set the session base path if any. This is done inside
2982 * cmd_set_consumer_uri to preserve backward compatibility of the
2983 * previous session creation api vs the session descriptor api.
2984 */
2985 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2986 if (ret != LTTNG_OK) {
2987 goto error;
2988 }
2989
2990 /* Set the "global" consumer URIs */
2991 for (i = 0; i < nb_uri; i++) {
2992 ret = add_uri_to_consumer(session, session->consumer, &uris[i], LTTNG_DOMAIN_NONE);
2993 if (ret != LTTNG_OK) {
2994 goto error;
2995 }
2996 }
2997
2998 /* Set UST session URIs */
2999 if (session->ust_session) {
3000 for (i = 0; i < nb_uri; i++) {
3001 ret = add_uri_to_consumer(session,
3002 session->ust_session->consumer,
3003 &uris[i],
3004 LTTNG_DOMAIN_UST);
3005 if (ret != LTTNG_OK) {
3006 goto error;
3007 }
3008 }
3009 }
3010
3011 /* Set kernel session URIs */
3012 if (session->kernel_session) {
3013 for (i = 0; i < nb_uri; i++) {
3014 ret = add_uri_to_consumer(session,
3015 session->kernel_session->consumer,
3016 &uris[i],
3017 LTTNG_DOMAIN_KERNEL);
3018 if (ret != LTTNG_OK) {
3019 goto error;
3020 }
3021 }
3022 }
3023
3024 /*
3025 * Make sure to set the session in output mode after we set URI since a
3026 * session can be created without URL (thus flagged in no output mode).
3027 */
3028 session->output_traces = 1;
3029 if (ksess) {
3030 ksess->output_traces = 1;
3031 }
3032
3033 if (usess) {
3034 usess->output_traces = 1;
3035 }
3036
3037 /* All good! */
3038 ret = LTTNG_OK;
3039
3040 error:
3041 return ret;
3042 }
3043
3044 static enum lttng_error_code
3045 set_session_output_from_descriptor(struct ltt_session *session,
3046 const struct lttng_session_descriptor *descriptor)
3047 {
3048 int ret;
3049 enum lttng_error_code ret_code = LTTNG_OK;
3050 enum lttng_session_descriptor_type session_type =
3051 lttng_session_descriptor_get_type(descriptor);
3052 enum lttng_session_descriptor_output_type output_type =
3053 lttng_session_descriptor_get_output_type(descriptor);
3054 struct lttng_uri uris[2] = {};
3055 size_t uri_count = 0;
3056
3057 switch (output_type) {
3058 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3059 goto end;
3060 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3061 lttng_session_descriptor_get_local_output_uri(descriptor, &uris[0]);
3062 uri_count = 1;
3063 break;
3064 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3065 lttng_session_descriptor_get_network_output_uris(descriptor, &uris[0], &uris[1]);
3066 uri_count = 2;
3067 break;
3068 default:
3069 ret_code = LTTNG_ERR_INVALID;
3070 goto end;
3071 }
3072
3073 switch (session_type) {
3074 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3075 {
3076 struct snapshot_output *new_output = nullptr;
3077
3078 new_output = snapshot_output_alloc();
3079 if (!new_output) {
3080 ret_code = LTTNG_ERR_NOMEM;
3081 goto end;
3082 }
3083
3084 ret = snapshot_output_init_with_uri(session,
3085 DEFAULT_SNAPSHOT_MAX_SIZE,
3086 nullptr,
3087 uris,
3088 uri_count,
3089 session->consumer,
3090 new_output,
3091 &session->snapshot);
3092 if (ret < 0) {
3093 ret_code = (ret == -ENOMEM) ? LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3094 snapshot_output_destroy(new_output);
3095 goto end;
3096 }
3097 snapshot_add_output(&session->snapshot, new_output);
3098 break;
3099 }
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3101 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3102 {
3103 ret_code = (lttng_error_code) cmd_set_consumer_uri(session, uri_count, uris);
3104 break;
3105 }
3106 default:
3107 ret_code = LTTNG_ERR_INVALID;
3108 goto end;
3109 }
3110 end:
3111 return ret_code;
3112 }
3113
3114 static enum lttng_error_code
3115 cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
3116 const lttng_sock_cred *creds,
3117 const char *home_path)
3118 {
3119 int ret;
3120 enum lttng_error_code ret_code;
3121 const char *session_name;
3122 struct ltt_session *new_session = nullptr;
3123 enum lttng_session_descriptor_status descriptor_status;
3124
3125 session_lock_list();
3126 if (home_path) {
3127 if (*home_path != '/') {
3128 ERR("Home path provided by client is not absolute");
3129 ret_code = LTTNG_ERR_INVALID;
3130 goto end;
3131 }
3132 }
3133
3134 descriptor_status = lttng_session_descriptor_get_session_name(descriptor, &session_name);
3135 switch (descriptor_status) {
3136 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3137 break;
3138 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3139 session_name = nullptr;
3140 break;
3141 default:
3142 ret_code = LTTNG_ERR_INVALID;
3143 goto end;
3144 }
3145
3146 ret_code = session_create(session_name, creds->uid, creds->gid, &new_session);
3147 if (ret_code != LTTNG_OK) {
3148 goto end;
3149 }
3150
3151 ret_code = notification_thread_command_add_session(the_notification_thread_handle,
3152 new_session->id,
3153 new_session->name,
3154 new_session->uid,
3155 new_session->gid);
3156 if (ret_code != LTTNG_OK) {
3157 goto end;
3158 }
3159
3160 /* Announce the session's destruction to the notification thread when it is destroyed. */
3161 ret = session_add_destroy_notifier(
3162 new_session,
3163 [](const struct ltt_session *session, void *user_data __attribute__((unused))) {
3164 (void) notification_thread_command_remove_session(
3165 the_notification_thread_handle, session->id);
3166 },
3167 nullptr);
3168 if (ret) {
3169 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3170 new_session->name);
3171 ret = LTTNG_ERR_NOMEM;
3172 goto end;
3173 }
3174
3175 if (!session_name) {
3176 ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name);
3177 if (ret) {
3178 ret_code = LTTNG_ERR_SESSION_FAIL;
3179 goto end;
3180 }
3181 }
3182
3183 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor)) {
3184 /*
3185 * Only include the session's creation time in the output
3186 * destination if the name of the session itself was
3187 * not auto-generated.
3188 */
3189 ret_code = lttng_session_descriptor_set_default_output(
3190 descriptor,
3191 session_name ? &new_session->creation_time : nullptr,
3192 home_path);
3193 if (ret_code != LTTNG_OK) {
3194 goto end;
3195 }
3196 } else {
3197 new_session->has_user_specified_directory =
3198 lttng_session_descriptor_has_output_directory(descriptor);
3199 }
3200
3201 switch (lttng_session_descriptor_get_type(descriptor)) {
3202 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3203 new_session->snapshot_mode = 1;
3204 break;
3205 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3206 new_session->live_timer =
3207 lttng_session_descriptor_live_get_timer_interval(descriptor);
3208 break;
3209 default:
3210 break;
3211 }
3212
3213 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3214 if (ret_code != LTTNG_OK) {
3215 goto end;
3216 }
3217 new_session->consumer->enabled = true;
3218 ret_code = LTTNG_OK;
3219 end:
3220 /* Release reference provided by the session_create function. */
3221 session_put(new_session);
3222 if (ret_code != LTTNG_OK && new_session) {
3223 /* Release the global reference on error. */
3224 session_destroy(new_session);
3225 }
3226 session_unlock_list();
3227 return ret_code;
3228 }
3229
3230 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx,
3231 int sock,
3232 struct lttng_session_descriptor **return_descriptor)
3233 {
3234 int ret;
3235 size_t payload_size;
3236 struct lttng_dynamic_buffer payload;
3237 struct lttng_buffer_view home_dir_view;
3238 struct lttng_buffer_view session_descriptor_view;
3239 struct lttng_session_descriptor *session_descriptor = nullptr;
3240 enum lttng_error_code ret_code;
3241
3242 lttng_dynamic_buffer_init(&payload);
3243 if (cmd_ctx->lsm.u.create_session.home_dir_size >= LTTNG_PATH_MAX) {
3244 ret_code = LTTNG_ERR_INVALID;
3245 goto error;
3246 }
3247 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3248 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3249 ret_code = LTTNG_ERR_INVALID;
3250 goto error;
3251 }
3252
3253 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3254 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3255 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3256 if (ret) {
3257 ret_code = LTTNG_ERR_NOMEM;
3258 goto error;
3259 }
3260
3261 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3262 if (ret <= 0) {
3263 ERR("Reception of session descriptor failed, aborting.");
3264 ret_code = LTTNG_ERR_SESSION_FAIL;
3265 goto error;
3266 }
3267
3268 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3269 &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size);
3270 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3271 !lttng_buffer_view_is_valid(&home_dir_view)) {
3272 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3273 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3274 goto error;
3275 }
3276
3277 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3278 &payload,
3279 cmd_ctx->lsm.u.create_session.home_dir_size,
3280 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3281 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3282 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3283 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3284 goto error;
3285 }
3286
3287 ret = lttng_session_descriptor_create_from_buffer(&session_descriptor_view,
3288 &session_descriptor);
3289 if (ret < 0) {
3290 ERR("Failed to create session descriptor from payload of \"create session\" command");
3291 ret_code = LTTNG_ERR_INVALID;
3292 goto error;
3293 }
3294
3295 /*
3296 * Sets the descriptor's auto-generated properties (name, output) if
3297 * needed.
3298 */
3299 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3300 &cmd_ctx->creds,
3301 home_dir_view.size ? home_dir_view.data :
3302 nullptr);
3303 if (ret_code != LTTNG_OK) {
3304 goto error;
3305 }
3306
3307 ret_code = LTTNG_OK;
3308 *return_descriptor = session_descriptor;
3309 session_descriptor = nullptr;
3310 error:
3311 lttng_dynamic_buffer_reset(&payload);
3312 lttng_session_descriptor_destroy(session_descriptor);
3313 return ret_code;
3314 }
3315
3316 static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context)
3317 {
3318 int ret;
3319 ssize_t comm_ret;
3320 const struct cmd_destroy_session_reply_context *reply_context =
3321 (cmd_destroy_session_reply_context *) _reply_context;
3322 struct lttng_dynamic_buffer payload;
3323 struct lttcomm_session_destroy_command_header cmd_header;
3324 struct lttng_trace_archive_location *location = nullptr;
3325 struct lttcomm_lttng_msg llm = {
3326 .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION,
3327 .ret_code = reply_context->destruction_status,
3328 .pid = UINT32_MAX,
3329 .cmd_header_size = sizeof(struct lttcomm_session_destroy_command_header),
3330 .data_size = 0,
3331 .fd_count = 0,
3332 };
3333 size_t payload_size_before_location;
3334
3335 lttng_dynamic_buffer_init(&payload);
3336
3337 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3338 if (ret) {
3339 ERR("Failed to append session destruction message");
3340 goto error;
3341 }
3342
3343 cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ?
3344 session->rotation_state :
3345 LTTNG_ROTATION_STATE_NO_ROTATION);
3346 ret = lttng_dynamic_buffer_append(&payload, &cmd_header, sizeof(cmd_header));
3347 if (ret) {
3348 ERR("Failed to append session destruction command header");
3349 goto error;
3350 }
3351
3352 if (!reply_context->implicit_rotation_on_destroy) {
3353 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3354 session->name);
3355 goto send_reply;
3356 }
3357 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3358 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3359 session->name);
3360 goto send_reply;
3361 }
3362
3363 location = session_get_trace_archive_location(session);
3364 if (!location) {
3365 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3366 session->name);
3367 goto error;
3368 }
3369
3370 payload_size_before_location = payload.size;
3371 comm_ret = lttng_trace_archive_location_serialize(location, &payload);
3372 lttng_trace_archive_location_put(location);
3373 if (comm_ret < 0) {
3374 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3375 session->name);
3376 goto error;
3377 }
3378 /* Update the message to indicate the location's length. */
3379 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3380 payload.size - payload_size_before_location;
3381 send_reply:
3382 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, payload.data, payload.size);
3383 if (comm_ret != (ssize_t) payload.size) {
3384 ERR("Failed to send result of the destruction of session \"%s\" to client",
3385 session->name);
3386 }
3387 error:
3388 ret = close(reply_context->reply_sock_fd);
3389 if (ret) {
3390 PERROR("Failed to close client socket in deferred session destroy reply");
3391 }
3392 lttng_dynamic_buffer_reset(&payload);
3393 free(_reply_context);
3394 }
3395
3396 /*
3397 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3398 *
3399 * Called with session lock held.
3400 */
3401 int cmd_destroy_session(struct ltt_session *session, int *sock_fd)
3402 {
3403 int ret;
3404 enum lttng_error_code destruction_last_error = LTTNG_OK;
3405 struct cmd_destroy_session_reply_context *reply_context = nullptr;
3406
3407 if (sock_fd) {
3408 reply_context = zmalloc<cmd_destroy_session_reply_context>();
3409 if (!reply_context) {
3410 ret = LTTNG_ERR_NOMEM;
3411 goto end;
3412 }
3413
3414 reply_context->reply_sock_fd = *sock_fd;
3415 }
3416
3417 /* Safety net */
3418 LTTNG_ASSERT(session);
3419
3420 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id);
3421 if (session->active) {
3422 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3423 session->name);
3424 ret = cmd_stop_trace(session);
3425 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3426 /* Carry on with the destruction of the session. */
3427 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3428 session->name,
3429 lttng_strerror(-ret));
3430 destruction_last_error = (lttng_error_code) ret;
3431 }
3432 }
3433
3434 if (session->rotation_schedule_timer_enabled) {
3435 if (timer_session_rotation_schedule_timer_stop(session)) {
3436 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3437 session->name);
3438 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3439 }
3440 }
3441
3442 if (session->rotate_size) {
3443 try {
3444 the_rotation_thread_handle->unsubscribe_session_consumed_size_rotation(
3445 *session);
3446 } catch (const std::exception& e) {
3447 /* Continue the destruction of the session anyway. */
3448 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3449 e.what());
3450 }
3451
3452 session->rotate_size = 0;
3453 }
3454
3455 if (session->rotated && session->current_trace_chunk && session->output_traces) {
3456 /*
3457 * Perform a last rotation on destruction if rotations have
3458 * occurred during the session's lifetime.
3459 */
3460 ret = cmd_rotate_session(
3461 session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
3462 if (ret != LTTNG_OK) {
3463 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3464 session->name,
3465 lttng_strerror(-ret));
3466 destruction_last_error = (lttng_error_code) -ret;
3467 }
3468 if (reply_context) {
3469 reply_context->implicit_rotation_on_destroy = true;
3470 }
3471 } else if (session->has_been_started && session->current_trace_chunk) {
3472 /*
3473 * The user has not triggered a session rotation. However, to
3474 * ensure all data has been consumed, the session is rotated
3475 * to a 'null' trace chunk before it is destroyed.
3476 *
3477 * This is a "quiet" rotation meaning that no notification is
3478 * emitted and no renaming of the current trace chunk takes
3479 * place.
3480 */
3481 ret = cmd_rotate_session(
3482 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
3483 /*
3484 * Rotation operations may not be supported by the kernel
3485 * tracer. Hence, do not consider this implicit rotation as
3486 * a session destruction error. The library has already stopped
3487 * the session and waited for pending data; there is nothing
3488 * left to do but complete the destruction of the session.
3489 */
3490 if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
3491 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3492 session->name,
3493 lttng_strerror(ret));
3494 destruction_last_error = (lttng_error_code) -ret;
3495 }
3496 }
3497
3498 if (session->shm_path[0]) {
3499 /*
3500 * When a session is created with an explicit shm_path,
3501 * the consumer daemon will create its shared memory files
3502 * at that location and will *not* unlink them. This is normal
3503 * as the intention of that feature is to make it possible
3504 * to retrieve the content of those files should a crash occur.
3505 *
3506 * To ensure the content of those files can be used, the
3507 * sessiond daemon will replicate the content of the metadata
3508 * cache in a metadata file.
3509 *
3510 * On clean-up, it is expected that the consumer daemon will
3511 * unlink the shared memory files and that the session daemon
3512 * will unlink the metadata file. Then, the session's directory
3513 * in the shm path can be removed.
3514 *
3515 * Unfortunately, a flaw in the design of the sessiond's and
3516 * consumerd's tear down of channels makes it impossible to
3517 * determine when the sessiond _and_ the consumerd have both
3518 * destroyed their representation of a channel. For one, the
3519 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3520 * callbacks in both daemons.
3521 *
3522 * However, it is also impossible for the sessiond to know when
3523 * the consumer daemon is done destroying its channel(s) since
3524 * it occurs as a reaction to the closing of the channel's file
3525 * descriptor. There is no resulting communication initiated
3526 * from the consumerd to the sessiond to confirm that the
3527 * operation is completed (and was successful).
3528 *
3529 * Until this is all fixed, the session daemon checks for the
3530 * removal of the session's shm path which makes it possible
3531 * to safely advertise a session as having been destroyed.
3532 *
3533 * Prior to this fix, it was not possible to reliably save
3534 * a session making use of the --shm-path option, destroy it,
3535 * and load it again. This is because the creation of the
3536 * session would fail upon seeing the session's shm path
3537 * already in existence.
3538 *
3539 * Note that none of the error paths in the check for the
3540 * directory's existence return an error. This is normal
3541 * as there isn't much that can be done. The session will
3542 * be destroyed properly, except that we can't offer the
3543 * guarantee that the same session can be re-created.
3544 */
3545 current_completion_handler = &destroy_completion_handler.handler;
3546 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3547 session->shm_path,
3548 sizeof(destroy_completion_handler.shm_path));
3549 LTTNG_ASSERT(!ret);
3550 }
3551
3552 /*
3553 * The session is destroyed. However, note that the command context
3554 * still holds a reference to the session, thus delaying its destruction
3555 * _at least_ up to the point when that reference is released.
3556 */
3557 session_destroy(session);
3558 if (reply_context) {
3559 reply_context->destruction_status = destruction_last_error;
3560 ret = session_add_destroy_notifier(
3561 session, cmd_destroy_session_reply, (void *) reply_context);
3562 if (ret) {
3563 ret = LTTNG_ERR_FATAL;
3564 goto end;
3565 } else {
3566 *sock_fd = -1;
3567 }
3568 }
3569 ret = LTTNG_OK;
3570 end:
3571 return ret;
3572 }
3573
3574 /*
3575 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3576 */
3577 int cmd_register_consumer(struct ltt_session *session,
3578 enum lttng_domain_type domain,
3579 const char *sock_path,
3580 struct consumer_data *cdata)
3581 {
3582 int ret, sock;
3583 struct consumer_socket *socket = nullptr;
3584
3585 LTTNG_ASSERT(session);
3586 LTTNG_ASSERT(cdata);
3587 LTTNG_ASSERT(sock_path);
3588
3589 switch (domain) {
3590 case LTTNG_DOMAIN_KERNEL:
3591 {
3592 struct ltt_kernel_session *ksess = session->kernel_session;
3593
3594 LTTNG_ASSERT(ksess);
3595
3596 /* Can't register a consumer if there is already one */
3597 if (ksess->consumer_fds_sent != 0) {
3598 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3599 goto error;
3600 }
3601
3602 sock = lttcomm_connect_unix_sock(sock_path);
3603 if (sock < 0) {
3604 ret = LTTNG_ERR_CONNECT_FAIL;
3605 goto error;
3606 }
3607 cdata->cmd_sock = sock;
3608
3609 socket = consumer_allocate_socket(&cdata->cmd_sock);
3610 if (socket == nullptr) {
3611 ret = close(sock);
3612 if (ret < 0) {
3613 PERROR("close register consumer");
3614 }
3615 cdata->cmd_sock = -1;
3616 ret = LTTNG_ERR_FATAL;
3617 goto error;
3618 }
3619
3620 socket->lock = zmalloc<pthread_mutex_t>();
3621 if (socket->lock == nullptr) {
3622 PERROR("zmalloc pthread mutex");
3623 ret = LTTNG_ERR_FATAL;
3624 goto error;
3625 }
3626
3627 pthread_mutex_init(socket->lock, nullptr);
3628 socket->registered = 1;
3629
3630 lttng::urcu::read_lock_guard read_lock;
3631 consumer_add_socket(socket, ksess->consumer);
3632
3633 pthread_mutex_lock(&cdata->pid_mutex);
3634 cdata->pid = -1;
3635 pthread_mutex_unlock(&cdata->pid_mutex);
3636
3637 break;
3638 }
3639 default:
3640 /* TODO: Userspace tracing */
3641 ret = LTTNG_ERR_UND;
3642 goto error;
3643 }
3644
3645 return LTTNG_OK;
3646
3647 error:
3648 if (socket) {
3649 consumer_destroy_socket(socket);
3650 }
3651 return ret;
3652 }
3653
3654 /*
3655 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3656 */
3657 ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains)
3658 {
3659 int ret, index = 0;
3660 ssize_t nb_dom = 0;
3661 struct agent *agt;
3662 struct lttng_ht_iter iter;
3663
3664 if (session->kernel_session != nullptr) {
3665 DBG3("Listing domains found kernel domain");
3666 nb_dom++;
3667 }
3668
3669 if (session->ust_session != nullptr) {
3670 DBG3("Listing domains found UST global domain");
3671 nb_dom++;
3672
3673 lttng::urcu::read_lock_guard read_lock;
3674
3675 cds_lfht_for_each_entry (
3676 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3677 if (agt->being_used) {
3678 nb_dom++;
3679 }
3680 }
3681 }
3682
3683 if (!nb_dom) {
3684 goto end;
3685 }
3686
3687 *domains = calloc<lttng_domain>(nb_dom);
3688 if (*domains == nullptr) {
3689 ret = LTTNG_ERR_FATAL;
3690 goto error;
3691 }
3692
3693 if (session->kernel_session != nullptr) {
3694 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3695
3696 /* Kernel session buffer type is always GLOBAL */
3697 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3698
3699 index++;
3700 }
3701
3702 if (session->ust_session != nullptr) {
3703 (*domains)[index].type = LTTNG_DOMAIN_UST;
3704 (*domains)[index].buf_type = session->ust_session->buffer_type;
3705 index++;
3706
3707 {
3708 lttng::urcu::read_lock_guard read_lock;
3709
3710 cds_lfht_for_each_entry (
3711 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3712 if (agt->being_used) {
3713 (*domains)[index].type = agt->domain;
3714 (*domains)[index].buf_type =
3715 session->ust_session->buffer_type;
3716 index++;
3717 }
3718 }
3719 }
3720 }
3721 end:
3722 return nb_dom;
3723
3724 error:
3725 /* Return negative value to differentiate return code */
3726 return -ret;
3727 }
3728
3729 /*
3730 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3731 */
3732 enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
3733 struct ltt_session *session,
3734 struct lttng_payload *payload)
3735 {
3736 int ret = 0;
3737 unsigned int i = 0;
3738 struct lttcomm_list_command_header cmd_header = {};
3739 size_t cmd_header_offset;
3740 enum lttng_error_code ret_code;
3741
3742 assert(session);
3743 assert(payload);
3744
3745 DBG("Listing channels for session %s", session->name);
3746
3747 cmd_header_offset = payload->buffer.size;
3748
3749 /* Reserve space for command reply header. */
3750 ret = lttng_dynamic_buffer_set_size(&payload->buffer,
3751 cmd_header_offset + sizeof(cmd_header));
3752 if (ret) {
3753 ret_code = LTTNG_ERR_NOMEM;
3754 goto end;
3755 }
3756
3757 switch (domain) {
3758 case LTTNG_DOMAIN_KERNEL:
3759 {
3760 /* Kernel channels */
3761 struct ltt_kernel_channel *kchan;
3762 if (session->kernel_session != nullptr) {
3763 cds_list_for_each_entry (
3764 kchan, &session->kernel_session->channel_list.head, list) {
3765 uint64_t discarded_events, lost_packets;
3766 struct lttng_channel_extended *extended;
3767
3768 extended = (struct lttng_channel_extended *)
3769 kchan->channel->attr.extended.ptr;
3770
3771 ret = get_kernel_runtime_stats(
3772 session, kchan, &discarded_events, &lost_packets);
3773 if (ret < 0) {
3774 ret_code = LTTNG_ERR_UNK;
3775 goto end;
3776 }
3777
3778 /*
3779 * Update the discarded_events and lost_packets
3780 * count for the channel
3781 */
3782 extended->discarded_events = discarded_events;
3783 extended->lost_packets = lost_packets;
3784
3785 ret = lttng_channel_serialize(kchan->channel, &payload->buffer);
3786 if (ret) {
3787 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3788 kchan->channel->name);
3789 ret_code = LTTNG_ERR_UNK;
3790 goto end;
3791 }
3792
3793 i++;
3794 }
3795 }
3796 break;
3797 }
3798 case LTTNG_DOMAIN_UST:
3799 {
3800 struct lttng_ht_iter iter;
3801 struct ltt_ust_channel *uchan;
3802
3803 {
3804 lttng::urcu::read_lock_guard read_lock;
3805
3806 cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht,
3807 &iter.iter,
3808 uchan,
3809 node.node) {
3810 uint64_t discarded_events = 0, lost_packets = 0;
3811 struct lttng_channel *channel = nullptr;
3812 struct lttng_channel_extended *extended;
3813
3814 channel = trace_ust_channel_to_lttng_channel(uchan);
3815 if (!channel) {
3816 ret_code = LTTNG_ERR_NOMEM;
3817 goto end;
3818 }
3819
3820 extended = (struct lttng_channel_extended *)
3821 channel->attr.extended.ptr;
3822
3823 ret = get_ust_runtime_stats(
3824 session, uchan, &discarded_events, &lost_packets);
3825 if (ret < 0) {
3826 lttng_channel_destroy(channel);
3827 ret_code = LTTNG_ERR_UNK;
3828 goto end;
3829 }
3830
3831 extended->discarded_events = discarded_events;
3832 extended->lost_packets = lost_packets;
3833
3834 ret = lttng_channel_serialize(channel, &payload->buffer);
3835 if (ret) {
3836 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3837 channel->name);
3838 lttng_channel_destroy(channel);
3839 ret_code = LTTNG_ERR_UNK;
3840 goto end;
3841 }
3842
3843 lttng_channel_destroy(channel);
3844 i++;
3845 }
3846 }
3847
3848 break;
3849 }
3850 default:
3851 break;
3852 }
3853
3854 if (i > UINT32_MAX) {
3855 ERR("Channel count would overflow the channel listing command's reply");
3856 ret_code = LTTNG_ERR_OVERFLOW;
3857 goto end;
3858 }
3859
3860 /* Update command reply header. */
3861 cmd_header.count = (uint32_t) i;
3862 memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header));
3863 ret_code = LTTNG_OK;
3864
3865 end:
3866 return ret_code;
3867 }
3868
3869 /*
3870 * Command LTTNG_LIST_EVENTS processed by the client thread.
3871 */
3872 enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
3873 struct ltt_session *session,
3874 char *channel_name,
3875 struct lttng_payload *reply_payload)
3876 {
3877 int buffer_resize_ret;
3878 enum lttng_error_code ret_code = LTTNG_OK;
3879 struct lttcomm_list_command_header reply_command_header = {};
3880 size_t reply_command_header_offset;
3881 unsigned int nb_events = 0;
3882
3883 assert(reply_payload);
3884
3885 /* Reserve space for command reply header. */
3886 reply_command_header_offset = reply_payload->buffer.size;
3887 buffer_resize_ret = lttng_dynamic_buffer_set_size(
3888 &reply_payload->buffer,
3889 reply_command_header_offset + sizeof(struct lttcomm_list_command_header));
3890 if (buffer_resize_ret) {
3891 ret_code = LTTNG_ERR_NOMEM;
3892 goto end;
3893 }
3894
3895 switch (domain) {
3896 case LTTNG_DOMAIN_KERNEL:
3897 if (session->kernel_session != nullptr) {
3898 ret_code = list_lttng_kernel_events(
3899 channel_name, session->kernel_session, reply_payload, &nb_events);
3900 }
3901
3902 break;
3903 case LTTNG_DOMAIN_UST:
3904 {
3905 if (session->ust_session != nullptr) {
3906 ret_code =
3907 list_lttng_ust_global_events(channel_name,
3908 &session->ust_session->domain_global,
3909 reply_payload,
3910 &nb_events);
3911 }
3912
3913 break;
3914 }
3915 case LTTNG_DOMAIN_LOG4J:
3916 case LTTNG_DOMAIN_JUL:
3917 case LTTNG_DOMAIN_PYTHON:
3918 if (session->ust_session) {
3919 struct lttng_ht_iter iter;
3920 struct agent *agt;
3921
3922 lttng::urcu::read_lock_guard read_lock;
3923
3924 cds_lfht_for_each_entry (
3925 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3926 if (agt->domain == domain) {
3927 ret_code = list_lttng_agent_events(
3928 agt, reply_payload, &nb_events);
3929 break;
3930 }
3931 }
3932 }
3933 break;
3934 default:
3935 ret_code = LTTNG_ERR_UND;
3936 break;
3937 }
3938
3939 if (nb_events > UINT32_MAX) {
3940 ret_code = LTTNG_ERR_OVERFLOW;
3941 goto end;
3942 }
3943
3944 /* Update command reply header. */
3945 reply_command_header.count = (uint32_t) nb_events;
3946 memcpy(reply_payload->buffer.data + reply_command_header_offset,
3947 &reply_command_header,
3948 sizeof(reply_command_header));
3949
3950 end:
3951 return ret_code;
3952 }
3953
3954 /*
3955 * Using the session list, filled a lttng_session array to send back to the
3956 * client for session listing.
3957 *
3958 * The session list lock MUST be acquired before calling this function. Use
3959 * session_lock_list() and session_unlock_list().
3960 */
3961 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3962 size_t session_count,
3963 uid_t uid,
3964 gid_t gid)
3965 {
3966 int ret;
3967 unsigned int i = 0;
3968 struct ltt_session *session;
3969 struct ltt_session_list *list = session_get_list();
3970 struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
3971
3972 DBG("Getting all available session for UID %d GID %d", uid, gid);
3973 /*
3974 * Iterate over session list and append data after the control struct in
3975 * the buffer.
3976 */
3977 cds_list_for_each_entry (session, &list->head, list) {
3978 if (!session_get(session)) {
3979 continue;
3980 }
3981 /*
3982 * Only list the sessions the user can control.
3983 */
3984 if (!session_access_ok(session, uid) || session->destroyed) {
3985 session_put(session);
3986 continue;
3987 }
3988
3989 struct ltt_kernel_session *ksess = session->kernel_session;
3990 struct ltt_ust_session *usess = session->ust_session;
3991
3992 if (session->consumer->type == CONSUMER_DST_NET ||
3993 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3994 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3995 ret = build_network_session_path(
3996 sessions[i].path, sizeof(sessions[i].path), session);
3997 } else {
3998 ret = snprintf(sessions[i].path,
3999 sizeof(sessions[i].path),
4000 "%s",
4001 session->consumer->dst.session_root_path);
4002 }
4003 if (ret < 0) {
4004 PERROR("snprintf session path");
4005 session_put(session);
4006 continue;
4007 }
4008
4009 strncpy(sessions[i].name, session->name, NAME_MAX);
4010 sessions[i].name[NAME_MAX - 1] = '\0';
4011 sessions[i].enabled = session->active;
4012 sessions[i].snapshot_mode = session->snapshot_mode;
4013 sessions[i].live_timer_interval = session->live_timer;
4014 extended[i].creation_time.value = (uint64_t) session->creation_time;
4015 extended[i].creation_time.is_set = 1;
4016 i++;
4017 session_put(session);
4018 }
4019 }
4020
4021 /*
4022 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4023 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4024 */
4025 int cmd_data_pending(struct ltt_session *session)
4026 {
4027 int ret;
4028 struct ltt_kernel_session *ksess = session->kernel_session;
4029 struct ltt_ust_session *usess = session->ust_session;
4030
4031 LTTNG_ASSERT(session);
4032
4033 DBG("Data pending for session %s", session->name);
4034
4035 /* Session MUST be stopped to ask for data availability. */
4036 if (session->active) {
4037 ret = LTTNG_ERR_SESSION_STARTED;
4038 goto error;
4039 } else {
4040 /*
4041 * If stopped, just make sure we've started before else the above call
4042 * will always send that there is data pending.
4043 *
4044 * The consumer assumes that when the data pending command is received,
4045 * the trace has been started before or else no output data is written
4046 * by the streams which is a condition for data pending. So, this is
4047 * *VERY* important that we don't ask the consumer before a start
4048 * trace.
4049 */
4050 if (!session->has_been_started) {
4051 ret = 0;
4052 goto error;
4053 }
4054 }
4055
4056 /* A rotation is still pending, we have to wait. */
4057 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
4058 DBG("Rotate still pending for session %s", session->name);
4059 ret = 1;
4060 goto error;
4061 }
4062
4063 if (ksess && ksess->consumer) {
4064 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4065 if (ret == 1) {
4066 /* Data is still being extracted for the kernel. */
4067 goto error;
4068 }
4069 }
4070
4071 if (usess && usess->consumer) {
4072 ret = consumer_is_data_pending(usess->id, usess->consumer);
4073 if (ret == 1) {
4074 /* Data is still being extracted for the kernel. */
4075 goto error;
4076 }
4077 }
4078
4079 /* Data is ready to be read by a viewer */
4080 ret = 0;
4081
4082 error:
4083 return ret;
4084 }
4085
4086 /*
4087 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4088 *
4089 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4090 */
4091 int cmd_snapshot_add_output(struct ltt_session *session,
4092 const struct lttng_snapshot_output *output,
4093 uint32_t *id)
4094 {
4095 int ret;
4096 struct snapshot_output *new_output;
4097
4098 LTTNG_ASSERT(session);
4099 LTTNG_ASSERT(output);
4100
4101 DBG("Cmd snapshot add output for session %s", session->name);
4102
4103 /*
4104 * Can't create an output if the session is not set in no-output mode.
4105 */
4106 if (session->output_traces) {
4107 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4108 goto error;
4109 }
4110
4111 if (session->has_non_mmap_channel) {
4112 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4113 goto error;
4114 }
4115
4116 /* Only one output is allowed until we have the "tee" feature. */
4117 if (session->snapshot.nb_output == 1) {
4118 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4119 goto error;
4120 }
4121
4122 new_output = snapshot_output_alloc();
4123 if (!new_output) {
4124 ret = LTTNG_ERR_NOMEM;
4125 goto error;
4126 }
4127
4128 ret = snapshot_output_init(session,
4129 output->max_size,
4130 output->name,
4131 output->ctrl_url,
4132 output->data_url,
4133 session->consumer,
4134 new_output,
4135 &session->snapshot);
4136 if (ret < 0) {
4137 if (ret == -ENOMEM) {
4138 ret = LTTNG_ERR_NOMEM;
4139 } else {
4140 ret = LTTNG_ERR_INVALID;
4141 }
4142 goto free_error;
4143 }
4144
4145 snapshot_add_output(&session->snapshot, new_output);
4146 if (id) {
4147 *id = new_output->id;
4148 }
4149
4150 return LTTNG_OK;
4151
4152 free_error:
4153 snapshot_output_destroy(new_output);
4154 error:
4155 return ret;
4156 }
4157
4158 /*
4159 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4160 *
4161 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4162 */
4163 int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
4164 {
4165 int ret;
4166 struct snapshot_output *sout = nullptr;
4167
4168 LTTNG_ASSERT(session);
4169 LTTNG_ASSERT(output);
4170
4171 lttng::urcu::read_lock_guard read_lock;
4172
4173 /*
4174 * Permission denied to create an output if the session is not
4175 * set in no output mode.
4176 */
4177 if (session->output_traces) {
4178 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4179 goto error;
4180 }
4181
4182 if (output->id) {
4183 DBG("Cmd snapshot del output id %" PRIu32 " for session %s",
4184 output->id,
4185 session->name);
4186 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4187 } else if (*output->name != '\0') {
4188 DBG("Cmd snapshot del output name %s for session %s", output->name, session->name);
4189 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4190 }
4191 if (!sout) {
4192 ret = LTTNG_ERR_INVALID;
4193 goto error;
4194 }
4195
4196 snapshot_delete_output(&session->snapshot, sout);
4197 snapshot_output_destroy(sout);
4198 ret = LTTNG_OK;
4199
4200 error:
4201 return ret;
4202 }
4203
4204 /*
4205 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4206 *
4207 * If no output is available, outputs is untouched and 0 is returned.
4208 *
4209 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4210 */
4211 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
4212 struct lttng_snapshot_output **outputs)
4213 {
4214 int ret, idx = 0;
4215 struct lttng_snapshot_output *list = nullptr;
4216 struct lttng_ht_iter iter;
4217 struct snapshot_output *output;
4218
4219 LTTNG_ASSERT(session);
4220 LTTNG_ASSERT(outputs);
4221
4222 DBG("Cmd snapshot list outputs for session %s", session->name);
4223
4224 /*
4225 * Permission denied to create an output if the session is not
4226 * set in no output mode.
4227 */
4228 if (session->output_traces) {
4229 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4230 goto end;
4231 }
4232
4233 if (session->snapshot.nb_output == 0) {
4234 ret = 0;
4235 goto end;
4236 }
4237
4238 list = calloc<lttng_snapshot_output>(session->snapshot.nb_output);
4239 if (!list) {
4240 ret = -LTTNG_ERR_NOMEM;
4241 goto end;
4242 }
4243
4244 /* Copy list from session to the new list object. */
4245 {
4246 lttng::urcu::read_lock_guard read_lock;
4247
4248 cds_lfht_for_each_entry (
4249 session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
4250 LTTNG_ASSERT(output->consumer);
4251 list[idx].id = output->id;
4252 list[idx].max_size = output->max_size;
4253 if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) {
4254 ret = -LTTNG_ERR_INVALID;
4255 goto error;
4256 }
4257
4258 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4259 if (lttng_strncpy(list[idx].ctrl_url,
4260 output->consumer->dst.session_root_path,
4261 sizeof(list[idx].ctrl_url))) {
4262 ret = -LTTNG_ERR_INVALID;
4263 goto error;
4264 }
4265 } else {
4266 /* Control URI. */
4267 ret = uri_to_str_url(&output->consumer->dst.net.control,
4268 list[idx].ctrl_url,
4269 sizeof(list[idx].ctrl_url));
4270 if (ret < 0) {
4271 ret = -LTTNG_ERR_NOMEM;
4272 goto error;
4273 }
4274
4275 /* Data URI. */
4276 ret = uri_to_str_url(&output->consumer->dst.net.data,
4277 list[idx].data_url,
4278 sizeof(list[idx].data_url));
4279 if (ret < 0) {
4280 ret = -LTTNG_ERR_NOMEM;
4281 goto error;
4282 }
4283 }
4284
4285 idx++;
4286 }
4287 }
4288
4289 *outputs = list;
4290 list = nullptr;
4291 ret = session->snapshot.nb_output;
4292 error:
4293 free(list);
4294 end:
4295 return ret;
4296 }
4297
4298 /*
4299 * Check if we can regenerate the metadata for this session.
4300 * Only kernel, UST per-uid and non-live sessions are supported.
4301 *
4302 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4303 */
4304 static int check_regenerate_metadata_support(struct ltt_session *session)
4305 {
4306 int ret;
4307
4308 LTTNG_ASSERT(session);
4309
4310 if (session->live_timer != 0) {
4311 ret = LTTNG_ERR_LIVE_SESSION;
4312 goto end;
4313 }
4314 if (!session->active) {
4315 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4316 goto end;
4317 }
4318 if (session->ust_session) {
4319 switch (session->ust_session->buffer_type) {
4320 case LTTNG_BUFFER_PER_UID:
4321 break;
4322 case LTTNG_BUFFER_PER_PID:
4323 ret = LTTNG_ERR_PER_PID_SESSION;
4324 goto end;
4325 default:
4326 abort();
4327 ret = LTTNG_ERR_UNK;
4328 goto end;
4329 }
4330 }
4331 if (session->consumer->type == CONSUMER_DST_NET &&
4332 session->consumer->relay_minor_version < 8) {
4333 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4334 goto end;
4335 }
4336 ret = 0;
4337
4338 end:
4339 return ret;
4340 }
4341
4342 /*
4343 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4344 *
4345 * Ask the consumer to truncate the existing metadata file(s) and
4346 * then regenerate the metadata. Live and per-pid sessions are not
4347 * supported and return an error.
4348 *
4349 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4350 */
4351 int cmd_regenerate_metadata(struct ltt_session *session)
4352 {
4353 int ret;
4354
4355 LTTNG_ASSERT(session);
4356
4357 ret = check_regenerate_metadata_support(session);
4358 if (ret) {
4359 goto end;
4360 }
4361
4362 if (session->kernel_session) {
4363 ret = kernctl_session_regenerate_metadata(session->kernel_session->fd);
4364 if (ret < 0) {
4365 ERR("Failed to regenerate the kernel metadata");
4366 goto end;
4367 }
4368 }
4369
4370 if (session->ust_session) {
4371 ret = trace_ust_regenerate_metadata(session->ust_session);
4372 if (ret < 0) {
4373 ERR("Failed to regenerate the UST metadata");
4374 goto end;
4375 }
4376 }
4377 DBG("Cmd metadata regenerate for session %s", session->name);
4378 ret = LTTNG_OK;
4379
4380 end:
4381 return ret;
4382 }
4383
4384 /*
4385 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4386 *
4387 * Ask the tracer to regenerate a new statedump.
4388 *
4389 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4390 */
4391 int cmd_regenerate_statedump(struct ltt_session *session)
4392 {
4393 int ret;
4394
4395 LTTNG_ASSERT(session);
4396
4397 if (!session->active) {
4398 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4399 goto end;
4400 }
4401
4402 if (session->kernel_session) {
4403 ret = kernctl_session_regenerate_statedump(session->kernel_session->fd);
4404 /*
4405 * Currently, the statedump in kernel can only fail if out
4406 * of memory.
4407 */
4408 if (ret < 0) {
4409 if (ret == -ENOMEM) {
4410 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4411 } else {
4412 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4413 }
4414 ERR("Failed to regenerate the kernel statedump");
4415 goto end;
4416 }
4417 }
4418
4419 if (session->ust_session) {
4420 ret = ust_app_regenerate_statedump_all(session->ust_session);
4421 /*
4422 * Currently, the statedump in UST always returns 0.
4423 */
4424 if (ret < 0) {
4425 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4426 ERR("Failed to regenerate the UST statedump");
4427 goto end;
4428 }
4429 }
4430 DBG("Cmd regenerate statedump for session %s", session->name);
4431 ret = LTTNG_OK;
4432
4433 end:
4434 return ret;
4435 }
4436
4437 static enum lttng_error_code
4438 synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread,
4439 struct lttng_trigger *trigger,
4440 const struct lttng_credentials *cmd_creds)
4441 {
4442 enum lttng_error_code ret_code;
4443 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4444 const char *trigger_name;
4445 uid_t trigger_owner;
4446 enum lttng_trigger_status trigger_status;
4447 const enum lttng_domain_type trigger_domain =
4448 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4449
4450 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4451 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4452
4453 LTTNG_ASSERT(condition);
4454 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4455 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4456
4457 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4458 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4459
4460 session_lock_list();
4461 switch (trigger_domain) {
4462 case LTTNG_DOMAIN_KERNEL:
4463 {
4464 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4465 if (ret_code != LTTNG_OK) {
4466 enum lttng_error_code notif_thread_unregister_ret;
4467
4468 notif_thread_unregister_ret =
4469 notification_thread_command_unregister_trigger(notification_thread,
4470 trigger);
4471
4472 if (notif_thread_unregister_ret != LTTNG_OK) {
4473 /* Return the original error code. */
4474 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4475 trigger_name,
4476 (int) trigger_owner,
4477 ret_code);
4478 }
4479
4480 goto end_unlock_session_list;
4481 }
4482 break;
4483 }
4484 case LTTNG_DOMAIN_UST:
4485 ust_app_global_update_all_event_notifier_rules();
4486 break;
4487 case LTTNG_DOMAIN_JUL:
4488 case LTTNG_DOMAIN_LOG4J:
4489 case LTTNG_DOMAIN_PYTHON:
4490 {
4491 /* Agent domains. */
4492 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4493
4494 if (!agt) {
4495 agt = agent_create(trigger_domain);
4496 if (!agt) {
4497 ret_code = LTTNG_ERR_NOMEM;
4498 goto end_unlock_session_list;
4499 }
4500
4501 agent_add(agt, the_trigger_agents_ht_by_domain);
4502 }
4503
4504 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
4505 if (ret_code != LTTNG_OK) {
4506 goto end_unlock_session_list;
4507 }
4508
4509 break;
4510 }
4511 case LTTNG_DOMAIN_NONE:
4512 default:
4513 abort();
4514 }
4515
4516 ret_code = LTTNG_OK;
4517 end_unlock_session_list:
4518 session_unlock_list();
4519 return ret_code;
4520 }
4521
4522 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4523 struct lttng_trigger *trigger,
4524 bool is_trigger_anonymous,
4525 struct notification_thread_handle *notification_thread,
4526 struct lttng_trigger **return_trigger)
4527 {
4528 enum lttng_error_code ret_code;
4529 const char *trigger_name;
4530 uid_t trigger_owner;
4531 enum lttng_trigger_status trigger_status;
4532
4533 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4534 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4535
4536 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4537 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4538
4539 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4540 trigger_name,
4541 (int) trigger_owner,
4542 (int) lttng_credentials_get_uid(cmd_creds));
4543
4544 /*
4545 * Validate the trigger credentials against the command credentials.
4546 * Only the root user can register a trigger with non-matching
4547 * credentials.
4548 */
4549 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4550 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4551 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4552 trigger_name,
4553 (int) trigger_owner,
4554 (int) lttng_credentials_get_uid(cmd_creds));
4555 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4556 goto end;
4557 }
4558 }
4559
4560 /*
4561 * The bytecode generation also serves as a validation step for the
4562 * bytecode expressions.
4563 */
4564 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4565 if (ret_code != LTTNG_OK) {
4566 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4567 trigger_name,
4568 (int) trigger_owner,
4569 ret_code);
4570 goto end;
4571 }
4572
4573 /*
4574 * A reference to the trigger is acquired by the notification thread.
4575 * It is safe to return the same trigger to the caller since it the
4576 * other user holds a reference.
4577 *
4578 * The trigger is modified during the execution of the
4579 * "register trigger" command. However, by the time the command returns,
4580 * it is safe to use without any locking as its properties are
4581 * immutable.
4582 */
4583 ret_code = notification_thread_command_register_trigger(
4584 notification_thread, trigger, is_trigger_anonymous);
4585 if (ret_code != LTTNG_OK) {
4586 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4587 trigger_name,
4588 (int) trigger_owner,
4589 ret_code);
4590 goto end;
4591 }
4592
4593 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4594 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4595
4596 /*
4597 * Synchronize tracers if the trigger adds an event notifier.
4598 */
4599 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4600 ret_code = synchronize_tracer_notifier_register(
4601 notification_thread, trigger, cmd_creds);
4602 if (ret_code != LTTNG_OK) {
4603 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
4604 goto end;
4605 }
4606 }
4607
4608 /*
4609 * Return an updated trigger to the client.
4610 *
4611 * Since a modified version of the same trigger is returned, acquire a
4612 * reference to the trigger so the caller doesn't have to care if those
4613 * are distinct instances or not.
4614 */
4615 if (ret_code == LTTNG_OK) {
4616 lttng_trigger_get(trigger);
4617 *return_trigger = trigger;
4618 /* Ownership of trigger was transferred to caller. */
4619 trigger = nullptr;
4620 }
4621 end:
4622 return ret_code;
4623 }
4624
4625 static enum lttng_error_code
4626 synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
4627 {
4628 enum lttng_error_code ret_code;
4629 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4630 const enum lttng_domain_type trigger_domain =
4631 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4632
4633 LTTNG_ASSERT(condition);
4634 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4635 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4636
4637 session_lock_list();
4638 switch (trigger_domain) {
4639 case LTTNG_DOMAIN_KERNEL:
4640 ret_code = kernel_unregister_event_notifier(trigger);
4641 if (ret_code != LTTNG_OK) {
4642 goto end_unlock_session_list;
4643 }
4644
4645 break;
4646 case LTTNG_DOMAIN_UST:
4647 ust_app_global_update_all_event_notifier_rules();
4648 break;
4649 case LTTNG_DOMAIN_JUL:
4650 case LTTNG_DOMAIN_LOG4J:
4651 case LTTNG_DOMAIN_PYTHON:
4652 {
4653 /* Agent domains. */
4654 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4655
4656 /*
4657 * This trigger was never registered in the first place. Calling
4658 * this function under those circumstances is an internal error.
4659 */
4660 LTTNG_ASSERT(agt);
4661 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
4662 if (ret_code != LTTNG_OK) {
4663 goto end_unlock_session_list;
4664 }
4665
4666 break;
4667 }
4668 case LTTNG_DOMAIN_NONE:
4669 default:
4670 abort();
4671 }
4672
4673 ret_code = LTTNG_OK;
4674
4675 end_unlock_session_list:
4676 session_unlock_list();
4677 return ret_code;
4678 }
4679
4680 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4681 const struct lttng_trigger *trigger,
4682 struct notification_thread_handle *notification_thread)
4683 {
4684 enum lttng_error_code ret_code;
4685 const char *trigger_name;
4686 uid_t trigger_owner;
4687 enum lttng_trigger_status trigger_status;
4688 struct lttng_trigger *sessiond_trigger = nullptr;
4689
4690 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4691 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4692 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4693 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4694
4695 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4696 trigger_name,
4697 (int) trigger_owner,
4698 (int) lttng_credentials_get_uid(cmd_creds));
4699
4700 /*
4701 * Validate the trigger credentials against the command credentials.
4702 * Only the root user can unregister a trigger with non-matching
4703 * credentials.
4704 */
4705 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4706 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4707 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4708 trigger_name,
4709 (int) trigger_owner,
4710 (int) lttng_credentials_get_uid(cmd_creds));
4711 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4712 goto end;
4713 }
4714 }
4715
4716 /* Fetch the sessiond side trigger object. */
4717 ret_code = notification_thread_command_get_trigger(
4718 notification_thread, trigger, &sessiond_trigger);
4719 if (ret_code != LTTNG_OK) {
4720 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4721 trigger_name,
4722 (int) trigger_owner,
4723 ret_code);
4724 goto end;
4725 }
4726
4727 LTTNG_ASSERT(sessiond_trigger);
4728
4729 /*
4730 * From this point on, no matter what, consider the trigger
4731 * unregistered.
4732 *
4733 * We set the unregistered state of the sessiond side trigger object in
4734 * the client thread since we want to minimize the possibility of the
4735 * notification thread being stalled due to a long execution of an
4736 * action that required the trigger lock.
4737 */
4738 lttng_trigger_set_as_unregistered(sessiond_trigger);
4739
4740 ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger);
4741 if (ret_code != LTTNG_OK) {
4742 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4743 trigger_name,
4744 (int) trigger_owner,
4745 ret_code);
4746 goto end;
4747 }
4748
4749 /*
4750 * Synchronize tracers if the trigger removes an event notifier.
4751 * Do this even if the trigger unregistration failed to at least stop
4752 * the tracers from producing notifications associated with this
4753 * event notifier.
4754 */
4755 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4756 ret_code = synchronize_tracer_notifier_unregister(trigger);
4757 if (ret_code != LTTNG_OK) {
4758 ERR("Error unregistering trigger to tracer.");
4759 goto end;
4760 }
4761 }
4762
4763 end:
4764 lttng_trigger_put(sessiond_trigger);
4765 return ret_code;
4766 }
4767
4768 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4769 struct notification_thread_handle *notification_thread,
4770 struct lttng_triggers **return_triggers)
4771 {
4772 int ret;
4773 enum lttng_error_code ret_code;
4774 struct lttng_triggers *triggers = nullptr;
4775
4776 /* Get the set of triggers from the notification thread. */
4777 ret_code = notification_thread_command_list_triggers(
4778 notification_thread, cmd_ctx->creds.uid, &triggers);
4779 if (ret_code != LTTNG_OK) {
4780 goto end;
4781 }
4782
4783 ret = lttng_triggers_remove_hidden_triggers(triggers);
4784 if (ret) {
4785 ret_code = LTTNG_ERR_UNK;
4786 goto end;
4787 }
4788
4789 *return_triggers = triggers;
4790 triggers = nullptr;
4791 ret_code = LTTNG_OK;
4792 end:
4793 lttng_triggers_destroy(triggers);
4794 return ret_code;
4795 }
4796
4797 enum lttng_error_code
4798 cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4799 const struct lttng_error_query *query,
4800 struct lttng_error_query_results **_results,
4801 struct notification_thread_handle *notification_thread)
4802 {
4803 enum lttng_error_code ret_code;
4804 const struct lttng_trigger *query_target_trigger;
4805 const struct lttng_action *query_target_action = nullptr;
4806 struct lttng_trigger *matching_trigger = nullptr;
4807 const char *trigger_name;
4808 uid_t trigger_owner;
4809 enum lttng_trigger_status trigger_status;
4810 struct lttng_error_query_results *results = nullptr;
4811
4812 switch (lttng_error_query_get_target_type(query)) {
4813 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4814 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4815 break;
4816 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4817 query_target_trigger = lttng_error_query_condition_borrow_target(query);
4818 break;
4819 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4820 query_target_trigger = lttng_error_query_action_borrow_trigger_target(query);
4821 break;
4822 default:
4823 abort();
4824 }
4825
4826 LTTNG_ASSERT(query_target_trigger);
4827
4828 ret_code = notification_thread_command_get_trigger(
4829 notification_thread, query_target_trigger, &matching_trigger);
4830 if (ret_code != LTTNG_OK) {
4831 goto end;
4832 }
4833
4834 /* No longer needed. */
4835 query_target_trigger = nullptr;
4836
4837 if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4838 /* Get the sessiond-side version of the target action. */
4839 query_target_action =
4840 lttng_error_query_action_borrow_action_target(query, matching_trigger);
4841 }
4842
4843 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4844 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4845 trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner);
4846 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4847
4848 results = lttng_error_query_results_create();
4849 if (!results) {
4850 ret_code = LTTNG_ERR_NOMEM;
4851 goto end;
4852 }
4853
4854 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4855 trigger_name,
4856 (int) trigger_owner,
4857 (int) lttng_credentials_get_uid(cmd_creds));
4858
4859 /*
4860 * Validate the trigger credentials against the command credentials.
4861 * Only the root user can target a trigger with non-matching
4862 * credentials.
4863 */
4864 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger),
4865 cmd_creds)) {
4866 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4867 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4868 trigger_name,
4869 (int) trigger_owner,
4870 (int) lttng_credentials_get_uid(cmd_creds));
4871 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4872 goto end;
4873 }
4874 }
4875
4876 switch (lttng_error_query_get_target_type(query)) {
4877 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4878 trigger_status = lttng_trigger_add_error_results(matching_trigger, results);
4879
4880 switch (trigger_status) {
4881 case LTTNG_TRIGGER_STATUS_OK:
4882 break;
4883 default:
4884 ret_code = LTTNG_ERR_UNK;
4885 goto end;
4886 }
4887
4888 break;
4889 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4890 {
4891 trigger_status =
4892 lttng_trigger_condition_add_error_results(matching_trigger, results);
4893
4894 switch (trigger_status) {
4895 case LTTNG_TRIGGER_STATUS_OK:
4896 break;
4897 default:
4898 ret_code = LTTNG_ERR_UNK;
4899 goto end;
4900 }
4901
4902 break;
4903 }
4904 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4905 {
4906 const enum lttng_action_status action_status =
4907 lttng_action_add_error_query_results(query_target_action, results);
4908
4909 switch (action_status) {
4910 case LTTNG_ACTION_STATUS_OK:
4911 break;
4912 default:
4913 ret_code = LTTNG_ERR_UNK;
4914 goto end;
4915 }
4916
4917 break;
4918 }
4919 default:
4920 abort();
4921 break;
4922 }
4923
4924 *_results = results;
4925 results = nullptr;
4926 ret_code = LTTNG_OK;
4927 end:
4928 lttng_trigger_put(matching_trigger);
4929 lttng_error_query_results_destroy(results);
4930 return ret_code;
4931 }
4932
4933 /*
4934 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4935 * snapshot output is *not* set with a remote destination.
4936 *
4937 * Return LTTNG_OK on success or a LTTNG_ERR code.
4938 */
4939 static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
4940 const struct ltt_session *session)
4941 {
4942 enum lttng_error_code status = LTTNG_OK;
4943 struct lttng_ht_iter iter;
4944 struct consumer_socket *socket;
4945 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4946 const char *base_path;
4947
4948 LTTNG_ASSERT(output);
4949 LTTNG_ASSERT(session);
4950
4951 DBG2("Set relayd object from snapshot output");
4952
4953 if (session->current_trace_chunk) {
4954 enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
4955 session->current_trace_chunk, &current_chunk_id.value);
4956
4957 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
4958 current_chunk_id.is_set = true;
4959 } else {
4960 ERR("Failed to get current trace chunk id");
4961 status = LTTNG_ERR_UNK;
4962 goto error;
4963 }
4964 }
4965
4966 /* Ignore if snapshot consumer output is not network. */
4967 if (output->type != CONSUMER_DST_NET) {
4968 goto error;
4969 }
4970
4971 /*
4972 * The snapshot record URI base path overrides the session
4973 * base path.
4974 */
4975 if (output->dst.net.control.subdir[0] != '\0') {
4976 base_path = output->dst.net.control.subdir;
4977 } else {
4978 base_path = session->base_path;
4979 }
4980
4981 /*
4982 * For each consumer socket, create and send the relayd object of the
4983 * snapshot output.
4984 */
4985 {
4986 lttng::urcu::read_lock_guard read_lock;
4987
4988 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
4989 pthread_mutex_lock(socket->lock);
4990 status = send_consumer_relayd_sockets(
4991 session->id,
4992 output,
4993 socket,
4994 session->name,
4995 session->hostname,
4996 base_path,
4997 session->live_timer,
4998 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
4999 session->creation_time,
5000 session->name_contains_creation_time);
5001 pthread_mutex_unlock(socket->lock);
5002 if (status != LTTNG_OK) {
5003 goto error;
5004 }
5005 }
5006 }
5007
5008 error:
5009 return status;
5010 }
5011
5012 /*
5013 * Record a kernel snapshot.
5014 *
5015 * Return LTTNG_OK on success or a LTTNG_ERR code.
5016 */
5017 static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
5018 const struct consumer_output *output,
5019 const struct ltt_session *session,
5020 uint64_t nb_packets_per_stream)
5021 {
5022 enum lttng_error_code status;
5023
5024 LTTNG_ASSERT(ksess);
5025 LTTNG_ASSERT(output);
5026 LTTNG_ASSERT(session);
5027
5028 status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
5029 return status;
5030 }
5031
5032 /*
5033 * Record a UST snapshot.
5034 *
5035 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5036 */
5037 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
5038 const struct consumer_output *output,
5039 const struct ltt_session *session,
5040 uint64_t nb_packets_per_stream)
5041 {
5042 enum lttng_error_code status;
5043
5044 LTTNG_ASSERT(usess);
5045 LTTNG_ASSERT(output);
5046 LTTNG_ASSERT(session);
5047
5048 status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
5049 return status;
5050 }
5051
5052 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
5053 uint64_t cur_nr_packets)
5054 {
5055 uint64_t tot_size = 0;
5056
5057 if (session->kernel_session) {
5058 struct ltt_kernel_channel *chan;
5059 const struct ltt_kernel_session *ksess = session->kernel_session;
5060
5061 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
5062 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5063 /*
5064 * Don't take channel into account if we
5065 * already grab all its packets.
5066 */
5067 continue;
5068 }
5069 tot_size += chan->channel->attr.subbuf_size * chan->stream_count;
5070 }
5071 }
5072
5073 if (session->ust_session) {
5074 const struct ltt_ust_session *usess = session->ust_session;
5075
5076 tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets);
5077 }
5078
5079 return tot_size;
5080 }
5081
5082 /*
5083 * Calculate the number of packets we can grab from each stream that
5084 * fits within the overall snapshot max size.
5085 *
5086 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5087 * the number of packets per stream.
5088 *
5089 * TODO: this approach is not perfect: we consider the worse case
5090 * (packet filling the sub-buffers) as an upper bound, but we could do
5091 * better if we do this calculation while we actually grab the packet
5092 * content: we would know how much padding we don't actually store into
5093 * the file.
5094 *
5095 * This algorithm is currently bounded by the number of packets per
5096 * stream.
5097 *
5098 * Since we call this algorithm before actually grabbing the data, it's
5099 * an approximation: for instance, applications could appear/disappear
5100 * in between this call and actually grabbing data.
5101 */
5102 static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5103 uint64_t max_size)
5104 {
5105 int64_t size_left;
5106 uint64_t cur_nb_packets = 0;
5107
5108 if (!max_size) {
5109 return 0; /* Infinite */
5110 }
5111
5112 size_left = max_size;
5113 for (;;) {
5114 uint64_t one_more_packet_tot_size;
5115
5116 one_more_packet_tot_size =
5117 get_session_size_one_more_packet_per_stream(session, cur_nb_packets);
5118 if (!one_more_packet_tot_size) {
5119 /* We are already grabbing all packets. */
5120 break;
5121 }
5122 size_left -= one_more_packet_tot_size;
5123 if (size_left < 0) {
5124 break;
5125 }
5126 cur_nb_packets++;
5127 }
5128 if (!cur_nb_packets && size_left != max_size) {
5129 /* Not enough room to grab one packet of each stream, error. */
5130 return -1;
5131 }
5132 return cur_nb_packets;
5133 }
5134
5135 static enum lttng_error_code snapshot_record(struct ltt_session *session,
5136 const struct snapshot_output *snapshot_output)
5137 {
5138 int64_t nb_packets_per_stream;
5139 char snapshot_chunk_name[LTTNG_NAME_MAX];
5140 int ret;
5141 enum lttng_error_code ret_code = LTTNG_OK;
5142 struct lttng_trace_chunk *snapshot_trace_chunk;
5143 struct consumer_output *original_ust_consumer_output = nullptr;
5144 struct consumer_output *original_kernel_consumer_output = nullptr;
5145 struct consumer_output *snapshot_ust_consumer_output = nullptr;
5146 struct consumer_output *snapshot_kernel_consumer_output = nullptr;
5147
5148 ret = snprintf(snapshot_chunk_name,
5149 sizeof(snapshot_chunk_name),
5150 "%s-%s-%" PRIu64,
5151 snapshot_output->name,
5152 snapshot_output->datetime,
5153 snapshot_output->nb_snapshot);
5154 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5155 ERR("Failed to format snapshot name");
5156 ret_code = LTTNG_ERR_INVALID;
5157 goto error;
5158 }
5159 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5160 snapshot_output->name,
5161 session->name,
5162 snapshot_chunk_name);
5163 if (!session->kernel_session && !session->ust_session) {
5164 ERR("Failed to record snapshot as no channels exist");
5165 ret_code = LTTNG_ERR_NO_CHANNEL;
5166 goto error;
5167 }
5168
5169 if (session->kernel_session) {
5170 original_kernel_consumer_output = session->kernel_session->consumer;
5171 snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer);
5172 strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name);
5173
5174 /* Copy the original domain subdir. */
5175 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5176 original_kernel_consumer_output->domain_subdir);
5177
5178 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5179 original_kernel_consumer_output);
5180 if (ret < 0) {
5181 ERR("Failed to copy consumer sockets from snapshot output configuration");
5182 ret_code = LTTNG_ERR_NOMEM;
5183 goto error;
5184 }
5185 ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session);
5186 if (ret_code != LTTNG_OK) {
5187 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5188 goto error;
5189 }
5190 session->kernel_session->consumer = snapshot_kernel_consumer_output;
5191 }
5192 if (session->ust_session) {
5193 original_ust_consumer_output = session->ust_session->consumer;
5194 snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer);
5195 strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name);
5196
5197 /* Copy the original domain subdir. */
5198 strcpy(snapshot_ust_consumer_output->domain_subdir,
5199 original_ust_consumer_output->domain_subdir);
5200
5201 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5202 original_ust_consumer_output);
5203 if (ret < 0) {
5204 ERR("Failed to copy consumer sockets from snapshot output configuration");
5205 ret_code = LTTNG_ERR_NOMEM;
5206 goto error;
5207 }
5208 ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session);
5209 if (ret_code != LTTNG_OK) {
5210 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5211 goto error;
5212 }
5213 session->ust_session->consumer = snapshot_ust_consumer_output;
5214 }
5215
5216 snapshot_trace_chunk = session_create_new_trace_chunk(
5217 session,
5218 snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output,
5219 consumer_output_get_base_path(snapshot_output->consumer),
5220 snapshot_chunk_name);
5221 if (!snapshot_trace_chunk) {
5222 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5223 session->name);
5224 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5225 goto error;
5226 }
5227 LTTNG_ASSERT(!session->current_trace_chunk);
5228 ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
5229 lttng_trace_chunk_put(snapshot_trace_chunk);
5230 snapshot_trace_chunk = nullptr;
5231 if (ret) {
5232 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5233 session->name);
5234 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5235 goto error;
5236 }
5237
5238 nb_packets_per_stream =
5239 get_session_nb_packets_per_stream(session, snapshot_output->max_size);
5240 if (nb_packets_per_stream < 0) {
5241 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5242 goto error_close_trace_chunk;
5243 }
5244
5245 if (session->kernel_session) {
5246 ret_code = record_kernel_snapshot(session->kernel_session,
5247 snapshot_kernel_consumer_output,
5248 session,
5249 nb_packets_per_stream);
5250 if (ret_code != LTTNG_OK) {
5251 goto error_close_trace_chunk;
5252 }
5253 }
5254
5255 if (session->ust_session) {
5256 ret_code = record_ust_snapshot(session->ust_session,
5257 snapshot_ust_consumer_output,
5258 session,
5259 nb_packets_per_stream);
5260 if (ret_code != LTTNG_OK) {
5261 goto error_close_trace_chunk;
5262 }
5263 }
5264
5265 error_close_trace_chunk:
5266 if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
5267 ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
5268 ret_code = LTTNG_ERR_UNK;
5269 }
5270
5271 if (session_close_trace_chunk(session,
5272 snapshot_trace_chunk,
5273 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
5274 nullptr)) {
5275 /*
5276 * Don't goto end; make sure the chunk is closed for the session
5277 * to allow future snapshots.
5278 */
5279 ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name);
5280 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5281 }
5282
5283 lttng_trace_chunk_put(snapshot_trace_chunk);
5284 snapshot_trace_chunk = nullptr;
5285 error:
5286 if (original_ust_consumer_output) {
5287 session->ust_session->consumer = original_ust_consumer_output;
5288 }
5289 if (original_kernel_consumer_output) {
5290 session->kernel_session->consumer = original_kernel_consumer_output;
5291 }
5292 consumer_output_put(snapshot_ust_consumer_output);
5293 consumer_output_put(snapshot_kernel_consumer_output);
5294 return ret_code;
5295 }
5296
5297 /*
5298 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5299 *
5300 * The wait parameter is ignored so this call always wait for the snapshot to
5301 * complete before returning.
5302 *
5303 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5304 */
5305 int cmd_snapshot_record(struct ltt_session *session,
5306 const struct lttng_snapshot_output *output,
5307 int wait __attribute__((unused)))
5308 {
5309 enum lttng_error_code cmd_ret = LTTNG_OK;
5310 int ret;
5311 unsigned int snapshot_success = 0;
5312 char datetime[16];
5313 struct snapshot_output *tmp_output = nullptr;
5314
5315 LTTNG_ASSERT(session);
5316 LTTNG_ASSERT(output);
5317
5318 DBG("Cmd snapshot record for session %s", session->name);
5319
5320 /* Get the datetime for the snapshot output directory. */
5321 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime));
5322 if (!ret) {
5323 cmd_ret = LTTNG_ERR_INVALID;
5324 goto error;
5325 }
5326
5327 /*
5328 * Permission denied to create an output if the session is not
5329 * set in no output mode.
5330 */
5331 if (session->output_traces) {
5332 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5333 goto error;
5334 }
5335
5336 /* The session needs to be started at least once. */
5337 if (!session->has_been_started) {
5338 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5339 goto error;
5340 }
5341
5342 /* Use temporary output for the session. */
5343 if (*output->ctrl_url != '\0') {
5344 tmp_output = snapshot_output_alloc();
5345 if (!tmp_output) {
5346 cmd_ret = LTTNG_ERR_NOMEM;
5347 goto error;
5348 }
5349
5350 ret = snapshot_output_init(session,
5351 output->max_size,
5352 output->name,
5353 output->ctrl_url,
5354 output->data_url,
5355 session->consumer,
5356 tmp_output,
5357 nullptr);
5358 if (ret < 0) {
5359 if (ret == -ENOMEM) {
5360 cmd_ret = LTTNG_ERR_NOMEM;
5361 } else {
5362 cmd_ret = LTTNG_ERR_INVALID;
5363 }
5364 goto error;
5365 }
5366 /* Use the global session count for the temporary snapshot. */
5367 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5368
5369 /* Use the global datetime */
5370 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5371 cmd_ret = snapshot_record(session, tmp_output);
5372 if (cmd_ret != LTTNG_OK) {
5373 goto error;
5374 }
5375 snapshot_success = 1;
5376 } else {
5377 struct snapshot_output *sout;
5378 struct lttng_ht_iter iter;
5379
5380 lttng::urcu::read_lock_guard read_lock;
5381
5382 cds_lfht_for_each_entry (
5383 session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
5384 struct snapshot_output output_copy;
5385
5386 /*
5387 * Make a local copy of the output and override output
5388 * parameters with those provided as part of the
5389 * command.
5390 */
5391 memcpy(&output_copy, sout, sizeof(output_copy));
5392
5393 if (output->max_size != (uint64_t) -1ULL) {
5394 output_copy.max_size = output->max_size;
5395 }
5396
5397 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5398 memcpy(output_copy.datetime, datetime, sizeof(datetime));
5399
5400 /* Use temporary name. */
5401 if (*output->name != '\0') {
5402 if (lttng_strncpy(output_copy.name,
5403 output->name,
5404 sizeof(output_copy.name))) {
5405 cmd_ret = LTTNG_ERR_INVALID;
5406 goto error;
5407 }
5408 }
5409
5410 cmd_ret = snapshot_record(session, &output_copy);
5411 if (cmd_ret != LTTNG_OK) {
5412 goto error;
5413 }
5414
5415 snapshot_success = 1;
5416 }
5417 }
5418
5419 if (snapshot_success) {
5420 session->snapshot.nb_snapshot++;
5421 } else {
5422 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5423 }
5424
5425 error:
5426 if (tmp_output) {
5427 snapshot_output_destroy(tmp_output);
5428 }
5429
5430 return cmd_ret;
5431 }
5432
5433 /*
5434 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5435 */
5436 int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
5437 {
5438 /* Safety net */
5439 LTTNG_ASSERT(session);
5440
5441 /*
5442 * Can only set shm path before session is started.
5443 */
5444 if (session->has_been_started) {
5445 return LTTNG_ERR_SESSION_STARTED;
5446 }
5447
5448 strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
5449 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5450
5451 return LTTNG_OK;
5452 }
5453
5454 /*
5455 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5456 *
5457 * Ask the consumer to rotate the session output directory.
5458 * The session lock must be held.
5459 *
5460 * Returns LTTNG_OK on success or else a negative LTTng error code.
5461 */
5462 int cmd_rotate_session(struct ltt_session *session,
5463 struct lttng_rotate_session_return *rotate_return,
5464 bool quiet_rotation,
5465 enum lttng_trace_chunk_command_type command)
5466 {
5467 int ret;
5468 uint64_t ongoing_rotation_chunk_id;
5469 enum lttng_error_code cmd_ret = LTTNG_OK;
5470 struct lttng_trace_chunk *chunk_being_archived = nullptr;
5471 struct lttng_trace_chunk *new_trace_chunk = nullptr;
5472 enum lttng_trace_chunk_status chunk_status;
5473 bool failed_to_rotate = false;
5474 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5475
5476 LTTNG_ASSERT(session);
5477
5478 if (!session->has_been_started) {
5479 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5480 goto end;
5481 }
5482
5483 /*
5484 * Explicit rotation is not supported for live sessions.
5485 * However, live sessions can perform a quiet rotation on
5486 * destroy.
5487 * Rotation is not supported for snapshot traces (no output).
5488 */
5489 if ((!quiet_rotation && session->live_timer) || !session->output_traces) {
5490 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5491 goto end;
5492 }
5493
5494 /* Unsupported feature in lttng-relayd before 2.11. */
5495 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5496 (session->consumer->relay_major_version == 2 &&
5497 session->consumer->relay_minor_version < 11)) {
5498 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5499 goto end;
5500 }
5501
5502 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5503 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5504 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5505 goto end;
5506 }
5507
5508 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5509 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5510 session->name);
5511 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5512 goto end;
5513 }
5514
5515 /*
5516 * After a stop, we only allow one rotation to occur, the other ones are
5517 * useless until a new start.
5518 */
5519 if (session->rotated_after_last_stop) {
5520 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5521 session->name);
5522 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5523 goto end;
5524 }
5525
5526 /*
5527 * After a stop followed by a clear, disallow following rotations a they would
5528 * generate empty chunks.
5529 */
5530 if (session->cleared_after_last_stop) {
5531 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5532 session->name);
5533 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5534 goto end;
5535 }
5536
5537 if (session->active) {
5538 new_trace_chunk =
5539 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
5540 if (!new_trace_chunk) {
5541 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5542 goto error;
5543 }
5544 }
5545
5546 /*
5547 * The current trace chunk becomes the chunk being archived.
5548 *
5549 * After this point, "chunk_being_archived" must absolutely
5550 * be closed on the consumer(s), otherwise it will never be
5551 * cleaned-up, which will result in a leak.
5552 */
5553 ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived);
5554 if (ret) {
5555 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5556 goto error;
5557 }
5558
5559 if (session->kernel_session) {
5560 cmd_ret = kernel_rotate_session(session);
5561 if (cmd_ret != LTTNG_OK) {
5562 failed_to_rotate = true;
5563 rotation_fail_code = cmd_ret;
5564 }
5565 }
5566 if (session->ust_session) {
5567 cmd_ret = ust_app_rotate_session(session);
5568 if (cmd_ret != LTTNG_OK) {
5569 failed_to_rotate = true;
5570 rotation_fail_code = cmd_ret;
5571 }
5572 }
5573
5574 if (!session->active) {
5575 session->rotated_after_last_stop = true;
5576 }
5577
5578 if (!chunk_being_archived) {
5579 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5580 session->name);
5581 if (failed_to_rotate) {
5582 cmd_ret = rotation_fail_code;
5583 goto error;
5584 }
5585 cmd_ret = LTTNG_OK;
5586 goto end;
5587 }
5588
5589 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5590 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id);
5591 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5592
5593 ret = session_close_trace_chunk(
5594 session, chunk_being_archived, command, session->last_chunk_path);
5595 if (ret) {
5596 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5597 goto error;
5598 }
5599
5600 if (failed_to_rotate) {
5601 cmd_ret = rotation_fail_code;
5602 goto error;
5603 }
5604
5605 session->quiet_rotation = quiet_rotation;
5606 ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER);
5607 if (ret) {
5608 cmd_ret = LTTNG_ERR_UNK;
5609 goto error;
5610 }
5611
5612 if (rotate_return) {
5613 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5614 }
5615
5616 session->chunk_being_archived = chunk_being_archived;
5617 chunk_being_archived = nullptr;
5618 if (!quiet_rotation) {
5619 ret = notification_thread_command_session_rotation_ongoing(
5620 the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
5621 if (ret != LTTNG_OK) {
5622 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5623 session->name);
5624 cmd_ret = (lttng_error_code) ret;
5625 }
5626 }
5627
5628 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5629 session->name,
5630 ongoing_rotation_chunk_id);
5631 end:
5632 lttng_trace_chunk_put(new_trace_chunk);
5633 lttng_trace_chunk_put(chunk_being_archived);
5634 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5635 return ret;
5636 error:
5637 if (session_reset_rotation_state(*session, LTTNG_ROTATION_STATE_ERROR)) {
5638 ERR("Failed to reset rotation state of session \"%s\"", session->name);
5639 }
5640 goto end;
5641 }
5642
5643 /*
5644 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5645 *
5646 * Check if the session has finished its rotation.
5647 *
5648 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5649 */
5650 int cmd_rotate_get_info(struct ltt_session *session,
5651 struct lttng_rotation_get_info_return *info_return,
5652 uint64_t rotation_id)
5653 {
5654 enum lttng_error_code cmd_ret = LTTNG_OK;
5655 enum lttng_rotation_state rotation_state;
5656
5657 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64,
5658 session->name,
5659 session->most_recent_chunk_id.value);
5660
5661 if (session->chunk_being_archived) {
5662 enum lttng_trace_chunk_status chunk_status;
5663 uint64_t chunk_id;
5664
5665 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
5666 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5667
5668 rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING :
5669 LTTNG_ROTATION_STATE_EXPIRED;
5670 } else {
5671 if (session->last_archived_chunk_id.is_set &&
5672 rotation_id != session->last_archived_chunk_id.value) {
5673 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5674 } else {
5675 rotation_state = session->rotation_state;
5676 }
5677 }
5678
5679 switch (rotation_state) {
5680 case LTTNG_ROTATION_STATE_NO_ROTATION:
5681 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5682 session->name);
5683 goto end;
5684 case LTTNG_ROTATION_STATE_EXPIRED:
5685 DBG("Reporting that the rotation state of rotation id %" PRIu64
5686 " of session \"%s\" has expired",
5687 rotation_id,
5688 session->name);
5689 break;
5690 case LTTNG_ROTATION_STATE_ONGOING:
5691 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5692 rotation_id,
5693 session->name);
5694 break;
5695 case LTTNG_ROTATION_STATE_COMPLETED:
5696 {
5697 int fmt_ret;
5698 char *chunk_path;
5699 char *current_tracing_path_reply;
5700 size_t current_tracing_path_reply_len;
5701
5702 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5703 rotation_id,
5704 session->name);
5705
5706 switch (session_get_consumer_destination_type(session)) {
5707 case CONSUMER_DST_LOCAL:
5708 current_tracing_path_reply = info_return->location.local.absolute_path;
5709 current_tracing_path_reply_len =
5710 sizeof(info_return->location.local.absolute_path);
5711 info_return->location_type =
5712 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5713 fmt_ret = asprintf(&chunk_path,
5714 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5715 session_get_base_path(session),
5716 session->last_archived_chunk_name);
5717 if (fmt_ret == -1) {
5718 PERROR("Failed to format the path of the last archived trace chunk");
5719 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5720 cmd_ret = LTTNG_ERR_UNK;
5721 goto end;
5722 }
5723 break;
5724 case CONSUMER_DST_NET:
5725 {
5726 uint16_t ctrl_port, data_port;
5727
5728 current_tracing_path_reply = info_return->location.relay.relative_path;
5729 current_tracing_path_reply_len =
5730 sizeof(info_return->location.relay.relative_path);
5731 /* Currently the only supported relay protocol. */
5732 info_return->location.relay.protocol =
5733 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5734
5735 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5736 session_get_net_consumer_hostname(session),
5737 sizeof(info_return->location.relay.host));
5738 if (fmt_ret) {
5739 ERR("Failed to copy host name to rotate_get_info reply");
5740 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5741 cmd_ret = LTTNG_ERR_SET_URL;
5742 goto end;
5743 }
5744
5745 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5746 info_return->location.relay.ports.control = ctrl_port;
5747 info_return->location.relay.ports.data = data_port;
5748 info_return->location_type =
5749 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5750 chunk_path = strdup(session->last_chunk_path);
5751 if (!chunk_path) {
5752 ERR("Failed to allocate the path of the last archived trace chunk");
5753 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5754 cmd_ret = LTTNG_ERR_UNK;
5755 goto end;
5756 }
5757 break;
5758 }
5759 default:
5760 abort();
5761 }
5762
5763 fmt_ret = lttng_strncpy(
5764 current_tracing_path_reply, chunk_path, current_tracing_path_reply_len);
5765 free(chunk_path);
5766 if (fmt_ret) {
5767 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5768 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5769 cmd_ret = LTTNG_ERR_UNK;
5770 goto end;
5771 }
5772
5773 break;
5774 }
5775 case LTTNG_ROTATION_STATE_ERROR:
5776 DBG("Reporting that an error occurred during rotation %" PRIu64
5777 " of session \"%s\"",
5778 rotation_id,
5779 session->name);
5780 break;
5781 default:
5782 abort();
5783 }
5784
5785 cmd_ret = LTTNG_OK;
5786 end:
5787 info_return->status = (int32_t) rotation_state;
5788 return cmd_ret;
5789 }
5790
5791 /*
5792 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5793 *
5794 * Configure the automatic rotation parameters.
5795 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5796 * 'activate' to false means deactivate the rotation schedule and validate that
5797 * 'new_value' has the same value as the currently active value.
5798 *
5799 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5800 */
5801 int cmd_rotation_set_schedule(struct ltt_session *session,
5802 bool activate,
5803 enum lttng_rotation_schedule_type schedule_type,
5804 uint64_t new_value)
5805 {
5806 int ret;
5807 uint64_t *parameter_value;
5808
5809 LTTNG_ASSERT(session);
5810
5811 DBG("Cmd rotate set schedule session %s", session->name);
5812
5813 if (session->live_timer || !session->output_traces) {
5814 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5815 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5816 goto end;
5817 }
5818
5819 switch (schedule_type) {
5820 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5821 parameter_value = &session->rotate_size;
5822 break;
5823 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5824 parameter_value = &session->rotate_timer_period;
5825 if (new_value >= UINT_MAX) {
5826 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5827 " > %u (UINT_MAX)",
5828 new_value,
5829 UINT_MAX);
5830 ret = LTTNG_ERR_INVALID;
5831 goto end;
5832 }
5833 break;
5834 default:
5835 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5836 ret = LTTNG_ERR_INVALID;
5837 goto end;
5838 }
5839
5840 /* Improper use of the API. */
5841 if (new_value == -1ULL) {
5842 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5843 ret = LTTNG_ERR_INVALID;
5844 goto end;
5845 }
5846
5847 /*
5848 * As indicated in struct ltt_session's comments, a value of == 0 means
5849 * this schedule rotation type is not in use.
5850 *
5851 * Reject the command if we were asked to activate a schedule that was
5852 * already active.
5853 */
5854 if (activate && *parameter_value != 0) {
5855 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5856 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5857 goto end;
5858 }
5859
5860 /*
5861 * Reject the command if we were asked to deactivate a schedule that was
5862 * not active.
5863 */
5864 if (!activate && *parameter_value == 0) {
5865 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5866 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5867 goto end;
5868 }
5869
5870 /*
5871 * Reject the command if we were asked to deactivate a schedule that
5872 * doesn't exist.
5873 */
5874 if (!activate && *parameter_value != new_value) {
5875 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5876 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5877 goto end;
5878 }
5879
5880 *parameter_value = activate ? new_value : 0;
5881
5882 switch (schedule_type) {
5883 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5884 if (activate && session->active) {
5885 /*
5886 * Only start the timer if the session is active,
5887 * otherwise it will be started when the session starts.
5888 */
5889 ret = timer_session_rotation_schedule_timer_start(session, new_value);
5890 if (ret) {
5891 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5892 ret = LTTNG_ERR_UNK;
5893 goto end;
5894 }
5895 } else {
5896 ret = timer_session_rotation_schedule_timer_stop(session);
5897 if (ret) {
5898 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5899 ret = LTTNG_ERR_UNK;
5900 goto end;
5901 }
5902 }
5903 break;
5904 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5905 if (activate) {
5906 try {
5907 the_rotation_thread_handle->subscribe_session_consumed_size_rotation(
5908 *session, new_value);
5909 } catch (const std::exception& e) {
5910 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5911 e.what());
5912 ret = LTTNG_ERR_UNK;
5913 goto end;
5914 }
5915 } else {
5916 try {
5917 the_rotation_thread_handle
5918 ->unsubscribe_session_consumed_size_rotation(*session);
5919 } catch (const std::exception& e) {
5920 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5921 e.what());
5922 ret = LTTNG_ERR_UNK;
5923 goto end;
5924 }
5925 }
5926 break;
5927 default:
5928 /* Would have been caught before. */
5929 abort();
5930 }
5931
5932 ret = LTTNG_OK;
5933
5934 goto end;
5935
5936 end:
5937 return ret;
5938 }
5939
5940 /* Wait for a given path to be removed before continuing. */
5941 static enum lttng_error_code wait_on_path(void *path_data)
5942 {
5943 const char *shm_path = (const char *) path_data;
5944
5945 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5946 shm_path);
5947 while (true) {
5948 int ret;
5949 struct stat st;
5950
5951 ret = stat(shm_path, &st);
5952 if (ret) {
5953 if (errno != ENOENT) {
5954 PERROR("stat() returned an error while checking for the existence of the shm path");
5955 } else {
5956 DBG("shm path no longer exists, completing the destruction of session");
5957 }
5958 break;
5959 } else {
5960 if (!S_ISDIR(st.st_mode)) {
5961 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5962 shm_path);
5963 break;
5964 }
5965 }
5966 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5967 }
5968 return LTTNG_OK;
5969 }
5970
5971 /*
5972 * Returns a pointer to a handler to run on completion of a command.
5973 * Returns NULL if no handler has to be run for the last command executed.
5974 */
5975 const struct cmd_completion_handler *cmd_pop_completion_handler()
5976 {
5977 struct cmd_completion_handler *handler = current_completion_handler;
5978
5979 current_completion_handler = nullptr;
5980 return handler;
5981 }
5982
5983 /*
5984 * Init command subsystem.
5985 */
5986 void cmd_init()
5987 {
5988 /*
5989 * Set network sequence index to 1 for streams to match a relayd
5990 * socket on the consumer side.
5991 */
5992 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5993 relayd_net_seq_idx = 1;
5994 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
5995
5996 DBG("Command subsystem initialized");
5997 }
This page took 0.259158 seconds and 4 git commands to generate.