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