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