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