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