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