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