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