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