Fix: leak on error in lttng-crash
[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 }
915#if 0
916 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
917 case LTTNG_DOMAIN_UST_EXEC_NAME:
918 case LTTNG_DOMAIN_UST_PID:
919#endif
920 default:
f73fabfd 921 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
922 goto error;
923 }
924
f73fabfd 925 ret = LTTNG_OK;
2f77fc4b
DG
926
927error:
2223c96f 928 rcu_read_unlock();
2f77fc4b
DG
929 return ret;
930}
931
932/*
933 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
934 *
935 * The wpipe arguments is used as a notifier for the kernel thread.
936 */
937int cmd_enable_channel(struct ltt_session *session,
7972aab2 938 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
2f77fc4b
DG
939{
940 int ret;
941 struct ltt_ust_session *usess = session->ust_session;
942 struct lttng_ht *chan_ht;
1f345e94 943 size_t len;
2f77fc4b
DG
944
945 assert(session);
946 assert(attr);
7972aab2 947 assert(domain);
2f77fc4b 948
1f345e94
PP
949 len = strnlen(attr->name, sizeof(attr->name));
950
951 /* Validate channel name */
952 if (attr->name[0] == '.' ||
953 memchr(attr->name, '/', len) != NULL) {
954 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
955 goto end;
956 }
957
2f77fc4b
DG
958 DBG("Enabling channel %s for session %s", attr->name, session->name);
959
03b4fdcf
DG
960 rcu_read_lock();
961
ecc48a90
JD
962 /*
963 * Don't try to enable a channel if the session has been started at
964 * some point in time before. The tracer does not allow it.
965 */
8382cf6f 966 if (session->has_been_started) {
ecc48a90
JD
967 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
968 goto error;
969 }
970
971 /*
972 * If the session is a live session, remove the switch timer, the
973 * live timer does the same thing but sends also synchronisation
974 * beacons for inactive streams.
975 */
976 if (session->live_timer > 0) {
977 attr->attr.live_timer_interval = session->live_timer;
978 attr->attr.switch_timer_interval = 0;
979 }
980
33fbd469
DG
981 /*
982 * The ringbuffer (both in user space and kernel) behave badly in overwrite
983 * mode and with less than 2 subbuf so block it right away and send back an
984 * invalid attribute error.
985 */
986 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
987 ret = LTTNG_ERR_INVALID;
988 goto error;
989 }
990
7972aab2 991 switch (domain->type) {
2f77fc4b
DG
992 case LTTNG_DOMAIN_KERNEL:
993 {
994 struct ltt_kernel_channel *kchan;
995
2f77fc4b
DG
996 kchan = trace_kernel_get_channel_by_name(attr->name,
997 session->kernel_session);
998 if (kchan == NULL) {
999 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
85076754
MD
1000 if (attr->name[0] != '\0') {
1001 session->kernel_session->has_non_default_channel = 1;
1002 }
2f77fc4b
DG
1003 } else {
1004 ret = channel_kernel_enable(session->kernel_session, kchan);
1005 }
1006
f73fabfd 1007 if (ret != LTTNG_OK) {
2f77fc4b
DG
1008 goto error;
1009 }
1010
784303b0 1011 kernel_wait_quiescent(kernel_tracer_fd);
2f77fc4b
DG
1012 break;
1013 }
1014 case LTTNG_DOMAIN_UST:
1015 {
1016 struct ltt_ust_channel *uchan;
1017
1018 chan_ht = usess->domain_global.channels;
1019
1020 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1021 if (uchan == NULL) {
7972aab2 1022 ret = channel_ust_create(usess, attr, domain->buf_type);
85076754
MD
1023 if (attr->name[0] != '\0') {
1024 usess->has_non_default_channel = 1;
1025 }
2f77fc4b 1026 } else {
7972aab2 1027 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1028 }
1029 break;
1030 }
2f77fc4b 1031 default:
f73fabfd 1032 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1033 goto error;
1034 }
1035
1036error:
2223c96f 1037 rcu_read_unlock();
1f345e94 1038end:
2f77fc4b
DG
1039 return ret;
1040}
1041
1042
1043/*
1044 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1045 */
1046int cmd_disable_event(struct ltt_session *session, int domain,
6e911cad
MD
1047 char *channel_name,
1048 struct lttng_event *event)
2f77fc4b
DG
1049{
1050 int ret;
6e911cad
MD
1051 char *event_name;
1052
18a720cd
MD
1053 DBG("Disable event command for event \'%s\'", event->name);
1054
6e911cad
MD
1055 event_name = event->name;
1056
9b7431cf
JG
1057 /* Error out on unhandled search criteria */
1058 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad
MD
1059 || event->pid || event->filter || event->exclusion) {
1060 return LTTNG_ERR_UNK;
1061 }
18a720cd
MD
1062 /* Special handling for kernel domain all events. */
1063 if (domain == LTTNG_DOMAIN_KERNEL && !strcmp(event_name, "*")) {
1064 return disable_kevent_all(session, domain, channel_name, event);
1065 }
2f77fc4b 1066
2223c96f
DG
1067 rcu_read_lock();
1068
2f77fc4b
DG
1069 switch (domain) {
1070 case LTTNG_DOMAIN_KERNEL:
1071 {
1072 struct ltt_kernel_channel *kchan;
1073 struct ltt_kernel_session *ksess;
1074
1075 ksess = session->kernel_session;
1076
85076754
MD
1077 /*
1078 * If a non-default channel has been created in the
1079 * session, explicitely require that -c chan_name needs
1080 * to be provided.
1081 */
1082 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1083 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1084 goto error;
1085 }
1086
2f77fc4b
DG
1087 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1088 if (kchan == NULL) {
f73fabfd 1089 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
1090 goto error;
1091 }
1092
6e911cad
MD
1093 switch (event->type) {
1094 case LTTNG_EVENT_ALL:
1095 case LTTNG_EVENT_TRACEPOINT:
1096 ret = event_kernel_disable_tracepoint(kchan, event_name);
1097 if (ret != LTTNG_OK) {
1098 goto error;
1099 }
1100 break;
1101 case LTTNG_EVENT_SYSCALL:
1102 ret = event_kernel_disable_syscall(kchan, event_name);
e2b957af
MD
1103 if (ret != LTTNG_OK) {
1104 goto error;
1105 }
6e911cad
MD
1106 break;
1107 default:
1108 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1109 goto error;
1110 }
1111
1112 kernel_wait_quiescent(kernel_tracer_fd);
1113 break;
1114 }
1115 case LTTNG_DOMAIN_UST:
1116 {
1117 struct ltt_ust_channel *uchan;
1118 struct ltt_ust_session *usess;
1119
1120 usess = session->ust_session;
1121
85076754
MD
1122 /*
1123 * If a non-default channel has been created in the
1124 * session, explicitely require that -c chan_name needs
1125 * to be provided.
1126 */
1127 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1128 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1129 goto error;
1130 }
1131
2f77fc4b
DG
1132 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1133 channel_name);
1134 if (uchan == NULL) {
f73fabfd 1135 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1136 goto error;
1137 }
1138
6e911cad
MD
1139 switch (event->type) {
1140 case LTTNG_EVENT_ALL:
1141 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
1142 if (ret != LTTNG_OK) {
1143 goto error;
1144 }
1145 break;
1146 default:
1147 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1148 goto error;
1149 }
1150
1151 DBG3("Disable UST event %s in channel %s completed", event_name,
1152 channel_name);
1153 break;
1154 }
5cdb6027 1155 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1156 case LTTNG_DOMAIN_JUL:
0e115563 1157 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1158 {
fefd409b 1159 struct agent *agt;
f20baf8e
DG
1160 struct ltt_ust_session *usess = session->ust_session;
1161
1162 assert(usess);
1163
6e911cad
MD
1164 switch (event->type) {
1165 case LTTNG_EVENT_ALL:
1166 break;
1167 default:
1168 ret = LTTNG_ERR_UNK;
1169 goto error;
1170 }
1171
5cdb6027 1172 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1173 if (!agt) {
1174 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1175 goto error;
1176 }
18a720cd
MD
1177 /* The wild card * means that everything should be disabled. */
1178 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
1179 ret = event_agent_disable_all(usess, agt);
1180 } else {
1181 ret = event_agent_disable(usess, agt, event_name);
1182 }
f20baf8e
DG
1183 if (ret != LTTNG_OK) {
1184 goto error;
1185 }
1186
1187 break;
1188 }
2f77fc4b
DG
1189#if 0
1190 case LTTNG_DOMAIN_UST_EXEC_NAME:
1191 case LTTNG_DOMAIN_UST_PID:
1192 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1193#endif
1194 default:
f73fabfd 1195 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1196 goto error;
1197 }
1198
f73fabfd 1199 ret = LTTNG_OK;
2f77fc4b
DG
1200
1201error:
2223c96f 1202 rcu_read_unlock();
2f77fc4b
DG
1203 return ret;
1204}
1205
1206/*
18a720cd 1207 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
2f77fc4b 1208 */
18a720cd
MD
1209static
1210int disable_kevent_all(struct ltt_session *session, int domain,
6e911cad
MD
1211 char *channel_name,
1212 struct lttng_event *event)
2f77fc4b
DG
1213{
1214 int ret;
1215
2223c96f
DG
1216 rcu_read_lock();
1217
2f77fc4b
DG
1218 switch (domain) {
1219 case LTTNG_DOMAIN_KERNEL:
1220 {
1221 struct ltt_kernel_session *ksess;
1222 struct ltt_kernel_channel *kchan;
1223
1224 ksess = session->kernel_session;
1225
85076754
MD
1226 /*
1227 * If a non-default channel has been created in the
1228 * session, explicitely require that -c chan_name needs
1229 * to be provided.
1230 */
1231 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1232 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1233 goto error;
1234 }
1235
2f77fc4b
DG
1236 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1237 if (kchan == NULL) {
f73fabfd 1238 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
1239 goto error;
1240 }
1241
6e911cad
MD
1242 switch (event->type) {
1243 case LTTNG_EVENT_ALL:
1244 ret = event_kernel_disable_all(kchan);
1245 if (ret != LTTNG_OK) {
1246 goto error;
1247 }
1248 break;
1249 case LTTNG_EVENT_SYSCALL:
e2b957af
MD
1250 ret = event_kernel_disable_syscall(kchan, "");
1251 if (ret != LTTNG_OK) {
1252 goto error;
1253 }
6e911cad
MD
1254 break;
1255 default:
1256 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1257 goto error;
1258 }
1259
1260 kernel_wait_quiescent(kernel_tracer_fd);
1261 break;
1262 }
2f77fc4b 1263 default:
f73fabfd 1264 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1265 goto error;
1266 }
1267
f73fabfd 1268 ret = LTTNG_OK;
2f77fc4b
DG
1269
1270error:
2223c96f 1271 rcu_read_unlock();
2f77fc4b
DG
1272 return ret;
1273}
1274
1275/*
1276 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1277 */
1278int cmd_add_context(struct ltt_session *session, int domain,
601d5acf 1279 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1280{
d5979e4a 1281 int ret, chan_kern_created = 0, chan_ust_created = 0;
2f77fc4b
DG
1282
1283 switch (domain) {
1284 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
1285 assert(session->kernel_session);
1286
1287 if (session->kernel_session->channel_count == 0) {
1288 /* Create default channel */
1289 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1290 if (ret != LTTNG_OK) {
1291 goto error;
1292 }
d5979e4a 1293 chan_kern_created = 1;
979e618e 1294 }
2f77fc4b 1295 /* Add kernel context to kernel tracer */
601d5acf 1296 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1297 if (ret != LTTNG_OK) {
2f77fc4b
DG
1298 goto error;
1299 }
1300 break;
1301 case LTTNG_DOMAIN_UST:
1302 {
1303 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1304 unsigned int chan_count;
1305
2f77fc4b
DG
1306 assert(usess);
1307
85076754 1308 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1309 if (chan_count == 0) {
1310 struct lttng_channel *attr;
1311 /* Create default channel */
0a9c6494 1312 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1313 if (attr == NULL) {
1314 ret = LTTNG_ERR_FATAL;
1315 goto error;
1316 }
1317
7972aab2 1318 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1319 if (ret != LTTNG_OK) {
1320 free(attr);
1321 goto error;
1322 }
1323 free(attr);
d5979e4a 1324 chan_ust_created = 1;
979e618e
DG
1325 }
1326
601d5acf 1327 ret = context_ust_add(usess, domain, ctx, channel_name);
f73fabfd 1328 if (ret != LTTNG_OK) {
2f77fc4b
DG
1329 goto error;
1330 }
1331 break;
1332 }
1333#if 0
1334 case LTTNG_DOMAIN_UST_EXEC_NAME:
1335 case LTTNG_DOMAIN_UST_PID:
1336 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1337#endif
1338 default:
f73fabfd 1339 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1340 goto error;
1341 }
1342
d5979e4a 1343 return LTTNG_OK;
2f77fc4b
DG
1344
1345error:
d5979e4a
DG
1346 if (chan_kern_created) {
1347 struct ltt_kernel_channel *kchan =
1348 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1349 session->kernel_session);
1350 /* Created previously, this should NOT fail. */
1351 assert(kchan);
1352 kernel_destroy_channel(kchan);
1353 }
1354
1355 if (chan_ust_created) {
1356 struct ltt_ust_channel *uchan =
1357 trace_ust_find_channel_by_name(
1358 session->ust_session->domain_global.channels,
1359 DEFAULT_CHANNEL_NAME);
1360 /* Created previously, this should NOT fail. */
1361 assert(uchan);
1362 /* Remove from the channel list of the session. */
1363 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1364 uchan);
1365 trace_ust_destroy_channel(uchan);
1366 }
2f77fc4b
DG
1367 return ret;
1368}
1369
930a2e99
JG
1370static int validate_event_name(const char *name)
1371{
1372 int ret = 0;
1373 const char *c = name;
1374 const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN;
1375
1376 /*
1377 * Make sure that unescaped wildcards are only used as the last
1378 * character of the event name.
1379 */
1380 while (c < event_name_end) {
1381 switch (*c) {
1382 case '\0':
1383 goto end;
1384 case '\\':
1385 c++;
1386 break;
1387 case '*':
1388 if ((c + 1) < event_name_end && *(c + 1)) {
1389 /* Wildcard is not the last character */
1390 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1391 goto end;
1392 }
1393 default:
1394 break;
1395 }
1396 c++;
1397 }
1398end:
1399 return ret;
1400}
1401
2f77fc4b
DG
1402/*
1403 * Command LTTNG_ENABLE_EVENT processed by the client thread.
49d21f93 1404 * We own filter, exclusion, and filter_expression.
2f77fc4b 1405 */
7972aab2 1406int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
025faf73 1407 char *channel_name, struct lttng_event *event,
6b453b5e 1408 char *filter_expression,
db8f1377
JI
1409 struct lttng_filter_bytecode *filter,
1410 struct lttng_event_exclusion *exclusion,
1411 int wpipe)
2f77fc4b 1412{
e5f5db7f 1413 int ret, channel_created = 0;
2f77fc4b
DG
1414 struct lttng_channel *attr;
1415
1416 assert(session);
1417 assert(event);
1418 assert(channel_name);
1419
18a720cd
MD
1420 DBG("Enable event command for event \'%s\'", event->name);
1421
1422 /* Special handling for kernel domain all events. */
1423 if (domain->type == LTTNG_DOMAIN_KERNEL && !strcmp(event->name, "*")) {
1424 return enable_kevent_all(session, domain, channel_name, event,
1425 filter_expression, filter, wpipe);
1426 }
1427
930a2e99
JG
1428 ret = validate_event_name(event->name);
1429 if (ret) {
1430 goto error;
1431 }
1432
2223c96f
DG
1433 rcu_read_lock();
1434
7972aab2 1435 switch (domain->type) {
2f77fc4b
DG
1436 case LTTNG_DOMAIN_KERNEL:
1437 {
1438 struct ltt_kernel_channel *kchan;
1439
85076754
MD
1440 /*
1441 * If a non-default channel has been created in the
1442 * session, explicitely require that -c chan_name needs
1443 * to be provided.
1444 */
1445 if (session->kernel_session->has_non_default_channel
1446 && channel_name[0] == '\0') {
1447 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1448 goto error;
1449 }
1450
2f77fc4b
DG
1451 kchan = trace_kernel_get_channel_by_name(channel_name,
1452 session->kernel_session);
1453 if (kchan == NULL) {
0a9c6494
DG
1454 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1455 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1456 if (attr == NULL) {
f73fabfd 1457 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1458 goto error;
1459 }
1460 strncpy(attr->name, channel_name, sizeof(attr->name));
1461
1462 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1463 if (ret != LTTNG_OK) {
2f77fc4b
DG
1464 free(attr);
1465 goto error;
1466 }
1467 free(attr);
e5f5db7f
DG
1468
1469 channel_created = 1;
2f77fc4b
DG
1470 }
1471
1472 /* Get the newly created kernel channel pointer */
1473 kchan = trace_kernel_get_channel_by_name(channel_name,
1474 session->kernel_session);
1475 if (kchan == NULL) {
1476 /* This sould not happen... */
f73fabfd 1477 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1478 goto error;
1479 }
1480
6e911cad
MD
1481 switch (event->type) {
1482 case LTTNG_EVENT_ALL:
6e6ef3d7
DG
1483 case LTTNG_EVENT_PROBE:
1484 case LTTNG_EVENT_FUNCTION:
1485 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad
MD
1486 case LTTNG_EVENT_TRACEPOINT:
1487 ret = event_kernel_enable_tracepoint(kchan, event);
1488 if (ret != LTTNG_OK) {
1489 if (channel_created) {
1490 /* Let's not leak a useless channel. */
1491 kernel_destroy_channel(kchan);
1492 }
1493 goto error;
e5f5db7f 1494 }
6e911cad
MD
1495 break;
1496 case LTTNG_EVENT_SYSCALL:
1497 ret = event_kernel_enable_syscall(kchan, event->name);
e2b957af
MD
1498 if (ret != LTTNG_OK) {
1499 goto error;
1500 }
6e911cad
MD
1501 break;
1502 default:
1503 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1504 goto error;
1505 }
1506
1507 kernel_wait_quiescent(kernel_tracer_fd);
1508 break;
1509 }
1510 case LTTNG_DOMAIN_UST:
1511 {
1512 struct ltt_ust_channel *uchan;
1513 struct ltt_ust_session *usess = session->ust_session;
1514
1515 assert(usess);
1516
85076754
MD
1517 /*
1518 * If a non-default channel has been created in the
1519 * session, explicitely require that -c chan_name needs
1520 * to be provided.
1521 */
1522 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1523 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1524 goto error;
1525 }
1526
2f77fc4b
DG
1527 /* Get channel from global UST domain */
1528 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1529 channel_name);
1530 if (uchan == NULL) {
1531 /* Create default channel */
0a9c6494
DG
1532 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1533 usess->buffer_type);
2f77fc4b 1534 if (attr == NULL) {
f73fabfd 1535 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1536 goto error;
1537 }
1538 strncpy(attr->name, channel_name, sizeof(attr->name));
1539
1540 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1541 if (ret != LTTNG_OK) {
2f77fc4b
DG
1542 free(attr);
1543 goto error;
1544 }
1545 free(attr);
1546
1547 /* Get the newly created channel reference back */
1548 uchan = trace_ust_find_channel_by_name(
1549 usess->domain_global.channels, channel_name);
1550 assert(uchan);
1551 }
1552
1553 /* At this point, the session and channel exist on the tracer */
6b453b5e
JG
1554 ret = event_ust_enable_tracepoint(usess, uchan, event,
1555 filter_expression, filter, exclusion);
49d21f93
MD
1556 /* We have passed ownership */
1557 filter_expression = NULL;
1558 filter = NULL;
1559 exclusion = NULL;
f73fabfd 1560 if (ret != LTTNG_OK) {
2f77fc4b
DG
1561 goto error;
1562 }
1563 break;
1564 }
5cdb6027 1565 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1566 case LTTNG_DOMAIN_JUL:
0e115563 1567 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1568 {
da6c3a50 1569 const char *default_event_name, *default_chan_name;
fefd409b 1570 struct agent *agt;
f20baf8e
DG
1571 struct lttng_event uevent;
1572 struct lttng_domain tmp_dom;
1573 struct ltt_ust_session *usess = session->ust_session;
1574
1575 assert(usess);
1576
5cdb6027 1577 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 1578 if (!agt) {
5cdb6027 1579 agt = agent_create(domain->type);
fefd409b
DG
1580 if (!agt) {
1581 ret = -LTTNG_ERR_NOMEM;
1582 goto error;
1583 }
1584 agent_add(agt, usess->agents);
1585 }
1586
022d91ba 1587 /* Create the default tracepoint. */
996de3c7 1588 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
1589 uevent.type = LTTNG_EVENT_TRACEPOINT;
1590 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
da6c3a50
DG
1591 default_event_name = event_get_default_agent_ust_name(domain->type);
1592 if (!default_event_name) {
1593 ret = -LTTNG_ERR_FATAL;
1594 goto error;
f43f95a9 1595 }
da6c3a50 1596 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
1597 uevent.name[sizeof(uevent.name) - 1] = '\0';
1598
1599 /*
1600 * The domain type is changed because we are about to enable the
1601 * default channel and event for the JUL domain that are hardcoded.
1602 * This happens in the UST domain.
1603 */
1604 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
1605 tmp_dom.type = LTTNG_DOMAIN_UST;
1606
0e115563
DG
1607 switch (domain->type) {
1608 case LTTNG_DOMAIN_LOG4J:
da6c3a50 1609 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
1610 break;
1611 case LTTNG_DOMAIN_JUL:
da6c3a50 1612 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
1613 break;
1614 case LTTNG_DOMAIN_PYTHON:
1615 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
1616 break;
1617 default:
1618 /* The switch/case we are in should avoid this else big problem */
1619 assert(0);
da6c3a50
DG
1620 }
1621
971da06a
JG
1622 {
1623 struct lttng_filter_bytecode *filter_copy = NULL;
1624
1625 if (filter) {
1626 filter_copy = zmalloc(
1627 sizeof(struct lttng_filter_bytecode)
1628 + filter->len);
1629 if (!filter_copy) {
1630 goto error;
1631 }
1632
1633 memcpy(filter_copy, filter,
1634 sizeof(struct lttng_filter_bytecode)
1635 + filter->len);
1636 }
1637
1638 ret = cmd_enable_event(session, &tmp_dom,
1639 (char *) default_chan_name,
1640 &uevent, filter_expression, filter_copy,
1641 NULL, wpipe);
1642 /* We have passed ownership */
1643 filter_expression = NULL;
1644 }
1645
f20baf8e
DG
1646 if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
1647 goto error;
1648 }
1649
1650 /* The wild card * means that everything should be enabled. */
1651 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
fefd409b 1652 ret = event_agent_enable_all(usess, agt, event, filter);
971da06a 1653 filter = NULL;
f20baf8e 1654 } else {
fefd409b 1655 ret = event_agent_enable(usess, agt, event, filter);
971da06a 1656 filter = NULL;
f20baf8e
DG
1657 }
1658 if (ret != LTTNG_OK) {
1659 goto error;
1660 }
1661
1662 break;
1663 }
2f77fc4b
DG
1664#if 0
1665 case LTTNG_DOMAIN_UST_EXEC_NAME:
1666 case LTTNG_DOMAIN_UST_PID:
1667 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1668#endif
1669 default:
f73fabfd 1670 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1671 goto error;
1672 }
1673
f73fabfd 1674 ret = LTTNG_OK;
2f77fc4b
DG
1675
1676error:
49d21f93
MD
1677 free(filter_expression);
1678 free(filter);
1679 free(exclusion);
2223c96f 1680 rcu_read_unlock();
2f77fc4b
DG
1681 return ret;
1682}
1683
1684/*
18a720cd 1685 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
2f77fc4b 1686 */
18a720cd
MD
1687static
1688int enable_kevent_all(struct ltt_session *session,
1689 struct lttng_domain *domain, char *channel_name,
1690 struct lttng_event *event,
6b453b5e 1691 char *filter_expression,
025faf73 1692 struct lttng_filter_bytecode *filter, int wpipe)
2f77fc4b
DG
1693{
1694 int ret;
1695 struct lttng_channel *attr;
1696
1697 assert(session);
1698 assert(channel_name);
1699
2223c96f
DG
1700 rcu_read_lock();
1701
7972aab2 1702 switch (domain->type) {
2f77fc4b
DG
1703 case LTTNG_DOMAIN_KERNEL:
1704 {
1705 struct ltt_kernel_channel *kchan;
1706
1707 assert(session->kernel_session);
1708
85076754
MD
1709 /*
1710 * If a non-default channel has been created in the
1711 * session, explicitely require that -c chan_name needs
1712 * to be provided.
1713 */
1714 if (session->kernel_session->has_non_default_channel
1715 && channel_name[0] == '\0') {
1716 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1717 goto error;
1718 }
1719
2f77fc4b
DG
1720 kchan = trace_kernel_get_channel_by_name(channel_name,
1721 session->kernel_session);
1722 if (kchan == NULL) {
1723 /* Create default channel */
0a9c6494
DG
1724 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1725 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1726 if (attr == NULL) {
f73fabfd 1727 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1728 goto error;
1729 }
1730 strncpy(attr->name, channel_name, sizeof(attr->name));
1731
1732 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1733 if (ret != LTTNG_OK) {
2f77fc4b
DG
1734 free(attr);
1735 goto error;
1736 }
1737 free(attr);
1738
1739 /* Get the newly created kernel channel pointer */
1740 kchan = trace_kernel_get_channel_by_name(channel_name,
1741 session->kernel_session);
1742 assert(kchan);
1743 }
1744
18a720cd 1745 switch (event->type) {
2f77fc4b 1746 case LTTNG_EVENT_SYSCALL:
6e911cad 1747 ret = event_kernel_enable_syscall(kchan, "");
e2b957af
MD
1748 if (ret != LTTNG_OK) {
1749 goto error;
1750 }
2f77fc4b
DG
1751 break;
1752 case LTTNG_EVENT_TRACEPOINT:
1753 /*
1754 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1755 * events already registered to the channel.
1756 */
d3a56674 1757 ret = event_kernel_enable_all_tracepoints(kchan, kernel_tracer_fd);
2f77fc4b
DG
1758 break;
1759 case LTTNG_EVENT_ALL:
1760 /* Enable syscalls and tracepoints */
d3a56674 1761 ret = event_kernel_enable_all(kchan, kernel_tracer_fd);
2f77fc4b
DG
1762 break;
1763 default:
f73fabfd 1764 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2f77fc4b
DG
1765 goto error;
1766 }
1767
1768 /* Manage return value */
f73fabfd 1769 if (ret != LTTNG_OK) {
e5f5db7f
DG
1770 /*
1771 * On error, cmd_enable_channel call will take care of destroying
1772 * the created channel if it was needed.
1773 */
2f77fc4b
DG
1774 goto error;
1775 }
1776
1777 kernel_wait_quiescent(kernel_tracer_fd);
1778 break;
1779 }
2f77fc4b 1780 default:
f73fabfd 1781 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1782 goto error;
1783 }
1784
f73fabfd 1785 ret = LTTNG_OK;
2f77fc4b
DG
1786
1787error:
2223c96f 1788 rcu_read_unlock();
2f77fc4b
DG
1789 return ret;
1790}
1791
1792
1793/*
1794 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1795 */
1796ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1797{
1798 int ret;
1799 ssize_t nb_events = 0;
1800
1801 switch (domain) {
1802 case LTTNG_DOMAIN_KERNEL:
1803 nb_events = kernel_list_events(kernel_tracer_fd, events);
1804 if (nb_events < 0) {
f73fabfd 1805 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
1806 goto error;
1807 }
1808 break;
1809 case LTTNG_DOMAIN_UST:
1810 nb_events = ust_app_list_events(events);
1811 if (nb_events < 0) {
f73fabfd 1812 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
1813 goto error;
1814 }
1815 break;
5cdb6027 1816 case LTTNG_DOMAIN_LOG4J:
3c6a091f 1817 case LTTNG_DOMAIN_JUL:
0e115563 1818 case LTTNG_DOMAIN_PYTHON:
f60140a1 1819 nb_events = agent_list_events(events, domain);
3c6a091f
DG
1820 if (nb_events < 0) {
1821 ret = LTTNG_ERR_UST_LIST_FAIL;
1822 goto error;
1823 }
1824 break;
2f77fc4b 1825 default:
f73fabfd 1826 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1827 goto error;
1828 }
1829
1830 return nb_events;
1831
1832error:
1833 /* Return negative value to differentiate return code */
1834 return -ret;
1835}
1836
1837/*
1838 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1839 */
1840ssize_t cmd_list_tracepoint_fields(int domain,
1841 struct lttng_event_field **fields)
1842{
1843 int ret;
1844 ssize_t nb_fields = 0;
1845
1846 switch (domain) {
1847 case LTTNG_DOMAIN_UST:
1848 nb_fields = ust_app_list_event_fields(fields);
1849 if (nb_fields < 0) {
f73fabfd 1850 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
1851 goto error;
1852 }
1853 break;
1854 case LTTNG_DOMAIN_KERNEL:
1855 default: /* fall-through */
f73fabfd 1856 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1857 goto error;
1858 }
1859
1860 return nb_fields;
1861
1862error:
1863 /* Return negative value to differentiate return code */
1864 return -ret;
1865}
1866
834978fd
DG
1867ssize_t cmd_list_syscalls(struct lttng_event **events)
1868{
1869 return syscall_table_list(events);
1870}
1871
2f77fc4b
DG
1872/*
1873 * Command LTTNG_START_TRACE processed by the client thread.
1874 */
1875int cmd_start_trace(struct ltt_session *session)
1876{
1877 int ret;
cde3e505 1878 unsigned long nb_chan = 0;
2f77fc4b
DG
1879 struct ltt_kernel_session *ksession;
1880 struct ltt_ust_session *usess;
2f77fc4b
DG
1881
1882 assert(session);
1883
1884 /* Ease our life a bit ;) */
1885 ksession = session->kernel_session;
1886 usess = session->ust_session;
1887
8382cf6f
DG
1888 /* Is the session already started? */
1889 if (session->active) {
f73fabfd 1890 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
1891 goto error;
1892 }
1893
cde3e505
DG
1894 /*
1895 * Starting a session without channel is useless since after that it's not
1896 * possible to enable channel thus inform the client.
1897 */
1898 if (usess && usess->domain_global.channels) {
a9e41044 1899 rcu_read_lock();
cde3e505 1900 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
a9e41044 1901 rcu_read_unlock();
cde3e505
DG
1902 }
1903 if (ksession) {
1904 nb_chan += ksession->channel_count;
1905 }
1906 if (!nb_chan) {
1907 ret = LTTNG_ERR_NO_CHANNEL;
1908 goto error;
1909 }
1910
2f77fc4b
DG
1911 /* Kernel tracing */
1912 if (ksession != NULL) {
9b6c7ec5
DG
1913 ret = start_kernel_session(ksession, kernel_tracer_fd);
1914 if (ret != LTTNG_OK) {
2f77fc4b
DG
1915 goto error;
1916 }
2f77fc4b
DG
1917 }
1918
1919 /* Flag session that trace should start automatically */
1920 if (usess) {
14fb1ebe
DG
1921 /*
1922 * Even though the start trace might fail, flag this session active so
1923 * other application coming in are started by default.
1924 */
1925 usess->active = 1;
2f77fc4b
DG
1926
1927 ret = ust_app_start_trace_all(usess);
1928 if (ret < 0) {
f73fabfd 1929 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
1930 goto error;
1931 }
1932 }
1933
8382cf6f
DG
1934 /* Flag this after a successful start. */
1935 session->has_been_started = 1;
1936 session->active = 1;
9b6c7ec5 1937
f73fabfd 1938 ret = LTTNG_OK;
2f77fc4b
DG
1939
1940error:
1941 return ret;
1942}
1943
1944/*
1945 * Command LTTNG_STOP_TRACE processed by the client thread.
1946 */
1947int cmd_stop_trace(struct ltt_session *session)
1948{
1949 int ret;
1950 struct ltt_kernel_channel *kchan;
1951 struct ltt_kernel_session *ksession;
1952 struct ltt_ust_session *usess;
1953
1954 assert(session);
1955
1956 /* Short cut */
1957 ksession = session->kernel_session;
1958 usess = session->ust_session;
1959
8382cf6f
DG
1960 /* Session is not active. Skip everythong and inform the client. */
1961 if (!session->active) {
f73fabfd 1962 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
1963 goto error;
1964 }
1965
2f77fc4b 1966 /* Kernel tracer */
14fb1ebe 1967 if (ksession && ksession->active) {
2f77fc4b
DG
1968 DBG("Stop kernel tracing");
1969
1970 /* Flush metadata if exist */
1971 if (ksession->metadata_stream_fd >= 0) {
1972 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1973 if (ret < 0) {
1974 ERR("Kernel metadata flush failed");
1975 }
1976 }
1977
1978 /* Flush all buffers before stopping */
1979 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1980 ret = kernel_flush_buffer(kchan);
1981 if (ret < 0) {
1982 ERR("Kernel flush buffer error");
1983 }
1984 }
1985
1986 ret = kernel_stop_session(ksession);
1987 if (ret < 0) {
f73fabfd 1988 ret = LTTNG_ERR_KERN_STOP_FAIL;
2f77fc4b
DG
1989 goto error;
1990 }
1991
1992 kernel_wait_quiescent(kernel_tracer_fd);
9b6c7ec5 1993
14fb1ebe 1994 ksession->active = 0;
2f77fc4b
DG
1995 }
1996
14fb1ebe
DG
1997 if (usess && usess->active) {
1998 /*
1999 * Even though the stop trace might fail, flag this session inactive so
2000 * other application coming in are not started by default.
2001 */
2002 usess->active = 0;
2f77fc4b
DG
2003
2004 ret = ust_app_stop_trace_all(usess);
2005 if (ret < 0) {
f73fabfd 2006 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2007 goto error;
2008 }
2009 }
2010
8382cf6f
DG
2011 /* Flag inactive after a successful stop. */
2012 session->active = 0;
f73fabfd 2013 ret = LTTNG_OK;
2f77fc4b
DG
2014
2015error:
2016 return ret;
2017}
2018
2019/*
2020 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2021 */
bda32d56
JG
2022int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2023 struct lttng_uri *uris)
2f77fc4b
DG
2024{
2025 int ret, i;
2026 struct ltt_kernel_session *ksess = session->kernel_session;
2027 struct ltt_ust_session *usess = session->ust_session;
2f77fc4b
DG
2028
2029 assert(session);
2030 assert(uris);
2031 assert(nb_uri > 0);
2032
8382cf6f
DG
2033 /* Can't set consumer URI if the session is active. */
2034 if (session->active) {
f73fabfd 2035 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
2036 goto error;
2037 }
2038
bda32d56 2039 /* Set the "global" consumer URIs */
2f77fc4b 2040 for (i = 0; i < nb_uri; i++) {
bda32d56
JG
2041 ret = add_uri_to_consumer(session->consumer,
2042 &uris[i], 0, session->name);
a74934ba 2043 if (ret != LTTNG_OK) {
2f77fc4b
DG
2044 goto error;
2045 }
2f77fc4b
DG
2046 }
2047
bda32d56
JG
2048 /* Set UST session URIs */
2049 if (session->ust_session) {
2050 for (i = 0; i < nb_uri; i++) {
2051 ret = add_uri_to_consumer(
2052 session->ust_session->consumer,
2053 &uris[i], LTTNG_DOMAIN_UST,
2054 session->name);
2055 if (ret != LTTNG_OK) {
2056 goto error;
2057 }
2058 }
2059 }
2060
2061 /* Set kernel session URIs */
2062 if (session->kernel_session) {
2063 for (i = 0; i < nb_uri; i++) {
2064 ret = add_uri_to_consumer(
2065 session->kernel_session->consumer,
2066 &uris[i], LTTNG_DOMAIN_KERNEL,
2067 session->name);
2068 if (ret != LTTNG_OK) {
2069 goto error;
2070 }
2071 }
2072 }
2073
7ab70fe0
DG
2074 /*
2075 * Make sure to set the session in output mode after we set URI since a
2076 * session can be created without URL (thus flagged in no output mode).
2077 */
2078 session->output_traces = 1;
2079 if (ksess) {
2080 ksess->output_traces = 1;
bda32d56
JG
2081 }
2082
2083 if (usess) {
7ab70fe0
DG
2084 usess->output_traces = 1;
2085 }
2086
2f77fc4b 2087 /* All good! */
f73fabfd 2088 ret = LTTNG_OK;
2f77fc4b
DG
2089
2090error:
2091 return ret;
2092}
2093
2094/*
2095 * Command LTTNG_CREATE_SESSION processed by the client thread.
2096 */
2097int cmd_create_session_uri(char *name, struct lttng_uri *uris,
ecc48a90 2098 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2f77fc4b
DG
2099{
2100 int ret;
2f77fc4b
DG
2101 struct ltt_session *session;
2102
2103 assert(name);
2bba9e53 2104 assert(creds);
785d2d0d 2105
2f77fc4b
DG
2106 /*
2107 * Verify if the session already exist
2108 *
2109 * XXX: There is no need for the session lock list here since the caller
2110 * (process_client_msg) is holding it. We might want to change that so a
2111 * single command does not lock the entire session list.
2112 */
2113 session = session_find_by_name(name);
2114 if (session != NULL) {
f73fabfd 2115 ret = LTTNG_ERR_EXIST_SESS;
2f77fc4b
DG
2116 goto find_error;
2117 }
2118
2119 /* Create tracing session in the registry */
dec56f6c 2120 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2f77fc4b 2121 LTTNG_SOCK_GET_GID_CRED(creds));
f73fabfd 2122 if (ret != LTTNG_OK) {
2f77fc4b
DG
2123 goto session_error;
2124 }
2125
2126 /*
2127 * Get the newly created session pointer back
2128 *
2129 * XXX: There is no need for the session lock list here since the caller
2130 * (process_client_msg) is holding it. We might want to change that so a
2131 * single command does not lock the entire session list.
2132 */
2133 session = session_find_by_name(name);
2134 assert(session);
2135
ecc48a90 2136 session->live_timer = live_timer;
2f77fc4b
DG
2137 /* Create default consumer output for the session not yet created. */
2138 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2139 if (session->consumer == NULL) {
f73fabfd 2140 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2141 goto consumer_error;
2142 }
2143
2bba9e53 2144 if (uris) {
bda32d56 2145 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2bba9e53
DG
2146 if (ret != LTTNG_OK) {
2147 goto consumer_error;
2148 }
2149 session->output_traces = 1;
2150 } else {
2151 session->output_traces = 0;
2152 DBG2("Session %s created with no output", session->name);
2f77fc4b
DG
2153 }
2154
2155 session->consumer->enabled = 1;
2156
f73fabfd 2157 return LTTNG_OK;
2f77fc4b
DG
2158
2159consumer_error:
2160 session_destroy(session);
2161session_error:
2162find_error:
2163 return ret;
2164}
2165
27babd3a
DG
2166/*
2167 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2168 */
2169int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2170 size_t nb_uri, lttng_sock_cred *creds)
2171{
2172 int ret;
2173 struct ltt_session *session;
2174 struct snapshot_output *new_output = NULL;
2175
2176 assert(name);
2177 assert(creds);
2178
2179 /*
2180 * Create session in no output mode with URIs set to NULL. The uris we've
2181 * received are for a default snapshot output if one.
2182 */
ecc48a90 2183 ret = cmd_create_session_uri(name, NULL, 0, creds, -1);
27babd3a
DG
2184 if (ret != LTTNG_OK) {
2185 goto error;
2186 }
2187
2188 /* Get the newly created session pointer back. This should NEVER fail. */
2189 session = session_find_by_name(name);
2190 assert(session);
2191
2192 /* Flag session for snapshot mode. */
2193 session->snapshot_mode = 1;
2194
2195 /* Skip snapshot output creation if no URI is given. */
2196 if (nb_uri == 0) {
2197 goto end;
2198 }
2199
2200 new_output = snapshot_output_alloc();
2201 if (!new_output) {
2202 ret = LTTNG_ERR_NOMEM;
2203 goto error_snapshot_alloc;
2204 }
2205
2206 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2207 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2208 if (ret < 0) {
2209 if (ret == -ENOMEM) {
2210 ret = LTTNG_ERR_NOMEM;
2211 } else {
2212 ret = LTTNG_ERR_INVALID;
2213 }
2214 goto error_snapshot;
2215 }
2216
2217 rcu_read_lock();
2218 snapshot_add_output(&session->snapshot, new_output);
2219 rcu_read_unlock();
2220
2221end:
2222 return LTTNG_OK;
2223
2224error_snapshot:
2225 snapshot_output_destroy(new_output);
2226error_snapshot_alloc:
2227 session_destroy(session);
2228error:
2229 return ret;
2230}
2231
2f77fc4b
DG
2232/*
2233 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2234 */
2235int cmd_destroy_session(struct ltt_session *session, int wpipe)
2236{
2237 int ret;
2238 struct ltt_ust_session *usess;
2239 struct ltt_kernel_session *ksess;
2240
2241 /* Safety net */
2242 assert(session);
2243
2244 usess = session->ust_session;
2245 ksess = session->kernel_session;
2246
2247 /* Clean kernel session teardown */
2248 kernel_destroy_session(ksess);
2249
2250 /* UST session teardown */
2251 if (usess) {
2252 /* Close any relayd session */
2253 consumer_output_send_destroy_relayd(usess->consumer);
2254
2255 /* Destroy every UST application related to this session. */
2256 ret = ust_app_destroy_trace_all(usess);
2257 if (ret) {
2258 ERR("Error in ust_app_destroy_trace_all");
2259 }
2260
2261 /* Clean up the rest. */
2262 trace_ust_destroy_session(usess);
2263 }
2264
2265 /*
2266 * Must notify the kernel thread here to update it's poll set in order to
2267 * remove the channel(s)' fd just destroyed.
2268 */
2269 ret = notify_thread_pipe(wpipe);
2270 if (ret < 0) {
2271 PERROR("write kernel poll pipe");
2272 }
2273
2274 ret = session_destroy(session);
2275
2276 return ret;
2277}
2278
2279/*
2280 * Command LTTNG_CALIBRATE processed by the client thread.
2281 */
2282int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
2283{
2284 int ret;
2285
2286 switch (domain) {
2287 case LTTNG_DOMAIN_KERNEL:
2288 {
2289 struct lttng_kernel_calibrate kcalibrate;
2290
2e84128e
DG
2291 switch (calibrate->type) {
2292 case LTTNG_CALIBRATE_FUNCTION:
2293 default:
2294 /* Default and only possible calibrate option. */
2295 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2296 break;
2297 }
2298
2f77fc4b
DG
2299 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2300 if (ret < 0) {
f73fabfd 2301 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2f77fc4b
DG
2302 goto error;
2303 }
2304 break;
2305 }
2306 case LTTNG_DOMAIN_UST:
2307 {
2308 struct lttng_ust_calibrate ucalibrate;
2309
2e84128e
DG
2310 switch (calibrate->type) {
2311 case LTTNG_CALIBRATE_FUNCTION:
2312 default:
2313 /* Default and only possible calibrate option. */
2314 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2315 break;
2316 }
2317
2f77fc4b
DG
2318 ret = ust_app_calibrate_glb(&ucalibrate);
2319 if (ret < 0) {
f73fabfd 2320 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2f77fc4b
DG
2321 goto error;
2322 }
2323 break;
2324 }
2325 default:
f73fabfd 2326 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2327 goto error;
2328 }
2329
f73fabfd 2330 ret = LTTNG_OK;
2f77fc4b
DG
2331
2332error:
2333 return ret;
2334}
2335
2336/*
2337 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2338 */
2339int cmd_register_consumer(struct ltt_session *session, int domain,
2340 const char *sock_path, struct consumer_data *cdata)
2341{
2342 int ret, sock;
dd81b457 2343 struct consumer_socket *socket = NULL;
2f77fc4b
DG
2344
2345 assert(session);
2346 assert(cdata);
2347 assert(sock_path);
2348
2349 switch (domain) {
2350 case LTTNG_DOMAIN_KERNEL:
2351 {
2352 struct ltt_kernel_session *ksess = session->kernel_session;
2353
2354 assert(ksess);
2355
2356 /* Can't register a consumer if there is already one */
2357 if (ksess->consumer_fds_sent != 0) {
f73fabfd 2358 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
2359 goto error;
2360 }
2361
2362 sock = lttcomm_connect_unix_sock(sock_path);
2363 if (sock < 0) {
f73fabfd 2364 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
2365 goto error;
2366 }
4ce514c4 2367 cdata->cmd_sock = sock;
2f77fc4b 2368
4ce514c4 2369 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 2370 if (socket == NULL) {
f66c074c
DG
2371 ret = close(sock);
2372 if (ret < 0) {
2373 PERROR("close register consumer");
2374 }
4ce514c4 2375 cdata->cmd_sock = -1;
f73fabfd 2376 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2377 goto error;
2378 }
2379
2380 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2381 if (socket->lock == NULL) {
2382 PERROR("zmalloc pthread mutex");
f73fabfd 2383 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2384 goto error;
2385 }
2386 pthread_mutex_init(socket->lock, NULL);
2387 socket->registered = 1;
2388
2389 rcu_read_lock();
2390 consumer_add_socket(socket, ksess->consumer);
2391 rcu_read_unlock();
2392
2393 pthread_mutex_lock(&cdata->pid_mutex);
2394 cdata->pid = -1;
2395 pthread_mutex_unlock(&cdata->pid_mutex);
2396
2397 break;
2398 }
2399 default:
2400 /* TODO: Userspace tracing */
f73fabfd 2401 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2402 goto error;
2403 }
2404
dd81b457 2405 return LTTNG_OK;
2f77fc4b
DG
2406
2407error:
dd81b457
DG
2408 if (socket) {
2409 consumer_destroy_socket(socket);
2410 }
2f77fc4b
DG
2411 return ret;
2412}
2413
2414/*
2415 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2416 */
2417ssize_t cmd_list_domains(struct ltt_session *session,
2418 struct lttng_domain **domains)
2419{
2420 int ret, index = 0;
2421 ssize_t nb_dom = 0;
fefd409b
DG
2422 struct agent *agt;
2423 struct lttng_ht_iter iter;
2f77fc4b
DG
2424
2425 if (session->kernel_session != NULL) {
2426 DBG3("Listing domains found kernel domain");
2427 nb_dom++;
2428 }
2429
2430 if (session->ust_session != NULL) {
2431 DBG3("Listing domains found UST global domain");
2432 nb_dom++;
3c6a091f 2433
e0a74f01 2434 rcu_read_lock();
fefd409b
DG
2435 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2436 agt, node.node) {
2437 if (agt->being_used) {
2438 nb_dom++;
2439 }
3c6a091f 2440 }
e0a74f01 2441 rcu_read_unlock();
2f77fc4b
DG
2442 }
2443
fa64dfb4
JG
2444 if (!nb_dom) {
2445 goto end;
2446 }
2447
2f77fc4b
DG
2448 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2449 if (*domains == NULL) {
f73fabfd 2450 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2451 goto error;
2452 }
2453
2454 if (session->kernel_session != NULL) {
2455 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2456 index++;
2457 }
2458
2459 if (session->ust_session != NULL) {
2460 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 2461 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 2462 index++;
3c6a091f 2463
e0a74f01 2464 rcu_read_lock();
fefd409b
DG
2465 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2466 agt, node.node) {
2467 if (agt->being_used) {
2468 (*domains)[index].type = agt->domain;
2469 (*domains)[index].buf_type = session->ust_session->buffer_type;
2470 index++;
2471 }
3c6a091f 2472 }
e0a74f01 2473 rcu_read_unlock();
2f77fc4b 2474 }
fa64dfb4 2475end:
2f77fc4b
DG
2476 return nb_dom;
2477
2478error:
f73fabfd
DG
2479 /* Return negative value to differentiate return code */
2480 return -ret;
2f77fc4b
DG
2481}
2482
2483
2484/*
2485 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2486 */
2487ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2488 struct lttng_channel **channels)
2489{
2490 int ret;
2491 ssize_t nb_chan = 0;
2492
2493 switch (domain) {
2494 case LTTNG_DOMAIN_KERNEL:
2495 if (session->kernel_session != NULL) {
2496 nb_chan = session->kernel_session->channel_count;
2497 }
2498 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2
DG
2499 if (nb_chan <= 0) {
2500 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2501 }
2f77fc4b
DG
2502 break;
2503 case LTTNG_DOMAIN_UST:
2504 if (session->ust_session != NULL) {
7ffc31a2 2505 rcu_read_lock();
2f77fc4b 2506 nb_chan = lttng_ht_get_count(
7ffc31a2
JG
2507 session->ust_session->domain_global.channels);
2508 rcu_read_unlock();
2f77fc4b
DG
2509 }
2510 DBG3("Number of UST global channels %zd", nb_chan);
ade7ce52 2511 if (nb_chan < 0) {
c7d620a2 2512 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
ade7ce52 2513 goto error;
c7d620a2 2514 }
2f77fc4b
DG
2515 break;
2516 default:
f73fabfd 2517 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2518 goto error;
2519 }
2520
2521 if (nb_chan > 0) {
2522 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2523 if (*channels == NULL) {
f73fabfd 2524 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2525 goto error;
2526 }
2527
2528 list_lttng_channels(domain, session, *channels);
2f77fc4b
DG
2529 }
2530
2531 return nb_chan;
2532
2533error:
f73fabfd
DG
2534 /* Return negative value to differentiate return code */
2535 return -ret;
2f77fc4b
DG
2536}
2537
2538/*
2539 * Command LTTNG_LIST_EVENTS processed by the client thread.
2540 */
2541ssize_t cmd_list_events(int domain, struct ltt_session *session,
2542 char *channel_name, struct lttng_event **events)
2543{
2544 int ret = 0;
2545 ssize_t nb_event = 0;
2546
2547 switch (domain) {
2548 case LTTNG_DOMAIN_KERNEL:
2549 if (session->kernel_session != NULL) {
2550 nb_event = list_lttng_kernel_events(channel_name,
2551 session->kernel_session, events);
2552 }
2553 break;
2554 case LTTNG_DOMAIN_UST:
2555 {
2556 if (session->ust_session != NULL) {
2557 nb_event = list_lttng_ust_global_events(channel_name,
2558 &session->ust_session->domain_global, events);
2559 }
2560 break;
2561 }
5cdb6027 2562 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2563 case LTTNG_DOMAIN_JUL:
0e115563 2564 case LTTNG_DOMAIN_PYTHON:
3c6a091f 2565 if (session->ust_session) {
fefd409b
DG
2566 struct lttng_ht_iter iter;
2567 struct agent *agt;
2568
b11feea5 2569 rcu_read_lock();
fefd409b
DG
2570 cds_lfht_for_each_entry(session->ust_session->agents->ht,
2571 &iter.iter, agt, node.node) {
2572 nb_event = list_lttng_agent_events(agt, events);
2573 }
b11feea5 2574 rcu_read_unlock();
3c6a091f
DG
2575 }
2576 break;
2f77fc4b 2577 default:
f73fabfd 2578 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2579 goto error;
2580 }
2581
f73fabfd 2582 return nb_event;
2f77fc4b
DG
2583
2584error:
f73fabfd
DG
2585 /* Return negative value to differentiate return code */
2586 return -ret;
2f77fc4b
DG
2587}
2588
2589/*
2590 * Using the session list, filled a lttng_session array to send back to the
2591 * client for session listing.
2592 *
2593 * The session list lock MUST be acquired before calling this function. Use
2594 * session_lock_list() and session_unlock_list().
2595 */
2596void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2597 gid_t gid)
2598{
2599 int ret;
2600 unsigned int i = 0;
2601 struct ltt_session *session;
2602 struct ltt_session_list *list = session_get_list();
2603
2604 DBG("Getting all available session for UID %d GID %d",
2605 uid, gid);
2606 /*
2607 * Iterate over session list and append data after the control struct in
2608 * the buffer.
2609 */
2610 cds_list_for_each_entry(session, &list->head, list) {
2611 /*
2612 * Only list the sessions the user can control.
2613 */
2614 if (!session_access_ok(session, uid, gid)) {
2615 continue;
2616 }
2617
2618 struct ltt_kernel_session *ksess = session->kernel_session;
2619 struct ltt_ust_session *usess = session->ust_session;
2620
2621 if (session->consumer->type == CONSUMER_DST_NET ||
2622 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2623 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2624 ret = build_network_session_path(sessions[i].path,
dec56f6c 2625 sizeof(sessions[i].path), session);
2f77fc4b 2626 } else {
dec56f6c 2627 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2f77fc4b
DG
2628 session->consumer->dst.trace_path);
2629 }
2630 if (ret < 0) {
2631 PERROR("snprintf session path");
2632 continue;
2633 }
2634
2635 strncpy(sessions[i].name, session->name, NAME_MAX);
2636 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 2637 sessions[i].enabled = session->active;
2cbf8fed 2638 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 2639 sessions[i].live_timer_interval = session->live_timer;
2f77fc4b
DG
2640 i++;
2641 }
2642}
2643
806e2684 2644/*
6d805429 2645 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 2646 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 2647 */
6d805429 2648int cmd_data_pending(struct ltt_session *session)
806e2684
DG
2649{
2650 int ret;
2651 struct ltt_kernel_session *ksess = session->kernel_session;
2652 struct ltt_ust_session *usess = session->ust_session;
2653
2654 assert(session);
2655
2656 /* Session MUST be stopped to ask for data availability. */
8382cf6f 2657 if (session->active) {
806e2684
DG
2658 ret = LTTNG_ERR_SESSION_STARTED;
2659 goto error;
3a89d11a
DG
2660 } else {
2661 /*
2662 * If stopped, just make sure we've started before else the above call
2663 * will always send that there is data pending.
2664 *
2665 * The consumer assumes that when the data pending command is received,
2666 * the trace has been started before or else no output data is written
2667 * by the streams which is a condition for data pending. So, this is
2668 * *VERY* important that we don't ask the consumer before a start
2669 * trace.
2670 */
8382cf6f 2671 if (!session->has_been_started) {
3a89d11a
DG
2672 ret = 0;
2673 goto error;
2674 }
806e2684
DG
2675 }
2676
2677 if (ksess && ksess->consumer) {
6d805429
DG
2678 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2679 if (ret == 1) {
806e2684
DG
2680 /* Data is still being extracted for the kernel. */
2681 goto error;
2682 }
2683 }
2684
2685 if (usess && usess->consumer) {
6d805429
DG
2686 ret = consumer_is_data_pending(usess->id, usess->consumer);
2687 if (ret == 1) {
806e2684
DG
2688 /* Data is still being extracted for the kernel. */
2689 goto error;
2690 }
2691 }
2692
2693 /* Data is ready to be read by a viewer */
6d805429 2694 ret = 0;
806e2684
DG
2695
2696error:
2697 return ret;
2698}
2699
6dc3064a
DG
2700/*
2701 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2702 *
2703 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2704 */
2705int cmd_snapshot_add_output(struct ltt_session *session,
2706 struct lttng_snapshot_output *output, uint32_t *id)
2707{
2708 int ret;
2709 struct snapshot_output *new_output;
2710
2711 assert(session);
2712 assert(output);
2713
2714 DBG("Cmd snapshot add output for session %s", session->name);
2715
2716 /*
d3f14b8a
MD
2717 * Permission denied to create an output if the session is not
2718 * set in no output mode.
6dc3064a
DG
2719 */
2720 if (session->output_traces) {
2721 ret = LTTNG_ERR_EPERM;
2722 goto error;
2723 }
2724
2725 /* Only one output is allowed until we have the "tee" feature. */
2726 if (session->snapshot.nb_output == 1) {
2727 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
2728 goto error;
2729 }
2730
2731 new_output = snapshot_output_alloc();
2732 if (!new_output) {
2733 ret = LTTNG_ERR_NOMEM;
2734 goto error;
2735 }
2736
2737 ret = snapshot_output_init(output->max_size, output->name,
2738 output->ctrl_url, output->data_url, session->consumer, new_output,
2739 &session->snapshot);
2740 if (ret < 0) {
2741 if (ret == -ENOMEM) {
2742 ret = LTTNG_ERR_NOMEM;
2743 } else {
2744 ret = LTTNG_ERR_INVALID;
2745 }
2746 goto free_error;
2747 }
2748
6dc3064a
DG
2749 rcu_read_lock();
2750 snapshot_add_output(&session->snapshot, new_output);
2751 if (id) {
2752 *id = new_output->id;
2753 }
2754 rcu_read_unlock();
2755
2756 return LTTNG_OK;
2757
2758free_error:
2759 snapshot_output_destroy(new_output);
2760error:
2761 return ret;
2762}
2763
2764/*
2765 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2766 *
2767 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2768 */
2769int cmd_snapshot_del_output(struct ltt_session *session,
2770 struct lttng_snapshot_output *output)
2771{
2772 int ret;
eb240553 2773 struct snapshot_output *sout = NULL;
6dc3064a
DG
2774
2775 assert(session);
2776 assert(output);
2777
6dc3064a
DG
2778 rcu_read_lock();
2779
2780 /*
d3f14b8a
MD
2781 * Permission denied to create an output if the session is not
2782 * set in no output mode.
6dc3064a
DG
2783 */
2784 if (session->output_traces) {
2785 ret = LTTNG_ERR_EPERM;
2786 goto error;
2787 }
2788
eb240553
DG
2789 if (output->id) {
2790 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
2791 session->name);
2792 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
2793 } else if (*output->name != '\0') {
2794 DBG("Cmd snapshot del output name %s for session %s", output->name,
2795 session->name);
2796 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
2797 }
6dc3064a
DG
2798 if (!sout) {
2799 ret = LTTNG_ERR_INVALID;
2800 goto error;
2801 }
2802
2803 snapshot_delete_output(&session->snapshot, sout);
2804 snapshot_output_destroy(sout);
2805 ret = LTTNG_OK;
2806
2807error:
2808 rcu_read_unlock();
2809 return ret;
2810}
2811
2812/*
2813 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2814 *
2815 * If no output is available, outputs is untouched and 0 is returned.
2816 *
2817 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2818 */
2819ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
2820 struct lttng_snapshot_output **outputs)
2821{
2822 int ret, idx = 0;
b223ca94 2823 struct lttng_snapshot_output *list = NULL;
6dc3064a
DG
2824 struct lttng_ht_iter iter;
2825 struct snapshot_output *output;
2826
2827 assert(session);
2828 assert(outputs);
2829
2830 DBG("Cmd snapshot list outputs for session %s", session->name);
2831
2832 /*
d3f14b8a
MD
2833 * Permission denied to create an output if the session is not
2834 * set in no output mode.
6dc3064a
DG
2835 */
2836 if (session->output_traces) {
b223ca94 2837 ret = -LTTNG_ERR_EPERM;
6dc3064a
DG
2838 goto error;
2839 }
2840
2841 if (session->snapshot.nb_output == 0) {
2842 ret = 0;
2843 goto error;
2844 }
2845
2846 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
2847 if (!list) {
b223ca94 2848 ret = -LTTNG_ERR_NOMEM;
6dc3064a
DG
2849 goto error;
2850 }
2851
2852 /* Copy list from session to the new list object. */
b223ca94 2853 rcu_read_lock();
6dc3064a
DG
2854 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
2855 output, node.node) {
2856 assert(output->consumer);
2857 list[idx].id = output->id;
2858 list[idx].max_size = output->max_size;
2859 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
2860 if (output->consumer->type == CONSUMER_DST_LOCAL) {
2861 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
2862 sizeof(list[idx].ctrl_url));
2863 } else {
2864 /* Control URI. */
2865 ret = uri_to_str_url(&output->consumer->dst.net.control,
2866 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
2867 if (ret < 0) {
b223ca94
JG
2868 ret = -LTTNG_ERR_NOMEM;
2869 goto error;
6dc3064a
DG
2870 }
2871
2872 /* Data URI. */
2873 ret = uri_to_str_url(&output->consumer->dst.net.data,
2874 list[idx].data_url, sizeof(list[idx].data_url));
2875 if (ret < 0) {
b223ca94
JG
2876 ret = -LTTNG_ERR_NOMEM;
2877 goto error;
6dc3064a
DG
2878 }
2879 }
2880 idx++;
2881 }
2882
2883 *outputs = list;
b223ca94
JG
2884 list = NULL;
2885 ret = session->snapshot.nb_output;
6dc3064a 2886error:
b223ca94
JG
2887 free(list);
2888 rcu_read_unlock();
2889 return ret;
6dc3064a
DG
2890}
2891
2892/*
2893 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2894 * snapshot output is *not* set with a remote destination.
2895 *
a934f4af 2896 * Return 0 on success or a LTTNG_ERR code.
6dc3064a
DG
2897 */
2898static int set_relayd_for_snapshot(struct consumer_output *consumer,
2899 struct snapshot_output *snap_output, struct ltt_session *session)
2900{
a934f4af 2901 int ret = LTTNG_OK;
6dc3064a
DG
2902 struct lttng_ht_iter iter;
2903 struct consumer_socket *socket;
2904
2905 assert(consumer);
2906 assert(snap_output);
2907 assert(session);
2908
2909 DBG2("Set relayd object from snapshot output");
2910
2911 /* Ignore if snapshot consumer output is not network. */
2912 if (snap_output->consumer->type != CONSUMER_DST_NET) {
2913 goto error;
2914 }
2915
2916 /*
2917 * For each consumer socket, create and send the relayd object of the
2918 * snapshot output.
2919 */
2920 rcu_read_lock();
5eecee74
DG
2921 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
2922 socket, node.node) {
6dc3064a 2923 ret = send_consumer_relayd_sockets(0, session->id,
d3e2ba59
JD
2924 snap_output->consumer, socket,
2925 session->name, session->hostname,
2926 session->live_timer);
a934f4af 2927 if (ret != LTTNG_OK) {
6dc3064a
DG
2928 rcu_read_unlock();
2929 goto error;
2930 }
2931 }
2932 rcu_read_unlock();
2933
2934error:
2935 return ret;
2936}
2937
2938/*
2939 * Record a kernel snapshot.
2940 *
fac41e72 2941 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a
DG
2942 */
2943static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
5c786ded 2944 struct snapshot_output *output, struct ltt_session *session,
d07ceecd 2945 int wait, uint64_t nb_packets_per_stream)
6dc3064a
DG
2946{
2947 int ret;
2948
2949 assert(ksess);
2950 assert(output);
2951 assert(session);
2952
10a50311
JD
2953 /* Get the datetime for the snapshot output directory. */
2954 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
2955 sizeof(output->datetime));
2956 if (!ret) {
a934f4af 2957 ret = LTTNG_ERR_INVALID;
10a50311
JD
2958 goto error;
2959 }
2960
af706bb7
DG
2961 /*
2962 * Copy kernel session sockets so we can communicate with the right
2963 * consumer for the snapshot record command.
2964 */
2965 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
2966 if (ret < 0) {
a934f4af 2967 ret = LTTNG_ERR_NOMEM;
af706bb7 2968 goto error;
6dc3064a
DG
2969 }
2970
2971 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
a934f4af 2972 if (ret != LTTNG_OK) {
af706bb7 2973 goto error_snapshot;
6dc3064a
DG
2974 }
2975
d07ceecd 2976 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
2a06df8d 2977 if (ret != LTTNG_OK) {
af706bb7 2978 goto error_snapshot;
6dc3064a
DG
2979 }
2980
a934f4af 2981 ret = LTTNG_OK;
1371fc12 2982 goto end;
a934f4af 2983
af706bb7
DG
2984error_snapshot:
2985 /* Clean up copied sockets so this output can use some other later on. */
2986 consumer_destroy_output_sockets(output->consumer);
6dc3064a 2987error:
1371fc12 2988end:
6dc3064a
DG
2989 return ret;
2990}
2991
2992/*
2993 * Record a UST snapshot.
2994 *
a934f4af 2995 * Return 0 on success or a LTTNG_ERR error code.
6dc3064a
DG
2996 */
2997static int record_ust_snapshot(struct ltt_ust_session *usess,
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(usess);
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 3015 /*
d3f14b8a 3016 * Copy UST session sockets so we can communicate with the right
af706bb7
DG
3017 * consumer for the snapshot record command.
3018 */
3019 ret = consumer_copy_sockets(output->consumer, usess->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(usess->consumer, output, session);
a934f4af 3026 if (ret != LTTNG_OK) {
af706bb7 3027 goto error_snapshot;
6dc3064a
DG
3028 }
3029
d07ceecd 3030 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
6dc3064a 3031 if (ret < 0) {
7badf927
DG
3032 switch (-ret) {
3033 case EINVAL:
a934f4af 3034 ret = LTTNG_ERR_INVALID;
7badf927
DG
3035 break;
3036 case ENODATA:
3037 ret = LTTNG_ERR_SNAPSHOT_NODATA;
3038 break;
3039 default:
3040 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3041 break;
5c786ded 3042 }
af706bb7 3043 goto error_snapshot;
6dc3064a
DG
3044 }
3045
a934f4af
DG
3046 ret = LTTNG_OK;
3047
af706bb7
DG
3048error_snapshot:
3049 /* Clean up copied sockets so this output can use some other later on. */
3050 consumer_destroy_output_sockets(output->consumer);
6dc3064a
DG
3051error:
3052 return ret;
3053}
3054
d07ceecd
MD
3055static
3056uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
3057 uint64_t cur_nr_packets)
68808f4e 3058{
d07ceecd 3059 uint64_t tot_size = 0;
68808f4e
DG
3060
3061 if (session->kernel_session) {
3062 struct ltt_kernel_channel *chan;
3063 struct ltt_kernel_session *ksess = session->kernel_session;
3064
68808f4e 3065 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
d07ceecd
MD
3066 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
3067 /*
3068 * Don't take channel into account if we
3069 * already grab all its packets.
3070 */
3071 continue;
68808f4e 3072 }
d07ceecd
MD
3073 tot_size += chan->channel->attr.subbuf_size
3074 * chan->stream_count;
68808f4e
DG
3075 }
3076 }
3077
3078 if (session->ust_session) {
68808f4e
DG
3079 struct ltt_ust_session *usess = session->ust_session;
3080
d07ceecd
MD
3081 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
3082 cur_nr_packets);
68808f4e
DG
3083 }
3084
d07ceecd 3085 return tot_size;
68808f4e
DG
3086}
3087
5c786ded 3088/*
d07ceecd
MD
3089 * Calculate the number of packets we can grab from each stream that
3090 * fits within the overall snapshot max size.
3091 *
3092 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3093 * the number of packets per stream.
3094 *
3095 * TODO: this approach is not perfect: we consider the worse case
3096 * (packet filling the sub-buffers) as an upper bound, but we could do
3097 * better if we do this calculation while we actually grab the packet
3098 * content: we would know how much padding we don't actually store into
3099 * the file.
3100 *
3101 * This algorithm is currently bounded by the number of packets per
3102 * stream.
3103 *
3104 * Since we call this algorithm before actually grabbing the data, it's
3105 * an approximation: for instance, applications could appear/disappear
3106 * in between this call and actually grabbing data.
5c786ded 3107 */
d07ceecd
MD
3108static
3109int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
5c786ded 3110{
d07ceecd
MD
3111 int64_t size_left;
3112 uint64_t cur_nb_packets = 0;
5c786ded 3113
d07ceecd
MD
3114 if (!max_size) {
3115 return 0; /* Infinite */
5c786ded
JD
3116 }
3117
d07ceecd
MD
3118 size_left = max_size;
3119 for (;;) {
3120 uint64_t one_more_packet_tot_size;
5c786ded 3121
d07ceecd
MD
3122 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3123 cur_nb_packets);
3124 if (!one_more_packet_tot_size) {
3125 /* We are already grabbing all packets. */
3126 break;
3127 }
3128 size_left -= one_more_packet_tot_size;
3129 if (size_left < 0) {
3130 break;
3131 }
3132 cur_nb_packets++;
5c786ded 3133 }
d07ceecd
MD
3134 if (!cur_nb_packets) {
3135 /* Not enough room to grab one packet of each stream, error. */
3136 return -1;
3137 }
3138 return cur_nb_packets;
5c786ded
JD
3139}
3140
6dc3064a
DG
3141/*
3142 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3143 *
3144 * The wait parameter is ignored so this call always wait for the snapshot to
3145 * complete before returning.
3146 *
3147 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3148 */
3149int cmd_snapshot_record(struct ltt_session *session,
3150 struct lttng_snapshot_output *output, int wait)
3151{
3152 int ret = LTTNG_OK;
e1986656
DG
3153 unsigned int use_tmp_output = 0;
3154 struct snapshot_output tmp_output;
00e1dfc4 3155 unsigned int snapshot_success = 0;
6dc3064a
DG
3156
3157 assert(session);
ba45d9f0 3158 assert(output);
6dc3064a
DG
3159
3160 DBG("Cmd snapshot record for session %s", session->name);
3161
3162 /*
d3f14b8a
MD
3163 * Permission denied to create an output if the session is not
3164 * set in no output mode.
6dc3064a
DG
3165 */
3166 if (session->output_traces) {
3167 ret = LTTNG_ERR_EPERM;
3168 goto error;
3169 }
3170
3171 /* The session needs to be started at least once. */
8382cf6f 3172 if (!session->has_been_started) {
6dc3064a
DG
3173 ret = LTTNG_ERR_START_SESSION_ONCE;
3174 goto error;
3175 }
3176
3177 /* Use temporary output for the session. */
ba45d9f0 3178 if (*output->ctrl_url != '\0') {
6dc3064a
DG
3179 ret = snapshot_output_init(output->max_size, output->name,
3180 output->ctrl_url, output->data_url, session->consumer,
e1986656 3181 &tmp_output, NULL);
6dc3064a
DG
3182 if (ret < 0) {
3183 if (ret == -ENOMEM) {
3184 ret = LTTNG_ERR_NOMEM;
3185 } else {
3186 ret = LTTNG_ERR_INVALID;
3187 }
3188 goto error;
3189 }
1bfe7328
DG
3190 /* Use the global session count for the temporary snapshot. */
3191 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
e1986656 3192 use_tmp_output = 1;
6dc3064a
DG
3193 }
3194
3195 if (session->kernel_session) {
3196 struct ltt_kernel_session *ksess = session->kernel_session;
3197
e1986656 3198 if (use_tmp_output) {
d07ceecd
MD
3199 int64_t nb_packets_per_stream;
3200
3201 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3202 tmp_output.max_size);
3203 if (nb_packets_per_stream < 0) {
3204 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3205 goto error;
3206 }
e1986656 3207 ret = record_kernel_snapshot(ksess, &tmp_output, session,
d07ceecd 3208 wait, nb_packets_per_stream);
a934f4af 3209 if (ret != LTTNG_OK) {
6dc3064a
DG
3210 goto error;
3211 }
1bfe7328 3212 snapshot_success = 1;
6dc3064a
DG
3213 } else {
3214 struct snapshot_output *sout;
3215 struct lttng_ht_iter iter;
3216
3217 rcu_read_lock();
3218 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3219 &iter.iter, sout, node.node) {
d07ceecd
MD
3220 int64_t nb_packets_per_stream;
3221
e1986656
DG
3222 /*
3223 * Make a local copy of the output and assign the possible
3224 * temporary value given by the caller.
3225 */
3226 memset(&tmp_output, 0, sizeof(tmp_output));
3227 memcpy(&tmp_output, sout, sizeof(tmp_output));
3228
e1986656
DG
3229 if (output->max_size != (uint64_t) -1ULL) {
3230 tmp_output.max_size = output->max_size;
3231 }
3232
d07ceecd
MD
3233 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3234 tmp_output.max_size);
3235 if (nb_packets_per_stream < 0) {
68808f4e
DG
3236 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3237 goto error;
3238 }
3239
e1986656
DG
3240 /* Use temporary name. */
3241 if (*output->name != '\0') {
3242 strncpy(tmp_output.name, output->name,
3243 sizeof(tmp_output.name));
3244 }
3245
1bfe7328
DG
3246 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3247
e1986656 3248 ret = record_kernel_snapshot(ksess, &tmp_output,
d07ceecd 3249 session, wait, nb_packets_per_stream);
a934f4af 3250 if (ret != LTTNG_OK) {
6dc3064a
DG
3251 rcu_read_unlock();
3252 goto error;
3253 }
1bfe7328 3254 snapshot_success = 1;
6dc3064a
DG
3255 }
3256 rcu_read_unlock();
3257 }
3258 }
3259
3260 if (session->ust_session) {
3261 struct ltt_ust_session *usess = session->ust_session;
3262
e1986656 3263 if (use_tmp_output) {
d07ceecd
MD
3264 int64_t nb_packets_per_stream;
3265
3266 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3267 tmp_output.max_size);
3268 if (nb_packets_per_stream < 0) {
3269 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3270 goto error;
3271 }
e1986656 3272 ret = record_ust_snapshot(usess, &tmp_output, session,
d07ceecd 3273 wait, nb_packets_per_stream);
a934f4af 3274 if (ret != LTTNG_OK) {
6dc3064a
DG
3275 goto error;
3276 }
1bfe7328 3277 snapshot_success = 1;
6dc3064a
DG
3278 } else {
3279 struct snapshot_output *sout;
3280 struct lttng_ht_iter iter;
3281
3282 rcu_read_lock();
3283 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3284 &iter.iter, sout, node.node) {
d07ceecd
MD
3285 int64_t nb_packets_per_stream;
3286
e1986656
DG
3287 /*
3288 * Make a local copy of the output and assign the possible
3289 * temporary value given by the caller.
3290 */
3291 memset(&tmp_output, 0, sizeof(tmp_output));
3292 memcpy(&tmp_output, sout, sizeof(tmp_output));
3293
e1986656
DG
3294 if (output->max_size != (uint64_t) -1ULL) {
3295 tmp_output.max_size = output->max_size;
3296 }
3297
d07ceecd
MD
3298 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3299 tmp_output.max_size);
3300 if (nb_packets_per_stream < 0) {
68808f4e 3301 ret = LTTNG_ERR_MAX_SIZE_INVALID;
d07ceecd 3302 rcu_read_unlock();
68808f4e
DG
3303 goto error;
3304 }
3305
e1986656
DG
3306 /* Use temporary name. */
3307 if (*output->name != '\0') {
3308 strncpy(tmp_output.name, output->name,
3309 sizeof(tmp_output.name));
3310 }
3311
1bfe7328
DG
3312 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3313
e1986656 3314 ret = record_ust_snapshot(usess, &tmp_output, session,
d07ceecd 3315 wait, nb_packets_per_stream);
a934f4af 3316 if (ret != LTTNG_OK) {
6dc3064a
DG
3317 rcu_read_unlock();
3318 goto error;
3319 }
1bfe7328 3320 snapshot_success = 1;
6dc3064a
DG
3321 }
3322 rcu_read_unlock();
3323 }
3324 }
3325
1bfe7328
DG
3326 if (snapshot_success) {
3327 session->snapshot.nb_snapshot++;
b67578cb
MD
3328 } else {
3329 ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
3330 }
3331
6dc3064a 3332error:
6dc3064a
DG
3333 return ret;
3334}
3335
d7ba1388
MD
3336/*
3337 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3338 */
3339int cmd_set_session_shm_path(struct ltt_session *session,
3340 const char *shm_path)
3341{
3342 /* Safety net */
3343 assert(session);
3344
3345 /*
3346 * Can only set shm path before session is started.
3347 */
3348 if (session->has_been_started) {
3349 return LTTNG_ERR_SESSION_STARTED;
3350 }
3351
3352 strncpy(session->shm_path, shm_path,
3353 sizeof(session->shm_path));
3354 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
3355
3356 return 0;
3357}
3358
2f77fc4b
DG
3359/*
3360 * Init command subsystem.
3361 */
3362void cmd_init(void)
3363{
3364 /*
d88aee68
DG
3365 * Set network sequence index to 1 for streams to match a relayd
3366 * socket on the consumer side.
2f77fc4b 3367 */
d88aee68
DG
3368 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3369 relayd_net_seq_idx = 1;
3370 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
3371
3372 DBG("Command subsystem initialized");
3373}
This page took 0.218504 seconds and 5 git commands to generate.