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