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