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