Extract the lost packets and discarded events counters
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
24
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
30 #include <common/compat/string.h>
31
32 #include "channel.h"
33 #include "consumer.h"
34 #include "event.h"
35 #include "health-sessiond.h"
36 #include "kernel.h"
37 #include "kernel-consumer.h"
38 #include "lttng-sessiond.h"
39 #include "utils.h"
40 #include "syscall.h"
41 #include "agent.h"
42
43 #include "cmd.h"
44
45 /*
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
49 * held.
50 */
51 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
52 static uint64_t relayd_net_seq_idx;
53
54 static int validate_event_name(const char *);
55 static int validate_ust_event_name(const char *);
56 static int cmd_enable_event_internal(struct ltt_session *session,
57 struct lttng_domain *domain,
58 char *channel_name, struct lttng_event *event,
59 char *filter_expression,
60 struct lttng_filter_bytecode *filter,
61 struct lttng_event_exclusion *exclusion,
62 int wpipe);
63
64 /*
65 * Create a session path used by list_lttng_sessions for the case that the
66 * session consumer is on the network.
67 */
68 static int build_network_session_path(char *dst, size_t size,
69 struct ltt_session *session)
70 {
71 int ret, kdata_port, udata_port;
72 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
73 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
74
75 assert(session);
76 assert(dst);
77
78 memset(tmp_urls, 0, sizeof(tmp_urls));
79 memset(tmp_uurl, 0, sizeof(tmp_uurl));
80
81 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
82
83 if (session->kernel_session && session->kernel_session->consumer) {
84 kuri = &session->kernel_session->consumer->dst.net.control;
85 kdata_port = session->kernel_session->consumer->dst.net.data.port;
86 }
87
88 if (session->ust_session && session->ust_session->consumer) {
89 uuri = &session->ust_session->consumer->dst.net.control;
90 udata_port = session->ust_session->consumer->dst.net.data.port;
91 }
92
93 if (uuri == NULL && kuri == NULL) {
94 uri = &session->consumer->dst.net.control;
95 kdata_port = session->consumer->dst.net.data.port;
96 } else if (kuri && uuri) {
97 ret = uri_compare(kuri, uuri);
98 if (ret) {
99 /* Not Equal */
100 uri = kuri;
101 /* Build uuri URL string */
102 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
103 if (ret < 0) {
104 goto error;
105 }
106 } else {
107 uri = kuri;
108 }
109 } else if (kuri && uuri == NULL) {
110 uri = kuri;
111 } else if (uuri && kuri == NULL) {
112 uri = uuri;
113 }
114
115 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
116 if (ret < 0) {
117 goto error;
118 }
119
120 /*
121 * Do we have a UST url set. If yes, this means we have both kernel and UST
122 * to print.
123 */
124 if (*tmp_uurl != '\0') {
125 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
126 tmp_urls, kdata_port, tmp_uurl, udata_port);
127 } else {
128 int dport;
129 if (kuri || (!kuri && !uuri)) {
130 dport = kdata_port;
131 } else {
132 /* No kernel URI, use the UST port. */
133 dport = udata_port;
134 }
135 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
136 }
137
138 error:
139 return ret;
140 }
141
142 /*
143 * Get run-time attributes if the session has been started (discarded events,
144 * lost packets).
145 */
146 static int get_kernel_runtime_stats(struct ltt_session *session,
147 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
148 uint64_t *lost_packets)
149 {
150 int ret;
151
152 if (!session->has_been_started) {
153 ret = 0;
154 *discarded_events = 0;
155 *lost_packets = 0;
156 goto end;
157 }
158
159 ret = consumer_get_discarded_events(session->id, kchan->fd,
160 session->kernel_session->consumer,
161 discarded_events);
162 if (ret < 0) {
163 goto end;
164 }
165
166 ret = consumer_get_lost_packets(session->id, kchan->fd,
167 session->kernel_session->consumer,
168 lost_packets);
169 if (ret < 0) {
170 goto end;
171 }
172
173 end:
174 return ret;
175 }
176
177 /*
178 * Get run-time attributes if the session has been started (discarded events,
179 * lost packets).
180 */
181 static int get_ust_runtime_stats(struct ltt_session *session,
182 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
183 uint64_t *lost_packets)
184 {
185 int ret;
186 struct ltt_ust_session *usess;
187
188 usess = session->ust_session;
189
190 if (!usess || !session->has_been_started) {
191 *discarded_events = 0;
192 *lost_packets = 0;
193 ret = 0;
194 goto end;
195 }
196
197 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
198 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
199 &usess->buffer_reg_uid_list,
200 usess->consumer, uchan->id,
201 uchan->attr.overwrite,
202 discarded_events,
203 lost_packets);
204 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
205 ret = ust_app_pid_get_channel_runtime_stats(usess,
206 uchan, usess->consumer,
207 uchan->attr.overwrite,
208 discarded_events,
209 lost_packets);
210 if (ret < 0) {
211 goto end;
212 }
213 *discarded_events += uchan->per_pid_closed_app_discarded;
214 *lost_packets += uchan->per_pid_closed_app_lost;
215 } else {
216 ERR("Unsupported buffer type");
217 ret = -1;
218 goto end;
219 }
220
221 end:
222 return ret;
223 }
224
225 /*
226 * Fill lttng_channel array of all channels.
227 */
228 static void list_lttng_channels(enum lttng_domain_type domain,
229 struct ltt_session *session, struct lttng_channel *channels,
230 struct lttcomm_channel_extended *chan_exts)
231 {
232 int i = 0, ret;
233 struct ltt_kernel_channel *kchan;
234
235 DBG("Listing channels for session %s", session->name);
236
237 switch (domain) {
238 case LTTNG_DOMAIN_KERNEL:
239 /* Kernel channels */
240 if (session->kernel_session != NULL) {
241 cds_list_for_each_entry(kchan,
242 &session->kernel_session->channel_list.head, list) {
243 uint64_t discarded_events, lost_packets;
244
245 ret = get_kernel_runtime_stats(session, kchan,
246 &discarded_events, &lost_packets);
247 if (ret < 0) {
248 goto end;
249 }
250 /* Copy lttng_channel struct to array */
251 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
252 channels[i].enabled = kchan->enabled;
253 chan_exts[i].discarded_events =
254 discarded_events;
255 chan_exts[i].lost_packets = lost_packets;
256 i++;
257 }
258 }
259 break;
260 case LTTNG_DOMAIN_UST:
261 {
262 struct lttng_ht_iter iter;
263 struct ltt_ust_channel *uchan;
264
265 rcu_read_lock();
266 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
267 &iter.iter, uchan, node.node) {
268 uint64_t discarded_events, lost_packets;
269
270 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
271 channels[i].attr.overwrite = uchan->attr.overwrite;
272 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
273 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
274 channels[i].attr.switch_timer_interval =
275 uchan->attr.switch_timer_interval;
276 channels[i].attr.read_timer_interval =
277 uchan->attr.read_timer_interval;
278 channels[i].enabled = uchan->enabled;
279 channels[i].attr.tracefile_size = uchan->tracefile_size;
280 channels[i].attr.tracefile_count = uchan->tracefile_count;
281 switch (uchan->attr.output) {
282 case LTTNG_UST_MMAP:
283 default:
284 channels[i].attr.output = LTTNG_EVENT_MMAP;
285 break;
286 }
287
288 ret = get_ust_runtime_stats(session, uchan,
289 &discarded_events, &lost_packets);
290 if (ret < 0) {
291 break;
292 }
293 chan_exts[i].discarded_events = discarded_events;
294 chan_exts[i].lost_packets = lost_packets;
295 i++;
296 }
297 rcu_read_unlock();
298 break;
299 }
300 default:
301 break;
302 }
303
304 end:
305 return;
306 }
307
308 static void increment_extended_len(const char *filter_expression,
309 struct lttng_event_exclusion *exclusion, size_t *extended_len)
310 {
311 *extended_len += sizeof(struct lttcomm_event_extended_header);
312
313 if (filter_expression) {
314 *extended_len += strlen(filter_expression) + 1;
315 }
316
317 if (exclusion) {
318 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
319 }
320 }
321
322 static void append_extended_info(const char *filter_expression,
323 struct lttng_event_exclusion *exclusion, void **extended_at)
324 {
325 struct lttcomm_event_extended_header extended_header;
326 size_t filter_len = 0;
327 size_t nb_exclusions = 0;
328
329 if (filter_expression) {
330 filter_len = strlen(filter_expression) + 1;
331 }
332
333 if (exclusion) {
334 nb_exclusions = exclusion->count;
335 }
336
337 /* Set header fields */
338 extended_header.filter_len = filter_len;
339 extended_header.nb_exclusions = nb_exclusions;
340
341 /* Copy header */
342 memcpy(*extended_at, &extended_header, sizeof(extended_header));
343 *extended_at += sizeof(extended_header);
344
345 /* Copy filter string */
346 if (filter_expression) {
347 memcpy(*extended_at, filter_expression, filter_len);
348 *extended_at += filter_len;
349 }
350
351 /* Copy exclusion names */
352 if (exclusion) {
353 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
354
355 memcpy(*extended_at, &exclusion->names, len);
356 *extended_at += len;
357 }
358 }
359
360 /*
361 * Create a list of agent domain events.
362 *
363 * Return number of events in list on success or else a negative value.
364 */
365 static int list_lttng_agent_events(struct agent *agt,
366 struct lttng_event **events, size_t *total_size)
367 {
368 int i = 0, ret = 0;
369 unsigned int nb_event = 0;
370 struct agent_event *event;
371 struct lttng_event *tmp_events;
372 struct lttng_ht_iter iter;
373 size_t extended_len = 0;
374 void *extended_at;
375
376 assert(agt);
377 assert(events);
378
379 DBG3("Listing agent events");
380
381 rcu_read_lock();
382 nb_event = lttng_ht_get_count(agt->events);
383 rcu_read_unlock();
384 if (nb_event == 0) {
385 ret = nb_event;
386 *total_size = 0;
387 goto error;
388 }
389
390 /* Compute required extended infos size */
391 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
392
393 /*
394 * This is only valid because the commands which add events are
395 * processed in the same thread as the listing.
396 */
397 rcu_read_lock();
398 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
399 increment_extended_len(event->filter_expression, NULL,
400 &extended_len);
401 }
402 rcu_read_unlock();
403
404 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
405 tmp_events = zmalloc(*total_size);
406 if (!tmp_events) {
407 PERROR("zmalloc agent events session");
408 ret = -LTTNG_ERR_FATAL;
409 goto error;
410 }
411
412 extended_at = ((uint8_t *) tmp_events) +
413 nb_event * sizeof(struct lttng_event);
414
415 rcu_read_lock();
416 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
417 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
418 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
419 tmp_events[i].enabled = event->enabled;
420 tmp_events[i].loglevel = event->loglevel_value;
421 tmp_events[i].loglevel_type = event->loglevel_type;
422 i++;
423
424 /* Append extended info */
425 append_extended_info(event->filter_expression, NULL,
426 &extended_at);
427 }
428 rcu_read_unlock();
429
430 *events = tmp_events;
431 ret = nb_event;
432
433 error:
434 assert(nb_event == i);
435 return ret;
436 }
437
438 /*
439 * Create a list of ust global domain events.
440 */
441 static int list_lttng_ust_global_events(char *channel_name,
442 struct ltt_ust_domain_global *ust_global,
443 struct lttng_event **events, size_t *total_size)
444 {
445 int i = 0, ret = 0;
446 unsigned int nb_event = 0;
447 struct lttng_ht_iter iter;
448 struct lttng_ht_node_str *node;
449 struct ltt_ust_channel *uchan;
450 struct ltt_ust_event *uevent;
451 struct lttng_event *tmp;
452 size_t extended_len = 0;
453 void *extended_at;
454
455 DBG("Listing UST global events for channel %s", channel_name);
456
457 rcu_read_lock();
458
459 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
460 node = lttng_ht_iter_get_node_str(&iter);
461 if (node == NULL) {
462 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
463 goto end;
464 }
465
466 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
467
468 nb_event = lttng_ht_get_count(uchan->events);
469 if (nb_event == 0) {
470 ret = nb_event;
471 *total_size = 0;
472 goto end;
473 }
474
475 DBG3("Listing UST global %d events", nb_event);
476
477 /* Compute required extended infos size */
478 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
479 if (uevent->internal) {
480 nb_event--;
481 continue;
482 }
483
484 increment_extended_len(uevent->filter_expression,
485 uevent->exclusion, &extended_len);
486 }
487 if (nb_event == 0) {
488 /* All events are internal, skip. */
489 ret = 0;
490 *total_size = 0;
491 goto end;
492 }
493
494 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
495 tmp = zmalloc(*total_size);
496 if (tmp == NULL) {
497 ret = -LTTNG_ERR_FATAL;
498 goto end;
499 }
500
501 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
502
503 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
504 if (uevent->internal) {
505 /* This event should remain hidden from clients */
506 continue;
507 }
508 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
509 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
510 tmp[i].enabled = uevent->enabled;
511
512 switch (uevent->attr.instrumentation) {
513 case LTTNG_UST_TRACEPOINT:
514 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
515 break;
516 case LTTNG_UST_PROBE:
517 tmp[i].type = LTTNG_EVENT_PROBE;
518 break;
519 case LTTNG_UST_FUNCTION:
520 tmp[i].type = LTTNG_EVENT_FUNCTION;
521 break;
522 }
523
524 tmp[i].loglevel = uevent->attr.loglevel;
525 switch (uevent->attr.loglevel_type) {
526 case LTTNG_UST_LOGLEVEL_ALL:
527 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
528 break;
529 case LTTNG_UST_LOGLEVEL_RANGE:
530 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
531 break;
532 case LTTNG_UST_LOGLEVEL_SINGLE:
533 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
534 break;
535 }
536 if (uevent->filter) {
537 tmp[i].filter = 1;
538 }
539 if (uevent->exclusion) {
540 tmp[i].exclusion = 1;
541 }
542 i++;
543
544 /* Append extended info */
545 append_extended_info(uevent->filter_expression,
546 uevent->exclusion, &extended_at);
547 }
548
549 ret = nb_event;
550 *events = tmp;
551 end:
552 rcu_read_unlock();
553 return ret;
554 }
555
556 /*
557 * Fill lttng_event array of all kernel events in the channel.
558 */
559 static int list_lttng_kernel_events(char *channel_name,
560 struct ltt_kernel_session *kernel_session,
561 struct lttng_event **events, size_t *total_size)
562 {
563 int i = 0, ret;
564 unsigned int nb_event;
565 struct ltt_kernel_event *event;
566 struct ltt_kernel_channel *kchan;
567 size_t extended_len = 0;
568 void *extended_at;
569
570 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
571 if (kchan == NULL) {
572 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
573 goto error;
574 }
575
576 nb_event = kchan->event_count;
577
578 DBG("Listing events for channel %s", kchan->channel->name);
579
580 if (nb_event == 0) {
581 *total_size = 0;
582 *events = NULL;
583 goto syscall;
584 }
585
586 /* Compute required extended infos size */
587 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
588 increment_extended_len(event->filter_expression, NULL,
589 &extended_len);
590 }
591
592 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
593 *events = zmalloc(*total_size);
594 if (*events == NULL) {
595 ret = LTTNG_ERR_FATAL;
596 goto error;
597 }
598
599 extended_at = ((uint8_t *) events) +
600 nb_event * sizeof(struct lttng_event);
601
602 /* Kernel channels */
603 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
604 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
605 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
606 (*events)[i].enabled = event->enabled;
607 (*events)[i].filter =
608 (unsigned char) !!event->filter_expression;
609
610 switch (event->event->instrumentation) {
611 case LTTNG_KERNEL_TRACEPOINT:
612 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
613 break;
614 case LTTNG_KERNEL_KRETPROBE:
615 (*events)[i].type = LTTNG_EVENT_FUNCTION;
616 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
617 sizeof(struct lttng_kernel_kprobe));
618 break;
619 case LTTNG_KERNEL_KPROBE:
620 (*events)[i].type = LTTNG_EVENT_PROBE;
621 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
622 sizeof(struct lttng_kernel_kprobe));
623 break;
624 case LTTNG_KERNEL_FUNCTION:
625 (*events)[i].type = LTTNG_EVENT_FUNCTION;
626 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
627 sizeof(struct lttng_kernel_function));
628 break;
629 case LTTNG_KERNEL_NOOP:
630 (*events)[i].type = LTTNG_EVENT_NOOP;
631 break;
632 case LTTNG_KERNEL_SYSCALL:
633 (*events)[i].type = LTTNG_EVENT_SYSCALL;
634 break;
635 case LTTNG_KERNEL_ALL:
636 assert(0);
637 break;
638 }
639 i++;
640
641 /* Append extended info */
642 append_extended_info(event->filter_expression, NULL,
643 &extended_at);
644 }
645
646 syscall:
647 if (syscall_table) {
648 ssize_t new_size;
649
650 new_size = syscall_list_channel(kchan, events, nb_event);
651 if (new_size < 0) {
652 free(events);
653 ret = -new_size;
654 goto error;
655 }
656 nb_event = new_size;
657 }
658
659 return nb_event;
660
661 error:
662 /* Negate the error code to differentiate the size from an error */
663 return -ret;
664 }
665
666 /*
667 * Add URI so the consumer output object. Set the correct path depending on the
668 * domain adding the default trace directory.
669 */
670 static int add_uri_to_consumer(struct consumer_output *consumer,
671 struct lttng_uri *uri, enum lttng_domain_type domain,
672 const char *session_name)
673 {
674 int ret = LTTNG_OK;
675 const char *default_trace_dir;
676
677 assert(uri);
678
679 if (consumer == NULL) {
680 DBG("No consumer detected. Don't add URI. Stopping.");
681 ret = LTTNG_ERR_NO_CONSUMER;
682 goto error;
683 }
684
685 switch (domain) {
686 case LTTNG_DOMAIN_KERNEL:
687 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
688 break;
689 case LTTNG_DOMAIN_UST:
690 default_trace_dir = DEFAULT_UST_TRACE_DIR;
691 break;
692 default:
693 /*
694 * This case is possible is we try to add the URI to the global tracing
695 * session consumer object which in this case there is no subdir.
696 */
697 default_trace_dir = "";
698 }
699
700 switch (uri->dtype) {
701 case LTTNG_DST_IPV4:
702 case LTTNG_DST_IPV6:
703 DBG2("Setting network URI to consumer");
704
705 if (consumer->type == CONSUMER_DST_NET) {
706 if ((uri->stype == LTTNG_STREAM_CONTROL &&
707 consumer->dst.net.control_isset) ||
708 (uri->stype == LTTNG_STREAM_DATA &&
709 consumer->dst.net.data_isset)) {
710 ret = LTTNG_ERR_URL_EXIST;
711 goto error;
712 }
713 } else {
714 memset(&consumer->dst.net, 0, sizeof(consumer->dst.net));
715 }
716
717 consumer->type = CONSUMER_DST_NET;
718
719 /* Set URI into consumer output object */
720 ret = consumer_set_network_uri(consumer, uri);
721 if (ret < 0) {
722 ret = -ret;
723 goto error;
724 } else if (ret == 1) {
725 /*
726 * URI was the same in the consumer so we do not append the subdir
727 * again so to not duplicate output dir.
728 */
729 ret = LTTNG_OK;
730 goto error;
731 }
732
733 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
734 ret = consumer_set_subdir(consumer, session_name);
735 if (ret < 0) {
736 ret = LTTNG_ERR_FATAL;
737 goto error;
738 }
739 }
740
741 if (uri->stype == LTTNG_STREAM_CONTROL) {
742 /* On a new subdir, reappend the default trace dir. */
743 strncat(consumer->subdir, default_trace_dir,
744 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
745 DBG3("Append domain trace name to subdir %s", consumer->subdir);
746 }
747
748 break;
749 case LTTNG_DST_PATH:
750 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
751 memset(consumer->dst.trace_path, 0,
752 sizeof(consumer->dst.trace_path));
753 strncpy(consumer->dst.trace_path, uri->dst.path,
754 sizeof(consumer->dst.trace_path));
755 /* Append default trace dir */
756 strncat(consumer->dst.trace_path, default_trace_dir,
757 sizeof(consumer->dst.trace_path) -
758 strlen(consumer->dst.trace_path) - 1);
759 /* Flag consumer as local. */
760 consumer->type = CONSUMER_DST_LOCAL;
761 break;
762 }
763
764 ret = LTTNG_OK;
765
766 error:
767 return ret;
768 }
769
770 /*
771 * Init tracing by creating trace directory and sending fds kernel consumer.
772 */
773 static int init_kernel_tracing(struct ltt_kernel_session *session)
774 {
775 int ret = 0;
776 struct lttng_ht_iter iter;
777 struct consumer_socket *socket;
778
779 assert(session);
780
781 rcu_read_lock();
782
783 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
784 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
785 socket, node.node) {
786 pthread_mutex_lock(socket->lock);
787 ret = kernel_consumer_send_session(socket, session);
788 pthread_mutex_unlock(socket->lock);
789 if (ret < 0) {
790 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
791 goto error;
792 }
793 }
794 }
795
796 error:
797 rcu_read_unlock();
798 return ret;
799 }
800
801 /*
802 * Create a socket to the relayd using the URI.
803 *
804 * On success, the relayd_sock pointer is set to the created socket.
805 * Else, it's stays untouched and a lttcomm error code is returned.
806 */
807 static int create_connect_relayd(struct lttng_uri *uri,
808 struct lttcomm_relayd_sock **relayd_sock)
809 {
810 int ret;
811 struct lttcomm_relayd_sock *rsock;
812
813 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
814 RELAYD_VERSION_COMM_MINOR);
815 if (!rsock) {
816 ret = LTTNG_ERR_FATAL;
817 goto error;
818 }
819
820 /*
821 * Connect to relayd so we can proceed with a session creation. This call
822 * can possibly block for an arbitrary amount of time to set the health
823 * state to be in poll execution.
824 */
825 health_poll_entry();
826 ret = relayd_connect(rsock);
827 health_poll_exit();
828 if (ret < 0) {
829 ERR("Unable to reach lttng-relayd");
830 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
831 goto free_sock;
832 }
833
834 /* Create socket for control stream. */
835 if (uri->stype == LTTNG_STREAM_CONTROL) {
836 DBG3("Creating relayd stream socket from URI");
837
838 /* Check relayd version */
839 ret = relayd_version_check(rsock);
840 if (ret < 0) {
841 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
842 goto close_sock;
843 }
844 } else if (uri->stype == LTTNG_STREAM_DATA) {
845 DBG3("Creating relayd data socket from URI");
846 } else {
847 /* Command is not valid */
848 ERR("Relayd invalid stream type: %d", uri->stype);
849 ret = LTTNG_ERR_INVALID;
850 goto close_sock;
851 }
852
853 *relayd_sock = rsock;
854
855 return LTTNG_OK;
856
857 close_sock:
858 /* The returned value is not useful since we are on an error path. */
859 (void) relayd_close(rsock);
860 free_sock:
861 free(rsock);
862 error:
863 return ret;
864 }
865
866 /*
867 * Connect to the relayd using URI and send the socket to the right consumer.
868 */
869 static int send_consumer_relayd_socket(enum lttng_domain_type domain,
870 unsigned int session_id, struct lttng_uri *relayd_uri,
871 struct consumer_output *consumer,
872 struct consumer_socket *consumer_sock,
873 char *session_name, char *hostname, int session_live_timer)
874 {
875 int ret;
876 struct lttcomm_relayd_sock *rsock = NULL;
877
878 /* Connect to relayd and make version check if uri is the control. */
879 ret = create_connect_relayd(relayd_uri, &rsock);
880 if (ret != LTTNG_OK) {
881 goto error;
882 }
883 assert(rsock);
884
885 /* Set the network sequence index if not set. */
886 if (consumer->net_seq_index == (uint64_t) -1ULL) {
887 pthread_mutex_lock(&relayd_net_seq_idx_lock);
888 /*
889 * Increment net_seq_idx because we are about to transfer the
890 * new relayd socket to the consumer.
891 * Assign unique key so the consumer can match streams.
892 */
893 consumer->net_seq_index = ++relayd_net_seq_idx;
894 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
895 }
896
897 /* Send relayd socket to consumer. */
898 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
899 relayd_uri->stype, session_id,
900 session_name, hostname, session_live_timer);
901 if (ret < 0) {
902 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
903 goto close_sock;
904 }
905
906 /* Flag that the corresponding socket was sent. */
907 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
908 consumer_sock->control_sock_sent = 1;
909 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
910 consumer_sock->data_sock_sent = 1;
911 }
912
913 ret = LTTNG_OK;
914
915 /*
916 * Close socket which was dup on the consumer side. The session daemon does
917 * NOT keep track of the relayd socket(s) once transfer to the consumer.
918 */
919
920 close_sock:
921 (void) relayd_close(rsock);
922 free(rsock);
923
924 error:
925 if (ret != LTTNG_OK) {
926 /*
927 * The consumer output for this session should not be used anymore
928 * since the relayd connection failed thus making any tracing or/and
929 * streaming not usable.
930 */
931 consumer->enabled = 0;
932 }
933 return ret;
934 }
935
936 /*
937 * Send both relayd sockets to a specific consumer and domain. This is a
938 * helper function to facilitate sending the information to the consumer for a
939 * session.
940 */
941 static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
942 unsigned int session_id, struct consumer_output *consumer,
943 struct consumer_socket *sock, char *session_name,
944 char *hostname, int session_live_timer)
945 {
946 int ret = LTTNG_OK;
947
948 assert(consumer);
949 assert(sock);
950
951 /* Sending control relayd socket. */
952 if (!sock->control_sock_sent) {
953 ret = send_consumer_relayd_socket(domain, session_id,
954 &consumer->dst.net.control, consumer, sock,
955 session_name, hostname, session_live_timer);
956 if (ret != LTTNG_OK) {
957 goto error;
958 }
959 }
960
961 /* Sending data relayd socket. */
962 if (!sock->data_sock_sent) {
963 ret = send_consumer_relayd_socket(domain, session_id,
964 &consumer->dst.net.data, consumer, sock,
965 session_name, hostname, session_live_timer);
966 if (ret != LTTNG_OK) {
967 goto error;
968 }
969 }
970
971 error:
972 return ret;
973 }
974
975 /*
976 * Setup relayd connections for a tracing session. First creates the socket to
977 * the relayd and send them to the right domain consumer. Consumer type MUST be
978 * network.
979 */
980 int cmd_setup_relayd(struct ltt_session *session)
981 {
982 int ret = LTTNG_OK;
983 struct ltt_ust_session *usess;
984 struct ltt_kernel_session *ksess;
985 struct consumer_socket *socket;
986 struct lttng_ht_iter iter;
987
988 assert(session);
989
990 usess = session->ust_session;
991 ksess = session->kernel_session;
992
993 DBG("Setting relayd for session %s", session->name);
994
995 rcu_read_lock();
996
997 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
998 && usess->consumer->enabled) {
999 /* For each consumer socket, send relayd sockets */
1000 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1001 socket, node.node) {
1002 pthread_mutex_lock(socket->lock);
1003 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1004 usess->consumer, socket,
1005 session->name, session->hostname,
1006 session->live_timer);
1007 pthread_mutex_unlock(socket->lock);
1008 if (ret != LTTNG_OK) {
1009 goto error;
1010 }
1011 /* Session is now ready for network streaming. */
1012 session->net_handle = 1;
1013 }
1014 }
1015
1016 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1017 && ksess->consumer->enabled) {
1018 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1019 socket, node.node) {
1020 pthread_mutex_lock(socket->lock);
1021 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1022 ksess->consumer, socket,
1023 session->name, session->hostname,
1024 session->live_timer);
1025 pthread_mutex_unlock(socket->lock);
1026 if (ret != LTTNG_OK) {
1027 goto error;
1028 }
1029 /* Session is now ready for network streaming. */
1030 session->net_handle = 1;
1031 }
1032 }
1033
1034 error:
1035 rcu_read_unlock();
1036 return ret;
1037 }
1038
1039 /*
1040 * Start a kernel session by opening all necessary streams.
1041 */
1042 static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
1043 {
1044 int ret;
1045 struct ltt_kernel_channel *kchan;
1046
1047 /* Open kernel metadata */
1048 if (ksess->metadata == NULL && ksess->output_traces) {
1049 ret = kernel_open_metadata(ksess);
1050 if (ret < 0) {
1051 ret = LTTNG_ERR_KERN_META_FAIL;
1052 goto error;
1053 }
1054 }
1055
1056 /* Open kernel metadata stream */
1057 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1058 ret = kernel_open_metadata_stream(ksess);
1059 if (ret < 0) {
1060 ERR("Kernel create metadata stream failed");
1061 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1062 goto error;
1063 }
1064 }
1065
1066 /* For each channel */
1067 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1068 if (kchan->stream_count == 0) {
1069 ret = kernel_open_channel_stream(kchan);
1070 if (ret < 0) {
1071 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1072 goto error;
1073 }
1074 /* Update the stream global counter */
1075 ksess->stream_count_global += ret;
1076 }
1077 }
1078
1079 /* Setup kernel consumer socket and send fds to it */
1080 ret = init_kernel_tracing(ksess);
1081 if (ret != 0) {
1082 ret = LTTNG_ERR_KERN_START_FAIL;
1083 goto error;
1084 }
1085
1086 /* This start the kernel tracing */
1087 ret = kernel_start_session(ksess);
1088 if (ret < 0) {
1089 ret = LTTNG_ERR_KERN_START_FAIL;
1090 goto error;
1091 }
1092
1093 /* Quiescent wait after starting trace */
1094 kernel_wait_quiescent(kernel_tracer_fd);
1095
1096 ksess->active = 1;
1097
1098 ret = LTTNG_OK;
1099
1100 error:
1101 return ret;
1102 }
1103
1104 /*
1105 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1106 */
1107 int cmd_disable_channel(struct ltt_session *session,
1108 enum lttng_domain_type domain, char *channel_name)
1109 {
1110 int ret;
1111 struct ltt_ust_session *usess;
1112
1113 usess = session->ust_session;
1114
1115 rcu_read_lock();
1116
1117 switch (domain) {
1118 case LTTNG_DOMAIN_KERNEL:
1119 {
1120 ret = channel_kernel_disable(session->kernel_session,
1121 channel_name);
1122 if (ret != LTTNG_OK) {
1123 goto error;
1124 }
1125
1126 kernel_wait_quiescent(kernel_tracer_fd);
1127 break;
1128 }
1129 case LTTNG_DOMAIN_UST:
1130 {
1131 struct ltt_ust_channel *uchan;
1132 struct lttng_ht *chan_ht;
1133
1134 chan_ht = usess->domain_global.channels;
1135
1136 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1137 if (uchan == NULL) {
1138 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1139 goto error;
1140 }
1141
1142 ret = channel_ust_disable(usess, uchan);
1143 if (ret != LTTNG_OK) {
1144 goto error;
1145 }
1146 break;
1147 }
1148 default:
1149 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1150 goto error;
1151 }
1152
1153 ret = LTTNG_OK;
1154
1155 error:
1156 rcu_read_unlock();
1157 return ret;
1158 }
1159
1160 /*
1161 * Command LTTNG_TRACK_PID processed by the client thread.
1162 *
1163 * Called with session lock held.
1164 */
1165 int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
1166 int pid)
1167 {
1168 int ret;
1169
1170 rcu_read_lock();
1171
1172 switch (domain) {
1173 case LTTNG_DOMAIN_KERNEL:
1174 {
1175 struct ltt_kernel_session *ksess;
1176
1177 ksess = session->kernel_session;
1178
1179 ret = kernel_track_pid(ksess, pid);
1180 if (ret != LTTNG_OK) {
1181 goto error;
1182 }
1183
1184 kernel_wait_quiescent(kernel_tracer_fd);
1185 break;
1186 }
1187 case LTTNG_DOMAIN_UST:
1188 {
1189 struct ltt_ust_session *usess;
1190
1191 usess = session->ust_session;
1192
1193 ret = trace_ust_track_pid(usess, pid);
1194 if (ret != LTTNG_OK) {
1195 goto error;
1196 }
1197 break;
1198 }
1199 default:
1200 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1201 goto error;
1202 }
1203
1204 ret = LTTNG_OK;
1205
1206 error:
1207 rcu_read_unlock();
1208 return ret;
1209 }
1210
1211 /*
1212 * Command LTTNG_UNTRACK_PID processed by the client thread.
1213 *
1214 * Called with session lock held.
1215 */
1216 int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
1217 int pid)
1218 {
1219 int ret;
1220
1221 rcu_read_lock();
1222
1223 switch (domain) {
1224 case LTTNG_DOMAIN_KERNEL:
1225 {
1226 struct ltt_kernel_session *ksess;
1227
1228 ksess = session->kernel_session;
1229
1230 ret = kernel_untrack_pid(ksess, pid);
1231 if (ret != LTTNG_OK) {
1232 goto error;
1233 }
1234
1235 kernel_wait_quiescent(kernel_tracer_fd);
1236 break;
1237 }
1238 case LTTNG_DOMAIN_UST:
1239 {
1240 struct ltt_ust_session *usess;
1241
1242 usess = session->ust_session;
1243
1244 ret = trace_ust_untrack_pid(usess, pid);
1245 if (ret != LTTNG_OK) {
1246 goto error;
1247 }
1248 break;
1249 }
1250 default:
1251 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1252 goto error;
1253 }
1254
1255 ret = LTTNG_OK;
1256
1257 error:
1258 rcu_read_unlock();
1259 return ret;
1260 }
1261
1262 /*
1263 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1264 *
1265 * The wpipe arguments is used as a notifier for the kernel thread.
1266 */
1267 int cmd_enable_channel(struct ltt_session *session,
1268 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
1269 {
1270 int ret;
1271 struct ltt_ust_session *usess = session->ust_session;
1272 struct lttng_ht *chan_ht;
1273 size_t len;
1274
1275 assert(session);
1276 assert(attr);
1277 assert(domain);
1278
1279 len = lttng_strnlen(attr->name, sizeof(attr->name));
1280
1281 /* Validate channel name */
1282 if (attr->name[0] == '.' ||
1283 memchr(attr->name, '/', len) != NULL) {
1284 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1285 goto end;
1286 }
1287
1288 DBG("Enabling channel %s for session %s", attr->name, session->name);
1289
1290 rcu_read_lock();
1291
1292 /*
1293 * Don't try to enable a channel if the session has been started at
1294 * some point in time before. The tracer does not allow it.
1295 */
1296 if (session->has_been_started) {
1297 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1298 goto error;
1299 }
1300
1301 /*
1302 * If the session is a live session, remove the switch timer, the
1303 * live timer does the same thing but sends also synchronisation
1304 * beacons for inactive streams.
1305 */
1306 if (session->live_timer > 0) {
1307 attr->attr.live_timer_interval = session->live_timer;
1308 attr->attr.switch_timer_interval = 0;
1309 }
1310
1311 /*
1312 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1313 * mode and with less than 2 subbuf so block it right away and send back an
1314 * invalid attribute error.
1315 */
1316 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
1317 ret = LTTNG_ERR_INVALID;
1318 goto error;
1319 }
1320
1321 switch (domain->type) {
1322 case LTTNG_DOMAIN_KERNEL:
1323 {
1324 struct ltt_kernel_channel *kchan;
1325
1326 kchan = trace_kernel_get_channel_by_name(attr->name,
1327 session->kernel_session);
1328 if (kchan == NULL) {
1329 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
1330 if (attr->name[0] != '\0') {
1331 session->kernel_session->has_non_default_channel = 1;
1332 }
1333 } else {
1334 ret = channel_kernel_enable(session->kernel_session, kchan);
1335 }
1336
1337 if (ret != LTTNG_OK) {
1338 goto error;
1339 }
1340
1341 kernel_wait_quiescent(kernel_tracer_fd);
1342 break;
1343 }
1344 case LTTNG_DOMAIN_UST:
1345 case LTTNG_DOMAIN_JUL:
1346 case LTTNG_DOMAIN_LOG4J:
1347 case LTTNG_DOMAIN_PYTHON:
1348 {
1349 struct ltt_ust_channel *uchan;
1350
1351 /*
1352 * FIXME
1353 *
1354 * Current agent implementation limitations force us to allow
1355 * only one channel at once in "agent" subdomains. Each
1356 * subdomain has a default channel name which must be strictly
1357 * adhered to.
1358 */
1359 if (domain->type == LTTNG_DOMAIN_JUL) {
1360 if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME,
1361 LTTNG_SYMBOL_NAME_LEN)) {
1362 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1363 goto error;
1364 }
1365 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1366 if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME,
1367 LTTNG_SYMBOL_NAME_LEN)) {
1368 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1369 goto error;
1370 }
1371 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1372 if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME,
1373 LTTNG_SYMBOL_NAME_LEN)) {
1374 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1375 goto error;
1376 }
1377 }
1378
1379 chan_ht = usess->domain_global.channels;
1380
1381 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1382 if (uchan == NULL) {
1383 ret = channel_ust_create(usess, attr, domain->buf_type);
1384 if (attr->name[0] != '\0') {
1385 usess->has_non_default_channel = 1;
1386 }
1387 } else {
1388 ret = channel_ust_enable(usess, uchan);
1389 }
1390 break;
1391 }
1392 default:
1393 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1394 goto error;
1395 }
1396
1397 error:
1398 rcu_read_unlock();
1399 end:
1400 return ret;
1401 }
1402
1403 /*
1404 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1405 */
1406 int cmd_disable_event(struct ltt_session *session,
1407 enum lttng_domain_type domain, char *channel_name,
1408 struct lttng_event *event)
1409 {
1410 int ret;
1411 char *event_name;
1412
1413 DBG("Disable event command for event \'%s\'", event->name);
1414
1415 event_name = event->name;
1416 if (validate_event_name(event_name)) {
1417 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1418 goto error;
1419 }
1420
1421 /* Error out on unhandled search criteria */
1422 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1423 || event->pid || event->filter || event->exclusion) {
1424 ret = LTTNG_ERR_UNK;
1425 goto error;
1426 }
1427
1428 rcu_read_lock();
1429
1430 switch (domain) {
1431 case LTTNG_DOMAIN_KERNEL:
1432 {
1433 struct ltt_kernel_channel *kchan;
1434 struct ltt_kernel_session *ksess;
1435
1436 ksess = session->kernel_session;
1437
1438 /*
1439 * If a non-default channel has been created in the
1440 * session, explicitely require that -c chan_name needs
1441 * to be provided.
1442 */
1443 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1444 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1445 goto error_unlock;
1446 }
1447
1448 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1449 if (kchan == NULL) {
1450 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1451 goto error_unlock;
1452 }
1453
1454 switch (event->type) {
1455 case LTTNG_EVENT_ALL:
1456 case LTTNG_EVENT_TRACEPOINT:
1457 case LTTNG_EVENT_SYSCALL:
1458 case LTTNG_EVENT_PROBE:
1459 case LTTNG_EVENT_FUNCTION:
1460 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1461 if (event_name[0] == '\0') {
1462 ret = event_kernel_disable_event(kchan,
1463 NULL, event->type);
1464 } else {
1465 ret = event_kernel_disable_event(kchan,
1466 event_name, event->type);
1467 }
1468 if (ret != LTTNG_OK) {
1469 goto error_unlock;
1470 }
1471 break;
1472 default:
1473 ret = LTTNG_ERR_UNK;
1474 goto error_unlock;
1475 }
1476
1477 kernel_wait_quiescent(kernel_tracer_fd);
1478 break;
1479 }
1480 case LTTNG_DOMAIN_UST:
1481 {
1482 struct ltt_ust_channel *uchan;
1483 struct ltt_ust_session *usess;
1484
1485 usess = session->ust_session;
1486
1487 if (validate_ust_event_name(event_name)) {
1488 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1489 goto error_unlock;
1490 }
1491
1492 /*
1493 * If a non-default channel has been created in the
1494 * session, explicitly require that -c chan_name needs
1495 * to be provided.
1496 */
1497 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1498 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1499 goto error_unlock;
1500 }
1501
1502 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1503 channel_name);
1504 if (uchan == NULL) {
1505 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1506 goto error_unlock;
1507 }
1508
1509 switch (event->type) {
1510 case LTTNG_EVENT_ALL:
1511 /*
1512 * An empty event name means that everything
1513 * should be disabled.
1514 */
1515 if (event->name[0] == '\0') {
1516 ret = event_ust_disable_all_tracepoints(usess, uchan);
1517 } else {
1518 ret = event_ust_disable_tracepoint(usess, uchan,
1519 event_name);
1520 }
1521 if (ret != LTTNG_OK) {
1522 goto error_unlock;
1523 }
1524 break;
1525 default:
1526 ret = LTTNG_ERR_UNK;
1527 goto error_unlock;
1528 }
1529
1530 DBG3("Disable UST event %s in channel %s completed", event_name,
1531 channel_name);
1532 break;
1533 }
1534 case LTTNG_DOMAIN_LOG4J:
1535 case LTTNG_DOMAIN_JUL:
1536 case LTTNG_DOMAIN_PYTHON:
1537 {
1538 struct agent *agt;
1539 struct ltt_ust_session *usess = session->ust_session;
1540
1541 assert(usess);
1542
1543 switch (event->type) {
1544 case LTTNG_EVENT_ALL:
1545 break;
1546 default:
1547 ret = LTTNG_ERR_UNK;
1548 goto error_unlock;
1549 }
1550
1551 agt = trace_ust_find_agent(usess, domain);
1552 if (!agt) {
1553 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1554 goto error_unlock;
1555 }
1556 /*
1557 * An empty event name means that everything
1558 * should be disabled.
1559 */
1560 if (event->name[0] == '\0') {
1561 ret = event_agent_disable_all(usess, agt);
1562 } else {
1563 ret = event_agent_disable(usess, agt, event_name);
1564 }
1565 if (ret != LTTNG_OK) {
1566 goto error_unlock;
1567 }
1568
1569 break;
1570 }
1571 default:
1572 ret = LTTNG_ERR_UND;
1573 goto error_unlock;
1574 }
1575
1576 ret = LTTNG_OK;
1577
1578 error_unlock:
1579 rcu_read_unlock();
1580 error:
1581 return ret;
1582 }
1583
1584 /*
1585 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1586 */
1587 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1588 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
1589 {
1590 int ret, chan_kern_created = 0, chan_ust_created = 0;
1591 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1592
1593 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1594 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1595 app_ctx_name = ctx->u.app_ctx.ctx_name;
1596 }
1597
1598 switch (domain) {
1599 case LTTNG_DOMAIN_KERNEL:
1600 assert(session->kernel_session);
1601
1602 if (session->kernel_session->channel_count == 0) {
1603 /* Create default channel */
1604 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1605 if (ret != LTTNG_OK) {
1606 goto error;
1607 }
1608 chan_kern_created = 1;
1609 }
1610 /* Add kernel context to kernel tracer */
1611 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1612 if (ret != LTTNG_OK) {
1613 goto error;
1614 }
1615 break;
1616 case LTTNG_DOMAIN_JUL:
1617 case LTTNG_DOMAIN_LOG4J:
1618 {
1619 /*
1620 * Validate channel name.
1621 * If no channel name is given and the domain is JUL or LOG4J,
1622 * set it to the appropriate domain-specific channel name. If
1623 * a name is provided but does not match the expexted channel
1624 * name, return an error.
1625 */
1626 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1627 strcmp(channel_name,
1628 DEFAULT_JUL_CHANNEL_NAME)) {
1629 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1630 goto error;
1631 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1632 strcmp(channel_name,
1633 DEFAULT_LOG4J_CHANNEL_NAME)) {
1634 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1635 goto error;
1636 }
1637 /* break is _not_ missing here. */
1638 }
1639 case LTTNG_DOMAIN_UST:
1640 {
1641 struct ltt_ust_session *usess = session->ust_session;
1642 unsigned int chan_count;
1643
1644 assert(usess);
1645
1646 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1647 if (chan_count == 0) {
1648 struct lttng_channel *attr;
1649 /* Create default channel */
1650 attr = channel_new_default_attr(domain, usess->buffer_type);
1651 if (attr == NULL) {
1652 ret = LTTNG_ERR_FATAL;
1653 goto error;
1654 }
1655
1656 ret = channel_ust_create(usess, attr, usess->buffer_type);
1657 if (ret != LTTNG_OK) {
1658 free(attr);
1659 goto error;
1660 }
1661 free(attr);
1662 chan_ust_created = 1;
1663 }
1664
1665 ret = context_ust_add(usess, domain, ctx, channel_name);
1666 free(app_ctx_provider_name);
1667 free(app_ctx_name);
1668 app_ctx_name = NULL;
1669 app_ctx_provider_name = NULL;
1670 if (ret != LTTNG_OK) {
1671 goto error;
1672 }
1673 break;
1674 }
1675 default:
1676 ret = LTTNG_ERR_UND;
1677 goto error;
1678 }
1679
1680 ret = LTTNG_OK;
1681 goto end;
1682
1683 error:
1684 if (chan_kern_created) {
1685 struct ltt_kernel_channel *kchan =
1686 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1687 session->kernel_session);
1688 /* Created previously, this should NOT fail. */
1689 assert(kchan);
1690 kernel_destroy_channel(kchan);
1691 }
1692
1693 if (chan_ust_created) {
1694 struct ltt_ust_channel *uchan =
1695 trace_ust_find_channel_by_name(
1696 session->ust_session->domain_global.channels,
1697 DEFAULT_CHANNEL_NAME);
1698 /* Created previously, this should NOT fail. */
1699 assert(uchan);
1700 /* Remove from the channel list of the session. */
1701 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1702 uchan);
1703 trace_ust_destroy_channel(uchan);
1704 }
1705 end:
1706 free(app_ctx_provider_name);
1707 free(app_ctx_name);
1708 return ret;
1709 }
1710
1711 static int validate_event_name(const char *name)
1712 {
1713 int ret = 0;
1714 const char *c = name;
1715 const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN;
1716 bool null_terminated = false;
1717
1718 /*
1719 * Make sure that unescaped wildcards are only used as the last
1720 * character of the event name.
1721 */
1722 while (c < event_name_end) {
1723 switch (*c) {
1724 case '\0':
1725 null_terminated = true;
1726 goto end;
1727 case '\\':
1728 c++;
1729 break;
1730 case '*':
1731 if ((c + 1) < event_name_end && *(c + 1)) {
1732 /* Wildcard is not the last character */
1733 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1734 goto end;
1735 }
1736 default:
1737 break;
1738 }
1739 c++;
1740 }
1741 end:
1742 if (!ret && !null_terminated) {
1743 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1744 }
1745 return ret;
1746 }
1747
1748 static inline bool name_starts_with(const char *name, const char *prefix)
1749 {
1750 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1751
1752 return !strncmp(name, prefix, max_cmp_len);
1753 }
1754
1755 /* Perform userspace-specific event name validation */
1756 static int validate_ust_event_name(const char *name)
1757 {
1758 int ret = 0;
1759
1760 if (!name) {
1761 ret = -1;
1762 goto end;
1763 }
1764
1765 /*
1766 * Check name against all internal UST event component namespaces used
1767 * by the agents.
1768 */
1769 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
1770 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
1771 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
1772 ret = -1;
1773 }
1774
1775 end:
1776 return ret;
1777 }
1778
1779 /*
1780 * Internal version of cmd_enable_event() with a supplemental
1781 * "internal_event" flag which is used to enable internal events which should
1782 * be hidden from clients. Such events are used in the agent implementation to
1783 * enable the events through which all "agent" events are funeled.
1784 */
1785 static int _cmd_enable_event(struct ltt_session *session,
1786 struct lttng_domain *domain,
1787 char *channel_name, struct lttng_event *event,
1788 char *filter_expression,
1789 struct lttng_filter_bytecode *filter,
1790 struct lttng_event_exclusion *exclusion,
1791 int wpipe, bool internal_event)
1792 {
1793 int ret, channel_created = 0;
1794 struct lttng_channel *attr;
1795
1796 assert(session);
1797 assert(event);
1798 assert(channel_name);
1799
1800 /* If we have a filter, we must have its filter expression */
1801 assert(!(!!filter_expression ^ !!filter));
1802
1803 DBG("Enable event command for event \'%s\'", event->name);
1804
1805 rcu_read_lock();
1806
1807 ret = validate_event_name(event->name);
1808 if (ret) {
1809 goto error;
1810 }
1811
1812 switch (domain->type) {
1813 case LTTNG_DOMAIN_KERNEL:
1814 {
1815 struct ltt_kernel_channel *kchan;
1816
1817 /*
1818 * If a non-default channel has been created in the
1819 * session, explicitely require that -c chan_name needs
1820 * to be provided.
1821 */
1822 if (session->kernel_session->has_non_default_channel
1823 && channel_name[0] == '\0') {
1824 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1825 goto error;
1826 }
1827
1828 kchan = trace_kernel_get_channel_by_name(channel_name,
1829 session->kernel_session);
1830 if (kchan == NULL) {
1831 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1832 LTTNG_BUFFER_GLOBAL);
1833 if (attr == NULL) {
1834 ret = LTTNG_ERR_FATAL;
1835 goto error;
1836 }
1837 strncpy(attr->name, channel_name, sizeof(attr->name));
1838
1839 ret = cmd_enable_channel(session, domain, attr, wpipe);
1840 if (ret != LTTNG_OK) {
1841 free(attr);
1842 goto error;
1843 }
1844 free(attr);
1845
1846 channel_created = 1;
1847 }
1848
1849 /* Get the newly created kernel channel pointer */
1850 kchan = trace_kernel_get_channel_by_name(channel_name,
1851 session->kernel_session);
1852 if (kchan == NULL) {
1853 /* This sould not happen... */
1854 ret = LTTNG_ERR_FATAL;
1855 goto error;
1856 }
1857
1858 switch (event->type) {
1859 case LTTNG_EVENT_ALL:
1860 {
1861 char *filter_expression_a = NULL;
1862 struct lttng_filter_bytecode *filter_a = NULL;
1863
1864 /*
1865 * We need to duplicate filter_expression and filter,
1866 * because ownership is passed to first enable
1867 * event.
1868 */
1869 if (filter_expression) {
1870 filter_expression_a = strdup(filter_expression);
1871 if (!filter_expression_a) {
1872 ret = LTTNG_ERR_FATAL;
1873 goto error;
1874 }
1875 }
1876 if (filter) {
1877 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
1878 if (!filter_a) {
1879 free(filter_expression_a);
1880 ret = LTTNG_ERR_FATAL;
1881 goto error;
1882 }
1883 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
1884 }
1885 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
1886 ret = event_kernel_enable_event(kchan, event,
1887 filter_expression, filter);
1888 /* We have passed ownership */
1889 filter_expression = NULL;
1890 filter = NULL;
1891 if (ret != LTTNG_OK) {
1892 if (channel_created) {
1893 /* Let's not leak a useless channel. */
1894 kernel_destroy_channel(kchan);
1895 }
1896 free(filter_expression_a);
1897 free(filter_a);
1898 goto error;
1899 }
1900 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
1901 ret = event_kernel_enable_event(kchan, event,
1902 filter_expression_a, filter_a);
1903 /* We have passed ownership */
1904 filter_expression_a = NULL;
1905 filter_a = NULL;
1906 if (ret != LTTNG_OK) {
1907 goto error;
1908 }
1909 break;
1910 }
1911 case LTTNG_EVENT_PROBE:
1912 case LTTNG_EVENT_FUNCTION:
1913 case LTTNG_EVENT_FUNCTION_ENTRY:
1914 case LTTNG_EVENT_TRACEPOINT:
1915 ret = event_kernel_enable_event(kchan, event,
1916 filter_expression, filter);
1917 /* We have passed ownership */
1918 filter_expression = NULL;
1919 filter = NULL;
1920 if (ret != LTTNG_OK) {
1921 if (channel_created) {
1922 /* Let's not leak a useless channel. */
1923 kernel_destroy_channel(kchan);
1924 }
1925 goto error;
1926 }
1927 break;
1928 case LTTNG_EVENT_SYSCALL:
1929 ret = event_kernel_enable_event(kchan, event,
1930 filter_expression, filter);
1931 /* We have passed ownership */
1932 filter_expression = NULL;
1933 filter = NULL;
1934 if (ret != LTTNG_OK) {
1935 goto error;
1936 }
1937 break;
1938 default:
1939 ret = LTTNG_ERR_UNK;
1940 goto error;
1941 }
1942
1943 kernel_wait_quiescent(kernel_tracer_fd);
1944 break;
1945 }
1946 case LTTNG_DOMAIN_UST:
1947 {
1948 struct ltt_ust_channel *uchan;
1949 struct ltt_ust_session *usess = session->ust_session;
1950
1951 assert(usess);
1952
1953 /*
1954 * If a non-default channel has been created in the
1955 * session, explicitely require that -c chan_name needs
1956 * to be provided.
1957 */
1958 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1959 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1960 goto error;
1961 }
1962
1963 /* Get channel from global UST domain */
1964 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1965 channel_name);
1966 if (uchan == NULL) {
1967 /* Create default channel */
1968 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1969 usess->buffer_type);
1970 if (attr == NULL) {
1971 ret = LTTNG_ERR_FATAL;
1972 goto error;
1973 }
1974 strncpy(attr->name, channel_name, sizeof(attr->name));
1975
1976 ret = cmd_enable_channel(session, domain, attr, wpipe);
1977 if (ret != LTTNG_OK) {
1978 free(attr);
1979 goto error;
1980 }
1981 free(attr);
1982
1983 /* Get the newly created channel reference back */
1984 uchan = trace_ust_find_channel_by_name(
1985 usess->domain_global.channels, channel_name);
1986 assert(uchan);
1987 }
1988
1989 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
1990 /*
1991 * Don't allow users to add UST events to channels which
1992 * are assigned to a userspace subdomain (JUL, Log4J,
1993 * Python, etc.).
1994 */
1995 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
1996 goto error;
1997 }
1998
1999 if (!internal_event) {
2000 /*
2001 * Ensure the event name is not reserved for internal
2002 * use.
2003 */
2004 ret = validate_ust_event_name(event->name);
2005 if (ret) {
2006 WARN("Userspace event name %s failed validation.",
2007 event->name ?
2008 event->name : "NULL");
2009 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2010 goto error;
2011 }
2012 }
2013
2014 /* At this point, the session and channel exist on the tracer */
2015 ret = event_ust_enable_tracepoint(usess, uchan, event,
2016 filter_expression, filter, exclusion,
2017 internal_event);
2018 /* We have passed ownership */
2019 filter_expression = NULL;
2020 filter = NULL;
2021 exclusion = NULL;
2022 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2023 goto already_enabled;
2024 } else if (ret != LTTNG_OK) {
2025 goto error;
2026 }
2027 break;
2028 }
2029 case LTTNG_DOMAIN_LOG4J:
2030 case LTTNG_DOMAIN_JUL:
2031 case LTTNG_DOMAIN_PYTHON:
2032 {
2033 const char *default_event_name, *default_chan_name;
2034 struct agent *agt;
2035 struct lttng_event uevent;
2036 struct lttng_domain tmp_dom;
2037 struct ltt_ust_session *usess = session->ust_session;
2038
2039 assert(usess);
2040
2041 agt = trace_ust_find_agent(usess, domain->type);
2042 if (!agt) {
2043 agt = agent_create(domain->type);
2044 if (!agt) {
2045 ret = LTTNG_ERR_NOMEM;
2046 goto error;
2047 }
2048 agent_add(agt, usess->agents);
2049 }
2050
2051 /* Create the default tracepoint. */
2052 memset(&uevent, 0, sizeof(uevent));
2053 uevent.type = LTTNG_EVENT_TRACEPOINT;
2054 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2055 default_event_name = event_get_default_agent_ust_name(
2056 domain->type);
2057 if (!default_event_name) {
2058 ret = LTTNG_ERR_FATAL;
2059 goto error;
2060 }
2061 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2062 uevent.name[sizeof(uevent.name) - 1] = '\0';
2063
2064 /*
2065 * The domain type is changed because we are about to enable the
2066 * default channel and event for the JUL domain that are hardcoded.
2067 * This happens in the UST domain.
2068 */
2069 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2070 tmp_dom.type = LTTNG_DOMAIN_UST;
2071
2072 switch (domain->type) {
2073 case LTTNG_DOMAIN_LOG4J:
2074 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2075 break;
2076 case LTTNG_DOMAIN_JUL:
2077 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2078 break;
2079 case LTTNG_DOMAIN_PYTHON:
2080 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2081 break;
2082 default:
2083 /* The switch/case we are in makes this impossible */
2084 assert(0);
2085 }
2086
2087 {
2088 char *filter_expression_copy = NULL;
2089 struct lttng_filter_bytecode *filter_copy = NULL;
2090
2091 if (filter) {
2092 const size_t filter_size = sizeof(
2093 struct lttng_filter_bytecode)
2094 + filter->len;
2095
2096 filter_copy = zmalloc(filter_size);
2097 if (!filter_copy) {
2098 ret = LTTNG_ERR_NOMEM;
2099 goto error;
2100 }
2101 memcpy(filter_copy, filter, filter_size);
2102
2103 filter_expression_copy =
2104 strdup(filter_expression);
2105 if (!filter_expression) {
2106 ret = LTTNG_ERR_NOMEM;
2107 }
2108
2109 if (!filter_expression_copy || !filter_copy) {
2110 free(filter_expression_copy);
2111 free(filter_copy);
2112 goto error;
2113 }
2114 }
2115
2116 ret = cmd_enable_event_internal(session, &tmp_dom,
2117 (char *) default_chan_name,
2118 &uevent, filter_expression_copy,
2119 filter_copy, NULL, wpipe);
2120 }
2121
2122 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2123 goto already_enabled;
2124 } else if (ret != LTTNG_OK) {
2125 goto error;
2126 }
2127
2128 /* The wild card * means that everything should be enabled. */
2129 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2130 ret = event_agent_enable_all(usess, agt, event, filter,
2131 filter_expression);
2132 } else {
2133 ret = event_agent_enable(usess, agt, event, filter,
2134 filter_expression);
2135 }
2136 filter = NULL;
2137 filter_expression = NULL;
2138 if (ret != LTTNG_OK) {
2139 goto error;
2140 }
2141
2142 break;
2143 }
2144 default:
2145 ret = LTTNG_ERR_UND;
2146 goto error;
2147 }
2148
2149 ret = LTTNG_OK;
2150
2151 already_enabled:
2152 error:
2153 free(filter_expression);
2154 free(filter);
2155 free(exclusion);
2156 rcu_read_unlock();
2157 return ret;
2158 }
2159
2160 /*
2161 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2162 * We own filter, exclusion, and filter_expression.
2163 */
2164 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
2165 char *channel_name, struct lttng_event *event,
2166 char *filter_expression,
2167 struct lttng_filter_bytecode *filter,
2168 struct lttng_event_exclusion *exclusion,
2169 int wpipe)
2170 {
2171 return _cmd_enable_event(session, domain, channel_name, event,
2172 filter_expression, filter, exclusion, wpipe, false);
2173 }
2174
2175 /*
2176 * Enable an event which is internal to LTTng. An internal should
2177 * never be made visible to clients and are immune to checks such as
2178 * reserved names.
2179 */
2180 static int cmd_enable_event_internal(struct ltt_session *session,
2181 struct lttng_domain *domain,
2182 char *channel_name, struct lttng_event *event,
2183 char *filter_expression,
2184 struct lttng_filter_bytecode *filter,
2185 struct lttng_event_exclusion *exclusion,
2186 int wpipe)
2187 {
2188 return _cmd_enable_event(session, domain, channel_name, event,
2189 filter_expression, filter, exclusion, wpipe, true);
2190 }
2191
2192 /*
2193 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2194 */
2195 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2196 struct lttng_event **events)
2197 {
2198 int ret;
2199 ssize_t nb_events = 0;
2200
2201 switch (domain) {
2202 case LTTNG_DOMAIN_KERNEL:
2203 nb_events = kernel_list_events(kernel_tracer_fd, events);
2204 if (nb_events < 0) {
2205 ret = LTTNG_ERR_KERN_LIST_FAIL;
2206 goto error;
2207 }
2208 break;
2209 case LTTNG_DOMAIN_UST:
2210 nb_events = ust_app_list_events(events);
2211 if (nb_events < 0) {
2212 ret = LTTNG_ERR_UST_LIST_FAIL;
2213 goto error;
2214 }
2215 break;
2216 case LTTNG_DOMAIN_LOG4J:
2217 case LTTNG_DOMAIN_JUL:
2218 case LTTNG_DOMAIN_PYTHON:
2219 nb_events = agent_list_events(events, domain);
2220 if (nb_events < 0) {
2221 ret = LTTNG_ERR_UST_LIST_FAIL;
2222 goto error;
2223 }
2224 break;
2225 default:
2226 ret = LTTNG_ERR_UND;
2227 goto error;
2228 }
2229
2230 return nb_events;
2231
2232 error:
2233 /* Return negative value to differentiate return code */
2234 return -ret;
2235 }
2236
2237 /*
2238 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2239 */
2240 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2241 struct lttng_event_field **fields)
2242 {
2243 int ret;
2244 ssize_t nb_fields = 0;
2245
2246 switch (domain) {
2247 case LTTNG_DOMAIN_UST:
2248 nb_fields = ust_app_list_event_fields(fields);
2249 if (nb_fields < 0) {
2250 ret = LTTNG_ERR_UST_LIST_FAIL;
2251 goto error;
2252 }
2253 break;
2254 case LTTNG_DOMAIN_KERNEL:
2255 default: /* fall-through */
2256 ret = LTTNG_ERR_UND;
2257 goto error;
2258 }
2259
2260 return nb_fields;
2261
2262 error:
2263 /* Return negative value to differentiate return code */
2264 return -ret;
2265 }
2266
2267 ssize_t cmd_list_syscalls(struct lttng_event **events)
2268 {
2269 return syscall_table_list(events);
2270 }
2271
2272 /*
2273 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2274 *
2275 * Called with session lock held.
2276 */
2277 ssize_t cmd_list_tracker_pids(struct ltt_session *session,
2278 enum lttng_domain_type domain, int32_t **pids)
2279 {
2280 int ret;
2281 ssize_t nr_pids = 0;
2282
2283 switch (domain) {
2284 case LTTNG_DOMAIN_KERNEL:
2285 {
2286 struct ltt_kernel_session *ksess;
2287
2288 ksess = session->kernel_session;
2289 nr_pids = kernel_list_tracker_pids(ksess, pids);
2290 if (nr_pids < 0) {
2291 ret = LTTNG_ERR_KERN_LIST_FAIL;
2292 goto error;
2293 }
2294 break;
2295 }
2296 case LTTNG_DOMAIN_UST:
2297 {
2298 struct ltt_ust_session *usess;
2299
2300 usess = session->ust_session;
2301 nr_pids = trace_ust_list_tracker_pids(usess, pids);
2302 if (nr_pids < 0) {
2303 ret = LTTNG_ERR_UST_LIST_FAIL;
2304 goto error;
2305 }
2306 break;
2307 }
2308 case LTTNG_DOMAIN_LOG4J:
2309 case LTTNG_DOMAIN_JUL:
2310 case LTTNG_DOMAIN_PYTHON:
2311 default:
2312 ret = LTTNG_ERR_UND;
2313 goto error;
2314 }
2315
2316 return nr_pids;
2317
2318 error:
2319 /* Return negative value to differentiate return code */
2320 return -ret;
2321 }
2322
2323 /*
2324 * Command LTTNG_START_TRACE processed by the client thread.
2325 *
2326 * Called with session mutex held.
2327 */
2328 int cmd_start_trace(struct ltt_session *session)
2329 {
2330 int ret;
2331 unsigned long nb_chan = 0;
2332 struct ltt_kernel_session *ksession;
2333 struct ltt_ust_session *usess;
2334
2335 assert(session);
2336
2337 /* Ease our life a bit ;) */
2338 ksession = session->kernel_session;
2339 usess = session->ust_session;
2340
2341 /* Is the session already started? */
2342 if (session->active) {
2343 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2344 goto error;
2345 }
2346
2347 /*
2348 * Starting a session without channel is useless since after that it's not
2349 * possible to enable channel thus inform the client.
2350 */
2351 if (usess && usess->domain_global.channels) {
2352 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2353 }
2354 if (ksession) {
2355 nb_chan += ksession->channel_count;
2356 }
2357 if (!nb_chan) {
2358 ret = LTTNG_ERR_NO_CHANNEL;
2359 goto error;
2360 }
2361
2362 /* Kernel tracing */
2363 if (ksession != NULL) {
2364 ret = start_kernel_session(ksession, kernel_tracer_fd);
2365 if (ret != LTTNG_OK) {
2366 goto error;
2367 }
2368 }
2369
2370 /* Flag session that trace should start automatically */
2371 if (usess) {
2372 /*
2373 * Even though the start trace might fail, flag this session active so
2374 * other application coming in are started by default.
2375 */
2376 usess->active = 1;
2377
2378 ret = ust_app_start_trace_all(usess);
2379 if (ret < 0) {
2380 ret = LTTNG_ERR_UST_START_FAIL;
2381 goto error;
2382 }
2383 }
2384
2385 /* Flag this after a successful start. */
2386 session->has_been_started = 1;
2387 session->active = 1;
2388
2389 ret = LTTNG_OK;
2390
2391 error:
2392 return ret;
2393 }
2394
2395 /*
2396 * Command LTTNG_STOP_TRACE processed by the client thread.
2397 */
2398 int cmd_stop_trace(struct ltt_session *session)
2399 {
2400 int ret;
2401 struct ltt_kernel_channel *kchan;
2402 struct ltt_kernel_session *ksession;
2403 struct ltt_ust_session *usess;
2404
2405 assert(session);
2406
2407 /* Short cut */
2408 ksession = session->kernel_session;
2409 usess = session->ust_session;
2410
2411 /* Session is not active. Skip everythong and inform the client. */
2412 if (!session->active) {
2413 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2414 goto error;
2415 }
2416
2417 /* Kernel tracer */
2418 if (ksession && ksession->active) {
2419 DBG("Stop kernel tracing");
2420
2421 /* Flush metadata if exist */
2422 if (ksession->metadata_stream_fd >= 0) {
2423 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2424 if (ret < 0) {
2425 ERR("Kernel metadata flush failed");
2426 }
2427 }
2428
2429 /* Flush all buffers before stopping */
2430 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2431 ret = kernel_flush_buffer(kchan);
2432 if (ret < 0) {
2433 ERR("Kernel flush buffer error");
2434 }
2435 }
2436
2437 ret = kernel_stop_session(ksession);
2438 if (ret < 0) {
2439 ret = LTTNG_ERR_KERN_STOP_FAIL;
2440 goto error;
2441 }
2442
2443 kernel_wait_quiescent(kernel_tracer_fd);
2444
2445 ksession->active = 0;
2446 }
2447
2448 if (usess && usess->active) {
2449 /*
2450 * Even though the stop trace might fail, flag this session inactive so
2451 * other application coming in are not started by default.
2452 */
2453 usess->active = 0;
2454
2455 ret = ust_app_stop_trace_all(usess);
2456 if (ret < 0) {
2457 ret = LTTNG_ERR_UST_STOP_FAIL;
2458 goto error;
2459 }
2460 }
2461
2462 /* Flag inactive after a successful stop. */
2463 session->active = 0;
2464 ret = LTTNG_OK;
2465
2466 error:
2467 return ret;
2468 }
2469
2470 /*
2471 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2472 */
2473 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2474 struct lttng_uri *uris)
2475 {
2476 int ret, i;
2477 struct ltt_kernel_session *ksess = session->kernel_session;
2478 struct ltt_ust_session *usess = session->ust_session;
2479
2480 assert(session);
2481 assert(uris);
2482 assert(nb_uri > 0);
2483
2484 /* Can't set consumer URI if the session is active. */
2485 if (session->active) {
2486 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2487 goto error;
2488 }
2489
2490 /* Set the "global" consumer URIs */
2491 for (i = 0; i < nb_uri; i++) {
2492 ret = add_uri_to_consumer(session->consumer,
2493 &uris[i], 0, session->name);
2494 if (ret != LTTNG_OK) {
2495 goto error;
2496 }
2497 }
2498
2499 /* Set UST session URIs */
2500 if (session->ust_session) {
2501 for (i = 0; i < nb_uri; i++) {
2502 ret = add_uri_to_consumer(
2503 session->ust_session->consumer,
2504 &uris[i], LTTNG_DOMAIN_UST,
2505 session->name);
2506 if (ret != LTTNG_OK) {
2507 goto error;
2508 }
2509 }
2510 }
2511
2512 /* Set kernel session URIs */
2513 if (session->kernel_session) {
2514 for (i = 0; i < nb_uri; i++) {
2515 ret = add_uri_to_consumer(
2516 session->kernel_session->consumer,
2517 &uris[i], LTTNG_DOMAIN_KERNEL,
2518 session->name);
2519 if (ret != LTTNG_OK) {
2520 goto error;
2521 }
2522 }
2523 }
2524
2525 /*
2526 * Make sure to set the session in output mode after we set URI since a
2527 * session can be created without URL (thus flagged in no output mode).
2528 */
2529 session->output_traces = 1;
2530 if (ksess) {
2531 ksess->output_traces = 1;
2532 }
2533
2534 if (usess) {
2535 usess->output_traces = 1;
2536 }
2537
2538 /* All good! */
2539 ret = LTTNG_OK;
2540
2541 error:
2542 return ret;
2543 }
2544
2545 /*
2546 * Command LTTNG_CREATE_SESSION processed by the client thread.
2547 */
2548 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
2549 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2550 {
2551 int ret;
2552 struct ltt_session *session;
2553
2554 assert(name);
2555 assert(creds);
2556
2557 /*
2558 * Verify if the session already exist
2559 *
2560 * XXX: There is no need for the session lock list here since the caller
2561 * (process_client_msg) is holding it. We might want to change that so a
2562 * single command does not lock the entire session list.
2563 */
2564 session = session_find_by_name(name);
2565 if (session != NULL) {
2566 ret = LTTNG_ERR_EXIST_SESS;
2567 goto find_error;
2568 }
2569
2570 /* Create tracing session in the registry */
2571 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2572 LTTNG_SOCK_GET_GID_CRED(creds));
2573 if (ret != LTTNG_OK) {
2574 goto session_error;
2575 }
2576
2577 /*
2578 * Get the newly created session pointer back
2579 *
2580 * XXX: There is no need for the session lock list here since the caller
2581 * (process_client_msg) is holding it. We might want to change that so a
2582 * single command does not lock the entire session list.
2583 */
2584 session = session_find_by_name(name);
2585 assert(session);
2586
2587 session->live_timer = live_timer;
2588 /* Create default consumer output for the session not yet created. */
2589 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2590 if (session->consumer == NULL) {
2591 ret = LTTNG_ERR_FATAL;
2592 goto consumer_error;
2593 }
2594
2595 if (uris) {
2596 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2597 if (ret != LTTNG_OK) {
2598 goto consumer_error;
2599 }
2600 session->output_traces = 1;
2601 } else {
2602 session->output_traces = 0;
2603 DBG2("Session %s created with no output", session->name);
2604 }
2605
2606 session->consumer->enabled = 1;
2607
2608 return LTTNG_OK;
2609
2610 consumer_error:
2611 session_destroy(session);
2612 session_error:
2613 find_error:
2614 return ret;
2615 }
2616
2617 /*
2618 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2619 */
2620 int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2621 size_t nb_uri, lttng_sock_cred *creds)
2622 {
2623 int ret;
2624 struct ltt_session *session;
2625 struct snapshot_output *new_output = NULL;
2626
2627 assert(name);
2628 assert(creds);
2629
2630 /*
2631 * Create session in no output mode with URIs set to NULL. The uris we've
2632 * received are for a default snapshot output if one.
2633 */
2634 ret = cmd_create_session_uri(name, NULL, 0, creds, 0);
2635 if (ret != LTTNG_OK) {
2636 goto error;
2637 }
2638
2639 /* Get the newly created session pointer back. This should NEVER fail. */
2640 session = session_find_by_name(name);
2641 assert(session);
2642
2643 /* Flag session for snapshot mode. */
2644 session->snapshot_mode = 1;
2645
2646 /* Skip snapshot output creation if no URI is given. */
2647 if (nb_uri == 0) {
2648 goto end;
2649 }
2650
2651 new_output = snapshot_output_alloc();
2652 if (!new_output) {
2653 ret = LTTNG_ERR_NOMEM;
2654 goto error_snapshot_alloc;
2655 }
2656
2657 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2658 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2659 if (ret < 0) {
2660 if (ret == -ENOMEM) {
2661 ret = LTTNG_ERR_NOMEM;
2662 } else {
2663 ret = LTTNG_ERR_INVALID;
2664 }
2665 goto error_snapshot;
2666 }
2667
2668 rcu_read_lock();
2669 snapshot_add_output(&session->snapshot, new_output);
2670 rcu_read_unlock();
2671
2672 end:
2673 return LTTNG_OK;
2674
2675 error_snapshot:
2676 snapshot_output_destroy(new_output);
2677 error_snapshot_alloc:
2678 session_destroy(session);
2679 error:
2680 return ret;
2681 }
2682
2683 /*
2684 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2685 *
2686 * Called with session lock held.
2687 */
2688 int cmd_destroy_session(struct ltt_session *session, int wpipe)
2689 {
2690 int ret;
2691 struct ltt_ust_session *usess;
2692 struct ltt_kernel_session *ksess;
2693
2694 /* Safety net */
2695 assert(session);
2696
2697 usess = session->ust_session;
2698 ksess = session->kernel_session;
2699
2700 /* Clean kernel session teardown */
2701 kernel_destroy_session(ksess);
2702
2703 /* UST session teardown */
2704 if (usess) {
2705 /* Close any relayd session */
2706 consumer_output_send_destroy_relayd(usess->consumer);
2707
2708 /* Destroy every UST application related to this session. */
2709 ret = ust_app_destroy_trace_all(usess);
2710 if (ret) {
2711 ERR("Error in ust_app_destroy_trace_all");
2712 }
2713
2714 /* Clean up the rest. */
2715 trace_ust_destroy_session(usess);
2716 }
2717
2718 /*
2719 * Must notify the kernel thread here to update it's poll set in order to
2720 * remove the channel(s)' fd just destroyed.
2721 */
2722 ret = notify_thread_pipe(wpipe);
2723 if (ret < 0) {
2724 PERROR("write kernel poll pipe");
2725 }
2726
2727 ret = session_destroy(session);
2728
2729 return ret;
2730 }
2731
2732 /*
2733 * Command LTTNG_CALIBRATE processed by the client thread.
2734 */
2735 int cmd_calibrate(enum lttng_domain_type domain,
2736 struct lttng_calibrate *calibrate)
2737 {
2738 int ret;
2739
2740 switch (domain) {
2741 case LTTNG_DOMAIN_KERNEL:
2742 {
2743 struct lttng_kernel_calibrate kcalibrate;
2744
2745 switch (calibrate->type) {
2746 case LTTNG_CALIBRATE_FUNCTION:
2747 default:
2748 /* Default and only possible calibrate option. */
2749 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2750 break;
2751 }
2752
2753 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2754 if (ret < 0) {
2755 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2756 goto error;
2757 }
2758 break;
2759 }
2760 case LTTNG_DOMAIN_UST:
2761 {
2762 struct lttng_ust_calibrate ucalibrate;
2763
2764 switch (calibrate->type) {
2765 case LTTNG_CALIBRATE_FUNCTION:
2766 default:
2767 /* Default and only possible calibrate option. */
2768 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2769 break;
2770 }
2771
2772 ret = ust_app_calibrate_glb(&ucalibrate);
2773 if (ret < 0) {
2774 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2775 goto error;
2776 }
2777 break;
2778 }
2779 default:
2780 ret = LTTNG_ERR_UND;
2781 goto error;
2782 }
2783
2784 ret = LTTNG_OK;
2785
2786 error:
2787 return ret;
2788 }
2789
2790 /*
2791 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2792 */
2793 int cmd_register_consumer(struct ltt_session *session,
2794 enum lttng_domain_type domain, const char *sock_path,
2795 struct consumer_data *cdata)
2796 {
2797 int ret, sock;
2798 struct consumer_socket *socket = NULL;
2799
2800 assert(session);
2801 assert(cdata);
2802 assert(sock_path);
2803
2804 switch (domain) {
2805 case LTTNG_DOMAIN_KERNEL:
2806 {
2807 struct ltt_kernel_session *ksess = session->kernel_session;
2808
2809 assert(ksess);
2810
2811 /* Can't register a consumer if there is already one */
2812 if (ksess->consumer_fds_sent != 0) {
2813 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2814 goto error;
2815 }
2816
2817 sock = lttcomm_connect_unix_sock(sock_path);
2818 if (sock < 0) {
2819 ret = LTTNG_ERR_CONNECT_FAIL;
2820 goto error;
2821 }
2822 cdata->cmd_sock = sock;
2823
2824 socket = consumer_allocate_socket(&cdata->cmd_sock);
2825 if (socket == NULL) {
2826 ret = close(sock);
2827 if (ret < 0) {
2828 PERROR("close register consumer");
2829 }
2830 cdata->cmd_sock = -1;
2831 ret = LTTNG_ERR_FATAL;
2832 goto error;
2833 }
2834
2835 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2836 if (socket->lock == NULL) {
2837 PERROR("zmalloc pthread mutex");
2838 ret = LTTNG_ERR_FATAL;
2839 goto error;
2840 }
2841 pthread_mutex_init(socket->lock, NULL);
2842 socket->registered = 1;
2843
2844 rcu_read_lock();
2845 consumer_add_socket(socket, ksess->consumer);
2846 rcu_read_unlock();
2847
2848 pthread_mutex_lock(&cdata->pid_mutex);
2849 cdata->pid = -1;
2850 pthread_mutex_unlock(&cdata->pid_mutex);
2851
2852 break;
2853 }
2854 default:
2855 /* TODO: Userspace tracing */
2856 ret = LTTNG_ERR_UND;
2857 goto error;
2858 }
2859
2860 return LTTNG_OK;
2861
2862 error:
2863 if (socket) {
2864 consumer_destroy_socket(socket);
2865 }
2866 return ret;
2867 }
2868
2869 /*
2870 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2871 */
2872 ssize_t cmd_list_domains(struct ltt_session *session,
2873 struct lttng_domain **domains)
2874 {
2875 int ret, index = 0;
2876 ssize_t nb_dom = 0;
2877 struct agent *agt;
2878 struct lttng_ht_iter iter;
2879
2880 if (session->kernel_session != NULL) {
2881 DBG3("Listing domains found kernel domain");
2882 nb_dom++;
2883 }
2884
2885 if (session->ust_session != NULL) {
2886 DBG3("Listing domains found UST global domain");
2887 nb_dom++;
2888
2889 rcu_read_lock();
2890 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2891 agt, node.node) {
2892 if (agt->being_used) {
2893 nb_dom++;
2894 }
2895 }
2896 rcu_read_unlock();
2897 }
2898
2899 if (!nb_dom) {
2900 goto end;
2901 }
2902
2903 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2904 if (*domains == NULL) {
2905 ret = LTTNG_ERR_FATAL;
2906 goto error;
2907 }
2908
2909 if (session->kernel_session != NULL) {
2910 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2911
2912 /* Kernel session buffer type is always GLOBAL */
2913 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
2914
2915 index++;
2916 }
2917
2918 if (session->ust_session != NULL) {
2919 (*domains)[index].type = LTTNG_DOMAIN_UST;
2920 (*domains)[index].buf_type = session->ust_session->buffer_type;
2921 index++;
2922
2923 rcu_read_lock();
2924 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2925 agt, node.node) {
2926 if (agt->being_used) {
2927 (*domains)[index].type = agt->domain;
2928 (*domains)[index].buf_type = session->ust_session->buffer_type;
2929 index++;
2930 }
2931 }
2932 rcu_read_unlock();
2933 }
2934 end:
2935 return nb_dom;
2936
2937 error:
2938 /* Return negative value to differentiate return code */
2939 return -ret;
2940 }
2941
2942
2943 /*
2944 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2945 */
2946 ssize_t cmd_list_channels(enum lttng_domain_type domain,
2947 struct ltt_session *session, struct lttng_channel **channels)
2948 {
2949 ssize_t nb_chan = 0, payload_size = 0, ret;
2950
2951 switch (domain) {
2952 case LTTNG_DOMAIN_KERNEL:
2953 if (session->kernel_session != NULL) {
2954 nb_chan = session->kernel_session->channel_count;
2955 }
2956 DBG3("Number of kernel channels %zd", nb_chan);
2957 if (nb_chan <= 0) {
2958 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2959 goto end;
2960 }
2961 break;
2962 case LTTNG_DOMAIN_UST:
2963 if (session->ust_session != NULL) {
2964 rcu_read_lock();
2965 nb_chan = lttng_ht_get_count(
2966 session->ust_session->domain_global.channels);
2967 rcu_read_unlock();
2968 }
2969 DBG3("Number of UST global channels %zd", nb_chan);
2970 if (nb_chan < 0) {
2971 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
2972 goto end;
2973 }
2974 break;
2975 default:
2976 ret = -LTTNG_ERR_UND;
2977 goto end;
2978 }
2979
2980 if (nb_chan > 0) {
2981 const size_t channel_size = sizeof(struct lttng_channel) +
2982 sizeof(struct lttcomm_channel_extended);
2983 struct lttcomm_channel_extended *channel_exts;
2984
2985 payload_size = nb_chan * channel_size;
2986 *channels = zmalloc(payload_size);
2987 if (*channels == NULL) {
2988 ret = -LTTNG_ERR_FATAL;
2989 goto end;
2990 }
2991
2992 channel_exts = ((void *) *channels) +
2993 (nb_chan * sizeof(struct lttng_channel));
2994 list_lttng_channels(domain, session, *channels, channel_exts);
2995 } else {
2996 *channels = NULL;
2997 }
2998
2999 ret = payload_size;
3000 end:
3001 return ret;
3002 }
3003
3004 /*
3005 * Command LTTNG_LIST_EVENTS processed by the client thread.
3006 */
3007 ssize_t cmd_list_events(enum lttng_domain_type domain,
3008 struct ltt_session *session, char *channel_name,
3009 struct lttng_event **events, size_t *total_size)
3010 {
3011 int ret = 0;
3012 ssize_t nb_event = 0;
3013
3014 switch (domain) {
3015 case LTTNG_DOMAIN_KERNEL:
3016 if (session->kernel_session != NULL) {
3017 nb_event = list_lttng_kernel_events(channel_name,
3018 session->kernel_session, events,
3019 total_size);
3020 }
3021 break;
3022 case LTTNG_DOMAIN_UST:
3023 {
3024 if (session->ust_session != NULL) {
3025 nb_event = list_lttng_ust_global_events(channel_name,
3026 &session->ust_session->domain_global, events,
3027 total_size);
3028 }
3029 break;
3030 }
3031 case LTTNG_DOMAIN_LOG4J:
3032 case LTTNG_DOMAIN_JUL:
3033 case LTTNG_DOMAIN_PYTHON:
3034 if (session->ust_session) {
3035 struct lttng_ht_iter iter;
3036 struct agent *agt;
3037
3038 rcu_read_lock();
3039 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3040 &iter.iter, agt, node.node) {
3041 if (agt->domain == domain) {
3042 nb_event = list_lttng_agent_events(
3043 agt, events,
3044 total_size);
3045 break;
3046 }
3047 }
3048 rcu_read_unlock();
3049 }
3050 break;
3051 default:
3052 ret = LTTNG_ERR_UND;
3053 goto error;
3054 }
3055
3056 return nb_event;
3057
3058 error:
3059 /* Return negative value to differentiate return code */
3060 return -ret;
3061 }
3062
3063 /*
3064 * Using the session list, filled a lttng_session array to send back to the
3065 * client for session listing.
3066 *
3067 * The session list lock MUST be acquired before calling this function. Use
3068 * session_lock_list() and session_unlock_list().
3069 */
3070 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
3071 gid_t gid)
3072 {
3073 int ret;
3074 unsigned int i = 0;
3075 struct ltt_session *session;
3076 struct ltt_session_list *list = session_get_list();
3077
3078 DBG("Getting all available session for UID %d GID %d",
3079 uid, gid);
3080 /*
3081 * Iterate over session list and append data after the control struct in
3082 * the buffer.
3083 */
3084 cds_list_for_each_entry(session, &list->head, list) {
3085 /*
3086 * Only list the sessions the user can control.
3087 */
3088 if (!session_access_ok(session, uid, gid)) {
3089 continue;
3090 }
3091
3092 struct ltt_kernel_session *ksess = session->kernel_session;
3093 struct ltt_ust_session *usess = session->ust_session;
3094
3095 if (session->consumer->type == CONSUMER_DST_NET ||
3096 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3097 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3098 ret = build_network_session_path(sessions[i].path,
3099 sizeof(sessions[i].path), session);
3100 } else {
3101 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
3102 session->consumer->dst.trace_path);
3103 }
3104 if (ret < 0) {
3105 PERROR("snprintf session path");
3106 continue;
3107 }
3108
3109 strncpy(sessions[i].name, session->name, NAME_MAX);
3110 sessions[i].name[NAME_MAX - 1] = '\0';
3111 sessions[i].enabled = session->active;
3112 sessions[i].snapshot_mode = session->snapshot_mode;
3113 sessions[i].live_timer_interval = session->live_timer;
3114 i++;
3115 }
3116 }
3117
3118 /*
3119 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3120 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3121 */
3122 int cmd_data_pending(struct ltt_session *session)
3123 {
3124 int ret;
3125 struct ltt_kernel_session *ksess = session->kernel_session;
3126 struct ltt_ust_session *usess = session->ust_session;
3127
3128 assert(session);
3129
3130 /* Session MUST be stopped to ask for data availability. */
3131 if (session->active) {
3132 ret = LTTNG_ERR_SESSION_STARTED;
3133 goto error;
3134 } else {
3135 /*
3136 * If stopped, just make sure we've started before else the above call
3137 * will always send that there is data pending.
3138 *
3139 * The consumer assumes that when the data pending command is received,
3140 * the trace has been started before or else no output data is written
3141 * by the streams which is a condition for data pending. So, this is
3142 * *VERY* important that we don't ask the consumer before a start
3143 * trace.
3144 */
3145 if (!session->has_been_started) {
3146 ret = 0;
3147 goto error;
3148 }
3149 }
3150
3151 if (ksess && ksess->consumer) {
3152 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3153 if (ret == 1) {
3154 /* Data is still being extracted for the kernel. */
3155 goto error;
3156 }
3157 }
3158
3159 if (usess && usess->consumer) {
3160 ret = consumer_is_data_pending(usess->id, usess->consumer);
3161 if (ret == 1) {
3162 /* Data is still being extracted for the kernel. */
3163 goto error;
3164 }
3165 }
3166
3167 /* Data is ready to be read by a viewer */
3168 ret = 0;
3169
3170 error:
3171 return ret;
3172 }
3173
3174 /*
3175 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3176 *
3177 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3178 */
3179 int cmd_snapshot_add_output(struct ltt_session *session,
3180 struct lttng_snapshot_output *output, uint32_t *id)
3181 {
3182 int ret;
3183 struct snapshot_output *new_output;
3184
3185 assert(session);
3186 assert(output);
3187
3188 DBG("Cmd snapshot add output for session %s", session->name);
3189
3190 /*
3191 * Permission denied to create an output if the session is not
3192 * set in no output mode.
3193 */
3194 if (session->output_traces) {
3195 ret = LTTNG_ERR_EPERM;
3196 goto error;
3197 }
3198
3199 /* Only one output is allowed until we have the "tee" feature. */
3200 if (session->snapshot.nb_output == 1) {
3201 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3202 goto error;
3203 }
3204
3205 new_output = snapshot_output_alloc();
3206 if (!new_output) {
3207 ret = LTTNG_ERR_NOMEM;
3208 goto error;
3209 }
3210
3211 ret = snapshot_output_init(output->max_size, output->name,
3212 output->ctrl_url, output->data_url, session->consumer, new_output,
3213 &session->snapshot);
3214 if (ret < 0) {
3215 if (ret == -ENOMEM) {
3216 ret = LTTNG_ERR_NOMEM;
3217 } else {
3218 ret = LTTNG_ERR_INVALID;
3219 }
3220 goto free_error;
3221 }
3222
3223 rcu_read_lock();
3224 snapshot_add_output(&session->snapshot, new_output);
3225 if (id) {
3226 *id = new_output->id;
3227 }
3228 rcu_read_unlock();
3229
3230 return LTTNG_OK;
3231
3232 free_error:
3233 snapshot_output_destroy(new_output);
3234 error:
3235 return ret;
3236 }
3237
3238 /*
3239 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3240 *
3241 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3242 */
3243 int cmd_snapshot_del_output(struct ltt_session *session,
3244 struct lttng_snapshot_output *output)
3245 {
3246 int ret;
3247 struct snapshot_output *sout = NULL;
3248
3249 assert(session);
3250 assert(output);
3251
3252 rcu_read_lock();
3253
3254 /*
3255 * Permission denied to create an output if the session is not
3256 * set in no output mode.
3257 */
3258 if (session->output_traces) {
3259 ret = LTTNG_ERR_EPERM;
3260 goto error;
3261 }
3262
3263 if (output->id) {
3264 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3265 session->name);
3266 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3267 } else if (*output->name != '\0') {
3268 DBG("Cmd snapshot del output name %s for session %s", output->name,
3269 session->name);
3270 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3271 }
3272 if (!sout) {
3273 ret = LTTNG_ERR_INVALID;
3274 goto error;
3275 }
3276
3277 snapshot_delete_output(&session->snapshot, sout);
3278 snapshot_output_destroy(sout);
3279 ret = LTTNG_OK;
3280
3281 error:
3282 rcu_read_unlock();
3283 return ret;
3284 }
3285
3286 /*
3287 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3288 *
3289 * If no output is available, outputs is untouched and 0 is returned.
3290 *
3291 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3292 */
3293 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3294 struct lttng_snapshot_output **outputs)
3295 {
3296 int ret, idx = 0;
3297 struct lttng_snapshot_output *list = NULL;
3298 struct lttng_ht_iter iter;
3299 struct snapshot_output *output;
3300
3301 assert(session);
3302 assert(outputs);
3303
3304 DBG("Cmd snapshot list outputs for session %s", session->name);
3305
3306 /*
3307 * Permission denied to create an output if the session is not
3308 * set in no output mode.
3309 */
3310 if (session->output_traces) {
3311 ret = -LTTNG_ERR_EPERM;
3312 goto error;
3313 }
3314
3315 if (session->snapshot.nb_output == 0) {
3316 ret = 0;
3317 goto error;
3318 }
3319
3320 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
3321 if (!list) {
3322 ret = -LTTNG_ERR_NOMEM;
3323 goto error;
3324 }
3325
3326 /* Copy list from session to the new list object. */
3327 rcu_read_lock();
3328 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
3329 output, node.node) {
3330 assert(output->consumer);
3331 list[idx].id = output->id;
3332 list[idx].max_size = output->max_size;
3333 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
3334 if (output->consumer->type == CONSUMER_DST_LOCAL) {
3335 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
3336 sizeof(list[idx].ctrl_url));
3337 } else {
3338 /* Control URI. */
3339 ret = uri_to_str_url(&output->consumer->dst.net.control,
3340 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
3341 if (ret < 0) {
3342 ret = -LTTNG_ERR_NOMEM;
3343 goto error;
3344 }
3345
3346 /* Data URI. */
3347 ret = uri_to_str_url(&output->consumer->dst.net.data,
3348 list[idx].data_url, sizeof(list[idx].data_url));
3349 if (ret < 0) {
3350 ret = -LTTNG_ERR_NOMEM;
3351 goto error;
3352 }
3353 }
3354 idx++;
3355 }
3356
3357 *outputs = list;
3358 list = NULL;
3359 ret = session->snapshot.nb_output;
3360 error:
3361 free(list);
3362 rcu_read_unlock();
3363 return ret;
3364 }
3365
3366 /*
3367 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3368 * snapshot output is *not* set with a remote destination.
3369 *
3370 * Return 0 on success or a LTTNG_ERR code.
3371 */
3372 static int set_relayd_for_snapshot(struct consumer_output *consumer,
3373 struct snapshot_output *snap_output, struct ltt_session *session)
3374 {
3375 int ret = LTTNG_OK;
3376 struct lttng_ht_iter iter;
3377 struct consumer_socket *socket;
3378
3379 assert(consumer);
3380 assert(snap_output);
3381 assert(session);
3382
3383 DBG2("Set relayd object from snapshot output");
3384
3385 /* Ignore if snapshot consumer output is not network. */
3386 if (snap_output->consumer->type != CONSUMER_DST_NET) {
3387 goto error;
3388 }
3389
3390 /*
3391 * For each consumer socket, create and send the relayd object of the
3392 * snapshot output.
3393 */
3394 rcu_read_lock();
3395 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
3396 socket, node.node) {
3397 ret = send_consumer_relayd_sockets(0, session->id,
3398 snap_output->consumer, socket,
3399 session->name, session->hostname,
3400 session->live_timer);
3401 if (ret != LTTNG_OK) {
3402 rcu_read_unlock();
3403 goto error;
3404 }
3405 }
3406 rcu_read_unlock();
3407
3408 error:
3409 return ret;
3410 }
3411
3412 /*
3413 * Record a kernel snapshot.
3414 *
3415 * Return LTTNG_OK on success or a LTTNG_ERR code.
3416 */
3417 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
3418 struct snapshot_output *output, struct ltt_session *session,
3419 int wait, uint64_t nb_packets_per_stream)
3420 {
3421 int ret;
3422
3423 assert(ksess);
3424 assert(output);
3425 assert(session);
3426
3427 /* Get the datetime for the snapshot output directory. */
3428 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
3429 sizeof(output->datetime));
3430 if (!ret) {
3431 ret = LTTNG_ERR_INVALID;
3432 goto error;
3433 }
3434
3435 /*
3436 * Copy kernel session sockets so we can communicate with the right
3437 * consumer for the snapshot record command.
3438 */
3439 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
3440 if (ret < 0) {
3441 ret = LTTNG_ERR_NOMEM;
3442 goto error;
3443 }
3444
3445 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
3446 if (ret != LTTNG_OK) {
3447 goto error_snapshot;
3448 }
3449
3450 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
3451 if (ret != LTTNG_OK) {
3452 goto error_snapshot;
3453 }
3454
3455 ret = LTTNG_OK;
3456 goto end;
3457
3458 error_snapshot:
3459 /* Clean up copied sockets so this output can use some other later on. */
3460 consumer_destroy_output_sockets(output->consumer);
3461 error:
3462 end:
3463 return ret;
3464 }
3465
3466 /*
3467 * Record a UST snapshot.
3468 *
3469 * Return 0 on success or a LTTNG_ERR error code.
3470 */
3471 static int record_ust_snapshot(struct ltt_ust_session *usess,
3472 struct snapshot_output *output, struct ltt_session *session,
3473 int wait, uint64_t nb_packets_per_stream)
3474 {
3475 int ret;
3476
3477 assert(usess);
3478 assert(output);
3479 assert(session);
3480
3481 /* Get the datetime for the snapshot output directory. */
3482 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
3483 sizeof(output->datetime));
3484 if (!ret) {
3485 ret = LTTNG_ERR_INVALID;
3486 goto error;
3487 }
3488
3489 /*
3490 * Copy UST session sockets so we can communicate with the right
3491 * consumer for the snapshot record command.
3492 */
3493 ret = consumer_copy_sockets(output->consumer, usess->consumer);
3494 if (ret < 0) {
3495 ret = LTTNG_ERR_NOMEM;
3496 goto error;
3497 }
3498
3499 ret = set_relayd_for_snapshot(usess->consumer, output, session);
3500 if (ret != LTTNG_OK) {
3501 goto error_snapshot;
3502 }
3503
3504 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
3505 if (ret < 0) {
3506 switch (-ret) {
3507 case EINVAL:
3508 ret = LTTNG_ERR_INVALID;
3509 break;
3510 case ENODATA:
3511 ret = LTTNG_ERR_SNAPSHOT_NODATA;
3512 break;
3513 default:
3514 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3515 break;
3516 }
3517 goto error_snapshot;
3518 }
3519
3520 ret = LTTNG_OK;
3521
3522 error_snapshot:
3523 /* Clean up copied sockets so this output can use some other later on. */
3524 consumer_destroy_output_sockets(output->consumer);
3525 error:
3526 return ret;
3527 }
3528
3529 static
3530 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
3531 uint64_t cur_nr_packets)
3532 {
3533 uint64_t tot_size = 0;
3534
3535 if (session->kernel_session) {
3536 struct ltt_kernel_channel *chan;
3537 struct ltt_kernel_session *ksess = session->kernel_session;
3538
3539 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
3540 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
3541 /*
3542 * Don't take channel into account if we
3543 * already grab all its packets.
3544 */
3545 continue;
3546 }
3547 tot_size += chan->channel->attr.subbuf_size
3548 * chan->stream_count;
3549 }
3550 }
3551
3552 if (session->ust_session) {
3553 struct ltt_ust_session *usess = session->ust_session;
3554
3555 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
3556 cur_nr_packets);
3557 }
3558
3559 return tot_size;
3560 }
3561
3562 /*
3563 * Calculate the number of packets we can grab from each stream that
3564 * fits within the overall snapshot max size.
3565 *
3566 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3567 * the number of packets per stream.
3568 *
3569 * TODO: this approach is not perfect: we consider the worse case
3570 * (packet filling the sub-buffers) as an upper bound, but we could do
3571 * better if we do this calculation while we actually grab the packet
3572 * content: we would know how much padding we don't actually store into
3573 * the file.
3574 *
3575 * This algorithm is currently bounded by the number of packets per
3576 * stream.
3577 *
3578 * Since we call this algorithm before actually grabbing the data, it's
3579 * an approximation: for instance, applications could appear/disappear
3580 * in between this call and actually grabbing data.
3581 */
3582 static
3583 int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
3584 {
3585 int64_t size_left;
3586 uint64_t cur_nb_packets = 0;
3587
3588 if (!max_size) {
3589 return 0; /* Infinite */
3590 }
3591
3592 size_left = max_size;
3593 for (;;) {
3594 uint64_t one_more_packet_tot_size;
3595
3596 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3597 cur_nb_packets);
3598 if (!one_more_packet_tot_size) {
3599 /* We are already grabbing all packets. */
3600 break;
3601 }
3602 size_left -= one_more_packet_tot_size;
3603 if (size_left < 0) {
3604 break;
3605 }
3606 cur_nb_packets++;
3607 }
3608 if (!cur_nb_packets) {
3609 /* Not enough room to grab one packet of each stream, error. */
3610 return -1;
3611 }
3612 return cur_nb_packets;
3613 }
3614
3615 /*
3616 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3617 *
3618 * The wait parameter is ignored so this call always wait for the snapshot to
3619 * complete before returning.
3620 *
3621 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3622 */
3623 int cmd_snapshot_record(struct ltt_session *session,
3624 struct lttng_snapshot_output *output, int wait)
3625 {
3626 int ret = LTTNG_OK;
3627 unsigned int use_tmp_output = 0;
3628 struct snapshot_output tmp_output;
3629 unsigned int snapshot_success = 0;
3630
3631 assert(session);
3632 assert(output);
3633
3634 DBG("Cmd snapshot record for session %s", session->name);
3635
3636 /*
3637 * Permission denied to create an output if the session is not
3638 * set in no output mode.
3639 */
3640 if (session->output_traces) {
3641 ret = LTTNG_ERR_EPERM;
3642 goto error;
3643 }
3644
3645 /* The session needs to be started at least once. */
3646 if (!session->has_been_started) {
3647 ret = LTTNG_ERR_START_SESSION_ONCE;
3648 goto error;
3649 }
3650
3651 /* Use temporary output for the session. */
3652 if (*output->ctrl_url != '\0') {
3653 ret = snapshot_output_init(output->max_size, output->name,
3654 output->ctrl_url, output->data_url, session->consumer,
3655 &tmp_output, NULL);
3656 if (ret < 0) {
3657 if (ret == -ENOMEM) {
3658 ret = LTTNG_ERR_NOMEM;
3659 } else {
3660 ret = LTTNG_ERR_INVALID;
3661 }
3662 goto error;
3663 }
3664 /* Use the global session count for the temporary snapshot. */
3665 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3666 use_tmp_output = 1;
3667 }
3668
3669 if (session->kernel_session) {
3670 struct ltt_kernel_session *ksess = session->kernel_session;
3671
3672 if (use_tmp_output) {
3673 int64_t nb_packets_per_stream;
3674
3675 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3676 tmp_output.max_size);
3677 if (nb_packets_per_stream < 0) {
3678 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3679 goto error;
3680 }
3681 ret = record_kernel_snapshot(ksess, &tmp_output, session,
3682 wait, nb_packets_per_stream);
3683 if (ret != LTTNG_OK) {
3684 goto error;
3685 }
3686 snapshot_success = 1;
3687 } else {
3688 struct snapshot_output *sout;
3689 struct lttng_ht_iter iter;
3690
3691 rcu_read_lock();
3692 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3693 &iter.iter, sout, node.node) {
3694 int64_t nb_packets_per_stream;
3695
3696 /*
3697 * Make a local copy of the output and assign the possible
3698 * temporary value given by the caller.
3699 */
3700 memset(&tmp_output, 0, sizeof(tmp_output));
3701 memcpy(&tmp_output, sout, sizeof(tmp_output));
3702
3703 if (output->max_size != (uint64_t) -1ULL) {
3704 tmp_output.max_size = output->max_size;
3705 }
3706
3707 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3708 tmp_output.max_size);
3709 if (nb_packets_per_stream < 0) {
3710 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3711 goto error;
3712 }
3713
3714 /* Use temporary name. */
3715 if (*output->name != '\0') {
3716 strncpy(tmp_output.name, output->name,
3717 sizeof(tmp_output.name));
3718 }
3719
3720 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3721
3722 ret = record_kernel_snapshot(ksess, &tmp_output,
3723 session, wait, nb_packets_per_stream);
3724 if (ret != LTTNG_OK) {
3725 rcu_read_unlock();
3726 goto error;
3727 }
3728 snapshot_success = 1;
3729 }
3730 rcu_read_unlock();
3731 }
3732 }
3733
3734 if (session->ust_session) {
3735 struct ltt_ust_session *usess = session->ust_session;
3736
3737 if (use_tmp_output) {
3738 int64_t nb_packets_per_stream;
3739
3740 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3741 tmp_output.max_size);
3742 if (nb_packets_per_stream < 0) {
3743 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3744 goto error;
3745 }
3746 ret = record_ust_snapshot(usess, &tmp_output, session,
3747 wait, nb_packets_per_stream);
3748 if (ret != LTTNG_OK) {
3749 goto error;
3750 }
3751 snapshot_success = 1;
3752 } else {
3753 struct snapshot_output *sout;
3754 struct lttng_ht_iter iter;
3755
3756 rcu_read_lock();
3757 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3758 &iter.iter, sout, node.node) {
3759 int64_t nb_packets_per_stream;
3760
3761 /*
3762 * Make a local copy of the output and assign the possible
3763 * temporary value given by the caller.
3764 */
3765 memset(&tmp_output, 0, sizeof(tmp_output));
3766 memcpy(&tmp_output, sout, sizeof(tmp_output));
3767
3768 if (output->max_size != (uint64_t) -1ULL) {
3769 tmp_output.max_size = output->max_size;
3770 }
3771
3772 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3773 tmp_output.max_size);
3774 if (nb_packets_per_stream < 0) {
3775 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3776 rcu_read_unlock();
3777 goto error;
3778 }
3779
3780 /* Use temporary name. */
3781 if (*output->name != '\0') {
3782 strncpy(tmp_output.name, output->name,
3783 sizeof(tmp_output.name));
3784 }
3785
3786 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3787
3788 ret = record_ust_snapshot(usess, &tmp_output, session,
3789 wait, nb_packets_per_stream);
3790 if (ret != LTTNG_OK) {
3791 rcu_read_unlock();
3792 goto error;
3793 }
3794 snapshot_success = 1;
3795 }
3796 rcu_read_unlock();
3797 }
3798 }
3799
3800 if (snapshot_success) {
3801 session->snapshot.nb_snapshot++;
3802 } else {
3803 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3804 }
3805
3806 error:
3807 return ret;
3808 }
3809
3810 /*
3811 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3812 */
3813 int cmd_set_session_shm_path(struct ltt_session *session,
3814 const char *shm_path)
3815 {
3816 /* Safety net */
3817 assert(session);
3818
3819 /*
3820 * Can only set shm path before session is started.
3821 */
3822 if (session->has_been_started) {
3823 return LTTNG_ERR_SESSION_STARTED;
3824 }
3825
3826 strncpy(session->shm_path, shm_path,
3827 sizeof(session->shm_path));
3828 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
3829
3830 return 0;
3831 }
3832
3833 /*
3834 * Init command subsystem.
3835 */
3836 void cmd_init(void)
3837 {
3838 /*
3839 * Set network sequence index to 1 for streams to match a relayd
3840 * socket on the consumer side.
3841 */
3842 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3843 relayd_net_seq_idx = 1;
3844 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
3845
3846 DBG("Command subsystem initialized");
3847 }
This page took 0.17 seconds and 4 git commands to generate.