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