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