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