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