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