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