Fix: don't set enabled flag is session start fails
[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
a5a2d0c2
DG
853 /*
854 * Don't try to enable a channel if the session has been started at
855 * some point in time before. The tracer does not allow it.
856 */
857 if (session->has_been_started) {
858 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
859 goto error;
860 }
861
1124da52
DG
862 /*
863 * The ringbuffer (both in user space and kernel) behave badly in overwrite
864 * mode and with less than 2 subbuf so block it right away and send back an
865 * invalid attribute error.
866 */
867 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
868 ret = LTTNG_ERR_INVALID;
869 goto error;
870 }
871
7972aab2 872 switch (domain->type) {
2f77fc4b
DG
873 case LTTNG_DOMAIN_KERNEL:
874 {
875 struct ltt_kernel_channel *kchan;
876
2f77fc4b
DG
877 kchan = trace_kernel_get_channel_by_name(attr->name,
878 session->kernel_session);
879 if (kchan == NULL) {
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) {
7972aab2 903 ret = channel_ust_create(usess, attr, domain->buf_type);
85076754
MD
904 if (attr->name[0] != '\0') {
905 usess->has_non_default_channel = 1;
906 }
2f77fc4b 907 } else {
7972aab2 908 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
909 }
910 break;
911 }
2f77fc4b 912 default:
f73fabfd 913 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
914 goto error;
915 }
916
917error:
2223c96f 918 rcu_read_unlock();
2f77fc4b
DG
919 return ret;
920}
921
922
923/*
924 * Command LTTNG_DISABLE_EVENT processed by the client thread.
925 */
926int cmd_disable_event(struct ltt_session *session, int domain,
927 char *channel_name, char *event_name)
928{
929 int ret;
930
2223c96f
DG
931 rcu_read_lock();
932
2f77fc4b
DG
933 switch (domain) {
934 case LTTNG_DOMAIN_KERNEL:
935 {
936 struct ltt_kernel_channel *kchan;
937 struct ltt_kernel_session *ksess;
938
939 ksess = session->kernel_session;
940
85076754
MD
941 /*
942 * If a non-default channel has been created in the
943 * session, explicitely require that -c chan_name needs
944 * to be provided.
945 */
946 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
947 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
948 goto error;
949 }
950
2f77fc4b
DG
951 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
952 if (kchan == NULL) {
f73fabfd 953 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
954 goto error;
955 }
956
d3a56674 957 ret = event_kernel_disable_tracepoint(kchan, event_name);
f73fabfd 958 if (ret != LTTNG_OK) {
2f77fc4b
DG
959 goto error;
960 }
961
962 kernel_wait_quiescent(kernel_tracer_fd);
963 break;
964 }
965 case LTTNG_DOMAIN_UST:
966 {
967 struct ltt_ust_channel *uchan;
968 struct ltt_ust_session *usess;
969
970 usess = session->ust_session;
971
85076754
MD
972 /*
973 * If a non-default channel has been created in the
974 * session, explicitely require that -c chan_name needs
975 * to be provided.
976 */
977 if (usess->has_non_default_channel && channel_name[0] == '\0') {
978 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
979 goto error;
980 }
981
2f77fc4b
DG
982 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
983 channel_name);
984 if (uchan == NULL) {
f73fabfd 985 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
986 goto error;
987 }
988
7972aab2 989 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
f73fabfd 990 if (ret != LTTNG_OK) {
2f77fc4b
DG
991 goto error;
992 }
993
994 DBG3("Disable UST event %s in channel %s completed", event_name,
995 channel_name);
996 break;
997 }
998#if 0
999 case LTTNG_DOMAIN_UST_EXEC_NAME:
1000 case LTTNG_DOMAIN_UST_PID:
1001 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1002#endif
1003 default:
f73fabfd 1004 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1005 goto error;
1006 }
1007
f73fabfd 1008 ret = LTTNG_OK;
2f77fc4b
DG
1009
1010error:
2223c96f 1011 rcu_read_unlock();
2f77fc4b
DG
1012 return ret;
1013}
1014
1015/*
1016 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1017 */
1018int cmd_disable_event_all(struct ltt_session *session, int domain,
1019 char *channel_name)
1020{
1021 int ret;
1022
2223c96f
DG
1023 rcu_read_lock();
1024
2f77fc4b
DG
1025 switch (domain) {
1026 case LTTNG_DOMAIN_KERNEL:
1027 {
1028 struct ltt_kernel_session *ksess;
1029 struct ltt_kernel_channel *kchan;
1030
1031 ksess = session->kernel_session;
1032
85076754
MD
1033 /*
1034 * If a non-default channel has been created in the
1035 * session, explicitely require that -c chan_name needs
1036 * to be provided.
1037 */
1038 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1039 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1040 goto error;
1041 }
1042
2f77fc4b
DG
1043 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1044 if (kchan == NULL) {
f73fabfd 1045 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
1046 goto error;
1047 }
1048
d3a56674 1049 ret = event_kernel_disable_all(kchan);
f73fabfd 1050 if (ret != LTTNG_OK) {
2f77fc4b
DG
1051 goto error;
1052 }
1053
1054 kernel_wait_quiescent(kernel_tracer_fd);
1055 break;
1056 }
1057 case LTTNG_DOMAIN_UST:
1058 {
1059 struct ltt_ust_session *usess;
1060 struct ltt_ust_channel *uchan;
1061
1062 usess = session->ust_session;
1063
85076754
MD
1064 /*
1065 * If a non-default channel has been created in the
1066 * session, explicitely require that -c chan_name needs
1067 * to be provided.
1068 */
1069 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1070 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1071 goto error;
1072 }
1073
2f77fc4b
DG
1074 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1075 channel_name);
1076 if (uchan == NULL) {
f73fabfd 1077 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1078 goto error;
1079 }
1080
7972aab2 1081 ret = event_ust_disable_all_tracepoints(usess, uchan);
2f77fc4b
DG
1082 if (ret != 0) {
1083 goto error;
1084 }
1085
1086 DBG3("Disable all UST events in channel %s completed", channel_name);
1087
1088 break;
1089 }
1090#if 0
1091 case LTTNG_DOMAIN_UST_EXEC_NAME:
1092 case LTTNG_DOMAIN_UST_PID:
1093 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1094#endif
1095 default:
f73fabfd 1096 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1097 goto error;
1098 }
1099
f73fabfd 1100 ret = LTTNG_OK;
2f77fc4b
DG
1101
1102error:
2223c96f 1103 rcu_read_unlock();
2f77fc4b
DG
1104 return ret;
1105}
1106
1107/*
1108 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1109 */
1110int cmd_add_context(struct ltt_session *session, int domain,
601d5acf 1111 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1112{
d5979e4a 1113 int ret, chan_kern_created = 0, chan_ust_created = 0;
2f77fc4b
DG
1114
1115 switch (domain) {
1116 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
1117 assert(session->kernel_session);
1118
1119 if (session->kernel_session->channel_count == 0) {
1120 /* Create default channel */
1121 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1122 if (ret != LTTNG_OK) {
1123 goto error;
1124 }
d5979e4a 1125 chan_kern_created = 1;
979e618e 1126 }
2f77fc4b 1127 /* Add kernel context to kernel tracer */
601d5acf 1128 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1129 if (ret != LTTNG_OK) {
2f77fc4b
DG
1130 goto error;
1131 }
1132 break;
1133 case LTTNG_DOMAIN_UST:
1134 {
1135 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1136 unsigned int chan_count;
1137
2f77fc4b
DG
1138 assert(usess);
1139
85076754 1140 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1141 if (chan_count == 0) {
1142 struct lttng_channel *attr;
1143 /* Create default channel */
0a9c6494 1144 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1145 if (attr == NULL) {
1146 ret = LTTNG_ERR_FATAL;
1147 goto error;
1148 }
1149
7972aab2 1150 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1151 if (ret != LTTNG_OK) {
1152 free(attr);
1153 goto error;
1154 }
1155 free(attr);
d5979e4a 1156 chan_ust_created = 1;
979e618e
DG
1157 }
1158
601d5acf 1159 ret = context_ust_add(usess, domain, ctx, channel_name);
f73fabfd 1160 if (ret != LTTNG_OK) {
2f77fc4b
DG
1161 goto error;
1162 }
1163 break;
1164 }
1165#if 0
1166 case LTTNG_DOMAIN_UST_EXEC_NAME:
1167 case LTTNG_DOMAIN_UST_PID:
1168 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1169#endif
1170 default:
f73fabfd 1171 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1172 goto error;
1173 }
1174
d5979e4a 1175 return LTTNG_OK;
2f77fc4b
DG
1176
1177error:
d5979e4a
DG
1178 if (chan_kern_created) {
1179 struct ltt_kernel_channel *kchan =
1180 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1181 session->kernel_session);
1182 /* Created previously, this should NOT fail. */
1183 assert(kchan);
1184 kernel_destroy_channel(kchan);
1185 }
1186
1187 if (chan_ust_created) {
1188 struct ltt_ust_channel *uchan =
1189 trace_ust_find_channel_by_name(
1190 session->ust_session->domain_global.channels,
1191 DEFAULT_CHANNEL_NAME);
1192 /* Created previously, this should NOT fail. */
1193 assert(uchan);
1194 /* Remove from the channel list of the session. */
1195 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1196 uchan);
1197 trace_ust_destroy_channel(uchan);
1198 }
2f77fc4b
DG
1199 return ret;
1200}
1201
2f77fc4b
DG
1202/*
1203 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1204 */
7972aab2 1205int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
025faf73
DG
1206 char *channel_name, struct lttng_event *event,
1207 struct lttng_filter_bytecode *filter, int wpipe)
2f77fc4b 1208{
e5f5db7f 1209 int ret, channel_created = 0;
2f77fc4b
DG
1210 struct lttng_channel *attr;
1211
1212 assert(session);
1213 assert(event);
1214 assert(channel_name);
1215
2223c96f
DG
1216 rcu_read_lock();
1217
7972aab2 1218 switch (domain->type) {
2f77fc4b
DG
1219 case LTTNG_DOMAIN_KERNEL:
1220 {
1221 struct ltt_kernel_channel *kchan;
1222
85076754
MD
1223 /*
1224 * If a non-default channel has been created in the
1225 * session, explicitely require that -c chan_name needs
1226 * to be provided.
1227 */
1228 if (session->kernel_session->has_non_default_channel
1229 && channel_name[0] == '\0') {
1230 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1231 goto error;
1232 }
1233
2f77fc4b
DG
1234 kchan = trace_kernel_get_channel_by_name(channel_name,
1235 session->kernel_session);
1236 if (kchan == NULL) {
0a9c6494
DG
1237 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1238 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1239 if (attr == NULL) {
f73fabfd 1240 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1241 goto error;
1242 }
1243 strncpy(attr->name, channel_name, sizeof(attr->name));
1244
1245 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1246 if (ret != LTTNG_OK) {
2f77fc4b
DG
1247 free(attr);
1248 goto error;
1249 }
1250 free(attr);
e5f5db7f
DG
1251
1252 channel_created = 1;
2f77fc4b
DG
1253 }
1254
1255 /* Get the newly created kernel channel pointer */
1256 kchan = trace_kernel_get_channel_by_name(channel_name,
1257 session->kernel_session);
1258 if (kchan == NULL) {
1259 /* This sould not happen... */
f73fabfd 1260 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1261 goto error;
1262 }
1263
d3a56674 1264 ret = event_kernel_enable_tracepoint(kchan, event);
f73fabfd 1265 if (ret != LTTNG_OK) {
e5f5db7f
DG
1266 if (channel_created) {
1267 /* Let's not leak a useless channel. */
1268 kernel_destroy_channel(kchan);
1269 }
2f77fc4b
DG
1270 goto error;
1271 }
1272
1273 kernel_wait_quiescent(kernel_tracer_fd);
1274 break;
1275 }
1276 case LTTNG_DOMAIN_UST:
1277 {
1278 struct ltt_ust_channel *uchan;
1279 struct ltt_ust_session *usess = session->ust_session;
1280
1281 assert(usess);
1282
85076754
MD
1283 /*
1284 * If a non-default channel has been created in the
1285 * session, explicitely require that -c chan_name needs
1286 * to be provided.
1287 */
1288 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1289 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1290 goto error;
1291 }
1292
2f77fc4b
DG
1293 /* Get channel from global UST domain */
1294 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1295 channel_name);
1296 if (uchan == NULL) {
1297 /* Create default channel */
0a9c6494
DG
1298 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1299 usess->buffer_type);
2f77fc4b 1300 if (attr == NULL) {
f73fabfd 1301 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1302 goto error;
1303 }
1304 strncpy(attr->name, channel_name, sizeof(attr->name));
1305
1306 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1307 if (ret != LTTNG_OK) {
2f77fc4b
DG
1308 free(attr);
1309 goto error;
1310 }
1311 free(attr);
1312
1313 /* Get the newly created channel reference back */
1314 uchan = trace_ust_find_channel_by_name(
1315 usess->domain_global.channels, channel_name);
1316 assert(uchan);
1317 }
1318
1319 /* At this point, the session and channel exist on the tracer */
7972aab2 1320 ret = event_ust_enable_tracepoint(usess, uchan, event, filter);
f73fabfd 1321 if (ret != LTTNG_OK) {
2f77fc4b
DG
1322 goto error;
1323 }
1324 break;
1325 }
1326#if 0
1327 case LTTNG_DOMAIN_UST_EXEC_NAME:
1328 case LTTNG_DOMAIN_UST_PID:
1329 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1330#endif
1331 default:
f73fabfd 1332 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1333 goto error;
1334 }
1335
f73fabfd 1336 ret = LTTNG_OK;
2f77fc4b
DG
1337
1338error:
2223c96f 1339 rcu_read_unlock();
2f77fc4b
DG
1340 return ret;
1341}
1342
1343/*
1344 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1345 */
7972aab2
DG
1346int cmd_enable_event_all(struct ltt_session *session,
1347 struct lttng_domain *domain, char *channel_name, int event_type,
025faf73 1348 struct lttng_filter_bytecode *filter, int wpipe)
2f77fc4b
DG
1349{
1350 int ret;
1351 struct lttng_channel *attr;
1352
1353 assert(session);
1354 assert(channel_name);
1355
2223c96f
DG
1356 rcu_read_lock();
1357
7972aab2 1358 switch (domain->type) {
2f77fc4b
DG
1359 case LTTNG_DOMAIN_KERNEL:
1360 {
1361 struct ltt_kernel_channel *kchan;
1362
1363 assert(session->kernel_session);
1364
85076754
MD
1365 /*
1366 * If a non-default channel has been created in the
1367 * session, explicitely require that -c chan_name needs
1368 * to be provided.
1369 */
1370 if (session->kernel_session->has_non_default_channel
1371 && channel_name[0] == '\0') {
1372 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1373 goto error;
1374 }
1375
2f77fc4b
DG
1376 kchan = trace_kernel_get_channel_by_name(channel_name,
1377 session->kernel_session);
1378 if (kchan == NULL) {
1379 /* Create default channel */
0a9c6494
DG
1380 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1381 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1382 if (attr == NULL) {
f73fabfd 1383 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1384 goto error;
1385 }
1386 strncpy(attr->name, channel_name, sizeof(attr->name));
1387
1388 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1389 if (ret != LTTNG_OK) {
2f77fc4b
DG
1390 free(attr);
1391 goto error;
1392 }
1393 free(attr);
1394
1395 /* Get the newly created kernel channel pointer */
1396 kchan = trace_kernel_get_channel_by_name(channel_name,
1397 session->kernel_session);
1398 assert(kchan);
1399 }
1400
1401 switch (event_type) {
1402 case LTTNG_EVENT_SYSCALL:
d3a56674 1403 ret = event_kernel_enable_all_syscalls(kchan, kernel_tracer_fd);
2f77fc4b
DG
1404 break;
1405 case LTTNG_EVENT_TRACEPOINT:
1406 /*
1407 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1408 * events already registered to the channel.
1409 */
d3a56674 1410 ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
2f77fc4b
DG
1411 break;
1412 case LTTNG_EVENT_ALL:
1413 /* Enable syscalls and tracepoints */
d3a56674 1414 ret = event_kernel_enable_all(kchan, kernel_tracer_fd);
2f77fc4b
DG
1415 break;
1416 default:
f73fabfd 1417 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2f77fc4b
DG
1418 goto error;
1419 }
1420
1421 /* Manage return value */
f73fabfd 1422 if (ret != LTTNG_OK) {
e5f5db7f
DG
1423 /*
1424 * On error, cmd_enable_channel call will take care of destroying
1425 * the created channel if it was needed.
1426 */
2f77fc4b
DG
1427 goto error;
1428 }
1429
1430 kernel_wait_quiescent(kernel_tracer_fd);
1431 break;
1432 }
1433 case LTTNG_DOMAIN_UST:
1434 {
1435 struct ltt_ust_channel *uchan;
1436 struct ltt_ust_session *usess = session->ust_session;
1437
1438 assert(usess);
1439
85076754
MD
1440 /*
1441 * If a non-default channel has been created in the
1442 * session, explicitely require that -c chan_name needs
1443 * to be provided.
1444 */
1445 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1446 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1447 goto error;
1448 }
1449
2f77fc4b
DG
1450 /* Get channel from global UST domain */
1451 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1452 channel_name);
1453 if (uchan == NULL) {
1454 /* Create default channel */
0a9c6494
DG
1455 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1456 usess->buffer_type);
2f77fc4b 1457 if (attr == NULL) {
f73fabfd 1458 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1459 goto error;
1460 }
1461 strncpy(attr->name, channel_name, sizeof(attr->name));
1462
1463 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1464 if (ret != LTTNG_OK) {
2f77fc4b
DG
1465 free(attr);
1466 goto error;
1467 }
1468 free(attr);
1469
1470 /* Get the newly created channel reference back */
1471 uchan = trace_ust_find_channel_by_name(
1472 usess->domain_global.channels, channel_name);
1473 assert(uchan);
1474 }
1475
1476 /* At this point, the session and channel exist on the tracer */
1477
1478 switch (event_type) {
1479 case LTTNG_EVENT_ALL:
1480 case LTTNG_EVENT_TRACEPOINT:
7972aab2 1481 ret = event_ust_enable_all_tracepoints(usess, uchan, filter);
f73fabfd 1482 if (ret != LTTNG_OK) {
2f77fc4b
DG
1483 goto error;
1484 }
1485 break;
1486 default:
f73fabfd 1487 ret = LTTNG_ERR_UST_ENABLE_FAIL;
2f77fc4b
DG
1488 goto error;
1489 }
1490
1491 /* Manage return value */
f73fabfd 1492 if (ret != LTTNG_OK) {
2f77fc4b
DG
1493 goto error;
1494 }
1495
1496 break;
1497 }
1498#if 0
1499 case LTTNG_DOMAIN_UST_EXEC_NAME:
1500 case LTTNG_DOMAIN_UST_PID:
1501 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1502#endif
1503 default:
f73fabfd 1504 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1505 goto error;
1506 }
1507
f73fabfd 1508 ret = LTTNG_OK;
2f77fc4b
DG
1509
1510error:
2223c96f 1511 rcu_read_unlock();
2f77fc4b
DG
1512 return ret;
1513}
1514
1515
1516/*
1517 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1518 */
1519ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1520{
1521 int ret;
1522 ssize_t nb_events = 0;
1523
1524 switch (domain) {
1525 case LTTNG_DOMAIN_KERNEL:
1526 nb_events = kernel_list_events(kernel_tracer_fd, events);
1527 if (nb_events < 0) {
f73fabfd 1528 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
1529 goto error;
1530 }
1531 break;
1532 case LTTNG_DOMAIN_UST:
1533 nb_events = ust_app_list_events(events);
1534 if (nb_events < 0) {
f73fabfd 1535 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
1536 goto error;
1537 }
1538 break;
1539 default:
f73fabfd 1540 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1541 goto error;
1542 }
1543
1544 return nb_events;
1545
1546error:
1547 /* Return negative value to differentiate return code */
1548 return -ret;
1549}
1550
1551/*
1552 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1553 */
1554ssize_t cmd_list_tracepoint_fields(int domain,
1555 struct lttng_event_field **fields)
1556{
1557 int ret;
1558 ssize_t nb_fields = 0;
1559
1560 switch (domain) {
1561 case LTTNG_DOMAIN_UST:
1562 nb_fields = ust_app_list_event_fields(fields);
1563 if (nb_fields < 0) {
f73fabfd 1564 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
1565 goto error;
1566 }
1567 break;
1568 case LTTNG_DOMAIN_KERNEL:
1569 default: /* fall-through */
f73fabfd 1570 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1571 goto error;
1572 }
1573
1574 return nb_fields;
1575
1576error:
1577 /* Return negative value to differentiate return code */
1578 return -ret;
1579}
1580
1581/*
1582 * Command LTTNG_START_TRACE processed by the client thread.
1583 */
1584int cmd_start_trace(struct ltt_session *session)
1585{
1586 int ret;
1148bd3a 1587 unsigned long nb_chan = 0;
2f77fc4b
DG
1588 struct ltt_kernel_session *ksession;
1589 struct ltt_ust_session *usess;
2f77fc4b
DG
1590
1591 assert(session);
1592
1593 /* Ease our life a bit ;) */
1594 ksession = session->kernel_session;
1595 usess = session->ust_session;
1596
a5a2d0c2
DG
1597 /* Is the session already started? */
1598 if (session->active) {
f73fabfd 1599 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
1600 goto error;
1601 }
1602
1148bd3a
DG
1603 /*
1604 * Starting a session without channel is useless since after that it's not
1605 * possible to enable channel thus inform the client.
1606 */
1607 if (usess && usess->domain_global.channels) {
1608 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
1609 }
1610 if (ksession) {
1611 nb_chan += ksession->channel_count;
1612 }
1613 if (!nb_chan) {
1614 ret = LTTNG_ERR_NO_CHANNEL;
1615 goto error;
1616 }
1617
2f77fc4b
DG
1618 /* Kernel tracing */
1619 if (ksession != NULL) {
9b6c7ec5
DG
1620 ret = start_kernel_session(ksession, kernel_tracer_fd);
1621 if (ret != LTTNG_OK) {
2f77fc4b
DG
1622 goto error;
1623 }
2f77fc4b
DG
1624 }
1625
1626 /* Flag session that trace should start automatically */
1627 if (usess) {
1628 usess->start_trace = 1;
1629
1630 ret = ust_app_start_trace_all(usess);
1631 if (ret < 0) {
f73fabfd 1632 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
1633 goto error;
1634 }
1635 }
1636
a5a2d0c2
DG
1637 /* Flag this after a successful start. */
1638 session->has_been_started = 1;
1639 session->active = 1;
9b6c7ec5 1640
f73fabfd 1641 ret = LTTNG_OK;
2f77fc4b
DG
1642
1643error:
1644 return ret;
1645}
1646
1647/*
1648 * Command LTTNG_STOP_TRACE processed by the client thread.
1649 */
1650int cmd_stop_trace(struct ltt_session *session)
1651{
1652 int ret;
1653 struct ltt_kernel_channel *kchan;
1654 struct ltt_kernel_session *ksession;
1655 struct ltt_ust_session *usess;
1656
1657 assert(session);
1658
1659 /* Short cut */
1660 ksession = session->kernel_session;
1661 usess = session->ust_session;
1662
a5a2d0c2
DG
1663 /* Session is not active. Skip everythong and inform the client. */
1664 if (!session->active) {
f73fabfd 1665 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
1666 goto error;
1667 }
1668
2f77fc4b 1669 /* Kernel tracer */
d39829a1 1670 if (ksession && ksession->started) {
2f77fc4b
DG
1671 DBG("Stop kernel tracing");
1672
1673 /* Flush metadata if exist */
1674 if (ksession->metadata_stream_fd >= 0) {
1675 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1676 if (ret < 0) {
1677 ERR("Kernel metadata flush failed");
1678 }
1679 }
1680
1681 /* Flush all buffers before stopping */
1682 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1683 ret = kernel_flush_buffer(kchan);
1684 if (ret < 0) {
1685 ERR("Kernel flush buffer error");
1686 }
1687 }
1688
1689 ret = kernel_stop_session(ksession);
1690 if (ret < 0) {
f73fabfd 1691 ret = LTTNG_ERR_KERN_STOP_FAIL;
2f77fc4b
DG
1692 goto error;
1693 }
1694
1695 kernel_wait_quiescent(kernel_tracer_fd);
9b6c7ec5
DG
1696
1697 ksession->started = 0;
2f77fc4b
DG
1698 }
1699
d39829a1 1700 if (usess && usess->start_trace) {
2f77fc4b
DG
1701 usess->start_trace = 0;
1702
1703 ret = ust_app_stop_trace_all(usess);
1704 if (ret < 0) {
f73fabfd 1705 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
1706 goto error;
1707 }
1708 }
1709
a5a2d0c2
DG
1710 /* Flag inactive after a successful stop. */
1711 session->active = 0;
f73fabfd 1712 ret = LTTNG_OK;
2f77fc4b
DG
1713
1714error:
1715 return ret;
1716}
1717
1718/*
1719 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1720 */
1721int cmd_set_consumer_uri(int domain, struct ltt_session *session,
1722 size_t nb_uri, struct lttng_uri *uris)
1723{
1724 int ret, i;
1725 struct ltt_kernel_session *ksess = session->kernel_session;
1726 struct ltt_ust_session *usess = session->ust_session;
1727 struct consumer_output *consumer = NULL;
1728
1729 assert(session);
1730 assert(uris);
1731 assert(nb_uri > 0);
1732
a5a2d0c2
DG
1733 /* Can't set consumer URI if the session is active. */
1734 if (session->active) {
f73fabfd 1735 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
1736 goto error;
1737 }
1738
2f77fc4b
DG
1739 /*
1740 * This case switch makes sure the domain session has a temporary consumer
1741 * so the URL can be set.
1742 */
1743 switch (domain) {
1744 case 0:
1745 /* Code flow error. A session MUST always have a consumer object */
1746 assert(session->consumer);
1747 /*
1748 * The URL will be added to the tracing session consumer instead of a
1749 * specific domain consumer.
1750 */
1751 consumer = session->consumer;
1752 break;
1753 case LTTNG_DOMAIN_KERNEL:
1754 /* Code flow error if we don't have a kernel session here. */
1755 assert(ksess);
785d2d0d
DG
1756 assert(ksess->consumer);
1757 consumer = ksess->consumer;
2f77fc4b
DG
1758 break;
1759 case LTTNG_DOMAIN_UST:
1760 /* Code flow error if we don't have a kernel session here. */
1761 assert(usess);
785d2d0d
DG
1762 assert(usess->consumer);
1763 consumer = usess->consumer;
2f77fc4b
DG
1764 break;
1765 }
1766
1767 for (i = 0; i < nb_uri; i++) {
2f77fc4b 1768 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
a74934ba 1769 if (ret != LTTNG_OK) {
2f77fc4b
DG
1770 goto error;
1771 }
2f77fc4b
DG
1772 }
1773
b747c300
DG
1774 /*
1775 * Make sure to set the session in output mode after we set URI since a
1776 * session can be created without URL (thus flagged in no output mode).
1777 */
1778 session->output_traces = 1;
1779 if (ksess) {
1780 ksess->output_traces = 1;
1781 } else if (usess) {
1782 usess->output_traces = 1;
1783 }
1784
2f77fc4b 1785 /* All good! */
f73fabfd 1786 ret = LTTNG_OK;
2f77fc4b
DG
1787
1788error:
1789 return ret;
1790}
1791
1792/*
1793 * Command LTTNG_CREATE_SESSION processed by the client thread.
1794 */
1795int cmd_create_session_uri(char *name, struct lttng_uri *uris,
1796 size_t nb_uri, lttng_sock_cred *creds)
1797{
1798 int ret;
2f77fc4b
DG
1799 struct ltt_session *session;
1800
1801 assert(name);
2bba9e53 1802 assert(creds);
785d2d0d 1803
2f77fc4b
DG
1804 /*
1805 * Verify if the session already exist
1806 *
1807 * XXX: There is no need for the session lock list here since the caller
1808 * (process_client_msg) is holding it. We might want to change that so a
1809 * single command does not lock the entire session list.
1810 */
1811 session = session_find_by_name(name);
1812 if (session != NULL) {
f73fabfd 1813 ret = LTTNG_ERR_EXIST_SESS;
2f77fc4b
DG
1814 goto find_error;
1815 }
1816
1817 /* Create tracing session in the registry */
dec56f6c 1818 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2f77fc4b 1819 LTTNG_SOCK_GET_GID_CRED(creds));
f73fabfd 1820 if (ret != LTTNG_OK) {
2f77fc4b
DG
1821 goto session_error;
1822 }
1823
1824 /*
1825 * Get the newly created session pointer back
1826 *
1827 * XXX: There is no need for the session lock list here since the caller
1828 * (process_client_msg) is holding it. We might want to change that so a
1829 * single command does not lock the entire session list.
1830 */
1831 session = session_find_by_name(name);
1832 assert(session);
1833
1834 /* Create default consumer output for the session not yet created. */
1835 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1836 if (session->consumer == NULL) {
f73fabfd 1837 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1838 goto consumer_error;
1839 }
1840
2bba9e53
DG
1841 if (uris) {
1842 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
1843 if (ret != LTTNG_OK) {
1844 goto consumer_error;
1845 }
1846 session->output_traces = 1;
1847 } else {
1848 session->output_traces = 0;
1849 DBG2("Session %s created with no output", session->name);
2f77fc4b
DG
1850 }
1851
1852 session->consumer->enabled = 1;
1853
f73fabfd 1854 return LTTNG_OK;
2f77fc4b
DG
1855
1856consumer_error:
1857 session_destroy(session);
1858session_error:
1859find_error:
1860 return ret;
1861}
1862
27babd3a
DG
1863/*
1864 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
1865 */
1866int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
1867 size_t nb_uri, lttng_sock_cred *creds)
1868{
1869 int ret;
1870 struct ltt_session *session;
1871 struct snapshot_output *new_output = NULL;
1872
1873 assert(name);
1874 assert(creds);
1875
1876 /*
1877 * Create session in no output mode with URIs set to NULL. The uris we've
1878 * received are for a default snapshot output if one.
1879 */
1880 ret = cmd_create_session_uri(name, NULL, 0, creds);
1881 if (ret != LTTNG_OK) {
1882 goto error;
1883 }
1884
1885 /* Get the newly created session pointer back. This should NEVER fail. */
1886 session = session_find_by_name(name);
1887 assert(session);
1888
1889 /* Flag session for snapshot mode. */
1890 session->snapshot_mode = 1;
1891
1892 /* Skip snapshot output creation if no URI is given. */
1893 if (nb_uri == 0) {
1894 goto end;
1895 }
1896
1897 new_output = snapshot_output_alloc();
1898 if (!new_output) {
1899 ret = LTTNG_ERR_NOMEM;
1900 goto error_snapshot_alloc;
1901 }
1902
1903 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
1904 uris, nb_uri, session->consumer, new_output, &session->snapshot);
1905 if (ret < 0) {
1906 if (ret == -ENOMEM) {
1907 ret = LTTNG_ERR_NOMEM;
1908 } else {
1909 ret = LTTNG_ERR_INVALID;
1910 }
1911 goto error_snapshot;
1912 }
1913
1914 rcu_read_lock();
1915 snapshot_add_output(&session->snapshot, new_output);
1916 rcu_read_unlock();
1917
1918end:
1919 return LTTNG_OK;
1920
1921error_snapshot:
1922 snapshot_output_destroy(new_output);
1923error_snapshot_alloc:
1924 session_destroy(session);
1925error:
1926 return ret;
1927}
1928
2f77fc4b
DG
1929/*
1930 * Command LTTNG_DESTROY_SESSION processed by the client thread.
1931 */
1932int cmd_destroy_session(struct ltt_session *session, int wpipe)
1933{
1934 int ret;
1935 struct ltt_ust_session *usess;
1936 struct ltt_kernel_session *ksess;
1937
1938 /* Safety net */
1939 assert(session);
1940
1941 usess = session->ust_session;
1942 ksess = session->kernel_session;
1943
1944 /* Clean kernel session teardown */
1945 kernel_destroy_session(ksess);
1946
1947 /* UST session teardown */
1948 if (usess) {
1949 /* Close any relayd session */
1950 consumer_output_send_destroy_relayd(usess->consumer);
1951
1952 /* Destroy every UST application related to this session. */
1953 ret = ust_app_destroy_trace_all(usess);
1954 if (ret) {
1955 ERR("Error in ust_app_destroy_trace_all");
1956 }
1957
1958 /* Clean up the rest. */
1959 trace_ust_destroy_session(usess);
1960 }
1961
1962 /*
1963 * Must notify the kernel thread here to update it's poll set in order to
1964 * remove the channel(s)' fd just destroyed.
1965 */
1966 ret = notify_thread_pipe(wpipe);
1967 if (ret < 0) {
1968 PERROR("write kernel poll pipe");
1969 }
1970
1971 ret = session_destroy(session);
1972
1973 return ret;
1974}
1975
1976/*
1977 * Command LTTNG_CALIBRATE processed by the client thread.
1978 */
1979int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
1980{
1981 int ret;
1982
1983 switch (domain) {
1984 case LTTNG_DOMAIN_KERNEL:
1985 {
1986 struct lttng_kernel_calibrate kcalibrate;
1987
72b6f5b6
DG
1988 switch (calibrate->type) {
1989 case LTTNG_CALIBRATE_FUNCTION:
1990 default:
1991 /* Default and only possible calibrate option. */
1992 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
1993 break;
1994 }
1995
2f77fc4b
DG
1996 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
1997 if (ret < 0) {
f73fabfd 1998 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2f77fc4b
DG
1999 goto error;
2000 }
2001 break;
2002 }
2003 case LTTNG_DOMAIN_UST:
2004 {
2005 struct lttng_ust_calibrate ucalibrate;
2006
72b6f5b6
DG
2007 switch (calibrate->type) {
2008 case LTTNG_CALIBRATE_FUNCTION:
2009 default:
2010 /* Default and only possible calibrate option. */
2011 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2012 break;
2013 }
2014
2f77fc4b
DG
2015 ret = ust_app_calibrate_glb(&ucalibrate);
2016 if (ret < 0) {
f73fabfd 2017 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2f77fc4b
DG
2018 goto error;
2019 }
2020 break;
2021 }
2022 default:
f73fabfd 2023 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2024 goto error;
2025 }
2026
f73fabfd 2027 ret = LTTNG_OK;
2f77fc4b
DG
2028
2029error:
2030 return ret;
2031}
2032
2033/*
2034 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2035 */
2036int cmd_register_consumer(struct ltt_session *session, int domain,
2037 const char *sock_path, struct consumer_data *cdata)
2038{
2039 int ret, sock;
dd81b457 2040 struct consumer_socket *socket = NULL;
2f77fc4b
DG
2041
2042 assert(session);
2043 assert(cdata);
2044 assert(sock_path);
2045
2046 switch (domain) {
2047 case LTTNG_DOMAIN_KERNEL:
2048 {
2049 struct ltt_kernel_session *ksess = session->kernel_session;
2050
2051 assert(ksess);
2052
2053 /* Can't register a consumer if there is already one */
2054 if (ksess->consumer_fds_sent != 0) {
f73fabfd 2055 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
2056 goto error;
2057 }
2058
2059 sock = lttcomm_connect_unix_sock(sock_path);
2060 if (sock < 0) {
f73fabfd 2061 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
2062 goto error;
2063 }
4ce514c4 2064 cdata->cmd_sock = sock;
2f77fc4b 2065
4ce514c4 2066 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 2067 if (socket == NULL) {
f66c074c
DG
2068 ret = close(sock);
2069 if (ret < 0) {
2070 PERROR("close register consumer");
2071 }
4ce514c4 2072 cdata->cmd_sock = -1;
f73fabfd 2073 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2074 goto error;
2075 }
2076
2077 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2078 if (socket->lock == NULL) {
2079 PERROR("zmalloc pthread mutex");
f73fabfd 2080 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2081 goto error;
2082 }
2083 pthread_mutex_init(socket->lock, NULL);
2084 socket->registered = 1;
2085
2086 rcu_read_lock();
2087 consumer_add_socket(socket, ksess->consumer);
2088 rcu_read_unlock();
2089
2090 pthread_mutex_lock(&cdata->pid_mutex);
2091 cdata->pid = -1;
2092 pthread_mutex_unlock(&cdata->pid_mutex);
2093
2094 break;
2095 }
2096 default:
2097 /* TODO: Userspace tracing */
f73fabfd 2098 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2099 goto error;
2100 }
2101
dd81b457 2102 return LTTNG_OK;
2f77fc4b
DG
2103
2104error:
dd81b457
DG
2105 if (socket) {
2106 consumer_destroy_socket(socket);
2107 }
2f77fc4b
DG
2108 return ret;
2109}
2110
2111/*
2112 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2113 */
2114ssize_t cmd_list_domains(struct ltt_session *session,
2115 struct lttng_domain **domains)
2116{
2117 int ret, index = 0;
2118 ssize_t nb_dom = 0;
2119
2120 if (session->kernel_session != NULL) {
2121 DBG3("Listing domains found kernel domain");
2122 nb_dom++;
2123 }
2124
2125 if (session->ust_session != NULL) {
2126 DBG3("Listing domains found UST global domain");
2127 nb_dom++;
2128 }
2129
2130 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2131 if (*domains == NULL) {
f73fabfd 2132 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2133 goto error;
2134 }
2135
2136 if (session->kernel_session != NULL) {
2137 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2138 index++;
2139 }
2140
2141 if (session->ust_session != NULL) {
2142 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 2143 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b
DG
2144 index++;
2145 }
2146
2147 return nb_dom;
2148
2149error:
f73fabfd
DG
2150 /* Return negative value to differentiate return code */
2151 return -ret;
2f77fc4b
DG
2152}
2153
2154
2155/*
2156 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2157 */
2158ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2159 struct lttng_channel **channels)
2160{
2161 int ret;
2162 ssize_t nb_chan = 0;
2163
2164 switch (domain) {
2165 case LTTNG_DOMAIN_KERNEL:
2166 if (session->kernel_session != NULL) {
2167 nb_chan = session->kernel_session->channel_count;
2168 }
2169 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2
DG
2170 if (nb_chan <= 0) {
2171 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2172 }
2f77fc4b
DG
2173 break;
2174 case LTTNG_DOMAIN_UST:
2175 if (session->ust_session != NULL) {
2176 nb_chan = lttng_ht_get_count(
2177 session->ust_session->domain_global.channels);
2178 }
2179 DBG3("Number of UST global channels %zd", nb_chan);
c7d620a2
DG
2180 if (nb_chan <= 0) {
2181 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2182 }
2f77fc4b
DG
2183 break;
2184 default:
2185 *channels = NULL;
f73fabfd 2186 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2187 goto error;
2188 }
2189
2190 if (nb_chan > 0) {
2191 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2192 if (*channels == NULL) {
f73fabfd 2193 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2194 goto error;
2195 }
2196
2197 list_lttng_channels(domain, session, *channels);
2198 } else {
2199 *channels = NULL;
c7d620a2
DG
2200 /* Ret value was set in the domain switch case */
2201 goto error;
2f77fc4b
DG
2202 }
2203
2204 return nb_chan;
2205
2206error:
f73fabfd
DG
2207 /* Return negative value to differentiate return code */
2208 return -ret;
2f77fc4b
DG
2209}
2210
2211/*
2212 * Command LTTNG_LIST_EVENTS processed by the client thread.
2213 */
2214ssize_t cmd_list_events(int domain, struct ltt_session *session,
2215 char *channel_name, struct lttng_event **events)
2216{
2217 int ret = 0;
2218 ssize_t nb_event = 0;
2219
2220 switch (domain) {
2221 case LTTNG_DOMAIN_KERNEL:
2222 if (session->kernel_session != NULL) {
2223 nb_event = list_lttng_kernel_events(channel_name,
2224 session->kernel_session, events);
2225 }
2226 break;
2227 case LTTNG_DOMAIN_UST:
2228 {
2229 if (session->ust_session != NULL) {
2230 nb_event = list_lttng_ust_global_events(channel_name,
2231 &session->ust_session->domain_global, events);
2232 }
2233 break;
2234 }
2235 default:
f73fabfd 2236 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2237 goto error;
2238 }
2239
f73fabfd 2240 return nb_event;
2f77fc4b
DG
2241
2242error:
f73fabfd
DG
2243 /* Return negative value to differentiate return code */
2244 return -ret;
2f77fc4b
DG
2245}
2246
2247/*
2248 * Using the session list, filled a lttng_session array to send back to the
2249 * client for session listing.
2250 *
2251 * The session list lock MUST be acquired before calling this function. Use
2252 * session_lock_list() and session_unlock_list().
2253 */
2254void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2255 gid_t gid)
2256{
2257 int ret;
2258 unsigned int i = 0;
2259 struct ltt_session *session;
2260 struct ltt_session_list *list = session_get_list();
2261
2262 DBG("Getting all available session for UID %d GID %d",
2263 uid, gid);
2264 /*
2265 * Iterate over session list and append data after the control struct in
2266 * the buffer.
2267 */
2268 cds_list_for_each_entry(session, &list->head, list) {
2269 /*
2270 * Only list the sessions the user can control.
2271 */
2272 if (!session_access_ok(session, uid, gid)) {
2273 continue;
2274 }
2275
2276 struct ltt_kernel_session *ksess = session->kernel_session;
2277 struct ltt_ust_session *usess = session->ust_session;
2278
2279 if (session->consumer->type == CONSUMER_DST_NET ||
2280 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2281 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2282 ret = build_network_session_path(sessions[i].path,
dec56f6c 2283 sizeof(sessions[i].path), session);
2f77fc4b 2284 } else {
dec56f6c 2285 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2f77fc4b
DG
2286 session->consumer->dst.trace_path);
2287 }
2288 if (ret < 0) {
2289 PERROR("snprintf session path");
2290 continue;
2291 }
2292
2293 strncpy(sessions[i].name, session->name, NAME_MAX);
2294 sessions[i].name[NAME_MAX - 1] = '\0';
a5a2d0c2 2295 sessions[i].enabled = session->active;
2cbf8fed 2296 sessions[i].snapshot_mode = session->snapshot_mode;
2f77fc4b
DG
2297 i++;
2298 }
2299}
2300
806e2684 2301/*
6d805429 2302 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 2303 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 2304 */
6d805429 2305int cmd_data_pending(struct ltt_session *session)
806e2684
DG
2306{
2307 int ret;
2308 struct ltt_kernel_session *ksess = session->kernel_session;
2309 struct ltt_ust_session *usess = session->ust_session;
2310
2311 assert(session);
2312
2313 /* Session MUST be stopped to ask for data availability. */
a5a2d0c2 2314 if (session->active) {
806e2684
DG
2315 ret = LTTNG_ERR_SESSION_STARTED;
2316 goto error;
2e4e8c2d
DG
2317 } else {
2318 /*
2319 * If stopped, just make sure we've started before else the above call
2320 * will always send that there is data pending.
2321 *
2322 * The consumer assumes that when the data pending command is received,
2323 * the trace has been started before or else no output data is written
2324 * by the streams which is a condition for data pending. So, this is
2325 * *VERY* important that we don't ask the consumer before a start
2326 * trace.
2327 */
a5a2d0c2 2328 if (!session->has_been_started) {
2e4e8c2d
DG
2329 ret = 0;
2330 goto error;
2331 }
806e2684
DG
2332 }
2333
2334 if (ksess && ksess->consumer) {
6d805429
DG
2335 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2336 if (ret == 1) {
806e2684
DG
2337 /* Data is still being extracted for the kernel. */
2338 goto error;
2339 }
2340 }
2341
2342 if (usess && usess->consumer) {
6d805429
DG
2343 ret = consumer_is_data_pending(usess->id, usess->consumer);
2344 if (ret == 1) {
806e2684
DG
2345 /* Data is still being extracted for the kernel. */
2346 goto error;
2347 }
2348 }
2349
2350 /* Data is ready to be read by a viewer */
6d805429 2351 ret = 0;
806e2684
DG
2352
2353error:
2354 return ret;
2355}
2356
6dc3064a
DG
2357/*
2358 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2359 *
2360 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2361 */
2362int cmd_snapshot_add_output(struct ltt_session *session,
2363 struct lttng_snapshot_output *output, uint32_t *id)
2364{
2365 int ret;
2366 struct snapshot_output *new_output;
2367
2368 assert(session);
2369 assert(output);
2370
2371 DBG("Cmd snapshot add output for session %s", session->name);
2372
2373 /*
d3f14b8a
MD
2374 * Permission denied to create an output if the session is not
2375 * set in no output mode.
6dc3064a
DG
2376 */
2377 if (session->output_traces) {
2378 ret = LTTNG_ERR_EPERM;
2379 goto error;
2380 }
2381
2382 /* Only one output is allowed until we have the "tee" feature. */
2383 if (session->snapshot.nb_output == 1) {
2384 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
2385 goto error;
2386 }
2387
2388 new_output = snapshot_output_alloc();
2389 if (!new_output) {
2390 ret = LTTNG_ERR_NOMEM;
2391 goto error;
2392 }
2393
2394 ret = snapshot_output_init(output->max_size, output->name,
2395 output->ctrl_url, output->data_url, session->consumer, new_output,
2396 &session->snapshot);
2397 if (ret < 0) {
2398 if (ret == -ENOMEM) {
2399 ret = LTTNG_ERR_NOMEM;
2400 } else {
2401 ret = LTTNG_ERR_INVALID;
2402 }
2403 goto free_error;
2404 }
2405
6dc3064a
DG
2406 rcu_read_lock();
2407 snapshot_add_output(&session->snapshot, new_output);
2408 if (id) {
2409 *id = new_output->id;
2410 }
2411 rcu_read_unlock();
2412
2413 return LTTNG_OK;
2414
2415free_error:
2416 snapshot_output_destroy(new_output);
2417error:
2418 return ret;
2419}
2420
2421/*
2422 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2423 *
2424 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2425 */
2426int cmd_snapshot_del_output(struct ltt_session *session,
2427 struct lttng_snapshot_output *output)
2428{
2429 int ret;
eb240553 2430 struct snapshot_output *sout = NULL;
6dc3064a
DG
2431
2432 assert(session);
2433 assert(output);
2434
6dc3064a
DG
2435 rcu_read_lock();
2436
2437 /*
d3f14b8a
MD
2438 * Permission denied to create an output if the session is not
2439 * set in no output mode.
6dc3064a
DG
2440 */
2441 if (session->output_traces) {
2442 ret = LTTNG_ERR_EPERM;
2443 goto error;
2444 }
2445
eb240553
DG
2446 if (output->id) {
2447 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
2448 session->name);
2449 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
2450 } else if (*output->name != '\0') {
2451 DBG("Cmd snapshot del output name %s for session %s", output->name,
2452 session->name);
2453 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
2454 }
6dc3064a
DG
2455 if (!sout) {
2456 ret = LTTNG_ERR_INVALID;
2457 goto error;
2458 }
2459
2460 snapshot_delete_output(&session->snapshot, sout);
2461 snapshot_output_destroy(sout);
2462 ret = LTTNG_OK;
2463
2464error:
2465 rcu_read_unlock();
2466 return ret;
2467}
2468
2469/*
2470 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2471 *
2472 * If no output is available, outputs is untouched and 0 is returned.
2473 *
2474 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2475 */
2476ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
2477 struct lttng_snapshot_output **outputs)
2478{
2479 int ret, idx = 0;
2480 struct lttng_snapshot_output *list;
2481 struct lttng_ht_iter iter;
2482 struct snapshot_output *output;
2483
2484 assert(session);
2485 assert(outputs);
2486
2487 DBG("Cmd snapshot list outputs for session %s", session->name);
2488
2489 /*
d3f14b8a
MD
2490 * Permission denied to create an output if the session is not
2491 * set in no output mode.
6dc3064a
DG
2492 */
2493 if (session->output_traces) {
2494 ret = LTTNG_ERR_EPERM;
2495 goto error;
2496 }
2497
2498 if (session->snapshot.nb_output == 0) {
2499 ret = 0;
2500 goto error;
2501 }
2502
2503 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
2504 if (!list) {
2505 ret = LTTNG_ERR_NOMEM;
2506 goto error;
2507 }
2508
2509 /* Copy list from session to the new list object. */
2510 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
2511 output, node.node) {
2512 assert(output->consumer);
2513 list[idx].id = output->id;
2514 list[idx].max_size = output->max_size;
2515 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
2516 if (output->consumer->type == CONSUMER_DST_LOCAL) {
2517 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
2518 sizeof(list[idx].ctrl_url));
2519 } else {
2520 /* Control URI. */
2521 ret = uri_to_str_url(&output->consumer->dst.net.control,
2522 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
2523 if (ret < 0) {
2524 ret = LTTNG_ERR_NOMEM;
2525 goto free_error;
2526 }
2527
2528 /* Data URI. */
2529 ret = uri_to_str_url(&output->consumer->dst.net.data,
2530 list[idx].data_url, sizeof(list[idx].data_url));
2531 if (ret < 0) {
2532 ret = LTTNG_ERR_NOMEM;
2533 goto free_error;
2534 }
2535 }
2536 idx++;
2537 }
2538
2539 *outputs = list;
2540 return session->snapshot.nb_output;
2541
2542free_error:
2543 free(list);
2544error:
2545 return -ret;
2546}
2547
2548/*
2549 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2550 * snapshot output is *not* set with a remote destination.
2551 *
a934f4af 2552 * Return 0 on success or a LTTNG_ERR code.
6dc3064a
DG
2553 */
2554static int set_relayd_for_snapshot(struct consumer_output *consumer,
2555 struct snapshot_output *snap_output, struct ltt_session *session)
2556{
a934f4af 2557 int ret = LTTNG_OK;
6dc3064a
DG
2558 struct lttng_ht_iter iter;
2559 struct consumer_socket *socket;
2560
2561 assert(consumer);
2562 assert(snap_output);
2563 assert(session);
2564
2565 DBG2("Set relayd object from snapshot output");
2566
2567 /* Ignore if snapshot consumer output is not network. */
2568 if (snap_output->consumer->type != CONSUMER_DST_NET) {
2569 goto error;
2570 }
2571
2572 /*
2573 * For each consumer socket, create and send the relayd object of the
2574 * snapshot output.
2575 */
2576 rcu_read_lock();
5eecee74
DG
2577 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
2578 socket, node.node) {
6dc3064a
DG
2579 ret = send_consumer_relayd_sockets(0, session->id,
2580 snap_output->consumer, socket);
a934f4af 2581 if (ret != LTTNG_OK) {
6dc3064a
DG
2582 rcu_read_unlock();
2583 goto error;
2584 }
2585 }
2586 rcu_read_unlock();
2587
2588error:
2589 return ret;
2590}
2591
2592/*
2593 * Record a kernel snapshot.
2594 *
a934f4af 2595 * Return 0 on success or a LTTNG_ERR code.
6dc3064a
DG
2596 */
2597static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
5c786ded
JD
2598 struct snapshot_output *output, struct ltt_session *session,
2599 int wait, int nb_streams)
6dc3064a
DG
2600{
2601 int ret;
2602
2603 assert(ksess);
2604 assert(output);
2605 assert(session);
2606
10a50311
JD
2607 /* Get the datetime for the snapshot output directory. */
2608 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2609 sizeof(output->datetime));
2610 if (!ret) {
a934f4af 2611 ret = LTTNG_ERR_INVALID;
10a50311
JD
2612 goto error;
2613 }
2614
af706bb7
DG
2615 /*
2616 * Copy kernel session sockets so we can communicate with the right
2617 * consumer for the snapshot record command.
2618 */
2619 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
2620 if (ret < 0) {
a934f4af 2621 ret = LTTNG_ERR_NOMEM;
af706bb7 2622 goto error;
6dc3064a
DG
2623 }
2624
2625 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
a934f4af 2626 if (ret != LTTNG_OK) {
af706bb7 2627 goto error_snapshot;
6dc3064a
DG
2628 }
2629
5c786ded 2630 ret = kernel_snapshot_record(ksess, output, wait, nb_streams);
2a06df8d 2631 if (ret != LTTNG_OK) {
af706bb7 2632 goto error_snapshot;
6dc3064a
DG
2633 }
2634
a934f4af
DG
2635 ret = LTTNG_OK;
2636
af706bb7
DG
2637error_snapshot:
2638 /* Clean up copied sockets so this output can use some other later on. */
2639 consumer_destroy_output_sockets(output->consumer);
6dc3064a
DG
2640error:
2641 return ret;
2642}
2643
2644/*
2645 * Record a UST snapshot.
2646 *
a934f4af 2647 * Return 0 on success or a LTTNG_ERR error code.
6dc3064a
DG
2648 */
2649static int record_ust_snapshot(struct ltt_ust_session *usess,
5c786ded
JD
2650 struct snapshot_output *output, struct ltt_session *session,
2651 int wait, int nb_streams)
6dc3064a
DG
2652{
2653 int ret;
2654
2655 assert(usess);
2656 assert(output);
2657 assert(session);
2658
10a50311
JD
2659 /* Get the datetime for the snapshot output directory. */
2660 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2661 sizeof(output->datetime));
2662 if (!ret) {
a934f4af 2663 ret = LTTNG_ERR_INVALID;
10a50311
JD
2664 goto error;
2665 }
2666
af706bb7 2667 /*
d3f14b8a 2668 * Copy UST session sockets so we can communicate with the right
af706bb7
DG
2669 * consumer for the snapshot record command.
2670 */
2671 ret = consumer_copy_sockets(output->consumer, usess->consumer);
2672 if (ret < 0) {
a934f4af 2673 ret = LTTNG_ERR_NOMEM;
af706bb7 2674 goto error;
6dc3064a
DG
2675 }
2676
2677 ret = set_relayd_for_snapshot(usess->consumer, output, session);
a934f4af 2678 if (ret != LTTNG_OK) {
af706bb7 2679 goto error_snapshot;
6dc3064a
DG
2680 }
2681
5c786ded 2682 ret = ust_app_snapshot_record(usess, output, wait, nb_streams);
6dc3064a 2683 if (ret < 0) {
e9accd4f
DG
2684 switch (-ret) {
2685 case EINVAL:
a934f4af 2686 ret = LTTNG_ERR_INVALID;
e9accd4f
DG
2687 break;
2688 case ENODATA:
2689 ret = LTTNG_ERR_SNAPSHOT_NODATA;
2690 break;
2691 default:
2692 ret = LTTNG_ERR_SNAPSHOT_FAIL;
2693 break;
5c786ded 2694 }
af706bb7 2695 goto error_snapshot;
6dc3064a
DG
2696 }
2697
a934f4af
DG
2698 ret = LTTNG_OK;
2699
af706bb7
DG
2700error_snapshot:
2701 /* Clean up copied sockets so this output can use some other later on. */
2702 consumer_destroy_output_sockets(output->consumer);
6dc3064a
DG
2703error:
2704 return ret;
2705}
2706
5c786ded
JD
2707/*
2708 * Returns the total number of streams for a session or a negative value
2709 * on error.
2710 */
2711static unsigned int get_total_nb_stream(struct ltt_session *session)
2712{
2713 unsigned int total_streams = 0;
2714
2715 if (session->kernel_session) {
2716 struct ltt_kernel_session *ksess = session->kernel_session;
2717
2718 total_streams += ksess->stream_count_global;
2719 }
2720
2721 if (session->ust_session) {
2722 struct ltt_ust_session *usess = session->ust_session;
2723
2724 total_streams += ust_app_get_nb_stream(usess);
2725 }
2726
2727 return total_streams;
2728}
2729
6dc3064a
DG
2730/*
2731 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2732 *
2733 * The wait parameter is ignored so this call always wait for the snapshot to
2734 * complete before returning.
2735 *
2736 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2737 */
2738int cmd_snapshot_record(struct ltt_session *session,
2739 struct lttng_snapshot_output *output, int wait)
2740{
2741 int ret = LTTNG_OK;
e1986656
DG
2742 unsigned int use_tmp_output = 0;
2743 struct snapshot_output tmp_output;
1bfe7328 2744 unsigned int nb_streams, snapshot_success = 0;
6dc3064a
DG
2745
2746 assert(session);
2747
2748 DBG("Cmd snapshot record for session %s", session->name);
2749
2750 /*
d3f14b8a
MD
2751 * Permission denied to create an output if the session is not
2752 * set in no output mode.
6dc3064a
DG
2753 */
2754 if (session->output_traces) {
2755 ret = LTTNG_ERR_EPERM;
2756 goto error;
2757 }
2758
2759 /* The session needs to be started at least once. */
a5a2d0c2 2760 if (!session->has_been_started) {
6dc3064a
DG
2761 ret = LTTNG_ERR_START_SESSION_ONCE;
2762 goto error;
2763 }
2764
2765 /* Use temporary output for the session. */
2766 if (output && *output->ctrl_url != '\0') {
6dc3064a
DG
2767 ret = snapshot_output_init(output->max_size, output->name,
2768 output->ctrl_url, output->data_url, session->consumer,
e1986656 2769 &tmp_output, NULL);
6dc3064a
DG
2770 if (ret < 0) {
2771 if (ret == -ENOMEM) {
2772 ret = LTTNG_ERR_NOMEM;
2773 } else {
2774 ret = LTTNG_ERR_INVALID;
2775 }
2776 goto error;
2777 }
1bfe7328
DG
2778 /* Use the global session count for the temporary snapshot. */
2779 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
e1986656 2780 use_tmp_output = 1;
6dc3064a
DG
2781 }
2782
5c786ded
JD
2783 /*
2784 * Get the total number of stream of that session which is used by the
2785 * maximum size of the snapshot feature.
2786 */
2787 nb_streams = get_total_nb_stream(session);
2788
6dc3064a
DG
2789 if (session->kernel_session) {
2790 struct ltt_kernel_session *ksess = session->kernel_session;
2791
e1986656
DG
2792 if (use_tmp_output) {
2793 ret = record_kernel_snapshot(ksess, &tmp_output, session,
5c786ded 2794 wait, nb_streams);
a934f4af 2795 if (ret != LTTNG_OK) {
6dc3064a
DG
2796 goto error;
2797 }
1bfe7328 2798 snapshot_success = 1;
6dc3064a
DG
2799 } else {
2800 struct snapshot_output *sout;
2801 struct lttng_ht_iter iter;
2802
2803 rcu_read_lock();
2804 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
2805 &iter.iter, sout, node.node) {
e1986656
DG
2806 /*
2807 * Make a local copy of the output and assign the possible
2808 * temporary value given by the caller.
2809 */
2810 memset(&tmp_output, 0, sizeof(tmp_output));
2811 memcpy(&tmp_output, sout, sizeof(tmp_output));
2812
2813 /* Use temporary max size. */
2814 if (output->max_size != (uint64_t) -1ULL) {
2815 tmp_output.max_size = output->max_size;
2816 }
2817
2818 /* Use temporary name. */
2819 if (*output->name != '\0') {
2820 strncpy(tmp_output.name, output->name,
2821 sizeof(tmp_output.name));
2822 }
2823
1bfe7328
DG
2824 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
2825
e1986656 2826 ret = record_kernel_snapshot(ksess, &tmp_output,
5c786ded 2827 session, wait, nb_streams);
a934f4af 2828 if (ret != LTTNG_OK) {
6dc3064a
DG
2829 rcu_read_unlock();
2830 goto error;
2831 }
1bfe7328 2832 snapshot_success = 1;
6dc3064a
DG
2833 }
2834 rcu_read_unlock();
2835 }
2836 }
2837
2838 if (session->ust_session) {
2839 struct ltt_ust_session *usess = session->ust_session;
2840
e1986656
DG
2841 if (use_tmp_output) {
2842 ret = record_ust_snapshot(usess, &tmp_output, session,
5c786ded 2843 wait, nb_streams);
a934f4af 2844 if (ret != LTTNG_OK) {
6dc3064a
DG
2845 goto error;
2846 }
1bfe7328 2847 snapshot_success = 1;
6dc3064a
DG
2848 } else {
2849 struct snapshot_output *sout;
2850 struct lttng_ht_iter iter;
2851
2852 rcu_read_lock();
2853 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
2854 &iter.iter, sout, node.node) {
e1986656
DG
2855 /*
2856 * Make a local copy of the output and assign the possible
2857 * temporary value given by the caller.
2858 */
2859 memset(&tmp_output, 0, sizeof(tmp_output));
2860 memcpy(&tmp_output, sout, sizeof(tmp_output));
2861
e1986656
DG
2862 /* Use temporary max size. */
2863 if (output->max_size != (uint64_t) -1ULL) {
2864 tmp_output.max_size = output->max_size;
2865 }
2866
2867 /* Use temporary name. */
2868 if (*output->name != '\0') {
2869 strncpy(tmp_output.name, output->name,
2870 sizeof(tmp_output.name));
2871 }
2872
1bfe7328
DG
2873 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
2874
e1986656 2875 ret = record_ust_snapshot(usess, &tmp_output, session,
5c786ded 2876 wait, nb_streams);
a934f4af 2877 if (ret != LTTNG_OK) {
6dc3064a
DG
2878 rcu_read_unlock();
2879 goto error;
2880 }
1bfe7328 2881 snapshot_success = 1;
6dc3064a
DG
2882 }
2883 rcu_read_unlock();
2884 }
2885 }
2886
1bfe7328
DG
2887 if (snapshot_success) {
2888 session->snapshot.nb_snapshot++;
b67578cb
MD
2889 } else {
2890 ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
2891 }
2892
6dc3064a 2893error:
6dc3064a
DG
2894 return ret;
2895}
2896
2f77fc4b
DG
2897/*
2898 * Init command subsystem.
2899 */
2900void cmd_init(void)
2901{
2902 /*
d88aee68
DG
2903 * Set network sequence index to 1 for streams to match a relayd
2904 * socket on the consumer side.
2f77fc4b 2905 */
d88aee68
DG
2906 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2907 relayd_net_seq_idx = 1;
2908 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
2909
2910 DBG("Command subsystem initialized");
2911}
This page took 0.188738 seconds and 4 git commands to generate.