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