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