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