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