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