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