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