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