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