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