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