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