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