Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / src / bin / lttng-sessiond / session.cpp
CommitLineData
5b74c7b1 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
5b74c7b1 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
91d76f53 5 *
5b74c7b1
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
16d64977 9#include "buffer-registry.hpp"
28ab034a
JG
10#include "cmd.hpp"
11#include "kernel.hpp"
12#include "lttng-sessiond.hpp"
13#include "session.hpp"
14#include "timer.hpp"
15#include "trace-ust.hpp"
16d64977 16#include "ust-app.hpp"
28ab034a
JG
17#include "utils.hpp"
18
19#include <common/common.hpp>
16d64977 20#include <common/ctl/format.hpp>
28ab034a
JG
21#include <common/sessiond-comm/sessiond-comm.hpp>
22#include <common/trace-chunk.hpp>
23#include <common/urcu.hpp>
24#include <common/utils.hpp>
25
26#include <lttng/location-internal.hpp>
27
e99e3664 28#include <dirent.h>
d022620a 29#include <inttypes.h>
e99e3664
JG
30#include <limits.h>
31#include <pthread.h>
5b74c7b1
DG
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
a304c14c 35#include <sys/stat.h>
3d071855 36#include <sys/types.h>
e99e3664 37#include <urcu.h>
5b74c7b1 38
f1494934 39namespace {
3e3665b8
JG
40struct ltt_session_destroy_notifier_element {
41 ltt_session_destroy_notifier notifier;
42 void *user_data;
43};
44
ccbdaca4
MD
45struct ltt_session_clear_notifier_element {
46 ltt_session_clear_notifier notifier;
47 void *user_data;
48};
49
e99e3664
JG
50namespace ls = lttng::sessiond;
51
8c0faa1d 52/*
b5541356 53 * NOTES:
8c0faa1d 54 *
b5541356 55 * No ltt_session.lock is taken here because those data structure are widely
e1bbf989 56 * spread across the lttng-tools code base so before calling functions below
b5541356 57 * that can read/write a session, the caller MUST acquire the session lock
54d01ffb 58 * using session_lock() and session_unlock().
8c0faa1d 59 */
8c0faa1d 60
f1494934
JG
61/* These characters are forbidden in a session name. Used by validate_name. */
62const char *forbidden_name_chars = "/";
63
64/* Global hash table to keep the sessions, indexed by id. */
cd9adb8b 65struct lttng_ht *ltt_sessions_ht_by_id = nullptr;
f1494934 66/* Global hash table to keep the sessions, indexed by name. */
cd9adb8b 67struct lttng_ht *ltt_sessions_ht_by_name = nullptr;
f1494934 68
5b74c7b1 69/*
b5541356 70 * Init tracing session list.
5b74c7b1 71 *
b5541356 72 * Please see session.h for more explanation and correct usage of the list.
5b74c7b1 73 */
1a496780 74struct ltt_session_list the_session_list;
d9a970b7
JG
75
76/*
77 * Return a ltt_session structure ptr that matches name. If no session found,
78 * NULL is returned. This must be called with the session list lock held using
79 * session_lock_list and session_unlock_list.
80 * A reference to the session is implicitly acquired by this function.
81 */
82struct ltt_session *session_find_by_name(const char *name)
83{
b634601e 84 struct ltt_session *session_to_return;
d9a970b7
JG
85
86 LTTNG_ASSERT(name);
87 ASSERT_SESSION_LIST_LOCKED();
88
89 DBG2("Trying to find session by name %s", name);
90
b634601e
JG
91 for (auto session : lttng::urcu::list_iteration_adapter<ltt_session, &ltt_session::list>(
92 the_session_list.head)) {
93 if (!strncmp(session->name, name, NAME_MAX) && !session->destroyed) {
94 session_to_return = session;
d9a970b7
JG
95 goto found;
96 }
97 }
98
99 return nullptr;
100found:
b634601e 101 return session_get(session_to_return) ? session_to_return : nullptr;
d9a970b7
JG
102}
103
104/*
105 * Return an ltt_session that matches the id. If no session is found,
106 * NULL is returned. This must be called with rcu_read_lock and
107 * session list lock held (to guarantee the lifetime of the session).
108 */
109struct ltt_session *session_find_by_id(uint64_t id)
110{
111 struct lttng_ht_node_u64 *node;
112 struct lttng_ht_iter iter;
113 struct ltt_session *ls;
114
115 ASSERT_RCU_READ_LOCKED();
116 ASSERT_SESSION_LIST_LOCKED();
117
118 if (!ltt_sessions_ht_by_id) {
119 goto end;
120 }
121
122 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
00d7d903 123 node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
d9a970b7
JG
124 if (node == nullptr) {
125 goto end;
126 }
127 ls = lttng::utils::container_of(node, &ltt_session::node);
128
129 DBG3("Session %" PRIu64 " found by id.", id);
130 return session_get(ls) ? ls : nullptr;
131
132end:
133 DBG3("Session %" PRIu64 " NOT found by id", id);
134 return nullptr;
135}
f1494934 136} /* namespace */
23324029 137
1c1c3634
DG
138/*
139 * Validate the session name for forbidden characters.
140 *
141 * Return 0 on success else -1 meaning a forbidden char. has been found.
142 */
143static int validate_name(const char *name)
144{
145 int ret;
146 char *tok, *tmp_name;
147
a0377dfe 148 LTTNG_ASSERT(name);
1c1c3634
DG
149
150 tmp_name = strdup(name);
151 if (!tmp_name) {
152 /* ENOMEM here. */
153 ret = -1;
154 goto error;
155 }
156
157 tok = strpbrk(tmp_name, forbidden_name_chars);
158 if (tok) {
159 DBG("Session name %s contains a forbidden character", name);
160 /* Forbidden character has been found. */
161 ret = -1;
162 goto error;
163 }
164 ret = 0;
165
166error:
167 free(tmp_name);
168 return ret;
169}
170
5b74c7b1 171/*
050349bb 172 * Add a ltt_session structure to the global list.
5b74c7b1 173 *
050349bb 174 * The caller MUST acquire the session list lock before.
44e96653 175 * Returns the unique identifier for the session.
5b74c7b1 176 */
d022620a 177static uint64_t add_session_list(struct ltt_session *ls)
5b74c7b1 178{
a0377dfe 179 LTTNG_ASSERT(ls);
0525e9ae 180
f1494934
JG
181 cds_list_add(&ls->list, &the_session_list.head);
182 return the_session_list.next_uuid++;
5b74c7b1
DG
183}
184
185/*
050349bb 186 * Delete a ltt_session structure to the global list.
b5541356 187 *
050349bb 188 * The caller MUST acquire the session list lock before.
5b74c7b1
DG
189 */
190static void del_session_list(struct ltt_session *ls)
191{
a0377dfe 192 LTTNG_ASSERT(ls);
0525e9ae 193
5b74c7b1 194 cds_list_del(&ls->list);
5b74c7b1
DG
195}
196
b5541356 197/*
050349bb 198 * Return a pointer to the session list.
b5541356 199 */
cd9adb8b 200struct ltt_session_list *session_get_list()
b5541356 201{
f1494934 202 return &the_session_list;
b5541356
DG
203}
204
99d688f2
JG
205/*
206 * Returns once the session list is empty.
207 */
d9a970b7 208void session_list_wait_empty(std::unique_lock<std::mutex> list_lock)
99d688f2 209{
1a496780
JG
210 /* Keep waiting until the session list is empty. */
211 the_session_list.removal_cond.wait(list_lock,
212 [] { return cds_list_empty(&the_session_list.head); });
99d688f2
JG
213}
214
71e0a100
JG
215/*
216 * Try to acquire session list lock
217 */
0038180d 218int session_trylock_list() noexcept
71e0a100 219{
1a496780
JG
220 /* Return 0 if successfully acquired. */
221 return the_session_list.lock.try_lock() ? 0 : 1;
71e0a100
JG
222}
223
dd73d57b
JG
224/*
225 * Get the session's consumer destination type.
dd73d57b 226 */
a0a4f314 227enum consumer_dst_type session_get_consumer_destination_type(const ltt_session::locked_ref& session)
dd73d57b
JG
228{
229 /*
230 * The output information is duplicated in both of those session types.
231 * Hence, it doesn't matter from which it is retrieved. However, it is
232 * possible for only one of them to be set.
233 */
28ab034a
JG
234 return session->kernel_session ? session->kernel_session->consumer->type :
235 session->ust_session->consumer->type;
dd73d57b
JG
236}
237
238/*
239 * Get the session's consumer network hostname.
240 * The caller must ensure that the destination is of type "net".
dd73d57b 241 */
a0a4f314 242const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session)
dd73d57b 243{
cd9adb8b 244 const char *hostname = nullptr;
dd73d57b
JG
245 const struct consumer_output *output;
246
28ab034a
JG
247 output = session->kernel_session ? session->kernel_session->consumer :
248 session->ust_session->consumer;
dd73d57b
JG
249
250 /*
251 * hostname is assumed to be the same for both control and data
252 * connections.
253 */
254 switch (output->dst.net.control.dtype) {
255 case LTTNG_DST_IPV4:
256 hostname = output->dst.net.control.dst.ipv4;
257 break;
258 case LTTNG_DST_IPV6:
259 hostname = output->dst.net.control.dst.ipv6;
260 break;
261 default:
262 abort();
263 }
264 return hostname;
265}
266
267/*
268 * Get the session's consumer network control and data ports.
269 * The caller must ensure that the destination is of type "net".
dd73d57b 270 */
a0a4f314 271void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
28ab034a
JG
272 uint16_t *control_port,
273 uint16_t *data_port)
dd73d57b
JG
274{
275 const struct consumer_output *output;
276
28ab034a
JG
277 output = session->kernel_session ? session->kernel_session->consumer :
278 session->ust_session->consumer;
dd73d57b
JG
279 *control_port = output->dst.net.control.port;
280 *data_port = output->dst.net.data.port;
281}
282
5d65beab
JG
283/*
284 * Get the location of the latest trace archive produced by a rotation.
5d65beab 285 */
28ab034a 286struct lttng_trace_archive_location *
a0a4f314 287session_get_trace_archive_location(const ltt_session::locked_ref& session)
5d65beab 288{
d2956687 289 int ret;
cd9adb8b
JG
290 struct lttng_trace_archive_location *location = nullptr;
291 char *chunk_path = nullptr;
d2956687
JG
292
293 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED ||
28ab034a 294 !session->last_archived_chunk_name) {
d2956687
JG
295 goto end;
296 }
5d65beab 297
5d65beab
JG
298 switch (session_get_consumer_destination_type(session)) {
299 case CONSUMER_DST_LOCAL:
ecd1a12f 300 ret = asprintf(&chunk_path,
28ab034a
JG
301 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
302 session_get_base_path(session),
303 session->last_archived_chunk_name);
ecd1a12f
MD
304 if (ret == -1) {
305 goto end;
306 }
28ab034a 307 location = lttng_trace_archive_location_local_create(chunk_path);
5d65beab
JG
308 break;
309 case CONSUMER_DST_NET:
310 {
311 const char *hostname;
312 uint16_t control_port, data_port;
313
314 hostname = session_get_net_consumer_hostname(session);
28ab034a 315 session_get_net_consumer_ports(session, &control_port, &data_port);
5d65beab 316 location = lttng_trace_archive_location_relay_create(
28ab034a
JG
317 hostname,
318 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
319 control_port,
320 data_port,
321 session->last_chunk_path);
5d65beab
JG
322 break;
323 }
324 default:
325 abort();
326 }
327end:
d2956687 328 free(chunk_path);
5d65beab
JG
329 return location;
330}
331
23324029 332/*
e1bbf989 333 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
9c6518bc
JG
334 *
335 * The session list lock must be held.
23324029 336 */
cd9adb8b 337static int ltt_sessions_ht_alloc()
23324029
JD
338{
339 int ret = 0;
340
341 DBG("Allocating ltt_sessions_ht_by_id");
342 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
343 if (!ltt_sessions_ht_by_id) {
344 ret = -1;
345 ERR("Failed to allocate ltt_sessions_ht_by_id");
346 goto end;
347 }
e1bbf989
JR
348
349 DBG("Allocating ltt_sessions_ht_by_name");
350 ltt_sessions_ht_by_name = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
351 if (!ltt_sessions_ht_by_name) {
352 ret = -1;
353 ERR("Failed to allocate ltt_sessions_ht_by_name");
354 goto end;
355 }
356
23324029
JD
357end:
358 return ret;
359}
360
361/*
362 * Destroy the ltt_sessions_ht_by_id HT.
9c6518bc
JG
363 *
364 * The session list lock must be held.
23324029 365 */
cd9adb8b 366static void ltt_sessions_ht_destroy()
23324029 367{
e1bbf989 368 if (ltt_sessions_ht_by_id) {
3c339053 369 lttng_ht_destroy(ltt_sessions_ht_by_id);
cd9adb8b 370 ltt_sessions_ht_by_id = nullptr;
e1bbf989
JR
371 }
372
373 if (ltt_sessions_ht_by_name) {
3c339053 374 lttng_ht_destroy(ltt_sessions_ht_by_name);
cd9adb8b 375 ltt_sessions_ht_by_name = nullptr;
23324029 376 }
e1bbf989
JR
377
378 return;
23324029
JD
379}
380
381/*
e1bbf989
JR
382 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
383 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
384 * are allocated. The session list lock must be held.
23324029
JD
385 */
386static void add_session_ht(struct ltt_session *ls)
387{
388 int ret;
389
a0377dfe 390 LTTNG_ASSERT(ls);
23324029
JD
391
392 if (!ltt_sessions_ht_by_id) {
393 ret = ltt_sessions_ht_alloc();
394 if (ret) {
395 ERR("Error allocating the sessions HT");
396 goto end;
397 }
398 }
e1bbf989
JR
399
400 /* Should always be present with ltt_sessions_ht_by_id. */
a0377dfe 401 LTTNG_ASSERT(ltt_sessions_ht_by_name);
e1bbf989 402
23324029
JD
403 lttng_ht_node_init_u64(&ls->node, ls->id);
404 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
405
e1bbf989
JR
406 lttng_ht_node_init_str(&ls->node_by_name, ls->name);
407 lttng_ht_add_unique_str(ltt_sessions_ht_by_name, &ls->node_by_name);
408
23324029
JD
409end:
410 return;
411}
412
413/*
e1bbf989 414 * Test if ltt_sessions_ht_by_id/name are empty.
2a6ebf6b 415 * Return `false` if hash table objects are null.
23324029
JD
416 * The session list lock must be held.
417 */
cd9adb8b 418static bool ltt_sessions_ht_empty()
23324029 419{
2a6ebf6b 420 bool empty = false;
23324029
JD
421
422 if (!ltt_sessions_ht_by_id) {
2a6ebf6b 423 /* The hash tables do not exist yet. */
23324029
JD
424 goto end;
425 }
426
a0377dfe 427 LTTNG_ASSERT(ltt_sessions_ht_by_name);
e1bbf989 428
6f729565 429 if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) {
2a6ebf6b
JR
430 /* Not empty.*/
431 goto end;
432 }
433
434 /*
435 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
436 * empty.
437 *
438 * The removal from both hash tables is done in two different
439 * places:
440 * - removal from `ltt_sessions_ht_by_name` is done during
441 * `session_destroy()`
442 * - removal from `ltt_sessions_ht_by_id` is done later
443 * in `session_release()` on the last reference put.
444 *
445 * This means that it is possible for `ltt_sessions_ht_by_name` to be
446 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
447 * multiple sessions exists. The reverse is false, hence this sanity
448 * check.
449 */
450 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0);
451 empty = true;
23324029 452end:
2a6ebf6b 453 return empty;
23324029
JD
454}
455
456/*
ed41e570 457 * Remove a ltt_session from the ltt_sessions_ht_by_id.
e1bbf989 458 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
23324029
JD
459 * The session list lock must be held.
460 */
461static void del_session_ht(struct ltt_session *ls)
462{
463 struct lttng_ht_iter iter;
464 int ret;
465
a0377dfe
FD
466 LTTNG_ASSERT(ls);
467 LTTNG_ASSERT(ltt_sessions_ht_by_id);
468 LTTNG_ASSERT(ltt_sessions_ht_by_name);
23324029
JD
469
470 iter.iter.node = &ls->node.node;
471 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
a0377dfe 472 LTTNG_ASSERT(!ret);
23324029
JD
473
474 if (ltt_sessions_ht_empty()) {
b5950ff1 475 DBG("Empty ltt_sessions_ht_by_id/name, destroying hash tables");
23324029
JD
476 ltt_sessions_ht_destroy();
477 }
478}
479
b5541356 480/*
6c9cc2ab 481 * Acquire session lock
b5541356 482 */
16d64977 483void session_lock(const ltt_session *session)
b5541356 484{
a0377dfe 485 LTTNG_ASSERT(session);
d9a970b7
JG
486 session->lock();
487}
0525e9ae 488
d9a970b7
JG
489void ltt_session::lock() const noexcept
490{
491 pthread_mutex_lock(&_lock);
492}
493
494void ltt_session::unlock() const noexcept
495{
496 ltt_session::_const_session_unlock(*this);
6c9cc2ab 497}
b5541356 498
6c9cc2ab
DG
499/*
500 * Release session lock
501 */
16d64977 502void session_unlock(const ltt_session *session)
6c9cc2ab 503{
a0377dfe 504 LTTNG_ASSERT(session);
d9a970b7
JG
505 session->unlock();
506}
0525e9ae 507
a0a4f314 508void ltt_session::_const_session_unlock(const ltt_session& session)
d9a970b7
JG
509{
510 pthread_mutex_unlock(&session._lock);
b5541356
DG
511}
512
a0a4f314 513static int _session_set_trace_chunk_no_lock_check(const ltt_session::locked_ref& session,
28ab034a
JG
514 struct lttng_trace_chunk *new_trace_chunk,
515 struct lttng_trace_chunk **_current_trace_chunk)
82b69413 516{
78fc586b 517 int ret = 0;
82b69413 518 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
d2956687
JG
519 struct lttng_trace_chunk *current_trace_chunk;
520 uint64_t chunk_id;
521 enum lttng_trace_chunk_status chunk_status;
82b69413 522
07c4863f 523 const lttng::urcu::read_lock_guard read_lock;
82b69413 524 /*
d2956687
JG
525 * Ownership of current trace chunk is transferred to
526 * `current_trace_chunk`.
82b69413 527 */
d2956687 528 current_trace_chunk = session->current_trace_chunk;
cd9adb8b 529 session->current_trace_chunk = nullptr;
82b69413 530 if (session->ust_session) {
28ab034a 531 lttng_trace_chunk_put(session->ust_session->current_trace_chunk);
cd9adb8b 532 session->ust_session->current_trace_chunk = nullptr;
82b69413
JG
533 }
534 if (session->kernel_session) {
28ab034a 535 lttng_trace_chunk_put(session->kernel_session->current_trace_chunk);
cd9adb8b 536 session->kernel_session->current_trace_chunk = nullptr;
82b69413 537 }
d2956687
JG
538 if (!new_trace_chunk) {
539 ret = 0;
540 goto end;
82b69413 541 }
d2956687 542 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
a0377dfe 543 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
82b69413 544
d2956687
JG
545 refs_to_acquire = 1;
546 refs_to_acquire += !!session->ust_session;
547 refs_to_acquire += !!session->kernel_session;
548
28ab034a 549 for (refs_acquired = 0; refs_acquired < refs_to_acquire; refs_acquired++) {
d2956687
JG
550 if (!lttng_trace_chunk_get(new_trace_chunk)) {
551 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
28ab034a 552 session->name);
d2956687 553 goto error;
82b69413
JG
554 }
555 }
556
d2956687 557 if (session->ust_session) {
28ab034a
JG
558 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
559 const bool is_local_trace = session->ust_session->consumer->type ==
560 CONSUMER_DST_LOCAL;
e5add6d0 561
d2956687 562 session->ust_session->current_trace_chunk = new_trace_chunk;
f8a37411 563 if (is_local_trace) {
d2956687 564 enum lttng_error_code ret_error_code;
82b69413 565
28ab034a
JG
566 ret_error_code =
567 ust_app_create_channel_subdirectories(session->ust_session);
d2956687 568 if (ret_error_code != LTTNG_OK) {
82b69413
JG
569 goto error;
570 }
f8a37411 571 }
1758ca2a
JG
572
573 for (auto *socket :
574 lttng::urcu::lfht_iteration_adapter<consumer_socket,
575 decltype(consumer_socket::node),
576 &consumer_socket::node>(
577 *session->ust_session->consumer->socks->ht)) {
d2956687
JG
578 pthread_mutex_lock(socket->lock);
579 ret = consumer_create_trace_chunk(socket,
28ab034a
JG
580 relayd_id,
581 session->id,
582 new_trace_chunk,
583 DEFAULT_UST_TRACE_DIR);
d2956687 584 pthread_mutex_unlock(socket->lock);
f8a37411 585 if (ret) {
d2956687 586 goto error;
f8a37411
JG
587 }
588 }
589 }
1758ca2a 590
82b69413 591 if (session->kernel_session) {
28ab034a
JG
592 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
593 const bool is_local_trace = session->kernel_session->consumer->type ==
594 CONSUMER_DST_LOCAL;
e5add6d0 595
d2956687
JG
596 session->kernel_session->current_trace_chunk = new_trace_chunk;
597 if (is_local_trace) {
598 enum lttng_error_code ret_error_code;
599
28ab034a
JG
600 ret_error_code =
601 kernel_create_channel_subdirectories(session->kernel_session);
d2956687 602 if (ret_error_code != LTTNG_OK) {
d2956687
JG
603 goto error;
604 }
f8a37411 605 }
1758ca2a
JG
606
607 for (auto *socket :
608 lttng::urcu::lfht_iteration_adapter<consumer_socket,
609 decltype(consumer_socket::node),
610 &consumer_socket::node>(
611 *session->kernel_session->consumer->socks->ht)) {
d2956687
JG
612 pthread_mutex_lock(socket->lock);
613 ret = consumer_create_trace_chunk(socket,
28ab034a
JG
614 relayd_id,
615 session->id,
616 new_trace_chunk,
617 DEFAULT_KERNEL_TRACE_DIR);
d2956687 618 pthread_mutex_unlock(socket->lock);
f8a37411 619 if (ret) {
d2956687 620 goto error;
f8a37411
JG
621 }
622 }
623 }
82b69413
JG
624
625 /*
626 * Update local current trace chunk state last, only if all remote
d2956687 627 * creations succeeded.
82b69413
JG
628 */
629 session->current_trace_chunk = new_trace_chunk;
d2956687
JG
630 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
631end:
632 if (_current_trace_chunk) {
633 *_current_trace_chunk = current_trace_chunk;
cd9adb8b 634 current_trace_chunk = nullptr;
d2956687
JG
635 }
636end_no_move:
d2956687
JG
637 lttng_trace_chunk_put(current_trace_chunk);
638 return ret;
639error:
82b69413 640 if (session->ust_session) {
cd9adb8b 641 session->ust_session->current_trace_chunk = nullptr;
82b69413
JG
642 }
643 if (session->kernel_session) {
cd9adb8b 644 session->kernel_session->current_trace_chunk = nullptr;
82b69413 645 }
f8a37411 646 /*
82b69413
JG
647 * Release references taken in the case where all references could not
648 * be acquired.
649 */
650 refs_to_release = refs_to_acquire - refs_acquired;
651 for (i = 0; i < refs_to_release; i++) {
652 lttng_trace_chunk_put(new_trace_chunk);
653 }
d2956687
JG
654 ret = -1;
655 goto end_no_move;
82b69413
JG
656}
657
28ab034a 658struct lttng_trace_chunk *
a0a4f314 659session_create_new_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
660 const struct consumer_output *consumer_output_override,
661 const char *session_base_path_override,
662 const char *chunk_name_override)
82b69413
JG
663{
664 int ret;
cd9adb8b 665 struct lttng_trace_chunk *trace_chunk = nullptr;
82b69413 666 enum lttng_trace_chunk_status chunk_status;
cd9adb8b 667 const time_t chunk_creation_ts = time(nullptr);
348a81dc
JG
668 bool is_local_trace;
669 const char *base_path;
cd9adb8b 670 struct lttng_directory_handle *session_output_directory = nullptr;
82b69413 671 const struct lttng_credentials session_credentials = {
ff588497
JR
672 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
673 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
82b69413
JG
674 };
675 uint64_t next_chunk_id;
348a81dc 676 const struct consumer_output *output;
a7ceb342 677 const char *new_path;
348a81dc
JG
678
679 if (consumer_output_override) {
680 output = consumer_output_override;
681 } else {
a0377dfe 682 LTTNG_ASSERT(session->ust_session || session->kernel_session);
28ab034a
JG
683 output = session->ust_session ? session->ust_session->consumer :
684 session->kernel_session->consumer;
348a81dc
JG
685 }
686
687 is_local_trace = output->type == CONSUMER_DST_LOCAL;
28ab034a 688 base_path = session_base_path_override ?: consumer_output_get_base_path(output);
82b69413 689
d2956687
JG
690 if (chunk_creation_ts == (time_t) -1) {
691 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
28ab034a 692 session->name);
82b69413
JG
693 goto error;
694 }
82b69413 695
28ab034a
JG
696 next_chunk_id =
697 session->most_recent_chunk_id.is_set ? session->most_recent_chunk_id.value + 1 : 0;
82b69413 698
a7ceb342 699 if (session->current_trace_chunk &&
28ab034a 700 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
a7ceb342 701 chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk,
28ab034a 702 DEFAULT_CHUNK_TMP_OLD_DIRECTORY);
a7ceb342
MD
703 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
704 goto error;
705 }
706 }
707 if (!session->current_trace_chunk) {
708 if (!session->rotated) {
709 new_path = "";
710 } else {
cd9adb8b 711 new_path = nullptr;
a7ceb342
MD
712 }
713 } else {
714 new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY;
715 }
716
28ab034a 717 trace_chunk = lttng_trace_chunk_create(next_chunk_id, chunk_creation_ts, new_path);
82b69413 718 if (!trace_chunk) {
82b69413
JG
719 goto error;
720 }
721
722 if (chunk_name_override) {
28ab034a 723 chunk_status = lttng_trace_chunk_override_name(trace_chunk, chunk_name_override);
d2956687 724 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
725 goto error;
726 }
727 }
728
729 if (!is_local_trace) {
730 /*
731 * No need to set crendentials and output directory
732 * for remote trace chunks.
733 */
d2956687 734 goto end;
82b69413
JG
735 }
736
28ab034a 737 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, &session_credentials);
82b69413 738 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
739 goto error;
740 }
741
28ab034a
JG
742 DBG("Creating base output directory of session \"%s\" at %s", session->name, base_path);
743 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, session->uid, session->gid);
82b69413 744 if (ret) {
82b69413
JG
745 goto error;
746 }
cbf53d23
JG
747 session_output_directory = lttng_directory_handle_create(base_path);
748 if (!session_output_directory) {
82b69413
JG
749 goto error;
750 }
28ab034a 751 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, session_output_directory);
cbf53d23 752 lttng_directory_handle_put(session_output_directory);
cd9adb8b 753 session_output_directory = nullptr;
82b69413 754 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
82b69413
JG
755 goto error;
756 }
d2956687
JG
757end:
758 return trace_chunk;
82b69413 759error:
cbf53d23 760 lttng_directory_handle_put(session_output_directory);
82b69413 761 lttng_trace_chunk_put(trace_chunk);
cd9adb8b 762 trace_chunk = nullptr;
d2956687
JG
763 goto end;
764}
765
a0a4f314 766int session_close_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
767 struct lttng_trace_chunk *trace_chunk,
768 enum lttng_trace_chunk_command_type close_command,
769 char *closed_trace_chunk_path)
d2956687
JG
770{
771 int ret = 0;
772 bool error_occurred = false;
d2956687 773 enum lttng_trace_chunk_status chunk_status;
cd9adb8b 774 const time_t chunk_close_timestamp = time(nullptr);
a7ceb342 775 const char *new_path;
d2956687 776
28ab034a 777 chunk_status = lttng_trace_chunk_set_close_command(trace_chunk, close_command);
343defc2
MD
778 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
779 ret = -1;
780 goto end;
bbc4768c
JG
781 }
782
d2956687
JG
783 if (chunk_close_timestamp == (time_t) -1) {
784 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
28ab034a 785 session->name);
d2956687
JG
786 ret = -1;
787 goto end;
788 }
a7ceb342
MD
789
790 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) {
791 /* New chunk stays in session output directory. */
792 new_path = "";
793 } else {
794 /* Use chunk name for new chunk. */
cd9adb8b 795 new_path = nullptr;
a7ceb342
MD
796 }
797 if (session->current_trace_chunk &&
28ab034a 798 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
a7ceb342 799 /* Rename new chunk path. */
28ab034a
JG
800 chunk_status =
801 lttng_trace_chunk_rename_path(session->current_trace_chunk, new_path);
a7ceb342
MD
802 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
803 ret = -1;
804 goto end;
805 }
806 }
807 if (!lttng_trace_chunk_get_name_overridden(trace_chunk) &&
28ab034a 808 close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) {
a7ceb342
MD
809 const char *old_path;
810
811 if (!session->rotated) {
812 old_path = "";
813 } else {
cd9adb8b 814 old_path = nullptr;
a7ceb342
MD
815 }
816 /* We need to move back the .tmp_old_chunk to its rightful place. */
28ab034a 817 chunk_status = lttng_trace_chunk_rename_path(trace_chunk, old_path);
a7ceb342
MD
818 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
819 ret = -1;
820 goto end;
821 }
822 }
823 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) {
824 session->rotated = true;
825 }
28ab034a 826 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, chunk_close_timestamp);
d2956687
JG
827 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
828 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
28ab034a 829 session->name);
d2956687
JG
830 ret = -1;
831 goto end;
832 }
833
834 if (session->ust_session) {
28ab034a 835 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
0a184d4e 836
1758ca2a
JG
837 for (auto *socket :
838 lttng::urcu::lfht_iteration_adapter<consumer_socket,
839 decltype(consumer_socket::node),
840 &consumer_socket::node>(
841 *session->ust_session->consumer->socks->ht)) {
d2956687
JG
842 pthread_mutex_lock(socket->lock);
843 ret = consumer_close_trace_chunk(socket,
28ab034a
JG
844 relayd_id,
845 session->id,
846 trace_chunk,
847 closed_trace_chunk_path);
d2956687
JG
848 pthread_mutex_unlock(socket->lock);
849 if (ret) {
850 ERR("Failed to close trace chunk on user space consumer");
851 error_occurred = true;
852 }
853 }
854 }
855 if (session->kernel_session) {
28ab034a 856 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
0a184d4e 857
1758ca2a
JG
858 for (auto *socket :
859 lttng::urcu::lfht_iteration_adapter<consumer_socket,
860 decltype(consumer_socket::node),
861 &consumer_socket::node>(
862 *session->kernel_session->consumer->socks->ht)) {
d2956687
JG
863 pthread_mutex_lock(socket->lock);
864 ret = consumer_close_trace_chunk(socket,
28ab034a
JG
865 relayd_id,
866 session->id,
867 trace_chunk,
868 closed_trace_chunk_path);
d2956687
JG
869 pthread_mutex_unlock(socket->lock);
870 if (ret) {
871 ERR("Failed to close trace chunk on kernel consumer");
872 error_occurred = true;
873 }
874 }
875 }
876 ret = error_occurred ? -1 : 0;
82b69413 877end:
d2956687 878 return ret;
82b69413
JG
879}
880
04ed9e10
JG
881/*
882 * This function skips the metadata channel as the begin/end timestamps of a
883 * metadata packet are useless.
884 *
885 * Moreover, opening a packet after a "clear" will cause problems for live
886 * sessions as it will introduce padding that was not part of the first trace
887 * chunk. The relay daemon expects the content of the metadata stream of
888 * successive metadata trace chunks to be strict supersets of one another.
889 *
890 * For example, flushing a packet at the beginning of the metadata stream of
891 * a trace chunk resulting from a "clear" session command will cause the
892 * size of the metadata stream of the new trace chunk to not match the size of
893 * the metadata stream of the original chunk. This will confuse the relay
894 * daemon as the same "offset" in a metadata stream will no longer point
895 * to the same content.
896 */
a0a4f314 897static enum lttng_error_code session_kernel_open_packets(const ltt_session::locked_ref& session)
04ed9e10
JG
898{
899 enum lttng_error_code ret = LTTNG_OK;
04ed9e10
JG
900 struct lttng_ht_iter iter;
901 struct cds_lfht_node *node;
04ed9e10 902
07c4863f 903 const lttng::urcu::read_lock_guard read_lock;
04ed9e10
JG
904
905 cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter);
906 node = cds_lfht_iter_get_node(&iter.iter);
6fe86373 907 auto *socket = lttng_ht_node_container_of(node, &consumer_socket::node);
04ed9e10 908
b634601e
JG
909 for (auto chan :
910 lttng::urcu::list_iteration_adapter<ltt_kernel_channel, &ltt_kernel_channel::list>(
911 session->kernel_session->channel_list.head)) {
04ed9e10
JG
912 int open_ret;
913
914 DBG("Open packet of kernel channel: channel key = %" PRIu64
28ab034a
JG
915 ", session name = %s, session_id = %" PRIu64,
916 chan->key,
917 session->name,
918 session->id);
04ed9e10
JG
919
920 open_ret = consumer_open_channel_packets(socket, chan->key);
921 if (open_ret < 0) {
922 /* General error (no known error expected). */
923 ret = LTTNG_ERR_UNK;
924 goto end;
925 }
926 }
927
928end:
04ed9e10
JG
929 return ret;
930}
931
a0a4f314 932enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session)
04ed9e10
JG
933{
934 enum lttng_error_code ret = LTTNG_OK;
935
936 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64,
28ab034a
JG
937 session->name,
938 session->id);
04ed9e10
JG
939
940 if (session->ust_session) {
941 ret = ust_app_open_packets(session);
942 if (ret != LTTNG_OK) {
943 goto end;
944 }
945 }
946
947 if (session->kernel_session) {
948 ret = session_kernel_open_packets(session);
949 if (ret != LTTNG_OK) {
950 goto end;
951 }
952 }
953
954end:
955 return ret;
956}
957
82b69413
JG
958/*
959 * Set a session's current trace chunk.
960 *
961 * Must be called with the session lock held.
962 */
a0a4f314 963int session_set_trace_chunk(const ltt_session::locked_ref& session,
28ab034a
JG
964 struct lttng_trace_chunk *new_trace_chunk,
965 struct lttng_trace_chunk **current_trace_chunk)
82b69413 966{
d9a970b7 967 ASSERT_LOCKED(session->_lock);
28ab034a
JG
968 return _session_set_trace_chunk_no_lock_check(
969 session, new_trace_chunk, current_trace_chunk);
82b69413
JG
970}
971
a0a4f314 972static void session_notify_destruction(const ltt_session::locked_ref& session)
3e3665b8
JG
973{
974 size_t i;
a0a4f314 975 const auto count = lttng_dynamic_array_get_count(&session->destroy_notifiers);
3e3665b8
JG
976
977 for (i = 0; i < count; i++) {
978 const struct ltt_session_destroy_notifier_element *element =
7966af57 979 (ltt_session_destroy_notifier_element *) lttng_dynamic_array_get_element(
28ab034a 980 &session->destroy_notifiers, i);
3e3665b8
JG
981
982 element->notifier(session, element->user_data);
983 }
984}
985
ccbdaca4
MD
986/*
987 * Fire each clear notifier once, and remove them from the array.
988 */
a0a4f314 989void session_notify_clear(const ltt_session::locked_ref& session)
ccbdaca4
MD
990{
991 size_t i;
a0a4f314 992 const auto count = lttng_dynamic_array_get_count(&session->clear_notifiers);
ccbdaca4
MD
993
994 for (i = 0; i < count; i++) {
995 const struct ltt_session_clear_notifier_element *element =
7966af57 996 (ltt_session_clear_notifier_element *) lttng_dynamic_array_get_element(
a0a4f314 997 &session->clear_notifiers, i);
ccbdaca4 998
a0a4f314 999 element->notifier(session, element->user_data);
ccbdaca4 1000 }
a0a4f314 1001 lttng_dynamic_array_clear(&session->clear_notifiers);
ccbdaca4
MD
1002}
1003
28ab034a 1004static void session_release(struct urcu_ref *ref)
e32d7f27
JG
1005{
1006 int ret;
1007 struct ltt_ust_session *usess;
1008 struct ltt_kernel_session *ksess;
d9a970b7 1009 struct ltt_session *session = lttng::utils::container_of(ref, &ltt_session::ref_count);
7fdbed1c 1010 const bool session_published = session->published;
e32d7f27 1011
a0377dfe 1012 LTTNG_ASSERT(!session->chunk_being_archived);
d2956687 1013
e32d7f27
JG
1014 usess = session->ust_session;
1015 ksess = session->kernel_session;
3e3665b8 1016
f8a37411 1017 /* Clean kernel session teardown, keeping data for destroy notifier. */
e32d7f27
JG
1018 kernel_destroy_session(ksess);
1019
d070c424 1020 /* UST session teardown, keeping data for destroy notifier. */
e32d7f27
JG
1021 if (usess) {
1022 /* Close any relayd session */
1023 consumer_output_send_destroy_relayd(usess->consumer);
1024
1025 /* Destroy every UST application related to this session. */
1026 ret = ust_app_destroy_trace_all(usess);
1027 if (ret) {
1028 ERR("Error in ust_app_destroy_trace_all");
1029 }
1030
d070c424 1031 /* Clean up the rest, keeping destroy notifier data. */
e32d7f27
JG
1032 trace_ust_destroy_session(usess);
1033 }
1034
1035 /*
1036 * Must notify the kernel thread here to update it's poll set in order to
1037 * remove the channel(s)' fd just destroyed.
1038 */
412d7227 1039 ret = notify_thread_pipe(the_kernel_poll_pipe[1]);
e32d7f27
JG
1040 if (ret < 0) {
1041 PERROR("write kernel poll pipe");
1042 }
1043
1044 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
e32d7f27 1045
e32d7f27 1046 snapshot_destroy(&session->snapshot);
99d688f2 1047
7fdbed1c 1048 if (session_published) {
1a496780 1049 ASSERT_SESSION_LIST_LOCKED();
f4cc5e83
JG
1050 del_session_list(session);
1051 del_session_ht(session);
f4cc5e83 1052 }
a0a4f314
JG
1053
1054 /*
1055 * The notifiers make use of free-functions that expect a locked reference to a session.
1056 * To create such a reference, we need to acquire the lock and acquire a reference (increase
1057 * the ref-count). To ensure the release of the reference does not re-cross the zero value,
1058 * set the refcount to a tombstone value.
1059 */
1060 session->ref_count.refcount = 0xDEAD5E55;
1061 session_notify_destruction([session]() {
1062 session_lock(session);
1063 session_get(session);
16d64977 1064 return ltt_session::make_locked_ref(*session);
a0a4f314
JG
1065 }());
1066
1067 pthread_mutex_destroy(&session->_lock);
d070c424 1068
1ac9cb73 1069 consumer_output_put(session->consumer);
d070c424 1070 kernel_free_session(ksess);
cd9adb8b 1071 session->kernel_session = nullptr;
d070c424
MD
1072 if (usess) {
1073 trace_ust_free_session(usess);
cd9adb8b 1074 session->ust_session = nullptr;
d070c424 1075 }
7fdbed1c 1076 lttng_dynamic_array_reset(&session->destroy_notifiers);
ccbdaca4 1077 lttng_dynamic_array_reset(&session->clear_notifiers);
d2956687 1078 free(session->last_archived_chunk_name);
6fa5fe7c 1079 free(session->base_path);
c08136a3 1080 lttng_trigger_put(session->rotate_trigger);
e32d7f27 1081 free(session);
7fdbed1c
JG
1082 if (session_published) {
1083 /*
1a496780
JG
1084 * Notify after free-ing to ensure the memory is
1085 * reclaimed before the main thread exits (and prevent memory leak
1086 * reports).
7fdbed1c 1087 */
1a496780
JG
1088 ASSERT_SESSION_LIST_LOCKED();
1089 the_session_list.removal_cond.notify_all();
7fdbed1c 1090 }
e32d7f27
JG
1091}
1092
1093/*
1094 * Acquire a reference to a session.
1095 * This function may fail (return false); its return value must be checked.
1096 */
1097bool session_get(struct ltt_session *session)
1098{
d9a970b7 1099 return urcu_ref_get_unless_zero(&session->ref_count);
e32d7f27
JG
1100}
1101
1102/*
1103 * Release a reference to a session.
1104 */
1105void session_put(struct ltt_session *session)
1106{
b178f53e
JG
1107 if (!session) {
1108 return;
1109 }
e32d7f27
JG
1110 /*
1111 * The session list lock must be held as any session_put()
1112 * may cause the removal of the session from the session_list.
1113 */
1a496780 1114 ASSERT_SESSION_LIST_LOCKED();
d9a970b7
JG
1115 LTTNG_ASSERT(session->ref_count.refcount);
1116 urcu_ref_put(&session->ref_count, session_release);
e32d7f27
JG
1117}
1118
1119/*
1120 * Destroy a session.
1121 *
1122 * This method does not immediately release/free the session as other
1123 * components may still hold a reference to the session. However,
1124 * the session should no longer be presented to the user.
1125 *
1126 * Releases the session list's reference to the session
1127 * and marks it as destroyed. Iterations on the session list should be
1128 * mindful of the "destroyed" flag.
1129 */
1130void session_destroy(struct ltt_session *session)
1131{
ed41e570
JR
1132 int ret;
1133 struct lttng_ht_iter iter;
1134
a0377dfe 1135 LTTNG_ASSERT(!session->destroyed);
e32d7f27 1136 session->destroyed = true;
ed41e570
JR
1137
1138 /*
1139 * Remove immediately from the "session by name" hash table. Only one
1140 * session is expected to exist with a given name for at any given time.
1141 *
1142 * Even if a session still technically exists for a little while longer,
1143 * there is no point in performing action on a "destroyed" session.
1144 */
1145 iter.iter.node = &session->node_by_name.node;
1146 ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter);
1147 LTTNG_ASSERT(!ret);
1148
e32d7f27
JG
1149 session_put(session);
1150}
1151
a0a4f314 1152int session_add_destroy_notifier(const ltt_session::locked_ref& session,
28ab034a
JG
1153 ltt_session_destroy_notifier notifier,
1154 void *user_data)
3e3665b8 1155{
28ab034a
JG
1156 const struct ltt_session_destroy_notifier_element element = { .notifier = notifier,
1157 .user_data = user_data };
3e3665b8 1158
28ab034a 1159 return lttng_dynamic_array_add_element(&session->destroy_notifiers, &element);
3e3665b8
JG
1160}
1161
a0a4f314 1162int session_add_clear_notifier(const ltt_session::locked_ref& session,
28ab034a
JG
1163 ltt_session_clear_notifier notifier,
1164 void *user_data)
ccbdaca4 1165{
28ab034a
JG
1166 const struct ltt_session_clear_notifier_element element = { .notifier = notifier,
1167 .user_data = user_data };
ccbdaca4 1168
28ab034a 1169 return lttng_dynamic_array_add_element(&session->clear_notifiers, &element);
ccbdaca4
MD
1170}
1171
5b74c7b1 1172/*
b178f53e
JG
1173 * Create a new session and add it to the session list.
1174 * Session list lock must be held by the caller.
5b74c7b1 1175 */
28ab034a
JG
1176enum lttng_error_code
1177session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session)
5b74c7b1 1178{
f3ed775e 1179 int ret;
b178f53e 1180 enum lttng_error_code ret_code;
cd9adb8b 1181 struct ltt_session *new_session = nullptr;
e07ae692 1182
1a496780 1183 ASSERT_SESSION_LIST_LOCKED();
b178f53e
JG
1184 if (name) {
1185 struct ltt_session *clashing_session;
1186
1187 clashing_session = session_find_by_name(name);
1188 if (clashing_session) {
1189 session_put(clashing_session);
1190 ret_code = LTTNG_ERR_EXIST_SESS;
1191 goto error;
1192 }
1193 }
64803277 1194 new_session = zmalloc<ltt_session>();
b178f53e
JG
1195 if (!new_session) {
1196 PERROR("Failed to allocate an ltt_session structure");
1197 ret_code = LTTNG_ERR_NOMEM;
1198 goto error;
5b74c7b1
DG
1199 }
1200
3e3665b8 1201 lttng_dynamic_array_init(&new_session->destroy_notifiers,
28ab034a 1202 sizeof(struct ltt_session_destroy_notifier_element),
cd9adb8b 1203 nullptr);
ccbdaca4 1204 lttng_dynamic_array_init(&new_session->clear_notifiers,
28ab034a 1205 sizeof(struct ltt_session_clear_notifier_element),
cd9adb8b 1206 nullptr);
d9a970b7
JG
1207 urcu_ref_init(&new_session->ref_count);
1208 pthread_mutex_init(&new_session->_lock, nullptr);
e32d7f27 1209
cd9adb8b 1210 new_session->creation_time = time(nullptr);
b178f53e
JG
1211 if (new_session->creation_time == (time_t) -1) {
1212 PERROR("Failed to sample session creation time");
1213 ret_code = LTTNG_ERR_SESSION_FAIL;
f3ed775e
DG
1214 goto error;
1215 }
1216
b178f53e
JG
1217 /* Create default consumer output. */
1218 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
cd9adb8b 1219 if (new_session->consumer == nullptr) {
b178f53e 1220 ret_code = LTTNG_ERR_NOMEM;
1c1c3634
DG
1221 goto error;
1222 }
1223
b178f53e
JG
1224 if (name) {
1225 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1226 if (ret) {
1227 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1228 goto error;
1229 }
1230 ret = validate_name(name);
1231 if (ret < 0) {
1232 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1233 goto error;
1234 }
1235 } else {
1236 int i = 0;
1237 bool found_name = false;
1238 char datetime[16];
1239 struct tm *timeinfo;
1240
1241 timeinfo = localtime(&new_session->creation_time);
1242 if (!timeinfo) {
1243 ret_code = LTTNG_ERR_SESSION_FAIL;
1244 goto error;
1245 }
1246 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1247 for (i = 0; i < INT_MAX; i++) {
1248 struct ltt_session *clashing_session;
1249
1250 if (i == 0) {
1251 ret = snprintf(new_session->name,
28ab034a
JG
1252 sizeof(new_session->name),
1253 "%s-%s",
1254 DEFAULT_SESSION_NAME,
1255 datetime);
b178f53e
JG
1256 } else {
1257 ret = snprintf(new_session->name,
28ab034a
JG
1258 sizeof(new_session->name),
1259 "%s%d-%s",
1260 DEFAULT_SESSION_NAME,
1261 i,
1262 datetime);
b178f53e 1263 }
46ef2188 1264 new_session->name_contains_creation_time = true;
b178f53e
JG
1265 if (ret == -1 || ret >= sizeof(new_session->name)) {
1266 /*
1267 * Null-terminate in case the name is used
1268 * in logging statements.
1269 */
1270 new_session->name[sizeof(new_session->name) - 1] = '\0';
1271 ret_code = LTTNG_ERR_SESSION_FAIL;
1272 goto error;
1273 }
1274
28ab034a 1275 clashing_session = session_find_by_name(new_session->name);
b178f53e
JG
1276 session_put(clashing_session);
1277 if (!clashing_session) {
1278 found_name = true;
1279 break;
1280 }
1281 }
1282 if (found_name) {
1283 DBG("Generated session name \"%s\"", new_session->name);
1284 new_session->has_auto_generated_name = true;
1285 } else {
1286 ERR("Failed to auto-generate a session name");
1287 ret_code = LTTNG_ERR_SESSION_FAIL;
1288 goto error;
1289 }
1290 }
1291
d3e2ba59 1292 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
73184835
DG
1293 if (ret < 0) {
1294 if (errno == ENAMETOOLONG) {
1295 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
b178f53e 1296 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
28ab034a 1297 new_session->hostname);
73184835 1298 } else {
b178f53e 1299 ret_code = LTTNG_ERR_SESSION_FAIL;
73184835
DG
1300 goto error;
1301 }
d3e2ba59
JD
1302 }
1303
6df2e2c9
MD
1304 new_session->uid = uid;
1305 new_session->gid = gid;
1306
6dc3064a
DG
1307 ret = snapshot_init(&new_session->snapshot);
1308 if (ret < 0) {
b178f53e 1309 ret_code = LTTNG_ERR_NOMEM;
6dc3064a
DG
1310 goto error;
1311 }
1312
4f23c583 1313 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
92816cc3 1314
b178f53e 1315 /* Add new session to the session list. */
a991f516 1316 new_session->id = add_session_list(new_session);
b178f53e 1317
23324029
JD
1318 /*
1319 * Add the new session to the ltt_sessions_ht_by_id.
1320 * No ownership is taken by the hash table; it is merely
1321 * a wrapper around the session list used for faster access
1322 * by session id.
1323 */
1324 add_session_ht(new_session);
f4cc5e83 1325 new_session->published = true;
b5541356 1326
a4b92340 1327 /*
b178f53e
JG
1328 * Consumer is left to NULL since the create_session_uri command will
1329 * set it up and, if valid, assign it to the session.
a4b92340 1330 */
b178f53e 1331 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
28ab034a
JG
1332 new_session->name,
1333 new_session->id,
1334 new_session->uid,
1335 new_session->gid);
b178f53e
JG
1336 ret_code = LTTNG_OK;
1337end:
1338 if (new_session) {
1339 (void) session_get(new_session);
1340 *out_session = new_session;
1341 }
1342 return ret_code;
5b74c7b1 1343error:
f4cc5e83 1344 session_put(new_session);
cd9adb8b 1345 new_session = nullptr;
b178f53e 1346 goto end;
5b74c7b1 1347}
2f77fc4b
DG
1348
1349/*
d7b377ed 1350 * Check if the UID matches the session. Root user has access to all
2f77fc4b
DG
1351 * sessions.
1352 */
a0a4f314 1353bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid)
2f77fc4b 1354{
d7b377ed 1355 return (uid == session->uid) || uid == 0;
2f77fc4b 1356}
2961f09e
JG
1357
1358/*
1359 * Set a session's rotation state and reset all associated state.
1360 *
1361 * This function resets the rotation state (check timers, pending
1362 * flags, etc.) and sets the result of the last rotation. The result
1363 * can be queries by a liblttng-ctl client.
1364 *
1365 * Be careful of the result passed to this function. For instance,
1366 * on failure to launch a rotation, a client will expect the rotation
83ed9e90 1367 * state to be set to "NO_ROTATION". If an error occurred while the
2961f09e
JG
1368 * rotation was "ONGOING", result should be set to "ERROR", which will
1369 * allow a client to report it.
1370 *
1371 * Must be called with the session and session_list locks held.
1372 */
a0a4f314
JG
1373int session_reset_rotation_state(const ltt_session::locked_ref& session,
1374 enum lttng_rotation_state result)
2961f09e
JG
1375{
1376 int ret = 0;
1377
1a496780 1378 ASSERT_SESSION_LIST_LOCKED();
2961f09e 1379
a0a4f314
JG
1380 session->rotation_state = result;
1381 if (session->rotation_pending_check_timer_enabled) {
2961f09e
JG
1382 ret = timer_session_rotation_pending_check_stop(session);
1383 }
a0a4f314 1384 if (session->chunk_being_archived) {
d2956687
JG
1385 uint64_t chunk_id;
1386 enum lttng_trace_chunk_status chunk_status;
1387
a0a4f314 1388 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
a0377dfe 1389 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
a0a4f314
JG
1390 LTTNG_OPTIONAL_SET(&session->last_archived_chunk_id, chunk_id);
1391 lttng_trace_chunk_put(session->chunk_being_archived);
1392 session->chunk_being_archived = nullptr;
ccbdaca4
MD
1393 /*
1394 * Fire the clear reply notifiers if we are completing a clear
1395 * rotation.
1396 */
1397 session_notify_clear(session);
d2956687 1398 }
2961f09e
JG
1399 return ret;
1400}
e1bbf989
JR
1401
1402/*
1403 * Sample the id of a session looked up via its name.
1404 * Here the term "sampling" hint the caller that this return the id at a given
1405 * point in time with no guarantee that the session for which the id was
1406 * sampled still exist at that point.
1407 *
1408 * Return 0 when the session is not found,
1409 * Return 1 when the session is found and set `id`.
1410 */
1411bool sample_session_id_by_name(const char *name, uint64_t *id)
1412{
1413 bool found = false;
1414 struct lttng_ht_node_str *node;
1415 struct lttng_ht_iter iter;
1416 struct ltt_session *ls;
1417
07c4863f 1418 const lttng::urcu::read_lock_guard read_lock;
e1bbf989
JR
1419
1420 if (!ltt_sessions_ht_by_name) {
1421 found = false;
1422 goto end;
1423 }
1424
1425 lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter);
00d7d903 1426 node = lttng_ht_iter_get_node<lttng_ht_node_str>(&iter);
cd9adb8b 1427 if (node == nullptr) {
e1bbf989
JR
1428 found = false;
1429 goto end;
1430 }
1431
0114db0e 1432 ls = lttng::utils::container_of(node, &ltt_session::node_by_name);
e1bbf989
JR
1433 *id = ls->id;
1434 found = true;
1435
1436 DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name);
1437end:
e1bbf989
JR
1438 return found;
1439}
e99e3664 1440
d9a970b7 1441void ltt_session::_locked_session_release(ltt_session *session)
e99e3664 1442{
0038180d
JG
1443 if (!session) {
1444 return;
1445 }
1446
e99e3664
JG
1447 session_unlock(session);
1448 session_put(session);
1449}
1450
d9a970b7
JG
1451void ltt_session::_locked_const_session_release(const ltt_session *session)
1452{
1453 if (!session) {
1454 return;
1455 }
1456
1457 ltt_session::_const_session_unlock(*session);
1458 ltt_session::_const_session_put(session);
1459}
1460
1461ltt_session::locked_ref ltt_session::find_locked_session(ltt_session::id_t id)
e99e3664 1462{
07c4863f 1463 const lttng::urcu::read_lock_guard rcu_lock;
e99e3664
JG
1464 auto session = session_find_by_id(id);
1465
1466 if (!session) {
d9a970b7 1467 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
e99e3664
JG
1468 }
1469
1470 /*
1471 * The pointer falling out of scope will unlock and release the reference to the
1472 * session.
1473 */
1474 session_lock(session);
16d64977 1475 return ltt_session::make_locked_ref(*session);
e99e3664
JG
1476}
1477
d9a970b7
JG
1478ltt_session::locked_ref ltt_session::find_locked_session(lttng::c_string_view name)
1479{
07c4863f 1480 const lttng::urcu::read_lock_guard rcu_lock;
d9a970b7
JG
1481 auto session = session_find_by_name(name.data());
1482
1483 if (!session) {
1484 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1485 }
1486
1487 session_lock(session);
16d64977 1488 return ltt_session::make_locked_ref(*session);
d9a970b7
JG
1489}
1490
1491ltt_session::const_locked_ref ltt_session::find_locked_const_session(ltt_session::id_t id)
1492{
07c4863f 1493 const lttng::urcu::read_lock_guard rcu_lock;
16d64977 1494 const auto *session = session_find_by_id(id);
d9a970b7
JG
1495
1496 if (!session) {
1497 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1498 }
1499
1500 session_lock(session);
16d64977 1501 return ltt_session::make_locked_ref(*session);
d9a970b7
JG
1502}
1503
1504ltt_session::const_locked_ref ltt_session::find_locked_const_session(lttng::c_string_view name)
1505{
07c4863f 1506 const lttng::urcu::read_lock_guard rcu_lock;
16d64977 1507 const auto *session = session_find_by_name(name.data());
d9a970b7
JG
1508
1509 if (!session) {
1510 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1511 }
1512
1513 session_lock(session);
16d64977 1514 return ltt_session::make_locked_ref(*session);
d9a970b7
JG
1515}
1516
1517ltt_session::ref ltt_session::find_session(ltt_session::id_t id)
1518{
07c4863f 1519 const lttng::urcu::read_lock_guard rcu_lock;
d9a970b7
JG
1520 auto session = session_find_by_id(id);
1521
1522 if (!session) {
1523 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1524 }
1525
16d64977 1526 return ltt_session::make_ref(*session);
d9a970b7
JG
1527}
1528
1529ltt_session::ref ltt_session::find_session(lttng::c_string_view name)
1530{
07c4863f 1531 const lttng::urcu::read_lock_guard rcu_lock;
d9a970b7
JG
1532 auto session = session_find_by_name(name.data());
1533
1534 if (!session) {
1535 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1536 }
1537
16d64977 1538 return ltt_session::make_ref(*session);
d9a970b7
JG
1539}
1540
1541ltt_session::const_ref ltt_session::find_const_session(ltt_session::id_t id)
e99e3664 1542{
07c4863f 1543 const lttng::urcu::read_lock_guard rcu_lock;
16d64977 1544 const auto *session = session_find_by_id(id);
e99e3664
JG
1545
1546 if (!session) {
d9a970b7
JG
1547 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1548 }
1549
16d64977 1550 return ltt_session::make_ref(*session);
d9a970b7
JG
1551}
1552
1553ltt_session::const_ref ltt_session::find_const_session(lttng::c_string_view name)
1554{
07c4863f 1555 const lttng::urcu::read_lock_guard rcu_lock;
16d64977 1556 const auto *session = session_find_by_name(name.data());
d9a970b7
JG
1557
1558 if (!session) {
1559 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
e99e3664
JG
1560 }
1561
16d64977 1562 return ltt_session::make_ref(*session);
d9a970b7
JG
1563}
1564
1565void ltt_session::_const_session_put(const ltt_session *session)
1566{
1567 /*
1568 * The session list lock must be held as any session_put()
1569 * may cause the removal of the session from the session_list.
1570 */
1571 ASSERT_SESSION_LIST_LOCKED();
1572 LTTNG_ASSERT(session->ref_count.refcount);
1573 urcu_ref_put(&session->ref_count, session_release);
1574}
1575
1576std::unique_lock<std::mutex> ls::lock_session_list()
1577{
1578 return std::unique_lock<std::mutex>(the_session_list.lock);
e99e3664 1579}
16d64977
JG
1580
1581lttng::sessiond::user_space_consumer_channel_keys
1582ltt_session::user_space_consumer_channel_keys() const
1583{
1584 switch (ust_session->buffer_type) {
1585 case LTTNG_BUFFER_PER_PID:
1586 return lttng::sessiond::user_space_consumer_channel_keys(*ust_session,
1587 *ust_app_get_all());
1588 case LTTNG_BUFFER_PER_UID:
1589 return lttng::sessiond::user_space_consumer_channel_keys(
1590 *ust_session, ust_session->buffer_reg_uid_list);
1591 default:
1592 abort();
1593 }
1594}
1595
1596ls::user_space_consumer_channel_keys::iterator
1597ls::user_space_consumer_channel_keys::begin() const noexcept
1598{
1599 return ls::user_space_consumer_channel_keys::iterator(_creation_context);
1600}
1601
1602ls::user_space_consumer_channel_keys::iterator
1603ls::user_space_consumer_channel_keys::end() const noexcept
1604{
1605 return ls::user_space_consumer_channel_keys::iterator(_creation_context, true);
1606}
1607
1608ls::user_space_consumer_channel_keys::iterator&
1609ls::user_space_consumer_channel_keys::iterator::operator++()
1610{
1611 if (_is_end) {
1612 LTTNG_THROW_OUT_OF_RANGE(fmt::format(
1613 "Attempted to advance channel key iterator past the end of channel keys: iteration_mode={}",
1614 _creation_context._session.buffer_type));
1615 }
1616
1617 switch (_creation_context._session.buffer_type) {
1618 case LTTNG_BUFFER_PER_PID:
1619 _advance_one_per_pid();
1620 break;
1621 case LTTNG_BUFFER_PER_UID:
1622 _advance_one_per_uid();
1623 break;
1624 default:
1625 abort();
1626 }
1627
1628 return *this;
1629}
1630
1631namespace {
1632bool is_list_empty(const cds_list_head *head)
1633{
1634 return head == head->next;
1635}
1636
1637bool is_last_element_of_list(const cds_list_head *head)
1638{
1639 return head->next == head->prev;
1640}
1641} /* namespace */
1642
1643ls::user_space_consumer_channel_keys::iterator::iterator(
1644 const _iterator_creation_context& creation_context, bool is_end) :
1645 _creation_context(creation_context), _is_end(is_end)
1646{
1647 if (_is_end) {
1648 return;
1649 }
1650
1651 switch (_creation_context._mode) {
1652 case _iteration_mode::PER_PID:
1653 _init_per_pid();
1654 break;
1655 case _iteration_mode::PER_UID:
1656 _init_per_uid();
1657 break;
1658 }
1659}
1660
1661void ls::user_space_consumer_channel_keys::iterator::_skip_to_next_app_per_pid(
1662 bool try_current) noexcept
1663{
1664 auto& position = _position._per_pid;
1665
1666 while (true) {
1667 if (!try_current) {
1668 lttng_ht_get_next(_creation_context._container.apps,
1669 &position.app_iterator);
1670 } else {
1671 try_current = false;
1672 }
1673
1674 const auto app_node =
1675 lttng_ht_iter_get_node<lttng_ht_node_ulong>(&position.app_iterator);
1676 if (!app_node) {
1677 _is_end = true;
1678 return;
1679 }
1680
1681 const auto& app = *lttng::utils::container_of(app_node, &ust_app::pid_n);
1682 auto app_session = ust_app_lookup_app_session(&_creation_context._session, &app);
1683
1684 if (!app_session) {
1685 /* This app is not traced by the target session. */
1686 continue;
1687 }
1688
1689 position.current_app_session = app_session->lock();
1690
1691 auto *registry = ust_app_get_session_registry(
1692 (*_position._per_pid.current_app_session)->get_identifier());
1693 if (!registry) {
1694 DBG_FMT("Application session is being torn down: skipping application: app={}",
1695 app);
1696 continue;
1697 }
1698
1699 position.current_registry_session = registry;
1700 lttng_ht_get_first((*position.current_app_session)->channels,
1701 &_position.channel_iterator);
1702 break;
1703 }
1704}
1705
1706void ls::user_space_consumer_channel_keys::iterator::_init_per_pid() noexcept
1707{
1708 auto& position = _position._per_pid;
1709
1710 lttng_ht_get_first(_creation_context._container.apps, &position.app_iterator);
1711 _skip_to_next_app_per_pid(true);
1712}
1713
1714void ls::user_space_consumer_channel_keys::iterator::_init_per_uid() noexcept
1715{
1716 auto& position = _position._per_uid;
1717
1718 /* Start the iteration: get the first registry and point to its first channel. */
1719 if (is_list_empty(&_creation_context._session.buffer_reg_uid_list)) {
1720 _is_end = true;
1721 return;
1722 }
1723
1724 position.current_registry = lttng::utils::container_of(
1725 _creation_context._session.buffer_reg_uid_list.next, &buffer_reg_uid::lnode);
1726 lttng_ht_get_first(position.current_registry->registry->channels,
1727 &_position.channel_iterator);
1728}
1729
1730void ls::user_space_consumer_channel_keys::iterator::_advance_one_per_pid()
1731{
1732 auto& position = _position._per_pid;
1733
1734 if (!cds_lfht_iter_get_node(&_position.channel_iterator.iter)) {
1735 /* Reached the last channel. Move on to the next app. */
1736 _skip_to_next_app_per_pid(false);
1737 return;
1738 }
1739
1740 const auto current_app_node =
1741 lttng_ht_iter_get_node<lttng_ht_node_ulong>(&position.app_iterator);
1742 LTTNG_ASSERT(current_app_node);
1743
1744 lttng_ht_get_next((*position.current_app_session)->channels, &_position.channel_iterator);
1745}
1746
1747void ls::user_space_consumer_channel_keys::iterator::_advance_one_per_uid()
1748{
1749 auto& position = _position._per_uid;
1750
1751 if (!cds_lfht_iter_get_node(&_position.channel_iterator.iter)) {
1752 /* Reached the last channel of the registry. Move on to the next registry. */
1753 if (is_last_element_of_list(&position.current_registry->lnode)) {
1754 _is_end = true;
1755 return;
1756 }
1757
1758 position.current_registry = lttng::utils::container_of(
1759 position.current_registry->lnode.next, &buffer_reg_uid::lnode);
1760 cds_lfht_first(position.current_registry->registry->channels->ht,
1761 &_position.channel_iterator.iter);
1762
1763 /* Assumes a registry can't be empty. */
1764 LTTNG_ASSERT(cds_lfht_iter_get_node(&_position.channel_iterator.iter));
1765 }
1766
1767 cds_lfht_next(position.current_registry->registry->channels->ht,
1768 &_position.channel_iterator.iter);
1769}
1770
1771bool ls::user_space_consumer_channel_keys::iterator::operator==(const iterator& other) const noexcept
1772{
1773 if (_is_end && other._is_end) {
1774 return true;
1775 }
1776
1777 /* Channel keys are unique; use them to compare the iterators. */
1778 return !_is_end && !other._is_end && **this == *other;
1779}
1780
1781bool ls::user_space_consumer_channel_keys::iterator::operator!=(const iterator& other) const noexcept
1782{
1783 return !(*this == other);
1784}
1785
1786ls::user_space_consumer_channel_keys::iterator::key
1787ls::user_space_consumer_channel_keys::iterator::_get_current_value_per_pid() const noexcept
1788{
1789 auto& position = _position._per_pid;
1790
1791 const auto *channel_node =
1792 lttng_ht_iter_get_node<lttng_ht_node_str>(&_position.channel_iterator);
1793 const auto current_app_node =
1794 lttng_ht_iter_get_node<lttng_ht_node_ulong>(&position.app_iterator);
1795 LTTNG_ASSERT(current_app_node);
1796
1797 const auto& app = *lttng::utils::container_of(current_app_node, &ust_app::pid_n);
1798
1799 if (channel_node) {
1800 const auto& channel =
1801 *lttng::utils::container_of(channel_node, &ust_app_channel::node);
1802
1803 return { static_cast<consumer_bitness>(app.abi.bits_per_long),
1804 channel.key,
1805 ls::user_space_consumer_channel_keys::channel_type::DATA };
1806 } else {
1807 LTTNG_ASSERT(position.current_registry_session);
1808
1809 /*
1810 * Once the last data channel is delivered (iter points to the 'end' of the ht),
1811 * deliver the metadata channel's key.
1812 */
1813 return { static_cast<consumer_bitness>(app.abi.bits_per_long),
1814 position.current_registry_session->_metadata_key,
1815 ls::user_space_consumer_channel_keys::channel_type::METADATA };
1816 }
1817}
1818
1819ls::user_space_consumer_channel_keys::iterator::key
1820ls::user_space_consumer_channel_keys::iterator::_get_current_value_per_uid() const noexcept
1821{
1822 const auto *channel_node =
1823 lttng_ht_iter_get_node<lttng_ht_node_u64>(&_position.channel_iterator);
1824
1825 if (channel_node) {
1826 const auto& channel =
1827 *lttng::utils::container_of(channel_node, &buffer_reg_channel::node);
1828
1829 return { static_cast<consumer_bitness>(
1830 _position._per_uid.current_registry->bits_per_long),
1831 channel.consumer_key,
1832 ls::user_space_consumer_channel_keys::channel_type::DATA };
1833 } else {
1834 /*
1835 * Once the last data channel is delivered (iter points to the 'end' of the ht),
1836 * deliver the metadata channel's key.
1837 */
1838 return { static_cast<consumer_bitness>(
1839 _position._per_uid.current_registry->bits_per_long),
1840 _position._per_uid.current_registry->registry->reg.ust->_metadata_key,
1841 ls::user_space_consumer_channel_keys::channel_type::METADATA };
1842 }
1843}
1844
1845ls::user_space_consumer_channel_keys::iterator::key
1846ls::user_space_consumer_channel_keys::iterator::operator*() const
1847{
1848 if (_is_end) {
1849 LTTNG_THROW_OUT_OF_RANGE(
1850 "Attempt to use operator* on user_space_consumer_channel_keys iterator at the end position");
1851 }
1852
1853 switch (_creation_context._mode) {
1854 case _iteration_mode::PER_PID:
1855 return _get_current_value_per_pid();
1856 case _iteration_mode::PER_UID:
1857 return _get_current_value_per_uid();
1858 }
1859
1860 std::abort();
1861}
1862
1863ls::ust::registry_session *ls::user_space_consumer_channel_keys::iterator::get_registry_session()
1864{
1865 if (_is_end) {
1866 LTTNG_THROW_OUT_OF_RANGE(
1867 "Attempt to get registry session on user_space_consumer_channel_keys iterator at the end position");
1868 }
1869
1870 switch (_creation_context._mode) {
1871 case _iteration_mode::PER_PID:
1872 return _get_registry_session_per_pid();
1873 case _iteration_mode::PER_UID:
1874 return _get_registry_session_per_uid();
1875 }
1876
1877 std::abort();
1878}
1879
1880ls::ust::registry_session *
1881ls::user_space_consumer_channel_keys::iterator::_get_registry_session_per_pid()
1882{
1883 return _position._per_pid.current_registry_session;
1884}
1885
1886ls::ust::registry_session *
1887ls::user_space_consumer_channel_keys::iterator::_get_registry_session_per_uid()
1888{
1889 return _position._per_uid.current_registry->registry->reg.ust;
1890}
This page took 0.202568 seconds and 5 git commands to generate.