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