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