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