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