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