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