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