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