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