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