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