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