data pending returns true when rotate is pending
[deliverable/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 <common/string-utils/string-utils.h>
40
41 #include "channel.h"
42 #include "consumer.h"
43 #include "event.h"
44 #include "health-sessiond.h"
45 #include "kernel.h"
46 #include "kernel-consumer.h"
47 #include "lttng-sessiond.h"
48 #include "utils.h"
49 #include "syscall.h"
50 #include "agent.h"
51 #include "buffer-registry.h"
52 #include "notification-thread.h"
53 #include "notification-thread-commands.h"
54 #include "rotate.h"
55 #include "rotation-thread.h"
56 #include "sessiond-timer.h"
57
58 #include "cmd.h"
59
60 /*
61 * Used to keep a unique index for each relayd socket created where this value
62 * is associated with streams on the consumer so it can match the right relayd
63 * to send to. It must be accessed with the relayd_net_seq_idx_lock
64 * held.
65 */
66 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
67 static uint64_t relayd_net_seq_idx;
68
69 static int validate_ust_event_name(const char *);
70 static int cmd_enable_event_internal(struct ltt_session *session,
71 struct lttng_domain *domain,
72 char *channel_name, struct lttng_event *event,
73 char *filter_expression,
74 struct lttng_filter_bytecode *filter,
75 struct lttng_event_exclusion *exclusion,
76 int wpipe);
77
78 /*
79 * Create a session path used by list_lttng_sessions for the case that the
80 * session consumer is on the network.
81 */
82 static int build_network_session_path(char *dst, size_t size,
83 struct ltt_session *session)
84 {
85 int ret, kdata_port, udata_port;
86 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
87 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
88
89 assert(session);
90 assert(dst);
91
92 memset(tmp_urls, 0, sizeof(tmp_urls));
93 memset(tmp_uurl, 0, sizeof(tmp_uurl));
94
95 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
96
97 if (session->kernel_session && session->kernel_session->consumer) {
98 kuri = &session->kernel_session->consumer->dst.net.control;
99 kdata_port = session->kernel_session->consumer->dst.net.data.port;
100 }
101
102 if (session->ust_session && session->ust_session->consumer) {
103 uuri = &session->ust_session->consumer->dst.net.control;
104 udata_port = session->ust_session->consumer->dst.net.data.port;
105 }
106
107 if (uuri == NULL && kuri == NULL) {
108 uri = &session->consumer->dst.net.control;
109 kdata_port = session->consumer->dst.net.data.port;
110 } else if (kuri && uuri) {
111 ret = uri_compare(kuri, uuri);
112 if (ret) {
113 /* Not Equal */
114 uri = kuri;
115 /* Build uuri URL string */
116 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
117 if (ret < 0) {
118 goto error;
119 }
120 } else {
121 uri = kuri;
122 }
123 } else if (kuri && uuri == NULL) {
124 uri = kuri;
125 } else if (uuri && kuri == NULL) {
126 uri = uuri;
127 }
128
129 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
130 if (ret < 0) {
131 goto error;
132 }
133
134 /*
135 * Do we have a UST url set. If yes, this means we have both kernel and UST
136 * to print.
137 */
138 if (*tmp_uurl != '\0') {
139 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
140 tmp_urls, kdata_port, tmp_uurl, udata_port);
141 } else {
142 int dport;
143 if (kuri || (!kuri && !uuri)) {
144 dport = kdata_port;
145 } else {
146 /* No kernel URI, use the UST port. */
147 dport = udata_port;
148 }
149 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
150 }
151
152 error:
153 return ret;
154 }
155
156 /*
157 * Get run-time attributes if the session has been started (discarded events,
158 * lost packets).
159 */
160 static int get_kernel_runtime_stats(struct ltt_session *session,
161 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
162 uint64_t *lost_packets)
163 {
164 int ret;
165
166 if (!session->has_been_started) {
167 ret = 0;
168 *discarded_events = 0;
169 *lost_packets = 0;
170 goto end;
171 }
172
173 ret = consumer_get_discarded_events(session->id, kchan->fd,
174 session->kernel_session->consumer,
175 discarded_events);
176 if (ret < 0) {
177 goto end;
178 }
179
180 ret = consumer_get_lost_packets(session->id, kchan->fd,
181 session->kernel_session->consumer,
182 lost_packets);
183 if (ret < 0) {
184 goto end;
185 }
186
187 end:
188 return ret;
189 }
190
191 /*
192 * Get run-time attributes if the session has been started (discarded events,
193 * lost packets).
194 */
195 static int get_ust_runtime_stats(struct ltt_session *session,
196 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
197 uint64_t *lost_packets)
198 {
199 int ret;
200 struct ltt_ust_session *usess;
201
202 if (!discarded_events || !lost_packets) {
203 ret = -1;
204 goto end;
205 }
206
207 usess = session->ust_session;
208 assert(discarded_events);
209 assert(lost_packets);
210
211 if (!usess || !session->has_been_started) {
212 *discarded_events = 0;
213 *lost_packets = 0;
214 ret = 0;
215 goto end;
216 }
217
218 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
219 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
220 &usess->buffer_reg_uid_list,
221 usess->consumer, uchan->id,
222 uchan->attr.overwrite,
223 discarded_events,
224 lost_packets);
225 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
226 ret = ust_app_pid_get_channel_runtime_stats(usess,
227 uchan, usess->consumer,
228 uchan->attr.overwrite,
229 discarded_events,
230 lost_packets);
231 if (ret < 0) {
232 goto end;
233 }
234 *discarded_events += uchan->per_pid_closed_app_discarded;
235 *lost_packets += uchan->per_pid_closed_app_lost;
236 } else {
237 ERR("Unsupported buffer type");
238 assert(0);
239 ret = -1;
240 goto end;
241 }
242
243 end:
244 return ret;
245 }
246
247 /*
248 * Fill lttng_channel array of all channels.
249 */
250 static ssize_t list_lttng_channels(enum lttng_domain_type domain,
251 struct ltt_session *session, struct lttng_channel *channels,
252 struct lttng_channel_extended *chan_exts)
253 {
254 int i = 0, ret = 0;
255 struct ltt_kernel_channel *kchan;
256
257 DBG("Listing channels for session %s", session->name);
258
259 switch (domain) {
260 case LTTNG_DOMAIN_KERNEL:
261 /* Kernel channels */
262 if (session->kernel_session != NULL) {
263 cds_list_for_each_entry(kchan,
264 &session->kernel_session->channel_list.head, list) {
265 uint64_t discarded_events, lost_packets;
266 struct lttng_channel_extended *extended;
267
268 extended = (struct lttng_channel_extended *)
269 kchan->channel->attr.extended.ptr;
270
271 ret = get_kernel_runtime_stats(session, kchan,
272 &discarded_events, &lost_packets);
273 if (ret < 0) {
274 goto end;
275 }
276 /* Copy lttng_channel struct to array */
277 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
278 channels[i].enabled = kchan->enabled;
279 chan_exts[i].discarded_events =
280 discarded_events;
281 chan_exts[i].lost_packets = lost_packets;
282 chan_exts[i].monitor_timer_interval =
283 extended->monitor_timer_interval;
284 chan_exts[i].blocking_timeout = 0;
285 i++;
286 }
287 }
288 break;
289 case LTTNG_DOMAIN_UST:
290 {
291 struct lttng_ht_iter iter;
292 struct ltt_ust_channel *uchan;
293
294 rcu_read_lock();
295 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
296 &iter.iter, uchan, node.node) {
297 uint64_t discarded_events = 0, lost_packets = 0;
298
299 if (lttng_strncpy(channels[i].name, uchan->name,
300 LTTNG_SYMBOL_NAME_LEN)) {
301 break;
302 }
303 channels[i].attr.overwrite = uchan->attr.overwrite;
304 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
305 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
306 channels[i].attr.switch_timer_interval =
307 uchan->attr.switch_timer_interval;
308 channels[i].attr.read_timer_interval =
309 uchan->attr.read_timer_interval;
310 channels[i].enabled = uchan->enabled;
311 channels[i].attr.tracefile_size = uchan->tracefile_size;
312 channels[i].attr.tracefile_count = uchan->tracefile_count;
313
314 /*
315 * Map enum lttng_ust_output to enum lttng_event_output.
316 */
317 switch (uchan->attr.output) {
318 case LTTNG_UST_MMAP:
319 channels[i].attr.output = LTTNG_EVENT_MMAP;
320 break;
321 default:
322 /*
323 * LTTNG_UST_MMAP is the only supported UST
324 * output mode.
325 */
326 assert(0);
327 break;
328 }
329
330 chan_exts[i].monitor_timer_interval =
331 uchan->monitor_timer_interval;
332 chan_exts[i].blocking_timeout =
333 uchan->attr.u.s.blocking_timeout;
334
335 ret = get_ust_runtime_stats(session, uchan,
336 &discarded_events, &lost_packets);
337 if (ret < 0) {
338 break;
339 }
340 chan_exts[i].discarded_events = discarded_events;
341 chan_exts[i].lost_packets = lost_packets;
342 i++;
343 }
344 rcu_read_unlock();
345 break;
346 }
347 default:
348 break;
349 }
350
351 end:
352 if (ret < 0) {
353 return -LTTNG_ERR_FATAL;
354 } else {
355 return LTTNG_OK;
356 }
357 }
358
359 static void increment_extended_len(const char *filter_expression,
360 struct lttng_event_exclusion *exclusion, size_t *extended_len)
361 {
362 *extended_len += sizeof(struct lttcomm_event_extended_header);
363
364 if (filter_expression) {
365 *extended_len += strlen(filter_expression) + 1;
366 }
367
368 if (exclusion) {
369 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
370 }
371 }
372
373 static void append_extended_info(const char *filter_expression,
374 struct lttng_event_exclusion *exclusion, void **extended_at)
375 {
376 struct lttcomm_event_extended_header extended_header;
377 size_t filter_len = 0;
378 size_t nb_exclusions = 0;
379
380 if (filter_expression) {
381 filter_len = strlen(filter_expression) + 1;
382 }
383
384 if (exclusion) {
385 nb_exclusions = exclusion->count;
386 }
387
388 /* Set header fields */
389 extended_header.filter_len = filter_len;
390 extended_header.nb_exclusions = nb_exclusions;
391
392 /* Copy header */
393 memcpy(*extended_at, &extended_header, sizeof(extended_header));
394 *extended_at += sizeof(extended_header);
395
396 /* Copy filter string */
397 if (filter_expression) {
398 memcpy(*extended_at, filter_expression, filter_len);
399 *extended_at += filter_len;
400 }
401
402 /* Copy exclusion names */
403 if (exclusion) {
404 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
405
406 memcpy(*extended_at, &exclusion->names, len);
407 *extended_at += len;
408 }
409 }
410
411 /*
412 * Create a list of agent domain events.
413 *
414 * Return number of events in list on success or else a negative value.
415 */
416 static int list_lttng_agent_events(struct agent *agt,
417 struct lttng_event **events, size_t *total_size)
418 {
419 int i = 0, ret = 0;
420 unsigned int nb_event = 0;
421 struct agent_event *event;
422 struct lttng_event *tmp_events;
423 struct lttng_ht_iter iter;
424 size_t extended_len = 0;
425 void *extended_at;
426
427 assert(agt);
428 assert(events);
429
430 DBG3("Listing agent events");
431
432 rcu_read_lock();
433 nb_event = lttng_ht_get_count(agt->events);
434 rcu_read_unlock();
435 if (nb_event == 0) {
436 ret = nb_event;
437 *total_size = 0;
438 goto error;
439 }
440
441 /* Compute required extended infos size */
442 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
443
444 /*
445 * This is only valid because the commands which add events are
446 * processed in the same thread as the listing.
447 */
448 rcu_read_lock();
449 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
450 increment_extended_len(event->filter_expression, NULL,
451 &extended_len);
452 }
453 rcu_read_unlock();
454
455 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
456 tmp_events = zmalloc(*total_size);
457 if (!tmp_events) {
458 PERROR("zmalloc agent events session");
459 ret = -LTTNG_ERR_FATAL;
460 goto error;
461 }
462
463 extended_at = ((uint8_t *) tmp_events) +
464 nb_event * sizeof(struct lttng_event);
465
466 rcu_read_lock();
467 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
468 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
469 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
470 tmp_events[i].enabled = event->enabled;
471 tmp_events[i].loglevel = event->loglevel_value;
472 tmp_events[i].loglevel_type = event->loglevel_type;
473 i++;
474
475 /* Append extended info */
476 append_extended_info(event->filter_expression, NULL,
477 &extended_at);
478 }
479 rcu_read_unlock();
480
481 *events = tmp_events;
482 ret = nb_event;
483
484 error:
485 assert(nb_event == i);
486 return ret;
487 }
488
489 /*
490 * Create a list of ust global domain events.
491 */
492 static int list_lttng_ust_global_events(char *channel_name,
493 struct ltt_ust_domain_global *ust_global,
494 struct lttng_event **events, size_t *total_size)
495 {
496 int i = 0, ret = 0;
497 unsigned int nb_event = 0;
498 struct lttng_ht_iter iter;
499 struct lttng_ht_node_str *node;
500 struct ltt_ust_channel *uchan;
501 struct ltt_ust_event *uevent;
502 struct lttng_event *tmp;
503 size_t extended_len = 0;
504 void *extended_at;
505
506 DBG("Listing UST global events for channel %s", channel_name);
507
508 rcu_read_lock();
509
510 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
511 node = lttng_ht_iter_get_node_str(&iter);
512 if (node == NULL) {
513 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
514 goto end;
515 }
516
517 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
518
519 nb_event = lttng_ht_get_count(uchan->events);
520 if (nb_event == 0) {
521 ret = nb_event;
522 *total_size = 0;
523 goto end;
524 }
525
526 DBG3("Listing UST global %d events", nb_event);
527
528 /* Compute required extended infos size */
529 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
530 if (uevent->internal) {
531 nb_event--;
532 continue;
533 }
534
535 increment_extended_len(uevent->filter_expression,
536 uevent->exclusion, &extended_len);
537 }
538 if (nb_event == 0) {
539 /* All events are internal, skip. */
540 ret = 0;
541 *total_size = 0;
542 goto end;
543 }
544
545 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
546 tmp = zmalloc(*total_size);
547 if (tmp == NULL) {
548 ret = -LTTNG_ERR_FATAL;
549 goto end;
550 }
551
552 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
553
554 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
555 if (uevent->internal) {
556 /* This event should remain hidden from clients */
557 continue;
558 }
559 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
560 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
561 tmp[i].enabled = uevent->enabled;
562
563 switch (uevent->attr.instrumentation) {
564 case LTTNG_UST_TRACEPOINT:
565 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
566 break;
567 case LTTNG_UST_PROBE:
568 tmp[i].type = LTTNG_EVENT_PROBE;
569 break;
570 case LTTNG_UST_FUNCTION:
571 tmp[i].type = LTTNG_EVENT_FUNCTION;
572 break;
573 }
574
575 tmp[i].loglevel = uevent->attr.loglevel;
576 switch (uevent->attr.loglevel_type) {
577 case LTTNG_UST_LOGLEVEL_ALL:
578 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
579 break;
580 case LTTNG_UST_LOGLEVEL_RANGE:
581 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
582 break;
583 case LTTNG_UST_LOGLEVEL_SINGLE:
584 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
585 break;
586 }
587 if (uevent->filter) {
588 tmp[i].filter = 1;
589 }
590 if (uevent->exclusion) {
591 tmp[i].exclusion = 1;
592 }
593 i++;
594
595 /* Append extended info */
596 append_extended_info(uevent->filter_expression,
597 uevent->exclusion, &extended_at);
598 }
599
600 ret = nb_event;
601 *events = tmp;
602 end:
603 rcu_read_unlock();
604 return ret;
605 }
606
607 /*
608 * Fill lttng_event array of all kernel events in the channel.
609 */
610 static int list_lttng_kernel_events(char *channel_name,
611 struct ltt_kernel_session *kernel_session,
612 struct lttng_event **events, size_t *total_size)
613 {
614 int i = 0, ret;
615 unsigned int nb_event;
616 struct ltt_kernel_event *event;
617 struct ltt_kernel_channel *kchan;
618 size_t extended_len = 0;
619 void *extended_at;
620
621 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
622 if (kchan == NULL) {
623 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
624 goto error;
625 }
626
627 nb_event = kchan->event_count;
628
629 DBG("Listing events for channel %s", kchan->channel->name);
630
631 if (nb_event == 0) {
632 *total_size = 0;
633 *events = NULL;
634 goto end;
635 }
636
637 /* Compute required extended infos size */
638 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
639 increment_extended_len(event->filter_expression, NULL,
640 &extended_len);
641 }
642
643 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
644 *events = zmalloc(*total_size);
645 if (*events == NULL) {
646 ret = LTTNG_ERR_FATAL;
647 goto error;
648 }
649
650 extended_at = ((void *) *events) +
651 nb_event * sizeof(struct lttng_event);
652
653 /* Kernel channels */
654 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
655 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
656 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
657 (*events)[i].enabled = event->enabled;
658 (*events)[i].filter =
659 (unsigned char) !!event->filter_expression;
660
661 switch (event->event->instrumentation) {
662 case LTTNG_KERNEL_TRACEPOINT:
663 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
664 break;
665 case LTTNG_KERNEL_KRETPROBE:
666 (*events)[i].type = LTTNG_EVENT_FUNCTION;
667 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
668 sizeof(struct lttng_kernel_kprobe));
669 break;
670 case LTTNG_KERNEL_KPROBE:
671 (*events)[i].type = LTTNG_EVENT_PROBE;
672 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
673 sizeof(struct lttng_kernel_kprobe));
674 break;
675 case LTTNG_KERNEL_FUNCTION:
676 (*events)[i].type = LTTNG_EVENT_FUNCTION;
677 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
678 sizeof(struct lttng_kernel_function));
679 break;
680 case LTTNG_KERNEL_NOOP:
681 (*events)[i].type = LTTNG_EVENT_NOOP;
682 break;
683 case LTTNG_KERNEL_SYSCALL:
684 (*events)[i].type = LTTNG_EVENT_SYSCALL;
685 break;
686 case LTTNG_KERNEL_ALL:
687 assert(0);
688 break;
689 }
690 i++;
691
692 /* Append extended info */
693 append_extended_info(event->filter_expression, NULL,
694 &extended_at);
695 }
696
697 end:
698 return nb_event;
699
700 error:
701 /* Negate the error code to differentiate the size from an error */
702 return -ret;
703 }
704
705 /*
706 * Add URI so the consumer output object. Set the correct path depending on the
707 * domain adding the default trace directory.
708 */
709 static int add_uri_to_consumer(struct consumer_output *consumer,
710 struct lttng_uri *uri, enum lttng_domain_type domain,
711 const char *session_name)
712 {
713 int ret = LTTNG_OK;
714 const char *default_trace_dir;
715
716 assert(uri);
717
718 if (consumer == NULL) {
719 DBG("No consumer detected. Don't add URI. Stopping.");
720 ret = LTTNG_ERR_NO_CONSUMER;
721 goto error;
722 }
723
724 switch (domain) {
725 case LTTNG_DOMAIN_KERNEL:
726 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
727 break;
728 case LTTNG_DOMAIN_UST:
729 default_trace_dir = DEFAULT_UST_TRACE_DIR;
730 break;
731 default:
732 /*
733 * This case is possible is we try to add the URI to the global tracing
734 * session consumer object which in this case there is no subdir.
735 */
736 default_trace_dir = "";
737 }
738
739 switch (uri->dtype) {
740 case LTTNG_DST_IPV4:
741 case LTTNG_DST_IPV6:
742 DBG2("Setting network URI to consumer");
743
744 if (consumer->type == CONSUMER_DST_NET) {
745 if ((uri->stype == LTTNG_STREAM_CONTROL &&
746 consumer->dst.net.control_isset) ||
747 (uri->stype == LTTNG_STREAM_DATA &&
748 consumer->dst.net.data_isset)) {
749 ret = LTTNG_ERR_URL_EXIST;
750 goto error;
751 }
752 } else {
753 memset(&consumer->dst.net, 0, sizeof(consumer->dst.net));
754 }
755
756 consumer->type = CONSUMER_DST_NET;
757
758 /* Set URI into consumer output object */
759 ret = consumer_set_network_uri(consumer, uri);
760 if (ret < 0) {
761 ret = -ret;
762 goto error;
763 } else if (ret == 1) {
764 /*
765 * URI was the same in the consumer so we do not append the subdir
766 * again so to not duplicate output dir.
767 */
768 ret = LTTNG_OK;
769 goto error;
770 }
771
772 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
773 ret = consumer_set_subdir(consumer, session_name);
774 if (ret < 0) {
775 ret = LTTNG_ERR_FATAL;
776 goto error;
777 }
778 }
779
780 if (uri->stype == LTTNG_STREAM_CONTROL) {
781 /* On a new subdir, reappend the default trace dir. */
782 strncat(consumer->subdir, default_trace_dir,
783 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
784 DBG3("Append domain trace name to subdir %s", consumer->subdir);
785 }
786
787 break;
788 case LTTNG_DST_PATH:
789 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
790 memset(consumer->dst.session_root_path, 0,
791 sizeof(consumer->dst.session_root_path));
792 /* Explicit length checks for strcpy and strcat. */
793 if (strlen(uri->dst.path) + strlen(default_trace_dir)
794 >= sizeof(consumer->dst.session_root_path)) {
795 ret = LTTNG_ERR_FATAL;
796 goto error;
797 }
798 strcpy(consumer->dst.session_root_path, uri->dst.path);
799 /* Append default trace dir */
800 strcat(consumer->dst.session_root_path, default_trace_dir);
801 /* Flag consumer as local. */
802 consumer->type = CONSUMER_DST_LOCAL;
803 break;
804 }
805
806 ret = LTTNG_OK;
807
808 error:
809 return ret;
810 }
811
812 /*
813 * Init tracing by creating trace directory and sending fds kernel consumer.
814 */
815 static int init_kernel_tracing(struct ltt_kernel_session *session)
816 {
817 int ret = 0;
818 struct lttng_ht_iter iter;
819 struct consumer_socket *socket;
820
821 assert(session);
822
823 rcu_read_lock();
824
825 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
826 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
827 socket, node.node) {
828 pthread_mutex_lock(socket->lock);
829 ret = kernel_consumer_send_session(socket, session);
830 pthread_mutex_unlock(socket->lock);
831 if (ret < 0) {
832 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
833 goto error;
834 }
835 }
836 }
837
838 error:
839 rcu_read_unlock();
840 return ret;
841 }
842
843 /*
844 * Create a socket to the relayd using the URI.
845 *
846 * On success, the relayd_sock pointer is set to the created socket.
847 * Else, it's stays untouched and a lttcomm error code is returned.
848 */
849 static int create_connect_relayd(struct lttng_uri *uri,
850 struct lttcomm_relayd_sock **relayd_sock,
851 struct consumer_output *consumer)
852 {
853 int ret;
854 struct lttcomm_relayd_sock *rsock;
855
856 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
857 RELAYD_VERSION_COMM_MINOR);
858 if (!rsock) {
859 ret = LTTNG_ERR_FATAL;
860 goto error;
861 }
862
863 /*
864 * Connect to relayd so we can proceed with a session creation. This call
865 * can possibly block for an arbitrary amount of time to set the health
866 * state to be in poll execution.
867 */
868 health_poll_entry();
869 ret = relayd_connect(rsock);
870 health_poll_exit();
871 if (ret < 0) {
872 ERR("Unable to reach lttng-relayd");
873 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
874 goto free_sock;
875 }
876
877 /* Create socket for control stream. */
878 if (uri->stype == LTTNG_STREAM_CONTROL) {
879 DBG3("Creating relayd stream socket from URI");
880
881 /* Check relayd version */
882 ret = relayd_version_check(rsock);
883 if (ret < 0) {
884 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
885 goto close_sock;
886 }
887 consumer->relay_major_version = rsock->major;
888 consumer->relay_minor_version = rsock->minor;
889 } else if (uri->stype == LTTNG_STREAM_DATA) {
890 DBG3("Creating relayd data socket from URI");
891 } else {
892 /* Command is not valid */
893 ERR("Relayd invalid stream type: %d", uri->stype);
894 ret = LTTNG_ERR_INVALID;
895 goto close_sock;
896 }
897
898 *relayd_sock = rsock;
899
900 return LTTNG_OK;
901
902 close_sock:
903 /* The returned value is not useful since we are on an error path. */
904 (void) relayd_close(rsock);
905 free_sock:
906 free(rsock);
907 error:
908 return ret;
909 }
910
911 /*
912 * Connect to the relayd using URI and send the socket to the right consumer.
913 */
914 static int send_consumer_relayd_socket(enum lttng_domain_type domain,
915 unsigned int session_id, struct lttng_uri *relayd_uri,
916 struct consumer_output *consumer,
917 struct consumer_socket *consumer_sock,
918 char *session_name, char *hostname, int session_live_timer)
919 {
920 int ret;
921 struct lttcomm_relayd_sock *rsock = NULL;
922
923 /* Connect to relayd and make version check if uri is the control. */
924 ret = create_connect_relayd(relayd_uri, &rsock, consumer);
925 if (ret != LTTNG_OK) {
926 goto error;
927 }
928 assert(rsock);
929
930 /* Set the network sequence index if not set. */
931 if (consumer->net_seq_index == (uint64_t) -1ULL) {
932 pthread_mutex_lock(&relayd_net_seq_idx_lock);
933 /*
934 * Increment net_seq_idx because we are about to transfer the
935 * new relayd socket to the consumer.
936 * Assign unique key so the consumer can match streams.
937 */
938 consumer->net_seq_index = ++relayd_net_seq_idx;
939 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
940 }
941
942 /* Send relayd socket to consumer. */
943 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
944 relayd_uri->stype, session_id,
945 session_name, hostname, session_live_timer);
946 if (ret < 0) {
947 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
948 goto close_sock;
949 }
950
951 /* Flag that the corresponding socket was sent. */
952 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
953 consumer_sock->control_sock_sent = 1;
954 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
955 consumer_sock->data_sock_sent = 1;
956 }
957
958 ret = LTTNG_OK;
959
960 /*
961 * Close socket which was dup on the consumer side. The session daemon does
962 * NOT keep track of the relayd socket(s) once transfer to the consumer.
963 */
964
965 close_sock:
966 (void) relayd_close(rsock);
967 free(rsock);
968
969 error:
970 if (ret != LTTNG_OK) {
971 /*
972 * The consumer output for this session should not be used anymore
973 * since the relayd connection failed thus making any tracing or/and
974 * streaming not usable.
975 */
976 consumer->enabled = 0;
977 }
978 return ret;
979 }
980
981 /*
982 * Send both relayd sockets to a specific consumer and domain. This is a
983 * helper function to facilitate sending the information to the consumer for a
984 * session.
985 */
986 static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
987 unsigned int session_id, struct consumer_output *consumer,
988 struct consumer_socket *sock, char *session_name,
989 char *hostname, int session_live_timer)
990 {
991 int ret = LTTNG_OK;
992
993 assert(consumer);
994 assert(sock);
995
996 /* Sending control relayd socket. */
997 if (!sock->control_sock_sent) {
998 ret = send_consumer_relayd_socket(domain, session_id,
999 &consumer->dst.net.control, consumer, sock,
1000 session_name, hostname, session_live_timer);
1001 if (ret != LTTNG_OK) {
1002 goto error;
1003 }
1004 }
1005
1006 /* Sending data relayd socket. */
1007 if (!sock->data_sock_sent) {
1008 ret = send_consumer_relayd_socket(domain, session_id,
1009 &consumer->dst.net.data, consumer, sock,
1010 session_name, hostname, session_live_timer);
1011 if (ret != LTTNG_OK) {
1012 goto error;
1013 }
1014 }
1015
1016 error:
1017 return ret;
1018 }
1019
1020 /*
1021 * Setup relayd connections for a tracing session. First creates the socket to
1022 * the relayd and send them to the right domain consumer. Consumer type MUST be
1023 * network.
1024 */
1025 int cmd_setup_relayd(struct ltt_session *session)
1026 {
1027 int ret = LTTNG_OK;
1028 struct ltt_ust_session *usess;
1029 struct ltt_kernel_session *ksess;
1030 struct consumer_socket *socket;
1031 struct lttng_ht_iter iter;
1032
1033 assert(session);
1034
1035 usess = session->ust_session;
1036 ksess = session->kernel_session;
1037
1038 DBG("Setting relayd for session %s", session->name);
1039
1040 rcu_read_lock();
1041
1042 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1043 && usess->consumer->enabled) {
1044 /* For each consumer socket, send relayd sockets */
1045 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1046 socket, node.node) {
1047 pthread_mutex_lock(socket->lock);
1048 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1049 usess->consumer, socket,
1050 session->name, session->hostname,
1051 session->live_timer);
1052 pthread_mutex_unlock(socket->lock);
1053 if (ret != LTTNG_OK) {
1054 goto error;
1055 }
1056 /* Session is now ready for network streaming. */
1057 session->net_handle = 1;
1058 }
1059 session->consumer->relay_major_version =
1060 usess->consumer->relay_major_version;
1061 session->consumer->relay_minor_version =
1062 usess->consumer->relay_minor_version;
1063 }
1064
1065 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1066 && ksess->consumer->enabled) {
1067 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1068 socket, node.node) {
1069 pthread_mutex_lock(socket->lock);
1070 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1071 ksess->consumer, socket,
1072 session->name, session->hostname,
1073 session->live_timer);
1074 pthread_mutex_unlock(socket->lock);
1075 if (ret != LTTNG_OK) {
1076 goto error;
1077 }
1078 /* Session is now ready for network streaming. */
1079 session->net_handle = 1;
1080 }
1081 session->consumer->relay_major_version =
1082 ksess->consumer->relay_major_version;
1083 session->consumer->relay_minor_version =
1084 ksess->consumer->relay_minor_version;
1085 }
1086
1087 error:
1088 rcu_read_unlock();
1089 return ret;
1090 }
1091
1092 /*
1093 * Start a kernel session by opening all necessary streams.
1094 */
1095 static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
1096 {
1097 int ret;
1098 struct ltt_kernel_channel *kchan;
1099
1100 /* Open kernel metadata */
1101 if (ksess->metadata == NULL && ksess->output_traces) {
1102 ret = kernel_open_metadata(ksess);
1103 if (ret < 0) {
1104 ret = LTTNG_ERR_KERN_META_FAIL;
1105 goto error;
1106 }
1107 }
1108
1109 /* Open kernel metadata stream */
1110 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1111 ret = kernel_open_metadata_stream(ksess);
1112 if (ret < 0) {
1113 ERR("Kernel create metadata stream failed");
1114 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1115 goto error;
1116 }
1117 }
1118
1119 /* For each channel */
1120 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1121 if (kchan->stream_count == 0) {
1122 ret = kernel_open_channel_stream(kchan);
1123 if (ret < 0) {
1124 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1125 goto error;
1126 }
1127 /* Update the stream global counter */
1128 ksess->stream_count_global += ret;
1129 }
1130 }
1131
1132 /* Setup kernel consumer socket and send fds to it */
1133 ret = init_kernel_tracing(ksess);
1134 if (ret != 0) {
1135 ret = LTTNG_ERR_KERN_START_FAIL;
1136 goto error;
1137 }
1138
1139 /* This start the kernel tracing */
1140 ret = kernel_start_session(ksess);
1141 if (ret < 0) {
1142 ret = LTTNG_ERR_KERN_START_FAIL;
1143 goto error;
1144 }
1145
1146 /* Quiescent wait after starting trace */
1147 kernel_wait_quiescent(kernel_tracer_fd);
1148
1149 ksess->active = 1;
1150
1151 ret = LTTNG_OK;
1152
1153 error:
1154 return ret;
1155 }
1156
1157 /*
1158 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1159 */
1160 int cmd_disable_channel(struct ltt_session *session,
1161 enum lttng_domain_type domain, char *channel_name)
1162 {
1163 int ret;
1164 struct ltt_ust_session *usess;
1165
1166 usess = session->ust_session;
1167
1168 rcu_read_lock();
1169
1170 switch (domain) {
1171 case LTTNG_DOMAIN_KERNEL:
1172 {
1173 ret = channel_kernel_disable(session->kernel_session,
1174 channel_name);
1175 if (ret != LTTNG_OK) {
1176 goto error;
1177 }
1178
1179 kernel_wait_quiescent(kernel_tracer_fd);
1180 break;
1181 }
1182 case LTTNG_DOMAIN_UST:
1183 {
1184 struct ltt_ust_channel *uchan;
1185 struct lttng_ht *chan_ht;
1186
1187 chan_ht = usess->domain_global.channels;
1188
1189 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1190 if (uchan == NULL) {
1191 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1192 goto error;
1193 }
1194
1195 ret = channel_ust_disable(usess, uchan);
1196 if (ret != LTTNG_OK) {
1197 goto error;
1198 }
1199 break;
1200 }
1201 default:
1202 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1203 goto error;
1204 }
1205
1206 ret = LTTNG_OK;
1207
1208 error:
1209 rcu_read_unlock();
1210 return ret;
1211 }
1212
1213 /*
1214 * Command LTTNG_TRACK_PID processed by the client thread.
1215 *
1216 * Called with session lock held.
1217 */
1218 int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
1219 int pid)
1220 {
1221 int ret;
1222
1223 rcu_read_lock();
1224
1225 switch (domain) {
1226 case LTTNG_DOMAIN_KERNEL:
1227 {
1228 struct ltt_kernel_session *ksess;
1229
1230 ksess = session->kernel_session;
1231
1232 ret = kernel_track_pid(ksess, pid);
1233 if (ret != LTTNG_OK) {
1234 goto error;
1235 }
1236
1237 kernel_wait_quiescent(kernel_tracer_fd);
1238 break;
1239 }
1240 case LTTNG_DOMAIN_UST:
1241 {
1242 struct ltt_ust_session *usess;
1243
1244 usess = session->ust_session;
1245
1246 ret = trace_ust_track_pid(usess, pid);
1247 if (ret != LTTNG_OK) {
1248 goto error;
1249 }
1250 break;
1251 }
1252 default:
1253 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1254 goto error;
1255 }
1256
1257 ret = LTTNG_OK;
1258
1259 error:
1260 rcu_read_unlock();
1261 return ret;
1262 }
1263
1264 /*
1265 * Command LTTNG_UNTRACK_PID processed by the client thread.
1266 *
1267 * Called with session lock held.
1268 */
1269 int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
1270 int pid)
1271 {
1272 int ret;
1273
1274 rcu_read_lock();
1275
1276 switch (domain) {
1277 case LTTNG_DOMAIN_KERNEL:
1278 {
1279 struct ltt_kernel_session *ksess;
1280
1281 ksess = session->kernel_session;
1282
1283 ret = kernel_untrack_pid(ksess, pid);
1284 if (ret != LTTNG_OK) {
1285 goto error;
1286 }
1287
1288 kernel_wait_quiescent(kernel_tracer_fd);
1289 break;
1290 }
1291 case LTTNG_DOMAIN_UST:
1292 {
1293 struct ltt_ust_session *usess;
1294
1295 usess = session->ust_session;
1296
1297 ret = trace_ust_untrack_pid(usess, pid);
1298 if (ret != LTTNG_OK) {
1299 goto error;
1300 }
1301 break;
1302 }
1303 default:
1304 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1305 goto error;
1306 }
1307
1308 ret = LTTNG_OK;
1309
1310 error:
1311 rcu_read_unlock();
1312 return ret;
1313 }
1314
1315 /*
1316 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1317 *
1318 * The wpipe arguments is used as a notifier for the kernel thread.
1319 */
1320 int cmd_enable_channel(struct ltt_session *session,
1321 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
1322 {
1323 int ret;
1324 struct ltt_ust_session *usess = session->ust_session;
1325 struct lttng_ht *chan_ht;
1326 size_t len;
1327
1328 assert(session);
1329 assert(attr);
1330 assert(domain);
1331
1332 len = lttng_strnlen(attr->name, sizeof(attr->name));
1333
1334 /* Validate channel name */
1335 if (attr->name[0] == '.' ||
1336 memchr(attr->name, '/', len) != NULL) {
1337 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1338 goto end;
1339 }
1340
1341 DBG("Enabling channel %s for session %s", attr->name, session->name);
1342
1343 rcu_read_lock();
1344
1345 /*
1346 * Don't try to enable a channel if the session has been started at
1347 * some point in time before. The tracer does not allow it.
1348 */
1349 if (session->has_been_started) {
1350 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1351 goto error;
1352 }
1353
1354 /*
1355 * If the session is a live session, remove the switch timer, the
1356 * live timer does the same thing but sends also synchronisation
1357 * beacons for inactive streams.
1358 */
1359 if (session->live_timer > 0) {
1360 attr->attr.live_timer_interval = session->live_timer;
1361 attr->attr.switch_timer_interval = 0;
1362 }
1363
1364 /* Check for feature support */
1365 switch (domain->type) {
1366 case LTTNG_DOMAIN_KERNEL:
1367 {
1368 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd) != 1) {
1369 /* Sampling position of buffer is not supported */
1370 WARN("Kernel tracer does not support buffer monitoring. "
1371 "Setting the monitor interval timer to 0 "
1372 "(disabled) for channel '%s' of session '%s'",
1373 attr-> name, session->name);
1374 lttng_channel_set_monitor_timer_interval(attr, 0);
1375 }
1376 break;
1377 }
1378 case LTTNG_DOMAIN_UST:
1379 case LTTNG_DOMAIN_JUL:
1380 case LTTNG_DOMAIN_LOG4J:
1381 case LTTNG_DOMAIN_PYTHON:
1382 break;
1383 default:
1384 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1385 goto error;
1386 }
1387
1388 switch (domain->type) {
1389 case LTTNG_DOMAIN_KERNEL:
1390 {
1391 struct ltt_kernel_channel *kchan;
1392
1393 kchan = trace_kernel_get_channel_by_name(attr->name,
1394 session->kernel_session);
1395 if (kchan == NULL) {
1396 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
1397 if (attr->name[0] != '\0') {
1398 session->kernel_session->has_non_default_channel = 1;
1399 }
1400 } else {
1401 ret = channel_kernel_enable(session->kernel_session, kchan);
1402 }
1403
1404 if (ret != LTTNG_OK) {
1405 goto error;
1406 }
1407
1408 kernel_wait_quiescent(kernel_tracer_fd);
1409 break;
1410 }
1411 case LTTNG_DOMAIN_UST:
1412 case LTTNG_DOMAIN_JUL:
1413 case LTTNG_DOMAIN_LOG4J:
1414 case LTTNG_DOMAIN_PYTHON:
1415 {
1416 struct ltt_ust_channel *uchan;
1417
1418 /*
1419 * FIXME
1420 *
1421 * Current agent implementation limitations force us to allow
1422 * only one channel at once in "agent" subdomains. Each
1423 * subdomain has a default channel name which must be strictly
1424 * adhered to.
1425 */
1426 if (domain->type == LTTNG_DOMAIN_JUL) {
1427 if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME,
1428 LTTNG_SYMBOL_NAME_LEN)) {
1429 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1430 goto error;
1431 }
1432 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1433 if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME,
1434 LTTNG_SYMBOL_NAME_LEN)) {
1435 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1436 goto error;
1437 }
1438 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1439 if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME,
1440 LTTNG_SYMBOL_NAME_LEN)) {
1441 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1442 goto error;
1443 }
1444 }
1445
1446 chan_ht = usess->domain_global.channels;
1447
1448 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1449 if (uchan == NULL) {
1450 ret = channel_ust_create(usess, attr, domain->buf_type);
1451 if (attr->name[0] != '\0') {
1452 usess->has_non_default_channel = 1;
1453 }
1454 } else {
1455 ret = channel_ust_enable(usess, uchan);
1456 }
1457 break;
1458 }
1459 default:
1460 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1461 goto error;
1462 }
1463
1464 error:
1465 rcu_read_unlock();
1466 end:
1467 return ret;
1468 }
1469
1470 /*
1471 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1472 */
1473 int cmd_disable_event(struct ltt_session *session,
1474 enum lttng_domain_type domain, char *channel_name,
1475 struct lttng_event *event)
1476 {
1477 int ret;
1478 char *event_name;
1479
1480 DBG("Disable event command for event \'%s\'", event->name);
1481
1482 event_name = event->name;
1483
1484 /* Error out on unhandled search criteria */
1485 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1486 || event->pid || event->filter || event->exclusion) {
1487 ret = LTTNG_ERR_UNK;
1488 goto error;
1489 }
1490
1491 rcu_read_lock();
1492
1493 switch (domain) {
1494 case LTTNG_DOMAIN_KERNEL:
1495 {
1496 struct ltt_kernel_channel *kchan;
1497 struct ltt_kernel_session *ksess;
1498
1499 ksess = session->kernel_session;
1500
1501 /*
1502 * If a non-default channel has been created in the
1503 * session, explicitely require that -c chan_name needs
1504 * to be provided.
1505 */
1506 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1507 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1508 goto error_unlock;
1509 }
1510
1511 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1512 if (kchan == NULL) {
1513 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1514 goto error_unlock;
1515 }
1516
1517 switch (event->type) {
1518 case LTTNG_EVENT_ALL:
1519 case LTTNG_EVENT_TRACEPOINT:
1520 case LTTNG_EVENT_SYSCALL:
1521 case LTTNG_EVENT_PROBE:
1522 case LTTNG_EVENT_FUNCTION:
1523 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1524 if (event_name[0] == '\0') {
1525 ret = event_kernel_disable_event(kchan,
1526 NULL, event->type);
1527 } else {
1528 ret = event_kernel_disable_event(kchan,
1529 event_name, event->type);
1530 }
1531 if (ret != LTTNG_OK) {
1532 goto error_unlock;
1533 }
1534 break;
1535 default:
1536 ret = LTTNG_ERR_UNK;
1537 goto error_unlock;
1538 }
1539
1540 kernel_wait_quiescent(kernel_tracer_fd);
1541 break;
1542 }
1543 case LTTNG_DOMAIN_UST:
1544 {
1545 struct ltt_ust_channel *uchan;
1546 struct ltt_ust_session *usess;
1547
1548 usess = session->ust_session;
1549
1550 if (validate_ust_event_name(event_name)) {
1551 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1552 goto error_unlock;
1553 }
1554
1555 /*
1556 * If a non-default channel has been created in the
1557 * session, explicitly require that -c chan_name needs
1558 * to be provided.
1559 */
1560 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1561 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1562 goto error_unlock;
1563 }
1564
1565 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1566 channel_name);
1567 if (uchan == NULL) {
1568 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1569 goto error_unlock;
1570 }
1571
1572 switch (event->type) {
1573 case LTTNG_EVENT_ALL:
1574 /*
1575 * An empty event name means that everything
1576 * should be disabled.
1577 */
1578 if (event->name[0] == '\0') {
1579 ret = event_ust_disable_all_tracepoints(usess, uchan);
1580 } else {
1581 ret = event_ust_disable_tracepoint(usess, uchan,
1582 event_name);
1583 }
1584 if (ret != LTTNG_OK) {
1585 goto error_unlock;
1586 }
1587 break;
1588 default:
1589 ret = LTTNG_ERR_UNK;
1590 goto error_unlock;
1591 }
1592
1593 DBG3("Disable UST event %s in channel %s completed", event_name,
1594 channel_name);
1595 break;
1596 }
1597 case LTTNG_DOMAIN_LOG4J:
1598 case LTTNG_DOMAIN_JUL:
1599 case LTTNG_DOMAIN_PYTHON:
1600 {
1601 struct agent *agt;
1602 struct ltt_ust_session *usess = session->ust_session;
1603
1604 assert(usess);
1605
1606 switch (event->type) {
1607 case LTTNG_EVENT_ALL:
1608 break;
1609 default:
1610 ret = LTTNG_ERR_UNK;
1611 goto error_unlock;
1612 }
1613
1614 agt = trace_ust_find_agent(usess, domain);
1615 if (!agt) {
1616 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1617 goto error_unlock;
1618 }
1619 /*
1620 * An empty event name means that everything
1621 * should be disabled.
1622 */
1623 if (event->name[0] == '\0') {
1624 ret = event_agent_disable_all(usess, agt);
1625 } else {
1626 ret = event_agent_disable(usess, agt, event_name);
1627 }
1628 if (ret != LTTNG_OK) {
1629 goto error_unlock;
1630 }
1631
1632 break;
1633 }
1634 default:
1635 ret = LTTNG_ERR_UND;
1636 goto error_unlock;
1637 }
1638
1639 ret = LTTNG_OK;
1640
1641 error_unlock:
1642 rcu_read_unlock();
1643 error:
1644 return ret;
1645 }
1646
1647 /*
1648 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1649 */
1650 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1651 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
1652 {
1653 int ret, chan_kern_created = 0, chan_ust_created = 0;
1654 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1655
1656 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1657 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1658 app_ctx_name = ctx->u.app_ctx.ctx_name;
1659 }
1660
1661 switch (domain) {
1662 case LTTNG_DOMAIN_KERNEL:
1663 assert(session->kernel_session);
1664
1665 if (session->kernel_session->channel_count == 0) {
1666 /* Create default channel */
1667 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1668 if (ret != LTTNG_OK) {
1669 goto error;
1670 }
1671 chan_kern_created = 1;
1672 }
1673 /* Add kernel context to kernel tracer */
1674 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1675 if (ret != LTTNG_OK) {
1676 goto error;
1677 }
1678 break;
1679 case LTTNG_DOMAIN_JUL:
1680 case LTTNG_DOMAIN_LOG4J:
1681 {
1682 /*
1683 * Validate channel name.
1684 * If no channel name is given and the domain is JUL or LOG4J,
1685 * set it to the appropriate domain-specific channel name. If
1686 * a name is provided but does not match the expexted channel
1687 * name, return an error.
1688 */
1689 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1690 strcmp(channel_name,
1691 DEFAULT_JUL_CHANNEL_NAME)) {
1692 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1693 goto error;
1694 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1695 strcmp(channel_name,
1696 DEFAULT_LOG4J_CHANNEL_NAME)) {
1697 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1698 goto error;
1699 }
1700 /* break is _not_ missing here. */
1701 }
1702 case LTTNG_DOMAIN_UST:
1703 {
1704 struct ltt_ust_session *usess = session->ust_session;
1705 unsigned int chan_count;
1706
1707 assert(usess);
1708
1709 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1710 if (chan_count == 0) {
1711 struct lttng_channel *attr;
1712 /* Create default channel */
1713 attr = channel_new_default_attr(domain, usess->buffer_type);
1714 if (attr == NULL) {
1715 ret = LTTNG_ERR_FATAL;
1716 goto error;
1717 }
1718
1719 ret = channel_ust_create(usess, attr, usess->buffer_type);
1720 if (ret != LTTNG_OK) {
1721 free(attr);
1722 goto error;
1723 }
1724 channel_attr_destroy(attr);
1725 chan_ust_created = 1;
1726 }
1727
1728 ret = context_ust_add(usess, domain, ctx, channel_name);
1729 free(app_ctx_provider_name);
1730 free(app_ctx_name);
1731 app_ctx_name = NULL;
1732 app_ctx_provider_name = NULL;
1733 if (ret != LTTNG_OK) {
1734 goto error;
1735 }
1736 break;
1737 }
1738 default:
1739 ret = LTTNG_ERR_UND;
1740 goto error;
1741 }
1742
1743 ret = LTTNG_OK;
1744 goto end;
1745
1746 error:
1747 if (chan_kern_created) {
1748 struct ltt_kernel_channel *kchan =
1749 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1750 session->kernel_session);
1751 /* Created previously, this should NOT fail. */
1752 assert(kchan);
1753 kernel_destroy_channel(kchan);
1754 }
1755
1756 if (chan_ust_created) {
1757 struct ltt_ust_channel *uchan =
1758 trace_ust_find_channel_by_name(
1759 session->ust_session->domain_global.channels,
1760 DEFAULT_CHANNEL_NAME);
1761 /* Created previously, this should NOT fail. */
1762 assert(uchan);
1763 /* Remove from the channel list of the session. */
1764 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1765 uchan);
1766 trace_ust_destroy_channel(uchan);
1767 }
1768 end:
1769 free(app_ctx_provider_name);
1770 free(app_ctx_name);
1771 return ret;
1772 }
1773
1774 static inline bool name_starts_with(const char *name, const char *prefix)
1775 {
1776 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1777
1778 return !strncmp(name, prefix, max_cmp_len);
1779 }
1780
1781 /* Perform userspace-specific event name validation */
1782 static int validate_ust_event_name(const char *name)
1783 {
1784 int ret = 0;
1785
1786 if (!name) {
1787 ret = -1;
1788 goto end;
1789 }
1790
1791 /*
1792 * Check name against all internal UST event component namespaces used
1793 * by the agents.
1794 */
1795 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
1796 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
1797 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
1798 ret = -1;
1799 }
1800
1801 end:
1802 return ret;
1803 }
1804
1805 /*
1806 * Internal version of cmd_enable_event() with a supplemental
1807 * "internal_event" flag which is used to enable internal events which should
1808 * be hidden from clients. Such events are used in the agent implementation to
1809 * enable the events through which all "agent" events are funeled.
1810 */
1811 static int _cmd_enable_event(struct ltt_session *session,
1812 struct lttng_domain *domain,
1813 char *channel_name, struct lttng_event *event,
1814 char *filter_expression,
1815 struct lttng_filter_bytecode *filter,
1816 struct lttng_event_exclusion *exclusion,
1817 int wpipe, bool internal_event)
1818 {
1819 int ret = 0, channel_created = 0;
1820 struct lttng_channel *attr = NULL;
1821
1822 assert(session);
1823 assert(event);
1824 assert(channel_name);
1825
1826 /* If we have a filter, we must have its filter expression */
1827 assert(!(!!filter_expression ^ !!filter));
1828
1829 /* Normalize event name as a globbing pattern */
1830 strutils_normalize_star_glob_pattern(event->name);
1831
1832 /* Normalize exclusion names as globbing patterns */
1833 if (exclusion) {
1834 size_t i;
1835
1836 for (i = 0; i < exclusion->count; i++) {
1837 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
1838
1839 strutils_normalize_star_glob_pattern(name);
1840 }
1841 }
1842
1843 DBG("Enable event command for event \'%s\'", event->name);
1844
1845 rcu_read_lock();
1846
1847 switch (domain->type) {
1848 case LTTNG_DOMAIN_KERNEL:
1849 {
1850 struct ltt_kernel_channel *kchan;
1851
1852 /*
1853 * If a non-default channel has been created in the
1854 * session, explicitely require that -c chan_name needs
1855 * to be provided.
1856 */
1857 if (session->kernel_session->has_non_default_channel
1858 && channel_name[0] == '\0') {
1859 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1860 goto error;
1861 }
1862
1863 kchan = trace_kernel_get_channel_by_name(channel_name,
1864 session->kernel_session);
1865 if (kchan == NULL) {
1866 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1867 LTTNG_BUFFER_GLOBAL);
1868 if (attr == NULL) {
1869 ret = LTTNG_ERR_FATAL;
1870 goto error;
1871 }
1872 if (lttng_strncpy(attr->name, channel_name,
1873 sizeof(attr->name))) {
1874 ret = LTTNG_ERR_INVALID;
1875 goto error;
1876 }
1877
1878 ret = cmd_enable_channel(session, domain, attr, wpipe);
1879 if (ret != LTTNG_OK) {
1880 goto error;
1881 }
1882 channel_created = 1;
1883 }
1884
1885 /* Get the newly created kernel channel pointer */
1886 kchan = trace_kernel_get_channel_by_name(channel_name,
1887 session->kernel_session);
1888 if (kchan == NULL) {
1889 /* This sould not happen... */
1890 ret = LTTNG_ERR_FATAL;
1891 goto error;
1892 }
1893
1894 switch (event->type) {
1895 case LTTNG_EVENT_ALL:
1896 {
1897 char *filter_expression_a = NULL;
1898 struct lttng_filter_bytecode *filter_a = NULL;
1899
1900 /*
1901 * We need to duplicate filter_expression and filter,
1902 * because ownership is passed to first enable
1903 * event.
1904 */
1905 if (filter_expression) {
1906 filter_expression_a = strdup(filter_expression);
1907 if (!filter_expression_a) {
1908 ret = LTTNG_ERR_FATAL;
1909 goto error;
1910 }
1911 }
1912 if (filter) {
1913 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
1914 if (!filter_a) {
1915 free(filter_expression_a);
1916 ret = LTTNG_ERR_FATAL;
1917 goto error;
1918 }
1919 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
1920 }
1921 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
1922 ret = event_kernel_enable_event(kchan, event,
1923 filter_expression, filter);
1924 /* We have passed ownership */
1925 filter_expression = NULL;
1926 filter = NULL;
1927 if (ret != LTTNG_OK) {
1928 if (channel_created) {
1929 /* Let's not leak a useless channel. */
1930 kernel_destroy_channel(kchan);
1931 }
1932 free(filter_expression_a);
1933 free(filter_a);
1934 goto error;
1935 }
1936 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
1937 ret = event_kernel_enable_event(kchan, event,
1938 filter_expression_a, filter_a);
1939 /* We have passed ownership */
1940 filter_expression_a = NULL;
1941 filter_a = NULL;
1942 if (ret != LTTNG_OK) {
1943 goto error;
1944 }
1945 break;
1946 }
1947 case LTTNG_EVENT_PROBE:
1948 case LTTNG_EVENT_FUNCTION:
1949 case LTTNG_EVENT_FUNCTION_ENTRY:
1950 case LTTNG_EVENT_TRACEPOINT:
1951 ret = event_kernel_enable_event(kchan, event,
1952 filter_expression, filter);
1953 /* We have passed ownership */
1954 filter_expression = NULL;
1955 filter = NULL;
1956 if (ret != LTTNG_OK) {
1957 if (channel_created) {
1958 /* Let's not leak a useless channel. */
1959 kernel_destroy_channel(kchan);
1960 }
1961 goto error;
1962 }
1963 break;
1964 case LTTNG_EVENT_SYSCALL:
1965 ret = event_kernel_enable_event(kchan, event,
1966 filter_expression, filter);
1967 /* We have passed ownership */
1968 filter_expression = NULL;
1969 filter = NULL;
1970 if (ret != LTTNG_OK) {
1971 goto error;
1972 }
1973 break;
1974 default:
1975 ret = LTTNG_ERR_UNK;
1976 goto error;
1977 }
1978
1979 kernel_wait_quiescent(kernel_tracer_fd);
1980 break;
1981 }
1982 case LTTNG_DOMAIN_UST:
1983 {
1984 struct ltt_ust_channel *uchan;
1985 struct ltt_ust_session *usess = session->ust_session;
1986
1987 assert(usess);
1988
1989 /*
1990 * If a non-default channel has been created in the
1991 * session, explicitely require that -c chan_name needs
1992 * to be provided.
1993 */
1994 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1995 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1996 goto error;
1997 }
1998
1999 /* Get channel from global UST domain */
2000 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2001 channel_name);
2002 if (uchan == NULL) {
2003 /* Create default channel */
2004 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2005 usess->buffer_type);
2006 if (attr == NULL) {
2007 ret = LTTNG_ERR_FATAL;
2008 goto error;
2009 }
2010 if (lttng_strncpy(attr->name, channel_name,
2011 sizeof(attr->name))) {
2012 ret = LTTNG_ERR_INVALID;
2013 goto error;
2014 }
2015
2016 ret = cmd_enable_channel(session, domain, attr, wpipe);
2017 if (ret != LTTNG_OK) {
2018 goto error;
2019 }
2020
2021 /* Get the newly created channel reference back */
2022 uchan = trace_ust_find_channel_by_name(
2023 usess->domain_global.channels, channel_name);
2024 assert(uchan);
2025 }
2026
2027 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2028 /*
2029 * Don't allow users to add UST events to channels which
2030 * are assigned to a userspace subdomain (JUL, Log4J,
2031 * Python, etc.).
2032 */
2033 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2034 goto error;
2035 }
2036
2037 if (!internal_event) {
2038 /*
2039 * Ensure the event name is not reserved for internal
2040 * use.
2041 */
2042 ret = validate_ust_event_name(event->name);
2043 if (ret) {
2044 WARN("Userspace event name %s failed validation.",
2045 event->name ?
2046 event->name : "NULL");
2047 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2048 goto error;
2049 }
2050 }
2051
2052 /* At this point, the session and channel exist on the tracer */
2053 ret = event_ust_enable_tracepoint(usess, uchan, event,
2054 filter_expression, filter, exclusion,
2055 internal_event);
2056 /* We have passed ownership */
2057 filter_expression = NULL;
2058 filter = NULL;
2059 exclusion = NULL;
2060 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2061 goto already_enabled;
2062 } else if (ret != LTTNG_OK) {
2063 goto error;
2064 }
2065 break;
2066 }
2067 case LTTNG_DOMAIN_LOG4J:
2068 case LTTNG_DOMAIN_JUL:
2069 case LTTNG_DOMAIN_PYTHON:
2070 {
2071 const char *default_event_name, *default_chan_name;
2072 struct agent *agt;
2073 struct lttng_event uevent;
2074 struct lttng_domain tmp_dom;
2075 struct ltt_ust_session *usess = session->ust_session;
2076
2077 assert(usess);
2078
2079 agt = trace_ust_find_agent(usess, domain->type);
2080 if (!agt) {
2081 agt = agent_create(domain->type);
2082 if (!agt) {
2083 ret = LTTNG_ERR_NOMEM;
2084 goto error;
2085 }
2086 agent_add(agt, usess->agents);
2087 }
2088
2089 /* Create the default tracepoint. */
2090 memset(&uevent, 0, sizeof(uevent));
2091 uevent.type = LTTNG_EVENT_TRACEPOINT;
2092 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2093 default_event_name = event_get_default_agent_ust_name(
2094 domain->type);
2095 if (!default_event_name) {
2096 ret = LTTNG_ERR_FATAL;
2097 goto error;
2098 }
2099 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2100 uevent.name[sizeof(uevent.name) - 1] = '\0';
2101
2102 /*
2103 * The domain type is changed because we are about to enable the
2104 * default channel and event for the JUL domain that are hardcoded.
2105 * This happens in the UST domain.
2106 */
2107 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2108 tmp_dom.type = LTTNG_DOMAIN_UST;
2109
2110 switch (domain->type) {
2111 case LTTNG_DOMAIN_LOG4J:
2112 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2113 break;
2114 case LTTNG_DOMAIN_JUL:
2115 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2116 break;
2117 case LTTNG_DOMAIN_PYTHON:
2118 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2119 break;
2120 default:
2121 /* The switch/case we are in makes this impossible */
2122 assert(0);
2123 }
2124
2125 {
2126 char *filter_expression_copy = NULL;
2127 struct lttng_filter_bytecode *filter_copy = NULL;
2128
2129 if (filter) {
2130 const size_t filter_size = sizeof(
2131 struct lttng_filter_bytecode)
2132 + filter->len;
2133
2134 filter_copy = zmalloc(filter_size);
2135 if (!filter_copy) {
2136 ret = LTTNG_ERR_NOMEM;
2137 goto error;
2138 }
2139 memcpy(filter_copy, filter, filter_size);
2140
2141 filter_expression_copy =
2142 strdup(filter_expression);
2143 if (!filter_expression) {
2144 ret = LTTNG_ERR_NOMEM;
2145 }
2146
2147 if (!filter_expression_copy || !filter_copy) {
2148 free(filter_expression_copy);
2149 free(filter_copy);
2150 goto error;
2151 }
2152 }
2153
2154 ret = cmd_enable_event_internal(session, &tmp_dom,
2155 (char *) default_chan_name,
2156 &uevent, filter_expression_copy,
2157 filter_copy, NULL, wpipe);
2158 }
2159
2160 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2161 goto already_enabled;
2162 } else if (ret != LTTNG_OK) {
2163 goto error;
2164 }
2165
2166 /* The wild card * means that everything should be enabled. */
2167 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2168 ret = event_agent_enable_all(usess, agt, event, filter,
2169 filter_expression);
2170 } else {
2171 ret = event_agent_enable(usess, agt, event, filter,
2172 filter_expression);
2173 }
2174 filter = NULL;
2175 filter_expression = NULL;
2176 if (ret != LTTNG_OK) {
2177 goto error;
2178 }
2179
2180 break;
2181 }
2182 default:
2183 ret = LTTNG_ERR_UND;
2184 goto error;
2185 }
2186
2187 ret = LTTNG_OK;
2188
2189 already_enabled:
2190 error:
2191 free(filter_expression);
2192 free(filter);
2193 free(exclusion);
2194 channel_attr_destroy(attr);
2195 rcu_read_unlock();
2196 return ret;
2197 }
2198
2199 /*
2200 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2201 * We own filter, exclusion, and filter_expression.
2202 */
2203 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
2204 char *channel_name, struct lttng_event *event,
2205 char *filter_expression,
2206 struct lttng_filter_bytecode *filter,
2207 struct lttng_event_exclusion *exclusion,
2208 int wpipe)
2209 {
2210 return _cmd_enable_event(session, domain, channel_name, event,
2211 filter_expression, filter, exclusion, wpipe, false);
2212 }
2213
2214 /*
2215 * Enable an event which is internal to LTTng. An internal should
2216 * never be made visible to clients and are immune to checks such as
2217 * reserved names.
2218 */
2219 static int cmd_enable_event_internal(struct ltt_session *session,
2220 struct lttng_domain *domain,
2221 char *channel_name, struct lttng_event *event,
2222 char *filter_expression,
2223 struct lttng_filter_bytecode *filter,
2224 struct lttng_event_exclusion *exclusion,
2225 int wpipe)
2226 {
2227 return _cmd_enable_event(session, domain, channel_name, event,
2228 filter_expression, filter, exclusion, wpipe, true);
2229 }
2230
2231 /*
2232 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2233 */
2234 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2235 struct lttng_event **events)
2236 {
2237 int ret;
2238 ssize_t nb_events = 0;
2239
2240 switch (domain) {
2241 case LTTNG_DOMAIN_KERNEL:
2242 nb_events = kernel_list_events(kernel_tracer_fd, events);
2243 if (nb_events < 0) {
2244 ret = LTTNG_ERR_KERN_LIST_FAIL;
2245 goto error;
2246 }
2247 break;
2248 case LTTNG_DOMAIN_UST:
2249 nb_events = ust_app_list_events(events);
2250 if (nb_events < 0) {
2251 ret = LTTNG_ERR_UST_LIST_FAIL;
2252 goto error;
2253 }
2254 break;
2255 case LTTNG_DOMAIN_LOG4J:
2256 case LTTNG_DOMAIN_JUL:
2257 case LTTNG_DOMAIN_PYTHON:
2258 nb_events = agent_list_events(events, domain);
2259 if (nb_events < 0) {
2260 ret = LTTNG_ERR_UST_LIST_FAIL;
2261 goto error;
2262 }
2263 break;
2264 default:
2265 ret = LTTNG_ERR_UND;
2266 goto error;
2267 }
2268
2269 return nb_events;
2270
2271 error:
2272 /* Return negative value to differentiate return code */
2273 return -ret;
2274 }
2275
2276 /*
2277 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2278 */
2279 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2280 struct lttng_event_field **fields)
2281 {
2282 int ret;
2283 ssize_t nb_fields = 0;
2284
2285 switch (domain) {
2286 case LTTNG_DOMAIN_UST:
2287 nb_fields = ust_app_list_event_fields(fields);
2288 if (nb_fields < 0) {
2289 ret = LTTNG_ERR_UST_LIST_FAIL;
2290 goto error;
2291 }
2292 break;
2293 case LTTNG_DOMAIN_KERNEL:
2294 default: /* fall-through */
2295 ret = LTTNG_ERR_UND;
2296 goto error;
2297 }
2298
2299 return nb_fields;
2300
2301 error:
2302 /* Return negative value to differentiate return code */
2303 return -ret;
2304 }
2305
2306 ssize_t cmd_list_syscalls(struct lttng_event **events)
2307 {
2308 return syscall_table_list(events);
2309 }
2310
2311 /*
2312 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2313 *
2314 * Called with session lock held.
2315 */
2316 ssize_t cmd_list_tracker_pids(struct ltt_session *session,
2317 enum lttng_domain_type domain, int32_t **pids)
2318 {
2319 int ret;
2320 ssize_t nr_pids = 0;
2321
2322 switch (domain) {
2323 case LTTNG_DOMAIN_KERNEL:
2324 {
2325 struct ltt_kernel_session *ksess;
2326
2327 ksess = session->kernel_session;
2328 nr_pids = kernel_list_tracker_pids(ksess, pids);
2329 if (nr_pids < 0) {
2330 ret = LTTNG_ERR_KERN_LIST_FAIL;
2331 goto error;
2332 }
2333 break;
2334 }
2335 case LTTNG_DOMAIN_UST:
2336 {
2337 struct ltt_ust_session *usess;
2338
2339 usess = session->ust_session;
2340 nr_pids = trace_ust_list_tracker_pids(usess, pids);
2341 if (nr_pids < 0) {
2342 ret = LTTNG_ERR_UST_LIST_FAIL;
2343 goto error;
2344 }
2345 break;
2346 }
2347 case LTTNG_DOMAIN_LOG4J:
2348 case LTTNG_DOMAIN_JUL:
2349 case LTTNG_DOMAIN_PYTHON:
2350 default:
2351 ret = LTTNG_ERR_UND;
2352 goto error;
2353 }
2354
2355 return nr_pids;
2356
2357 error:
2358 /* Return negative value to differentiate return code */
2359 return -ret;
2360 }
2361
2362 /*
2363 * Command LTTNG_START_TRACE processed by the client thread.
2364 *
2365 * Called with session mutex held.
2366 */
2367 int cmd_start_trace(struct ltt_session *session)
2368 {
2369 int ret;
2370 unsigned long nb_chan = 0;
2371 struct ltt_kernel_session *ksession;
2372 struct ltt_ust_session *usess;
2373
2374 assert(session);
2375
2376 /* Ease our life a bit ;) */
2377 ksession = session->kernel_session;
2378 usess = session->ust_session;
2379
2380 /* Is the session already started? */
2381 if (session->active) {
2382 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2383 goto error;
2384 }
2385
2386 /*
2387 * Starting a session without channel is useless since after that it's not
2388 * possible to enable channel thus inform the client.
2389 */
2390 if (usess && usess->domain_global.channels) {
2391 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2392 }
2393 if (ksession) {
2394 nb_chan += ksession->channel_count;
2395 }
2396 if (!nb_chan) {
2397 ret = LTTNG_ERR_NO_CHANNEL;
2398 goto error;
2399 }
2400
2401 /*
2402 * Record the timestamp of the first time the session is started for
2403 * an eventual session rotation call.
2404 */
2405 if (!session->has_been_started) {
2406 session->current_chunk_start_ts = time(NULL);
2407 if (session->current_chunk_start_ts == (time_t) -1) {
2408 PERROR("Get start time");
2409 ret = LTTNG_ERR_FATAL;
2410 goto error;
2411 }
2412 }
2413
2414 /* Kernel tracing */
2415 if (ksession != NULL) {
2416 ret = start_kernel_session(ksession, kernel_tracer_fd);
2417 if (ret != LTTNG_OK) {
2418 goto error;
2419 }
2420 }
2421
2422 /* Flag session that trace should start automatically */
2423 if (usess) {
2424 /*
2425 * Even though the start trace might fail, flag this session active so
2426 * other application coming in are started by default.
2427 */
2428 usess->active = 1;
2429
2430 ret = ust_app_start_trace_all(usess);
2431 if (ret < 0) {
2432 ret = LTTNG_ERR_UST_START_FAIL;
2433 goto error;
2434 }
2435 }
2436
2437 /* Flag this after a successful start. */
2438 session->has_been_started = 1;
2439 session->active = 1;
2440
2441 if (session->rotate_timer_period) {
2442 ret = sessiond_rotate_timer_start(session,
2443 session->rotate_timer_period);
2444 if (ret < 0) {
2445 ERR("Failed to enable rotate timer");
2446 ret = LTTNG_ERR_UNK;
2447 goto error;
2448 }
2449 }
2450
2451 ret = LTTNG_OK;
2452
2453 error:
2454 return ret;
2455 }
2456
2457 /*
2458 * Command LTTNG_STOP_TRACE processed by the client thread.
2459 */
2460 int cmd_stop_trace(struct ltt_session *session)
2461 {
2462 int ret;
2463 struct ltt_kernel_channel *kchan;
2464 struct ltt_kernel_session *ksession;
2465 struct ltt_ust_session *usess;
2466
2467 assert(session);
2468
2469 /* Short cut */
2470 ksession = session->kernel_session;
2471 usess = session->ust_session;
2472
2473 /* Session is not active. Skip everythong and inform the client. */
2474 if (!session->active) {
2475 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2476 goto error;
2477 }
2478
2479 /* Kernel tracer */
2480 if (ksession && ksession->active) {
2481 DBG("Stop kernel tracing");
2482
2483 ret = kernel_stop_session(ksession);
2484 if (ret < 0) {
2485 ret = LTTNG_ERR_KERN_STOP_FAIL;
2486 goto error;
2487 }
2488
2489 kernel_wait_quiescent(kernel_tracer_fd);
2490
2491 /* Flush metadata after stopping (if exists) */
2492 if (ksession->metadata_stream_fd >= 0) {
2493 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2494 if (ret < 0) {
2495 ERR("Kernel metadata flush failed");
2496 }
2497 }
2498
2499 /* Flush all buffers after stopping */
2500 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2501 ret = kernel_flush_buffer(kchan);
2502 if (ret < 0) {
2503 ERR("Kernel flush buffer error");
2504 }
2505 }
2506
2507 ksession->active = 0;
2508 }
2509
2510 if (usess && usess->active) {
2511 /*
2512 * Even though the stop trace might fail, flag this session inactive so
2513 * other application coming in are not started by default.
2514 */
2515 usess->active = 0;
2516
2517 ret = ust_app_stop_trace_all(usess);
2518 if (ret < 0) {
2519 ret = LTTNG_ERR_UST_STOP_FAIL;
2520 goto error;
2521 }
2522 }
2523
2524 session->session_last_stop_ts = time(NULL);
2525
2526 /* Flag inactive after a successful stop. */
2527 session->active = 0;
2528 ret = LTTNG_OK;
2529
2530 error:
2531 return ret;
2532 }
2533
2534 /*
2535 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2536 */
2537 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2538 struct lttng_uri *uris)
2539 {
2540 int ret, i;
2541 struct ltt_kernel_session *ksess = session->kernel_session;
2542 struct ltt_ust_session *usess = session->ust_session;
2543
2544 assert(session);
2545 assert(uris);
2546 assert(nb_uri > 0);
2547
2548 /* Can't set consumer URI if the session is active. */
2549 if (session->active) {
2550 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2551 goto error;
2552 }
2553
2554 /* Set the "global" consumer URIs */
2555 for (i = 0; i < nb_uri; i++) {
2556 ret = add_uri_to_consumer(session->consumer,
2557 &uris[i], 0, session->name);
2558 if (ret != LTTNG_OK) {
2559 goto error;
2560 }
2561 }
2562
2563 /* Set UST session URIs */
2564 if (session->ust_session) {
2565 for (i = 0; i < nb_uri; i++) {
2566 ret = add_uri_to_consumer(
2567 session->ust_session->consumer,
2568 &uris[i], LTTNG_DOMAIN_UST,
2569 session->name);
2570 if (ret != LTTNG_OK) {
2571 goto error;
2572 }
2573 }
2574 }
2575
2576 /* Set kernel session URIs */
2577 if (session->kernel_session) {
2578 for (i = 0; i < nb_uri; i++) {
2579 ret = add_uri_to_consumer(
2580 session->kernel_session->consumer,
2581 &uris[i], LTTNG_DOMAIN_KERNEL,
2582 session->name);
2583 if (ret != LTTNG_OK) {
2584 goto error;
2585 }
2586 }
2587 }
2588
2589 /*
2590 * Make sure to set the session in output mode after we set URI since a
2591 * session can be created without URL (thus flagged in no output mode).
2592 */
2593 session->output_traces = 1;
2594 if (ksess) {
2595 ksess->output_traces = 1;
2596 }
2597
2598 if (usess) {
2599 usess->output_traces = 1;
2600 }
2601
2602 /* All good! */
2603 ret = LTTNG_OK;
2604
2605 error:
2606 return ret;
2607 }
2608
2609 /*
2610 * Command LTTNG_CREATE_SESSION processed by the client thread.
2611 */
2612 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
2613 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2614 {
2615 int ret;
2616 struct ltt_session *session;
2617
2618 assert(name);
2619 assert(creds);
2620
2621 /*
2622 * Verify if the session already exist
2623 *
2624 * XXX: There is no need for the session lock list here since the caller
2625 * (process_client_msg) is holding it. We might want to change that so a
2626 * single command does not lock the entire session list.
2627 */
2628 session = session_find_by_name(name);
2629 if (session != NULL) {
2630 ret = LTTNG_ERR_EXIST_SESS;
2631 goto find_error;
2632 }
2633
2634 /* Create tracing session in the registry */
2635 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2636 LTTNG_SOCK_GET_GID_CRED(creds));
2637 if (ret != LTTNG_OK) {
2638 goto session_error;
2639 }
2640
2641 /*
2642 * Get the newly created session pointer back
2643 *
2644 * XXX: There is no need for the session lock list here since the caller
2645 * (process_client_msg) is holding it. We might want to change that so a
2646 * single command does not lock the entire session list.
2647 */
2648 session = session_find_by_name(name);
2649 assert(session);
2650
2651 session->live_timer = live_timer;
2652 /* Create default consumer output for the session not yet created. */
2653 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2654 if (session->consumer == NULL) {
2655 ret = LTTNG_ERR_FATAL;
2656 goto consumer_error;
2657 }
2658
2659 if (uris) {
2660 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2661 if (ret != LTTNG_OK) {
2662 goto consumer_error;
2663 }
2664 session->output_traces = 1;
2665 } else {
2666 session->output_traces = 0;
2667 DBG2("Session %s created with no output", session->name);
2668 }
2669
2670 session->consumer->enabled = 1;
2671
2672 return LTTNG_OK;
2673
2674 consumer_error:
2675 session_destroy(session);
2676 session_error:
2677 find_error:
2678 return ret;
2679 }
2680
2681 /*
2682 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2683 */
2684 int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2685 size_t nb_uri, lttng_sock_cred *creds)
2686 {
2687 int ret;
2688 struct ltt_session *session;
2689 struct snapshot_output *new_output = NULL;
2690
2691 assert(name);
2692 assert(creds);
2693
2694 /*
2695 * Create session in no output mode with URIs set to NULL. The uris we've
2696 * received are for a default snapshot output if one.
2697 */
2698 ret = cmd_create_session_uri(name, NULL, 0, creds, 0);
2699 if (ret != LTTNG_OK) {
2700 goto error;
2701 }
2702
2703 /* Get the newly created session pointer back. This should NEVER fail. */
2704 session = session_find_by_name(name);
2705 assert(session);
2706
2707 /* Flag session for snapshot mode. */
2708 session->snapshot_mode = 1;
2709
2710 /* Skip snapshot output creation if no URI is given. */
2711 if (nb_uri == 0) {
2712 goto end;
2713 }
2714
2715 new_output = snapshot_output_alloc();
2716 if (!new_output) {
2717 ret = LTTNG_ERR_NOMEM;
2718 goto error_snapshot_alloc;
2719 }
2720
2721 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2722 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2723 if (ret < 0) {
2724 if (ret == -ENOMEM) {
2725 ret = LTTNG_ERR_NOMEM;
2726 } else {
2727 ret = LTTNG_ERR_INVALID;
2728 }
2729 goto error_snapshot;
2730 }
2731
2732 rcu_read_lock();
2733 snapshot_add_output(&session->snapshot, new_output);
2734 rcu_read_unlock();
2735
2736 end:
2737 return LTTNG_OK;
2738
2739 error_snapshot:
2740 snapshot_output_destroy(new_output);
2741 error_snapshot_alloc:
2742 session_destroy(session);
2743 error:
2744 return ret;
2745 }
2746
2747 /*
2748 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2749 *
2750 * Called with session lock held.
2751 */
2752 int cmd_destroy_session(struct ltt_session *session, int wpipe)
2753 {
2754 int ret;
2755 struct ltt_ust_session *usess;
2756 struct ltt_kernel_session *ksess;
2757
2758 /* Safety net */
2759 assert(session);
2760
2761 usess = session->ust_session;
2762 ksess = session->kernel_session;
2763
2764 if (session->rotate_count > 0) {
2765 session->rotate_count++;
2766 /*
2767 * The currently active tracing path is now the folder we
2768 * want to rename.
2769 */
2770 snprintf(session->rotation_chunk.current_rotate_path,
2771 PATH_MAX, "%s",
2772 session->rotation_chunk.active_tracing_path);
2773 ret = rename_complete_chunk(session,
2774 session->session_last_stop_ts);
2775 if (ret < 0) {
2776 ERR("Renaming session on destroy");
2777 }
2778 }
2779
2780 if (session->rotate_relay_pending_timer_enabled) {
2781 sessiond_timer_rotate_pending_stop(session);
2782 }
2783
2784 if (session->rotate_timer_enabled) {
2785 sessiond_rotate_timer_stop(session);
2786 }
2787
2788 /* Clean kernel session teardown */
2789 kernel_destroy_session(ksess);
2790
2791 /* UST session teardown */
2792 if (usess) {
2793 /* Close any relayd session */
2794 consumer_output_send_destroy_relayd(usess->consumer);
2795
2796 /* Destroy every UST application related to this session. */
2797 ret = ust_app_destroy_trace_all(usess);
2798 if (ret) {
2799 ERR("Error in ust_app_destroy_trace_all");
2800 }
2801
2802 /* Clean up the rest. */
2803 trace_ust_destroy_session(usess);
2804 }
2805
2806 /*
2807 * Must notify the kernel thread here to update it's poll set in order to
2808 * remove the channel(s)' fd just destroyed.
2809 */
2810 ret = notify_thread_pipe(wpipe);
2811 if (ret < 0) {
2812 PERROR("write kernel poll pipe");
2813 }
2814
2815 ret = session_destroy(session);
2816
2817 return ret;
2818 }
2819
2820 /*
2821 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2822 */
2823 int cmd_register_consumer(struct ltt_session *session,
2824 enum lttng_domain_type domain, const char *sock_path,
2825 struct consumer_data *cdata)
2826 {
2827 int ret, sock;
2828 struct consumer_socket *socket = NULL;
2829
2830 assert(session);
2831 assert(cdata);
2832 assert(sock_path);
2833
2834 switch (domain) {
2835 case LTTNG_DOMAIN_KERNEL:
2836 {
2837 struct ltt_kernel_session *ksess = session->kernel_session;
2838
2839 assert(ksess);
2840
2841 /* Can't register a consumer if there is already one */
2842 if (ksess->consumer_fds_sent != 0) {
2843 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2844 goto error;
2845 }
2846
2847 sock = lttcomm_connect_unix_sock(sock_path);
2848 if (sock < 0) {
2849 ret = LTTNG_ERR_CONNECT_FAIL;
2850 goto error;
2851 }
2852 cdata->cmd_sock = sock;
2853
2854 socket = consumer_allocate_socket(&cdata->cmd_sock);
2855 if (socket == NULL) {
2856 ret = close(sock);
2857 if (ret < 0) {
2858 PERROR("close register consumer");
2859 }
2860 cdata->cmd_sock = -1;
2861 ret = LTTNG_ERR_FATAL;
2862 goto error;
2863 }
2864
2865 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2866 if (socket->lock == NULL) {
2867 PERROR("zmalloc pthread mutex");
2868 ret = LTTNG_ERR_FATAL;
2869 goto error;
2870 }
2871 pthread_mutex_init(socket->lock, NULL);
2872 socket->registered = 1;
2873
2874 rcu_read_lock();
2875 consumer_add_socket(socket, ksess->consumer);
2876 rcu_read_unlock();
2877
2878 pthread_mutex_lock(&cdata->pid_mutex);
2879 cdata->pid = -1;
2880 pthread_mutex_unlock(&cdata->pid_mutex);
2881
2882 break;
2883 }
2884 default:
2885 /* TODO: Userspace tracing */
2886 ret = LTTNG_ERR_UND;
2887 goto error;
2888 }
2889
2890 return LTTNG_OK;
2891
2892 error:
2893 if (socket) {
2894 consumer_destroy_socket(socket);
2895 }
2896 return ret;
2897 }
2898
2899 /*
2900 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2901 */
2902 ssize_t cmd_list_domains(struct ltt_session *session,
2903 struct lttng_domain **domains)
2904 {
2905 int ret, index = 0;
2906 ssize_t nb_dom = 0;
2907 struct agent *agt;
2908 struct lttng_ht_iter iter;
2909
2910 if (session->kernel_session != NULL) {
2911 DBG3("Listing domains found kernel domain");
2912 nb_dom++;
2913 }
2914
2915 if (session->ust_session != NULL) {
2916 DBG3("Listing domains found UST global domain");
2917 nb_dom++;
2918
2919 rcu_read_lock();
2920 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2921 agt, node.node) {
2922 if (agt->being_used) {
2923 nb_dom++;
2924 }
2925 }
2926 rcu_read_unlock();
2927 }
2928
2929 if (!nb_dom) {
2930 goto end;
2931 }
2932
2933 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2934 if (*domains == NULL) {
2935 ret = LTTNG_ERR_FATAL;
2936 goto error;
2937 }
2938
2939 if (session->kernel_session != NULL) {
2940 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
2941
2942 /* Kernel session buffer type is always GLOBAL */
2943 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
2944
2945 index++;
2946 }
2947
2948 if (session->ust_session != NULL) {
2949 (*domains)[index].type = LTTNG_DOMAIN_UST;
2950 (*domains)[index].buf_type = session->ust_session->buffer_type;
2951 index++;
2952
2953 rcu_read_lock();
2954 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2955 agt, node.node) {
2956 if (agt->being_used) {
2957 (*domains)[index].type = agt->domain;
2958 (*domains)[index].buf_type = session->ust_session->buffer_type;
2959 index++;
2960 }
2961 }
2962 rcu_read_unlock();
2963 }
2964 end:
2965 return nb_dom;
2966
2967 error:
2968 /* Return negative value to differentiate return code */
2969 return -ret;
2970 }
2971
2972
2973 /*
2974 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2975 */
2976 ssize_t cmd_list_channels(enum lttng_domain_type domain,
2977 struct ltt_session *session, struct lttng_channel **channels)
2978 {
2979 ssize_t nb_chan = 0, payload_size = 0, ret;
2980
2981 switch (domain) {
2982 case LTTNG_DOMAIN_KERNEL:
2983 if (session->kernel_session != NULL) {
2984 nb_chan = session->kernel_session->channel_count;
2985 }
2986 DBG3("Number of kernel channels %zd", nb_chan);
2987 if (nb_chan <= 0) {
2988 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2989 goto end;
2990 }
2991 break;
2992 case LTTNG_DOMAIN_UST:
2993 if (session->ust_session != NULL) {
2994 rcu_read_lock();
2995 nb_chan = lttng_ht_get_count(
2996 session->ust_session->domain_global.channels);
2997 rcu_read_unlock();
2998 }
2999 DBG3("Number of UST global channels %zd", nb_chan);
3000 if (nb_chan < 0) {
3001 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
3002 goto end;
3003 }
3004 break;
3005 default:
3006 ret = -LTTNG_ERR_UND;
3007 goto end;
3008 }
3009
3010 if (nb_chan > 0) {
3011 const size_t channel_size = sizeof(struct lttng_channel) +
3012 sizeof(struct lttng_channel_extended);
3013 struct lttng_channel_extended *channel_exts;
3014
3015 payload_size = nb_chan * channel_size;
3016 *channels = zmalloc(payload_size);
3017 if (*channels == NULL) {
3018 ret = -LTTNG_ERR_FATAL;
3019 goto end;
3020 }
3021
3022 channel_exts = ((void *) *channels) +
3023 (nb_chan * sizeof(struct lttng_channel));
3024 ret = list_lttng_channels(domain, session, *channels, channel_exts);
3025 if (ret != LTTNG_OK) {
3026 free(*channels);
3027 *channels = NULL;
3028 goto end;
3029 }
3030 } else {
3031 *channels = NULL;
3032 }
3033
3034 ret = payload_size;
3035 end:
3036 return ret;
3037 }
3038
3039 /*
3040 * Command LTTNG_LIST_EVENTS processed by the client thread.
3041 */
3042 ssize_t cmd_list_events(enum lttng_domain_type domain,
3043 struct ltt_session *session, char *channel_name,
3044 struct lttng_event **events, size_t *total_size)
3045 {
3046 int ret = 0;
3047 ssize_t nb_event = 0;
3048
3049 switch (domain) {
3050 case LTTNG_DOMAIN_KERNEL:
3051 if (session->kernel_session != NULL) {
3052 nb_event = list_lttng_kernel_events(channel_name,
3053 session->kernel_session, events,
3054 total_size);
3055 }
3056 break;
3057 case LTTNG_DOMAIN_UST:
3058 {
3059 if (session->ust_session != NULL) {
3060 nb_event = list_lttng_ust_global_events(channel_name,
3061 &session->ust_session->domain_global, events,
3062 total_size);
3063 }
3064 break;
3065 }
3066 case LTTNG_DOMAIN_LOG4J:
3067 case LTTNG_DOMAIN_JUL:
3068 case LTTNG_DOMAIN_PYTHON:
3069 if (session->ust_session) {
3070 struct lttng_ht_iter iter;
3071 struct agent *agt;
3072
3073 rcu_read_lock();
3074 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3075 &iter.iter, agt, node.node) {
3076 if (agt->domain == domain) {
3077 nb_event = list_lttng_agent_events(
3078 agt, events,
3079 total_size);
3080 break;
3081 }
3082 }
3083 rcu_read_unlock();
3084 }
3085 break;
3086 default:
3087 ret = LTTNG_ERR_UND;
3088 goto error;
3089 }
3090
3091 return nb_event;
3092
3093 error:
3094 /* Return negative value to differentiate return code */
3095 return -ret;
3096 }
3097
3098 /*
3099 * Using the session list, filled a lttng_session array to send back to the
3100 * client for session listing.
3101 *
3102 * The session list lock MUST be acquired before calling this function. Use
3103 * session_lock_list() and session_unlock_list().
3104 */
3105 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
3106 gid_t gid)
3107 {
3108 int ret;
3109 unsigned int i = 0;
3110 struct ltt_session *session;
3111 struct ltt_session_list *list = session_get_list();
3112
3113 DBG("Getting all available session for UID %d GID %d",
3114 uid, gid);
3115 /*
3116 * Iterate over session list and append data after the control struct in
3117 * the buffer.
3118 */
3119 cds_list_for_each_entry(session, &list->head, list) {
3120 /*
3121 * Only list the sessions the user can control.
3122 */
3123 if (!session_access_ok(session, uid, gid)) {
3124 continue;
3125 }
3126
3127 struct ltt_kernel_session *ksess = session->kernel_session;
3128 struct ltt_ust_session *usess = session->ust_session;
3129
3130 if (session->consumer->type == CONSUMER_DST_NET ||
3131 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3132 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3133 ret = build_network_session_path(sessions[i].path,
3134 sizeof(sessions[i].path), session);
3135 } else {
3136 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
3137 session->consumer->dst.session_root_path);
3138 }
3139 if (ret < 0) {
3140 PERROR("snprintf session path");
3141 continue;
3142 }
3143
3144 strncpy(sessions[i].name, session->name, NAME_MAX);
3145 sessions[i].name[NAME_MAX - 1] = '\0';
3146 sessions[i].enabled = session->active;
3147 sessions[i].snapshot_mode = session->snapshot_mode;
3148 sessions[i].live_timer_interval = session->live_timer;
3149 i++;
3150 }
3151 }
3152
3153 /*
3154 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3155 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3156 */
3157 int cmd_data_pending(struct ltt_session *session)
3158 {
3159 int ret;
3160 struct ltt_kernel_session *ksess = session->kernel_session;
3161 struct ltt_ust_session *usess = session->ust_session;
3162
3163 assert(session);
3164
3165 /* Session MUST be stopped to ask for data availability. */
3166 if (session->active) {
3167 ret = LTTNG_ERR_SESSION_STARTED;
3168 goto error;
3169 } else {
3170 /*
3171 * If stopped, just make sure we've started before else the above call
3172 * will always send that there is data pending.
3173 *
3174 * The consumer assumes that when the data pending command is received,
3175 * the trace has been started before or else no output data is written
3176 * by the streams which is a condition for data pending. So, this is
3177 * *VERY* important that we don't ask the consumer before a start
3178 * trace.
3179 */
3180 if (!session->has_been_started) {
3181 ret = 0;
3182 goto error;
3183 }
3184 }
3185
3186 /*
3187 * A rotation is still pending, we have to wait.
3188 */
3189 if (session->rotate_pending) {
3190 ret = 1;
3191 goto error;
3192 }
3193
3194 if (ksess && ksess->consumer) {
3195 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3196 if (ret == 1) {
3197 /* Data is still being extracted for the kernel. */
3198 goto error;
3199 }
3200 }
3201
3202 if (usess && usess->consumer) {
3203 ret = consumer_is_data_pending(usess->id, usess->consumer);
3204 if (ret == 1) {
3205 /* Data is still being extracted for the kernel. */
3206 goto error;
3207 }
3208 }
3209
3210 /* Data is ready to be read by a viewer */
3211 ret = 0;
3212
3213 error:
3214 return ret;
3215 }
3216
3217 /*
3218 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3219 *
3220 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3221 */
3222 int cmd_snapshot_add_output(struct ltt_session *session,
3223 struct lttng_snapshot_output *output, uint32_t *id)
3224 {
3225 int ret;
3226 struct snapshot_output *new_output;
3227
3228 assert(session);
3229 assert(output);
3230
3231 DBG("Cmd snapshot add output for session %s", session->name);
3232
3233 /*
3234 * Can't create an output if the session is not set in no-output mode.
3235 */
3236 if (session->output_traces) {
3237 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3238 goto error;
3239 }
3240
3241 /* Only one output is allowed until we have the "tee" feature. */
3242 if (session->snapshot.nb_output == 1) {
3243 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3244 goto error;
3245 }
3246
3247 new_output = snapshot_output_alloc();
3248 if (!new_output) {
3249 ret = LTTNG_ERR_NOMEM;
3250 goto error;
3251 }
3252
3253 ret = snapshot_output_init(output->max_size, output->name,
3254 output->ctrl_url, output->data_url, session->consumer, new_output,
3255 &session->snapshot);
3256 if (ret < 0) {
3257 if (ret == -ENOMEM) {
3258 ret = LTTNG_ERR_NOMEM;
3259 } else {
3260 ret = LTTNG_ERR_INVALID;
3261 }
3262 goto free_error;
3263 }
3264
3265 rcu_read_lock();
3266 snapshot_add_output(&session->snapshot, new_output);
3267 if (id) {
3268 *id = new_output->id;
3269 }
3270 rcu_read_unlock();
3271
3272 return LTTNG_OK;
3273
3274 free_error:
3275 snapshot_output_destroy(new_output);
3276 error:
3277 return ret;
3278 }
3279
3280 /*
3281 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3282 *
3283 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3284 */
3285 int cmd_snapshot_del_output(struct ltt_session *session,
3286 struct lttng_snapshot_output *output)
3287 {
3288 int ret;
3289 struct snapshot_output *sout = NULL;
3290
3291 assert(session);
3292 assert(output);
3293
3294 rcu_read_lock();
3295
3296 /*
3297 * Permission denied to create an output if the session is not
3298 * set in no output mode.
3299 */
3300 if (session->output_traces) {
3301 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3302 goto error;
3303 }
3304
3305 if (output->id) {
3306 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3307 session->name);
3308 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3309 } else if (*output->name != '\0') {
3310 DBG("Cmd snapshot del output name %s for session %s", output->name,
3311 session->name);
3312 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3313 }
3314 if (!sout) {
3315 ret = LTTNG_ERR_INVALID;
3316 goto error;
3317 }
3318
3319 snapshot_delete_output(&session->snapshot, sout);
3320 snapshot_output_destroy(sout);
3321 ret = LTTNG_OK;
3322
3323 error:
3324 rcu_read_unlock();
3325 return ret;
3326 }
3327
3328 /*
3329 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3330 *
3331 * If no output is available, outputs is untouched and 0 is returned.
3332 *
3333 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3334 */
3335 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3336 struct lttng_snapshot_output **outputs)
3337 {
3338 int ret, idx = 0;
3339 struct lttng_snapshot_output *list = NULL;
3340 struct lttng_ht_iter iter;
3341 struct snapshot_output *output;
3342
3343 assert(session);
3344 assert(outputs);
3345
3346 DBG("Cmd snapshot list outputs for session %s", session->name);
3347
3348 /*
3349 * Permission denied to create an output if the session is not
3350 * set in no output mode.
3351 */
3352 if (session->output_traces) {
3353 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3354 goto end;
3355 }
3356
3357 if (session->snapshot.nb_output == 0) {
3358 ret = 0;
3359 goto end;
3360 }
3361
3362 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
3363 if (!list) {
3364 ret = -LTTNG_ERR_NOMEM;
3365 goto end;
3366 }
3367
3368 /* Copy list from session to the new list object. */
3369 rcu_read_lock();
3370 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
3371 output, node.node) {
3372 assert(output->consumer);
3373 list[idx].id = output->id;
3374 list[idx].max_size = output->max_size;
3375 if (lttng_strncpy(list[idx].name, output->name,
3376 sizeof(list[idx].name))) {
3377 ret = -LTTNG_ERR_INVALID;
3378 goto error;
3379 }
3380 if (output->consumer->type == CONSUMER_DST_LOCAL) {
3381 if (lttng_strncpy(list[idx].ctrl_url,
3382 output->consumer->dst.session_root_path,
3383 sizeof(list[idx].ctrl_url))) {
3384 ret = -LTTNG_ERR_INVALID;
3385 goto error;
3386 }
3387 } else {
3388 /* Control URI. */
3389 ret = uri_to_str_url(&output->consumer->dst.net.control,
3390 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
3391 if (ret < 0) {
3392 ret = -LTTNG_ERR_NOMEM;
3393 goto error;
3394 }
3395
3396 /* Data URI. */
3397 ret = uri_to_str_url(&output->consumer->dst.net.data,
3398 list[idx].data_url, sizeof(list[idx].data_url));
3399 if (ret < 0) {
3400 ret = -LTTNG_ERR_NOMEM;
3401 goto error;
3402 }
3403 }
3404 idx++;
3405 }
3406
3407 *outputs = list;
3408 list = NULL;
3409 ret = session->snapshot.nb_output;
3410 error:
3411 rcu_read_unlock();
3412 free(list);
3413 end:
3414 return ret;
3415 }
3416
3417 /*
3418 * Check if we can regenerate the metadata for this session.
3419 * Only kernel, UST per-uid and non-live sessions are supported.
3420 *
3421 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3422 */
3423 static
3424 int check_regenerate_metadata_support(struct ltt_session *session)
3425 {
3426 int ret;
3427
3428 assert(session);
3429
3430 if (session->live_timer != 0) {
3431 ret = LTTNG_ERR_LIVE_SESSION;
3432 goto end;
3433 }
3434 if (!session->active) {
3435 ret = LTTNG_ERR_SESSION_NOT_STARTED;
3436 goto end;
3437 }
3438 if (session->ust_session) {
3439 switch (session->ust_session->buffer_type) {
3440 case LTTNG_BUFFER_PER_UID:
3441 break;
3442 case LTTNG_BUFFER_PER_PID:
3443 ret = LTTNG_ERR_PER_PID_SESSION;
3444 goto end;
3445 default:
3446 assert(0);
3447 ret = LTTNG_ERR_UNK;
3448 goto end;
3449 }
3450 }
3451 if (session->consumer->type == CONSUMER_DST_NET &&
3452 session->consumer->relay_minor_version < 8) {
3453 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
3454 goto end;
3455 }
3456 ret = 0;
3457
3458 end:
3459 return ret;
3460 }
3461
3462 static
3463 int clear_metadata_file(int fd)
3464 {
3465 int ret;
3466
3467 ret = lseek(fd, 0, SEEK_SET);
3468 if (ret < 0) {
3469 PERROR("lseek");
3470 goto end;
3471 }
3472
3473 ret = ftruncate(fd, 0);
3474 if (ret < 0) {
3475 PERROR("ftruncate");
3476 goto end;
3477 }
3478
3479 end:
3480 return ret;
3481 }
3482
3483 static
3484 int ust_regenerate_metadata(struct ltt_ust_session *usess)
3485 {
3486 int ret = 0;
3487 struct buffer_reg_uid *uid_reg = NULL;
3488 struct buffer_reg_session *session_reg = NULL;
3489
3490 rcu_read_lock();
3491 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
3492 struct ust_registry_session *registry;
3493 struct ust_registry_channel *chan;
3494 struct lttng_ht_iter iter_chan;
3495
3496 session_reg = uid_reg->registry;
3497 registry = session_reg->reg.ust;
3498
3499 pthread_mutex_lock(&registry->lock);
3500 registry->metadata_len_sent = 0;
3501 memset(registry->metadata, 0, registry->metadata_alloc_len);
3502 registry->metadata_len = 0;
3503 registry->metadata_version++;
3504 if (registry->metadata_fd > 0) {
3505 /* Clear the metadata file's content. */
3506 ret = clear_metadata_file(registry->metadata_fd);
3507 if (ret) {
3508 pthread_mutex_unlock(&registry->lock);
3509 goto end;
3510 }
3511 }
3512
3513 ret = ust_metadata_session_statedump(registry, NULL,
3514 registry->major, registry->minor);
3515 if (ret) {
3516 pthread_mutex_unlock(&registry->lock);
3517 ERR("Failed to generate session metadata (err = %d)",
3518 ret);
3519 goto end;
3520 }
3521 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
3522 chan, node.node) {
3523 struct ust_registry_event *event;
3524 struct lttng_ht_iter iter_event;
3525
3526 ret = ust_metadata_channel_statedump(registry, chan);
3527 if (ret) {
3528 pthread_mutex_unlock(&registry->lock);
3529 ERR("Failed to generate channel metadata "
3530 "(err = %d)", ret);
3531 goto end;
3532 }
3533 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
3534 event, node.node) {
3535 ret = ust_metadata_event_statedump(registry,
3536 chan, event);
3537 if (ret) {
3538 pthread_mutex_unlock(&registry->lock);
3539 ERR("Failed to generate event metadata "
3540 "(err = %d)", ret);
3541 goto end;
3542 }
3543 }
3544 }
3545 pthread_mutex_unlock(&registry->lock);
3546 }
3547
3548 end:
3549 rcu_read_unlock();
3550 return ret;
3551 }
3552
3553 /*
3554 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3555 *
3556 * Ask the consumer to truncate the existing metadata file(s) and
3557 * then regenerate the metadata. Live and per-pid sessions are not
3558 * supported and return an error.
3559 *
3560 * Return 0 on success or else a LTTNG_ERR code.
3561 */
3562 int cmd_regenerate_metadata(struct ltt_session *session)
3563 {
3564 int ret;
3565
3566 assert(session);
3567
3568 ret = check_regenerate_metadata_support(session);
3569 if (ret) {
3570 goto end;
3571 }
3572
3573 if (session->kernel_session) {
3574 ret = kernctl_session_regenerate_metadata(
3575 session->kernel_session->fd);
3576 if (ret < 0) {
3577 ERR("Failed to regenerate the kernel metadata");
3578 goto end;
3579 }
3580 }
3581
3582 if (session->ust_session) {
3583 ret = ust_regenerate_metadata(session->ust_session);
3584 if (ret < 0) {
3585 ERR("Failed to regenerate the UST metadata");
3586 goto end;
3587 }
3588 }
3589 DBG("Cmd metadata regenerate for session %s", session->name);
3590 ret = LTTNG_OK;
3591
3592 end:
3593 return ret;
3594 }
3595
3596 /*
3597 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3598 *
3599 * Ask the tracer to regenerate a new statedump.
3600 *
3601 * Return 0 on success or else a LTTNG_ERR code.
3602 */
3603 int cmd_regenerate_statedump(struct ltt_session *session)
3604 {
3605 int ret;
3606
3607 assert(session);
3608
3609 if (!session->active) {
3610 ret = LTTNG_ERR_SESSION_NOT_STARTED;
3611 goto end;
3612 }
3613
3614 if (session->kernel_session) {
3615 ret = kernctl_session_regenerate_statedump(
3616 session->kernel_session->fd);
3617 /*
3618 * Currently, the statedump in kernel can only fail if out
3619 * of memory.
3620 */
3621 if (ret < 0) {
3622 if (ret == -ENOMEM) {
3623 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
3624 } else {
3625 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
3626 }
3627 ERR("Failed to regenerate the kernel statedump");
3628 goto end;
3629 }
3630 }
3631
3632 if (session->ust_session) {
3633 ret = ust_app_regenerate_statedump_all(session->ust_session);
3634 /*
3635 * Currently, the statedump in UST always returns 0.
3636 */
3637 if (ret < 0) {
3638 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
3639 ERR("Failed to regenerate the UST statedump");
3640 goto end;
3641 }
3642 }
3643 DBG("Cmd regenerate statedump for session %s", session->name);
3644 ret = LTTNG_OK;
3645
3646 end:
3647 return ret;
3648 }
3649
3650 int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
3651 struct notification_thread_handle *notification_thread)
3652 {
3653 int ret;
3654 size_t trigger_len;
3655 ssize_t sock_recv_len;
3656 struct lttng_trigger *trigger = NULL;
3657 struct lttng_buffer_view view;
3658 struct lttng_dynamic_buffer trigger_buffer;
3659
3660 lttng_dynamic_buffer_init(&trigger_buffer);
3661 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
3662 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
3663 if (ret) {
3664 ret = LTTNG_ERR_NOMEM;
3665 goto end;
3666 }
3667
3668 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
3669 trigger_len);
3670 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
3671 ERR("Failed to receive \"register trigger\" command payload");
3672 /* TODO: should this be a new error enum ? */
3673 ret = LTTNG_ERR_INVALID_TRIGGER;
3674 goto end;
3675 }
3676
3677 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
3678 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
3679 trigger_len) {
3680 ERR("Invalid trigger payload received in \"register trigger\" command");
3681 ret = LTTNG_ERR_INVALID_TRIGGER;
3682 goto end;
3683 }
3684
3685 ret = notification_thread_command_register_trigger(notification_thread,
3686 trigger);
3687 /* Ownership of trigger was transferred. */
3688 trigger = NULL;
3689 end:
3690 lttng_trigger_destroy(trigger);
3691 lttng_dynamic_buffer_reset(&trigger_buffer);
3692 return ret;
3693 }
3694
3695 int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock,
3696 struct notification_thread_handle *notification_thread)
3697 {
3698 int ret;
3699 size_t trigger_len;
3700 ssize_t sock_recv_len;
3701 struct lttng_trigger *trigger = NULL;
3702 struct lttng_buffer_view view;
3703 struct lttng_dynamic_buffer trigger_buffer;
3704
3705 lttng_dynamic_buffer_init(&trigger_buffer);
3706 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
3707 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
3708 if (ret) {
3709 ret = LTTNG_ERR_NOMEM;
3710 goto end;
3711 }
3712
3713 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
3714 trigger_len);
3715 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
3716 ERR("Failed to receive \"unregister trigger\" command payload");
3717 /* TODO: should this be a new error enum ? */
3718 ret = LTTNG_ERR_INVALID_TRIGGER;
3719 goto end;
3720 }
3721
3722 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
3723 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
3724 trigger_len) {
3725 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3726 ret = LTTNG_ERR_INVALID_TRIGGER;
3727 goto end;
3728 }
3729
3730 ret = notification_thread_command_unregister_trigger(notification_thread,
3731 trigger);
3732 end:
3733 lttng_trigger_destroy(trigger);
3734 lttng_dynamic_buffer_reset(&trigger_buffer);
3735 return ret;
3736 }
3737
3738 /*
3739 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3740 * snapshot output is *not* set with a remote destination.
3741 *
3742 * Return 0 on success or a LTTNG_ERR code.
3743 */
3744 static int set_relayd_for_snapshot(struct consumer_output *consumer,
3745 struct snapshot_output *snap_output, struct ltt_session *session)
3746 {
3747 int ret = LTTNG_OK;
3748 struct lttng_ht_iter iter;
3749 struct consumer_socket *socket;
3750
3751 assert(consumer);
3752 assert(snap_output);
3753 assert(session);
3754
3755 DBG2("Set relayd object from snapshot output");
3756
3757 /* Ignore if snapshot consumer output is not network. */
3758 if (snap_output->consumer->type != CONSUMER_DST_NET) {
3759 goto error;
3760 }
3761
3762 /*
3763 * For each consumer socket, create and send the relayd object of the
3764 * snapshot output.
3765 */
3766 rcu_read_lock();
3767 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
3768 socket, node.node) {
3769 ret = send_consumer_relayd_sockets(0, session->id,
3770 snap_output->consumer, socket,
3771 session->name, session->hostname,
3772 session->live_timer);
3773 if (ret != LTTNG_OK) {
3774 rcu_read_unlock();
3775 goto error;
3776 }
3777 }
3778 rcu_read_unlock();
3779
3780 error:
3781 return ret;
3782 }
3783
3784 /*
3785 * Record a kernel snapshot.
3786 *
3787 * Return LTTNG_OK on success or a LTTNG_ERR code.
3788 */
3789 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
3790 struct snapshot_output *output, struct ltt_session *session,
3791 int wait, uint64_t nb_packets_per_stream)
3792 {
3793 int ret;
3794
3795 assert(ksess);
3796 assert(output);
3797 assert(session);
3798
3799
3800 /*
3801 * Copy kernel session sockets so we can communicate with the right
3802 * consumer for the snapshot record command.
3803 */
3804 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
3805 if (ret < 0) {
3806 ret = LTTNG_ERR_NOMEM;
3807 goto error;
3808 }
3809
3810 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
3811 if (ret != LTTNG_OK) {
3812 goto error_snapshot;
3813 }
3814
3815 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
3816 if (ret != LTTNG_OK) {
3817 goto error_snapshot;
3818 }
3819
3820 ret = LTTNG_OK;
3821 goto end;
3822
3823 error_snapshot:
3824 /* Clean up copied sockets so this output can use some other later on. */
3825 consumer_destroy_output_sockets(output->consumer);
3826 error:
3827 end:
3828 return ret;
3829 }
3830
3831 /*
3832 * Record a UST snapshot.
3833 *
3834 * Return 0 on success or a LTTNG_ERR error code.
3835 */
3836 static int record_ust_snapshot(struct ltt_ust_session *usess,
3837 struct snapshot_output *output, struct ltt_session *session,
3838 int wait, uint64_t nb_packets_per_stream)
3839 {
3840 int ret;
3841
3842 assert(usess);
3843 assert(output);
3844 assert(session);
3845
3846 /*
3847 * Copy UST session sockets so we can communicate with the right
3848 * consumer for the snapshot record command.
3849 */
3850 ret = consumer_copy_sockets(output->consumer, usess->consumer);
3851 if (ret < 0) {
3852 ret = LTTNG_ERR_NOMEM;
3853 goto error;
3854 }
3855
3856 ret = set_relayd_for_snapshot(usess->consumer, output, session);
3857 if (ret != LTTNG_OK) {
3858 goto error_snapshot;
3859 }
3860
3861 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
3862 if (ret < 0) {
3863 switch (-ret) {
3864 case EINVAL:
3865 ret = LTTNG_ERR_INVALID;
3866 break;
3867 default:
3868 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3869 break;
3870 }
3871 goto error_snapshot;
3872 }
3873
3874 ret = LTTNG_OK;
3875
3876 error_snapshot:
3877 /* Clean up copied sockets so this output can use some other later on. */
3878 consumer_destroy_output_sockets(output->consumer);
3879 error:
3880 return ret;
3881 }
3882
3883 static
3884 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
3885 uint64_t cur_nr_packets)
3886 {
3887 uint64_t tot_size = 0;
3888
3889 if (session->kernel_session) {
3890 struct ltt_kernel_channel *chan;
3891 struct ltt_kernel_session *ksess = session->kernel_session;
3892
3893 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
3894 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
3895 /*
3896 * Don't take channel into account if we
3897 * already grab all its packets.
3898 */
3899 continue;
3900 }
3901 tot_size += chan->channel->attr.subbuf_size
3902 * chan->stream_count;
3903 }
3904 }
3905
3906 if (session->ust_session) {
3907 struct ltt_ust_session *usess = session->ust_session;
3908
3909 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
3910 cur_nr_packets);
3911 }
3912
3913 return tot_size;
3914 }
3915
3916 /*
3917 * Calculate the number of packets we can grab from each stream that
3918 * fits within the overall snapshot max size.
3919 *
3920 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3921 * the number of packets per stream.
3922 *
3923 * TODO: this approach is not perfect: we consider the worse case
3924 * (packet filling the sub-buffers) as an upper bound, but we could do
3925 * better if we do this calculation while we actually grab the packet
3926 * content: we would know how much padding we don't actually store into
3927 * the file.
3928 *
3929 * This algorithm is currently bounded by the number of packets per
3930 * stream.
3931 *
3932 * Since we call this algorithm before actually grabbing the data, it's
3933 * an approximation: for instance, applications could appear/disappear
3934 * in between this call and actually grabbing data.
3935 */
3936 static
3937 int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
3938 {
3939 int64_t size_left;
3940 uint64_t cur_nb_packets = 0;
3941
3942 if (!max_size) {
3943 return 0; /* Infinite */
3944 }
3945
3946 size_left = max_size;
3947 for (;;) {
3948 uint64_t one_more_packet_tot_size;
3949
3950 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3951 cur_nb_packets);
3952 if (!one_more_packet_tot_size) {
3953 /* We are already grabbing all packets. */
3954 break;
3955 }
3956 size_left -= one_more_packet_tot_size;
3957 if (size_left < 0) {
3958 break;
3959 }
3960 cur_nb_packets++;
3961 }
3962 if (!cur_nb_packets) {
3963 /* Not enough room to grab one packet of each stream, error. */
3964 return -1;
3965 }
3966 return cur_nb_packets;
3967 }
3968
3969 /*
3970 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3971 *
3972 * The wait parameter is ignored so this call always wait for the snapshot to
3973 * complete before returning.
3974 *
3975 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3976 */
3977 int cmd_snapshot_record(struct ltt_session *session,
3978 struct lttng_snapshot_output *output, int wait)
3979 {
3980 int ret = LTTNG_OK;
3981 unsigned int use_tmp_output = 0;
3982 struct snapshot_output tmp_output;
3983 unsigned int snapshot_success = 0;
3984 char datetime[16];
3985
3986 assert(session);
3987 assert(output);
3988
3989 DBG("Cmd snapshot record for session %s", session->name);
3990
3991 /* Get the datetime for the snapshot output directory. */
3992 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
3993 sizeof(datetime));
3994 if (!ret) {
3995 ret = LTTNG_ERR_INVALID;
3996 goto error;
3997 }
3998
3999 /*
4000 * Permission denied to create an output if the session is not
4001 * set in no output mode.
4002 */
4003 if (session->output_traces) {
4004 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4005 goto error;
4006 }
4007
4008 /* The session needs to be started at least once. */
4009 if (!session->has_been_started) {
4010 ret = LTTNG_ERR_START_SESSION_ONCE;
4011 goto error;
4012 }
4013
4014 /* Use temporary output for the session. */
4015 if (*output->ctrl_url != '\0') {
4016 ret = snapshot_output_init(output->max_size, output->name,
4017 output->ctrl_url, output->data_url, session->consumer,
4018 &tmp_output, NULL);
4019 if (ret < 0) {
4020 if (ret == -ENOMEM) {
4021 ret = LTTNG_ERR_NOMEM;
4022 } else {
4023 ret = LTTNG_ERR_INVALID;
4024 }
4025 goto error;
4026 }
4027 /* Use the global session count for the temporary snapshot. */
4028 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
4029
4030 /* Use the global datetime */
4031 memcpy(tmp_output.datetime, datetime, sizeof(datetime));
4032 use_tmp_output = 1;
4033 }
4034
4035 if (use_tmp_output) {
4036 int64_t nb_packets_per_stream;
4037
4038 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
4039 tmp_output.max_size);
4040 if (nb_packets_per_stream < 0) {
4041 ret = LTTNG_ERR_MAX_SIZE_INVALID;
4042 goto error;
4043 }
4044
4045 if (session->kernel_session) {
4046 ret = record_kernel_snapshot(session->kernel_session,
4047 &tmp_output, session,
4048 wait, nb_packets_per_stream);
4049 if (ret != LTTNG_OK) {
4050 goto error;
4051 }
4052 }
4053
4054 if (session->ust_session) {
4055 ret = record_ust_snapshot(session->ust_session,
4056 &tmp_output, session,
4057 wait, nb_packets_per_stream);
4058 if (ret != LTTNG_OK) {
4059 goto error;
4060 }
4061 }
4062
4063 snapshot_success = 1;
4064 } else {
4065 struct snapshot_output *sout;
4066 struct lttng_ht_iter iter;
4067
4068 rcu_read_lock();
4069 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
4070 &iter.iter, sout, node.node) {
4071 int64_t nb_packets_per_stream;
4072
4073 /*
4074 * Make a local copy of the output and assign the possible
4075 * temporary value given by the caller.
4076 */
4077 memset(&tmp_output, 0, sizeof(tmp_output));
4078 memcpy(&tmp_output, sout, sizeof(tmp_output));
4079
4080 if (output->max_size != (uint64_t) -1ULL) {
4081 tmp_output.max_size = output->max_size;
4082 }
4083
4084 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
4085 tmp_output.max_size);
4086 if (nb_packets_per_stream < 0) {
4087 ret = LTTNG_ERR_MAX_SIZE_INVALID;
4088 rcu_read_unlock();
4089 goto error;
4090 }
4091
4092 /* Use temporary name. */
4093 if (*output->name != '\0') {
4094 if (lttng_strncpy(tmp_output.name, output->name,
4095 sizeof(tmp_output.name))) {
4096 ret = LTTNG_ERR_INVALID;
4097 rcu_read_unlock();
4098 goto error;
4099 }
4100 }
4101
4102 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
4103 memcpy(tmp_output.datetime, datetime, sizeof(datetime));
4104
4105 if (session->kernel_session) {
4106 ret = record_kernel_snapshot(session->kernel_session,
4107 &tmp_output, session,
4108 wait, nb_packets_per_stream);
4109 if (ret != LTTNG_OK) {
4110 rcu_read_unlock();
4111 goto error;
4112 }
4113 }
4114
4115 if (session->ust_session) {
4116 ret = record_ust_snapshot(session->ust_session,
4117 &tmp_output, session,
4118 wait, nb_packets_per_stream);
4119 if (ret != LTTNG_OK) {
4120 rcu_read_unlock();
4121 goto error;
4122 }
4123 }
4124 snapshot_success = 1;
4125 }
4126 rcu_read_unlock();
4127 }
4128
4129 if (snapshot_success) {
4130 session->snapshot.nb_snapshot++;
4131 } else {
4132 ret = LTTNG_ERR_SNAPSHOT_FAIL;
4133 }
4134
4135 error:
4136 return ret;
4137 }
4138
4139 /*
4140 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4141 */
4142 int cmd_set_session_shm_path(struct ltt_session *session,
4143 const char *shm_path)
4144 {
4145 /* Safety net */
4146 assert(session);
4147
4148 /*
4149 * Can only set shm path before session is started.
4150 */
4151 if (session->has_been_started) {
4152 return LTTNG_ERR_SESSION_STARTED;
4153 }
4154
4155 strncpy(session->shm_path, shm_path,
4156 sizeof(session->shm_path));
4157 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
4158
4159 return 0;
4160 }
4161
4162 static
4163 const char *get_base_path(struct ltt_session *session,
4164 struct consumer_output *consumer)
4165 {
4166 if (session->net_handle > 0) {
4167 return consumer->dst.net.base_dir;
4168 } else {
4169 return consumer->dst.session_root_path;
4170 }
4171 }
4172
4173 /*
4174 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4175 *
4176 * Ask the consumer to rotate the session output directory.
4177 *
4178 * Return 0 on success or else a LTTNG_ERR code.
4179 */
4180 int cmd_rotate_session(struct ltt_session *session,
4181 struct lttng_rotate_session_return **rotate_return)
4182 {
4183 int ret;
4184 struct tm *timeinfo;
4185 char datetime[16];
4186 time_t now;
4187
4188 assert(session);
4189
4190 if (rotate_return) {
4191 *rotate_return = zmalloc(sizeof(struct lttng_rotate_session_return));
4192 if (!*rotate_return) {
4193 ret = -ENOMEM;
4194 goto end;
4195 }
4196 }
4197
4198 if (session->live_timer || session->snapshot_mode ||
4199 !session->output_traces) {
4200 ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
4201 goto error;
4202 }
4203
4204 if (session->rotate_pending || session->rotate_pending_relay) {
4205 ret = -LTTNG_ERR_ROTATE_PENDING;
4206 goto error;
4207 }
4208
4209 /* Special case for the first rotation. */
4210 if (session->rotate_count == 0) {
4211 const char *base_path = NULL;
4212
4213 /* Either one of the two sessions is enough to get the root path. */
4214 if (session->kernel_session) {
4215 base_path = get_base_path(session, session->kernel_session->consumer);
4216 } else if (session->ust_session) {
4217 base_path = get_base_path(session, session->ust_session->consumer);
4218 } else {
4219 assert(0);
4220 }
4221 assert(base_path);
4222 snprintf(session->rotation_chunk.current_rotate_path,
4223 PATH_MAX, "%s",
4224 base_path);
4225 fprintf(stderr, "b: %s\n", base_path);
4226 } else {
4227 /*
4228 * The currently active tracing path is now the folder we
4229 * want to rotate.
4230 */
4231 snprintf(session->rotation_chunk.current_rotate_path,
4232 PATH_MAX, "%s",
4233 session->rotation_chunk.active_tracing_path);
4234 }
4235
4236 session->rotate_count++;
4237 session->rotate_pending = true;
4238 session->rotate_pending_relay_check_in_progress = false;
4239 session->rotate_status = LTTNG_ROTATE_STARTED;
4240
4241 /*
4242 * Create the path name for the next chunk.
4243 */
4244 now = time(NULL);
4245 if (now == (time_t) -1) {
4246 ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
4247 goto error;
4248 }
4249 session->last_chunk_start_ts = session->current_chunk_start_ts;
4250 session->current_chunk_start_ts = now;
4251
4252 timeinfo = localtime(&now);
4253 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
4254 if (session->kernel_session) {
4255 /* The active path for the next rotation/destroy. */
4256 snprintf(session->rotation_chunk.active_tracing_path,
4257 PATH_MAX, "%s/%s-",
4258 get_base_path(session, session->kernel_session->consumer),
4259 datetime);
4260 /* The sub-directory for the consumer. */
4261 snprintf(session->kernel_session->consumer->chunk_path,
4262 PATH_MAX, "/%s-%s", datetime,
4263 session->kernel_session->consumer->subdir);
4264 ret = kernel_rotate_session(session);
4265 if (ret != LTTNG_OK) {
4266 goto error;
4267 }
4268 }
4269 if (session->ust_session) {
4270 snprintf(session->rotation_chunk.active_tracing_path,
4271 PATH_MAX, "%s/%s-",
4272 get_base_path(session, session->ust_session->consumer),
4273 datetime);
4274 snprintf(session->ust_session->consumer->chunk_path,
4275 PATH_MAX, "/%s-", datetime);
4276 ret = ust_app_rotate_session(session);
4277 if (ret != LTTNG_OK) {
4278 goto error;
4279 }
4280 }
4281
4282 if (rotate_return) {
4283 (*rotate_return)->rotate_id = session->rotate_count;
4284 (*rotate_return)->status = LTTNG_ROTATE_STARTED;
4285 }
4286
4287 DBG("Cmd rotate session %s, rotate_id %" PRIu64, session->name,
4288 session->rotate_count);
4289 ret = LTTNG_OK;
4290
4291 goto end;
4292
4293 error:
4294 if (rotate_return) {
4295 (*rotate_return)->status = LTTNG_ROTATE_ERROR;
4296 }
4297 end:
4298 return ret;
4299 }
4300
4301 /*
4302 * Command LTTNG_ROTATE_PENDING from the lttng-ctl library.
4303 *
4304 * Check if the session has finished its rotation.
4305 *
4306 * Return 0 on success or else a LTTNG_ERR code.
4307 */
4308 int cmd_rotate_pending(struct ltt_session *session,
4309 struct lttng_rotate_pending_return **pending_return,
4310 uint64_t rotate_id)
4311 {
4312 int ret;
4313
4314 assert(session);
4315
4316 DBG("Cmd rotate pending session %s, rotate_id %" PRIu64, session->name,
4317 session->rotate_count);
4318
4319 *pending_return = zmalloc(sizeof(struct lttng_rotate_pending_return));
4320 if (!*pending_return) {
4321 ret = -ENOMEM;
4322 goto end;
4323 }
4324
4325 if (session->rotate_count != rotate_id) {
4326 (*pending_return)->status = LTTNG_ROTATE_EXPIRED;
4327 ret = LTTNG_OK;
4328 goto end;
4329 }
4330
4331 if (session->rotate_status == LTTNG_ROTATE_ERROR) {
4332 DBG("An error occurred during rotation");
4333 (*pending_return)->status = LTTNG_ROTATE_ERROR;
4334 } else if (session->rotate_status == LTTNG_ROTATE_EMPTY) {
4335 DBG("Nothing to rotate");
4336 (*pending_return)->status = LTTNG_ROTATE_EMPTY;
4337 /* Rotate with a relay */
4338 } else if (session->rotate_pending_relay) {
4339 DBG("Session %s, rotate_id %" PRIu64 " still pending",
4340 session->name, session->rotate_count);
4341 (*pending_return)->status = LTTNG_ROTATE_STARTED;
4342 } else if (session->rotate_pending) {
4343 DBG("Session %s, rotate_id %" PRIu64 " still pending",
4344 session->name, session->rotate_count);
4345 (*pending_return)->status = LTTNG_ROTATE_STARTED;
4346 } else {
4347 DBG("Session %s, rotate_id %" PRIu64 " finished",
4348 session->name, session->rotate_count);
4349 (*pending_return)->status = LTTNG_ROTATE_COMPLETED;
4350 snprintf((*pending_return)->output_path, PATH_MAX, "%s",
4351 session->rotation_chunk.current_rotate_path);
4352 }
4353
4354 ret = LTTNG_OK;
4355
4356 goto end;
4357
4358 end:
4359 return ret;
4360 }
4361
4362 /*
4363 * Command LTTNG_ROTATE_SETUP from the lttng-ctl library.
4364 *
4365 * Configure the automatic rotation parameters.
4366 *
4367 * Return 0 on success or else a LTTNG_ERR code.
4368 */
4369 int cmd_rotate_setup(struct ltt_session *session,
4370 uint64_t timer_us, uint64_t size)
4371 {
4372 int ret;
4373
4374 assert(session);
4375
4376 DBG("Cmd rotate setup session %s", session->name);
4377
4378 if (timer_us && session->rotate_timer_period) {
4379 ret = LTTNG_ERR_ROTATE_TIMER_EXISTS;
4380 goto end;
4381 }
4382
4383 if (size && session->rotate_size) {
4384 ret = LTTNG_ERR_ROTATE_SIZE_EXISTS;
4385 goto end;
4386 }
4387
4388 if (timer_us && !session->rotate_timer_period) {
4389 session->rotate_timer_period = timer_us;
4390 /*
4391 * Only start the timer if the session is active, otherwise
4392 * it will be started when the session starts.
4393 */
4394 if (session->active) {
4395 ret = sessiond_rotate_timer_start(session, timer_us);
4396 if (ret) {
4397 ERR("Failed to enable rotate timer");
4398 ret = LTTNG_ERR_UNK;
4399 goto end;
4400 }
4401 }
4402 } else if (timer_us == -1ULL && session->rotate_timer_period > 0) {
4403 sessiond_rotate_timer_stop(session);
4404 }
4405 session->rotate_size = size;
4406
4407 ret = LTTNG_OK;
4408
4409 goto end;
4410
4411 end:
4412 return ret;
4413 }
4414
4415 /*
4416 * Init command subsystem.
4417 */
4418 void cmd_init(void)
4419 {
4420 /*
4421 * Set network sequence index to 1 for streams to match a relayd
4422 * socket on the consumer side.
4423 */
4424 pthread_mutex_lock(&relayd_net_seq_idx_lock);
4425 relayd_net_seq_idx = 1;
4426 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
4427
4428 DBG("Command subsystem initialized");
4429 }
This page took 0.12217 seconds and 5 git commands to generate.