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