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