Fix: deny overwrite mode and num subbuf less than 2
[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 /*
937 * The ringbuffer (both in user space and kernel) behave badly in overwrite
938 * mode and with less than 2 subbuf so block it right away and send back an
939 * invalid attribute error.
940 */
941 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
942 ret = LTTNG_ERR_INVALID;
943 goto error;
944 }
945
946 switch (domain->type) {
947 case LTTNG_DOMAIN_KERNEL:
948 {
949 struct ltt_kernel_channel *kchan;
950
951 kchan = trace_kernel_get_channel_by_name(attr->name,
952 session->kernel_session);
953 if (kchan == NULL) {
954 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
955 if (attr->name[0] != '\0') {
956 session->kernel_session->has_non_default_channel = 1;
957 }
958 } else {
959 ret = channel_kernel_enable(session->kernel_session, kchan);
960 }
961
962 if (ret != LTTNG_OK) {
963 goto error;
964 }
965
966 kernel_wait_quiescent(kernel_tracer_fd);
967 break;
968 }
969 case LTTNG_DOMAIN_UST:
970 {
971 struct ltt_ust_channel *uchan;
972
973 chan_ht = usess->domain_global.channels;
974
975 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
976 if (uchan == NULL) {
977 ret = channel_ust_create(usess, attr, domain->buf_type);
978 if (attr->name[0] != '\0') {
979 usess->has_non_default_channel = 1;
980 }
981 } else {
982 ret = channel_ust_enable(usess, uchan);
983 }
984 break;
985 }
986 default:
987 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
988 goto error;
989 }
990
991 error:
992 rcu_read_unlock();
993 return ret;
994 }
995
996
997 /*
998 * Command LTTNG_DISABLE_EVENT processed by the client thread.
999 */
1000 int cmd_disable_event(struct ltt_session *session, int domain,
1001 char *channel_name, char *event_name)
1002 {
1003 int ret;
1004
1005 rcu_read_lock();
1006
1007 switch (domain) {
1008 case LTTNG_DOMAIN_KERNEL:
1009 {
1010 struct ltt_kernel_channel *kchan;
1011 struct ltt_kernel_session *ksess;
1012
1013 ksess = session->kernel_session;
1014
1015 /*
1016 * If a non-default channel has been created in the
1017 * session, explicitely require that -c chan_name needs
1018 * to be provided.
1019 */
1020 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1021 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1022 goto error;
1023 }
1024
1025 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1026 if (kchan == NULL) {
1027 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1028 goto error;
1029 }
1030
1031 ret = event_kernel_disable_tracepoint(kchan, event_name);
1032 if (ret != LTTNG_OK) {
1033 goto error;
1034 }
1035
1036 kernel_wait_quiescent(kernel_tracer_fd);
1037 break;
1038 }
1039 case LTTNG_DOMAIN_UST:
1040 {
1041 struct ltt_ust_channel *uchan;
1042 struct ltt_ust_session *usess;
1043
1044 usess = session->ust_session;
1045
1046 /*
1047 * If a non-default channel has been created in the
1048 * session, explicitely require that -c chan_name needs
1049 * to be provided.
1050 */
1051 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1052 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1053 goto error;
1054 }
1055
1056 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1057 channel_name);
1058 if (uchan == NULL) {
1059 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1060 goto error;
1061 }
1062
1063 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
1064 if (ret != LTTNG_OK) {
1065 goto error;
1066 }
1067
1068 DBG3("Disable UST event %s in channel %s completed", event_name,
1069 channel_name);
1070 break;
1071 }
1072 case LTTNG_DOMAIN_JUL:
1073 {
1074 struct ltt_ust_session *usess = session->ust_session;
1075
1076 assert(usess);
1077
1078 ret = event_jul_disable(usess, event_name);
1079 if (ret != LTTNG_OK) {
1080 goto error;
1081 }
1082
1083 break;
1084 }
1085 #if 0
1086 case LTTNG_DOMAIN_UST_EXEC_NAME:
1087 case LTTNG_DOMAIN_UST_PID:
1088 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1089 #endif
1090 default:
1091 ret = LTTNG_ERR_UND;
1092 goto error;
1093 }
1094
1095 ret = LTTNG_OK;
1096
1097 error:
1098 rcu_read_unlock();
1099 return ret;
1100 }
1101
1102 /*
1103 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1104 */
1105 int cmd_disable_event_all(struct ltt_session *session, int domain,
1106 char *channel_name)
1107 {
1108 int ret;
1109
1110 rcu_read_lock();
1111
1112 switch (domain) {
1113 case LTTNG_DOMAIN_KERNEL:
1114 {
1115 struct ltt_kernel_session *ksess;
1116 struct ltt_kernel_channel *kchan;
1117
1118 ksess = session->kernel_session;
1119
1120 /*
1121 * If a non-default channel has been created in the
1122 * session, explicitely require that -c chan_name needs
1123 * to be provided.
1124 */
1125 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1126 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1127 goto error;
1128 }
1129
1130 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1131 if (kchan == NULL) {
1132 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1133 goto error;
1134 }
1135
1136 ret = event_kernel_disable_all(kchan);
1137 if (ret != LTTNG_OK) {
1138 goto error;
1139 }
1140
1141 kernel_wait_quiescent(kernel_tracer_fd);
1142 break;
1143 }
1144 case LTTNG_DOMAIN_UST:
1145 {
1146 struct ltt_ust_session *usess;
1147 struct ltt_ust_channel *uchan;
1148
1149 usess = session->ust_session;
1150
1151 /*
1152 * If a non-default channel has been created in the
1153 * session, explicitely require that -c chan_name needs
1154 * to be provided.
1155 */
1156 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1157 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1158 goto error;
1159 }
1160
1161 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1162 channel_name);
1163 if (uchan == NULL) {
1164 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1165 goto error;
1166 }
1167
1168 ret = event_ust_disable_all_tracepoints(usess, uchan);
1169 if (ret != 0) {
1170 goto error;
1171 }
1172
1173 DBG3("Disable all UST events in channel %s completed", channel_name);
1174
1175 break;
1176 }
1177 case LTTNG_DOMAIN_JUL:
1178 {
1179 struct ltt_ust_session *usess = session->ust_session;
1180
1181 assert(usess);
1182
1183 ret = event_jul_disable_all(usess);
1184 if (ret != LTTNG_OK) {
1185 goto error;
1186 }
1187
1188 break;
1189 }
1190 #if 0
1191 case LTTNG_DOMAIN_UST_EXEC_NAME:
1192 case LTTNG_DOMAIN_UST_PID:
1193 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1194 #endif
1195 default:
1196 ret = LTTNG_ERR_UND;
1197 goto error;
1198 }
1199
1200 ret = LTTNG_OK;
1201
1202 error:
1203 rcu_read_unlock();
1204 return ret;
1205 }
1206
1207 /*
1208 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1209 */
1210 int cmd_add_context(struct ltt_session *session, int domain,
1211 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
1212 {
1213 int ret, chan_kern_created = 0, chan_ust_created = 0;
1214
1215 switch (domain) {
1216 case LTTNG_DOMAIN_KERNEL:
1217 assert(session->kernel_session);
1218
1219 if (session->kernel_session->channel_count == 0) {
1220 /* Create default channel */
1221 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1222 if (ret != LTTNG_OK) {
1223 goto error;
1224 }
1225 chan_kern_created = 1;
1226 }
1227 /* Add kernel context to kernel tracer */
1228 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1229 if (ret != LTTNG_OK) {
1230 goto error;
1231 }
1232 break;
1233 case LTTNG_DOMAIN_UST:
1234 {
1235 struct ltt_ust_session *usess = session->ust_session;
1236 unsigned int chan_count;
1237
1238 assert(usess);
1239
1240 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1241 if (chan_count == 0) {
1242 struct lttng_channel *attr;
1243 /* Create default channel */
1244 attr = channel_new_default_attr(domain, usess->buffer_type);
1245 if (attr == NULL) {
1246 ret = LTTNG_ERR_FATAL;
1247 goto error;
1248 }
1249
1250 ret = channel_ust_create(usess, attr, usess->buffer_type);
1251 if (ret != LTTNG_OK) {
1252 free(attr);
1253 goto error;
1254 }
1255 free(attr);
1256 chan_ust_created = 1;
1257 }
1258
1259 ret = context_ust_add(usess, domain, ctx, channel_name);
1260 if (ret != LTTNG_OK) {
1261 goto error;
1262 }
1263 break;
1264 }
1265 #if 0
1266 case LTTNG_DOMAIN_UST_EXEC_NAME:
1267 case LTTNG_DOMAIN_UST_PID:
1268 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1269 #endif
1270 default:
1271 ret = LTTNG_ERR_UND;
1272 goto error;
1273 }
1274
1275 return LTTNG_OK;
1276
1277 error:
1278 if (chan_kern_created) {
1279 struct ltt_kernel_channel *kchan =
1280 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1281 session->kernel_session);
1282 /* Created previously, this should NOT fail. */
1283 assert(kchan);
1284 kernel_destroy_channel(kchan);
1285 }
1286
1287 if (chan_ust_created) {
1288 struct ltt_ust_channel *uchan =
1289 trace_ust_find_channel_by_name(
1290 session->ust_session->domain_global.channels,
1291 DEFAULT_CHANNEL_NAME);
1292 /* Created previously, this should NOT fail. */
1293 assert(uchan);
1294 /* Remove from the channel list of the session. */
1295 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1296 uchan);
1297 trace_ust_destroy_channel(uchan);
1298 }
1299 return ret;
1300 }
1301
1302 /*
1303 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1304 */
1305 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
1306 char *channel_name, struct lttng_event *event,
1307 char *filter_expression,
1308 struct lttng_filter_bytecode *filter,
1309 struct lttng_event_exclusion *exclusion,
1310 int wpipe)
1311 {
1312 int ret, channel_created = 0;
1313 struct lttng_channel *attr;
1314
1315 assert(session);
1316 assert(event);
1317 assert(channel_name);
1318
1319 rcu_read_lock();
1320
1321 switch (domain->type) {
1322 case LTTNG_DOMAIN_KERNEL:
1323 {
1324 struct ltt_kernel_channel *kchan;
1325
1326 /*
1327 * If a non-default channel has been created in the
1328 * session, explicitely require that -c chan_name needs
1329 * to be provided.
1330 */
1331 if (session->kernel_session->has_non_default_channel
1332 && channel_name[0] == '\0') {
1333 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1334 goto error;
1335 }
1336
1337 kchan = trace_kernel_get_channel_by_name(channel_name,
1338 session->kernel_session);
1339 if (kchan == NULL) {
1340 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1341 LTTNG_BUFFER_GLOBAL);
1342 if (attr == NULL) {
1343 ret = LTTNG_ERR_FATAL;
1344 goto error;
1345 }
1346 strncpy(attr->name, channel_name, sizeof(attr->name));
1347
1348 ret = cmd_enable_channel(session, domain, attr, wpipe);
1349 if (ret != LTTNG_OK) {
1350 free(attr);
1351 goto error;
1352 }
1353 free(attr);
1354
1355 channel_created = 1;
1356 }
1357
1358 /* Get the newly created kernel channel pointer */
1359 kchan = trace_kernel_get_channel_by_name(channel_name,
1360 session->kernel_session);
1361 if (kchan == NULL) {
1362 /* This sould not happen... */
1363 ret = LTTNG_ERR_FATAL;
1364 goto error;
1365 }
1366
1367 ret = event_kernel_enable_tracepoint(kchan, event);
1368 if (ret != LTTNG_OK) {
1369 if (channel_created) {
1370 /* Let's not leak a useless channel. */
1371 kernel_destroy_channel(kchan);
1372 }
1373 goto error;
1374 }
1375
1376 kernel_wait_quiescent(kernel_tracer_fd);
1377 break;
1378 }
1379 case LTTNG_DOMAIN_UST:
1380 {
1381 struct ltt_ust_channel *uchan;
1382 struct ltt_ust_session *usess = session->ust_session;
1383
1384 assert(usess);
1385
1386 /*
1387 * If a non-default channel has been created in the
1388 * session, explicitely require that -c chan_name needs
1389 * to be provided.
1390 */
1391 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1392 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1393 goto error;
1394 }
1395
1396 /* Get channel from global UST domain */
1397 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1398 channel_name);
1399 if (uchan == NULL) {
1400 /* Create default channel */
1401 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1402 usess->buffer_type);
1403 if (attr == NULL) {
1404 ret = LTTNG_ERR_FATAL;
1405 goto error;
1406 }
1407 strncpy(attr->name, channel_name, sizeof(attr->name));
1408
1409 ret = cmd_enable_channel(session, domain, attr, wpipe);
1410 if (ret != LTTNG_OK) {
1411 free(attr);
1412 goto error;
1413 }
1414 free(attr);
1415
1416 /* Get the newly created channel reference back */
1417 uchan = trace_ust_find_channel_by_name(
1418 usess->domain_global.channels, channel_name);
1419 assert(uchan);
1420 }
1421
1422 /* At this point, the session and channel exist on the tracer */
1423 ret = event_ust_enable_tracepoint(usess, uchan, event,
1424 filter_expression, filter, exclusion);
1425 if (ret != LTTNG_OK) {
1426 goto error;
1427 }
1428 break;
1429 }
1430 case LTTNG_DOMAIN_JUL:
1431 {
1432 struct lttng_event uevent;
1433 struct lttng_domain tmp_dom;
1434 struct ltt_ust_session *usess = session->ust_session;
1435
1436 assert(usess);
1437
1438 /* Create the default JUL tracepoint. */
1439 memset(&uevent, 0, sizeof(uevent));
1440 uevent.type = LTTNG_EVENT_TRACEPOINT;
1441 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1442 if (is_root) {
1443 strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME,
1444 sizeof(uevent.name));
1445 } else {
1446 strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME,
1447 sizeof(uevent.name));
1448 }
1449 uevent.name[sizeof(uevent.name) - 1] = '\0';
1450
1451 /*
1452 * The domain type is changed because we are about to enable the
1453 * default channel and event for the JUL domain that are hardcoded.
1454 * This happens in the UST domain.
1455 */
1456 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1457 tmp_dom.type = LTTNG_DOMAIN_UST;
1458
1459 ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
1460 &uevent, filter_expression, filter, NULL, wpipe);
1461 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1462 goto error;
1463 }
1464
1465 /* The wild card * means that everything should be enabled. */
1466 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
1467 ret = event_jul_enable_all(usess, event);
1468 } else {
1469 ret = event_jul_enable(usess, event);
1470 }
1471 if (ret != LTTNG_OK) {
1472 goto error;
1473 }
1474
1475 break;
1476 }
1477 #if 0
1478 case LTTNG_DOMAIN_UST_EXEC_NAME:
1479 case LTTNG_DOMAIN_UST_PID:
1480 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1481 #endif
1482 default:
1483 ret = LTTNG_ERR_UND;
1484 goto error;
1485 }
1486
1487 ret = LTTNG_OK;
1488
1489 error:
1490 rcu_read_unlock();
1491 return ret;
1492 }
1493
1494 /*
1495 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1496 */
1497 int cmd_enable_event_all(struct ltt_session *session,
1498 struct lttng_domain *domain, char *channel_name, int event_type,
1499 char *filter_expression,
1500 struct lttng_filter_bytecode *filter, int wpipe)
1501 {
1502 int ret;
1503 struct lttng_channel *attr;
1504
1505 assert(session);
1506 assert(channel_name);
1507
1508 rcu_read_lock();
1509
1510 switch (domain->type) {
1511 case LTTNG_DOMAIN_KERNEL:
1512 {
1513 struct ltt_kernel_channel *kchan;
1514
1515 assert(session->kernel_session);
1516
1517 /*
1518 * If a non-default channel has been created in the
1519 * session, explicitely require that -c chan_name needs
1520 * to be provided.
1521 */
1522 if (session->kernel_session->has_non_default_channel
1523 && channel_name[0] == '\0') {
1524 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1525 goto error;
1526 }
1527
1528 kchan = trace_kernel_get_channel_by_name(channel_name,
1529 session->kernel_session);
1530 if (kchan == NULL) {
1531 /* Create default channel */
1532 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1533 LTTNG_BUFFER_GLOBAL);
1534 if (attr == NULL) {
1535 ret = LTTNG_ERR_FATAL;
1536 goto error;
1537 }
1538 strncpy(attr->name, channel_name, sizeof(attr->name));
1539
1540 ret = cmd_enable_channel(session, domain, attr, wpipe);
1541 if (ret != LTTNG_OK) {
1542 free(attr);
1543 goto error;
1544 }
1545 free(attr);
1546
1547 /* Get the newly created kernel channel pointer */
1548 kchan = trace_kernel_get_channel_by_name(channel_name,
1549 session->kernel_session);
1550 assert(kchan);
1551 }
1552
1553 switch (event_type) {
1554 case LTTNG_EVENT_SYSCALL:
1555 ret = event_kernel_enable_all_syscalls(kchan, kernel_tracer_fd);
1556 break;
1557 case LTTNG_EVENT_TRACEPOINT:
1558 /*
1559 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1560 * events already registered to the channel.
1561 */
1562 ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
1563 break;
1564 case LTTNG_EVENT_ALL:
1565 /* Enable syscalls and tracepoints */
1566 ret = event_kernel_enable_all(kchan, kernel_tracer_fd);
1567 break;
1568 default:
1569 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
1570 goto error;
1571 }
1572
1573 /* Manage return value */
1574 if (ret != LTTNG_OK) {
1575 /*
1576 * On error, cmd_enable_channel call will take care of destroying
1577 * the created channel if it was needed.
1578 */
1579 goto error;
1580 }
1581
1582 kernel_wait_quiescent(kernel_tracer_fd);
1583 break;
1584 }
1585 case LTTNG_DOMAIN_UST:
1586 {
1587 struct ltt_ust_channel *uchan;
1588 struct ltt_ust_session *usess = session->ust_session;
1589
1590 assert(usess);
1591
1592 /*
1593 * If a non-default channel has been created in the
1594 * session, explicitely require that -c chan_name needs
1595 * to be provided.
1596 */
1597 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1598 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1599 goto error;
1600 }
1601
1602 /* Get channel from global UST domain */
1603 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1604 channel_name);
1605 if (uchan == NULL) {
1606 /* Create default channel */
1607 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1608 usess->buffer_type);
1609 if (attr == NULL) {
1610 ret = LTTNG_ERR_FATAL;
1611 goto error;
1612 }
1613 strncpy(attr->name, channel_name, sizeof(attr->name));
1614
1615 ret = cmd_enable_channel(session, domain, attr, wpipe);
1616 if (ret != LTTNG_OK) {
1617 free(attr);
1618 goto error;
1619 }
1620 free(attr);
1621
1622 /* Get the newly created channel reference back */
1623 uchan = trace_ust_find_channel_by_name(
1624 usess->domain_global.channels, channel_name);
1625 assert(uchan);
1626 }
1627
1628 /* At this point, the session and channel exist on the tracer */
1629
1630 switch (event_type) {
1631 case LTTNG_EVENT_ALL:
1632 case LTTNG_EVENT_TRACEPOINT:
1633 ret = event_ust_enable_all_tracepoints(usess, uchan,
1634 filter_expression, filter);
1635 if (ret != LTTNG_OK) {
1636 goto error;
1637 }
1638 break;
1639 default:
1640 ret = LTTNG_ERR_UST_ENABLE_FAIL;
1641 goto error;
1642 }
1643
1644 /* Manage return value */
1645 if (ret != LTTNG_OK) {
1646 goto error;
1647 }
1648
1649 break;
1650 }
1651 case LTTNG_DOMAIN_JUL:
1652 {
1653 struct lttng_event uevent, event;
1654 struct lttng_domain tmp_dom;
1655 struct ltt_ust_session *usess = session->ust_session;
1656
1657 assert(usess);
1658
1659 /* Create the default JUL tracepoint. */
1660 uevent.type = LTTNG_EVENT_TRACEPOINT;
1661 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1662 if (is_root) {
1663 strncpy(uevent.name, DEFAULT_SYS_JUL_EVENT_NAME,
1664 sizeof(uevent.name));
1665 } else {
1666 strncpy(uevent.name, DEFAULT_USER_JUL_EVENT_NAME,
1667 sizeof(uevent.name));
1668 }
1669 uevent.name[sizeof(uevent.name) - 1] = '\0';
1670
1671 /*
1672 * The domain type is changed because we are about to enable the
1673 * default channel and event for the JUL domain that are hardcoded.
1674 * This happens in the UST domain.
1675 */
1676 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1677 tmp_dom.type = LTTNG_DOMAIN_UST;
1678
1679 ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
1680 &uevent, NULL, NULL, NULL, wpipe);
1681 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1682 goto error;
1683 }
1684
1685 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1686 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
1687 strncpy(event.name, "*", sizeof(event.name));
1688 event.name[sizeof(event.name) - 1] = '\0';
1689
1690 ret = event_jul_enable_all(usess, &event);
1691 if (ret != LTTNG_OK) {
1692 goto error;
1693 }
1694
1695 break;
1696 }
1697 #if 0
1698 case LTTNG_DOMAIN_UST_EXEC_NAME:
1699 case LTTNG_DOMAIN_UST_PID:
1700 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1701 #endif
1702 default:
1703 ret = LTTNG_ERR_UND;
1704 goto error;
1705 }
1706
1707 ret = LTTNG_OK;
1708
1709 error:
1710 rcu_read_unlock();
1711 return ret;
1712 }
1713
1714
1715 /*
1716 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1717 */
1718 ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1719 {
1720 int ret;
1721 ssize_t nb_events = 0;
1722
1723 switch (domain) {
1724 case LTTNG_DOMAIN_KERNEL:
1725 nb_events = kernel_list_events(kernel_tracer_fd, events);
1726 if (nb_events < 0) {
1727 ret = LTTNG_ERR_KERN_LIST_FAIL;
1728 goto error;
1729 }
1730 break;
1731 case LTTNG_DOMAIN_UST:
1732 nb_events = ust_app_list_events(events);
1733 if (nb_events < 0) {
1734 ret = LTTNG_ERR_UST_LIST_FAIL;
1735 goto error;
1736 }
1737 break;
1738 case LTTNG_DOMAIN_JUL:
1739 nb_events = jul_list_events(events);
1740 if (nb_events < 0) {
1741 ret = LTTNG_ERR_UST_LIST_FAIL;
1742 goto error;
1743 }
1744 break;
1745 default:
1746 ret = LTTNG_ERR_UND;
1747 goto error;
1748 }
1749
1750 return nb_events;
1751
1752 error:
1753 /* Return negative value to differentiate return code */
1754 return -ret;
1755 }
1756
1757 /*
1758 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1759 */
1760 ssize_t cmd_list_tracepoint_fields(int domain,
1761 struct lttng_event_field **fields)
1762 {
1763 int ret;
1764 ssize_t nb_fields = 0;
1765
1766 switch (domain) {
1767 case LTTNG_DOMAIN_UST:
1768 nb_fields = ust_app_list_event_fields(fields);
1769 if (nb_fields < 0) {
1770 ret = LTTNG_ERR_UST_LIST_FAIL;
1771 goto error;
1772 }
1773 break;
1774 case LTTNG_DOMAIN_KERNEL:
1775 default: /* fall-through */
1776 ret = LTTNG_ERR_UND;
1777 goto error;
1778 }
1779
1780 return nb_fields;
1781
1782 error:
1783 /* Return negative value to differentiate return code */
1784 return -ret;
1785 }
1786
1787 /*
1788 * Command LTTNG_START_TRACE processed by the client thread.
1789 */
1790 int cmd_start_trace(struct ltt_session *session)
1791 {
1792 int ret;
1793 unsigned long nb_chan = 0;
1794 struct ltt_kernel_session *ksession;
1795 struct ltt_ust_session *usess;
1796
1797 assert(session);
1798
1799 /* Ease our life a bit ;) */
1800 ksession = session->kernel_session;
1801 usess = session->ust_session;
1802
1803 /* Is the session already started? */
1804 if (session->active) {
1805 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1806 goto error;
1807 }
1808
1809 /*
1810 * Starting a session without channel is useless since after that it's not
1811 * possible to enable channel thus inform the client.
1812 */
1813 if (usess && usess->domain_global.channels) {
1814 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
1815 }
1816 if (ksession) {
1817 nb_chan += ksession->channel_count;
1818 }
1819 if (!nb_chan) {
1820 ret = LTTNG_ERR_NO_CHANNEL;
1821 goto error;
1822 }
1823
1824 /* Kernel tracing */
1825 if (ksession != NULL) {
1826 ret = start_kernel_session(ksession, kernel_tracer_fd);
1827 if (ret != LTTNG_OK) {
1828 goto error;
1829 }
1830 }
1831
1832 /* Flag session that trace should start automatically */
1833 if (usess) {
1834 /*
1835 * Even though the start trace might fail, flag this session active so
1836 * other application coming in are started by default.
1837 */
1838 usess->active = 1;
1839
1840 ret = ust_app_start_trace_all(usess);
1841 if (ret < 0) {
1842 ret = LTTNG_ERR_UST_START_FAIL;
1843 goto error;
1844 }
1845 }
1846
1847 /* Flag this after a successful start. */
1848 session->has_been_started = 1;
1849 session->active = 1;
1850
1851 ret = LTTNG_OK;
1852
1853 error:
1854 return ret;
1855 }
1856
1857 /*
1858 * Command LTTNG_STOP_TRACE processed by the client thread.
1859 */
1860 int cmd_stop_trace(struct ltt_session *session)
1861 {
1862 int ret;
1863 struct ltt_kernel_channel *kchan;
1864 struct ltt_kernel_session *ksession;
1865 struct ltt_ust_session *usess;
1866
1867 assert(session);
1868
1869 /* Short cut */
1870 ksession = session->kernel_session;
1871 usess = session->ust_session;
1872
1873 /* Session is not active. Skip everythong and inform the client. */
1874 if (!session->active) {
1875 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
1876 goto error;
1877 }
1878
1879 /* Kernel tracer */
1880 if (ksession && ksession->active) {
1881 DBG("Stop kernel tracing");
1882
1883 /* Flush metadata if exist */
1884 if (ksession->metadata_stream_fd >= 0) {
1885 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1886 if (ret < 0) {
1887 ERR("Kernel metadata flush failed");
1888 }
1889 }
1890
1891 /* Flush all buffers before stopping */
1892 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1893 ret = kernel_flush_buffer(kchan);
1894 if (ret < 0) {
1895 ERR("Kernel flush buffer error");
1896 }
1897 }
1898
1899 ret = kernel_stop_session(ksession);
1900 if (ret < 0) {
1901 ret = LTTNG_ERR_KERN_STOP_FAIL;
1902 goto error;
1903 }
1904
1905 kernel_wait_quiescent(kernel_tracer_fd);
1906
1907 ksession->active = 0;
1908 }
1909
1910 if (usess && usess->active) {
1911 /*
1912 * Even though the stop trace might fail, flag this session inactive so
1913 * other application coming in are not started by default.
1914 */
1915 usess->active = 0;
1916
1917 ret = ust_app_stop_trace_all(usess);
1918 if (ret < 0) {
1919 ret = LTTNG_ERR_UST_STOP_FAIL;
1920 goto error;
1921 }
1922 }
1923
1924 /* Flag inactive after a successful stop. */
1925 session->active = 0;
1926 ret = LTTNG_OK;
1927
1928 error:
1929 return ret;
1930 }
1931
1932 /*
1933 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1934 */
1935 int cmd_set_consumer_uri(int domain, struct ltt_session *session,
1936 size_t nb_uri, struct lttng_uri *uris)
1937 {
1938 int ret, i;
1939 struct ltt_kernel_session *ksess = session->kernel_session;
1940 struct ltt_ust_session *usess = session->ust_session;
1941 struct consumer_output *consumer = NULL;
1942
1943 assert(session);
1944 assert(uris);
1945 assert(nb_uri > 0);
1946
1947 /* Can't set consumer URI if the session is active. */
1948 if (session->active) {
1949 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1950 goto error;
1951 }
1952
1953 /*
1954 * This case switch makes sure the domain session has a temporary consumer
1955 * so the URL can be set.
1956 */
1957 switch (domain) {
1958 case 0:
1959 /* Code flow error. A session MUST always have a consumer object */
1960 assert(session->consumer);
1961 /*
1962 * The URL will be added to the tracing session consumer instead of a
1963 * specific domain consumer.
1964 */
1965 consumer = session->consumer;
1966 break;
1967 case LTTNG_DOMAIN_KERNEL:
1968 /* Code flow error if we don't have a kernel session here. */
1969 assert(ksess);
1970 assert(ksess->consumer);
1971 consumer = ksess->consumer;
1972 break;
1973 case LTTNG_DOMAIN_UST:
1974 /* Code flow error if we don't have a kernel session here. */
1975 assert(usess);
1976 assert(usess->consumer);
1977 consumer = usess->consumer;
1978 break;
1979 }
1980
1981 for (i = 0; i < nb_uri; i++) {
1982 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
1983 if (ret != LTTNG_OK) {
1984 goto error;
1985 }
1986 }
1987
1988 /* All good! */
1989 ret = LTTNG_OK;
1990
1991 error:
1992 return ret;
1993 }
1994
1995 /*
1996 * Command LTTNG_CREATE_SESSION processed by the client thread.
1997 */
1998 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
1999 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2000 {
2001 int ret;
2002 struct ltt_session *session;
2003
2004 assert(name);
2005 assert(creds);
2006
2007 /*
2008 * Verify if the session already exist
2009 *
2010 * XXX: There is no need for the session lock list here since the caller
2011 * (process_client_msg) is holding it. We might want to change that so a
2012 * single command does not lock the entire session list.
2013 */
2014 session = session_find_by_name(name);
2015 if (session != NULL) {
2016 ret = LTTNG_ERR_EXIST_SESS;
2017 goto find_error;
2018 }
2019
2020 /* Create tracing session in the registry */
2021 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2022 LTTNG_SOCK_GET_GID_CRED(creds));
2023 if (ret != LTTNG_OK) {
2024 goto session_error;
2025 }
2026
2027 /*
2028 * Get the newly created session pointer back
2029 *
2030 * XXX: There is no need for the session lock list here since the caller
2031 * (process_client_msg) is holding it. We might want to change that so a
2032 * single command does not lock the entire session list.
2033 */
2034 session = session_find_by_name(name);
2035 assert(session);
2036
2037 session->live_timer = live_timer;
2038 /* Create default consumer output for the session not yet created. */
2039 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2040 if (session->consumer == NULL) {
2041 ret = LTTNG_ERR_FATAL;
2042 goto consumer_error;
2043 }
2044
2045 if (uris) {
2046 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
2047 if (ret != LTTNG_OK) {
2048 goto consumer_error;
2049 }
2050 session->output_traces = 1;
2051 } else {
2052 session->output_traces = 0;
2053 DBG2("Session %s created with no output", session->name);
2054 }
2055
2056 session->consumer->enabled = 1;
2057
2058 return LTTNG_OK;
2059
2060 consumer_error:
2061 session_destroy(session);
2062 session_error:
2063 find_error:
2064 return ret;
2065 }
2066
2067 /*
2068 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2069 */
2070 int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2071 size_t nb_uri, lttng_sock_cred *creds)
2072 {
2073 int ret;
2074 struct ltt_session *session;
2075 struct snapshot_output *new_output = NULL;
2076
2077 assert(name);
2078 assert(creds);
2079
2080 /*
2081 * Create session in no output mode with URIs set to NULL. The uris we've
2082 * received are for a default snapshot output if one.
2083 */
2084 ret = cmd_create_session_uri(name, NULL, 0, creds, -1);
2085 if (ret != LTTNG_OK) {
2086 goto error;
2087 }
2088
2089 /* Get the newly created session pointer back. This should NEVER fail. */
2090 session = session_find_by_name(name);
2091 assert(session);
2092
2093 /* Flag session for snapshot mode. */
2094 session->snapshot_mode = 1;
2095
2096 /* Skip snapshot output creation if no URI is given. */
2097 if (nb_uri == 0) {
2098 goto end;
2099 }
2100
2101 new_output = snapshot_output_alloc();
2102 if (!new_output) {
2103 ret = LTTNG_ERR_NOMEM;
2104 goto error_snapshot_alloc;
2105 }
2106
2107 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2108 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2109 if (ret < 0) {
2110 if (ret == -ENOMEM) {
2111 ret = LTTNG_ERR_NOMEM;
2112 } else {
2113 ret = LTTNG_ERR_INVALID;
2114 }
2115 goto error_snapshot;
2116 }
2117
2118 rcu_read_lock();
2119 snapshot_add_output(&session->snapshot, new_output);
2120 rcu_read_unlock();
2121
2122 end:
2123 return LTTNG_OK;
2124
2125 error_snapshot:
2126 snapshot_output_destroy(new_output);
2127 error_snapshot_alloc:
2128 session_destroy(session);
2129 error:
2130 return ret;
2131 }
2132
2133 /*
2134 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2135 */
2136 int cmd_destroy_session(struct ltt_session *session, int wpipe)
2137 {
2138 int ret;
2139 struct ltt_ust_session *usess;
2140 struct ltt_kernel_session *ksess;
2141
2142 /* Safety net */
2143 assert(session);
2144
2145 usess = session->ust_session;
2146 ksess = session->kernel_session;
2147
2148 /* Clean kernel session teardown */
2149 kernel_destroy_session(ksess);
2150
2151 /* UST session teardown */
2152 if (usess) {
2153 /* Close any relayd session */
2154 consumer_output_send_destroy_relayd(usess->consumer);
2155
2156 /* Destroy every UST application related to this session. */
2157 ret = ust_app_destroy_trace_all(usess);
2158 if (ret) {
2159 ERR("Error in ust_app_destroy_trace_all");
2160 }
2161
2162 /* Clean up the rest. */
2163 trace_ust_destroy_session(usess);
2164 }
2165
2166 /*
2167 * Must notify the kernel thread here to update it's poll set in order to
2168 * remove the channel(s)' fd just destroyed.
2169 */
2170 ret = notify_thread_pipe(wpipe);
2171 if (ret < 0) {
2172 PERROR("write kernel poll pipe");
2173 }
2174
2175 ret = session_destroy(session);
2176
2177 return ret;
2178 }
2179
2180 /*
2181 * Command LTTNG_CALIBRATE processed by the client thread.
2182 */
2183 int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
2184 {
2185 int ret;
2186
2187 switch (domain) {
2188 case LTTNG_DOMAIN_KERNEL:
2189 {
2190 struct lttng_kernel_calibrate kcalibrate;
2191
2192 switch (calibrate->type) {
2193 case LTTNG_CALIBRATE_FUNCTION:
2194 default:
2195 /* Default and only possible calibrate option. */
2196 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2197 break;
2198 }
2199
2200 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2201 if (ret < 0) {
2202 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2203 goto error;
2204 }
2205 break;
2206 }
2207 case LTTNG_DOMAIN_UST:
2208 {
2209 struct lttng_ust_calibrate ucalibrate;
2210
2211 switch (calibrate->type) {
2212 case LTTNG_CALIBRATE_FUNCTION:
2213 default:
2214 /* Default and only possible calibrate option. */
2215 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2216 break;
2217 }
2218
2219 ret = ust_app_calibrate_glb(&ucalibrate);
2220 if (ret < 0) {
2221 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2222 goto error;
2223 }
2224 break;
2225 }
2226 default:
2227 ret = LTTNG_ERR_UND;
2228 goto error;
2229 }
2230
2231 ret = LTTNG_OK;
2232
2233 error:
2234 return ret;
2235 }
2236
2237 /*
2238 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2239 */
2240 int cmd_register_consumer(struct ltt_session *session, int domain,
2241 const char *sock_path, struct consumer_data *cdata)
2242 {
2243 int ret, sock;
2244 struct consumer_socket *socket = NULL;
2245
2246 assert(session);
2247 assert(cdata);
2248 assert(sock_path);
2249
2250 switch (domain) {
2251 case LTTNG_DOMAIN_KERNEL:
2252 {
2253 struct ltt_kernel_session *ksess = session->kernel_session;
2254
2255 assert(ksess);
2256
2257 /* Can't register a consumer if there is already one */
2258 if (ksess->consumer_fds_sent != 0) {
2259 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2260 goto error;
2261 }
2262
2263 sock = lttcomm_connect_unix_sock(sock_path);
2264 if (sock < 0) {
2265 ret = LTTNG_ERR_CONNECT_FAIL;
2266 goto error;
2267 }
2268 cdata->cmd_sock = sock;
2269
2270 socket = consumer_allocate_socket(&cdata->cmd_sock);
2271 if (socket == NULL) {
2272 ret = close(sock);
2273 if (ret < 0) {
2274 PERROR("close register consumer");
2275 }
2276 cdata->cmd_sock = -1;
2277 ret = LTTNG_ERR_FATAL;
2278 goto error;
2279 }
2280
2281 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2282 if (socket->lock == NULL) {
2283 PERROR("zmalloc pthread mutex");
2284 ret = LTTNG_ERR_FATAL;
2285 goto error;
2286 }
2287 pthread_mutex_init(socket->lock, NULL);
2288 socket->registered = 1;
2289
2290 rcu_read_lock();
2291 consumer_add_socket(socket, ksess->consumer);
2292 rcu_read_unlock();
2293
2294 pthread_mutex_lock(&cdata->pid_mutex);
2295 cdata->pid = -1;
2296 pthread_mutex_unlock(&cdata->pid_mutex);
2297
2298 break;
2299 }
2300 default:
2301 /* TODO: Userspace tracing */
2302 ret = LTTNG_ERR_UND;
2303 goto error;
2304 }
2305
2306 return LTTNG_OK;
2307
2308 error:
2309 if (socket) {
2310 consumer_destroy_socket(socket);
2311 }
2312 return ret;
2313 }
2314
2315 /*
2316 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2317 */
2318 ssize_t cmd_list_domains(struct ltt_session *session,
2319 struct lttng_domain **domains)
2320 {
2321 int ret, index = 0;
2322 ssize_t nb_dom = 0;
2323
2324 if (session->kernel_session != NULL) {
2325 DBG3("Listing domains found kernel domain");
2326 nb_dom++;
2327 }
2328
2329 if (session->ust_session != NULL) {
2330 DBG3("Listing domains found UST global domain");
2331 nb_dom++;
2332
2333 if (session->ust_session->domain_jul.being_used) {
2334 nb_dom++;
2335 }
2336 }
2337
2338 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2339 if (*domains == NULL) {
2340 ret = LTTNG_ERR_FATAL;
2341 goto error;
2342 }
2343
2344 if (session->kernel_session != NULL) {
2345 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2346 index++;
2347 }
2348
2349 if (session->ust_session != NULL) {
2350 (*domains)[index].type = LTTNG_DOMAIN_UST;
2351 (*domains)[index].buf_type = session->ust_session->buffer_type;
2352 index++;
2353
2354 if (session->ust_session->domain_jul.being_used) {
2355 (*domains)[index].type = LTTNG_DOMAIN_JUL;
2356 (*domains)[index].buf_type = session->ust_session->buffer_type;
2357 index++;
2358 }
2359 }
2360
2361 return nb_dom;
2362
2363 error:
2364 /* Return negative value to differentiate return code */
2365 return -ret;
2366 }
2367
2368
2369 /*
2370 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2371 */
2372 ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2373 struct lttng_channel **channels)
2374 {
2375 int ret;
2376 ssize_t nb_chan = 0;
2377
2378 switch (domain) {
2379 case LTTNG_DOMAIN_KERNEL:
2380 if (session->kernel_session != NULL) {
2381 nb_chan = session->kernel_session->channel_count;
2382 }
2383 DBG3("Number of kernel channels %zd", nb_chan);
2384 if (nb_chan <= 0) {
2385 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2386 }
2387 break;
2388 case LTTNG_DOMAIN_UST:
2389 if (session->ust_session != NULL) {
2390 nb_chan = lttng_ht_get_count(
2391 session->ust_session->domain_global.channels);
2392 }
2393 DBG3("Number of UST global channels %zd", nb_chan);
2394 if (nb_chan <= 0) {
2395 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2396 }
2397 break;
2398 default:
2399 *channels = NULL;
2400 ret = LTTNG_ERR_UND;
2401 goto error;
2402 }
2403
2404 if (nb_chan > 0) {
2405 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2406 if (*channels == NULL) {
2407 ret = LTTNG_ERR_FATAL;
2408 goto error;
2409 }
2410
2411 list_lttng_channels(domain, session, *channels);
2412 } else {
2413 *channels = NULL;
2414 /* Ret value was set in the domain switch case */
2415 goto error;
2416 }
2417
2418 return nb_chan;
2419
2420 error:
2421 /* Return negative value to differentiate return code */
2422 return -ret;
2423 }
2424
2425 /*
2426 * Command LTTNG_LIST_EVENTS processed by the client thread.
2427 */
2428 ssize_t cmd_list_events(int domain, struct ltt_session *session,
2429 char *channel_name, struct lttng_event **events)
2430 {
2431 int ret = 0;
2432 ssize_t nb_event = 0;
2433
2434 switch (domain) {
2435 case LTTNG_DOMAIN_KERNEL:
2436 if (session->kernel_session != NULL) {
2437 nb_event = list_lttng_kernel_events(channel_name,
2438 session->kernel_session, events);
2439 }
2440 break;
2441 case LTTNG_DOMAIN_UST:
2442 {
2443 if (session->ust_session != NULL) {
2444 nb_event = list_lttng_ust_global_events(channel_name,
2445 &session->ust_session->domain_global, events);
2446 }
2447 break;
2448 }
2449 case LTTNG_DOMAIN_JUL:
2450 if (session->ust_session) {
2451 nb_event = list_lttng_jul_events(
2452 &session->ust_session->domain_jul, events);
2453 }
2454 break;
2455 default:
2456 ret = LTTNG_ERR_UND;
2457 goto error;
2458 }
2459
2460 return nb_event;
2461
2462 error:
2463 /* Return negative value to differentiate return code */
2464 return -ret;
2465 }
2466
2467 /*
2468 * Using the session list, filled a lttng_session array to send back to the
2469 * client for session listing.
2470 *
2471 * The session list lock MUST be acquired before calling this function. Use
2472 * session_lock_list() and session_unlock_list().
2473 */
2474 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2475 gid_t gid)
2476 {
2477 int ret;
2478 unsigned int i = 0;
2479 struct ltt_session *session;
2480 struct ltt_session_list *list = session_get_list();
2481
2482 DBG("Getting all available session for UID %d GID %d",
2483 uid, gid);
2484 /*
2485 * Iterate over session list and append data after the control struct in
2486 * the buffer.
2487 */
2488 cds_list_for_each_entry(session, &list->head, list) {
2489 /*
2490 * Only list the sessions the user can control.
2491 */
2492 if (!session_access_ok(session, uid, gid)) {
2493 continue;
2494 }
2495
2496 struct ltt_kernel_session *ksess = session->kernel_session;
2497 struct ltt_ust_session *usess = session->ust_session;
2498
2499 if (session->consumer->type == CONSUMER_DST_NET ||
2500 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2501 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2502 ret = build_network_session_path(sessions[i].path,
2503 sizeof(sessions[i].path), session);
2504 } else {
2505 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2506 session->consumer->dst.trace_path);
2507 }
2508 if (ret < 0) {
2509 PERROR("snprintf session path");
2510 continue;
2511 }
2512
2513 strncpy(sessions[i].name, session->name, NAME_MAX);
2514 sessions[i].name[NAME_MAX - 1] = '\0';
2515 sessions[i].enabled = session->active;
2516 sessions[i].snapshot_mode = session->snapshot_mode;
2517 sessions[i].live_timer_interval = session->live_timer;
2518 i++;
2519 }
2520 }
2521
2522 /*
2523 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2524 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2525 */
2526 int cmd_data_pending(struct ltt_session *session)
2527 {
2528 int ret;
2529 struct ltt_kernel_session *ksess = session->kernel_session;
2530 struct ltt_ust_session *usess = session->ust_session;
2531
2532 assert(session);
2533
2534 /* Session MUST be stopped to ask for data availability. */
2535 if (session->active) {
2536 ret = LTTNG_ERR_SESSION_STARTED;
2537 goto error;
2538 } else {
2539 /*
2540 * If stopped, just make sure we've started before else the above call
2541 * will always send that there is data pending.
2542 *
2543 * The consumer assumes that when the data pending command is received,
2544 * the trace has been started before or else no output data is written
2545 * by the streams which is a condition for data pending. So, this is
2546 * *VERY* important that we don't ask the consumer before a start
2547 * trace.
2548 */
2549 if (!session->has_been_started) {
2550 ret = 0;
2551 goto error;
2552 }
2553 }
2554
2555 if (ksess && ksess->consumer) {
2556 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2557 if (ret == 1) {
2558 /* Data is still being extracted for the kernel. */
2559 goto error;
2560 }
2561 }
2562
2563 if (usess && usess->consumer) {
2564 ret = consumer_is_data_pending(usess->id, usess->consumer);
2565 if (ret == 1) {
2566 /* Data is still being extracted for the kernel. */
2567 goto error;
2568 }
2569 }
2570
2571 /* Data is ready to be read by a viewer */
2572 ret = 0;
2573
2574 error:
2575 return ret;
2576 }
2577
2578 /*
2579 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2580 *
2581 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2582 */
2583 int cmd_snapshot_add_output(struct ltt_session *session,
2584 struct lttng_snapshot_output *output, uint32_t *id)
2585 {
2586 int ret;
2587 struct snapshot_output *new_output;
2588
2589 assert(session);
2590 assert(output);
2591
2592 DBG("Cmd snapshot add output for session %s", session->name);
2593
2594 /*
2595 * Permission denied to create an output if the session is not
2596 * set in no output mode.
2597 */
2598 if (session->output_traces) {
2599 ret = LTTNG_ERR_EPERM;
2600 goto error;
2601 }
2602
2603 /* Only one output is allowed until we have the "tee" feature. */
2604 if (session->snapshot.nb_output == 1) {
2605 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
2606 goto error;
2607 }
2608
2609 new_output = snapshot_output_alloc();
2610 if (!new_output) {
2611 ret = LTTNG_ERR_NOMEM;
2612 goto error;
2613 }
2614
2615 ret = snapshot_output_init(output->max_size, output->name,
2616 output->ctrl_url, output->data_url, session->consumer, new_output,
2617 &session->snapshot);
2618 if (ret < 0) {
2619 if (ret == -ENOMEM) {
2620 ret = LTTNG_ERR_NOMEM;
2621 } else {
2622 ret = LTTNG_ERR_INVALID;
2623 }
2624 goto free_error;
2625 }
2626
2627 rcu_read_lock();
2628 snapshot_add_output(&session->snapshot, new_output);
2629 if (id) {
2630 *id = new_output->id;
2631 }
2632 rcu_read_unlock();
2633
2634 return LTTNG_OK;
2635
2636 free_error:
2637 snapshot_output_destroy(new_output);
2638 error:
2639 return ret;
2640 }
2641
2642 /*
2643 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2644 *
2645 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2646 */
2647 int cmd_snapshot_del_output(struct ltt_session *session,
2648 struct lttng_snapshot_output *output)
2649 {
2650 int ret;
2651 struct snapshot_output *sout = NULL;
2652
2653 assert(session);
2654 assert(output);
2655
2656 rcu_read_lock();
2657
2658 /*
2659 * Permission denied to create an output if the session is not
2660 * set in no output mode.
2661 */
2662 if (session->output_traces) {
2663 ret = LTTNG_ERR_EPERM;
2664 goto error;
2665 }
2666
2667 if (output->id) {
2668 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
2669 session->name);
2670 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
2671 } else if (*output->name != '\0') {
2672 DBG("Cmd snapshot del output name %s for session %s", output->name,
2673 session->name);
2674 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
2675 }
2676 if (!sout) {
2677 ret = LTTNG_ERR_INVALID;
2678 goto error;
2679 }
2680
2681 snapshot_delete_output(&session->snapshot, sout);
2682 snapshot_output_destroy(sout);
2683 ret = LTTNG_OK;
2684
2685 error:
2686 rcu_read_unlock();
2687 return ret;
2688 }
2689
2690 /*
2691 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2692 *
2693 * If no output is available, outputs is untouched and 0 is returned.
2694 *
2695 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2696 */
2697 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
2698 struct lttng_snapshot_output **outputs)
2699 {
2700 int ret, idx = 0;
2701 struct lttng_snapshot_output *list;
2702 struct lttng_ht_iter iter;
2703 struct snapshot_output *output;
2704
2705 assert(session);
2706 assert(outputs);
2707
2708 DBG("Cmd snapshot list outputs for session %s", session->name);
2709
2710 /*
2711 * Permission denied to create an output if the session is not
2712 * set in no output mode.
2713 */
2714 if (session->output_traces) {
2715 ret = LTTNG_ERR_EPERM;
2716 goto error;
2717 }
2718
2719 if (session->snapshot.nb_output == 0) {
2720 ret = 0;
2721 goto error;
2722 }
2723
2724 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
2725 if (!list) {
2726 ret = LTTNG_ERR_NOMEM;
2727 goto error;
2728 }
2729
2730 /* Copy list from session to the new list object. */
2731 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
2732 output, node.node) {
2733 assert(output->consumer);
2734 list[idx].id = output->id;
2735 list[idx].max_size = output->max_size;
2736 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
2737 if (output->consumer->type == CONSUMER_DST_LOCAL) {
2738 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
2739 sizeof(list[idx].ctrl_url));
2740 } else {
2741 /* Control URI. */
2742 ret = uri_to_str_url(&output->consumer->dst.net.control,
2743 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
2744 if (ret < 0) {
2745 ret = LTTNG_ERR_NOMEM;
2746 goto free_error;
2747 }
2748
2749 /* Data URI. */
2750 ret = uri_to_str_url(&output->consumer->dst.net.data,
2751 list[idx].data_url, sizeof(list[idx].data_url));
2752 if (ret < 0) {
2753 ret = LTTNG_ERR_NOMEM;
2754 goto free_error;
2755 }
2756 }
2757 idx++;
2758 }
2759
2760 *outputs = list;
2761 return session->snapshot.nb_output;
2762
2763 free_error:
2764 free(list);
2765 error:
2766 return -ret;
2767 }
2768
2769 /*
2770 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2771 * snapshot output is *not* set with a remote destination.
2772 *
2773 * Return 0 on success or a LTTNG_ERR code.
2774 */
2775 static int set_relayd_for_snapshot(struct consumer_output *consumer,
2776 struct snapshot_output *snap_output, struct ltt_session *session)
2777 {
2778 int ret = LTTNG_OK;
2779 struct lttng_ht_iter iter;
2780 struct consumer_socket *socket;
2781
2782 assert(consumer);
2783 assert(snap_output);
2784 assert(session);
2785
2786 DBG2("Set relayd object from snapshot output");
2787
2788 /* Ignore if snapshot consumer output is not network. */
2789 if (snap_output->consumer->type != CONSUMER_DST_NET) {
2790 goto error;
2791 }
2792
2793 /*
2794 * For each consumer socket, create and send the relayd object of the
2795 * snapshot output.
2796 */
2797 rcu_read_lock();
2798 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
2799 socket, node.node) {
2800 ret = send_consumer_relayd_sockets(0, session->id,
2801 snap_output->consumer, socket,
2802 session->name, session->hostname,
2803 session->live_timer);
2804 if (ret != LTTNG_OK) {
2805 rcu_read_unlock();
2806 goto error;
2807 }
2808 }
2809 rcu_read_unlock();
2810
2811 error:
2812 return ret;
2813 }
2814
2815 /*
2816 * Record a kernel snapshot.
2817 *
2818 * Return LTTNG_OK on success or a LTTNG_ERR code.
2819 */
2820 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
2821 struct snapshot_output *output, struct ltt_session *session,
2822 int wait, int nb_streams)
2823 {
2824 int ret;
2825
2826 assert(ksess);
2827 assert(output);
2828 assert(session);
2829
2830 /* Get the datetime for the snapshot output directory. */
2831 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2832 sizeof(output->datetime));
2833 if (!ret) {
2834 ret = LTTNG_ERR_INVALID;
2835 goto error;
2836 }
2837
2838 /*
2839 * Copy kernel session sockets so we can communicate with the right
2840 * consumer for the snapshot record command.
2841 */
2842 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
2843 if (ret < 0) {
2844 ret = LTTNG_ERR_NOMEM;
2845 goto error;
2846 }
2847
2848 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
2849 if (ret != LTTNG_OK) {
2850 goto error_snapshot;
2851 }
2852
2853 ret = kernel_snapshot_record(ksess, output, wait, nb_streams);
2854 if (ret != LTTNG_OK) {
2855 goto error_snapshot;
2856 }
2857
2858 ret = LTTNG_OK;
2859
2860 error_snapshot:
2861 /* Clean up copied sockets so this output can use some other later on. */
2862 consumer_destroy_output_sockets(output->consumer);
2863 error:
2864 return ret;
2865 }
2866
2867 /*
2868 * Record a UST snapshot.
2869 *
2870 * Return 0 on success or a LTTNG_ERR error code.
2871 */
2872 static int record_ust_snapshot(struct ltt_ust_session *usess,
2873 struct snapshot_output *output, struct ltt_session *session,
2874 int wait, int nb_streams)
2875 {
2876 int ret;
2877
2878 assert(usess);
2879 assert(output);
2880 assert(session);
2881
2882 /* Get the datetime for the snapshot output directory. */
2883 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2884 sizeof(output->datetime));
2885 if (!ret) {
2886 ret = LTTNG_ERR_INVALID;
2887 goto error;
2888 }
2889
2890 /*
2891 * Copy UST session sockets so we can communicate with the right
2892 * consumer for the snapshot record command.
2893 */
2894 ret = consumer_copy_sockets(output->consumer, usess->consumer);
2895 if (ret < 0) {
2896 ret = LTTNG_ERR_NOMEM;
2897 goto error;
2898 }
2899
2900 ret = set_relayd_for_snapshot(usess->consumer, output, session);
2901 if (ret != LTTNG_OK) {
2902 goto error_snapshot;
2903 }
2904
2905 ret = ust_app_snapshot_record(usess, output, wait, nb_streams);
2906 if (ret < 0) {
2907 switch (-ret) {
2908 case EINVAL:
2909 ret = LTTNG_ERR_INVALID;
2910 break;
2911 case ENODATA:
2912 ret = LTTNG_ERR_SNAPSHOT_NODATA;
2913 break;
2914 default:
2915 ret = LTTNG_ERR_SNAPSHOT_FAIL;
2916 break;
2917 }
2918 goto error_snapshot;
2919 }
2920
2921 ret = LTTNG_OK;
2922
2923 error_snapshot:
2924 /* Clean up copied sockets so this output can use some other later on. */
2925 consumer_destroy_output_sockets(output->consumer);
2926 error:
2927 return ret;
2928 }
2929
2930 /*
2931 * Returns the total number of streams for a session or a negative value
2932 * on error.
2933 */
2934 static unsigned int get_total_nb_stream(struct ltt_session *session)
2935 {
2936 unsigned int total_streams = 0;
2937
2938 if (session->kernel_session) {
2939 struct ltt_kernel_session *ksess = session->kernel_session;
2940
2941 total_streams += ksess->stream_count_global;
2942 }
2943
2944 if (session->ust_session) {
2945 struct ltt_ust_session *usess = session->ust_session;
2946
2947 total_streams += ust_app_get_nb_stream(usess);
2948 }
2949
2950 return total_streams;
2951 }
2952
2953 /*
2954 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2955 *
2956 * The wait parameter is ignored so this call always wait for the snapshot to
2957 * complete before returning.
2958 *
2959 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2960 */
2961 int cmd_snapshot_record(struct ltt_session *session,
2962 struct lttng_snapshot_output *output, int wait)
2963 {
2964 int ret = LTTNG_OK;
2965 unsigned int use_tmp_output = 0;
2966 struct snapshot_output tmp_output;
2967 unsigned int nb_streams, snapshot_success = 0;
2968
2969 assert(session);
2970
2971 DBG("Cmd snapshot record for session %s", session->name);
2972
2973 /*
2974 * Permission denied to create an output if the session is not
2975 * set in no output mode.
2976 */
2977 if (session->output_traces) {
2978 ret = LTTNG_ERR_EPERM;
2979 goto error;
2980 }
2981
2982 /* The session needs to be started at least once. */
2983 if (!session->has_been_started) {
2984 ret = LTTNG_ERR_START_SESSION_ONCE;
2985 goto error;
2986 }
2987
2988 /* Use temporary output for the session. */
2989 if (output && *output->ctrl_url != '\0') {
2990 ret = snapshot_output_init(output->max_size, output->name,
2991 output->ctrl_url, output->data_url, session->consumer,
2992 &tmp_output, NULL);
2993 if (ret < 0) {
2994 if (ret == -ENOMEM) {
2995 ret = LTTNG_ERR_NOMEM;
2996 } else {
2997 ret = LTTNG_ERR_INVALID;
2998 }
2999 goto error;
3000 }
3001 /* Use the global session count for the temporary snapshot. */
3002 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3003 use_tmp_output = 1;
3004 }
3005
3006 /*
3007 * Get the total number of stream of that session which is used by the
3008 * maximum size of the snapshot feature.
3009 */
3010 nb_streams = get_total_nb_stream(session);
3011
3012 if (session->kernel_session) {
3013 struct ltt_kernel_session *ksess = session->kernel_session;
3014
3015 if (use_tmp_output) {
3016 ret = record_kernel_snapshot(ksess, &tmp_output, session,
3017 wait, nb_streams);
3018 if (ret != LTTNG_OK) {
3019 goto error;
3020 }
3021 snapshot_success = 1;
3022 } else {
3023 struct snapshot_output *sout;
3024 struct lttng_ht_iter iter;
3025
3026 rcu_read_lock();
3027 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3028 &iter.iter, sout, node.node) {
3029 /*
3030 * Make a local copy of the output and assign the possible
3031 * temporary value given by the caller.
3032 */
3033 memset(&tmp_output, 0, sizeof(tmp_output));
3034 memcpy(&tmp_output, sout, sizeof(tmp_output));
3035
3036 /* Use temporary max size. */
3037 if (output->max_size != (uint64_t) -1ULL) {
3038 tmp_output.max_size = output->max_size;
3039 }
3040
3041 /* Use temporary name. */
3042 if (*output->name != '\0') {
3043 strncpy(tmp_output.name, output->name,
3044 sizeof(tmp_output.name));
3045 }
3046
3047 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3048
3049 ret = record_kernel_snapshot(ksess, &tmp_output,
3050 session, wait, nb_streams);
3051 if (ret != LTTNG_OK) {
3052 rcu_read_unlock();
3053 goto error;
3054 }
3055 snapshot_success = 1;
3056 }
3057 rcu_read_unlock();
3058 }
3059 }
3060
3061 if (session->ust_session) {
3062 struct ltt_ust_session *usess = session->ust_session;
3063
3064 if (use_tmp_output) {
3065 ret = record_ust_snapshot(usess, &tmp_output, session,
3066 wait, nb_streams);
3067 if (ret != LTTNG_OK) {
3068 goto error;
3069 }
3070 snapshot_success = 1;
3071 } else {
3072 struct snapshot_output *sout;
3073 struct lttng_ht_iter iter;
3074
3075 rcu_read_lock();
3076 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3077 &iter.iter, sout, node.node) {
3078 /*
3079 * Make a local copy of the output and assign the possible
3080 * temporary value given by the caller.
3081 */
3082 memset(&tmp_output, 0, sizeof(tmp_output));
3083 memcpy(&tmp_output, sout, sizeof(tmp_output));
3084
3085 /* Use temporary max size. */
3086 if (output->max_size != (uint64_t) -1ULL) {
3087 tmp_output.max_size = output->max_size;
3088 }
3089
3090 /* Use temporary name. */
3091 if (*output->name != '\0') {
3092 strncpy(tmp_output.name, output->name,
3093 sizeof(tmp_output.name));
3094 }
3095
3096 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3097
3098 ret = record_ust_snapshot(usess, &tmp_output, session,
3099 wait, nb_streams);
3100 if (ret != LTTNG_OK) {
3101 rcu_read_unlock();
3102 goto error;
3103 }
3104 snapshot_success = 1;
3105 }
3106 rcu_read_unlock();
3107 }
3108 }
3109
3110 if (snapshot_success) {
3111 session->snapshot.nb_snapshot++;
3112 } else {
3113 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3114 }
3115
3116 error:
3117 return ret;
3118 }
3119
3120 /*
3121 * Init command subsystem.
3122 */
3123 void cmd_init(void)
3124 {
3125 /*
3126 * Set network sequence index to 1 for streams to match a relayd
3127 * socket on the consumer side.
3128 */
3129 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3130 relayd_net_seq_idx = 1;
3131 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
3132
3133 DBG("Command subsystem initialized");
3134 }
This page took 0.130124 seconds and 5 git commands to generate.