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