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