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