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