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