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