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