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