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