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