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