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