Fix: ust: segfault on lttng start on filter bytecode copy
[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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9
10 #define _LGPL_SOURCE
11 #include <inttypes.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <urcu/list.h>
15 #include <urcu/uatomic.h>
16
17 #include <common/buffer-view.h>
18 #include <common/common.h>
19 #include <common/compat/string.h>
20 #include <common/defaults.h>
21 #include <common/dynamic-buffer.h>
22 #include <common/kernel-ctl/kernel-ctl.h>
23 #include <common/payload-view.h>
24 #include <common/payload.h>
25 #include <common/relayd/relayd.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/string-utils/string-utils.h>
28 #include <common/trace-chunk.h>
29 #include <common/utils.h>
30 #include <lttng/action/action-internal.h>
31 #include <lttng/action/action.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/channel.h>
34 #include <lttng/condition/condition-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/condition/event-rule-matches-internal.h>
37 #include <lttng/condition/event-rule-matches.h>
38 #include <lttng/error-query-internal.h>
39 #include <lttng/event-rule/event-rule-internal.h>
40 #include <lttng/event-rule/event-rule.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/rotate-internal.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/tracker.h>
47 #include <lttng/trigger/trigger-internal.h>
48 #include <lttng/userspace-probe-internal.h>
49
50 #include "agent-thread.h"
51 #include "agent.h"
52 #include "buffer-registry.h"
53 #include "channel.h"
54 #include "cmd.h"
55 #include "consumer.h"
56 #include "event-notifier-error-accounting.h"
57 #include "event.h"
58 #include "health-sessiond.h"
59 #include "kernel-consumer.h"
60 #include "kernel.h"
61 #include "lttng-sessiond.h"
62 #include "lttng-syscall.h"
63 #include "notification-thread-commands.h"
64 #include "notification-thread.h"
65 #include "rotate.h"
66 #include "rotation-thread.h"
67 #include "session.h"
68 #include "timer.h"
69 #include "tracker.h"
70 #include "utils.h"
71
72 /* Sleep for 100ms between each check for the shm path's deletion. */
73 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
74
75 struct cmd_destroy_session_reply_context {
76 int reply_sock_fd;
77 bool implicit_rotation_on_destroy;
78 /*
79 * Indicates whether or not an error occurred while launching the
80 * destruction of a session.
81 */
82 enum lttng_error_code destruction_status;
83 };
84
85 static enum lttng_error_code wait_on_path(void *path);
86
87 /*
88 * Command completion handler that is used by the destroy command
89 * when a session that has a non-default shm_path is being destroyed.
90 *
91 * See comment in cmd_destroy_session() for the rationale.
92 */
93 static struct destroy_completion_handler {
94 struct cmd_completion_handler handler;
95 char shm_path[member_sizeof(struct ltt_session, shm_path)];
96 } destroy_completion_handler = {
97 .handler = {
98 .run = wait_on_path,
99 .data = destroy_completion_handler.shm_path
100 },
101 .shm_path = { 0 },
102 };
103
104 static struct cmd_completion_handler *current_completion_handler;
105
106 /*
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 * held.
111 */
112 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
113 static uint64_t relayd_net_seq_idx;
114
115 static int validate_ust_event_name(const char *);
116 static int cmd_enable_event_internal(struct ltt_session *session,
117 const struct lttng_domain *domain,
118 char *channel_name, struct lttng_event *event,
119 char *filter_expression,
120 struct lttng_bytecode *filter,
121 struct lttng_event_exclusion *exclusion,
122 int wpipe);
123
124 /*
125 * Create a session path used by list_lttng_sessions for the case that the
126 * session consumer is on the network.
127 */
128 static int build_network_session_path(char *dst, size_t size,
129 struct ltt_session *session)
130 {
131 int ret, kdata_port, udata_port;
132 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
133 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
134
135 LTTNG_ASSERT(session);
136 LTTNG_ASSERT(dst);
137
138 memset(tmp_urls, 0, sizeof(tmp_urls));
139 memset(tmp_uurl, 0, sizeof(tmp_uurl));
140
141 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
142
143 if (session->kernel_session && session->kernel_session->consumer) {
144 kuri = &session->kernel_session->consumer->dst.net.control;
145 kdata_port = session->kernel_session->consumer->dst.net.data.port;
146 }
147
148 if (session->ust_session && session->ust_session->consumer) {
149 uuri = &session->ust_session->consumer->dst.net.control;
150 udata_port = session->ust_session->consumer->dst.net.data.port;
151 }
152
153 if (uuri == NULL && kuri == NULL) {
154 uri = &session->consumer->dst.net.control;
155 kdata_port = session->consumer->dst.net.data.port;
156 } else if (kuri && uuri) {
157 ret = uri_compare(kuri, uuri);
158 if (ret) {
159 /* Not Equal */
160 uri = kuri;
161 /* Build uuri URL string */
162 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
163 if (ret < 0) {
164 goto error;
165 }
166 } else {
167 uri = kuri;
168 }
169 } else if (kuri && uuri == NULL) {
170 uri = kuri;
171 } else if (uuri && kuri == NULL) {
172 uri = uuri;
173 }
174
175 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
176 if (ret < 0) {
177 goto error;
178 }
179
180 /*
181 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 * to print.
183 */
184 if (*tmp_uurl != '\0') {
185 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
186 tmp_urls, kdata_port, tmp_uurl, udata_port);
187 } else {
188 int dport;
189 if (kuri || (!kuri && !uuri)) {
190 dport = kdata_port;
191 } else {
192 /* No kernel URI, use the UST port. */
193 dport = udata_port;
194 }
195 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
196 }
197
198 error:
199 return ret;
200 }
201
202 /*
203 * Get run-time attributes if the session has been started (discarded events,
204 * lost packets).
205 */
206 static int get_kernel_runtime_stats(struct ltt_session *session,
207 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
208 uint64_t *lost_packets)
209 {
210 int ret;
211
212 if (!session->has_been_started) {
213 ret = 0;
214 *discarded_events = 0;
215 *lost_packets = 0;
216 goto end;
217 }
218
219 ret = consumer_get_discarded_events(session->id, kchan->key,
220 session->kernel_session->consumer,
221 discarded_events);
222 if (ret < 0) {
223 goto end;
224 }
225
226 ret = consumer_get_lost_packets(session->id, kchan->key,
227 session->kernel_session->consumer,
228 lost_packets);
229 if (ret < 0) {
230 goto end;
231 }
232
233 end:
234 return ret;
235 }
236
237 /*
238 * Get run-time attributes if the session has been started (discarded events,
239 * lost packets).
240 */
241 static int get_ust_runtime_stats(struct ltt_session *session,
242 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
243 uint64_t *lost_packets)
244 {
245 int ret;
246 struct ltt_ust_session *usess;
247
248 if (!discarded_events || !lost_packets) {
249 ret = -1;
250 goto end;
251 }
252
253 usess = session->ust_session;
254 LTTNG_ASSERT(discarded_events);
255 LTTNG_ASSERT(lost_packets);
256
257 if (!usess || !session->has_been_started) {
258 *discarded_events = 0;
259 *lost_packets = 0;
260 ret = 0;
261 goto end;
262 }
263
264 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
265 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
266 &usess->buffer_reg_uid_list,
267 usess->consumer, uchan->id,
268 uchan->attr.overwrite,
269 discarded_events,
270 lost_packets);
271 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
272 ret = ust_app_pid_get_channel_runtime_stats(usess,
273 uchan, usess->consumer,
274 uchan->attr.overwrite,
275 discarded_events,
276 lost_packets);
277 if (ret < 0) {
278 goto end;
279 }
280 *discarded_events += uchan->per_pid_closed_app_discarded;
281 *lost_packets += uchan->per_pid_closed_app_lost;
282 } else {
283 ERR("Unsupported buffer type");
284 abort();
285 ret = -1;
286 goto end;
287 }
288
289 end:
290 return ret;
291 }
292
293 /*
294 * Fill lttng_channel array of all channels.
295 */
296 static ssize_t list_lttng_channels(enum lttng_domain_type domain,
297 struct ltt_session *session, struct lttng_channel *channels,
298 struct lttng_channel_extended *chan_exts)
299 {
300 int i = 0, ret = 0;
301 struct ltt_kernel_channel *kchan;
302
303 DBG("Listing channels for session %s", session->name);
304
305 switch (domain) {
306 case LTTNG_DOMAIN_KERNEL:
307 /* Kernel channels */
308 if (session->kernel_session != NULL) {
309 cds_list_for_each_entry(kchan,
310 &session->kernel_session->channel_list.head, list) {
311 uint64_t discarded_events, lost_packets;
312 struct lttng_channel_extended *extended;
313
314 extended = (struct lttng_channel_extended *)
315 kchan->channel->attr.extended.ptr;
316
317 ret = get_kernel_runtime_stats(session, kchan,
318 &discarded_events, &lost_packets);
319 if (ret < 0) {
320 goto end;
321 }
322 /* Copy lttng_channel struct to array */
323 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
324 channels[i].enabled = kchan->enabled;
325 chan_exts[i].discarded_events =
326 discarded_events;
327 chan_exts[i].lost_packets = lost_packets;
328 chan_exts[i].monitor_timer_interval =
329 extended->monitor_timer_interval;
330 chan_exts[i].blocking_timeout = 0;
331 i++;
332 }
333 }
334 break;
335 case LTTNG_DOMAIN_UST:
336 {
337 struct lttng_ht_iter iter;
338 struct ltt_ust_channel *uchan;
339
340 rcu_read_lock();
341 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
342 &iter.iter, uchan, node.node) {
343 uint64_t discarded_events = 0, lost_packets = 0;
344
345 if (lttng_strncpy(channels[i].name, uchan->name,
346 LTTNG_SYMBOL_NAME_LEN)) {
347 break;
348 }
349 channels[i].attr.overwrite = uchan->attr.overwrite;
350 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
351 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
352 channels[i].attr.switch_timer_interval =
353 uchan->attr.switch_timer_interval;
354 channels[i].attr.read_timer_interval =
355 uchan->attr.read_timer_interval;
356 channels[i].enabled = uchan->enabled;
357 channels[i].attr.tracefile_size = uchan->tracefile_size;
358 channels[i].attr.tracefile_count = uchan->tracefile_count;
359
360 /*
361 * Map enum lttng_ust_output to enum lttng_event_output.
362 */
363 switch (uchan->attr.output) {
364 case LTTNG_UST_ABI_MMAP:
365 channels[i].attr.output = LTTNG_EVENT_MMAP;
366 break;
367 default:
368 /*
369 * LTTNG_UST_MMAP is the only supported UST
370 * output mode.
371 */
372 abort();
373 break;
374 }
375
376 chan_exts[i].monitor_timer_interval =
377 uchan->monitor_timer_interval;
378 chan_exts[i].blocking_timeout =
379 uchan->attr.u.s.blocking_timeout;
380
381 ret = get_ust_runtime_stats(session, uchan,
382 &discarded_events, &lost_packets);
383 if (ret < 0) {
384 break;
385 }
386 chan_exts[i].discarded_events = discarded_events;
387 chan_exts[i].lost_packets = lost_packets;
388 i++;
389 }
390 rcu_read_unlock();
391 break;
392 }
393 default:
394 break;
395 }
396
397 end:
398 if (ret < 0) {
399 return -LTTNG_ERR_FATAL;
400 } else {
401 return LTTNG_OK;
402 }
403 }
404
405 static int append_extended_info(const char *filter_expression,
406 struct lttng_event_exclusion *exclusion,
407 struct lttng_userspace_probe_location *probe_location,
408 struct lttng_payload *payload)
409 {
410 int ret = 0;
411 size_t filter_len = 0;
412 size_t nb_exclusions = 0;
413 size_t userspace_probe_location_len = 0;
414 struct lttcomm_event_extended_header extended_header = {};
415 struct lttcomm_event_extended_header *p_extended_header;
416 const size_t original_payload_size = payload->buffer.size;
417
418 ret = lttng_dynamic_buffer_append(&payload->buffer, &extended_header,
419 sizeof(extended_header));
420 if (ret) {
421 goto end;
422 }
423
424 if (filter_expression) {
425 filter_len = strlen(filter_expression) + 1;
426 ret = lttng_dynamic_buffer_append(&payload->buffer,
427 filter_expression, filter_len);
428 if (ret) {
429 goto end;
430 }
431 }
432
433 if (exclusion) {
434 const size_t len = exclusion->count * LTTNG_SYMBOL_NAME_LEN;
435
436 nb_exclusions = exclusion->count;
437
438 ret = lttng_dynamic_buffer_append(
439 &payload->buffer, &exclusion->names, len);
440 if (ret) {
441 goto end;
442 }
443 }
444
445 if (probe_location) {
446 const size_t size_before_probe = payload->buffer.size;
447
448 ret = lttng_userspace_probe_location_serialize(probe_location,
449 payload);
450 if (ret < 0) {
451 ret = -1;
452 goto end;
453 }
454
455 userspace_probe_location_len =
456 payload->buffer.size - size_before_probe;
457 }
458
459 /* Set header fields */
460 p_extended_header = (struct lttcomm_event_extended_header *)
461 (payload->buffer.data + original_payload_size);
462
463 p_extended_header->filter_len = filter_len;
464 p_extended_header->nb_exclusions = nb_exclusions;
465 p_extended_header->userspace_probe_location_len =
466 userspace_probe_location_len;
467
468 ret = 0;
469 end:
470 return ret;
471 }
472
473 /*
474 * Create a list of agent domain events.
475 *
476 * Return number of events in list on success or else a negative value.
477 */
478 static int list_lttng_agent_events(struct agent *agt,
479 struct lttng_payload *payload)
480 {
481 int nb_events = 0, ret = 0;
482 const struct agent_event *agent_event;
483 struct lttng_ht_iter iter;
484
485 LTTNG_ASSERT(agt);
486
487 DBG3("Listing agent events");
488
489 rcu_read_lock();
490 cds_lfht_for_each_entry (
491 agt->events->ht, &iter.iter, agent_event, node.node) {
492 struct lttng_event event = {
493 .enabled = AGENT_EVENT_IS_ENABLED(agent_event),
494 .loglevel = agent_event->loglevel_value,
495 .loglevel_type = agent_event->loglevel_type,
496 };
497
498 ret = lttng_strncpy(event.name, agent_event->name, sizeof(event.name));
499 if (ret) {
500 /* Internal error, invalid name. */
501 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
502 agent_event->name, sizeof(event.name));
503 ret = -LTTNG_ERR_UNK;
504 goto end;
505 }
506
507 ret = lttng_dynamic_buffer_append(
508 &payload->buffer, &event, sizeof(event));
509 if (ret) {
510 ERR("Failed to append event to payload");
511 ret = -LTTNG_ERR_NOMEM;
512 goto end;
513 }
514
515 nb_events++;
516 }
517
518 cds_lfht_for_each_entry (
519 agt->events->ht, &iter.iter, agent_event, node.node) {
520 /* Append extended info. */
521 ret = append_extended_info(agent_event->filter_expression, NULL,
522 NULL, payload);
523 if (ret) {
524 ERR("Failed to append extended event info to payload");
525 ret = -LTTNG_ERR_NOMEM;
526 goto end;
527 }
528 }
529
530 ret = nb_events;
531 end:
532 rcu_read_unlock();
533 return ret;
534 }
535
536 /*
537 * Create a list of ust global domain events.
538 */
539 static int list_lttng_ust_global_events(char *channel_name,
540 struct ltt_ust_domain_global *ust_global,
541 struct lttng_payload *payload)
542 {
543 int ret = 0;
544 unsigned int nb_events = 0;
545 struct lttng_ht_iter iter;
546 const struct lttng_ht_node_str *node;
547 const struct ltt_ust_channel *uchan;
548 const struct ltt_ust_event *uevent;
549
550 DBG("Listing UST global events for channel %s", channel_name);
551
552 rcu_read_lock();
553
554 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
555 node = lttng_ht_iter_get_node_str(&iter);
556 if (node == NULL) {
557 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
558 goto end;
559 }
560
561 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
562
563 DBG3("Listing UST global events");
564
565 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
566 struct lttng_event event = {};
567
568 if (uevent->internal) {
569 continue;
570 }
571
572 ret = lttng_strncpy(event.name, uevent->attr.name, sizeof(event.name));
573 if (ret) {
574 /* Internal error, invalid name. */
575 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
576 uevent->attr.name, sizeof(event.name));
577 ret = -LTTNG_ERR_UNK;
578 goto end;
579 }
580
581 event.enabled = uevent->enabled;
582
583 switch (uevent->attr.instrumentation) {
584 case LTTNG_UST_ABI_TRACEPOINT:
585 event.type = LTTNG_EVENT_TRACEPOINT;
586 break;
587 case LTTNG_UST_ABI_PROBE:
588 event.type = LTTNG_EVENT_PROBE;
589 break;
590 case LTTNG_UST_ABI_FUNCTION:
591 event.type = LTTNG_EVENT_FUNCTION;
592 break;
593 }
594
595 event.loglevel = uevent->attr.loglevel;
596 switch (uevent->attr.loglevel_type) {
597 case LTTNG_UST_ABI_LOGLEVEL_ALL:
598 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
599 break;
600 case LTTNG_UST_ABI_LOGLEVEL_RANGE:
601 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
602 break;
603 case LTTNG_UST_ABI_LOGLEVEL_SINGLE:
604 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
605 break;
606 }
607
608 if (uevent->filter) {
609 event.filter = 1;
610 }
611
612 if (uevent->exclusion) {
613 event.exclusion = 1;
614 }
615
616 ret = lttng_dynamic_buffer_append(&payload->buffer, &event, sizeof(event));
617 if (ret) {
618 ERR("Failed to append event to payload");
619 ret = -LTTNG_ERR_NOMEM;
620 goto end;
621 }
622
623 nb_events++;
624 }
625
626 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
627 /* Append extended info. */
628 ret = append_extended_info(uevent->filter_expression,
629 uevent->exclusion, NULL, payload);
630 if (ret) {
631 ERR("Failed to append extended event info to payload");
632 ret = -LTTNG_ERR_FATAL;
633 goto end;
634 }
635 }
636
637 ret = nb_events;
638 end:
639 rcu_read_unlock();
640 return ret;
641 }
642
643 /*
644 * Fill lttng_event array of all kernel events in the channel.
645 */
646 static int list_lttng_kernel_events(char *channel_name,
647 struct ltt_kernel_session *kernel_session,
648 struct lttng_payload *payload)
649 {
650 int ret;
651 unsigned int nb_event;
652 const struct ltt_kernel_event *kevent;
653 const struct ltt_kernel_channel *kchan;
654
655 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
656 if (kchan == NULL) {
657 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
658 goto error;
659 }
660
661 nb_event = kchan->event_count;
662
663 DBG("Listing events for channel %s", kchan->channel->name);
664
665 /* Kernel channels */
666 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
667 struct lttng_event event = {};
668
669 ret = lttng_strncpy(event.name, kevent->event->name, sizeof(event.name));
670 if (ret) {
671 /* Internal error, invalid name. */
672 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
673 kevent->event->name,
674 sizeof(event.name));
675 ret = -LTTNG_ERR_UNK;
676 goto end;
677 }
678
679 event.enabled = kevent->enabled;
680 event.filter = (unsigned char) !!kevent->filter_expression;
681
682 switch (kevent->event->instrumentation) {
683 case LTTNG_KERNEL_ABI_TRACEPOINT:
684 event.type = LTTNG_EVENT_TRACEPOINT;
685 break;
686 case LTTNG_KERNEL_ABI_KRETPROBE:
687 event.type = LTTNG_EVENT_FUNCTION;
688 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
689 sizeof(struct lttng_kernel_abi_kprobe));
690 break;
691 case LTTNG_KERNEL_ABI_KPROBE:
692 event.type = LTTNG_EVENT_PROBE;
693 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
694 sizeof(struct lttng_kernel_abi_kprobe));
695 break;
696 case LTTNG_KERNEL_ABI_UPROBE:
697 event.type = LTTNG_EVENT_USERSPACE_PROBE;
698 break;
699 case LTTNG_KERNEL_ABI_FUNCTION:
700 event.type = LTTNG_EVENT_FUNCTION;
701 memcpy(&event.attr.ftrace, &kevent->event->u.ftrace,
702 sizeof(struct lttng_kernel_abi_function));
703 break;
704 case LTTNG_KERNEL_ABI_NOOP:
705 event.type = LTTNG_EVENT_NOOP;
706 break;
707 case LTTNG_KERNEL_ABI_SYSCALL:
708 event.type = LTTNG_EVENT_SYSCALL;
709 break;
710 case LTTNG_KERNEL_ABI_ALL:
711 /* fall-through. */
712 default:
713 abort();
714 break;
715 }
716
717 ret = lttng_dynamic_buffer_append(
718 &payload->buffer, &event, sizeof(event));
719 if (ret) {
720 ERR("Failed to append event to payload");
721 ret = -LTTNG_ERR_NOMEM;
722 goto end;
723 }
724 }
725
726 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
727 /* Append extended info. */
728 ret = append_extended_info(kevent->filter_expression, NULL,
729 kevent->userspace_probe_location, payload);
730 if (ret) {
731 DBG("Error appending extended info message");
732 ret = -LTTNG_ERR_FATAL;
733 goto error;
734 }
735 }
736
737 end:
738 return nb_event;
739 error:
740 return ret;
741 }
742
743 /*
744 * Add URI so the consumer output object. Set the correct path depending on the
745 * domain adding the default trace directory.
746 */
747 static enum lttng_error_code add_uri_to_consumer(
748 const struct ltt_session *session,
749 struct consumer_output *consumer,
750 struct lttng_uri *uri, enum lttng_domain_type domain)
751 {
752 int ret;
753 enum lttng_error_code ret_code = LTTNG_OK;
754
755 LTTNG_ASSERT(uri);
756
757 if (consumer == NULL) {
758 DBG("No consumer detected. Don't add URI. Stopping.");
759 ret_code = LTTNG_ERR_NO_CONSUMER;
760 goto error;
761 }
762
763 switch (domain) {
764 case LTTNG_DOMAIN_KERNEL:
765 ret = lttng_strncpy(consumer->domain_subdir,
766 DEFAULT_KERNEL_TRACE_DIR,
767 sizeof(consumer->domain_subdir));
768 break;
769 case LTTNG_DOMAIN_UST:
770 ret = lttng_strncpy(consumer->domain_subdir,
771 DEFAULT_UST_TRACE_DIR,
772 sizeof(consumer->domain_subdir));
773 break;
774 default:
775 /*
776 * This case is possible is we try to add the URI to the global
777 * tracing session consumer object which in this case there is
778 * no subdir.
779 */
780 memset(consumer->domain_subdir, 0,
781 sizeof(consumer->domain_subdir));
782 ret = 0;
783 }
784 if (ret) {
785 ERR("Failed to initialize consumer output domain subdirectory");
786 ret_code = LTTNG_ERR_FATAL;
787 goto error;
788 }
789
790 switch (uri->dtype) {
791 case LTTNG_DST_IPV4:
792 case LTTNG_DST_IPV6:
793 DBG2("Setting network URI to consumer");
794
795 if (consumer->type == CONSUMER_DST_NET) {
796 if ((uri->stype == LTTNG_STREAM_CONTROL &&
797 consumer->dst.net.control_isset) ||
798 (uri->stype == LTTNG_STREAM_DATA &&
799 consumer->dst.net.data_isset)) {
800 ret_code = LTTNG_ERR_URL_EXIST;
801 goto error;
802 }
803 } else {
804 memset(&consumer->dst, 0, sizeof(consumer->dst));
805 }
806
807 /* Set URI into consumer output object */
808 ret = consumer_set_network_uri(session, consumer, uri);
809 if (ret < 0) {
810 ret_code = -ret;
811 goto error;
812 } else if (ret == 1) {
813 /*
814 * URI was the same in the consumer so we do not append the subdir
815 * again so to not duplicate output dir.
816 */
817 ret_code = LTTNG_OK;
818 goto error;
819 }
820 break;
821 case LTTNG_DST_PATH:
822 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
823 ret_code = LTTNG_ERR_INVALID;
824 goto error;
825 }
826 DBG2("Setting trace directory path from URI to %s",
827 uri->dst.path);
828 memset(&consumer->dst, 0, sizeof(consumer->dst));
829
830 ret = lttng_strncpy(consumer->dst.session_root_path,
831 uri->dst.path,
832 sizeof(consumer->dst.session_root_path));
833 if (ret) {
834 ret_code = LTTNG_ERR_FATAL;
835 goto error;
836 }
837 consumer->type = CONSUMER_DST_LOCAL;
838 break;
839 }
840
841 ret_code = LTTNG_OK;
842 error:
843 return ret_code;
844 }
845
846 /*
847 * Init tracing by creating trace directory and sending fds kernel consumer.
848 */
849 static int init_kernel_tracing(struct ltt_kernel_session *session)
850 {
851 int ret = 0;
852 struct lttng_ht_iter iter;
853 struct consumer_socket *socket;
854
855 LTTNG_ASSERT(session);
856
857 rcu_read_lock();
858
859 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
860 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
861 socket, node.node) {
862 pthread_mutex_lock(socket->lock);
863 ret = kernel_consumer_send_session(socket, session);
864 pthread_mutex_unlock(socket->lock);
865 if (ret < 0) {
866 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
867 goto error;
868 }
869 }
870 }
871
872 error:
873 rcu_read_unlock();
874 return ret;
875 }
876
877 /*
878 * Create a socket to the relayd using the URI.
879 *
880 * On success, the relayd_sock pointer is set to the created socket.
881 * Else, it remains untouched and an LTTng error code is returned.
882 */
883 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
884 struct lttcomm_relayd_sock **relayd_sock,
885 struct consumer_output *consumer)
886 {
887 int ret;
888 enum lttng_error_code status = LTTNG_OK;
889 struct lttcomm_relayd_sock *rsock;
890
891 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
892 RELAYD_VERSION_COMM_MINOR);
893 if (!rsock) {
894 status = LTTNG_ERR_FATAL;
895 goto error;
896 }
897
898 /*
899 * Connect to relayd so we can proceed with a session creation. This call
900 * can possibly block for an arbitrary amount of time to set the health
901 * state to be in poll execution.
902 */
903 health_poll_entry();
904 ret = relayd_connect(rsock);
905 health_poll_exit();
906 if (ret < 0) {
907 ERR("Unable to reach lttng-relayd");
908 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
909 goto free_sock;
910 }
911
912 /* Create socket for control stream. */
913 if (uri->stype == LTTNG_STREAM_CONTROL) {
914 uint64_t result_flags;
915
916 DBG3("Creating relayd stream socket from URI");
917
918 /* Check relayd version */
919 ret = relayd_version_check(rsock);
920 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
921 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
922 goto close_sock;
923 } else if (ret < 0) {
924 ERR("Unable to reach lttng-relayd");
925 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
926 goto close_sock;
927 }
928 consumer->relay_major_version = rsock->major;
929 consumer->relay_minor_version = rsock->minor;
930 ret = relayd_get_configuration(rsock, 0,
931 &result_flags);
932 if (ret < 0) {
933 ERR("Unable to get relayd configuration");
934 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
935 goto close_sock;
936 }
937 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
938 consumer->relay_allows_clear = true;
939 }
940 } else if (uri->stype == LTTNG_STREAM_DATA) {
941 DBG3("Creating relayd data socket from URI");
942 } else {
943 /* Command is not valid */
944 ERR("Relayd invalid stream type: %d", uri->stype);
945 status = LTTNG_ERR_INVALID;
946 goto close_sock;
947 }
948
949 *relayd_sock = rsock;
950
951 return status;
952
953 close_sock:
954 /* The returned value is not useful since we are on an error path. */
955 (void) relayd_close(rsock);
956 free_sock:
957 free(rsock);
958 error:
959 return status;
960 }
961
962 /*
963 * Connect to the relayd using URI and send the socket to the right consumer.
964 *
965 * The consumer socket lock must be held by the caller.
966 *
967 * Returns LTTNG_OK on success or an LTTng error code on failure.
968 */
969 static enum lttng_error_code send_consumer_relayd_socket(
970 unsigned int session_id,
971 struct lttng_uri *relayd_uri,
972 struct consumer_output *consumer,
973 struct consumer_socket *consumer_sock,
974 const char *session_name, const char *hostname,
975 const char *base_path, int session_live_timer,
976 const uint64_t *current_chunk_id,
977 time_t session_creation_time,
978 bool session_name_contains_creation_time)
979 {
980 int ret;
981 struct lttcomm_relayd_sock *rsock = NULL;
982 enum lttng_error_code status;
983
984 /* Connect to relayd and make version check if uri is the control. */
985 status = create_connect_relayd(relayd_uri, &rsock, consumer);
986 if (status != LTTNG_OK) {
987 goto relayd_comm_error;
988 }
989 LTTNG_ASSERT(rsock);
990
991 /* Set the network sequence index if not set. */
992 if (consumer->net_seq_index == (uint64_t) -1ULL) {
993 pthread_mutex_lock(&relayd_net_seq_idx_lock);
994 /*
995 * Increment net_seq_idx because we are about to transfer the
996 * new relayd socket to the consumer.
997 * Assign unique key so the consumer can match streams.
998 */
999 consumer->net_seq_index = ++relayd_net_seq_idx;
1000 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
1001 }
1002
1003 /* Send relayd socket to consumer. */
1004 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
1005 relayd_uri->stype, session_id,
1006 session_name, hostname, base_path,
1007 session_live_timer, current_chunk_id,
1008 session_creation_time, session_name_contains_creation_time);
1009 if (ret < 0) {
1010 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
1011 goto close_sock;
1012 }
1013
1014 /* Flag that the corresponding socket was sent. */
1015 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
1016 consumer_sock->control_sock_sent = 1;
1017 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
1018 consumer_sock->data_sock_sent = 1;
1019 }
1020
1021 /*
1022 * Close socket which was dup on the consumer side. The session daemon does
1023 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1024 */
1025
1026 close_sock:
1027 if (status != LTTNG_OK) {
1028 /*
1029 * The consumer output for this session should not be used anymore
1030 * since the relayd connection failed thus making any tracing or/and
1031 * streaming not usable.
1032 */
1033 consumer->enabled = 0;
1034 }
1035 (void) relayd_close(rsock);
1036 free(rsock);
1037
1038 relayd_comm_error:
1039 return status;
1040 }
1041
1042 /*
1043 * Send both relayd sockets to a specific consumer and domain. This is a
1044 * helper function to facilitate sending the information to the consumer for a
1045 * session.
1046 *
1047 * The consumer socket lock must be held by the caller.
1048 *
1049 * Returns LTTNG_OK, or an LTTng error code on failure.
1050 */
1051 static enum lttng_error_code send_consumer_relayd_sockets(
1052 enum lttng_domain_type domain,
1053 unsigned int session_id, struct consumer_output *consumer,
1054 struct consumer_socket *sock, const char *session_name,
1055 const char *hostname, const char *base_path, int session_live_timer,
1056 const uint64_t *current_chunk_id, time_t session_creation_time,
1057 bool session_name_contains_creation_time)
1058 {
1059 enum lttng_error_code status = LTTNG_OK;
1060
1061 LTTNG_ASSERT(consumer);
1062 LTTNG_ASSERT(sock);
1063
1064 /* Sending control relayd socket. */
1065 if (!sock->control_sock_sent) {
1066 status = send_consumer_relayd_socket(session_id,
1067 &consumer->dst.net.control, consumer, sock,
1068 session_name, hostname, base_path, session_live_timer,
1069 current_chunk_id, session_creation_time,
1070 session_name_contains_creation_time);
1071 if (status != LTTNG_OK) {
1072 goto error;
1073 }
1074 }
1075
1076 /* Sending data relayd socket. */
1077 if (!sock->data_sock_sent) {
1078 status = send_consumer_relayd_socket(session_id,
1079 &consumer->dst.net.data, consumer, sock,
1080 session_name, hostname, base_path, session_live_timer,
1081 current_chunk_id, session_creation_time,
1082 session_name_contains_creation_time);
1083 if (status != LTTNG_OK) {
1084 goto error;
1085 }
1086 }
1087
1088 error:
1089 return status;
1090 }
1091
1092 /*
1093 * Setup relayd connections for a tracing session. First creates the socket to
1094 * the relayd and send them to the right domain consumer. Consumer type MUST be
1095 * network.
1096 */
1097 int cmd_setup_relayd(struct ltt_session *session)
1098 {
1099 int ret = LTTNG_OK;
1100 struct ltt_ust_session *usess;
1101 struct ltt_kernel_session *ksess;
1102 struct consumer_socket *socket;
1103 struct lttng_ht_iter iter;
1104 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1105
1106 LTTNG_ASSERT(session);
1107
1108 usess = session->ust_session;
1109 ksess = session->kernel_session;
1110
1111 DBG("Setting relayd for session %s", session->name);
1112
1113 rcu_read_lock();
1114 if (session->current_trace_chunk) {
1115 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1116 session->current_trace_chunk, &current_chunk_id.value);
1117
1118 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1119 current_chunk_id.is_set = true;
1120 } else {
1121 ERR("Failed to get current trace chunk id");
1122 ret = LTTNG_ERR_UNK;
1123 goto error;
1124 }
1125 }
1126
1127 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1128 && usess->consumer->enabled) {
1129 /* For each consumer socket, send relayd sockets */
1130 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1131 socket, node.node) {
1132 pthread_mutex_lock(socket->lock);
1133 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1134 usess->consumer, socket,
1135 session->name, session->hostname,
1136 session->base_path,
1137 session->live_timer,
1138 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1139 session->creation_time,
1140 session->name_contains_creation_time);
1141 pthread_mutex_unlock(socket->lock);
1142 if (ret != LTTNG_OK) {
1143 goto error;
1144 }
1145 /* Session is now ready for network streaming. */
1146 session->net_handle = 1;
1147 }
1148 session->consumer->relay_major_version =
1149 usess->consumer->relay_major_version;
1150 session->consumer->relay_minor_version =
1151 usess->consumer->relay_minor_version;
1152 session->consumer->relay_allows_clear =
1153 usess->consumer->relay_allows_clear;
1154 }
1155
1156 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1157 && ksess->consumer->enabled) {
1158 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1159 socket, node.node) {
1160 pthread_mutex_lock(socket->lock);
1161 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1162 ksess->consumer, socket,
1163 session->name, session->hostname,
1164 session->base_path,
1165 session->live_timer,
1166 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1167 session->creation_time,
1168 session->name_contains_creation_time);
1169 pthread_mutex_unlock(socket->lock);
1170 if (ret != LTTNG_OK) {
1171 goto error;
1172 }
1173 /* Session is now ready for network streaming. */
1174 session->net_handle = 1;
1175 }
1176 session->consumer->relay_major_version =
1177 ksess->consumer->relay_major_version;
1178 session->consumer->relay_minor_version =
1179 ksess->consumer->relay_minor_version;
1180 session->consumer->relay_allows_clear =
1181 ksess->consumer->relay_allows_clear;
1182 }
1183
1184 error:
1185 rcu_read_unlock();
1186 return ret;
1187 }
1188
1189 /*
1190 * Start a kernel session by opening all necessary streams.
1191 */
1192 int start_kernel_session(struct ltt_kernel_session *ksess)
1193 {
1194 int ret;
1195 struct ltt_kernel_channel *kchan;
1196
1197 /* Open kernel metadata */
1198 if (ksess->metadata == NULL && ksess->output_traces) {
1199 ret = kernel_open_metadata(ksess);
1200 if (ret < 0) {
1201 ret = LTTNG_ERR_KERN_META_FAIL;
1202 goto error;
1203 }
1204 }
1205
1206 /* Open kernel metadata stream */
1207 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1208 ret = kernel_open_metadata_stream(ksess);
1209 if (ret < 0) {
1210 ERR("Kernel create metadata stream failed");
1211 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1212 goto error;
1213 }
1214 }
1215
1216 /* For each channel */
1217 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1218 if (kchan->stream_count == 0) {
1219 ret = kernel_open_channel_stream(kchan);
1220 if (ret < 0) {
1221 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1222 goto error;
1223 }
1224 /* Update the stream global counter */
1225 ksess->stream_count_global += ret;
1226 }
1227 }
1228
1229 /* Setup kernel consumer socket and send fds to it */
1230 ret = init_kernel_tracing(ksess);
1231 if (ret != 0) {
1232 ret = LTTNG_ERR_KERN_START_FAIL;
1233 goto error;
1234 }
1235
1236 /* This start the kernel tracing */
1237 ret = kernel_start_session(ksess);
1238 if (ret < 0) {
1239 ret = LTTNG_ERR_KERN_START_FAIL;
1240 goto error;
1241 }
1242
1243 /* Quiescent wait after starting trace */
1244 kernel_wait_quiescent();
1245
1246 ksess->active = 1;
1247
1248 ret = LTTNG_OK;
1249
1250 error:
1251 return ret;
1252 }
1253
1254 int stop_kernel_session(struct ltt_kernel_session *ksess)
1255 {
1256 struct ltt_kernel_channel *kchan;
1257 bool error_occurred = false;
1258 int ret;
1259
1260 if (!ksess || !ksess->active) {
1261 return LTTNG_OK;
1262 }
1263 DBG("Stopping kernel tracing");
1264
1265 ret = kernel_stop_session(ksess);
1266 if (ret < 0) {
1267 ret = LTTNG_ERR_KERN_STOP_FAIL;
1268 goto error;
1269 }
1270
1271 kernel_wait_quiescent();
1272
1273 /* Flush metadata after stopping (if exists) */
1274 if (ksess->metadata_stream_fd >= 0) {
1275 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1276 if (ret < 0) {
1277 ERR("Kernel metadata flush failed");
1278 error_occurred = true;
1279 }
1280 }
1281
1282 /* Flush all buffers after stopping */
1283 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1284 ret = kernel_flush_buffer(kchan);
1285 if (ret < 0) {
1286 ERR("Kernel flush buffer error");
1287 error_occurred = true;
1288 }
1289 }
1290
1291 ksess->active = 0;
1292 if (error_occurred) {
1293 ret = LTTNG_ERR_UNK;
1294 } else {
1295 ret = LTTNG_OK;
1296 }
1297 error:
1298 return ret;
1299 }
1300
1301 /*
1302 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1303 */
1304 int cmd_disable_channel(struct ltt_session *session,
1305 enum lttng_domain_type domain, char *channel_name)
1306 {
1307 int ret;
1308 struct ltt_ust_session *usess;
1309
1310 usess = session->ust_session;
1311
1312 rcu_read_lock();
1313
1314 switch (domain) {
1315 case LTTNG_DOMAIN_KERNEL:
1316 {
1317 ret = channel_kernel_disable(session->kernel_session,
1318 channel_name);
1319 if (ret != LTTNG_OK) {
1320 goto error;
1321 }
1322
1323 kernel_wait_quiescent();
1324 break;
1325 }
1326 case LTTNG_DOMAIN_UST:
1327 {
1328 struct ltt_ust_channel *uchan;
1329 struct lttng_ht *chan_ht;
1330
1331 chan_ht = usess->domain_global.channels;
1332
1333 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1334 if (uchan == NULL) {
1335 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1336 goto error;
1337 }
1338
1339 ret = channel_ust_disable(usess, uchan);
1340 if (ret != LTTNG_OK) {
1341 goto error;
1342 }
1343 break;
1344 }
1345 default:
1346 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1347 goto error;
1348 }
1349
1350 ret = LTTNG_OK;
1351
1352 error:
1353 rcu_read_unlock();
1354 return ret;
1355 }
1356
1357 /*
1358 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1359 *
1360 * The wpipe arguments is used as a notifier for the kernel thread.
1361 */
1362 int cmd_enable_channel(struct ltt_session *session,
1363 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
1364 {
1365 int ret;
1366 struct ltt_ust_session *usess = session->ust_session;
1367 struct lttng_ht *chan_ht;
1368 size_t len;
1369 struct lttng_channel attr;
1370
1371 LTTNG_ASSERT(session);
1372 LTTNG_ASSERT(_attr);
1373 LTTNG_ASSERT(domain);
1374
1375 attr = *_attr;
1376 len = lttng_strnlen(attr.name, sizeof(attr.name));
1377
1378 /* Validate channel name */
1379 if (attr.name[0] == '.' ||
1380 memchr(attr.name, '/', len) != NULL) {
1381 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1382 goto end;
1383 }
1384
1385 DBG("Enabling channel %s for session %s", attr.name, session->name);
1386
1387 rcu_read_lock();
1388
1389 /*
1390 * Don't try to enable a channel if the session has been started at
1391 * some point in time before. The tracer does not allow it.
1392 */
1393 if (session->has_been_started) {
1394 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1395 goto error;
1396 }
1397
1398 /*
1399 * If the session is a live session, remove the switch timer, the
1400 * live timer does the same thing but sends also synchronisation
1401 * beacons for inactive streams.
1402 */
1403 if (session->live_timer > 0) {
1404 attr.attr.live_timer_interval = session->live_timer;
1405 attr.attr.switch_timer_interval = 0;
1406 }
1407
1408 /* Check for feature support */
1409 switch (domain->type) {
1410 case LTTNG_DOMAIN_KERNEL:
1411 {
1412 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1413 /* Sampling position of buffer is not supported */
1414 WARN("Kernel tracer does not support buffer monitoring. "
1415 "Setting the monitor interval timer to 0 "
1416 "(disabled) for channel '%s' of session '%s'",
1417 attr.name, session->name);
1418 lttng_channel_set_monitor_timer_interval(&attr, 0);
1419 }
1420 break;
1421 }
1422 case LTTNG_DOMAIN_UST:
1423 break;
1424 case LTTNG_DOMAIN_JUL:
1425 case LTTNG_DOMAIN_LOG4J:
1426 case LTTNG_DOMAIN_PYTHON:
1427 if (!agent_tracing_is_enabled()) {
1428 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1429 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1430 goto error;
1431 }
1432 break;
1433 default:
1434 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1435 goto error;
1436 }
1437
1438 switch (domain->type) {
1439 case LTTNG_DOMAIN_KERNEL:
1440 {
1441 struct ltt_kernel_channel *kchan;
1442
1443 kchan = trace_kernel_get_channel_by_name(attr.name,
1444 session->kernel_session);
1445 if (kchan == NULL) {
1446 if (session->snapshot.nb_output > 0 ||
1447 session->snapshot_mode) {
1448 /* Enforce mmap output for snapshot sessions. */
1449 attr.attr.output = LTTNG_EVENT_MMAP;
1450 }
1451 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1452 if (attr.name[0] != '\0') {
1453 session->kernel_session->has_non_default_channel = 1;
1454 }
1455 } else {
1456 ret = channel_kernel_enable(session->kernel_session, kchan);
1457 }
1458
1459 if (ret != LTTNG_OK) {
1460 goto error;
1461 }
1462
1463 kernel_wait_quiescent();
1464 break;
1465 }
1466 case LTTNG_DOMAIN_UST:
1467 case LTTNG_DOMAIN_JUL:
1468 case LTTNG_DOMAIN_LOG4J:
1469 case LTTNG_DOMAIN_PYTHON:
1470 {
1471 struct ltt_ust_channel *uchan;
1472
1473 /*
1474 * FIXME
1475 *
1476 * Current agent implementation limitations force us to allow
1477 * only one channel at once in "agent" subdomains. Each
1478 * subdomain has a default channel name which must be strictly
1479 * adhered to.
1480 */
1481 if (domain->type == LTTNG_DOMAIN_JUL) {
1482 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
1483 LTTNG_SYMBOL_NAME_LEN)) {
1484 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1485 goto error;
1486 }
1487 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1488 if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME,
1489 LTTNG_SYMBOL_NAME_LEN)) {
1490 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1491 goto error;
1492 }
1493 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1494 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
1495 LTTNG_SYMBOL_NAME_LEN)) {
1496 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1497 goto error;
1498 }
1499 }
1500
1501 chan_ht = usess->domain_global.channels;
1502
1503 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
1504 if (uchan == NULL) {
1505 ret = channel_ust_create(usess, &attr, domain->buf_type);
1506 if (attr.name[0] != '\0') {
1507 usess->has_non_default_channel = 1;
1508 }
1509 } else {
1510 ret = channel_ust_enable(usess, uchan);
1511 }
1512 break;
1513 }
1514 default:
1515 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1516 goto error;
1517 }
1518
1519 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
1520 session->has_non_mmap_channel = true;
1521 }
1522 error:
1523 rcu_read_unlock();
1524 end:
1525 return ret;
1526 }
1527
1528 enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1529 struct ltt_session *session,
1530 enum lttng_domain_type domain,
1531 enum lttng_process_attr process_attr,
1532 enum lttng_tracking_policy *policy)
1533 {
1534 enum lttng_error_code ret_code = LTTNG_OK;
1535 const struct process_attr_tracker *tracker;
1536
1537 switch (domain) {
1538 case LTTNG_DOMAIN_KERNEL:
1539 if (!session->kernel_session) {
1540 ret_code = LTTNG_ERR_INVALID;
1541 goto end;
1542 }
1543 tracker = kernel_get_process_attr_tracker(
1544 session->kernel_session, process_attr);
1545 break;
1546 case LTTNG_DOMAIN_UST:
1547 if (!session->ust_session) {
1548 ret_code = LTTNG_ERR_INVALID;
1549 goto end;
1550 }
1551 tracker = trace_ust_get_process_attr_tracker(
1552 session->ust_session, process_attr);
1553 break;
1554 default:
1555 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1556 goto end;
1557 }
1558 if (tracker) {
1559 *policy = process_attr_tracker_get_tracking_policy(tracker);
1560 } else {
1561 ret_code = LTTNG_ERR_INVALID;
1562 }
1563 end:
1564 return ret_code;
1565 }
1566
1567 enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1568 struct ltt_session *session,
1569 enum lttng_domain_type domain,
1570 enum lttng_process_attr process_attr,
1571 enum lttng_tracking_policy policy)
1572 {
1573 enum lttng_error_code ret_code = LTTNG_OK;
1574
1575 switch (policy) {
1576 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1577 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1578 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1579 break;
1580 default:
1581 ret_code = LTTNG_ERR_INVALID;
1582 goto end;
1583 }
1584
1585 switch (domain) {
1586 case LTTNG_DOMAIN_KERNEL:
1587 if (!session->kernel_session) {
1588 ret_code = LTTNG_ERR_INVALID;
1589 goto end;
1590 }
1591 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1592 session->kernel_session, process_attr, policy);
1593 break;
1594 case LTTNG_DOMAIN_UST:
1595 if (!session->ust_session) {
1596 ret_code = LTTNG_ERR_INVALID;
1597 goto end;
1598 }
1599 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1600 session->ust_session, process_attr, policy);
1601 break;
1602 default:
1603 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1604 break;
1605 }
1606 end:
1607 return ret_code;
1608 }
1609
1610 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1611 struct ltt_session *session,
1612 enum lttng_domain_type domain,
1613 enum lttng_process_attr process_attr,
1614 const struct process_attr_value *value)
1615 {
1616 enum lttng_error_code ret_code = LTTNG_OK;
1617
1618 switch (domain) {
1619 case LTTNG_DOMAIN_KERNEL:
1620 if (!session->kernel_session) {
1621 ret_code = LTTNG_ERR_INVALID;
1622 goto end;
1623 }
1624 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1625 session->kernel_session, process_attr, value);
1626 break;
1627 case LTTNG_DOMAIN_UST:
1628 if (!session->ust_session) {
1629 ret_code = LTTNG_ERR_INVALID;
1630 goto end;
1631 }
1632 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1633 session->ust_session, process_attr, value);
1634 break;
1635 default:
1636 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1637 break;
1638 }
1639 end:
1640 return ret_code;
1641 }
1642
1643 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1644 struct ltt_session *session,
1645 enum lttng_domain_type domain,
1646 enum lttng_process_attr process_attr,
1647 const struct process_attr_value *value)
1648 {
1649 enum lttng_error_code ret_code = LTTNG_OK;
1650
1651 switch (domain) {
1652 case LTTNG_DOMAIN_KERNEL:
1653 if (!session->kernel_session) {
1654 ret_code = LTTNG_ERR_INVALID;
1655 goto end;
1656 }
1657 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1658 session->kernel_session, process_attr, value);
1659 break;
1660 case LTTNG_DOMAIN_UST:
1661 if (!session->ust_session) {
1662 ret_code = LTTNG_ERR_INVALID;
1663 goto end;
1664 }
1665 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1666 session->ust_session, process_attr, value);
1667 break;
1668 default:
1669 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1670 break;
1671 }
1672 end:
1673 return ret_code;
1674 }
1675
1676 enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1677 struct ltt_session *session,
1678 enum lttng_domain_type domain,
1679 enum lttng_process_attr process_attr,
1680 struct lttng_process_attr_values **values)
1681 {
1682 enum lttng_error_code ret_code = LTTNG_OK;
1683 const struct process_attr_tracker *tracker;
1684 enum process_attr_tracker_status status;
1685
1686 switch (domain) {
1687 case LTTNG_DOMAIN_KERNEL:
1688 if (!session->kernel_session) {
1689 ret_code = LTTNG_ERR_INVALID;
1690 goto end;
1691 }
1692 tracker = kernel_get_process_attr_tracker(
1693 session->kernel_session, process_attr);
1694 break;
1695 case LTTNG_DOMAIN_UST:
1696 if (!session->ust_session) {
1697 ret_code = LTTNG_ERR_INVALID;
1698 goto end;
1699 }
1700 tracker = trace_ust_get_process_attr_tracker(
1701 session->ust_session, process_attr);
1702 break;
1703 default:
1704 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1705 goto end;
1706 }
1707
1708 if (!tracker) {
1709 ret_code = LTTNG_ERR_INVALID;
1710 goto end;
1711 }
1712
1713 status = process_attr_tracker_get_inclusion_set(tracker, values);
1714 switch (status) {
1715 case PROCESS_ATTR_TRACKER_STATUS_OK:
1716 ret_code = LTTNG_OK;
1717 break;
1718 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1719 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1720 break;
1721 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1722 ret_code = LTTNG_ERR_NOMEM;
1723 break;
1724 default:
1725 ret_code = LTTNG_ERR_UNK;
1726 break;
1727 }
1728
1729 end:
1730 return ret_code;
1731 }
1732
1733 /*
1734 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1735 */
1736 int cmd_disable_event(struct ltt_session *session,
1737 enum lttng_domain_type domain, const char *channel_name,
1738 const struct lttng_event *event)
1739 {
1740 int ret;
1741 const char *event_name;
1742
1743 DBG("Disable event command for event \'%s\'", event->name);
1744
1745 event_name = event->name;
1746
1747 /* Error out on unhandled search criteria */
1748 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1749 || event->pid || event->filter || event->exclusion) {
1750 ret = LTTNG_ERR_UNK;
1751 goto error;
1752 }
1753
1754 rcu_read_lock();
1755
1756 switch (domain) {
1757 case LTTNG_DOMAIN_KERNEL:
1758 {
1759 struct ltt_kernel_channel *kchan;
1760 struct ltt_kernel_session *ksess;
1761
1762 ksess = session->kernel_session;
1763
1764 /*
1765 * If a non-default channel has been created in the
1766 * session, explicitely require that -c chan_name needs
1767 * to be provided.
1768 */
1769 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1770 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1771 goto error_unlock;
1772 }
1773
1774 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1775 if (kchan == NULL) {
1776 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1777 goto error_unlock;
1778 }
1779
1780 switch (event->type) {
1781 case LTTNG_EVENT_ALL:
1782 case LTTNG_EVENT_TRACEPOINT:
1783 case LTTNG_EVENT_SYSCALL:
1784 case LTTNG_EVENT_PROBE:
1785 case LTTNG_EVENT_FUNCTION:
1786 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1787 if (event_name[0] == '\0') {
1788 ret = event_kernel_disable_event(kchan,
1789 NULL, event->type);
1790 } else {
1791 ret = event_kernel_disable_event(kchan,
1792 event_name, event->type);
1793 }
1794 if (ret != LTTNG_OK) {
1795 goto error_unlock;
1796 }
1797 break;
1798 default:
1799 ret = LTTNG_ERR_UNK;
1800 goto error_unlock;
1801 }
1802
1803 kernel_wait_quiescent();
1804 break;
1805 }
1806 case LTTNG_DOMAIN_UST:
1807 {
1808 struct ltt_ust_channel *uchan;
1809 struct ltt_ust_session *usess;
1810
1811 usess = session->ust_session;
1812
1813 if (validate_ust_event_name(event_name)) {
1814 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1815 goto error_unlock;
1816 }
1817
1818 /*
1819 * If a non-default channel has been created in the
1820 * session, explicitly require that -c chan_name needs
1821 * to be provided.
1822 */
1823 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1824 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1825 goto error_unlock;
1826 }
1827
1828 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1829 channel_name);
1830 if (uchan == NULL) {
1831 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1832 goto error_unlock;
1833 }
1834
1835 switch (event->type) {
1836 case LTTNG_EVENT_ALL:
1837 /*
1838 * An empty event name means that everything
1839 * should be disabled.
1840 */
1841 if (event->name[0] == '\0') {
1842 ret = event_ust_disable_all_tracepoints(usess, uchan);
1843 } else {
1844 ret = event_ust_disable_tracepoint(usess, uchan,
1845 event_name);
1846 }
1847 if (ret != LTTNG_OK) {
1848 goto error_unlock;
1849 }
1850 break;
1851 default:
1852 ret = LTTNG_ERR_UNK;
1853 goto error_unlock;
1854 }
1855
1856 DBG3("Disable UST event %s in channel %s completed", event_name,
1857 channel_name);
1858 break;
1859 }
1860 case LTTNG_DOMAIN_LOG4J:
1861 case LTTNG_DOMAIN_JUL:
1862 case LTTNG_DOMAIN_PYTHON:
1863 {
1864 struct agent *agt;
1865 struct ltt_ust_session *usess = session->ust_session;
1866
1867 LTTNG_ASSERT(usess);
1868
1869 switch (event->type) {
1870 case LTTNG_EVENT_ALL:
1871 break;
1872 default:
1873 ret = LTTNG_ERR_UNK;
1874 goto error_unlock;
1875 }
1876
1877 agt = trace_ust_find_agent(usess, domain);
1878 if (!agt) {
1879 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1880 goto error_unlock;
1881 }
1882 /*
1883 * An empty event name means that everything
1884 * should be disabled.
1885 */
1886 if (event->name[0] == '\0') {
1887 ret = event_agent_disable_all(usess, agt);
1888 } else {
1889 ret = event_agent_disable(usess, agt, event_name);
1890 }
1891 if (ret != LTTNG_OK) {
1892 goto error_unlock;
1893 }
1894
1895 break;
1896 }
1897 default:
1898 ret = LTTNG_ERR_UND;
1899 goto error_unlock;
1900 }
1901
1902 ret = LTTNG_OK;
1903
1904 error_unlock:
1905 rcu_read_unlock();
1906 error:
1907 return ret;
1908 }
1909
1910 /*
1911 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1912 */
1913 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1914 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
1915 {
1916 int ret, chan_kern_created = 0, chan_ust_created = 0;
1917 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1918
1919 /*
1920 * Don't try to add a context if the session has been started at
1921 * some point in time before. The tracer does not allow it and would
1922 * result in a corrupted trace.
1923 */
1924 if (session->has_been_started) {
1925 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1926 goto end;
1927 }
1928
1929 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1930 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1931 app_ctx_name = ctx->u.app_ctx.ctx_name;
1932 }
1933
1934 switch (domain) {
1935 case LTTNG_DOMAIN_KERNEL:
1936 LTTNG_ASSERT(session->kernel_session);
1937
1938 if (session->kernel_session->channel_count == 0) {
1939 /* Create default channel */
1940 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1941 if (ret != LTTNG_OK) {
1942 goto error;
1943 }
1944 chan_kern_created = 1;
1945 }
1946 /* Add kernel context to kernel tracer */
1947 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1948 if (ret != LTTNG_OK) {
1949 goto error;
1950 }
1951 break;
1952 case LTTNG_DOMAIN_JUL:
1953 case LTTNG_DOMAIN_LOG4J:
1954 {
1955 /*
1956 * Validate channel name.
1957 * If no channel name is given and the domain is JUL or LOG4J,
1958 * set it to the appropriate domain-specific channel name. If
1959 * a name is provided but does not match the expexted channel
1960 * name, return an error.
1961 */
1962 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1963 strcmp(channel_name,
1964 DEFAULT_JUL_CHANNEL_NAME)) {
1965 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1966 goto error;
1967 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1968 strcmp(channel_name,
1969 DEFAULT_LOG4J_CHANNEL_NAME)) {
1970 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1971 goto error;
1972 }
1973 /* break is _not_ missing here. */
1974 }
1975 case LTTNG_DOMAIN_UST:
1976 {
1977 struct ltt_ust_session *usess = session->ust_session;
1978 unsigned int chan_count;
1979
1980 LTTNG_ASSERT(usess);
1981
1982 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1983 if (chan_count == 0) {
1984 struct lttng_channel *attr;
1985 /* Create default channel */
1986 attr = channel_new_default_attr(domain, usess->buffer_type);
1987 if (attr == NULL) {
1988 ret = LTTNG_ERR_FATAL;
1989 goto error;
1990 }
1991
1992 ret = channel_ust_create(usess, attr, usess->buffer_type);
1993 if (ret != LTTNG_OK) {
1994 free(attr);
1995 goto error;
1996 }
1997 channel_attr_destroy(attr);
1998 chan_ust_created = 1;
1999 }
2000
2001 ret = context_ust_add(usess, domain, ctx, channel_name);
2002 free(app_ctx_provider_name);
2003 free(app_ctx_name);
2004 app_ctx_name = NULL;
2005 app_ctx_provider_name = NULL;
2006 if (ret != LTTNG_OK) {
2007 goto error;
2008 }
2009 break;
2010 }
2011 default:
2012 ret = LTTNG_ERR_UND;
2013 goto error;
2014 }
2015
2016 ret = LTTNG_OK;
2017 goto end;
2018
2019 error:
2020 if (chan_kern_created) {
2021 struct ltt_kernel_channel *kchan =
2022 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2023 session->kernel_session);
2024 /* Created previously, this should NOT fail. */
2025 LTTNG_ASSERT(kchan);
2026 kernel_destroy_channel(kchan);
2027 }
2028
2029 if (chan_ust_created) {
2030 struct ltt_ust_channel *uchan =
2031 trace_ust_find_channel_by_name(
2032 session->ust_session->domain_global.channels,
2033 DEFAULT_CHANNEL_NAME);
2034 /* Created previously, this should NOT fail. */
2035 LTTNG_ASSERT(uchan);
2036 /* Remove from the channel list of the session. */
2037 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2038 uchan);
2039 trace_ust_destroy_channel(uchan);
2040 }
2041 end:
2042 free(app_ctx_provider_name);
2043 free(app_ctx_name);
2044 return ret;
2045 }
2046
2047 static inline bool name_starts_with(const char *name, const char *prefix)
2048 {
2049 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2050
2051 return !strncmp(name, prefix, max_cmp_len);
2052 }
2053
2054 /* Perform userspace-specific event name validation */
2055 static int validate_ust_event_name(const char *name)
2056 {
2057 int ret = 0;
2058
2059 if (!name) {
2060 ret = -1;
2061 goto end;
2062 }
2063
2064 /*
2065 * Check name against all internal UST event component namespaces used
2066 * by the agents.
2067 */
2068 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2069 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2070 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2071 ret = -1;
2072 }
2073
2074 end:
2075 return ret;
2076 }
2077
2078 /*
2079 * Internal version of cmd_enable_event() with a supplemental
2080 * "internal_event" flag which is used to enable internal events which should
2081 * be hidden from clients. Such events are used in the agent implementation to
2082 * enable the events through which all "agent" events are funeled.
2083 */
2084 static int _cmd_enable_event(struct ltt_session *session,
2085 const struct lttng_domain *domain,
2086 char *channel_name, struct lttng_event *event,
2087 char *filter_expression,
2088 struct lttng_bytecode *filter,
2089 struct lttng_event_exclusion *exclusion,
2090 int wpipe, bool internal_event)
2091 {
2092 int ret = 0, channel_created = 0;
2093 struct lttng_channel *attr = NULL;
2094
2095 LTTNG_ASSERT(session);
2096 LTTNG_ASSERT(event);
2097 LTTNG_ASSERT(channel_name);
2098
2099 /* If we have a filter, we must have its filter expression */
2100 LTTNG_ASSERT(!(!!filter_expression ^ !!filter));
2101
2102 /* Normalize event name as a globbing pattern */
2103 strutils_normalize_star_glob_pattern(event->name);
2104
2105 /* Normalize exclusion names as globbing patterns */
2106 if (exclusion) {
2107 size_t i;
2108
2109 for (i = 0; i < exclusion->count; i++) {
2110 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2111
2112 strutils_normalize_star_glob_pattern(name);
2113 }
2114 }
2115
2116 DBG("Enable event command for event \'%s\'", event->name);
2117
2118 rcu_read_lock();
2119
2120 switch (domain->type) {
2121 case LTTNG_DOMAIN_KERNEL:
2122 {
2123 struct ltt_kernel_channel *kchan;
2124
2125 /*
2126 * If a non-default channel has been created in the
2127 * session, explicitely require that -c chan_name needs
2128 * to be provided.
2129 */
2130 if (session->kernel_session->has_non_default_channel
2131 && channel_name[0] == '\0') {
2132 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2133 goto error;
2134 }
2135
2136 kchan = trace_kernel_get_channel_by_name(channel_name,
2137 session->kernel_session);
2138 if (kchan == NULL) {
2139 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2140 LTTNG_BUFFER_GLOBAL);
2141 if (attr == NULL) {
2142 ret = LTTNG_ERR_FATAL;
2143 goto error;
2144 }
2145 if (lttng_strncpy(attr->name, channel_name,
2146 sizeof(attr->name))) {
2147 ret = LTTNG_ERR_INVALID;
2148 goto error;
2149 }
2150
2151 ret = cmd_enable_channel(session, domain, attr, wpipe);
2152 if (ret != LTTNG_OK) {
2153 goto error;
2154 }
2155 channel_created = 1;
2156 }
2157
2158 /* Get the newly created kernel channel pointer */
2159 kchan = trace_kernel_get_channel_by_name(channel_name,
2160 session->kernel_session);
2161 if (kchan == NULL) {
2162 /* This sould not happen... */
2163 ret = LTTNG_ERR_FATAL;
2164 goto error;
2165 }
2166
2167 switch (event->type) {
2168 case LTTNG_EVENT_ALL:
2169 {
2170 char *filter_expression_a = NULL;
2171 struct lttng_bytecode *filter_a = NULL;
2172
2173 /*
2174 * We need to duplicate filter_expression and filter,
2175 * because ownership is passed to first enable
2176 * event.
2177 */
2178 if (filter_expression) {
2179 filter_expression_a = strdup(filter_expression);
2180 if (!filter_expression_a) {
2181 ret = LTTNG_ERR_FATAL;
2182 goto error;
2183 }
2184 }
2185 if (filter) {
2186 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2187 if (!filter_a) {
2188 free(filter_expression_a);
2189 ret = LTTNG_ERR_FATAL;
2190 goto error;
2191 }
2192 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2193 }
2194 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2195 ret = event_kernel_enable_event(kchan, event,
2196 filter_expression, filter);
2197 /* We have passed ownership */
2198 filter_expression = NULL;
2199 filter = NULL;
2200 if (ret != LTTNG_OK) {
2201 if (channel_created) {
2202 /* Let's not leak a useless channel. */
2203 kernel_destroy_channel(kchan);
2204 }
2205 free(filter_expression_a);
2206 free(filter_a);
2207 goto error;
2208 }
2209 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2210 ret = event_kernel_enable_event(kchan, event,
2211 filter_expression_a, filter_a);
2212 /* We have passed ownership */
2213 filter_expression_a = NULL;
2214 filter_a = NULL;
2215 if (ret != LTTNG_OK) {
2216 goto error;
2217 }
2218 break;
2219 }
2220 case LTTNG_EVENT_PROBE:
2221 case LTTNG_EVENT_USERSPACE_PROBE:
2222 case LTTNG_EVENT_FUNCTION:
2223 case LTTNG_EVENT_FUNCTION_ENTRY:
2224 case LTTNG_EVENT_TRACEPOINT:
2225 ret = event_kernel_enable_event(kchan, event,
2226 filter_expression, filter);
2227 /* We have passed ownership */
2228 filter_expression = NULL;
2229 filter = NULL;
2230 if (ret != LTTNG_OK) {
2231 if (channel_created) {
2232 /* Let's not leak a useless channel. */
2233 kernel_destroy_channel(kchan);
2234 }
2235 goto error;
2236 }
2237 break;
2238 case LTTNG_EVENT_SYSCALL:
2239 ret = event_kernel_enable_event(kchan, event,
2240 filter_expression, filter);
2241 /* We have passed ownership */
2242 filter_expression = NULL;
2243 filter = NULL;
2244 if (ret != LTTNG_OK) {
2245 goto error;
2246 }
2247 break;
2248 default:
2249 ret = LTTNG_ERR_UNK;
2250 goto error;
2251 }
2252
2253 kernel_wait_quiescent();
2254 break;
2255 }
2256 case LTTNG_DOMAIN_UST:
2257 {
2258 struct ltt_ust_channel *uchan;
2259 struct ltt_ust_session *usess = session->ust_session;
2260
2261 LTTNG_ASSERT(usess);
2262
2263 /*
2264 * If a non-default channel has been created in the
2265 * session, explicitely require that -c chan_name needs
2266 * to be provided.
2267 */
2268 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2269 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2270 goto error;
2271 }
2272
2273 /* Get channel from global UST domain */
2274 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2275 channel_name);
2276 if (uchan == NULL) {
2277 /* Create default channel */
2278 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2279 usess->buffer_type);
2280 if (attr == NULL) {
2281 ret = LTTNG_ERR_FATAL;
2282 goto error;
2283 }
2284 if (lttng_strncpy(attr->name, channel_name,
2285 sizeof(attr->name))) {
2286 ret = LTTNG_ERR_INVALID;
2287 goto error;
2288 }
2289
2290 ret = cmd_enable_channel(session, domain, attr, wpipe);
2291 if (ret != LTTNG_OK) {
2292 goto error;
2293 }
2294
2295 /* Get the newly created channel reference back */
2296 uchan = trace_ust_find_channel_by_name(
2297 usess->domain_global.channels, channel_name);
2298 LTTNG_ASSERT(uchan);
2299 }
2300
2301 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2302 /*
2303 * Don't allow users to add UST events to channels which
2304 * are assigned to a userspace subdomain (JUL, Log4J,
2305 * Python, etc.).
2306 */
2307 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2308 goto error;
2309 }
2310
2311 if (!internal_event) {
2312 /*
2313 * Ensure the event name is not reserved for internal
2314 * use.
2315 */
2316 ret = validate_ust_event_name(event->name);
2317 if (ret) {
2318 WARN("Userspace event name %s failed validation.",
2319 event->name);
2320 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2321 goto error;
2322 }
2323 }
2324
2325 /* At this point, the session and channel exist on the tracer */
2326 ret = event_ust_enable_tracepoint(usess, uchan, event,
2327 filter_expression, filter, exclusion,
2328 internal_event);
2329 /* We have passed ownership */
2330 filter_expression = NULL;
2331 filter = NULL;
2332 exclusion = NULL;
2333 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2334 goto already_enabled;
2335 } else if (ret != LTTNG_OK) {
2336 goto error;
2337 }
2338 break;
2339 }
2340 case LTTNG_DOMAIN_LOG4J:
2341 case LTTNG_DOMAIN_JUL:
2342 case LTTNG_DOMAIN_PYTHON:
2343 {
2344 const char *default_event_name, *default_chan_name;
2345 struct agent *agt;
2346 struct lttng_event uevent;
2347 struct lttng_domain tmp_dom;
2348 struct ltt_ust_session *usess = session->ust_session;
2349
2350 LTTNG_ASSERT(usess);
2351
2352 if (!agent_tracing_is_enabled()) {
2353 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2354 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2355 goto error;
2356 }
2357
2358 agt = trace_ust_find_agent(usess, domain->type);
2359 if (!agt) {
2360 agt = agent_create(domain->type);
2361 if (!agt) {
2362 ret = LTTNG_ERR_NOMEM;
2363 goto error;
2364 }
2365 agent_add(agt, usess->agents);
2366 }
2367
2368 /* Create the default tracepoint. */
2369 memset(&uevent, 0, sizeof(uevent));
2370 uevent.type = LTTNG_EVENT_TRACEPOINT;
2371 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2372 default_event_name = event_get_default_agent_ust_name(
2373 domain->type);
2374 if (!default_event_name) {
2375 ret = LTTNG_ERR_FATAL;
2376 goto error;
2377 }
2378 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2379 uevent.name[sizeof(uevent.name) - 1] = '\0';
2380
2381 /*
2382 * The domain type is changed because we are about to enable the
2383 * default channel and event for the JUL domain that are hardcoded.
2384 * This happens in the UST domain.
2385 */
2386 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2387 tmp_dom.type = LTTNG_DOMAIN_UST;
2388
2389 switch (domain->type) {
2390 case LTTNG_DOMAIN_LOG4J:
2391 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2392 break;
2393 case LTTNG_DOMAIN_JUL:
2394 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2395 break;
2396 case LTTNG_DOMAIN_PYTHON:
2397 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2398 break;
2399 default:
2400 /* The switch/case we are in makes this impossible */
2401 abort();
2402 }
2403
2404 {
2405 char *filter_expression_copy = NULL;
2406 struct lttng_bytecode *filter_copy = NULL;
2407
2408 if (filter) {
2409 const size_t filter_size = sizeof(
2410 struct lttng_bytecode)
2411 + filter->len;
2412
2413 filter_copy = zmalloc(filter_size);
2414 if (!filter_copy) {
2415 ret = LTTNG_ERR_NOMEM;
2416 goto error;
2417 }
2418 memcpy(filter_copy, filter, filter_size);
2419
2420 filter_expression_copy =
2421 strdup(filter_expression);
2422 if (!filter_expression) {
2423 ret = LTTNG_ERR_NOMEM;
2424 }
2425
2426 if (!filter_expression_copy || !filter_copy) {
2427 free(filter_expression_copy);
2428 free(filter_copy);
2429 goto error;
2430 }
2431 }
2432
2433 ret = cmd_enable_event_internal(session, &tmp_dom,
2434 (char *) default_chan_name,
2435 &uevent, filter_expression_copy,
2436 filter_copy, NULL, wpipe);
2437 }
2438
2439 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2440 goto already_enabled;
2441 } else if (ret != LTTNG_OK) {
2442 goto error;
2443 }
2444
2445 /* The wild card * means that everything should be enabled. */
2446 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2447 ret = event_agent_enable_all(usess, agt, event, filter,
2448 filter_expression);
2449 } else {
2450 ret = event_agent_enable(usess, agt, event, filter,
2451 filter_expression);
2452 }
2453 filter = NULL;
2454 filter_expression = NULL;
2455 if (ret != LTTNG_OK) {
2456 goto error;
2457 }
2458
2459 break;
2460 }
2461 default:
2462 ret = LTTNG_ERR_UND;
2463 goto error;
2464 }
2465
2466 ret = LTTNG_OK;
2467
2468 already_enabled:
2469 error:
2470 free(filter_expression);
2471 free(filter);
2472 free(exclusion);
2473 channel_attr_destroy(attr);
2474 rcu_read_unlock();
2475 return ret;
2476 }
2477
2478 /*
2479 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2480 * We own filter, exclusion, and filter_expression.
2481 */
2482 int cmd_enable_event(struct ltt_session *session,
2483 const struct lttng_domain *domain,
2484 char *channel_name, struct lttng_event *event,
2485 char *filter_expression,
2486 struct lttng_bytecode *filter,
2487 struct lttng_event_exclusion *exclusion,
2488 int wpipe)
2489 {
2490 return _cmd_enable_event(session, domain, channel_name, event,
2491 filter_expression, filter, exclusion, wpipe, false);
2492 }
2493
2494 /*
2495 * Enable an event which is internal to LTTng. An internal should
2496 * never be made visible to clients and are immune to checks such as
2497 * reserved names.
2498 */
2499 static int cmd_enable_event_internal(struct ltt_session *session,
2500 const struct lttng_domain *domain,
2501 char *channel_name, struct lttng_event *event,
2502 char *filter_expression,
2503 struct lttng_bytecode *filter,
2504 struct lttng_event_exclusion *exclusion,
2505 int wpipe)
2506 {
2507 return _cmd_enable_event(session, domain, channel_name, event,
2508 filter_expression, filter, exclusion, wpipe, true);
2509 }
2510
2511 /*
2512 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2513 */
2514 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2515 struct lttng_event **events)
2516 {
2517 int ret;
2518 ssize_t nb_events = 0;
2519
2520 switch (domain) {
2521 case LTTNG_DOMAIN_KERNEL:
2522 nb_events = kernel_list_events(events);
2523 if (nb_events < 0) {
2524 ret = LTTNG_ERR_KERN_LIST_FAIL;
2525 goto error;
2526 }
2527 break;
2528 case LTTNG_DOMAIN_UST:
2529 nb_events = ust_app_list_events(events);
2530 if (nb_events < 0) {
2531 ret = LTTNG_ERR_UST_LIST_FAIL;
2532 goto error;
2533 }
2534 break;
2535 case LTTNG_DOMAIN_LOG4J:
2536 case LTTNG_DOMAIN_JUL:
2537 case LTTNG_DOMAIN_PYTHON:
2538 nb_events = agent_list_events(events, domain);
2539 if (nb_events < 0) {
2540 ret = LTTNG_ERR_UST_LIST_FAIL;
2541 goto error;
2542 }
2543 break;
2544 default:
2545 ret = LTTNG_ERR_UND;
2546 goto error;
2547 }
2548
2549 return nb_events;
2550
2551 error:
2552 /* Return negative value to differentiate return code */
2553 return -ret;
2554 }
2555
2556 /*
2557 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2558 */
2559 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2560 struct lttng_event_field **fields)
2561 {
2562 int ret;
2563 ssize_t nb_fields = 0;
2564
2565 switch (domain) {
2566 case LTTNG_DOMAIN_UST:
2567 nb_fields = ust_app_list_event_fields(fields);
2568 if (nb_fields < 0) {
2569 ret = LTTNG_ERR_UST_LIST_FAIL;
2570 goto error;
2571 }
2572 break;
2573 case LTTNG_DOMAIN_KERNEL:
2574 default: /* fall-through */
2575 ret = LTTNG_ERR_UND;
2576 goto error;
2577 }
2578
2579 return nb_fields;
2580
2581 error:
2582 /* Return negative value to differentiate return code */
2583 return -ret;
2584 }
2585
2586 ssize_t cmd_list_syscalls(struct lttng_event **events)
2587 {
2588 return syscall_table_list(events);
2589 }
2590
2591 /*
2592 * Command LTTNG_START_TRACE processed by the client thread.
2593 *
2594 * Called with session mutex held.
2595 */
2596 int cmd_start_trace(struct ltt_session *session)
2597 {
2598 enum lttng_error_code ret;
2599 unsigned long nb_chan = 0;
2600 struct ltt_kernel_session *ksession;
2601 struct ltt_ust_session *usess;
2602 const bool session_rotated_after_last_stop =
2603 session->rotated_after_last_stop;
2604 const bool session_cleared_after_last_stop =
2605 session->cleared_after_last_stop;
2606
2607 LTTNG_ASSERT(session);
2608
2609 /* Ease our life a bit ;) */
2610 ksession = session->kernel_session;
2611 usess = session->ust_session;
2612
2613 /* Is the session already started? */
2614 if (session->active) {
2615 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2616 /* Perform nothing */
2617 goto end;
2618 }
2619
2620 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2621 !session->current_trace_chunk) {
2622 /*
2623 * A rotation was launched while the session was stopped and
2624 * it has not been completed yet. It is not possible to start
2625 * the session since starting the session here would require a
2626 * rotation from "NULL" to a new trace chunk. That rotation
2627 * would overlap with the ongoing rotation, which is not
2628 * supported.
2629 */
2630 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2631 session->name);
2632 ret = LTTNG_ERR_ROTATION_PENDING;
2633 goto error;
2634 }
2635
2636 /*
2637 * Starting a session without channel is useless since after that it's not
2638 * possible to enable channel thus inform the client.
2639 */
2640 if (usess && usess->domain_global.channels) {
2641 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2642 }
2643 if (ksession) {
2644 nb_chan += ksession->channel_count;
2645 }
2646 if (!nb_chan) {
2647 ret = LTTNG_ERR_NO_CHANNEL;
2648 goto error;
2649 }
2650
2651 session->active = 1;
2652 session->rotated_after_last_stop = false;
2653 session->cleared_after_last_stop = false;
2654 if (session->output_traces && !session->current_trace_chunk) {
2655 if (!session->has_been_started) {
2656 struct lttng_trace_chunk *trace_chunk;
2657
2658 DBG("Creating initial trace chunk of session \"%s\"",
2659 session->name);
2660 trace_chunk = session_create_new_trace_chunk(
2661 session, NULL, NULL, NULL);
2662 if (!trace_chunk) {
2663 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2664 goto error;
2665 }
2666 LTTNG_ASSERT(!session->current_trace_chunk);
2667 ret = session_set_trace_chunk(session, trace_chunk,
2668 NULL);
2669 lttng_trace_chunk_put(trace_chunk);
2670 if (ret) {
2671 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2672 goto error;
2673 }
2674 } else {
2675 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2676 session->name);
2677 /*
2678 * Rotate existing streams into the new chunk.
2679 * This is a "quiet" rotation has no client has
2680 * explicitly requested this operation.
2681 *
2682 * There is also no need to wait for the rotation
2683 * to complete as it will happen immediately. No data
2684 * was produced as the session was stopped, so the
2685 * rotation should happen on reception of the command.
2686 */
2687 ret = cmd_rotate_session(session, NULL, true,
2688 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2689 if (ret != LTTNG_OK) {
2690 goto error;
2691 }
2692 }
2693 }
2694
2695 /* Kernel tracing */
2696 if (ksession != NULL) {
2697 DBG("Start kernel tracing session %s", session->name);
2698 ret = start_kernel_session(ksession);
2699 if (ret != LTTNG_OK) {
2700 goto error;
2701 }
2702 }
2703
2704 /* Flag session that trace should start automatically */
2705 if (usess) {
2706 int int_ret = ust_app_start_trace_all(usess);
2707
2708 if (int_ret < 0) {
2709 ret = LTTNG_ERR_UST_START_FAIL;
2710 goto error;
2711 }
2712 }
2713
2714 /*
2715 * Open a packet in every stream of the session to ensure that viewers
2716 * can correctly identify the boundaries of the periods during which
2717 * tracing was active for this session.
2718 */
2719 ret = session_open_packets(session);
2720 if (ret != LTTNG_OK) {
2721 goto error;
2722 }
2723
2724 /*
2725 * Clear the flag that indicates that a rotation was done while the
2726 * session was stopped.
2727 */
2728 session->rotated_after_last_stop = false;
2729
2730 if (session->rotate_timer_period) {
2731 int int_ret = timer_session_rotation_schedule_timer_start(
2732 session, session->rotate_timer_period);
2733
2734 if (int_ret < 0) {
2735 ERR("Failed to enable rotate timer");
2736 ret = LTTNG_ERR_UNK;
2737 goto error;
2738 }
2739 }
2740
2741 ret = LTTNG_OK;
2742
2743 error:
2744 if (ret == LTTNG_OK) {
2745 /* Flag this after a successful start. */
2746 session->has_been_started |= 1;
2747 } else {
2748 session->active = 0;
2749 /* Restore initial state on error. */
2750 session->rotated_after_last_stop =
2751 session_rotated_after_last_stop;
2752 session->cleared_after_last_stop =
2753 session_cleared_after_last_stop;
2754 }
2755 end:
2756 return ret;
2757 }
2758
2759 /*
2760 * Command LTTNG_STOP_TRACE processed by the client thread.
2761 */
2762 int cmd_stop_trace(struct ltt_session *session)
2763 {
2764 int ret;
2765 struct ltt_kernel_session *ksession;
2766 struct ltt_ust_session *usess;
2767
2768 LTTNG_ASSERT(session);
2769
2770 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2771 /* Short cut */
2772 ksession = session->kernel_session;
2773 usess = session->ust_session;
2774
2775 /* Session is not active. Skip everythong and inform the client. */
2776 if (!session->active) {
2777 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2778 goto error;
2779 }
2780
2781 ret = stop_kernel_session(ksession);
2782 if (ret != LTTNG_OK) {
2783 goto error;
2784 }
2785
2786 if (usess && usess->active) {
2787 ret = ust_app_stop_trace_all(usess);
2788 if (ret < 0) {
2789 ret = LTTNG_ERR_UST_STOP_FAIL;
2790 goto error;
2791 }
2792 }
2793
2794 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2795 session->id);
2796 /* Flag inactive after a successful stop. */
2797 session->active = 0;
2798 ret = LTTNG_OK;
2799
2800 error:
2801 return ret;
2802 }
2803
2804 /*
2805 * Set the base_path of the session only if subdir of a control uris is set.
2806 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2807 */
2808 static int set_session_base_path_from_uris(struct ltt_session *session,
2809 size_t nb_uri,
2810 struct lttng_uri *uris)
2811 {
2812 int ret;
2813 size_t i;
2814
2815 for (i = 0; i < nb_uri; i++) {
2816 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2817 uris[i].subdir[0] == '\0') {
2818 /* Not interested in these URIs */
2819 continue;
2820 }
2821
2822 if (session->base_path != NULL) {
2823 free(session->base_path);
2824 session->base_path = NULL;
2825 }
2826
2827 /* Set session base_path */
2828 session->base_path = strdup(uris[i].subdir);
2829 if (!session->base_path) {
2830 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2831 uris[i].subdir, session->name);
2832 ret = LTTNG_ERR_NOMEM;
2833 goto error;
2834 }
2835 DBG2("Setting base path \"%s\" for session \"%s\"",
2836 session->base_path, session->name);
2837 }
2838 ret = LTTNG_OK;
2839 error:
2840 return ret;
2841 }
2842
2843 /*
2844 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2845 */
2846 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2847 struct lttng_uri *uris)
2848 {
2849 int ret, i;
2850 struct ltt_kernel_session *ksess = session->kernel_session;
2851 struct ltt_ust_session *usess = session->ust_session;
2852
2853 LTTNG_ASSERT(session);
2854 LTTNG_ASSERT(uris);
2855 LTTNG_ASSERT(nb_uri > 0);
2856
2857 /* Can't set consumer URI if the session is active. */
2858 if (session->active) {
2859 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2860 goto error;
2861 }
2862
2863 /*
2864 * Set the session base path if any. This is done inside
2865 * cmd_set_consumer_uri to preserve backward compatibility of the
2866 * previous session creation api vs the session descriptor api.
2867 */
2868 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2869 if (ret != LTTNG_OK) {
2870 goto error;
2871 }
2872
2873 /* Set the "global" consumer URIs */
2874 for (i = 0; i < nb_uri; i++) {
2875 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2876 LTTNG_DOMAIN_NONE);
2877 if (ret != LTTNG_OK) {
2878 goto error;
2879 }
2880 }
2881
2882 /* Set UST session URIs */
2883 if (session->ust_session) {
2884 for (i = 0; i < nb_uri; i++) {
2885 ret = add_uri_to_consumer(session,
2886 session->ust_session->consumer,
2887 &uris[i], LTTNG_DOMAIN_UST);
2888 if (ret != LTTNG_OK) {
2889 goto error;
2890 }
2891 }
2892 }
2893
2894 /* Set kernel session URIs */
2895 if (session->kernel_session) {
2896 for (i = 0; i < nb_uri; i++) {
2897 ret = add_uri_to_consumer(session,
2898 session->kernel_session->consumer,
2899 &uris[i], LTTNG_DOMAIN_KERNEL);
2900 if (ret != LTTNG_OK) {
2901 goto error;
2902 }
2903 }
2904 }
2905
2906 /*
2907 * Make sure to set the session in output mode after we set URI since a
2908 * session can be created without URL (thus flagged in no output mode).
2909 */
2910 session->output_traces = 1;
2911 if (ksess) {
2912 ksess->output_traces = 1;
2913 }
2914
2915 if (usess) {
2916 usess->output_traces = 1;
2917 }
2918
2919 /* All good! */
2920 ret = LTTNG_OK;
2921
2922 error:
2923 return ret;
2924 }
2925
2926 static
2927 enum lttng_error_code set_session_output_from_descriptor(
2928 struct ltt_session *session,
2929 const struct lttng_session_descriptor *descriptor)
2930 {
2931 int ret;
2932 enum lttng_error_code ret_code = LTTNG_OK;
2933 enum lttng_session_descriptor_type session_type =
2934 lttng_session_descriptor_get_type(descriptor);
2935 enum lttng_session_descriptor_output_type output_type =
2936 lttng_session_descriptor_get_output_type(descriptor);
2937 struct lttng_uri uris[2] = {};
2938 size_t uri_count = 0;
2939
2940 switch (output_type) {
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
2942 goto end;
2943 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
2944 lttng_session_descriptor_get_local_output_uri(descriptor,
2945 &uris[0]);
2946 uri_count = 1;
2947 break;
2948 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
2949 lttng_session_descriptor_get_network_output_uris(descriptor,
2950 &uris[0], &uris[1]);
2951 uri_count = 2;
2952 break;
2953 default:
2954 ret_code = LTTNG_ERR_INVALID;
2955 goto end;
2956 }
2957
2958 switch (session_type) {
2959 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
2960 {
2961 struct snapshot_output *new_output = NULL;
2962
2963 new_output = snapshot_output_alloc();
2964 if (!new_output) {
2965 ret_code = LTTNG_ERR_NOMEM;
2966 goto end;
2967 }
2968
2969 ret = snapshot_output_init_with_uri(session,
2970 DEFAULT_SNAPSHOT_MAX_SIZE,
2971 NULL, uris, uri_count, session->consumer,
2972 new_output, &session->snapshot);
2973 if (ret < 0) {
2974 ret_code = (ret == -ENOMEM) ?
2975 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
2976 snapshot_output_destroy(new_output);
2977 goto end;
2978 }
2979 snapshot_add_output(&session->snapshot, new_output);
2980 break;
2981 }
2982 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
2983 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
2984 {
2985 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
2986 break;
2987 }
2988 default:
2989 ret_code = LTTNG_ERR_INVALID;
2990 goto end;
2991 }
2992 end:
2993 return ret_code;
2994 }
2995
2996 static
2997 enum lttng_error_code cmd_create_session_from_descriptor(
2998 struct lttng_session_descriptor *descriptor,
2999 const lttng_sock_cred *creds,
3000 const char *home_path)
3001 {
3002 int ret;
3003 enum lttng_error_code ret_code;
3004 const char *session_name;
3005 struct ltt_session *new_session = NULL;
3006 enum lttng_session_descriptor_status descriptor_status;
3007
3008 session_lock_list();
3009 if (home_path) {
3010 if (*home_path != '/') {
3011 ERR("Home path provided by client is not absolute");
3012 ret_code = LTTNG_ERR_INVALID;
3013 goto end;
3014 }
3015 }
3016
3017 descriptor_status = lttng_session_descriptor_get_session_name(
3018 descriptor, &session_name);
3019 switch (descriptor_status) {
3020 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3021 break;
3022 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3023 session_name = NULL;
3024 break;
3025 default:
3026 ret_code = LTTNG_ERR_INVALID;
3027 goto end;
3028 }
3029
3030 ret_code = session_create(session_name, creds->uid, creds->gid,
3031 &new_session);
3032 if (ret_code != LTTNG_OK) {
3033 goto end;
3034 }
3035
3036 if (!session_name) {
3037 ret = lttng_session_descriptor_set_session_name(descriptor,
3038 new_session->name);
3039 if (ret) {
3040 ret_code = LTTNG_ERR_SESSION_FAIL;
3041 goto end;
3042 }
3043 }
3044
3045 if (!lttng_session_descriptor_is_output_destination_initialized(
3046 descriptor)) {
3047 /*
3048 * Only include the session's creation time in the output
3049 * destination if the name of the session itself was
3050 * not auto-generated.
3051 */
3052 ret_code = lttng_session_descriptor_set_default_output(
3053 descriptor,
3054 session_name ? &new_session->creation_time : NULL,
3055 home_path);
3056 if (ret_code != LTTNG_OK) {
3057 goto end;
3058 }
3059 } else {
3060 new_session->has_user_specified_directory =
3061 lttng_session_descriptor_has_output_directory(
3062 descriptor);
3063 }
3064
3065 switch (lttng_session_descriptor_get_type(descriptor)) {
3066 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3067 new_session->snapshot_mode = 1;
3068 break;
3069 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3070 new_session->live_timer =
3071 lttng_session_descriptor_live_get_timer_interval(
3072 descriptor);
3073 break;
3074 default:
3075 break;
3076 }
3077
3078 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3079 if (ret_code != LTTNG_OK) {
3080 goto end;
3081 }
3082 new_session->consumer->enabled = 1;
3083 ret_code = LTTNG_OK;
3084 end:
3085 /* Release reference provided by the session_create function. */
3086 session_put(new_session);
3087 if (ret_code != LTTNG_OK && new_session) {
3088 /* Release the global reference on error. */
3089 session_destroy(new_session);
3090 }
3091 session_unlock_list();
3092 return ret_code;
3093 }
3094
3095 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3096 struct lttng_session_descriptor **return_descriptor)
3097 {
3098 int ret;
3099 size_t payload_size;
3100 struct lttng_dynamic_buffer payload;
3101 struct lttng_buffer_view home_dir_view;
3102 struct lttng_buffer_view session_descriptor_view;
3103 struct lttng_session_descriptor *session_descriptor = NULL;
3104 enum lttng_error_code ret_code;
3105
3106 lttng_dynamic_buffer_init(&payload);
3107 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
3108 LTTNG_PATH_MAX) {
3109 ret_code = LTTNG_ERR_INVALID;
3110 goto error;
3111 }
3112 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3113 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3114 ret_code = LTTNG_ERR_INVALID;
3115 goto error;
3116 }
3117
3118 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3119 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3120 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3121 if (ret) {
3122 ret_code = LTTNG_ERR_NOMEM;
3123 goto error;
3124 }
3125
3126 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3127 if (ret <= 0) {
3128 ERR("Reception of session descriptor failed, aborting.");
3129 ret_code = LTTNG_ERR_SESSION_FAIL;
3130 goto error;
3131 }
3132
3133 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3134 &payload,
3135 0,
3136 cmd_ctx->lsm.u.create_session.home_dir_size);
3137 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3138 !lttng_buffer_view_is_valid(&home_dir_view)) {
3139 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3140 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3141 goto error;
3142 }
3143
3144 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3145 &payload,
3146 cmd_ctx->lsm.u.create_session.home_dir_size,
3147 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3148 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3149 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3150 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3151 goto error;
3152 }
3153
3154 ret = lttng_session_descriptor_create_from_buffer(
3155 &session_descriptor_view, &session_descriptor);
3156 if (ret < 0) {
3157 ERR("Failed to create session descriptor from payload of \"create session\" command");
3158 ret_code = LTTNG_ERR_INVALID;
3159 goto error;
3160 }
3161
3162 /*
3163 * Sets the descriptor's auto-generated properties (name, output) if
3164 * needed.
3165 */
3166 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3167 &cmd_ctx->creds,
3168 home_dir_view.size ? home_dir_view.data : NULL);
3169 if (ret_code != LTTNG_OK) {
3170 goto error;
3171 }
3172
3173 ret_code = LTTNG_OK;
3174 *return_descriptor = session_descriptor;
3175 session_descriptor = NULL;
3176 error:
3177 lttng_dynamic_buffer_reset(&payload);
3178 lttng_session_descriptor_destroy(session_descriptor);
3179 return ret_code;
3180 }
3181
3182 static
3183 void cmd_destroy_session_reply(const struct ltt_session *session,
3184 void *_reply_context)
3185 {
3186 int ret;
3187 ssize_t comm_ret;
3188 const struct cmd_destroy_session_reply_context *reply_context =
3189 _reply_context;
3190 struct lttng_dynamic_buffer payload;
3191 struct lttcomm_session_destroy_command_header cmd_header;
3192 struct lttng_trace_archive_location *location = NULL;
3193 struct lttcomm_lttng_msg llm = {
3194 .cmd_type = LTTNG_DESTROY_SESSION,
3195 .ret_code = reply_context->destruction_status,
3196 .pid = UINT32_MAX,
3197 .cmd_header_size =
3198 sizeof(struct lttcomm_session_destroy_command_header),
3199 .data_size = 0,
3200 };
3201 size_t payload_size_before_location;
3202
3203 lttng_dynamic_buffer_init(&payload);
3204
3205 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3206 if (ret) {
3207 ERR("Failed to append session destruction message");
3208 goto error;
3209 }
3210
3211 cmd_header.rotation_state =
3212 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3213 session->rotation_state :
3214 LTTNG_ROTATION_STATE_NO_ROTATION);
3215 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3216 sizeof(cmd_header));
3217 if (ret) {
3218 ERR("Failed to append session destruction command header");
3219 goto error;
3220 }
3221
3222 if (!reply_context->implicit_rotation_on_destroy) {
3223 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3224 session->name);
3225 goto send_reply;
3226 }
3227 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3228 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3229 session->name);
3230 goto send_reply;
3231 }
3232
3233 location = session_get_trace_archive_location(session);
3234 if (!location) {
3235 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3236 session->name);
3237 goto error;
3238 }
3239
3240 payload_size_before_location = payload.size;
3241 comm_ret = lttng_trace_archive_location_serialize(location,
3242 &payload);
3243 lttng_trace_archive_location_put(location);
3244 if (comm_ret < 0) {
3245 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3246 session->name);
3247 goto error;
3248 }
3249 /* Update the message to indicate the location's length. */
3250 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3251 payload.size - payload_size_before_location;
3252 send_reply:
3253 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3254 payload.data, payload.size);
3255 if (comm_ret != (ssize_t) payload.size) {
3256 ERR("Failed to send result of the destruction of session \"%s\" to client",
3257 session->name);
3258 }
3259 error:
3260 ret = close(reply_context->reply_sock_fd);
3261 if (ret) {
3262 PERROR("Failed to close client socket in deferred session destroy reply");
3263 }
3264 lttng_dynamic_buffer_reset(&payload);
3265 free(_reply_context);
3266 }
3267
3268 /*
3269 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3270 *
3271 * Called with session lock held.
3272 */
3273 int cmd_destroy_session(struct ltt_session *session,
3274 struct notification_thread_handle *notification_thread_handle,
3275 int *sock_fd)
3276 {
3277 int ret;
3278 enum lttng_error_code destruction_last_error = LTTNG_OK;
3279 struct cmd_destroy_session_reply_context *reply_context = NULL;
3280
3281 if (sock_fd) {
3282 reply_context = zmalloc(sizeof(*reply_context));
3283 if (!reply_context) {
3284 ret = LTTNG_ERR_NOMEM;
3285 goto end;
3286 }
3287 reply_context->reply_sock_fd = *sock_fd;
3288 }
3289
3290 /* Safety net */
3291 LTTNG_ASSERT(session);
3292
3293 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name,
3294 session->id);
3295 if (session->active) {
3296 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3297 session->name);
3298 ret = cmd_stop_trace(session);
3299 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3300 /* Carry on with the destruction of the session. */
3301 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3302 session->name, lttng_strerror(-ret));
3303 destruction_last_error = ret;
3304 }
3305 }
3306
3307 if (session->rotation_schedule_timer_enabled) {
3308 if (timer_session_rotation_schedule_timer_stop(
3309 session)) {
3310 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3311 session->name);
3312 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3313 }
3314 }
3315
3316 if (session->rotate_size) {
3317 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3318 session->rotate_size = 0;
3319 }
3320
3321 if (session->rotated && session->current_trace_chunk && session->output_traces) {
3322 /*
3323 * Perform a last rotation on destruction if rotations have
3324 * occurred during the session's lifetime.
3325 */
3326 ret = cmd_rotate_session(session, NULL, false,
3327 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
3328 if (ret != LTTNG_OK) {
3329 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3330 session->name, lttng_strerror(-ret));
3331 destruction_last_error = -ret;
3332 }
3333 if (reply_context) {
3334 reply_context->implicit_rotation_on_destroy = true;
3335 }
3336 } else if (session->has_been_started && session->current_trace_chunk) {
3337 /*
3338 * The user has not triggered a session rotation. However, to
3339 * ensure all data has been consumed, the session is rotated
3340 * to a 'null' trace chunk before it is destroyed.
3341 *
3342 * This is a "quiet" rotation meaning that no notification is
3343 * emitted and no renaming of the current trace chunk takes
3344 * place.
3345 */
3346 ret = cmd_rotate_session(session, NULL, true,
3347 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
3348 /*
3349 * Rotation operations may not be supported by the kernel
3350 * tracer. Hence, do not consider this implicit rotation as
3351 * a session destruction error. The library has already stopped
3352 * the session and waited for pending data; there is nothing
3353 * left to do but complete the destruction of the session.
3354 */
3355 if (ret != LTTNG_OK &&
3356 ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
3357 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3358 session->name, lttng_strerror(ret));
3359 destruction_last_error = -ret;
3360 }
3361 }
3362
3363 if (session->shm_path[0]) {
3364 /*
3365 * When a session is created with an explicit shm_path,
3366 * the consumer daemon will create its shared memory files
3367 * at that location and will *not* unlink them. This is normal
3368 * as the intention of that feature is to make it possible
3369 * to retrieve the content of those files should a crash occur.
3370 *
3371 * To ensure the content of those files can be used, the
3372 * sessiond daemon will replicate the content of the metadata
3373 * cache in a metadata file.
3374 *
3375 * On clean-up, it is expected that the consumer daemon will
3376 * unlink the shared memory files and that the session daemon
3377 * will unlink the metadata file. Then, the session's directory
3378 * in the shm path can be removed.
3379 *
3380 * Unfortunately, a flaw in the design of the sessiond's and
3381 * consumerd's tear down of channels makes it impossible to
3382 * determine when the sessiond _and_ the consumerd have both
3383 * destroyed their representation of a channel. For one, the
3384 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3385 * callbacks in both daemons.
3386 *
3387 * However, it is also impossible for the sessiond to know when
3388 * the consumer daemon is done destroying its channel(s) since
3389 * it occurs as a reaction to the closing of the channel's file
3390 * descriptor. There is no resulting communication initiated
3391 * from the consumerd to the sessiond to confirm that the
3392 * operation is completed (and was successful).
3393 *
3394 * Until this is all fixed, the session daemon checks for the
3395 * removal of the session's shm path which makes it possible
3396 * to safely advertise a session as having been destroyed.
3397 *
3398 * Prior to this fix, it was not possible to reliably save
3399 * a session making use of the --shm-path option, destroy it,
3400 * and load it again. This is because the creation of the
3401 * session would fail upon seeing the session's shm path
3402 * already in existence.
3403 *
3404 * Note that none of the error paths in the check for the
3405 * directory's existence return an error. This is normal
3406 * as there isn't much that can be done. The session will
3407 * be destroyed properly, except that we can't offer the
3408 * guarantee that the same session can be re-created.
3409 */
3410 current_completion_handler = &destroy_completion_handler.handler;
3411 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3412 session->shm_path,
3413 sizeof(destroy_completion_handler.shm_path));
3414 LTTNG_ASSERT(!ret);
3415 }
3416
3417 /*
3418 * The session is destroyed. However, note that the command context
3419 * still holds a reference to the session, thus delaying its destruction
3420 * _at least_ up to the point when that reference is released.
3421 */
3422 session_destroy(session);
3423 if (reply_context) {
3424 reply_context->destruction_status = destruction_last_error;
3425 ret = session_add_destroy_notifier(session,
3426 cmd_destroy_session_reply,
3427 (void *) reply_context);
3428 if (ret) {
3429 ret = LTTNG_ERR_FATAL;
3430 goto end;
3431 } else {
3432 *sock_fd = -1;
3433 }
3434 }
3435 ret = LTTNG_OK;
3436 end:
3437 return ret;
3438 }
3439
3440 /*
3441 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3442 */
3443 int cmd_register_consumer(struct ltt_session *session,
3444 enum lttng_domain_type domain, const char *sock_path,
3445 struct consumer_data *cdata)
3446 {
3447 int ret, sock;
3448 struct consumer_socket *socket = NULL;
3449
3450 LTTNG_ASSERT(session);
3451 LTTNG_ASSERT(cdata);
3452 LTTNG_ASSERT(sock_path);
3453
3454 switch (domain) {
3455 case LTTNG_DOMAIN_KERNEL:
3456 {
3457 struct ltt_kernel_session *ksess = session->kernel_session;
3458
3459 LTTNG_ASSERT(ksess);
3460
3461 /* Can't register a consumer if there is already one */
3462 if (ksess->consumer_fds_sent != 0) {
3463 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3464 goto error;
3465 }
3466
3467 sock = lttcomm_connect_unix_sock(sock_path);
3468 if (sock < 0) {
3469 ret = LTTNG_ERR_CONNECT_FAIL;
3470 goto error;
3471 }
3472 cdata->cmd_sock = sock;
3473
3474 socket = consumer_allocate_socket(&cdata->cmd_sock);
3475 if (socket == NULL) {
3476 ret = close(sock);
3477 if (ret < 0) {
3478 PERROR("close register consumer");
3479 }
3480 cdata->cmd_sock = -1;
3481 ret = LTTNG_ERR_FATAL;
3482 goto error;
3483 }
3484
3485 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3486 if (socket->lock == NULL) {
3487 PERROR("zmalloc pthread mutex");
3488 ret = LTTNG_ERR_FATAL;
3489 goto error;
3490 }
3491 pthread_mutex_init(socket->lock, NULL);
3492 socket->registered = 1;
3493
3494 rcu_read_lock();
3495 consumer_add_socket(socket, ksess->consumer);
3496 rcu_read_unlock();
3497
3498 pthread_mutex_lock(&cdata->pid_mutex);
3499 cdata->pid = -1;
3500 pthread_mutex_unlock(&cdata->pid_mutex);
3501
3502 break;
3503 }
3504 default:
3505 /* TODO: Userspace tracing */
3506 ret = LTTNG_ERR_UND;
3507 goto error;
3508 }
3509
3510 return LTTNG_OK;
3511
3512 error:
3513 if (socket) {
3514 consumer_destroy_socket(socket);
3515 }
3516 return ret;
3517 }
3518
3519 /*
3520 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3521 */
3522 ssize_t cmd_list_domains(struct ltt_session *session,
3523 struct lttng_domain **domains)
3524 {
3525 int ret, index = 0;
3526 ssize_t nb_dom = 0;
3527 struct agent *agt;
3528 struct lttng_ht_iter iter;
3529
3530 if (session->kernel_session != NULL) {
3531 DBG3("Listing domains found kernel domain");
3532 nb_dom++;
3533 }
3534
3535 if (session->ust_session != NULL) {
3536 DBG3("Listing domains found UST global domain");
3537 nb_dom++;
3538
3539 rcu_read_lock();
3540 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3541 agt, node.node) {
3542 if (agt->being_used) {
3543 nb_dom++;
3544 }
3545 }
3546 rcu_read_unlock();
3547 }
3548
3549 if (!nb_dom) {
3550 goto end;
3551 }
3552
3553 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3554 if (*domains == NULL) {
3555 ret = LTTNG_ERR_FATAL;
3556 goto error;
3557 }
3558
3559 if (session->kernel_session != NULL) {
3560 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3561
3562 /* Kernel session buffer type is always GLOBAL */
3563 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3564
3565 index++;
3566 }
3567
3568 if (session->ust_session != NULL) {
3569 (*domains)[index].type = LTTNG_DOMAIN_UST;
3570 (*domains)[index].buf_type = session->ust_session->buffer_type;
3571 index++;
3572
3573 rcu_read_lock();
3574 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3575 agt, node.node) {
3576 if (agt->being_used) {
3577 (*domains)[index].type = agt->domain;
3578 (*domains)[index].buf_type = session->ust_session->buffer_type;
3579 index++;
3580 }
3581 }
3582 rcu_read_unlock();
3583 }
3584 end:
3585 return nb_dom;
3586
3587 error:
3588 /* Return negative value to differentiate return code */
3589 return -ret;
3590 }
3591
3592
3593 /*
3594 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3595 */
3596 ssize_t cmd_list_channels(enum lttng_domain_type domain,
3597 struct ltt_session *session, struct lttng_channel **channels)
3598 {
3599 ssize_t nb_chan = 0, payload_size = 0, ret;
3600
3601 switch (domain) {
3602 case LTTNG_DOMAIN_KERNEL:
3603 if (session->kernel_session != NULL) {
3604 nb_chan = session->kernel_session->channel_count;
3605 }
3606 DBG3("Number of kernel channels %zd", nb_chan);
3607 if (nb_chan <= 0) {
3608 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
3609 goto end;
3610 }
3611 break;
3612 case LTTNG_DOMAIN_UST:
3613 if (session->ust_session != NULL) {
3614 rcu_read_lock();
3615 nb_chan = lttng_ht_get_count(
3616 session->ust_session->domain_global.channels);
3617 rcu_read_unlock();
3618 }
3619 DBG3("Number of UST global channels %zd", nb_chan);
3620 if (nb_chan < 0) {
3621 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
3622 goto end;
3623 }
3624 break;
3625 default:
3626 ret = -LTTNG_ERR_UND;
3627 goto end;
3628 }
3629
3630 if (nb_chan > 0) {
3631 const size_t channel_size = sizeof(struct lttng_channel) +
3632 sizeof(struct lttng_channel_extended);
3633 struct lttng_channel_extended *channel_exts;
3634
3635 payload_size = nb_chan * channel_size;
3636 *channels = zmalloc(payload_size);
3637 if (*channels == NULL) {
3638 ret = -LTTNG_ERR_FATAL;
3639 goto end;
3640 }
3641
3642 channel_exts = ((void *) *channels) +
3643 (nb_chan * sizeof(struct lttng_channel));
3644 ret = list_lttng_channels(domain, session, *channels, channel_exts);
3645 if (ret != LTTNG_OK) {
3646 free(*channels);
3647 *channels = NULL;
3648 goto end;
3649 }
3650 } else {
3651 *channels = NULL;
3652 }
3653
3654 ret = payload_size;
3655 end:
3656 return ret;
3657 }
3658
3659 /*
3660 * Command LTTNG_LIST_EVENTS processed by the client thread.
3661 */
3662 ssize_t cmd_list_events(enum lttng_domain_type domain,
3663 struct ltt_session *session, char *channel_name,
3664 struct lttng_payload *payload)
3665 {
3666 int ret = 0;
3667 ssize_t nb_events = 0;
3668 struct lttcomm_event_command_header cmd_header = {};
3669 const size_t cmd_header_offset = payload->buffer.size;
3670
3671 ret = lttng_dynamic_buffer_append(
3672 &payload->buffer, &cmd_header, sizeof(cmd_header));
3673 if (ret) {
3674 ret = LTTNG_ERR_NOMEM;
3675 goto error;
3676 }
3677
3678 switch (domain) {
3679 case LTTNG_DOMAIN_KERNEL:
3680 if (session->kernel_session != NULL) {
3681 nb_events = list_lttng_kernel_events(channel_name,
3682 session->kernel_session, payload);
3683 }
3684 break;
3685 case LTTNG_DOMAIN_UST:
3686 {
3687 if (session->ust_session != NULL) {
3688 nb_events = list_lttng_ust_global_events(channel_name,
3689 &session->ust_session->domain_global,
3690 payload);
3691 }
3692 break;
3693 }
3694 case LTTNG_DOMAIN_LOG4J:
3695 case LTTNG_DOMAIN_JUL:
3696 case LTTNG_DOMAIN_PYTHON:
3697 if (session->ust_session) {
3698 struct lttng_ht_iter iter;
3699 struct agent *agt;
3700
3701 rcu_read_lock();
3702 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3703 &iter.iter, agt, node.node) {
3704 if (agt->domain == domain) {
3705 nb_events = list_lttng_agent_events(
3706 agt, payload);
3707 break;
3708 }
3709 }
3710 rcu_read_unlock();
3711 }
3712 break;
3713 default:
3714 ret = LTTNG_ERR_UND;
3715 goto error;
3716 }
3717
3718 ((struct lttcomm_event_command_header *) (payload->buffer.data +
3719 cmd_header_offset))->nb_events = (uint32_t) nb_events;
3720
3721 return nb_events;
3722
3723 error:
3724 /* Return negative value to differentiate return code */
3725 return -ret;
3726 }
3727
3728 /*
3729 * Using the session list, filled a lttng_session array to send back to the
3730 * client for session listing.
3731 *
3732 * The session list lock MUST be acquired before calling this function. Use
3733 * session_lock_list() and session_unlock_list().
3734 */
3735 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3736 size_t session_count, uid_t uid, gid_t gid)
3737 {
3738 int ret;
3739 unsigned int i = 0;
3740 struct ltt_session *session;
3741 struct ltt_session_list *list = session_get_list();
3742 struct lttng_session_extended *extended =
3743 (typeof(extended)) (&sessions[session_count]);
3744
3745 DBG("Getting all available session for UID %d GID %d",
3746 uid, gid);
3747 /*
3748 * Iterate over session list and append data after the control struct in
3749 * the buffer.
3750 */
3751 cds_list_for_each_entry(session, &list->head, list) {
3752 if (!session_get(session)) {
3753 continue;
3754 }
3755 /*
3756 * Only list the sessions the user can control.
3757 */
3758 if (!session_access_ok(session, uid) ||
3759 session->destroyed) {
3760 session_put(session);
3761 continue;
3762 }
3763
3764 struct ltt_kernel_session *ksess = session->kernel_session;
3765 struct ltt_ust_session *usess = session->ust_session;
3766
3767 if (session->consumer->type == CONSUMER_DST_NET ||
3768 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3769 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3770 ret = build_network_session_path(sessions[i].path,
3771 sizeof(sessions[i].path), session);
3772 } else {
3773 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
3774 session->consumer->dst.session_root_path);
3775 }
3776 if (ret < 0) {
3777 PERROR("snprintf session path");
3778 session_put(session);
3779 continue;
3780 }
3781
3782 strncpy(sessions[i].name, session->name, NAME_MAX);
3783 sessions[i].name[NAME_MAX - 1] = '\0';
3784 sessions[i].enabled = session->active;
3785 sessions[i].snapshot_mode = session->snapshot_mode;
3786 sessions[i].live_timer_interval = session->live_timer;
3787 extended[i].creation_time.value = (uint64_t) session->creation_time;
3788 extended[i].creation_time.is_set = 1;
3789 i++;
3790 session_put(session);
3791 }
3792 }
3793
3794 /*
3795 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3796 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3797 */
3798 int cmd_data_pending(struct ltt_session *session)
3799 {
3800 int ret;
3801 struct ltt_kernel_session *ksess = session->kernel_session;
3802 struct ltt_ust_session *usess = session->ust_session;
3803
3804 LTTNG_ASSERT(session);
3805
3806 DBG("Data pending for session %s", session->name);
3807
3808 /* Session MUST be stopped to ask for data availability. */
3809 if (session->active) {
3810 ret = LTTNG_ERR_SESSION_STARTED;
3811 goto error;
3812 } else {
3813 /*
3814 * If stopped, just make sure we've started before else the above call
3815 * will always send that there is data pending.
3816 *
3817 * The consumer assumes that when the data pending command is received,
3818 * the trace has been started before or else no output data is written
3819 * by the streams which is a condition for data pending. So, this is
3820 * *VERY* important that we don't ask the consumer before a start
3821 * trace.
3822 */
3823 if (!session->has_been_started) {
3824 ret = 0;
3825 goto error;
3826 }
3827 }
3828
3829 /* A rotation is still pending, we have to wait. */
3830 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
3831 DBG("Rotate still pending for session %s", session->name);
3832 ret = 1;
3833 goto error;
3834 }
3835
3836 if (ksess && ksess->consumer) {
3837 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3838 if (ret == 1) {
3839 /* Data is still being extracted for the kernel. */
3840 goto error;
3841 }
3842 }
3843
3844 if (usess && usess->consumer) {
3845 ret = consumer_is_data_pending(usess->id, usess->consumer);
3846 if (ret == 1) {
3847 /* Data is still being extracted for the kernel. */
3848 goto error;
3849 }
3850 }
3851
3852 /* Data is ready to be read by a viewer */
3853 ret = 0;
3854
3855 error:
3856 return ret;
3857 }
3858
3859 /*
3860 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3861 *
3862 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3863 */
3864 int cmd_snapshot_add_output(struct ltt_session *session,
3865 const struct lttng_snapshot_output *output, uint32_t *id)
3866 {
3867 int ret;
3868 struct snapshot_output *new_output;
3869
3870 LTTNG_ASSERT(session);
3871 LTTNG_ASSERT(output);
3872
3873 DBG("Cmd snapshot add output for session %s", session->name);
3874
3875 /*
3876 * Can't create an output if the session is not set in no-output mode.
3877 */
3878 if (session->output_traces) {
3879 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3880 goto error;
3881 }
3882
3883 if (session->has_non_mmap_channel) {
3884 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
3885 goto error;
3886 }
3887
3888 /* Only one output is allowed until we have the "tee" feature. */
3889 if (session->snapshot.nb_output == 1) {
3890 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3891 goto error;
3892 }
3893
3894 new_output = snapshot_output_alloc();
3895 if (!new_output) {
3896 ret = LTTNG_ERR_NOMEM;
3897 goto error;
3898 }
3899
3900 ret = snapshot_output_init(session, output->max_size, output->name,
3901 output->ctrl_url, output->data_url, session->consumer, new_output,
3902 &session->snapshot);
3903 if (ret < 0) {
3904 if (ret == -ENOMEM) {
3905 ret = LTTNG_ERR_NOMEM;
3906 } else {
3907 ret = LTTNG_ERR_INVALID;
3908 }
3909 goto free_error;
3910 }
3911
3912 rcu_read_lock();
3913 snapshot_add_output(&session->snapshot, new_output);
3914 if (id) {
3915 *id = new_output->id;
3916 }
3917 rcu_read_unlock();
3918
3919 return LTTNG_OK;
3920
3921 free_error:
3922 snapshot_output_destroy(new_output);
3923 error:
3924 return ret;
3925 }
3926
3927 /*
3928 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3929 *
3930 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3931 */
3932 int cmd_snapshot_del_output(struct ltt_session *session,
3933 const struct lttng_snapshot_output *output)
3934 {
3935 int ret;
3936 struct snapshot_output *sout = NULL;
3937
3938 LTTNG_ASSERT(session);
3939 LTTNG_ASSERT(output);
3940
3941 rcu_read_lock();
3942
3943 /*
3944 * Permission denied to create an output if the session is not
3945 * set in no output mode.
3946 */
3947 if (session->output_traces) {
3948 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3949 goto error;
3950 }
3951
3952 if (output->id) {
3953 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3954 session->name);
3955 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3956 } else if (*output->name != '\0') {
3957 DBG("Cmd snapshot del output name %s for session %s", output->name,
3958 session->name);
3959 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3960 }
3961 if (!sout) {
3962 ret = LTTNG_ERR_INVALID;
3963 goto error;
3964 }
3965
3966 snapshot_delete_output(&session->snapshot, sout);
3967 snapshot_output_destroy(sout);
3968 ret = LTTNG_OK;
3969
3970 error:
3971 rcu_read_unlock();
3972 return ret;
3973 }
3974
3975 /*
3976 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3977 *
3978 * If no output is available, outputs is untouched and 0 is returned.
3979 *
3980 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3981 */
3982 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3983 struct lttng_snapshot_output **outputs)
3984 {
3985 int ret, idx = 0;
3986 struct lttng_snapshot_output *list = NULL;
3987 struct lttng_ht_iter iter;
3988 struct snapshot_output *output;
3989
3990 LTTNG_ASSERT(session);
3991 LTTNG_ASSERT(outputs);
3992
3993 DBG("Cmd snapshot list outputs for session %s", session->name);
3994
3995 /*
3996 * Permission denied to create an output if the session is not
3997 * set in no output mode.
3998 */
3999 if (session->output_traces) {
4000 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4001 goto end;
4002 }
4003
4004 if (session->snapshot.nb_output == 0) {
4005 ret = 0;
4006 goto end;
4007 }
4008
4009 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
4010 if (!list) {
4011 ret = -LTTNG_ERR_NOMEM;
4012 goto end;
4013 }
4014
4015 /* Copy list from session to the new list object. */
4016 rcu_read_lock();
4017 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
4018 output, node.node) {
4019 LTTNG_ASSERT(output->consumer);
4020 list[idx].id = output->id;
4021 list[idx].max_size = output->max_size;
4022 if (lttng_strncpy(list[idx].name, output->name,
4023 sizeof(list[idx].name))) {
4024 ret = -LTTNG_ERR_INVALID;
4025 goto error;
4026 }
4027 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4028 if (lttng_strncpy(list[idx].ctrl_url,
4029 output->consumer->dst.session_root_path,
4030 sizeof(list[idx].ctrl_url))) {
4031 ret = -LTTNG_ERR_INVALID;
4032 goto error;
4033 }
4034 } else {
4035 /* Control URI. */
4036 ret = uri_to_str_url(&output->consumer->dst.net.control,
4037 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
4038 if (ret < 0) {
4039 ret = -LTTNG_ERR_NOMEM;
4040 goto error;
4041 }
4042
4043 /* Data URI. */
4044 ret = uri_to_str_url(&output->consumer->dst.net.data,
4045 list[idx].data_url, sizeof(list[idx].data_url));
4046 if (ret < 0) {
4047 ret = -LTTNG_ERR_NOMEM;
4048 goto error;
4049 }
4050 }
4051 idx++;
4052 }
4053
4054 *outputs = list;
4055 list = NULL;
4056 ret = session->snapshot.nb_output;
4057 error:
4058 rcu_read_unlock();
4059 free(list);
4060 end:
4061 return ret;
4062 }
4063
4064 /*
4065 * Check if we can regenerate the metadata for this session.
4066 * Only kernel, UST per-uid and non-live sessions are supported.
4067 *
4068 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4069 */
4070 static
4071 int check_regenerate_metadata_support(struct ltt_session *session)
4072 {
4073 int ret;
4074
4075 LTTNG_ASSERT(session);
4076
4077 if (session->live_timer != 0) {
4078 ret = LTTNG_ERR_LIVE_SESSION;
4079 goto end;
4080 }
4081 if (!session->active) {
4082 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4083 goto end;
4084 }
4085 if (session->ust_session) {
4086 switch (session->ust_session->buffer_type) {
4087 case LTTNG_BUFFER_PER_UID:
4088 break;
4089 case LTTNG_BUFFER_PER_PID:
4090 ret = LTTNG_ERR_PER_PID_SESSION;
4091 goto end;
4092 default:
4093 abort();
4094 ret = LTTNG_ERR_UNK;
4095 goto end;
4096 }
4097 }
4098 if (session->consumer->type == CONSUMER_DST_NET &&
4099 session->consumer->relay_minor_version < 8) {
4100 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4101 goto end;
4102 }
4103 ret = 0;
4104
4105 end:
4106 return ret;
4107 }
4108
4109 static
4110 int clear_metadata_file(int fd)
4111 {
4112 int ret;
4113 off_t lseek_ret;
4114
4115 lseek_ret = lseek(fd, 0, SEEK_SET);
4116 if (lseek_ret < 0) {
4117 PERROR("lseek");
4118 ret = -1;
4119 goto end;
4120 }
4121
4122 ret = ftruncate(fd, 0);
4123 if (ret < 0) {
4124 PERROR("ftruncate");
4125 goto end;
4126 }
4127
4128 end:
4129 return ret;
4130 }
4131
4132 static
4133 int ust_regenerate_metadata(struct ltt_ust_session *usess)
4134 {
4135 int ret = 0;
4136 struct buffer_reg_uid *uid_reg = NULL;
4137 struct buffer_reg_session *session_reg = NULL;
4138
4139 rcu_read_lock();
4140 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
4141 struct ust_registry_session *registry;
4142 struct ust_registry_channel *chan;
4143 struct lttng_ht_iter iter_chan;
4144
4145 session_reg = uid_reg->registry;
4146 registry = session_reg->reg.ust;
4147
4148 pthread_mutex_lock(&registry->lock);
4149 registry->metadata_len_sent = 0;
4150 memset(registry->metadata, 0, registry->metadata_alloc_len);
4151 registry->metadata_len = 0;
4152 registry->metadata_version++;
4153 if (registry->metadata_fd > 0) {
4154 /* Clear the metadata file's content. */
4155 ret = clear_metadata_file(registry->metadata_fd);
4156 if (ret) {
4157 pthread_mutex_unlock(&registry->lock);
4158 goto end;
4159 }
4160 }
4161
4162 ret = ust_metadata_session_statedump(registry, NULL,
4163 registry->major, registry->minor);
4164 if (ret) {
4165 pthread_mutex_unlock(&registry->lock);
4166 ERR("Failed to generate session metadata (err = %d)",
4167 ret);
4168 goto end;
4169 }
4170 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4171 chan, node.node) {
4172 struct ust_registry_event *event;
4173 struct lttng_ht_iter iter_event;
4174
4175 ret = ust_metadata_channel_statedump(registry, chan);
4176 if (ret) {
4177 pthread_mutex_unlock(&registry->lock);
4178 ERR("Failed to generate channel metadata "
4179 "(err = %d)", ret);
4180 goto end;
4181 }
4182 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
4183 event, node.node) {
4184 ret = ust_metadata_event_statedump(registry,
4185 chan, event);
4186 if (ret) {
4187 pthread_mutex_unlock(&registry->lock);
4188 ERR("Failed to generate event metadata "
4189 "(err = %d)", ret);
4190 goto end;
4191 }
4192 }
4193 }
4194 pthread_mutex_unlock(&registry->lock);
4195 }
4196
4197 end:
4198 rcu_read_unlock();
4199 return ret;
4200 }
4201
4202 /*
4203 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4204 *
4205 * Ask the consumer to truncate the existing metadata file(s) and
4206 * then regenerate the metadata. Live and per-pid sessions are not
4207 * supported and return an error.
4208 *
4209 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4210 */
4211 int cmd_regenerate_metadata(struct ltt_session *session)
4212 {
4213 int ret;
4214
4215 LTTNG_ASSERT(session);
4216
4217 ret = check_regenerate_metadata_support(session);
4218 if (ret) {
4219 goto end;
4220 }
4221
4222 if (session->kernel_session) {
4223 ret = kernctl_session_regenerate_metadata(
4224 session->kernel_session->fd);
4225 if (ret < 0) {
4226 ERR("Failed to regenerate the kernel metadata");
4227 goto end;
4228 }
4229 }
4230
4231 if (session->ust_session) {
4232 ret = ust_regenerate_metadata(session->ust_session);
4233 if (ret < 0) {
4234 ERR("Failed to regenerate the UST metadata");
4235 goto end;
4236 }
4237 }
4238 DBG("Cmd metadata regenerate for session %s", session->name);
4239 ret = LTTNG_OK;
4240
4241 end:
4242 return ret;
4243 }
4244
4245 /*
4246 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4247 *
4248 * Ask the tracer to regenerate a new statedump.
4249 *
4250 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4251 */
4252 int cmd_regenerate_statedump(struct ltt_session *session)
4253 {
4254 int ret;
4255
4256 LTTNG_ASSERT(session);
4257
4258 if (!session->active) {
4259 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4260 goto end;
4261 }
4262
4263 if (session->kernel_session) {
4264 ret = kernctl_session_regenerate_statedump(
4265 session->kernel_session->fd);
4266 /*
4267 * Currently, the statedump in kernel can only fail if out
4268 * of memory.
4269 */
4270 if (ret < 0) {
4271 if (ret == -ENOMEM) {
4272 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4273 } else {
4274 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4275 }
4276 ERR("Failed to regenerate the kernel statedump");
4277 goto end;
4278 }
4279 }
4280
4281 if (session->ust_session) {
4282 ret = ust_app_regenerate_statedump_all(session->ust_session);
4283 /*
4284 * Currently, the statedump in UST always returns 0.
4285 */
4286 if (ret < 0) {
4287 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4288 ERR("Failed to regenerate the UST statedump");
4289 goto end;
4290 }
4291 }
4292 DBG("Cmd regenerate statedump for session %s", session->name);
4293 ret = LTTNG_OK;
4294
4295 end:
4296 return ret;
4297 }
4298
4299 static
4300 enum lttng_error_code synchronize_tracer_notifier_register(
4301 struct notification_thread_handle *notification_thread,
4302 struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds)
4303 {
4304 enum lttng_error_code ret_code;
4305 const struct lttng_condition *condition =
4306 lttng_trigger_get_const_condition(trigger);
4307 const char *trigger_name;
4308 uid_t trigger_owner;
4309 enum lttng_trigger_status trigger_status;
4310 const enum lttng_domain_type trigger_domain =
4311 lttng_trigger_get_underlying_domain_type_restriction(
4312 trigger);
4313
4314 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4315 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4316
4317 LTTNG_ASSERT(condition);
4318 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4319 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4320
4321 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4322 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4323 trigger_name : "(anonymous)";
4324
4325 session_lock_list();
4326 switch (trigger_domain) {
4327 case LTTNG_DOMAIN_KERNEL:
4328 {
4329 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4330 if (ret_code != LTTNG_OK) {
4331 enum lttng_error_code notif_thread_unregister_ret;
4332
4333 notif_thread_unregister_ret =
4334 notification_thread_command_unregister_trigger(
4335 notification_thread, trigger);
4336
4337 if (notif_thread_unregister_ret != LTTNG_OK) {
4338 /* Return the original error code. */
4339 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4340 trigger_name,
4341 (int) trigger_owner,
4342 ret_code);
4343 }
4344 }
4345 break;
4346 }
4347 case LTTNG_DOMAIN_UST:
4348 ust_app_global_update_all_event_notifier_rules();
4349 break;
4350 case LTTNG_DOMAIN_JUL:
4351 case LTTNG_DOMAIN_LOG4J:
4352 case LTTNG_DOMAIN_PYTHON:
4353 {
4354 /* Agent domains. */
4355 struct agent *agt = agent_find_by_event_notifier_domain(
4356 trigger_domain);
4357
4358 if (!agt) {
4359 agt = agent_create(trigger_domain);
4360 if (!agt) {
4361 ret_code = LTTNG_ERR_NOMEM;
4362 goto end_unlock_session_list;
4363 }
4364
4365 agent_add(agt, the_trigger_agents_ht_by_domain);
4366 }
4367
4368 ret_code = trigger_agent_enable(trigger, agt);
4369 if (ret_code != LTTNG_OK) {
4370 goto end_unlock_session_list;
4371 }
4372
4373 break;
4374 }
4375 case LTTNG_DOMAIN_NONE:
4376 default:
4377 abort();
4378 }
4379
4380 ret_code = LTTNG_OK;
4381 end_unlock_session_list:
4382 session_unlock_list();
4383 return ret_code;
4384 }
4385
4386 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4387 struct lttng_trigger *trigger,
4388 bool is_trigger_anonymous,
4389 struct notification_thread_handle *notification_thread,
4390 struct lttng_trigger **return_trigger)
4391 {
4392 enum lttng_error_code ret_code;
4393 const char *trigger_name;
4394 uid_t trigger_owner;
4395 enum lttng_trigger_status trigger_status;
4396
4397 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4398 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4399 trigger_name : "(anonymous)";
4400
4401 trigger_status = lttng_trigger_get_owner_uid(
4402 trigger, &trigger_owner);
4403 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4404
4405 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4406 trigger_name, (int) trigger_owner,
4407 (int) lttng_credentials_get_uid(cmd_creds));
4408
4409 /*
4410 * Validate the trigger credentials against the command credentials.
4411 * Only the root user can register a trigger with non-matching
4412 * credentials.
4413 */
4414 if (!lttng_credentials_is_equal_uid(
4415 lttng_trigger_get_credentials(trigger),
4416 cmd_creds)) {
4417 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4418 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4419 trigger_name, (int) trigger_owner,
4420 (int) lttng_credentials_get_uid(cmd_creds));
4421 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4422 goto end;
4423 }
4424 }
4425
4426 /*
4427 * The bytecode generation also serves as a validation step for the
4428 * bytecode expressions.
4429 */
4430 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4431 if (ret_code != LTTNG_OK) {
4432 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4433 trigger_name, (int) trigger_owner, ret_code);
4434 goto end;
4435 }
4436
4437 /*
4438 * A reference to the trigger is acquired by the notification thread.
4439 * It is safe to return the same trigger to the caller since it the
4440 * other user holds a reference.
4441 *
4442 * The trigger is modified during the execution of the
4443 * "register trigger" command. However, by the time the command returns,
4444 * it is safe to use without any locking as its properties are
4445 * immutable.
4446 */
4447 ret_code = notification_thread_command_register_trigger(
4448 notification_thread, trigger, is_trigger_anonymous);
4449 if (ret_code != LTTNG_OK) {
4450 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4451 trigger_name, (int) trigger_owner, ret_code);
4452 goto end;
4453 }
4454
4455 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4456 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4457 trigger_name : "(anonymous)";
4458
4459 /*
4460 * Synchronize tracers if the trigger adds an event notifier.
4461 */
4462 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4463 ret_code = synchronize_tracer_notifier_register(notification_thread,
4464 trigger, cmd_creds);
4465 if (ret_code != LTTNG_OK) {
4466 ERR("Error registering tracer notifier: %s",
4467 lttng_strerror(-ret_code));
4468 goto end;
4469 }
4470 }
4471
4472 /*
4473 * Return an updated trigger to the client.
4474 *
4475 * Since a modified version of the same trigger is returned, acquire a
4476 * reference to the trigger so the caller doesn't have to care if those
4477 * are distinct instances or not.
4478 */
4479 if (ret_code == LTTNG_OK) {
4480 lttng_trigger_get(trigger);
4481 *return_trigger = trigger;
4482 /* Ownership of trigger was transferred to caller. */
4483 trigger = NULL;
4484 }
4485 end:
4486 return ret_code;
4487 }
4488
4489 static
4490 enum lttng_error_code synchronize_tracer_notifier_unregister(
4491 const struct lttng_trigger *trigger)
4492 {
4493 enum lttng_error_code ret_code;
4494 const struct lttng_condition *condition =
4495 lttng_trigger_get_const_condition(trigger);
4496 const enum lttng_domain_type trigger_domain =
4497 lttng_trigger_get_underlying_domain_type_restriction(
4498 trigger);
4499
4500 LTTNG_ASSERT(condition);
4501 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4502 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4503
4504 session_lock_list();
4505 switch (trigger_domain) {
4506 case LTTNG_DOMAIN_KERNEL:
4507 ret_code = kernel_unregister_event_notifier(trigger);
4508 if (ret_code != LTTNG_OK) {
4509 goto end_unlock_session_list;
4510 }
4511
4512 break;
4513 case LTTNG_DOMAIN_UST:
4514 ust_app_global_update_all_event_notifier_rules();
4515 break;
4516 case LTTNG_DOMAIN_JUL:
4517 case LTTNG_DOMAIN_LOG4J:
4518 case LTTNG_DOMAIN_PYTHON:
4519 {
4520 /* Agent domains. */
4521 struct agent *agt = agent_find_by_event_notifier_domain(
4522 trigger_domain);
4523
4524 /*
4525 * This trigger was never registered in the first place. Calling
4526 * this function under those circumstances is an internal error.
4527 */
4528 LTTNG_ASSERT(agt);
4529 ret_code = trigger_agent_disable(trigger, agt);
4530 if (ret_code != LTTNG_OK) {
4531 goto end_unlock_session_list;
4532 }
4533
4534 break;
4535 }
4536 case LTTNG_DOMAIN_NONE:
4537 default:
4538 abort();
4539 }
4540
4541 ret_code = LTTNG_OK;
4542
4543 end_unlock_session_list:
4544 session_unlock_list();
4545 return ret_code;
4546 }
4547
4548 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4549 const struct lttng_trigger *trigger,
4550 struct notification_thread_handle *notification_thread)
4551 {
4552 enum lttng_error_code ret_code;
4553 const char *trigger_name;
4554 uid_t trigger_owner;
4555 enum lttng_trigger_status trigger_status;
4556 struct lttng_trigger *sessiond_trigger = NULL;
4557
4558 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4559 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4560 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4561 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4562
4563 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4564 trigger_name, (int) trigger_owner,
4565 (int) lttng_credentials_get_uid(cmd_creds));
4566
4567 /*
4568 * Validate the trigger credentials against the command credentials.
4569 * Only the root user can unregister a trigger with non-matching
4570 * credentials.
4571 */
4572 if (!lttng_credentials_is_equal_uid(
4573 lttng_trigger_get_credentials(trigger),
4574 cmd_creds)) {
4575 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4576 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4577 trigger_name, (int) trigger_owner,
4578 (int) lttng_credentials_get_uid(cmd_creds));
4579 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4580 goto end;
4581 }
4582 }
4583
4584 /* Fetch the sessiond side trigger object. */
4585 ret_code = notification_thread_command_get_trigger(
4586 notification_thread, trigger, &sessiond_trigger);
4587 if (ret_code != LTTNG_OK) {
4588 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4589 trigger_name, (int) trigger_owner, ret_code);
4590 goto end;
4591 }
4592
4593 LTTNG_ASSERT(sessiond_trigger);
4594
4595 /*
4596 * From this point on, no matter what, consider the trigger
4597 * unregistered.
4598 *
4599 * We set the unregistered state of the sessiond side trigger object in
4600 * the client thread since we want to minimize the possibility of the
4601 * notification thread being stalled due to a long execution of an
4602 * action that required the trigger lock.
4603 */
4604 lttng_trigger_set_as_unregistered(sessiond_trigger);
4605
4606 ret_code = notification_thread_command_unregister_trigger(notification_thread,
4607 trigger);
4608 if (ret_code != LTTNG_OK) {
4609 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4610 trigger_name, (int) trigger_owner, ret_code);
4611 goto end;
4612 }
4613
4614 /*
4615 * Synchronize tracers if the trigger removes an event notifier.
4616 * Do this even if the trigger unregistration failed to at least stop
4617 * the tracers from producing notifications associated with this
4618 * event notifier.
4619 */
4620 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4621 ret_code = synchronize_tracer_notifier_unregister(trigger);
4622 if (ret_code != LTTNG_OK) {
4623 ERR("Error unregistering trigger to tracer.");
4624 goto end;
4625 }
4626
4627 }
4628
4629 end:
4630 lttng_trigger_put(sessiond_trigger);
4631 return ret_code;
4632 }
4633
4634 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4635 struct notification_thread_handle *notification_thread,
4636 struct lttng_triggers **return_triggers)
4637 {
4638 int ret;
4639 enum lttng_error_code ret_code;
4640 struct lttng_triggers *triggers = NULL;
4641
4642 /* Get the set of triggers from the notification thread. */
4643 ret_code = notification_thread_command_list_triggers(
4644 notification_thread, cmd_ctx->creds.uid, &triggers);
4645 if (ret_code != LTTNG_OK) {
4646 goto end;
4647 }
4648
4649 ret = lttng_triggers_remove_hidden_triggers(triggers);
4650 if (ret) {
4651 ret_code = LTTNG_ERR_UNK;
4652 goto end;
4653 }
4654
4655 *return_triggers = triggers;
4656 triggers = NULL;
4657 ret_code = LTTNG_OK;
4658 end:
4659 lttng_triggers_destroy(triggers);
4660 return ret_code;
4661 }
4662
4663 enum lttng_error_code cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4664 const struct lttng_error_query *query,
4665 struct lttng_error_query_results **_results,
4666 struct notification_thread_handle *notification_thread)
4667 {
4668 enum lttng_error_code ret_code;
4669 const struct lttng_trigger *query_target_trigger;
4670 const struct lttng_action *query_target_action = NULL;
4671 struct lttng_trigger *matching_trigger = NULL;
4672 const char *trigger_name;
4673 uid_t trigger_owner;
4674 enum lttng_trigger_status trigger_status;
4675 struct lttng_error_query_results *results = NULL;
4676
4677 switch (lttng_error_query_get_target_type(query)) {
4678 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4679 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4680 break;
4681 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4682 query_target_trigger =
4683 lttng_error_query_condition_borrow_target(query);
4684 break;
4685 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4686 query_target_trigger = lttng_error_query_action_borrow_trigger_target(
4687 query);
4688 break;
4689 default:
4690 abort();
4691 }
4692
4693 LTTNG_ASSERT(query_target_trigger);
4694
4695 ret_code = notification_thread_command_get_trigger(notification_thread,
4696 query_target_trigger, &matching_trigger);
4697 if (ret_code != LTTNG_OK) {
4698 goto end;
4699 }
4700
4701 /* No longer needed. */
4702 query_target_trigger = NULL;
4703
4704 if (lttng_error_query_get_target_type(query) ==
4705 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4706 /* Get the sessiond-side version of the target action. */
4707 query_target_action =
4708 lttng_error_query_action_borrow_action_target(
4709 query, matching_trigger);
4710 }
4711
4712 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4713 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4714 trigger_name : "(anonymous)";
4715 trigger_status = lttng_trigger_get_owner_uid(matching_trigger,
4716 &trigger_owner);
4717 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4718
4719 results = lttng_error_query_results_create();
4720 if (!results) {
4721 ret_code = LTTNG_ERR_NOMEM;
4722 goto end;
4723 }
4724
4725 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4726 trigger_name, (int) trigger_owner,
4727 (int) lttng_credentials_get_uid(cmd_creds));
4728
4729 /*
4730 * Validate the trigger credentials against the command credentials.
4731 * Only the root user can target a trigger with non-matching
4732 * credentials.
4733 */
4734 if (!lttng_credentials_is_equal_uid(
4735 lttng_trigger_get_credentials(matching_trigger),
4736 cmd_creds)) {
4737 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4738 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4739 trigger_name, (int) trigger_owner,
4740 (int) lttng_credentials_get_uid(cmd_creds));
4741 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4742 goto end;
4743 }
4744 }
4745
4746 switch (lttng_error_query_get_target_type(query)) {
4747 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4748 trigger_status = lttng_trigger_add_error_results(
4749 matching_trigger, results);
4750
4751 switch (trigger_status) {
4752 case LTTNG_TRIGGER_STATUS_OK:
4753 break;
4754 default:
4755 ret_code = LTTNG_ERR_UNK;
4756 goto end;
4757 }
4758
4759 break;
4760 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4761 {
4762 trigger_status = lttng_trigger_condition_add_error_results(
4763 matching_trigger, results);
4764
4765 switch (trigger_status) {
4766 case LTTNG_TRIGGER_STATUS_OK:
4767 break;
4768 default:
4769 ret_code = LTTNG_ERR_UNK;
4770 goto end;
4771 }
4772
4773 break;
4774 }
4775 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4776 {
4777 const enum lttng_action_status action_status =
4778 lttng_action_add_error_query_results(
4779 query_target_action, results);
4780
4781 switch (action_status) {
4782 case LTTNG_ACTION_STATUS_OK:
4783 break;
4784 default:
4785 ret_code = LTTNG_ERR_UNK;
4786 goto end;
4787 }
4788
4789 break;
4790 }
4791 default:
4792 abort();
4793 break;
4794 }
4795
4796 *_results = results;
4797 results = NULL;
4798 ret_code = LTTNG_OK;
4799 end:
4800 lttng_trigger_put(matching_trigger);
4801 lttng_error_query_results_destroy(results);
4802 return ret_code;
4803 }
4804
4805 /*
4806 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4807 * snapshot output is *not* set with a remote destination.
4808 *
4809 * Return LTTNG_OK on success or a LTTNG_ERR code.
4810 */
4811 static enum lttng_error_code set_relayd_for_snapshot(
4812 struct consumer_output *output,
4813 const struct ltt_session *session)
4814 {
4815 enum lttng_error_code status = LTTNG_OK;
4816 struct lttng_ht_iter iter;
4817 struct consumer_socket *socket;
4818 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4819 const char *base_path;
4820
4821 LTTNG_ASSERT(output);
4822 LTTNG_ASSERT(session);
4823
4824 DBG2("Set relayd object from snapshot output");
4825
4826 if (session->current_trace_chunk) {
4827 enum lttng_trace_chunk_status chunk_status =
4828 lttng_trace_chunk_get_id(
4829 session->current_trace_chunk,
4830 &current_chunk_id.value);
4831
4832 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
4833 current_chunk_id.is_set = true;
4834 } else {
4835 ERR("Failed to get current trace chunk id");
4836 status = LTTNG_ERR_UNK;
4837 goto error;
4838 }
4839 }
4840
4841 /* Ignore if snapshot consumer output is not network. */
4842 if (output->type != CONSUMER_DST_NET) {
4843 goto error;
4844 }
4845
4846 /*
4847 * The snapshot record URI base path overrides the session
4848 * base path.
4849 */
4850 if (output->dst.net.control.subdir[0] != '\0') {
4851 base_path = output->dst.net.control.subdir;
4852 } else {
4853 base_path = session->base_path;
4854 }
4855
4856 /*
4857 * For each consumer socket, create and send the relayd object of the
4858 * snapshot output.
4859 */
4860 rcu_read_lock();
4861 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
4862 socket, node.node) {
4863 pthread_mutex_lock(socket->lock);
4864 status = send_consumer_relayd_sockets(0, session->id,
4865 output, socket,
4866 session->name, session->hostname,
4867 base_path,
4868 session->live_timer,
4869 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
4870 session->creation_time,
4871 session->name_contains_creation_time);
4872 pthread_mutex_unlock(socket->lock);
4873 if (status != LTTNG_OK) {
4874 rcu_read_unlock();
4875 goto error;
4876 }
4877 }
4878 rcu_read_unlock();
4879
4880 error:
4881 return status;
4882 }
4883
4884 /*
4885 * Record a kernel snapshot.
4886 *
4887 * Return LTTNG_OK on success or a LTTNG_ERR code.
4888 */
4889 static enum lttng_error_code record_kernel_snapshot(
4890 struct ltt_kernel_session *ksess,
4891 const struct consumer_output *output,
4892 const struct ltt_session *session,
4893 int wait, uint64_t nb_packets_per_stream)
4894 {
4895 enum lttng_error_code status;
4896
4897 LTTNG_ASSERT(ksess);
4898 LTTNG_ASSERT(output);
4899 LTTNG_ASSERT(session);
4900
4901 status = kernel_snapshot_record(
4902 ksess, output, wait, nb_packets_per_stream);
4903 return status;
4904 }
4905
4906 /*
4907 * Record a UST snapshot.
4908 *
4909 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4910 */
4911 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
4912 const struct consumer_output *output,
4913 const struct ltt_session *session,
4914 int wait, uint64_t nb_packets_per_stream)
4915 {
4916 enum lttng_error_code status;
4917
4918 LTTNG_ASSERT(usess);
4919 LTTNG_ASSERT(output);
4920 LTTNG_ASSERT(session);
4921
4922 status = ust_app_snapshot_record(
4923 usess, output, wait, nb_packets_per_stream);
4924 return status;
4925 }
4926
4927 static
4928 uint64_t get_session_size_one_more_packet_per_stream(
4929 const struct ltt_session *session, uint64_t cur_nr_packets)
4930 {
4931 uint64_t tot_size = 0;
4932
4933 if (session->kernel_session) {
4934 struct ltt_kernel_channel *chan;
4935 const struct ltt_kernel_session *ksess =
4936 session->kernel_session;
4937
4938 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
4939 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
4940 /*
4941 * Don't take channel into account if we
4942 * already grab all its packets.
4943 */
4944 continue;
4945 }
4946 tot_size += chan->channel->attr.subbuf_size
4947 * chan->stream_count;
4948 }
4949 }
4950
4951 if (session->ust_session) {
4952 const struct ltt_ust_session *usess = session->ust_session;
4953
4954 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
4955 cur_nr_packets);
4956 }
4957
4958 return tot_size;
4959 }
4960
4961 /*
4962 * Calculate the number of packets we can grab from each stream that
4963 * fits within the overall snapshot max size.
4964 *
4965 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4966 * the number of packets per stream.
4967 *
4968 * TODO: this approach is not perfect: we consider the worse case
4969 * (packet filling the sub-buffers) as an upper bound, but we could do
4970 * better if we do this calculation while we actually grab the packet
4971 * content: we would know how much padding we don't actually store into
4972 * the file.
4973 *
4974 * This algorithm is currently bounded by the number of packets per
4975 * stream.
4976 *
4977 * Since we call this algorithm before actually grabbing the data, it's
4978 * an approximation: for instance, applications could appear/disappear
4979 * in between this call and actually grabbing data.
4980 */
4981 static
4982 int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
4983 uint64_t max_size)
4984 {
4985 int64_t size_left;
4986 uint64_t cur_nb_packets = 0;
4987
4988 if (!max_size) {
4989 return 0; /* Infinite */
4990 }
4991
4992 size_left = max_size;
4993 for (;;) {
4994 uint64_t one_more_packet_tot_size;
4995
4996 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
4997 session, cur_nb_packets);
4998 if (!one_more_packet_tot_size) {
4999 /* We are already grabbing all packets. */
5000 break;
5001 }
5002 size_left -= one_more_packet_tot_size;
5003 if (size_left < 0) {
5004 break;
5005 }
5006 cur_nb_packets++;
5007 }
5008 if (!cur_nb_packets && size_left != max_size) {
5009 /* Not enough room to grab one packet of each stream, error. */
5010 return -1;
5011 }
5012 return cur_nb_packets;
5013 }
5014
5015 static
5016 enum lttng_error_code snapshot_record(struct ltt_session *session,
5017 const struct snapshot_output *snapshot_output, int wait)
5018 {
5019 int64_t nb_packets_per_stream;
5020 char snapshot_chunk_name[LTTNG_NAME_MAX];
5021 int ret;
5022 enum lttng_error_code ret_code = LTTNG_OK;
5023 struct lttng_trace_chunk *snapshot_trace_chunk;
5024 struct consumer_output *original_ust_consumer_output = NULL;
5025 struct consumer_output *original_kernel_consumer_output = NULL;
5026 struct consumer_output *snapshot_ust_consumer_output = NULL;
5027 struct consumer_output *snapshot_kernel_consumer_output = NULL;
5028
5029 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
5030 "%s-%s-%" PRIu64,
5031 snapshot_output->name,
5032 snapshot_output->datetime,
5033 snapshot_output->nb_snapshot);
5034 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5035 ERR("Failed to format snapshot name");
5036 ret_code = LTTNG_ERR_INVALID;
5037 goto error;
5038 }
5039 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5040 snapshot_output->name, session->name,
5041 snapshot_chunk_name);
5042 if (!session->kernel_session && !session->ust_session) {
5043 ERR("Failed to record snapshot as no channels exist");
5044 ret_code = LTTNG_ERR_NO_CHANNEL;
5045 goto error;
5046 }
5047
5048 if (session->kernel_session) {
5049 original_kernel_consumer_output =
5050 session->kernel_session->consumer;
5051 snapshot_kernel_consumer_output =
5052 consumer_copy_output(snapshot_output->consumer);
5053 strcpy(snapshot_kernel_consumer_output->chunk_path,
5054 snapshot_chunk_name);
5055
5056 /* Copy the original domain subdir. */
5057 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5058 original_kernel_consumer_output->domain_subdir);
5059
5060 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5061 original_kernel_consumer_output);
5062 if (ret < 0) {
5063 ERR("Failed to copy consumer sockets from snapshot output configuration");
5064 ret_code = LTTNG_ERR_NOMEM;
5065 goto error;
5066 }
5067 ret_code = set_relayd_for_snapshot(
5068 snapshot_kernel_consumer_output, session);
5069 if (ret_code != LTTNG_OK) {
5070 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5071 goto error;
5072 }
5073 session->kernel_session->consumer =
5074 snapshot_kernel_consumer_output;
5075 }
5076 if (session->ust_session) {
5077 original_ust_consumer_output = session->ust_session->consumer;
5078 snapshot_ust_consumer_output =
5079 consumer_copy_output(snapshot_output->consumer);
5080 strcpy(snapshot_ust_consumer_output->chunk_path,
5081 snapshot_chunk_name);
5082
5083 /* Copy the original domain subdir. */
5084 strcpy(snapshot_ust_consumer_output->domain_subdir,
5085 original_ust_consumer_output->domain_subdir);
5086
5087 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5088 original_ust_consumer_output);
5089 if (ret < 0) {
5090 ERR("Failed to copy consumer sockets from snapshot output configuration");
5091 ret_code = LTTNG_ERR_NOMEM;
5092 goto error;
5093 }
5094 ret_code = set_relayd_for_snapshot(
5095 snapshot_ust_consumer_output, session);
5096 if (ret_code != LTTNG_OK) {
5097 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5098 goto error;
5099 }
5100 session->ust_session->consumer =
5101 snapshot_ust_consumer_output;
5102 }
5103
5104 snapshot_trace_chunk = session_create_new_trace_chunk(session,
5105 snapshot_kernel_consumer_output ?:
5106 snapshot_ust_consumer_output,
5107 consumer_output_get_base_path(
5108 snapshot_output->consumer),
5109 snapshot_chunk_name);
5110 if (!snapshot_trace_chunk) {
5111 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5112 session->name);
5113 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5114 goto error;
5115 }
5116 LTTNG_ASSERT(!session->current_trace_chunk);
5117 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
5118 lttng_trace_chunk_put(snapshot_trace_chunk);
5119 snapshot_trace_chunk = NULL;
5120 if (ret) {
5121 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5122 session->name);
5123 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5124 goto error;
5125 }
5126
5127 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
5128 snapshot_output->max_size);
5129 if (nb_packets_per_stream < 0) {
5130 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5131 goto error_close_trace_chunk;
5132 }
5133
5134 if (session->kernel_session) {
5135 ret_code = record_kernel_snapshot(session->kernel_session,
5136 snapshot_kernel_consumer_output, session,
5137 wait, nb_packets_per_stream);
5138 if (ret_code != LTTNG_OK) {
5139 goto error_close_trace_chunk;
5140 }
5141 }
5142
5143 if (session->ust_session) {
5144 ret_code = record_ust_snapshot(session->ust_session,
5145 snapshot_ust_consumer_output, session,
5146 wait, nb_packets_per_stream);
5147 if (ret_code != LTTNG_OK) {
5148 goto error_close_trace_chunk;
5149 }
5150 }
5151
5152 error_close_trace_chunk:
5153 if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
5154 ERR("Failed to release the current trace chunk of session \"%s\"",
5155 session->name);
5156 ret_code = LTTNG_ERR_UNK;
5157 }
5158
5159 if (session_close_trace_chunk(session, snapshot_trace_chunk,
5160 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
5161 /*
5162 * Don't goto end; make sure the chunk is closed for the session
5163 * to allow future snapshots.
5164 */
5165 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5166 session->name);
5167 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5168 }
5169 error:
5170 if (original_ust_consumer_output) {
5171 session->ust_session->consumer = original_ust_consumer_output;
5172 }
5173 if (original_kernel_consumer_output) {
5174 session->kernel_session->consumer =
5175 original_kernel_consumer_output;
5176 }
5177 consumer_output_put(snapshot_ust_consumer_output);
5178 consumer_output_put(snapshot_kernel_consumer_output);
5179 return ret_code;
5180 }
5181
5182 /*
5183 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5184 *
5185 * The wait parameter is ignored so this call always wait for the snapshot to
5186 * complete before returning.
5187 *
5188 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5189 */
5190 int cmd_snapshot_record(struct ltt_session *session,
5191 const struct lttng_snapshot_output *output, int wait)
5192 {
5193 enum lttng_error_code cmd_ret = LTTNG_OK;
5194 int ret;
5195 unsigned int snapshot_success = 0;
5196 char datetime[16];
5197 struct snapshot_output *tmp_output = NULL;
5198
5199 LTTNG_ASSERT(session);
5200 LTTNG_ASSERT(output);
5201
5202 DBG("Cmd snapshot record for session %s", session->name);
5203
5204 /* Get the datetime for the snapshot output directory. */
5205 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
5206 sizeof(datetime));
5207 if (!ret) {
5208 cmd_ret = LTTNG_ERR_INVALID;
5209 goto error;
5210 }
5211
5212 /*
5213 * Permission denied to create an output if the session is not
5214 * set in no output mode.
5215 */
5216 if (session->output_traces) {
5217 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5218 goto error;
5219 }
5220
5221 /* The session needs to be started at least once. */
5222 if (!session->has_been_started) {
5223 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5224 goto error;
5225 }
5226
5227 /* Use temporary output for the session. */
5228 if (*output->ctrl_url != '\0') {
5229 tmp_output = snapshot_output_alloc();
5230 if (!tmp_output) {
5231 cmd_ret = LTTNG_ERR_NOMEM;
5232 goto error;
5233 }
5234
5235 ret = snapshot_output_init(session, output->max_size,
5236 output->name,
5237 output->ctrl_url, output->data_url,
5238 session->consumer,
5239 tmp_output, NULL);
5240 if (ret < 0) {
5241 if (ret == -ENOMEM) {
5242 cmd_ret = LTTNG_ERR_NOMEM;
5243 } else {
5244 cmd_ret = LTTNG_ERR_INVALID;
5245 }
5246 goto error;
5247 }
5248 /* Use the global session count for the temporary snapshot. */
5249 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5250
5251 /* Use the global datetime */
5252 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5253 cmd_ret = snapshot_record(session, tmp_output, wait);
5254 if (cmd_ret != LTTNG_OK) {
5255 goto error;
5256 }
5257 snapshot_success = 1;
5258 } else {
5259 struct snapshot_output *sout;
5260 struct lttng_ht_iter iter;
5261
5262 rcu_read_lock();
5263 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
5264 &iter.iter, sout, node.node) {
5265 struct snapshot_output output_copy;
5266
5267 /*
5268 * Make a local copy of the output and override output
5269 * parameters with those provided as part of the
5270 * command.
5271 */
5272 memcpy(&output_copy, sout, sizeof(output_copy));
5273
5274 if (output->max_size != (uint64_t) -1ULL) {
5275 output_copy.max_size = output->max_size;
5276 }
5277
5278 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5279 memcpy(output_copy.datetime, datetime,
5280 sizeof(datetime));
5281
5282 /* Use temporary name. */
5283 if (*output->name != '\0') {
5284 if (lttng_strncpy(output_copy.name,
5285 output->name,
5286 sizeof(output_copy.name))) {
5287 cmd_ret = LTTNG_ERR_INVALID;
5288 rcu_read_unlock();
5289 goto error;
5290 }
5291 }
5292
5293 cmd_ret = snapshot_record(session, &output_copy, wait);
5294 if (cmd_ret != LTTNG_OK) {
5295 rcu_read_unlock();
5296 goto error;
5297 }
5298 snapshot_success = 1;
5299 }
5300 rcu_read_unlock();
5301 }
5302
5303 if (snapshot_success) {
5304 session->snapshot.nb_snapshot++;
5305 } else {
5306 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5307 }
5308
5309 error:
5310 if (tmp_output) {
5311 snapshot_output_destroy(tmp_output);
5312 }
5313 return cmd_ret;
5314 }
5315
5316 /*
5317 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5318 */
5319 int cmd_set_session_shm_path(struct ltt_session *session,
5320 const char *shm_path)
5321 {
5322 /* Safety net */
5323 LTTNG_ASSERT(session);
5324
5325 /*
5326 * Can only set shm path before session is started.
5327 */
5328 if (session->has_been_started) {
5329 return LTTNG_ERR_SESSION_STARTED;
5330 }
5331
5332 strncpy(session->shm_path, shm_path,
5333 sizeof(session->shm_path));
5334 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5335
5336 return LTTNG_OK;
5337 }
5338
5339 /*
5340 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5341 *
5342 * Ask the consumer to rotate the session output directory.
5343 * The session lock must be held.
5344 *
5345 * Returns LTTNG_OK on success or else a negative LTTng error code.
5346 */
5347 int cmd_rotate_session(struct ltt_session *session,
5348 struct lttng_rotate_session_return *rotate_return,
5349 bool quiet_rotation,
5350 enum lttng_trace_chunk_command_type command)
5351 {
5352 int ret;
5353 uint64_t ongoing_rotation_chunk_id;
5354 enum lttng_error_code cmd_ret = LTTNG_OK;
5355 struct lttng_trace_chunk *chunk_being_archived = NULL;
5356 struct lttng_trace_chunk *new_trace_chunk = NULL;
5357 enum lttng_trace_chunk_status chunk_status;
5358 bool failed_to_rotate = false;
5359 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5360
5361 LTTNG_ASSERT(session);
5362
5363 if (!session->has_been_started) {
5364 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5365 goto end;
5366 }
5367
5368 /*
5369 * Explicit rotation is not supported for live sessions.
5370 * However, live sessions can perform a quiet rotation on
5371 * destroy.
5372 * Rotation is not supported for snapshot traces (no output).
5373 */
5374 if ((!quiet_rotation && session->live_timer) ||
5375 !session->output_traces) {
5376 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5377 goto end;
5378 }
5379
5380 /* Unsupported feature in lttng-relayd before 2.11. */
5381 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5382 (session->consumer->relay_major_version == 2 &&
5383 session->consumer->relay_minor_version < 11)) {
5384 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5385 goto end;
5386 }
5387
5388 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5389 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5390 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5391 goto end;
5392 }
5393
5394 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5395 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5396 session->name);
5397 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5398 goto end;
5399 }
5400
5401 /*
5402 * After a stop, we only allow one rotation to occur, the other ones are
5403 * useless until a new start.
5404 */
5405 if (session->rotated_after_last_stop) {
5406 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5407 session->name);
5408 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5409 goto end;
5410 }
5411
5412 /*
5413 * After a stop followed by a clear, disallow following rotations a they would
5414 * generate empty chunks.
5415 */
5416 if (session->cleared_after_last_stop) {
5417 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5418 session->name);
5419 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5420 goto end;
5421 }
5422
5423 if (session->active) {
5424 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
5425 NULL, NULL);
5426 if (!new_trace_chunk) {
5427 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5428 goto error;
5429 }
5430 }
5431
5432 /*
5433 * The current trace chunk becomes the chunk being archived.
5434 *
5435 * After this point, "chunk_being_archived" must absolutely
5436 * be closed on the consumer(s), otherwise it will never be
5437 * cleaned-up, which will result in a leak.
5438 */
5439 ret = session_set_trace_chunk(session, new_trace_chunk,
5440 &chunk_being_archived);
5441 if (ret) {
5442 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5443 goto error;
5444 }
5445
5446 if (session->kernel_session) {
5447 cmd_ret = kernel_rotate_session(session);
5448 if (cmd_ret != LTTNG_OK) {
5449 failed_to_rotate = true;
5450 rotation_fail_code = cmd_ret;
5451 }
5452 }
5453 if (session->ust_session) {
5454 cmd_ret = ust_app_rotate_session(session);
5455 if (cmd_ret != LTTNG_OK) {
5456 failed_to_rotate = true;
5457 rotation_fail_code = cmd_ret;
5458 }
5459 }
5460
5461 if (!session->active) {
5462 session->rotated_after_last_stop = true;
5463 }
5464
5465 if (!chunk_being_archived) {
5466 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5467 session->name);
5468 if (failed_to_rotate) {
5469 cmd_ret = rotation_fail_code;
5470 goto error;
5471 }
5472 cmd_ret = LTTNG_OK;
5473 goto end;
5474 }
5475
5476 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5477 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
5478 &ongoing_rotation_chunk_id);
5479 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5480
5481 ret = session_close_trace_chunk(session, chunk_being_archived,
5482 command, session->last_chunk_path);
5483 if (ret) {
5484 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5485 goto error;
5486 }
5487
5488 if (failed_to_rotate) {
5489 cmd_ret = rotation_fail_code;
5490 goto error;
5491 }
5492
5493 session->quiet_rotation = quiet_rotation;
5494 ret = timer_session_rotation_pending_check_start(session,
5495 DEFAULT_ROTATE_PENDING_TIMER);
5496 if (ret) {
5497 cmd_ret = LTTNG_ERR_UNK;
5498 goto error;
5499 }
5500
5501 if (rotate_return) {
5502 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5503 }
5504
5505 session->chunk_being_archived = chunk_being_archived;
5506 chunk_being_archived = NULL;
5507 if (!quiet_rotation) {
5508 ret = notification_thread_command_session_rotation_ongoing(
5509 the_notification_thread_handle, session->name,
5510 session->uid, session->gid,
5511 ongoing_rotation_chunk_id);
5512 if (ret != LTTNG_OK) {
5513 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5514 session->name);
5515 cmd_ret = ret;
5516 }
5517 }
5518
5519 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5520 session->name, ongoing_rotation_chunk_id);
5521 end:
5522 lttng_trace_chunk_put(new_trace_chunk);
5523 lttng_trace_chunk_put(chunk_being_archived);
5524 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5525 return ret;
5526 error:
5527 if (session_reset_rotation_state(session,
5528 LTTNG_ROTATION_STATE_ERROR)) {
5529 ERR("Failed to reset rotation state of session \"%s\"",
5530 session->name);
5531 }
5532 goto end;
5533 }
5534
5535 /*
5536 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5537 *
5538 * Check if the session has finished its rotation.
5539 *
5540 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5541 */
5542 int cmd_rotate_get_info(struct ltt_session *session,
5543 struct lttng_rotation_get_info_return *info_return,
5544 uint64_t rotation_id)
5545 {
5546 enum lttng_error_code cmd_ret = LTTNG_OK;
5547 enum lttng_rotation_state rotation_state;
5548
5549 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
5550 session->most_recent_chunk_id.value);
5551
5552 if (session->chunk_being_archived) {
5553 enum lttng_trace_chunk_status chunk_status;
5554 uint64_t chunk_id;
5555
5556 chunk_status = lttng_trace_chunk_get_id(
5557 session->chunk_being_archived,
5558 &chunk_id);
5559 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5560
5561 rotation_state = rotation_id == chunk_id ?
5562 LTTNG_ROTATION_STATE_ONGOING :
5563 LTTNG_ROTATION_STATE_EXPIRED;
5564 } else {
5565 if (session->last_archived_chunk_id.is_set &&
5566 rotation_id != session->last_archived_chunk_id.value) {
5567 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5568 } else {
5569 rotation_state = session->rotation_state;
5570 }
5571 }
5572
5573 switch (rotation_state) {
5574 case LTTNG_ROTATION_STATE_NO_ROTATION:
5575 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5576 session->name);
5577 goto end;
5578 case LTTNG_ROTATION_STATE_EXPIRED:
5579 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5580 rotation_id, session->name);
5581 break;
5582 case LTTNG_ROTATION_STATE_ONGOING:
5583 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5584 rotation_id, session->name);
5585 break;
5586 case LTTNG_ROTATION_STATE_COMPLETED:
5587 {
5588 int fmt_ret;
5589 char *chunk_path;
5590 char *current_tracing_path_reply;
5591 size_t current_tracing_path_reply_len;
5592
5593 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5594 rotation_id, session->name);
5595
5596 switch (session_get_consumer_destination_type(session)) {
5597 case CONSUMER_DST_LOCAL:
5598 current_tracing_path_reply =
5599 info_return->location.local.absolute_path;
5600 current_tracing_path_reply_len =
5601 sizeof(info_return->location.local.absolute_path);
5602 info_return->location_type =
5603 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5604 fmt_ret = asprintf(&chunk_path,
5605 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5606 session_get_base_path(session),
5607 session->last_archived_chunk_name);
5608 if (fmt_ret == -1) {
5609 PERROR("Failed to format the path of the last archived trace chunk");
5610 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5611 cmd_ret = LTTNG_ERR_UNK;
5612 goto end;
5613 }
5614 break;
5615 case CONSUMER_DST_NET:
5616 {
5617 uint16_t ctrl_port, data_port;
5618
5619 current_tracing_path_reply =
5620 info_return->location.relay.relative_path;
5621 current_tracing_path_reply_len =
5622 sizeof(info_return->location.relay.relative_path);
5623 /* Currently the only supported relay protocol. */
5624 info_return->location.relay.protocol =
5625 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5626
5627 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5628 session_get_net_consumer_hostname(session),
5629 sizeof(info_return->location.relay.host));
5630 if (fmt_ret) {
5631 ERR("Failed to copy host name to rotate_get_info reply");
5632 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5633 cmd_ret = LTTNG_ERR_SET_URL;
5634 goto end;
5635 }
5636
5637 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5638 info_return->location.relay.ports.control = ctrl_port;
5639 info_return->location.relay.ports.data = data_port;
5640 info_return->location_type =
5641 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5642 chunk_path = strdup(session->last_chunk_path);
5643 if (!chunk_path) {
5644 ERR("Failed to allocate the path of the last archived trace chunk");
5645 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5646 cmd_ret = LTTNG_ERR_UNK;
5647 goto end;
5648 }
5649 break;
5650 }
5651 default:
5652 abort();
5653 }
5654
5655 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5656 chunk_path, current_tracing_path_reply_len);
5657 free(chunk_path);
5658 if (fmt_ret) {
5659 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5660 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5661 cmd_ret = LTTNG_ERR_UNK;
5662 goto end;
5663 }
5664
5665 break;
5666 }
5667 case LTTNG_ROTATION_STATE_ERROR:
5668 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
5669 rotation_id, session->name);
5670 break;
5671 default:
5672 abort();
5673 }
5674
5675 cmd_ret = LTTNG_OK;
5676 end:
5677 info_return->status = (int32_t) rotation_state;
5678 return cmd_ret;
5679 }
5680
5681 /*
5682 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5683 *
5684 * Configure the automatic rotation parameters.
5685 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5686 * 'activate' to false means deactivate the rotation schedule and validate that
5687 * 'new_value' has the same value as the currently active value.
5688 *
5689 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5690 */
5691 int cmd_rotation_set_schedule(struct ltt_session *session,
5692 bool activate, enum lttng_rotation_schedule_type schedule_type,
5693 uint64_t new_value,
5694 struct notification_thread_handle *notification_thread_handle)
5695 {
5696 int ret;
5697 uint64_t *parameter_value;
5698
5699 LTTNG_ASSERT(session);
5700
5701 DBG("Cmd rotate set schedule session %s", session->name);
5702
5703 if (session->live_timer || !session->output_traces) {
5704 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5705 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5706 goto end;
5707 }
5708
5709 switch (schedule_type) {
5710 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5711 parameter_value = &session->rotate_size;
5712 break;
5713 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5714 parameter_value = &session->rotate_timer_period;
5715 if (new_value >= UINT_MAX) {
5716 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5717 new_value, UINT_MAX);
5718 ret = LTTNG_ERR_INVALID;
5719 goto end;
5720 }
5721 break;
5722 default:
5723 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5724 ret = LTTNG_ERR_INVALID;
5725 goto end;
5726 }
5727
5728 /* Improper use of the API. */
5729 if (new_value == -1ULL) {
5730 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5731 ret = LTTNG_ERR_INVALID;
5732 goto end;
5733 }
5734
5735 /*
5736 * As indicated in struct ltt_session's comments, a value of == 0 means
5737 * this schedule rotation type is not in use.
5738 *
5739 * Reject the command if we were asked to activate a schedule that was
5740 * already active.
5741 */
5742 if (activate && *parameter_value != 0) {
5743 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5744 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5745 goto end;
5746 }
5747
5748 /*
5749 * Reject the command if we were asked to deactivate a schedule that was
5750 * not active.
5751 */
5752 if (!activate && *parameter_value == 0) {
5753 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5754 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5755 goto end;
5756 }
5757
5758 /*
5759 * Reject the command if we were asked to deactivate a schedule that
5760 * doesn't exist.
5761 */
5762 if (!activate && *parameter_value != new_value) {
5763 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5764 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5765 goto end;
5766 }
5767
5768 *parameter_value = activate ? new_value : 0;
5769
5770 switch (schedule_type) {
5771 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5772 if (activate && session->active) {
5773 /*
5774 * Only start the timer if the session is active,
5775 * otherwise it will be started when the session starts.
5776 */
5777 ret = timer_session_rotation_schedule_timer_start(
5778 session, new_value);
5779 if (ret) {
5780 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5781 ret = LTTNG_ERR_UNK;
5782 goto end;
5783 }
5784 } else {
5785 ret = timer_session_rotation_schedule_timer_stop(
5786 session);
5787 if (ret) {
5788 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5789 ret = LTTNG_ERR_UNK;
5790 goto end;
5791 }
5792 }
5793 break;
5794 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5795 if (activate) {
5796 ret = subscribe_session_consumed_size_rotation(session,
5797 new_value, notification_thread_handle);
5798 if (ret) {
5799 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5800 ret = LTTNG_ERR_UNK;
5801 goto end;
5802 }
5803 } else {
5804 ret = unsubscribe_session_consumed_size_rotation(session,
5805 notification_thread_handle);
5806 if (ret) {
5807 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5808 ret = LTTNG_ERR_UNK;
5809 goto end;
5810 }
5811
5812 }
5813 break;
5814 default:
5815 /* Would have been caught before. */
5816 abort();
5817 }
5818
5819 ret = LTTNG_OK;
5820
5821 goto end;
5822
5823 end:
5824 return ret;
5825 }
5826
5827 /* Wait for a given path to be removed before continuing. */
5828 static enum lttng_error_code wait_on_path(void *path_data)
5829 {
5830 const char *shm_path = path_data;
5831
5832 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5833 shm_path);
5834 while (true) {
5835 int ret;
5836 struct stat st;
5837
5838 ret = stat(shm_path, &st);
5839 if (ret) {
5840 if (errno != ENOENT) {
5841 PERROR("stat() returned an error while checking for the existence of the shm path");
5842 } else {
5843 DBG("shm path no longer exists, completing the destruction of session");
5844 }
5845 break;
5846 } else {
5847 if (!S_ISDIR(st.st_mode)) {
5848 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5849 shm_path);
5850 break;
5851 }
5852 }
5853 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5854 }
5855 return LTTNG_OK;
5856 }
5857
5858 /*
5859 * Returns a pointer to a handler to run on completion of a command.
5860 * Returns NULL if no handler has to be run for the last command executed.
5861 */
5862 const struct cmd_completion_handler *cmd_pop_completion_handler(void)
5863 {
5864 struct cmd_completion_handler *handler = current_completion_handler;
5865
5866 current_completion_handler = NULL;
5867 return handler;
5868 }
5869
5870 /*
5871 * Init command subsystem.
5872 */
5873 void cmd_init(void)
5874 {
5875 /*
5876 * Set network sequence index to 1 for streams to match a relayd
5877 * socket on the consumer side.
5878 */
5879 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5880 relayd_net_seq_idx = 1;
5881 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
5882
5883 DBG("Command subsystem initialized");
5884 }
This page took 0.166324 seconds and 4 git commands to generate.