Fix: sessiond: don't negate error code on list error
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "common/sessiond-comm/payload.h"
11 #include "lttng/lttng-error.h"
12 #include "lttng/tracker.h"
13 #define _LGPL_SOURCE
14 #include <assert.h>
15 #include <inttypes.h>
16 #include <urcu/list.h>
17 #include <urcu/uatomic.h>
18 #include <sys/stat.h>
19 #include <stdio.h>
20
21 #include <common/defaults.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <common/relayd/relayd.h>
25 #include <common/utils.h>
26 #include <common/compat/string.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/buffer-view.h>
30 #include <common/sessiond-comm/payload.h>
31 #include <common/sessiond-comm/payload-view.h>
32 #include <common/trace-chunk.h>
33 #include <lttng/location-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/action/action.h>
37 #include <lttng/channel.h>
38 #include <lttng/channel-internal.h>
39 #include <lttng/rotate-internal.h>
40 #include <lttng/location-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/userspace-probe-internal.h>
43 #include <lttng/session-descriptor-internal.h>
44 #include <common/string-utils/string-utils.h>
45
46 #include "channel.h"
47 #include "consumer.h"
48 #include "event.h"
49 #include "health-sessiond.h"
50 #include "kernel.h"
51 #include "kernel-consumer.h"
52 #include "lttng-sessiond.h"
53 #include "utils.h"
54 #include "lttng-syscall.h"
55 #include "agent.h"
56 #include "buffer-registry.h"
57 #include "notification-thread.h"
58 #include "notification-thread-commands.h"
59 #include "rotate.h"
60 #include "rotation-thread.h"
61 #include "timer.h"
62 #include "agent-thread.h"
63
64 #include "cmd.h"
65
66 /* Sleep for 100ms between each check for the shm path's deletion. */
67 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
68
69 struct cmd_destroy_session_reply_context {
70 int reply_sock_fd;
71 bool implicit_rotation_on_destroy;
72 /*
73 * Indicates whether or not an error occurred while launching the
74 * destruction of a session.
75 */
76 enum lttng_error_code destruction_status;
77 };
78
79 static enum lttng_error_code wait_on_path(void *path);
80
81 /*
82 * Command completion handler that is used by the destroy command
83 * when a session that has a non-default shm_path is being destroyed.
84 *
85 * See comment in cmd_destroy_session() for the rationale.
86 */
87 static struct destroy_completion_handler {
88 struct cmd_completion_handler handler;
89 char shm_path[member_sizeof(struct ltt_session, shm_path)];
90 } destroy_completion_handler = {
91 .handler = {
92 .run = wait_on_path,
93 .data = destroy_completion_handler.shm_path
94 },
95 .shm_path = { 0 },
96 };
97
98 static struct cmd_completion_handler *current_completion_handler;
99
100 /*
101 * Used to keep a unique index for each relayd socket created where this value
102 * is associated with streams on the consumer so it can match the right relayd
103 * to send to. It must be accessed with the relayd_net_seq_idx_lock
104 * held.
105 */
106 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
107 static uint64_t relayd_net_seq_idx;
108
109 static int validate_ust_event_name(const char *);
110 static int cmd_enable_event_internal(struct ltt_session *session,
111 const struct lttng_domain *domain,
112 char *channel_name, struct lttng_event *event,
113 char *filter_expression,
114 struct lttng_filter_bytecode *filter,
115 struct lttng_event_exclusion *exclusion,
116 int wpipe);
117
118 /*
119 * Create a session path used by list_lttng_sessions for the case that the
120 * session consumer is on the network.
121 */
122 static int build_network_session_path(char *dst, size_t size,
123 struct ltt_session *session)
124 {
125 int ret, kdata_port, udata_port;
126 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
127 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
128
129 assert(session);
130 assert(dst);
131
132 memset(tmp_urls, 0, sizeof(tmp_urls));
133 memset(tmp_uurl, 0, sizeof(tmp_uurl));
134
135 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
136
137 if (session->kernel_session && session->kernel_session->consumer) {
138 kuri = &session->kernel_session->consumer->dst.net.control;
139 kdata_port = session->kernel_session->consumer->dst.net.data.port;
140 }
141
142 if (session->ust_session && session->ust_session->consumer) {
143 uuri = &session->ust_session->consumer->dst.net.control;
144 udata_port = session->ust_session->consumer->dst.net.data.port;
145 }
146
147 if (uuri == NULL && kuri == NULL) {
148 uri = &session->consumer->dst.net.control;
149 kdata_port = session->consumer->dst.net.data.port;
150 } else if (kuri && uuri) {
151 ret = uri_compare(kuri, uuri);
152 if (ret) {
153 /* Not Equal */
154 uri = kuri;
155 /* Build uuri URL string */
156 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
157 if (ret < 0) {
158 goto error;
159 }
160 } else {
161 uri = kuri;
162 }
163 } else if (kuri && uuri == NULL) {
164 uri = kuri;
165 } else if (uuri && kuri == NULL) {
166 uri = uuri;
167 }
168
169 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
170 if (ret < 0) {
171 goto error;
172 }
173
174 /*
175 * Do we have a UST url set. If yes, this means we have both kernel and UST
176 * to print.
177 */
178 if (*tmp_uurl != '\0') {
179 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
180 tmp_urls, kdata_port, tmp_uurl, udata_port);
181 } else {
182 int dport;
183 if (kuri || (!kuri && !uuri)) {
184 dport = kdata_port;
185 } else {
186 /* No kernel URI, use the UST port. */
187 dport = udata_port;
188 }
189 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
190 }
191
192 error:
193 return ret;
194 }
195
196 /*
197 * Get run-time attributes if the session has been started (discarded events,
198 * lost packets).
199 */
200 static int get_kernel_runtime_stats(struct ltt_session *session,
201 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
202 uint64_t *lost_packets)
203 {
204 int ret;
205
206 if (!session->has_been_started) {
207 ret = 0;
208 *discarded_events = 0;
209 *lost_packets = 0;
210 goto end;
211 }
212
213 ret = consumer_get_discarded_events(session->id, kchan->key,
214 session->kernel_session->consumer,
215 discarded_events);
216 if (ret < 0) {
217 goto end;
218 }
219
220 ret = consumer_get_lost_packets(session->id, kchan->key,
221 session->kernel_session->consumer,
222 lost_packets);
223 if (ret < 0) {
224 goto end;
225 }
226
227 end:
228 return ret;
229 }
230
231 /*
232 * Get run-time attributes if the session has been started (discarded events,
233 * lost packets).
234 */
235 static int get_ust_runtime_stats(struct ltt_session *session,
236 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
237 uint64_t *lost_packets)
238 {
239 int ret;
240 struct ltt_ust_session *usess;
241
242 if (!discarded_events || !lost_packets) {
243 ret = -1;
244 goto end;
245 }
246
247 usess = session->ust_session;
248 assert(discarded_events);
249 assert(lost_packets);
250
251 if (!usess || !session->has_been_started) {
252 *discarded_events = 0;
253 *lost_packets = 0;
254 ret = 0;
255 goto end;
256 }
257
258 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
259 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
260 &usess->buffer_reg_uid_list,
261 usess->consumer, uchan->id,
262 uchan->attr.overwrite,
263 discarded_events,
264 lost_packets);
265 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
266 ret = ust_app_pid_get_channel_runtime_stats(usess,
267 uchan, usess->consumer,
268 uchan->attr.overwrite,
269 discarded_events,
270 lost_packets);
271 if (ret < 0) {
272 goto end;
273 }
274 *discarded_events += uchan->per_pid_closed_app_discarded;
275 *lost_packets += uchan->per_pid_closed_app_lost;
276 } else {
277 ERR("Unsupported buffer type");
278 assert(0);
279 ret = -1;
280 goto end;
281 }
282
283 end:
284 return ret;
285 }
286
287 /*
288 * Fill lttng_channel array of all channels.
289 */
290 static ssize_t list_lttng_channels(enum lttng_domain_type domain,
291 struct ltt_session *session, struct lttng_channel *channels,
292 struct lttng_channel_extended *chan_exts)
293 {
294 int i = 0, ret = 0;
295 struct ltt_kernel_channel *kchan;
296
297 DBG("Listing channels for session %s", session->name);
298
299 switch (domain) {
300 case LTTNG_DOMAIN_KERNEL:
301 /* Kernel channels */
302 if (session->kernel_session != NULL) {
303 cds_list_for_each_entry(kchan,
304 &session->kernel_session->channel_list.head, list) {
305 uint64_t discarded_events, lost_packets;
306 struct lttng_channel_extended *extended;
307
308 extended = (struct lttng_channel_extended *)
309 kchan->channel->attr.extended.ptr;
310
311 ret = get_kernel_runtime_stats(session, kchan,
312 &discarded_events, &lost_packets);
313 if (ret < 0) {
314 goto end;
315 }
316 /* Copy lttng_channel struct to array */
317 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
318 channels[i].enabled = kchan->enabled;
319 chan_exts[i].discarded_events =
320 discarded_events;
321 chan_exts[i].lost_packets = lost_packets;
322 chan_exts[i].monitor_timer_interval =
323 extended->monitor_timer_interval;
324 chan_exts[i].blocking_timeout = 0;
325 i++;
326 }
327 }
328 break;
329 case LTTNG_DOMAIN_UST:
330 {
331 struct lttng_ht_iter iter;
332 struct ltt_ust_channel *uchan;
333
334 rcu_read_lock();
335 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
336 &iter.iter, uchan, node.node) {
337 uint64_t discarded_events = 0, lost_packets = 0;
338
339 if (lttng_strncpy(channels[i].name, uchan->name,
340 LTTNG_SYMBOL_NAME_LEN)) {
341 break;
342 }
343 channels[i].attr.overwrite = uchan->attr.overwrite;
344 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
345 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
346 channels[i].attr.switch_timer_interval =
347 uchan->attr.switch_timer_interval;
348 channels[i].attr.read_timer_interval =
349 uchan->attr.read_timer_interval;
350 channels[i].enabled = uchan->enabled;
351 channels[i].attr.tracefile_size = uchan->tracefile_size;
352 channels[i].attr.tracefile_count = uchan->tracefile_count;
353
354 /*
355 * Map enum lttng_ust_output to enum lttng_event_output.
356 */
357 switch (uchan->attr.output) {
358 case LTTNG_UST_MMAP:
359 channels[i].attr.output = LTTNG_EVENT_MMAP;
360 break;
361 default:
362 /*
363 * LTTNG_UST_MMAP is the only supported UST
364 * output mode.
365 */
366 assert(0);
367 break;
368 }
369
370 chan_exts[i].monitor_timer_interval =
371 uchan->monitor_timer_interval;
372 chan_exts[i].blocking_timeout =
373 uchan->attr.u.s.blocking_timeout;
374
375 ret = get_ust_runtime_stats(session, uchan,
376 &discarded_events, &lost_packets);
377 if (ret < 0) {
378 break;
379 }
380 chan_exts[i].discarded_events = discarded_events;
381 chan_exts[i].lost_packets = lost_packets;
382 i++;
383 }
384 rcu_read_unlock();
385 break;
386 }
387 default:
388 break;
389 }
390
391 end:
392 if (ret < 0) {
393 return -LTTNG_ERR_FATAL;
394 } else {
395 return LTTNG_OK;
396 }
397 }
398
399 static int increment_extended_len(const char *filter_expression,
400 struct lttng_event_exclusion *exclusion,
401 const struct lttng_userspace_probe_location *probe_location,
402 size_t *extended_len)
403 {
404 int ret = 0;
405
406 *extended_len += sizeof(struct lttcomm_event_extended_header);
407
408 if (filter_expression) {
409 *extended_len += strlen(filter_expression) + 1;
410 }
411
412 if (exclusion) {
413 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
414 }
415
416 if (probe_location) {
417 ret = lttng_userspace_probe_location_serialize(probe_location,
418 NULL, NULL);
419 if (ret < 0) {
420 goto end;
421 }
422 *extended_len += ret;
423 }
424 ret = 0;
425 end:
426 return ret;
427 }
428
429 static int append_extended_info(const char *filter_expression,
430 struct lttng_event_exclusion *exclusion,
431 struct lttng_userspace_probe_location *probe_location,
432 void **extended_at)
433 {
434 int ret = 0;
435 size_t filter_len = 0;
436 size_t nb_exclusions = 0;
437 size_t userspace_probe_location_len = 0;
438 struct lttng_dynamic_buffer location_buffer;
439 struct lttcomm_event_extended_header extended_header;
440
441 if (filter_expression) {
442 filter_len = strlen(filter_expression) + 1;
443 }
444
445 if (exclusion) {
446 nb_exclusions = exclusion->count;
447 }
448
449 if (probe_location) {
450 lttng_dynamic_buffer_init(&location_buffer);
451 ret = lttng_userspace_probe_location_serialize(probe_location,
452 &location_buffer, NULL);
453 if (ret < 0) {
454 ret = -1;
455 goto end;
456 }
457 userspace_probe_location_len = location_buffer.size;
458 }
459
460 /* Set header fields */
461 extended_header.filter_len = filter_len;
462 extended_header.nb_exclusions = nb_exclusions;
463 extended_header.userspace_probe_location_len = userspace_probe_location_len;
464
465 /* Copy header */
466 memcpy(*extended_at, &extended_header, sizeof(extended_header));
467 *extended_at += sizeof(extended_header);
468
469 /* Copy filter string */
470 if (filter_expression) {
471 memcpy(*extended_at, filter_expression, filter_len);
472 *extended_at += filter_len;
473 }
474
475 /* Copy exclusion names */
476 if (exclusion) {
477 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
478
479 memcpy(*extended_at, &exclusion->names, len);
480 *extended_at += len;
481 }
482
483 if (probe_location) {
484 memcpy(*extended_at, location_buffer.data, location_buffer.size);
485 *extended_at += location_buffer.size;
486 lttng_dynamic_buffer_reset(&location_buffer);
487 }
488 ret = 0;
489 end:
490 return ret;
491 }
492
493 /*
494 * Create a list of agent domain events.
495 *
496 * Return number of events in list on success or else a negative value.
497 */
498 static int list_lttng_agent_events(struct agent *agt,
499 struct lttng_event **events, size_t *total_size)
500 {
501 int i = 0, ret = 0;
502 unsigned int nb_event = 0;
503 struct agent_event *event;
504 struct lttng_event *tmp_events = NULL;
505 struct lttng_ht_iter iter;
506 size_t extended_len = 0;
507 void *extended_at;
508
509 assert(agt);
510 assert(events);
511
512 DBG3("Listing agent events");
513
514 rcu_read_lock();
515 nb_event = lttng_ht_get_count(agt->events);
516 rcu_read_unlock();
517 if (nb_event == 0) {
518 ret = nb_event;
519 *total_size = 0;
520 goto error;
521 }
522
523 /* Compute required extended infos size */
524 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
525
526 /*
527 * This is only valid because the commands which add events are
528 * processed in the same thread as the listing.
529 */
530 rcu_read_lock();
531 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
532 ret = increment_extended_len(event->filter_expression, NULL, NULL,
533 &extended_len);
534 if (ret) {
535 DBG("Error computing the length of extended info message");
536 ret = -LTTNG_ERR_FATAL;
537 goto error;
538 }
539 }
540 rcu_read_unlock();
541
542 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
543 tmp_events = zmalloc(*total_size);
544 if (!tmp_events) {
545 PERROR("zmalloc agent events session");
546 ret = -LTTNG_ERR_FATAL;
547 goto error;
548 }
549
550 extended_at = ((uint8_t *) tmp_events) +
551 nb_event * sizeof(struct lttng_event);
552
553 rcu_read_lock();
554 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
555 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
556 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
557 tmp_events[i].enabled = event->enabled;
558 tmp_events[i].loglevel = event->loglevel_value;
559 tmp_events[i].loglevel_type = event->loglevel_type;
560 i++;
561
562 /* Append extended info */
563 ret = append_extended_info(event->filter_expression, NULL, NULL,
564 &extended_at);
565 if (ret) {
566 DBG("Error appending extended info message");
567 ret = -LTTNG_ERR_FATAL;
568 goto error;
569 }
570 }
571
572 *events = tmp_events;
573 ret = nb_event;
574 assert(nb_event == i);
575
576 end:
577 rcu_read_unlock();
578 return ret;
579 error:
580 free(tmp_events);
581 goto end;
582 }
583
584 /*
585 * Create a list of ust global domain events.
586 */
587 static int list_lttng_ust_global_events(char *channel_name,
588 struct ltt_ust_domain_global *ust_global,
589 struct lttng_event **events, size_t *total_size)
590 {
591 int i = 0, ret = 0;
592 unsigned int nb_event = 0;
593 struct lttng_ht_iter iter;
594 struct lttng_ht_node_str *node;
595 struct ltt_ust_channel *uchan;
596 struct ltt_ust_event *uevent;
597 struct lttng_event *tmp;
598 size_t extended_len = 0;
599 void *extended_at;
600
601 DBG("Listing UST global events for channel %s", channel_name);
602
603 rcu_read_lock();
604
605 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
606 node = lttng_ht_iter_get_node_str(&iter);
607 if (node == NULL) {
608 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
609 goto end;
610 }
611
612 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
613
614 nb_event = lttng_ht_get_count(uchan->events);
615 if (nb_event == 0) {
616 ret = nb_event;
617 *total_size = 0;
618 goto end;
619 }
620
621 DBG3("Listing UST global %d events", nb_event);
622
623 /* Compute required extended infos size */
624 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
625 if (uevent->internal) {
626 nb_event--;
627 continue;
628 }
629
630 ret = increment_extended_len(uevent->filter_expression,
631 uevent->exclusion, NULL, &extended_len);
632 if (ret) {
633 DBG("Error computing the length of extended info message");
634 ret = -LTTNG_ERR_FATAL;
635 goto end;
636 }
637 }
638 if (nb_event == 0) {
639 /* All events are internal, skip. */
640 ret = 0;
641 *total_size = 0;
642 goto end;
643 }
644
645 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
646 tmp = zmalloc(*total_size);
647 if (tmp == NULL) {
648 ret = -LTTNG_ERR_FATAL;
649 goto end;
650 }
651
652 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
653
654 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
655 if (uevent->internal) {
656 /* This event should remain hidden from clients */
657 continue;
658 }
659 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
660 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
661 tmp[i].enabled = uevent->enabled;
662
663 switch (uevent->attr.instrumentation) {
664 case LTTNG_UST_TRACEPOINT:
665 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
666 break;
667 case LTTNG_UST_PROBE:
668 tmp[i].type = LTTNG_EVENT_PROBE;
669 break;
670 case LTTNG_UST_FUNCTION:
671 tmp[i].type = LTTNG_EVENT_FUNCTION;
672 break;
673 }
674
675 tmp[i].loglevel = uevent->attr.loglevel;
676 switch (uevent->attr.loglevel_type) {
677 case LTTNG_UST_LOGLEVEL_ALL:
678 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
679 break;
680 case LTTNG_UST_LOGLEVEL_RANGE:
681 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
682 break;
683 case LTTNG_UST_LOGLEVEL_SINGLE:
684 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
685 break;
686 }
687 if (uevent->filter) {
688 tmp[i].filter = 1;
689 }
690 if (uevent->exclusion) {
691 tmp[i].exclusion = 1;
692 }
693 i++;
694
695 /* Append extended info */
696 ret = append_extended_info(uevent->filter_expression,
697 uevent->exclusion, NULL, &extended_at);
698 if (ret) {
699 DBG("Error appending extended info message");
700 ret = -LTTNG_ERR_FATAL;
701 goto end;
702 }
703 }
704
705 ret = nb_event;
706 *events = tmp;
707 end:
708 rcu_read_unlock();
709 return ret;
710 }
711
712 /*
713 * Fill lttng_event array of all kernel events in the channel.
714 */
715 static int list_lttng_kernel_events(char *channel_name,
716 struct ltt_kernel_session *kernel_session,
717 struct lttng_event **events, size_t *total_size)
718 {
719 int i = 0, ret;
720 unsigned int nb_event;
721 struct ltt_kernel_event *event;
722 struct ltt_kernel_channel *kchan;
723 size_t extended_len = 0;
724 void *extended_at;
725
726 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
727 if (kchan == NULL) {
728 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
729 goto error;
730 }
731
732 nb_event = kchan->event_count;
733
734 DBG("Listing events for channel %s", kchan->channel->name);
735
736 if (nb_event == 0) {
737 *total_size = 0;
738 *events = NULL;
739 goto end;
740 }
741
742 /* Compute required extended infos size */
743 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
744 ret = increment_extended_len(event->filter_expression, NULL,
745 event->userspace_probe_location,
746 &extended_len);
747 if (ret) {
748 DBG("Error computing the length of extended info message");
749 ret = -LTTNG_ERR_FATAL;
750 goto error;
751 }
752 }
753
754 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
755 *events = zmalloc(*total_size);
756 if (*events == NULL) {
757 ret = -LTTNG_ERR_FATAL;
758 goto error;
759 }
760
761 extended_at = ((void *) *events) +
762 nb_event * sizeof(struct lttng_event);
763
764 /* Kernel channels */
765 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
766 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
767 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
768 (*events)[i].enabled = event->enabled;
769 (*events)[i].filter =
770 (unsigned char) !!event->filter_expression;
771
772 switch (event->event->instrumentation) {
773 case LTTNG_KERNEL_TRACEPOINT:
774 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
775 break;
776 case LTTNG_KERNEL_KRETPROBE:
777 (*events)[i].type = LTTNG_EVENT_FUNCTION;
778 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
779 sizeof(struct lttng_kernel_kprobe));
780 break;
781 case LTTNG_KERNEL_KPROBE:
782 (*events)[i].type = LTTNG_EVENT_PROBE;
783 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
784 sizeof(struct lttng_kernel_kprobe));
785 break;
786 case LTTNG_KERNEL_UPROBE:
787 (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE;
788 break;
789 case LTTNG_KERNEL_FUNCTION:
790 (*events)[i].type = LTTNG_EVENT_FUNCTION;
791 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
792 sizeof(struct lttng_kernel_function));
793 break;
794 case LTTNG_KERNEL_NOOP:
795 (*events)[i].type = LTTNG_EVENT_NOOP;
796 break;
797 case LTTNG_KERNEL_SYSCALL:
798 (*events)[i].type = LTTNG_EVENT_SYSCALL;
799 break;
800 case LTTNG_KERNEL_ALL:
801 /* fall-through. */
802 default:
803 assert(0);
804 break;
805 }
806 i++;
807
808 /* Append extended info */
809 ret = append_extended_info(event->filter_expression, NULL,
810 event->userspace_probe_location, &extended_at);
811 if (ret) {
812 DBG("Error appending extended info message");
813 ret = -LTTNG_ERR_FATAL;
814 goto error;
815 }
816 }
817
818 end:
819 return nb_event;
820
821 error:
822 return ret;
823 }
824
825 /*
826 * Add URI so the consumer output object. Set the correct path depending on the
827 * domain adding the default trace directory.
828 */
829 static enum lttng_error_code add_uri_to_consumer(
830 const struct ltt_session *session,
831 struct consumer_output *consumer,
832 struct lttng_uri *uri, enum lttng_domain_type domain)
833 {
834 int ret;
835 enum lttng_error_code ret_code = LTTNG_OK;
836
837 assert(uri);
838
839 if (consumer == NULL) {
840 DBG("No consumer detected. Don't add URI. Stopping.");
841 ret_code = LTTNG_ERR_NO_CONSUMER;
842 goto error;
843 }
844
845 switch (domain) {
846 case LTTNG_DOMAIN_KERNEL:
847 ret = lttng_strncpy(consumer->domain_subdir,
848 DEFAULT_KERNEL_TRACE_DIR,
849 sizeof(consumer->domain_subdir));
850 break;
851 case LTTNG_DOMAIN_UST:
852 ret = lttng_strncpy(consumer->domain_subdir,
853 DEFAULT_UST_TRACE_DIR,
854 sizeof(consumer->domain_subdir));
855 break;
856 default:
857 /*
858 * This case is possible is we try to add the URI to the global
859 * tracing session consumer object which in this case there is
860 * no subdir.
861 */
862 memset(consumer->domain_subdir, 0,
863 sizeof(consumer->domain_subdir));
864 ret = 0;
865 }
866 if (ret) {
867 ERR("Failed to initialize consumer output domain subdirectory");
868 ret_code = LTTNG_ERR_FATAL;
869 goto error;
870 }
871
872 switch (uri->dtype) {
873 case LTTNG_DST_IPV4:
874 case LTTNG_DST_IPV6:
875 DBG2("Setting network URI to consumer");
876
877 if (consumer->type == CONSUMER_DST_NET) {
878 if ((uri->stype == LTTNG_STREAM_CONTROL &&
879 consumer->dst.net.control_isset) ||
880 (uri->stype == LTTNG_STREAM_DATA &&
881 consumer->dst.net.data_isset)) {
882 ret_code = LTTNG_ERR_URL_EXIST;
883 goto error;
884 }
885 } else {
886 memset(&consumer->dst, 0, sizeof(consumer->dst));
887 }
888
889 /* Set URI into consumer output object */
890 ret = consumer_set_network_uri(session, consumer, uri);
891 if (ret < 0) {
892 ret_code = -ret;
893 goto error;
894 } else if (ret == 1) {
895 /*
896 * URI was the same in the consumer so we do not append the subdir
897 * again so to not duplicate output dir.
898 */
899 ret_code = LTTNG_OK;
900 goto error;
901 }
902 break;
903 case LTTNG_DST_PATH:
904 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
905 ret_code = LTTNG_ERR_INVALID;
906 goto error;
907 }
908 DBG2("Setting trace directory path from URI to %s",
909 uri->dst.path);
910 memset(&consumer->dst, 0, sizeof(consumer->dst));
911
912 ret = lttng_strncpy(consumer->dst.session_root_path,
913 uri->dst.path,
914 sizeof(consumer->dst.session_root_path));
915 if (ret) {
916 ret_code = LTTNG_ERR_FATAL;
917 goto error;
918 }
919 consumer->type = CONSUMER_DST_LOCAL;
920 break;
921 }
922
923 ret_code = LTTNG_OK;
924 error:
925 return ret_code;
926 }
927
928 /*
929 * Init tracing by creating trace directory and sending fds kernel consumer.
930 */
931 static int init_kernel_tracing(struct ltt_kernel_session *session)
932 {
933 int ret = 0;
934 struct lttng_ht_iter iter;
935 struct consumer_socket *socket;
936
937 assert(session);
938
939 rcu_read_lock();
940
941 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
942 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
943 socket, node.node) {
944 pthread_mutex_lock(socket->lock);
945 ret = kernel_consumer_send_session(socket, session);
946 pthread_mutex_unlock(socket->lock);
947 if (ret < 0) {
948 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
949 goto error;
950 }
951 }
952 }
953
954 error:
955 rcu_read_unlock();
956 return ret;
957 }
958
959 /*
960 * Create a socket to the relayd using the URI.
961 *
962 * On success, the relayd_sock pointer is set to the created socket.
963 * Else, it remains untouched and an LTTng error code is returned.
964 */
965 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
966 struct lttcomm_relayd_sock **relayd_sock,
967 struct consumer_output *consumer)
968 {
969 int ret;
970 enum lttng_error_code status = LTTNG_OK;
971 struct lttcomm_relayd_sock *rsock;
972
973 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
974 RELAYD_VERSION_COMM_MINOR);
975 if (!rsock) {
976 status = LTTNG_ERR_FATAL;
977 goto error;
978 }
979
980 /*
981 * Connect to relayd so we can proceed with a session creation. This call
982 * can possibly block for an arbitrary amount of time to set the health
983 * state to be in poll execution.
984 */
985 health_poll_entry();
986 ret = relayd_connect(rsock);
987 health_poll_exit();
988 if (ret < 0) {
989 ERR("Unable to reach lttng-relayd");
990 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
991 goto free_sock;
992 }
993
994 /* Create socket for control stream. */
995 if (uri->stype == LTTNG_STREAM_CONTROL) {
996 uint64_t result_flags;
997
998 DBG3("Creating relayd stream socket from URI");
999
1000 /* Check relayd version */
1001 ret = relayd_version_check(rsock);
1002 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
1003 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
1004 goto close_sock;
1005 } else if (ret < 0) {
1006 ERR("Unable to reach lttng-relayd");
1007 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
1008 goto close_sock;
1009 }
1010 consumer->relay_major_version = rsock->major;
1011 consumer->relay_minor_version = rsock->minor;
1012 ret = relayd_get_configuration(rsock, 0,
1013 &result_flags);
1014 if (ret < 0) {
1015 ERR("Unable to get relayd configuration");
1016 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
1017 goto close_sock;
1018 }
1019 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
1020 consumer->relay_allows_clear = true;
1021 }
1022 } else if (uri->stype == LTTNG_STREAM_DATA) {
1023 DBG3("Creating relayd data socket from URI");
1024 } else {
1025 /* Command is not valid */
1026 ERR("Relayd invalid stream type: %d", uri->stype);
1027 status = LTTNG_ERR_INVALID;
1028 goto close_sock;
1029 }
1030
1031 *relayd_sock = rsock;
1032
1033 return status;
1034
1035 close_sock:
1036 /* The returned value is not useful since we are on an error path. */
1037 (void) relayd_close(rsock);
1038 free_sock:
1039 free(rsock);
1040 error:
1041 return status;
1042 }
1043
1044 /*
1045 * Connect to the relayd using URI and send the socket to the right consumer.
1046 *
1047 * The consumer socket lock must be held by the caller.
1048 *
1049 * Returns LTTNG_OK on success or an LTTng error code on failure.
1050 */
1051 static enum lttng_error_code send_consumer_relayd_socket(
1052 unsigned int session_id,
1053 struct lttng_uri *relayd_uri,
1054 struct consumer_output *consumer,
1055 struct consumer_socket *consumer_sock,
1056 const char *session_name, const char *hostname,
1057 const char *base_path, int session_live_timer,
1058 const uint64_t *current_chunk_id,
1059 time_t session_creation_time,
1060 bool session_name_contains_creation_time)
1061 {
1062 int ret;
1063 struct lttcomm_relayd_sock *rsock = NULL;
1064 enum lttng_error_code status;
1065
1066 /* Connect to relayd and make version check if uri is the control. */
1067 status = create_connect_relayd(relayd_uri, &rsock, consumer);
1068 if (status != LTTNG_OK) {
1069 goto relayd_comm_error;
1070 }
1071 assert(rsock);
1072
1073 /* Set the network sequence index if not set. */
1074 if (consumer->net_seq_index == (uint64_t) -1ULL) {
1075 pthread_mutex_lock(&relayd_net_seq_idx_lock);
1076 /*
1077 * Increment net_seq_idx because we are about to transfer the
1078 * new relayd socket to the consumer.
1079 * Assign unique key so the consumer can match streams.
1080 */
1081 consumer->net_seq_index = ++relayd_net_seq_idx;
1082 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
1083 }
1084
1085 /* Send relayd socket to consumer. */
1086 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
1087 relayd_uri->stype, session_id,
1088 session_name, hostname, base_path,
1089 session_live_timer, current_chunk_id,
1090 session_creation_time, session_name_contains_creation_time);
1091 if (ret < 0) {
1092 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
1093 goto close_sock;
1094 }
1095
1096 /* Flag that the corresponding socket was sent. */
1097 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
1098 consumer_sock->control_sock_sent = 1;
1099 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
1100 consumer_sock->data_sock_sent = 1;
1101 }
1102
1103 /*
1104 * Close socket which was dup on the consumer side. The session daemon does
1105 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1106 */
1107
1108 close_sock:
1109 if (status != LTTNG_OK) {
1110 /*
1111 * The consumer output for this session should not be used anymore
1112 * since the relayd connection failed thus making any tracing or/and
1113 * streaming not usable.
1114 */
1115 consumer->enabled = 0;
1116 }
1117 (void) relayd_close(rsock);
1118 free(rsock);
1119
1120 relayd_comm_error:
1121 return status;
1122 }
1123
1124 /*
1125 * Send both relayd sockets to a specific consumer and domain. This is a
1126 * helper function to facilitate sending the information to the consumer for a
1127 * session.
1128 *
1129 * The consumer socket lock must be held by the caller.
1130 *
1131 * Returns LTTNG_OK, or an LTTng error code on failure.
1132 */
1133 static enum lttng_error_code send_consumer_relayd_sockets(
1134 enum lttng_domain_type domain,
1135 unsigned int session_id, struct consumer_output *consumer,
1136 struct consumer_socket *sock, const char *session_name,
1137 const char *hostname, const char *base_path, int session_live_timer,
1138 const uint64_t *current_chunk_id, time_t session_creation_time,
1139 bool session_name_contains_creation_time)
1140 {
1141 enum lttng_error_code status = LTTNG_OK;
1142
1143 assert(consumer);
1144 assert(sock);
1145
1146 /* Sending control relayd socket. */
1147 if (!sock->control_sock_sent) {
1148 status = send_consumer_relayd_socket(session_id,
1149 &consumer->dst.net.control, consumer, sock,
1150 session_name, hostname, base_path, session_live_timer,
1151 current_chunk_id, session_creation_time,
1152 session_name_contains_creation_time);
1153 if (status != LTTNG_OK) {
1154 goto error;
1155 }
1156 }
1157
1158 /* Sending data relayd socket. */
1159 if (!sock->data_sock_sent) {
1160 status = send_consumer_relayd_socket(session_id,
1161 &consumer->dst.net.data, consumer, sock,
1162 session_name, hostname, base_path, session_live_timer,
1163 current_chunk_id, session_creation_time,
1164 session_name_contains_creation_time);
1165 if (status != LTTNG_OK) {
1166 goto error;
1167 }
1168 }
1169
1170 error:
1171 return status;
1172 }
1173
1174 /*
1175 * Setup relayd connections for a tracing session. First creates the socket to
1176 * the relayd and send them to the right domain consumer. Consumer type MUST be
1177 * network.
1178 */
1179 int cmd_setup_relayd(struct ltt_session *session)
1180 {
1181 int ret = LTTNG_OK;
1182 struct ltt_ust_session *usess;
1183 struct ltt_kernel_session *ksess;
1184 struct consumer_socket *socket;
1185 struct lttng_ht_iter iter;
1186 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1187
1188 assert(session);
1189
1190 usess = session->ust_session;
1191 ksess = session->kernel_session;
1192
1193 DBG("Setting relayd for session %s", session->name);
1194
1195 rcu_read_lock();
1196 if (session->current_trace_chunk) {
1197 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1198 session->current_trace_chunk, &current_chunk_id.value);
1199
1200 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1201 current_chunk_id.is_set = true;
1202 } else {
1203 ERR("Failed to get current trace chunk id");
1204 ret = LTTNG_ERR_UNK;
1205 goto error;
1206 }
1207 }
1208
1209 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1210 && usess->consumer->enabled) {
1211 /* For each consumer socket, send relayd sockets */
1212 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1213 socket, node.node) {
1214 pthread_mutex_lock(socket->lock);
1215 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1216 usess->consumer, socket,
1217 session->name, session->hostname,
1218 session->base_path,
1219 session->live_timer,
1220 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1221 session->creation_time,
1222 session->name_contains_creation_time);
1223 pthread_mutex_unlock(socket->lock);
1224 if (ret != LTTNG_OK) {
1225 goto error;
1226 }
1227 /* Session is now ready for network streaming. */
1228 session->net_handle = 1;
1229 }
1230 session->consumer->relay_major_version =
1231 usess->consumer->relay_major_version;
1232 session->consumer->relay_minor_version =
1233 usess->consumer->relay_minor_version;
1234 session->consumer->relay_allows_clear =
1235 usess->consumer->relay_allows_clear;
1236 }
1237
1238 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1239 && ksess->consumer->enabled) {
1240 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1241 socket, node.node) {
1242 pthread_mutex_lock(socket->lock);
1243 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1244 ksess->consumer, socket,
1245 session->name, session->hostname,
1246 session->base_path,
1247 session->live_timer,
1248 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1249 session->creation_time,
1250 session->name_contains_creation_time);
1251 pthread_mutex_unlock(socket->lock);
1252 if (ret != LTTNG_OK) {
1253 goto error;
1254 }
1255 /* Session is now ready for network streaming. */
1256 session->net_handle = 1;
1257 }
1258 session->consumer->relay_major_version =
1259 ksess->consumer->relay_major_version;
1260 session->consumer->relay_minor_version =
1261 ksess->consumer->relay_minor_version;
1262 session->consumer->relay_allows_clear =
1263 ksess->consumer->relay_allows_clear;
1264 }
1265
1266 error:
1267 rcu_read_unlock();
1268 return ret;
1269 }
1270
1271 /*
1272 * Start a kernel session by opening all necessary streams.
1273 */
1274 int start_kernel_session(struct ltt_kernel_session *ksess)
1275 {
1276 int ret;
1277 struct ltt_kernel_channel *kchan;
1278
1279 /* Open kernel metadata */
1280 if (ksess->metadata == NULL && ksess->output_traces) {
1281 ret = kernel_open_metadata(ksess);
1282 if (ret < 0) {
1283 ret = LTTNG_ERR_KERN_META_FAIL;
1284 goto error;
1285 }
1286 }
1287
1288 /* Open kernel metadata stream */
1289 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1290 ret = kernel_open_metadata_stream(ksess);
1291 if (ret < 0) {
1292 ERR("Kernel create metadata stream failed");
1293 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1294 goto error;
1295 }
1296 }
1297
1298 /* For each channel */
1299 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1300 if (kchan->stream_count == 0) {
1301 ret = kernel_open_channel_stream(kchan);
1302 if (ret < 0) {
1303 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1304 goto error;
1305 }
1306 /* Update the stream global counter */
1307 ksess->stream_count_global += ret;
1308 }
1309 }
1310
1311 /* Setup kernel consumer socket and send fds to it */
1312 ret = init_kernel_tracing(ksess);
1313 if (ret != 0) {
1314 ret = LTTNG_ERR_KERN_START_FAIL;
1315 goto error;
1316 }
1317
1318 /* This start the kernel tracing */
1319 ret = kernel_start_session(ksess);
1320 if (ret < 0) {
1321 ret = LTTNG_ERR_KERN_START_FAIL;
1322 goto error;
1323 }
1324
1325 /* Quiescent wait after starting trace */
1326 kernel_wait_quiescent();
1327
1328 ksess->active = 1;
1329
1330 ret = LTTNG_OK;
1331
1332 error:
1333 return ret;
1334 }
1335
1336 int stop_kernel_session(struct ltt_kernel_session *ksess)
1337 {
1338 struct ltt_kernel_channel *kchan;
1339 bool error_occurred = false;
1340 int ret;
1341
1342 if (!ksess || !ksess->active) {
1343 return LTTNG_OK;
1344 }
1345 DBG("Stopping kernel tracing");
1346
1347 ret = kernel_stop_session(ksess);
1348 if (ret < 0) {
1349 ret = LTTNG_ERR_KERN_STOP_FAIL;
1350 goto error;
1351 }
1352
1353 kernel_wait_quiescent();
1354
1355 /* Flush metadata after stopping (if exists) */
1356 if (ksess->metadata_stream_fd >= 0) {
1357 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1358 if (ret < 0) {
1359 ERR("Kernel metadata flush failed");
1360 error_occurred = true;
1361 }
1362 }
1363
1364 /* Flush all buffers after stopping */
1365 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1366 ret = kernel_flush_buffer(kchan);
1367 if (ret < 0) {
1368 ERR("Kernel flush buffer error");
1369 error_occurred = true;
1370 }
1371 }
1372
1373 ksess->active = 0;
1374 if (error_occurred) {
1375 ret = LTTNG_ERR_UNK;
1376 } else {
1377 ret = LTTNG_OK;
1378 }
1379 error:
1380 return ret;
1381 }
1382
1383 /*
1384 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1385 */
1386 int cmd_disable_channel(struct ltt_session *session,
1387 enum lttng_domain_type domain, char *channel_name)
1388 {
1389 int ret;
1390 struct ltt_ust_session *usess;
1391
1392 usess = session->ust_session;
1393
1394 rcu_read_lock();
1395
1396 switch (domain) {
1397 case LTTNG_DOMAIN_KERNEL:
1398 {
1399 ret = channel_kernel_disable(session->kernel_session,
1400 channel_name);
1401 if (ret != LTTNG_OK) {
1402 goto error;
1403 }
1404
1405 kernel_wait_quiescent();
1406 break;
1407 }
1408 case LTTNG_DOMAIN_UST:
1409 {
1410 struct ltt_ust_channel *uchan;
1411 struct lttng_ht *chan_ht;
1412
1413 chan_ht = usess->domain_global.channels;
1414
1415 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1416 if (uchan == NULL) {
1417 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1418 goto error;
1419 }
1420
1421 ret = channel_ust_disable(usess, uchan);
1422 if (ret != LTTNG_OK) {
1423 goto error;
1424 }
1425 break;
1426 }
1427 default:
1428 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1429 goto error;
1430 }
1431
1432 ret = LTTNG_OK;
1433
1434 error:
1435 rcu_read_unlock();
1436 return ret;
1437 }
1438
1439 /*
1440 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1441 *
1442 * The wpipe arguments is used as a notifier for the kernel thread.
1443 */
1444 int cmd_enable_channel(struct ltt_session *session,
1445 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
1446 {
1447 int ret;
1448 struct ltt_ust_session *usess = session->ust_session;
1449 struct lttng_ht *chan_ht;
1450 size_t len;
1451 struct lttng_channel attr;
1452
1453 assert(session);
1454 assert(_attr);
1455 assert(domain);
1456
1457 attr = *_attr;
1458 len = lttng_strnlen(attr.name, sizeof(attr.name));
1459
1460 /* Validate channel name */
1461 if (attr.name[0] == '.' ||
1462 memchr(attr.name, '/', len) != NULL) {
1463 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1464 goto end;
1465 }
1466
1467 DBG("Enabling channel %s for session %s", attr.name, session->name);
1468
1469 rcu_read_lock();
1470
1471 /*
1472 * Don't try to enable a channel if the session has been started at
1473 * some point in time before. The tracer does not allow it.
1474 */
1475 if (session->has_been_started) {
1476 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1477 goto error;
1478 }
1479
1480 /*
1481 * If the session is a live session, remove the switch timer, the
1482 * live timer does the same thing but sends also synchronisation
1483 * beacons for inactive streams.
1484 */
1485 if (session->live_timer > 0) {
1486 attr.attr.live_timer_interval = session->live_timer;
1487 attr.attr.switch_timer_interval = 0;
1488 }
1489
1490 /* Check for feature support */
1491 switch (domain->type) {
1492 case LTTNG_DOMAIN_KERNEL:
1493 {
1494 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1495 /* Sampling position of buffer is not supported */
1496 WARN("Kernel tracer does not support buffer monitoring. "
1497 "Setting the monitor interval timer to 0 "
1498 "(disabled) for channel '%s' of session '%s'",
1499 attr.name, session->name);
1500 lttng_channel_set_monitor_timer_interval(&attr, 0);
1501 }
1502 break;
1503 }
1504 case LTTNG_DOMAIN_UST:
1505 break;
1506 case LTTNG_DOMAIN_JUL:
1507 case LTTNG_DOMAIN_LOG4J:
1508 case LTTNG_DOMAIN_PYTHON:
1509 if (!agent_tracing_is_enabled()) {
1510 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1511 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1512 goto error;
1513 }
1514 break;
1515 default:
1516 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1517 goto error;
1518 }
1519
1520 switch (domain->type) {
1521 case LTTNG_DOMAIN_KERNEL:
1522 {
1523 struct ltt_kernel_channel *kchan;
1524
1525 kchan = trace_kernel_get_channel_by_name(attr.name,
1526 session->kernel_session);
1527 if (kchan == NULL) {
1528 if (session->snapshot.nb_output > 0 ||
1529 session->snapshot_mode) {
1530 /* Enforce mmap output for snapshot sessions. */
1531 attr.attr.output = LTTNG_EVENT_MMAP;
1532 }
1533 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1534 if (attr.name[0] != '\0') {
1535 session->kernel_session->has_non_default_channel = 1;
1536 }
1537 } else {
1538 ret = channel_kernel_enable(session->kernel_session, kchan);
1539 }
1540
1541 if (ret != LTTNG_OK) {
1542 goto error;
1543 }
1544
1545 kernel_wait_quiescent();
1546 break;
1547 }
1548 case LTTNG_DOMAIN_UST:
1549 case LTTNG_DOMAIN_JUL:
1550 case LTTNG_DOMAIN_LOG4J:
1551 case LTTNG_DOMAIN_PYTHON:
1552 {
1553 struct ltt_ust_channel *uchan;
1554
1555 /*
1556 * FIXME
1557 *
1558 * Current agent implementation limitations force us to allow
1559 * only one channel at once in "agent" subdomains. Each
1560 * subdomain has a default channel name which must be strictly
1561 * adhered to.
1562 */
1563 if (domain->type == LTTNG_DOMAIN_JUL) {
1564 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
1565 LTTNG_SYMBOL_NAME_LEN)) {
1566 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1567 goto error;
1568 }
1569 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1570 if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME,
1571 LTTNG_SYMBOL_NAME_LEN)) {
1572 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1573 goto error;
1574 }
1575 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1576 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
1577 LTTNG_SYMBOL_NAME_LEN)) {
1578 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1579 goto error;
1580 }
1581 }
1582
1583 chan_ht = usess->domain_global.channels;
1584
1585 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
1586 if (uchan == NULL) {
1587 ret = channel_ust_create(usess, &attr, domain->buf_type);
1588 if (attr.name[0] != '\0') {
1589 usess->has_non_default_channel = 1;
1590 }
1591 } else {
1592 ret = channel_ust_enable(usess, uchan);
1593 }
1594 break;
1595 }
1596 default:
1597 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1598 goto error;
1599 }
1600
1601 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
1602 session->has_non_mmap_channel = true;
1603 }
1604 error:
1605 rcu_read_unlock();
1606 end:
1607 return ret;
1608 }
1609
1610 enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1611 struct ltt_session *session,
1612 enum lttng_domain_type domain,
1613 enum lttng_process_attr process_attr,
1614 enum lttng_tracking_policy *policy)
1615 {
1616 enum lttng_error_code ret_code = LTTNG_OK;
1617 const struct process_attr_tracker *tracker;
1618
1619 switch (domain) {
1620 case LTTNG_DOMAIN_KERNEL:
1621 if (!session->kernel_session) {
1622 ret_code = LTTNG_ERR_INVALID;
1623 goto end;
1624 }
1625 tracker = kernel_get_process_attr_tracker(
1626 session->kernel_session, process_attr);
1627 break;
1628 case LTTNG_DOMAIN_UST:
1629 if (!session->ust_session) {
1630 ret_code = LTTNG_ERR_INVALID;
1631 goto end;
1632 }
1633 tracker = trace_ust_get_process_attr_tracker(
1634 session->ust_session, process_attr);
1635 break;
1636 default:
1637 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1638 goto end;
1639 }
1640 if (tracker) {
1641 *policy = process_attr_tracker_get_tracking_policy(tracker);
1642 } else {
1643 ret_code = LTTNG_ERR_INVALID;
1644 }
1645 end:
1646 return ret_code;
1647 }
1648
1649 enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1650 struct ltt_session *session,
1651 enum lttng_domain_type domain,
1652 enum lttng_process_attr process_attr,
1653 enum lttng_tracking_policy policy)
1654 {
1655 enum lttng_error_code ret_code = LTTNG_OK;
1656
1657 switch (policy) {
1658 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1659 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1660 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1661 break;
1662 default:
1663 ret_code = LTTNG_ERR_INVALID;
1664 goto end;
1665 }
1666
1667 switch (domain) {
1668 case LTTNG_DOMAIN_KERNEL:
1669 if (!session->kernel_session) {
1670 ret_code = LTTNG_ERR_INVALID;
1671 goto end;
1672 }
1673 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1674 session->kernel_session, process_attr, policy);
1675 break;
1676 case LTTNG_DOMAIN_UST:
1677 if (!session->ust_session) {
1678 ret_code = LTTNG_ERR_INVALID;
1679 goto end;
1680 }
1681 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1682 session->ust_session, process_attr, policy);
1683 break;
1684 default:
1685 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1686 break;
1687 }
1688 end:
1689 return ret_code;
1690 }
1691
1692 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1693 struct ltt_session *session,
1694 enum lttng_domain_type domain,
1695 enum lttng_process_attr process_attr,
1696 const struct process_attr_value *value)
1697 {
1698 enum lttng_error_code ret_code = LTTNG_OK;
1699
1700 switch (domain) {
1701 case LTTNG_DOMAIN_KERNEL:
1702 if (!session->kernel_session) {
1703 ret_code = LTTNG_ERR_INVALID;
1704 goto end;
1705 }
1706 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1707 session->kernel_session, process_attr, value);
1708 break;
1709 case LTTNG_DOMAIN_UST:
1710 if (!session->ust_session) {
1711 ret_code = LTTNG_ERR_INVALID;
1712 goto end;
1713 }
1714 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1715 session->ust_session, process_attr, value);
1716 break;
1717 default:
1718 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1719 break;
1720 }
1721 end:
1722 return ret_code;
1723 }
1724
1725 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1726 struct ltt_session *session,
1727 enum lttng_domain_type domain,
1728 enum lttng_process_attr process_attr,
1729 const struct process_attr_value *value)
1730 {
1731 enum lttng_error_code ret_code = LTTNG_OK;
1732
1733 switch (domain) {
1734 case LTTNG_DOMAIN_KERNEL:
1735 if (!session->kernel_session) {
1736 ret_code = LTTNG_ERR_INVALID;
1737 goto end;
1738 }
1739 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1740 session->kernel_session, process_attr, value);
1741 break;
1742 case LTTNG_DOMAIN_UST:
1743 if (!session->ust_session) {
1744 ret_code = LTTNG_ERR_INVALID;
1745 goto end;
1746 }
1747 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1748 session->ust_session, process_attr, value);
1749 break;
1750 default:
1751 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1752 break;
1753 }
1754 end:
1755 return ret_code;
1756 }
1757
1758 enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1759 struct ltt_session *session,
1760 enum lttng_domain_type domain,
1761 enum lttng_process_attr process_attr,
1762 struct lttng_process_attr_values **values)
1763 {
1764 enum lttng_error_code ret_code = LTTNG_OK;
1765 const struct process_attr_tracker *tracker;
1766 enum process_attr_tracker_status status;
1767
1768 switch (domain) {
1769 case LTTNG_DOMAIN_KERNEL:
1770 if (!session->kernel_session) {
1771 ret_code = LTTNG_ERR_INVALID;
1772 goto end;
1773 }
1774 tracker = kernel_get_process_attr_tracker(
1775 session->kernel_session, process_attr);
1776 break;
1777 case LTTNG_DOMAIN_UST:
1778 if (!session->ust_session) {
1779 ret_code = LTTNG_ERR_INVALID;
1780 goto end;
1781 }
1782 tracker = trace_ust_get_process_attr_tracker(
1783 session->ust_session, process_attr);
1784 break;
1785 default:
1786 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1787 goto end;
1788 }
1789
1790 if (!tracker) {
1791 ret_code = LTTNG_ERR_INVALID;
1792 goto end;
1793 }
1794
1795 status = process_attr_tracker_get_inclusion_set(tracker, values);
1796 switch (status) {
1797 case PROCESS_ATTR_TRACKER_STATUS_OK:
1798 ret_code = LTTNG_OK;
1799 break;
1800 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1801 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1802 break;
1803 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1804 ret_code = LTTNG_ERR_NOMEM;
1805 break;
1806 default:
1807 ret_code = LTTNG_ERR_UNK;
1808 break;
1809 }
1810
1811 end:
1812 return ret_code;
1813 }
1814
1815 /*
1816 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1817 */
1818 int cmd_disable_event(struct ltt_session *session,
1819 enum lttng_domain_type domain, const char *channel_name,
1820 const struct lttng_event *event)
1821 {
1822 int ret;
1823 const char *event_name;
1824
1825 DBG("Disable event command for event \'%s\'", event->name);
1826
1827 event_name = event->name;
1828
1829 /* Error out on unhandled search criteria */
1830 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1831 || event->pid || event->filter || event->exclusion) {
1832 ret = LTTNG_ERR_UNK;
1833 goto error;
1834 }
1835
1836 rcu_read_lock();
1837
1838 switch (domain) {
1839 case LTTNG_DOMAIN_KERNEL:
1840 {
1841 struct ltt_kernel_channel *kchan;
1842 struct ltt_kernel_session *ksess;
1843
1844 ksess = session->kernel_session;
1845
1846 /*
1847 * If a non-default channel has been created in the
1848 * session, explicitely require that -c chan_name needs
1849 * to be provided.
1850 */
1851 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1852 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1853 goto error_unlock;
1854 }
1855
1856 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1857 if (kchan == NULL) {
1858 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1859 goto error_unlock;
1860 }
1861
1862 switch (event->type) {
1863 case LTTNG_EVENT_ALL:
1864 case LTTNG_EVENT_TRACEPOINT:
1865 case LTTNG_EVENT_SYSCALL:
1866 case LTTNG_EVENT_PROBE:
1867 case LTTNG_EVENT_FUNCTION:
1868 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1869 if (event_name[0] == '\0') {
1870 ret = event_kernel_disable_event(kchan,
1871 NULL, event->type);
1872 } else {
1873 ret = event_kernel_disable_event(kchan,
1874 event_name, event->type);
1875 }
1876 if (ret != LTTNG_OK) {
1877 goto error_unlock;
1878 }
1879 break;
1880 default:
1881 ret = LTTNG_ERR_UNK;
1882 goto error_unlock;
1883 }
1884
1885 kernel_wait_quiescent();
1886 break;
1887 }
1888 case LTTNG_DOMAIN_UST:
1889 {
1890 struct ltt_ust_channel *uchan;
1891 struct ltt_ust_session *usess;
1892
1893 usess = session->ust_session;
1894
1895 if (validate_ust_event_name(event_name)) {
1896 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1897 goto error_unlock;
1898 }
1899
1900 /*
1901 * If a non-default channel has been created in the
1902 * session, explicitly require that -c chan_name needs
1903 * to be provided.
1904 */
1905 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1906 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1907 goto error_unlock;
1908 }
1909
1910 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1911 channel_name);
1912 if (uchan == NULL) {
1913 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1914 goto error_unlock;
1915 }
1916
1917 switch (event->type) {
1918 case LTTNG_EVENT_ALL:
1919 /*
1920 * An empty event name means that everything
1921 * should be disabled.
1922 */
1923 if (event->name[0] == '\0') {
1924 ret = event_ust_disable_all_tracepoints(usess, uchan);
1925 } else {
1926 ret = event_ust_disable_tracepoint(usess, uchan,
1927 event_name);
1928 }
1929 if (ret != LTTNG_OK) {
1930 goto error_unlock;
1931 }
1932 break;
1933 default:
1934 ret = LTTNG_ERR_UNK;
1935 goto error_unlock;
1936 }
1937
1938 DBG3("Disable UST event %s in channel %s completed", event_name,
1939 channel_name);
1940 break;
1941 }
1942 case LTTNG_DOMAIN_LOG4J:
1943 case LTTNG_DOMAIN_JUL:
1944 case LTTNG_DOMAIN_PYTHON:
1945 {
1946 struct agent *agt;
1947 struct ltt_ust_session *usess = session->ust_session;
1948
1949 assert(usess);
1950
1951 switch (event->type) {
1952 case LTTNG_EVENT_ALL:
1953 break;
1954 default:
1955 ret = LTTNG_ERR_UNK;
1956 goto error_unlock;
1957 }
1958
1959 agt = trace_ust_find_agent(usess, domain);
1960 if (!agt) {
1961 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1962 goto error_unlock;
1963 }
1964 /*
1965 * An empty event name means that everything
1966 * should be disabled.
1967 */
1968 if (event->name[0] == '\0') {
1969 ret = event_agent_disable_all(usess, agt);
1970 } else {
1971 ret = event_agent_disable(usess, agt, event_name);
1972 }
1973 if (ret != LTTNG_OK) {
1974 goto error_unlock;
1975 }
1976
1977 break;
1978 }
1979 default:
1980 ret = LTTNG_ERR_UND;
1981 goto error_unlock;
1982 }
1983
1984 ret = LTTNG_OK;
1985
1986 error_unlock:
1987 rcu_read_unlock();
1988 error:
1989 return ret;
1990 }
1991
1992 /*
1993 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1994 */
1995 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1996 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
1997 {
1998 int ret, chan_kern_created = 0, chan_ust_created = 0;
1999 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
2000
2001 /*
2002 * Don't try to add a context if the session has been started at
2003 * some point in time before. The tracer does not allow it and would
2004 * result in a corrupted trace.
2005 */
2006 if (session->has_been_started) {
2007 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2008 goto end;
2009 }
2010
2011 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2012 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
2013 app_ctx_name = ctx->u.app_ctx.ctx_name;
2014 }
2015
2016 switch (domain) {
2017 case LTTNG_DOMAIN_KERNEL:
2018 assert(session->kernel_session);
2019
2020 if (session->kernel_session->channel_count == 0) {
2021 /* Create default channel */
2022 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
2023 if (ret != LTTNG_OK) {
2024 goto error;
2025 }
2026 chan_kern_created = 1;
2027 }
2028 /* Add kernel context to kernel tracer */
2029 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
2030 if (ret != LTTNG_OK) {
2031 goto error;
2032 }
2033 break;
2034 case LTTNG_DOMAIN_JUL:
2035 case LTTNG_DOMAIN_LOG4J:
2036 {
2037 /*
2038 * Validate channel name.
2039 * If no channel name is given and the domain is JUL or LOG4J,
2040 * set it to the appropriate domain-specific channel name. If
2041 * a name is provided but does not match the expexted channel
2042 * name, return an error.
2043 */
2044 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
2045 strcmp(channel_name,
2046 DEFAULT_JUL_CHANNEL_NAME)) {
2047 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2048 goto error;
2049 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
2050 strcmp(channel_name,
2051 DEFAULT_LOG4J_CHANNEL_NAME)) {
2052 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2053 goto error;
2054 }
2055 /* break is _not_ missing here. */
2056 }
2057 case LTTNG_DOMAIN_UST:
2058 {
2059 struct ltt_ust_session *usess = session->ust_session;
2060 unsigned int chan_count;
2061
2062 assert(usess);
2063
2064 chan_count = lttng_ht_get_count(usess->domain_global.channels);
2065 if (chan_count == 0) {
2066 struct lttng_channel *attr;
2067 /* Create default channel */
2068 attr = channel_new_default_attr(domain, usess->buffer_type);
2069 if (attr == NULL) {
2070 ret = LTTNG_ERR_FATAL;
2071 goto error;
2072 }
2073
2074 ret = channel_ust_create(usess, attr, usess->buffer_type);
2075 if (ret != LTTNG_OK) {
2076 free(attr);
2077 goto error;
2078 }
2079 channel_attr_destroy(attr);
2080 chan_ust_created = 1;
2081 }
2082
2083 ret = context_ust_add(usess, domain, ctx, channel_name);
2084 free(app_ctx_provider_name);
2085 free(app_ctx_name);
2086 app_ctx_name = NULL;
2087 app_ctx_provider_name = NULL;
2088 if (ret != LTTNG_OK) {
2089 goto error;
2090 }
2091 break;
2092 }
2093 default:
2094 ret = LTTNG_ERR_UND;
2095 goto error;
2096 }
2097
2098 ret = LTTNG_OK;
2099 goto end;
2100
2101 error:
2102 if (chan_kern_created) {
2103 struct ltt_kernel_channel *kchan =
2104 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2105 session->kernel_session);
2106 /* Created previously, this should NOT fail. */
2107 assert(kchan);
2108 kernel_destroy_channel(kchan);
2109 }
2110
2111 if (chan_ust_created) {
2112 struct ltt_ust_channel *uchan =
2113 trace_ust_find_channel_by_name(
2114 session->ust_session->domain_global.channels,
2115 DEFAULT_CHANNEL_NAME);
2116 /* Created previously, this should NOT fail. */
2117 assert(uchan);
2118 /* Remove from the channel list of the session. */
2119 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2120 uchan);
2121 trace_ust_destroy_channel(uchan);
2122 }
2123 end:
2124 free(app_ctx_provider_name);
2125 free(app_ctx_name);
2126 return ret;
2127 }
2128
2129 static inline bool name_starts_with(const char *name, const char *prefix)
2130 {
2131 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2132
2133 return !strncmp(name, prefix, max_cmp_len);
2134 }
2135
2136 /* Perform userspace-specific event name validation */
2137 static int validate_ust_event_name(const char *name)
2138 {
2139 int ret = 0;
2140
2141 if (!name) {
2142 ret = -1;
2143 goto end;
2144 }
2145
2146 /*
2147 * Check name against all internal UST event component namespaces used
2148 * by the agents.
2149 */
2150 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2151 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2152 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2153 ret = -1;
2154 }
2155
2156 end:
2157 return ret;
2158 }
2159
2160 /*
2161 * Internal version of cmd_enable_event() with a supplemental
2162 * "internal_event" flag which is used to enable internal events which should
2163 * be hidden from clients. Such events are used in the agent implementation to
2164 * enable the events through which all "agent" events are funeled.
2165 */
2166 static int _cmd_enable_event(struct ltt_session *session,
2167 const struct lttng_domain *domain,
2168 char *channel_name, struct lttng_event *event,
2169 char *filter_expression,
2170 struct lttng_filter_bytecode *filter,
2171 struct lttng_event_exclusion *exclusion,
2172 int wpipe, bool internal_event)
2173 {
2174 int ret = 0, channel_created = 0;
2175 struct lttng_channel *attr = NULL;
2176
2177 assert(session);
2178 assert(event);
2179 assert(channel_name);
2180
2181 /* If we have a filter, we must have its filter expression */
2182 assert(!(!!filter_expression ^ !!filter));
2183
2184 /* Normalize event name as a globbing pattern */
2185 strutils_normalize_star_glob_pattern(event->name);
2186
2187 /* Normalize exclusion names as globbing patterns */
2188 if (exclusion) {
2189 size_t i;
2190
2191 for (i = 0; i < exclusion->count; i++) {
2192 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2193
2194 strutils_normalize_star_glob_pattern(name);
2195 }
2196 }
2197
2198 DBG("Enable event command for event \'%s\'", event->name);
2199
2200 rcu_read_lock();
2201
2202 switch (domain->type) {
2203 case LTTNG_DOMAIN_KERNEL:
2204 {
2205 struct ltt_kernel_channel *kchan;
2206
2207 /*
2208 * If a non-default channel has been created in the
2209 * session, explicitely require that -c chan_name needs
2210 * to be provided.
2211 */
2212 if (session->kernel_session->has_non_default_channel
2213 && channel_name[0] == '\0') {
2214 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2215 goto error;
2216 }
2217
2218 kchan = trace_kernel_get_channel_by_name(channel_name,
2219 session->kernel_session);
2220 if (kchan == NULL) {
2221 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2222 LTTNG_BUFFER_GLOBAL);
2223 if (attr == NULL) {
2224 ret = LTTNG_ERR_FATAL;
2225 goto error;
2226 }
2227 if (lttng_strncpy(attr->name, channel_name,
2228 sizeof(attr->name))) {
2229 ret = LTTNG_ERR_INVALID;
2230 goto error;
2231 }
2232
2233 ret = cmd_enable_channel(session, domain, attr, wpipe);
2234 if (ret != LTTNG_OK) {
2235 goto error;
2236 }
2237 channel_created = 1;
2238 }
2239
2240 /* Get the newly created kernel channel pointer */
2241 kchan = trace_kernel_get_channel_by_name(channel_name,
2242 session->kernel_session);
2243 if (kchan == NULL) {
2244 /* This sould not happen... */
2245 ret = LTTNG_ERR_FATAL;
2246 goto error;
2247 }
2248
2249 switch (event->type) {
2250 case LTTNG_EVENT_ALL:
2251 {
2252 char *filter_expression_a = NULL;
2253 struct lttng_filter_bytecode *filter_a = NULL;
2254
2255 /*
2256 * We need to duplicate filter_expression and filter,
2257 * because ownership is passed to first enable
2258 * event.
2259 */
2260 if (filter_expression) {
2261 filter_expression_a = strdup(filter_expression);
2262 if (!filter_expression_a) {
2263 ret = LTTNG_ERR_FATAL;
2264 goto error;
2265 }
2266 }
2267 if (filter) {
2268 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2269 if (!filter_a) {
2270 free(filter_expression_a);
2271 ret = LTTNG_ERR_FATAL;
2272 goto error;
2273 }
2274 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2275 }
2276 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2277 ret = event_kernel_enable_event(kchan, event,
2278 filter_expression, filter);
2279 /* We have passed ownership */
2280 filter_expression = NULL;
2281 filter = NULL;
2282 if (ret != LTTNG_OK) {
2283 if (channel_created) {
2284 /* Let's not leak a useless channel. */
2285 kernel_destroy_channel(kchan);
2286 }
2287 free(filter_expression_a);
2288 free(filter_a);
2289 goto error;
2290 }
2291 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2292 ret = event_kernel_enable_event(kchan, event,
2293 filter_expression_a, filter_a);
2294 /* We have passed ownership */
2295 filter_expression_a = NULL;
2296 filter_a = NULL;
2297 if (ret != LTTNG_OK) {
2298 goto error;
2299 }
2300 break;
2301 }
2302 case LTTNG_EVENT_PROBE:
2303 case LTTNG_EVENT_USERSPACE_PROBE:
2304 case LTTNG_EVENT_FUNCTION:
2305 case LTTNG_EVENT_FUNCTION_ENTRY:
2306 case LTTNG_EVENT_TRACEPOINT:
2307 ret = event_kernel_enable_event(kchan, event,
2308 filter_expression, filter);
2309 /* We have passed ownership */
2310 filter_expression = NULL;
2311 filter = NULL;
2312 if (ret != LTTNG_OK) {
2313 if (channel_created) {
2314 /* Let's not leak a useless channel. */
2315 kernel_destroy_channel(kchan);
2316 }
2317 goto error;
2318 }
2319 break;
2320 case LTTNG_EVENT_SYSCALL:
2321 ret = event_kernel_enable_event(kchan, event,
2322 filter_expression, filter);
2323 /* We have passed ownership */
2324 filter_expression = NULL;
2325 filter = NULL;
2326 if (ret != LTTNG_OK) {
2327 goto error;
2328 }
2329 break;
2330 default:
2331 ret = LTTNG_ERR_UNK;
2332 goto error;
2333 }
2334
2335 kernel_wait_quiescent();
2336 break;
2337 }
2338 case LTTNG_DOMAIN_UST:
2339 {
2340 struct ltt_ust_channel *uchan;
2341 struct ltt_ust_session *usess = session->ust_session;
2342
2343 assert(usess);
2344
2345 /*
2346 * If a non-default channel has been created in the
2347 * session, explicitely require that -c chan_name needs
2348 * to be provided.
2349 */
2350 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2351 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2352 goto error;
2353 }
2354
2355 /* Get channel from global UST domain */
2356 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2357 channel_name);
2358 if (uchan == NULL) {
2359 /* Create default channel */
2360 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2361 usess->buffer_type);
2362 if (attr == NULL) {
2363 ret = LTTNG_ERR_FATAL;
2364 goto error;
2365 }
2366 if (lttng_strncpy(attr->name, channel_name,
2367 sizeof(attr->name))) {
2368 ret = LTTNG_ERR_INVALID;
2369 goto error;
2370 }
2371
2372 ret = cmd_enable_channel(session, domain, attr, wpipe);
2373 if (ret != LTTNG_OK) {
2374 goto error;
2375 }
2376
2377 /* Get the newly created channel reference back */
2378 uchan = trace_ust_find_channel_by_name(
2379 usess->domain_global.channels, channel_name);
2380 assert(uchan);
2381 }
2382
2383 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2384 /*
2385 * Don't allow users to add UST events to channels which
2386 * are assigned to a userspace subdomain (JUL, Log4J,
2387 * Python, etc.).
2388 */
2389 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2390 goto error;
2391 }
2392
2393 if (!internal_event) {
2394 /*
2395 * Ensure the event name is not reserved for internal
2396 * use.
2397 */
2398 ret = validate_ust_event_name(event->name);
2399 if (ret) {
2400 WARN("Userspace event name %s failed validation.",
2401 event->name);
2402 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2403 goto error;
2404 }
2405 }
2406
2407 /* At this point, the session and channel exist on the tracer */
2408 ret = event_ust_enable_tracepoint(usess, uchan, event,
2409 filter_expression, filter, exclusion,
2410 internal_event);
2411 /* We have passed ownership */
2412 filter_expression = NULL;
2413 filter = NULL;
2414 exclusion = NULL;
2415 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2416 goto already_enabled;
2417 } else if (ret != LTTNG_OK) {
2418 goto error;
2419 }
2420 break;
2421 }
2422 case LTTNG_DOMAIN_LOG4J:
2423 case LTTNG_DOMAIN_JUL:
2424 case LTTNG_DOMAIN_PYTHON:
2425 {
2426 const char *default_event_name, *default_chan_name;
2427 struct agent *agt;
2428 struct lttng_event uevent;
2429 struct lttng_domain tmp_dom;
2430 struct ltt_ust_session *usess = session->ust_session;
2431
2432 assert(usess);
2433
2434 if (!agent_tracing_is_enabled()) {
2435 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2436 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2437 goto error;
2438 }
2439
2440 agt = trace_ust_find_agent(usess, domain->type);
2441 if (!agt) {
2442 agt = agent_create(domain->type);
2443 if (!agt) {
2444 ret = LTTNG_ERR_NOMEM;
2445 goto error;
2446 }
2447 agent_add(agt, usess->agents);
2448 }
2449
2450 /* Create the default tracepoint. */
2451 memset(&uevent, 0, sizeof(uevent));
2452 uevent.type = LTTNG_EVENT_TRACEPOINT;
2453 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2454 default_event_name = event_get_default_agent_ust_name(
2455 domain->type);
2456 if (!default_event_name) {
2457 ret = LTTNG_ERR_FATAL;
2458 goto error;
2459 }
2460 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2461 uevent.name[sizeof(uevent.name) - 1] = '\0';
2462
2463 /*
2464 * The domain type is changed because we are about to enable the
2465 * default channel and event for the JUL domain that are hardcoded.
2466 * This happens in the UST domain.
2467 */
2468 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2469 tmp_dom.type = LTTNG_DOMAIN_UST;
2470
2471 switch (domain->type) {
2472 case LTTNG_DOMAIN_LOG4J:
2473 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2474 break;
2475 case LTTNG_DOMAIN_JUL:
2476 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2477 break;
2478 case LTTNG_DOMAIN_PYTHON:
2479 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2480 break;
2481 default:
2482 /* The switch/case we are in makes this impossible */
2483 assert(0);
2484 }
2485
2486 {
2487 char *filter_expression_copy = NULL;
2488 struct lttng_filter_bytecode *filter_copy = NULL;
2489
2490 if (filter) {
2491 const size_t filter_size = sizeof(
2492 struct lttng_filter_bytecode)
2493 + filter->len;
2494
2495 filter_copy = zmalloc(filter_size);
2496 if (!filter_copy) {
2497 ret = LTTNG_ERR_NOMEM;
2498 goto error;
2499 }
2500 memcpy(filter_copy, filter, filter_size);
2501
2502 filter_expression_copy =
2503 strdup(filter_expression);
2504 if (!filter_expression) {
2505 ret = LTTNG_ERR_NOMEM;
2506 }
2507
2508 if (!filter_expression_copy || !filter_copy) {
2509 free(filter_expression_copy);
2510 free(filter_copy);
2511 goto error;
2512 }
2513 }
2514
2515 ret = cmd_enable_event_internal(session, &tmp_dom,
2516 (char *) default_chan_name,
2517 &uevent, filter_expression_copy,
2518 filter_copy, NULL, wpipe);
2519 }
2520
2521 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2522 goto already_enabled;
2523 } else if (ret != LTTNG_OK) {
2524 goto error;
2525 }
2526
2527 /* The wild card * means that everything should be enabled. */
2528 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2529 ret = event_agent_enable_all(usess, agt, event, filter,
2530 filter_expression);
2531 } else {
2532 ret = event_agent_enable(usess, agt, event, filter,
2533 filter_expression);
2534 }
2535 filter = NULL;
2536 filter_expression = NULL;
2537 if (ret != LTTNG_OK) {
2538 goto error;
2539 }
2540
2541 break;
2542 }
2543 default:
2544 ret = LTTNG_ERR_UND;
2545 goto error;
2546 }
2547
2548 ret = LTTNG_OK;
2549
2550 already_enabled:
2551 error:
2552 free(filter_expression);
2553 free(filter);
2554 free(exclusion);
2555 channel_attr_destroy(attr);
2556 rcu_read_unlock();
2557 return ret;
2558 }
2559
2560 /*
2561 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2562 * We own filter, exclusion, and filter_expression.
2563 */
2564 int cmd_enable_event(struct ltt_session *session,
2565 const struct lttng_domain *domain,
2566 char *channel_name, struct lttng_event *event,
2567 char *filter_expression,
2568 struct lttng_filter_bytecode *filter,
2569 struct lttng_event_exclusion *exclusion,
2570 int wpipe)
2571 {
2572 return _cmd_enable_event(session, domain, channel_name, event,
2573 filter_expression, filter, exclusion, wpipe, false);
2574 }
2575
2576 /*
2577 * Enable an event which is internal to LTTng. An internal should
2578 * never be made visible to clients and are immune to checks such as
2579 * reserved names.
2580 */
2581 static int cmd_enable_event_internal(struct ltt_session *session,
2582 const struct lttng_domain *domain,
2583 char *channel_name, struct lttng_event *event,
2584 char *filter_expression,
2585 struct lttng_filter_bytecode *filter,
2586 struct lttng_event_exclusion *exclusion,
2587 int wpipe)
2588 {
2589 return _cmd_enable_event(session, domain, channel_name, event,
2590 filter_expression, filter, exclusion, wpipe, true);
2591 }
2592
2593 /*
2594 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2595 */
2596 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2597 struct lttng_event **events)
2598 {
2599 int ret;
2600 ssize_t nb_events = 0;
2601
2602 switch (domain) {
2603 case LTTNG_DOMAIN_KERNEL:
2604 nb_events = kernel_list_events(events);
2605 if (nb_events < 0) {
2606 ret = LTTNG_ERR_KERN_LIST_FAIL;
2607 goto error;
2608 }
2609 break;
2610 case LTTNG_DOMAIN_UST:
2611 nb_events = ust_app_list_events(events);
2612 if (nb_events < 0) {
2613 ret = LTTNG_ERR_UST_LIST_FAIL;
2614 goto error;
2615 }
2616 break;
2617 case LTTNG_DOMAIN_LOG4J:
2618 case LTTNG_DOMAIN_JUL:
2619 case LTTNG_DOMAIN_PYTHON:
2620 nb_events = agent_list_events(events, domain);
2621 if (nb_events < 0) {
2622 ret = LTTNG_ERR_UST_LIST_FAIL;
2623 goto error;
2624 }
2625 break;
2626 default:
2627 ret = LTTNG_ERR_UND;
2628 goto error;
2629 }
2630
2631 return nb_events;
2632
2633 error:
2634 /* Return negative value to differentiate return code */
2635 return -ret;
2636 }
2637
2638 /*
2639 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2640 */
2641 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2642 struct lttng_event_field **fields)
2643 {
2644 int ret;
2645 ssize_t nb_fields = 0;
2646
2647 switch (domain) {
2648 case LTTNG_DOMAIN_UST:
2649 nb_fields = ust_app_list_event_fields(fields);
2650 if (nb_fields < 0) {
2651 ret = LTTNG_ERR_UST_LIST_FAIL;
2652 goto error;
2653 }
2654 break;
2655 case LTTNG_DOMAIN_KERNEL:
2656 default: /* fall-through */
2657 ret = LTTNG_ERR_UND;
2658 goto error;
2659 }
2660
2661 return nb_fields;
2662
2663 error:
2664 /* Return negative value to differentiate return code */
2665 return -ret;
2666 }
2667
2668 ssize_t cmd_list_syscalls(struct lttng_event **events)
2669 {
2670 return syscall_table_list(events);
2671 }
2672
2673 /*
2674 * Command LTTNG_START_TRACE processed by the client thread.
2675 *
2676 * Called with session mutex held.
2677 */
2678 int cmd_start_trace(struct ltt_session *session)
2679 {
2680 enum lttng_error_code ret;
2681 unsigned long nb_chan = 0;
2682 struct ltt_kernel_session *ksession;
2683 struct ltt_ust_session *usess;
2684 const bool session_rotated_after_last_stop =
2685 session->rotated_after_last_stop;
2686 const bool session_cleared_after_last_stop =
2687 session->cleared_after_last_stop;
2688
2689 assert(session);
2690
2691 /* Ease our life a bit ;) */
2692 ksession = session->kernel_session;
2693 usess = session->ust_session;
2694
2695 /* Is the session already started? */
2696 if (session->active) {
2697 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2698 /* Perform nothing */
2699 goto end;
2700 }
2701
2702 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2703 !session->current_trace_chunk) {
2704 /*
2705 * A rotation was launched while the session was stopped and
2706 * it has not been completed yet. It is not possible to start
2707 * the session since starting the session here would require a
2708 * rotation from "NULL" to a new trace chunk. That rotation
2709 * would overlap with the ongoing rotation, which is not
2710 * supported.
2711 */
2712 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2713 session->name);
2714 ret = LTTNG_ERR_ROTATION_PENDING;
2715 goto error;
2716 }
2717
2718 /*
2719 * Starting a session without channel is useless since after that it's not
2720 * possible to enable channel thus inform the client.
2721 */
2722 if (usess && usess->domain_global.channels) {
2723 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2724 }
2725 if (ksession) {
2726 nb_chan += ksession->channel_count;
2727 }
2728 if (!nb_chan) {
2729 ret = LTTNG_ERR_NO_CHANNEL;
2730 goto error;
2731 }
2732
2733 session->active = 1;
2734 session->rotated_after_last_stop = false;
2735 session->cleared_after_last_stop = false;
2736 if (session->output_traces && !session->current_trace_chunk) {
2737 if (!session->has_been_started) {
2738 struct lttng_trace_chunk *trace_chunk;
2739
2740 DBG("Creating initial trace chunk of session \"%s\"",
2741 session->name);
2742 trace_chunk = session_create_new_trace_chunk(
2743 session, NULL, NULL, NULL);
2744 if (!trace_chunk) {
2745 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2746 goto error;
2747 }
2748 assert(!session->current_trace_chunk);
2749 ret = session_set_trace_chunk(session, trace_chunk,
2750 NULL);
2751 lttng_trace_chunk_put(trace_chunk);
2752 if (ret) {
2753 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2754 goto error;
2755 }
2756 } else {
2757 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2758 session->name);
2759 /*
2760 * Rotate existing streams into the new chunk.
2761 * This is a "quiet" rotation has no client has
2762 * explicitly requested this operation.
2763 *
2764 * There is also no need to wait for the rotation
2765 * to complete as it will happen immediately. No data
2766 * was produced as the session was stopped, so the
2767 * rotation should happen on reception of the command.
2768 */
2769 ret = cmd_rotate_session(session, NULL, true,
2770 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2771 if (ret != LTTNG_OK) {
2772 goto error;
2773 }
2774 }
2775 }
2776
2777 /* Kernel tracing */
2778 if (ksession != NULL) {
2779 DBG("Start kernel tracing session %s", session->name);
2780 ret = start_kernel_session(ksession);
2781 if (ret != LTTNG_OK) {
2782 goto error;
2783 }
2784 }
2785
2786 /* Flag session that trace should start automatically */
2787 if (usess) {
2788 int int_ret = ust_app_start_trace_all(usess);
2789
2790 if (int_ret < 0) {
2791 ret = LTTNG_ERR_UST_START_FAIL;
2792 goto error;
2793 }
2794 }
2795
2796 /*
2797 * Open a packet in every stream of the session to ensure that viewers
2798 * can correctly identify the boundaries of the periods during which
2799 * tracing was active for this session.
2800 */
2801 ret = session_open_packets(session);
2802 if (ret != LTTNG_OK) {
2803 goto error;
2804 }
2805
2806 /*
2807 * Clear the flag that indicates that a rotation was done while the
2808 * session was stopped.
2809 */
2810 session->rotated_after_last_stop = false;
2811
2812 if (session->rotate_timer_period) {
2813 int int_ret = timer_session_rotation_schedule_timer_start(
2814 session, session->rotate_timer_period);
2815
2816 if (int_ret < 0) {
2817 ERR("Failed to enable rotate timer");
2818 ret = LTTNG_ERR_UNK;
2819 goto error;
2820 }
2821 }
2822
2823 ret = LTTNG_OK;
2824
2825 error:
2826 if (ret == LTTNG_OK) {
2827 /* Flag this after a successful start. */
2828 session->has_been_started |= 1;
2829 } else {
2830 session->active = 0;
2831 /* Restore initial state on error. */
2832 session->rotated_after_last_stop =
2833 session_rotated_after_last_stop;
2834 session->cleared_after_last_stop =
2835 session_cleared_after_last_stop;
2836 }
2837 end:
2838 return ret;
2839 }
2840
2841 /*
2842 * Command LTTNG_STOP_TRACE processed by the client thread.
2843 */
2844 int cmd_stop_trace(struct ltt_session *session)
2845 {
2846 int ret;
2847 struct ltt_kernel_session *ksession;
2848 struct ltt_ust_session *usess;
2849
2850 assert(session);
2851
2852 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2853 /* Short cut */
2854 ksession = session->kernel_session;
2855 usess = session->ust_session;
2856
2857 /* Session is not active. Skip everythong and inform the client. */
2858 if (!session->active) {
2859 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2860 goto error;
2861 }
2862
2863 ret = stop_kernel_session(ksession);
2864 if (ret != LTTNG_OK) {
2865 goto error;
2866 }
2867
2868 if (usess && usess->active) {
2869 ret = ust_app_stop_trace_all(usess);
2870 if (ret < 0) {
2871 ret = LTTNG_ERR_UST_STOP_FAIL;
2872 goto error;
2873 }
2874 }
2875
2876 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2877 session->id);
2878 /* Flag inactive after a successful stop. */
2879 session->active = 0;
2880 ret = LTTNG_OK;
2881
2882 error:
2883 return ret;
2884 }
2885
2886 /*
2887 * Set the base_path of the session only if subdir of a control uris is set.
2888 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2889 */
2890 static int set_session_base_path_from_uris(struct ltt_session *session,
2891 size_t nb_uri,
2892 struct lttng_uri *uris)
2893 {
2894 int ret;
2895 size_t i;
2896
2897 for (i = 0; i < nb_uri; i++) {
2898 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2899 uris[i].subdir[0] == '\0') {
2900 /* Not interested in these URIs */
2901 continue;
2902 }
2903
2904 if (session->base_path != NULL) {
2905 free(session->base_path);
2906 session->base_path = NULL;
2907 }
2908
2909 /* Set session base_path */
2910 session->base_path = strdup(uris[i].subdir);
2911 if (!session->base_path) {
2912 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2913 uris[i].subdir, session->name);
2914 ret = LTTNG_ERR_NOMEM;
2915 goto error;
2916 }
2917 DBG2("Setting base path \"%s\" for session \"%s\"",
2918 session->base_path, session->name);
2919 }
2920 ret = LTTNG_OK;
2921 error:
2922 return ret;
2923 }
2924
2925 /*
2926 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2927 */
2928 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2929 struct lttng_uri *uris)
2930 {
2931 int ret, i;
2932 struct ltt_kernel_session *ksess = session->kernel_session;
2933 struct ltt_ust_session *usess = session->ust_session;
2934
2935 assert(session);
2936 assert(uris);
2937 assert(nb_uri > 0);
2938
2939 /* Can't set consumer URI if the session is active. */
2940 if (session->active) {
2941 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2942 goto error;
2943 }
2944
2945 /*
2946 * Set the session base path if any. This is done inside
2947 * cmd_set_consumer_uri to preserve backward compatibility of the
2948 * previous session creation api vs the session descriptor api.
2949 */
2950 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2951 if (ret != LTTNG_OK) {
2952 goto error;
2953 }
2954
2955 /* Set the "global" consumer URIs */
2956 for (i = 0; i < nb_uri; i++) {
2957 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2958 LTTNG_DOMAIN_NONE);
2959 if (ret != LTTNG_OK) {
2960 goto error;
2961 }
2962 }
2963
2964 /* Set UST session URIs */
2965 if (session->ust_session) {
2966 for (i = 0; i < nb_uri; i++) {
2967 ret = add_uri_to_consumer(session,
2968 session->ust_session->consumer,
2969 &uris[i], LTTNG_DOMAIN_UST);
2970 if (ret != LTTNG_OK) {
2971 goto error;
2972 }
2973 }
2974 }
2975
2976 /* Set kernel session URIs */
2977 if (session->kernel_session) {
2978 for (i = 0; i < nb_uri; i++) {
2979 ret = add_uri_to_consumer(session,
2980 session->kernel_session->consumer,
2981 &uris[i], LTTNG_DOMAIN_KERNEL);
2982 if (ret != LTTNG_OK) {
2983 goto error;
2984 }
2985 }
2986 }
2987
2988 /*
2989 * Make sure to set the session in output mode after we set URI since a
2990 * session can be created without URL (thus flagged in no output mode).
2991 */
2992 session->output_traces = 1;
2993 if (ksess) {
2994 ksess->output_traces = 1;
2995 }
2996
2997 if (usess) {
2998 usess->output_traces = 1;
2999 }
3000
3001 /* All good! */
3002 ret = LTTNG_OK;
3003
3004 error:
3005 return ret;
3006 }
3007
3008 static
3009 enum lttng_error_code set_session_output_from_descriptor(
3010 struct ltt_session *session,
3011 const struct lttng_session_descriptor *descriptor)
3012 {
3013 int ret;
3014 enum lttng_error_code ret_code = LTTNG_OK;
3015 enum lttng_session_descriptor_type session_type =
3016 lttng_session_descriptor_get_type(descriptor);
3017 enum lttng_session_descriptor_output_type output_type =
3018 lttng_session_descriptor_get_output_type(descriptor);
3019 struct lttng_uri uris[2] = {};
3020 size_t uri_count = 0;
3021
3022 switch (output_type) {
3023 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3024 goto end;
3025 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3026 lttng_session_descriptor_get_local_output_uri(descriptor,
3027 &uris[0]);
3028 uri_count = 1;
3029 break;
3030 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3031 lttng_session_descriptor_get_network_output_uris(descriptor,
3032 &uris[0], &uris[1]);
3033 uri_count = 2;
3034 break;
3035 default:
3036 ret_code = LTTNG_ERR_INVALID;
3037 goto end;
3038 }
3039
3040 switch (session_type) {
3041 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3042 {
3043 struct snapshot_output *new_output = NULL;
3044
3045 new_output = snapshot_output_alloc();
3046 if (!new_output) {
3047 ret_code = LTTNG_ERR_NOMEM;
3048 goto end;
3049 }
3050
3051 ret = snapshot_output_init_with_uri(session,
3052 DEFAULT_SNAPSHOT_MAX_SIZE,
3053 NULL, uris, uri_count, session->consumer,
3054 new_output, &session->snapshot);
3055 if (ret < 0) {
3056 ret_code = (ret == -ENOMEM) ?
3057 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3058 snapshot_output_destroy(new_output);
3059 goto end;
3060 }
3061 snapshot_add_output(&session->snapshot, new_output);
3062 break;
3063 }
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3065 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3066 {
3067 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
3068 break;
3069 }
3070 default:
3071 ret_code = LTTNG_ERR_INVALID;
3072 goto end;
3073 }
3074 end:
3075 return ret_code;
3076 }
3077
3078 static
3079 enum lttng_error_code cmd_create_session_from_descriptor(
3080 struct lttng_session_descriptor *descriptor,
3081 const lttng_sock_cred *creds,
3082 const char *home_path)
3083 {
3084 int ret;
3085 enum lttng_error_code ret_code;
3086 const char *session_name;
3087 struct ltt_session *new_session = NULL;
3088 enum lttng_session_descriptor_status descriptor_status;
3089
3090 session_lock_list();
3091 if (home_path) {
3092 if (*home_path != '/') {
3093 ERR("Home path provided by client is not absolute");
3094 ret_code = LTTNG_ERR_INVALID;
3095 goto end;
3096 }
3097 }
3098
3099 descriptor_status = lttng_session_descriptor_get_session_name(
3100 descriptor, &session_name);
3101 switch (descriptor_status) {
3102 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3103 break;
3104 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3105 session_name = NULL;
3106 break;
3107 default:
3108 ret_code = LTTNG_ERR_INVALID;
3109 goto end;
3110 }
3111
3112 ret_code = session_create(session_name, creds->uid, creds->gid,
3113 &new_session);
3114 if (ret_code != LTTNG_OK) {
3115 goto end;
3116 }
3117
3118 if (!session_name) {
3119 ret = lttng_session_descriptor_set_session_name(descriptor,
3120 new_session->name);
3121 if (ret) {
3122 ret_code = LTTNG_ERR_SESSION_FAIL;
3123 goto end;
3124 }
3125 }
3126
3127 if (!lttng_session_descriptor_is_output_destination_initialized(
3128 descriptor)) {
3129 /*
3130 * Only include the session's creation time in the output
3131 * destination if the name of the session itself was
3132 * not auto-generated.
3133 */
3134 ret_code = lttng_session_descriptor_set_default_output(
3135 descriptor,
3136 session_name ? &new_session->creation_time : NULL,
3137 home_path);
3138 if (ret_code != LTTNG_OK) {
3139 goto end;
3140 }
3141 } else {
3142 new_session->has_user_specified_directory =
3143 lttng_session_descriptor_has_output_directory(
3144 descriptor);
3145 }
3146
3147 switch (lttng_session_descriptor_get_type(descriptor)) {
3148 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3149 new_session->snapshot_mode = 1;
3150 break;
3151 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3152 new_session->live_timer =
3153 lttng_session_descriptor_live_get_timer_interval(
3154 descriptor);
3155 break;
3156 default:
3157 break;
3158 }
3159
3160 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3161 if (ret_code != LTTNG_OK) {
3162 goto end;
3163 }
3164 new_session->consumer->enabled = 1;
3165 ret_code = LTTNG_OK;
3166 end:
3167 /* Release reference provided by the session_create function. */
3168 session_put(new_session);
3169 if (ret_code != LTTNG_OK && new_session) {
3170 /* Release the global reference on error. */
3171 session_destroy(new_session);
3172 }
3173 session_unlock_list();
3174 return ret_code;
3175 }
3176
3177 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3178 struct lttng_session_descriptor **return_descriptor)
3179 {
3180 int ret;
3181 size_t payload_size;
3182 struct lttng_dynamic_buffer payload;
3183 struct lttng_buffer_view home_dir_view;
3184 struct lttng_buffer_view session_descriptor_view;
3185 struct lttng_session_descriptor *session_descriptor = NULL;
3186 enum lttng_error_code ret_code;
3187
3188 lttng_dynamic_buffer_init(&payload);
3189 if (cmd_ctx->lsm->u.create_session.home_dir_size >=
3190 LTTNG_PATH_MAX) {
3191 ret_code = LTTNG_ERR_INVALID;
3192 goto error;
3193 }
3194 if (cmd_ctx->lsm->u.create_session.session_descriptor_size >
3195 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3196 ret_code = LTTNG_ERR_INVALID;
3197 goto error;
3198 }
3199
3200 payload_size = cmd_ctx->lsm->u.create_session.home_dir_size +
3201 cmd_ctx->lsm->u.create_session.session_descriptor_size;
3202 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3203 if (ret) {
3204 ret_code = LTTNG_ERR_NOMEM;
3205 goto error;
3206 }
3207
3208 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3209 if (ret <= 0) {
3210 ERR("Reception of session descriptor failed, aborting.");
3211 ret_code = LTTNG_ERR_SESSION_FAIL;
3212 goto error;
3213 }
3214
3215 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3216 &payload,
3217 0,
3218 cmd_ctx->lsm->u.create_session.home_dir_size);
3219 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3220 &payload,
3221 cmd_ctx->lsm->u.create_session.home_dir_size,
3222 cmd_ctx->lsm->u.create_session.session_descriptor_size);
3223
3224 ret = lttng_session_descriptor_create_from_buffer(
3225 &session_descriptor_view, &session_descriptor);
3226 if (ret < 0) {
3227 ERR("Failed to create session descriptor from payload of \"create session\" command");
3228 ret_code = LTTNG_ERR_INVALID;
3229 goto error;
3230 }
3231
3232 /*
3233 * Sets the descriptor's auto-generated properties (name, output) if
3234 * needed.
3235 */
3236 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3237 &cmd_ctx->creds,
3238 home_dir_view.size ? home_dir_view.data : NULL);
3239 if (ret_code != LTTNG_OK) {
3240 goto error;
3241 }
3242