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