Fix: missing hostname context
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
CommitLineData
2f77fc4b
DG
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 <urcu/list.h>
21#include <urcu/uatomic.h>
22
23#include <common/defaults.h>
24#include <common/common.h>
25#include <common/sessiond-comm/sessiond-comm.h>
26#include <common/relayd/relayd.h>
27
28#include "channel.h"
29#include "consumer.h"
30#include "event.h"
31#include "kernel.h"
32#include "kernel-consumer.h"
33#include "lttng-sessiond.h"
34#include "utils.h"
35
36#include "cmd.h"
37
38/*
39 * Used to keep a unique index for each relayd socket created where this value
40 * is associated with streams on the consumer so it can match the right relayd
41 * to send to.
42 *
43 * This value should be incremented atomically for safety purposes and future
44 * possible concurrent access.
45 */
46static unsigned int relayd_net_seq_idx;
47
48/*
49 * Create a session path used by list_lttng_sessions for the case that the
50 * session consumer is on the network.
51 */
52static int build_network_session_path(char *dst, size_t size,
53 struct ltt_session *session)
54{
55 int ret, kdata_port, udata_port;
56 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
57 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
58
59 assert(session);
60 assert(dst);
61
62 memset(tmp_urls, 0, sizeof(tmp_urls));
63 memset(tmp_uurl, 0, sizeof(tmp_uurl));
64
65 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
66
67 if (session->kernel_session && session->kernel_session->consumer) {
68 kuri = &session->kernel_session->consumer->dst.net.control;
69 kdata_port = session->kernel_session->consumer->dst.net.data.port;
70 }
71
72 if (session->ust_session && session->ust_session->consumer) {
73 uuri = &session->ust_session->consumer->dst.net.control;
74 udata_port = session->ust_session->consumer->dst.net.data.port;
75 }
76
77 if (uuri == NULL && kuri == NULL) {
78 uri = &session->consumer->dst.net.control;
79 kdata_port = session->consumer->dst.net.data.port;
80 } else if (kuri && uuri) {
81 ret = uri_compare(kuri, uuri);
82 if (ret) {
83 /* Not Equal */
84 uri = kuri;
85 /* Build uuri URL string */
86 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
87 if (ret < 0) {
88 goto error;
89 }
90 } else {
91 uri = kuri;
92 }
93 } else if (kuri && uuri == NULL) {
94 uri = kuri;
95 } else if (uuri && kuri == NULL) {
96 uri = uuri;
97 }
98
99 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
100 if (ret < 0) {
101 goto error;
102 }
103
104 if (strlen(tmp_uurl) > 0) {
105 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
106 tmp_urls, kdata_port, tmp_uurl, udata_port);
107 } else {
108 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, kdata_port);
109 }
110
111error:
112 return ret;
113}
114
115/*
116 * Fill lttng_channel array of all channels.
117 */
118static void list_lttng_channels(int domain, struct ltt_session *session,
119 struct lttng_channel *channels)
120{
121 int i = 0;
122 struct ltt_kernel_channel *kchan;
123
124 DBG("Listing channels for session %s", session->name);
125
126 switch (domain) {
127 case LTTNG_DOMAIN_KERNEL:
128 /* Kernel channels */
129 if (session->kernel_session != NULL) {
130 cds_list_for_each_entry(kchan,
131 &session->kernel_session->channel_list.head, list) {
132 /* Copy lttng_channel struct to array */
133 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
134 channels[i].enabled = kchan->enabled;
135 i++;
136 }
137 }
138 break;
139 case LTTNG_DOMAIN_UST:
140 {
141 struct lttng_ht_iter iter;
142 struct ltt_ust_channel *uchan;
143
144 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
145 &iter.iter, uchan, node.node) {
146 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
147 channels[i].attr.overwrite = uchan->attr.overwrite;
148 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
149 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
150 channels[i].attr.switch_timer_interval =
151 uchan->attr.switch_timer_interval;
152 channels[i].attr.read_timer_interval =
153 uchan->attr.read_timer_interval;
154 channels[i].enabled = uchan->enabled;
155 switch (uchan->attr.output) {
156 case LTTNG_UST_MMAP:
157 default:
158 channels[i].attr.output = LTTNG_EVENT_MMAP;
159 break;
160 }
161 i++;
162 }
163 break;
164 }
165 default:
166 break;
167 }
168}
169
170/*
171 * Create a list of ust global domain events.
172 */
173static int list_lttng_ust_global_events(char *channel_name,
174 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
175{
176 int i = 0, ret = 0;
177 unsigned int nb_event = 0;
178 struct lttng_ht_iter iter;
179 struct lttng_ht_node_str *node;
180 struct ltt_ust_channel *uchan;
181 struct ltt_ust_event *uevent;
182 struct lttng_event *tmp;
183
184 DBG("Listing UST global events for channel %s", channel_name);
185
186 rcu_read_lock();
187
188 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
189 node = lttng_ht_iter_get_node_str(&iter);
190 if (node == NULL) {
191 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
192 goto error;
193 }
194
195 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
196
197 nb_event += lttng_ht_get_count(uchan->events);
198
199 if (nb_event == 0) {
200 ret = nb_event;
201 goto error;
202 }
203
204 DBG3("Listing UST global %d events", nb_event);
205
206 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
207 if (tmp == NULL) {
208 ret = -LTTCOMM_FATAL;
209 goto error;
210 }
211
212 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
213 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
214 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
215 tmp[i].enabled = uevent->enabled;
216
217 switch (uevent->attr.instrumentation) {
218 case LTTNG_UST_TRACEPOINT:
219 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
220 break;
221 case LTTNG_UST_PROBE:
222 tmp[i].type = LTTNG_EVENT_PROBE;
223 break;
224 case LTTNG_UST_FUNCTION:
225 tmp[i].type = LTTNG_EVENT_FUNCTION;
226 break;
227 }
228
229 tmp[i].loglevel = uevent->attr.loglevel;
230 switch (uevent->attr.loglevel_type) {
231 case LTTNG_UST_LOGLEVEL_ALL:
232 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
233 break;
234 case LTTNG_UST_LOGLEVEL_RANGE:
235 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
236 break;
237 case LTTNG_UST_LOGLEVEL_SINGLE:
238 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
239 break;
240 }
241 if (uevent->filter) {
242 tmp[i].filter = 1;
243 }
244 i++;
245 }
246
247 ret = nb_event;
248 *events = tmp;
249
250error:
251 rcu_read_unlock();
252 return ret;
253}
254
255/*
256 * Fill lttng_event array of all kernel events in the channel.
257 */
258static int list_lttng_kernel_events(char *channel_name,
259 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
260{
261 int i = 0, ret;
262 unsigned int nb_event;
263 struct ltt_kernel_event *event;
264 struct ltt_kernel_channel *kchan;
265
266 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
267 if (kchan == NULL) {
268 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
269 goto error;
270 }
271
272 nb_event = kchan->event_count;
273
274 DBG("Listing events for channel %s", kchan->channel->name);
275
276 if (nb_event == 0) {
277 ret = nb_event;
278 goto error;
279 }
280
281 *events = zmalloc(nb_event * sizeof(struct lttng_event));
282 if (*events == NULL) {
283 ret = LTTCOMM_FATAL;
284 goto error;
285 }
286
287 /* Kernel channels */
288 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
289 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
290 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
291 (*events)[i].enabled = event->enabled;
292
293 switch (event->event->instrumentation) {
294 case LTTNG_KERNEL_TRACEPOINT:
295 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
296 break;
297 case LTTNG_KERNEL_KPROBE:
298 case LTTNG_KERNEL_KRETPROBE:
299 (*events)[i].type = LTTNG_EVENT_PROBE;
300 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
301 sizeof(struct lttng_kernel_kprobe));
302 break;
303 case LTTNG_KERNEL_FUNCTION:
304 (*events)[i].type = LTTNG_EVENT_FUNCTION;
305 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
306 sizeof(struct lttng_kernel_function));
307 break;
308 case LTTNG_KERNEL_NOOP:
309 (*events)[i].type = LTTNG_EVENT_NOOP;
310 break;
311 case LTTNG_KERNEL_SYSCALL:
312 (*events)[i].type = LTTNG_EVENT_SYSCALL;
313 break;
314 case LTTNG_KERNEL_ALL:
315 assert(0);
316 break;
317 }
318 i++;
319 }
320
321 return nb_event;
322
323error:
324 return ret;
325}
326
327/*
328 * Add URI so the consumer output object. Set the correct path depending on the
329 * domain adding the default trace directory.
330 */
331static int add_uri_to_consumer(struct consumer_output *consumer,
332 struct lttng_uri *uri, int domain, const char *session_name)
333{
334 int ret = LTTCOMM_OK;
335 const char *default_trace_dir;
336
337 assert(uri);
338
339 if (consumer == NULL) {
340 DBG("No consumer detected. Don't add URI. Stopping.");
341 ret = LTTCOMM_NO_CONSUMER;
342 goto error;
343 }
344
345 switch (domain) {
346 case LTTNG_DOMAIN_KERNEL:
347 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
348 break;
349 case LTTNG_DOMAIN_UST:
350 default_trace_dir = DEFAULT_UST_TRACE_DIR;
351 break;
352 default:
353 /*
354 * This case is possible is we try to add the URI to the global tracing
355 * session consumer object which in this case there is no subdir.
356 */
357 default_trace_dir = "";
358 }
359
360 switch (uri->dtype) {
361 case LTTNG_DST_IPV4:
362 case LTTNG_DST_IPV6:
363 DBG2("Setting network URI to consumer");
364
365 /* Set URI into consumer output object */
366 ret = consumer_set_network_uri(consumer, uri);
367 if (ret < 0) {
368 ret = LTTCOMM_FATAL;
369 goto error;
370 } else if (ret == 1) {
371 /*
372 * URI was the same in the consumer so we do not append the subdir
373 * again so to not duplicate output dir.
374 */
375 goto error;
376 }
377
378 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
379 ret = consumer_set_subdir(consumer, session_name);
380 if (ret < 0) {
381 ret = LTTCOMM_FATAL;
382 goto error;
383 }
384 }
385
386 if (uri->stype == LTTNG_STREAM_CONTROL) {
387 /* On a new subdir, reappend the default trace dir. */
c30ce0b3
CB
388 strncat(consumer->subdir, default_trace_dir,
389 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
390 DBG3("Append domain trace name to subdir %s", consumer->subdir);
391 }
392
393 break;
394 case LTTNG_DST_PATH:
395 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
396 memset(consumer->dst.trace_path, 0,
397 sizeof(consumer->dst.trace_path));
398 strncpy(consumer->dst.trace_path, uri->dst.path,
399 sizeof(consumer->dst.trace_path));
400 /* Append default trace dir */
401 strncat(consumer->dst.trace_path, default_trace_dir,
c30ce0b3
CB
402 sizeof(consumer->dst.trace_path) -
403 strlen(consumer->dst.trace_path) - 1);
2f77fc4b
DG
404 /* Flag consumer as local. */
405 consumer->type = CONSUMER_DST_LOCAL;
406 break;
407 }
408
409error:
410 return ret;
411}
412
413/*
414 * Init tracing by creating trace directory and sending fds kernel consumer.
415 */
416static int init_kernel_tracing(struct ltt_kernel_session *session)
417{
418 int ret = 0;
419 struct lttng_ht_iter iter;
420 struct consumer_socket *socket;
421
422 assert(session);
423
424 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
425 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
426 socket, node.node) {
427 /* Code flow error */
428 assert(socket->fd >= 0);
429
430 pthread_mutex_lock(socket->lock);
431 ret = kernel_consumer_send_session(socket->fd, session);
432 pthread_mutex_unlock(socket->lock);
433 if (ret < 0) {
434 ret = LTTCOMM_KERN_CONSUMER_FAIL;
435 goto error;
436 }
437 }
438 }
439
440error:
441 return ret;
442}
443
444/*
445 * Create a socket to the relayd using the URI.
446 *
447 * On success, the relayd_sock pointer is set to the created socket.
448 * Else, it's stays untouched and a lttcomm error code is returned.
449 */
450static int create_connect_relayd(struct consumer_output *output,
451 const char *session_name, struct lttng_uri *uri,
452 struct lttcomm_sock **relayd_sock)
453{
454 int ret;
455 struct lttcomm_sock *sock;
456
457 /* Create socket object from URI */
458 sock = lttcomm_alloc_sock_from_uri(uri);
459 if (sock == NULL) {
460 ret = LTTCOMM_FATAL;
461 goto error;
462 }
463
464 ret = lttcomm_create_sock(sock);
465 if (ret < 0) {
466 ret = LTTCOMM_FATAL;
467 goto error;
468 }
469
470 /* Connect to relayd so we can proceed with a session creation. */
471 ret = relayd_connect(sock);
472 if (ret < 0) {
473 ERR("Unable to reach lttng-relayd");
474 ret = LTTCOMM_RELAYD_SESSION_FAIL;
475 goto free_sock;
476 }
477
478 /* Create socket for control stream. */
479 if (uri->stype == LTTNG_STREAM_CONTROL) {
480 DBG3("Creating relayd stream socket from URI");
481
482 /* Check relayd version */
483 ret = relayd_version_check(sock, RELAYD_VERSION_COMM_MAJOR,
484 RELAYD_VERSION_COMM_MINOR);
485 if (ret < 0) {
486 ret = LTTCOMM_RELAYD_VERSION_FAIL;
487 goto close_sock;
488 }
489 } else if (uri->stype == LTTNG_STREAM_DATA) {
490 DBG3("Creating relayd data socket from URI");
491 } else {
492 /* Command is not valid */
493 ERR("Relayd invalid stream type: %d", uri->stype);
494 ret = LTTCOMM_INVALID;
495 goto close_sock;
496 }
497
498 *relayd_sock = sock;
499
500 return LTTCOMM_OK;
501
502close_sock:
503 if (sock) {
504 (void) relayd_close(sock);
505 }
506free_sock:
507 if (sock) {
508 lttcomm_destroy_sock(sock);
509 }
510error:
511 return ret;
512}
513
514/*
515 * Connect to the relayd using URI and send the socket to the right consumer.
516 */
517static int send_consumer_relayd_socket(int domain, struct ltt_session *session,
518 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
519 int consumer_fd)
520{
521 int ret;
522 struct lttcomm_sock *sock = NULL;
523
524 /* Set the network sequence index if not set. */
525 if (consumer->net_seq_index == -1) {
526 /*
527 * Increment net_seq_idx because we are about to transfer the
528 * new relayd socket to the consumer.
529 */
530 uatomic_inc(&relayd_net_seq_idx);
531 /* Assign unique key so the consumer can match streams */
532 uatomic_set(&consumer->net_seq_index,
533 uatomic_read(&relayd_net_seq_idx));
534 }
535
536 /* Connect to relayd and make version check if uri is the control. */
537 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
538 if (ret != LTTCOMM_OK) {
539 goto close_sock;
540 }
541
542 /* If the control socket is connected, network session is ready */
543 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
544 session->net_handle = 1;
545 }
546
547 /* Send relayd socket to consumer. */
548 ret = consumer_send_relayd_socket(consumer_fd, sock,
549 consumer, relayd_uri->stype);
550 if (ret < 0) {
551 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
552 goto close_sock;
553 }
554
555 ret = LTTCOMM_OK;
556
557 /*
558 * Close socket which was dup on the consumer side. The session daemon does
559 * NOT keep track of the relayd socket(s) once transfer to the consumer.
560 */
561
562close_sock:
563 if (sock) {
564 (void) relayd_close(sock);
565 lttcomm_destroy_sock(sock);
566 }
567
568 return ret;
569}
570
571/*
572 * Send both relayd sockets to a specific consumer and domain. This is a
573 * helper function to facilitate sending the information to the consumer for a
574 * session.
575 */
576static int send_consumer_relayd_sockets(int domain,
577 struct ltt_session *session, struct consumer_output *consumer, int fd)
578{
579 int ret;
580
581 assert(session);
582 assert(consumer);
583
584 /* Don't resend the sockets to the consumer. */
585 if (consumer->dst.net.relayd_socks_sent) {
586 ret = LTTCOMM_OK;
587 goto error;
588 }
589
590 /* Sending control relayd socket. */
591 ret = send_consumer_relayd_socket(domain, session,
592 &consumer->dst.net.control, consumer, fd);
593 if (ret != LTTCOMM_OK) {
594 goto error;
595 }
596
597 /* Sending data relayd socket. */
598 ret = send_consumer_relayd_socket(domain, session,
599 &consumer->dst.net.data, consumer, fd);
600 if (ret != LTTCOMM_OK) {
601 goto error;
602 }
603
604 /* Flag that all relayd sockets were sent to the consumer. */
605 consumer->dst.net.relayd_socks_sent = 1;
606
607error:
608 return ret;
609}
610
611/*
612 * Setup relayd connections for a tracing session. First creates the socket to
613 * the relayd and send them to the right domain consumer. Consumer type MUST be
614 * network.
615 */
616static int setup_relayd(struct ltt_session *session)
617{
618 int ret = LTTCOMM_OK;
619 struct ltt_ust_session *usess;
620 struct ltt_kernel_session *ksess;
621 struct consumer_socket *socket;
622 struct lttng_ht_iter iter;
623
624 assert(session);
625
626 usess = session->ust_session;
627 ksess = session->kernel_session;
628
629 DBG2("Setting relayd for session %s", session->name);
630
631 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
632 && usess->consumer->enabled) {
633 /* For each consumer socket, send relayd sockets */
634 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
635 socket, node.node) {
636 /* Code flow error */
637 assert(socket->fd >= 0);
638
639 pthread_mutex_lock(socket->lock);
640 send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session,
641 usess->consumer, socket->fd);
642 pthread_mutex_unlock(socket->lock);
643 if (ret != LTTCOMM_OK) {
644 goto error;
645 }
646 }
647 }
648
649 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
650 && ksess->consumer->enabled) {
651 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
652 socket, node.node) {
653 /* Code flow error */
654 assert(socket->fd >= 0);
655
656 pthread_mutex_lock(socket->lock);
657 send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session,
658 ksess->consumer, socket->fd);
659 pthread_mutex_unlock(socket->lock);
660 if (ret != LTTCOMM_OK) {
661 goto error;
662 }
663 }
664 }
665
666error:
667 return ret;
668}
669
670/*
671 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
672 */
673int cmd_disable_channel(struct ltt_session *session, int domain,
674 char *channel_name)
675{
676 int ret;
677 struct ltt_ust_session *usess;
678
679 usess = session->ust_session;
680
681 switch (domain) {
682 case LTTNG_DOMAIN_KERNEL:
683 {
684 ret = channel_kernel_disable(session->kernel_session,
685 channel_name);
686 if (ret != LTTCOMM_OK) {
687 goto error;
688 }
689
690 kernel_wait_quiescent(kernel_tracer_fd);
691 break;
692 }
693 case LTTNG_DOMAIN_UST:
694 {
695 struct ltt_ust_channel *uchan;
696 struct lttng_ht *chan_ht;
697
698 chan_ht = usess->domain_global.channels;
699
700 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
701 if (uchan == NULL) {
702 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
703 goto error;
704 }
705
706 ret = channel_ust_disable(usess, domain, uchan);
707 if (ret != LTTCOMM_OK) {
708 goto error;
709 }
710 break;
711 }
712#if 0
713 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
714 case LTTNG_DOMAIN_UST_EXEC_NAME:
715 case LTTNG_DOMAIN_UST_PID:
716#endif
717 default:
718 ret = LTTCOMM_UNKNOWN_DOMAIN;
719 goto error;
720 }
721
722 ret = LTTCOMM_OK;
723
724error:
725 return ret;
726}
727
728/*
729 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
730 *
731 * The wpipe arguments is used as a notifier for the kernel thread.
732 */
733int cmd_enable_channel(struct ltt_session *session,
734 int domain, struct lttng_channel *attr, int wpipe)
735{
736 int ret;
737 struct ltt_ust_session *usess = session->ust_session;
738 struct lttng_ht *chan_ht;
739
740 assert(session);
741 assert(attr);
742
743 DBG("Enabling channel %s for session %s", attr->name, session->name);
744
745 switch (domain) {
746 case LTTNG_DOMAIN_KERNEL:
747 {
748 struct ltt_kernel_channel *kchan;
749
750 /* Mandatory for a kernel channel. */
751 assert(wpipe > 0);
752
753 kchan = trace_kernel_get_channel_by_name(attr->name,
754 session->kernel_session);
755 if (kchan == NULL) {
756 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
757 } else {
758 ret = channel_kernel_enable(session->kernel_session, kchan);
759 }
760
761 if (ret != LTTCOMM_OK) {
762 goto error;
763 }
764
765 kernel_wait_quiescent(kernel_tracer_fd);
766 break;
767 }
768 case LTTNG_DOMAIN_UST:
769 {
770 struct ltt_ust_channel *uchan;
771
772 chan_ht = usess->domain_global.channels;
773
774 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
775 if (uchan == NULL) {
776 ret = channel_ust_create(usess, domain, attr);
777 } else {
778 ret = channel_ust_enable(usess, domain, uchan);
779 }
780 break;
781 }
782#if 0
783 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
784 case LTTNG_DOMAIN_UST_EXEC_NAME:
785 case LTTNG_DOMAIN_UST_PID:
786#endif
787 default:
788 ret = LTTCOMM_UNKNOWN_DOMAIN;
789 goto error;
790 }
791
792error:
793 return ret;
794}
795
796
797/*
798 * Command LTTNG_DISABLE_EVENT processed by the client thread.
799 */
800int cmd_disable_event(struct ltt_session *session, int domain,
801 char *channel_name, char *event_name)
802{
803 int ret;
804
805 switch (domain) {
806 case LTTNG_DOMAIN_KERNEL:
807 {
808 struct ltt_kernel_channel *kchan;
809 struct ltt_kernel_session *ksess;
810
811 ksess = session->kernel_session;
812
813 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
814 if (kchan == NULL) {
815 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
816 goto error;
817 }
818
819 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
820 if (ret != LTTCOMM_OK) {
821 goto error;
822 }
823
824 kernel_wait_quiescent(kernel_tracer_fd);
825 break;
826 }
827 case LTTNG_DOMAIN_UST:
828 {
829 struct ltt_ust_channel *uchan;
830 struct ltt_ust_session *usess;
831
832 usess = session->ust_session;
833
834 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
835 channel_name);
836 if (uchan == NULL) {
837 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
838 goto error;
839 }
840
841 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
842 if (ret != LTTCOMM_OK) {
843 goto error;
844 }
845
846 DBG3("Disable UST event %s in channel %s completed", event_name,
847 channel_name);
848 break;
849 }
850#if 0
851 case LTTNG_DOMAIN_UST_EXEC_NAME:
852 case LTTNG_DOMAIN_UST_PID:
853 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
854#endif
855 default:
856 ret = LTTCOMM_UND;
857 goto error;
858 }
859
860 ret = LTTCOMM_OK;
861
862error:
863 return ret;
864}
865
866/*
867 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
868 */
869int cmd_disable_event_all(struct ltt_session *session, int domain,
870 char *channel_name)
871{
872 int ret;
873
874 switch (domain) {
875 case LTTNG_DOMAIN_KERNEL:
876 {
877 struct ltt_kernel_session *ksess;
878 struct ltt_kernel_channel *kchan;
879
880 ksess = session->kernel_session;
881
882 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
883 if (kchan == NULL) {
884 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
885 goto error;
886 }
887
888 ret = event_kernel_disable_all(ksess, kchan);
889 if (ret != LTTCOMM_OK) {
890 goto error;
891 }
892
893 kernel_wait_quiescent(kernel_tracer_fd);
894 break;
895 }
896 case LTTNG_DOMAIN_UST:
897 {
898 struct ltt_ust_session *usess;
899 struct ltt_ust_channel *uchan;
900
901 usess = session->ust_session;
902
903 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
904 channel_name);
905 if (uchan == NULL) {
906 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
907 goto error;
908 }
909
910 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
911 if (ret != 0) {
912 goto error;
913 }
914
915 DBG3("Disable all UST events in channel %s completed", channel_name);
916
917 break;
918 }
919#if 0
920 case LTTNG_DOMAIN_UST_EXEC_NAME:
921 case LTTNG_DOMAIN_UST_PID:
922 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
923#endif
924 default:
925 ret = LTTCOMM_UND;
926 goto error;
927 }
928
929 ret = LTTCOMM_OK;
930
931error:
932 return ret;
933}
934
935/*
936 * Command LTTNG_ADD_CONTEXT processed by the client thread.
937 */
938int cmd_add_context(struct ltt_session *session, int domain,
939 char *channel_name, char *event_name, struct lttng_event_context *ctx)
940{
941 int ret;
942
943 switch (domain) {
944 case LTTNG_DOMAIN_KERNEL:
945 /* Add kernel context to kernel tracer */
946 ret = context_kernel_add(session->kernel_session, ctx,
947 event_name, channel_name);
948 if (ret != LTTCOMM_OK) {
949 goto error;
950 }
951 break;
952 case LTTNG_DOMAIN_UST:
953 {
954 struct ltt_ust_session *usess = session->ust_session;
955
956 assert(usess);
957
958 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
959 if (ret != LTTCOMM_OK) {
960 goto error;
961 }
962 break;
963 }
964#if 0
965 case LTTNG_DOMAIN_UST_EXEC_NAME:
966 case LTTNG_DOMAIN_UST_PID:
967 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
968#endif
969 default:
970 ret = LTTCOMM_UND;
971 goto error;
972 }
973
974 ret = LTTCOMM_OK;
975
976error:
977 return ret;
978}
979
980/*
981 * Command LTTNG_SET_FILTER processed by the client thread.
982 */
983int cmd_set_filter(struct ltt_session *session, int domain,
984 char *channel_name, char *event_name,
985 struct lttng_filter_bytecode *bytecode)
986{
987 int ret;
988
989 switch (domain) {
990 case LTTNG_DOMAIN_KERNEL:
991 ret = LTTCOMM_FATAL;
992 break;
993 case LTTNG_DOMAIN_UST:
994 {
995 struct ltt_ust_session *usess = session->ust_session;
996
997 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
998 if (ret != LTTCOMM_OK) {
999 goto error;
1000 }
1001 break;
1002 }
1003#if 0
1004 case LTTNG_DOMAIN_UST_EXEC_NAME:
1005 case LTTNG_DOMAIN_UST_PID:
1006 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1007#endif
1008 default:
1009 ret = LTTCOMM_UND;
1010 goto error;
1011 }
1012
1013 ret = LTTCOMM_OK;
1014
1015error:
1016 return ret;
1017
1018}
1019
1020
1021/*
1022 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1023 */
1024int cmd_enable_event(struct ltt_session *session, int domain,
1025 char *channel_name, struct lttng_event *event, int wpipe)
1026{
1027 int ret;
1028 struct lttng_channel *attr;
1029
1030 assert(session);
1031 assert(event);
1032 assert(channel_name);
1033
1034 switch (domain) {
1035 case LTTNG_DOMAIN_KERNEL:
1036 {
1037 struct ltt_kernel_channel *kchan;
1038
1039 kchan = trace_kernel_get_channel_by_name(channel_name,
1040 session->kernel_session);
1041 if (kchan == NULL) {
1042 attr = channel_new_default_attr(domain);
1043 if (attr == NULL) {
1044 ret = LTTCOMM_FATAL;
1045 goto error;
1046 }
1047 strncpy(attr->name, channel_name, sizeof(attr->name));
1048
1049 ret = cmd_enable_channel(session, domain, attr, wpipe);
1050 if (ret != LTTCOMM_OK) {
1051 free(attr);
1052 goto error;
1053 }
1054 free(attr);
1055 }
1056
1057 /* Get the newly created kernel channel pointer */
1058 kchan = trace_kernel_get_channel_by_name(channel_name,
1059 session->kernel_session);
1060 if (kchan == NULL) {
1061 /* This sould not happen... */
1062 ret = LTTCOMM_FATAL;
1063 goto error;
1064 }
1065
1066 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
1067 event);
1068 if (ret != LTTCOMM_OK) {
1069 goto error;
1070 }
1071
1072 kernel_wait_quiescent(kernel_tracer_fd);
1073 break;
1074 }
1075 case LTTNG_DOMAIN_UST:
1076 {
1077 struct ltt_ust_channel *uchan;
1078 struct ltt_ust_session *usess = session->ust_session;
1079
1080 assert(usess);
1081
1082 /* Get channel from global UST domain */
1083 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1084 channel_name);
1085 if (uchan == NULL) {
1086 /* Create default channel */
1087 attr = channel_new_default_attr(domain);
1088 if (attr == NULL) {
1089 ret = LTTCOMM_FATAL;
1090 goto error;
1091 }
1092 strncpy(attr->name, channel_name, sizeof(attr->name));
1093
1094 ret = cmd_enable_channel(session, domain, attr, wpipe);
1095 if (ret != LTTCOMM_OK) {
1096 free(attr);
1097 goto error;
1098 }
1099 free(attr);
1100
1101 /* Get the newly created channel reference back */
1102 uchan = trace_ust_find_channel_by_name(
1103 usess->domain_global.channels, channel_name);
1104 assert(uchan);
1105 }
1106
1107 /* At this point, the session and channel exist on the tracer */
1108 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
1109 if (ret != LTTCOMM_OK) {
1110 goto error;
1111 }
1112 break;
1113 }
1114#if 0
1115 case LTTNG_DOMAIN_UST_EXEC_NAME:
1116 case LTTNG_DOMAIN_UST_PID:
1117 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1118#endif
1119 default:
1120 ret = LTTCOMM_UND;
1121 goto error;
1122 }
1123
1124 ret = LTTCOMM_OK;
1125
1126error:
1127 return ret;
1128}
1129
1130/*
1131 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1132 */
1133int cmd_enable_event_all(struct ltt_session *session, int domain,
1134 char *channel_name, int event_type, int wpipe)
1135{
1136 int ret;
1137 struct lttng_channel *attr;
1138
1139 assert(session);
1140 assert(channel_name);
1141
1142 switch (domain) {
1143 case LTTNG_DOMAIN_KERNEL:
1144 {
1145 struct ltt_kernel_channel *kchan;
1146
1147 assert(session->kernel_session);
1148
1149 kchan = trace_kernel_get_channel_by_name(channel_name,
1150 session->kernel_session);
1151 if (kchan == NULL) {
1152 /* Create default channel */
1153 attr = channel_new_default_attr(domain);
1154 if (attr == NULL) {
1155 ret = LTTCOMM_FATAL;
1156 goto error;
1157 }
1158 strncpy(attr->name, channel_name, sizeof(attr->name));
1159
1160 ret = cmd_enable_channel(session, domain, attr, wpipe);
1161 if (ret != LTTCOMM_OK) {
1162 free(attr);
1163 goto error;
1164 }
1165 free(attr);
1166
1167 /* Get the newly created kernel channel pointer */
1168 kchan = trace_kernel_get_channel_by_name(channel_name,
1169 session->kernel_session);
1170 assert(kchan);
1171 }
1172
1173 switch (event_type) {
1174 case LTTNG_EVENT_SYSCALL:
1175 ret = event_kernel_enable_all_syscalls(session->kernel_session,
1176 kchan, kernel_tracer_fd);
1177 break;
1178 case LTTNG_EVENT_TRACEPOINT:
1179 /*
1180 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1181 * events already registered to the channel.
1182 */
1183 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
1184 kchan, kernel_tracer_fd);
1185 break;
1186 case LTTNG_EVENT_ALL:
1187 /* Enable syscalls and tracepoints */
1188 ret = event_kernel_enable_all(session->kernel_session,
1189 kchan, kernel_tracer_fd);
1190 break;
1191 default:
1192 ret = LTTCOMM_KERN_ENABLE_FAIL;
1193 goto error;
1194 }
1195
1196 /* Manage return value */
1197 if (ret != LTTCOMM_OK) {
1198 goto error;
1199 }
1200
1201 kernel_wait_quiescent(kernel_tracer_fd);
1202 break;
1203 }
1204 case LTTNG_DOMAIN_UST:
1205 {
1206 struct ltt_ust_channel *uchan;
1207 struct ltt_ust_session *usess = session->ust_session;
1208
1209 assert(usess);
1210
1211 /* Get channel from global UST domain */
1212 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1213 channel_name);
1214 if (uchan == NULL) {
1215 /* Create default channel */
1216 attr = channel_new_default_attr(domain);
1217 if (attr == NULL) {
1218 ret = LTTCOMM_FATAL;
1219 goto error;
1220 }
1221 strncpy(attr->name, channel_name, sizeof(attr->name));
1222
1223 ret = cmd_enable_channel(session, domain, attr, wpipe);
1224 if (ret != LTTCOMM_OK) {
1225 free(attr);
1226 goto error;
1227 }
1228 free(attr);
1229
1230 /* Get the newly created channel reference back */
1231 uchan = trace_ust_find_channel_by_name(
1232 usess->domain_global.channels, channel_name);
1233 assert(uchan);
1234 }
1235
1236 /* At this point, the session and channel exist on the tracer */
1237
1238 switch (event_type) {
1239 case LTTNG_EVENT_ALL:
1240 case LTTNG_EVENT_TRACEPOINT:
1241 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
1242 if (ret != LTTCOMM_OK) {
1243 goto error;
1244 }
1245 break;
1246 default:
1247 ret = LTTCOMM_UST_ENABLE_FAIL;
1248 goto error;
1249 }
1250
1251 /* Manage return value */
1252 if (ret != LTTCOMM_OK) {
1253 goto error;
1254 }
1255
1256 break;
1257 }
1258#if 0
1259 case LTTNG_DOMAIN_UST_EXEC_NAME:
1260 case LTTNG_DOMAIN_UST_PID:
1261 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1262#endif
1263 default:
1264 ret = LTTCOMM_UND;
1265 goto error;
1266 }
1267
1268 ret = LTTCOMM_OK;
1269
1270error:
1271 return ret;
1272}
1273
1274
1275/*
1276 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1277 */
1278ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1279{
1280 int ret;
1281 ssize_t nb_events = 0;
1282
1283 switch (domain) {
1284 case LTTNG_DOMAIN_KERNEL:
1285 nb_events = kernel_list_events(kernel_tracer_fd, events);
1286 if (nb_events < 0) {
1287 ret = LTTCOMM_KERN_LIST_FAIL;
1288 goto error;
1289 }
1290 break;
1291 case LTTNG_DOMAIN_UST:
1292 nb_events = ust_app_list_events(events);
1293 if (nb_events < 0) {
1294 ret = LTTCOMM_UST_LIST_FAIL;
1295 goto error;
1296 }
1297 break;
1298 default:
1299 ret = LTTCOMM_UND;
1300 goto error;
1301 }
1302
1303 return nb_events;
1304
1305error:
1306 /* Return negative value to differentiate return code */
1307 return -ret;
1308}
1309
1310/*
1311 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1312 */
1313ssize_t cmd_list_tracepoint_fields(int domain,
1314 struct lttng_event_field **fields)
1315{
1316 int ret;
1317 ssize_t nb_fields = 0;
1318
1319 switch (domain) {
1320 case LTTNG_DOMAIN_UST:
1321 nb_fields = ust_app_list_event_fields(fields);
1322 if (nb_fields < 0) {
1323 ret = LTTCOMM_UST_LIST_FAIL;
1324 goto error;
1325 }
1326 break;
1327 case LTTNG_DOMAIN_KERNEL:
1328 default: /* fall-through */
1329 ret = LTTCOMM_UND;
1330 goto error;
1331 }
1332
1333 return nb_fields;
1334
1335error:
1336 /* Return negative value to differentiate return code */
1337 return -ret;
1338}
1339
1340/*
1341 * Command LTTNG_START_TRACE processed by the client thread.
1342 */
1343int cmd_start_trace(struct ltt_session *session)
1344{
1345 int ret;
1346 struct ltt_kernel_session *ksession;
1347 struct ltt_ust_session *usess;
1348 struct ltt_kernel_channel *kchan;
1349
1350 assert(session);
1351
1352 /* Ease our life a bit ;) */
1353 ksession = session->kernel_session;
1354 usess = session->ust_session;
1355
1356 if (session->enabled) {
1357 /* Already started. */
1358 ret = LTTCOMM_TRACE_ALREADY_STARTED;
1359 goto error;
1360 }
1361
1362 session->enabled = 1;
1363
1364 ret = setup_relayd(session);
1365 if (ret != LTTCOMM_OK) {
1366 ERR("Error setting up relayd for session %s", session->name);
1367 goto error;
1368 }
1369
1370 /* Kernel tracing */
1371 if (ksession != NULL) {
1372 /* Open kernel metadata */
1373 if (ksession->metadata == NULL) {
1374 ret = kernel_open_metadata(ksession);
1375 if (ret < 0) {
1376 ret = LTTCOMM_KERN_META_FAIL;
1377 goto error;
1378 }
1379 }
1380
1381 /* Open kernel metadata stream */
1382 if (ksession->metadata_stream_fd < 0) {
1383 ret = kernel_open_metadata_stream(ksession);
1384 if (ret < 0) {
1385 ERR("Kernel create metadata stream failed");
1386 ret = LTTCOMM_KERN_STREAM_FAIL;
1387 goto error;
1388 }
1389 }
1390
1391 /* For each channel */
1392 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1393 if (kchan->stream_count == 0) {
1394 ret = kernel_open_channel_stream(kchan);
1395 if (ret < 0) {
1396 ret = LTTCOMM_KERN_STREAM_FAIL;
1397 goto error;
1398 }
1399 /* Update the stream global counter */
1400 ksession->stream_count_global += ret;
1401 }
1402 }
1403
1404 /* Setup kernel consumer socket and send fds to it */
1405 ret = init_kernel_tracing(ksession);
1406 if (ret < 0) {
1407 ret = LTTCOMM_KERN_START_FAIL;
1408 goto error;
1409 }
1410
1411 /* This start the kernel tracing */
1412 ret = kernel_start_session(ksession);
1413 if (ret < 0) {
1414 ret = LTTCOMM_KERN_START_FAIL;
1415 goto error;
1416 }
1417
1418 /* Quiescent wait after starting trace */
1419 kernel_wait_quiescent(kernel_tracer_fd);
1420 }
1421
1422 /* Flag session that trace should start automatically */
1423 if (usess) {
1424 usess->start_trace = 1;
1425
1426 ret = ust_app_start_trace_all(usess);
1427 if (ret < 0) {
1428 ret = LTTCOMM_UST_START_FAIL;
1429 goto error;
1430 }
1431 }
1432
1433 ret = LTTCOMM_OK;
1434
1435error:
1436 return ret;
1437}
1438
1439/*
1440 * Command LTTNG_STOP_TRACE processed by the client thread.
1441 */
1442int cmd_stop_trace(struct ltt_session *session)
1443{
1444 int ret;
1445 struct ltt_kernel_channel *kchan;
1446 struct ltt_kernel_session *ksession;
1447 struct ltt_ust_session *usess;
1448
1449 assert(session);
1450
1451 /* Short cut */
1452 ksession = session->kernel_session;
1453 usess = session->ust_session;
1454
1455 if (!session->enabled) {
1456 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
1457 goto error;
1458 }
1459
1460 session->enabled = 0;
1461
1462 /* Kernel tracer */
1463 if (ksession) {
1464 DBG("Stop kernel tracing");
1465
1466 /* Flush metadata if exist */
1467 if (ksession->metadata_stream_fd >= 0) {
1468 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1469 if (ret < 0) {
1470 ERR("Kernel metadata flush failed");
1471 }
1472 }
1473
1474 /* Flush all buffers before stopping */
1475 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1476 ret = kernel_flush_buffer(kchan);
1477 if (ret < 0) {
1478 ERR("Kernel flush buffer error");
1479 }
1480 }
1481
1482 ret = kernel_stop_session(ksession);
1483 if (ret < 0) {
1484 ret = LTTCOMM_KERN_STOP_FAIL;
1485 goto error;
1486 }
1487
1488 kernel_wait_quiescent(kernel_tracer_fd);
1489 }
1490
1491 if (usess) {
1492 usess->start_trace = 0;
1493
1494 ret = ust_app_stop_trace_all(usess);
1495 if (ret < 0) {
1496 ret = LTTCOMM_UST_STOP_FAIL;
1497 goto error;
1498 }
1499 }
1500
1501 ret = LTTCOMM_OK;
1502
1503error:
1504 return ret;
1505}
1506
1507/*
1508 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1509 */
1510int cmd_set_consumer_uri(int domain, struct ltt_session *session,
1511 size_t nb_uri, struct lttng_uri *uris)
1512{
1513 int ret, i;
1514 struct ltt_kernel_session *ksess = session->kernel_session;
1515 struct ltt_ust_session *usess = session->ust_session;
1516 struct consumer_output *consumer = NULL;
1517
1518 assert(session);
1519 assert(uris);
1520 assert(nb_uri > 0);
1521
1522 /* Can't enable consumer after session started. */
1523 if (session->enabled) {
1524 ret = LTTCOMM_TRACE_ALREADY_STARTED;
1525 goto error;
1526 }
1527
1528 if (!session->start_consumer) {
1529 ret = LTTCOMM_NO_CONSUMER;
1530 goto error;
1531 }
1532
1533 /*
1534 * This case switch makes sure the domain session has a temporary consumer
1535 * so the URL can be set.
1536 */
1537 switch (domain) {
1538 case 0:
1539 /* Code flow error. A session MUST always have a consumer object */
1540 assert(session->consumer);
1541 /*
1542 * The URL will be added to the tracing session consumer instead of a
1543 * specific domain consumer.
1544 */
1545 consumer = session->consumer;
1546 break;
1547 case LTTNG_DOMAIN_KERNEL:
1548 /* Code flow error if we don't have a kernel session here. */
1549 assert(ksess);
1550
1551 /* Create consumer output if none exists */
1552 consumer = ksess->tmp_consumer;
1553 if (consumer == NULL) {
1554 consumer = consumer_copy_output(ksess->consumer);
1555 if (consumer == NULL) {
1556 ret = LTTCOMM_FATAL;
1557 goto error;
1558 }
1559 /* Trash the consumer subdir, we are about to set a new one. */
1560 memset(consumer->subdir, 0, sizeof(consumer->subdir));
1561 ksess->tmp_consumer = consumer;
1562 }
1563
1564 break;
1565 case LTTNG_DOMAIN_UST:
1566 /* Code flow error if we don't have a kernel session here. */
1567 assert(usess);
1568
1569 /* Create consumer output if none exists */
1570 consumer = usess->tmp_consumer;
1571 if (consumer == NULL) {
1572 consumer = consumer_copy_output(usess->consumer);
1573 if (consumer == NULL) {
1574 ret = LTTCOMM_FATAL;
1575 goto error;
1576 }
1577 /* Trash the consumer subdir, we are about to set a new one. */
1578 memset(consumer->subdir, 0, sizeof(consumer->subdir));
1579 usess->tmp_consumer = consumer;
1580 }
1581
1582 break;
1583 }
1584
1585 for (i = 0; i < nb_uri; i++) {
1586 struct consumer_socket *socket;
1587 struct lttng_ht_iter iter;
1588
1589 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
1590 if (ret < 0) {
1591 goto error;
1592 }
1593
1594 /*
1595 * Don't send relayd socket if URI is NOT remote or if the relayd
1596 * sockets for the session are already sent.
1597 */
1598 if (uris[i].dtype == LTTNG_DST_PATH ||
1599 consumer->dst.net.relayd_socks_sent) {
1600 continue;
1601 }
1602
1603 /* Try to send relayd URI to the consumer if exist. */
1604 rcu_read_lock();
1605 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
1606 socket, node.node) {
1607
1608 /* A socket in the HT should never have a negative fd */
1609 assert(socket->fd >= 0);
1610
1611 pthread_mutex_lock(socket->lock);
1612 ret = send_consumer_relayd_socket(domain, session, &uris[i],
1613 consumer, socket->fd);
1614 pthread_mutex_unlock(socket->lock);
1615 if (ret != LTTCOMM_OK) {
1616 rcu_read_unlock();
1617 goto error;
1618 }
1619 }
1620 rcu_read_unlock();
1621 }
1622
1623 /* All good! */
1624 ret = LTTCOMM_OK;
1625
1626error:
1627 return ret;
1628}
1629
1630/*
1631 * Command LTTNG_CREATE_SESSION processed by the client thread.
1632 */
1633int cmd_create_session_uri(char *name, struct lttng_uri *uris,
1634 size_t nb_uri, lttng_sock_cred *creds)
1635{
1636 int ret;
1637 char *path = NULL;
1638 struct ltt_session *session;
1639
1640 assert(name);
1641
1642 /*
1643 * Verify if the session already exist
1644 *
1645 * XXX: There is no need for the session lock list here since the caller
1646 * (process_client_msg) is holding it. We might want to change that so a
1647 * single command does not lock the entire session list.
1648 */
1649 session = session_find_by_name(name);
1650 if (session != NULL) {
1651 ret = LTTCOMM_EXIST_SESS;
1652 goto find_error;
1653 }
1654
1655 /* Create tracing session in the registry */
1656 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
1657 LTTNG_SOCK_GET_GID_CRED(creds));
1658 if (ret != LTTCOMM_OK) {
1659 goto session_error;
1660 }
1661
1662 /*
1663 * Get the newly created session pointer back
1664 *
1665 * XXX: There is no need for the session lock list here since the caller
1666 * (process_client_msg) is holding it. We might want to change that so a
1667 * single command does not lock the entire session list.
1668 */
1669 session = session_find_by_name(name);
1670 assert(session);
1671
1672 /* Create default consumer output for the session not yet created. */
1673 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1674 if (session->consumer == NULL) {
1675 ret = LTTCOMM_FATAL;
1676 goto consumer_error;
1677 }
1678
1679 /*
1680 * This means that the lttng_create_session call was called with the _path_
1681 * argument set to NULL.
1682 */
1683 if (uris == NULL) {
1684 /*
1685 * At this point, we'll skip the consumer URI setup and create a
1686 * session with a NULL path which will flag the session to NOT spawn a
1687 * consumer.
1688 */
1689 DBG("Create session %s with NO uri, skipping consumer setup", name);
1690 goto end;
1691 }
1692
1693 session->start_consumer = 1;
1694
1695 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
1696 if (ret != LTTCOMM_OK) {
1697 goto consumer_error;
1698 }
1699
1700 session->consumer->enabled = 1;
1701
1702end:
1703 return LTTCOMM_OK;
1704
1705consumer_error:
1706 session_destroy(session);
1707session_error:
1708find_error:
1709 return ret;
1710}
1711
1712/*
1713 * Command LTTNG_DESTROY_SESSION processed by the client thread.
1714 */
1715int cmd_destroy_session(struct ltt_session *session, int wpipe)
1716{
1717 int ret;
1718 struct ltt_ust_session *usess;
1719 struct ltt_kernel_session *ksess;
1720
1721 /* Safety net */
1722 assert(session);
1723
1724 usess = session->ust_session;
1725 ksess = session->kernel_session;
1726
1727 /* Clean kernel session teardown */
1728 kernel_destroy_session(ksess);
1729
1730 /* UST session teardown */
1731 if (usess) {
1732 /* Close any relayd session */
1733 consumer_output_send_destroy_relayd(usess->consumer);
1734
1735 /* Destroy every UST application related to this session. */
1736 ret = ust_app_destroy_trace_all(usess);
1737 if (ret) {
1738 ERR("Error in ust_app_destroy_trace_all");
1739 }
1740
1741 /* Clean up the rest. */
1742 trace_ust_destroy_session(usess);
1743 }
1744
1745 /*
1746 * Must notify the kernel thread here to update it's poll set in order to
1747 * remove the channel(s)' fd just destroyed.
1748 */
1749 ret = notify_thread_pipe(wpipe);
1750 if (ret < 0) {
1751 PERROR("write kernel poll pipe");
1752 }
1753
1754 ret = session_destroy(session);
1755
1756 return ret;
1757}
1758
1759/*
1760 * Command LTTNG_CALIBRATE processed by the client thread.
1761 */
1762int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
1763{
1764 int ret;
1765
1766 switch (domain) {
1767 case LTTNG_DOMAIN_KERNEL:
1768 {
1769 struct lttng_kernel_calibrate kcalibrate;
1770
1771 kcalibrate.type = calibrate->type;
1772 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
1773 if (ret < 0) {
1774 ret = LTTCOMM_KERN_ENABLE_FAIL;
1775 goto error;
1776 }
1777 break;
1778 }
1779 case LTTNG_DOMAIN_UST:
1780 {
1781 struct lttng_ust_calibrate ucalibrate;
1782
1783 ucalibrate.type = calibrate->type;
1784 ret = ust_app_calibrate_glb(&ucalibrate);
1785 if (ret < 0) {
1786 ret = LTTCOMM_UST_CALIBRATE_FAIL;
1787 goto error;
1788 }
1789 break;
1790 }
1791 default:
1792 ret = LTTCOMM_UND;
1793 goto error;
1794 }
1795
1796 ret = LTTCOMM_OK;
1797
1798error:
1799 return ret;
1800}
1801
1802/*
1803 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
1804 */
1805int cmd_register_consumer(struct ltt_session *session, int domain,
1806 const char *sock_path, struct consumer_data *cdata)
1807{
1808 int ret, sock;
1809 struct consumer_socket *socket;
1810
1811 assert(session);
1812 assert(cdata);
1813 assert(sock_path);
1814
1815 switch (domain) {
1816 case LTTNG_DOMAIN_KERNEL:
1817 {
1818 struct ltt_kernel_session *ksess = session->kernel_session;
1819
1820 assert(ksess);
1821
1822 /* Can't register a consumer if there is already one */
1823 if (ksess->consumer_fds_sent != 0) {
1824 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1825 goto error;
1826 }
1827
1828 sock = lttcomm_connect_unix_sock(sock_path);
1829 if (sock < 0) {
1830 ret = LTTCOMM_CONNECT_FAIL;
1831 goto error;
1832 }
1833
1834 socket = consumer_allocate_socket(sock);
1835 if (socket == NULL) {
1836 ret = LTTCOMM_FATAL;
1837 close(sock);
1838 goto error;
1839 }
1840
1841 socket->lock = zmalloc(sizeof(pthread_mutex_t));
1842 if (socket->lock == NULL) {
1843 PERROR("zmalloc pthread mutex");
1844 ret = LTTCOMM_FATAL;
1845 goto error;
1846 }
1847 pthread_mutex_init(socket->lock, NULL);
1848 socket->registered = 1;
1849
1850 rcu_read_lock();
1851 consumer_add_socket(socket, ksess->consumer);
1852 rcu_read_unlock();
1853
1854 pthread_mutex_lock(&cdata->pid_mutex);
1855 cdata->pid = -1;
1856 pthread_mutex_unlock(&cdata->pid_mutex);
1857
1858 break;
1859 }
1860 default:
1861 /* TODO: Userspace tracing */
1862 ret = LTTCOMM_UND;
1863 goto error;
1864 }
1865
1866 ret = LTTCOMM_OK;
1867
1868error:
1869 return ret;
1870}
1871
1872/*
1873 * Command LTTNG_LIST_DOMAINS processed by the client thread.
1874 */
1875ssize_t cmd_list_domains(struct ltt_session *session,
1876 struct lttng_domain **domains)
1877{
1878 int ret, index = 0;
1879 ssize_t nb_dom = 0;
1880
1881 if (session->kernel_session != NULL) {
1882 DBG3("Listing domains found kernel domain");
1883 nb_dom++;
1884 }
1885
1886 if (session->ust_session != NULL) {
1887 DBG3("Listing domains found UST global domain");
1888 nb_dom++;
1889 }
1890
1891 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
1892 if (*domains == NULL) {
1893 ret = -LTTCOMM_FATAL;
1894 goto error;
1895 }
1896
1897 if (session->kernel_session != NULL) {
1898 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
1899 index++;
1900 }
1901
1902 if (session->ust_session != NULL) {
1903 (*domains)[index].type = LTTNG_DOMAIN_UST;
1904 index++;
1905 }
1906
1907 return nb_dom;
1908
1909error:
1910 return ret;
1911}
1912
1913
1914/*
1915 * Command LTTNG_LIST_CHANNELS processed by the client thread.
1916 */
1917ssize_t cmd_list_channels(int domain, struct ltt_session *session,
1918 struct lttng_channel **channels)
1919{
1920 int ret;
1921 ssize_t nb_chan = 0;
1922
1923 switch (domain) {
1924 case LTTNG_DOMAIN_KERNEL:
1925 if (session->kernel_session != NULL) {
1926 nb_chan = session->kernel_session->channel_count;
1927 }
1928 DBG3("Number of kernel channels %zd", nb_chan);
1929 break;
1930 case LTTNG_DOMAIN_UST:
1931 if (session->ust_session != NULL) {
1932 nb_chan = lttng_ht_get_count(
1933 session->ust_session->domain_global.channels);
1934 }
1935 DBG3("Number of UST global channels %zd", nb_chan);
1936 break;
1937 default:
1938 *channels = NULL;
1939 ret = -LTTCOMM_UND;
1940 goto error;
1941 }
1942
1943 if (nb_chan > 0) {
1944 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
1945 if (*channels == NULL) {
1946 ret = -LTTCOMM_FATAL;
1947 goto error;
1948 }
1949
1950 list_lttng_channels(domain, session, *channels);
1951 } else {
1952 *channels = NULL;
1953 }
1954
1955 return nb_chan;
1956
1957error:
1958 return ret;
1959}
1960
1961/*
1962 * Command LTTNG_LIST_EVENTS processed by the client thread.
1963 */
1964ssize_t cmd_list_events(int domain, struct ltt_session *session,
1965 char *channel_name, struct lttng_event **events)
1966{
1967 int ret = 0;
1968 ssize_t nb_event = 0;
1969
1970 switch (domain) {
1971 case LTTNG_DOMAIN_KERNEL:
1972 if (session->kernel_session != NULL) {
1973 nb_event = list_lttng_kernel_events(channel_name,
1974 session->kernel_session, events);
1975 }
1976 break;
1977 case LTTNG_DOMAIN_UST:
1978 {
1979 if (session->ust_session != NULL) {
1980 nb_event = list_lttng_ust_global_events(channel_name,
1981 &session->ust_session->domain_global, events);
1982 }
1983 break;
1984 }
1985 default:
1986 ret = -LTTCOMM_UND;
1987 goto error;
1988 }
1989
1990 ret = nb_event;
1991
1992error:
1993 return ret;
1994}
1995
1996/*
1997 * Using the session list, filled a lttng_session array to send back to the
1998 * client for session listing.
1999 *
2000 * The session list lock MUST be acquired before calling this function. Use
2001 * session_lock_list() and session_unlock_list().
2002 */
2003void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2004 gid_t gid)
2005{
2006 int ret;
2007 unsigned int i = 0;
2008 struct ltt_session *session;
2009 struct ltt_session_list *list = session_get_list();
2010
2011 DBG("Getting all available session for UID %d GID %d",
2012 uid, gid);
2013 /*
2014 * Iterate over session list and append data after the control struct in
2015 * the buffer.
2016 */
2017 cds_list_for_each_entry(session, &list->head, list) {
2018 /*
2019 * Only list the sessions the user can control.
2020 */
2021 if (!session_access_ok(session, uid, gid)) {
2022 continue;
2023 }
2024
2025 struct ltt_kernel_session *ksess = session->kernel_session;
2026 struct ltt_ust_session *usess = session->ust_session;
2027
2028 if (session->consumer->type == CONSUMER_DST_NET ||
2029 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2030 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2031 ret = build_network_session_path(sessions[i].path,
2032 sizeof(session[i].path), session);
2033 } else {
2034 ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s",
2035 session->consumer->dst.trace_path);
2036 }
2037 if (ret < 0) {
2038 PERROR("snprintf session path");
2039 continue;
2040 }
2041
2042 strncpy(sessions[i].name, session->name, NAME_MAX);
2043 sessions[i].name[NAME_MAX - 1] = '\0';
2044 sessions[i].enabled = session->enabled;
2045 i++;
2046 }
2047}
2048
2049/*
2050 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
2051 */
2052int cmd_disable_consumer(int domain, struct ltt_session *session)
2053{
2054 int ret;
2055 struct ltt_kernel_session *ksess = session->kernel_session;
2056 struct ltt_ust_session *usess = session->ust_session;
2057 struct consumer_output *consumer;
2058
2059 assert(session);
2060
2061 if (session->enabled) {
2062 /* Can't disable consumer on an already started session */
2063 ret = LTTCOMM_TRACE_ALREADY_STARTED;
2064 goto error;
2065 }
2066
2067 if (!session->start_consumer) {
2068 ret = LTTCOMM_NO_CONSUMER;
2069 goto error;
2070 }
2071
2072 switch (domain) {
2073 case 0:
2074 DBG("Disable tracing session %s consumer", session->name);
2075 consumer = session->consumer;
2076 break;
2077 case LTTNG_DOMAIN_KERNEL:
2078 /* Code flow error if we don't have a kernel session here. */
2079 assert(ksess);
2080
2081 DBG("Disabling kernel consumer");
2082 consumer = ksess->consumer;
2083
2084 break;
2085 case LTTNG_DOMAIN_UST:
2086 /* Code flow error if we don't have a UST session here. */
2087 assert(usess);
2088
2089 DBG("Disabling UST consumer");
2090 consumer = usess->consumer;
2091
2092 break;
2093 default:
2094 ret = LTTCOMM_UNKNOWN_DOMAIN;
2095 goto error;
2096 }
2097
2098 if (consumer) {
2099 consumer->enabled = 0;
2100 /* Success at this point */
2101 ret = LTTCOMM_OK;
2102 } else {
2103 ret = LTTCOMM_NO_CONSUMER;
2104 }
2105
2106error:
2107 return ret;
2108}
2109
2110/*
2111 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
2112 */
2113int cmd_enable_consumer(int domain, struct ltt_session *session)
2114{
2115 int ret;
2116 struct ltt_kernel_session *ksess = session->kernel_session;
2117 struct ltt_ust_session *usess = session->ust_session;
2118 struct consumer_output *consumer = NULL;
2119
2120 assert(session);
2121
2122 /* Can't enable consumer after session started. */
2123 if (session->enabled) {
2124 ret = LTTCOMM_TRACE_ALREADY_STARTED;
2125 goto error;
2126 }
2127
2128 if (!session->start_consumer) {
2129 ret = LTTCOMM_NO_CONSUMER;
2130 goto error;
2131 }
2132
2133 switch (domain) {
2134 case 0:
2135 assert(session->consumer);
2136 consumer = session->consumer;
2137 break;
2138 case LTTNG_DOMAIN_KERNEL:
2139 /* Code flow error if we don't have a kernel session here. */
2140 assert(ksess);
2141
2142 /*
2143 * Check if we have already sent fds to the consumer. In that case,
2144 * the enable-consumer command can't be used because a start trace
2145 * had previously occured.
2146 */
2147 if (ksess->consumer_fds_sent) {
2148 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2149 goto error;
2150 }
2151
2152 consumer = ksess->tmp_consumer;
2153 if (consumer == NULL) {
2154 ret = LTTCOMM_OK;
2155 /* No temp. consumer output exists. Using the current one. */
2156 DBG3("No temporary consumer. Using default");
2157 consumer = ksess->consumer;
2158 goto error;
2159 }
2160
2161 switch (consumer->type) {
2162 case CONSUMER_DST_LOCAL:
2163 DBG2("Consumer output is local. Creating directory(ies)");
2164
2165 /* Create directory(ies) */
2166 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
2167 S_IRWXU | S_IRWXG, session->uid, session->gid);
2168 if (ret < 0) {
2169 if (ret != -EEXIST) {
2170 ERR("Trace directory creation error");
2171 ret = LTTCOMM_FATAL;
2172 goto error;
2173 }
2174 }
2175 break;
2176 case CONSUMER_DST_NET:
2177 DBG2("Consumer output is network. Validating URIs");
2178 /* Validate if we have both control and data path set. */
2179 if (!consumer->dst.net.control_isset) {
2180 ret = LTTCOMM_URL_CTRL_MISS;
2181 goto error;
2182 }
2183
2184 if (!consumer->dst.net.data_isset) {
2185 ret = LTTCOMM_URL_DATA_MISS;
2186 goto error;
2187 }
2188
2189 /* Check established network session state */
2190 if (session->net_handle == 0) {
2191 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2192 ERR("Session network handle is not set on enable-consumer");
2193 goto error;
2194 }
2195
2196 break;
2197 }
2198
2199 /* Append default kernel trace dir to subdir */
2200 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
c30ce0b3
CB
2201 sizeof(ksess->consumer->subdir) -
2202 strlen(ksess->consumer->subdir) - 1);
2f77fc4b
DG
2203
2204 /*
2205 * @session-lock
2206 * This is race free for now since the session lock is acquired before
2207 * ending up in this function. No other threads can access this kernel
2208 * session without this lock hence freeing the consumer output object
2209 * is valid.
2210 */
2211 rcu_read_lock();
2212 consumer_destroy_output(ksess->consumer);
2213 rcu_read_unlock();
2214 ksess->consumer = consumer;
2215 ksess->tmp_consumer = NULL;
2216
2217 break;
2218 case LTTNG_DOMAIN_UST:
2219 /* Code flow error if we don't have a UST session here. */
2220 assert(usess);
2221
2222 /*
2223 * Check if we have already sent fds to the consumer. In that case,
2224 * the enable-consumer command can't be used because a start trace
2225 * had previously occured.
2226 */
2227 if (usess->start_trace) {
2228 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2229 goto error;
2230 }
2231
2232 consumer = usess->tmp_consumer;
2233 if (consumer == NULL) {
2234 ret = LTTCOMM_OK;
2235 /* No temp. consumer output exists. Using the current one. */
2236 DBG3("No temporary consumer. Using default");
2237 consumer = usess->consumer;
2238 goto error;
2239 }
2240
2241 switch (consumer->type) {
2242 case CONSUMER_DST_LOCAL:
2243 DBG2("Consumer output is local. Creating directory(ies)");
2244
2245 /* Create directory(ies) */
2246 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
2247 S_IRWXU | S_IRWXG, session->uid, session->gid);
2248 if (ret < 0) {
2249 if (ret != -EEXIST) {
2250 ERR("Trace directory creation error");
2251 ret = LTTCOMM_FATAL;
2252 goto error;
2253 }
2254 }
2255 break;
2256 case CONSUMER_DST_NET:
2257 DBG2("Consumer output is network. Validating URIs");
2258 /* Validate if we have both control and data path set. */
2259 if (!consumer->dst.net.control_isset) {
2260 ret = LTTCOMM_URL_CTRL_MISS;
2261 goto error;
2262 }
2263
2264 if (!consumer->dst.net.data_isset) {
2265 ret = LTTCOMM_URL_DATA_MISS;
2266 goto error;
2267 }
2268
2269 /* Check established network session state */
2270 if (session->net_handle == 0) {
2271 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2272 DBG2("Session network handle is not set on enable-consumer");
2273 goto error;
2274 }
2275
2276 if (consumer->net_seq_index == -1) {
2277 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2278 DBG2("Network index is not set on the consumer");
2279 goto error;
2280 }
2281
2282 break;
2283 }
2284
2285 /* Append default kernel trace dir to subdir */
2286 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
c30ce0b3
CB
2287 sizeof(usess->consumer->subdir) -
2288 strlen(usess->consumer->subdir) - 1);
2f77fc4b
DG
2289
2290 /*
2291 * @session-lock
2292 * This is race free for now since the session lock is acquired before
2293 * ending up in this function. No other threads can access this kernel
2294 * session without this lock hence freeing the consumer output object
2295 * is valid.
2296 */
2297 rcu_read_lock();
2298 consumer_destroy_output(usess->consumer);
2299 rcu_read_unlock();
2300 usess->consumer = consumer;
2301 usess->tmp_consumer = NULL;
2302
2303 break;
2304 }
2305
2306 /* Enable it */
2307 if (consumer) {
2308 consumer->enabled = 1;
2309 /* Success at this point */
2310 ret = LTTCOMM_OK;
2311 } else {
2312 /* Should not really happend... */
2313 ret = LTTCOMM_NO_CONSUMER;
2314 }
2315
2316error:
2317 return ret;
2318}
2319
2320/*
2321 * Init command subsystem.
2322 */
2323void cmd_init(void)
2324{
2325 /*
2326 * Set network sequence index to 1 for streams to match a relayd socket on
2327 * the consumer side.
2328 */
2329 uatomic_set(&relayd_net_seq_idx, 1);
2330
2331 DBG("Command subsystem initialized");
2332}
This page took 0.183017 seconds and 4 git commands to generate.