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